bridge: netlink: add group_addr support
[linux/fpc-iii.git] / net / bridge / br_netlink.c
bloba05a4306d42d9901128c7a369a9f1b53822de8b6
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 },
772 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
773 struct nlattr *data[])
775 struct net_bridge *br = netdev_priv(brdev);
776 int err;
778 if (!data)
779 return 0;
781 if (data[IFLA_BR_FORWARD_DELAY]) {
782 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
783 if (err)
784 return err;
787 if (data[IFLA_BR_HELLO_TIME]) {
788 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
789 if (err)
790 return err;
793 if (data[IFLA_BR_MAX_AGE]) {
794 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
795 if (err)
796 return err;
799 if (data[IFLA_BR_AGEING_TIME]) {
800 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
802 br->ageing_time = clock_t_to_jiffies(ageing_time);
805 if (data[IFLA_BR_STP_STATE]) {
806 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
808 br_stp_set_enabled(br, stp_enabled);
811 if (data[IFLA_BR_PRIORITY]) {
812 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
814 br_stp_set_bridge_priority(br, priority);
817 if (data[IFLA_BR_VLAN_FILTERING]) {
818 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
820 err = __br_vlan_filter_toggle(br, vlan_filter);
821 if (err)
822 return err;
825 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
826 if (data[IFLA_BR_VLAN_PROTOCOL]) {
827 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
829 err = __br_vlan_set_proto(br, vlan_proto);
830 if (err)
831 return err;
833 #endif
835 if (data[IFLA_BR_GROUP_FWD_MASK]) {
836 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
838 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
839 return -EINVAL;
840 br->group_fwd_mask = fwd_mask;
843 if (data[IFLA_BR_GROUP_ADDR]) {
844 u8 new_addr[ETH_ALEN];
846 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
847 return -EINVAL;
848 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
849 if (!is_link_local_ether_addr(new_addr))
850 return -EINVAL;
851 if (new_addr[5] == 1 || /* 802.3x Pause address */
852 new_addr[5] == 2 || /* 802.3ad Slow protocols */
853 new_addr[5] == 3) /* 802.1X PAE address */
854 return -EINVAL;
855 spin_lock_bh(&br->lock);
856 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
857 spin_unlock_bh(&br->lock);
858 br->group_addr_set = true;
859 br_recalculate_fwd_mask(br);
862 return 0;
865 static size_t br_get_size(const struct net_device *brdev)
867 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
868 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
869 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
870 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
871 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
872 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
873 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
874 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
875 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
876 #endif
877 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
878 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
879 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
880 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
881 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
882 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
883 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
884 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
885 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
886 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
887 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
888 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
892 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
894 struct net_bridge *br = netdev_priv(brdev);
895 u64 hello_timer, tcn_timer, topology_change_timer, gc_timer;
896 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
897 u32 hello_time = jiffies_to_clock_t(br->hello_time);
898 u32 age_time = jiffies_to_clock_t(br->max_age);
899 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
900 u32 stp_enabled = br->stp_enabled;
901 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
902 u16 group_fwd_mask = br->group_fwd_mask;
903 u8 vlan_enabled = br_vlan_enabled(br);
904 struct ifla_bridge_id root_id, bridge_id;
906 memset(&bridge_id, 0, sizeof(bridge_id));
907 memset(&root_id, 0, sizeof(root_id));
908 memcpy(root_id.prio, br->designated_root.prio, sizeof(root_id.prio));
909 memcpy(root_id.addr, br->designated_root.addr, sizeof(root_id.addr));
910 memcpy(bridge_id.prio, br->bridge_id.prio, sizeof(bridge_id.prio));
911 memcpy(bridge_id.addr, br->bridge_id.addr, sizeof(bridge_id.addr));
912 hello_timer = br_timer_value(&br->hello_timer);
913 tcn_timer = br_timer_value(&br->tcn_timer);
914 topology_change_timer = br_timer_value(&br->topology_change_timer);
915 gc_timer = br_timer_value(&br->gc_timer);
917 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
918 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
919 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
920 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
921 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
922 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
923 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
924 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, group_fwd_mask) ||
925 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(root_id), &root_id) ||
926 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(bridge_id), &bridge_id) ||
927 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
928 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
929 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
930 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
931 br->topology_change_detected) ||
932 nla_put_u64(skb, IFLA_BR_HELLO_TIMER, hello_timer) ||
933 nla_put_u64(skb, IFLA_BR_TCN_TIMER, tcn_timer) ||
934 nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER,
935 topology_change_timer) ||
936 nla_put_u64(skb, IFLA_BR_GC_TIMER, gc_timer) ||
937 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
938 return -EMSGSIZE;
940 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
941 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto))
942 return -EMSGSIZE;
943 #endif
945 return 0;
948 static size_t br_get_link_af_size(const struct net_device *dev)
950 struct net_bridge_port *p;
951 struct net_bridge *br;
952 int num_vlans = 0;
954 if (br_port_exists(dev)) {
955 p = br_port_get_rtnl(dev);
956 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
957 RTEXT_FILTER_BRVLAN);
958 } else if (dev->priv_flags & IFF_EBRIDGE) {
959 br = netdev_priv(dev);
960 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
961 RTEXT_FILTER_BRVLAN);
964 /* Each VLAN is returned in bridge_vlan_info along with flags */
965 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
968 static struct rtnl_af_ops br_af_ops __read_mostly = {
969 .family = AF_BRIDGE,
970 .get_link_af_size = br_get_link_af_size,
973 struct rtnl_link_ops br_link_ops __read_mostly = {
974 .kind = "bridge",
975 .priv_size = sizeof(struct net_bridge),
976 .setup = br_dev_setup,
977 .maxtype = IFLA_BR_MAX,
978 .policy = br_policy,
979 .validate = br_validate,
980 .newlink = br_dev_newlink,
981 .changelink = br_changelink,
982 .dellink = br_dev_delete,
983 .get_size = br_get_size,
984 .fill_info = br_fill_info,
986 .slave_maxtype = IFLA_BRPORT_MAX,
987 .slave_policy = br_port_policy,
988 .slave_changelink = br_port_slave_changelink,
989 .get_slave_size = br_port_get_slave_size,
990 .fill_slave_info = br_port_fill_slave_info,
993 int __init br_netlink_init(void)
995 int err;
997 br_mdb_init();
998 rtnl_af_register(&br_af_ops);
1000 err = rtnl_link_register(&br_link_ops);
1001 if (err)
1002 goto out_af;
1004 return 0;
1006 out_af:
1007 rtnl_af_unregister(&br_af_ops);
1008 br_mdb_uninit();
1009 return err;
1012 void br_netlink_fini(void)
1014 br_mdb_uninit();
1015 rtnl_af_unregister(&br_af_ops);
1016 rtnl_link_unregister(&br_link_ops);