2 * Bridge netlink control interface
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>
19 #include <uapi/linux/if_bridge.h>
21 #include "br_private.h"
22 #include "br_private_stp.h"
24 static inline size_t br_port_info_size(void)
26 return nla_total_size(1) /* IFLA_BRPORT_STATE */
27 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
28 + nla_total_size(4) /* IFLA_BRPORT_COST */
29 + nla_total_size(1) /* IFLA_BRPORT_MODE */
30 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
31 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
32 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
33 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
34 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
38 static inline size_t br_nlmsg_size(void)
40 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
41 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
42 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
43 + nla_total_size(4) /* IFLA_MASTER */
44 + nla_total_size(4) /* IFLA_MTU */
45 + nla_total_size(4) /* IFLA_LINK */
46 + nla_total_size(1) /* IFLA_OPERSTATE */
47 + nla_total_size(br_port_info_size()); /* IFLA_PROTINFO */
50 static int br_port_fill_attrs(struct sk_buff *skb,
51 const struct net_bridge_port *p)
53 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
55 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
56 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
57 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
58 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
59 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
60 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
61 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
62 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
63 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
64 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)))
70 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
71 u16 vid_end, u16 flags)
73 struct bridge_vlan_info vinfo;
75 if ((vid_end - vid_start) > 0) {
76 /* add range to skb */
77 vinfo.vid = vid_start;
78 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
79 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
80 sizeof(vinfo), &vinfo))
83 vinfo.flags &= ~BRIDGE_VLAN_INFO_RANGE_BEGIN;
86 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
87 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
88 sizeof(vinfo), &vinfo))
91 vinfo.vid = vid_start;
93 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
94 sizeof(vinfo), &vinfo))
104 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
105 const struct net_port_vlans *pv)
107 u16 vid_range_start = 0, vid_range_end = 0;
108 u16 vid_range_flags = 0;
109 u16 pvid, vid, flags;
112 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
113 * and mark vlan info with begin and end flags
114 * if vlaninfo represents a range
116 pvid = br_get_pvid(pv);
117 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
120 flags |= BRIDGE_VLAN_INFO_PVID;
122 if (test_bit(vid, pv->untagged_bitmap))
123 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
125 if (vid_range_start == 0) {
127 } else if ((vid - vid_range_end) == 1 &&
128 flags == vid_range_flags) {
132 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
140 vid_range_start = vid;
142 vid_range_flags = flags;
145 if (vid_range_start != 0) {
146 /* Call it once more to send any left over vlans */
147 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
157 static int br_fill_ifvlaninfo(struct sk_buff *skb,
158 const struct net_port_vlans *pv)
160 struct bridge_vlan_info vinfo;
163 pvid = br_get_pvid(pv);
164 for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
168 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
170 if (test_bit(vid, pv->untagged_bitmap))
171 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
173 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
174 sizeof(vinfo), &vinfo))
175 goto nla_put_failure;
185 * Create one netlink message for one interface
186 * Contains port and master info as well as carrier and bridge state.
188 static int br_fill_ifinfo(struct sk_buff *skb,
189 const struct net_bridge_port *port,
190 u32 pid, u32 seq, int event, unsigned int flags,
191 u32 filter_mask, const struct net_device *dev)
193 const struct net_bridge *br;
194 struct ifinfomsg *hdr;
195 struct nlmsghdr *nlh;
196 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
201 br = netdev_priv(dev);
203 br_debug(br, "br_fill_info event %d port %s master %s\n",
204 event, dev->name, br->dev->name);
206 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
210 hdr = nlmsg_data(nlh);
211 hdr->ifi_family = AF_BRIDGE;
213 hdr->ifi_type = dev->type;
214 hdr->ifi_index = dev->ifindex;
215 hdr->ifi_flags = dev_get_flags(dev);
218 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
219 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
220 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
221 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
223 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
224 (dev->ifindex != dev->iflink &&
225 nla_put_u32(skb, IFLA_LINK, dev->iflink)))
226 goto nla_put_failure;
228 if (event == RTM_NEWLINK && port) {
230 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
232 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
233 goto nla_put_failure;
234 nla_nest_end(skb, nest);
237 /* Check if the VID information is requested */
238 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
239 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
240 const struct net_port_vlans *pv;
245 pv = nbp_get_vlan_info(port);
247 pv = br_get_vlan_info(br);
249 if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
252 af = nla_nest_start(skb, IFLA_AF_SPEC);
254 goto nla_put_failure;
256 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
257 err = br_fill_ifvlaninfo_compressed(skb, pv);
259 err = br_fill_ifvlaninfo(skb, pv);
261 goto nla_put_failure;
262 nla_nest_end(skb, af);
270 nlmsg_cancel(skb, nlh);
275 * Notify listeners of a change in port information
277 void br_ifinfo_notify(int event, struct net_bridge_port *port)
286 net = dev_net(port->dev);
287 br_debug(port->br, "port %u(%s) event %d\n",
288 (unsigned int)port->port_no, port->dev->name, event);
290 skb = nlmsg_new(br_nlmsg_size(), GFP_ATOMIC);
294 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, 0, port->dev);
296 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
297 WARN_ON(err == -EMSGSIZE);
301 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
304 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
309 * Dump information about all ports, in response to GETLINK
311 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
312 struct net_device *dev, u32 filter_mask)
315 struct net_bridge_port *port = br_port_get_rtnl(dev);
317 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
318 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
321 err = br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, NLM_F_MULTI,
327 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
328 int cmd, struct bridge_vlan_info *vinfo)
335 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
339 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
340 err = br_vlan_add(p->br, vinfo->vid,
343 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
349 nbp_vlan_delete(p, vinfo->vid);
350 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
351 br_vlan_delete(p->br, vinfo->vid);
353 br_vlan_delete(br, vinfo->vid);
361 static int br_afspec(struct net_bridge *br,
362 struct net_bridge_port *p,
363 struct nlattr *af_spec,
366 struct bridge_vlan_info *vinfo_start = NULL;
367 struct bridge_vlan_info *vinfo = NULL;
372 nla_for_each_nested(attr, af_spec, rem) {
373 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
375 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
377 vinfo = nla_data(attr);
378 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
386 struct bridge_vlan_info tmp_vinfo;
389 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
392 if (vinfo->vid <= vinfo_start->vid)
395 memcpy(&tmp_vinfo, vinfo_start,
396 sizeof(struct bridge_vlan_info));
398 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
400 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
406 err = br_vlan_info(br, p, cmd, vinfo);
415 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
416 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
417 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
418 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
419 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
420 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
421 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
422 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
423 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
424 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
427 /* Change the state of the port and notify spanning tree */
428 static int br_set_port_state(struct net_bridge_port *p, u8 state)
430 if (state > BR_STATE_BLOCKING)
433 /* if kernel STP is running, don't allow changes */
434 if (p->br->stp_enabled == BR_KERNEL_STP)
437 /* if device is not up, change is not allowed
438 * if link is not present, only allowable state is disabled
440 if (!netif_running(p->dev) ||
441 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
444 br_set_state(p, state);
446 br_port_state_selection(p->br);
450 /* Set/clear or port flags based on attribute */
451 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
452 int attrtype, unsigned long mask)
455 u8 flag = nla_get_u8(tb[attrtype]);
463 /* Process bridge protocol info on port */
464 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
467 unsigned long old_flags = p->flags;
469 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
470 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
471 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
472 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
473 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
474 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
475 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
477 if (tb[IFLA_BRPORT_COST]) {
478 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
483 if (tb[IFLA_BRPORT_PRIORITY]) {
484 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
489 if (tb[IFLA_BRPORT_STATE]) {
490 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
495 br_port_flags_change(p, old_flags ^ p->flags);
499 /* Change state and parameters on port. */
500 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh)
502 struct nlattr *protinfo;
503 struct nlattr *afspec;
504 struct net_bridge_port *p;
505 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
508 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
509 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
510 if (!protinfo && !afspec)
513 p = br_port_get_rtnl(dev);
514 /* We want to accept dev as bridge itself if the AF_SPEC
515 * is set to see if someone is setting vlan info on the bridge
521 if (protinfo->nla_type & NLA_F_NESTED) {
522 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
523 protinfo, br_port_policy);
527 spin_lock_bh(&p->br->lock);
528 err = br_setport(p, tb);
529 spin_unlock_bh(&p->br->lock);
531 /* Binary compatibility with old RSTP */
532 if (nla_len(protinfo) < sizeof(u8))
535 spin_lock_bh(&p->br->lock);
536 err = br_set_port_state(p, nla_get_u8(protinfo));
537 spin_unlock_bh(&p->br->lock);
544 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
545 afspec, RTM_SETLINK);
549 br_ifinfo_notify(RTM_NEWLINK, p);
555 /* Delete port information */
556 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh)
558 struct nlattr *afspec;
559 struct net_bridge_port *p;
562 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
566 p = br_port_get_rtnl(dev);
567 /* We want to accept dev as bridge itself as well */
568 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
571 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
572 afspec, RTM_DELLINK);
574 /* Send RTM_NEWLINK because userspace
575 * expects RTM_NEWLINK for vlan dels
577 br_ifinfo_notify(RTM_NEWLINK, p);
581 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
583 if (tb[IFLA_ADDRESS]) {
584 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
586 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
587 return -EADDRNOTAVAIL;
593 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
594 struct nlattr *tb[], struct nlattr *data[])
596 struct net_bridge *br = netdev_priv(dev);
598 if (tb[IFLA_ADDRESS]) {
599 spin_lock_bh(&br->lock);
600 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
601 spin_unlock_bh(&br->lock);
604 return register_netdevice(dev);
607 static int br_port_slave_changelink(struct net_device *brdev,
608 struct net_device *dev,
610 struct nlattr *data[])
614 return br_setport(br_port_get_rtnl(dev), data);
617 static int br_port_fill_slave_info(struct sk_buff *skb,
618 const struct net_device *brdev,
619 const struct net_device *dev)
621 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
624 static size_t br_port_get_slave_size(const struct net_device *brdev,
625 const struct net_device *dev)
627 return br_port_info_size();
630 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
631 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
632 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
633 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
636 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
637 struct nlattr *data[])
639 struct net_bridge *br = netdev_priv(brdev);
645 if (data[IFLA_BR_FORWARD_DELAY]) {
646 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
651 if (data[IFLA_BR_HELLO_TIME]) {
652 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
657 if (data[IFLA_BR_MAX_AGE]) {
658 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
666 static size_t br_get_size(const struct net_device *brdev)
668 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
669 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
670 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
674 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
676 struct net_bridge *br = netdev_priv(brdev);
677 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
678 u32 hello_time = jiffies_to_clock_t(br->hello_time);
679 u32 age_time = jiffies_to_clock_t(br->max_age);
681 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
682 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
683 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time))
689 static size_t br_get_link_af_size(const struct net_device *dev)
691 struct net_port_vlans *pv;
693 if (br_port_exists(dev))
694 pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
695 else if (dev->priv_flags & IFF_EBRIDGE)
696 pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
703 /* Each VLAN is returned in bridge_vlan_info along with flags */
704 return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
707 static struct rtnl_af_ops br_af_ops = {
709 .get_link_af_size = br_get_link_af_size,
712 struct rtnl_link_ops br_link_ops __read_mostly = {
714 .priv_size = sizeof(struct net_bridge),
715 .setup = br_dev_setup,
716 .maxtype = IFLA_BRPORT_MAX,
718 .validate = br_validate,
719 .newlink = br_dev_newlink,
720 .changelink = br_changelink,
721 .dellink = br_dev_delete,
722 .get_size = br_get_size,
723 .fill_info = br_fill_info,
725 .slave_maxtype = IFLA_BRPORT_MAX,
726 .slave_policy = br_port_policy,
727 .slave_changelink = br_port_slave_changelink,
728 .get_slave_size = br_port_get_slave_size,
729 .fill_slave_info = br_port_fill_slave_info,
732 int __init br_netlink_init(void)
737 rtnl_af_register(&br_af_ops);
739 err = rtnl_link_register(&br_link_ops);
746 rtnl_af_unregister(&br_af_ops);
751 void br_netlink_fini(void)
754 rtnl_af_unregister(&br_af_ops);
755 rtnl_link_unregister(&br_link_ops);