blob: a7ea2d431714300bec42f4c28c6cdba46d22d2c6 [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{
81 struct net_bridge *br = netdev_priv(dev);
82 struct net_bridge_mdb_htable *mdb;
83 struct nlattr *nest, *nest2;
84 int i, err = 0;
85 int idx = 0, s_idx = cb->args[1];
86
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +030087 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangee07c6e2012-12-07 00:04:48 +000088 return 0;
89
90 mdb = rcu_dereference(br->mdb);
91 if (!mdb)
92 return 0;
93
94 nest = nla_nest_start(skb, MDBA_MDB);
95 if (nest == NULL)
96 return -EMSGSIZE;
97
98 for (i = 0; i < mdb->max; i++) {
Cong Wangee07c6e2012-12-07 00:04:48 +000099 struct net_bridge_mdb_entry *mp;
stephen hemminger762a3d82013-08-04 17:19:38 -0700100 struct net_bridge_port_group *p;
101 struct net_bridge_port_group __rcu **pp;
Cong Wangee07c6e2012-12-07 00:04:48 +0000102 struct net_bridge_port *port;
103
Sasha Levinb67bfe02013-02-27 17:06:00 -0800104 hlist_for_each_entry_rcu(mp, &mdb->mhash[i], hlist[mdb->ver]) {
Cong Wangee07c6e2012-12-07 00:04:48 +0000105 if (idx < s_idx)
106 goto skip;
107
108 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
109 if (nest2 == NULL) {
110 err = -EMSGSIZE;
111 goto out;
112 }
113
114 for (pp = &mp->ports;
115 (p = rcu_dereference(*pp)) != NULL;
116 pp = &p->next) {
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100117 struct nlattr *nest_ent;
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100118 struct br_mdb_entry e;
119
Cong Wangee07c6e2012-12-07 00:04:48 +0000120 port = p->port;
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100121 if (!port)
122 continue;
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100123
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100124 memset(&e, 0, sizeof(e));
125 e.ifindex = port->dev->ifindex;
126 e.vid = p->addr.vid;
127 __mdb_entry_fill_flags(&e, p->flags);
128 if (p->addr.proto == htons(ETH_P_IP))
129 e.addr.u.ip4 = p->addr.u.ip4;
Cong Wangee07c6e2012-12-07 00:04:48 +0000130#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100131 if (p->addr.proto == htons(ETH_P_IPV6))
132 e.addr.u.ip6 = p->addr.u.ip6;
Cong Wangee07c6e2012-12-07 00:04:48 +0000133#endif
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100134 e.addr.proto = p->addr.proto;
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100135 nest_ent = nla_nest_start(skb,
136 MDBA_MDB_ENTRY_INFO);
137 if (!nest_ent) {
Nikolay Aleksandrov76cc1732016-02-16 12:46:53 +0100138 nla_nest_cancel(skb, nest2);
139 err = -EMSGSIZE;
140 goto out;
Cong Wangee07c6e2012-12-07 00:04:48 +0000141 }
Nikolay Aleksandrov21257152016-02-16 12:46:54 +0100142 if (nla_put_nohdr(skb, sizeof(e), &e) ||
143 nla_put_u32(skb,
144 MDBA_MDB_EATTR_TIMER,
145 br_timer_value(&p->timer))) {
146 nla_nest_cancel(skb, nest_ent);
147 nla_nest_cancel(skb, nest2);
148 err = -EMSGSIZE;
149 goto out;
150 }
151 nla_nest_end(skb, nest_ent);
Cong Wangee07c6e2012-12-07 00:04:48 +0000152 }
153 nla_nest_end(skb, nest2);
154 skip:
155 idx++;
156 }
157 }
158
159out:
160 cb->args[1] = idx;
161 nla_nest_end(skb, nest);
162 return err;
163}
164
David Ahernc77b9362018-10-07 20:16:42 -0700165static int br_mdb_valid_dump_req(const struct nlmsghdr *nlh,
166 struct netlink_ext_ack *extack)
167{
168 struct br_port_msg *bpm;
169
170 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
171 NL_SET_ERR_MSG_MOD(extack, "Invalid header for mdb dump request");
172 return -EINVAL;
173 }
174
175 bpm = nlmsg_data(nlh);
176 if (bpm->ifindex) {
177 NL_SET_ERR_MSG_MOD(extack, "Filtering by device index is not supported for mdb dump request");
178 return -EINVAL;
179 }
180 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
181 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
182 return -EINVAL;
183 }
184
185 return 0;
186}
187
Cong Wangee07c6e2012-12-07 00:04:48 +0000188static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
189{
190 struct net_device *dev;
191 struct net *net = sock_net(skb->sk);
192 struct nlmsghdr *nlh = NULL;
193 int idx = 0, s_idx;
194
David Ahernc77b9362018-10-07 20:16:42 -0700195 if (cb->strict_check) {
196 int err = br_mdb_valid_dump_req(cb->nlh, cb->extack);
197
198 if (err < 0)
199 return err;
200 }
201
Cong Wangee07c6e2012-12-07 00:04:48 +0000202 s_idx = cb->args[0];
203
204 rcu_read_lock();
205
Cong Wang2ce297f2012-12-10 02:15:35 +0000206 /* In theory this could be wrapped to 0... */
207 cb->seq = net->dev_base_seq + br_mdb_rehash_seq;
Cong Wangee07c6e2012-12-07 00:04:48 +0000208
209 for_each_netdev_rcu(net, dev) {
210 if (dev->priv_flags & IFF_EBRIDGE) {
211 struct br_port_msg *bpm;
212
213 if (idx < s_idx)
214 goto skip;
215
216 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
217 cb->nlh->nlmsg_seq, RTM_GETMDB,
218 sizeof(*bpm), NLM_F_MULTI);
219 if (nlh == NULL)
220 break;
221
222 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000223 memset(bpm, 0, sizeof(*bpm));
Cong Wangee07c6e2012-12-07 00:04:48 +0000224 bpm->ifindex = dev->ifindex;
225 if (br_mdb_fill_info(skb, cb, dev) < 0)
226 goto out;
227 if (br_rports_fill_info(skb, cb, dev) < 0)
228 goto out;
229
230 cb->args[1] = 0;
231 nlmsg_end(skb, nlh);
232 skip:
233 idx++;
234 }
235 }
236
237out:
238 if (nlh)
239 nlmsg_end(skb, nlh);
240 rcu_read_unlock();
241 cb->args[0] = idx;
242 return skb->len;
243}
244
Cong Wang37a393b2012-12-11 22:23:07 +0000245static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
246 struct net_device *dev,
247 struct br_mdb_entry *entry, u32 pid,
248 u32 seq, int type, unsigned int flags)
249{
250 struct nlmsghdr *nlh;
251 struct br_port_msg *bpm;
252 struct nlattr *nest, *nest2;
253
Nicolas Dichtel82199672015-04-28 18:33:48 +0200254 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
Cong Wang37a393b2012-12-11 22:23:07 +0000255 if (!nlh)
256 return -EMSGSIZE;
257
258 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000259 memset(bpm, 0, sizeof(*bpm));
Cong Wang37a393b2012-12-11 22:23:07 +0000260 bpm->family = AF_BRIDGE;
261 bpm->ifindex = dev->ifindex;
262 nest = nla_nest_start(skb, MDBA_MDB);
263 if (nest == NULL)
264 goto cancel;
265 nest2 = nla_nest_start(skb, MDBA_MDB_ENTRY);
266 if (nest2 == NULL)
267 goto end;
268
269 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
270 goto end;
271
272 nla_nest_end(skb, nest2);
273 nla_nest_end(skb, nest);
Johannes Berg053c0952015-01-16 22:09:00 +0100274 nlmsg_end(skb, nlh);
275 return 0;
Cong Wang37a393b2012-12-11 22:23:07 +0000276
277end:
278 nla_nest_end(skb, nest);
279cancel:
280 nlmsg_cancel(skb, nlh);
281 return -EMSGSIZE;
282}
283
284static inline size_t rtnl_mdb_nlmsg_size(void)
285{
286 return NLMSG_ALIGN(sizeof(struct br_port_msg))
287 + nla_total_size(sizeof(struct br_mdb_entry));
288}
289
Elad Raz45ebcce2016-04-21 12:52:45 +0200290struct br_mdb_complete_info {
291 struct net_bridge_port *port;
292 struct br_ip ip;
293};
294
295static void br_mdb_complete(struct net_device *dev, int err, void *priv)
Cong Wang37a393b2012-12-11 22:23:07 +0000296{
Elad Raz45ebcce2016-04-21 12:52:45 +0200297 struct br_mdb_complete_info *data = priv;
298 struct net_bridge_port_group __rcu **pp;
299 struct net_bridge_port_group *p;
300 struct net_bridge_mdb_htable *mdb;
301 struct net_bridge_mdb_entry *mp;
302 struct net_bridge_port *port = data->port;
303 struct net_bridge *br = port->br;
304
305 if (err)
306 goto err;
307
308 spin_lock_bh(&br->multicast_lock);
309 mdb = mlock_dereference(br->mdb, br);
310 mp = br_mdb_ip_get(mdb, &data->ip);
311 if (!mp)
312 goto out;
313 for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
314 pp = &p->next) {
315 if (p->port != port)
316 continue;
317 p->flags |= MDB_PG_FLAGS_OFFLOAD;
318 }
319out:
320 spin_unlock_bh(&br->multicast_lock);
321err:
322 kfree(priv);
323}
324
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100325static void br_mdb_switchdev_host_port(struct net_device *dev,
326 struct net_device *lower_dev,
327 struct br_mdb_entry *entry, int type)
328{
329 struct switchdev_obj_port_mdb mdb = {
330 .obj = {
331 .id = SWITCHDEV_OBJ_ID_HOST_MDB,
332 .flags = SWITCHDEV_F_DEFER,
333 },
334 .vid = entry->vid,
335 };
336
337 if (entry->addr.proto == htons(ETH_P_IP))
338 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
339#if IS_ENABLED(CONFIG_IPV6)
340 else
341 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
342#endif
343
344 mdb.obj.orig_dev = dev;
345 switch (type) {
346 case RTM_NEWMDB:
347 switchdev_port_obj_add(lower_dev, &mdb.obj);
348 break;
349 case RTM_DELMDB:
350 switchdev_port_obj_del(lower_dev, &mdb.obj);
351 break;
352 }
353}
354
355static void br_mdb_switchdev_host(struct net_device *dev,
356 struct br_mdb_entry *entry, int type)
357{
358 struct net_device *lower_dev;
359 struct list_head *iter;
360
361 netdev_for_each_lower_dev(dev, lower_dev, iter)
362 br_mdb_switchdev_host_port(dev, lower_dev, entry, type);
363}
364
Elad Raz45ebcce2016-04-21 12:52:45 +0200365static void __br_mdb_notify(struct net_device *dev, struct net_bridge_port *p,
366 struct br_mdb_entry *entry, int type)
367{
368 struct br_mdb_complete_info *complete_info;
Elad Razf1fecb12016-01-10 21:06:23 +0100369 struct switchdev_obj_port_mdb mdb = {
370 .obj = {
371 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
372 .flags = SWITCHDEV_F_DEFER,
373 },
374 .vid = entry->vid,
375 };
376 struct net_device *port_dev;
Cong Wang37a393b2012-12-11 22:23:07 +0000377 struct net *net = dev_net(dev);
378 struct sk_buff *skb;
379 int err = -ENOBUFS;
380
Elad Razf1fecb12016-01-10 21:06:23 +0100381 port_dev = __dev_get_by_index(net, entry->ifindex);
382 if (entry->addr.proto == htons(ETH_P_IP))
383 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
384#if IS_ENABLED(CONFIG_IPV6)
385 else
386 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
387#endif
388
389 mdb.obj.orig_dev = port_dev;
Andrew Lunn2a260282017-11-09 23:10:58 +0100390 if (p && port_dev && type == RTM_NEWMDB) {
Elad Raz45ebcce2016-04-21 12:52:45 +0200391 complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
392 if (complete_info) {
393 complete_info->port = p;
394 __mdb_entry_to_br_ip(entry, &complete_info->ip);
395 mdb.obj.complete_priv = complete_info;
396 mdb.obj.complete = br_mdb_complete;
Eduardo Valentin1bfb1592017-07-11 14:55:12 -0700397 if (switchdev_port_obj_add(port_dev, &mdb.obj))
398 kfree(complete_info);
Elad Raz45ebcce2016-04-21 12:52:45 +0200399 }
Andrew Lunn2a260282017-11-09 23:10:58 +0100400 } else if (p && port_dev && type == RTM_DELMDB) {
Elad Razf1fecb12016-01-10 21:06:23 +0100401 switchdev_port_obj_del(port_dev, &mdb.obj);
Elad Raz9e8430f2016-02-03 09:57:06 +0100402 }
Elad Razf1fecb12016-01-10 21:06:23 +0100403
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100404 if (!p)
405 br_mdb_switchdev_host(dev, entry, type);
406
Cong Wang37a393b2012-12-11 22:23:07 +0000407 skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
408 if (!skb)
409 goto errout;
410
411 err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
412 if (err < 0) {
413 kfree_skb(skb);
414 goto errout;
415 }
416
417 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
418 return;
419errout:
420 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
421}
422
Elad Raz45ebcce2016-04-21 12:52:45 +0200423void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
424 struct br_ip *group, int type, u8 flags)
Cong Wang37a393b2012-12-11 22:23:07 +0000425{
426 struct br_mdb_entry entry;
427
Mathias Krausec085c492013-03-09 05:52:19 +0000428 memset(&entry, 0, sizeof(entry));
Andrew Lunn2a260282017-11-09 23:10:58 +0100429 if (port)
430 entry.ifindex = port->dev->ifindex;
431 else
432 entry.ifindex = dev->ifindex;
Elad Raz45ebcce2016-04-21 12:52:45 +0200433 entry.addr.proto = group->proto;
434 entry.addr.u.ip4 = group->u.ip4;
Cong Wang37a393b2012-12-11 22:23:07 +0000435#if IS_ENABLED(CONFIG_IPV6)
Elad Raz45ebcce2016-04-21 12:52:45 +0200436 entry.addr.u.ip6 = group->u.ip6;
Cong Wang37a393b2012-12-11 22:23:07 +0000437#endif
Elad Raz45ebcce2016-04-21 12:52:45 +0200438 entry.vid = group->vid;
439 __mdb_entry_fill_flags(&entry, flags);
440 __br_mdb_notify(dev, port, &entry, type);
Cong Wang37a393b2012-12-11 22:23:07 +0000441}
442
Satish Ashok949f1e32015-07-23 05:00:53 -0700443static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
444 struct net_device *dev,
445 int ifindex, u32 pid,
446 u32 seq, int type, unsigned int flags)
447{
448 struct br_port_msg *bpm;
449 struct nlmsghdr *nlh;
450 struct nlattr *nest;
451
452 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), NLM_F_MULTI);
453 if (!nlh)
454 return -EMSGSIZE;
455
456 bpm = nlmsg_data(nlh);
457 memset(bpm, 0, sizeof(*bpm));
458 bpm->family = AF_BRIDGE;
459 bpm->ifindex = dev->ifindex;
460 nest = nla_nest_start(skb, MDBA_ROUTER);
461 if (!nest)
462 goto cancel;
463
464 if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex))
465 goto end;
466
467 nla_nest_end(skb, nest);
468 nlmsg_end(skb, nlh);
469 return 0;
470
471end:
472 nla_nest_end(skb, nest);
473cancel:
474 nlmsg_cancel(skb, nlh);
475 return -EMSGSIZE;
476}
477
478static inline size_t rtnl_rtr_nlmsg_size(void)
479{
480 return NLMSG_ALIGN(sizeof(struct br_port_msg))
481 + nla_total_size(sizeof(__u32));
482}
483
484void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
485 int type)
486{
487 struct net *net = dev_net(dev);
488 struct sk_buff *skb;
489 int err = -ENOBUFS;
490 int ifindex;
491
492 ifindex = port ? port->dev->ifindex : 0;
493 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
494 if (!skb)
495 goto errout;
496
497 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF);
498 if (err < 0) {
499 kfree_skb(skb);
500 goto errout;
501 }
502
503 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
504 return;
505
506errout:
507 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
508}
509
Cong Wangcfd56752012-12-11 22:23:08 +0000510static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
511{
512 if (entry->ifindex == 0)
513 return false;
514
515 if (entry->addr.proto == htons(ETH_P_IP)) {
516 if (!ipv4_is_multicast(entry->addr.u.ip4))
517 return false;
518 if (ipv4_is_local_multicast(entry->addr.u.ip4))
519 return false;
520#if IS_ENABLED(CONFIG_IPV6)
521 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200522 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
Cong Wangcfd56752012-12-11 22:23:08 +0000523 return false;
524#endif
525 } else
526 return false;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000527 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
528 return false;
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700529 if (entry->vid >= VLAN_VID_MASK)
530 return false;
Cong Wangcfd56752012-12-11 22:23:08 +0000531
532 return true;
533}
534
535static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
536 struct net_device **pdev, struct br_mdb_entry **pentry)
537{
538 struct net *net = sock_net(skb->sk);
539 struct br_mdb_entry *entry;
540 struct br_port_msg *bpm;
541 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
542 struct net_device *dev;
543 int err;
544
Johannes Bergfceb6432017-04-12 14:34:07 +0200545 err = nlmsg_parse(nlh, sizeof(*bpm), tb, MDBA_SET_ENTRY_MAX, NULL,
546 NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000547 if (err < 0)
548 return err;
549
550 bpm = nlmsg_data(nlh);
551 if (bpm->ifindex == 0) {
552 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
553 return -EINVAL;
554 }
555
556 dev = __dev_get_by_index(net, bpm->ifindex);
557 if (dev == NULL) {
558 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
559 return -ENODEV;
560 }
561
562 if (!(dev->priv_flags & IFF_EBRIDGE)) {
563 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
564 return -EOPNOTSUPP;
565 }
566
567 *pdev = dev;
568
569 if (!tb[MDBA_SET_ENTRY] ||
570 nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
571 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
572 return -EINVAL;
573 }
574
575 entry = nla_data(tb[MDBA_SET_ENTRY]);
576 if (!is_valid_mdb_entry(entry)) {
577 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
578 return -EINVAL;
579 }
580
581 *pentry = entry;
582 return 0;
583}
584
585static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
Elad Raz45ebcce2016-04-21 12:52:45 +0200586 struct br_ip *group, unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000587{
588 struct net_bridge_mdb_entry *mp;
589 struct net_bridge_port_group *p;
590 struct net_bridge_port_group __rcu **pp;
591 struct net_bridge_mdb_htable *mdb;
Satish Ashokf7e29652015-07-06 05:53:35 -0700592 unsigned long now = jiffies;
Cong Wangcfd56752012-12-11 22:23:08 +0000593 int err;
594
595 mdb = mlock_dereference(br->mdb, br);
596 mp = br_mdb_ip_get(mdb, group);
597 if (!mp) {
598 mp = br_multicast_new_group(br, port, group);
Arnd Bergmann56bb7fd2016-02-10 16:09:02 +0100599 err = PTR_ERR_OR_ZERO(mp);
600 if (err)
Cong Wangcfd56752012-12-11 22:23:08 +0000601 return err;
602 }
603
604 for (pp = &mp->ports;
605 (p = mlock_dereference(*pp, br)) != NULL;
606 pp = &p->next) {
607 if (p->port == port)
608 return -EEXIST;
609 if ((unsigned long)p->port < (unsigned long)port)
610 break;
611 }
612
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100613 p = br_multicast_new_port_group(port, group, *pp, state, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000614 if (unlikely(!p))
615 return -ENOMEM;
616 rcu_assign_pointer(*pp, p);
Satish Ashokf7e29652015-07-06 05:53:35 -0700617 if (state == MDB_TEMPORARY)
618 mod_timer(&p->timer, now + br->multicast_membership_interval);
Cong Wangcfd56752012-12-11 22:23:08 +0000619
Cong Wangcfd56752012-12-11 22:23:08 +0000620 return 0;
621}
622
623static int __br_mdb_add(struct net *net, struct net_bridge *br,
Elad Raz45ebcce2016-04-21 12:52:45 +0200624 struct br_mdb_entry *entry)
Cong Wangcfd56752012-12-11 22:23:08 +0000625{
626 struct br_ip ip;
627 struct net_device *dev;
628 struct net_bridge_port *p;
629 int ret;
630
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300631 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000632 return -EINVAL;
633
634 dev = __dev_get_by_index(net, entry->ifindex);
635 if (!dev)
636 return -ENODEV;
637
638 p = br_port_get_rtnl(dev);
639 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
640 return -EINVAL;
641
Elad Raz6dd684c2016-04-21 12:52:44 +0200642 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000643
644 spin_lock_bh(&br->multicast_lock);
Elad Raz45ebcce2016-04-21 12:52:45 +0200645 ret = br_mdb_add_group(br, p, &ip, entry->state);
Cong Wangcfd56752012-12-11 22:23:08 +0000646 spin_unlock_bh(&br->multicast_lock);
647 return ret;
648}
649
David Ahernc21ef3e2017-04-16 09:48:24 -0700650static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
651 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000652{
653 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200654 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200655 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000656 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200657 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200658 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000659 struct net_bridge *br;
660 int err;
661
662 err = br_mdb_parse(skb, nlh, &dev, &entry);
663 if (err < 0)
664 return err;
665
666 br = netdev_priv(dev);
667
Satish Ashoke44deb22015-08-03 13:29:16 +0200668 /* If vlan filtering is enabled and VLAN is not specified
669 * install mdb entry on all vlans configured on the port.
670 */
671 pdev = __dev_get_by_index(net, entry->ifindex);
672 if (!pdev)
673 return -ENODEV;
674
675 p = br_port_get_rtnl(pdev);
676 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
677 return -EINVAL;
678
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200679 vg = nbp_vlan_group(p);
Ido Schimmel1f514452017-05-26 08:37:23 +0200680 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200681 list_for_each_entry(v, &vg->vlan_list, vlist) {
682 entry->vid = v->vid;
Elad Raz45ebcce2016-04-21 12:52:45 +0200683 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200684 if (err)
685 break;
Elad Raz45ebcce2016-04-21 12:52:45 +0200686 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200687 }
688 } else {
Elad Raz45ebcce2016-04-21 12:52:45 +0200689 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200690 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200691 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200692 }
693
Cong Wangcfd56752012-12-11 22:23:08 +0000694 return err;
695}
696
697static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
698{
699 struct net_bridge_mdb_htable *mdb;
700 struct net_bridge_mdb_entry *mp;
701 struct net_bridge_port_group *p;
702 struct net_bridge_port_group __rcu **pp;
703 struct br_ip ip;
704 int err = -EINVAL;
705
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300706 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000707 return -EINVAL;
708
Elad Raz6dd684c2016-04-21 12:52:44 +0200709 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000710
711 spin_lock_bh(&br->multicast_lock);
712 mdb = mlock_dereference(br->mdb, br);
713
714 mp = br_mdb_ip_get(mdb, &ip);
715 if (!mp)
716 goto unlock;
717
718 for (pp = &mp->ports;
719 (p = mlock_dereference(*pp, br)) != NULL;
720 pp = &p->next) {
721 if (!p->port || p->port->dev->ifindex != entry->ifindex)
722 continue;
723
724 if (p->port->state == BR_STATE_DISABLED)
725 goto unlock;
726
Elad Raz9d06b6d2016-02-03 09:57:05 +0100727 __mdb_entry_fill_flags(entry, p->flags);
Cong Wangcfd56752012-12-11 22:23:08 +0000728 rcu_assign_pointer(*pp, p->next);
729 hlist_del_init(&p->mglist);
730 del_timer(&p->timer);
731 call_rcu_bh(&p->rcu, br_multicast_free_pg);
732 err = 0;
733
Andrew Lunnff0fd342017-11-09 23:10:57 +0100734 if (!mp->ports && !mp->host_joined &&
Cong Wangcfd56752012-12-11 22:23:08 +0000735 netif_running(br->dev))
736 mod_timer(&mp->timer, jiffies);
737 break;
738 }
739
740unlock:
741 spin_unlock_bh(&br->multicast_lock);
742 return err;
743}
744
David Ahernc21ef3e2017-04-16 09:48:24 -0700745static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
746 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000747{
Satish Ashoke44deb22015-08-03 13:29:16 +0200748 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200749 struct net_bridge_vlan_group *vg;
Satish Ashoke44deb22015-08-03 13:29:16 +0200750 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000751 struct br_mdb_entry *entry;
Satish Ashoke44deb22015-08-03 13:29:16 +0200752 struct net_bridge_port *p;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200753 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000754 struct net_bridge *br;
755 int err;
756
757 err = br_mdb_parse(skb, nlh, &dev, &entry);
758 if (err < 0)
759 return err;
760
761 br = netdev_priv(dev);
762
Satish Ashoke44deb22015-08-03 13:29:16 +0200763 /* If vlan filtering is enabled and VLAN is not specified
764 * delete mdb entry on all vlans configured on the port.
765 */
766 pdev = __dev_get_by_index(net, entry->ifindex);
767 if (!pdev)
768 return -ENODEV;
769
770 p = br_port_get_rtnl(pdev);
771 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
772 return -EINVAL;
773
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200774 vg = nbp_vlan_group(p);
Ido Schimmel1f514452017-05-26 08:37:23 +0200775 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200776 list_for_each_entry(v, &vg->vlan_list, vlist) {
777 entry->vid = v->vid;
Satish Ashoke44deb22015-08-03 13:29:16 +0200778 err = __br_mdb_del(br, entry);
779 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200780 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200781 }
782 } else {
783 err = __br_mdb_del(br, entry);
784 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200785 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200786 }
787
Cong Wangcfd56752012-12-11 22:23:08 +0000788 return err;
789}
790
Cong Wangee07c6e2012-12-07 00:04:48 +0000791void br_mdb_init(void)
792{
Florian Westphalc1c502b2017-12-02 21:44:07 +0100793 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, 0);
794 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, 0);
795 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, 0);
Cong Wangee07c6e2012-12-07 00:04:48 +0000796}
Vlad Yasevich63233152012-12-19 09:13:48 +0000797
798void br_mdb_uninit(void)
799{
800 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
801 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
802 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);
803}