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