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_ctx.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->multicast_ctx,
941 if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
944 hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
945 err = br_multicast_eht_set_hosts_limit(p, hlimit);
951 if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
952 u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
954 if (fwd_mask & BR_GROUPFWD_MACPAUSE)
956 p->group_fwd_mask = fwd_mask;
959 if (tb[IFLA_BRPORT_BACKUP_PORT]) {
960 struct net_device *backup_dev = NULL;
963 backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
964 if (backup_ifindex) {
965 backup_dev = __dev_get_by_index(dev_net(p->dev),
971 err = nbp_backup_change(p, backup_dev);
979 /* Change state and parameters on port. */
980 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
981 struct netlink_ext_ack *extack)
983 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
984 struct nlattr *tb[IFLA_BRPORT_MAX + 1];
985 struct net_bridge_port *p;
986 struct nlattr *protinfo;
987 struct nlattr *afspec;
988 bool changed = false;
991 protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
992 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
993 if (!protinfo && !afspec)
996 p = br_port_get_rtnl(dev);
997 /* We want to accept dev as bridge itself if the AF_SPEC
998 * is set to see if someone is setting vlan info on the bridge
1003 if (p && protinfo) {
1004 if (protinfo->nla_type & NLA_F_NESTED) {
1005 err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1012 spin_lock_bh(&p->br->lock);
1013 err = br_setport(p, tb, extack);
1014 spin_unlock_bh(&p->br->lock);
1016 /* Binary compatibility with old RSTP */
1017 if (nla_len(protinfo) < sizeof(u8))
1020 spin_lock_bh(&p->br->lock);
1021 err = br_set_port_state(p, nla_get_u8(protinfo));
1022 spin_unlock_bh(&p->br->lock);
1030 err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
1033 br_ifinfo_notify(RTM_NEWLINK, br, p);
1038 /* Delete port information */
1039 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
1041 struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1042 struct net_bridge_port *p;
1043 struct nlattr *afspec;
1044 bool changed = false;
1047 afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1051 p = br_port_get_rtnl(dev);
1052 /* We want to accept dev as bridge itself as well */
1053 if (!p && !(dev->priv_flags & IFF_EBRIDGE))
1056 err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
1058 /* Send RTM_NEWLINK because userspace
1059 * expects RTM_NEWLINK for vlan dels
1061 br_ifinfo_notify(RTM_NEWLINK, br, p);
1066 static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1067 struct netlink_ext_ack *extack)
1069 if (tb[IFLA_ADDRESS]) {
1070 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1072 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1073 return -EADDRNOTAVAIL;
1079 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1080 if (data[IFLA_BR_VLAN_PROTOCOL] &&
1081 !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1082 return -EPROTONOSUPPORT;
1084 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1085 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1087 if (defpvid >= VLAN_VID_MASK)
1095 static int br_port_slave_changelink(struct net_device *brdev,
1096 struct net_device *dev,
1097 struct nlattr *tb[],
1098 struct nlattr *data[],
1099 struct netlink_ext_ack *extack)
1101 struct net_bridge *br = netdev_priv(brdev);
1107 spin_lock_bh(&br->lock);
1108 ret = br_setport(br_port_get_rtnl(dev), data, extack);
1109 spin_unlock_bh(&br->lock);
1114 static int br_port_fill_slave_info(struct sk_buff *skb,
1115 const struct net_device *brdev,
1116 const struct net_device *dev)
1118 return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1121 static size_t br_port_get_slave_size(const struct net_device *brdev,
1122 const struct net_device *dev)
1124 return br_port_info_size();
1127 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1128 [IFLA_BR_FORWARD_DELAY] = { .type = NLA_U32 },
1129 [IFLA_BR_HELLO_TIME] = { .type = NLA_U32 },
1130 [IFLA_BR_MAX_AGE] = { .type = NLA_U32 },
1131 [IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1132 [IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1133 [IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1134 [IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1135 [IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1136 [IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1137 [IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1139 [IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1140 [IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1141 [IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1142 [IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1143 [IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1144 [IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1145 [IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1146 [IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1147 [IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1148 [IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1149 [IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1150 [IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1151 [IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1152 [IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1153 [IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1154 [IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1155 [IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1156 [IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1157 [IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1158 [IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1159 [IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1160 [IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1161 [IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1162 [IFLA_BR_MULTI_BOOLOPT] =
1163 NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1166 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1167 struct nlattr *data[],
1168 struct netlink_ext_ack *extack)
1170 struct net_bridge *br = netdev_priv(brdev);
1176 if (data[IFLA_BR_FORWARD_DELAY]) {
1177 err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1182 if (data[IFLA_BR_HELLO_TIME]) {
1183 err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1188 if (data[IFLA_BR_MAX_AGE]) {
1189 err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1194 if (data[IFLA_BR_AGEING_TIME]) {
1195 err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1200 if (data[IFLA_BR_STP_STATE]) {
1201 u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1203 err = br_stp_set_enabled(br, stp_enabled, extack);
1208 if (data[IFLA_BR_PRIORITY]) {
1209 u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1211 br_stp_set_bridge_priority(br, priority);
1214 if (data[IFLA_BR_VLAN_FILTERING]) {
1215 u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1217 err = br_vlan_filter_toggle(br, vlan_filter, extack);
1222 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1223 if (data[IFLA_BR_VLAN_PROTOCOL]) {
1224 __be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1226 err = __br_vlan_set_proto(br, vlan_proto, extack);
1231 if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1232 __u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1234 err = __br_vlan_set_default_pvid(br, defpvid, extack);
1239 if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1240 __u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1242 err = br_vlan_set_stats(br, vlan_stats);
1247 if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1248 __u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1250 err = br_vlan_set_stats_per_port(br, per_port);
1256 if (data[IFLA_BR_GROUP_FWD_MASK]) {
1257 u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1259 if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1261 br->group_fwd_mask = fwd_mask;
1264 if (data[IFLA_BR_GROUP_ADDR]) {
1265 u8 new_addr[ETH_ALEN];
1267 if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1269 memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1270 if (!is_link_local_ether_addr(new_addr))
1272 if (new_addr[5] == 1 || /* 802.3x Pause address */
1273 new_addr[5] == 2 || /* 802.3ad Slow protocols */
1274 new_addr[5] == 3) /* 802.1X PAE address */
1276 spin_lock_bh(&br->lock);
1277 memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1278 spin_unlock_bh(&br->lock);
1279 br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1280 br_recalculate_fwd_mask(br);
1283 if (data[IFLA_BR_FDB_FLUSH])
1286 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1287 if (data[IFLA_BR_MCAST_ROUTER]) {
1288 u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1290 err = br_multicast_set_router(&br->multicast_ctx,
1296 if (data[IFLA_BR_MCAST_SNOOPING]) {
1297 u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1299 err = br_multicast_toggle(br, mcast_snooping, extack);
1304 if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1307 val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1308 br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1311 if (data[IFLA_BR_MCAST_QUERIER]) {
1312 u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1314 err = br_multicast_set_querier(&br->multicast_ctx,
1320 if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1321 br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1324 if (data[IFLA_BR_MCAST_HASH_MAX])
1325 br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1327 if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1328 u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1330 br->multicast_ctx.multicast_last_member_count = val;
1333 if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1334 u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1336 br->multicast_ctx.multicast_startup_query_count = val;
1339 if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1340 u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1342 br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
1345 if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1346 u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1348 br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
1351 if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1352 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1354 br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
1357 if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1358 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1360 br_multicast_set_query_intvl(&br->multicast_ctx, val);
1363 if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1364 u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1366 br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
1369 if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1370 u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1372 br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
1375 if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1378 mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1379 br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1382 if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1385 igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1386 err = br_multicast_set_igmp_version(&br->multicast_ctx,
1392 #if IS_ENABLED(CONFIG_IPV6)
1393 if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1396 mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1397 err = br_multicast_set_mld_version(&br->multicast_ctx,
1404 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1405 if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1406 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1408 br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1411 if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1412 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1414 br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1417 if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1418 u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1420 br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1424 if (data[IFLA_BR_MULTI_BOOLOPT]) {
1425 struct br_boolopt_multi *bm;
1427 bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1428 err = br_boolopt_multi_toggle(br, bm, extack);
1436 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1437 struct nlattr *tb[], struct nlattr *data[],
1438 struct netlink_ext_ack *extack)
1440 struct net_bridge *br = netdev_priv(dev);
1443 err = register_netdevice(dev);
1447 if (tb[IFLA_ADDRESS]) {
1448 spin_lock_bh(&br->lock);
1449 br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1450 spin_unlock_bh(&br->lock);
1453 err = br_changelink(dev, tb, data, extack);
1455 br_dev_delete(dev, NULL);
1460 static size_t br_get_size(const struct net_device *brdev)
1462 return nla_total_size(sizeof(u32)) + /* IFLA_BR_FORWARD_DELAY */
1463 nla_total_size(sizeof(u32)) + /* IFLA_BR_HELLO_TIME */
1464 nla_total_size(sizeof(u32)) + /* IFLA_BR_MAX_AGE */
1465 nla_total_size(sizeof(u32)) + /* IFLA_BR_AGEING_TIME */
1466 nla_total_size(sizeof(u32)) + /* IFLA_BR_STP_STATE */
1467 nla_total_size(sizeof(u16)) + /* IFLA_BR_PRIORITY */
1468 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_FILTERING */
1469 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1470 nla_total_size(sizeof(__be16)) + /* IFLA_BR_VLAN_PROTOCOL */
1471 nla_total_size(sizeof(u16)) + /* IFLA_BR_VLAN_DEFAULT_PVID */
1472 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_ENABLED */
1473 nla_total_size(sizeof(u8)) + /* IFLA_BR_VLAN_STATS_PER_PORT */
1475 nla_total_size(sizeof(u16)) + /* IFLA_BR_GROUP_FWD_MASK */
1476 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_ROOT_ID */
1477 nla_total_size(sizeof(struct ifla_bridge_id)) + /* IFLA_BR_BRIDGE_ID */
1478 nla_total_size(sizeof(u16)) + /* IFLA_BR_ROOT_PORT */
1479 nla_total_size(sizeof(u32)) + /* IFLA_BR_ROOT_PATH_COST */
1480 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE */
1481 nla_total_size(sizeof(u8)) + /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1482 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1483 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1484 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1485 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1486 nla_total_size(ETH_ALEN) + /* IFLA_BR_GROUP_ADDR */
1487 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1488 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_ROUTER */
1489 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_SNOOPING */
1490 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1491 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_QUERIER */
1492 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_STATS_ENABLED */
1493 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_ELASTICITY */
1494 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_HASH_MAX */
1495 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1496 nla_total_size(sizeof(u32)) + /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1497 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1498 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1499 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1500 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1501 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1502 nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1503 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_IGMP_VERSION */
1504 nla_total_size(sizeof(u8)) + /* IFLA_BR_MCAST_MLD_VERSION */
1505 br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
1507 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1508 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IPTABLES */
1509 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_IP6TABLES */
1510 nla_total_size(sizeof(u8)) + /* IFLA_BR_NF_CALL_ARPTABLES */
1512 nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1516 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1518 struct net_bridge *br = netdev_priv(brdev);
1519 u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1520 u32 hello_time = jiffies_to_clock_t(br->hello_time);
1521 u32 age_time = jiffies_to_clock_t(br->max_age);
1522 u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1523 u32 stp_enabled = br->stp_enabled;
1524 u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1525 u8 vlan_enabled = br_vlan_enabled(br->dev);
1526 struct br_boolopt_multi bm;
1529 clockval = br_timer_value(&br->hello_timer);
1530 if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1532 clockval = br_timer_value(&br->tcn_timer);
1533 if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1535 clockval = br_timer_value(&br->topology_change_timer);
1536 if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1539 clockval = br_timer_value(&br->gc_work.timer);
1540 if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1543 br_boolopt_multi_get(br, &bm);
1544 if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1545 nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1546 nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1547 nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1548 nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1549 nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1550 nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1551 nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1552 nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1554 nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1555 &br->designated_root) ||
1556 nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1557 nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1558 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1559 nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1560 br->topology_change_detected) ||
1561 nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1562 nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
1565 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1566 if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1567 nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1568 nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1569 br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1570 nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1571 br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1574 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1575 if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1576 br->multicast_ctx.multicast_router) ||
1577 nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1578 br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1579 nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1580 br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1581 nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1582 br->multicast_ctx.multicast_querier) ||
1583 nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1584 br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1585 nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1586 nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1587 nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1588 br->multicast_ctx.multicast_last_member_count) ||
1589 nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1590 br->multicast_ctx.multicast_startup_query_count) ||
1591 nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1592 br->multicast_ctx.multicast_igmp_version) ||
1593 br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1594 IFLA_BR_MCAST_QUERIER_STATE))
1596 #if IS_ENABLED(CONFIG_IPV6)
1597 if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1598 br->multicast_ctx.multicast_mld_version))
1601 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
1602 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1605 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
1606 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1609 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
1610 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1613 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
1614 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1617 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
1618 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1621 clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
1622 if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1626 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1627 if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1628 br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1629 nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1630 br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1631 nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1632 br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1639 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1641 struct net_bridge_port *p = NULL;
1642 struct net_bridge_vlan_group *vg;
1643 struct net_bridge_vlan *v;
1644 struct net_bridge *br;
1648 case IFLA_STATS_LINK_XSTATS:
1649 br = netdev_priv(dev);
1650 vg = br_vlan_group(br);
1652 case IFLA_STATS_LINK_XSTATS_SLAVE:
1653 p = br_port_get_rtnl(dev);
1656 vg = nbp_vlan_group(p);
1663 /* we need to count all, even placeholder entries */
1664 list_for_each_entry(v, &vg->vlan_list, vlist)
1668 return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1669 nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
1670 (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
1674 static int br_fill_linkxstats(struct sk_buff *skb,
1675 const struct net_device *dev,
1676 int *prividx, int attr)
1678 struct nlattr *nla __maybe_unused;
1679 struct net_bridge_port *p = NULL;
1680 struct net_bridge_vlan_group *vg;
1681 struct net_bridge_vlan *v;
1682 struct net_bridge *br;
1683 struct nlattr *nest;
1687 case IFLA_STATS_LINK_XSTATS:
1688 br = netdev_priv(dev);
1689 vg = br_vlan_group(br);
1691 case IFLA_STATS_LINK_XSTATS_SLAVE:
1692 p = br_port_get_rtnl(dev);
1696 vg = nbp_vlan_group(p);
1702 nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1709 pvid = br_get_pvid(vg);
1710 list_for_each_entry(v, &vg->vlan_list, vlist) {
1711 struct bridge_vlan_xstats vxi;
1712 struct pcpu_sw_netstats stats;
1714 if (++vl_idx < *prividx)
1716 memset(&vxi, 0, sizeof(vxi));
1718 vxi.flags = v->flags;
1720 vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1721 br_vlan_get_stats(v, &stats);
1722 vxi.rx_bytes = stats.rx_bytes;
1723 vxi.rx_packets = stats.rx_packets;
1724 vxi.tx_bytes = stats.tx_bytes;
1725 vxi.tx_packets = stats.tx_packets;
1727 if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1728 goto nla_put_failure;
1732 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1733 if (++vl_idx >= *prividx) {
1734 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1735 sizeof(struct br_mcast_stats),
1738 goto nla_put_failure;
1739 br_multicast_get_stats(br, p, nla_data(nla));
1744 nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1745 sizeof(p->stp_xstats),
1748 goto nla_put_failure;
1750 spin_lock_bh(&br->lock);
1751 memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1752 spin_unlock_bh(&br->lock);
1755 nla_nest_end(skb, nest);
1761 nla_nest_end(skb, nest);
1767 static struct rtnl_af_ops br_af_ops __read_mostly = {
1768 .family = AF_BRIDGE,
1769 .get_link_af_size = br_get_link_af_size_filtered,
1772 struct rtnl_link_ops br_link_ops __read_mostly = {
1774 .priv_size = sizeof(struct net_bridge),
1775 .setup = br_dev_setup,
1776 .maxtype = IFLA_BR_MAX,
1777 .policy = br_policy,
1778 .validate = br_validate,
1779 .newlink = br_dev_newlink,
1780 .changelink = br_changelink,
1781 .dellink = br_dev_delete,
1782 .get_size = br_get_size,
1783 .fill_info = br_fill_info,
1784 .fill_linkxstats = br_fill_linkxstats,
1785 .get_linkxstats_size = br_get_linkxstats_size,
1787 .slave_maxtype = IFLA_BRPORT_MAX,
1788 .slave_policy = br_port_policy,
1789 .slave_changelink = br_port_slave_changelink,
1790 .get_slave_size = br_port_get_slave_size,
1791 .fill_slave_info = br_port_fill_slave_info,
1794 int __init br_netlink_init(void)
1799 br_vlan_rtnl_init();
1800 rtnl_af_register(&br_af_ops);
1802 err = rtnl_link_register(&br_link_ops);
1809 rtnl_af_unregister(&br_af_ops);
1814 void br_netlink_fini(void)
1817 br_vlan_rtnl_uninit();
1818 rtnl_af_unregister(&br_af_ops);
1819 rtnl_link_unregister(&br_link_ops);