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.state = MDB_PG_FLAGS_PERMANENT;
264 e.addr.proto = mp->addr.proto;
265 nest_ent = nla_nest_start_noflag(skb,
266 MDBA_MDB_ENTRY_INFO);
270 if (nla_put_nohdr(skb, sizeof(e), &e) ||
272 MDBA_MDB_EATTR_TIMER,
273 br_timer_value(mtimer)))
276 switch (mp->addr.proto) {
277 case htons(ETH_P_IP):
278 dump_srcs_mode = !!(mp->br->multicast_ctx.multicast_igmp_version == 3);
279 if (mp->addr.src.ip4) {
280 if (nla_put_in_addr(skb, MDBA_MDB_EATTR_SOURCE,
286 #if IS_ENABLED(CONFIG_IPV6)
287 case htons(ETH_P_IPV6):
288 dump_srcs_mode = !!(mp->br->multicast_ctx.multicast_mld_version == 2);
289 if (!ipv6_addr_any(&mp->addr.src.ip6)) {
290 if (nla_put_in6_addr(skb, MDBA_MDB_EATTR_SOURCE,
298 ether_addr_copy(e.addr.u.mac_addr, mp->addr.dst.mac_addr);
301 if (nla_put_u8(skb, MDBA_MDB_EATTR_RTPROT, p->rt_protocol))
303 if (dump_srcs_mode &&
304 (__mdb_fill_srcs(skb, p) ||
305 nla_put_u8(skb, MDBA_MDB_EATTR_GROUP_MODE,
309 nla_nest_end(skb, nest_ent);
314 nla_nest_cancel(skb, nest_ent);
318 static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
319 struct net_device *dev)
321 int idx = 0, s_idx = cb->args[1], err = 0, pidx = 0, s_pidx = cb->args[2];
322 struct net_bridge *br = netdev_priv(dev);
323 struct net_bridge_mdb_entry *mp;
324 struct nlattr *nest, *nest2;
326 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
329 nest = nla_nest_start_noflag(skb, MDBA_MDB);
333 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
334 struct net_bridge_port_group *p;
335 struct net_bridge_port_group __rcu **pp;
340 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
346 if (!s_pidx && mp->host_joined) {
347 err = __mdb_fill_info(skb, mp, NULL);
349 nla_nest_cancel(skb, nest2);
354 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
361 err = __mdb_fill_info(skb, mp, p);
363 nla_nest_end(skb, nest2);
371 nla_nest_end(skb, nest2);
379 nla_nest_end(skb, nest);
383 static int br_mdb_valid_dump_req(const struct nlmsghdr *nlh,
384 struct netlink_ext_ack *extack)
386 struct br_port_msg *bpm;
388 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
389 NL_SET_ERR_MSG_MOD(extack, "Invalid header for mdb dump request");
393 bpm = nlmsg_data(nlh);
395 NL_SET_ERR_MSG_MOD(extack, "Filtering by device index is not supported for mdb dump request");
398 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
399 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
406 static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
408 struct net_device *dev;
409 struct net *net = sock_net(skb->sk);
410 struct nlmsghdr *nlh = NULL;
413 if (cb->strict_check) {
414 int err = br_mdb_valid_dump_req(cb->nlh, cb->extack);
424 cb->seq = net->dev_base_seq;
426 for_each_netdev_rcu(net, dev) {
427 if (netif_is_bridge_master(dev)) {
428 struct net_bridge *br = netdev_priv(dev);
429 struct br_port_msg *bpm;
434 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
435 cb->nlh->nlmsg_seq, RTM_GETMDB,
436 sizeof(*bpm), NLM_F_MULTI);
440 bpm = nlmsg_data(nlh);
441 memset(bpm, 0, sizeof(*bpm));
442 bpm->ifindex = dev->ifindex;
443 if (br_mdb_fill_info(skb, cb, dev) < 0)
445 if (br_rports_fill_info(skb, &br->multicast_ctx) < 0)
463 static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
464 struct net_device *dev,
465 struct net_bridge_mdb_entry *mp,
466 struct net_bridge_port_group *pg,
469 struct nlmsghdr *nlh;
470 struct br_port_msg *bpm;
471 struct nlattr *nest, *nest2;
473 nlh = nlmsg_put(skb, 0, 0, type, sizeof(*bpm), 0);
477 bpm = nlmsg_data(nlh);
478 memset(bpm, 0, sizeof(*bpm));
479 bpm->family = AF_BRIDGE;
480 bpm->ifindex = dev->ifindex;
481 nest = nla_nest_start_noflag(skb, MDBA_MDB);
484 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
488 if (__mdb_fill_info(skb, mp, pg))
491 nla_nest_end(skb, nest2);
492 nla_nest_end(skb, nest);
497 nla_nest_end(skb, nest);
499 nlmsg_cancel(skb, nlh);
503 static size_t rtnl_mdb_nlmsg_size(struct net_bridge_port_group *pg)
505 size_t nlmsg_size = NLMSG_ALIGN(sizeof(struct br_port_msg)) +
506 nla_total_size(sizeof(struct br_mdb_entry)) +
507 nla_total_size(sizeof(u32));
508 struct net_bridge_group_src *ent;
509 size_t addr_size = 0;
514 /* MDBA_MDB_EATTR_RTPROT */
515 nlmsg_size += nla_total_size(sizeof(u8));
517 switch (pg->key.addr.proto) {
518 case htons(ETH_P_IP):
519 /* MDBA_MDB_EATTR_SOURCE */
520 if (pg->key.addr.src.ip4)
521 nlmsg_size += nla_total_size(sizeof(__be32));
522 if (pg->key.port->br->multicast_ctx.multicast_igmp_version == 2)
524 addr_size = sizeof(__be32);
526 #if IS_ENABLED(CONFIG_IPV6)
527 case htons(ETH_P_IPV6):
528 /* MDBA_MDB_EATTR_SOURCE */
529 if (!ipv6_addr_any(&pg->key.addr.src.ip6))
530 nlmsg_size += nla_total_size(sizeof(struct in6_addr));
531 if (pg->key.port->br->multicast_ctx.multicast_mld_version == 1)
533 addr_size = sizeof(struct in6_addr);
538 /* MDBA_MDB_EATTR_GROUP_MODE */
539 nlmsg_size += nla_total_size(sizeof(u8));
541 /* MDBA_MDB_EATTR_SRC_LIST nested attr */
542 if (!hlist_empty(&pg->src_list))
543 nlmsg_size += nla_total_size(0);
545 hlist_for_each_entry(ent, &pg->src_list, node) {
546 /* MDBA_MDB_SRCLIST_ENTRY nested attr +
547 * MDBA_MDB_SRCATTR_ADDRESS + MDBA_MDB_SRCATTR_TIMER
549 nlmsg_size += nla_total_size(0) +
550 nla_total_size(addr_size) +
551 nla_total_size(sizeof(u32));
557 void br_mdb_notify(struct net_device *dev,
558 struct net_bridge_mdb_entry *mp,
559 struct net_bridge_port_group *pg,
562 struct net *net = dev_net(dev);
566 br_switchdev_mdb_notify(dev, mp, pg, type);
568 skb = nlmsg_new(rtnl_mdb_nlmsg_size(pg), GFP_ATOMIC);
572 err = nlmsg_populate_mdb_fill(skb, dev, mp, pg, type);
578 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
581 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
584 static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
585 struct net_device *dev,
586 int ifindex, u16 vid, u32 pid,
587 u32 seq, int type, unsigned int flags)
589 struct nlattr *nest, *port_nest;
590 struct br_port_msg *bpm;
591 struct nlmsghdr *nlh;
593 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
597 bpm = nlmsg_data(nlh);
598 memset(bpm, 0, sizeof(*bpm));
599 bpm->family = AF_BRIDGE;
600 bpm->ifindex = dev->ifindex;
601 nest = nla_nest_start_noflag(skb, MDBA_ROUTER);
605 port_nest = nla_nest_start_noflag(skb, MDBA_ROUTER_PORT);
608 if (nla_put_nohdr(skb, sizeof(u32), &ifindex)) {
609 nla_nest_cancel(skb, port_nest);
612 if (vid && nla_put_u16(skb, MDBA_ROUTER_PATTR_VID, vid)) {
613 nla_nest_cancel(skb, port_nest);
616 nla_nest_end(skb, port_nest);
618 nla_nest_end(skb, nest);
623 nla_nest_end(skb, nest);
625 nlmsg_cancel(skb, nlh);
629 static inline size_t rtnl_rtr_nlmsg_size(void)
631 return NLMSG_ALIGN(sizeof(struct br_port_msg))
632 + nla_total_size(sizeof(__u32))
633 + nla_total_size(sizeof(u16));
636 void br_rtr_notify(struct net_device *dev, struct net_bridge_mcast_port *pmctx,
639 struct net *net = dev_net(dev);
645 ifindex = pmctx ? pmctx->port->dev->ifindex : 0;
646 vid = pmctx && br_multicast_port_ctx_is_vlan(pmctx) ? pmctx->vlan->vid :
648 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
652 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, vid, 0, 0, type,
659 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
663 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
666 static bool is_valid_mdb_entry(struct br_mdb_entry *entry,
667 struct netlink_ext_ack *extack)
669 if (entry->ifindex == 0) {
670 NL_SET_ERR_MSG_MOD(extack, "Zero entry ifindex is not allowed");
674 if (entry->addr.proto == htons(ETH_P_IP)) {
675 if (!ipv4_is_multicast(entry->addr.u.ip4)) {
676 NL_SET_ERR_MSG_MOD(extack, "IPv4 entry group address is not multicast");
679 if (ipv4_is_local_multicast(entry->addr.u.ip4)) {
680 NL_SET_ERR_MSG_MOD(extack, "IPv4 entry group address is local multicast");
683 #if IS_ENABLED(CONFIG_IPV6)
684 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
685 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6)) {
686 NL_SET_ERR_MSG_MOD(extack, "IPv6 entry group address is link-local all nodes");
690 } else if (entry->addr.proto == 0) {
692 if (!is_multicast_ether_addr(entry->addr.u.mac_addr)) {
693 NL_SET_ERR_MSG_MOD(extack, "L2 entry group is not multicast");
697 NL_SET_ERR_MSG_MOD(extack, "Unknown entry protocol");
701 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY) {
702 NL_SET_ERR_MSG_MOD(extack, "Unknown entry state");
705 if (entry->vid >= VLAN_VID_MASK) {
706 NL_SET_ERR_MSG_MOD(extack, "Invalid entry VLAN id");
713 static bool is_valid_mdb_source(struct nlattr *attr, __be16 proto,
714 struct netlink_ext_ack *extack)
717 case htons(ETH_P_IP):
718 if (nla_len(attr) != sizeof(struct in_addr)) {
719 NL_SET_ERR_MSG_MOD(extack, "IPv4 invalid source address length");
722 if (ipv4_is_multicast(nla_get_in_addr(attr))) {
723 NL_SET_ERR_MSG_MOD(extack, "IPv4 multicast source address is not allowed");
727 #if IS_ENABLED(CONFIG_IPV6)
728 case htons(ETH_P_IPV6): {
731 if (nla_len(attr) != sizeof(struct in6_addr)) {
732 NL_SET_ERR_MSG_MOD(extack, "IPv6 invalid source address length");
735 src = nla_get_in6_addr(attr);
736 if (ipv6_addr_is_multicast(&src)) {
737 NL_SET_ERR_MSG_MOD(extack, "IPv6 multicast source address is not allowed");
744 NL_SET_ERR_MSG_MOD(extack, "Invalid protocol used with source address");
751 static const struct nla_policy br_mdbe_attrs_pol[MDBE_ATTR_MAX + 1] = {
752 [MDBE_ATTR_SOURCE] = NLA_POLICY_RANGE(NLA_BINARY,
753 sizeof(struct in_addr),
754 sizeof(struct in6_addr)),
757 static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
758 struct net_device **pdev, struct br_mdb_entry **pentry,
759 struct nlattr **mdb_attrs, struct netlink_ext_ack *extack)
761 struct net *net = sock_net(skb->sk);
762 struct br_mdb_entry *entry;
763 struct br_port_msg *bpm;
764 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
765 struct net_device *dev;
768 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
769 MDBA_SET_ENTRY_MAX, NULL, NULL);
773 bpm = nlmsg_data(nlh);
774 if (bpm->ifindex == 0) {
775 NL_SET_ERR_MSG_MOD(extack, "Invalid bridge ifindex");
779 dev = __dev_get_by_index(net, bpm->ifindex);
781 NL_SET_ERR_MSG_MOD(extack, "Bridge device doesn't exist");
785 if (!netif_is_bridge_master(dev)) {
786 NL_SET_ERR_MSG_MOD(extack, "Device is not a bridge");
792 if (!tb[MDBA_SET_ENTRY]) {
793 NL_SET_ERR_MSG_MOD(extack, "Missing MDBA_SET_ENTRY attribute");
796 if (nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
797 NL_SET_ERR_MSG_MOD(extack, "Invalid MDBA_SET_ENTRY attribute length");
801 entry = nla_data(tb[MDBA_SET_ENTRY]);
802 if (!is_valid_mdb_entry(entry, extack))
806 if (tb[MDBA_SET_ENTRY_ATTRS]) {
807 err = nla_parse_nested(mdb_attrs, MDBE_ATTR_MAX,
808 tb[MDBA_SET_ENTRY_ATTRS],
809 br_mdbe_attrs_pol, extack);
812 if (mdb_attrs[MDBE_ATTR_SOURCE] &&
813 !is_valid_mdb_source(mdb_attrs[MDBE_ATTR_SOURCE],
814 entry->addr.proto, extack))
818 sizeof(struct nlattr *) * (MDBE_ATTR_MAX + 1));
824 static struct net_bridge_mcast *
825 __br_mdb_choose_context(struct net_bridge *br,
826 const struct br_mdb_entry *entry,
827 struct netlink_ext_ack *extack)
829 struct net_bridge_mcast *brmctx = NULL;
830 struct net_bridge_vlan *v;
832 if (!br_opt_get(br, BROPT_MCAST_VLAN_SNOOPING_ENABLED)) {
833 brmctx = &br->multicast_ctx;
838 NL_SET_ERR_MSG_MOD(extack, "Cannot add an entry without a vlan when vlan snooping is enabled");
842 v = br_vlan_find(br_vlan_group(br), entry->vid);
844 NL_SET_ERR_MSG_MOD(extack, "Vlan is not configured");
847 if (br_multicast_ctx_vlan_global_disabled(&v->br_mcast_ctx)) {
848 NL_SET_ERR_MSG_MOD(extack, "Vlan's multicast processing is disabled");
851 brmctx = &v->br_mcast_ctx;
856 static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
857 struct br_mdb_entry *entry,
858 struct nlattr **mdb_attrs,
859 struct netlink_ext_ack *extack)
861 struct net_bridge_mdb_entry *mp, *star_mp;
862 struct net_bridge_port_group __rcu **pp;
863 struct net_bridge_port_group *p;
864 struct net_bridge_mcast *brmctx;
865 struct br_ip group, star_group;
866 unsigned long now = jiffies;
867 unsigned char flags = 0;
871 __mdb_entry_to_br_ip(entry, &group, mdb_attrs);
873 brmctx = __br_mdb_choose_context(br, entry, extack);
877 /* host join errors which can happen before creating the group */
878 if (!port && !br_group_is_l2(&group)) {
879 /* don't allow any flags for host-joined IP groups */
881 NL_SET_ERR_MSG_MOD(extack, "Flags are not allowed for host groups");
884 if (!br_multicast_is_star_g(&group)) {
885 NL_SET_ERR_MSG_MOD(extack, "Groups with sources cannot be manually host joined");
890 if (br_group_is_l2(&group) && entry->state != MDB_PERMANENT) {
891 NL_SET_ERR_MSG_MOD(extack, "Only permanent L2 entries allowed");
895 mp = br_mdb_ip_get(br, &group);
897 mp = br_multicast_new_group(br, &group);
898 err = PTR_ERR_OR_ZERO(mp);
905 if (mp->host_joined) {
906 NL_SET_ERR_MSG_MOD(extack, "Group is already joined by host");
910 br_multicast_host_join(brmctx, mp, false);
911 br_mdb_notify(br->dev, mp, NULL, RTM_NEWMDB);
916 for (pp = &mp->ports;
917 (p = mlock_dereference(*pp, br)) != NULL;
919 if (p->key.port == port) {
920 NL_SET_ERR_MSG_MOD(extack, "Group is already joined by port");
923 if ((unsigned long)p->key.port < (unsigned long)port)
927 filter_mode = br_multicast_is_star_g(&group) ? MCAST_EXCLUDE :
930 if (entry->state == MDB_PERMANENT)
931 flags |= MDB_PG_FLAGS_PERMANENT;
933 p = br_multicast_new_port_group(port, &group, *pp, flags, NULL,
934 filter_mode, RTPROT_STATIC);
936 NL_SET_ERR_MSG_MOD(extack, "Couldn't allocate new port group");
939 rcu_assign_pointer(*pp, p);
940 if (entry->state == MDB_TEMPORARY)
942 now + brmctx->multicast_membership_interval);
943 br_mdb_notify(br->dev, mp, p, RTM_NEWMDB);
944 /* if we are adding a new EXCLUDE port group (*,G) it needs to be also
945 * added to all S,G entries for proper replication, if we are adding
946 * a new INCLUDE port (S,G) then all of *,G EXCLUDE ports need to be
947 * added to it for proper replication
949 if (br_multicast_should_handle_mode(brmctx, group.proto)) {
950 switch (filter_mode) {
952 br_multicast_star_g_handle_mode(p, MCAST_EXCLUDE);
955 star_group = p->key.addr;
956 memset(&star_group.src, 0, sizeof(star_group.src));
957 star_mp = br_mdb_ip_get(br, &star_group);
959 br_multicast_sg_add_exclude_ports(star_mp, p);
967 static int __br_mdb_add(struct net *net, struct net_bridge *br,
968 struct net_bridge_port *p,
969 struct br_mdb_entry *entry,
970 struct nlattr **mdb_attrs,
971 struct netlink_ext_ack *extack)
975 spin_lock_bh(&br->multicast_lock);
976 ret = br_mdb_add_group(br, p, entry, mdb_attrs, extack);
977 spin_unlock_bh(&br->multicast_lock);
982 static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
983 struct netlink_ext_ack *extack)
985 struct nlattr *mdb_attrs[MDBE_ATTR_MAX + 1];
986 struct net *net = sock_net(skb->sk);
987 struct net_bridge_vlan_group *vg;
988 struct net_bridge_port *p = NULL;
989 struct net_device *dev, *pdev;
990 struct br_mdb_entry *entry;
991 struct net_bridge_vlan *v;
992 struct net_bridge *br;
995 err = br_mdb_parse(skb, nlh, &dev, &entry, mdb_attrs, extack);
999 br = netdev_priv(dev);
1001 if (!netif_running(br->dev)) {
1002 NL_SET_ERR_MSG_MOD(extack, "Bridge device is not running");
1006 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED)) {
1007 NL_SET_ERR_MSG_MOD(extack, "Bridge's multicast processing is disabled");
1011 if (entry->ifindex != br->dev->ifindex) {
1012 pdev = __dev_get_by_index(net, entry->ifindex);
1014 NL_SET_ERR_MSG_MOD(extack, "Port net device doesn't exist");
1018 p = br_port_get_rtnl(pdev);
1020 NL_SET_ERR_MSG_MOD(extack, "Net device is not a bridge port");
1025 NL_SET_ERR_MSG_MOD(extack, "Port belongs to a different bridge device");
1028 if (p->state == BR_STATE_DISABLED && entry->state != MDB_PERMANENT) {
1029 NL_SET_ERR_MSG_MOD(extack, "Port is in disabled state and entry is not permanent");
1032 vg = nbp_vlan_group(p);
1034 vg = br_vlan_group(br);
1037 /* If vlan filtering is enabled and VLAN is not specified
1038 * install mdb entry on all vlans configured on the port.
1040 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
1041 list_for_each_entry(v, &vg->vlan_list, vlist) {
1042 entry->vid = v->vid;
1043 err = __br_mdb_add(net, br, p, entry, mdb_attrs, extack);
1048 err = __br_mdb_add(net, br, p, entry, mdb_attrs, extack);
1054 static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry,
1055 struct nlattr **mdb_attrs)
1057 struct net_bridge_mdb_entry *mp;
1058 struct net_bridge_port_group *p;
1059 struct net_bridge_port_group __rcu **pp;
1063 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
1066 __mdb_entry_to_br_ip(entry, &ip, mdb_attrs);
1068 spin_lock_bh(&br->multicast_lock);
1069 mp = br_mdb_ip_get(br, &ip);
1074 if (entry->ifindex == mp->br->dev->ifindex && mp->host_joined) {
1075 br_multicast_host_leave(mp, false);
1077 br_mdb_notify(br->dev, mp, NULL, RTM_DELMDB);
1078 if (!mp->ports && netif_running(br->dev))
1079 mod_timer(&mp->timer, jiffies);
1083 for (pp = &mp->ports;
1084 (p = mlock_dereference(*pp, br)) != NULL;
1086 if (!p->key.port || p->key.port->dev->ifindex != entry->ifindex)
1089 br_multicast_del_pg(mp, p, pp);
1095 spin_unlock_bh(&br->multicast_lock);
1099 static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
1100 struct netlink_ext_ack *extack)
1102 struct nlattr *mdb_attrs[MDBE_ATTR_MAX + 1];
1103 struct net *net = sock_net(skb->sk);
1104 struct net_bridge_vlan_group *vg;
1105 struct net_bridge_port *p = NULL;
1106 struct net_device *dev, *pdev;
1107 struct br_mdb_entry *entry;
1108 struct net_bridge_vlan *v;
1109 struct net_bridge *br;
1112 err = br_mdb_parse(skb, nlh, &dev, &entry, mdb_attrs, extack);
1116 br = netdev_priv(dev);
1118 if (entry->ifindex != br->dev->ifindex) {
1119 pdev = __dev_get_by_index(net, entry->ifindex);
1123 p = br_port_get_rtnl(pdev);
1125 NL_SET_ERR_MSG_MOD(extack, "Net device is not a bridge port");
1129 NL_SET_ERR_MSG_MOD(extack, "Port belongs to a different bridge device");
1132 vg = nbp_vlan_group(p);
1134 vg = br_vlan_group(br);
1137 /* If vlan filtering is enabled and VLAN is not specified
1138 * delete mdb entry on all vlans configured on the port.
1140 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
1141 list_for_each_entry(v, &vg->vlan_list, vlist) {
1142 entry->vid = v->vid;
1143 err = __br_mdb_del(br, entry, mdb_attrs);
1146 err = __br_mdb_del(br, entry, mdb_attrs);
1152 void br_mdb_init(void)
1154 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, 0);
1155 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, 0);
1156 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, 0);
1159 void br_mdb_uninit(void)
1161 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
1162 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
1163 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);