]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/bridge/br_netlink.c
Merge tag 'armsoc-dt' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[mirror_ubuntu-bionic-kernel.git] / net / bridge / br_netlink.c
1 /*
2 * Bridge netlink control interface
3 *
4 * Authors:
5 * Stephen Hemminger <shemminger@osdl.org>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 */
12
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/etherdevice.h>
16 #include <net/rtnetlink.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 #include <uapi/linux/if_bridge.h>
20
21 #include "br_private.h"
22 #include "br_private_stp.h"
23 #include "br_private_tunnel.h"
24
25 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
26 u32 filter_mask)
27 {
28 struct net_bridge_vlan *v;
29 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
30 u16 flags, pvid;
31 int num_vlans = 0;
32
33 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
34 return 0;
35
36 pvid = br_get_pvid(vg);
37 /* Count number of vlan infos */
38 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
39 flags = 0;
40 /* only a context, bridge vlan not activated */
41 if (!br_vlan_should_use(v))
42 continue;
43 if (v->vid == pvid)
44 flags |= BRIDGE_VLAN_INFO_PVID;
45
46 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
47 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
48
49 if (vid_range_start == 0) {
50 goto initvars;
51 } else if ((v->vid - vid_range_end) == 1 &&
52 flags == vid_range_flags) {
53 vid_range_end = v->vid;
54 continue;
55 } else {
56 if ((vid_range_end - vid_range_start) > 0)
57 num_vlans += 2;
58 else
59 num_vlans += 1;
60 }
61 initvars:
62 vid_range_start = v->vid;
63 vid_range_end = v->vid;
64 vid_range_flags = flags;
65 }
66
67 if (vid_range_start != 0) {
68 if ((vid_range_end - vid_range_start) > 0)
69 num_vlans += 2;
70 else
71 num_vlans += 1;
72 }
73
74 return num_vlans;
75 }
76
77 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
78 u32 filter_mask)
79 {
80 int num_vlans;
81
82 if (!vg)
83 return 0;
84
85 if (filter_mask & RTEXT_FILTER_BRVLAN)
86 return vg->num_vlans;
87
88 rcu_read_lock();
89 num_vlans = __get_num_vlan_infos(vg, filter_mask);
90 rcu_read_unlock();
91
92 return num_vlans;
93 }
94
95 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
96 u32 filter_mask)
97 {
98 struct net_bridge_vlan_group *vg = NULL;
99 struct net_bridge_port *p = NULL;
100 struct net_bridge *br;
101 int num_vlan_infos;
102 size_t vinfo_sz = 0;
103
104 rcu_read_lock();
105 if (br_port_exists(dev)) {
106 p = br_port_get_rcu(dev);
107 vg = nbp_vlan_group_rcu(p);
108 } else if (dev->priv_flags & IFF_EBRIDGE) {
109 br = netdev_priv(dev);
110 vg = br_vlan_group_rcu(br);
111 }
112 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
113 rcu_read_unlock();
114
115 if (p && (p->flags & BR_VLAN_TUNNEL))
116 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
117
118 /* Each VLAN is returned in bridge_vlan_info along with flags */
119 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
120
121 return vinfo_sz;
122 }
123
124 static inline size_t br_port_info_size(void)
125 {
126 return nla_total_size(1) /* IFLA_BRPORT_STATE */
127 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
128 + nla_total_size(4) /* IFLA_BRPORT_COST */
129 + nla_total_size(1) /* IFLA_BRPORT_MODE */
130 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
131 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
132 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
133 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
134 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
135 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
136 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
137 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
138 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
139 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
140 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
141 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
142 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
143 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
144 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
145 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
146 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
147 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
148 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
149 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
150 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
151 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
152 #endif
153 + 0;
154 }
155
156 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
157 {
158 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
159 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
160 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
161 + nla_total_size(4) /* IFLA_MASTER */
162 + nla_total_size(4) /* IFLA_MTU */
163 + nla_total_size(4) /* IFLA_LINK */
164 + nla_total_size(1) /* IFLA_OPERSTATE */
165 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
166 + nla_total_size(br_get_link_af_size_filtered(dev,
167 filter_mask)); /* IFLA_AF_SPEC */
168 }
169
170 static int br_port_fill_attrs(struct sk_buff *skb,
171 const struct net_bridge_port *p)
172 {
173 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
174 u64 timerval;
175
176 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
177 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
178 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
179 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
180 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
181 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
182 !!(p->flags & BR_ROOT_BLOCK)) ||
183 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
184 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
185 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
186 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
187 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
188 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
189 !!(p->flags & BR_FLOOD)) ||
190 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
191 !!(p->flags & BR_MCAST_FLOOD)) ||
192 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
193 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
194 !!(p->flags & BR_PROXYARP_WIFI)) ||
195 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
196 &p->designated_root) ||
197 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
198 &p->designated_bridge) ||
199 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
200 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
201 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
202 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
203 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
204 p->topology_change_ack) ||
205 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
206 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
207 BR_VLAN_TUNNEL)))
208 return -EMSGSIZE;
209
210 timerval = br_timer_value(&p->message_age_timer);
211 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
212 IFLA_BRPORT_PAD))
213 return -EMSGSIZE;
214 timerval = br_timer_value(&p->forward_delay_timer);
215 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
216 IFLA_BRPORT_PAD))
217 return -EMSGSIZE;
218 timerval = br_timer_value(&p->hold_timer);
219 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
220 IFLA_BRPORT_PAD))
221 return -EMSGSIZE;
222
223 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
224 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
225 p->multicast_router))
226 return -EMSGSIZE;
227 #endif
228
229 return 0;
230 }
231
232 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
233 u16 vid_end, u16 flags)
234 {
235 struct bridge_vlan_info vinfo;
236
237 if ((vid_end - vid_start) > 0) {
238 /* add range to skb */
239 vinfo.vid = vid_start;
240 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
241 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
242 sizeof(vinfo), &vinfo))
243 goto nla_put_failure;
244
245 vinfo.vid = vid_end;
246 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
247 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
248 sizeof(vinfo), &vinfo))
249 goto nla_put_failure;
250 } else {
251 vinfo.vid = vid_start;
252 vinfo.flags = flags;
253 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
254 sizeof(vinfo), &vinfo))
255 goto nla_put_failure;
256 }
257
258 return 0;
259
260 nla_put_failure:
261 return -EMSGSIZE;
262 }
263
264 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
265 struct net_bridge_vlan_group *vg)
266 {
267 struct net_bridge_vlan *v;
268 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
269 u16 flags, pvid;
270 int err = 0;
271
272 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
273 * and mark vlan info with begin and end flags
274 * if vlaninfo represents a range
275 */
276 pvid = br_get_pvid(vg);
277 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
278 flags = 0;
279 if (!br_vlan_should_use(v))
280 continue;
281 if (v->vid == pvid)
282 flags |= BRIDGE_VLAN_INFO_PVID;
283
284 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
285 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
286
287 if (vid_range_start == 0) {
288 goto initvars;
289 } else if ((v->vid - vid_range_end) == 1 &&
290 flags == vid_range_flags) {
291 vid_range_end = v->vid;
292 continue;
293 } else {
294 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
295 vid_range_end,
296 vid_range_flags);
297 if (err)
298 return err;
299 }
300
301 initvars:
302 vid_range_start = v->vid;
303 vid_range_end = v->vid;
304 vid_range_flags = flags;
305 }
306
307 if (vid_range_start != 0) {
308 /* Call it once more to send any left over vlans */
309 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
310 vid_range_end,
311 vid_range_flags);
312 if (err)
313 return err;
314 }
315
316 return 0;
317 }
318
319 static int br_fill_ifvlaninfo(struct sk_buff *skb,
320 struct net_bridge_vlan_group *vg)
321 {
322 struct bridge_vlan_info vinfo;
323 struct net_bridge_vlan *v;
324 u16 pvid;
325
326 pvid = br_get_pvid(vg);
327 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
328 if (!br_vlan_should_use(v))
329 continue;
330
331 vinfo.vid = v->vid;
332 vinfo.flags = 0;
333 if (v->vid == pvid)
334 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
335
336 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
337 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
338
339 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
340 sizeof(vinfo), &vinfo))
341 goto nla_put_failure;
342 }
343
344 return 0;
345
346 nla_put_failure:
347 return -EMSGSIZE;
348 }
349
350 /*
351 * Create one netlink message for one interface
352 * Contains port and master info as well as carrier and bridge state.
353 */
354 static int br_fill_ifinfo(struct sk_buff *skb,
355 struct net_bridge_port *port,
356 u32 pid, u32 seq, int event, unsigned int flags,
357 u32 filter_mask, const struct net_device *dev)
358 {
359 struct net_bridge *br;
360 struct ifinfomsg *hdr;
361 struct nlmsghdr *nlh;
362 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
363
364 if (port)
365 br = port->br;
366 else
367 br = netdev_priv(dev);
368
369 br_debug(br, "br_fill_info event %d port %s master %s\n",
370 event, dev->name, br->dev->name);
371
372 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
373 if (nlh == NULL)
374 return -EMSGSIZE;
375
376 hdr = nlmsg_data(nlh);
377 hdr->ifi_family = AF_BRIDGE;
378 hdr->__ifi_pad = 0;
379 hdr->ifi_type = dev->type;
380 hdr->ifi_index = dev->ifindex;
381 hdr->ifi_flags = dev_get_flags(dev);
382 hdr->ifi_change = 0;
383
384 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
385 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
386 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
387 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
388 (dev->addr_len &&
389 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
390 (dev->ifindex != dev_get_iflink(dev) &&
391 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
392 goto nla_put_failure;
393
394 if (event == RTM_NEWLINK && port) {
395 struct nlattr *nest
396 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
397
398 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
399 goto nla_put_failure;
400 nla_nest_end(skb, nest);
401 }
402
403 /* Check if the VID information is requested */
404 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
405 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
406 struct net_bridge_vlan_group *vg;
407 struct nlattr *af;
408 int err;
409
410 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
411 rcu_read_lock();
412 if (port)
413 vg = nbp_vlan_group_rcu(port);
414 else
415 vg = br_vlan_group_rcu(br);
416
417 if (!vg || !vg->num_vlans) {
418 rcu_read_unlock();
419 goto done;
420 }
421 af = nla_nest_start(skb, IFLA_AF_SPEC);
422 if (!af) {
423 rcu_read_unlock();
424 goto nla_put_failure;
425 }
426 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
427 err = br_fill_ifvlaninfo_compressed(skb, vg);
428 else
429 err = br_fill_ifvlaninfo(skb, vg);
430
431 if (port && (port->flags & BR_VLAN_TUNNEL))
432 err = br_fill_vlan_tunnel_info(skb, vg);
433 rcu_read_unlock();
434 if (err)
435 goto nla_put_failure;
436 nla_nest_end(skb, af);
437 }
438
439 done:
440 nlmsg_end(skb, nlh);
441 return 0;
442
443 nla_put_failure:
444 nlmsg_cancel(skb, nlh);
445 return -EMSGSIZE;
446 }
447
448 /*
449 * Notify listeners of a change in port information
450 */
451 void br_ifinfo_notify(int event, struct net_bridge_port *port)
452 {
453 struct net *net;
454 struct sk_buff *skb;
455 int err = -ENOBUFS;
456 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
457
458 if (!port)
459 return;
460
461 net = dev_net(port->dev);
462 br_debug(port->br, "port %u(%s) event %d\n",
463 (unsigned int)port->port_no, port->dev->name, event);
464
465 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
466 if (skb == NULL)
467 goto errout;
468
469 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
470 if (err < 0) {
471 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
472 WARN_ON(err == -EMSGSIZE);
473 kfree_skb(skb);
474 goto errout;
475 }
476 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
477 return;
478 errout:
479 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
480 }
481
482
483 /*
484 * Dump information about all ports, in response to GETLINK
485 */
486 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
487 struct net_device *dev, u32 filter_mask, int nlflags)
488 {
489 struct net_bridge_port *port = br_port_get_rtnl(dev);
490
491 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
492 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
493 return 0;
494
495 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
496 filter_mask, dev);
497 }
498
499 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
500 int cmd, struct bridge_vlan_info *vinfo)
501 {
502 int err = 0;
503
504 switch (cmd) {
505 case RTM_SETLINK:
506 if (p) {
507 /* if the MASTER flag is set this will act on the global
508 * per-VLAN entry as well
509 */
510 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
511 if (err)
512 break;
513 } else {
514 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
515 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
516 }
517 break;
518
519 case RTM_DELLINK:
520 if (p) {
521 nbp_vlan_delete(p, vinfo->vid);
522 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
523 br_vlan_delete(p->br, vinfo->vid);
524 } else {
525 br_vlan_delete(br, vinfo->vid);
526 }
527 break;
528 }
529
530 return err;
531 }
532
533 static int br_process_vlan_info(struct net_bridge *br,
534 struct net_bridge_port *p, int cmd,
535 struct bridge_vlan_info *vinfo_curr,
536 struct bridge_vlan_info **vinfo_last)
537 {
538 if (!vinfo_curr->vid || vinfo_curr->vid >= VLAN_VID_MASK)
539 return -EINVAL;
540
541 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
542 /* check if we are already processing a range */
543 if (*vinfo_last)
544 return -EINVAL;
545 *vinfo_last = vinfo_curr;
546 /* don't allow range of pvids */
547 if ((*vinfo_last)->flags & BRIDGE_VLAN_INFO_PVID)
548 return -EINVAL;
549 return 0;
550 }
551
552 if (*vinfo_last) {
553 struct bridge_vlan_info tmp_vinfo;
554 int v, err;
555
556 if (!(vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_END))
557 return -EINVAL;
558
559 if (vinfo_curr->vid <= (*vinfo_last)->vid)
560 return -EINVAL;
561
562 memcpy(&tmp_vinfo, *vinfo_last,
563 sizeof(struct bridge_vlan_info));
564 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
565 tmp_vinfo.vid = v;
566 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
567 if (err)
568 break;
569 }
570 *vinfo_last = NULL;
571
572 return 0;
573 }
574
575 return br_vlan_info(br, p, cmd, vinfo_curr);
576 }
577
578 static int br_afspec(struct net_bridge *br,
579 struct net_bridge_port *p,
580 struct nlattr *af_spec,
581 int cmd)
582 {
583 struct bridge_vlan_info *vinfo_curr = NULL;
584 struct bridge_vlan_info *vinfo_last = NULL;
585 struct nlattr *attr;
586 struct vtunnel_info tinfo_last = {};
587 struct vtunnel_info tinfo_curr = {};
588 int err = 0, rem;
589
590 nla_for_each_nested(attr, af_spec, rem) {
591 err = 0;
592 switch (nla_type(attr)) {
593 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
594 if (!(p->flags & BR_VLAN_TUNNEL))
595 return -EINVAL;
596 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
597 if (err)
598 return err;
599 err = br_process_vlan_tunnel_info(br, p, cmd,
600 &tinfo_curr,
601 &tinfo_last);
602 if (err)
603 return err;
604 break;
605 case IFLA_BRIDGE_VLAN_INFO:
606 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
607 return -EINVAL;
608 vinfo_curr = nla_data(attr);
609 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
610 &vinfo_last);
611 if (err)
612 return err;
613 break;
614 }
615 }
616
617 return err;
618 }
619
620 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
621 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
622 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
623 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
624 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
625 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
626 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
627 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
628 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
629 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
630 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
631 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
632 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
633 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
634 };
635
636 /* Change the state of the port and notify spanning tree */
637 static int br_set_port_state(struct net_bridge_port *p, u8 state)
638 {
639 if (state > BR_STATE_BLOCKING)
640 return -EINVAL;
641
642 /* if kernel STP is running, don't allow changes */
643 if (p->br->stp_enabled == BR_KERNEL_STP)
644 return -EBUSY;
645
646 /* if device is not up, change is not allowed
647 * if link is not present, only allowable state is disabled
648 */
649 if (!netif_running(p->dev) ||
650 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
651 return -ENETDOWN;
652
653 br_set_state(p, state);
654 br_port_state_selection(p->br);
655 return 0;
656 }
657
658 /* Set/clear or port flags based on attribute */
659 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
660 int attrtype, unsigned long mask)
661 {
662 if (tb[attrtype]) {
663 u8 flag = nla_get_u8(tb[attrtype]);
664 if (flag)
665 p->flags |= mask;
666 else
667 p->flags &= ~mask;
668 }
669 }
670
671 /* Process bridge protocol info on port */
672 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
673 {
674 unsigned long old_flags = p->flags;
675 bool br_vlan_tunnel_old = false;
676 int err;
677
678 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
679 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
680 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
681 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
682 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
683 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
684 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
685 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST, BR_MULTICAST_TO_UNICAST);
686 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
687 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
688
689 br_vlan_tunnel_old = (p->flags & BR_VLAN_TUNNEL) ? true : false;
690 br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
691 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
692 nbp_vlan_tunnel_info_flush(p);
693
694 if (tb[IFLA_BRPORT_COST]) {
695 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
696 if (err)
697 return err;
698 }
699
700 if (tb[IFLA_BRPORT_PRIORITY]) {
701 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
702 if (err)
703 return err;
704 }
705
706 if (tb[IFLA_BRPORT_STATE]) {
707 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
708 if (err)
709 return err;
710 }
711
712 if (tb[IFLA_BRPORT_FLUSH])
713 br_fdb_delete_by_port(p->br, p, 0, 0);
714
715 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
716 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
717 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
718
719 err = br_multicast_set_port_router(p, mcast_router);
720 if (err)
721 return err;
722 }
723 #endif
724 br_port_flags_change(p, old_flags ^ p->flags);
725 return 0;
726 }
727
728 /* Change state and parameters on port. */
729 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
730 {
731 struct nlattr *protinfo;
732 struct nlattr *afspec;
733 struct net_bridge_port *p;
734 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
735 int err = 0;
736
737 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
738 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
739 if (!protinfo && !afspec)
740 return 0;
741
742 p = br_port_get_rtnl(dev);
743 /* We want to accept dev as bridge itself if the AF_SPEC
744 * is set to see if someone is setting vlan info on the bridge
745 */
746 if (!p && !afspec)
747 return -EINVAL;
748
749 if (p && protinfo) {
750 if (protinfo->nla_type & NLA_F_NESTED) {
751 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
752 protinfo, br_port_policy);
753 if (err)
754 return err;
755
756 spin_lock_bh(&p->br->lock);
757 err = br_setport(p, tb);
758 spin_unlock_bh(&p->br->lock);
759 } else {
760 /* Binary compatibility with old RSTP */
761 if (nla_len(protinfo) < sizeof(u8))
762 return -EINVAL;
763
764 spin_lock_bh(&p->br->lock);
765 err = br_set_port_state(p, nla_get_u8(protinfo));
766 spin_unlock_bh(&p->br->lock);
767 }
768 if (err)
769 goto out;
770 }
771
772 if (afspec) {
773 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
774 afspec, RTM_SETLINK);
775 }
776
777 if (err == 0)
778 br_ifinfo_notify(RTM_NEWLINK, p);
779 out:
780 return err;
781 }
782
783 /* Delete port information */
784 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
785 {
786 struct nlattr *afspec;
787 struct net_bridge_port *p;
788 int err = 0;
789
790 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
791 if (!afspec)
792 return 0;
793
794 p = br_port_get_rtnl(dev);
795 /* We want to accept dev as bridge itself as well */
796 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
797 return -EINVAL;
798
799 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
800 afspec, RTM_DELLINK);
801 if (err == 0)
802 /* Send RTM_NEWLINK because userspace
803 * expects RTM_NEWLINK for vlan dels
804 */
805 br_ifinfo_notify(RTM_NEWLINK, p);
806
807 return err;
808 }
809 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
810 {
811 if (tb[IFLA_ADDRESS]) {
812 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
813 return -EINVAL;
814 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
815 return -EADDRNOTAVAIL;
816 }
817
818 if (!data)
819 return 0;
820
821 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
822 if (data[IFLA_BR_VLAN_PROTOCOL]) {
823 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
824 case htons(ETH_P_8021Q):
825 case htons(ETH_P_8021AD):
826 break;
827 default:
828 return -EPROTONOSUPPORT;
829 }
830 }
831 #endif
832
833 return 0;
834 }
835
836 static int br_port_slave_changelink(struct net_device *brdev,
837 struct net_device *dev,
838 struct nlattr *tb[],
839 struct nlattr *data[])
840 {
841 struct net_bridge *br = netdev_priv(brdev);
842 int ret;
843
844 if (!data)
845 return 0;
846
847 spin_lock_bh(&br->lock);
848 ret = br_setport(br_port_get_rtnl(dev), data);
849 spin_unlock_bh(&br->lock);
850
851 return ret;
852 }
853
854 static int br_port_fill_slave_info(struct sk_buff *skb,
855 const struct net_device *brdev,
856 const struct net_device *dev)
857 {
858 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
859 }
860
861 static size_t br_port_get_slave_size(const struct net_device *brdev,
862 const struct net_device *dev)
863 {
864 return br_port_info_size();
865 }
866
867 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
868 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
869 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
870 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
871 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
872 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
873 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
874 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
875 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
876 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
877 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
878 .len = ETH_ALEN },
879 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
880 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
881 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
882 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
883 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
884 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
885 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
886 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
887 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
888 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
889 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
890 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
891 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
892 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
893 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
894 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
895 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
896 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
897 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
898 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
899 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
900 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
901 };
902
903 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
904 struct nlattr *data[])
905 {
906 struct net_bridge *br = netdev_priv(brdev);
907 int err;
908
909 if (!data)
910 return 0;
911
912 if (data[IFLA_BR_FORWARD_DELAY]) {
913 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
914 if (err)
915 return err;
916 }
917
918 if (data[IFLA_BR_HELLO_TIME]) {
919 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
920 if (err)
921 return err;
922 }
923
924 if (data[IFLA_BR_MAX_AGE]) {
925 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
926 if (err)
927 return err;
928 }
929
930 if (data[IFLA_BR_AGEING_TIME]) {
931 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
932 if (err)
933 return err;
934 }
935
936 if (data[IFLA_BR_STP_STATE]) {
937 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
938
939 br_stp_set_enabled(br, stp_enabled);
940 }
941
942 if (data[IFLA_BR_PRIORITY]) {
943 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
944
945 br_stp_set_bridge_priority(br, priority);
946 }
947
948 if (data[IFLA_BR_VLAN_FILTERING]) {
949 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
950
951 err = __br_vlan_filter_toggle(br, vlan_filter);
952 if (err)
953 return err;
954 }
955
956 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
957 if (data[IFLA_BR_VLAN_PROTOCOL]) {
958 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
959
960 err = __br_vlan_set_proto(br, vlan_proto);
961 if (err)
962 return err;
963 }
964
965 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
966 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
967
968 err = __br_vlan_set_default_pvid(br, defpvid);
969 if (err)
970 return err;
971 }
972
973 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
974 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
975
976 err = br_vlan_set_stats(br, vlan_stats);
977 if (err)
978 return err;
979 }
980 #endif
981
982 if (data[IFLA_BR_GROUP_FWD_MASK]) {
983 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
984
985 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
986 return -EINVAL;
987 br->group_fwd_mask = fwd_mask;
988 }
989
990 if (data[IFLA_BR_GROUP_ADDR]) {
991 u8 new_addr[ETH_ALEN];
992
993 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
994 return -EINVAL;
995 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
996 if (!is_link_local_ether_addr(new_addr))
997 return -EINVAL;
998 if (new_addr[5] == 1 || /* 802.3x Pause address */
999 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1000 new_addr[5] == 3) /* 802.1X PAE address */
1001 return -EINVAL;
1002 spin_lock_bh(&br->lock);
1003 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1004 spin_unlock_bh(&br->lock);
1005 br->group_addr_set = true;
1006 br_recalculate_fwd_mask(br);
1007 }
1008
1009 if (data[IFLA_BR_FDB_FLUSH])
1010 br_fdb_flush(br);
1011
1012 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1013 if (data[IFLA_BR_MCAST_ROUTER]) {
1014 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1015
1016 err = br_multicast_set_router(br, multicast_router);
1017 if (err)
1018 return err;
1019 }
1020
1021 if (data[IFLA_BR_MCAST_SNOOPING]) {
1022 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1023
1024 err = br_multicast_toggle(br, mcast_snooping);
1025 if (err)
1026 return err;
1027 }
1028
1029 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1030 u8 val;
1031
1032 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1033 br->multicast_query_use_ifaddr = !!val;
1034 }
1035
1036 if (data[IFLA_BR_MCAST_QUERIER]) {
1037 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1038
1039 err = br_multicast_set_querier(br, mcast_querier);
1040 if (err)
1041 return err;
1042 }
1043
1044 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
1045 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
1046
1047 br->hash_elasticity = val;
1048 }
1049
1050 if (data[IFLA_BR_MCAST_HASH_MAX]) {
1051 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1052
1053 err = br_multicast_set_hash_max(br, hash_max);
1054 if (err)
1055 return err;
1056 }
1057
1058 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1059 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1060
1061 br->multicast_last_member_count = val;
1062 }
1063
1064 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1065 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1066
1067 br->multicast_startup_query_count = val;
1068 }
1069
1070 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1071 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1072
1073 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1074 }
1075
1076 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1077 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1078
1079 br->multicast_membership_interval = clock_t_to_jiffies(val);
1080 }
1081
1082 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1083 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1084
1085 br->multicast_querier_interval = clock_t_to_jiffies(val);
1086 }
1087
1088 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1089 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1090
1091 br->multicast_query_interval = clock_t_to_jiffies(val);
1092 }
1093
1094 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1095 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1096
1097 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1098 }
1099
1100 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1101 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1102
1103 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1104 }
1105
1106 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1107 __u8 mcast_stats;
1108
1109 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1110 br->multicast_stats_enabled = !!mcast_stats;
1111 }
1112
1113 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1114 __u8 igmp_version;
1115
1116 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1117 err = br_multicast_set_igmp_version(br, igmp_version);
1118 if (err)
1119 return err;
1120 }
1121
1122 #if IS_ENABLED(CONFIG_IPV6)
1123 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1124 __u8 mld_version;
1125
1126 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1127 err = br_multicast_set_mld_version(br, mld_version);
1128 if (err)
1129 return err;
1130 }
1131 #endif
1132 #endif
1133 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1134 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1135 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1136
1137 br->nf_call_iptables = val ? true : false;
1138 }
1139
1140 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1141 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1142
1143 br->nf_call_ip6tables = val ? true : false;
1144 }
1145
1146 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1147 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1148
1149 br->nf_call_arptables = val ? true : false;
1150 }
1151 #endif
1152
1153 return 0;
1154 }
1155
1156 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1157 struct nlattr *tb[], struct nlattr *data[])
1158 {
1159 struct net_bridge *br = netdev_priv(dev);
1160 int err;
1161
1162 if (tb[IFLA_ADDRESS]) {
1163 spin_lock_bh(&br->lock);
1164 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1165 spin_unlock_bh(&br->lock);
1166 }
1167
1168 err = br_changelink(dev, tb, data);
1169 if (err)
1170 return err;
1171
1172 return register_netdevice(dev);
1173 }
1174
1175 static size_t br_get_size(const struct net_device *brdev)
1176 {
1177 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1178 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1179 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1180 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1181 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1182 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1183 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1184 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1185 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1186 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1187 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
1188 #endif
1189 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1190 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1191 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1192 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1193 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1194 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1195 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1196 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1197 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1198 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1199 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1200 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1201 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1202 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1203 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1204 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1205 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1206 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
1207 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1208 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1209 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1210 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1211 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1212 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1213 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1214 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1215 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1216 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1217 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
1218 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
1219 #endif
1220 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1221 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1222 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1223 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1224 #endif
1225 0;
1226 }
1227
1228 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1229 {
1230 struct net_bridge *br = netdev_priv(brdev);
1231 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1232 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1233 u32 age_time = jiffies_to_clock_t(br->max_age);
1234 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1235 u32 stp_enabled = br->stp_enabled;
1236 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1237 u8 vlan_enabled = br_vlan_enabled(br);
1238 u64 clockval;
1239
1240 clockval = br_timer_value(&br->hello_timer);
1241 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1242 return -EMSGSIZE;
1243 clockval = br_timer_value(&br->tcn_timer);
1244 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1245 return -EMSGSIZE;
1246 clockval = br_timer_value(&br->topology_change_timer);
1247 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1248 IFLA_BR_PAD))
1249 return -EMSGSIZE;
1250 clockval = br_timer_value(&br->gc_work.timer);
1251 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1252 return -EMSGSIZE;
1253
1254 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1255 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1256 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1257 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1258 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1259 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1260 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1261 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1262 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1263 &br->bridge_id) ||
1264 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1265 &br->designated_root) ||
1266 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1267 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1268 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1269 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1270 br->topology_change_detected) ||
1271 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1272 return -EMSGSIZE;
1273
1274 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1275 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1276 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1277 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED, br->vlan_stats_enabled))
1278 return -EMSGSIZE;
1279 #endif
1280 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1281 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1282 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1283 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1284 br->multicast_query_use_ifaddr) ||
1285 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1286 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1287 br->multicast_stats_enabled) ||
1288 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1289 br->hash_elasticity) ||
1290 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1291 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1292 br->multicast_last_member_count) ||
1293 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1294 br->multicast_startup_query_count) ||
1295 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1296 br->multicast_igmp_version))
1297 return -EMSGSIZE;
1298 #if IS_ENABLED(CONFIG_IPV6)
1299 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1300 br->multicast_mld_version))
1301 return -EMSGSIZE;
1302 #endif
1303 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1304 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1305 IFLA_BR_PAD))
1306 return -EMSGSIZE;
1307 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1308 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1309 IFLA_BR_PAD))
1310 return -EMSGSIZE;
1311 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1312 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1313 IFLA_BR_PAD))
1314 return -EMSGSIZE;
1315 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1316 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1317 IFLA_BR_PAD))
1318 return -EMSGSIZE;
1319 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1320 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1321 IFLA_BR_PAD))
1322 return -EMSGSIZE;
1323 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1324 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1325 IFLA_BR_PAD))
1326 return -EMSGSIZE;
1327 #endif
1328 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1329 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1330 br->nf_call_iptables ? 1 : 0) ||
1331 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1332 br->nf_call_ip6tables ? 1 : 0) ||
1333 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1334 br->nf_call_arptables ? 1 : 0))
1335 return -EMSGSIZE;
1336 #endif
1337
1338 return 0;
1339 }
1340
1341 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1342 {
1343 struct net_bridge_port *p = NULL;
1344 struct net_bridge_vlan_group *vg;
1345 struct net_bridge_vlan *v;
1346 struct net_bridge *br;
1347 int numvls = 0;
1348
1349 switch (attr) {
1350 case IFLA_STATS_LINK_XSTATS:
1351 br = netdev_priv(dev);
1352 vg = br_vlan_group(br);
1353 break;
1354 case IFLA_STATS_LINK_XSTATS_SLAVE:
1355 p = br_port_get_rtnl(dev);
1356 if (!p)
1357 return 0;
1358 br = p->br;
1359 vg = nbp_vlan_group(p);
1360 break;
1361 default:
1362 return 0;
1363 }
1364
1365 if (vg) {
1366 /* we need to count all, even placeholder entries */
1367 list_for_each_entry(v, &vg->vlan_list, vlist)
1368 numvls++;
1369 }
1370
1371 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1372 nla_total_size(sizeof(struct br_mcast_stats)) +
1373 nla_total_size(0);
1374 }
1375
1376 static int br_fill_linkxstats(struct sk_buff *skb,
1377 const struct net_device *dev,
1378 int *prividx, int attr)
1379 {
1380 struct nlattr *nla __maybe_unused;
1381 struct net_bridge_port *p = NULL;
1382 struct net_bridge_vlan_group *vg;
1383 struct net_bridge_vlan *v;
1384 struct net_bridge *br;
1385 struct nlattr *nest;
1386 int vl_idx = 0;
1387
1388 switch (attr) {
1389 case IFLA_STATS_LINK_XSTATS:
1390 br = netdev_priv(dev);
1391 vg = br_vlan_group(br);
1392 break;
1393 case IFLA_STATS_LINK_XSTATS_SLAVE:
1394 p = br_port_get_rtnl(dev);
1395 if (!p)
1396 return 0;
1397 br = p->br;
1398 vg = nbp_vlan_group(p);
1399 break;
1400 default:
1401 return -EINVAL;
1402 }
1403
1404 nest = nla_nest_start(skb, LINK_XSTATS_TYPE_BRIDGE);
1405 if (!nest)
1406 return -EMSGSIZE;
1407
1408 if (vg) {
1409 u16 pvid;
1410
1411 pvid = br_get_pvid(vg);
1412 list_for_each_entry(v, &vg->vlan_list, vlist) {
1413 struct bridge_vlan_xstats vxi;
1414 struct br_vlan_stats stats;
1415
1416 if (++vl_idx < *prividx)
1417 continue;
1418 memset(&vxi, 0, sizeof(vxi));
1419 vxi.vid = v->vid;
1420 vxi.flags = v->flags;
1421 if (v->vid == pvid)
1422 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1423 br_vlan_get_stats(v, &stats);
1424 vxi.rx_bytes = stats.rx_bytes;
1425 vxi.rx_packets = stats.rx_packets;
1426 vxi.tx_bytes = stats.tx_bytes;
1427 vxi.tx_packets = stats.tx_packets;
1428
1429 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1430 goto nla_put_failure;
1431 }
1432 }
1433
1434 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1435 if (++vl_idx >= *prividx) {
1436 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1437 sizeof(struct br_mcast_stats),
1438 BRIDGE_XSTATS_PAD);
1439 if (!nla)
1440 goto nla_put_failure;
1441 br_multicast_get_stats(br, p, nla_data(nla));
1442 }
1443 #endif
1444 nla_nest_end(skb, nest);
1445 *prividx = 0;
1446
1447 return 0;
1448
1449 nla_put_failure:
1450 nla_nest_end(skb, nest);
1451 *prividx = vl_idx;
1452
1453 return -EMSGSIZE;
1454 }
1455
1456 static struct rtnl_af_ops br_af_ops __read_mostly = {
1457 .family = AF_BRIDGE,
1458 .get_link_af_size = br_get_link_af_size_filtered,
1459 };
1460
1461 struct rtnl_link_ops br_link_ops __read_mostly = {
1462 .kind = "bridge",
1463 .priv_size = sizeof(struct net_bridge),
1464 .setup = br_dev_setup,
1465 .maxtype = IFLA_BR_MAX,
1466 .policy = br_policy,
1467 .validate = br_validate,
1468 .newlink = br_dev_newlink,
1469 .changelink = br_changelink,
1470 .dellink = br_dev_delete,
1471 .get_size = br_get_size,
1472 .fill_info = br_fill_info,
1473 .fill_linkxstats = br_fill_linkxstats,
1474 .get_linkxstats_size = br_get_linkxstats_size,
1475
1476 .slave_maxtype = IFLA_BRPORT_MAX,
1477 .slave_policy = br_port_policy,
1478 .slave_changelink = br_port_slave_changelink,
1479 .get_slave_size = br_port_get_slave_size,
1480 .fill_slave_info = br_port_fill_slave_info,
1481 };
1482
1483 int __init br_netlink_init(void)
1484 {
1485 int err;
1486
1487 br_mdb_init();
1488 rtnl_af_register(&br_af_ops);
1489
1490 err = rtnl_link_register(&br_link_ops);
1491 if (err)
1492 goto out_af;
1493
1494 return 0;
1495
1496 out_af:
1497 rtnl_af_unregister(&br_af_ops);
1498 br_mdb_uninit();
1499 return err;
1500 }
1501
1502 void br_netlink_fini(void)
1503 {
1504 br_mdb_uninit();
1505 rtnl_af_unregister(&br_af_ops);
1506 rtnl_link_unregister(&br_link_ops);
1507 }