xref: /linux/net/bridge/br_netlink.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *	Bridge netlink control interface
4  *
5  *	Authors:
6  *	Stephen Hemminger		<shemminger@osdl.org>
7  */
8 
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>
14 #include <net/sock.h>
15 #include <uapi/linux/if_bridge.h>
16 
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"
22 
23 static int __get_num_vlan_infos(struct net_bridge_vlan_group *vg,
24 				u32 filter_mask)
25 {
26 	struct net_bridge_vlan *v;
27 	u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
28 	u16 flags, pvid;
29 	int num_vlans = 0;
30 
31 	if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
32 		return 0;
33 
34 	pvid = br_get_pvid(vg);
35 	/* Count number of vlan infos */
36 	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
37 		flags = 0;
38 		/* only a context, bridge vlan not activated */
39 		if (!br_vlan_should_use(v))
40 			continue;
41 		if (v->vid == pvid)
42 			flags |= BRIDGE_VLAN_INFO_PVID;
43 
44 		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
45 			flags |= BRIDGE_VLAN_INFO_UNTAGGED;
46 
47 		if (vid_range_start == 0) {
48 			goto initvars;
49 		} else if ((v->vid - vid_range_end) == 1 &&
50 			flags == vid_range_flags) {
51 			vid_range_end = v->vid;
52 			continue;
53 		} else {
54 			if ((vid_range_end - vid_range_start) > 0)
55 				num_vlans += 2;
56 			else
57 				num_vlans += 1;
58 		}
59 initvars:
60 		vid_range_start = v->vid;
61 		vid_range_end = v->vid;
62 		vid_range_flags = flags;
63 	}
64 
65 	if (vid_range_start != 0) {
66 		if ((vid_range_end - vid_range_start) > 0)
67 			num_vlans += 2;
68 		else
69 			num_vlans += 1;
70 	}
71 
72 	return num_vlans;
73 }
74 
75 static int br_get_num_vlan_infos(struct net_bridge_vlan_group *vg,
76 				 u32 filter_mask)
77 {
78 	int num_vlans;
79 
80 	if (!vg)
81 		return 0;
82 
83 	if (filter_mask & RTEXT_FILTER_BRVLAN)
84 		return vg->num_vlans;
85 
86 	rcu_read_lock();
87 	num_vlans = __get_num_vlan_infos(vg, filter_mask);
88 	rcu_read_unlock();
89 
90 	return num_vlans;
91 }
92 
93 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
94 					   u32 filter_mask)
95 {
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;
101 	size_t vinfo_sz = 0;
102 	int num_vlan_infos;
103 
104 	rcu_read_lock();
105 	if (netif_is_bridge_port(dev)) {
106 		p = br_port_get_check_rcu(dev);
107 		if (p)
108 			vg = nbp_vlan_group_rcu(p);
109 	} else if (netif_is_bridge_master(dev)) {
110 		br = netdev_priv(dev);
111 		vg = br_vlan_group_rcu(br);
112 	}
113 	num_vlan_infos = br_get_num_vlan_infos(vg, filter_mask);
114 	rcu_read_unlock();
115 
116 	if (p && (p->flags & BR_VLAN_TUNNEL))
117 		vinfo_sz += br_get_vlan_tunnel_info_size(vg);
118 
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));
121 
122 	if (p && vg && (filter_mask & RTEXT_FILTER_MST))
123 		vinfo_sz += br_mst_info_size(vg);
124 
125 	if (!(filter_mask & RTEXT_FILTER_CFM_STATUS))
126 		return vinfo_sz;
127 
128 	if (!br)
129 		return vinfo_sz;
130 
131 	/* CFM status info must be added */
132 	br_cfm_mep_count(br, &num_cfm_mep_infos);
133 	br_cfm_peer_mep_count(br, &num_cfm_peer_mep_infos);
134 
135 	vinfo_sz += nla_total_size(0);	/* IFLA_BRIDGE_CFM */
136 	/* For each status struct the MEP instance (u32) is added */
137 	/* MEP instance (u32) + br_cfm_mep_status */
138 	vinfo_sz += num_cfm_mep_infos *
139 		     /*IFLA_BRIDGE_CFM_MEP_STATUS_INSTANCE */
140 		    (nla_total_size(sizeof(u32))
141 		     /* IFLA_BRIDGE_CFM_MEP_STATUS_OPCODE_UNEXP_SEEN */
142 		     + nla_total_size(sizeof(u32))
143 		     /* IFLA_BRIDGE_CFM_MEP_STATUS_VERSION_UNEXP_SEEN */
144 		     + nla_total_size(sizeof(u32))
145 		     /* IFLA_BRIDGE_CFM_MEP_STATUS_RX_LEVEL_LOW_SEEN */
146 		     + nla_total_size(sizeof(u32)));
147 	/* MEP instance (u32) + br_cfm_cc_peer_status */
148 	vinfo_sz += num_cfm_peer_mep_infos *
149 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_INSTANCE */
150 		    (nla_total_size(sizeof(u32))
151 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PEER_MEPID */
152 		     + nla_total_size(sizeof(u32))
153 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_CCM_DEFECT */
154 		     + nla_total_size(sizeof(u32))
155 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_RDI */
156 		     + nla_total_size(sizeof(u32))
157 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_PORT_TLV_VALUE */
158 		     + nla_total_size(sizeof(u8))
159 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_IF_TLV_VALUE */
160 		     + nla_total_size(sizeof(u8))
161 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEEN */
162 		     + nla_total_size(sizeof(u32))
163 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_TLV_SEEN */
164 		     + nla_total_size(sizeof(u32))
165 		     /* IFLA_BRIDGE_CFM_CC_PEER_STATUS_SEQ_UNEXP_SEEN */
166 		     + nla_total_size(sizeof(u32)));
167 
168 	return vinfo_sz;
169 }
170 
171 static inline size_t br_port_info_size(void)
172 {
173 	return nla_total_size(1)	/* IFLA_BRPORT_STATE  */
174 		+ nla_total_size(2)	/* IFLA_BRPORT_PRIORITY */
175 		+ nla_total_size(4)	/* IFLA_BRPORT_COST */
176 		+ nla_total_size(1)	/* IFLA_BRPORT_MODE */
177 		+ nla_total_size(1)	/* IFLA_BRPORT_GUARD */
178 		+ nla_total_size(1)	/* IFLA_BRPORT_PROTECT */
179 		+ nla_total_size(1)	/* IFLA_BRPORT_FAST_LEAVE */
180 		+ nla_total_size(1)	/* IFLA_BRPORT_MCAST_TO_UCAST */
181 		+ nla_total_size(1)	/* IFLA_BRPORT_LEARNING */
182 		+ nla_total_size(1)	/* IFLA_BRPORT_UNICAST_FLOOD */
183 		+ nla_total_size(1)	/* IFLA_BRPORT_MCAST_FLOOD */
184 		+ nla_total_size(1)	/* IFLA_BRPORT_BCAST_FLOOD */
185 		+ nla_total_size(1)	/* IFLA_BRPORT_PROXYARP */
186 		+ nla_total_size(1)	/* IFLA_BRPORT_PROXYARP_WIFI */
187 		+ nla_total_size(1)	/* IFLA_BRPORT_VLAN_TUNNEL */
188 		+ nla_total_size(1)	/* IFLA_BRPORT_NEIGH_SUPPRESS */
189 		+ nla_total_size(1)	/* IFLA_BRPORT_ISOLATED */
190 		+ nla_total_size(1)	/* IFLA_BRPORT_LOCKED */
191 		+ nla_total_size(1)	/* IFLA_BRPORT_MAB */
192 		+ nla_total_size(1)	/* IFLA_BRPORT_NEIGH_VLAN_SUPPRESS */
193 		+ nla_total_size(sizeof(struct ifla_bridge_id))	/* IFLA_BRPORT_ROOT_ID */
194 		+ nla_total_size(sizeof(struct ifla_bridge_id))	/* IFLA_BRPORT_BRIDGE_ID */
195 		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_DESIGNATED_PORT */
196 		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_DESIGNATED_COST */
197 		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_ID */
198 		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_NO */
199 		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_TOPOLOGY_CHANGE_ACK */
200 		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_CONFIG_PENDING */
201 		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_MESSAGE_AGE_TIMER */
202 		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_FORWARD_DELAY_TIMER */
203 		+ nla_total_size_64bit(sizeof(u64)) /* IFLA_BRPORT_HOLD_TIMER */
204 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
205 		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_MULTICAST_ROUTER */
206 		+ nla_total_size(sizeof(u32))	/* IFLA_BRPORT_MCAST_N_GROUPS */
207 		+ nla_total_size(sizeof(u32))	/* IFLA_BRPORT_MCAST_MAX_GROUPS */
208 #endif
209 		+ nla_total_size(sizeof(u16))	/* IFLA_BRPORT_GROUP_FWD_MASK */
210 		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_MRP_RING_OPEN */
211 		+ nla_total_size(sizeof(u8))	/* IFLA_BRPORT_MRP_IN_OPEN */
212 		+ nla_total_size(sizeof(u32))	/* IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT */
213 		+ nla_total_size(sizeof(u32))	/* IFLA_BRPORT_MCAST_EHT_HOSTS_CNT */
214 		+ nla_total_size(sizeof(u32))	/* IFLA_BRPORT_BACKUP_NHID */
215 		+ 0;
216 }
217 
218 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
219 {
220 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
221 		+ nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
222 		+ nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
223 		+ nla_total_size(4) /* IFLA_MASTER */
224 		+ nla_total_size(4) /* IFLA_MTU */
225 		+ nla_total_size(4) /* IFLA_LINK */
226 		+ nla_total_size(1) /* IFLA_OPERSTATE */
227 		+ nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
228 		+ nla_total_size(br_get_link_af_size_filtered(dev,
229 				 filter_mask)) /* IFLA_AF_SPEC */
230 		+ nla_total_size(4); /* IFLA_BRPORT_BACKUP_PORT */
231 }
232 
233 static int br_port_fill_attrs(struct sk_buff *skb,
234 			      const struct net_bridge_port *p)
235 {
236 	u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
237 	struct net_bridge_port *backup_p;
238 	u64 timerval;
239 
240 	if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
241 	    nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
242 	    nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
243 	    nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
244 	    nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
245 	    nla_put_u8(skb, IFLA_BRPORT_PROTECT,
246 		       !!(p->flags & BR_ROOT_BLOCK)) ||
247 	    nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE,
248 		       !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
249 	    nla_put_u8(skb, IFLA_BRPORT_MCAST_TO_UCAST,
250 		       !!(p->flags & BR_MULTICAST_TO_UNICAST)) ||
251 	    nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
252 	    nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD,
253 		       !!(p->flags & BR_FLOOD)) ||
254 	    nla_put_u8(skb, IFLA_BRPORT_MCAST_FLOOD,
255 		       !!(p->flags & BR_MCAST_FLOOD)) ||
256 	    nla_put_u8(skb, IFLA_BRPORT_BCAST_FLOOD,
257 		       !!(p->flags & BR_BCAST_FLOOD)) ||
258 	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
259 	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
260 		       !!(p->flags & BR_PROXYARP_WIFI)) ||
261 	    nla_put(skb, IFLA_BRPORT_ROOT_ID, sizeof(struct ifla_bridge_id),
262 		    &p->designated_root) ||
263 	    nla_put(skb, IFLA_BRPORT_BRIDGE_ID, sizeof(struct ifla_bridge_id),
264 		    &p->designated_bridge) ||
265 	    nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_PORT, p->designated_port) ||
266 	    nla_put_u16(skb, IFLA_BRPORT_DESIGNATED_COST, p->designated_cost) ||
267 	    nla_put_u16(skb, IFLA_BRPORT_ID, p->port_id) ||
268 	    nla_put_u16(skb, IFLA_BRPORT_NO, p->port_no) ||
269 	    nla_put_u8(skb, IFLA_BRPORT_TOPOLOGY_CHANGE_ACK,
270 		       p->topology_change_ack) ||
271 	    nla_put_u8(skb, IFLA_BRPORT_CONFIG_PENDING, p->config_pending) ||
272 	    nla_put_u8(skb, IFLA_BRPORT_VLAN_TUNNEL, !!(p->flags &
273 							BR_VLAN_TUNNEL)) ||
274 	    nla_put_u16(skb, IFLA_BRPORT_GROUP_FWD_MASK, p->group_fwd_mask) ||
275 	    nla_put_u8(skb, IFLA_BRPORT_NEIGH_SUPPRESS,
276 		       !!(p->flags & BR_NEIGH_SUPPRESS)) ||
277 	    nla_put_u8(skb, IFLA_BRPORT_MRP_RING_OPEN, !!(p->flags &
278 							  BR_MRP_LOST_CONT)) ||
279 	    nla_put_u8(skb, IFLA_BRPORT_MRP_IN_OPEN,
280 		       !!(p->flags & BR_MRP_LOST_IN_CONT)) ||
281 	    nla_put_u8(skb, IFLA_BRPORT_ISOLATED, !!(p->flags & BR_ISOLATED)) ||
282 	    nla_put_u8(skb, IFLA_BRPORT_LOCKED, !!(p->flags & BR_PORT_LOCKED)) ||
283 	    nla_put_u8(skb, IFLA_BRPORT_MAB, !!(p->flags & BR_PORT_MAB)) ||
284 	    nla_put_u8(skb, IFLA_BRPORT_NEIGH_VLAN_SUPPRESS,
285 		       !!(p->flags & BR_NEIGH_VLAN_SUPPRESS)))
286 		return -EMSGSIZE;
287 
288 	timerval = br_timer_value(&p->message_age_timer);
289 	if (nla_put_u64_64bit(skb, IFLA_BRPORT_MESSAGE_AGE_TIMER, timerval,
290 			      IFLA_BRPORT_PAD))
291 		return -EMSGSIZE;
292 	timerval = br_timer_value(&p->forward_delay_timer);
293 	if (nla_put_u64_64bit(skb, IFLA_BRPORT_FORWARD_DELAY_TIMER, timerval,
294 			      IFLA_BRPORT_PAD))
295 		return -EMSGSIZE;
296 	timerval = br_timer_value(&p->hold_timer);
297 	if (nla_put_u64_64bit(skb, IFLA_BRPORT_HOLD_TIMER, timerval,
298 			      IFLA_BRPORT_PAD))
299 		return -EMSGSIZE;
300 
301 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
302 	if (nla_put_u8(skb, IFLA_BRPORT_MULTICAST_ROUTER,
303 		       p->multicast_ctx.multicast_router) ||
304 	    nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT,
305 			p->multicast_eht_hosts_limit) ||
306 	    nla_put_u32(skb, IFLA_BRPORT_MCAST_EHT_HOSTS_CNT,
307 			p->multicast_eht_hosts_cnt) ||
308 	    nla_put_u32(skb, IFLA_BRPORT_MCAST_N_GROUPS,
309 			br_multicast_ngroups_get(&p->multicast_ctx)) ||
310 	    nla_put_u32(skb, IFLA_BRPORT_MCAST_MAX_GROUPS,
311 			br_multicast_ngroups_get_max(&p->multicast_ctx)))
312 		return -EMSGSIZE;
313 #endif
314 
315 	/* we might be called only with br->lock */
316 	rcu_read_lock();
317 	backup_p = rcu_dereference(p->backup_port);
318 	if (backup_p)
319 		nla_put_u32(skb, IFLA_BRPORT_BACKUP_PORT,
320 			    backup_p->dev->ifindex);
321 	rcu_read_unlock();
322 
323 	if (p->backup_nhid &&
324 	    nla_put_u32(skb, IFLA_BRPORT_BACKUP_NHID, p->backup_nhid))
325 		return -EMSGSIZE;
326 
327 	return 0;
328 }
329 
330 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
331 				    u16 vid_end, u16 flags)
332 {
333 	struct  bridge_vlan_info vinfo;
334 
335 	if ((vid_end - vid_start) > 0) {
336 		/* add range to skb */
337 		vinfo.vid = vid_start;
338 		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
339 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
340 			    sizeof(vinfo), &vinfo))
341 			goto nla_put_failure;
342 
343 		vinfo.vid = vid_end;
344 		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
345 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
346 			    sizeof(vinfo), &vinfo))
347 			goto nla_put_failure;
348 	} else {
349 		vinfo.vid = vid_start;
350 		vinfo.flags = flags;
351 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
352 			    sizeof(vinfo), &vinfo))
353 			goto nla_put_failure;
354 	}
355 
356 	return 0;
357 
358 nla_put_failure:
359 	return -EMSGSIZE;
360 }
361 
362 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
363 					 struct net_bridge_vlan_group *vg)
364 {
365 	struct net_bridge_vlan *v;
366 	u16 vid_range_start = 0, vid_range_end = 0, vid_range_flags = 0;
367 	u16 flags, pvid;
368 	int err = 0;
369 
370 	/* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
371 	 * and mark vlan info with begin and end flags
372 	 * if vlaninfo represents a range
373 	 */
374 	pvid = br_get_pvid(vg);
375 	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
376 		flags = 0;
377 		if (!br_vlan_should_use(v))
378 			continue;
379 		if (v->vid == pvid)
380 			flags |= BRIDGE_VLAN_INFO_PVID;
381 
382 		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
383 			flags |= BRIDGE_VLAN_INFO_UNTAGGED;
384 
385 		if (vid_range_start == 0) {
386 			goto initvars;
387 		} else if ((v->vid - vid_range_end) == 1 &&
388 			flags == vid_range_flags) {
389 			vid_range_end = v->vid;
390 			continue;
391 		} else {
392 			err = br_fill_ifvlaninfo_range(skb, vid_range_start,
393 						       vid_range_end,
394 						       vid_range_flags);
395 			if (err)
396 				return err;
397 		}
398 
399 initvars:
400 		vid_range_start = v->vid;
401 		vid_range_end = v->vid;
402 		vid_range_flags = flags;
403 	}
404 
405 	if (vid_range_start != 0) {
406 		/* Call it once more to send any left over vlans */
407 		err = br_fill_ifvlaninfo_range(skb, vid_range_start,
408 					       vid_range_end,
409 					       vid_range_flags);
410 		if (err)
411 			return err;
412 	}
413 
414 	return 0;
415 }
416 
417 static int br_fill_ifvlaninfo(struct sk_buff *skb,
418 			      struct net_bridge_vlan_group *vg)
419 {
420 	struct bridge_vlan_info vinfo;
421 	struct net_bridge_vlan *v;
422 	u16 pvid;
423 
424 	pvid = br_get_pvid(vg);
425 	list_for_each_entry_rcu(v, &vg->vlan_list, vlist) {
426 		if (!br_vlan_should_use(v))
427 			continue;
428 
429 		vinfo.vid = v->vid;
430 		vinfo.flags = 0;
431 		if (v->vid == pvid)
432 			vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
433 
434 		if (v->flags & BRIDGE_VLAN_INFO_UNTAGGED)
435 			vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
436 
437 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
438 			    sizeof(vinfo), &vinfo))
439 			goto nla_put_failure;
440 	}
441 
442 	return 0;
443 
444 nla_put_failure:
445 	return -EMSGSIZE;
446 }
447 
448 /*
449  * Create one netlink message for one interface
450  * Contains port and master info as well as carrier and bridge state.
451  */
452 static int br_fill_ifinfo(struct sk_buff *skb,
453 			  const struct net_bridge_port *port,
454 			  u32 pid, u32 seq, int event, unsigned int flags,
455 			  u32 filter_mask, const struct net_device *dev,
456 			  bool getlink)
457 {
458 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
459 	struct nlattr *af = NULL;
460 	struct net_bridge *br;
461 	struct ifinfomsg *hdr;
462 	struct nlmsghdr *nlh;
463 
464 	if (port)
465 		br = port->br;
466 	else
467 		br = netdev_priv(dev);
468 
469 	br_debug(br, "br_fill_info event %d port %s master %s\n",
470 		     event, dev->name, br->dev->name);
471 
472 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
473 	if (nlh == NULL)
474 		return -EMSGSIZE;
475 
476 	hdr = nlmsg_data(nlh);
477 	hdr->ifi_family = AF_BRIDGE;
478 	hdr->__ifi_pad = 0;
479 	hdr->ifi_type = dev->type;
480 	hdr->ifi_index = dev->ifindex;
481 	hdr->ifi_flags = dev_get_flags(dev);
482 	hdr->ifi_change = 0;
483 
484 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
485 	    nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
486 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
487 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
488 	    (dev->addr_len &&
489 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
490 	    (dev->ifindex != dev_get_iflink(dev) &&
491 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
492 		goto nla_put_failure;
493 
494 	if (event == RTM_NEWLINK && port) {
495 		struct nlattr *nest;
496 
497 		nest = nla_nest_start(skb, IFLA_PROTINFO);
498 		if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
499 			goto nla_put_failure;
500 		nla_nest_end(skb, nest);
501 	}
502 
503 	if (filter_mask & (RTEXT_FILTER_BRVLAN |
504 			   RTEXT_FILTER_BRVLAN_COMPRESSED |
505 			   RTEXT_FILTER_MRP |
506 			   RTEXT_FILTER_CFM_CONFIG |
507 			   RTEXT_FILTER_CFM_STATUS |
508 			   RTEXT_FILTER_MST)) {
509 		af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
510 		if (!af)
511 			goto nla_put_failure;
512 	}
513 
514 	/* Check if  the VID information is requested */
515 	if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
516 	    (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
517 		struct net_bridge_vlan_group *vg;
518 		int err;
519 
520 		/* RCU needed because of the VLAN locking rules (rcu || rtnl) */
521 		rcu_read_lock();
522 		if (port)
523 			vg = nbp_vlan_group_rcu(port);
524 		else
525 			vg = br_vlan_group_rcu(br);
526 
527 		if (!vg || !vg->num_vlans) {
528 			rcu_read_unlock();
529 			goto done;
530 		}
531 		if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
532 			err = br_fill_ifvlaninfo_compressed(skb, vg);
533 		else
534 			err = br_fill_ifvlaninfo(skb, vg);
535 
536 		if (port && (port->flags & BR_VLAN_TUNNEL))
537 			err = br_fill_vlan_tunnel_info(skb, vg);
538 		rcu_read_unlock();
539 		if (err)
540 			goto nla_put_failure;
541 	}
542 
543 	if (filter_mask & RTEXT_FILTER_MRP) {
544 		int err;
545 
546 		if (!br_mrp_enabled(br) || port)
547 			goto done;
548 
549 		rcu_read_lock();
550 		err = br_mrp_fill_info(skb, br);
551 		rcu_read_unlock();
552 
553 		if (err)
554 			goto nla_put_failure;
555 	}
556 
557 	if (filter_mask & (RTEXT_FILTER_CFM_CONFIG | RTEXT_FILTER_CFM_STATUS)) {
558 		struct nlattr *cfm_nest = NULL;
559 		int err;
560 
561 		if (!br_cfm_created(br) || port)
562 			goto done;
563 
564 		cfm_nest = nla_nest_start(skb, IFLA_BRIDGE_CFM);
565 		if (!cfm_nest)
566 			goto nla_put_failure;
567 
568 		if (filter_mask & RTEXT_FILTER_CFM_CONFIG) {
569 			rcu_read_lock();
570 			err = br_cfm_config_fill_info(skb, br);
571 			rcu_read_unlock();
572 			if (err)
573 				goto nla_put_failure;
574 		}
575 
576 		if (filter_mask & RTEXT_FILTER_CFM_STATUS) {
577 			rcu_read_lock();
578 			err = br_cfm_status_fill_info(skb, br, getlink);
579 			rcu_read_unlock();
580 			if (err)
581 				goto nla_put_failure;
582 		}
583 
584 		nla_nest_end(skb, cfm_nest);
585 	}
586 
587 	if ((filter_mask & RTEXT_FILTER_MST) &&
588 	    br_opt_get(br, BROPT_MST_ENABLED) && port) {
589 		const struct net_bridge_vlan_group *vg = nbp_vlan_group(port);
590 		struct nlattr *mst_nest;
591 		int err;
592 
593 		if (!vg || !vg->num_vlans)
594 			goto done;
595 
596 		mst_nest = nla_nest_start(skb, IFLA_BRIDGE_MST);
597 		if (!mst_nest)
598 			goto nla_put_failure;
599 
600 		err = br_mst_fill_info(skb, vg);
601 		if (err)
602 			goto nla_put_failure;
603 
604 		nla_nest_end(skb, mst_nest);
605 	}
606 
607 done:
608 	if (af) {
609 		if (nlmsg_get_pos(skb) - (void *)af > nla_attr_size(0))
610 			nla_nest_end(skb, af);
611 		else
612 			nla_nest_cancel(skb, af);
613 	}
614 
615 	nlmsg_end(skb, nlh);
616 	return 0;
617 
618 nla_put_failure:
619 	nlmsg_cancel(skb, nlh);
620 	return -EMSGSIZE;
621 }
622 
623 void br_info_notify(int event, const struct net_bridge *br,
624 		    const struct net_bridge_port *port, u32 filter)
625 {
626 	struct net_device *dev;
627 	struct sk_buff *skb;
628 	int err = -ENOBUFS;
629 	struct net *net;
630 	u16 port_no = 0;
631 
632 	if (WARN_ON(!port && !br))
633 		return;
634 
635 	if (port) {
636 		dev = port->dev;
637 		br = port->br;
638 		port_no = port->port_no;
639 	} else {
640 		dev = br->dev;
641 	}
642 
643 	net = dev_net(dev);
644 	br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
645 
646 	skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
647 	if (skb == NULL)
648 		goto errout;
649 
650 	err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false);
651 	if (err < 0) {
652 		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
653 		WARN_ON(err == -EMSGSIZE);
654 		kfree_skb(skb);
655 		goto errout;
656 	}
657 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
658 	return;
659 errout:
660 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
661 }
662 
663 /* Notify listeners of a change in bridge or port information */
664 void br_ifinfo_notify(int event, const struct net_bridge *br,
665 		      const struct net_bridge_port *port)
666 {
667 	u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
668 
669 	return br_info_notify(event, br, port, filter);
670 }
671 
672 /*
673  * Dump information about all ports, in response to GETLINK
674  */
675 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
676 	       struct net_device *dev, u32 filter_mask, int nlflags)
677 {
678 	struct net_bridge_port *port = br_port_get_rtnl(dev);
679 
680 	if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
681 	    !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
682 	    !(filter_mask & RTEXT_FILTER_MRP) &&
683 	    !(filter_mask & RTEXT_FILTER_CFM_CONFIG) &&
684 	    !(filter_mask & RTEXT_FILTER_CFM_STATUS))
685 		return 0;
686 
687 	return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
688 			      filter_mask, dev, true);
689 }
690 
691 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
692 			int cmd, struct bridge_vlan_info *vinfo, bool *changed,
693 			struct netlink_ext_ack *extack)
694 {
695 	bool curr_change;
696 	int err = 0;
697 
698 	switch (cmd) {
699 	case RTM_SETLINK:
700 		if (p) {
701 			/* if the MASTER flag is set this will act on the global
702 			 * per-VLAN entry as well
703 			 */
704 			err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
705 					   &curr_change, extack);
706 		} else {
707 			vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
708 			err = br_vlan_add(br, vinfo->vid, vinfo->flags,
709 					  &curr_change, extack);
710 		}
711 		if (curr_change)
712 			*changed = true;
713 		break;
714 
715 	case RTM_DELLINK:
716 		if (p) {
717 			if (!nbp_vlan_delete(p, vinfo->vid))
718 				*changed = true;
719 
720 			if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
721 			    !br_vlan_delete(p->br, vinfo->vid))
722 				*changed = true;
723 		} else if (!br_vlan_delete(br, vinfo->vid)) {
724 			*changed = true;
725 		}
726 		break;
727 	}
728 
729 	return err;
730 }
731 
732 int br_process_vlan_info(struct net_bridge *br,
733 			 struct net_bridge_port *p, int cmd,
734 			 struct bridge_vlan_info *vinfo_curr,
735 			 struct bridge_vlan_info **vinfo_last,
736 			 bool *changed,
737 			 struct netlink_ext_ack *extack)
738 {
739 	int err, rtm_cmd;
740 
741 	if (!br_vlan_valid_id(vinfo_curr->vid, extack))
742 		return -EINVAL;
743 
744 	/* needed for vlan-only NEWVLAN/DELVLAN notifications */
745 	rtm_cmd = br_afspec_cmd_to_rtm(cmd);
746 
747 	if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
748 		if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
749 			return -EINVAL;
750 		*vinfo_last = vinfo_curr;
751 		return 0;
752 	}
753 
754 	if (*vinfo_last) {
755 		struct bridge_vlan_info tmp_vinfo;
756 		int v, v_change_start = 0;
757 
758 		if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
759 			return -EINVAL;
760 
761 		memcpy(&tmp_vinfo, *vinfo_last,
762 		       sizeof(struct bridge_vlan_info));
763 		for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
764 			bool curr_change = false;
765 
766 			tmp_vinfo.vid = v;
767 			err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
768 					   extack);
769 			if (err)
770 				break;
771 			if (curr_change) {
772 				*changed = curr_change;
773 				if (!v_change_start)
774 					v_change_start = v;
775 			} else {
776 				/* nothing to notify yet */
777 				if (!v_change_start)
778 					continue;
779 				br_vlan_notify(br, p, v_change_start,
780 					       v - 1, rtm_cmd);
781 				v_change_start = 0;
782 			}
783 			cond_resched();
784 		}
785 		/* v_change_start is set only if the last/whole range changed */
786 		if (v_change_start)
787 			br_vlan_notify(br, p, v_change_start,
788 				       v - 1, rtm_cmd);
789 
790 		*vinfo_last = NULL;
791 
792 		return err;
793 	}
794 
795 	err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
796 	if (*changed)
797 		br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
798 
799 	return err;
800 }
801 
802 static int br_afspec(struct net_bridge *br,
803 		     struct net_bridge_port *p,
804 		     struct nlattr *af_spec,
805 		     int cmd, bool *changed,
806 		     struct netlink_ext_ack *extack)
807 {
808 	struct bridge_vlan_info *vinfo_curr = NULL;
809 	struct bridge_vlan_info *vinfo_last = NULL;
810 	struct nlattr *attr;
811 	struct vtunnel_info tinfo_last = {};
812 	struct vtunnel_info tinfo_curr = {};
813 	int err = 0, rem;
814 
815 	nla_for_each_nested(attr, af_spec, rem) {
816 		err = 0;
817 		switch (nla_type(attr)) {
818 		case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
819 			if (!p || !(p->flags & BR_VLAN_TUNNEL))
820 				return -EINVAL;
821 			err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
822 			if (err)
823 				return err;
824 			err = br_process_vlan_tunnel_info(br, p, cmd,
825 							  &tinfo_curr,
826 							  &tinfo_last,
827 							  changed);
828 			if (err)
829 				return err;
830 			break;
831 		case IFLA_BRIDGE_VLAN_INFO:
832 			if (nla_len(attr) != sizeof(struct bridge_vlan_info))
833 				return -EINVAL;
834 			vinfo_curr = nla_data(attr);
835 			err = br_process_vlan_info(br, p, cmd, vinfo_curr,
836 						   &vinfo_last, changed,
837 						   extack);
838 			if (err)
839 				return err;
840 			break;
841 		case IFLA_BRIDGE_MRP:
842 			err = br_mrp_parse(br, p, attr, cmd, extack);
843 			if (err)
844 				return err;
845 			break;
846 		case IFLA_BRIDGE_CFM:
847 			err = br_cfm_parse(br, p, attr, cmd, extack);
848 			if (err)
849 				return err;
850 			break;
851 		case IFLA_BRIDGE_MST:
852 			if (!p) {
853 				NL_SET_ERR_MSG(extack,
854 					       "MST states can only be set on bridge ports");
855 				return -EINVAL;
856 			}
857 
858 			if (cmd != RTM_SETLINK) {
859 				NL_SET_ERR_MSG(extack,
860 					       "MST states can only be set through RTM_SETLINK");
861 				return -EINVAL;
862 			}
863 
864 			err = br_mst_process(p, attr, extack);
865 			if (err)
866 				return err;
867 			break;
868 		}
869 	}
870 
871 	return err;
872 }
873 
874 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
875 	[IFLA_BRPORT_UNSPEC]	= { .strict_start_type =
876 					IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT + 1 },
877 	[IFLA_BRPORT_STATE]	= { .type = NLA_U8 },
878 	[IFLA_BRPORT_COST]	= { .type = NLA_U32 },
879 	[IFLA_BRPORT_PRIORITY]	= { .type = NLA_U16 },
880 	[IFLA_BRPORT_MODE]	= { .type = NLA_U8 },
881 	[IFLA_BRPORT_GUARD]	= { .type = NLA_U8 },
882 	[IFLA_BRPORT_PROTECT]	= { .type = NLA_U8 },
883 	[IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
884 	[IFLA_BRPORT_LEARNING]	= { .type = NLA_U8 },
885 	[IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
886 	[IFLA_BRPORT_PROXYARP]	= { .type = NLA_U8 },
887 	[IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
888 	[IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
889 	[IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
890 	[IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
891 	[IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
892 	[IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
893 	[IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
894 	[IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
895 	[IFLA_BRPORT_ISOLATED]	= { .type = NLA_U8 },
896 	[IFLA_BRPORT_LOCKED] = { .type = NLA_U8 },
897 	[IFLA_BRPORT_MAB] = { .type = NLA_U8 },
898 	[IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
899 	[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 },
900 	[IFLA_BRPORT_MCAST_N_GROUPS] = { .type = NLA_REJECT },
901 	[IFLA_BRPORT_MCAST_MAX_GROUPS] = { .type = NLA_U32 },
902 	[IFLA_BRPORT_NEIGH_VLAN_SUPPRESS] = NLA_POLICY_MAX(NLA_U8, 1),
903 	[IFLA_BRPORT_BACKUP_NHID] = { .type = NLA_U32 },
904 };
905 
906 /* Change the state of the port and notify spanning tree */
907 static int br_set_port_state(struct net_bridge_port *p, u8 state)
908 {
909 	if (state > BR_STATE_BLOCKING)
910 		return -EINVAL;
911 
912 	/* if kernel STP is running, don't allow changes */
913 	if (p->br->stp_enabled == BR_KERNEL_STP)
914 		return -EBUSY;
915 
916 	/* if device is not up, change is not allowed
917 	 * if link is not present, only allowable state is disabled
918 	 */
919 	if (!netif_running(p->dev) ||
920 	    (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
921 		return -ENETDOWN;
922 
923 	br_set_state(p, state);
924 	br_port_state_selection(p->br);
925 	return 0;
926 }
927 
928 /* Set/clear or port flags based on attribute */
929 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
930 			     int attrtype, unsigned long mask)
931 {
932 	if (!tb[attrtype])
933 		return;
934 
935 	if (nla_get_u8(tb[attrtype]))
936 		p->flags |= mask;
937 	else
938 		p->flags &= ~mask;
939 }
940 
941 /* Process bridge protocol info on port */
942 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[],
943 		      struct netlink_ext_ack *extack)
944 {
945 	unsigned long old_flags, changed_mask;
946 	bool br_vlan_tunnel_old;
947 	int err;
948 
949 	old_flags = p->flags;
950 	br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false;
951 
952 	br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
953 	br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
954 	br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE,
955 			 BR_MULTICAST_FAST_LEAVE);
956 	br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
957 	br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
958 	br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
959 	br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
960 	br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST,
961 			 BR_MULTICAST_TO_UNICAST);
962 	br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
963 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
964 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
965 	br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
966 	br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS);
967 	br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
968 	br_set_port_flag(p, tb, IFLA_BRPORT_LOCKED, BR_PORT_LOCKED);
969 	br_set_port_flag(p, tb, IFLA_BRPORT_MAB, BR_PORT_MAB);
970 	br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_VLAN_SUPPRESS,
971 			 BR_NEIGH_VLAN_SUPPRESS);
972 
973 	if ((p->flags & BR_PORT_MAB) &&
974 	    (!(p->flags & BR_PORT_LOCKED) || !(p->flags & BR_LEARNING))) {
975 		NL_SET_ERR_MSG(extack, "Bridge port must be locked and have learning enabled when MAB is enabled");
976 		p->flags = old_flags;
977 		return -EINVAL;
978 	} else if (!(p->flags & BR_PORT_MAB) && (old_flags & BR_PORT_MAB)) {
979 		struct net_bridge_fdb_flush_desc desc = {
980 			.flags = BIT(BR_FDB_LOCKED),
981 			.flags_mask = BIT(BR_FDB_LOCKED),
982 			.port_ifindex = p->dev->ifindex,
983 		};
984 
985 		br_fdb_flush(p->br, &desc);
986 	}
987 
988 	changed_mask = old_flags ^ p->flags;
989 
990 	err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack);
991 	if (err) {
992 		p->flags = old_flags;
993 		return err;
994 	}
995 
996 	if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
997 		nbp_vlan_tunnel_info_flush(p);
998 
999 	br_port_flags_change(p, changed_mask);
1000 
1001 	if (tb[IFLA_BRPORT_COST]) {
1002 		err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
1003 		if (err)
1004 			return err;
1005 	}
1006 
1007 	if (tb[IFLA_BRPORT_PRIORITY]) {
1008 		err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
1009 		if (err)
1010 			return err;
1011 	}
1012 
1013 	if (tb[IFLA_BRPORT_STATE]) {
1014 		err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
1015 		if (err)
1016 			return err;
1017 	}
1018 
1019 	if (tb[IFLA_BRPORT_FLUSH])
1020 		br_fdb_delete_by_port(p->br, p, 0, 0);
1021 
1022 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1023 	if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
1024 		u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
1025 
1026 		err = br_multicast_set_port_router(&p->multicast_ctx,
1027 						   mcast_router);
1028 		if (err)
1029 			return err;
1030 	}
1031 
1032 	if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
1033 		u32 hlimit;
1034 
1035 		hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
1036 		err = br_multicast_eht_set_hosts_limit(p, hlimit);
1037 		if (err)
1038 			return err;
1039 	}
1040 
1041 	if (tb[IFLA_BRPORT_MCAST_MAX_GROUPS]) {
1042 		u32 max_groups;
1043 
1044 		max_groups = nla_get_u32(tb[IFLA_BRPORT_MCAST_MAX_GROUPS]);
1045 		br_multicast_ngroups_set_max(&p->multicast_ctx, max_groups);
1046 	}
1047 #endif
1048 
1049 	if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
1050 		u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
1051 
1052 		if (fwd_mask & BR_GROUPFWD_MACPAUSE)
1053 			return -EINVAL;
1054 		p->group_fwd_mask = fwd_mask;
1055 	}
1056 
1057 	if (tb[IFLA_BRPORT_BACKUP_PORT]) {
1058 		struct net_device *backup_dev = NULL;
1059 		u32 backup_ifindex;
1060 
1061 		backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
1062 		if (backup_ifindex) {
1063 			backup_dev = __dev_get_by_index(dev_net(p->dev),
1064 							backup_ifindex);
1065 			if (!backup_dev)
1066 				return -ENOENT;
1067 		}
1068 
1069 		err = nbp_backup_change(p, backup_dev);
1070 		if (err)
1071 			return err;
1072 	}
1073 
1074 	if (tb[IFLA_BRPORT_BACKUP_NHID]) {
1075 		u32 backup_nhid = nla_get_u32(tb[IFLA_BRPORT_BACKUP_NHID]);
1076 
1077 		WRITE_ONCE(p->backup_nhid, backup_nhid);
1078 	}
1079 
1080 	return 0;
1081 }
1082 
1083 /* Change state and parameters on port. */
1084 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
1085 	       struct netlink_ext_ack *extack)
1086 {
1087 	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1088 	struct nlattr *tb[IFLA_BRPORT_MAX + 1];
1089 	struct net_bridge_port *p;
1090 	struct nlattr *protinfo;
1091 	struct nlattr *afspec;
1092 	bool changed = false;
1093 	int err = 0;
1094 
1095 	protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
1096 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1097 	if (!protinfo && !afspec)
1098 		return 0;
1099 
1100 	p = br_port_get_rtnl(dev);
1101 	/* We want to accept dev as bridge itself if the AF_SPEC
1102 	 * is set to see if someone is setting vlan info on the bridge
1103 	 */
1104 	if (!p && !afspec)
1105 		return -EINVAL;
1106 
1107 	if (p && protinfo) {
1108 		if (protinfo->nla_type & NLA_F_NESTED) {
1109 			err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1110 							  protinfo,
1111 							  br_port_policy,
1112 							  NULL);
1113 			if (err)
1114 				return err;
1115 
1116 			spin_lock_bh(&p->br->lock);
1117 			err = br_setport(p, tb, extack);
1118 			spin_unlock_bh(&p->br->lock);
1119 		} else {
1120 			/* Binary compatibility with old RSTP */
1121 			if (nla_len(protinfo) < sizeof(u8))
1122 				return -EINVAL;
1123 
1124 			spin_lock_bh(&p->br->lock);
1125 			err = br_set_port_state(p, nla_get_u8(protinfo));
1126 			spin_unlock_bh(&p->br->lock);
1127 		}
1128 		if (err)
1129 			goto out;
1130 		changed = true;
1131 	}
1132 
1133 	if (afspec)
1134 		err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
1135 
1136 	if (changed)
1137 		br_ifinfo_notify(RTM_NEWLINK, br, p);
1138 out:
1139 	return err;
1140 }
1141 
1142 /* Delete port information */
1143 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
1144 {
1145 	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1146 	struct net_bridge_port *p;
1147 	struct nlattr *afspec;
1148 	bool changed = false;
1149 	int err = 0;
1150 
1151 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1152 	if (!afspec)
1153 		return 0;
1154 
1155 	p = br_port_get_rtnl(dev);
1156 	/* We want to accept dev as bridge itself as well */
1157 	if (!p && !netif_is_bridge_master(dev))
1158 		return -EINVAL;
1159 
1160 	err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
1161 	if (changed)
1162 		/* Send RTM_NEWLINK because userspace
1163 		 * expects RTM_NEWLINK for vlan dels
1164 		 */
1165 		br_ifinfo_notify(RTM_NEWLINK, br, p);
1166 
1167 	return err;
1168 }
1169 
1170 static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1171 		       struct netlink_ext_ack *extack)
1172 {
1173 	if (tb[IFLA_ADDRESS]) {
1174 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1175 			return -EINVAL;
1176 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1177 			return -EADDRNOTAVAIL;
1178 	}
1179 
1180 	if (!data)
1181 		return 0;
1182 
1183 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1184 	if (data[IFLA_BR_VLAN_PROTOCOL] &&
1185 	    !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1186 		return -EPROTONOSUPPORT;
1187 
1188 	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1189 		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1190 
1191 		if (defpvid >= VLAN_VID_MASK)
1192 			return -EINVAL;
1193 	}
1194 #endif
1195 
1196 	return 0;
1197 }
1198 
1199 static int br_port_slave_changelink(struct net_device *brdev,
1200 				    struct net_device *dev,
1201 				    struct nlattr *tb[],
1202 				    struct nlattr *data[],
1203 				    struct netlink_ext_ack *extack)
1204 {
1205 	struct net_bridge *br = netdev_priv(brdev);
1206 	int ret;
1207 
1208 	if (!data)
1209 		return 0;
1210 
1211 	spin_lock_bh(&br->lock);
1212 	ret = br_setport(br_port_get_rtnl(dev), data, extack);
1213 	spin_unlock_bh(&br->lock);
1214 
1215 	return ret;
1216 }
1217 
1218 static int br_port_fill_slave_info(struct sk_buff *skb,
1219 				   const struct net_device *brdev,
1220 				   const struct net_device *dev)
1221 {
1222 	return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1223 }
1224 
1225 static size_t br_port_get_slave_size(const struct net_device *brdev,
1226 				     const struct net_device *dev)
1227 {
1228 	return br_port_info_size();
1229 }
1230 
1231 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1232 	[IFLA_BR_UNSPEC]	= { .strict_start_type =
1233 				    IFLA_BR_FDB_N_LEARNED },
1234 	[IFLA_BR_FORWARD_DELAY]	= { .type = NLA_U32 },
1235 	[IFLA_BR_HELLO_TIME]	= { .type = NLA_U32 },
1236 	[IFLA_BR_MAX_AGE]	= { .type = NLA_U32 },
1237 	[IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1238 	[IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1239 	[IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1240 	[IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1241 	[IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1242 	[IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1243 	[IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1244 				 .len  = ETH_ALEN },
1245 	[IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1246 	[IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1247 	[IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1248 	[IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1249 	[IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1250 	[IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1251 	[IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1252 	[IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1253 	[IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1254 	[IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1255 	[IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1256 	[IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1257 	[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1258 	[IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1259 	[IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1260 	[IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1261 	[IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1262 	[IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1263 	[IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1264 	[IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1265 	[IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1266 	[IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1267 	[IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1268 	[IFLA_BR_MULTI_BOOLOPT] =
1269 		NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1270 	[IFLA_BR_FDB_N_LEARNED] = { .type = NLA_REJECT },
1271 	[IFLA_BR_FDB_MAX_LEARNED] = { .type = NLA_U32 },
1272 };
1273 
1274 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1275 			 struct nlattr *data[],
1276 			 struct netlink_ext_ack *extack)
1277 {
1278 	struct net_bridge *br = netdev_priv(brdev);
1279 	int err;
1280 
1281 	if (!data)
1282 		return 0;
1283 
1284 	if (data[IFLA_BR_FORWARD_DELAY]) {
1285 		err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1286 		if (err)
1287 			return err;
1288 	}
1289 
1290 	if (data[IFLA_BR_HELLO_TIME]) {
1291 		err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1292 		if (err)
1293 			return err;
1294 	}
1295 
1296 	if (data[IFLA_BR_MAX_AGE]) {
1297 		err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1298 		if (err)
1299 			return err;
1300 	}
1301 
1302 	if (data[IFLA_BR_AGEING_TIME]) {
1303 		err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1304 		if (err)
1305 			return err;
1306 	}
1307 
1308 	if (data[IFLA_BR_STP_STATE]) {
1309 		u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1310 
1311 		err = br_stp_set_enabled(br, stp_enabled, extack);
1312 		if (err)
1313 			return err;
1314 	}
1315 
1316 	if (data[IFLA_BR_PRIORITY]) {
1317 		u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1318 
1319 		br_stp_set_bridge_priority(br, priority);
1320 	}
1321 
1322 	if (data[IFLA_BR_VLAN_FILTERING]) {
1323 		u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1324 
1325 		err = br_vlan_filter_toggle(br, vlan_filter, extack);
1326 		if (err)
1327 			return err;
1328 	}
1329 
1330 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1331 	if (data[IFLA_BR_VLAN_PROTOCOL]) {
1332 		__be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1333 
1334 		err = __br_vlan_set_proto(br, vlan_proto, extack);
1335 		if (err)
1336 			return err;
1337 	}
1338 
1339 	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1340 		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1341 
1342 		err = __br_vlan_set_default_pvid(br, defpvid, extack);
1343 		if (err)
1344 			return err;
1345 	}
1346 
1347 	if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1348 		__u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1349 
1350 		err = br_vlan_set_stats(br, vlan_stats);
1351 		if (err)
1352 			return err;
1353 	}
1354 
1355 	if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1356 		__u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1357 
1358 		err = br_vlan_set_stats_per_port(br, per_port);
1359 		if (err)
1360 			return err;
1361 	}
1362 #endif
1363 
1364 	if (data[IFLA_BR_GROUP_FWD_MASK]) {
1365 		u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1366 
1367 		if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1368 			return -EINVAL;
1369 		br->group_fwd_mask = fwd_mask;
1370 	}
1371 
1372 	if (data[IFLA_BR_GROUP_ADDR]) {
1373 		u8 new_addr[ETH_ALEN];
1374 
1375 		if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1376 			return -EINVAL;
1377 		memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1378 		if (!is_link_local_ether_addr(new_addr))
1379 			return -EINVAL;
1380 		if (new_addr[5] == 1 ||		/* 802.3x Pause address */
1381 		    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
1382 		    new_addr[5] == 3)		/* 802.1X PAE address */
1383 			return -EINVAL;
1384 		spin_lock_bh(&br->lock);
1385 		memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1386 		spin_unlock_bh(&br->lock);
1387 		br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1388 		br_recalculate_fwd_mask(br);
1389 	}
1390 
1391 	if (data[IFLA_BR_FDB_FLUSH]) {
1392 		struct net_bridge_fdb_flush_desc desc = {
1393 			.flags_mask = BIT(BR_FDB_STATIC)
1394 		};
1395 
1396 		br_fdb_flush(br, &desc);
1397 	}
1398 
1399 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1400 	if (data[IFLA_BR_MCAST_ROUTER]) {
1401 		u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1402 
1403 		err = br_multicast_set_router(&br->multicast_ctx,
1404 					      multicast_router);
1405 		if (err)
1406 			return err;
1407 	}
1408 
1409 	if (data[IFLA_BR_MCAST_SNOOPING]) {
1410 		u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1411 
1412 		err = br_multicast_toggle(br, mcast_snooping, extack);
1413 		if (err)
1414 			return err;
1415 	}
1416 
1417 	if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1418 		u8 val;
1419 
1420 		val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1421 		br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1422 	}
1423 
1424 	if (data[IFLA_BR_MCAST_QUERIER]) {
1425 		u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1426 
1427 		err = br_multicast_set_querier(&br->multicast_ctx,
1428 					       mcast_querier);
1429 		if (err)
1430 			return err;
1431 	}
1432 
1433 	if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1434 		br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1435 			RHT_ELASTICITY);
1436 
1437 	if (data[IFLA_BR_MCAST_HASH_MAX])
1438 		br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1439 
1440 	if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1441 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1442 
1443 		br->multicast_ctx.multicast_last_member_count = val;
1444 	}
1445 
1446 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1447 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1448 
1449 		br->multicast_ctx.multicast_startup_query_count = val;
1450 	}
1451 
1452 	if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1453 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1454 
1455 		br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
1456 	}
1457 
1458 	if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1459 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1460 
1461 		br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
1462 	}
1463 
1464 	if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1465 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1466 
1467 		br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
1468 	}
1469 
1470 	if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1471 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1472 
1473 		br_multicast_set_query_intvl(&br->multicast_ctx, val);
1474 	}
1475 
1476 	if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1477 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1478 
1479 		br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
1480 	}
1481 
1482 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1483 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1484 
1485 		br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
1486 	}
1487 
1488 	if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1489 		__u8 mcast_stats;
1490 
1491 		mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1492 		br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1493 	}
1494 
1495 	if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1496 		__u8 igmp_version;
1497 
1498 		igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1499 		err = br_multicast_set_igmp_version(&br->multicast_ctx,
1500 						    igmp_version);
1501 		if (err)
1502 			return err;
1503 	}
1504 
1505 #if IS_ENABLED(CONFIG_IPV6)
1506 	if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1507 		__u8 mld_version;
1508 
1509 		mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1510 		err = br_multicast_set_mld_version(&br->multicast_ctx,
1511 						   mld_version);
1512 		if (err)
1513 			return err;
1514 	}
1515 #endif
1516 #endif
1517 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1518 	if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1519 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1520 
1521 		br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1522 	}
1523 
1524 	if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1525 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1526 
1527 		br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1528 	}
1529 
1530 	if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1531 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1532 
1533 		br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1534 	}
1535 #endif
1536 
1537 	if (data[IFLA_BR_MULTI_BOOLOPT]) {
1538 		struct br_boolopt_multi *bm;
1539 
1540 		bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1541 		err = br_boolopt_multi_toggle(br, bm, extack);
1542 		if (err)
1543 			return err;
1544 	}
1545 
1546 	if (data[IFLA_BR_FDB_MAX_LEARNED]) {
1547 		u32 val = nla_get_u32(data[IFLA_BR_FDB_MAX_LEARNED]);
1548 
1549 		WRITE_ONCE(br->fdb_max_learned, val);
1550 	}
1551 
1552 	return 0;
1553 }
1554 
1555 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1556 			  struct nlattr *tb[], struct nlattr *data[],
1557 			  struct netlink_ext_ack *extack)
1558 {
1559 	struct net_bridge *br = netdev_priv(dev);
1560 	int err;
1561 
1562 	err = register_netdevice(dev);
1563 	if (err)
1564 		return err;
1565 
1566 	if (tb[IFLA_ADDRESS]) {
1567 		spin_lock_bh(&br->lock);
1568 		br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1569 		spin_unlock_bh(&br->lock);
1570 	}
1571 
1572 	err = br_changelink(dev, tb, data, extack);
1573 	if (err)
1574 		br_dev_delete(dev, NULL);
1575 
1576 	return err;
1577 }
1578 
1579 static size_t br_get_size(const struct net_device *brdev)
1580 {
1581 	return nla_total_size(sizeof(u32)) +	/* IFLA_BR_FORWARD_DELAY  */
1582 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_HELLO_TIME */
1583 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_MAX_AGE */
1584 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1585 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1586 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1587 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1588 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1589 	       nla_total_size(sizeof(__be16)) +	/* IFLA_BR_VLAN_PROTOCOL */
1590 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1591 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_ENABLED */
1592 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_VLAN_STATS_PER_PORT */
1593 #endif
1594 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1595 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1596 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1597 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1598 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1599 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1600 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1601 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1602 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1603 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1604 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1605 	       nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1606 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_FDB_N_LEARNED */
1607 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_FDB_MAX_LEARNED */
1608 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1609 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1610 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1611 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1612 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1613 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_STATS_ENABLED */
1614 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1615 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1616 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1617 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1618 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1619 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1620 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1621 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1622 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1623 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1624 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_IGMP_VERSION */
1625 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_MLD_VERSION */
1626 	       br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
1627 #endif
1628 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1629 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1630 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1631 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1632 #endif
1633 	       nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1634 	       0;
1635 }
1636 
1637 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1638 {
1639 	struct net_bridge *br = netdev_priv(brdev);
1640 	u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1641 	u32 hello_time = jiffies_to_clock_t(br->hello_time);
1642 	u32 age_time = jiffies_to_clock_t(br->max_age);
1643 	u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1644 	u32 stp_enabled = br->stp_enabled;
1645 	u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1646 	u8 vlan_enabled = br_vlan_enabled(br->dev);
1647 	struct br_boolopt_multi bm;
1648 	u64 clockval;
1649 
1650 	clockval = br_timer_value(&br->hello_timer);
1651 	if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1652 		return -EMSGSIZE;
1653 	clockval = br_timer_value(&br->tcn_timer);
1654 	if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1655 		return -EMSGSIZE;
1656 	clockval = br_timer_value(&br->topology_change_timer);
1657 	if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1658 			      IFLA_BR_PAD))
1659 		return -EMSGSIZE;
1660 	clockval = br_timer_value(&br->gc_work.timer);
1661 	if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1662 		return -EMSGSIZE;
1663 
1664 	br_boolopt_multi_get(br, &bm);
1665 	if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1666 	    nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1667 	    nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1668 	    nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1669 	    nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1670 	    nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1671 	    nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1672 	    nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1673 	    nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1674 		    &br->bridge_id) ||
1675 	    nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1676 		    &br->designated_root) ||
1677 	    nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1678 	    nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1679 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1680 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1681 		       br->topology_change_detected) ||
1682 	    nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1683 	    nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm) ||
1684 	    nla_put_u32(skb, IFLA_BR_FDB_N_LEARNED,
1685 			atomic_read(&br->fdb_n_learned)) ||
1686 	    nla_put_u32(skb, IFLA_BR_FDB_MAX_LEARNED, br->fdb_max_learned))
1687 		return -EMSGSIZE;
1688 
1689 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1690 	if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1691 	    nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1692 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1693 		       br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1694 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1695 		       br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1696 		return -EMSGSIZE;
1697 #endif
1698 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1699 	if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1700 		       br->multicast_ctx.multicast_router) ||
1701 	    nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1702 		       br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1703 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1704 		       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1705 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1706 		       br->multicast_ctx.multicast_querier) ||
1707 	    nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1708 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1709 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1710 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1711 	    nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1712 			br->multicast_ctx.multicast_last_member_count) ||
1713 	    nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1714 			br->multicast_ctx.multicast_startup_query_count) ||
1715 	    nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1716 		       br->multicast_ctx.multicast_igmp_version) ||
1717 	    br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1718 					    IFLA_BR_MCAST_QUERIER_STATE))
1719 		return -EMSGSIZE;
1720 #if IS_ENABLED(CONFIG_IPV6)
1721 	if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1722 		       br->multicast_ctx.multicast_mld_version))
1723 		return -EMSGSIZE;
1724 #endif
1725 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
1726 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1727 			      IFLA_BR_PAD))
1728 		return -EMSGSIZE;
1729 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
1730 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1731 			      IFLA_BR_PAD))
1732 		return -EMSGSIZE;
1733 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
1734 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1735 			      IFLA_BR_PAD))
1736 		return -EMSGSIZE;
1737 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
1738 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1739 			      IFLA_BR_PAD))
1740 		return -EMSGSIZE;
1741 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
1742 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1743 			      IFLA_BR_PAD))
1744 		return -EMSGSIZE;
1745 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
1746 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1747 			      IFLA_BR_PAD))
1748 		return -EMSGSIZE;
1749 #endif
1750 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1751 	if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1752 		       br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1753 	    nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1754 		       br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1755 	    nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1756 		       br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1757 		return -EMSGSIZE;
1758 #endif
1759 
1760 	return 0;
1761 }
1762 
1763 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1764 {
1765 	struct net_bridge_port *p = NULL;
1766 	struct net_bridge_vlan_group *vg;
1767 	struct net_bridge_vlan *v;
1768 	struct net_bridge *br;
1769 	int numvls = 0;
1770 
1771 	switch (attr) {
1772 	case IFLA_STATS_LINK_XSTATS:
1773 		br = netdev_priv(dev);
1774 		vg = br_vlan_group(br);
1775 		break;
1776 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1777 		p = br_port_get_rtnl(dev);
1778 		if (!p)
1779 			return 0;
1780 		vg = nbp_vlan_group(p);
1781 		break;
1782 	default:
1783 		return 0;
1784 	}
1785 
1786 	if (vg) {
1787 		/* we need to count all, even placeholder entries */
1788 		list_for_each_entry(v, &vg->vlan_list, vlist)
1789 			numvls++;
1790 	}
1791 
1792 	return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1793 	       nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
1794 	       (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
1795 	       nla_total_size(0);
1796 }
1797 
1798 static int br_fill_linkxstats(struct sk_buff *skb,
1799 			      const struct net_device *dev,
1800 			      int *prividx, int attr)
1801 {
1802 	struct nlattr *nla __maybe_unused;
1803 	struct net_bridge_port *p = NULL;
1804 	struct net_bridge_vlan_group *vg;
1805 	struct net_bridge_vlan *v;
1806 	struct net_bridge *br;
1807 	struct nlattr *nest;
1808 	int vl_idx = 0;
1809 
1810 	switch (attr) {
1811 	case IFLA_STATS_LINK_XSTATS:
1812 		br = netdev_priv(dev);
1813 		vg = br_vlan_group(br);
1814 		break;
1815 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1816 		p = br_port_get_rtnl(dev);
1817 		if (!p)
1818 			return 0;
1819 		br = p->br;
1820 		vg = nbp_vlan_group(p);
1821 		break;
1822 	default:
1823 		return -EINVAL;
1824 	}
1825 
1826 	nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1827 	if (!nest)
1828 		return -EMSGSIZE;
1829 
1830 	if (vg) {
1831 		u16 pvid;
1832 
1833 		pvid = br_get_pvid(vg);
1834 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1835 			struct bridge_vlan_xstats vxi;
1836 			struct pcpu_sw_netstats stats;
1837 
1838 			if (++vl_idx < *prividx)
1839 				continue;
1840 			memset(&vxi, 0, sizeof(vxi));
1841 			vxi.vid = v->vid;
1842 			vxi.flags = v->flags;
1843 			if (v->vid == pvid)
1844 				vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1845 			br_vlan_get_stats(v, &stats);
1846 			vxi.rx_bytes = u64_stats_read(&stats.rx_bytes);
1847 			vxi.rx_packets = u64_stats_read(&stats.rx_packets);
1848 			vxi.tx_bytes = u64_stats_read(&stats.tx_bytes);
1849 			vxi.tx_packets = u64_stats_read(&stats.tx_packets);
1850 
1851 			if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1852 				goto nla_put_failure;
1853 		}
1854 	}
1855 
1856 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1857 	if (++vl_idx >= *prividx) {
1858 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1859 					sizeof(struct br_mcast_stats),
1860 					BRIDGE_XSTATS_PAD);
1861 		if (!nla)
1862 			goto nla_put_failure;
1863 		br_multicast_get_stats(br, p, nla_data(nla));
1864 	}
1865 #endif
1866 
1867 	if (p) {
1868 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1869 					sizeof(p->stp_xstats),
1870 					BRIDGE_XSTATS_PAD);
1871 		if (!nla)
1872 			goto nla_put_failure;
1873 
1874 		spin_lock_bh(&br->lock);
1875 		memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1876 		spin_unlock_bh(&br->lock);
1877 	}
1878 
1879 	nla_nest_end(skb, nest);
1880 	*prividx = 0;
1881 
1882 	return 0;
1883 
1884 nla_put_failure:
1885 	nla_nest_end(skb, nest);
1886 	*prividx = vl_idx;
1887 
1888 	return -EMSGSIZE;
1889 }
1890 
1891 static struct rtnl_af_ops br_af_ops __read_mostly = {
1892 	.family			= AF_BRIDGE,
1893 	.get_link_af_size	= br_get_link_af_size_filtered,
1894 };
1895 
1896 struct rtnl_link_ops br_link_ops __read_mostly = {
1897 	.kind			= "bridge",
1898 	.priv_size		= sizeof(struct net_bridge),
1899 	.setup			= br_dev_setup,
1900 	.maxtype		= IFLA_BR_MAX,
1901 	.policy			= br_policy,
1902 	.validate		= br_validate,
1903 	.newlink		= br_dev_newlink,
1904 	.changelink		= br_changelink,
1905 	.dellink		= br_dev_delete,
1906 	.get_size		= br_get_size,
1907 	.fill_info		= br_fill_info,
1908 	.fill_linkxstats	= br_fill_linkxstats,
1909 	.get_linkxstats_size	= br_get_linkxstats_size,
1910 
1911 	.slave_maxtype		= IFLA_BRPORT_MAX,
1912 	.slave_policy		= br_port_policy,
1913 	.slave_changelink	= br_port_slave_changelink,
1914 	.get_slave_size		= br_port_get_slave_size,
1915 	.fill_slave_info	= br_port_fill_slave_info,
1916 };
1917 
1918 int __init br_netlink_init(void)
1919 {
1920 	int err;
1921 
1922 	br_vlan_rtnl_init();
1923 	rtnl_af_register(&br_af_ops);
1924 
1925 	err = rtnl_link_register(&br_link_ops);
1926 	if (err)
1927 		goto out_af;
1928 
1929 	return 0;
1930 
1931 out_af:
1932 	rtnl_af_unregister(&br_af_ops);
1933 	return err;
1934 }
1935 
1936 void br_netlink_fini(void)
1937 {
1938 	br_vlan_rtnl_uninit();
1939 	rtnl_af_unregister(&br_af_ops);
1940 	rtnl_link_unregister(&br_link_ops);
1941 }
1942