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 */
134 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
135 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
139 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
141 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
142 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
143 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
144 + nla_total_size(4) /* IFLA_MASTER */
145 + nla_total_size(4) /* IFLA_MTU */
146 + nla_total_size(4) /* IFLA_LINK */
147 + nla_total_size(1) /* IFLA_OPERSTATE */
148 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
149 + nla_total_size(br_get_link_af_size_filtered(dev,
150 filter_mask)); /* IFLA_AF_SPEC */
153 static int br_port_fill_attrs(struct sk_buff *skb,
154 const struct net_bridge_port *p)
156 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
158 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
159 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
160 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
161 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
162 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
163 nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
164 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
165 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
166 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
167 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
168 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
169 !!(p->flags & BR_PROXYARP_WIFI)) ||
170 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
171 &p->designated_root) ||
172 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
173 &p->designated_bridge) ||
174 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
175 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
176 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
177 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no))
183 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
184 u16 vid_end, u16 flags)
186 struct bridge_vlan_info vinfo;
188 if ((vid_end - vid_start) > 0) {
189 /* add range to skb */
190 vinfo.vid = vid_start;
191 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
192 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
193 sizeof(vinfo), &vinfo))
194 goto nla_put_failure;
197 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
198 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
199 sizeof(vinfo), &vinfo))
200 goto nla_put_failure;
202 vinfo.vid = vid_start;
204 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
205 sizeof(vinfo), &vinfo))
206 goto nla_put_failure;
215 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
216 struct net_bridge_vlan_group *vg)
218 struct net_bridge_vlan *v;
219 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
223 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
224 * and mark vlan info with begin and end flags
225 * if vlaninfo represents a range
227 pvid = br_get_pvid(vg);
228 list_for_each_entry(v, &vg->vlan_list, vlist) {
230 if (!br_vlan_should_use(v))
233 flags |= BRIDGE_VLAN_INFO_PVID;
235 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
236 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
238 if (vid_range_start == 0) {
240 } else if ((v->vid - vid_range_end) == 1 &&
241 flags == vid_range_flags) {
242 vid_range_end = v->vid;
245 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
253 vid_range_start = v->vid;
254 vid_range_end = v->vid;
255 vid_range_flags = flags;
258 if (vid_range_start != 0) {
259 /* Call it once more to send any left over vlans */
260 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
270 static int br_fill_ifvlaninfo(struct sk_buff *skb,
271 struct net_bridge_vlan_group *vg)
273 struct bridge_vlan_info vinfo;
274 struct net_bridge_vlan *v;
277 pvid = br_get_pvid(vg);
278 list_for_each_entry(v, &vg->vlan_list, vlist) {
279 if (!br_vlan_should_use(v))
285 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
287 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
288 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
290 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
291 sizeof(vinfo), &vinfo))
292 goto nla_put_failure;
302 * Create one netlink message for one interface
303 * Contains port and master info as well as carrier and bridge state.
305 static int br_fill_ifinfo(struct sk_buff *skb,
306 struct net_bridge_port *port,
307 u32 pid, u32 seq, int event, unsigned int flags,
308 u32 filter_mask, const struct net_device *dev)
310 struct net_bridge *br;
311 struct ifinfomsg *hdr;
312 struct nlmsghdr *nlh;
313 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
318 br = netdev_priv(dev);
320 br_debug(br, "br_fill_info event %d port %s master %s\n",
321 event, dev->name, br->dev->name);
323 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
327 hdr = nlmsg_data(nlh);
328 hdr->ifi_family = AF_BRIDGE;
330 hdr->ifi_type = dev->type;
331 hdr->ifi_index = dev->ifindex;
332 hdr->ifi_flags = dev_get_flags(dev);
335 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
336 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
337 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
338 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
340 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
341 (dev->ifindex != dev_get_iflink(dev) &&
342 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
343 goto nla_put_failure;
345 if (event == RTM_NEWLINK && port) {
347 = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
349 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
350 goto nla_put_failure;
351 nla_nest_end(skb, nest);
354 /* Check if the VID information is requested */
355 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
356 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
357 struct net_bridge_vlan_group *vg;
362 vg = nbp_vlan_group(port);
364 vg = br_vlan_group(br);
366 if (!vg || !vg->num_vlans)
369 af = nla_nest_start(skb, IFLA_AF_SPEC);
371 goto nla_put_failure;
373 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
374 err = br_fill_ifvlaninfo_compressed(skb, vg);
376 err = br_fill_ifvlaninfo(skb, vg);
378 goto nla_put_failure;
379 nla_nest_end(skb, af);
387 nlmsg_cancel(skb, nlh);
392 * Notify listeners of a change in port information
394 void br_ifinfo_notify(int event, struct net_bridge_port *port)
399 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
404 net = dev_net(port->dev);
405 br_debug(port->br, "port %u(%s) event %d\n",
406 (unsigned int)port->port_no, port->dev->name, event);
408 skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
412 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
414 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
415 WARN_ON(err == -EMSGSIZE);
419 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
422 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
427 * Dump information about all ports, in response to GETLINK
429 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
430 struct net_device *dev, u32 filter_mask, int nlflags)
432 struct net_bridge_port *port = br_port_get_rtnl(dev);
434 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
435 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
438 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
442 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
443 int cmd, struct bridge_vlan_info *vinfo)
450 /* if the MASTER flag is set this will act on the global
451 * per-VLAN entry as well
453 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
457 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
458 err = br_vlan_add(br, vinfo->vid, vinfo->flags);
464 nbp_vlan_delete(p, vinfo->vid);
465 if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
466 br_vlan_delete(p->br, vinfo->vid);
468 br_vlan_delete(br, vinfo->vid);
476 static int br_afspec(struct net_bridge *br,
477 struct net_bridge_port *p,
478 struct nlattr *af_spec,
481 struct bridge_vlan_info *vinfo_start = NULL;
482 struct bridge_vlan_info *vinfo = NULL;
487 nla_for_each_nested(attr, af_spec, rem) {
488 if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
490 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
492 vinfo = nla_data(attr);
493 if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
495 if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
503 struct bridge_vlan_info tmp_vinfo;
506 if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
509 if (vinfo->vid <= vinfo_start->vid)
512 memcpy(&tmp_vinfo, vinfo_start,
513 sizeof(struct bridge_vlan_info));
515 for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
517 err = br_vlan_info(br, p, cmd, &tmp_vinfo);
523 err = br_vlan_info(br, p, cmd, vinfo);
532 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
533 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
534 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
535 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
536 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
537 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
538 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
539 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
540 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
541 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
542 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
543 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
546 /* Change the state of the port and notify spanning tree */
547 static int br_set_port_state(struct net_bridge_port *p, u8 state)
549 if (state > BR_STATE_BLOCKING)
552 /* if kernel STP is running, don't allow changes */
553 if (p->br->stp_enabled == BR_KERNEL_STP)
556 /* if device is not up, change is not allowed
557 * if link is not present, only allowable state is disabled
559 if (!netif_running(p->dev) ||
560 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
563 br_set_state(p, state);
565 br_port_state_selection(p->br);
569 /* Set/clear or port flags based on attribute */
570 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
571 int attrtype, unsigned long mask)
574 u8 flag = nla_get_u8(tb[attrtype]);
582 /* Process bridge protocol info on port */
583 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
586 unsigned long old_flags = p->flags;
588 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
589 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
590 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
591 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
592 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
593 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
594 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
595 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
597 if (tb[IFLA_BRPORT_COST]) {
598 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
603 if (tb[IFLA_BRPORT_PRIORITY]) {
604 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
609 if (tb[IFLA_BRPORT_STATE]) {
610 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
615 br_port_flags_change(p, old_flags ^ p->flags);
619 /* Change state and parameters on port. */
620 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
622 struct nlattr *protinfo;
623 struct nlattr *afspec;
624 struct net_bridge_port *p;
625 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
628 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
629 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
630 if (!protinfo && !afspec)
633 p = br_port_get_rtnl(dev);
634 /* We want to accept dev as bridge itself if the AF_SPEC
635 * is set to see if someone is setting vlan info on the bridge
641 if (protinfo->nla_type & NLA_F_NESTED) {
642 err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
643 protinfo, br_port_policy);
647 spin_lock_bh(&p->br->lock);
648 err = br_setport(p, tb);
649 spin_unlock_bh(&p->br->lock);
651 /* Binary compatibility with old RSTP */
652 if (nla_len(protinfo) < sizeof(u8))
655 spin_lock_bh(&p->br->lock);
656 err = br_set_port_state(p, nla_get_u8(protinfo));
657 spin_unlock_bh(&p->br->lock);
664 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
665 afspec, RTM_SETLINK);
669 br_ifinfo_notify(RTM_NEWLINK, p);
674 /* Delete port information */
675 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
677 struct nlattr *afspec;
678 struct net_bridge_port *p;
681 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
685 p = br_port_get_rtnl(dev);
686 /* We want to accept dev as bridge itself as well */
687 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
690 err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
691 afspec, RTM_DELLINK);
693 /* Send RTM_NEWLINK because userspace
694 * expects RTM_NEWLINK for vlan dels
696 br_ifinfo_notify(RTM_NEWLINK, p);
700 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
702 if (tb[IFLA_ADDRESS]) {
703 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
705 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
706 return -EADDRNOTAVAIL;
712 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
713 if (data[IFLA_BR_VLAN_PROTOCOL]) {
714 switch (nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])) {
715 case htons(ETH_P_8021Q):
716 case htons(ETH_P_8021AD):
719 return -EPROTONOSUPPORT;
727 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
728 struct nlattr *tb[], struct nlattr *data[])
730 struct net_bridge *br = netdev_priv(dev);
732 if (tb[IFLA_ADDRESS]) {
733 spin_lock_bh(&br->lock);
734 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
735 spin_unlock_bh(&br->lock);
738 return register_netdevice(dev);
741 static int br_port_slave_changelink(struct net_device *brdev,
742 struct net_device *dev,
744 struct nlattr *data[])
746 struct net_bridge *br = netdev_priv(brdev);
752 spin_lock_bh(&br->lock);
753 ret = br_setport(br_port_get_rtnl(dev), data);
754 spin_unlock_bh(&br->lock);
759 static int br_port_fill_slave_info(struct sk_buff *skb,
760 const struct net_device *brdev,
761 const struct net_device *dev)
763 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
766 static size_t br_port_get_slave_size(const struct net_device *brdev,
767 const struct net_device *dev)
769 return br_port_info_size();
772 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
773 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
774 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
775 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
776 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
777 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
778 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
779 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
780 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
781 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
782 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
784 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
785 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
786 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
787 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
788 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
789 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
790 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
791 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
792 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
793 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
794 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
795 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
796 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
797 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
798 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
799 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
800 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
801 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
804 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
805 struct nlattr *data[])
807 struct net_bridge *br = netdev_priv(brdev);
813 if (data[IFLA_BR_FORWARD_DELAY]) {
814 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
819 if (data[IFLA_BR_HELLO_TIME]) {
820 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
825 if (data[IFLA_BR_MAX_AGE]) {
826 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
831 if (data[IFLA_BR_AGEING_TIME]) {
832 u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
834 br->ageing_time = clock_t_to_jiffies(ageing_time);
837 if (data[IFLA_BR_STP_STATE]) {
838 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
840 br_stp_set_enabled(br, stp_enabled);
843 if (data[IFLA_BR_PRIORITY]) {
844 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
846 br_stp_set_bridge_priority(br, priority);
849 if (data[IFLA_BR_VLAN_FILTERING]) {
850 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
852 err = __br_vlan_filter_toggle(br, vlan_filter);
857 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
858 if (data[IFLA_BR_VLAN_PROTOCOL]) {
859 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
861 err = __br_vlan_set_proto(br, vlan_proto);
866 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
867 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
869 err = __br_vlan_set_default_pvid(br, defpvid);
875 if (data[IFLA_BR_GROUP_FWD_MASK]) {
876 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
878 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
880 br->group_fwd_mask = fwd_mask;
883 if (data[IFLA_BR_GROUP_ADDR]) {
884 u8 new_addr[ETH_ALEN];
886 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
888 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
889 if (!is_link_local_ether_addr(new_addr))
891 if (new_addr[5] == 1 || /* 802.3x Pause address */
892 new_addr[5] == 2 || /* 802.3ad Slow protocols */
893 new_addr[5] == 3) /* 802.1X PAE address */
895 spin_lock_bh(&br->lock);
896 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
897 spin_unlock_bh(&br->lock);
898 br->group_addr_set = true;
899 br_recalculate_fwd_mask(br);
902 if (data[IFLA_BR_FDB_FLUSH])
905 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
906 if (data[IFLA_BR_MCAST_ROUTER]) {
907 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
909 err = br_multicast_set_router(br, multicast_router);
914 if (data[IFLA_BR_MCAST_SNOOPING]) {
915 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
917 err = br_multicast_toggle(br, mcast_snooping);
922 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
925 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
926 br->multicast_query_use_ifaddr = !!val;
929 if (data[IFLA_BR_MCAST_QUERIER]) {
930 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
932 err = br_multicast_set_querier(br, mcast_querier);
937 if (data[IFLA_BR_MCAST_HASH_ELASTICITY]) {
938 u32 val = nla_get_u32(data[IFLA_BR_MCAST_HASH_ELASTICITY]);
940 br->hash_elasticity = val;
943 if (data[IFLA_BR_MCAST_HASH_MAX]) {
944 u32 hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
946 err = br_multicast_set_hash_max(br, hash_max);
951 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
952 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
954 br->multicast_last_member_count = val;
957 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
958 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
960 br->multicast_startup_query_count = val;
963 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
964 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
966 br->multicast_last_member_interval = clock_t_to_jiffies(val);
969 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
970 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
972 br->multicast_membership_interval = clock_t_to_jiffies(val);
975 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
976 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
978 br->multicast_querier_interval = clock_t_to_jiffies(val);
981 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
982 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
984 br->multicast_query_interval = clock_t_to_jiffies(val);
987 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
988 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
990 br->multicast_query_response_interval = clock_t_to_jiffies(val);
993 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
994 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
996 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
999 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1000 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1001 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1003 br->nf_call_iptables = val ? true : false;
1006 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1007 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1009 br->nf_call_ip6tables = val ? true : false;
1012 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1013 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1015 br->nf_call_arptables = val ? true : false;
1022 static size_t br_get_size(const struct net_device *brdev)
1024 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1025 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1026 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1027 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1028 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1029 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1030 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1031 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1032 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1033 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1035 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1036 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1037 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1038 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1039 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1040 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1041 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1042 nla_total_size(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1043 nla_total_size(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1044 nla_total_size(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1045 nla_total_size(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1046 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1047 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1048 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1049 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1050 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1051 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1052 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1053 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1054 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1055 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1056 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1057 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1058 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1059 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1060 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1061 nla_total_size(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1063 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1064 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1065 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1066 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1071 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1073 struct net_bridge *br = netdev_priv(brdev);
1074 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1075 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1076 u32 age_time = jiffies_to_clock_t(br->max_age);
1077 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1078 u32 stp_enabled = br->stp_enabled;
1079 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1080 u8 vlan_enabled = br_vlan_enabled(br);
1083 clockval = br_timer_value(&br->hello_timer);
1084 if (nla_put_u64(skb, IFLA_BR_HELLO_TIMER, clockval))
1086 clockval = br_timer_value(&br->tcn_timer);
1087 if (nla_put_u64(skb, IFLA_BR_TCN_TIMER, clockval))
1089 clockval = br_timer_value(&br->topology_change_timer);
1090 if (nla_put_u64(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval))
1092 clockval = br_timer_value(&br->gc_timer);
1093 if (nla_put_u64(skb, IFLA_BR_GC_TIMER, clockval))
1096 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1097 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1098 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1099 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1100 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1101 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1102 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1103 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1104 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1106 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1107 &br->designated_root) ||
1108 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1109 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1110 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1111 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1112 br->topology_change_detected) ||
1113 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr))
1116 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1117 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1118 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid))
1121 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1122 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1123 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING, !br->multicast_disabled) ||
1124 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1125 br->multicast_query_use_ifaddr) ||
1126 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER, br->multicast_querier) ||
1127 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY,
1128 br->hash_elasticity) ||
1129 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1130 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1131 br->multicast_last_member_count) ||
1132 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1133 br->multicast_startup_query_count))
1136 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1137 if (nla_put_u64(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval))
1139 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1140 if (nla_put_u64(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval))
1142 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1143 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval))
1145 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1146 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval))
1148 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1149 if (nla_put_u64(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval))
1151 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1152 if (nla_put_u64(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval))
1155 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1156 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1157 br->nf_call_iptables ? 1 : 0) ||
1158 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1159 br->nf_call_ip6tables ? 1 : 0) ||
1160 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1161 br->nf_call_arptables ? 1 : 0))
1168 static size_t br_get_link_af_size(const struct net_device *dev)
1170 struct net_bridge_port *p;
1171 struct net_bridge *br;
1174 if (br_port_exists(dev)) {
1175 p = br_port_get_rtnl(dev);
1176 num_vlans = br_get_num_vlan_infos(nbp_vlan_group(p),
1177 RTEXT_FILTER_BRVLAN);
1178 } else if (dev->priv_flags & IFF_EBRIDGE) {
1179 br = netdev_priv(dev);
1180 num_vlans = br_get_num_vlan_infos(br_vlan_group(br),
1181 RTEXT_FILTER_BRVLAN);
1184 /* Each VLAN is returned in bridge_vlan_info along with flags */
1185 return num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
1188 static struct rtnl_af_ops br_af_ops __read_mostly = {
1189 .family = AF_BRIDGE,
1190 .get_link_af_size = br_get_link_af_size,
1193 struct rtnl_link_ops br_link_ops __read_mostly = {
1195 .priv_size = sizeof(struct net_bridge),
1196 .setup = br_dev_setup,
1197 .maxtype = IFLA_BR_MAX,
1198 .policy = br_policy,
1199 .validate = br_validate,
1200 .newlink = br_dev_newlink,
1201 .changelink = br_changelink,
1202 .dellink = br_dev_delete,
1203 .get_size = br_get_size,
1204 .fill_info = br_fill_info,
1206 .slave_maxtype = IFLA_BRPORT_MAX,
1207 .slave_policy = br_port_policy,
1208 .slave_changelink = br_port_slave_changelink,
1209 .get_slave_size = br_port_get_slave_size,
1210 .fill_slave_info = br_port_fill_slave_info,
1213 int __init br_netlink_init(void)
1218 rtnl_af_register(&br_af_ops);
1220 err = rtnl_link_register(&br_link_ops);
1227 rtnl_af_unregister(&br_af_ops);
1232 void br_netlink_fini(void)
1235 rtnl_af_unregister(&br_af_ops);
1236 rtnl_link_unregister(&br_link_ops);