1 // SPDX-License-Identifier: GPL-2.0
3 #include <linux/igmp.h>
4 #include <linux/kernel.h>
5 #include <linux/netdevice.h>
6 #include <linux/rculist.h>
7 #include <linux/skbuff.h>
8 #include <linux/if_ether.h>
10 #include <net/netlink.h>
11 #include <net/switchdev.h>
12 #if IS_ENABLED(CONFIG_IPV6)
14 #include <net/addrconf.h>
17 #include "br_private.h"
19 static int br_rports_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
20 struct net_device
*dev
)
22 struct net_bridge
*br
= netdev_priv(dev
);
23 struct net_bridge_port
*p
;
24 struct nlattr
*nest
, *port_nest
;
26 if (!br
->multicast_router
|| hlist_empty(&br
->router_list
))
29 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
33 hlist_for_each_entry_rcu(p
, &br
->router_list
, rlist
) {
36 port_nest
= nla_nest_start(skb
, MDBA_ROUTER_PORT
);
39 if (nla_put_nohdr(skb
, sizeof(u32
), &p
->dev
->ifindex
) ||
40 nla_put_u32(skb
, MDBA_ROUTER_PATTR_TIMER
,
41 br_timer_value(&p
->multicast_router_timer
)) ||
42 nla_put_u8(skb
, MDBA_ROUTER_PATTR_TYPE
,
43 p
->multicast_router
)) {
44 nla_nest_cancel(skb
, port_nest
);
47 nla_nest_end(skb
, port_nest
);
50 nla_nest_end(skb
, nest
);
53 nla_nest_cancel(skb
, nest
);
57 static void __mdb_entry_fill_flags(struct br_mdb_entry
*e
, unsigned char flags
)
59 e
->state
= flags
& MDB_PG_FLAGS_PERMANENT
;
61 if (flags
& MDB_PG_FLAGS_OFFLOAD
)
62 e
->flags
|= MDB_FLAGS_OFFLOAD
;
65 static void __mdb_entry_to_br_ip(struct br_mdb_entry
*entry
, struct br_ip
*ip
)
67 memset(ip
, 0, sizeof(struct br_ip
));
69 ip
->proto
= entry
->addr
.proto
;
70 if (ip
->proto
== htons(ETH_P_IP
))
71 ip
->u
.ip4
= entry
->addr
.u
.ip4
;
72 #if IS_ENABLED(CONFIG_IPV6)
74 ip
->u
.ip6
= entry
->addr
.u
.ip6
;
78 static int br_mdb_fill_info(struct sk_buff
*skb
, struct netlink_callback
*cb
,
79 struct net_device
*dev
)
81 struct net_bridge
*br
= netdev_priv(dev
);
82 struct net_bridge_mdb_htable
*mdb
;
83 struct nlattr
*nest
, *nest2
;
85 int idx
= 0, s_idx
= cb
->args
[1];
87 if (br
->multicast_disabled
)
90 mdb
= rcu_dereference(br
->mdb
);
94 nest
= nla_nest_start(skb
, MDBA_MDB
);
98 for (i
= 0; i
< mdb
->max
; i
++) {
99 struct net_bridge_mdb_entry
*mp
;
100 struct net_bridge_port_group
*p
;
101 struct net_bridge_port_group __rcu
**pp
;
102 struct net_bridge_port
*port
;
104 hlist_for_each_entry_rcu(mp
, &mdb
->mhash
[i
], hlist
[mdb
->ver
]) {
108 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
114 for (pp
= &mp
->ports
;
115 (p
= rcu_dereference(*pp
)) != NULL
;
117 struct nlattr
*nest_ent
;
118 struct br_mdb_entry e
;
124 memset(&e
, 0, sizeof(e
));
125 e
.ifindex
= port
->dev
->ifindex
;
127 __mdb_entry_fill_flags(&e
, p
->flags
);
128 if (p
->addr
.proto
== htons(ETH_P_IP
))
129 e
.addr
.u
.ip4
= p
->addr
.u
.ip4
;
130 #if IS_ENABLED(CONFIG_IPV6)
131 if (p
->addr
.proto
== htons(ETH_P_IPV6
))
132 e
.addr
.u
.ip6
= p
->addr
.u
.ip6
;
134 e
.addr
.proto
= p
->addr
.proto
;
135 nest_ent
= nla_nest_start(skb
,
136 MDBA_MDB_ENTRY_INFO
);
138 nla_nest_cancel(skb
, nest2
);
142 if (nla_put_nohdr(skb
, sizeof(e
), &e
) ||
144 MDBA_MDB_EATTR_TIMER
,
145 br_timer_value(&p
->timer
))) {
146 nla_nest_cancel(skb
, nest_ent
);
147 nla_nest_cancel(skb
, nest2
);
151 nla_nest_end(skb
, nest_ent
);
153 nla_nest_end(skb
, nest2
);
161 nla_nest_end(skb
, nest
);
165 static int br_mdb_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
167 struct net_device
*dev
;
168 struct net
*net
= sock_net(skb
->sk
);
169 struct nlmsghdr
*nlh
= NULL
;
176 /* In theory this could be wrapped to 0... */
177 cb
->seq
= net
->dev_base_seq
+ br_mdb_rehash_seq
;
179 for_each_netdev_rcu(net
, dev
) {
180 if (dev
->priv_flags
& IFF_EBRIDGE
) {
181 struct br_port_msg
*bpm
;
186 nlh
= nlmsg_put(skb
, NETLINK_CB(cb
->skb
).portid
,
187 cb
->nlh
->nlmsg_seq
, RTM_GETMDB
,
188 sizeof(*bpm
), NLM_F_MULTI
);
192 bpm
= nlmsg_data(nlh
);
193 memset(bpm
, 0, sizeof(*bpm
));
194 bpm
->ifindex
= dev
->ifindex
;
195 if (br_mdb_fill_info(skb
, cb
, dev
) < 0)
197 if (br_rports_fill_info(skb
, cb
, dev
) < 0)
215 static int nlmsg_populate_mdb_fill(struct sk_buff
*skb
,
216 struct net_device
*dev
,
217 struct br_mdb_entry
*entry
, u32 pid
,
218 u32 seq
, int type
, unsigned int flags
)
220 struct nlmsghdr
*nlh
;
221 struct br_port_msg
*bpm
;
222 struct nlattr
*nest
, *nest2
;
224 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), 0);
228 bpm
= nlmsg_data(nlh
);
229 memset(bpm
, 0, sizeof(*bpm
));
230 bpm
->family
= AF_BRIDGE
;
231 bpm
->ifindex
= dev
->ifindex
;
232 nest
= nla_nest_start(skb
, MDBA_MDB
);
235 nest2
= nla_nest_start(skb
, MDBA_MDB_ENTRY
);
239 if (nla_put(skb
, MDBA_MDB_ENTRY_INFO
, sizeof(*entry
), entry
))
242 nla_nest_end(skb
, nest2
);
243 nla_nest_end(skb
, nest
);
248 nla_nest_end(skb
, nest
);
250 nlmsg_cancel(skb
, nlh
);
254 static inline size_t rtnl_mdb_nlmsg_size(void)
256 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
257 + nla_total_size(sizeof(struct br_mdb_entry
));
260 struct br_mdb_complete_info
{
261 struct net_bridge_port
*port
;
265 static void br_mdb_complete(struct net_device
*dev
, int err
, void *priv
)
267 struct br_mdb_complete_info
*data
= priv
;
268 struct net_bridge_port_group __rcu
**pp
;
269 struct net_bridge_port_group
*p
;
270 struct net_bridge_mdb_htable
*mdb
;
271 struct net_bridge_mdb_entry
*mp
;
272 struct net_bridge_port
*port
= data
->port
;
273 struct net_bridge
*br
= port
->br
;
278 spin_lock_bh(&br
->multicast_lock
);
279 mdb
= mlock_dereference(br
->mdb
, br
);
280 mp
= br_mdb_ip_get(mdb
, &data
->ip
);
283 for (pp
= &mp
->ports
; (p
= mlock_dereference(*pp
, br
)) != NULL
;
287 p
->flags
|= MDB_PG_FLAGS_OFFLOAD
;
290 spin_unlock_bh(&br
->multicast_lock
);
295 static void br_mdb_switchdev_host_port(struct net_device
*dev
,
296 struct net_device
*lower_dev
,
297 struct br_mdb_entry
*entry
, int type
)
299 struct switchdev_obj_port_mdb mdb
= {
301 .id
= SWITCHDEV_OBJ_ID_HOST_MDB
,
302 .flags
= SWITCHDEV_F_DEFER
,
307 if (entry
->addr
.proto
== htons(ETH_P_IP
))
308 ip_eth_mc_map(entry
->addr
.u
.ip4
, mdb
.addr
);
309 #if IS_ENABLED(CONFIG_IPV6)
311 ipv6_eth_mc_map(&entry
->addr
.u
.ip6
, mdb
.addr
);
314 mdb
.obj
.orig_dev
= dev
;
317 switchdev_port_obj_add(lower_dev
, &mdb
.obj
);
320 switchdev_port_obj_del(lower_dev
, &mdb
.obj
);
325 static void br_mdb_switchdev_host(struct net_device
*dev
,
326 struct br_mdb_entry
*entry
, int type
)
328 struct net_device
*lower_dev
;
329 struct list_head
*iter
;
331 netdev_for_each_lower_dev(dev
, lower_dev
, iter
)
332 br_mdb_switchdev_host_port(dev
, lower_dev
, entry
, type
);
335 static void __br_mdb_notify(struct net_device
*dev
, struct net_bridge_port
*p
,
336 struct br_mdb_entry
*entry
, int type
)
338 struct br_mdb_complete_info
*complete_info
;
339 struct switchdev_obj_port_mdb mdb
= {
341 .id
= SWITCHDEV_OBJ_ID_PORT_MDB
,
342 .flags
= SWITCHDEV_F_DEFER
,
346 struct net_device
*port_dev
;
347 struct net
*net
= dev_net(dev
);
351 port_dev
= __dev_get_by_index(net
, entry
->ifindex
);
352 if (entry
->addr
.proto
== htons(ETH_P_IP
))
353 ip_eth_mc_map(entry
->addr
.u
.ip4
, mdb
.addr
);
354 #if IS_ENABLED(CONFIG_IPV6)
356 ipv6_eth_mc_map(&entry
->addr
.u
.ip6
, mdb
.addr
);
359 mdb
.obj
.orig_dev
= port_dev
;
360 if (p
&& port_dev
&& type
== RTM_NEWMDB
) {
361 complete_info
= kmalloc(sizeof(*complete_info
), GFP_ATOMIC
);
363 complete_info
->port
= p
;
364 __mdb_entry_to_br_ip(entry
, &complete_info
->ip
);
365 mdb
.obj
.complete_priv
= complete_info
;
366 mdb
.obj
.complete
= br_mdb_complete
;
367 if (switchdev_port_obj_add(port_dev
, &mdb
.obj
))
368 kfree(complete_info
);
370 } else if (p
&& port_dev
&& type
== RTM_DELMDB
) {
371 switchdev_port_obj_del(port_dev
, &mdb
.obj
);
375 br_mdb_switchdev_host(dev
, entry
, type
);
377 skb
= nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC
);
381 err
= nlmsg_populate_mdb_fill(skb
, dev
, entry
, 0, 0, type
, NTF_SELF
);
387 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
390 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
393 void br_mdb_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
394 struct br_ip
*group
, int type
, u8 flags
)
396 struct br_mdb_entry entry
;
398 memset(&entry
, 0, sizeof(entry
));
400 entry
.ifindex
= port
->dev
->ifindex
;
402 entry
.ifindex
= dev
->ifindex
;
403 entry
.addr
.proto
= group
->proto
;
404 entry
.addr
.u
.ip4
= group
->u
.ip4
;
405 #if IS_ENABLED(CONFIG_IPV6)
406 entry
.addr
.u
.ip6
= group
->u
.ip6
;
408 entry
.vid
= group
->vid
;
409 __mdb_entry_fill_flags(&entry
, flags
);
410 __br_mdb_notify(dev
, port
, &entry
, type
);
413 static int nlmsg_populate_rtr_fill(struct sk_buff
*skb
,
414 struct net_device
*dev
,
415 int ifindex
, u32 pid
,
416 u32 seq
, int type
, unsigned int flags
)
418 struct br_port_msg
*bpm
;
419 struct nlmsghdr
*nlh
;
422 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*bpm
), NLM_F_MULTI
);
426 bpm
= nlmsg_data(nlh
);
427 memset(bpm
, 0, sizeof(*bpm
));
428 bpm
->family
= AF_BRIDGE
;
429 bpm
->ifindex
= dev
->ifindex
;
430 nest
= nla_nest_start(skb
, MDBA_ROUTER
);
434 if (nla_put_u32(skb
, MDBA_ROUTER_PORT
, ifindex
))
437 nla_nest_end(skb
, nest
);
442 nla_nest_end(skb
, nest
);
444 nlmsg_cancel(skb
, nlh
);
448 static inline size_t rtnl_rtr_nlmsg_size(void)
450 return NLMSG_ALIGN(sizeof(struct br_port_msg
))
451 + nla_total_size(sizeof(__u32
));
454 void br_rtr_notify(struct net_device
*dev
, struct net_bridge_port
*port
,
457 struct net
*net
= dev_net(dev
);
462 ifindex
= port
? port
->dev
->ifindex
: 0;
463 skb
= nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC
);
467 err
= nlmsg_populate_rtr_fill(skb
, dev
, ifindex
, 0, 0, type
, NTF_SELF
);
473 rtnl_notify(skb
, net
, 0, RTNLGRP_MDB
, NULL
, GFP_ATOMIC
);
477 rtnl_set_sk_err(net
, RTNLGRP_MDB
, err
);
480 static bool is_valid_mdb_entry(struct br_mdb_entry
*entry
)
482 if (entry
->ifindex
== 0)
485 if (entry
->addr
.proto
== htons(ETH_P_IP
)) {
486 if (!ipv4_is_multicast(entry
->addr
.u
.ip4
))
488 if (ipv4_is_local_multicast(entry
->addr
.u
.ip4
))
490 #if IS_ENABLED(CONFIG_IPV6)
491 } else if (entry
->addr
.proto
== htons(ETH_P_IPV6
)) {
492 if (ipv6_addr_is_ll_all_nodes(&entry
->addr
.u
.ip6
))
497 if (entry
->state
!= MDB_PERMANENT
&& entry
->state
!= MDB_TEMPORARY
)
499 if (entry
->vid
>= VLAN_VID_MASK
)
505 static int br_mdb_parse(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
506 struct net_device
**pdev
, struct br_mdb_entry
**pentry
)
508 struct net
*net
= sock_net(skb
->sk
);
509 struct br_mdb_entry
*entry
;
510 struct br_port_msg
*bpm
;
511 struct nlattr
*tb
[MDBA_SET_ENTRY_MAX
+1];
512 struct net_device
*dev
;
515 err
= nlmsg_parse(nlh
, sizeof(*bpm
), tb
, MDBA_SET_ENTRY_MAX
, NULL
,
520 bpm
= nlmsg_data(nlh
);
521 if (bpm
->ifindex
== 0) {
522 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
526 dev
= __dev_get_by_index(net
, bpm
->ifindex
);
528 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
532 if (!(dev
->priv_flags
& IFF_EBRIDGE
)) {
533 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
539 if (!tb
[MDBA_SET_ENTRY
] ||
540 nla_len(tb
[MDBA_SET_ENTRY
]) != sizeof(struct br_mdb_entry
)) {
541 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
545 entry
= nla_data(tb
[MDBA_SET_ENTRY
]);
546 if (!is_valid_mdb_entry(entry
)) {
547 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
555 static int br_mdb_add_group(struct net_bridge
*br
, struct net_bridge_port
*port
,
556 struct br_ip
*group
, unsigned char state
)
558 struct net_bridge_mdb_entry
*mp
;
559 struct net_bridge_port_group
*p
;
560 struct net_bridge_port_group __rcu
**pp
;
561 struct net_bridge_mdb_htable
*mdb
;
562 unsigned long now
= jiffies
;
565 mdb
= mlock_dereference(br
->mdb
, br
);
566 mp
= br_mdb_ip_get(mdb
, group
);
568 mp
= br_multicast_new_group(br
, port
, group
);
569 err
= PTR_ERR_OR_ZERO(mp
);
574 for (pp
= &mp
->ports
;
575 (p
= mlock_dereference(*pp
, br
)) != NULL
;
579 if ((unsigned long)p
->port
< (unsigned long)port
)
583 p
= br_multicast_new_port_group(port
, group
, *pp
, state
, NULL
);
586 rcu_assign_pointer(*pp
, p
);
587 if (state
== MDB_TEMPORARY
)
588 mod_timer(&p
->timer
, now
+ br
->multicast_membership_interval
);
593 static int __br_mdb_add(struct net
*net
, struct net_bridge
*br
,
594 struct br_mdb_entry
*entry
)
597 struct net_device
*dev
;
598 struct net_bridge_port
*p
;
601 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
604 dev
= __dev_get_by_index(net
, entry
->ifindex
);
608 p
= br_port_get_rtnl(dev
);
609 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
612 __mdb_entry_to_br_ip(entry
, &ip
);
614 spin_lock_bh(&br
->multicast_lock
);
615 ret
= br_mdb_add_group(br
, p
, &ip
, entry
->state
);
616 spin_unlock_bh(&br
->multicast_lock
);
620 static int br_mdb_add(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
621 struct netlink_ext_ack
*extack
)
623 struct net
*net
= sock_net(skb
->sk
);
624 struct net_bridge_vlan_group
*vg
;
625 struct net_device
*dev
, *pdev
;
626 struct br_mdb_entry
*entry
;
627 struct net_bridge_port
*p
;
628 struct net_bridge_vlan
*v
;
629 struct net_bridge
*br
;
632 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
636 br
= netdev_priv(dev
);
638 /* If vlan filtering is enabled and VLAN is not specified
639 * install mdb entry on all vlans configured on the port.
641 pdev
= __dev_get_by_index(net
, entry
->ifindex
);
645 p
= br_port_get_rtnl(pdev
);
646 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
649 vg
= nbp_vlan_group(p
);
650 if (br_vlan_enabled(br
->dev
) && vg
&& entry
->vid
== 0) {
651 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
653 err
= __br_mdb_add(net
, br
, entry
);
656 __br_mdb_notify(dev
, p
, entry
, RTM_NEWMDB
);
659 err
= __br_mdb_add(net
, br
, entry
);
661 __br_mdb_notify(dev
, p
, entry
, RTM_NEWMDB
);
667 static int __br_mdb_del(struct net_bridge
*br
, struct br_mdb_entry
*entry
)
669 struct net_bridge_mdb_htable
*mdb
;
670 struct net_bridge_mdb_entry
*mp
;
671 struct net_bridge_port_group
*p
;
672 struct net_bridge_port_group __rcu
**pp
;
676 if (!netif_running(br
->dev
) || br
->multicast_disabled
)
679 __mdb_entry_to_br_ip(entry
, &ip
);
681 spin_lock_bh(&br
->multicast_lock
);
682 mdb
= mlock_dereference(br
->mdb
, br
);
684 mp
= br_mdb_ip_get(mdb
, &ip
);
688 for (pp
= &mp
->ports
;
689 (p
= mlock_dereference(*pp
, br
)) != NULL
;
691 if (!p
->port
|| p
->port
->dev
->ifindex
!= entry
->ifindex
)
694 if (p
->port
->state
== BR_STATE_DISABLED
)
697 __mdb_entry_fill_flags(entry
, p
->flags
);
698 rcu_assign_pointer(*pp
, p
->next
);
699 hlist_del_init(&p
->mglist
);
700 del_timer(&p
->timer
);
701 call_rcu_bh(&p
->rcu
, br_multicast_free_pg
);
704 if (!mp
->ports
&& !mp
->host_joined
&&
705 netif_running(br
->dev
))
706 mod_timer(&mp
->timer
, jiffies
);
711 spin_unlock_bh(&br
->multicast_lock
);
715 static int br_mdb_del(struct sk_buff
*skb
, struct nlmsghdr
*nlh
,
716 struct netlink_ext_ack
*extack
)
718 struct net
*net
= sock_net(skb
->sk
);
719 struct net_bridge_vlan_group
*vg
;
720 struct net_device
*dev
, *pdev
;
721 struct br_mdb_entry
*entry
;
722 struct net_bridge_port
*p
;
723 struct net_bridge_vlan
*v
;
724 struct net_bridge
*br
;
727 err
= br_mdb_parse(skb
, nlh
, &dev
, &entry
);
731 br
= netdev_priv(dev
);
733 /* If vlan filtering is enabled and VLAN is not specified
734 * delete mdb entry on all vlans configured on the port.
736 pdev
= __dev_get_by_index(net
, entry
->ifindex
);
740 p
= br_port_get_rtnl(pdev
);
741 if (!p
|| p
->br
!= br
|| p
->state
== BR_STATE_DISABLED
)
744 vg
= nbp_vlan_group(p
);
745 if (br_vlan_enabled(br
->dev
) && vg
&& entry
->vid
== 0) {
746 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
748 err
= __br_mdb_del(br
, entry
);
750 __br_mdb_notify(dev
, p
, entry
, RTM_DELMDB
);
753 err
= __br_mdb_del(br
, entry
);
755 __br_mdb_notify(dev
, p
, entry
, RTM_DELMDB
);
761 void br_mdb_init(void)
763 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_GETMDB
, NULL
, br_mdb_dump
, 0);
764 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_NEWMDB
, br_mdb_add
, NULL
, 0);
765 rtnl_register_module(THIS_MODULE
, PF_BRIDGE
, RTM_DELMDB
, br_mdb_del
, NULL
, 0);
768 void br_mdb_uninit(void)
770 rtnl_unregister(PF_BRIDGE
, RTM_GETMDB
);
771 rtnl_unregister(PF_BRIDGE
, RTM_NEWMDB
);
772 rtnl_unregister(PF_BRIDGE
, RTM_DELMDB
);