xref: /openbmc/linux/net/bridge/br_netlink.c (revision f0931824)
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 	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_FORWARD_DELAY]	= { .type = NLA_U32 },
1233 	[IFLA_BR_HELLO_TIME]	= { .type = NLA_U32 },
1234 	[IFLA_BR_MAX_AGE]	= { .type = NLA_U32 },
1235 	[IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1236 	[IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1237 	[IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1238 	[IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1239 	[IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1240 	[IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1241 	[IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1242 				 .len  = ETH_ALEN },
1243 	[IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1244 	[IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1245 	[IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1246 	[IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1247 	[IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1248 	[IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1249 	[IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1250 	[IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1251 	[IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1252 	[IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1253 	[IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1254 	[IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1255 	[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1256 	[IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1257 	[IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1258 	[IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1259 	[IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1260 	[IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1261 	[IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1262 	[IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1263 	[IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1264 	[IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1265 	[IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1266 	[IFLA_BR_MULTI_BOOLOPT] =
1267 		NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1268 };
1269 
1270 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1271 			 struct nlattr *data[],
1272 			 struct netlink_ext_ack *extack)
1273 {
1274 	struct net_bridge *br = netdev_priv(brdev);
1275 	int err;
1276 
1277 	if (!data)
1278 		return 0;
1279 
1280 	if (data[IFLA_BR_FORWARD_DELAY]) {
1281 		err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1282 		if (err)
1283 			return err;
1284 	}
1285 
1286 	if (data[IFLA_BR_HELLO_TIME]) {
1287 		err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1288 		if (err)
1289 			return err;
1290 	}
1291 
1292 	if (data[IFLA_BR_MAX_AGE]) {
1293 		err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1294 		if (err)
1295 			return err;
1296 	}
1297 
1298 	if (data[IFLA_BR_AGEING_TIME]) {
1299 		err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1300 		if (err)
1301 			return err;
1302 	}
1303 
1304 	if (data[IFLA_BR_STP_STATE]) {
1305 		u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1306 
1307 		err = br_stp_set_enabled(br, stp_enabled, extack);
1308 		if (err)
1309 			return err;
1310 	}
1311 
1312 	if (data[IFLA_BR_PRIORITY]) {
1313 		u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1314 
1315 		br_stp_set_bridge_priority(br, priority);
1316 	}
1317 
1318 	if (data[IFLA_BR_VLAN_FILTERING]) {
1319 		u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1320 
1321 		err = br_vlan_filter_toggle(br, vlan_filter, extack);
1322 		if (err)
1323 			return err;
1324 	}
1325 
1326 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1327 	if (data[IFLA_BR_VLAN_PROTOCOL]) {
1328 		__be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1329 
1330 		err = __br_vlan_set_proto(br, vlan_proto, extack);
1331 		if (err)
1332 			return err;
1333 	}
1334 
1335 	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1336 		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1337 
1338 		err = __br_vlan_set_default_pvid(br, defpvid, extack);
1339 		if (err)
1340 			return err;
1341 	}
1342 
1343 	if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1344 		__u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1345 
1346 		err = br_vlan_set_stats(br, vlan_stats);
1347 		if (err)
1348 			return err;
1349 	}
1350 
1351 	if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1352 		__u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1353 
1354 		err = br_vlan_set_stats_per_port(br, per_port);
1355 		if (err)
1356 			return err;
1357 	}
1358 #endif
1359 
1360 	if (data[IFLA_BR_GROUP_FWD_MASK]) {
1361 		u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1362 
1363 		if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1364 			return -EINVAL;
1365 		br->group_fwd_mask = fwd_mask;
1366 	}
1367 
1368 	if (data[IFLA_BR_GROUP_ADDR]) {
1369 		u8 new_addr[ETH_ALEN];
1370 
1371 		if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1372 			return -EINVAL;
1373 		memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1374 		if (!is_link_local_ether_addr(new_addr))
1375 			return -EINVAL;
1376 		if (new_addr[5] == 1 ||		/* 802.3x Pause address */
1377 		    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
1378 		    new_addr[5] == 3)		/* 802.1X PAE address */
1379 			return -EINVAL;
1380 		spin_lock_bh(&br->lock);
1381 		memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1382 		spin_unlock_bh(&br->lock);
1383 		br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1384 		br_recalculate_fwd_mask(br);
1385 	}
1386 
1387 	if (data[IFLA_BR_FDB_FLUSH]) {
1388 		struct net_bridge_fdb_flush_desc desc = {
1389 			.flags_mask = BIT(BR_FDB_STATIC)
1390 		};
1391 
1392 		br_fdb_flush(br, &desc);
1393 	}
1394 
1395 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1396 	if (data[IFLA_BR_MCAST_ROUTER]) {
1397 		u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1398 
1399 		err = br_multicast_set_router(&br->multicast_ctx,
1400 					      multicast_router);
1401 		if (err)
1402 			return err;
1403 	}
1404 
1405 	if (data[IFLA_BR_MCAST_SNOOPING]) {
1406 		u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1407 
1408 		err = br_multicast_toggle(br, mcast_snooping, extack);
1409 		if (err)
1410 			return err;
1411 	}
1412 
1413 	if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1414 		u8 val;
1415 
1416 		val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1417 		br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1418 	}
1419 
1420 	if (data[IFLA_BR_MCAST_QUERIER]) {
1421 		u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1422 
1423 		err = br_multicast_set_querier(&br->multicast_ctx,
1424 					       mcast_querier);
1425 		if (err)
1426 			return err;
1427 	}
1428 
1429 	if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1430 		br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1431 			RHT_ELASTICITY);
1432 
1433 	if (data[IFLA_BR_MCAST_HASH_MAX])
1434 		br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1435 
1436 	if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1437 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1438 
1439 		br->multicast_ctx.multicast_last_member_count = val;
1440 	}
1441 
1442 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1443 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1444 
1445 		br->multicast_ctx.multicast_startup_query_count = val;
1446 	}
1447 
1448 	if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1449 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1450 
1451 		br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
1452 	}
1453 
1454 	if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1455 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1456 
1457 		br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
1458 	}
1459 
1460 	if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1461 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1462 
1463 		br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
1464 	}
1465 
1466 	if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1467 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1468 
1469 		br_multicast_set_query_intvl(&br->multicast_ctx, val);
1470 	}
1471 
1472 	if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1473 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1474 
1475 		br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
1476 	}
1477 
1478 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1479 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1480 
1481 		br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
1482 	}
1483 
1484 	if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1485 		__u8 mcast_stats;
1486 
1487 		mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1488 		br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1489 	}
1490 
1491 	if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1492 		__u8 igmp_version;
1493 
1494 		igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1495 		err = br_multicast_set_igmp_version(&br->multicast_ctx,
1496 						    igmp_version);
1497 		if (err)
1498 			return err;
1499 	}
1500 
1501 #if IS_ENABLED(CONFIG_IPV6)
1502 	if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1503 		__u8 mld_version;
1504 
1505 		mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1506 		err = br_multicast_set_mld_version(&br->multicast_ctx,
1507 						   mld_version);
1508 		if (err)
1509 			return err;
1510 	}
1511 #endif
1512 #endif
1513 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1514 	if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1515 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1516 
1517 		br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1518 	}
1519 
1520 	if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1521 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1522 
1523 		br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1524 	}
1525 
1526 	if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1527 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1528 
1529 		br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1530 	}
1531 #endif
1532 
1533 	if (data[IFLA_BR_MULTI_BOOLOPT]) {
1534 		struct br_boolopt_multi *bm;
1535 
1536 		bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1537 		err = br_boolopt_multi_toggle(br, bm, extack);
1538 		if (err)
1539 			return err;
1540 	}
1541 
1542 	return 0;
1543 }
1544 
1545 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1546 			  struct nlattr *tb[], struct nlattr *data[],
1547 			  struct netlink_ext_ack *extack)
1548 {
1549 	struct net_bridge *br = netdev_priv(dev);
1550 	int err;
1551 
1552 	err = register_netdevice(dev);
1553 	if (err)
1554 		return err;
1555 
1556 	if (tb[IFLA_ADDRESS]) {
1557 		spin_lock_bh(&br->lock);
1558 		br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1559 		spin_unlock_bh(&br->lock);
1560 	}
1561 
1562 	err = br_changelink(dev, tb, data, extack);
1563 	if (err)
1564 		br_dev_delete(dev, NULL);
1565 
1566 	return err;
1567 }
1568 
1569 static size_t br_get_size(const struct net_device *brdev)
1570 {
1571 	return nla_total_size(sizeof(u32)) +	/* IFLA_BR_FORWARD_DELAY  */
1572 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_HELLO_TIME */
1573 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_MAX_AGE */
1574 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1575 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1576 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1577 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1578 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1579 	       nla_total_size(sizeof(__be16)) +	/* IFLA_BR_VLAN_PROTOCOL */
1580 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1581 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_ENABLED */
1582 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_VLAN_STATS_PER_PORT */
1583 #endif
1584 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1585 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1586 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1587 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1588 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1589 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1590 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1591 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1592 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1593 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1594 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1595 	       nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1596 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1597 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1598 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1599 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1600 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1601 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_STATS_ENABLED */
1602 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1603 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1604 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1605 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1606 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1607 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1608 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1609 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1610 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1611 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1612 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_IGMP_VERSION */
1613 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_MLD_VERSION */
1614 	       br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
1615 #endif
1616 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1617 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1618 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1619 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1620 #endif
1621 	       nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1622 	       0;
1623 }
1624 
1625 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1626 {
1627 	struct net_bridge *br = netdev_priv(brdev);
1628 	u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1629 	u32 hello_time = jiffies_to_clock_t(br->hello_time);
1630 	u32 age_time = jiffies_to_clock_t(br->max_age);
1631 	u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1632 	u32 stp_enabled = br->stp_enabled;
1633 	u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1634 	u8 vlan_enabled = br_vlan_enabled(br->dev);
1635 	struct br_boolopt_multi bm;
1636 	u64 clockval;
1637 
1638 	clockval = br_timer_value(&br->hello_timer);
1639 	if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1640 		return -EMSGSIZE;
1641 	clockval = br_timer_value(&br->tcn_timer);
1642 	if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1643 		return -EMSGSIZE;
1644 	clockval = br_timer_value(&br->topology_change_timer);
1645 	if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1646 			      IFLA_BR_PAD))
1647 		return -EMSGSIZE;
1648 	clockval = br_timer_value(&br->gc_work.timer);
1649 	if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1650 		return -EMSGSIZE;
1651 
1652 	br_boolopt_multi_get(br, &bm);
1653 	if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1654 	    nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1655 	    nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1656 	    nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1657 	    nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1658 	    nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1659 	    nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1660 	    nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1661 	    nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1662 		    &br->bridge_id) ||
1663 	    nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1664 		    &br->designated_root) ||
1665 	    nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1666 	    nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1667 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1668 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1669 		       br->topology_change_detected) ||
1670 	    nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1671 	    nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
1672 		return -EMSGSIZE;
1673 
1674 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1675 	if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1676 	    nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1677 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1678 		       br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1679 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1680 		       br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1681 		return -EMSGSIZE;
1682 #endif
1683 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1684 	if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1685 		       br->multicast_ctx.multicast_router) ||
1686 	    nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1687 		       br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1688 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1689 		       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1690 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1691 		       br->multicast_ctx.multicast_querier) ||
1692 	    nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1693 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1694 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1695 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1696 	    nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1697 			br->multicast_ctx.multicast_last_member_count) ||
1698 	    nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1699 			br->multicast_ctx.multicast_startup_query_count) ||
1700 	    nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1701 		       br->multicast_ctx.multicast_igmp_version) ||
1702 	    br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1703 					    IFLA_BR_MCAST_QUERIER_STATE))
1704 		return -EMSGSIZE;
1705 #if IS_ENABLED(CONFIG_IPV6)
1706 	if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1707 		       br->multicast_ctx.multicast_mld_version))
1708 		return -EMSGSIZE;
1709 #endif
1710 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
1711 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1712 			      IFLA_BR_PAD))
1713 		return -EMSGSIZE;
1714 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
1715 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1716 			      IFLA_BR_PAD))
1717 		return -EMSGSIZE;
1718 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
1719 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1720 			      IFLA_BR_PAD))
1721 		return -EMSGSIZE;
1722 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
1723 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1724 			      IFLA_BR_PAD))
1725 		return -EMSGSIZE;
1726 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
1727 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1728 			      IFLA_BR_PAD))
1729 		return -EMSGSIZE;
1730 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
1731 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1732 			      IFLA_BR_PAD))
1733 		return -EMSGSIZE;
1734 #endif
1735 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1736 	if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1737 		       br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1738 	    nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1739 		       br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1740 	    nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1741 		       br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1742 		return -EMSGSIZE;
1743 #endif
1744 
1745 	return 0;
1746 }
1747 
1748 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1749 {
1750 	struct net_bridge_port *p = NULL;
1751 	struct net_bridge_vlan_group *vg;
1752 	struct net_bridge_vlan *v;
1753 	struct net_bridge *br;
1754 	int numvls = 0;
1755 
1756 	switch (attr) {
1757 	case IFLA_STATS_LINK_XSTATS:
1758 		br = netdev_priv(dev);
1759 		vg = br_vlan_group(br);
1760 		break;
1761 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1762 		p = br_port_get_rtnl(dev);
1763 		if (!p)
1764 			return 0;
1765 		vg = nbp_vlan_group(p);
1766 		break;
1767 	default:
1768 		return 0;
1769 	}
1770 
1771 	if (vg) {
1772 		/* we need to count all, even placeholder entries */
1773 		list_for_each_entry(v, &vg->vlan_list, vlist)
1774 			numvls++;
1775 	}
1776 
1777 	return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1778 	       nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
1779 	       (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
1780 	       nla_total_size(0);
1781 }
1782 
1783 static int br_fill_linkxstats(struct sk_buff *skb,
1784 			      const struct net_device *dev,
1785 			      int *prividx, int attr)
1786 {
1787 	struct nlattr *nla __maybe_unused;
1788 	struct net_bridge_port *p = NULL;
1789 	struct net_bridge_vlan_group *vg;
1790 	struct net_bridge_vlan *v;
1791 	struct net_bridge *br;
1792 	struct nlattr *nest;
1793 	int vl_idx = 0;
1794 
1795 	switch (attr) {
1796 	case IFLA_STATS_LINK_XSTATS:
1797 		br = netdev_priv(dev);
1798 		vg = br_vlan_group(br);
1799 		break;
1800 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1801 		p = br_port_get_rtnl(dev);
1802 		if (!p)
1803 			return 0;
1804 		br = p->br;
1805 		vg = nbp_vlan_group(p);
1806 		break;
1807 	default:
1808 		return -EINVAL;
1809 	}
1810 
1811 	nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1812 	if (!nest)
1813 		return -EMSGSIZE;
1814 
1815 	if (vg) {
1816 		u16 pvid;
1817 
1818 		pvid = br_get_pvid(vg);
1819 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1820 			struct bridge_vlan_xstats vxi;
1821 			struct pcpu_sw_netstats stats;
1822 
1823 			if (++vl_idx < *prividx)
1824 				continue;
1825 			memset(&vxi, 0, sizeof(vxi));
1826 			vxi.vid = v->vid;
1827 			vxi.flags = v->flags;
1828 			if (v->vid == pvid)
1829 				vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1830 			br_vlan_get_stats(v, &stats);
1831 			vxi.rx_bytes = u64_stats_read(&stats.rx_bytes);
1832 			vxi.rx_packets = u64_stats_read(&stats.rx_packets);
1833 			vxi.tx_bytes = u64_stats_read(&stats.tx_bytes);
1834 			vxi.tx_packets = u64_stats_read(&stats.tx_packets);
1835 
1836 			if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1837 				goto nla_put_failure;
1838 		}
1839 	}
1840 
1841 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1842 	if (++vl_idx >= *prividx) {
1843 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1844 					sizeof(struct br_mcast_stats),
1845 					BRIDGE_XSTATS_PAD);
1846 		if (!nla)
1847 			goto nla_put_failure;
1848 		br_multicast_get_stats(br, p, nla_data(nla));
1849 	}
1850 #endif
1851 
1852 	if (p) {
1853 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1854 					sizeof(p->stp_xstats),
1855 					BRIDGE_XSTATS_PAD);
1856 		if (!nla)
1857 			goto nla_put_failure;
1858 
1859 		spin_lock_bh(&br->lock);
1860 		memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1861 		spin_unlock_bh(&br->lock);
1862 	}
1863 
1864 	nla_nest_end(skb, nest);
1865 	*prividx = 0;
1866 
1867 	return 0;
1868 
1869 nla_put_failure:
1870 	nla_nest_end(skb, nest);
1871 	*prividx = vl_idx;
1872 
1873 	return -EMSGSIZE;
1874 }
1875 
1876 static struct rtnl_af_ops br_af_ops __read_mostly = {
1877 	.family			= AF_BRIDGE,
1878 	.get_link_af_size	= br_get_link_af_size_filtered,
1879 };
1880 
1881 struct rtnl_link_ops br_link_ops __read_mostly = {
1882 	.kind			= "bridge",
1883 	.priv_size		= sizeof(struct net_bridge),
1884 	.setup			= br_dev_setup,
1885 	.maxtype		= IFLA_BR_MAX,
1886 	.policy			= br_policy,
1887 	.validate		= br_validate,
1888 	.newlink		= br_dev_newlink,
1889 	.changelink		= br_changelink,
1890 	.dellink		= br_dev_delete,
1891 	.get_size		= br_get_size,
1892 	.fill_info		= br_fill_info,
1893 	.fill_linkxstats	= br_fill_linkxstats,
1894 	.get_linkxstats_size	= br_get_linkxstats_size,
1895 
1896 	.slave_maxtype		= IFLA_BRPORT_MAX,
1897 	.slave_policy		= br_port_policy,
1898 	.slave_changelink	= br_port_slave_changelink,
1899 	.get_slave_size		= br_port_get_slave_size,
1900 	.fill_slave_info	= br_port_fill_slave_info,
1901 };
1902 
1903 int __init br_netlink_init(void)
1904 {
1905 	int err;
1906 
1907 	br_vlan_rtnl_init();
1908 	rtnl_af_register(&br_af_ops);
1909 
1910 	err = rtnl_link_register(&br_link_ops);
1911 	if (err)
1912 		goto out_af;
1913 
1914 	return 0;
1915 
1916 out_af:
1917 	rtnl_af_unregister(&br_af_ops);
1918 	return err;
1919 }
1920 
1921 void br_netlink_fini(void)
1922 {
1923 	br_vlan_rtnl_uninit();
1924 	rtnl_af_unregister(&br_af_ops);
1925 	rtnl_link_unregister(&br_link_ops);
1926 }
1927