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)
12 #include <net/addrconf.h>
15 #include "br_private.h"
17 static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
18 struct net_device *dev)
20 struct net_bridge *br = netdev_priv(dev);
21 struct net_bridge_port *p;
24 if (!br->multicast_router || hlist_empty(&br->router_list))
27 nest = nla_nest_start(skb, MDBA_ROUTER);
31 hlist_for_each_entry_rcu(p, &br->router_list, rlist) {
32 if (p && nla_put_u32(skb, MDBA_ROUTER_PORT, p->dev->ifindex))
36 nla_nest_end(skb, nest);
39 nla_nest_cancel(skb, nest);
43 static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
44 struct net_device *dev)
46 struct net_bridge *br = netdev_priv(dev);
47 struct net_bridge_mdb_htable *mdb;
48 struct nlattr *nest, *nest2;
50 int idx = 0, s_idx = cb->args[1];
52 if (br->multicast_disabled)
55 mdb = rcu_dereference(br->mdb);
59 nest = nla_nest_start(skb, MDBA_MDB);
63 for (i = 0; i < mdb->max; i++) {
64 struct net_bridge_mdb_entry *mp;
65 struct net_bridge_port_group *p;
66 struct net_bridge_port_group __rcu **pp;
67 struct net_bridge_port *port;
69 hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) {
73 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
80 (p = rcu_dereference(*pp)) != NULL;
84 struct br_mdb_entry e;
85 memset(&e, 0, sizeof(e));
86 e.ifindex = port->dev->ifindex;
88 if (p->addr.proto == htons(ETH_P_IP))
89 e.addr.u.ip4 = p->addr.u.ip4;
90 #if IS_ENABLED(CONFIG_IPV6)
91 if (p->addr.proto == htons(ETH_P_IPV6))
92 e.addr.u.ip6 = p->addr.u.ip6;
94 e.addr.proto = p->addr.proto;
95 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(e), &e)) {
96 nla_nest_cancel(skb, nest2);
102 nla_nest_end(skb, nest2);
110 nla_nest_end(skb, nest);
114 static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
116 struct net_device *dev;
117 struct net *net = sock_net(skb->sk);
118 struct nlmsghdr *nlh = NULL;
125 /* In theory this could be wrapped to 0... */
126 cb->seq = net->dev_base_seq + br_mdb_rehash_seq;
128 for_each_netdev_rcu(net, dev) {
129 if (dev->priv_flags & IFF_EBRIDGE) {
130 struct br_port_msg *bpm;
135 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
136 cb->nlh->nlmsg_seq, RTM_GETMDB,
137 sizeof(*bpm), NLM_F_MULTI);
141 bpm = nlmsg_data(nlh);
142 memset(bpm, 0, sizeof(*bpm));
143 bpm->ifindex = dev->ifindex;
144 if (br_mdb_fill_info(skb, cb, dev) < 0)
146 if (br_rports_fill_info(skb, cb, dev) < 0)
164 static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
165 struct net_device *dev,
166 struct br_mdb_entry *entry, u32 pid,
167 u32 seq, int type, unsigned int flags)
169 struct nlmsghdr *nlh;
170 struct br_port_msg *bpm;
171 struct nlattr *nest, *nest2;
173 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI);
177 bpm = nlmsg_data(nlh);
178 memset(bpm, 0, sizeof(*bpm));
179 bpm->family = AF_BRIDGE;
180 bpm->ifindex = dev->ifindex;
181 nest = nla_nest_start(skb, MDBA_MDB);
184 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
188 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
191 nla_nest_end(skb, nest2);
192 nla_nest_end(skb, nest);
193 return nlmsg_end(skb, nlh);
196 nla_nest_end(skb, nest);
198 nlmsg_cancel(skb, nlh);
202 static inline size_t rtnl_mdb_nlmsg_size(void)
204 return NLMSG_ALIGN(sizeof(struct br_port_msg))
205 + nla_total_size(sizeof(struct br_mdb_entry));
208 static void __br_mdb_notify(struct net_device *dev, struct br_mdb_entry *entry,
211 struct net *net = dev_net(dev);
215 skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
219 err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
225 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
228 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
231 void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
232 struct br_ip *group, int type)
234 struct br_mdb_entry entry;
236 memset(&entry, 0, sizeof(entry));
237 entry.ifindex = port->dev->ifindex;
238 entry.addr.proto = group->proto;
239 entry.addr.u.ip4 = group->u.ip4;
240 #if IS_ENABLED(CONFIG_IPV6)
241 entry.addr.u.ip6 = group->u.ip6;
243 __br_mdb_notify(dev, &entry, type);
246 static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
248 if (entry->ifindex == 0)
251 if (entry->addr.proto == htons(ETH_P_IP)) {
252 if (!ipv4_is_multicast(entry->addr.u.ip4))
254 if (ipv4_is_local_multicast(entry->addr.u.ip4))
256 #if IS_ENABLED(CONFIG_IPV6)
257 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
258 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
263 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
269 static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
270 struct net_device **pdev, struct br_mdb_entry **pentry)
272 struct net *net = sock_net(skb->sk);
273 struct br_mdb_entry *entry;
274 struct br_port_msg *bpm;
275 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
276 struct net_device *dev;
279 err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY, NULL);
283 bpm = nlmsg_data(nlh);
284 if (bpm->ifindex == 0) {
285 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
289 dev = __dev_get_by_index(net, bpm->ifindex);
291 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
295 if (!(dev->priv_flags & IFF_EBRIDGE)) {
296 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
302 if (!tb[MDBA_SET_ENTRY] ||
303 nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
304 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
308 entry = nla_data(tb[MDBA_SET_ENTRY]);
309 if (!is_valid_mdb_entry(entry)) {
310 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
318 static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
319 struct br_ip *group, unsigned char state)
321 struct net_bridge_mdb_entry *mp;
322 struct net_bridge_port_group *p;
323 struct net_bridge_port_group __rcu **pp;
324 struct net_bridge_mdb_htable *mdb;
327 mdb = mlock_dereference(br->mdb, br);
328 mp = br_mdb_ip_get(mdb, group);
330 mp = br_multicast_new_group(br, port, group);
336 for (pp = &mp->ports;
337 (p = mlock_dereference(*pp, br)) != NULL;
341 if ((unsigned long)p->port < (unsigned long)port)
345 p = br_multicast_new_port_group(port, group, *pp, state);
348 rcu_assign_pointer(*pp, p);
350 br_mdb_notify(br->dev, port, group, RTM_NEWMDB);
354 static int __br_mdb_add(struct net *net, struct net_bridge *br,
355 struct br_mdb_entry *entry)
358 struct net_device *dev;
359 struct net_bridge_port *p;
362 if (!netif_running(br->dev) || br->multicast_disabled)
365 dev = __dev_get_by_index(net, entry->ifindex);
369 p = br_port_get_rtnl(dev);
370 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
373 ip.proto = entry->addr.proto;
374 if (ip.proto == htons(ETH_P_IP))
375 ip.u.ip4 = entry->addr.u.ip4;
376 #if IS_ENABLED(CONFIG_IPV6)
378 ip.u.ip6 = entry->addr.u.ip6;
381 spin_lock_bh(&br->multicast_lock);
382 ret = br_mdb_add_group(br, p, &ip, entry->state);
383 spin_unlock_bh(&br->multicast_lock);
387 static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
389 struct net *net = sock_net(skb->sk);
390 struct br_mdb_entry *entry;
391 struct net_device *dev;
392 struct net_bridge *br;
395 err = br_mdb_parse(skb, nlh, &dev, &entry);
399 br = netdev_priv(dev);
401 err = __br_mdb_add(net, br, entry);
403 __br_mdb_notify(dev, entry, RTM_NEWMDB);
407 static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
409 struct net_bridge_mdb_htable *mdb;
410 struct net_bridge_mdb_entry *mp;
411 struct net_bridge_port_group *p;
412 struct net_bridge_port_group __rcu **pp;
416 if (!netif_running(br->dev) || br->multicast_disabled)
419 ip.proto = entry->addr.proto;
420 if (ip.proto == htons(ETH_P_IP)) {
421 if (timer_pending(&br->ip4_querier.timer))
424 ip.u.ip4 = entry->addr.u.ip4;
425 #if IS_ENABLED(CONFIG_IPV6)
427 if (timer_pending(&br->ip6_querier.timer))
430 ip.u.ip6 = entry->addr.u.ip6;
434 spin_lock_bh(&br->multicast_lock);
435 mdb = mlock_dereference(br->mdb, br);
437 mp = br_mdb_ip_get(mdb, &ip);
441 for (pp = &mp->ports;
442 (p = mlock_dereference(*pp, br)) != NULL;
444 if (!p->port || p->port->dev->ifindex != entry->ifindex)
447 if (p->port->state == BR_STATE_DISABLED)
450 rcu_assign_pointer(*pp, p->next);
451 hlist_del_init(&p->mglist);
452 del_timer(&p->timer);
453 call_rcu_bh(&p->rcu, br_multicast_free_pg);
456 if (!mp->ports && !mp->mglist &&
457 netif_running(br->dev))
458 mod_timer(&mp->timer, jiffies);
463 spin_unlock_bh(&br->multicast_lock);
467 static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
469 struct net_device *dev;
470 struct br_mdb_entry *entry;
471 struct net_bridge *br;
474 err = br_mdb_parse(skb, nlh, &dev, &entry);
478 br = netdev_priv(dev);
480 err = __br_mdb_del(br, entry);
482 __br_mdb_notify(dev, entry, RTM_DELMDB);
486 void br_mdb_init(void)
488 rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, NULL);
489 rtnl_register(PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, NULL);
490 rtnl_register(PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, NULL);
493 void br_mdb_uninit(void)
495 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
496 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
497 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);