xref: /openbmc/linux/net/bridge/br_netlink.c (revision bbde9fc1824aab58bc78c084163007dd6c03fe5b)
1 /*
2  *	Bridge netlink control interface
3  *
4  *	Authors:
5  *	Stephen Hemminger		<shemminger@osdl.org>
6  *
7  *	This program is free software; you can redistribute it and/or
8  *	modify it under the terms of the GNU General Public License
9  *	as published by the Free Software Foundation; either version
10  *	2 of the License, or (at your option) any later version.
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/etherdevice.h>
16 #include <net/rtnetlink.h>
17 #include <net/net_namespace.h>
18 #include <net/sock.h>
19 #include <net/switchdev.h>
20 #include <uapi/linux/if_bridge.h>
21 
22 #include "br_private.h"
23 #include "br_private_stp.h"
24 
25 static int br_get_num_vlan_infos(const struct net_port_vlans *pv,
26 				 u32 filter_mask)
27 {
28 	u16 vid_range_start = 0, vid_range_end = 0;
29 	u16 vid_range_flags = 0;
30 	u16 pvid, vid, flags;
31 	int num_vlans = 0;
32 
33 	if (filter_mask & RTEXT_FILTER_BRVLAN)
34 		return pv->num_vlans;
35 
36 	if (!(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
37 		return 0;
38 
39 	/* Count number of vlan info's
40 	 */
41 	pvid = br_get_pvid(pv);
42 	for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
43 		flags = 0;
44 		if (vid == pvid)
45 			flags |= BRIDGE_VLAN_INFO_PVID;
46 
47 		if (test_bit(vid, pv->untagged_bitmap))
48 			flags |= BRIDGE_VLAN_INFO_UNTAGGED;
49 
50 		if (vid_range_start == 0) {
51 			goto initvars;
52 		} else if ((vid - vid_range_end) == 1 &&
53 			flags == vid_range_flags) {
54 			vid_range_end = vid;
55 			continue;
56 		} else {
57 			if ((vid_range_end - vid_range_start) > 0)
58 				num_vlans += 2;
59 			else
60 				num_vlans += 1;
61 		}
62 initvars:
63 		vid_range_start = vid;
64 		vid_range_end = vid;
65 		vid_range_flags = flags;
66 	}
67 
68 	if (vid_range_start != 0) {
69 		if ((vid_range_end - vid_range_start) > 0)
70 			num_vlans += 2;
71 		else
72 			num_vlans += 1;
73 	}
74 
75 	return num_vlans;
76 }
77 
78 static size_t br_get_link_af_size_filtered(const struct net_device *dev,
79 					   u32 filter_mask)
80 {
81 	struct net_port_vlans *pv;
82 	int num_vlan_infos;
83 
84 	rcu_read_lock();
85 	if (br_port_exists(dev))
86 		pv = nbp_get_vlan_info(br_port_get_rcu(dev));
87 	else if (dev->priv_flags & IFF_EBRIDGE)
88 		pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
89 	else
90 		pv = NULL;
91 	if (pv)
92 		num_vlan_infos = br_get_num_vlan_infos(pv, filter_mask);
93 	else
94 		num_vlan_infos = 0;
95 	rcu_read_unlock();
96 
97 	if (!num_vlan_infos)
98 		return 0;
99 
100 	/* Each VLAN is returned in bridge_vlan_info along with flags */
101 	return num_vlan_infos * nla_total_size(sizeof(struct bridge_vlan_info));
102 }
103 
104 static inline size_t br_port_info_size(void)
105 {
106 	return nla_total_size(1)	/* IFLA_BRPORT_STATE  */
107 		+ nla_total_size(2)	/* IFLA_BRPORT_PRIORITY */
108 		+ nla_total_size(4)	/* IFLA_BRPORT_COST */
109 		+ nla_total_size(1)	/* IFLA_BRPORT_MODE */
110 		+ nla_total_size(1)	/* IFLA_BRPORT_GUARD */
111 		+ nla_total_size(1)	/* IFLA_BRPORT_PROTECT */
112 		+ nla_total_size(1)	/* IFLA_BRPORT_FAST_LEAVE */
113 		+ nla_total_size(1)	/* IFLA_BRPORT_LEARNING */
114 		+ nla_total_size(1)	/* IFLA_BRPORT_UNICAST_FLOOD */
115 		+ 0;
116 }
117 
118 static inline size_t br_nlmsg_size(struct net_device *dev, u32 filter_mask)
119 {
120 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
121 		+ nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
122 		+ nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
123 		+ nla_total_size(4) /* IFLA_MASTER */
124 		+ nla_total_size(4) /* IFLA_MTU */
125 		+ nla_total_size(4) /* IFLA_LINK */
126 		+ nla_total_size(1) /* IFLA_OPERSTATE */
127 		+ nla_total_size(br_port_info_size()) /* IFLA_PROTINFO */
128 		+ nla_total_size(br_get_link_af_size_filtered(dev,
129 				 filter_mask)); /* IFLA_AF_SPEC */
130 }
131 
132 static int br_port_fill_attrs(struct sk_buff *skb,
133 			      const struct net_bridge_port *p)
134 {
135 	u8 mode = !!(p->flags & BR_HAIRPIN_MODE);
136 
137 	if (nla_put_u8(skb, IFLA_BRPORT_STATE, p->state) ||
138 	    nla_put_u16(skb, IFLA_BRPORT_PRIORITY, p->priority) ||
139 	    nla_put_u32(skb, IFLA_BRPORT_COST, p->path_cost) ||
140 	    nla_put_u8(skb, IFLA_BRPORT_MODE, mode) ||
141 	    nla_put_u8(skb, IFLA_BRPORT_GUARD, !!(p->flags & BR_BPDU_GUARD)) ||
142 	    nla_put_u8(skb, IFLA_BRPORT_PROTECT, !!(p->flags & BR_ROOT_BLOCK)) ||
143 	    nla_put_u8(skb, IFLA_BRPORT_FAST_LEAVE, !!(p->flags & BR_MULTICAST_FAST_LEAVE)) ||
144 	    nla_put_u8(skb, IFLA_BRPORT_LEARNING, !!(p->flags & BR_LEARNING)) ||
145 	    nla_put_u8(skb, IFLA_BRPORT_UNICAST_FLOOD, !!(p->flags & BR_FLOOD)) ||
146 	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP, !!(p->flags & BR_PROXYARP)) ||
147 	    nla_put_u8(skb, IFLA_BRPORT_PROXYARP_WIFI,
148 		       !!(p->flags & BR_PROXYARP_WIFI)))
149 		return -EMSGSIZE;
150 
151 	return 0;
152 }
153 
154 static int br_fill_ifvlaninfo_range(struct sk_buff *skb, u16 vid_start,
155 				    u16 vid_end, u16 flags)
156 {
157 	struct  bridge_vlan_info vinfo;
158 
159 	if ((vid_end - vid_start) > 0) {
160 		/* add range to skb */
161 		vinfo.vid = vid_start;
162 		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_BEGIN;
163 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
164 			    sizeof(vinfo), &vinfo))
165 			goto nla_put_failure;
166 
167 		vinfo.vid = vid_end;
168 		vinfo.flags = flags | BRIDGE_VLAN_INFO_RANGE_END;
169 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
170 			    sizeof(vinfo), &vinfo))
171 			goto nla_put_failure;
172 	} else {
173 		vinfo.vid = vid_start;
174 		vinfo.flags = flags;
175 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
176 			    sizeof(vinfo), &vinfo))
177 			goto nla_put_failure;
178 	}
179 
180 	return 0;
181 
182 nla_put_failure:
183 	return -EMSGSIZE;
184 }
185 
186 static int br_fill_ifvlaninfo_compressed(struct sk_buff *skb,
187 					 const struct net_port_vlans *pv)
188 {
189 	u16 vid_range_start = 0, vid_range_end = 0;
190 	u16 vid_range_flags = 0;
191 	u16 pvid, vid, flags;
192 	int err = 0;
193 
194 	/* Pack IFLA_BRIDGE_VLAN_INFO's for every vlan
195 	 * and mark vlan info with begin and end flags
196 	 * if vlaninfo represents a range
197 	 */
198 	pvid = br_get_pvid(pv);
199 	for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
200 		flags = 0;
201 		if (vid == pvid)
202 			flags |= BRIDGE_VLAN_INFO_PVID;
203 
204 		if (test_bit(vid, pv->untagged_bitmap))
205 			flags |= BRIDGE_VLAN_INFO_UNTAGGED;
206 
207 		if (vid_range_start == 0) {
208 			goto initvars;
209 		} else if ((vid - vid_range_end) == 1 &&
210 			flags == vid_range_flags) {
211 			vid_range_end = vid;
212 			continue;
213 		} else {
214 			err = br_fill_ifvlaninfo_range(skb, vid_range_start,
215 						       vid_range_end,
216 						       vid_range_flags);
217 			if (err)
218 				return err;
219 		}
220 
221 initvars:
222 		vid_range_start = vid;
223 		vid_range_end = vid;
224 		vid_range_flags = flags;
225 	}
226 
227 	if (vid_range_start != 0) {
228 		/* Call it once more to send any left over vlans */
229 		err = br_fill_ifvlaninfo_range(skb, vid_range_start,
230 					       vid_range_end,
231 					       vid_range_flags);
232 		if (err)
233 			return err;
234 	}
235 
236 	return 0;
237 }
238 
239 static int br_fill_ifvlaninfo(struct sk_buff *skb,
240 			      const struct net_port_vlans *pv)
241 {
242 	struct bridge_vlan_info vinfo;
243 	u16 pvid, vid;
244 
245 	pvid = br_get_pvid(pv);
246 	for_each_set_bit(vid, pv->vlan_bitmap, VLAN_N_VID) {
247 		vinfo.vid = vid;
248 		vinfo.flags = 0;
249 		if (vid == pvid)
250 			vinfo.flags |= BRIDGE_VLAN_INFO_PVID;
251 
252 		if (test_bit(vid, pv->untagged_bitmap))
253 			vinfo.flags |= BRIDGE_VLAN_INFO_UNTAGGED;
254 
255 		if (nla_put(skb, IFLA_BRIDGE_VLAN_INFO,
256 			    sizeof(vinfo), &vinfo))
257 			goto nla_put_failure;
258 	}
259 
260 	return 0;
261 
262 nla_put_failure:
263 	return -EMSGSIZE;
264 }
265 
266 /*
267  * Create one netlink message for one interface
268  * Contains port and master info as well as carrier and bridge state.
269  */
270 static int br_fill_ifinfo(struct sk_buff *skb,
271 			  const struct net_bridge_port *port,
272 			  u32 pid, u32 seq, int event, unsigned int flags,
273 			  u32 filter_mask, const struct net_device *dev)
274 {
275 	const struct net_bridge *br;
276 	struct ifinfomsg *hdr;
277 	struct nlmsghdr *nlh;
278 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
279 
280 	if (port)
281 		br = port->br;
282 	else
283 		br = netdev_priv(dev);
284 
285 	br_debug(br, "br_fill_info event %d port %s master %s\n",
286 		     event, dev->name, br->dev->name);
287 
288 	nlh = nlmsg_put(skb, pid, seq, event, sizeof(*hdr), flags);
289 	if (nlh == NULL)
290 		return -EMSGSIZE;
291 
292 	hdr = nlmsg_data(nlh);
293 	hdr->ifi_family = AF_BRIDGE;
294 	hdr->__ifi_pad = 0;
295 	hdr->ifi_type = dev->type;
296 	hdr->ifi_index = dev->ifindex;
297 	hdr->ifi_flags = dev_get_flags(dev);
298 	hdr->ifi_change = 0;
299 
300 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
301 	    nla_put_u32(skb, IFLA_MASTER, br->dev->ifindex) ||
302 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
303 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
304 	    (dev->addr_len &&
305 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
306 	    (dev->ifindex != dev_get_iflink(dev) &&
307 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
308 		goto nla_put_failure;
309 
310 	if (event == RTM_NEWLINK && port) {
311 		struct nlattr *nest
312 			= nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
313 
314 		if (nest == NULL || br_port_fill_attrs(skb, port) < 0)
315 			goto nla_put_failure;
316 		nla_nest_end(skb, nest);
317 	}
318 
319 	/* Check if  the VID information is requested */
320 	if ((filter_mask & RTEXT_FILTER_BRVLAN) ||
321 	    (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)) {
322 		const struct net_port_vlans *pv;
323 		struct nlattr *af;
324 		int err;
325 
326 		if (port)
327 			pv = nbp_get_vlan_info(port);
328 		else
329 			pv = br_get_vlan_info(br);
330 
331 		if (!pv || bitmap_empty(pv->vlan_bitmap, VLAN_N_VID))
332 			goto done;
333 
334 		af = nla_nest_start(skb, IFLA_AF_SPEC);
335 		if (!af)
336 			goto nla_put_failure;
337 
338 		if (filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED)
339 			err = br_fill_ifvlaninfo_compressed(skb, pv);
340 		else
341 			err = br_fill_ifvlaninfo(skb, pv);
342 		if (err)
343 			goto nla_put_failure;
344 		nla_nest_end(skb, af);
345 	}
346 
347 done:
348 	nlmsg_end(skb, nlh);
349 	return 0;
350 
351 nla_put_failure:
352 	nlmsg_cancel(skb, nlh);
353 	return -EMSGSIZE;
354 }
355 
356 /*
357  * Notify listeners of a change in port information
358  */
359 void br_ifinfo_notify(int event, struct net_bridge_port *port)
360 {
361 	struct net *net;
362 	struct sk_buff *skb;
363 	int err = -ENOBUFS;
364 	u32 filter = RTEXT_FILTER_BRVLAN_COMPRESSED;
365 
366 	if (!port)
367 		return;
368 
369 	net = dev_net(port->dev);
370 	br_debug(port->br, "port %u(%s) event %d\n",
371 		 (unsigned int)port->port_no, port->dev->name, event);
372 
373 	skb = nlmsg_new(br_nlmsg_size(port->dev, filter), GFP_ATOMIC);
374 	if (skb == NULL)
375 		goto errout;
376 
377 	err = br_fill_ifinfo(skb, port, 0, 0, event, 0, filter, port->dev);
378 	if (err < 0) {
379 		/* -EMSGSIZE implies BUG in br_nlmsg_size() */
380 		WARN_ON(err == -EMSGSIZE);
381 		kfree_skb(skb);
382 		goto errout;
383 	}
384 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
385 	return;
386 errout:
387 	rtnl_set_sk_err(net, RTNLGRP_LINK, err);
388 }
389 
390 
391 /*
392  * Dump information about all ports, in response to GETLINK
393  */
394 int br_getlink(struct sk_buff *skb, u32 pid, u32 seq,
395 	       struct net_device *dev, u32 filter_mask, int nlflags)
396 {
397 	struct net_bridge_port *port = br_port_get_rtnl(dev);
398 
399 	if (!port && !(filter_mask & RTEXT_FILTER_BRVLAN) &&
400 	    !(filter_mask & RTEXT_FILTER_BRVLAN_COMPRESSED))
401 		return 0;
402 
403 	return br_fill_ifinfo(skb, port, pid, seq, RTM_NEWLINK, nlflags,
404 			      filter_mask, dev);
405 }
406 
407 static int br_vlan_info(struct net_bridge *br, struct net_bridge_port *p,
408 			int cmd, struct bridge_vlan_info *vinfo)
409 {
410 	int err = 0;
411 
412 	switch (cmd) {
413 	case RTM_SETLINK:
414 		if (p) {
415 			err = nbp_vlan_add(p, vinfo->vid, vinfo->flags);
416 			if (err)
417 				break;
418 
419 			if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
420 				err = br_vlan_add(p->br, vinfo->vid,
421 						  vinfo->flags);
422 		} else {
423 			err = br_vlan_add(br, vinfo->vid, vinfo->flags);
424 		}
425 		break;
426 
427 	case RTM_DELLINK:
428 		if (p) {
429 			nbp_vlan_delete(p, vinfo->vid);
430 			if (vinfo->flags & BRIDGE_VLAN_INFO_MASTER)
431 				br_vlan_delete(p->br, vinfo->vid);
432 		} else {
433 			br_vlan_delete(br, vinfo->vid);
434 		}
435 		break;
436 	}
437 
438 	return err;
439 }
440 
441 static int br_afspec(struct net_bridge *br,
442 		     struct net_bridge_port *p,
443 		     struct nlattr *af_spec,
444 		     int cmd)
445 {
446 	struct bridge_vlan_info *vinfo_start = NULL;
447 	struct bridge_vlan_info *vinfo = NULL;
448 	struct nlattr *attr;
449 	int err = 0;
450 	int rem;
451 
452 	nla_for_each_nested(attr, af_spec, rem) {
453 		if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
454 			continue;
455 		if (nla_len(attr) != sizeof(struct bridge_vlan_info))
456 			return -EINVAL;
457 		vinfo = nla_data(attr);
458 		if (!vinfo->vid || vinfo->vid >= VLAN_VID_MASK)
459 			return -EINVAL;
460 		if (vinfo->flags & BRIDGE_VLAN_INFO_RANGE_BEGIN) {
461 			if (vinfo_start)
462 				return -EINVAL;
463 			vinfo_start = vinfo;
464 			continue;
465 		}
466 
467 		if (vinfo_start) {
468 			struct bridge_vlan_info tmp_vinfo;
469 			int v;
470 
471 			if (!(vinfo->flags & BRIDGE_VLAN_INFO_RANGE_END))
472 				return -EINVAL;
473 
474 			if (vinfo->vid <= vinfo_start->vid)
475 				return -EINVAL;
476 
477 			memcpy(&tmp_vinfo, vinfo_start,
478 			       sizeof(struct bridge_vlan_info));
479 
480 			for (v = vinfo_start->vid; v <= vinfo->vid; v++) {
481 				tmp_vinfo.vid = v;
482 				err = br_vlan_info(br, p, cmd, &tmp_vinfo);
483 				if (err)
484 					break;
485 			}
486 			vinfo_start = NULL;
487 		} else {
488 			err = br_vlan_info(br, p, cmd, vinfo);
489 		}
490 		if (err)
491 			break;
492 	}
493 
494 	return err;
495 }
496 
497 static const struct nla_policy br_port_policy[IFLA_BRPORT_MAX + 1] = {
498 	[IFLA_BRPORT_STATE]	= { .type = NLA_U8 },
499 	[IFLA_BRPORT_COST]	= { .type = NLA_U32 },
500 	[IFLA_BRPORT_PRIORITY]	= { .type = NLA_U16 },
501 	[IFLA_BRPORT_MODE]	= { .type = NLA_U8 },
502 	[IFLA_BRPORT_GUARD]	= { .type = NLA_U8 },
503 	[IFLA_BRPORT_PROTECT]	= { .type = NLA_U8 },
504 	[IFLA_BRPORT_FAST_LEAVE]= { .type = NLA_U8 },
505 	[IFLA_BRPORT_LEARNING]	= { .type = NLA_U8 },
506 	[IFLA_BRPORT_UNICAST_FLOOD] = { .type = NLA_U8 },
507 };
508 
509 /* Change the state of the port and notify spanning tree */
510 static int br_set_port_state(struct net_bridge_port *p, u8 state)
511 {
512 	if (state > BR_STATE_BLOCKING)
513 		return -EINVAL;
514 
515 	/* if kernel STP is running, don't allow changes */
516 	if (p->br->stp_enabled == BR_KERNEL_STP)
517 		return -EBUSY;
518 
519 	/* if device is not up, change is not allowed
520 	 * if link is not present, only allowable state is disabled
521 	 */
522 	if (!netif_running(p->dev) ||
523 	    (!netif_oper_up(p->dev) && state != BR_STATE_DISABLED))
524 		return -ENETDOWN;
525 
526 	br_set_state(p, state);
527 	br_log_state(p);
528 	br_port_state_selection(p->br);
529 	return 0;
530 }
531 
532 /* Set/clear or port flags based on attribute */
533 static void br_set_port_flag(struct net_bridge_port *p, struct nlattr *tb[],
534 			   int attrtype, unsigned long mask)
535 {
536 	if (tb[attrtype]) {
537 		u8 flag = nla_get_u8(tb[attrtype]);
538 		if (flag)
539 			p->flags |= mask;
540 		else
541 			p->flags &= ~mask;
542 	}
543 }
544 
545 /* Process bridge protocol info on port */
546 static int br_setport(struct net_bridge_port *p, struct nlattr *tb[])
547 {
548 	int err;
549 	unsigned long old_flags = p->flags;
550 
551 	br_set_port_flag(p, tb, IFLA_BRPORT_MODE, BR_HAIRPIN_MODE);
552 	br_set_port_flag(p, tb, IFLA_BRPORT_GUARD, BR_BPDU_GUARD);
553 	br_set_port_flag(p, tb, IFLA_BRPORT_FAST_LEAVE, BR_MULTICAST_FAST_LEAVE);
554 	br_set_port_flag(p, tb, IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK);
555 	br_set_port_flag(p, tb, IFLA_BRPORT_LEARNING, BR_LEARNING);
556 	br_set_port_flag(p, tb, IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD);
557 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP, BR_PROXYARP);
558 	br_set_port_flag(p, tb, IFLA_BRPORT_PROXYARP_WIFI, BR_PROXYARP_WIFI);
559 
560 	if (tb[IFLA_BRPORT_COST]) {
561 		err = br_stp_set_path_cost(p, nla_get_u32(tb[IFLA_BRPORT_COST]));
562 		if (err)
563 			return err;
564 	}
565 
566 	if (tb[IFLA_BRPORT_PRIORITY]) {
567 		err = br_stp_set_port_priority(p, nla_get_u16(tb[IFLA_BRPORT_PRIORITY]));
568 		if (err)
569 			return err;
570 	}
571 
572 	if (tb[IFLA_BRPORT_STATE]) {
573 		err = br_set_port_state(p, nla_get_u8(tb[IFLA_BRPORT_STATE]));
574 		if (err)
575 			return err;
576 	}
577 
578 	br_port_flags_change(p, old_flags ^ p->flags);
579 	return 0;
580 }
581 
582 /* Change state and parameters on port. */
583 int br_setlink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
584 {
585 	struct nlattr *protinfo;
586 	struct nlattr *afspec;
587 	struct net_bridge_port *p;
588 	struct nlattr *tb[IFLA_BRPORT_MAX + 1];
589 	int err = 0;
590 
591 	protinfo = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_PROTINFO);
592 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
593 	if (!protinfo && !afspec)
594 		return 0;
595 
596 	p = br_port_get_rtnl(dev);
597 	/* We want to accept dev as bridge itself if the AF_SPEC
598 	 * is set to see if someone is setting vlan info on the bridge
599 	 */
600 	if (!p && !afspec)
601 		return -EINVAL;
602 
603 	if (p && protinfo) {
604 		if (protinfo->nla_type & NLA_F_NESTED) {
605 			err = nla_parse_nested(tb, IFLA_BRPORT_MAX,
606 					       protinfo, br_port_policy);
607 			if (err)
608 				return err;
609 
610 			spin_lock_bh(&p->br->lock);
611 			err = br_setport(p, tb);
612 			spin_unlock_bh(&p->br->lock);
613 		} else {
614 			/* Binary compatibility with old RSTP */
615 			if (nla_len(protinfo) < sizeof(u8))
616 				return -EINVAL;
617 
618 			spin_lock_bh(&p->br->lock);
619 			err = br_set_port_state(p, nla_get_u8(protinfo));
620 			spin_unlock_bh(&p->br->lock);
621 		}
622 		if (err)
623 			goto out;
624 	}
625 
626 	if (afspec) {
627 		err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
628 				afspec, RTM_SETLINK);
629 	}
630 
631 	if (err == 0)
632 		br_ifinfo_notify(RTM_NEWLINK, p);
633 out:
634 	return err;
635 }
636 
637 /* Delete port information */
638 int br_dellink(struct net_device *dev, struct nlmsghdr *nlh, u16 flags)
639 {
640 	struct nlattr *afspec;
641 	struct net_bridge_port *p;
642 	int err = 0;
643 
644 	afspec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
645 	if (!afspec)
646 		return 0;
647 
648 	p = br_port_get_rtnl(dev);
649 	/* We want to accept dev as bridge itself as well */
650 	if (!p && !(dev->priv_flags & IFF_EBRIDGE))
651 		return -EINVAL;
652 
653 	err = br_afspec((struct net_bridge *)netdev_priv(dev), p,
654 			afspec, RTM_DELLINK);
655 	if (err == 0)
656 		/* Send RTM_NEWLINK because userspace
657 		 * expects RTM_NEWLINK for vlan dels
658 		 */
659 		br_ifinfo_notify(RTM_NEWLINK, p);
660 
661 	return err;
662 }
663 static int br_validate(struct nlattr *tb[], struct nlattr *data[])
664 {
665 	if (tb[IFLA_ADDRESS]) {
666 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
667 			return -EINVAL;
668 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
669 			return -EADDRNOTAVAIL;
670 	}
671 
672 	return 0;
673 }
674 
675 static int br_dev_newlink(struct net *src_net, struct net_device *dev,
676 			  struct nlattr *tb[], struct nlattr *data[])
677 {
678 	struct net_bridge *br = netdev_priv(dev);
679 
680 	if (tb[IFLA_ADDRESS]) {
681 		spin_lock_bh(&br->lock);
682 		br_stp_change_bridge_id(br, nla_data(tb[IFLA_ADDRESS]));
683 		spin_unlock_bh(&br->lock);
684 	}
685 
686 	return register_netdevice(dev);
687 }
688 
689 static int br_port_slave_changelink(struct net_device *brdev,
690 				    struct net_device *dev,
691 				    struct nlattr *tb[],
692 				    struct nlattr *data[])
693 {
694 	struct net_bridge *br = netdev_priv(brdev);
695 	int ret;
696 
697 	if (!data)
698 		return 0;
699 
700 	spin_lock_bh(&br->lock);
701 	ret = br_setport(br_port_get_rtnl(dev), data);
702 	spin_unlock_bh(&br->lock);
703 
704 	return ret;
705 }
706 
707 static int br_port_fill_slave_info(struct sk_buff *skb,
708 				   const struct net_device *brdev,
709 				   const struct net_device *dev)
710 {
711 	return br_port_fill_attrs(skb, br_port_get_rtnl(dev));
712 }
713 
714 static size_t br_port_get_slave_size(const struct net_device *brdev,
715 				     const struct net_device *dev)
716 {
717 	return br_port_info_size();
718 }
719 
720 static const struct nla_policy br_policy[IFLA_BR_MAX + 1] = {
721 	[IFLA_BR_FORWARD_DELAY]	= { .type = NLA_U32 },
722 	[IFLA_BR_HELLO_TIME]	= { .type = NLA_U32 },
723 	[IFLA_BR_MAX_AGE]	= { .type = NLA_U32 },
724 	[IFLA_BR_AGEING_TIME] = { .type = NLA_U32 },
725 	[IFLA_BR_STP_STATE] = { .type = NLA_U32 },
726 	[IFLA_BR_PRIORITY] = { .type = NLA_U16 },
727 };
728 
729 static int br_changelink(struct net_device *brdev, struct nlattr *tb[],
730 			 struct nlattr *data[])
731 {
732 	struct net_bridge *br = netdev_priv(brdev);
733 	int err;
734 
735 	if (!data)
736 		return 0;
737 
738 	if (data[IFLA_BR_FORWARD_DELAY]) {
739 		err = br_set_forward_delay(br, nla_get_u32(data[IFLA_BR_FORWARD_DELAY]));
740 		if (err)
741 			return err;
742 	}
743 
744 	if (data[IFLA_BR_HELLO_TIME]) {
745 		err = br_set_hello_time(br, nla_get_u32(data[IFLA_BR_HELLO_TIME]));
746 		if (err)
747 			return err;
748 	}
749 
750 	if (data[IFLA_BR_MAX_AGE]) {
751 		err = br_set_max_age(br, nla_get_u32(data[IFLA_BR_MAX_AGE]));
752 		if (err)
753 			return err;
754 	}
755 
756 	if (data[IFLA_BR_AGEING_TIME]) {
757 		u32 ageing_time = nla_get_u32(data[IFLA_BR_AGEING_TIME]);
758 
759 		br->ageing_time = clock_t_to_jiffies(ageing_time);
760 	}
761 
762 	if (data[IFLA_BR_STP_STATE]) {
763 		u32 stp_enabled = nla_get_u32(data[IFLA_BR_STP_STATE]);
764 
765 		br_stp_set_enabled(br, stp_enabled);
766 	}
767 
768 	if (data[IFLA_BR_PRIORITY]) {
769 		u32 priority = nla_get_u16(data[IFLA_BR_PRIORITY]);
770 
771 		br_stp_set_bridge_priority(br, priority);
772 	}
773 
774 	return 0;
775 }
776 
777 static size_t br_get_size(const struct net_device *brdev)
778 {
779 	return nla_total_size(sizeof(u32)) +	/* IFLA_BR_FORWARD_DELAY  */
780 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_HELLO_TIME */
781 	       nla_total_size(sizeof(u32)) +	/* IFLA_BR_MAX_AGE */
782 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_AGEING_TIME */
783 	       nla_total_size(sizeof(u32)) +    /* IFLA_BR_STP_STATE */
784 	       nla_total_size(sizeof(u16)) +    /* IFLA_BR_PRIORITY */
785 	       0;
786 }
787 
788 static int br_fill_info(struct sk_buff *skb, const struct net_device *brdev)
789 {
790 	struct net_bridge *br = netdev_priv(brdev);
791 	u32 forward_delay = jiffies_to_clock_t(br->forward_delay);
792 	u32 hello_time = jiffies_to_clock_t(br->hello_time);
793 	u32 age_time = jiffies_to_clock_t(br->max_age);
794 	u32 ageing_time = jiffies_to_clock_t(br->ageing_time);
795 	u32 stp_enabled = br->stp_enabled;
796 	u16 priority = (br->bridge_id.prio[0] << 8) | br->bridge_id.prio[1];
797 
798 	if (nla_put_u32(skb, IFLA_BR_FORWARD_DELAY, forward_delay) ||
799 	    nla_put_u32(skb, IFLA_BR_HELLO_TIME, hello_time) ||
800 	    nla_put_u32(skb, IFLA_BR_MAX_AGE, age_time) ||
801 	    nla_put_u32(skb, IFLA_BR_AGEING_TIME, ageing_time) ||
802 	    nla_put_u32(skb, IFLA_BR_STP_STATE, stp_enabled) ||
803 	    nla_put_u16(skb, IFLA_BR_PRIORITY, priority))
804 		return -EMSGSIZE;
805 
806 	return 0;
807 }
808 
809 static size_t br_get_link_af_size(const struct net_device *dev)
810 {
811 	struct net_port_vlans *pv;
812 
813 	if (br_port_exists(dev))
814 		pv = nbp_get_vlan_info(br_port_get_rtnl(dev));
815 	else if (dev->priv_flags & IFF_EBRIDGE)
816 		pv = br_get_vlan_info((struct net_bridge *)netdev_priv(dev));
817 	else
818 		return 0;
819 
820 	if (!pv)
821 		return 0;
822 
823 	/* Each VLAN is returned in bridge_vlan_info along with flags */
824 	return pv->num_vlans * nla_total_size(sizeof(struct bridge_vlan_info));
825 }
826 
827 static struct rtnl_af_ops br_af_ops __read_mostly = {
828 	.family			= AF_BRIDGE,
829 	.get_link_af_size	= br_get_link_af_size,
830 };
831 
832 struct rtnl_link_ops br_link_ops __read_mostly = {
833 	.kind			= "bridge",
834 	.priv_size		= sizeof(struct net_bridge),
835 	.setup			= br_dev_setup,
836 	.maxtype		= IFLA_BRPORT_MAX,
837 	.policy			= br_policy,
838 	.validate		= br_validate,
839 	.newlink		= br_dev_newlink,
840 	.changelink		= br_changelink,
841 	.dellink		= br_dev_delete,
842 	.get_size		= br_get_size,
843 	.fill_info		= br_fill_info,
844 
845 	.slave_maxtype		= IFLA_BRPORT_MAX,
846 	.slave_policy		= br_port_policy,
847 	.slave_changelink	= br_port_slave_changelink,
848 	.get_slave_size		= br_port_get_slave_size,
849 	.fill_slave_info	= br_port_fill_slave_info,
850 };
851 
852 int __init br_netlink_init(void)
853 {
854 	int err;
855 
856 	br_mdb_init();
857 	rtnl_af_register(&br_af_ops);
858 
859 	err = rtnl_link_register(&br_link_ops);
860 	if (err)
861 		goto out_af;
862 
863 	return 0;
864 
865 out_af:
866 	rtnl_af_unregister(&br_af_ops);
867 	br_mdb_uninit();
868 	return err;
869 }
870 
871 void br_netlink_fini(void)
872 {
873 	br_mdb_uninit();
874 	rtnl_af_unregister(&br_af_ops);
875 	rtnl_link_unregister(&br_link_ops);
876 }
877