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