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