blob: f69c8d91dc8189773223080c4c8c8f8ef0924a36 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Cong Wangee07c6e2012-12-07 00:04:48 +00002#include <linux/err.h>
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>
Cong Wangcfd56752012-12-11 22:23:08 +00008#include <linux/if_ether.h>
Cong Wangee07c6e2012-12-07 00:04:48 +00009#include <net/ip.h>
10#include <net/netlink.h>
Elad Razf1fecb12016-01-10 21:06:23 +010011#include <net/switchdev.h>
Cong Wangee07c6e2012-12-07 00:04:48 +000012#if IS_ENABLED(CONFIG_IPV6)
13#include <net/ipv6.h>
Linus Lüssing3c3769e2013-09-04 02:13:39 +020014#include <net/addrconf.h>
Cong Wangee07c6e2012-12-07 00:04:48 +000015#endif
16
17#include "br_private.h"
18
19static int br_rports_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
20 struct net_device *dev)
21{
22 struct net_bridge *br = netdev_priv(dev);
23 struct net_bridge_port *p;
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010024 struct nlattr *nest, *port_nest;
Cong Wangee07c6e2012-12-07 00:04:48 +000025
26 if (!br->multicast_router || hlist_empty(&br->router_list))
27 return 0;
28
29 nest = nla_nest_start(skb, MDBA_ROUTER);
30 if (nest == NULL)
31 return -EMSGSIZE;
32
Sasha Levinb67bfe02013-02-27 17:06:00 -080033 hlist_for_each_entry_rcu(p, &br->router_list, rlist) {
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010034 if (!p)
35 continue;
36 port_nest = nla_nest_start(skb, MDBA_ROUTER_PORT);
37 if (!port_nest)
Cong Wangee07c6e2012-12-07 00:04:48 +000038 goto fail;
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010039 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);
45 goto fail;
46 }
47 nla_nest_end(skb, port_nest);
Cong Wangee07c6e2012-12-07 00:04:48 +000048 }
49
50 nla_nest_end(skb, nest);
51 return 0;
52fail:
53 nla_nest_cancel(skb, nest);
54 return -EMSGSIZE;
55}
56
Elad Raz9d06b6d2016-02-03 09:57:05 +010057static void __mdb_entry_fill_flags(struct br_mdb_entry *e, unsigned char flags)
58{
59 e->state = flags & MDB_PG_FLAGS_PERMANENT;
60 e->flags = 0;
61 if (flags & MDB_PG_FLAGS_OFFLOAD)
62 e->flags |= MDB_FLAGS_OFFLOAD;
63}
64
Elad Raz6dd684c2016-04-21 12:52:44 +020065static void __mdb_entry_to_br_ip(struct br_mdb_entry *entry, struct br_ip *ip)
66{
67 memset(ip, 0, sizeof(struct br_ip));
68 ip->vid = entry->vid;
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)
73 else
74 ip->u.ip6 = entry->addr.u.ip6;
75#endif
76}
77
Cong Wangee07c6e2012-12-07 00:04:48 +000078static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
79 struct net_device *dev)
80{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020081 int idx = 0, s_idx = cb->args[1], err = 0;
Cong Wangee07c6e2012-12-07 00:04:48 +000082 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020083 struct net_bridge_mdb_entry *mp;
Cong Wangee07c6e2012-12-07 00:04:48 +000084 struct nlattr *nest, *nest2;
Cong Wangee07c6e2012-12-07 00:04:48 +000085
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +030086 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangee07c6e2012-12-07 00:04:48 +000087 return 0;
88
Cong Wangee07c6e2012-12-07 00:04:48 +000089 nest = nla_nest_start(skb, MDBA_MDB);
90 if (nest == NULL)
91 return -EMSGSIZE;
92
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020093 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
stephen hemminger762a3d82013-08-04 17:19:38 -070094 struct net_bridge_port_group *p;
95 struct net_bridge_port_group __rcu **pp;
Cong Wangee07c6e2012-12-07 00:04:48 +000096 struct net_bridge_port *port;
97
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +020098 if (idx < s_idx)
99 goto skip;
Cong Wangee07c6e2012-12-07 00:04:48 +0000100
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200101 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
102 if (!nest2) {
103 err = -EMSGSIZE;
104 break;
105 }
106
107 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
108 pp = &p->next) {
109 struct nlattr *nest_ent;
110 struct br_mdb_entry e;
111
112 port = p->port;
113 if (!port)
114 continue;
115
116 memset(&e, 0, sizeof(e));
117 e.ifindex = port->dev->ifindex;
118 e.vid = p->addr.vid;
119 __mdb_entry_fill_flags(&e, p->flags);
120 if (p->addr.proto == htons(ETH_P_IP))
121 e.addr.u.ip4 = p->addr.u.ip4;
122#if IS_ENABLED(CONFIG_IPV6)
123 if (p->addr.proto == htons(ETH_P_IPV6))
124 e.addr.u.ip6 = p->addr.u.ip6;
125#endif
126 e.addr.proto = p->addr.proto;
127 nest_ent = nla_nest_start(skb, MDBA_MDB_ENTRY_INFO);
128 if (!nest_ent) {
129 nla_nest_cancel(skb, nest2);
Cong Wangee07c6e2012-12-07 00:04:48 +0000130 err = -EMSGSIZE;
131 goto out;
132 }
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200133 if (nla_put_nohdr(skb, sizeof(e), &e) ||
134 nla_put_u32(skb,
135 MDBA_MDB_EATTR_TIMER,
136 br_timer_value(&p->timer))) {
137 nla_nest_cancel(skb, nest_ent);
138 nla_nest_cancel(skb, nest2);
139 err = -EMSGSIZE;
140 goto out;
Cong Wangee07c6e2012-12-07 00:04:48 +0000141 }
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200142 nla_nest_end(skb, nest_ent);
Cong Wangee07c6e2012-12-07 00:04:48 +0000143 }
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200144 nla_nest_end(skb, nest2);
145skip:
146 idx++;
Cong Wangee07c6e2012-12-07 00:04:48 +0000147 }
148
149out:
150 cb->args[1] = idx;
151 nla_nest_end(skb, nest);
152 return err;
153}
154
David Ahernc77b9362018-10-07 20:16:42 -0700155static int br_mdb_valid_dump_req(const struct nlmsghdr *nlh,
156 struct netlink_ext_ack *extack)
157{
158 struct br_port_msg *bpm;
159
160 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
161 NL_SET_ERR_MSG_MOD(extack, "Invalid header for mdb dump request");
162 return -EINVAL;
163 }
164
165 bpm = nlmsg_data(nlh);
166 if (bpm->ifindex) {
167 NL_SET_ERR_MSG_MOD(extack, "Filtering by device index is not supported for mdb dump request");
168 return -EINVAL;
169 }
170 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
171 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
172 return -EINVAL;
173 }
174
175 return 0;
176}
177
Cong Wangee07c6e2012-12-07 00:04:48 +0000178static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
179{
180 struct net_device *dev;
181 struct net *net = sock_net(skb->sk);
182 struct nlmsghdr *nlh = NULL;
183 int idx = 0, s_idx;
184
David Ahernc77b9362018-10-07 20:16:42 -0700185 if (cb->strict_check) {
186 int err = br_mdb_valid_dump_req(cb->nlh, cb->extack);
187
188 if (err < 0)
189 return err;
190 }
191
Cong Wangee07c6e2012-12-07 00:04:48 +0000192 s_idx = cb->args[0];
193
194 rcu_read_lock();
195
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200196 cb->seq = net->dev_base_seq;
Cong Wangee07c6e2012-12-07 00:04:48 +0000197
198 for_each_netdev_rcu(net, dev) {
199 if (dev->priv_flags & IFF_EBRIDGE) {
200 struct br_port_msg *bpm;
201
202 if (idx < s_idx)
203 goto skip;
204
205 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
206 cb->nlh->nlmsg_seq, RTM_GETMDB,
207 sizeof(*bpm), NLM_F_MULTI);
208 if (nlh == NULL)
209 break;
210
211 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000212 memset(bpm, 0, sizeof(*bpm));
Cong Wangee07c6e2012-12-07 00:04:48 +0000213 bpm->ifindex = dev->ifindex;
214 if (br_mdb_fill_info(skb, cb, dev) < 0)
215 goto out;
216 if (br_rports_fill_info(skb, cb, dev) < 0)
217 goto out;
218
219 cb->args[1] = 0;
220 nlmsg_end(skb, nlh);
221 skip:
222 idx++;
223 }
224 }
225
226out:
227 if (nlh)
228 nlmsg_end(skb, nlh);
229 rcu_read_unlock();
230 cb->args[0] = idx;
231 return skb->len;
232}
233
Cong Wang37a393b2012-12-11 22:23:07 +0000234static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
235 struct net_device *dev,
236 struct br_mdb_entry *entry, u32 pid,
237 u32 seq, int type, unsigned int flags)
238{
239 struct nlmsghdr *nlh;
240 struct br_port_msg *bpm;
241 struct nlattr *nest, *nest2;
242
Nicolas Dichtel82199672015-04-28 18:33:48 +0200243 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
Cong Wang37a393b2012-12-11 22:23:07 +0000244 if (!nlh)
245 return -EMSGSIZE;
246
247 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000248 memset(bpm, 0, sizeof(*bpm));
Cong Wang37a393b2012-12-11 22:23:07 +0000249 bpm->family = AF_BRIDGE;
250 bpm->ifindex = dev->ifindex;
251 nest = nla_nest_start(skb, MDBA_MDB);
252 if (nest == NULL)
253 goto cancel;
254 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
255 if (nest2 == NULL)
256 goto end;
257
258 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
259 goto end;
260
261 nla_nest_end(skb, nest2);
262 nla_nest_end(skb, nest);
Johannes Berg053c0952015-01-16 22:09:00 +0100263 nlmsg_end(skb, nlh);
264 return 0;
Cong Wang37a393b2012-12-11 22:23:07 +0000265
266end:
267 nla_nest_end(skb, nest);
268cancel:
269 nlmsg_cancel(skb, nlh);
270 return -EMSGSIZE;
271}
272
273static inline size_t rtnl_mdb_nlmsg_size(void)
274{
275 return NLMSG_ALIGN(sizeof(struct br_port_msg))
276 + nla_total_size(sizeof(struct br_mdb_entry));
277}
278
Elad Raz45ebcce2016-04-21 12:52:45 +0200279struct br_mdb_complete_info {
280 struct net_bridge_port *port;
281 struct br_ip ip;
282};
283
284static void br_mdb_complete(struct net_device *dev, int err, void *priv)
Cong Wang37a393b2012-12-11 22:23:07 +0000285{
Elad Raz45ebcce2016-04-21 12:52:45 +0200286 struct br_mdb_complete_info *data = priv;
287 struct net_bridge_port_group __rcu **pp;
288 struct net_bridge_port_group *p;
Elad Raz45ebcce2016-04-21 12:52:45 +0200289 struct net_bridge_mdb_entry *mp;
290 struct net_bridge_port *port = data->port;
291 struct net_bridge *br = port->br;
292
293 if (err)
294 goto err;
295
296 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200297 mp = br_mdb_ip_get(br, &data->ip);
Elad Raz45ebcce2016-04-21 12:52:45 +0200298 if (!mp)
299 goto out;
300 for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
301 pp = &p->next) {
302 if (p->port != port)
303 continue;
304 p->flags |= MDB_PG_FLAGS_OFFLOAD;
305 }
306out:
307 spin_unlock_bh(&br->multicast_lock);
308err:
309 kfree(priv);
310}
311
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100312static void br_mdb_switchdev_host_port(struct net_device *dev,
313 struct net_device *lower_dev,
314 struct br_mdb_entry *entry, int type)
315{
316 struct switchdev_obj_port_mdb mdb = {
317 .obj = {
318 .id = SWITCHDEV_OBJ_ID_HOST_MDB,
319 .flags = SWITCHDEV_F_DEFER,
320 },
321 .vid = entry->vid,
322 };
323
324 if (entry->addr.proto == htons(ETH_P_IP))
325 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
326#if IS_ENABLED(CONFIG_IPV6)
327 else
328 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
329#endif
330
331 mdb.obj.orig_dev = dev;
332 switch (type) {
333 case RTM_NEWMDB:
Petr Machata69b73202018-12-12 17:02:52 +0000334 switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100335 break;
336 case RTM_DELMDB:
337 switchdev_port_obj_del(lower_dev, &mdb.obj);
338 break;
339 }
340}
341
342static void br_mdb_switchdev_host(struct net_device *dev,
343 struct br_mdb_entry *entry, int type)
344{
345 struct net_device *lower_dev;
346 struct list_head *iter;
347
348 netdev_for_each_lower_dev(dev, lower_dev, iter)
349 br_mdb_switchdev_host_port(dev, lower_dev, entry, type);
350}
351
Elad Raz45ebcce2016-04-21 12:52:45 +0200352static void __br_mdb_notify(struct net_device *dev, struct net_bridge_port *p,
353 struct br_mdb_entry *entry, int type)
354{
355 struct br_mdb_complete_info *complete_info;
Elad Razf1fecb12016-01-10 21:06:23 +0100356 struct switchdev_obj_port_mdb mdb = {
357 .obj = {
358 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
359 .flags = SWITCHDEV_F_DEFER,
360 },
361 .vid = entry->vid,
362 };
363 struct net_device *port_dev;
Cong Wang37a393b2012-12-11 22:23:07 +0000364 struct net *net = dev_net(dev);
365 struct sk_buff *skb;
366 int err = -ENOBUFS;
367
Elad Razf1fecb12016-01-10 21:06:23 +0100368 port_dev = __dev_get_by_index(net, entry->ifindex);
369 if (entry->addr.proto == htons(ETH_P_IP))
370 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
371#if IS_ENABLED(CONFIG_IPV6)
372 else
373 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
374#endif
375
376 mdb.obj.orig_dev = port_dev;
Andrew Lunn2a260282017-11-09 23:10:58 +0100377 if (p && port_dev && type == RTM_NEWMDB) {
Elad Raz45ebcce2016-04-21 12:52:45 +0200378 complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
379 if (complete_info) {
380 complete_info->port = p;
381 __mdb_entry_to_br_ip(entry, &complete_info->ip);
382 mdb.obj.complete_priv = complete_info;
383 mdb.obj.complete = br_mdb_complete;
Petr Machata69b73202018-12-12 17:02:52 +0000384 if (switchdev_port_obj_add(port_dev, &mdb.obj, NULL))
Eduardo Valentin1bfb1592017-07-11 14:55:12 -0700385 kfree(complete_info);
Elad Raz45ebcce2016-04-21 12:52:45 +0200386 }
Andrew Lunn2a260282017-11-09 23:10:58 +0100387 } else if (p && port_dev && type == RTM_DELMDB) {
Elad Razf1fecb12016-01-10 21:06:23 +0100388 switchdev_port_obj_del(port_dev, &mdb.obj);
Elad Raz9e8430f2016-02-03 09:57:06 +0100389 }
Elad Razf1fecb12016-01-10 21:06:23 +0100390
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100391 if (!p)
392 br_mdb_switchdev_host(dev, entry, type);
393
Cong Wang37a393b2012-12-11 22:23:07 +0000394 skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
395 if (!skb)
396 goto errout;
397
398 err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
399 if (err < 0) {
400 kfree_skb(skb);
401 goto errout;
402 }
403
404 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
405 return;
406errout:
407 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
408}
409
Elad Raz45ebcce2016-04-21 12:52:45 +0200410void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
411 struct br_ip *group, int type, u8 flags)
Cong Wang37a393b2012-12-11 22:23:07 +0000412{
413 struct br_mdb_entry entry;
414
Mathias Krausec085c492013-03-09 05:52:19 +0000415 memset(&entry, 0, sizeof(entry));
Andrew Lunn2a260282017-11-09 23:10:58 +0100416 if (port)
417 entry.ifindex = port->dev->ifindex;
418 else
419 entry.ifindex = dev->ifindex;
Elad Raz45ebcce2016-04-21 12:52:45 +0200420 entry.addr.proto = group->proto;
421 entry.addr.u.ip4 = group->u.ip4;
Cong Wang37a393b2012-12-11 22:23:07 +0000422#if IS_ENABLED(CONFIG_IPV6)
Elad Raz45ebcce2016-04-21 12:52:45 +0200423 entry.addr.u.ip6 = group->u.ip6;
Cong Wang37a393b2012-12-11 22:23:07 +0000424#endif
Elad Raz45ebcce2016-04-21 12:52:45 +0200425 entry.vid = group->vid;
426 __mdb_entry_fill_flags(&entry, flags);
427 __br_mdb_notify(dev, port, &entry, type);
Cong Wang37a393b2012-12-11 22:23:07 +0000428}
429
Satish Ashok949f1e32015-07-23 05:00:53 -0700430static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
431 struct net_device *dev,
432 int ifindex, u32 pid,
433 u32 seq, int type, unsigned int flags)
434{
435 struct br_port_msg *bpm;
436 struct nlmsghdr *nlh;
437 struct nlattr *nest;
438
439 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI);
440 if (!nlh)
441 return -EMSGSIZE;
442
443 bpm = nlmsg_data(nlh);
444 memset(bpm, 0, sizeof(*bpm));
445 bpm->family = AF_BRIDGE;
446 bpm->ifindex = dev->ifindex;
447 nest = nla_nest_start(skb, MDBA_ROUTER);
448 if (!nest)
449 goto cancel;
450
451 if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex))
452 goto end;
453
454 nla_nest_end(skb, nest);
455 nlmsg_end(skb, nlh);
456 return 0;
457
458end:
459 nla_nest_end(skb, nest);
460cancel:
461 nlmsg_cancel(skb, nlh);
462 return -EMSGSIZE;
463}
464
465static inline size_t rtnl_rtr_nlmsg_size(void)
466{
467 return NLMSG_ALIGN(sizeof(struct br_port_msg))
468 + nla_total_size(sizeof(__u32));
469}
470
471void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
472 int type)
473{
474 struct net *net = dev_net(dev);
475 struct sk_buff *skb;
476 int err = -ENOBUFS;
477 int ifindex;
478
479 ifindex = port ? port->dev->ifindex : 0;
480 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
481 if (!skb)
482 goto errout;
483
484 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF);
485 if (err < 0) {
486 kfree_skb(skb);
487 goto errout;
488 }
489
490 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
491 return;
492
493errout:
494 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
495}
496
Cong Wangcfd56752012-12-11 22:23:08 +0000497static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
498{
499 if (entry->ifindex == 0)
500 return false;
501
502 if (entry->addr.proto == htons(ETH_P_IP)) {
503 if (!ipv4_is_multicast(entry->addr.u.ip4))
504 return false;
505 if (ipv4_is_local_multicast(entry->addr.u.ip4))
506 return false;
507#if IS_ENABLED(CONFIG_IPV6)
508 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200509 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
Cong Wangcfd56752012-12-11 22:23:08 +0000510 return false;
511#endif
512 } else
513 return false;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000514 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
515 return false;
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700516 if (entry->vid >= VLAN_VID_MASK)
517 return false;
Cong Wangcfd56752012-12-11 22:23:08 +0000518
519 return true;
520}
521
522static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
523 struct net_device **pdev, struct br_mdb_entry **pentry)
524{
525 struct net *net = sock_net(skb->sk);
526 struct br_mdb_entry *entry;
527 struct br_port_msg *bpm;
528 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
529 struct net_device *dev;
530 int err;
531
Johannes Bergfceb6432017-04-12 14:34:07 +0200532 err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL,
533 NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000534 if (err < 0)
535 return err;
536
537 bpm = nlmsg_data(nlh);
538 if (bpm->ifindex == 0) {
539 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
540 return -EINVAL;
541 }
542
543 dev = __dev_get_by_index(net, bpm->ifindex);
544 if (dev == NULL) {
545 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
546 return -ENODEV;
547 }
548
549 if (!(dev->priv_flags & IFF_EBRIDGE)) {
550 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
551 return -EOPNOTSUPP;
552 }
553
554 *pdev = dev;
555
556 if (!tb[MDBA_SET_ENTRY] ||
557 nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
558 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
559 return -EINVAL;
560 }
561
562 entry = nla_data(tb[MDBA_SET_ENTRY]);
563 if (!is_valid_mdb_entry(entry)) {
564 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
565 return -EINVAL;
566 }
567
568 *pentry = entry;
569 return 0;
570}
571
572static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
Elad Raz45ebcce2016-04-21 12:52:45 +0200573 struct br_ip *group, unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000574{
575 struct net_bridge_mdb_entry *mp;
576 struct net_bridge_port_group *p;
577 struct net_bridge_port_group __rcu **pp;
Satish Ashokf7e29652015-07-06 05:53:35 -0700578 unsigned long now = jiffies;
Cong Wangcfd56752012-12-11 22:23:08 +0000579 int err;
580
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200581 mp = br_mdb_ip_get(br, group);
Cong Wangcfd56752012-12-11 22:23:08 +0000582 if (!mp) {
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200583 mp = br_multicast_new_group(br, group);
Arnd Bergmann56bb7fd2016-02-10 16:09:02 +0100584 err = PTR_ERR_OR_ZERO(mp);
585 if (err)
Cong Wangcfd56752012-12-11 22:23:08 +0000586 return err;
587 }
588
589 for (pp = &mp->ports;
590 (p = mlock_dereference(*pp, br)) != NULL;
591 pp = &p->next) {
592 if (p->port == port)
593 return -EEXIST;
594 if ((unsigned long)p->port < (unsigned long)port)
595 break;
596 }
597
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100598 p = br_multicast_new_port_group(port, group, *pp, state, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000599 if (unlikely(!p))
600 return -ENOMEM;
601 rcu_assign_pointer(*pp, p);
Satish Ashokf7e29652015-07-06 05:53:35 -0700602 if (state == MDB_TEMPORARY)
603 mod_timer(&p->timer, now + br->multicast_membership_interval);
Cong Wangcfd56752012-12-11 22:23:08 +0000604
Cong Wangcfd56752012-12-11 22:23:08 +0000605 return 0;
606}
607
608static int __br_mdb_add(struct net *net, struct net_bridge *br,
Elad Raz45ebcce2016-04-21 12:52:45 +0200609 struct br_mdb_entry *entry)
Cong Wangcfd56752012-12-11 22:23:08 +0000610{
611 struct br_ip ip;
612 struct net_device *dev;
613 struct net_bridge_port *p;
614 int ret;
615
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300616 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000617 return -EINVAL;
618
619 dev = __dev_get_by_index(net, entry->ifindex);
620 if (!dev)
621 return -ENODEV;
622
623 p = br_port_get_rtnl(dev);
624 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
625 return -EINVAL;
626
Elad Raz6dd684c2016-04-21 12:52:44 +0200627 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000628
629 spin_lock_bh(&br->multicast_lock);
Elad Raz45ebcce2016-04-21 12:52:45 +0200630 ret = br_mdb_add_group(br, p, &ip, entry->state);
Cong Wangcfd56752012-12-11 22:23:08 +0000631 spin_unlock_bh(&br->multicast_lock);
632 return ret;
633}
634
David Ahernc21ef3e2017-04-16 09:48:24 -0700635static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
636 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000637{
638 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200639 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200640 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000641 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200642 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200643 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000644 struct net_bridge *br;
645 int err;
646
647 err = br_mdb_parse(skb, nlh, &dev, &entry);
648 if (err < 0)
649 return err;
650
651 br = netdev_priv(dev);
652
Satish Ashoke44deb22015-08-03 13:29:16 +0200653 /* If vlan filtering is enabled and VLAN is not specified
654 * install mdb entry on all vlans configured on the port.
655 */
656 pdev = __dev_get_by_index(net, entry->ifindex);
657 if (!pdev)
658 return -ENODEV;
659
660 p = br_port_get_rtnl(pdev);
661 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
662 return -EINVAL;
663
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200664 vg = nbp_vlan_group(p);
Ido Schimmel1f514452017-05-26 08:37:23 +0200665 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200666 list_for_each_entry(v, &vg->vlan_list, vlist) {
667 entry->vid = v->vid;
Elad Raz45ebcce2016-04-21 12:52:45 +0200668 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200669 if (err)
670 break;
Elad Raz45ebcce2016-04-21 12:52:45 +0200671 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200672 }
673 } else {
Elad Raz45ebcce2016-04-21 12:52:45 +0200674 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200675 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200676 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200677 }
678
Cong Wangcfd56752012-12-11 22:23:08 +0000679 return err;
680}
681
682static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
683{
Cong Wangcfd56752012-12-11 22:23:08 +0000684 struct net_bridge_mdb_entry *mp;
685 struct net_bridge_port_group *p;
686 struct net_bridge_port_group __rcu **pp;
687 struct br_ip ip;
688 int err = -EINVAL;
689
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300690 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000691 return -EINVAL;
692
Elad Raz6dd684c2016-04-21 12:52:44 +0200693 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000694
695 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200696 mp = br_mdb_ip_get(br, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000697 if (!mp)
698 goto unlock;
699
700 for (pp = &mp->ports;
701 (p = mlock_dereference(*pp, br)) != NULL;
702 pp = &p->next) {
703 if (!p->port || p->port->dev->ifindex != entry->ifindex)
704 continue;
705
706 if (p->port->state == BR_STATE_DISABLED)
707 goto unlock;
708
Elad Raz9d06b6d2016-02-03 09:57:05 +0100709 __mdb_entry_fill_flags(entry, p->flags);
Cong Wangcfd56752012-12-11 22:23:08 +0000710 rcu_assign_pointer(*pp, p->next);
711 hlist_del_init(&p->mglist);
712 del_timer(&p->timer);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200713 kfree_rcu(p, rcu);
Cong Wangcfd56752012-12-11 22:23:08 +0000714 err = 0;
715
Andrew Lunnff0fd342017-11-09 23:10:57 +0100716 if (!mp->ports && !mp->host_joined &&
Cong Wangcfd56752012-12-11 22:23:08 +0000717 netif_running(br->dev))
718 mod_timer(&mp->timer, jiffies);
719 break;
720 }
721
722unlock:
723 spin_unlock_bh(&br->multicast_lock);
724 return err;
725}
726
David Ahernc21ef3e2017-04-16 09:48:24 -0700727static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
728 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000729{
Satish Ashoke44deb22015-08-03 13:29:16 +0200730 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200731 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200732 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000733 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200734 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200735 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000736 struct net_bridge *br;
737 int err;
738
739 err = br_mdb_parse(skb, nlh, &dev, &entry);
740 if (err < 0)
741 return err;
742
743 br = netdev_priv(dev);
744
Satish Ashoke44deb22015-08-03 13:29:16 +0200745 /* If vlan filtering is enabled and VLAN is not specified
746 * delete mdb entry on all vlans configured on the port.
747 */
748 pdev = __dev_get_by_index(net, entry->ifindex);
749 if (!pdev)
750 return -ENODEV;
751
752 p = br_port_get_rtnl(pdev);
753 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
754 return -EINVAL;
755
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200756 vg = nbp_vlan_group(p);
Ido Schimmel1f514452017-05-26 08:37:23 +0200757 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200758 list_for_each_entry(v, &vg->vlan_list, vlist) {
759 entry->vid = v->vid;
Satish Ashoke44deb22015-08-03 13:29:16 +0200760 err = __br_mdb_del(br, entry);
761 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200762 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200763 }
764 } else {
765 err = __br_mdb_del(br, entry);
766 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200767 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200768 }
769
Cong Wangcfd56752012-12-11 22:23:08 +0000770 return err;
771}
772
Cong Wangee07c6e2012-12-07 00:04:48 +0000773void br_mdb_init(void)
774{
Florian Westphalc1c502b2017-12-02 21:44:07 +0100775 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, 0);
776 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, 0);
777 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, 0);
Cong Wangee07c6e2012-12-07 00:04:48 +0000778}
Vlad Yasevich63233152012-12-19 09:13:48 +0000779
780void br_mdb_uninit(void)
781{
782 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
783 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
784 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);
785}