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"
20 br_ip4_rports_get_timer(struct net_bridge_mcast_port *pmctx,
23 *timer = br_timer_value(&pmctx->ip4_mc_router_timer);
24 return !hlist_unhashed(&pmctx->ip4_rlist);
28 br_ip6_rports_get_timer(struct net_bridge_mcast_port *pmctx,
31 #if IS_ENABLED(CONFIG_IPV6)
32 *timer = br_timer_value(&pmctx->ip6_mc_router_timer);
33 return !hlist_unhashed(&pmctx->ip6_rlist);
40 static size_t __br_rports_one_size(void)
42 return nla_total_size(sizeof(u32)) + /* MDBA_ROUTER_PORT */
43 nla_total_size(sizeof(u32)) + /* MDBA_ROUTER_PATTR_TIMER */
44 nla_total_size(sizeof(u8)) + /* MDBA_ROUTER_PATTR_TYPE */
45 nla_total_size(sizeof(u32)) + /* MDBA_ROUTER_PATTR_INET_TIMER */
46 nla_total_size(sizeof(u32)) + /* MDBA_ROUTER_PATTR_INET6_TIMER */
47 nla_total_size(sizeof(u32)); /* MDBA_ROUTER_PATTR_VID */
50 size_t br_rports_size(const struct net_bridge_mcast *brmctx)
52 struct net_bridge_mcast_port *pmctx;
53 size_t size = nla_total_size(0); /* MDBA_ROUTER */
56 hlist_for_each_entry_rcu(pmctx, &brmctx->ip4_mc_router_list,
58 size += __br_rports_one_size();
60 #if IS_ENABLED(CONFIG_IPV6)
61 hlist_for_each_entry_rcu(pmctx, &brmctx->ip6_mc_router_list,
63 size += __br_rports_one_size();
70 int br_rports_fill_info(struct sk_buff *skb,
71 const struct net_bridge_mcast *brmctx)
73 u16 vid = brmctx->vlan ? brmctx->vlan->vid : 0;
74 bool have_ip4_mc_rtr, have_ip6_mc_rtr;
75 unsigned long ip4_timer, ip6_timer;
76 struct nlattr *nest, *port_nest;
77 struct net_bridge_port *p;
79 if (!brmctx->multicast_router || !br_rports_have_mc_router(brmctx))
82 nest = nla_nest_start_noflag(skb, MDBA_ROUTER);
86 list_for_each_entry_rcu(p, &brmctx->br->port_list, list) {
87 struct net_bridge_mcast_port *pmctx;
90 struct net_bridge_vlan *v;
92 v = br_vlan_find(nbp_vlan_group(p), vid);
95 pmctx = &v->port_mcast_ctx;
97 pmctx = &p->multicast_ctx;
100 have_ip4_mc_rtr = br_ip4_rports_get_timer(pmctx, &ip4_timer);
101 have_ip6_mc_rtr = br_ip6_rports_get_timer(pmctx, &ip6_timer);
103 if (!have_ip4_mc_rtr && !have_ip6_mc_rtr)
106 port_nest = nla_nest_start_noflag(skb, MDBA_ROUTER_PORT);
110 if (nla_put_nohdr(skb, sizeof(u32), &p->dev->ifindex) ||
111 nla_put_u32(skb, MDBA_ROUTER_PATTR_TIMER,
112 max(ip4_timer, ip6_timer)) ||
113 nla_put_u8(skb, MDBA_ROUTER_PATTR_TYPE,
114 p->multicast_ctx.multicast_router) ||
116 nla_put_u32(skb, MDBA_ROUTER_PATTR_INET_TIMER,
119 nla_put_u32(skb, MDBA_ROUTER_PATTR_INET6_TIMER,
121 (vid && nla_put_u16(skb, MDBA_ROUTER_PATTR_VID, vid))) {
122 nla_nest_cancel(skb, port_nest);
125 nla_nest_end(skb, port_nest);
128 nla_nest_end(skb, nest);
131 nla_nest_cancel(skb, nest);
135 static void __mdb_entry_fill_flags(struct br_mdb_entry *e, unsigned char flags)
137 e->state = flags & MDB_PG_FLAGS_PERMANENT;
139 if (flags & MDB_PG_FLAGS_OFFLOAD)
140 e->flags |= MDB_FLAGS_OFFLOAD;
141 if (flags & MDB_PG_FLAGS_FAST_LEAVE)
142 e->flags |= MDB_FLAGS_FAST_LEAVE;
143 if (flags & MDB_PG_FLAGS_STAR_EXCL)
144 e->flags |= MDB_FLAGS_STAR_EXCL;
145 if (flags & MDB_PG_FLAGS_BLOCKED)
146 e->flags |= MDB_FLAGS_BLOCKED;
149 static void __mdb_entry_to_br_ip(struct br_mdb_entry *entry, struct br_ip *ip,
150 struct nlattr **mdb_attrs)
152 memset(ip, 0, sizeof(struct br_ip));
153 ip->vid = entry->vid;
154 ip->proto = entry->addr.proto;
156 case htons(ETH_P_IP):
157 ip->dst.ip4 = entry->addr.u.ip4;
158 if (mdb_attrs && mdb_attrs[MDBE_ATTR_SOURCE])
159 ip->src.ip4 = nla_get_in_addr(mdb_attrs[MDBE_ATTR_SOURCE]);
161 #if IS_ENABLED(CONFIG_IPV6)
162 case htons(ETH_P_IPV6):
163 ip->dst.ip6 = entry->addr.u.ip6;
164 if (mdb_attrs && mdb_attrs[MDBE_ATTR_SOURCE])
165 ip->src.ip6 = nla_get_in6_addr(mdb_attrs[MDBE_ATTR_SOURCE]);
169 ether_addr_copy(ip->dst.mac_addr, entry->addr.u.mac_addr);
174 static int __mdb_fill_srcs(struct sk_buff *skb,
175 struct net_bridge_port_group *p)
177 struct net_bridge_group_src *ent;
178 struct nlattr *nest, *nest_ent;
180 if (hlist_empty(&p->src_list))
183 nest = nla_nest_start(skb, MDBA_MDB_EATTR_SRC_LIST);
187 hlist_for_each_entry_rcu(ent, &p->src_list, node,
188 lockdep_is_held(&p->key.port->br->multicast_lock)) {
189 nest_ent = nla_nest_start(skb, MDBA_MDB_SRCLIST_ENTRY);
192 switch (ent->addr.proto) {
193 case htons(ETH_P_IP):
194 if (nla_put_in_addr(skb, MDBA_MDB_SRCATTR_ADDRESS,
195 ent->addr.src.ip4)) {
196 nla_nest_cancel(skb, nest_ent);
200 #if IS_ENABLED(CONFIG_IPV6)
201 case htons(ETH_P_IPV6):
202 if (nla_put_in6_addr(skb, MDBA_MDB_SRCATTR_ADDRESS,
203 &ent->addr.src.ip6)) {
204 nla_nest_cancel(skb, nest_ent);
210 nla_nest_cancel(skb, nest_ent);
213 if (nla_put_u32(skb, MDBA_MDB_SRCATTR_TIMER,
214 br_timer_value(&ent->timer))) {
215 nla_nest_cancel(skb, nest_ent);
218 nla_nest_end(skb, nest_ent);
221 nla_nest_end(skb, nest);
226 nla_nest_cancel(skb, nest);
230 static int __mdb_fill_info(struct sk_buff *skb,
231 struct net_bridge_mdb_entry *mp,
232 struct net_bridge_port_group *p)
234 bool dump_srcs_mode = false;
235 struct timer_list *mtimer;
236 struct nlattr *nest_ent;
237 struct br_mdb_entry e;
241 memset(&e, 0, sizeof(e));
243 ifindex = p->key.port->dev->ifindex;
247 ifindex = mp->br->dev->ifindex;
251 __mdb_entry_fill_flags(&e, flags);
253 e.vid = mp->addr.vid;
254 if (mp->addr.proto == htons(ETH_P_IP))
255 e.addr.u.ip4 = mp->addr.dst.ip4;
256 #if IS_ENABLED(CONFIG_IPV6)
257 else if (mp->addr.proto == htons(ETH_P_IPV6))
258 e.addr.u.ip6 = mp->addr.dst.ip6;
261 ether_addr_copy(e.addr.u.mac_addr, mp->addr.dst.mac_addr);
262 e.addr.proto = mp->addr.proto;
263 nest_ent = nla_nest_start_noflag(skb,
264 MDBA_MDB_ENTRY_INFO);
268 if (nla_put_nohdr(skb, sizeof(e), &e) ||
270 MDBA_MDB_EATTR_TIMER,
271 br_timer_value(mtimer)))
274 switch (mp->addr.proto) {
275 case htons(ETH_P_IP):
276 dump_srcs_mode = !!(mp->br->multicast_ctx.multicast_igmp_version == 3);
277 if (mp->addr.src.ip4) {
278 if (nla_put_in_addr(skb, MDBA_MDB_EATTR_SOURCE,
284 #if IS_ENABLED(CONFIG_IPV6)
285 case htons(ETH_P_IPV6):
286 dump_srcs_mode = !!(mp->br->multicast_ctx.multicast_mld_version == 2);
287 if (!ipv6_addr_any(&mp->addr.src.ip6)) {
288 if (nla_put_in6_addr(skb, MDBA_MDB_EATTR_SOURCE,
296 ether_addr_copy(e.addr.u.mac_addr, mp->addr.dst.mac_addr);
299 if (nla_put_u8(skb, MDBA_MDB_EATTR_RTPROT, p->rt_protocol))
301 if (dump_srcs_mode &&
302 (__mdb_fill_srcs(skb, p) ||
303 nla_put_u8(skb, MDBA_MDB_EATTR_GROUP_MODE,
307 nla_nest_end(skb, nest_ent);
312 nla_nest_cancel(skb, nest_ent);
316 static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
317 struct net_device *dev)
319 int idx = 0, s_idx = cb->args[1], err = 0, pidx = 0, s_pidx = cb->args[2];
320 struct net_bridge *br = netdev_priv(dev);
321 struct net_bridge_mdb_entry *mp;
322 struct nlattr *nest, *nest2;
324 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
327 nest = nla_nest_start_noflag(skb, MDBA_MDB);
331 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
332 struct net_bridge_port_group *p;
333 struct net_bridge_port_group __rcu **pp;
338 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
344 if (!s_pidx && mp->host_joined) {
345 err = __mdb_fill_info(skb, mp, NULL);
347 nla_nest_cancel(skb, nest2);
352 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
359 err = __mdb_fill_info(skb, mp, p);
361 nla_nest_end(skb, nest2);
369 nla_nest_end(skb, nest2);
377 nla_nest_end(skb, nest);
381 static int br_mdb_valid_dump_req(const struct nlmsghdr *nlh,
382 struct netlink_ext_ack *extack)
384 struct br_port_msg *bpm;
386 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
387 NL_SET_ERR_MSG_MOD(extack, "Invalid header for mdb dump request");
391 bpm = nlmsg_data(nlh);
393 NL_SET_ERR_MSG_MOD(extack, "Filtering by device index is not supported for mdb dump request");
396 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
397 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
404 static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
406 struct net_device *dev;
407 struct net *net = sock_net(skb->sk);
408 struct nlmsghdr *nlh = NULL;
411 if (cb->strict_check) {
412 int err = br_mdb_valid_dump_req(cb->nlh, cb->extack);
422 cb->seq = net->dev_base_seq;
424 for_each_netdev_rcu(net, dev) {
425 if (dev->priv_flags & IFF_EBRIDGE) {
426 struct net_bridge *br = netdev_priv(dev);
427 struct br_port_msg *bpm;
432 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
433 cb->nlh->nlmsg_seq, RTM_GETMDB,
434 sizeof(*bpm), NLM_F_MULTI);
438 bpm = nlmsg_data(nlh);
439 memset(bpm, 0, sizeof(*bpm));
440 bpm->ifindex = dev->ifindex;
441 if (br_mdb_fill_info(skb, cb, dev) < 0)
443 if (br_rports_fill_info(skb, &br->multicast_ctx) < 0)
461 static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
462 struct net_device *dev,
463 struct net_bridge_mdb_entry *mp,
464 struct net_bridge_port_group *pg,
467 struct nlmsghdr *nlh;
468 struct br_port_msg *bpm;
469 struct nlattr *nest, *nest2;
471 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*bpm), 0);
475 bpm = nlmsg_data(nlh);
476 memset(bpm, 0, sizeof(*bpm));
477 bpm->family = AF_BRIDGE;
478 bpm->ifindex = dev->ifindex;
479 nest = nla_nest_start_noflag(skb, MDBA_MDB);
482 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
486 if (__mdb_fill_info(skb, mp, pg))
489 nla_nest_end(skb, nest2);
490 nla_nest_end(skb, nest);
495 nla_nest_end(skb, nest);
497 nlmsg_cancel(skb, nlh);
501 static size_t rtnl_mdb_nlmsg_size(struct net_bridge_port_group *pg)
503 size_t nlmsg_size = NLMSG_ALIGN(sizeof(struct br_port_msg)) +
504 nla_total_size(sizeof(struct br_mdb_entry)) +
505 nla_total_size(sizeof(u32));
506 struct net_bridge_group_src *ent;
507 size_t addr_size = 0;
512 /* MDBA_MDB_EATTR_RTPROT */
513 nlmsg_size += nla_total_size(sizeof(u8));
515 switch (pg->key.addr.proto) {
516 case htons(ETH_P_IP):
517 /* MDBA_MDB_EATTR_SOURCE */
518 if (pg->key.addr.src.ip4)
519 nlmsg_size += nla_total_size(sizeof(__be32));
520 if (pg->key.port->br->multicast_ctx.multicast_igmp_version == 2)
522 addr_size = sizeof(__be32);
524 #if IS_ENABLED(CONFIG_IPV6)
525 case htons(ETH_P_IPV6):
526 /* MDBA_MDB_EATTR_SOURCE */
527 if (!ipv6_addr_any(&pg->key.addr.src.ip6))
528 nlmsg_size += nla_total_size(sizeof(struct in6_addr));
529 if (pg->key.port->br->multicast_ctx.multicast_mld_version == 1)
531 addr_size = sizeof(struct in6_addr);
536 /* MDBA_MDB_EATTR_GROUP_MODE */
537 nlmsg_size += nla_total_size(sizeof(u8));
539 /* MDBA_MDB_EATTR_SRC_LIST nested attr */
540 if (!hlist_empty(&pg->src_list))
541 nlmsg_size += nla_total_size(0);
543 hlist_for_each_entry(ent, &pg->src_list, node) {
544 /* MDBA_MDB_SRCLIST_ENTRY nested attr +
545 * MDBA_MDB_SRCATTR_ADDRESS + MDBA_MDB_SRCATTR_TIMER
547 nlmsg_size += nla_total_size(0) +
548 nla_total_size(addr_size) +
549 nla_total_size(sizeof(u32));
555 struct br_mdb_complete_info {
556 struct net_bridge_port *port;
560 static void br_mdb_complete(struct net_device *dev, int err, void *priv)
562 struct br_mdb_complete_info *data = priv;
563 struct net_bridge_port_group __rcu **pp;
564 struct net_bridge_port_group *p;
565 struct net_bridge_mdb_entry *mp;
566 struct net_bridge_port *port = data->port;
567 struct net_bridge *br = port->br;
572 spin_lock_bh(&br->multicast_lock);
573 mp = br_mdb_ip_get(br, &data->ip);
576 for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
578 if (p->key.port != port)
580 p->flags |= MDB_PG_FLAGS_OFFLOAD;
583 spin_unlock_bh(&br->multicast_lock);
588 static void br_switchdev_mdb_populate(struct switchdev_obj_port_mdb *mdb,
589 const struct net_bridge_mdb_entry *mp)
591 if (mp->addr.proto == htons(ETH_P_IP))
592 ip_eth_mc_map(mp->addr.dst.ip4, mdb->addr);
593 #if IS_ENABLED(CONFIG_IPV6)
594 else if (mp->addr.proto == htons(ETH_P_IPV6))
595 ipv6_eth_mc_map(&mp->addr.dst.ip6, mdb->addr);
598 ether_addr_copy(mdb->addr, mp->addr.dst.mac_addr);
600 mdb->vid = mp->addr.vid;
603 static int br_mdb_replay_one(struct notifier_block *nb, struct net_device *dev,
604 const struct switchdev_obj_port_mdb *mdb,
605 unsigned long action, const void *ctx,
606 struct netlink_ext_ack *extack)
608 struct switchdev_notifier_port_obj_info obj_info = {
618 err = nb->notifier_call(nb, action, &obj_info);
619 return notifier_to_errno(err);
622 static int br_mdb_queue_one(struct list_head *mdb_list,
623 enum switchdev_obj_id id,
624 const struct net_bridge_mdb_entry *mp,
625 struct net_device *orig_dev)
627 struct switchdev_obj_port_mdb *mdb;
629 mdb = kzalloc(sizeof(*mdb), GFP_ATOMIC);
634 mdb->obj.orig_dev = orig_dev;
635 br_switchdev_mdb_populate(mdb, mp);
636 list_add_tail(&mdb->obj.list, mdb_list);
641 int br_mdb_replay(struct net_device *br_dev, struct net_device *dev,
642 const void *ctx, bool adding, struct notifier_block *nb,
643 struct netlink_ext_ack *extack)
645 const struct net_bridge_mdb_entry *mp;
646 struct switchdev_obj *obj, *tmp;
647 struct net_bridge *br;
648 unsigned long action;
657 if (!netif_is_bridge_master(br_dev) || !netif_is_bridge_port(dev))
660 br = netdev_priv(br_dev);
662 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
665 /* We cannot walk over br->mdb_list protected just by the rtnl_mutex,
666 * because the write-side protection is br->multicast_lock. But we
667 * need to emulate the [ blocking ] calling context of a regular
668 * switchdev event, so since both br->multicast_lock and RCU read side
669 * critical sections are atomic, we have no choice but to pick the RCU
670 * read side lock, queue up all our events, leave the critical section
671 * and notify switchdev from blocking context.
675 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
676 struct net_bridge_port_group __rcu * const *pp;
677 const struct net_bridge_port_group *p;
679 if (mp->host_joined) {
680 err = br_mdb_queue_one(&mdb_list,
681 SWITCHDEV_OBJ_ID_HOST_MDB,
689 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
691 if (p->key.port->dev != dev)
694 err = br_mdb_queue_one(&mdb_list,
695 SWITCHDEV_OBJ_ID_PORT_MDB,
707 action = SWITCHDEV_PORT_OBJ_ADD;
709 action = SWITCHDEV_PORT_OBJ_DEL;
711 list_for_each_entry(obj, &mdb_list, list) {
712 err = br_mdb_replay_one(nb, dev, SWITCHDEV_OBJ_PORT_MDB(obj),
713 action, ctx, extack);
719 list_for_each_entry_safe(obj, tmp, &mdb_list, list) {
720 list_del(&obj->list);
721 kfree(SWITCHDEV_OBJ_PORT_MDB(obj));
727 static void br_mdb_switchdev_host_port(struct net_device *dev,
728 struct net_device *lower_dev,
729 struct net_bridge_mdb_entry *mp,
732 struct switchdev_obj_port_mdb mdb = {
734 .id = SWITCHDEV_OBJ_ID_HOST_MDB,
735 .flags = SWITCHDEV_F_DEFER,
740 br_switchdev_mdb_populate(&mdb, mp);
744 switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
747 switchdev_port_obj_del(lower_dev, &mdb.obj);
752 static void br_mdb_switchdev_host(struct net_device *dev,
753 struct net_bridge_mdb_entry *mp, int type)
755 struct net_device *lower_dev;
756 struct list_head *iter;
758 netdev_for_each_lower_dev(dev, lower_dev, iter)
759 br_mdb_switchdev_host_port(dev, lower_dev, mp, type);
762 void br_mdb_notify(struct net_device *dev,
763 struct net_bridge_mdb_entry *mp,
764 struct net_bridge_port_group *pg,
767 struct br_mdb_complete_info *complete_info;
768 struct switchdev_obj_port_mdb mdb = {
770 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
771 .flags = SWITCHDEV_F_DEFER,
774 struct net *net = dev_net(dev);
779 br_switchdev_mdb_populate(&mdb, mp);
781 mdb.obj.orig_dev = pg->key.port->dev;
784 complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
787 complete_info->port = pg->key.port;
788 complete_info->ip = mp->addr;
789 mdb.obj.complete_priv = complete_info;
790 mdb.obj.complete = br_mdb_complete;
791 if (switchdev_port_obj_add(pg->key.port->dev, &mdb.obj, NULL))
792 kfree(complete_info);
795 switchdev_port_obj_del(pg->key.port->dev, &mdb.obj);
799 br_mdb_switchdev_host(dev, mp, type);
802 skb = nlmsg_new(rtnl_mdb_nlmsg_size(pg), GFP_ATOMIC);
806 err = nlmsg_populate_mdb_fill(skb, dev, mp, pg, type);
812 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
815 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
818 static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
819 struct net_device *dev,
820 int ifindex, u16 vid, u32 pid,
821 u32 seq, int type, unsigned int flags)
823 struct nlattr *nest, *port_nest;
824 struct br_port_msg *bpm;
825 struct nlmsghdr *nlh;
827 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
831 bpm = nlmsg_data(nlh);
832 memset(bpm, 0, sizeof(*bpm));
833 bpm->family = AF_BRIDGE;
834 bpm->ifindex = dev->ifindex;
835 nest = nla_nest_start_noflag(skb, MDBA_ROUTER);
839 port_nest = nla_nest_start_noflag(skb, MDBA_ROUTER_PORT);
842 if (nla_put_nohdr(skb, sizeof(u32), &ifindex)) {
843 nla_nest_cancel(skb, port_nest);
846 if (vid && nla_put_u16(skb, MDBA_ROUTER_PATTR_VID, vid)) {
847 nla_nest_cancel(skb, port_nest);
850 nla_nest_end(skb, port_nest);
852 nla_nest_end(skb, nest);
857 nla_nest_end(skb, nest);
859 nlmsg_cancel(skb, nlh);
863 static inline size_t rtnl_rtr_nlmsg_size(void)
865 return NLMSG_ALIGN(sizeof(struct br_port_msg))
866 + nla_total_size(sizeof(__u32))
867 + nla_total_size(sizeof(u16));
870 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
873 struct net *net = dev_net(dev);
879 ifindex = pmctx ? pmctx->port->dev->ifindex : 0;
880 vid = pmctx && br_multicast_port_ctx_is_vlan(pmctx) ? pmctx->vlan->vid :
882 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
886 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, vid, 0, 0, type,
893 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
897 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
900 static bool is_valid_mdb_entry(struct br_mdb_entry *entry,
901 struct netlink_ext_ack *extack)
903 if (entry->ifindex == 0) {
904 NL_SET_ERR_MSG_MOD(extack, "Zero entry ifindex is not allowed");
908 if (entry->addr.proto == htons(ETH_P_IP)) {
909 if (!ipv4_is_multicast(entry->addr.u.ip4)) {
910 NL_SET_ERR_MSG_MOD(extack, "IPv4 entry group address is not multicast");
913 if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
914 NL_SET_ERR_MSG_MOD(extack, "IPv4 entry group address is local multicast");
917 #if IS_ENABLED(CONFIG_IPV6)
918 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
919 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
920 NL_SET_ERR_MSG_MOD(extack, "IPv6 entry group address is link-local all nodes");
924 } else if (entry->addr.proto == 0) {
926 if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
927 NL_SET_ERR_MSG_MOD(extack, "L2 entry group is not multicast");
931 NL_SET_ERR_MSG_MOD(extack, "Unknown entry protocol");
935 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
936 NL_SET_ERR_MSG_MOD(extack, "Unknown entry state");
939 if (entry->vid >= VLAN_VID_MASK) {
940 NL_SET_ERR_MSG_MOD(extack, "Invalid entry VLAN id");
947 static bool is_valid_mdb_source(struct nlattr *attr, __be16 proto,
948 struct netlink_ext_ack *extack)
951 case htons(ETH_P_IP):
952 if (nla_len(attr) != sizeof(struct in_addr)) {
953 NL_SET_ERR_MSG_MOD(extack, "IPv4 invalid source address length");
956 if (ipv4_is_multicast(nla_get_in_addr(attr))) {
957 NL_SET_ERR_MSG_MOD(extack, "IPv4 multicast source address is not allowed");
961 #if IS_ENABLED(CONFIG_IPV6)
962 case htons(ETH_P_IPV6): {
965 if (nla_len(attr) != sizeof(struct in6_addr)) {
966 NL_SET_ERR_MSG_MOD(extack, "IPv6 invalid source address length");
969 src = nla_get_in6_addr(attr);
970 if (ipv6_addr_is_multicast(&src)) {
971 NL_SET_ERR_MSG_MOD(extack, "IPv6 multicast source address is not allowed");
978 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol used with source address");
985 static const struct nla_policy br_mdbe_attrs_pol[MDBE_ATTR_MAX + 1] = {
986 [MDBE_ATTR_SOURCE] = NLA_POLICY_RANGE(NLA_BINARY,
987 sizeof(struct in_addr),
988 sizeof(struct in6_addr)),
991 static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
992 struct net_device **pdev, struct br_mdb_entry **pentry,
993 struct nlattr **mdb_attrs, struct netlink_ext_ack *extack)
995 struct net *net = sock_net(skb->sk);
996 struct br_mdb_entry *entry;
997 struct br_port_msg *bpm;
998 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
999 struct net_device *dev;
1002 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
1003 MDBA_SET_ENTRY_MAX, NULL, NULL);
1007 bpm = nlmsg_data(nlh);
1008 if (bpm->ifindex == 0) {
1009 NL_SET_ERR_MSG_MOD(extack, "Invalid bridge ifindex");
1013 dev = __dev_get_by_index(net, bpm->ifindex);
1015 NL_SET_ERR_MSG_MOD(extack, "Bridge device doesn't exist");
1019 if (!(dev->priv_flags & IFF_EBRIDGE)) {
1020 NL_SET_ERR_MSG_MOD(extack, "Device is not a bridge");
1026 if (!tb[MDBA_SET_ENTRY]) {
1027 NL_SET_ERR_MSG_MOD(extack, "Missing MDBA_SET_ENTRY attribute");
1030 if (nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
1031 NL_SET_ERR_MSG_MOD(extack, "Invalid MDBA_SET_ENTRY attribute length");
1035 entry = nla_data(tb[MDBA_SET_ENTRY]);
1036 if (!is_valid_mdb_entry(entry, extack))
1040 if (tb[MDBA_SET_ENTRY_ATTRS]) {
1041 err = nla_parse_nested(mdb_attrs, MDBE_ATTR_MAX,
1042 tb[MDBA_SET_ENTRY_ATTRS],
1043 br_mdbe_attrs_pol, extack);
1046 if (mdb_attrs[MDBE_ATTR_SOURCE] &&
1047 !is_valid_mdb_source(mdb_attrs[MDBE_ATTR_SOURCE],
1048 entry->addr.proto, extack))
1051 memset(mdb_attrs, 0,
1052 sizeof(struct nlattr *) * (MDBE_ATTR_MAX + 1));
1058 static struct net_bridge_mcast *
1059 __br_mdb_choose_context(struct net_bridge *br,
1060 const struct br_mdb_entry *entry,
1061 struct netlink_ext_ack *extack)
1063 struct net_bridge_mcast *brmctx = NULL;
1064 struct net_bridge_vlan *v;
1066 if (!br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
1067 brmctx = &br->multicast_ctx;
1072 NL_SET_ERR_MSG_MOD(extack, "Cannot add an entry without a vlan when vlan snooping is enabled");
1076 v = br_vlan_find(br_vlan_group(br), entry->vid);
1078 NL_SET_ERR_MSG_MOD(extack, "Vlan is not configured");
1081 if (br_multicast_ctx_vlan_global_disabled(&v->br_mcast_ctx)) {
1082 NL_SET_ERR_MSG_MOD(extack, "Vlan's multicast processing is disabled");
1085 brmctx = &v->br_mcast_ctx;
1090 static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
1091 struct br_mdb_entry *entry,
1092 struct nlattr **mdb_attrs,
1093 struct netlink_ext_ack *extack)
1095 struct net_bridge_mdb_entry *mp, *star_mp;
1096 struct net_bridge_port_group __rcu **pp;
1097 struct net_bridge_port_group *p;
1098 struct net_bridge_mcast *brmctx;
1099 struct br_ip group, star_group;
1100 unsigned long now = jiffies;
1101 unsigned char flags = 0;
1105 __mdb_entry_to_br_ip(entry, &group, mdb_attrs);
1107 brmctx = __br_mdb_choose_context(br, entry, extack);
1111 /* host join errors which can happen before creating the group */
1113 /* don't allow any flags for host-joined groups */
1115 NL_SET_ERR_MSG_MOD(extack, "Flags are not allowed for host groups");
1118 if (!br_multicast_is_star_g(&group)) {
1119 NL_SET_ERR_MSG_MOD(extack, "Groups with sources cannot be manually host joined");
1124 if (br_group_is_l2(&group) && entry->state != MDB_PERMANENT) {
1125 NL_SET_ERR_MSG_MOD(extack, "Only permanent L2 entries allowed");
1129 mp = br_mdb_ip_get(br, &group);
1131 mp = br_multicast_new_group(br, &group);
1132 err = PTR_ERR_OR_ZERO(mp);
1139 if (mp->host_joined) {
1140 NL_SET_ERR_MSG_MOD(extack, "Group is already joined by host");
1144 br_multicast_host_join(brmctx, mp, false);
1145 br_mdb_notify(br->dev, mp, NULL, RTM_NEWMDB);
1150 for (pp = &mp->ports;
1151 (p = mlock_dereference(*pp, br)) != NULL;
1153 if (p->key.port == port) {
1154 NL_SET_ERR_MSG_MOD(extack, "Group is already joined by port");
1157 if ((unsigned long)p->key.port < (unsigned long)port)
1161 filter_mode = br_multicast_is_star_g(&group) ? MCAST_EXCLUDE :
1164 if (entry->state == MDB_PERMANENT)
1165 flags |= MDB_PG_FLAGS_PERMANENT;
1167 p = br_multicast_new_port_group(port, &group, *pp, flags, NULL,
1168 filter_mode, RTPROT_STATIC);
1170 NL_SET_ERR_MSG_MOD(extack, "Couldn't allocate new port group");
1173 rcu_assign_pointer(*pp, p);
1174 if (entry->state == MDB_TEMPORARY)
1175 mod_timer(&p->timer,
1176 now + brmctx->multicast_membership_interval);
1177 br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
1178 /* if we are adding a new EXCLUDE port group (*,G) it needs to be also
1179 * added to all S,G entries for proper replication, if we are adding
1180 * a new INCLUDE port (S,G) then all of *,G EXCLUDE ports need to be
1181 * added to it for proper replication
1183 if (br_multicast_should_handle_mode(brmctx, group.proto)) {
1184 switch (filter_mode) {
1186 br_multicast_star_g_handle_mode(p, MCAST_EXCLUDE);
1189 star_group = p->key.addr;
1190 memset(&star_group.src, 0, sizeof(star_group.src));
1191 star_mp = br_mdb_ip_get(br, &star_group);
1193 br_multicast_sg_add_exclude_ports(star_mp, p);
1201 static int __br_mdb_add(struct net *net, struct net_bridge *br,
1202 struct net_bridge_port *p,
1203 struct br_mdb_entry *entry,
1204 struct nlattr **mdb_attrs,
1205 struct netlink_ext_ack *extack)
1209 spin_lock_bh(&br->multicast_lock);
1210 ret = br_mdb_add_group(br, p, entry, mdb_attrs, extack);
1211 spin_unlock_bh(&br->multicast_lock);
1216 static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
1217 struct netlink_ext_ack *extack)
1219 struct nlattr *mdb_attrs[MDBE_ATTR_MAX + 1];
1220 struct net *net = sock_net(skb->sk);
1221 struct net_bridge_vlan_group *vg;
1222 struct net_bridge_port *p = NULL;
1223 struct net_device *dev, *pdev;
1224 struct br_mdb_entry *entry;
1225 struct net_bridge_vlan *v;
1226 struct net_bridge *br;
1229 err = br_mdb_parse(skb, nlh, &dev, &entry, mdb_attrs, extack);
1233 br = netdev_priv(dev);
1235 if (!netif_running(br->dev)) {
1236 NL_SET_ERR_MSG_MOD(extack, "Bridge device is not running");
1240 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
1241 NL_SET_ERR_MSG_MOD(extack, "Bridge's multicast processing is disabled");
1245 if (entry->ifindex != br->dev->ifindex) {
1246 pdev = __dev_get_by_index(net, entry->ifindex);
1248 NL_SET_ERR_MSG_MOD(extack, "Port net device doesn't exist");
1252 p = br_port_get_rtnl(pdev);
1254 NL_SET_ERR_MSG_MOD(extack, "Net device is not a bridge port");
1259 NL_SET_ERR_MSG_MOD(extack, "Port belongs to a different bridge device");
1262 if (p->state == BR_STATE_DISABLED) {
1263 NL_SET_ERR_MSG_MOD(extack, "Port is in disabled state");
1266 vg = nbp_vlan_group(p);
1268 vg = br_vlan_group(br);
1271 /* If vlan filtering is enabled and VLAN is not specified
1272 * install mdb entry on all vlans configured on the port.
1274 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
1275 list_for_each_entry(v, &vg->vlan_list, vlist) {
1276 entry->vid = v->vid;
1277 err = __br_mdb_add(net, br, p, entry, mdb_attrs, extack);
1282 err = __br_mdb_add(net, br, p, entry, mdb_attrs, extack);
1288 static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry,
1289 struct nlattr **mdb_attrs)
1291 struct net_bridge_mdb_entry *mp;
1292 struct net_bridge_port_group *p;
1293 struct net_bridge_port_group __rcu **pp;
1297 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1300 __mdb_entry_to_br_ip(entry, &ip, mdb_attrs);
1302 spin_lock_bh(&br->multicast_lock);
1303 mp = br_mdb_ip_get(br, &ip);
1308 if (entry->ifindex == mp->br->dev->ifindex && mp->host_joined) {
1309 br_multicast_host_leave(mp, false);
1311 br_mdb_notify(br->dev, mp, NULL, RTM_DELMDB);
1312 if (!mp->ports && netif_running(br->dev))
1313 mod_timer(&mp->timer, jiffies);
1317 for (pp = &mp->ports;
1318 (p = mlock_dereference(*pp, br)) != NULL;
1320 if (!p->key.port || p->key.port->dev->ifindex != entry->ifindex)
1323 if (p->key.port->state == BR_STATE_DISABLED)
1326 br_multicast_del_pg(mp, p, pp);
1332 spin_unlock_bh(&br->multicast_lock);
1336 static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
1337 struct netlink_ext_ack *extack)
1339 struct nlattr *mdb_attrs[MDBE_ATTR_MAX + 1];
1340 struct net *net = sock_net(skb->sk);
1341 struct net_bridge_vlan_group *vg;
1342 struct net_bridge_port *p = NULL;
1343 struct net_device *dev, *pdev;
1344 struct br_mdb_entry *entry;
1345 struct net_bridge_vlan *v;
1346 struct net_bridge *br;
1349 err = br_mdb_parse(skb, nlh, &dev, &entry, mdb_attrs, extack);
1353 br = netdev_priv(dev);
1355 if (entry->ifindex != br->dev->ifindex) {
1356 pdev = __dev_get_by_index(net, entry->ifindex);
1360 p = br_port_get_rtnl(pdev);
1361 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
1363 vg = nbp_vlan_group(p);
1365 vg = br_vlan_group(br);
1368 /* If vlan filtering is enabled and VLAN is not specified
1369 * delete mdb entry on all vlans configured on the port.
1371 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
1372 list_for_each_entry(v, &vg->vlan_list, vlist) {
1373 entry->vid = v->vid;
1374 err = __br_mdb_del(br, entry, mdb_attrs);
1377 err = __br_mdb_del(br, entry, mdb_attrs);
1383 void br_mdb_init(void)
1385 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, 0);
1386 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, 0);
1387 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, 0);
1390 void br_mdb_uninit(void)
1392 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
1393 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
1394 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);