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 */
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
)))
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
;
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
;
188 vinfo
.vid
= vid_start
;
190 if (nla_put(skb
, IFLA_BRIDGE_VLAN_INFO
,
191 sizeof(vinfo
), &vinfo
))
192 goto nla_put_failure
;
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;
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
) {
216 if (!br_vlan_should_use(v
))
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) {
226 } else if ((v
->vid
- vid_range_end
) == 1 &&
227 flags
== vid_range_flags
) {
228 vid_range_end
= v
->vid
;
231 err
= br_fill_ifvlaninfo_range(skb
, vid_range_start
,
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
,
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
;
263 pvid
= br_get_pvid(vg
);
264 list_for_each_entry(v
, &vg
->vlan_list
, vlist
) {
265 if (!br_vlan_should_use(v
))
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
;
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
;
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
);
313 hdr
= nlmsg_data(nlh
);
314 hdr
->ifi_family
= AF_BRIDGE
;
316 hdr
->ifi_type
= dev
->type
;
317 hdr
->ifi_index
= dev
->ifindex
;
318 hdr
->ifi_flags
= dev_get_flags(dev
);
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
) ||
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
) {
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
;
348 vg
= nbp_vlan_group(port
);
350 vg
= br_vlan_group(br
);
352 if (!vg
|| !vg
->num_vlans
)
355 af
= nla_nest_start(skb
, IFLA_AF_SPEC
);
357 goto nla_put_failure
;
359 if (filter_mask
& RTEXT_FILTER_BRVLAN_COMPRESSED
)
360 err
= br_fill_ifvlaninfo_compressed(skb
, vg
);
362 err
= br_fill_ifvlaninfo(skb
, vg
);
364 goto nla_put_failure
;
365 nla_nest_end(skb
, af
);
373 nlmsg_cancel(skb
, nlh
);
378 * Notify listeners of a change in port information
380 void br_ifinfo_notify(int event
, struct net_bridge_port
*port
)
385 u32 filter
= RTEXT_FILTER_BRVLAN_COMPRESSED
;
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
);
398 err
= br_fill_ifinfo(skb
, port
, 0, 0, event
, 0, filter
, port
->dev
);
400 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
401 WARN_ON(err
== -EMSGSIZE
);
405 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_ATOMIC
);
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
))
424 return br_fill_ifinfo(skb
, port
, pid
, seq
, RTM_NEWLINK
, nlflags
,
428 static int br_vlan_info(struct net_bridge
*br
, struct net_bridge_port
*p
,
429 int cmd
, struct bridge_vlan_info
*vinfo
)
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
);
443 vinfo
->flags
|= BRIDGE_VLAN_INFO_BRENTRY
;
444 err
= br_vlan_add(br
, vinfo
->vid
, vinfo
->flags
);
450 nbp_vlan_delete(p
, vinfo
->vid
);
451 if (vinfo
->flags
& BRIDGE_VLAN_INFO_MASTER
)
452 br_vlan_delete(p
->br
, vinfo
->vid
);
454 br_vlan_delete(br
, vinfo
->vid
);
462 static int br_afspec(struct net_bridge
*br
,
463 struct net_bridge_port
*p
,
464 struct nlattr
*af_spec
,
467 struct bridge_vlan_info
*vinfo_start
= NULL
;
468 struct bridge_vlan_info
*vinfo
= NULL
;
473 nla_for_each_nested(attr
, af_spec
, rem
) {
474 if (nla_type(attr
) != IFLA_BRIDGE_VLAN_INFO
)
476 if (nla_len(attr
) != sizeof(struct bridge_vlan_info
))
478 vinfo
= nla_data(attr
);
479 if (!vinfo
->vid
|| vinfo
->vid
>= VLAN_VID_MASK
)
481 if (vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_BEGIN
) {
489 struct bridge_vlan_info tmp_vinfo
;
492 if (!(vinfo
->flags
& BRIDGE_VLAN_INFO_RANGE_END
))
495 if (vinfo
->vid
<= vinfo_start
->vid
)
498 memcpy(&tmp_vinfo
, vinfo_start
,
499 sizeof(struct bridge_vlan_info
));
501 for (v
= vinfo_start
->vid
; v
<= vinfo
->vid
; v
++) {
503 err
= br_vlan_info(br
, p
, cmd
, &tmp_vinfo
);
509 err
= br_vlan_info(br
, p
, cmd
, vinfo
);
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
)
538 /* if kernel STP is running, don't allow changes */
539 if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
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
))
549 br_set_state(p
, state
);
551 br_port_state_selection(p
->br
);
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
)
560 u8 flag
= nla_get_u8(tb
[attrtype
]);
568 /* Process bridge protocol info on port */
569 static int br_setport(struct net_bridge_port
*p
, struct nlattr
*tb
[])
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
]));
589 if (tb
[IFLA_BRPORT_PRIORITY
]) {
590 err
= br_stp_set_port_priority(p
, nla_get_u16(tb
[IFLA_BRPORT_PRIORITY
]));
595 if (tb
[IFLA_BRPORT_STATE
]) {
596 err
= br_set_port_state(p
, nla_get_u8(tb
[IFLA_BRPORT_STATE
]));
601 br_port_flags_change(p
, old_flags
^ p
->flags
);
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];
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
)
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
627 if (protinfo
->nla_type
& NLA_F_NESTED
) {
628 err
= nla_parse_nested(tb
, IFLA_BRPORT_MAX
,
629 protinfo
, br_port_policy
);
633 spin_lock_bh(&p
->br
->lock
);
634 err
= br_setport(p
, tb
);
635 spin_unlock_bh(&p
->br
->lock
);
637 /* Binary compatibility with old RSTP */
638 if (nla_len(protinfo
) < sizeof(u8
))
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
);
650 err
= br_afspec((struct net_bridge
*)netdev_priv(dev
), p
,
651 afspec
, RTM_SETLINK
);
655 br_ifinfo_notify(RTM_NEWLINK
, p
);
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
;
667 afspec
= nlmsg_find_attr(nlh
, sizeof(struct ifinfomsg
), IFLA_AF_SPEC
);
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
))
676 err
= br_afspec((struct net_bridge
*)netdev_priv(dev
), p
,
677 afspec
, RTM_DELLINK
);
679 /* Send RTM_NEWLINK because userspace
680 * expects RTM_NEWLINK for vlan dels
682 br_ifinfo_notify(RTM_NEWLINK
, p
);
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
)
691 if (!is_valid_ether_addr(nla_data(tb
[IFLA_ADDRESS
])))
692 return -EADDRNOTAVAIL
;
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
):
705 return -EPROTONOSUPPORT
;
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
,
730 struct nlattr
*data
[])
732 struct net_bridge
*br
= netdev_priv(brdev
);
738 spin_lock_bh(&br
->lock
);
739 ret
= br_setport(br_port_get_rtnl(dev
), data
);
740 spin_unlock_bh(&br
->lock
);
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
,
772 static int br_changelink(struct net_device
*brdev
, struct nlattr
*tb
[],
773 struct nlattr
*data
[])
775 struct net_bridge
*br
= netdev_priv(brdev
);
781 if (data
[IFLA_BR_FORWARD_DELAY
]) {
782 err
= br_set_forward_delay(br
, nla_get_u32(data
[IFLA_BR_FORWARD_DELAY
]));
787 if (data
[IFLA_BR_HELLO_TIME
]) {
788 err
= br_set_hello_time(br
, nla_get_u32(data
[IFLA_BR_HELLO_TIME
]));
793 if (data
[IFLA_BR_MAX_AGE
]) {
794 err
= br_set_max_age(br
, nla_get_u32(data
[IFLA_BR_MAX_AGE
]));
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
);
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
);
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
)
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
)
848 memcpy(new_addr
, nla_data(data
[IFLA_BR_GROUP_ADDR
]), ETH_ALEN
);
849 if (!is_link_local_ether_addr(new_addr
))
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 */
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
);
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 */
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
))
940 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
941 if (nla_put_be16(skb
, IFLA_BR_VLAN_PROTOCOL
, br
->vlan_proto
))
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
;
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
= {
970 .get_link_af_size
= br_get_link_af_size
,
973 struct rtnl_link_ops br_link_ops __read_mostly
= {
975 .priv_size
= sizeof(struct net_bridge
),
976 .setup
= br_dev_setup
,
977 .maxtype
= IFLA_BR_MAX
,
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)
998 rtnl_af_register(&br_af_ops
);
1000 err
= rtnl_link_register(&br_link_ops
);
1007 rtnl_af_unregister(&br_af_ops
);
1012 void br_netlink_fini(void)
1015 rtnl_af_unregister(&br_af_ops
);
1016 rtnl_link_unregister(&br_link_ops
);