1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Bridge netlink control interface
6 * Stephen Hemminger <shemminger@osdl.org>
9 #include <linux/kernel.h>
10 #include <linux/slab.h>
11 #include <linux/etherdevice.h>
12 #include <net/rtnetlink.h>
13 #include <net/net_namespace.h>
15 #include <uapi/linux/if_bridge.h>
17 #include "br_private.h"
18 #include "br_private_stp.h"
19 #include "br_private_cfm.h"
20 #include "br_private_tunnel.h"
21 #include "br_private_mcast_eht.h"
23 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
26 struct net_bridge_vlan *v;
27 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
31 if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
34 pvid = br_get_pvid(vg);
35 /* Count number of vlan infos */
36 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
38 /* only a context, bridge vlan not activated */
39 if (!br_vlan_should_use(v))
42 flags |= BRIDGE_VLAN_INFO_PVID;
44 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
45 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
47 if (vid_range_start == 0) {
49 } else if ((v->vid - vid_range_end) == 1 &&
50 flags == vid_range_flags) {
51 vid_range_end = v->vid;
54 if ((vid_range_end - vid_range_start) > 0)
60 vid_range_start = v->vid;
61 vid_range_end = v->vid;
62 vid_range_flags = flags;
65 if (vid_range_start != 0) {
66 if ((vid_range_end - vid_range_start) > 0)
75 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
83 if (filter_mask & RTEXT_FILTER_BRVLAN)
87 num_vlans = __get_num_vlan_infos(vg, filter_mask);
93 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
96 struct net_bridge_vlan_group *vg = NULL;
97 struct net_bridge_port *p = NULL;
98 struct net_bridge *br = NULL;
99 u32 num_cfm_peer_mep_infos;
100 u32 num_cfm_mep_infos;
105 if (netif_is_bridge_port(dev)) {
106 p = br_port_get_check_rcu(dev);
108 vg = nbp_vlan_group_rcu(p);
109 } else if (dev->priv_flags & IFF_EBRIDGE) {
110 br = netdev_priv(dev);
111 vg = br_vlan_group_rcu(br);
113 num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
116 if (p && (p->flags & BR_VLAN_TUNNEL))
117 vinfo_sz += br_get_vlan_tunnel_info_size(vg);
119 /* Each VLAN is returned in bridge_vlan_info along with flags */
120 vinfo_sz += num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
122 if (!(filter_mask & RTEXT_FILTER_CFM_STATUS))
128 /* CFM status info must be added */
129 br_cfm_mep_count(br, &num_cfm_mep_infos);
130 br_cfm_peer_mep_count(br, &num_cfm_peer_mep_infos);
132 vinfo_sz += nla_total_size(0); /* IFLA_BRIDGE_CFM */
133 /* For each status struct the MEP instance (u32) is added */
134 /* MEP instance (u32) + br_cfm_mep_status */
135 vinfo_sz += num_cfm_mep_infos *
136 /*IFLA_BRIDGE_CFM_MEP_STATUS_INSTANCE */
137 (nla_total_size(sizeof(u32))
138 /* IFLA_BRIDGE_CFM_MEP_STATUS_OPCODE_UNEXP_SEEN */
139 + nla_total_size(sizeof(u32))
140 /* IFLA_BRIDGE_CFM_MEP_STATUS_VERSION_UNEXP_SEEN */
141 + nla_total_size(sizeof(u32))
142 /* IFLA_BRIDGE_CFM_MEP_STATUS_RX_LEVEL_LOW_SEEN */
143 + nla_total_size(sizeof(u32)));
144 /* MEP instance (u32) + br_cfm_cc_peer_status */
145 vinfo_sz += num_cfm_peer_mep_infos *
146 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE */
147 (nla_total_size(sizeof(u32))
148 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID */
149 + nla_total_size(sizeof(u32))
150 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT */
151 + nla_total_size(sizeof(u32))
152 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_RDI */
153 + nla_total_size(sizeof(u32))
154 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PORT_TLV_VALUE */
155 + nla_total_size(sizeof(u8))
156 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_IF_TLV_VALUE */
157 + nla_total_size(sizeof(u8))
158 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEEN */
159 + nla_total_size(sizeof(u32))
160 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_TLV_SEEN */
161 + nla_total_size(sizeof(u32))
162 /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEQ_UNEXP_SEEN */
163 + nla_total_size(sizeof(u32)));
168 static inline size_t br_port_info_size(void)
170 return nla_total_size(1) /* IFLA_BRPORT_STATE */
171 + nla_total_size(2) /* IFLA_BRPORT_PRIORITY */
172 + nla_total_size(4) /* IFLA_BRPORT_COST */
173 + nla_total_size(1) /* IFLA_BRPORT_MODE */
174 + nla_total_size(1) /* IFLA_BRPORT_GUARD */
175 + nla_total_size(1) /* IFLA_BRPORT_PROTECT */
176 + nla_total_size(1) /* IFLA_BRPORT_FAST_LEAVE */
177 + nla_total_size(1) /* IFLA_BRPORT_MCAST_TO_UCAST */
178 + nla_total_size(1) /* IFLA_BRPORT_LEARNING */
179 + nla_total_size(1) /* IFLA_BRPORT_UNICAST_FLOOD */
180 + nla_total_size(1) /* IFLA_BRPORT_MCAST_FLOOD */
181 + nla_total_size(1) /* IFLA_BRPORT_BCAST_FLOOD */
182 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP */
183 + nla_total_size(1) /* IFLA_BRPORT_PROXYARP_WIFI */
184 + nla_total_size(1) /* IFLA_BRPORT_VLAN_TUNNEL */
185 + nla_total_size(1) /* IFLA_BRPORT_NEIGH_SUPPRESS */
186 + nla_total_size(1) /* IFLA_BRPORT_ISOLATED */
187 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_ROOT_ID */
188 + nla_total_size(sizeof(struct ifla_bridge_id)) /* IFLA_BRPORT_BRIDGE_ID */
189 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_PORT */
190 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_DESIGNATED_COST */
191 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_ID */
192 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_NO */
193 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
194 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_CONFIG_PENDING */
195 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
196 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
197 + nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
198 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
199 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MULTICAST_ROUTER */
201 + nla_total_size(sizeof(u16)) /* IFLA_BRPORT_GROUP_FWD_MASK */
202 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_RING_OPEN */
203 + nla_total_size(sizeof(u8)) /* IFLA_BRPORT_MRP_IN_OPEN */
204 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT */
205 + nla_total_size(sizeof(u32)) /* IFLA_BRPORT_MCAST_EHT_HOSTS_CNT */
209 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
211 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
212 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
213 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
214 + nla_total_size(4) /* IFLA_MASTER */
215 + nla_total_size(4) /* IFLA_MTU */
216 + nla_total_size(4) /* IFLA_LINK */
217 + nla_total_size(1) /* IFLA_OPERSTATE */
218 + nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
219 + nla_total_size(br_get_link_af_size_filtered(dev,
220 filter_mask)) /* IFLA_AF_SPEC */
221 + nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
224 static int br_port_fill_attrs(struct sk_buff *skb,
225 const struct net_bridge_port *p)
227 u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
228 struct net_bridge_port *backup_p;
231 if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
232 nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
233 nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
234 nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
235 nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
236 nla_put_u8(skb, IFLA_BRPORT_PROTECT,
237 !!(p->flags & BR_ROOT_BLOCK)) ||
238 nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
239 !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
240 nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
241 !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
242 nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
243 nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
244 !!(p->flags & BR_FLOOD)) ||
245 nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
246 !!(p->flags & BR_MCAST_FLOOD)) ||
247 nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
248 !!(p->flags & BR_BCAST_FLOOD)) ||
249 nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
250 nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
251 !!(p->flags & BR_PROXYARP_WIFI)) ||
252 nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
253 &p->designated_root) ||
254 nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
255 &p->designated_bridge) ||
256 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
257 nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
258 nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
259 nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
260 nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
261 p->topology_change_ack) ||
262 nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
263 nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
265 nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
266 nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
267 !!(p->flags & BR_NEIGH_SUPPRESS)) ||
268 nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
269 BR_MRP_LOST_CONT)) ||
270 nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN,
271 !!(p->flags & BR_MRP_LOST_IN_CONT)) ||
272 nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)))
275 timerval = br_timer_value(&p->message_age_timer);
276 if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
279 timerval = br_timer_value(&p->forward_delay_timer);
280 if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
283 timerval = br_timer_value(&p->hold_timer);
284 if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
288 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
289 if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
290 p->multicast_router) ||
291 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT,
292 p->multicast_eht_hosts_limit) ||
293 nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_CNT,
294 p->multicast_eht_hosts_cnt))
298 /* we might be called only with br->lock */
300 backup_p = rcu_dereference(p->backup_port);
302 nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
303 backup_p->dev->ifindex);
309 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
310 u16 vid_end, u16 flags)
312 struct bridge_vlan_info vinfo;
314 if ((vid_end - vid_start) > 0) {
315 /* add range to skb */
316 vinfo.vid = vid_start;
317 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
318 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
319 sizeof(vinfo), &vinfo))
320 goto nla_put_failure;
323 vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
324 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
325 sizeof(vinfo), &vinfo))
326 goto nla_put_failure;
328 vinfo.vid = vid_start;
330 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
331 sizeof(vinfo), &vinfo))
332 goto nla_put_failure;
341 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
342 struct net_bridge_vlan_group *vg)
344 struct net_bridge_vlan *v;
345 u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
349 /* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
350 * and mark vlan info with begin and end flags
351 * if vlaninfo represents a range
353 pvid = br_get_pvid(vg);
354 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
356 if (!br_vlan_should_use(v))
359 flags |= BRIDGE_VLAN_INFO_PVID;
361 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
362 flags |= BRIDGE_VLAN_INFO_UNTAGGED;
364 if (vid_range_start == 0) {
366 } else if ((v->vid - vid_range_end) == 1 &&
367 flags == vid_range_flags) {
368 vid_range_end = v->vid;
371 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
379 vid_range_start = v->vid;
380 vid_range_end = v->vid;
381 vid_range_flags = flags;
384 if (vid_range_start != 0) {
385 /* Call it once more to send any left over vlans */
386 err = br_fill_ifvlaninfo_range(skb, vid_range_start,
396 static int br_fill_ifvlaninfo(struct sk_buff *skb,
397 struct net_bridge_vlan_group *vg)
399 struct bridge_vlan_info vinfo;
400 struct net_bridge_vlan *v;
403 pvid = br_get_pvid(vg);
404 list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
405 if (!br_vlan_should_use(v))
411 vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
413 if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
414 vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
416 if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
417 sizeof(vinfo), &vinfo))
418 goto nla_put_failure;
428 * Create one netlink message for one interface
429 * Contains port and master info as well as carrier and bridge state.
431 static int br_fill_ifinfo(struct sk_buff *skb,
432 const struct net_bridge_port *port,
433 u32 pid, u32 seq, int event, unsigned int flags,
434 u32 filter_mask, const struct net_device *dev,
437 u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
438 struct nlattr *af = NULL;
439 struct net_bridge *br;
440 struct ifinfomsg *hdr;
441 struct nlmsghdr *nlh;
446 br = netdev_priv(dev);
448 br_debug(br, "br_fill_info event %d port %s master %s\n",
449 event, dev->name, br->dev->name);
451 nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
455 hdr = nlmsg_data(nlh);
456 hdr->ifi_family = AF_BRIDGE;
458 hdr->ifi_type = dev->type;
459 hdr->ifi_index = dev->ifindex;
460 hdr->ifi_flags = dev_get_flags(dev);
463 if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
464 nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
465 nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
466 nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
468 nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
469 (dev->ifindex != dev_get_iflink(dev) &&
470 nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
471 goto nla_put_failure;
473 if (event == RTM_NEWLINK && port) {
476 nest = nla_nest_start(skb, IFLA_PROTINFO);
477 if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
478 goto nla_put_failure;
479 nla_nest_end(skb, nest);
482 if (filter_mask & (RTEXT_FILTER_BRVLAN |
483 RTEXT_FILTER_BRVLAN_COMPRESSED |
485 RTEXT_FILTER_CFM_CONFIG |
486 RTEXT_FILTER_CFM_STATUS)) {
487 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
489 goto nla_put_failure;
492 /* Check if the VID information is requested */
493 if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
494 (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
495 struct net_bridge_vlan_group *vg;
498 /* RCU needed because of the VLAN locking rules (rcu || rtnl) */
501 vg = nbp_vlan_group_rcu(port);
503 vg = br_vlan_group_rcu(br);
505 if (!vg || !vg->num_vlans) {
509 if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
510 err = br_fill_ifvlaninfo_compressed(skb, vg);
512 err = br_fill_ifvlaninfo(skb, vg);
514 if (port && (port->flags & BR_VLAN_TUNNEL))
515 err = br_fill_vlan_tunnel_info(skb, vg);
518 goto nla_put_failure;
521 if (filter_mask & RTEXT_FILTER_MRP) {
524 if (!br_mrp_enabled(br) || port)
528 err = br_mrp_fill_info(skb, br);
532 goto nla_put_failure;
535 if (filter_mask & (RTEXT_FILTER_CFM_CONFIG | RTEXT_FILTER_CFM_STATUS)) {
536 struct nlattr *cfm_nest = NULL;
539 if (!br_cfm_created(br) || port)
542 cfm_nest = nla_nest_start(skb, IFLA_BRIDGE_CFM);
544 goto nla_put_failure;
546 if (filter_mask & RTEXT_FILTER_CFM_CONFIG) {
548 err = br_cfm_config_fill_info(skb, br);
551 goto nla_put_failure;
554 if (filter_mask & RTEXT_FILTER_CFM_STATUS) {
556 err = br_cfm_status_fill_info(skb, br, getlink);
559 goto nla_put_failure;
562 nla_nest_end(skb, cfm_nest);
567 nla_nest_end(skb, af);
572 nlmsg_cancel(skb, nlh);
576 void br_info_notify(int event, const struct net_bridge *br,
577 const struct net_bridge_port *port, u32 filter)
579 struct net_device *dev;
585 if (WARN_ON(!port && !br))
591 port_no = port->port_no;
597 br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
599 skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
603 err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false);
605 /* -EMSGSIZE implies BUG in br_nlmsg_size() */
606 WARN_ON(err == -EMSGSIZE);
610 rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
613 rtnl_set_sk_err(net, RTNLGRP_LINK, err);
616 /* Notify listeners of a change in bridge or port information */
617 void br_ifinfo_notify(int event, const struct net_bridge *br,
618 const struct net_bridge_port *port)
620 u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
622 return br_info_notify(event, br, port, filter);
626 * Dump information about all ports, in response to GETLINK
628 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
629 struct net_device *dev, u32 filter_mask, int nlflags)
631 struct net_bridge_port *port = br_port_get_rtnl(dev);
633 if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
634 !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
635 !(filter_mask & RTEXT_FILTER_MRP) &&
636 !(filter_mask & RTEXT_FILTER_CFM_CONFIG) &&
637 !(filter_mask & RTEXT_FILTER_CFM_STATUS))
640 return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
641 filter_mask, dev, true);
644 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
645 int cmd, struct bridge_vlan_info *vinfo, bool *changed,
646 struct netlink_ext_ack *extack)
654 /* if the MASTER flag is set this will act on the global
655 * per-VLAN entry as well
657 err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
658 &curr_change, extack);
660 vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
661 err = br_vlan_add(br, vinfo->vid, vinfo->flags,
662 &curr_change, extack);
670 if (!nbp_vlan_delete(p, vinfo->vid))
673 if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
674 !br_vlan_delete(p->br, vinfo->vid))
676 } else if (!br_vlan_delete(br, vinfo->vid)) {
685 int br_process_vlan_info(struct net_bridge *br,
686 struct net_bridge_port *p, int cmd,
687 struct bridge_vlan_info *vinfo_curr,
688 struct bridge_vlan_info **vinfo_last,
690 struct netlink_ext_ack *extack)
694 if (!br_vlan_valid_id(vinfo_curr->vid, extack))
697 /* needed for vlan-only NEWVLAN/DELVLAN notifications */
698 rtm_cmd = br_afspec_cmd_to_rtm(cmd);
700 if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
701 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
703 *vinfo_last = vinfo_curr;
708 struct bridge_vlan_info tmp_vinfo;
709 int v, v_change_start = 0;
711 if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
714 memcpy(&tmp_vinfo, *vinfo_last,
715 sizeof(struct bridge_vlan_info));
716 for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
717 bool curr_change = false;
720 err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
725 *changed = curr_change;
729 /* nothing to notify yet */
732 br_vlan_notify(br, p, v_change_start,
738 /* v_change_start is set only if the last/whole range changed */
740 br_vlan_notify(br, p, v_change_start,
748 err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
750 br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
755 static int br_afspec(struct net_bridge *br,
756 struct net_bridge_port *p,
757 struct nlattr *af_spec,
758 int cmd, bool *changed,
759 struct netlink_ext_ack *extack)
761 struct bridge_vlan_info *vinfo_curr = NULL;
762 struct bridge_vlan_info *vinfo_last = NULL;
764 struct vtunnel_info tinfo_last = {};
765 struct vtunnel_info tinfo_curr = {};
768 nla_for_each_nested(attr, af_spec, rem) {
770 switch (nla_type(attr)) {
771 case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
772 if (!p || !(p->flags & BR_VLAN_TUNNEL))
774 err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
777 err = br_process_vlan_tunnel_info(br, p, cmd,
784 case IFLA_BRIDGE_VLAN_INFO:
785 if (nla_len(attr) != sizeof(struct bridge_vlan_info))
787 vinfo_curr = nla_data(attr);
788 err = br_process_vlan_info(br, p, cmd, vinfo_curr,
789 &vinfo_last, changed,
794 case IFLA_BRIDGE_MRP:
795 err = br_mrp_parse(br, p, attr, cmd, extack);
799 case IFLA_BRIDGE_CFM:
800 err = br_cfm_parse(br, p, attr, cmd, extack);
810 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
811 [IFLA_BRPORT_STATE] = { .type = NLA_U8 },
812 [IFLA_BRPORT_COST] = { .type = NLA_U32 },
813 [IFLA_BRPORT_PRIORITY] = { .type = NLA_U16 },
814 [IFLA_BRPORT_MODE] = { .type = NLA_U8 },
815 [IFLA_BRPORT_GUARD] = { .type = NLA_U8 },
816 [IFLA_BRPORT_PROTECT] = { .type = NLA_U8 },
817 [IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
818 [IFLA_BRPORT_LEARNING] = { .type = NLA_U8 },
819 [IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
820 [IFLA_BRPORT_PROXYARP] = { .type = NLA_U8 },
821 [IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
822 [IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
823 [IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
824 [IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
825 [IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
826 [IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
827 [IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
828 [IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
829 [IFLA_BRPORT_ISOLATED] = { .type = NLA_U8 },
830 [IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
831 [IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 },
834 /* Change the state of the port and notify spanning tree */
835 static int br_set_port_state(struct net_bridge_port *p, u8 state)
837 if (state > BR_STATE_BLOCKING)
840 /* if kernel STP is running, don't allow changes */
841 if (p->br->stp_enabled == BR_KERNEL_STP)
844 /* if device is not up, change is not allowed
845 * if link is not present, only allowable state is disabled
847 if (!netif_running(p->dev) ||
848 (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
851 br_set_state(p, state);
852 br_port_state_selection(p->br);
856 /* Set/clear or port flags based on attribute */
857 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
858 int attrtype, unsigned long mask)
863 if (nla_get_u8(tb[attrtype]))
869 /* Process bridge protocol info on port */
870 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[],
871 struct netlink_ext_ack *extack)
873 unsigned long old_flags, changed_mask;
874 bool br_vlan_tunnel_old;
877 old_flags = p->flags;
878 br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false;
880 br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
881 br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
882 br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE,
883 BR_MULTICAST_FAST_LEAVE);
884 br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
885 br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
886 br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
887 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
888 br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST,
889 BR_MULTICAST_TO_UNICAST);
890 br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
891 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
892 br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
893 br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
894 br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS);
895 br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
897 changed_mask = old_flags ^ p->flags;
899 err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack);
901 p->flags = old_flags;
905 if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
906 nbp_vlan_tunnel_info_flush(p);
908 br_port_flags_change(p, changed_mask);
910 if (tb[IFLA_BRPORT_COST]) {
911 err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
916 if (tb[IFLA_BRPORT_PRIORITY]) {
917 err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
922 if (tb[IFLA_BRPORT_STATE]) {
923 err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
928 if (tb[IFLA_BRPORT_FLUSH])
929 br_fdb_delete_by_port(p->br, p, 0, 0);
931 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
932 if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
933 u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
935 err = br_multicast_set_port_router(p, mcast_router);
940 if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
943 hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
944 err = br_multicast_eht_set_hosts_limit(p, hlimit);
950 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
951 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
953 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
955 p->group_fwd_mask = fwd_mask;
958 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
959 struct net_device *backup_dev = NULL;
962 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
963 if (backup_ifindex) {
964 backup_dev = __dev_get_by_index(dev_net(p->dev),
970 err = nbp_backup_change(p, backup_dev);
978 /* Change state and parameters on port. */
979 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
980 struct netlink_ext_ack *extack)
982 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
983 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
984 struct net_bridge_port *p;
985 struct nlattr *protinfo;
986 struct nlattr *afspec;
987 bool changed = false;
990 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
991 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
992 if (!protinfo && !afspec)
995 p = br_port_get_rtnl(dev);
996 /* We want to accept dev as bridge itself if the AF_SPEC
997 * is set to see if someone is setting vlan info on the bridge
1002 if (p && protinfo) {
1003 if (protinfo->nla_type & NLA_F_NESTED) {
1004 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1011 spin_lock_bh(&p->br->lock);
1012 err = br_setport(p, tb, extack);
1013 spin_unlock_bh(&p->br->lock);
1015 /* Binary compatibility with old RSTP */
1016 if (nla_len(protinfo) < sizeof(u8))
1019 spin_lock_bh(&p->br->lock);
1020 err = br_set_port_state(p, nla_get_u8(protinfo));
1021 spin_unlock_bh(&p->br->lock);
1029 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
1032 br_ifinfo_notify(RTM_NEWLINK, br, p);
1037 /* Delete port information */
1038 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
1040 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1041 struct net_bridge_port *p;
1042 struct nlattr *afspec;
1043 bool changed = false;
1046 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1050 p = br_port_get_rtnl(dev);
1051 /* We want to accept dev as bridge itself as well */
1052 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
1055 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
1057 /* Send RTM_NEWLINK because userspace
1058 * expects RTM_NEWLINK for vlan dels
1060 br_ifinfo_notify(RTM_NEWLINK, br, p);
1065 static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1066 struct netlink_ext_ack *extack)
1068 if (tb[IFLA_ADDRESS]) {
1069 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1071 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1072 return -EADDRNOTAVAIL;
1078 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1079 if (data[IFLA_BR_VLAN_PROTOCOL] &&
1080 !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1081 return -EPROTONOSUPPORT;
1083 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1084 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1086 if (defpvid >= VLAN_VID_MASK)
1094 static int br_port_slave_changelink(struct net_device *brdev,
1095 struct net_device *dev,
1096 struct nlattr *tb[],
1097 struct nlattr *data[],
1098 struct netlink_ext_ack *extack)
1100 struct net_bridge *br = netdev_priv(brdev);
1106 spin_lock_bh(&br->lock);
1107 ret = br_setport(br_port_get_rtnl(dev), data, extack);
1108 spin_unlock_bh(&br->lock);
1113 static int br_port_fill_slave_info(struct sk_buff *skb,
1114 const struct net_device *brdev,
1115 const struct net_device *dev)
1117 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1120 static size_t br_port_get_slave_size(const struct net_device *brdev,
1121 const struct net_device *dev)
1123 return br_port_info_size();
1126 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1127 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1128 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1129 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
1130 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1131 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1132 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1133 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1134 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1135 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1136 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1138 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1139 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1140 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1141 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1142 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1143 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1144 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1145 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1146 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1147 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1148 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1149 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1150 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1151 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1152 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1153 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1154 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1155 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1156 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1157 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1158 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1159 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1160 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1161 [IFLA_BR_MULTI_BOOLOPT] =
1162 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1165 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1166 struct nlattr *data[],
1167 struct netlink_ext_ack *extack)
1169 struct net_bridge *br = netdev_priv(brdev);
1175 if (data[IFLA_BR_FORWARD_DELAY]) {
1176 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1181 if (data[IFLA_BR_HELLO_TIME]) {
1182 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1187 if (data[IFLA_BR_MAX_AGE]) {
1188 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1193 if (data[IFLA_BR_AGEING_TIME]) {
1194 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1199 if (data[IFLA_BR_STP_STATE]) {
1200 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1202 err = br_stp_set_enabled(br, stp_enabled, extack);
1207 if (data[IFLA_BR_PRIORITY]) {
1208 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1210 br_stp_set_bridge_priority(br, priority);
1213 if (data[IFLA_BR_VLAN_FILTERING]) {
1214 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1216 err = br_vlan_filter_toggle(br, vlan_filter, extack);
1221 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1222 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1223 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1225 err = __br_vlan_set_proto(br, vlan_proto, extack);
1230 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1231 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1233 err = __br_vlan_set_default_pvid(br, defpvid, extack);
1238 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1239 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1241 err = br_vlan_set_stats(br, vlan_stats);
1246 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1247 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1249 err = br_vlan_set_stats_per_port(br, per_port);
1255 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1256 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1258 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1260 br->group_fwd_mask = fwd_mask;
1263 if (data[IFLA_BR_GROUP_ADDR]) {
1264 u8 new_addr[ETH_ALEN];
1266 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1268 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1269 if (!is_link_local_ether_addr(new_addr))
1271 if (new_addr[5] == 1 || /* 802.3x Pause address */
1272 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1273 new_addr[5] == 3) /* 802.1X PAE address */
1275 spin_lock_bh(&br->lock);
1276 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1277 spin_unlock_bh(&br->lock);
1278 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1279 br_recalculate_fwd_mask(br);
1282 if (data[IFLA_BR_FDB_FLUSH])
1285 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1286 if (data[IFLA_BR_MCAST_ROUTER]) {
1287 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1289 err = br_multicast_set_router(br, multicast_router);
1294 if (data[IFLA_BR_MCAST_SNOOPING]) {
1295 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1297 err = br_multicast_toggle(br, mcast_snooping, extack);
1302 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1305 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1306 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1309 if (data[IFLA_BR_MCAST_QUERIER]) {
1310 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1312 err = br_multicast_set_querier(br, mcast_querier);
1317 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1318 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1321 if (data[IFLA_BR_MCAST_HASH_MAX])
1322 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1324 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1325 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1327 br->multicast_last_member_count = val;
1330 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1331 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1333 br->multicast_startup_query_count = val;
1336 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1337 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1339 br->multicast_last_member_interval = clock_t_to_jiffies(val);
1342 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1343 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1345 br->multicast_membership_interval = clock_t_to_jiffies(val);
1348 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1349 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1351 br->multicast_querier_interval = clock_t_to_jiffies(val);
1354 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1355 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1357 br->multicast_query_interval = clock_t_to_jiffies(val);
1360 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1361 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1363 br->multicast_query_response_interval = clock_t_to_jiffies(val);
1366 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1367 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1369 br->multicast_startup_query_interval = clock_t_to_jiffies(val);
1372 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1375 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1376 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1379 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1382 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1383 err = br_multicast_set_igmp_version(br, igmp_version);
1388 #if IS_ENABLED(CONFIG_IPV6)
1389 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1392 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1393 err = br_multicast_set_mld_version(br, mld_version);
1399 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1400 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1401 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1403 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1406 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1407 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1409 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1412 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1413 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1415 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1419 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1420 struct br_boolopt_multi *bm;
1422 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1423 err = br_boolopt_multi_toggle(br, bm, extack);
1431 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1432 struct nlattr *tb[], struct nlattr *data[],
1433 struct netlink_ext_ack *extack)
1435 struct net_bridge *br = netdev_priv(dev);
1438 err = register_netdevice(dev);
1442 if (tb[IFLA_ADDRESS]) {
1443 spin_lock_bh(&br->lock);
1444 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1445 spin_unlock_bh(&br->lock);
1448 err = br_changelink(dev, tb, data, extack);
1450 br_dev_delete(dev, NULL);
1455 static size_t br_get_size(const struct net_device *brdev)
1457 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1458 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1459 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1460 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1461 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1462 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1463 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1464 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1465 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1466 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1467 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
1468 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
1470 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1471 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1472 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1473 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1474 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1475 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1476 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1477 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1478 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1479 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1480 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1481 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1482 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1483 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1484 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1485 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1486 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1487 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
1488 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1489 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1490 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1491 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1492 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1493 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1494 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1495 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1496 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1497 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1498 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
1499 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
1501 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1502 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1503 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1504 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1506 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1510 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1512 struct net_bridge *br = netdev_priv(brdev);
1513 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1514 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1515 u32 age_time = jiffies_to_clock_t(br->max_age);
1516 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1517 u32 stp_enabled = br->stp_enabled;
1518 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1519 u8 vlan_enabled = br_vlan_enabled(br->dev);
1520 struct br_boolopt_multi bm;
1523 clockval = br_timer_value(&br->hello_timer);
1524 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1526 clockval = br_timer_value(&br->tcn_timer);
1527 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1529 clockval = br_timer_value(&br->topology_change_timer);
1530 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1533 clockval = br_timer_value(&br->gc_work.timer);
1534 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1537 br_boolopt_multi_get(br, &bm);
1538 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1539 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1540 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1541 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1542 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1543 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1544 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1545 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1546 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1548 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1549 &br->designated_root) ||
1550 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1551 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1552 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1553 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1554 br->topology_change_detected) ||
1555 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1556 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
1559 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1560 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1561 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1562 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1563 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1564 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1565 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1568 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1569 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER, br->multicast_router) ||
1570 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1571 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1572 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1573 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1574 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1575 br_opt_get(br, BROPT_MULTICAST_QUERIER)) ||
1576 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1577 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1578 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1579 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1580 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1581 br->multicast_last_member_count) ||
1582 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1583 br->multicast_startup_query_count) ||
1584 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1585 br->multicast_igmp_version))
1587 #if IS_ENABLED(CONFIG_IPV6)
1588 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1589 br->multicast_mld_version))
1592 clockval = jiffies_to_clock_t(br->multicast_last_member_interval);
1593 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1596 clockval = jiffies_to_clock_t(br->multicast_membership_interval);
1597 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1600 clockval = jiffies_to_clock_t(br->multicast_querier_interval);
1601 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1604 clockval = jiffies_to_clock_t(br->multicast_query_interval);
1605 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1608 clockval = jiffies_to_clock_t(br->multicast_query_response_interval);
1609 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1612 clockval = jiffies_to_clock_t(br->multicast_startup_query_interval);
1613 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1617 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1618 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1619 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1620 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1621 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1622 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1623 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1630 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1632 struct net_bridge_port *p = NULL;
1633 struct net_bridge_vlan_group *vg;
1634 struct net_bridge_vlan *v;
1635 struct net_bridge *br;
1639 case IFLA_STATS_LINK_XSTATS:
1640 br = netdev_priv(dev);
1641 vg = br_vlan_group(br);
1643 case IFLA_STATS_LINK_XSTATS_SLAVE:
1644 p = br_port_get_rtnl(dev);
1647 vg = nbp_vlan_group(p);
1654 /* we need to count all, even placeholder entries */
1655 list_for_each_entry(v, &vg->vlan_list, vlist)
1659 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1660 nla_total_size(sizeof(struct br_mcast_stats)) +
1664 static int br_fill_linkxstats(struct sk_buff *skb,
1665 const struct net_device *dev,
1666 int *prividx, int attr)
1668 struct nlattr *nla __maybe_unused;
1669 struct net_bridge_port *p = NULL;
1670 struct net_bridge_vlan_group *vg;
1671 struct net_bridge_vlan *v;
1672 struct net_bridge *br;
1673 struct nlattr *nest;
1677 case IFLA_STATS_LINK_XSTATS:
1678 br = netdev_priv(dev);
1679 vg = br_vlan_group(br);
1681 case IFLA_STATS_LINK_XSTATS_SLAVE:
1682 p = br_port_get_rtnl(dev);
1686 vg = nbp_vlan_group(p);
1692 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1699 pvid = br_get_pvid(vg);
1700 list_for_each_entry(v, &vg->vlan_list, vlist) {
1701 struct bridge_vlan_xstats vxi;
1702 struct pcpu_sw_netstats stats;
1704 if (++vl_idx < *prividx)
1706 memset(&vxi, 0, sizeof(vxi));
1708 vxi.flags = v->flags;
1710 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1711 br_vlan_get_stats(v, &stats);
1712 vxi.rx_bytes = stats.rx_bytes;
1713 vxi.rx_packets = stats.rx_packets;
1714 vxi.tx_bytes = stats.tx_bytes;
1715 vxi.tx_packets = stats.tx_packets;
1717 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1718 goto nla_put_failure;
1722 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1723 if (++vl_idx >= *prividx) {
1724 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1725 sizeof(struct br_mcast_stats),
1728 goto nla_put_failure;
1729 br_multicast_get_stats(br, p, nla_data(nla));
1734 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1735 sizeof(p->stp_xstats),
1738 goto nla_put_failure;
1740 spin_lock_bh(&br->lock);
1741 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1742 spin_unlock_bh(&br->lock);
1745 nla_nest_end(skb, nest);
1751 nla_nest_end(skb, nest);
1757 static struct rtnl_af_ops br_af_ops __read_mostly = {
1758 .family = AF_BRIDGE,
1759 .get_link_af_size = br_get_link_af_size_filtered,
1762 struct rtnl_link_ops br_link_ops __read_mostly = {
1764 .priv_size = sizeof(struct net_bridge),
1765 .setup = br_dev_setup,
1766 .maxtype = IFLA_BR_MAX,
1767 .policy = br_policy,
1768 .validate = br_validate,
1769 .newlink = br_dev_newlink,
1770 .changelink = br_changelink,
1771 .dellink = br_dev_delete,
1772 .get_size = br_get_size,
1773 .fill_info = br_fill_info,
1774 .fill_linkxstats = br_fill_linkxstats,
1775 .get_linkxstats_size = br_get_linkxstats_size,
1777 .slave_maxtype = IFLA_BRPORT_MAX,
1778 .slave_policy = br_port_policy,
1779 .slave_changelink = br_port_slave_changelink,
1780 .get_slave_size = br_port_get_slave_size,
1781 .fill_slave_info = br_port_fill_slave_info,
1784 int __init br_netlink_init(void)
1789 br_vlan_rtnl_init();
1790 rtnl_af_register(&br_af_ops);
1792 err = rtnl_link_register(&br_link_ops);
1799 rtnl_af_unregister(&br_af_ops);
1804 void br_netlink_fini(void)
1807 br_vlan_rtnl_uninit();
1808 rtnl_af_unregister(&br_af_ops);
1809 rtnl_link_unregister(&br_link_ops);