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