blob: da5ed4cf92333b6a61bc775e7a9bf0678c6826c6 [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
Michal Kubecekae0be8d2019-04-26 11:13:06 +020029 nest = nla_nest_start_noflag(skb, MDBA_ROUTER);
Cong Wangee07c6e2012-12-07 00:04:48 +000030 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;
Michal Kubecekae0be8d2019-04-26 11:13:06 +020036 port_nest = nla_nest_start_noflag(skb, MDBA_ROUTER_PORT);
Nikolay Aleksandrov59f78f92016-02-26 21:20:04 +010037 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;
Nikolay Aleksandrov3247b272019-07-30 15:20:41 +030063 if (flags & MDB_PG_FLAGS_FAST_LEAVE)
64 e->flags |= MDB_FLAGS_FAST_LEAVE;
Elad Raz9d06b6d2016-02-03 09:57:05 +010065}
66
Elad Raz6dd684c2016-04-21 12:52:44 +020067static void __mdb_entry_to_br_ip(struct br_mdb_entry *entry, struct br_ip *ip)
68{
69 memset(ip, 0, sizeof(struct br_ip));
70 ip->vid = entry->vid;
71 ip->proto = entry->addr.proto;
72 if (ip->proto == htons(ETH_P_IP))
73 ip->u.ip4 = entry->addr.u.ip4;
74#if IS_ENABLED(CONFIG_IPV6)
75 else
76 ip->u.ip6 = entry->addr.u.ip6;
77#endif
78}
79
Nikolay Aleksandrov65459162019-08-17 14:22:11 +030080static int __mdb_fill_info(struct sk_buff *skb,
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +030081 struct net_bridge_mdb_entry *mp,
Nikolay Aleksandrov65459162019-08-17 14:22:11 +030082 struct net_bridge_port_group *p)
83{
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +030084 struct timer_list *mtimer;
Nikolay Aleksandrov65459162019-08-17 14:22:11 +030085 struct nlattr *nest_ent;
86 struct br_mdb_entry e;
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +030087 u8 flags = 0;
88 int ifindex;
Nikolay Aleksandrov65459162019-08-17 14:22:11 +030089
90 memset(&e, 0, sizeof(e));
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +030091 if (p) {
92 ifindex = p->port->dev->ifindex;
93 mtimer = &p->timer;
94 flags = p->flags;
95 } else {
96 ifindex = mp->br->dev->ifindex;
97 mtimer = &mp->timer;
98 }
99
100 __mdb_entry_fill_flags(&e, flags);
101 e.ifindex = ifindex;
102 e.vid = mp->addr.vid;
103 if (mp->addr.proto == htons(ETH_P_IP))
104 e.addr.u.ip4 = mp->addr.u.ip4;
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300105#if IS_ENABLED(CONFIG_IPV6)
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +0300106 if (mp->addr.proto == htons(ETH_P_IPV6))
107 e.addr.u.ip6 = mp->addr.u.ip6;
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300108#endif
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +0300109 e.addr.proto = mp->addr.proto;
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300110 nest_ent = nla_nest_start_noflag(skb,
111 MDBA_MDB_ENTRY_INFO);
112 if (!nest_ent)
113 return -EMSGSIZE;
114
115 if (nla_put_nohdr(skb, sizeof(e), &e) ||
116 nla_put_u32(skb,
117 MDBA_MDB_EATTR_TIMER,
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +0300118 br_timer_value(mtimer))) {
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300119 nla_nest_cancel(skb, nest_ent);
120 return -EMSGSIZE;
121 }
122 nla_nest_end(skb, nest_ent);
123
124 return 0;
125}
126
Cong Wangee07c6e2012-12-07 00:04:48 +0000127static int br_mdb_fill_info(struct sk_buff *skb, struct netlink_callback *cb,
128 struct net_device *dev)
129{
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200130 int idx = 0, s_idx = cb->args[1], err = 0;
Cong Wangee07c6e2012-12-07 00:04:48 +0000131 struct net_bridge *br = netdev_priv(dev);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200132 struct net_bridge_mdb_entry *mp;
Cong Wangee07c6e2012-12-07 00:04:48 +0000133 struct nlattr *nest, *nest2;
Cong Wangee07c6e2012-12-07 00:04:48 +0000134
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300135 if (!br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangee07c6e2012-12-07 00:04:48 +0000136 return 0;
137
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200138 nest = nla_nest_start_noflag(skb, MDBA_MDB);
Cong Wangee07c6e2012-12-07 00:04:48 +0000139 if (nest == NULL)
140 return -EMSGSIZE;
141
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200142 hlist_for_each_entry_rcu(mp, &br->mdb_list, mdb_node) {
stephen hemminger762a3d82013-08-04 17:19:38 -0700143 struct net_bridge_port_group *p;
144 struct net_bridge_port_group __rcu **pp;
Cong Wangee07c6e2012-12-07 00:04:48 +0000145
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200146 if (idx < s_idx)
147 goto skip;
Cong Wangee07c6e2012-12-07 00:04:48 +0000148
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200149 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200150 if (!nest2) {
151 err = -EMSGSIZE;
152 break;
153 }
154
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +0300155 if (mp->host_joined) {
156 err = __mdb_fill_info(skb, mp, NULL);
157 if (err) {
158 nla_nest_cancel(skb, nest2);
159 break;
160 }
161 }
162
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200163 for (pp = &mp->ports; (p = rcu_dereference(*pp)) != NULL;
164 pp = &p->next) {
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300165 if (!p->port)
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200166 continue;
167
Nikolay Aleksandrove77b0c82019-08-17 14:22:12 +0300168 err = __mdb_fill_info(skb, mp, p);
Nikolay Aleksandrov65459162019-08-17 14:22:11 +0300169 if (err) {
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200170 nla_nest_cancel(skb, nest2);
Cong Wangee07c6e2012-12-07 00:04:48 +0000171 goto out;
172 }
Cong Wangee07c6e2012-12-07 00:04:48 +0000173 }
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200174 nla_nest_end(skb, nest2);
175skip:
176 idx++;
Cong Wangee07c6e2012-12-07 00:04:48 +0000177 }
178
179out:
180 cb->args[1] = idx;
181 nla_nest_end(skb, nest);
182 return err;
183}
184
David Ahernc77b9362018-10-07 20:16:42 -0700185static int br_mdb_valid_dump_req(const struct nlmsghdr *nlh,
186 struct netlink_ext_ack *extack)
187{
188 struct br_port_msg *bpm;
189
190 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*bpm))) {
191 NL_SET_ERR_MSG_MOD(extack, "Invalid header for mdb dump request");
192 return -EINVAL;
193 }
194
195 bpm = nlmsg_data(nlh);
196 if (bpm->ifindex) {
197 NL_SET_ERR_MSG_MOD(extack, "Filtering by device index is not supported for mdb dump request");
198 return -EINVAL;
199 }
200 if (nlmsg_attrlen(nlh, sizeof(*bpm))) {
201 NL_SET_ERR_MSG(extack, "Invalid data after header in mdb dump request");
202 return -EINVAL;
203 }
204
205 return 0;
206}
207
Cong Wangee07c6e2012-12-07 00:04:48 +0000208static int br_mdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
209{
210 struct net_device *dev;
211 struct net *net = sock_net(skb->sk);
212 struct nlmsghdr *nlh = NULL;
213 int idx = 0, s_idx;
214
David Ahernc77b9362018-10-07 20:16:42 -0700215 if (cb->strict_check) {
216 int err = br_mdb_valid_dump_req(cb->nlh, cb->extack);
217
218 if (err < 0)
219 return err;
220 }
221
Cong Wangee07c6e2012-12-07 00:04:48 +0000222 s_idx = cb->args[0];
223
224 rcu_read_lock();
225
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200226 cb->seq = net->dev_base_seq;
Cong Wangee07c6e2012-12-07 00:04:48 +0000227
228 for_each_netdev_rcu(net, dev) {
229 if (dev->priv_flags & IFF_EBRIDGE) {
230 struct br_port_msg *bpm;
231
232 if (idx < s_idx)
233 goto skip;
234
235 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
236 cb->nlh->nlmsg_seq, RTM_GETMDB,
237 sizeof(*bpm), NLM_F_MULTI);
238 if (nlh == NULL)
239 break;
240
241 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000242 memset(bpm, 0, sizeof(*bpm));
Cong Wangee07c6e2012-12-07 00:04:48 +0000243 bpm->ifindex = dev->ifindex;
244 if (br_mdb_fill_info(skb, cb, dev) < 0)
245 goto out;
246 if (br_rports_fill_info(skb, cb, dev) < 0)
247 goto out;
248
249 cb->args[1] = 0;
250 nlmsg_end(skb, nlh);
251 skip:
252 idx++;
253 }
254 }
255
256out:
257 if (nlh)
258 nlmsg_end(skb, nlh);
259 rcu_read_unlock();
260 cb->args[0] = idx;
261 return skb->len;
262}
263
Cong Wang37a393b2012-12-11 22:23:07 +0000264static int nlmsg_populate_mdb_fill(struct sk_buff *skb,
265 struct net_device *dev,
266 struct br_mdb_entry *entry, u32 pid,
267 u32 seq, int type, unsigned int flags)
268{
269 struct nlmsghdr *nlh;
270 struct br_port_msg *bpm;
271 struct nlattr *nest, *nest2;
272
Nicolas Dichtel82199672015-04-28 18:33:48 +0200273 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
Cong Wang37a393b2012-12-11 22:23:07 +0000274 if (!nlh)
275 return -EMSGSIZE;
276
277 bpm = nlmsg_data(nlh);
Mathias Krausec085c492013-03-09 05:52:19 +0000278 memset(bpm, 0, sizeof(*bpm));
Cong Wang37a393b2012-12-11 22:23:07 +0000279 bpm->family = AF_BRIDGE;
280 bpm->ifindex = dev->ifindex;
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200281 nest = nla_nest_start_noflag(skb, MDBA_MDB);
Cong Wang37a393b2012-12-11 22:23:07 +0000282 if (nest == NULL)
283 goto cancel;
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200284 nest2 = nla_nest_start_noflag(skb, MDBA_MDB_ENTRY);
Cong Wang37a393b2012-12-11 22:23:07 +0000285 if (nest2 == NULL)
286 goto end;
287
288 if (nla_put(skb, MDBA_MDB_ENTRY_INFO, sizeof(*entry), entry))
289 goto end;
290
291 nla_nest_end(skb, nest2);
292 nla_nest_end(skb, nest);
Johannes Berg053c0952015-01-16 22:09:00 +0100293 nlmsg_end(skb, nlh);
294 return 0;
Cong Wang37a393b2012-12-11 22:23:07 +0000295
296end:
297 nla_nest_end(skb, nest);
298cancel:
299 nlmsg_cancel(skb, nlh);
300 return -EMSGSIZE;
301}
302
303static inline size_t rtnl_mdb_nlmsg_size(void)
304{
305 return NLMSG_ALIGN(sizeof(struct br_port_msg))
306 + nla_total_size(sizeof(struct br_mdb_entry));
307}
308
Elad Raz45ebcce2016-04-21 12:52:45 +0200309struct br_mdb_complete_info {
310 struct net_bridge_port *port;
311 struct br_ip ip;
312};
313
314static void br_mdb_complete(struct net_device *dev, int err, void *priv)
Cong Wang37a393b2012-12-11 22:23:07 +0000315{
Elad Raz45ebcce2016-04-21 12:52:45 +0200316 struct br_mdb_complete_info *data = priv;
317 struct net_bridge_port_group __rcu **pp;
318 struct net_bridge_port_group *p;
Elad Raz45ebcce2016-04-21 12:52:45 +0200319 struct net_bridge_mdb_entry *mp;
320 struct net_bridge_port *port = data->port;
321 struct net_bridge *br = port->br;
322
323 if (err)
324 goto err;
325
326 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200327 mp = br_mdb_ip_get(br, &data->ip);
Elad Raz45ebcce2016-04-21 12:52:45 +0200328 if (!mp)
329 goto out;
330 for (pp = &mp->ports; (p = mlock_dereference(*pp, br)) != NULL;
331 pp = &p->next) {
332 if (p->port != port)
333 continue;
334 p->flags |= MDB_PG_FLAGS_OFFLOAD;
335 }
336out:
337 spin_unlock_bh(&br->multicast_lock);
338err:
339 kfree(priv);
340}
341
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100342static void br_mdb_switchdev_host_port(struct net_device *dev,
343 struct net_device *lower_dev,
344 struct br_mdb_entry *entry, int type)
345{
346 struct switchdev_obj_port_mdb mdb = {
347 .obj = {
348 .id = SWITCHDEV_OBJ_ID_HOST_MDB,
349 .flags = SWITCHDEV_F_DEFER,
350 },
351 .vid = entry->vid,
352 };
353
354 if (entry->addr.proto == htons(ETH_P_IP))
355 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
356#if IS_ENABLED(CONFIG_IPV6)
357 else
358 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
359#endif
360
361 mdb.obj.orig_dev = dev;
362 switch (type) {
363 case RTM_NEWMDB:
Petr Machata69b73202018-12-12 17:02:52 +0000364 switchdev_port_obj_add(lower_dev, &mdb.obj, NULL);
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100365 break;
366 case RTM_DELMDB:
367 switchdev_port_obj_del(lower_dev, &mdb.obj);
368 break;
369 }
370}
371
372static void br_mdb_switchdev_host(struct net_device *dev,
373 struct br_mdb_entry *entry, int type)
374{
375 struct net_device *lower_dev;
376 struct list_head *iter;
377
378 netdev_for_each_lower_dev(dev, lower_dev, iter)
379 br_mdb_switchdev_host_port(dev, lower_dev, entry, type);
380}
381
Elad Raz45ebcce2016-04-21 12:52:45 +0200382static void __br_mdb_notify(struct net_device *dev, struct net_bridge_port *p,
383 struct br_mdb_entry *entry, int type)
384{
385 struct br_mdb_complete_info *complete_info;
Elad Razf1fecb12016-01-10 21:06:23 +0100386 struct switchdev_obj_port_mdb mdb = {
387 .obj = {
388 .id = SWITCHDEV_OBJ_ID_PORT_MDB,
389 .flags = SWITCHDEV_F_DEFER,
390 },
391 .vid = entry->vid,
392 };
393 struct net_device *port_dev;
Cong Wang37a393b2012-12-11 22:23:07 +0000394 struct net *net = dev_net(dev);
395 struct sk_buff *skb;
396 int err = -ENOBUFS;
397
Elad Razf1fecb12016-01-10 21:06:23 +0100398 port_dev = __dev_get_by_index(net, entry->ifindex);
399 if (entry->addr.proto == htons(ETH_P_IP))
400 ip_eth_mc_map(entry->addr.u.ip4, mdb.addr);
401#if IS_ENABLED(CONFIG_IPV6)
402 else
403 ipv6_eth_mc_map(&entry->addr.u.ip6, mdb.addr);
404#endif
405
406 mdb.obj.orig_dev = port_dev;
Andrew Lunn2a260282017-11-09 23:10:58 +0100407 if (p && port_dev && type == RTM_NEWMDB) {
Elad Raz45ebcce2016-04-21 12:52:45 +0200408 complete_info = kmalloc(sizeof(*complete_info), GFP_ATOMIC);
409 if (complete_info) {
410 complete_info->port = p;
411 __mdb_entry_to_br_ip(entry, &complete_info->ip);
412 mdb.obj.complete_priv = complete_info;
413 mdb.obj.complete = br_mdb_complete;
Petr Machata69b73202018-12-12 17:02:52 +0000414 if (switchdev_port_obj_add(port_dev, &mdb.obj, NULL))
Eduardo Valentin1bfb1592017-07-11 14:55:12 -0700415 kfree(complete_info);
Elad Raz45ebcce2016-04-21 12:52:45 +0200416 }
Andrew Lunn2a260282017-11-09 23:10:58 +0100417 } else if (p && port_dev && type == RTM_DELMDB) {
Elad Razf1fecb12016-01-10 21:06:23 +0100418 switchdev_port_obj_del(port_dev, &mdb.obj);
Elad Raz9e8430f2016-02-03 09:57:06 +0100419 }
Elad Razf1fecb12016-01-10 21:06:23 +0100420
Andrew Lunn47d5b6d2017-11-09 23:10:59 +0100421 if (!p)
422 br_mdb_switchdev_host(dev, entry, type);
423
Cong Wang37a393b2012-12-11 22:23:07 +0000424 skb = nlmsg_new(rtnl_mdb_nlmsg_size(), GFP_ATOMIC);
425 if (!skb)
426 goto errout;
427
428 err = nlmsg_populate_mdb_fill(skb, dev, entry, 0, 0, type, NTF_SELF);
429 if (err < 0) {
430 kfree_skb(skb);
431 goto errout;
432 }
433
434 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
435 return;
436errout:
437 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
438}
439
Elad Raz45ebcce2016-04-21 12:52:45 +0200440void br_mdb_notify(struct net_device *dev, struct net_bridge_port *port,
441 struct br_ip *group, int type, u8 flags)
Cong Wang37a393b2012-12-11 22:23:07 +0000442{
443 struct br_mdb_entry entry;
444
Mathias Krausec085c492013-03-09 05:52:19 +0000445 memset(&entry, 0, sizeof(entry));
Andrew Lunn2a260282017-11-09 23:10:58 +0100446 if (port)
447 entry.ifindex = port->dev->ifindex;
448 else
449 entry.ifindex = dev->ifindex;
Elad Raz45ebcce2016-04-21 12:52:45 +0200450 entry.addr.proto = group->proto;
451 entry.addr.u.ip4 = group->u.ip4;
Cong Wang37a393b2012-12-11 22:23:07 +0000452#if IS_ENABLED(CONFIG_IPV6)
Elad Raz45ebcce2016-04-21 12:52:45 +0200453 entry.addr.u.ip6 = group->u.ip6;
Cong Wang37a393b2012-12-11 22:23:07 +0000454#endif
Elad Raz45ebcce2016-04-21 12:52:45 +0200455 entry.vid = group->vid;
456 __mdb_entry_fill_flags(&entry, flags);
457 __br_mdb_notify(dev, port, &entry, type);
Cong Wang37a393b2012-12-11 22:23:07 +0000458}
459
Satish Ashok949f1e32015-07-23 05:00:53 -0700460static int nlmsg_populate_rtr_fill(struct sk_buff *skb,
461 struct net_device *dev,
462 int ifindex, u32 pid,
463 u32 seq, int type, unsigned int flags)
464{
465 struct br_port_msg *bpm;
466 struct nlmsghdr *nlh;
467 struct nlattr *nest;
468
Nicolas Dichtel94a72b32019-09-06 11:47:02 +0200469 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*bpm), 0);
Satish Ashok949f1e32015-07-23 05:00:53 -0700470 if (!nlh)
471 return -EMSGSIZE;
472
473 bpm = nlmsg_data(nlh);
474 memset(bpm, 0, sizeof(*bpm));
475 bpm->family = AF_BRIDGE;
476 bpm->ifindex = dev->ifindex;
Michal Kubecekae0be8d2019-04-26 11:13:06 +0200477 nest = nla_nest_start_noflag(skb, MDBA_ROUTER);
Satish Ashok949f1e32015-07-23 05:00:53 -0700478 if (!nest)
479 goto cancel;
480
481 if (nla_put_u32(skb, MDBA_ROUTER_PORT, ifindex))
482 goto end;
483
484 nla_nest_end(skb, nest);
485 nlmsg_end(skb, nlh);
486 return 0;
487
488end:
489 nla_nest_end(skb, nest);
490cancel:
491 nlmsg_cancel(skb, nlh);
492 return -EMSGSIZE;
493}
494
495static inline size_t rtnl_rtr_nlmsg_size(void)
496{
497 return NLMSG_ALIGN(sizeof(struct br_port_msg))
498 + nla_total_size(sizeof(__u32));
499}
500
501void br_rtr_notify(struct net_device *dev, struct net_bridge_port *port,
502 int type)
503{
504 struct net *net = dev_net(dev);
505 struct sk_buff *skb;
506 int err = -ENOBUFS;
507 int ifindex;
508
509 ifindex = port ? port->dev->ifindex : 0;
510 skb = nlmsg_new(rtnl_rtr_nlmsg_size(), GFP_ATOMIC);
511 if (!skb)
512 goto errout;
513
514 err = nlmsg_populate_rtr_fill(skb, dev, ifindex, 0, 0, type, NTF_SELF);
515 if (err < 0) {
516 kfree_skb(skb);
517 goto errout;
518 }
519
520 rtnl_notify(skb, net, 0, RTNLGRP_MDB, NULL, GFP_ATOMIC);
521 return;
522
523errout:
524 rtnl_set_sk_err(net, RTNLGRP_MDB, err);
525}
526
Cong Wangcfd56752012-12-11 22:23:08 +0000527static bool is_valid_mdb_entry(struct br_mdb_entry *entry)
528{
529 if (entry->ifindex == 0)
530 return false;
531
532 if (entry->addr.proto == htons(ETH_P_IP)) {
533 if (!ipv4_is_multicast(entry->addr.u.ip4))
534 return false;
535 if (ipv4_is_local_multicast(entry->addr.u.ip4))
536 return false;
537#if IS_ENABLED(CONFIG_IPV6)
538 } else if (entry->addr.proto == htons(ETH_P_IPV6)) {
Linus Lüssing3c3769e2013-09-04 02:13:39 +0200539 if (ipv6_addr_is_ll_all_nodes(&entry->addr.u.ip6))
Cong Wangcfd56752012-12-11 22:23:08 +0000540 return false;
541#endif
542 } else
543 return false;
Amerigo Wangccb1c312012-12-14 22:09:51 +0000544 if (entry->state != MDB_PERMANENT && entry->state != MDB_TEMPORARY)
545 return false;
Nikolay Aleksandrov74fe61f2015-07-10 08:02:08 -0700546 if (entry->vid >= VLAN_VID_MASK)
547 return false;
Cong Wangcfd56752012-12-11 22:23:08 +0000548
549 return true;
550}
551
552static int br_mdb_parse(struct sk_buff *skb, struct nlmsghdr *nlh,
553 struct net_device **pdev, struct br_mdb_entry **pentry)
554{
555 struct net *net = sock_net(skb->sk);
556 struct br_mdb_entry *entry;
557 struct br_port_msg *bpm;
558 struct nlattr *tb[MDBA_SET_ENTRY_MAX+1];
559 struct net_device *dev;
560 int err;
561
Johannes Berg8cb08172019-04-26 14:07:28 +0200562 err = nlmsg_parse_deprecated(nlh, sizeof(*bpm), tb,
563 MDBA_SET_ENTRY_MAX, NULL, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000564 if (err < 0)
565 return err;
566
567 bpm = nlmsg_data(nlh);
568 if (bpm->ifindex == 0) {
569 pr_info("PF_BRIDGE: br_mdb_parse() with invalid ifindex\n");
570 return -EINVAL;
571 }
572
573 dev = __dev_get_by_index(net, bpm->ifindex);
574 if (dev == NULL) {
575 pr_info("PF_BRIDGE: br_mdb_parse() with unknown ifindex\n");
576 return -ENODEV;
577 }
578
579 if (!(dev->priv_flags & IFF_EBRIDGE)) {
580 pr_info("PF_BRIDGE: br_mdb_parse() with non-bridge\n");
581 return -EOPNOTSUPP;
582 }
583
584 *pdev = dev;
585
586 if (!tb[MDBA_SET_ENTRY] ||
587 nla_len(tb[MDBA_SET_ENTRY]) != sizeof(struct br_mdb_entry)) {
588 pr_info("PF_BRIDGE: br_mdb_parse() with invalid attr\n");
589 return -EINVAL;
590 }
591
592 entry = nla_data(tb[MDBA_SET_ENTRY]);
593 if (!is_valid_mdb_entry(entry)) {
594 pr_info("PF_BRIDGE: br_mdb_parse() with invalid entry\n");
595 return -EINVAL;
596 }
597
598 *pentry = entry;
599 return 0;
600}
601
602static int br_mdb_add_group(struct net_bridge *br, struct net_bridge_port *port,
Elad Raz45ebcce2016-04-21 12:52:45 +0200603 struct br_ip *group, unsigned char state)
Cong Wangcfd56752012-12-11 22:23:08 +0000604{
605 struct net_bridge_mdb_entry *mp;
606 struct net_bridge_port_group *p;
607 struct net_bridge_port_group __rcu **pp;
Satish Ashokf7e29652015-07-06 05:53:35 -0700608 unsigned long now = jiffies;
Cong Wangcfd56752012-12-11 22:23:08 +0000609 int err;
610
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200611 mp = br_mdb_ip_get(br, group);
Cong Wangcfd56752012-12-11 22:23:08 +0000612 if (!mp) {
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200613 mp = br_multicast_new_group(br, group);
Arnd Bergmann56bb7fd2016-02-10 16:09:02 +0100614 err = PTR_ERR_OR_ZERO(mp);
615 if (err)
Cong Wangcfd56752012-12-11 22:23:08 +0000616 return err;
617 }
618
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300619 /* host join */
620 if (!port) {
621 /* don't allow any flags for host-joined groups */
622 if (state)
623 return -EINVAL;
624 if (mp->host_joined)
625 return -EEXIST;
626
627 br_multicast_host_join(mp, false);
628
629 return 0;
630 }
631
Cong Wangcfd56752012-12-11 22:23:08 +0000632 for (pp = &mp->ports;
633 (p = mlock_dereference(*pp, br)) != NULL;
634 pp = &p->next) {
635 if (p->port == port)
636 return -EEXIST;
637 if ((unsigned long)p->port < (unsigned long)port)
638 break;
639 }
640
Felix Fietkau6db6f0e2017-01-21 21:01:32 +0100641 p = br_multicast_new_port_group(port, group, *pp, state, NULL);
Cong Wangcfd56752012-12-11 22:23:08 +0000642 if (unlikely(!p))
643 return -ENOMEM;
644 rcu_assign_pointer(*pp, p);
Satish Ashokf7e29652015-07-06 05:53:35 -0700645 if (state == MDB_TEMPORARY)
646 mod_timer(&p->timer, now + br->multicast_membership_interval);
Cong Wangcfd56752012-12-11 22:23:08 +0000647
Cong Wangcfd56752012-12-11 22:23:08 +0000648 return 0;
649}
650
651static int __br_mdb_add(struct net *net, struct net_bridge *br,
Elad Raz45ebcce2016-04-21 12:52:45 +0200652 struct br_mdb_entry *entry)
Cong Wangcfd56752012-12-11 22:23:08 +0000653{
654 struct br_ip ip;
655 struct net_device *dev;
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300656 struct net_bridge_port *p = NULL;
Cong Wangcfd56752012-12-11 22:23:08 +0000657 int ret;
658
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300659 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000660 return -EINVAL;
661
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300662 if (entry->ifindex != br->dev->ifindex) {
663 dev = __dev_get_by_index(net, entry->ifindex);
664 if (!dev)
665 return -ENODEV;
Cong Wangcfd56752012-12-11 22:23:08 +0000666
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300667 p = br_port_get_rtnl(dev);
668 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
669 return -EINVAL;
670 }
Cong Wangcfd56752012-12-11 22:23:08 +0000671
Elad Raz6dd684c2016-04-21 12:52:44 +0200672 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000673
674 spin_lock_bh(&br->multicast_lock);
Elad Raz45ebcce2016-04-21 12:52:45 +0200675 ret = br_mdb_add_group(br, p, &ip, entry->state);
Cong Wangcfd56752012-12-11 22:23:08 +0000676 spin_unlock_bh(&br->multicast_lock);
677 return ret;
678}
679
David Ahernc21ef3e2017-04-16 09:48:24 -0700680static int br_mdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
681 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000682{
683 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200684 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300685 struct net_bridge_port *p = NULL;
Satish Ashoke44deb22015-08-03 13:29:16 +0200686 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000687 struct br_mdb_entry *entry;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200688 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000689 struct net_bridge *br;
690 int err;
691
692 err = br_mdb_parse(skb, nlh, &dev, &entry);
693 if (err < 0)
694 return err;
695
696 br = netdev_priv(dev);
697
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300698 if (entry->ifindex != br->dev->ifindex) {
699 pdev = __dev_get_by_index(net, entry->ifindex);
700 if (!pdev)
701 return -ENODEV;
Satish Ashoke44deb22015-08-03 13:29:16 +0200702
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300703 p = br_port_get_rtnl(pdev);
704 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
705 return -EINVAL;
706 vg = nbp_vlan_group(p);
707 } else {
708 vg = br_vlan_group(br);
709 }
Satish Ashoke44deb22015-08-03 13:29:16 +0200710
Nikolay Aleksandrovf59783f2019-08-17 14:22:10 +0300711 /* If vlan filtering is enabled and VLAN is not specified
712 * install mdb entry on all vlans configured on the port.
713 */
Ido Schimmel1f514452017-05-26 08:37:23 +0200714 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200715 list_for_each_entry(v, &vg->vlan_list, vlist) {
716 entry->vid = v->vid;
Elad Raz45ebcce2016-04-21 12:52:45 +0200717 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200718 if (err)
719 break;
Elad Raz45ebcce2016-04-21 12:52:45 +0200720 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200721 }
722 } else {
Elad Raz45ebcce2016-04-21 12:52:45 +0200723 err = __br_mdb_add(net, br, entry);
Satish Ashoke44deb22015-08-03 13:29:16 +0200724 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200725 __br_mdb_notify(dev, p, entry, RTM_NEWMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200726 }
727
Cong Wangcfd56752012-12-11 22:23:08 +0000728 return err;
729}
730
731static int __br_mdb_del(struct net_bridge *br, struct br_mdb_entry *entry)
732{
Cong Wangcfd56752012-12-11 22:23:08 +0000733 struct net_bridge_mdb_entry *mp;
734 struct net_bridge_port_group *p;
735 struct net_bridge_port_group __rcu **pp;
736 struct br_ip ip;
737 int err = -EINVAL;
738
Nikolay Aleksandrov13cefad2018-09-26 17:01:03 +0300739 if (!netif_running(br->dev) || !br_opt_get(br, BROPT_MULTICAST_ENABLED))
Cong Wangcfd56752012-12-11 22:23:08 +0000740 return -EINVAL;
741
Elad Raz6dd684c2016-04-21 12:52:44 +0200742 __mdb_entry_to_br_ip(entry, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000743
744 spin_lock_bh(&br->multicast_lock);
Nikolay Aleksandrov19e3a9c2018-12-05 15:14:24 +0200745 mp = br_mdb_ip_get(br, &ip);
Cong Wangcfd56752012-12-11 22:23:08 +0000746 if (!mp)
747 goto unlock;
748
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300749 /* host leave */
750 if (entry->ifindex == mp->br->dev->ifindex && mp->host_joined) {
751 br_multicast_host_leave(mp, false);
752 err = 0;
753 if (!mp->ports && netif_running(br->dev))
754 mod_timer(&mp->timer, jiffies);
755 goto unlock;
756 }
757
Cong Wangcfd56752012-12-11 22:23:08 +0000758 for (pp = &mp->ports;
759 (p = mlock_dereference(*pp, br)) != NULL;
760 pp = &p->next) {
761 if (!p->port || p->port->dev->ifindex != entry->ifindex)
762 continue;
763
764 if (p->port->state == BR_STATE_DISABLED)
765 goto unlock;
766
Elad Raz9d06b6d2016-02-03 09:57:05 +0100767 __mdb_entry_fill_flags(entry, p->flags);
Cong Wangcfd56752012-12-11 22:23:08 +0000768 rcu_assign_pointer(*pp, p->next);
769 hlist_del_init(&p->mglist);
770 del_timer(&p->timer);
Nikolay Aleksandrov43295962018-12-05 15:14:25 +0200771 kfree_rcu(p, rcu);
Cong Wangcfd56752012-12-11 22:23:08 +0000772 err = 0;
773
Andrew Lunnff0fd342017-11-09 23:10:57 +0100774 if (!mp->ports && !mp->host_joined &&
Cong Wangcfd56752012-12-11 22:23:08 +0000775 netif_running(br->dev))
776 mod_timer(&mp->timer, jiffies);
777 break;
778 }
779
780unlock:
781 spin_unlock_bh(&br->multicast_lock);
782 return err;
783}
784
David Ahernc21ef3e2017-04-16 09:48:24 -0700785static int br_mdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
786 struct netlink_ext_ack *extack)
Cong Wangcfd56752012-12-11 22:23:08 +0000787{
Satish Ashoke44deb22015-08-03 13:29:16 +0200788 struct net *net = sock_net(skb->sk);
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200789 struct net_bridge_vlan_group *vg;
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300790 struct net_bridge_port *p = NULL;
Satish Ashoke44deb22015-08-03 13:29:16 +0200791 struct net_device *dev, *pdev;
Cong Wangcfd56752012-12-11 22:23:08 +0000792 struct br_mdb_entry *entry;
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200793 struct net_bridge_vlan *v;
Cong Wangcfd56752012-12-11 22:23:08 +0000794 struct net_bridge *br;
795 int err;
796
797 err = br_mdb_parse(skb, nlh, &dev, &entry);
798 if (err < 0)
799 return err;
800
801 br = netdev_priv(dev);
802
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300803 if (entry->ifindex != br->dev->ifindex) {
804 pdev = __dev_get_by_index(net, entry->ifindex);
805 if (!pdev)
806 return -ENODEV;
Satish Ashoke44deb22015-08-03 13:29:16 +0200807
Nikolay Aleksandrov1bc844e2019-08-17 14:22:13 +0300808 p = br_port_get_rtnl(pdev);
809 if (!p || p->br != br || p->state == BR_STATE_DISABLED)
810 return -EINVAL;
811 vg = nbp_vlan_group(p);
812 } else {
813 vg = br_vlan_group(br);
814 }
Satish Ashoke44deb22015-08-03 13:29:16 +0200815
Nikolay Aleksandrovf59783f2019-08-17 14:22:10 +0300816 /* If vlan filtering is enabled and VLAN is not specified
817 * delete mdb entry on all vlans configured on the port.
818 */
Ido Schimmel1f514452017-05-26 08:37:23 +0200819 if (br_vlan_enabled(br->dev) && vg && entry->vid == 0) {
Nikolay Aleksandrov2594e9062015-09-25 19:00:11 +0200820 list_for_each_entry(v, &vg->vlan_list, vlist) {
821 entry->vid = v->vid;
Satish Ashoke44deb22015-08-03 13:29:16 +0200822 err = __br_mdb_del(br, entry);
823 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200824 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200825 }
826 } else {
827 err = __br_mdb_del(br, entry);
828 if (!err)
Elad Raz45ebcce2016-04-21 12:52:45 +0200829 __br_mdb_notify(dev, p, entry, RTM_DELMDB);
Satish Ashoke44deb22015-08-03 13:29:16 +0200830 }
831
Cong Wangcfd56752012-12-11 22:23:08 +0000832 return err;
833}
834
Cong Wangee07c6e2012-12-07 00:04:48 +0000835void br_mdb_init(void)
836{
Florian Westphalc1c502b2017-12-02 21:44:07 +0100837 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_GETMDB, NULL, br_mdb_dump, 0);
838 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_NEWMDB, br_mdb_add, NULL, 0);
839 rtnl_register_module(THIS_MODULE, PF_BRIDGE, RTM_DELMDB, br_mdb_del, NULL, 0);
Cong Wangee07c6e2012-12-07 00:04:48 +0000840}
Vlad Yasevich63233152012-12-19 09:13:48 +0000841
842void br_mdb_uninit(void)
843{
844 rtnl_unregister(PF_BRIDGE, RTM_GETMDB);
845 rtnl_unregister(PF_BRIDGE, RTM_NEWMDB);
846 rtnl_unregister(PF_BRIDGE, RTM_DELMDB);
847}