2 #include <linux/igmp.h>
3 #include <linux/kernel.h>
4 #include <linux/netdevice.h>
5 #include <linux/rculist.h>
6 #include <linux/skbuff.h>
7 #include <linux/if_ether.h>
9 #include <net/netlink.h>
10 #if IS_ENABLED(CONFIG_IPV6)
14 #include "br_private.h"
16 static int br_rports_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
17 struct net_device
*dev
)
19 struct net_bridge
*br
= netdev_priv(dev
);
20 struct net_bridge_port
*p
;
23 if (!br
->multicast_router
|| hlist_empty(&br
->router_list
))
26 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
30 hlist_for_each_entry_rcu(p
, &br
->router_list
, rlist
) {
31 if (p
&& nla_put_u32(skb
, MDBA_ROUTER_PORT
, p
->dev
->ifindex
))
35 nla_nest_end(skb
, nest
);
38 nla_nest_cancel(skb
, nest
);
42 static int br_mdb_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
43 struct net_device
*dev
)
45 struct net_bridge
*br
= netdev_priv(dev
);
46 struct net_bridge_mdb_htable
*mdb
;
47 struct nlattr
*nest
, *nest2
;
49 int idx
= 0, s_idx
= cb
->args
[1];
51 if (br
->multicast_disabled
)
54 mdb
= rcu_dereference(br
->mdb
);
58 nest
= nla_nest_start(skb
, MDBA_MDB
);
62 for (i
= 0; i
< mdb
->max
; i
++) {
63 struct net_bridge_mdb_entry
*mp
;
64 struct net_bridge_port_group
*p
, **pp
;
65 struct net_bridge_port
*port
;
67 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[i
], hlist
[mdb
->ver
]) {
71 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
78 (p
= rcu_dereference(*pp
)) != NULL
;
82 struct br_mdb_entry e
;
83 memset(&e
, 0, sizeof(e
));
84 e
.ifindex
= port
->dev
->ifindex
;
86 if (p
->addr
.proto
== htons(ETH_P_IP
))
87 e
.addr
.u
.ip4
= p
->addr
.u
.ip4
;
88 #if IS_ENABLED(CONFIG_IPV6)
89 if (p
->addr
.proto
== htons(ETH_P_IPV6
))
90 e
.addr
.u
.ip6
= p
->addr
.u
.ip6
;
92 e
.addr
.proto
= p
->addr
.proto
;
93 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(e
), &e
)) {
94 nla_nest_cancel(skb
, nest2
);
100 nla_nest_end(skb
, nest2
);
108 nla_nest_end(skb
, nest
);
112 static int br_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
114 struct net_device
*dev
;
115 struct net
*net
= sock_net(skb
->sk
);
116 struct nlmsghdr
*nlh
= NULL
;
123 /* In theory this could be wrapped to 0... */
124 cb
->seq
= net
->dev_base_seq
+ br_mdb_rehash_seq
;
126 for_each_netdev_rcu(net
, dev
) {
127 if (dev
->priv_flags
& IFF_EBRIDGE
) {
128 struct br_port_msg
*bpm
;
133 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
134 cb
->nlh
->nlmsg_seq
, RTM_GETMDB
,
135 sizeof(*bpm
), NLM_F_MULTI
);
139 bpm
= nlmsg_data(nlh
);
140 memset(bpm
, 0, sizeof(*bpm
));
141 bpm
->ifindex
= dev
->ifindex
;
142 if (br_mdb_fill_info(skb
, cb
, dev
) < 0)
144 if (br_rports_fill_info(skb
, cb
, dev
) < 0)
162 static int nlmsg_populate_mdb_fill(struct sk_buff
*skb
,
163 struct net_device
*dev
,
164 struct br_mdb_entry
*entry
, u32 pid
,
165 u32 seq
, int type
, unsigned int flags
)
167 struct nlmsghdr
*nlh
;
168 struct br_port_msg
*bpm
;
169 struct nlattr
*nest
, *nest2
;
171 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), NLM_F_MULTI
);
175 bpm
= nlmsg_data(nlh
);
176 memset(bpm
, 0, sizeof(*bpm
));
177 bpm
->family
= AF_BRIDGE
;
178 bpm
->ifindex
= dev
->ifindex
;
179 nest
= nla_nest_start(skb
, MDBA_MDB
);
182 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
186 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(*entry
), entry
))
189 nla_nest_end(skb
, nest2
);
190 nla_nest_end(skb
, nest
);
191 return nlmsg_end(skb
, nlh
);
194 nla_nest_end(skb
, nest
);
196 nlmsg_cancel(skb
, nlh
);
200 static inline size_t rtnl_mdb_nlmsg_size(void)
202 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
203 + nla_total_size(sizeof(struct br_mdb_entry
));
206 static void __br_mdb_notify(struct net_device
*dev
, struct br_mdb_entry
*entry
,
209 struct net
*net
= dev_net(dev
);
213 skb
= nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC
);
217 err
= nlmsg_populate_mdb_fill(skb
, dev
, entry
, 0, 0, type
, NTF_SELF
);
223 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
226 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
229 void br_mdb_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
230 struct br_ip
*group
, int type
)
232 struct br_mdb_entry entry
;
234 memset(&entry
, 0, sizeof(entry
));
235 entry
.ifindex
= port
->dev
->ifindex
;
236 entry
.addr
.proto
= group
->proto
;
237 entry
.addr
.u
.ip4
= group
->u
.ip4
;
238 #if IS_ENABLED(CONFIG_IPV6)
239 entry
.addr
.u
.ip6
= group
->u
.ip6
;
241 __br_mdb_notify(dev
, &entry
, type
);
244 static bool is_valid_mdb_entry(struct br_mdb_entry
*entry
)
246 if (entry
->ifindex
== 0)
249 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
250 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
))
252 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
))
254 #if IS_ENABLED(CONFIG_IPV6)
255 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
256 if (!ipv6_is_transient_multicast(&entry
->addr
.u
.ip6
))
261 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
)
267 static int br_mdb_parse(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
268 struct net_device
**pdev
, struct br_mdb_entry
**pentry
)
270 struct net
*net
= sock_net(skb
->sk
);
271 struct br_mdb_entry
*entry
;
272 struct br_port_msg
*bpm
;
273 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+1];
274 struct net_device
*dev
;
277 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY
, NULL
);
281 bpm
= nlmsg_data(nlh
);
282 if (bpm
->ifindex
== 0) {
283 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
287 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
289 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
293 if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
294 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
300 if (!tb
[MDBA_SET_ENTRY
] ||
301 nla_len(tb
[MDBA_SET_ENTRY
]) != sizeof(struct br_mdb_entry
)) {
302 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
306 entry
= nla_data(tb
[MDBA_SET_ENTRY
]);
307 if (!is_valid_mdb_entry(entry
)) {
308 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
316 static int br_mdb_add_group(struct net_bridge
*br
, struct net_bridge_port
*port
,
317 struct br_ip
*group
, unsigned char state
)
319 struct net_bridge_mdb_entry
*mp
;
320 struct net_bridge_port_group
*p
;
321 struct net_bridge_port_group __rcu
**pp
;
322 struct net_bridge_mdb_htable
*mdb
;
325 mdb
= mlock_dereference(br
->mdb
, br
);
326 mp
= br_mdb_ip_get(mdb
, group
);
328 mp
= br_multicast_new_group(br
, port
, group
);
334 for (pp
= &mp
->ports
;
335 (p
= mlock_dereference(*pp
, br
)) != NULL
;
339 if ((unsigned long)p
->port
< (unsigned long)port
)
343 p
= br_multicast_new_port_group(port
, group
, *pp
, state
);
346 rcu_assign_pointer(*pp
, p
);
348 br_mdb_notify(br
->dev
, port
, group
, RTM_NEWMDB
);
352 static int __br_mdb_add(struct net
*net
, struct net_bridge
*br
,
353 struct br_mdb_entry
*entry
)
356 struct net_device
*dev
;
357 struct net_bridge_port
*p
;
360 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
363 dev
= __dev_get_by_index(net
, entry
->ifindex
);
367 p
= br_port_get_rtnl(dev
);
368 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
371 ip
.proto
= entry
->addr
.proto
;
372 if (ip
.proto
== htons(ETH_P_IP
))
373 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
374 #if IS_ENABLED(CONFIG_IPV6)
376 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
379 spin_lock_bh(&br
->multicast_lock
);
380 ret
= br_mdb_add_group(br
, p
, &ip
, entry
->state
);
381 spin_unlock_bh(&br
->multicast_lock
);
385 static int br_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
387 struct net
*net
= sock_net(skb
->sk
);
388 struct br_mdb_entry
*entry
;
389 struct net_device
*dev
;
390 struct net_bridge
*br
;
393 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
397 br
= netdev_priv(dev
);
399 err
= __br_mdb_add(net
, br
, entry
);
401 __br_mdb_notify(dev
, entry
, RTM_NEWMDB
);
405 static int __br_mdb_del(struct net_bridge
*br
, struct br_mdb_entry
*entry
)
407 struct net_bridge_mdb_htable
*mdb
;
408 struct net_bridge_mdb_entry
*mp
;
409 struct net_bridge_port_group
*p
;
410 struct net_bridge_port_group __rcu
**pp
;
414 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
417 if (timer_pending(&br
->multicast_querier_timer
))
420 ip
.proto
= entry
->addr
.proto
;
421 if (ip
.proto
== htons(ETH_P_IP
))
422 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
423 #if IS_ENABLED(CONFIG_IPV6)
425 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
428 spin_lock_bh(&br
->multicast_lock
);
429 mdb
= mlock_dereference(br
->mdb
, br
);
431 mp
= br_mdb_ip_get(mdb
, &ip
);
435 for (pp
= &mp
->ports
;
436 (p
= mlock_dereference(*pp
, br
)) != NULL
;
438 if (!p
->port
|| p
->port
->dev
->ifindex
!= entry
->ifindex
)
441 if (p
->port
->state
== BR_STATE_DISABLED
)
444 rcu_assign_pointer(*pp
, p
->next
);
445 hlist_del_init(&p
->mglist
);
446 del_timer(&p
->timer
);
447 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
450 if (!mp
->ports
&& !mp
->mglist
&& mp
->timer_armed
&&
451 netif_running(br
->dev
))
452 mod_timer(&mp
->timer
, jiffies
);
457 spin_unlock_bh(&br
->multicast_lock
);
461 static int br_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
463 struct net_device
*dev
;
464 struct br_mdb_entry
*entry
;
465 struct net_bridge
*br
;
468 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
472 br
= netdev_priv(dev
);
474 err
= __br_mdb_del(br
, entry
);
476 __br_mdb_notify(dev
, entry
, RTM_DELMDB
);
480 void br_mdb_init(void)
482 rtnl_register(PF_BRIDGE
, RTM_GETMDB
, NULL
, br_mdb_dump
, NULL
);
483 rtnl_register(PF_BRIDGE
, RTM_NEWMDB
, br_mdb_add
, NULL
, NULL
);
484 rtnl_register(PF_BRIDGE
, RTM_DELMDB
, br_mdb_del
, NULL
, NULL
);
487 void br_mdb_uninit(void)
489 rtnl_unregister(PF_BRIDGE
, RTM_GETMDB
);
490 rtnl_unregister(PF_BRIDGE
, RTM_NEWMDB
);
491 rtnl_unregister(PF_BRIDGE
, RTM_DELMDB
);