blob: 253bc77eda3bd1106021a7d93ba6854964f0a935 [file] [log] [blame]
Cong Wangee07c6e2012-12-07 00:04:48 +00001#include <linux/err.h>
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>
Cong Wangcfd56752012-12-11 22:23:08 +00007#include <linux/if_ether.h>
Cong Wangee07c6e2012-12-07 00:04:48 +00008#include <net/ip.h>
9#include <net/netlink.h>
Elad Razf1fecb12016-01-10 21:06:23 +010010#include <net/switchdev.h>
Cong Wangee07c6e2012-12-07 00:04:48 +000011#if IS_ENABLED(CONFIG_IPV6)
12#include <net/ipv6.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020013#include <net/addrconf.h>
Cong Wangee07c6e2012-12-07 00:04:48 +000014#endif
15
16#include "br_private.h"
17
18static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
19 struct net_device *dev)
20{
21 struct net_bridge *br = netdev_priv(dev);
22 struct net_bridge_port *p;
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010023 struct nlattr *nest, *port_nest;
Cong Wangee07c6e2012-12-07 00:04:48 +000024
25 if (!br->multicast_router || hlist_empty(&br->router_list))
26 return 0;
27
28 nest = nla_nest_start(skb, MDBA_ROUTER);
29 if (nest == NULL)
30 return -EMSGSIZE;
31
Sasha Levinb67bfe02013-02-27 17:06:00 -080032 hlist_for_each_entry_rcu(p, &br->router_list, rlist) {
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010033 if (!p)
34 continue;
35 port_nest = nla_nest_start(skb, MDBA_ROUTER_PORT);
36 if (!port_nest)
Cong Wangee07c6e2012-12-07 00:04:48 +000037 goto fail;
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010038 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);
44 goto fail;
45 }
46 nla_nest_end(skb, port_nest);
Cong Wangee07c6e2012-12-07 00:04:48 +000047 }
48
49 nla_nest_end(skb, nest);
50 return 0;
51fail:
52 nla_nest_cancel(skb, nest);
53 return -EMSGSIZE;
54}
55
Elad Raz9d06b6d2016-02-03 09:57:05 +010056static void __mdb_entry_fill_flags(struct br_mdb_entry *e, unsigned char flags)
57{
58 e->state = flags & MDB_PG_FLAGS_PERMANENT;
59 e->flags = 0;
60 if (flags & MDB_PG_FLAGS_OFFLOAD)
61 e->flags |= MDB_FLAGS_OFFLOAD;
62}
63
Cong Wangee07c6e2012-12-07 00:04:48 +000064static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
65 struct net_device *dev)
66{
67 struct net_bridge *br = netdev_priv(dev);
68 struct net_bridge_mdb_htable *mdb;
69 struct nlattr *nest, *nest2;
70 int i, err = 0;
71 int idx = 0, s_idx = cb->args[1];
72
73 if (br->multicast_disabled)
74 return 0;
75
76 mdb = rcu_dereference(br->mdb);
77 if (!mdb)
78 return 0;
79
80 nest = nla_nest_start(skb, MDBA_MDB);
81 if (nest == NULL)
82 return -EMSGSIZE;
83
84 for (i = 0; i < mdb->max; i++) {
Cong Wangee07c6e2012-12-07 00:04:48 +000085 struct net_bridge_mdb_entry *mp;
stephen hemminger762a3d82013-08-04 17:19:38 -070086 struct net_bridge_port_group *p;
87 struct net_bridge_port_group __rcu **pp;
Cong Wangee07c6e2012-12-07 00:04:48 +000088 struct net_bridge_port *port;
89
Sasha Levinb67bfe02013-02-27 17:06:00 -080090 hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) {
Cong Wangee07c6e2012-12-07 00:04:48 +000091 if (idx < s_idx)
92 goto skip;
93
94 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
95 if (nest2 == NULL) {
96 err = -EMSGSIZE;
97 goto out;
98 }
99
100 for (pp = &mp->ports;
101 (p = rcu_dereference(*pp)) != NULL;
102 pp = &p->next) {
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100103 struct nlattr *nest_ent;
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100104 struct br_mdb_entry e;
105
Cong Wangee07c6e2012-12-07 00:04:48 +0000106 port = p->port;
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100107 if (!port)
108 continue;
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100109
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100110 memset(&e, 0, sizeof(e));
111 e.ifindex = port->dev->ifindex;
112 e.vid = p->addr.vid;
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;
Cong Wangee07c6e2012-12-07 00:04:48 +0000116#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100117 if (p->addr.proto == htons(ETH_P_IPV6))
118 e.addr.u.ip6 = p->addr.u.ip6;
Cong Wangee07c6e2012-12-07 00:04:48 +0000119#endif
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100120 e.addr.proto = p->addr.proto;
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100121 nest_ent = nla_nest_start(skb,
122 MDBA_MDB_ENTRY_INFO);
123 if (!nest_ent) {
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100124 nla_nest_cancel(skb, nest2);
125 err = -EMSGSIZE;
126 goto out;
Cong Wangee07c6e2012-12-07 00:04:48 +0000127 }
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100128 if (nla_put_nohdr(skb, sizeof(e), &e) ||
129 nla_put_u32(skb,
130 MDBA_MDB_EATTR_TIMER,
131 br_timer_value(&p->timer))) {
132 nla_nest_cancel(skb, nest_ent);
133 nla_nest_cancel(skb, nest2);
134 err = -EMSGSIZE;
135 goto out;
136 }
137 nla_nest_end(skb, nest_ent);
Cong Wangee07c6e2012-12-07 00:04:48 +0000138 }
139 nla_nest_end(skb, nest2);
140 skip:
141 idx++;
142 }
143 }
144
145out:
146 cb->args[1] = idx;
147 nla_nest_end(skb, nest);
148 return err;
149}
150
151static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
152{
153 struct net_device *dev;
154 struct net *net = sock_net(skb->sk);
155 struct nlmsghdr *nlh = NULL;
156 int idx = 0, s_idx;
157
158 s_idx = cb->args[0];
159
160 rcu_read_lock();
161
Cong Wang2ce297f2012-12-10 02:15:35 +0000162 /* In theory this could be wrapped to 0... */
163 cb->seq = net->dev_base_seq + br_mdb_rehash_seq;
Cong Wangee07c6e2012-12-07 00:04:48 +0000164
165 for_each_netdev_rcu(net, dev) {
166 if (dev->priv_flags & IFF_EBRIDGE) {
167 struct br_port_msg *bpm;
168
169 if (idx < s_idx)
170 goto skip;
171
172 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
173 cb->nlh->nlmsg_seq, RTM_GETMDB,
174 sizeof(*bpm), NLM_F_MULTI);
175 if (nlh == NULL)
176 break;
177
178 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000179 memset(bpm, 0, sizeof(*bpm));
Cong Wangee07c6e2012-12-07 00:04:48 +0000180 bpm->ifindex = dev->ifindex;
181 if (br_mdb_fill_info(skb, cb, dev) < 0)
182 goto out;
183 if (br_rports_fill_info(skb, cb, dev) < 0)
184 goto out;
185
186 cb->args[1] = 0;
187 nlmsg_end(skb, nlh);
188 skip:
189 idx++;
190 }
191 }
192
193out:
194 if (nlh)
195 nlmsg_end(skb, nlh);
196 rcu_read_unlock();
197 cb->args[0] = idx;
198 return skb->len;
199}
200
Cong Wang37a393b2012-12-11 22:23:07 +0000201static 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)
205{
206 struct nlmsghdr *nlh;
207 struct br_port_msg *bpm;
208 struct nlattr *nest, *nest2;
209
Nicolas Dichtel82199672015-04-28 18:33:48 +0200210 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
Cong Wang37a393b2012-12-11 22:23:07 +0000211 if (!nlh)
212 return -EMSGSIZE;
213
214 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000215 memset(bpm, 0, sizeof(*bpm));
Cong Wang37a393b2012-12-11 22:23:07 +0000216 bpm->family = AF_BRIDGE;
217 bpm->ifindex = dev->ifindex;
218 nest = nla_nest_start(skb, MDBA_MDB);
219 if (nest == NULL)
220 goto cancel;
221 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
222 if (nest2 == NULL)
223 goto end;
224
225 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
226 goto end;
227
228 nla_nest_end(skb, nest2);
229 nla_nest_end(skb, nest);
Johannes Berg053c0952015-01-16 22:09:00 +0100230 nlmsg_end(skb, nlh);
231 return 0;
Cong Wang37a393b2012-12-11 22:23:07 +0000232
233end:
234 nla_nest_end(skb, nest);
235cancel:
236 nlmsg_cancel(skb, nlh);
237 return -EMSGSIZE;
238}
239
240static inline size_t rtnl_mdb_nlmsg_size(void)
241{
242 return NLMSG_ALIGN(sizeof(struct br_port_msg))
243 + nla_total_size(sizeof(struct br_mdb_entry));
244}
245
246static void __br_mdb_notify(struct net_device *dev, struct br_mdb_entry *entry,
Elad Raz9e8430f2016-02-03 09:57:06 +0100247 int type, struct net_bridge_port_group *pg)
Cong Wang37a393b2012-12-11 22:23:07 +0000248{
Elad Razf1fecb12016-01-10 21:06:23 +0100249 struct switchdev_obj_port_mdb mdb = {
250 .obj = {
251 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
252 .flags = SWITCHDEV_F_DEFER,
253 },
254 .vid = entry->vid,
255 };
256 struct net_device *port_dev;
Cong Wang37a393b2012-12-11 22:23:07 +0000257 struct net *net = dev_net(dev);
258 struct sk_buff *skb;
259 int err = -ENOBUFS;
260
Elad Razf1fecb12016-01-10 21:06:23 +0100261 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)
265 else
266 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
267#endif
268
269 mdb.obj.orig_dev = port_dev;
Elad Raz9e8430f2016-02-03 09:57:06 +0100270 if (port_dev && type == RTM_NEWMDB) {
271 err = switchdev_port_obj_add(port_dev, &mdb.obj);
272 if (!err && pg)
273 pg->flags |= MDB_PG_FLAGS_OFFLOAD;
274 } else if (port_dev && type == RTM_DELMDB) {
Elad Razf1fecb12016-01-10 21:06:23 +0100275 switchdev_port_obj_del(port_dev, &mdb.obj);
Elad Raz9e8430f2016-02-03 09:57:06 +0100276 }
Elad Razf1fecb12016-01-10 21:06:23 +0100277
Cong Wang37a393b2012-12-11 22:23:07 +0000278 skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
279 if (!skb)
280 goto errout;
281
282 err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
283 if (err < 0) {
284 kfree_skb(skb);
285 goto errout;
286 }
287
288 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
289 return;
290errout:
291 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
292}
293
Elad Raz9e8430f2016-02-03 09:57:06 +0100294void br_mdb_notify(struct net_device *dev, struct net_bridge_port_group *pg,
295 int type)
Cong Wang37a393b2012-12-11 22:23:07 +0000296{
297 struct br_mdb_entry entry;
298
Mathias Krausec085c492013-03-09 05:52:19 +0000299 memset(&entry, 0, sizeof(entry));
Elad Raz9e8430f2016-02-03 09:57:06 +0100300 entry.ifindex = pg->port->dev->ifindex;
301 entry.addr.proto = pg->addr.proto;
302 entry.addr.u.ip4 = pg->addr.u.ip4;
Cong Wang37a393b2012-12-11 22:23:07 +0000303#if IS_ENABLED(CONFIG_IPV6)
Elad Raz9e8430f2016-02-03 09:57:06 +0100304 entry.addr.u.ip6 = pg->addr.u.ip6;
Cong Wang37a393b2012-12-11 22:23:07 +0000305#endif
Elad Raz9e8430f2016-02-03 09:57:06 +0100306 entry.vid = pg->addr.vid;
307 __mdb_entry_fill_flags(&entry, pg->flags);
308 __br_mdb_notify(dev, &entry, type, pg);
Cong Wang37a393b2012-12-11 22:23:07 +0000309}
310
Satish Ashok949f1e32015-07-23 05:00:53 -0700311static 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)
315{
316 struct br_port_msg *bpm;
317 struct nlmsghdr *nlh;
318 struct nlattr *nest;
319
320 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI);
321 if (!nlh)
322 return -EMSGSIZE;
323
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);
329 if (!nest)
330 goto cancel;
331
332 if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex))
333 goto end;
334
335 nla_nest_end(skb, nest);
336 nlmsg_end(skb, nlh);
337 return 0;
338
339end:
340 nla_nest_end(skb, nest);
341cancel:
342 nlmsg_cancel(skb, nlh);
343 return -EMSGSIZE;
344}
345
346static inline size_t rtnl_rtr_nlmsg_size(void)
347{
348 return NLMSG_ALIGN(sizeof(struct br_port_msg))
349 + nla_total_size(sizeof(__u32));
350}
351
352void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
353 int type)
354{
355 struct net *net = dev_net(dev);
356 struct sk_buff *skb;
357 int err = -ENOBUFS;
358 int ifindex;
359
360 ifindex = port ? port->dev->ifindex : 0;
361 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
362 if (!skb)
363 goto errout;
364
365 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF);
366 if (err < 0) {
367 kfree_skb(skb);
368 goto errout;
369 }
370
371 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
372 return;
373
374errout:
375 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
376}
377
Cong Wangcfd56752012-12-11 22:23:08 +0000378static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
379{
380 if (entry->ifindex == 0)
381 return false;
382
383 if (entry->addr.proto == htons(ETH_P_IP)) {
384 if (!ipv4_is_multicast(entry->addr.u.ip4))
385 return false;
386 if (ipv4_is_local_multicast(entry->addr.u.ip4))
387 return false;
388#if IS_ENABLED(CONFIG_IPV6)
389 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200390 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
Cong Wangcfd56752012-12-11 22:23:08 +0000391 return false;
392#endif
393 } else
394 return false;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000395 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
396 return false;
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700397 if (entry->vid >= VLAN_VID_MASK)
398 return false;
Cong Wangcfd56752012-12-11 22:23:08 +0000399
400 return true;
401}
402
403static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
404 struct net_device **pdev, struct br_mdb_entry **pentry)
405{
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;
411 int err;
412
Nicolas Dichtel12d87252015-01-15 16:29:12 +0100413 err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000414 if (err < 0)
415 return err;
416
417 bpm = nlmsg_data(nlh);
418 if (bpm->ifindex == 0) {
419 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
420 return -EINVAL;
421 }
422
423 dev = __dev_get_by_index(net, bpm->ifindex);
424 if (dev == NULL) {
425 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
426 return -ENODEV;
427 }
428
429 if (!(dev->priv_flags & IFF_EBRIDGE)) {
430 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
431 return -EOPNOTSUPP;
432 }
433
434 *pdev = dev;
435
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");
439 return -EINVAL;
440 }
441
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");
445 return -EINVAL;
446 }
447
448 *pentry = entry;
449 return 0;
450}
451
452static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
Elad Raz9e8430f2016-02-03 09:57:06 +0100453 struct br_ip *group, unsigned char state,
454 struct net_bridge_port_group **pg)
Cong Wangcfd56752012-12-11 22:23:08 +0000455{
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;
Satish Ashokf7e29652015-07-06 05:53:35 -0700460 unsigned long now = jiffies;
Cong Wangcfd56752012-12-11 22:23:08 +0000461 int err;
462
463 mdb = mlock_dereference(br->mdb, br);
464 mp = br_mdb_ip_get(mdb, group);
465 if (!mp) {
466 mp = br_multicast_new_group(br, port, group);
Arnd Bergmann56bb7fd2016-02-10 16:09:02 +0100467 err = PTR_ERR_OR_ZERO(mp);
468 if (err)
Cong Wangcfd56752012-12-11 22:23:08 +0000469 return err;
470 }
471
472 for (pp = &mp->ports;
473 (p = mlock_dereference(*pp, br)) != NULL;
474 pp = &p->next) {
475 if (p->port == port)
476 return -EEXIST;
477 if ((unsigned long)p->port < (unsigned long)port)
478 break;
479 }
480
Amerigo Wangccb1c312012-12-14 22:09:51 +0000481 p = br_multicast_new_port_group(port, group, *pp, state);
Cong Wangcfd56752012-12-11 22:23:08 +0000482 if (unlikely(!p))
483 return -ENOMEM;
484 rcu_assign_pointer(*pp, p);
Elad Raz9e8430f2016-02-03 09:57:06 +0100485 *pg = p;
Satish Ashokf7e29652015-07-06 05:53:35 -0700486 if (state == MDB_TEMPORARY)
487 mod_timer(&p->timer, now + br->multicast_membership_interval);
Cong Wangcfd56752012-12-11 22:23:08 +0000488
Cong Wangcfd56752012-12-11 22:23:08 +0000489 return 0;
490}
491
492static int __br_mdb_add(struct net *net, struct net_bridge *br,
Elad Raz9e8430f2016-02-03 09:57:06 +0100493 struct br_mdb_entry *entry,
494 struct net_bridge_port_group **pg)
Cong Wangcfd56752012-12-11 22:23:08 +0000495{
496 struct br_ip ip;
497 struct net_device *dev;
498 struct net_bridge_port *p;
499 int ret;
500
501 if (!netif_running(br->dev) || br->multicast_disabled)
502 return -EINVAL;
503
504 dev = __dev_get_by_index(net, entry->ifindex);
505 if (!dev)
506 return -ENODEV;
507
508 p = br_port_get_rtnl(dev);
509 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
510 return -EINVAL;
511
Nikolay Aleksandrovf1158b72015-07-07 15:55:56 +0200512 memset(&ip, 0, sizeof(ip));
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700513 ip.vid = entry->vid;
Cong Wangcfd56752012-12-11 22:23:08 +0000514 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)
518 else
519 ip.u.ip6 = entry->addr.u.ip6;
520#endif
521
522 spin_lock_bh(&br->multicast_lock);
Elad Raz9e8430f2016-02-03 09:57:06 +0100523 ret = br_mdb_add_group(br, p, &ip, entry->state, pg);
Cong Wangcfd56752012-12-11 22:23:08 +0000524 spin_unlock_bh(&br->multicast_lock);
525 return ret;
526}
527
Thomas Graf661d2962013-03-21 07:45:29 +0000528static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
Cong Wangcfd56752012-12-11 22:23:08 +0000529{
530 struct net *net = sock_net(skb->sk);
Elad Raz9e8430f2016-02-03 09:57:06 +0100531 struct net_bridge_port_group *pg;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200532 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200533 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000534 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200535 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200536 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000537 struct net_bridge *br;
538 int err;
539
540 err = br_mdb_parse(skb, nlh, &dev, &entry);
541 if (err < 0)
542 return err;
543
544 br = netdev_priv(dev);
545
Satish Ashoke44deb22015-08-03 13:29:16 +0200546 /* If vlan filtering is enabled and VLAN is not specified
547 * install mdb entry on all vlans configured on the port.
548 */
549 pdev = __dev_get_by_index(net, entry->ifindex);
550 if (!pdev)
551 return -ENODEV;
552
553 p = br_port_get_rtnl(pdev);
554 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
555 return -EINVAL;
556
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200557 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) {
560 entry->vid = v->vid;
Elad Raz9e8430f2016-02-03 09:57:06 +0100561 err = __br_mdb_add(net, br, entry, &pg);
Satish Ashoke44deb22015-08-03 13:29:16 +0200562 if (err)
563 break;
Elad Raz9e8430f2016-02-03 09:57:06 +0100564 __br_mdb_notify(dev, entry, RTM_NEWMDB, pg);
Satish Ashoke44deb22015-08-03 13:29:16 +0200565 }
566 } else {
Elad Raz9e8430f2016-02-03 09:57:06 +0100567 err = __br_mdb_add(net, br, entry, &pg);
Satish Ashoke44deb22015-08-03 13:29:16 +0200568 if (!err)
Elad Raz9e8430f2016-02-03 09:57:06 +0100569 __br_mdb_notify(dev, entry, RTM_NEWMDB, pg);
Satish Ashoke44deb22015-08-03 13:29:16 +0200570 }
571
Cong Wangcfd56752012-12-11 22:23:08 +0000572 return err;
573}
574
575static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
576{
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;
581 struct br_ip ip;
582 int err = -EINVAL;
583
584 if (!netif_running(br->dev) || br->multicast_disabled)
585 return -EINVAL;
586
Nikolay Aleksandrovf1158b72015-07-07 15:55:56 +0200587 memset(&ip, 0, sizeof(ip));
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700588 ip.vid = entry->vid;
Cong Wangcfd56752012-12-11 22:23:08 +0000589 ip.proto = entry->addr.proto;
Satish Ashok51ed7f3e2015-07-09 04:12:45 -0700590 if (ip.proto == htons(ETH_P_IP))
Cong Wangcfd56752012-12-11 22:23:08 +0000591 ip.u.ip4 = entry->addr.u.ip4;
592#if IS_ENABLED(CONFIG_IPV6)
Satish Ashok51ed7f3e2015-07-09 04:12:45 -0700593 else
Cong Wangcfd56752012-12-11 22:23:08 +0000594 ip.u.ip6 = entry->addr.u.ip6;
595#endif
596
597 spin_lock_bh(&br->multicast_lock);
598 mdb = mlock_dereference(br->mdb, br);
599
600 mp = br_mdb_ip_get(mdb, &ip);
601 if (!mp)
602 goto unlock;
603
604 for (pp = &mp->ports;
605 (p = mlock_dereference(*pp, br)) != NULL;
606 pp = &p->next) {
607 if (!p->port || p->port->dev->ifindex != entry->ifindex)
608 continue;
609
610 if (p->port->state == BR_STATE_DISABLED)
611 goto unlock;
612
Elad Raz9d06b6d2016-02-03 09:57:05 +0100613 __mdb_entry_fill_flags(entry, p->flags);
Cong Wangcfd56752012-12-11 22:23:08 +0000614 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);
618 err = 0;
619
Linus Lüssing454594f2013-10-20 00:58:57 +0200620 if (!mp->ports && !mp->mglist &&
Cong Wangcfd56752012-12-11 22:23:08 +0000621 netif_running(br->dev))
622 mod_timer(&mp->timer, jiffies);
623 break;
624 }
625
626unlock:
627 spin_unlock_bh(&br->multicast_lock);
628 return err;
629}
630
Thomas Graf661d2962013-03-21 07:45:29 +0000631static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
Cong Wangcfd56752012-12-11 22:23:08 +0000632{
Satish Ashoke44deb22015-08-03 13:29:16 +0200633 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200634 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200635 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000636 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200637 struct net_bridge_port *p;
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200638 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000639 struct net_bridge *br;
640 int err;
641
642 err = br_mdb_parse(skb, nlh, &dev, &entry);
643 if (err < 0)
644 return err;
645
646 br = netdev_priv(dev);
647
Satish Ashoke44deb22015-08-03 13:29:16 +0200648 /* If vlan filtering is enabled and VLAN is not specified
649 * delete mdb entry on all vlans configured on the port.
650 */
651 pdev = __dev_get_by_index(net, entry->ifindex);
652 if (!pdev)
653 return -ENODEV;
654
655 p = br_port_get_rtnl(pdev);
656 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
657 return -EINVAL;
658
Nikolay Aleksandrov2594e902015-09-25 19:00:11 +0200659 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) {
662 entry->vid = v->vid;
Satish Ashoke44deb22015-08-03 13:29:16 +0200663 err = __br_mdb_del(br, entry);
664 if (!err)
Elad Raz9e8430f2016-02-03 09:57:06 +0100665 __br_mdb_notify(dev, entry, RTM_DELMDB, NULL);
Satish Ashoke44deb22015-08-03 13:29:16 +0200666 }
667 } else {
668 err = __br_mdb_del(br, entry);
669 if (!err)
Elad Raz9e8430f2016-02-03 09:57:06 +0100670 __br_mdb_notify(dev, entry, RTM_DELMDB, NULL);
Satish Ashoke44deb22015-08-03 13:29:16 +0200671 }
672
Cong Wangcfd56752012-12-11 22:23:08 +0000673 return err;
674}
675
Cong Wangee07c6e2012-12-07 00:04:48 +0000676void br_mdb_init(void)
677{
678 rtnl_register(PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000679 rtnl_register(PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, NULL);
680 rtnl_register(PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, NULL);
Cong Wangee07c6e2012-12-07 00:04:48 +0000681}
Vlad Yasevich63233152012-12-19 09:13:48 +0000682
683void br_mdb_uninit(void)
684{
685 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
686 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
687 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);
688}