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