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 #include <net/switchdev.h>
11 #if IS_ENABLED(CONFIG_IPV6)
13 #include <net/addrconf.h>
16 #include "br_private.h"
18 static int br_rports_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
19 struct net_device
*dev
)
21 struct net_bridge
*br
= netdev_priv(dev
);
22 struct net_bridge_port
*p
;
23 struct nlattr
*nest
, *port_nest
;
25 if (!br
->multicast_router
|| hlist_empty(&br
->router_list
))
28 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
32 hlist_for_each_entry_rcu(p
, &br
->router_list
, rlist
) {
35 port_nest
= nla_nest_start(skb
, MDBA_ROUTER_PORT
);
38 if (nla_put_nohdr(skb
, sizeof(u32
), &p
->dev
->ifindex
) ||
39 nla_put_u32(skb
, MDBA_ROUTER_PATTR_TIMER
,
40 br_timer_value(&p
->multicast_router_timer
)) ||
41 nla_put_u8(skb
, MDBA_ROUTER_PATTR_TYPE
,
42 p
->multicast_router
)) {
43 nla_nest_cancel(skb
, port_nest
);
46 nla_nest_end(skb
, port_nest
);
49 nla_nest_end(skb
, nest
);
52 nla_nest_cancel(skb
, nest
);
56 static void __mdb_entry_fill_flags(struct br_mdb_entry
*e
, unsigned char flags
)
58 e
->state
= flags
& MDB_PG_FLAGS_PERMANENT
;
60 if (flags
& MDB_PG_FLAGS_OFFLOAD
)
61 e
->flags
|= MDB_FLAGS_OFFLOAD
;
64 static int br_mdb_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
65 struct net_device
*dev
)
67 struct net_bridge
*br
= netdev_priv(dev
);
68 struct net_bridge_mdb_htable
*mdb
;
69 struct nlattr
*nest
, *nest2
;
71 int idx
= 0, s_idx
= cb
->args
[1];
73 if (br
->multicast_disabled
)
76 mdb
= rcu_dereference(br
->mdb
);
80 nest
= nla_nest_start(skb
, MDBA_MDB
);
84 for (i
= 0; i
< mdb
->max
; i
++) {
85 struct net_bridge_mdb_entry
*mp
;
86 struct net_bridge_port_group
*p
;
87 struct net_bridge_port_group __rcu
**pp
;
88 struct net_bridge_port
*port
;
90 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[i
], hlist
[mdb
->ver
]) {
94 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
100 for (pp
= &mp
->ports
;
101 (p
= rcu_dereference(*pp
)) != NULL
;
103 struct nlattr
*nest_ent
;
104 struct br_mdb_entry e
;
110 memset(&e
, 0, sizeof(e
));
111 e
.ifindex
= port
->dev
->ifindex
;
113 __mdb_entry_fill_flags(&e
, p
->flags
);
114 if (p
->addr
.proto
== htons(ETH_P_IP
))
115 e
.addr
.u
.ip4
= p
->addr
.u
.ip4
;
116 #if IS_ENABLED(CONFIG_IPV6)
117 if (p
->addr
.proto
== htons(ETH_P_IPV6
))
118 e
.addr
.u
.ip6
= p
->addr
.u
.ip6
;
120 e
.addr
.proto
= p
->addr
.proto
;
121 nest_ent
= nla_nest_start(skb
,
122 MDBA_MDB_ENTRY_INFO
);
124 nla_nest_cancel(skb
, nest2
);
128 if (nla_put_nohdr(skb
, sizeof(e
), &e
) ||
130 MDBA_MDB_EATTR_TIMER
,
131 br_timer_value(&p
->timer
))) {
132 nla_nest_cancel(skb
, nest_ent
);
133 nla_nest_cancel(skb
, nest2
);
137 nla_nest_end(skb
, nest_ent
);
139 nla_nest_end(skb
, nest2
);
147 nla_nest_end(skb
, nest
);
151 static int br_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
153 struct net_device
*dev
;
154 struct net
*net
= sock_net(skb
->sk
);
155 struct nlmsghdr
*nlh
= NULL
;
162 /* In theory this could be wrapped to 0... */
163 cb
->seq
= net
->dev_base_seq
+ br_mdb_rehash_seq
;
165 for_each_netdev_rcu(net
, dev
) {
166 if (dev
->priv_flags
& IFF_EBRIDGE
) {
167 struct br_port_msg
*bpm
;
172 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
173 cb
->nlh
->nlmsg_seq
, RTM_GETMDB
,
174 sizeof(*bpm
), NLM_F_MULTI
);
178 bpm
= nlmsg_data(nlh
);
179 memset(bpm
, 0, sizeof(*bpm
));
180 bpm
->ifindex
= dev
->ifindex
;
181 if (br_mdb_fill_info(skb
, cb
, dev
) < 0)
183 if (br_rports_fill_info(skb
, cb
, dev
) < 0)
201 static int nlmsg_populate_mdb_fill(struct sk_buff
*skb
,
202 struct net_device
*dev
,
203 struct br_mdb_entry
*entry
, u32 pid
,
204 u32 seq
, int type
, unsigned int flags
)
206 struct nlmsghdr
*nlh
;
207 struct br_port_msg
*bpm
;
208 struct nlattr
*nest
, *nest2
;
210 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), 0);
214 bpm
= nlmsg_data(nlh
);
215 memset(bpm
, 0, sizeof(*bpm
));
216 bpm
->family
= AF_BRIDGE
;
217 bpm
->ifindex
= dev
->ifindex
;
218 nest
= nla_nest_start(skb
, MDBA_MDB
);
221 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
225 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(*entry
), entry
))
228 nla_nest_end(skb
, nest2
);
229 nla_nest_end(skb
, nest
);
234 nla_nest_end(skb
, nest
);
236 nlmsg_cancel(skb
, nlh
);
240 static inline size_t rtnl_mdb_nlmsg_size(void)
242 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
243 + nla_total_size(sizeof(struct br_mdb_entry
));
246 static void __br_mdb_notify(struct net_device
*dev
, struct br_mdb_entry
*entry
,
247 int type
, struct net_bridge_port_group
*pg
)
249 struct switchdev_obj_port_mdb mdb
= {
251 .id
= SWITCHDEV_OBJ_ID_PORT_MDB
,
252 .flags
= SWITCHDEV_F_DEFER
,
256 struct net_device
*port_dev
;
257 struct net
*net
= dev_net(dev
);
261 port_dev
= __dev_get_by_index(net
, entry
->ifindex
);
262 if (entry
->addr
.proto
== htons(ETH_P_IP
))
263 ip_eth_mc_map(entry
->addr
.u
.ip4
, mdb
.addr
);
264 #if IS_ENABLED(CONFIG_IPV6)
266 ipv6_eth_mc_map(&entry
->addr
.u
.ip6
, mdb
.addr
);
269 mdb
.obj
.orig_dev
= port_dev
;
270 if (port_dev
&& type
== RTM_NEWMDB
) {
271 err
= switchdev_port_obj_add(port_dev
, &mdb
.obj
);
273 pg
->flags
|= MDB_PG_FLAGS_OFFLOAD
;
274 } else if (port_dev
&& type
== RTM_DELMDB
) {
275 switchdev_port_obj_del(port_dev
, &mdb
.obj
);
278 skb
= nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC
);
282 err
= nlmsg_populate_mdb_fill(skb
, dev
, entry
, 0, 0, type
, NTF_SELF
);
288 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
291 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
294 void br_mdb_notify(struct net_device
*dev
, struct net_bridge_port_group
*pg
,
297 struct br_mdb_entry entry
;
299 memset(&entry
, 0, sizeof(entry
));
300 entry
.ifindex
= pg
->port
->dev
->ifindex
;
301 entry
.addr
.proto
= pg
->addr
.proto
;
302 entry
.addr
.u
.ip4
= pg
->addr
.u
.ip4
;
303 #if IS_ENABLED(CONFIG_IPV6)
304 entry
.addr
.u
.ip6
= pg
->addr
.u
.ip6
;
306 entry
.vid
= pg
->addr
.vid
;
307 __mdb_entry_fill_flags(&entry
, pg
->flags
);
308 __br_mdb_notify(dev
, &entry
, type
, pg
);
311 static int nlmsg_populate_rtr_fill(struct sk_buff
*skb
,
312 struct net_device
*dev
,
313 int ifindex
, u32 pid
,
314 u32 seq
, int type
, unsigned int flags
)
316 struct br_port_msg
*bpm
;
317 struct nlmsghdr
*nlh
;
320 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), NLM_F_MULTI
);
324 bpm
= nlmsg_data(nlh
);
325 memset(bpm
, 0, sizeof(*bpm
));
326 bpm
->family
= AF_BRIDGE
;
327 bpm
->ifindex
= dev
->ifindex
;
328 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
332 if (nla_put_u32(skb
, MDBA_ROUTER_PORT
, ifindex
))
335 nla_nest_end(skb
, nest
);
340 nla_nest_end(skb
, nest
);
342 nlmsg_cancel(skb
, nlh
);
346 static inline size_t rtnl_rtr_nlmsg_size(void)
348 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
349 + nla_total_size(sizeof(__u32
));
352 void br_rtr_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
355 struct net
*net
= dev_net(dev
);
360 ifindex
= port
? port
->dev
->ifindex
: 0;
361 skb
= nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC
);
365 err
= nlmsg_populate_rtr_fill(skb
, dev
, ifindex
, 0, 0, type
, NTF_SELF
);
371 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
375 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
378 static bool is_valid_mdb_entry(struct br_mdb_entry
*entry
)
380 if (entry
->ifindex
== 0)
383 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
384 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
))
386 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
))
388 #if IS_ENABLED(CONFIG_IPV6)
389 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
390 if (ipv6_addr_is_ll_all_nodes(&entry
->addr
.u
.ip6
))
395 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
)
397 if (entry
->vid
>= VLAN_VID_MASK
)
403 static int br_mdb_parse(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
404 struct net_device
**pdev
, struct br_mdb_entry
**pentry
)
406 struct net
*net
= sock_net(skb
->sk
);
407 struct br_mdb_entry
*entry
;
408 struct br_port_msg
*bpm
;
409 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+1];
410 struct net_device
*dev
;
413 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY_MAX
, NULL
);
417 bpm
= nlmsg_data(nlh
);
418 if (bpm
->ifindex
== 0) {
419 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
423 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
425 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
429 if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
430 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
436 if (!tb
[MDBA_SET_ENTRY
] ||
437 nla_len(tb
[MDBA_SET_ENTRY
]) != sizeof(struct br_mdb_entry
)) {
438 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
442 entry
= nla_data(tb
[MDBA_SET_ENTRY
]);
443 if (!is_valid_mdb_entry(entry
)) {
444 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
452 static int br_mdb_add_group(struct net_bridge
*br
, struct net_bridge_port
*port
,
453 struct br_ip
*group
, unsigned char state
,
454 struct net_bridge_port_group
**pg
)
456 struct net_bridge_mdb_entry
*mp
;
457 struct net_bridge_port_group
*p
;
458 struct net_bridge_port_group __rcu
**pp
;
459 struct net_bridge_mdb_htable
*mdb
;
460 unsigned long now
= jiffies
;
463 mdb
= mlock_dereference(br
->mdb
, br
);
464 mp
= br_mdb_ip_get(mdb
, group
);
466 mp
= br_multicast_new_group(br
, port
, group
);
467 err
= PTR_ERR_OR_ZERO(mp
);
472 for (pp
= &mp
->ports
;
473 (p
= mlock_dereference(*pp
, br
)) != NULL
;
477 if ((unsigned long)p
->port
< (unsigned long)port
)
481 p
= br_multicast_new_port_group(port
, group
, *pp
, state
);
484 rcu_assign_pointer(*pp
, p
);
486 if (state
== MDB_TEMPORARY
)
487 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
492 static int __br_mdb_add(struct net
*net
, struct net_bridge
*br
,
493 struct br_mdb_entry
*entry
,
494 struct net_bridge_port_group
**pg
)
497 struct net_device
*dev
;
498 struct net_bridge_port
*p
;
501 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
504 dev
= __dev_get_by_index(net
, entry
->ifindex
);
508 p
= br_port_get_rtnl(dev
);
509 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
512 memset(&ip
, 0, sizeof(ip
));
514 ip
.proto
= entry
->addr
.proto
;
515 if (ip
.proto
== htons(ETH_P_IP
))
516 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
517 #if IS_ENABLED(CONFIG_IPV6)
519 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
522 spin_lock_bh(&br
->multicast_lock
);
523 ret
= br_mdb_add_group(br
, p
, &ip
, entry
->state
, pg
);
524 spin_unlock_bh(&br
->multicast_lock
);
528 static int br_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
530 struct net
*net
= sock_net(skb
->sk
);
531 struct net_bridge_port_group
*pg
;
532 struct net_bridge_vlan_group
*vg
;
533 struct net_device
*dev
, *pdev
;
534 struct br_mdb_entry
*entry
;
535 struct net_bridge_port
*p
;
536 struct net_bridge_vlan
*v
;
537 struct net_bridge
*br
;
540 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
544 br
= netdev_priv(dev
);
546 /* If vlan filtering is enabled and VLAN is not specified
547 * install mdb entry on all vlans configured on the port.
549 pdev
= __dev_get_by_index(net
, entry
->ifindex
);
553 p
= br_port_get_rtnl(pdev
);
554 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
557 vg
= nbp_vlan_group(p
);
558 if (br_vlan_enabled(br
) && vg
&& entry
->vid
== 0) {
559 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
561 err
= __br_mdb_add(net
, br
, entry
, &pg
);
564 __br_mdb_notify(dev
, entry
, RTM_NEWMDB
, pg
);
567 err
= __br_mdb_add(net
, br
, entry
, &pg
);
569 __br_mdb_notify(dev
, entry
, RTM_NEWMDB
, pg
);
575 static int __br_mdb_del(struct net_bridge
*br
, struct br_mdb_entry
*entry
)
577 struct net_bridge_mdb_htable
*mdb
;
578 struct net_bridge_mdb_entry
*mp
;
579 struct net_bridge_port_group
*p
;
580 struct net_bridge_port_group __rcu
**pp
;
584 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
587 memset(&ip
, 0, sizeof(ip
));
589 ip
.proto
= entry
->addr
.proto
;
590 if (ip
.proto
== htons(ETH_P_IP
))
591 ip
.u
.ip4
= entry
->addr
.u
.ip4
;
592 #if IS_ENABLED(CONFIG_IPV6)
594 ip
.u
.ip6
= entry
->addr
.u
.ip6
;
597 spin_lock_bh(&br
->multicast_lock
);
598 mdb
= mlock_dereference(br
->mdb
, br
);
600 mp
= br_mdb_ip_get(mdb
, &ip
);
604 for (pp
= &mp
->ports
;
605 (p
= mlock_dereference(*pp
, br
)) != NULL
;
607 if (!p
->port
|| p
->port
->dev
->ifindex
!= entry
->ifindex
)
610 if (p
->port
->state
== BR_STATE_DISABLED
)
613 __mdb_entry_fill_flags(entry
, p
->flags
);
614 rcu_assign_pointer(*pp
, p
->next
);
615 hlist_del_init(&p
->mglist
);
616 del_timer(&p
->timer
);
617 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
620 if (!mp
->ports
&& !mp
->mglist
&&
621 netif_running(br
->dev
))
622 mod_timer(&mp
->timer
, jiffies
);
627 spin_unlock_bh(&br
->multicast_lock
);
631 static int br_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
633 struct net
*net
= sock_net(skb
->sk
);
634 struct net_bridge_vlan_group
*vg
;
635 struct net_device
*dev
, *pdev
;
636 struct br_mdb_entry
*entry
;
637 struct net_bridge_port
*p
;
638 struct net_bridge_vlan
*v
;
639 struct net_bridge
*br
;
642 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
646 br
= netdev_priv(dev
);
648 /* If vlan filtering is enabled and VLAN is not specified
649 * delete mdb entry on all vlans configured on the port.
651 pdev
= __dev_get_by_index(net
, entry
->ifindex
);
655 p
= br_port_get_rtnl(pdev
);
656 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
659 vg
= nbp_vlan_group(p
);
660 if (br_vlan_enabled(br
) && vg
&& entry
->vid
== 0) {
661 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
663 err
= __br_mdb_del(br
, entry
);
665 __br_mdb_notify(dev
, entry
, RTM_DELMDB
, NULL
);
668 err
= __br_mdb_del(br
, entry
);
670 __br_mdb_notify(dev
, entry
, RTM_DELMDB
, NULL
);
676 void br_mdb_init(void)
678 rtnl_register(PF_BRIDGE
, RTM_GETMDB
, NULL
, br_mdb_dump
, NULL
);
679 rtnl_register(PF_BRIDGE
, RTM_NEWMDB
, br_mdb_add
, NULL
, NULL
);
680 rtnl_register(PF_BRIDGE
, RTM_DELMDB
, br_mdb_del
, NULL
, NULL
);
683 void br_mdb_uninit(void)
685 rtnl_unregister(PF_BRIDGE
, RTM_GETMDB
);
686 rtnl_unregister(PF_BRIDGE
, RTM_NEWMDB
);
687 rtnl_unregister(PF_BRIDGE
, RTM_DELMDB
);