bridge: netlink: add support for default_pvid
[linux/fpc-iii.git] / net / bridge / br_netlink.c
blob70efe2edde2bf77d25f3a585fdcce0ea51748cf1
1 /*
2 * Bridge netlink control interface
4 * Authors:
5 * Stephen Hemminger <shemminger@osdl.org>
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.
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>
21 #include "br_private.h"
22 #include "br_private_stp.h"
24 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
25 u32 filter_mask)
27 struct net_bridge_vlan *v;
28 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
29 u16 flags, pvid;
30 int num_vlans = 0;
32 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
33 return 0;
35 pvid = br_get_pvid(vg);
36 /* Count number of vlan infos */
37 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
38 flags = 0;
39 /* only a context, bridge vlan not activated */
40 if (!br_vlan_should_use(v))
41 continue;
42 if (v->vid == pvid)
43 flags |= BRIDGE_VLAN_INFO_PVID;
45 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
46 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
48 if (vid_range_start == 0) {
49 goto initvars;
50 } else if ((v->vid - vid_range_end) == 1 &&
51 flags == vid_range_flags) {
52 vid_range_end = v->vid;
53 continue;
54 } else {
55 if ((vid_range_end - vid_range_start) > 0)
56 num_vlans += 2;
57 else
58 num_vlans += 1;
60 initvars:
61 vid_range_start = v->vid;
62 vid_range_end = v->vid;
63 vid_range_flags = flags;
66 if (vid_range_start != 0) {
67 if ((vid_range_end - vid_range_start) > 0)
68 num_vlans += 2;
69 else
70 num_vlans += 1;
73 return num_vlans;
76 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
77 u32 filter_mask)
79 int num_vlans;
81 if (!vg)
82 return 0;
84 if (filter_mask & RTEXT_FILTER_BRVLAN)
85 return vg->num_vlans;
87 rcu_read_lock();
88 num_vlans = __get_num_vlan_infos(vg, filter_mask);
89 rcu_read_unlock();
91 return num_vlans;
94 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
95 u32 filter_mask)
97 struct net_bridge_vlan_group *vg = NULL;
98 struct net_bridge_port *p;
99 struct net_bridge *br;
100 int num_vlan_infos;
102 rcu_read_lock();
103 if (br_port_exists(dev)) {
104 p = br_port_get_rcu(dev);
105 vg = nbp_vlan_group(p);
106 } else if (dev->priv_flags & IFF_EBRIDGE) {
107 br = netdev_priv(dev);
108 vg = br_vlan_group(br);
110 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
111 rcu_read_unlock();
113 /* Each VLAN is returned in bridge_vlan_info along with flags */
114 return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
117 static inline size_t br_port_info_size(void)
119 return nla_total_size(1) /* IFLA_BRPORT_STATE */
120 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
121 + nla_total_size(4) /* IFLA_BRPORT_COST */
122 + nla_total_size(1) /* IFLA_BRPORT_MODE */
123 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
124 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
125 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
126 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
127 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
128 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
129 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
130 + 0;
133 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
135 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
136 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
137 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
138 + nla_total_size(4) /* IFLA_MASTER */
139 + nla_total_size(4) /* IFLA_MTU */
140 + nla_total_size(4) /* IFLA_LINK */
141 + nla_total_size(1) /* IFLA_OPERSTATE */
142 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
143 + nla_total_size(br_get_link_af_size_filtered(dev,
144 filter_mask)); /* IFLA_AF_SPEC */
147 static int br_port_fill_attrs(struct sk_buff *skb,
148 const struct net_bridge_port *p)
150 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
152 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
153 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
154 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
155 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
156 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
157 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
158 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
159 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
160 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
161 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
162 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
163 !!(p->flags & BR_PROXYARP_WIFI)))
164 return -EMSGSIZE;
166 return 0;
169 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
170 u16 vid_end, u16 flags)
172 struct bridge_vlan_info vinfo;
174 if ((vid_end - vid_start) > 0) {
175 /* add range to skb */
176 vinfo.vid = vid_start;
177 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
178 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
179 sizeof(vinfo), &vinfo))
180 goto nla_put_failure;
182 vinfo.vid = vid_end;
183 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
184 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
185 sizeof(vinfo), &vinfo))
186 goto nla_put_failure;
187 } else {
188 vinfo.vid = vid_start;
189 vinfo.flags = flags;
190 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
191 sizeof(vinfo), &vinfo))
192 goto nla_put_failure;
195 return 0;
197 nla_put_failure:
198 return -EMSGSIZE;
201 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
202 struct net_bridge_vlan_group *vg)
204 struct net_bridge_vlan *v;
205 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
206 u16 flags, pvid;
207 int err = 0;
209 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
210 * and mark vlan info with begin and end flags
211 * if vlaninfo represents a range
213 pvid = br_get_pvid(vg);
214 list_for_each_entry(v, &vg->vlan_list, vlist) {
215 flags = 0;
216 if (!br_vlan_should_use(v))
217 continue;
218 if (v->vid == pvid)
219 flags |= BRIDGE_VLAN_INFO_PVID;
221 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
222 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
224 if (vid_range_start == 0) {
225 goto initvars;
226 } else if ((v->vid - vid_range_end) == 1 &&
227 flags == vid_range_flags) {
228 vid_range_end = v->vid;
229 continue;
230 } else {
231 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
232 vid_range_end,
233 vid_range_flags);
234 if (err)
235 return err;
238 initvars:
239 vid_range_start = v->vid;
240 vid_range_end = v->vid;
241 vid_range_flags = flags;
244 if (vid_range_start != 0) {
245 /* Call it once more to send any left over vlans */
246 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
247 vid_range_end,
248 vid_range_flags);
249 if (err)
250 return err;
253 return 0;
256 static int br_fill_ifvlaninfo(struct sk_buff *skb,
257 struct net_bridge_vlan_group *vg)
259 struct bridge_vlan_info vinfo;
260 struct net_bridge_vlan *v;
261 u16 pvid;
263 pvid = br_get_pvid(vg);
264 list_for_each_entry(v, &vg->vlan_list, vlist) {
265 if (!br_vlan_should_use(v))
266 continue;
268 vinfo.vid = v->vid;
269 vinfo.flags = 0;
270 if (v->vid == pvid)
271 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
273 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
274 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
276 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
277 sizeof(vinfo), &vinfo))
278 goto nla_put_failure;
281 return 0;
283 nla_put_failure:
284 return -EMSGSIZE;
288 * Create one netlink message for one interface
289 * Contains port and master info as well as carrier and bridge state.
291 static int br_fill_ifinfo(struct sk_buff *skb,
292 struct net_bridge_port *port,
293 u32 pid, u32 seq, int event, unsigned int flags,
294 u32 filter_mask, const struct net_device *dev)
296 struct net_bridge *br;
297 struct ifinfomsg *hdr;
298 struct nlmsghdr *nlh;
299 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
301 if (port)
302 br = port->br;
303 else
304 br = netdev_priv(dev);
306 br_debug(br, "br_fill_info event %d port %s master %s\n",
307 event, dev->name, br->dev->name);
309 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
310 if (nlh == NULL)
311 return -EMSGSIZE;
313 hdr = nlmsg_data(nlh);
314 hdr->ifi_family = AF_BRIDGE;
315 hdr->__ifi_pad = 0;
316 hdr->ifi_type = dev->type;
317 hdr->ifi_index = dev->ifindex;
318 hdr->ifi_flags = dev_get_flags(dev);
319 hdr->ifi_change = 0;
321 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
322 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
323 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
324 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
325 (dev->addr_len &&
326 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
327 (dev->ifindex != dev_get_iflink(dev) &&
328 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
329 goto nla_put_failure;
331 if (event == RTM_NEWLINK && port) {
332 struct nlattr *nest
333 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
335 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
336 goto nla_put_failure;
337 nla_nest_end(skb, nest);
340 /* Check if the VID information is requested */
341 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
342 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
343 struct net_bridge_vlan_group *vg;
344 struct nlattr *af;
345 int err;
347 if (port)
348 vg = nbp_vlan_group(port);
349 else
350 vg = br_vlan_group(br);
352 if (!vg || !vg->num_vlans)
353 goto done;
355 af = nla_nest_start(skb, IFLA_AF_SPEC);
356 if (!af)
357 goto nla_put_failure;
359 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
360 err = br_fill_ifvlaninfo_compressed(skb, vg);
361 else
362 err = br_fill_ifvlaninfo(skb, vg);
363 if (err)
364 goto nla_put_failure;
365 nla_nest_end(skb, af);
368 done:
369 nlmsg_end(skb, nlh);
370 return 0;
372 nla_put_failure:
373 nlmsg_cancel(skb, nlh);
374 return -EMSGSIZE;
378 * Notify listeners of a change in port information
380 void br_ifinfo_notify(int event, struct net_bridge_port *port)
382 struct net *net;
383 struct sk_buff *skb;
384 int err = -ENOBUFS;
385 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
387 if (!port)
388 return;
390 net = dev_net(port->dev);
391 br_debug(port->br, "port %u(%s) event %d\n",
392 (unsigned int)port->port_no, port->dev->name, event);
394 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
395 if (skb == NULL)
396 goto errout;
398 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
399 if (err < 0) {
400 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
401 WARN_ON(err == -EMSGSIZE);
402 kfree_skb(skb);
403 goto errout;
405 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
406 return;
407 errout:
408 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
413 * Dump information about all ports, in response to GETLINK
415 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
416 struct net_device *dev, u32 filter_mask, int nlflags)
418 struct net_bridge_port *port = br_port_get_rtnl(dev);
420 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
421 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
422 return 0;
424 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
425 filter_mask, dev);
428 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
429 int cmd, struct bridge_vlan_info *vinfo)
431 int err = 0;
433 switch (cmd) {
434 case RTM_SETLINK:
435 if (p) {
436 /* if the MASTER flag is set this will act on the global
437 * per-VLAN entry as well
439 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
440 if (err)
441 break;
442 } else {
443 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
444 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
446 break;
448 case RTM_DELLINK:
449 if (p) {
450 nbp_vlan_delete(p, vinfo->vid);
451 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
452 br_vlan_delete(p->br, vinfo->vid);
453 } else {
454 br_vlan_delete(br, vinfo->vid);
456 break;
459 return err;
462 static int br_afspec(struct net_bridge *br,
463 struct net_bridge_port *p,
464 struct nlattr *af_spec,
465 int cmd)
467 struct bridge_vlan_info *vinfo_start = NULL;
468 struct bridge_vlan_info *vinfo = NULL;
469 struct nlattr *attr;
470 int err = 0;
471 int rem;
473 nla_for_each_nested(attr, af_spec, rem) {
474 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
475 continue;
476 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
477 return -EINVAL;
478 vinfo = nla_data(attr);
479 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
480 return -EINVAL;
481 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
482 if (vinfo_start)
483 return -EINVAL;
484 vinfo_start = vinfo;
485 continue;
488 if (vinfo_start) {
489 struct bridge_vlan_info tmp_vinfo;
490 int v;
492 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
493 return -EINVAL;
495 if (vinfo->vid <= vinfo_start->vid)
496 return -EINVAL;
498 memcpy(&tmp_vinfo, vinfo_start,
499 sizeof(struct bridge_vlan_info));
501 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
502 tmp_vinfo.vid = v;
503 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
504 if (err)
505 break;
507 vinfo_start = NULL;
508 } else {
509 err = br_vlan_info(br, p, cmd, vinfo);
511 if (err)
512 break;
515 return err;
518 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
519 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
520 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
521 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
522 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
523 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
524 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
525 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
526 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
527 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
528 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
529 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
532 /* Change the state of the port and notify spanning tree */
533 static int br_set_port_state(struct net_bridge_port *p, u8 state)
535 if (state > BR_STATE_BLOCKING)
536 return -EINVAL;
538 /* if kernel STP is running, don't allow changes */
539 if (p->br->stp_enabled == BR_KERNEL_STP)
540 return -EBUSY;
542 /* if device is not up, change is not allowed
543 * if link is not present, only allowable state is disabled
545 if (!netif_running(p->dev) ||
546 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
547 return -ENETDOWN;
549 br_set_state(p, state);
550 br_log_state(p);
551 br_port_state_selection(p->br);
552 return 0;
555 /* Set/clear or port flags based on attribute */
556 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
557 int attrtype, unsigned long mask)
559 if (tb[attrtype]) {
560 u8 flag = nla_get_u8(tb[attrtype]);
561 if (flag)
562 p->flags |= mask;
563 else
564 p->flags &= ~mask;
568 /* Process bridge protocol info on port */
569 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
571 int err;
572 unsigned long old_flags = p->flags;
574 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
575 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
576 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
577 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
578 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
579 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
580 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
581 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
583 if (tb[IFLA_BRPORT_COST]) {
584 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
585 if (err)
586 return err;
589 if (tb[IFLA_BRPORT_PRIORITY]) {
590 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
591 if (err)
592 return err;
595 if (tb[IFLA_BRPORT_STATE]) {
596 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
597 if (err)
598 return err;
601 br_port_flags_change(p, old_flags ^ p->flags);
602 return 0;
605 /* Change state and parameters on port. */
606 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
608 struct nlattr *protinfo;
609 struct nlattr *afspec;
610 struct net_bridge_port *p;
611 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
612 int err = 0;
614 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
615 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
616 if (!protinfo && !afspec)
617 return 0;
619 p = br_port_get_rtnl(dev);
620 /* We want to accept dev as bridge itself if the AF_SPEC
621 * is set to see if someone is setting vlan info on the bridge
623 if (!p && !afspec)
624 return -EINVAL;
626 if (p && protinfo) {
627 if (protinfo->nla_type & NLA_F_NESTED) {
628 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
629 protinfo, br_port_policy);
630 if (err)
631 return err;
633 spin_lock_bh(&p->br->lock);
634 err = br_setport(p, tb);
635 spin_unlock_bh(&p->br->lock);
636 } else {
637 /* Binary compatibility with old RSTP */
638 if (nla_len(protinfo) < sizeof(u8))
639 return -EINVAL;
641 spin_lock_bh(&p->br->lock);
642 err = br_set_port_state(p, nla_get_u8(protinfo));
643 spin_unlock_bh(&p->br->lock);
645 if (err)
646 goto out;
649 if (afspec) {
650 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
651 afspec, RTM_SETLINK);
654 if (err == 0)
655 br_ifinfo_notify(RTM_NEWLINK, p);
656 out:
657 return err;
660 /* Delete port information */
661 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
663 struct nlattr *afspec;
664 struct net_bridge_port *p;
665 int err = 0;
667 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
668 if (!afspec)
669 return 0;
671 p = br_port_get_rtnl(dev);
672 /* We want to accept dev as bridge itself as well */
673 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
674 return -EINVAL;
676 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
677 afspec, RTM_DELLINK);
678 if (err == 0)
679 /* Send RTM_NEWLINK because userspace
680 * expects RTM_NEWLINK for vlan dels
682 br_ifinfo_notify(RTM_NEWLINK, p);
684 return err;
686 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
688 if (tb[IFLA_ADDRESS]) {
689 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
690 return -EINVAL;
691 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
692 return -EADDRNOTAVAIL;
695 if (!data)
696 return 0;
698 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
699 if (data[IFLA_BR_VLAN_PROTOCOL]) {
700 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
701 case htons(ETH_P_8021Q):
702 case htons(ETH_P_8021AD):
703 break;
704 default:
705 return -EPROTONOSUPPORT;
708 #endif
710 return 0;
713 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
714 struct nlattr *tb[], struct nlattr *data[])
716 struct net_bridge *br = netdev_priv(dev);
718 if (tb[IFLA_ADDRESS]) {
719 spin_lock_bh(&br->lock);
720 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
721 spin_unlock_bh(&br->lock);
724 return register_netdevice(dev);
727 static int br_port_slave_changelink(struct net_device *brdev,
728 struct net_device *dev,
729 struct nlattr *tb[],
730 struct nlattr *data[])
732 struct net_bridge *br = netdev_priv(brdev);
733 int ret;
735 if (!data)
736 return 0;
738 spin_lock_bh(&br->lock);
739 ret = br_setport(br_port_get_rtnl(dev), data);
740 spin_unlock_bh(&br->lock);
742 return ret;
745 static int br_port_fill_slave_info(struct sk_buff *skb,
746 const struct net_device *brdev,
747 const struct net_device *dev)
749 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
752 static size_t br_port_get_slave_size(const struct net_device *brdev,
753 const struct net_device *dev)
755 return br_port_info_size();
758 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
759 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
760 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
761 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
762 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
763 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
764 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
765 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
766 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
767 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
768 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
769 .len = ETH_ALEN },
770 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
771 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
772 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
773 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
774 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
775 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
776 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
777 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
778 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
779 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
780 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
781 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
782 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
783 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
784 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
785 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
786 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
787 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
790 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
791 struct nlattr *data[])
793 struct net_bridge *br = netdev_priv(brdev);
794 int err;
796 if (!data)
797 return 0;
799 if (data[IFLA_BR_FORWARD_DELAY]) {
800 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
801 if (err)
802 return err;
805 if (data[IFLA_BR_HELLO_TIME]) {
806 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
807 if (err)
808 return err;
811 if (data[IFLA_BR_MAX_AGE]) {
812 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
813 if (err)
814 return err;
817 if (data[IFLA_BR_AGEING_TIME]) {
818 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
820 br->ageing_time = clock_t_to_jiffies(ageing_time);
823 if (data[IFLA_BR_STP_STATE]) {
824 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
826 br_stp_set_enabled(br, stp_enabled);
829 if (data[IFLA_BR_PRIORITY]) {
830 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
832 br_stp_set_bridge_priority(br, priority);
835 if (data[IFLA_BR_VLAN_FILTERING]) {
836 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
838 err = __br_vlan_filter_toggle(br, vlan_filter);
839 if (err)
840 return err;
843 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
844 if (data[IFLA_BR_VLAN_PROTOCOL]) {
845 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
847 err = __br_vlan_set_proto(br, vlan_proto);
848 if (err)
849 return err;
852 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
853 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
855 err = __br_vlan_set_default_pvid(br, defpvid);
856 if (err)
857 return err;
859 #endif
861 if (data[IFLA_BR_GROUP_FWD_MASK]) {
862 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
864 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
865 return -EINVAL;
866 br->group_fwd_mask = fwd_mask;
869 if (data[IFLA_BR_GROUP_ADDR]) {
870 u8 new_addr[ETH_ALEN];
872 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
873 return -EINVAL;
874 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
875 if (!is_link_local_ether_addr(new_addr))
876 return -EINVAL;
877 if (new_addr[5] == 1 || /* 802.3x Pause address */
878 new_addr[5] == 2 || /* 802.3ad Slow protocols */
879 new_addr[5] == 3) /* 802.1X PAE address */
880 return -EINVAL;
881 spin_lock_bh(&br->lock);
882 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
883 spin_unlock_bh(&br->lock);
884 br->group_addr_set = true;
885 br_recalculate_fwd_mask(br);
888 if (data[IFLA_BR_FDB_FLUSH])
889 br_fdb_flush(br);
891 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
892 if (data[IFLA_BR_MCAST_ROUTER]) {
893 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
895 err = br_multicast_set_router(br, multicast_router);
896 if (err)
897 return err;
900 if (data[IFLA_BR_MCAST_SNOOPING]) {
901 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
903 err = br_multicast_toggle(br, mcast_snooping);
904 if (err)
905 return err;
908 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
909 u8 val;
911 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
912 br->multicast_query_use_ifaddr = !!val;
915 if (data[IFLA_BR_MCAST_QUERIER]) {
916 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
918 err = br_multicast_set_querier(br, mcast_querier);
919 if (err)
920 return err;
923 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
924 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
926 br->hash_elasticity = val;
929 if (data[IFLA_BR_MCAST_HASH_MAX]) {
930 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
932 err = br_multicast_set_hash_max(br, hash_max);
933 if (err)
934 return err;
937 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
938 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
940 br->multicast_last_member_count = val;
943 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
944 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
946 br->multicast_startup_query_count = val;
949 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
950 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
952 br->multicast_last_member_interval = clock_t_to_jiffies(val);
955 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
956 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
958 br->multicast_membership_interval = clock_t_to_jiffies(val);
961 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
962 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
964 br->multicast_querier_interval = clock_t_to_jiffies(val);
967 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
968 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
970 br->multicast_query_interval = clock_t_to_jiffies(val);
973 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
974 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
976 br->multicast_query_response_interval = clock_t_to_jiffies(val);
979 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
980 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
982 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
984 #endif
985 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
986 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
987 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
989 br->nf_call_iptables = val ? true : false;
992 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
993 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
995 br->nf_call_ip6tables = val ? true : false;
998 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
999 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1001 br->nf_call_arptables = val ? true : false;
1003 #endif
1005 return 0;
1008 static size_t br_get_size(const struct net_device *brdev)
1010 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1011 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1012 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1013 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1014 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1015 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1016 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1017 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1018 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1019 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1020 #endif
1021 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1022 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1023 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1024 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1025 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1026 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1027 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1028 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1029 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1030 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1031 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1032 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1033 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1034 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1035 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1036 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1037 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1038 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1039 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1040 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1041 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1042 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1043 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1044 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1045 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1046 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1047 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1048 #endif
1049 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1050 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1051 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1052 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1053 #endif
1057 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1059 struct net_bridge *br = netdev_priv(brdev);
1060 u64 hello_timer, tcn_timer, topology_change_timer, gc_timer, clockval;
1061 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1062 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1063 u32 age_time = jiffies_to_clock_t(br->max_age);
1064 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1065 u32 stp_enabled = br->stp_enabled;
1066 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1067 u16 group_fwd_mask = br->group_fwd_mask;
1068 u8 vlan_enabled = br_vlan_enabled(br);
1069 struct ifla_bridge_id root_id, bridge_id;
1071 memset(&bridge_id, 0, sizeof(bridge_id));
1072 memset(&root_id, 0, sizeof(root_id));
1073 memcpy(root_id.prio, br->designated_root.prio, sizeof(root_id.prio));
1074 memcpy(root_id.addr, br->designated_root.addr, sizeof(root_id.addr));
1075 memcpy(bridge_id.prio, br->bridge_id.prio, sizeof(bridge_id.prio));
1076 memcpy(bridge_id.addr, br->bridge_id.addr, sizeof(bridge_id.addr));
1077 hello_timer = br_timer_value(&br->hello_timer);
1078 tcn_timer = br_timer_value(&br->tcn_timer);
1079 topology_change_timer = br_timer_value(&br->topology_change_timer);
1080 gc_timer = br_timer_value(&br->gc_timer);
1081 clockval = 0;
1083 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1084 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1085 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1086 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1087 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1088 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1089 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1090 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, group_fwd_mask) ||
1091 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(root_id), &root_id) ||
1092 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(bridge_id), &bridge_id) ||
1093 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1094 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1095 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1096 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1097 br->topology_change_detected) ||
1098 nla_put_u64(skb, IFLA_BR_HELLO_TIMER, hello_timer) ||
1099 nla_put_u64(skb, IFLA_BR_TCN_TIMER, tcn_timer) ||
1100 nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER,
1101 topology_change_timer) ||
1102 nla_put_u64(skb, IFLA_BR_GC_TIMER, gc_timer) ||
1103 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1104 return -EMSGSIZE;
1106 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1107 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1108 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
1109 return -EMSGSIZE;
1110 #endif
1111 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1112 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1113 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1114 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1115 br->multicast_query_use_ifaddr) ||
1116 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1117 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1118 br->hash_elasticity) ||
1119 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1120 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1121 br->multicast_last_member_count) ||
1122 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1123 br->multicast_startup_query_count))
1124 return -EMSGSIZE;
1126 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1127 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1128 return -EMSGSIZE;
1129 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1130 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1131 return -EMSGSIZE;
1132 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1133 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1134 return -EMSGSIZE;
1135 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1136 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1137 return -EMSGSIZE;
1138 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1139 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1140 return -EMSGSIZE;
1141 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1142 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1143 return -EMSGSIZE;
1144 #endif
1145 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1146 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1147 br->nf_call_iptables ? 1 : 0) ||
1148 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1149 br->nf_call_ip6tables ? 1 : 0) ||
1150 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1151 br->nf_call_arptables ? 1 : 0))
1152 return -EMSGSIZE;
1153 #endif
1155 return 0;
1158 static size_t br_get_link_af_size(const struct net_device *dev)
1160 struct net_bridge_port *p;
1161 struct net_bridge *br;
1162 int num_vlans = 0;
1164 if (br_port_exists(dev)) {
1165 p = br_port_get_rtnl(dev);
1166 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
1167 RTEXT_FILTER_BRVLAN);
1168 } else if (dev->priv_flags & IFF_EBRIDGE) {
1169 br = netdev_priv(dev);
1170 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
1171 RTEXT_FILTER_BRVLAN);
1174 /* Each VLAN is returned in bridge_vlan_info along with flags */
1175 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
1178 static struct rtnl_af_ops br_af_ops __read_mostly = {
1179 .family = AF_BRIDGE,
1180 .get_link_af_size = br_get_link_af_size,
1183 struct rtnl_link_ops br_link_ops __read_mostly = {
1184 .kind = "bridge",
1185 .priv_size = sizeof(struct net_bridge),
1186 .setup = br_dev_setup,
1187 .maxtype = IFLA_BR_MAX,
1188 .policy = br_policy,
1189 .validate = br_validate,
1190 .newlink = br_dev_newlink,
1191 .changelink = br_changelink,
1192 .dellink = br_dev_delete,
1193 .get_size = br_get_size,
1194 .fill_info = br_fill_info,
1196 .slave_maxtype = IFLA_BRPORT_MAX,
1197 .slave_policy = br_port_policy,
1198 .slave_changelink = br_port_slave_changelink,
1199 .get_slave_size = br_port_get_slave_size,
1200 .fill_slave_info = br_port_fill_slave_info,
1203 int __init br_netlink_init(void)
1205 int err;
1207 br_mdb_init();
1208 rtnl_af_register(&br_af_ops);
1210 err = rtnl_link_register(&br_link_ops);
1211 if (err)
1212 goto out_af;
1214 return 0;
1216 out_af:
1217 rtnl_af_unregister(&br_af_ops);
1218 br_mdb_uninit();
1219 return err;
1222 void br_netlink_fini(void)
1224 br_mdb_uninit();
1225 rtnl_af_unregister(&br_af_ops);
1226 rtnl_link_unregister(&br_link_ops);