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