xref: /openbmc/linux/net/bridge/br_netlink.c (revision 77423a62)
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 	if (af) {
593 		if (nlmsg_get_pos(skb) - (void *)af > nla_attr_size(0))
594 			nla_nest_end(skb, af);
595 		else
596 			nla_nest_cancel(skb, af);
597 	}
598 
599 	nlmsg_end(skb, nlh);
600 	return 0;
601 
602 nla_put_failure:
603 	nlmsg_cancel(skb, nlh);
604 	return -EMSGSIZE;
605 }
606 
607 void br_info_notify(int event, const struct net_bridge *br,
608 		    const struct net_bridge_port *port, u32 filter)
609 {
610 	struct net_device *dev;
611 	struct sk_buff *skb;
612 	int err = -ENOBUFS;
613 	struct net *net;
614 	u16 port_no = 0;
615 
616 	if (WARN_ON(!port && !br))
617 		return;
618 
619 	if (port) {
620 		dev = port->dev;
621 		br = port->br;
622 		port_no = port->port_no;
623 	} else {
624 		dev = br->dev;
625 	}
626 
627 	net = dev_net(dev);
628 	br_debug(br, "port %u(%s) event %d\n", port_no, dev->name, event);
629 
630 	skb = nlmsg_new(br_nlmsg_size(dev, filter), GFP_ATOMIC);
631 	if (skb == NULL)
632 		goto errout;
633 
634 	err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, dev, false);
635 	if (err < 0) {
636 		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
637 		WARN_ON(err == -EMSGSIZE);
638 		kfree_skb(skb);
639 		goto errout;
640 	}
641 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
642 	return;
643 errout:
644 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
645 }
646 
647 /* Notify listeners of a change in bridge or port information */
648 void br_ifinfo_notify(int event, const struct net_bridge *br,
649 		      const struct net_bridge_port *port)
650 {
651 	u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
652 
653 	return br_info_notify(event, br, port, filter);
654 }
655 
656 /*
657  * Dump information about all ports, in response to GETLINK
658  */
659 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
660 	       struct net_device *dev, u32 filter_mask, int nlflags)
661 {
662 	struct net_bridge_port *port = br_port_get_rtnl(dev);
663 
664 	if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
665 	    !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED) &&
666 	    !(filter_mask & RTEXT_FILTER_MRP) &&
667 	    !(filter_mask & RTEXT_FILTER_CFM_CONFIG) &&
668 	    !(filter_mask & RTEXT_FILTER_CFM_STATUS))
669 		return 0;
670 
671 	return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
672 			      filter_mask, dev, true);
673 }
674 
675 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
676 			int cmd, struct bridge_vlan_info *vinfo, bool *changed,
677 			struct netlink_ext_ack *extack)
678 {
679 	bool curr_change;
680 	int err = 0;
681 
682 	switch (cmd) {
683 	case RTM_SETLINK:
684 		if (p) {
685 			/* if the MASTER flag is set this will act on the global
686 			 * per-VLAN entry as well
687 			 */
688 			err = nbp_vlan_add(p, vinfo->vid, vinfo->flags,
689 					   &curr_change, extack);
690 		} else {
691 			vinfo->flags |= BRIDGE_VLAN_INFO_BRENTRY;
692 			err = br_vlan_add(br, vinfo->vid, vinfo->flags,
693 					  &curr_change, extack);
694 		}
695 		if (curr_change)
696 			*changed = true;
697 		break;
698 
699 	case RTM_DELLINK:
700 		if (p) {
701 			if (!nbp_vlan_delete(p, vinfo->vid))
702 				*changed = true;
703 
704 			if ((vinfo->flags & BRIDGE_VLAN_INFO_MASTER) &&
705 			    !br_vlan_delete(p->br, vinfo->vid))
706 				*changed = true;
707 		} else if (!br_vlan_delete(br, vinfo->vid)) {
708 			*changed = true;
709 		}
710 		break;
711 	}
712 
713 	return err;
714 }
715 
716 int br_process_vlan_info(struct net_bridge *br,
717 			 struct net_bridge_port *p, int cmd,
718 			 struct bridge_vlan_info *vinfo_curr,
719 			 struct bridge_vlan_info **vinfo_last,
720 			 bool *changed,
721 			 struct netlink_ext_ack *extack)
722 {
723 	int err, rtm_cmd;
724 
725 	if (!br_vlan_valid_id(vinfo_curr->vid, extack))
726 		return -EINVAL;
727 
728 	/* needed for vlan-only NEWVLAN/DELVLAN notifications */
729 	rtm_cmd = br_afspec_cmd_to_rtm(cmd);
730 
731 	if (vinfo_curr->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
732 		if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
733 			return -EINVAL;
734 		*vinfo_last = vinfo_curr;
735 		return 0;
736 	}
737 
738 	if (*vinfo_last) {
739 		struct bridge_vlan_info tmp_vinfo;
740 		int v, v_change_start = 0;
741 
742 		if (!br_vlan_valid_range(vinfo_curr, *vinfo_last, extack))
743 			return -EINVAL;
744 
745 		memcpy(&tmp_vinfo, *vinfo_last,
746 		       sizeof(struct bridge_vlan_info));
747 		for (v = (*vinfo_last)->vid; v <= vinfo_curr->vid; v++) {
748 			bool curr_change = false;
749 
750 			tmp_vinfo.vid = v;
751 			err = br_vlan_info(br, p, cmd, &tmp_vinfo, &curr_change,
752 					   extack);
753 			if (err)
754 				break;
755 			if (curr_change) {
756 				*changed = curr_change;
757 				if (!v_change_start)
758 					v_change_start = v;
759 			} else {
760 				/* nothing to notify yet */
761 				if (!v_change_start)
762 					continue;
763 				br_vlan_notify(br, p, v_change_start,
764 					       v - 1, rtm_cmd);
765 				v_change_start = 0;
766 			}
767 			cond_resched();
768 		}
769 		/* v_change_start is set only if the last/whole range changed */
770 		if (v_change_start)
771 			br_vlan_notify(br, p, v_change_start,
772 				       v - 1, rtm_cmd);
773 
774 		*vinfo_last = NULL;
775 
776 		return err;
777 	}
778 
779 	err = br_vlan_info(br, p, cmd, vinfo_curr, changed, extack);
780 	if (*changed)
781 		br_vlan_notify(br, p, vinfo_curr->vid, 0, rtm_cmd);
782 
783 	return err;
784 }
785 
786 static int br_afspec(struct net_bridge *br,
787 		     struct net_bridge_port *p,
788 		     struct nlattr *af_spec,
789 		     int cmd, bool *changed,
790 		     struct netlink_ext_ack *extack)
791 {
792 	struct bridge_vlan_info *vinfo_curr = NULL;
793 	struct bridge_vlan_info *vinfo_last = NULL;
794 	struct nlattr *attr;
795 	struct vtunnel_info tinfo_last = {};
796 	struct vtunnel_info tinfo_curr = {};
797 	int err = 0, rem;
798 
799 	nla_for_each_nested(attr, af_spec, rem) {
800 		err = 0;
801 		switch (nla_type(attr)) {
802 		case IFLA_BRIDGE_VLAN_TUNNEL_INFO:
803 			if (!p || !(p->flags & BR_VLAN_TUNNEL))
804 				return -EINVAL;
805 			err = br_parse_vlan_tunnel_info(attr, &tinfo_curr);
806 			if (err)
807 				return err;
808 			err = br_process_vlan_tunnel_info(br, p, cmd,
809 							  &tinfo_curr,
810 							  &tinfo_last,
811 							  changed);
812 			if (err)
813 				return err;
814 			break;
815 		case IFLA_BRIDGE_VLAN_INFO:
816 			if (nla_len(attr) != sizeof(struct bridge_vlan_info))
817 				return -EINVAL;
818 			vinfo_curr = nla_data(attr);
819 			err = br_process_vlan_info(br, p, cmd, vinfo_curr,
820 						   &vinfo_last, changed,
821 						   extack);
822 			if (err)
823 				return err;
824 			break;
825 		case IFLA_BRIDGE_MRP:
826 			err = br_mrp_parse(br, p, attr, cmd, extack);
827 			if (err)
828 				return err;
829 			break;
830 		case IFLA_BRIDGE_CFM:
831 			err = br_cfm_parse(br, p, attr, cmd, extack);
832 			if (err)
833 				return err;
834 			break;
835 		case IFLA_BRIDGE_MST:
836 			if (!p) {
837 				NL_SET_ERR_MSG(extack,
838 					       "MST states can only be set on bridge ports");
839 				return -EINVAL;
840 			}
841 
842 			if (cmd != RTM_SETLINK) {
843 				NL_SET_ERR_MSG(extack,
844 					       "MST states can only be set through RTM_SETLINK");
845 				return -EINVAL;
846 			}
847 
848 			err = br_mst_process(p, attr, extack);
849 			if (err)
850 				return err;
851 			break;
852 		}
853 	}
854 
855 	return err;
856 }
857 
858 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
859 	[IFLA_BRPORT_STATE]	= { .type = NLA_U8 },
860 	[IFLA_BRPORT_COST]	= { .type = NLA_U32 },
861 	[IFLA_BRPORT_PRIORITY]	= { .type = NLA_U16 },
862 	[IFLA_BRPORT_MODE]	= { .type = NLA_U8 },
863 	[IFLA_BRPORT_GUARD]	= { .type = NLA_U8 },
864 	[IFLA_BRPORT_PROTECT]	= { .type = NLA_U8 },
865 	[IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
866 	[IFLA_BRPORT_LEARNING]	= { .type = NLA_U8 },
867 	[IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
868 	[IFLA_BRPORT_PROXYARP]	= { .type = NLA_U8 },
869 	[IFLA_BRPORT_PROXYARP_WIFI] = { .type = NLA_U8 },
870 	[IFLA_BRPORT_MULTICAST_ROUTER] = { .type = NLA_U8 },
871 	[IFLA_BRPORT_MCAST_TO_UCAST] = { .type = NLA_U8 },
872 	[IFLA_BRPORT_MCAST_FLOOD] = { .type = NLA_U8 },
873 	[IFLA_BRPORT_BCAST_FLOOD] = { .type = NLA_U8 },
874 	[IFLA_BRPORT_VLAN_TUNNEL] = { .type = NLA_U8 },
875 	[IFLA_BRPORT_GROUP_FWD_MASK] = { .type = NLA_U16 },
876 	[IFLA_BRPORT_NEIGH_SUPPRESS] = { .type = NLA_U8 },
877 	[IFLA_BRPORT_ISOLATED]	= { .type = NLA_U8 },
878 	[IFLA_BRPORT_LOCKED] = { .type = NLA_U8 },
879 	[IFLA_BRPORT_BACKUP_PORT] = { .type = NLA_U32 },
880 	[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT] = { .type = NLA_U32 },
881 };
882 
883 /* Change the state of the port and notify spanning tree */
884 static int br_set_port_state(struct net_bridge_port *p, u8 state)
885 {
886 	if (state > BR_STATE_BLOCKING)
887 		return -EINVAL;
888 
889 	/* if kernel STP is running, don't allow changes */
890 	if (p->br->stp_enabled == BR_KERNEL_STP)
891 		return -EBUSY;
892 
893 	/* if device is not up, change is not allowed
894 	 * if link is not present, only allowable state is disabled
895 	 */
896 	if (!netif_running(p->dev) ||
897 	    (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
898 		return -ENETDOWN;
899 
900 	br_set_state(p, state);
901 	br_port_state_selection(p->br);
902 	return 0;
903 }
904 
905 /* Set/clear or port flags based on attribute */
906 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
907 			     int attrtype, unsigned long mask)
908 {
909 	if (!tb[attrtype])
910 		return;
911 
912 	if (nla_get_u8(tb[attrtype]))
913 		p->flags |= mask;
914 	else
915 		p->flags &= ~mask;
916 }
917 
918 /* Process bridge protocol info on port */
919 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[],
920 		      struct netlink_ext_ack *extack)
921 {
922 	unsigned long old_flags, changed_mask;
923 	bool br_vlan_tunnel_old;
924 	int err;
925 
926 	old_flags = p->flags;
927 	br_vlan_tunnel_old = (old_flags & BR_VLAN_TUNNEL) ? true : false;
928 
929 	br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
930 	br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
931 	br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE,
932 			 BR_MULTICAST_FAST_LEAVE);
933 	br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
934 	br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
935 	br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
936 	br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD);
937 	br_set_port_flag(p, tb, IFLA_BRPORT_MCAST_TO_UCAST,
938 			 BR_MULTICAST_TO_UNICAST);
939 	br_set_port_flag(p, tb, IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD);
940 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
941 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
942 	br_set_port_flag(p, tb, IFLA_BRPORT_VLAN_TUNNEL, BR_VLAN_TUNNEL);
943 	br_set_port_flag(p, tb, IFLA_BRPORT_NEIGH_SUPPRESS, BR_NEIGH_SUPPRESS);
944 	br_set_port_flag(p, tb, IFLA_BRPORT_ISOLATED, BR_ISOLATED);
945 	br_set_port_flag(p, tb, IFLA_BRPORT_LOCKED, BR_PORT_LOCKED);
946 
947 	changed_mask = old_flags ^ p->flags;
948 
949 	err = br_switchdev_set_port_flag(p, p->flags, changed_mask, extack);
950 	if (err) {
951 		p->flags = old_flags;
952 		return err;
953 	}
954 
955 	if (br_vlan_tunnel_old && !(p->flags & BR_VLAN_TUNNEL))
956 		nbp_vlan_tunnel_info_flush(p);
957 
958 	br_port_flags_change(p, changed_mask);
959 
960 	if (tb[IFLA_BRPORT_COST]) {
961 		err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
962 		if (err)
963 			return err;
964 	}
965 
966 	if (tb[IFLA_BRPORT_PRIORITY]) {
967 		err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
968 		if (err)
969 			return err;
970 	}
971 
972 	if (tb[IFLA_BRPORT_STATE]) {
973 		err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
974 		if (err)
975 			return err;
976 	}
977 
978 	if (tb[IFLA_BRPORT_FLUSH])
979 		br_fdb_delete_by_port(p->br, p, 0, 0);
980 
981 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
982 	if (tb[IFLA_BRPORT_MULTICAST_ROUTER]) {
983 		u8 mcast_router = nla_get_u8(tb[IFLA_BRPORT_MULTICAST_ROUTER]);
984 
985 		err = br_multicast_set_port_router(&p->multicast_ctx,
986 						   mcast_router);
987 		if (err)
988 			return err;
989 	}
990 
991 	if (tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]) {
992 		u32 hlimit;
993 
994 		hlimit = nla_get_u32(tb[IFLA_BRPORT_MCAST_EHT_HOSTS_LIMIT]);
995 		err = br_multicast_eht_set_hosts_limit(p, hlimit);
996 		if (err)
997 			return err;
998 	}
999 #endif
1000 
1001 	if (tb[IFLA_BRPORT_GROUP_FWD_MASK]) {
1002 		u16 fwd_mask = nla_get_u16(tb[IFLA_BRPORT_GROUP_FWD_MASK]);
1003 
1004 		if (fwd_mask & BR_GROUPFWD_MACPAUSE)
1005 			return -EINVAL;
1006 		p->group_fwd_mask = fwd_mask;
1007 	}
1008 
1009 	if (tb[IFLA_BRPORT_BACKUP_PORT]) {
1010 		struct net_device *backup_dev = NULL;
1011 		u32 backup_ifindex;
1012 
1013 		backup_ifindex = nla_get_u32(tb[IFLA_BRPORT_BACKUP_PORT]);
1014 		if (backup_ifindex) {
1015 			backup_dev = __dev_get_by_index(dev_net(p->dev),
1016 							backup_ifindex);
1017 			if (!backup_dev)
1018 				return -ENOENT;
1019 		}
1020 
1021 		err = nbp_backup_change(p, backup_dev);
1022 		if (err)
1023 			return err;
1024 	}
1025 
1026 	return 0;
1027 }
1028 
1029 /* Change state and parameters on port. */
1030 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags,
1031 	       struct netlink_ext_ack *extack)
1032 {
1033 	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1034 	struct nlattr *tb[IFLA_BRPORT_MAX + 1];
1035 	struct net_bridge_port *p;
1036 	struct nlattr *protinfo;
1037 	struct nlattr *afspec;
1038 	bool changed = false;
1039 	int err = 0;
1040 
1041 	protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
1042 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1043 	if (!protinfo && !afspec)
1044 		return 0;
1045 
1046 	p = br_port_get_rtnl(dev);
1047 	/* We want to accept dev as bridge itself if the AF_SPEC
1048 	 * is set to see if someone is setting vlan info on the bridge
1049 	 */
1050 	if (!p && !afspec)
1051 		return -EINVAL;
1052 
1053 	if (p && protinfo) {
1054 		if (protinfo->nla_type & NLA_F_NESTED) {
1055 			err = nla_parse_nested_deprecated(tb, IFLA_BRPORT_MAX,
1056 							  protinfo,
1057 							  br_port_policy,
1058 							  NULL);
1059 			if (err)
1060 				return err;
1061 
1062 			spin_lock_bh(&p->br->lock);
1063 			err = br_setport(p, tb, extack);
1064 			spin_unlock_bh(&p->br->lock);
1065 		} else {
1066 			/* Binary compatibility with old RSTP */
1067 			if (nla_len(protinfo) < sizeof(u8))
1068 				return -EINVAL;
1069 
1070 			spin_lock_bh(&p->br->lock);
1071 			err = br_set_port_state(p, nla_get_u8(protinfo));
1072 			spin_unlock_bh(&p->br->lock);
1073 		}
1074 		if (err)
1075 			goto out;
1076 		changed = true;
1077 	}
1078 
1079 	if (afspec)
1080 		err = br_afspec(br, p, afspec, RTM_SETLINK, &changed, extack);
1081 
1082 	if (changed)
1083 		br_ifinfo_notify(RTM_NEWLINK, br, p);
1084 out:
1085 	return err;
1086 }
1087 
1088 /* Delete port information */
1089 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
1090 {
1091 	struct net_bridge *br = (struct net_bridge *)netdev_priv(dev);
1092 	struct net_bridge_port *p;
1093 	struct nlattr *afspec;
1094 	bool changed = false;
1095 	int err = 0;
1096 
1097 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
1098 	if (!afspec)
1099 		return 0;
1100 
1101 	p = br_port_get_rtnl(dev);
1102 	/* We want to accept dev as bridge itself as well */
1103 	if (!p && !netif_is_bridge_master(dev))
1104 		return -EINVAL;
1105 
1106 	err = br_afspec(br, p, afspec, RTM_DELLINK, &changed, NULL);
1107 	if (changed)
1108 		/* Send RTM_NEWLINK because userspace
1109 		 * expects RTM_NEWLINK for vlan dels
1110 		 */
1111 		br_ifinfo_notify(RTM_NEWLINK, br, p);
1112 
1113 	return err;
1114 }
1115 
1116 static int br_validate(struct nlattr *tb[], struct nlattr *data[],
1117 		       struct netlink_ext_ack *extack)
1118 {
1119 	if (tb[IFLA_ADDRESS]) {
1120 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1121 			return -EINVAL;
1122 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1123 			return -EADDRNOTAVAIL;
1124 	}
1125 
1126 	if (!data)
1127 		return 0;
1128 
1129 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1130 	if (data[IFLA_BR_VLAN_PROTOCOL] &&
1131 	    !eth_type_vlan(nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL])))
1132 		return -EPROTONOSUPPORT;
1133 
1134 	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1135 		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1136 
1137 		if (defpvid >= VLAN_VID_MASK)
1138 			return -EINVAL;
1139 	}
1140 #endif
1141 
1142 	return 0;
1143 }
1144 
1145 static int br_port_slave_changelink(struct net_device *brdev,
1146 				    struct net_device *dev,
1147 				    struct nlattr *tb[],
1148 				    struct nlattr *data[],
1149 				    struct netlink_ext_ack *extack)
1150 {
1151 	struct net_bridge *br = netdev_priv(brdev);
1152 	int ret;
1153 
1154 	if (!data)
1155 		return 0;
1156 
1157 	spin_lock_bh(&br->lock);
1158 	ret = br_setport(br_port_get_rtnl(dev), data, extack);
1159 	spin_unlock_bh(&br->lock);
1160 
1161 	return ret;
1162 }
1163 
1164 static int br_port_fill_slave_info(struct sk_buff *skb,
1165 				   const struct net_device *brdev,
1166 				   const struct net_device *dev)
1167 {
1168 	return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
1169 }
1170 
1171 static size_t br_port_get_slave_size(const struct net_device *brdev,
1172 				     const struct net_device *dev)
1173 {
1174 	return br_port_info_size();
1175 }
1176 
1177 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
1178 	[IFLA_BR_FORWARD_DELAY]	= { .type = NLA_U32 },
1179 	[IFLA_BR_HELLO_TIME]	= { .type = NLA_U32 },
1180 	[IFLA_BR_MAX_AGE]	= { .type = NLA_U32 },
1181 	[IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
1182 	[IFLA_BR_STP_STATE] = { .type = NLA_U32 },
1183 	[IFLA_BR_PRIORITY] = { .type = NLA_U16 },
1184 	[IFLA_BR_VLAN_FILTERING] = { .type = NLA_U8 },
1185 	[IFLA_BR_VLAN_PROTOCOL] = { .type = NLA_U16 },
1186 	[IFLA_BR_GROUP_FWD_MASK] = { .type = NLA_U16 },
1187 	[IFLA_BR_GROUP_ADDR] = { .type = NLA_BINARY,
1188 				 .len  = ETH_ALEN },
1189 	[IFLA_BR_MCAST_ROUTER] = { .type = NLA_U8 },
1190 	[IFLA_BR_MCAST_SNOOPING] = { .type = NLA_U8 },
1191 	[IFLA_BR_MCAST_QUERY_USE_IFADDR] = { .type = NLA_U8 },
1192 	[IFLA_BR_MCAST_QUERIER] = { .type = NLA_U8 },
1193 	[IFLA_BR_MCAST_HASH_ELASTICITY] = { .type = NLA_U32 },
1194 	[IFLA_BR_MCAST_HASH_MAX] = { .type = NLA_U32 },
1195 	[IFLA_BR_MCAST_LAST_MEMBER_CNT] = { .type = NLA_U32 },
1196 	[IFLA_BR_MCAST_STARTUP_QUERY_CNT] = { .type = NLA_U32 },
1197 	[IFLA_BR_MCAST_LAST_MEMBER_INTVL] = { .type = NLA_U64 },
1198 	[IFLA_BR_MCAST_MEMBERSHIP_INTVL] = { .type = NLA_U64 },
1199 	[IFLA_BR_MCAST_QUERIER_INTVL] = { .type = NLA_U64 },
1200 	[IFLA_BR_MCAST_QUERY_INTVL] = { .type = NLA_U64 },
1201 	[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL] = { .type = NLA_U64 },
1202 	[IFLA_BR_MCAST_STARTUP_QUERY_INTVL] = { .type = NLA_U64 },
1203 	[IFLA_BR_NF_CALL_IPTABLES] = { .type = NLA_U8 },
1204 	[IFLA_BR_NF_CALL_IP6TABLES] = { .type = NLA_U8 },
1205 	[IFLA_BR_NF_CALL_ARPTABLES] = { .type = NLA_U8 },
1206 	[IFLA_BR_VLAN_DEFAULT_PVID] = { .type = NLA_U16 },
1207 	[IFLA_BR_VLAN_STATS_ENABLED] = { .type = NLA_U8 },
1208 	[IFLA_BR_MCAST_STATS_ENABLED] = { .type = NLA_U8 },
1209 	[IFLA_BR_MCAST_IGMP_VERSION] = { .type = NLA_U8 },
1210 	[IFLA_BR_MCAST_MLD_VERSION] = { .type = NLA_U8 },
1211 	[IFLA_BR_VLAN_STATS_PER_PORT] = { .type = NLA_U8 },
1212 	[IFLA_BR_MULTI_BOOLOPT] =
1213 		NLA_POLICY_EXACT_LEN(sizeof(struct br_boolopt_multi)),
1214 };
1215 
1216 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
1217 			 struct nlattr *data[],
1218 			 struct netlink_ext_ack *extack)
1219 {
1220 	struct net_bridge *br = netdev_priv(brdev);
1221 	int err;
1222 
1223 	if (!data)
1224 		return 0;
1225 
1226 	if (data[IFLA_BR_FORWARD_DELAY]) {
1227 		err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
1228 		if (err)
1229 			return err;
1230 	}
1231 
1232 	if (data[IFLA_BR_HELLO_TIME]) {
1233 		err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
1234 		if (err)
1235 			return err;
1236 	}
1237 
1238 	if (data[IFLA_BR_MAX_AGE]) {
1239 		err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
1240 		if (err)
1241 			return err;
1242 	}
1243 
1244 	if (data[IFLA_BR_AGEING_TIME]) {
1245 		err = br_set_ageing_time(br, nla_get_u32(data[IFLA_BR_AGEING_TIME]));
1246 		if (err)
1247 			return err;
1248 	}
1249 
1250 	if (data[IFLA_BR_STP_STATE]) {
1251 		u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
1252 
1253 		err = br_stp_set_enabled(br, stp_enabled, extack);
1254 		if (err)
1255 			return err;
1256 	}
1257 
1258 	if (data[IFLA_BR_PRIORITY]) {
1259 		u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
1260 
1261 		br_stp_set_bridge_priority(br, priority);
1262 	}
1263 
1264 	if (data[IFLA_BR_VLAN_FILTERING]) {
1265 		u8 vlan_filter = nla_get_u8(data[IFLA_BR_VLAN_FILTERING]);
1266 
1267 		err = br_vlan_filter_toggle(br, vlan_filter, extack);
1268 		if (err)
1269 			return err;
1270 	}
1271 
1272 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1273 	if (data[IFLA_BR_VLAN_PROTOCOL]) {
1274 		__be16 vlan_proto = nla_get_be16(data[IFLA_BR_VLAN_PROTOCOL]);
1275 
1276 		err = __br_vlan_set_proto(br, vlan_proto, extack);
1277 		if (err)
1278 			return err;
1279 	}
1280 
1281 	if (data[IFLA_BR_VLAN_DEFAULT_PVID]) {
1282 		__u16 defpvid = nla_get_u16(data[IFLA_BR_VLAN_DEFAULT_PVID]);
1283 
1284 		err = __br_vlan_set_default_pvid(br, defpvid, extack);
1285 		if (err)
1286 			return err;
1287 	}
1288 
1289 	if (data[IFLA_BR_VLAN_STATS_ENABLED]) {
1290 		__u8 vlan_stats = nla_get_u8(data[IFLA_BR_VLAN_STATS_ENABLED]);
1291 
1292 		err = br_vlan_set_stats(br, vlan_stats);
1293 		if (err)
1294 			return err;
1295 	}
1296 
1297 	if (data[IFLA_BR_VLAN_STATS_PER_PORT]) {
1298 		__u8 per_port = nla_get_u8(data[IFLA_BR_VLAN_STATS_PER_PORT]);
1299 
1300 		err = br_vlan_set_stats_per_port(br, per_port);
1301 		if (err)
1302 			return err;
1303 	}
1304 #endif
1305 
1306 	if (data[IFLA_BR_GROUP_FWD_MASK]) {
1307 		u16 fwd_mask = nla_get_u16(data[IFLA_BR_GROUP_FWD_MASK]);
1308 
1309 		if (fwd_mask & BR_GROUPFWD_RESTRICTED)
1310 			return -EINVAL;
1311 		br->group_fwd_mask = fwd_mask;
1312 	}
1313 
1314 	if (data[IFLA_BR_GROUP_ADDR]) {
1315 		u8 new_addr[ETH_ALEN];
1316 
1317 		if (nla_len(data[IFLA_BR_GROUP_ADDR]) != ETH_ALEN)
1318 			return -EINVAL;
1319 		memcpy(new_addr, nla_data(data[IFLA_BR_GROUP_ADDR]), ETH_ALEN);
1320 		if (!is_link_local_ether_addr(new_addr))
1321 			return -EINVAL;
1322 		if (new_addr[5] == 1 ||		/* 802.3x Pause address */
1323 		    new_addr[5] == 2 ||		/* 802.3ad Slow protocols */
1324 		    new_addr[5] == 3)		/* 802.1X PAE address */
1325 			return -EINVAL;
1326 		spin_lock_bh(&br->lock);
1327 		memcpy(br->group_addr, new_addr, sizeof(br->group_addr));
1328 		spin_unlock_bh(&br->lock);
1329 		br_opt_toggle(br, BROPT_GROUP_ADDR_SET, true);
1330 		br_recalculate_fwd_mask(br);
1331 	}
1332 
1333 	if (data[IFLA_BR_FDB_FLUSH]) {
1334 		struct net_bridge_fdb_flush_desc desc = {
1335 			.flags_mask = BR_FDB_STATIC
1336 		};
1337 
1338 		br_fdb_flush(br, &desc);
1339 	}
1340 
1341 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1342 	if (data[IFLA_BR_MCAST_ROUTER]) {
1343 		u8 multicast_router = nla_get_u8(data[IFLA_BR_MCAST_ROUTER]);
1344 
1345 		err = br_multicast_set_router(&br->multicast_ctx,
1346 					      multicast_router);
1347 		if (err)
1348 			return err;
1349 	}
1350 
1351 	if (data[IFLA_BR_MCAST_SNOOPING]) {
1352 		u8 mcast_snooping = nla_get_u8(data[IFLA_BR_MCAST_SNOOPING]);
1353 
1354 		err = br_multicast_toggle(br, mcast_snooping, extack);
1355 		if (err)
1356 			return err;
1357 	}
1358 
1359 	if (data[IFLA_BR_MCAST_QUERY_USE_IFADDR]) {
1360 		u8 val;
1361 
1362 		val = nla_get_u8(data[IFLA_BR_MCAST_QUERY_USE_IFADDR]);
1363 		br_opt_toggle(br, BROPT_MULTICAST_QUERY_USE_IFADDR, !!val);
1364 	}
1365 
1366 	if (data[IFLA_BR_MCAST_QUERIER]) {
1367 		u8 mcast_querier = nla_get_u8(data[IFLA_BR_MCAST_QUERIER]);
1368 
1369 		err = br_multicast_set_querier(&br->multicast_ctx,
1370 					       mcast_querier);
1371 		if (err)
1372 			return err;
1373 	}
1374 
1375 	if (data[IFLA_BR_MCAST_HASH_ELASTICITY])
1376 		br_warn(br, "the hash_elasticity option has been deprecated and is always %u\n",
1377 			RHT_ELASTICITY);
1378 
1379 	if (data[IFLA_BR_MCAST_HASH_MAX])
1380 		br->hash_max = nla_get_u32(data[IFLA_BR_MCAST_HASH_MAX]);
1381 
1382 	if (data[IFLA_BR_MCAST_LAST_MEMBER_CNT]) {
1383 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_LAST_MEMBER_CNT]);
1384 
1385 		br->multicast_ctx.multicast_last_member_count = val;
1386 	}
1387 
1388 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]) {
1389 		u32 val = nla_get_u32(data[IFLA_BR_MCAST_STARTUP_QUERY_CNT]);
1390 
1391 		br->multicast_ctx.multicast_startup_query_count = val;
1392 	}
1393 
1394 	if (data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]) {
1395 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_LAST_MEMBER_INTVL]);
1396 
1397 		br->multicast_ctx.multicast_last_member_interval = clock_t_to_jiffies(val);
1398 	}
1399 
1400 	if (data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]) {
1401 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_MEMBERSHIP_INTVL]);
1402 
1403 		br->multicast_ctx.multicast_membership_interval = clock_t_to_jiffies(val);
1404 	}
1405 
1406 	if (data[IFLA_BR_MCAST_QUERIER_INTVL]) {
1407 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERIER_INTVL]);
1408 
1409 		br->multicast_ctx.multicast_querier_interval = clock_t_to_jiffies(val);
1410 	}
1411 
1412 	if (data[IFLA_BR_MCAST_QUERY_INTVL]) {
1413 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_INTVL]);
1414 
1415 		br_multicast_set_query_intvl(&br->multicast_ctx, val);
1416 	}
1417 
1418 	if (data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]) {
1419 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_QUERY_RESPONSE_INTVL]);
1420 
1421 		br->multicast_ctx.multicast_query_response_interval = clock_t_to_jiffies(val);
1422 	}
1423 
1424 	if (data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]) {
1425 		u64 val = nla_get_u64(data[IFLA_BR_MCAST_STARTUP_QUERY_INTVL]);
1426 
1427 		br_multicast_set_startup_query_intvl(&br->multicast_ctx, val);
1428 	}
1429 
1430 	if (data[IFLA_BR_MCAST_STATS_ENABLED]) {
1431 		__u8 mcast_stats;
1432 
1433 		mcast_stats = nla_get_u8(data[IFLA_BR_MCAST_STATS_ENABLED]);
1434 		br_opt_toggle(br, BROPT_MULTICAST_STATS_ENABLED, !!mcast_stats);
1435 	}
1436 
1437 	if (data[IFLA_BR_MCAST_IGMP_VERSION]) {
1438 		__u8 igmp_version;
1439 
1440 		igmp_version = nla_get_u8(data[IFLA_BR_MCAST_IGMP_VERSION]);
1441 		err = br_multicast_set_igmp_version(&br->multicast_ctx,
1442 						    igmp_version);
1443 		if (err)
1444 			return err;
1445 	}
1446 
1447 #if IS_ENABLED(CONFIG_IPV6)
1448 	if (data[IFLA_BR_MCAST_MLD_VERSION]) {
1449 		__u8 mld_version;
1450 
1451 		mld_version = nla_get_u8(data[IFLA_BR_MCAST_MLD_VERSION]);
1452 		err = br_multicast_set_mld_version(&br->multicast_ctx,
1453 						   mld_version);
1454 		if (err)
1455 			return err;
1456 	}
1457 #endif
1458 #endif
1459 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1460 	if (data[IFLA_BR_NF_CALL_IPTABLES]) {
1461 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IPTABLES]);
1462 
1463 		br_opt_toggle(br, BROPT_NF_CALL_IPTABLES, !!val);
1464 	}
1465 
1466 	if (data[IFLA_BR_NF_CALL_IP6TABLES]) {
1467 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_IP6TABLES]);
1468 
1469 		br_opt_toggle(br, BROPT_NF_CALL_IP6TABLES, !!val);
1470 	}
1471 
1472 	if (data[IFLA_BR_NF_CALL_ARPTABLES]) {
1473 		u8 val = nla_get_u8(data[IFLA_BR_NF_CALL_ARPTABLES]);
1474 
1475 		br_opt_toggle(br, BROPT_NF_CALL_ARPTABLES, !!val);
1476 	}
1477 #endif
1478 
1479 	if (data[IFLA_BR_MULTI_BOOLOPT]) {
1480 		struct br_boolopt_multi *bm;
1481 
1482 		bm = nla_data(data[IFLA_BR_MULTI_BOOLOPT]);
1483 		err = br_boolopt_multi_toggle(br, bm, extack);
1484 		if (err)
1485 			return err;
1486 	}
1487 
1488 	return 0;
1489 }
1490 
1491 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
1492 			  struct nlattr *tb[], struct nlattr *data[],
1493 			  struct netlink_ext_ack *extack)
1494 {
1495 	struct net_bridge *br = netdev_priv(dev);
1496 	int err;
1497 
1498 	err = register_netdevice(dev);
1499 	if (err)
1500 		return err;
1501 
1502 	if (tb[IFLA_ADDRESS]) {
1503 		spin_lock_bh(&br->lock);
1504 		br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
1505 		spin_unlock_bh(&br->lock);
1506 	}
1507 
1508 	err = br_changelink(dev, tb, data, extack);
1509 	if (err)
1510 		br_dev_delete(dev, NULL);
1511 
1512 	return err;
1513 }
1514 
1515 static size_t br_get_size(const struct net_device *brdev)
1516 {
1517 	return nla_total_size(sizeof(u32)) +	/* IFLA_BR_FORWARD_DELAY  */
1518 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_HELLO_TIME */
1519 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_MAX_AGE */
1520 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
1521 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
1522 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
1523 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_FILTERING */
1524 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1525 	       nla_total_size(sizeof(__be16)) +	/* IFLA_BR_VLAN_PROTOCOL */
1526 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_VLAN_DEFAULT_PVID */
1527 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_VLAN_STATS_ENABLED */
1528 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_VLAN_STATS_PER_PORT */
1529 #endif
1530 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_GROUP_FWD_MASK */
1531 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_ROOT_ID */
1532 	       nla_total_size(sizeof(struct ifla_bridge_id)) +   /* IFLA_BR_BRIDGE_ID */
1533 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_ROOT_PORT */
1534 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_ROOT_PATH_COST */
1535 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE */
1536 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_TOPOLOGY_CHANGE_DETECTED */
1537 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_HELLO_TIMER */
1538 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TCN_TIMER */
1539 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_TOPOLOGY_CHANGE_TIMER */
1540 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_GC_TIMER */
1541 	       nla_total_size(ETH_ALEN) +       /* IFLA_BR_GROUP_ADDR */
1542 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1543 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_ROUTER */
1544 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_SNOOPING */
1545 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERY_USE_IFADDR */
1546 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_QUERIER */
1547 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_MCAST_STATS_ENABLED */
1548 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_ELASTICITY */
1549 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_HASH_MAX */
1550 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_LAST_MEMBER_CNT */
1551 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_MCAST_STARTUP_QUERY_CNT */
1552 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_LAST_MEMBER_INTVL */
1553 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_MEMBERSHIP_INTVL */
1554 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERIER_INTVL */
1555 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_INTVL */
1556 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_QUERY_RESPONSE_INTVL */
1557 	       nla_total_size_64bit(sizeof(u64)) + /* IFLA_BR_MCAST_STARTUP_QUERY_INTVL */
1558 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_IGMP_VERSION */
1559 	       nla_total_size(sizeof(u8)) +	/* IFLA_BR_MCAST_MLD_VERSION */
1560 	       br_multicast_querier_state_size() + /* IFLA_BR_MCAST_QUERIER_STATE */
1561 #endif
1562 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1563 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IPTABLES */
1564 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_IP6TABLES */
1565 	       nla_total_size(sizeof(u8)) +     /* IFLA_BR_NF_CALL_ARPTABLES */
1566 #endif
1567 	       nla_total_size(sizeof(struct br_boolopt_multi)) + /* IFLA_BR_MULTI_BOOLOPT */
1568 	       0;
1569 }
1570 
1571 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
1572 {
1573 	struct net_bridge *br = netdev_priv(brdev);
1574 	u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
1575 	u32 hello_time = jiffies_to_clock_t(br->hello_time);
1576 	u32 age_time = jiffies_to_clock_t(br->max_age);
1577 	u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
1578 	u32 stp_enabled = br->stp_enabled;
1579 	u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
1580 	u8 vlan_enabled = br_vlan_enabled(br->dev);
1581 	struct br_boolopt_multi bm;
1582 	u64 clockval;
1583 
1584 	clockval = br_timer_value(&br->hello_timer);
1585 	if (nla_put_u64_64bit(skb, IFLA_BR_HELLO_TIMER, clockval, IFLA_BR_PAD))
1586 		return -EMSGSIZE;
1587 	clockval = br_timer_value(&br->tcn_timer);
1588 	if (nla_put_u64_64bit(skb, IFLA_BR_TCN_TIMER, clockval, IFLA_BR_PAD))
1589 		return -EMSGSIZE;
1590 	clockval = br_timer_value(&br->topology_change_timer);
1591 	if (nla_put_u64_64bit(skb, IFLA_BR_TOPOLOGY_CHANGE_TIMER, clockval,
1592 			      IFLA_BR_PAD))
1593 		return -EMSGSIZE;
1594 	clockval = br_timer_value(&br->gc_work.timer);
1595 	if (nla_put_u64_64bit(skb, IFLA_BR_GC_TIMER, clockval, IFLA_BR_PAD))
1596 		return -EMSGSIZE;
1597 
1598 	br_boolopt_multi_get(br, &bm);
1599 	if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
1600 	    nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
1601 	    nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
1602 	    nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
1603 	    nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
1604 	    nla_put_u16(skb, IFLA_BR_PRIORITY, priority) ||
1605 	    nla_put_u8(skb, IFLA_BR_VLAN_FILTERING, vlan_enabled) ||
1606 	    nla_put_u16(skb, IFLA_BR_GROUP_FWD_MASK, br->group_fwd_mask) ||
1607 	    nla_put(skb, IFLA_BR_BRIDGE_ID, sizeof(struct ifla_bridge_id),
1608 		    &br->bridge_id) ||
1609 	    nla_put(skb, IFLA_BR_ROOT_ID, sizeof(struct ifla_bridge_id),
1610 		    &br->designated_root) ||
1611 	    nla_put_u16(skb, IFLA_BR_ROOT_PORT, br->root_port) ||
1612 	    nla_put_u32(skb, IFLA_BR_ROOT_PATH_COST, br->root_path_cost) ||
1613 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE, br->topology_change) ||
1614 	    nla_put_u8(skb, IFLA_BR_TOPOLOGY_CHANGE_DETECTED,
1615 		       br->topology_change_detected) ||
1616 	    nla_put(skb, IFLA_BR_GROUP_ADDR, ETH_ALEN, br->group_addr) ||
1617 	    nla_put(skb, IFLA_BR_MULTI_BOOLOPT, sizeof(bm), &bm))
1618 		return -EMSGSIZE;
1619 
1620 #ifdef CONFIG_BRIDGE_VLAN_FILTERING
1621 	if (nla_put_be16(skb, IFLA_BR_VLAN_PROTOCOL, br->vlan_proto) ||
1622 	    nla_put_u16(skb, IFLA_BR_VLAN_DEFAULT_PVID, br->default_pvid) ||
1623 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_ENABLED,
1624 		       br_opt_get(br, BROPT_VLAN_STATS_ENABLED)) ||
1625 	    nla_put_u8(skb, IFLA_BR_VLAN_STATS_PER_PORT,
1626 		       br_opt_get(br, BROPT_VLAN_STATS_PER_PORT)))
1627 		return -EMSGSIZE;
1628 #endif
1629 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1630 	if (nla_put_u8(skb, IFLA_BR_MCAST_ROUTER,
1631 		       br->multicast_ctx.multicast_router) ||
1632 	    nla_put_u8(skb, IFLA_BR_MCAST_SNOOPING,
1633 		       br_opt_get(br, BROPT_MULTICAST_ENABLED)) ||
1634 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERY_USE_IFADDR,
1635 		       br_opt_get(br, BROPT_MULTICAST_QUERY_USE_IFADDR)) ||
1636 	    nla_put_u8(skb, IFLA_BR_MCAST_QUERIER,
1637 		       br->multicast_ctx.multicast_querier) ||
1638 	    nla_put_u8(skb, IFLA_BR_MCAST_STATS_ENABLED,
1639 		       br_opt_get(br, BROPT_MULTICAST_STATS_ENABLED)) ||
1640 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_ELASTICITY, RHT_ELASTICITY) ||
1641 	    nla_put_u32(skb, IFLA_BR_MCAST_HASH_MAX, br->hash_max) ||
1642 	    nla_put_u32(skb, IFLA_BR_MCAST_LAST_MEMBER_CNT,
1643 			br->multicast_ctx.multicast_last_member_count) ||
1644 	    nla_put_u32(skb, IFLA_BR_MCAST_STARTUP_QUERY_CNT,
1645 			br->multicast_ctx.multicast_startup_query_count) ||
1646 	    nla_put_u8(skb, IFLA_BR_MCAST_IGMP_VERSION,
1647 		       br->multicast_ctx.multicast_igmp_version) ||
1648 	    br_multicast_dump_querier_state(skb, &br->multicast_ctx,
1649 					    IFLA_BR_MCAST_QUERIER_STATE))
1650 		return -EMSGSIZE;
1651 #if IS_ENABLED(CONFIG_IPV6)
1652 	if (nla_put_u8(skb, IFLA_BR_MCAST_MLD_VERSION,
1653 		       br->multicast_ctx.multicast_mld_version))
1654 		return -EMSGSIZE;
1655 #endif
1656 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_last_member_interval);
1657 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_LAST_MEMBER_INTVL, clockval,
1658 			      IFLA_BR_PAD))
1659 		return -EMSGSIZE;
1660 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_membership_interval);
1661 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_MEMBERSHIP_INTVL, clockval,
1662 			      IFLA_BR_PAD))
1663 		return -EMSGSIZE;
1664 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_querier_interval);
1665 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERIER_INTVL, clockval,
1666 			      IFLA_BR_PAD))
1667 		return -EMSGSIZE;
1668 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_interval);
1669 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_INTVL, clockval,
1670 			      IFLA_BR_PAD))
1671 		return -EMSGSIZE;
1672 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_query_response_interval);
1673 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_QUERY_RESPONSE_INTVL, clockval,
1674 			      IFLA_BR_PAD))
1675 		return -EMSGSIZE;
1676 	clockval = jiffies_to_clock_t(br->multicast_ctx.multicast_startup_query_interval);
1677 	if (nla_put_u64_64bit(skb, IFLA_BR_MCAST_STARTUP_QUERY_INTVL, clockval,
1678 			      IFLA_BR_PAD))
1679 		return -EMSGSIZE;
1680 #endif
1681 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
1682 	if (nla_put_u8(skb, IFLA_BR_NF_CALL_IPTABLES,
1683 		       br_opt_get(br, BROPT_NF_CALL_IPTABLES) ? 1 : 0) ||
1684 	    nla_put_u8(skb, IFLA_BR_NF_CALL_IP6TABLES,
1685 		       br_opt_get(br, BROPT_NF_CALL_IP6TABLES) ? 1 : 0) ||
1686 	    nla_put_u8(skb, IFLA_BR_NF_CALL_ARPTABLES,
1687 		       br_opt_get(br, BROPT_NF_CALL_ARPTABLES) ? 1 : 0))
1688 		return -EMSGSIZE;
1689 #endif
1690 
1691 	return 0;
1692 }
1693 
1694 static size_t br_get_linkxstats_size(const struct net_device *dev, int attr)
1695 {
1696 	struct net_bridge_port *p = NULL;
1697 	struct net_bridge_vlan_group *vg;
1698 	struct net_bridge_vlan *v;
1699 	struct net_bridge *br;
1700 	int numvls = 0;
1701 
1702 	switch (attr) {
1703 	case IFLA_STATS_LINK_XSTATS:
1704 		br = netdev_priv(dev);
1705 		vg = br_vlan_group(br);
1706 		break;
1707 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1708 		p = br_port_get_rtnl(dev);
1709 		if (!p)
1710 			return 0;
1711 		vg = nbp_vlan_group(p);
1712 		break;
1713 	default:
1714 		return 0;
1715 	}
1716 
1717 	if (vg) {
1718 		/* we need to count all, even placeholder entries */
1719 		list_for_each_entry(v, &vg->vlan_list, vlist)
1720 			numvls++;
1721 	}
1722 
1723 	return numvls * nla_total_size(sizeof(struct bridge_vlan_xstats)) +
1724 	       nla_total_size_64bit(sizeof(struct br_mcast_stats)) +
1725 	       (p ? nla_total_size_64bit(sizeof(p->stp_xstats)) : 0) +
1726 	       nla_total_size(0);
1727 }
1728 
1729 static int br_fill_linkxstats(struct sk_buff *skb,
1730 			      const struct net_device *dev,
1731 			      int *prividx, int attr)
1732 {
1733 	struct nlattr *nla __maybe_unused;
1734 	struct net_bridge_port *p = NULL;
1735 	struct net_bridge_vlan_group *vg;
1736 	struct net_bridge_vlan *v;
1737 	struct net_bridge *br;
1738 	struct nlattr *nest;
1739 	int vl_idx = 0;
1740 
1741 	switch (attr) {
1742 	case IFLA_STATS_LINK_XSTATS:
1743 		br = netdev_priv(dev);
1744 		vg = br_vlan_group(br);
1745 		break;
1746 	case IFLA_STATS_LINK_XSTATS_SLAVE:
1747 		p = br_port_get_rtnl(dev);
1748 		if (!p)
1749 			return 0;
1750 		br = p->br;
1751 		vg = nbp_vlan_group(p);
1752 		break;
1753 	default:
1754 		return -EINVAL;
1755 	}
1756 
1757 	nest = nla_nest_start_noflag(skb, LINK_XSTATS_TYPE_BRIDGE);
1758 	if (!nest)
1759 		return -EMSGSIZE;
1760 
1761 	if (vg) {
1762 		u16 pvid;
1763 
1764 		pvid = br_get_pvid(vg);
1765 		list_for_each_entry(v, &vg->vlan_list, vlist) {
1766 			struct bridge_vlan_xstats vxi;
1767 			struct pcpu_sw_netstats stats;
1768 
1769 			if (++vl_idx < *prividx)
1770 				continue;
1771 			memset(&vxi, 0, sizeof(vxi));
1772 			vxi.vid = v->vid;
1773 			vxi.flags = v->flags;
1774 			if (v->vid == pvid)
1775 				vxi.flags |= BRIDGE_VLAN_INFO_PVID;
1776 			br_vlan_get_stats(v, &stats);
1777 			vxi.rx_bytes = u64_stats_read(&stats.rx_bytes);
1778 			vxi.rx_packets = u64_stats_read(&stats.rx_packets);
1779 			vxi.tx_bytes = u64_stats_read(&stats.tx_bytes);
1780 			vxi.tx_packets = u64_stats_read(&stats.tx_packets);
1781 
1782 			if (nla_put(skb, BRIDGE_XSTATS_VLAN, sizeof(vxi), &vxi))
1783 				goto nla_put_failure;
1784 		}
1785 	}
1786 
1787 #ifdef CONFIG_BRIDGE_IGMP_SNOOPING
1788 	if (++vl_idx >= *prividx) {
1789 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_MCAST,
1790 					sizeof(struct br_mcast_stats),
1791 					BRIDGE_XSTATS_PAD);
1792 		if (!nla)
1793 			goto nla_put_failure;
1794 		br_multicast_get_stats(br, p, nla_data(nla));
1795 	}
1796 #endif
1797 
1798 	if (p) {
1799 		nla = nla_reserve_64bit(skb, BRIDGE_XSTATS_STP,
1800 					sizeof(p->stp_xstats),
1801 					BRIDGE_XSTATS_PAD);
1802 		if (!nla)
1803 			goto nla_put_failure;
1804 
1805 		spin_lock_bh(&br->lock);
1806 		memcpy(nla_data(nla), &p->stp_xstats, sizeof(p->stp_xstats));
1807 		spin_unlock_bh(&br->lock);
1808 	}
1809 
1810 	nla_nest_end(skb, nest);
1811 	*prividx = 0;
1812 
1813 	return 0;
1814 
1815 nla_put_failure:
1816 	nla_nest_end(skb, nest);
1817 	*prividx = vl_idx;
1818 
1819 	return -EMSGSIZE;
1820 }
1821 
1822 static struct rtnl_af_ops br_af_ops __read_mostly = {
1823 	.family			= AF_BRIDGE,
1824 	.get_link_af_size	= br_get_link_af_size_filtered,
1825 };
1826 
1827 struct rtnl_link_ops br_link_ops __read_mostly = {
1828 	.kind			= "bridge",
1829 	.priv_size		= sizeof(struct net_bridge),
1830 	.setup			= br_dev_setup,
1831 	.maxtype		= IFLA_BR_MAX,
1832 	.policy			= br_policy,
1833 	.validate		= br_validate,
1834 	.newlink		= br_dev_newlink,
1835 	.changelink		= br_changelink,
1836 	.dellink		= br_dev_delete,
1837 	.get_size		= br_get_size,
1838 	.fill_info		= br_fill_info,
1839 	.fill_linkxstats	= br_fill_linkxstats,
1840 	.get_linkxstats_size	= br_get_linkxstats_size,
1841 
1842 	.slave_maxtype		= IFLA_BRPORT_MAX,
1843 	.slave_policy		= br_port_policy,
1844 	.slave_changelink	= br_port_slave_changelink,
1845 	.get_slave_size		= br_port_get_slave_size,
1846 	.fill_slave_info	= br_port_fill_slave_info,
1847 };
1848 
1849 int __init br_netlink_init(void)
1850 {
1851 	int err;
1852 
1853 	br_mdb_init();
1854 	br_vlan_rtnl_init();
1855 	rtnl_af_register(&br_af_ops);
1856 
1857 	err = rtnl_link_register(&br_link_ops);
1858 	if (err)
1859 		goto out_af;
1860 
1861 	return 0;
1862 
1863 out_af:
1864 	rtnl_af_unregister(&br_af_ops);
1865 	br_mdb_uninit();
1866 	return err;
1867 }
1868 
1869 void br_netlink_fini(void)
1870 {
1871 	br_mdb_uninit();
1872 	br_vlan_rtnl_uninit();
1873 	rtnl_af_unregister(&br_af_ops);
1874 	rtnl_link_unregister(&br_link_ops);
1875 }
1876