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 int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
27 struct net_bridge_vlan *v;
28 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
32 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
35 pvid = br_get_pvid(vg);
36 /* Count number of vlan infos */
37 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
39 /* only a context, bridge vlan not activated */
40 if (!br_vlan_should_use(v))
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) {
50 } else if ((v->vid - vid_range_end) == 1 &&
51 flags == vid_range_flags) {
52 vid_range_end = v->vid;
55 if ((vid_range_end - vid_range_start) > 0)
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)
76 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
84 if (filter_mask & RTEXT_FILTER_BRVLAN)
88 num_vlans = __get_num_vlan_infos(vg, filter_mask);
94 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
97 struct net_bridge_vlan_group *vg = NULL;
98 struct net_bridge_port *p;
99 struct net_bridge *br;
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);
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 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
131 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
132 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
133 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
137 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
139 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
140 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
141 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
142 + nla_total_size(4) /* IFLA_MASTER */
143 + nla_total_size(4) /* IFLA_MTU */
144 + nla_total_size(4) /* IFLA_LINK */
145 + nla_total_size(1) /* IFLA_OPERSTATE */
146 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
147 + nla_total_size(br_get_link_af_size_filtered(dev,
148 filter_mask)); /* IFLA_AF_SPEC */
151 static int br_port_fill_attrs(struct sk_buff *skb,
152 const struct net_bridge_port *p)
154 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
156 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
157 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
158 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
159 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
160 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
161 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
162 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
163 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
164 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
165 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
166 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
167 !!(p->flags & BR_PROXYARP_WIFI)) ||
168 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
169 &p->designated_root) ||
170 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
171 &p->designated_bridge) ||
172 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
173 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost))
179 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
180 u16 vid_end, u16 flags)
182 struct bridge_vlan_info vinfo;
184 if ((vid_end - vid_start) > 0) {
185 /* add range to skb */
186 vinfo.vid = vid_start;
187 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
188 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
189 sizeof(vinfo), &vinfo))
190 goto nla_put_failure;
193 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
194 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
195 sizeof(vinfo), &vinfo))
196 goto nla_put_failure;
198 vinfo.vid = vid_start;
200 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
201 sizeof(vinfo), &vinfo))
202 goto nla_put_failure;
211 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
212 struct net_bridge_vlan_group *vg)
214 struct net_bridge_vlan *v;
215 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
219 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
220 * and mark vlan info with begin and end flags
221 * if vlaninfo represents a range
223 pvid = br_get_pvid(vg);
224 list_for_each_entry(v, &vg->vlan_list, vlist) {
226 if (!br_vlan_should_use(v))
229 flags |= BRIDGE_VLAN_INFO_PVID;
231 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
232 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
234 if (vid_range_start == 0) {
236 } else if ((v->vid - vid_range_end) == 1 &&
237 flags == vid_range_flags) {
238 vid_range_end = v->vid;
241 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
249 vid_range_start = v->vid;
250 vid_range_end = v->vid;
251 vid_range_flags = flags;
254 if (vid_range_start != 0) {
255 /* Call it once more to send any left over vlans */
256 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
266 static int br_fill_ifvlaninfo(struct sk_buff *skb,
267 struct net_bridge_vlan_group *vg)
269 struct bridge_vlan_info vinfo;
270 struct net_bridge_vlan *v;
273 pvid = br_get_pvid(vg);
274 list_for_each_entry(v, &vg->vlan_list, vlist) {
275 if (!br_vlan_should_use(v))
281 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
283 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
284 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
286 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
287 sizeof(vinfo), &vinfo))
288 goto nla_put_failure;
298 * Create one netlink message for one interface
299 * Contains port and master info as well as carrier and bridge state.
301 static int br_fill_ifinfo(struct sk_buff *skb,
302 struct net_bridge_port *port,
303 u32 pid, u32 seq, int event, unsigned int flags,
304 u32 filter_mask, const struct net_device *dev)
306 struct net_bridge *br;
307 struct ifinfomsg *hdr;
308 struct nlmsghdr *nlh;
309 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
314 br = netdev_priv(dev);
316 br_debug(br, "br_fill_info event %d port %s master %s\n",
317 event, dev->name, br->dev->name);
319 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
323 hdr = nlmsg_data(nlh);
324 hdr->ifi_family = AF_BRIDGE;
326 hdr->ifi_type = dev->type;
327 hdr->ifi_index = dev->ifindex;
328 hdr->ifi_flags = dev_get_flags(dev);
331 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
332 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
333 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
334 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
336 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
337 (dev->ifindex != dev_get_iflink(dev) &&
338 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
339 goto nla_put_failure;
341 if (event == RTM_NEWLINK && port) {
343 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
345 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
346 goto nla_put_failure;
347 nla_nest_end(skb, nest);
350 /* Check if the VID information is requested */
351 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
352 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
353 struct net_bridge_vlan_group *vg;
358 vg = nbp_vlan_group(port);
360 vg = br_vlan_group(br);
362 if (!vg || !vg->num_vlans)
365 af = nla_nest_start(skb, IFLA_AF_SPEC);
367 goto nla_put_failure;
369 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
370 err = br_fill_ifvlaninfo_compressed(skb, vg);
372 err = br_fill_ifvlaninfo(skb, vg);
374 goto nla_put_failure;
375 nla_nest_end(skb, af);
383 nlmsg_cancel(skb, nlh);
388 * Notify listeners of a change in port information
390 void br_ifinfo_notify(int event, struct net_bridge_port *port)
395 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
400 net = dev_net(port->dev);
401 br_debug(port->br, "port %u(%s) event %d\n",
402 (unsigned int)port->port_no, port->dev->name, event);
404 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
408 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
410 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
411 WARN_ON(err == -EMSGSIZE);
415 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
418 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
423 * Dump information about all ports, in response to GETLINK
425 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
426 struct net_device *dev, u32 filter_mask, int nlflags)
428 struct net_bridge_port *port = br_port_get_rtnl(dev);
430 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
431 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
434 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
438 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
439 int cmd, struct bridge_vlan_info *vinfo)
446 /* if the MASTER flag is set this will act on the global
447 * per-VLAN entry as well
449 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
453 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
454 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
460 nbp_vlan_delete(p, vinfo->vid);
461 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
462 br_vlan_delete(p->br, vinfo->vid);
464 br_vlan_delete(br, vinfo->vid);
472 static int br_afspec(struct net_bridge *br,
473 struct net_bridge_port *p,
474 struct nlattr *af_spec,
477 struct bridge_vlan_info *vinfo_start = NULL;
478 struct bridge_vlan_info *vinfo = NULL;
483 nla_for_each_nested(attr, af_spec, rem) {
484 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
486 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
488 vinfo = nla_data(attr);
489 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
491 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
499 struct bridge_vlan_info tmp_vinfo;
502 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
505 if (vinfo->vid <= vinfo_start->vid)
508 memcpy(&tmp_vinfo, vinfo_start,
509 sizeof(struct bridge_vlan_info));
511 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
513 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
519 err = br_vlan_info(br, p, cmd, vinfo);
528 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
529 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
530 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
531 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
532 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
533 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
534 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
535 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
536 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
537 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
538 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
539 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
542 /* Change the state of the port and notify spanning tree */
543 static int br_set_port_state(struct net_bridge_port *p, u8 state)
545 if (state > BR_STATE_BLOCKING)
548 /* if kernel STP is running, don't allow changes */
549 if (p->br->stp_enabled == BR_KERNEL_STP)
552 /* if device is not up, change is not allowed
553 * if link is not present, only allowable state is disabled
555 if (!netif_running(p->dev) ||
556 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
559 br_set_state(p, state);
561 br_port_state_selection(p->br);
565 /* Set/clear or port flags based on attribute */
566 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
567 int attrtype, unsigned long mask)
570 u8 flag = nla_get_u8(tb[attrtype]);
578 /* Process bridge protocol info on port */
579 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
582 unsigned long old_flags = p->flags;
584 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
585 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
586 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
587 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
588 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
589 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
590 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
591 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
593 if (tb[IFLA_BRPORT_COST]) {
594 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
599 if (tb[IFLA_BRPORT_PRIORITY]) {
600 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
605 if (tb[IFLA_BRPORT_STATE]) {
606 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
611 br_port_flags_change(p, old_flags ^ p->flags);
615 /* Change state and parameters on port. */
616 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
618 struct nlattr *protinfo;
619 struct nlattr *afspec;
620 struct net_bridge_port *p;
621 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
624 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
625 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
626 if (!protinfo && !afspec)
629 p = br_port_get_rtnl(dev);
630 /* We want to accept dev as bridge itself if the AF_SPEC
631 * is set to see if someone is setting vlan info on the bridge
637 if (protinfo->nla_type & NLA_F_NESTED) {
638 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
639 protinfo, br_port_policy);
643 spin_lock_bh(&p->br->lock);
644 err = br_setport(p, tb);
645 spin_unlock_bh(&p->br->lock);
647 /* Binary compatibility with old RSTP */
648 if (nla_len(protinfo) < sizeof(u8))
651 spin_lock_bh(&p->br->lock);
652 err = br_set_port_state(p, nla_get_u8(protinfo));
653 spin_unlock_bh(&p->br->lock);
660 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
661 afspec, RTM_SETLINK);
665 br_ifinfo_notify(RTM_NEWLINK, p);
670 /* Delete port information */
671 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
673 struct nlattr *afspec;
674 struct net_bridge_port *p;
677 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
681 p = br_port_get_rtnl(dev);
682 /* We want to accept dev as bridge itself as well */
683 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
686 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
687 afspec, RTM_DELLINK);
689 /* Send RTM_NEWLINK because userspace
690 * expects RTM_NEWLINK for vlan dels
692 br_ifinfo_notify(RTM_NEWLINK, p);
696 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
698 if (tb[IFLA_ADDRESS]) {
699 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
701 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
702 return -EADDRNOTAVAIL;
708 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
709 if (data[IFLA_BR_VLAN_PROTOCOL]) {
710 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
711 case htons(ETH_P_8021Q):
712 case htons(ETH_P_8021AD):
715 return -EPROTONOSUPPORT;
723 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
724 struct nlattr *tb[], struct nlattr *data[])
726 struct net_bridge *br = netdev_priv(dev);
728 if (tb[IFLA_ADDRESS]) {
729 spin_lock_bh(&br->lock);
730 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
731 spin_unlock_bh(&br->lock);
734 return register_netdevice(dev);
737 static int br_port_slave_changelink(struct net_device *brdev,
738 struct net_device *dev,
740 struct nlattr *data[])
742 struct net_bridge *br = netdev_priv(brdev);
748 spin_lock_bh(&br->lock);
749 ret = br_setport(br_port_get_rtnl(dev), data);
750 spin_unlock_bh(&br->lock);
755 static int br_port_fill_slave_info(struct sk_buff *skb,
756 const struct net_device *brdev,
757 const struct net_device *dev)
759 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
762 static size_t br_port_get_slave_size(const struct net_device *brdev,
763 const struct net_device *dev)
765 return br_port_info_size();
768 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
769 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
770 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
771 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
772 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
773 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
774 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
775 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
776 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
777 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
778 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
780 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
781 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
782 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
783 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
784 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
785 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
786 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
787 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
788 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
789 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
790 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
791 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
792 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
793 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
794 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
795 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
796 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
797 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
800 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
801 struct nlattr *data[])
803 struct net_bridge *br = netdev_priv(brdev);
809 if (data[IFLA_BR_FORWARD_DELAY]) {
810 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
815 if (data[IFLA_BR_HELLO_TIME]) {
816 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
821 if (data[IFLA_BR_MAX_AGE]) {
822 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
827 if (data[IFLA_BR_AGEING_TIME]) {
828 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
830 br->ageing_time = clock_t_to_jiffies(ageing_time);
833 if (data[IFLA_BR_STP_STATE]) {
834 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
836 br_stp_set_enabled(br, stp_enabled);
839 if (data[IFLA_BR_PRIORITY]) {
840 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
842 br_stp_set_bridge_priority(br, priority);
845 if (data[IFLA_BR_VLAN_FILTERING]) {
846 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
848 err = __br_vlan_filter_toggle(br, vlan_filter);
853 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
854 if (data[IFLA_BR_VLAN_PROTOCOL]) {
855 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
857 err = __br_vlan_set_proto(br, vlan_proto);
862 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
863 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
865 err = __br_vlan_set_default_pvid(br, defpvid);
871 if (data[IFLA_BR_GROUP_FWD_MASK]) {
872 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
874 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
876 br->group_fwd_mask = fwd_mask;
879 if (data[IFLA_BR_GROUP_ADDR]) {
880 u8 new_addr[ETH_ALEN];
882 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
884 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
885 if (!is_link_local_ether_addr(new_addr))
887 if (new_addr[5] == 1 || /* 802.3x Pause address */
888 new_addr[5] == 2 || /* 802.3ad Slow protocols */
889 new_addr[5] == 3) /* 802.1X PAE address */
891 spin_lock_bh(&br->lock);
892 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
893 spin_unlock_bh(&br->lock);
894 br->group_addr_set = true;
895 br_recalculate_fwd_mask(br);
898 if (data[IFLA_BR_FDB_FLUSH])
901 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
902 if (data[IFLA_BR_MCAST_ROUTER]) {
903 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
905 err = br_multicast_set_router(br, multicast_router);
910 if (data[IFLA_BR_MCAST_SNOOPING]) {
911 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
913 err = br_multicast_toggle(br, mcast_snooping);
918 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
921 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
922 br->multicast_query_use_ifaddr = !!val;
925 if (data[IFLA_BR_MCAST_QUERIER]) {
926 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
928 err = br_multicast_set_querier(br, mcast_querier);
933 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
934 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
936 br->hash_elasticity = val;
939 if (data[IFLA_BR_MCAST_HASH_MAX]) {
940 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
942 err = br_multicast_set_hash_max(br, hash_max);
947 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
948 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
950 br->multicast_last_member_count = val;
953 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
954 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
956 br->multicast_startup_query_count = val;
959 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
960 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
962 br->multicast_last_member_interval = clock_t_to_jiffies(val);
965 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
966 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
968 br->multicast_membership_interval = clock_t_to_jiffies(val);
971 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
972 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
974 br->multicast_querier_interval = clock_t_to_jiffies(val);
977 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
978 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
980 br->multicast_query_interval = clock_t_to_jiffies(val);
983 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
984 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
986 br->multicast_query_response_interval = clock_t_to_jiffies(val);
989 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
990 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
992 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
995 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
996 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
997 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
999 br->nf_call_iptables = val ? true : false;
1002 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1003 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1005 br->nf_call_ip6tables = val ? true : false;
1008 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1009 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1011 br->nf_call_arptables = val ? true : false;
1018 static size_t br_get_size(const struct net_device *brdev)
1020 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1021 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1022 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1023 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1024 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1025 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1026 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1027 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1028 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1029 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1031 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1032 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1033 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1034 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1035 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1036 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1037 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1038 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1039 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1040 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1041 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1042 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1043 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1044 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1045 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1046 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1047 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1048 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1049 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1050 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1051 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1052 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1053 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1054 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1055 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1056 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1057 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1059 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1060 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1061 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1062 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1067 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1069 struct net_bridge *br = netdev_priv(brdev);
1070 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1071 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1072 u32 age_time = jiffies_to_clock_t(br->max_age);
1073 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1074 u32 stp_enabled = br->stp_enabled;
1075 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1076 u8 vlan_enabled = br_vlan_enabled(br);
1079 clockval = br_timer_value(&br->hello_timer);
1080 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1082 clockval = br_timer_value(&br->tcn_timer);
1083 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1085 clockval = br_timer_value(&br->topology_change_timer);
1086 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1088 clockval = br_timer_value(&br->gc_timer);
1089 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1092 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1093 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1094 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1095 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1096 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1097 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1098 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1099 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1100 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1102 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1103 &br->designated_root) ||
1104 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1105 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1106 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1107 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1108 br->topology_change_detected) ||
1109 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1112 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1113 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1114 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
1117 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1118 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1119 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1120 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1121 br->multicast_query_use_ifaddr) ||
1122 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1123 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1124 br->hash_elasticity) ||
1125 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1126 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1127 br->multicast_last_member_count) ||
1128 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1129 br->multicast_startup_query_count))
1132 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1133 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1135 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1136 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1138 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1139 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1141 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1142 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1144 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1145 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1147 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1148 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1151 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1152 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1153 br->nf_call_iptables ? 1 : 0) ||
1154 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1155 br->nf_call_ip6tables ? 1 : 0) ||
1156 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1157 br->nf_call_arptables ? 1 : 0))
1164 static size_t br_get_link_af_size(const struct net_device *dev)
1166 struct net_bridge_port *p;
1167 struct net_bridge *br;
1170 if (br_port_exists(dev)) {
1171 p = br_port_get_rtnl(dev);
1172 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
1173 RTEXT_FILTER_BRVLAN);
1174 } else if (dev->priv_flags & IFF_EBRIDGE) {
1175 br = netdev_priv(dev);
1176 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
1177 RTEXT_FILTER_BRVLAN);
1180 /* Each VLAN is returned in bridge_vlan_info along with flags */
1181 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
1184 static struct rtnl_af_ops br_af_ops __read_mostly = {
1185 .family = AF_BRIDGE,
1186 .get_link_af_size = br_get_link_af_size,
1189 struct rtnl_link_ops br_link_ops __read_mostly = {
1191 .priv_size = sizeof(struct net_bridge),
1192 .setup = br_dev_setup,
1193 .maxtype = IFLA_BR_MAX,
1194 .policy = br_policy,
1195 .validate = br_validate,
1196 .newlink = br_dev_newlink,
1197 .changelink = br_changelink,
1198 .dellink = br_dev_delete,
1199 .get_size = br_get_size,
1200 .fill_info = br_fill_info,
1202 .slave_maxtype = IFLA_BRPORT_MAX,
1203 .slave_policy = br_port_policy,
1204 .slave_changelink = br_port_slave_changelink,
1205 .get_slave_size = br_port_get_slave_size,
1206 .fill_slave_info = br_port_fill_slave_info,
1209 int __init br_netlink_init(void)
1214 rtnl_af_register(&br_af_ops);
1216 err = rtnl_link_register(&br_link_ops);
1223 rtnl_af_unregister(&br_af_ops);
1228 void br_netlink_fini(void)
1231 rtnl_af_unregister(&br_af_ops);
1232 rtnl_link_unregister(&br_link_ops);