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