xref: /openbmc/linux/net/core/rtnetlink.c (revision 9b9c2cd4)
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  *
15  *	Fixes:
16  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
17  */
18 
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/if_bridge.h>
39 #include <linux/if_vlan.h>
40 #include <linux/pci.h>
41 #include <linux/etherdevice.h>
42 
43 #include <asm/uaccess.h>
44 
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <net/switchdev.h>
48 #include <net/ip.h>
49 #include <net/protocol.h>
50 #include <net/arp.h>
51 #include <net/route.h>
52 #include <net/udp.h>
53 #include <net/tcp.h>
54 #include <net/sock.h>
55 #include <net/pkt_sched.h>
56 #include <net/fib_rules.h>
57 #include <net/rtnetlink.h>
58 #include <net/net_namespace.h>
59 
60 struct rtnl_link {
61 	rtnl_doit_func		doit;
62 	rtnl_dumpit_func	dumpit;
63 	rtnl_calcit_func 	calcit;
64 };
65 
66 static DEFINE_MUTEX(rtnl_mutex);
67 
68 void rtnl_lock(void)
69 {
70 	mutex_lock(&rtnl_mutex);
71 }
72 EXPORT_SYMBOL(rtnl_lock);
73 
74 void __rtnl_unlock(void)
75 {
76 	mutex_unlock(&rtnl_mutex);
77 }
78 
79 void rtnl_unlock(void)
80 {
81 	/* This fellow will unlock it for us. */
82 	netdev_run_todo();
83 }
84 EXPORT_SYMBOL(rtnl_unlock);
85 
86 int rtnl_trylock(void)
87 {
88 	return mutex_trylock(&rtnl_mutex);
89 }
90 EXPORT_SYMBOL(rtnl_trylock);
91 
92 int rtnl_is_locked(void)
93 {
94 	return mutex_is_locked(&rtnl_mutex);
95 }
96 EXPORT_SYMBOL(rtnl_is_locked);
97 
98 #ifdef CONFIG_PROVE_LOCKING
99 bool lockdep_rtnl_is_held(void)
100 {
101 	return lockdep_is_held(&rtnl_mutex);
102 }
103 EXPORT_SYMBOL(lockdep_rtnl_is_held);
104 #endif /* #ifdef CONFIG_PROVE_LOCKING */
105 
106 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
107 
108 static inline int rtm_msgindex(int msgtype)
109 {
110 	int msgindex = msgtype - RTM_BASE;
111 
112 	/*
113 	 * msgindex < 0 implies someone tried to register a netlink
114 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
115 	 * the message type has not been added to linux/rtnetlink.h
116 	 */
117 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
118 
119 	return msgindex;
120 }
121 
122 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
123 {
124 	struct rtnl_link *tab;
125 
126 	if (protocol <= RTNL_FAMILY_MAX)
127 		tab = rtnl_msg_handlers[protocol];
128 	else
129 		tab = NULL;
130 
131 	if (tab == NULL || tab[msgindex].doit == NULL)
132 		tab = rtnl_msg_handlers[PF_UNSPEC];
133 
134 	return tab[msgindex].doit;
135 }
136 
137 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
138 {
139 	struct rtnl_link *tab;
140 
141 	if (protocol <= RTNL_FAMILY_MAX)
142 		tab = rtnl_msg_handlers[protocol];
143 	else
144 		tab = NULL;
145 
146 	if (tab == NULL || tab[msgindex].dumpit == NULL)
147 		tab = rtnl_msg_handlers[PF_UNSPEC];
148 
149 	return tab[msgindex].dumpit;
150 }
151 
152 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
153 {
154 	struct rtnl_link *tab;
155 
156 	if (protocol <= RTNL_FAMILY_MAX)
157 		tab = rtnl_msg_handlers[protocol];
158 	else
159 		tab = NULL;
160 
161 	if (tab == NULL || tab[msgindex].calcit == NULL)
162 		tab = rtnl_msg_handlers[PF_UNSPEC];
163 
164 	return tab[msgindex].calcit;
165 }
166 
167 /**
168  * __rtnl_register - Register a rtnetlink message type
169  * @protocol: Protocol family or PF_UNSPEC
170  * @msgtype: rtnetlink message type
171  * @doit: Function pointer called for each request message
172  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
173  * @calcit: Function pointer to calc size of dump message
174  *
175  * Registers the specified function pointers (at least one of them has
176  * to be non-NULL) to be called whenever a request message for the
177  * specified protocol family and message type is received.
178  *
179  * The special protocol family PF_UNSPEC may be used to define fallback
180  * function pointers for the case when no entry for the specific protocol
181  * family exists.
182  *
183  * Returns 0 on success or a negative error code.
184  */
185 int __rtnl_register(int protocol, int msgtype,
186 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
187 		    rtnl_calcit_func calcit)
188 {
189 	struct rtnl_link *tab;
190 	int msgindex;
191 
192 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
193 	msgindex = rtm_msgindex(msgtype);
194 
195 	tab = rtnl_msg_handlers[protocol];
196 	if (tab == NULL) {
197 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
198 		if (tab == NULL)
199 			return -ENOBUFS;
200 
201 		rtnl_msg_handlers[protocol] = tab;
202 	}
203 
204 	if (doit)
205 		tab[msgindex].doit = doit;
206 
207 	if (dumpit)
208 		tab[msgindex].dumpit = dumpit;
209 
210 	if (calcit)
211 		tab[msgindex].calcit = calcit;
212 
213 	return 0;
214 }
215 EXPORT_SYMBOL_GPL(__rtnl_register);
216 
217 /**
218  * rtnl_register - Register a rtnetlink message type
219  *
220  * Identical to __rtnl_register() but panics on failure. This is useful
221  * as failure of this function is very unlikely, it can only happen due
222  * to lack of memory when allocating the chain to store all message
223  * handlers for a protocol. Meant for use in init functions where lack
224  * of memory implies no sense in continuing.
225  */
226 void rtnl_register(int protocol, int msgtype,
227 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
228 		   rtnl_calcit_func calcit)
229 {
230 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
231 		panic("Unable to register rtnetlink message handler, "
232 		      "protocol = %d, message type = %d\n",
233 		      protocol, msgtype);
234 }
235 EXPORT_SYMBOL_GPL(rtnl_register);
236 
237 /**
238  * rtnl_unregister - Unregister a rtnetlink message type
239  * @protocol: Protocol family or PF_UNSPEC
240  * @msgtype: rtnetlink message type
241  *
242  * Returns 0 on success or a negative error code.
243  */
244 int rtnl_unregister(int protocol, int msgtype)
245 {
246 	int msgindex;
247 
248 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
249 	msgindex = rtm_msgindex(msgtype);
250 
251 	if (rtnl_msg_handlers[protocol] == NULL)
252 		return -ENOENT;
253 
254 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
255 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
256 
257 	return 0;
258 }
259 EXPORT_SYMBOL_GPL(rtnl_unregister);
260 
261 /**
262  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
263  * @protocol : Protocol family or PF_UNSPEC
264  *
265  * Identical to calling rtnl_unregster() for all registered message types
266  * of a certain protocol family.
267  */
268 void rtnl_unregister_all(int protocol)
269 {
270 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
271 
272 	kfree(rtnl_msg_handlers[protocol]);
273 	rtnl_msg_handlers[protocol] = NULL;
274 }
275 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
276 
277 static LIST_HEAD(link_ops);
278 
279 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
280 {
281 	const struct rtnl_link_ops *ops;
282 
283 	list_for_each_entry(ops, &link_ops, list) {
284 		if (!strcmp(ops->kind, kind))
285 			return ops;
286 	}
287 	return NULL;
288 }
289 
290 /**
291  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
292  * @ops: struct rtnl_link_ops * to register
293  *
294  * The caller must hold the rtnl_mutex. This function should be used
295  * by drivers that create devices during module initialization. It
296  * must be called before registering the devices.
297  *
298  * Returns 0 on success or a negative error code.
299  */
300 int __rtnl_link_register(struct rtnl_link_ops *ops)
301 {
302 	if (rtnl_link_ops_get(ops->kind))
303 		return -EEXIST;
304 
305 	/* The check for setup is here because if ops
306 	 * does not have that filled up, it is not possible
307 	 * to use the ops for creating device. So do not
308 	 * fill up dellink as well. That disables rtnl_dellink.
309 	 */
310 	if (ops->setup && !ops->dellink)
311 		ops->dellink = unregister_netdevice_queue;
312 
313 	list_add_tail(&ops->list, &link_ops);
314 	return 0;
315 }
316 EXPORT_SYMBOL_GPL(__rtnl_link_register);
317 
318 /**
319  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
320  * @ops: struct rtnl_link_ops * to register
321  *
322  * Returns 0 on success or a negative error code.
323  */
324 int rtnl_link_register(struct rtnl_link_ops *ops)
325 {
326 	int err;
327 
328 	rtnl_lock();
329 	err = __rtnl_link_register(ops);
330 	rtnl_unlock();
331 	return err;
332 }
333 EXPORT_SYMBOL_GPL(rtnl_link_register);
334 
335 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
336 {
337 	struct net_device *dev;
338 	LIST_HEAD(list_kill);
339 
340 	for_each_netdev(net, dev) {
341 		if (dev->rtnl_link_ops == ops)
342 			ops->dellink(dev, &list_kill);
343 	}
344 	unregister_netdevice_many(&list_kill);
345 }
346 
347 /**
348  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
349  * @ops: struct rtnl_link_ops * to unregister
350  *
351  * The caller must hold the rtnl_mutex.
352  */
353 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
354 {
355 	struct net *net;
356 
357 	for_each_net(net) {
358 		__rtnl_kill_links(net, ops);
359 	}
360 	list_del(&ops->list);
361 }
362 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
363 
364 /* Return with the rtnl_lock held when there are no network
365  * devices unregistering in any network namespace.
366  */
367 static void rtnl_lock_unregistering_all(void)
368 {
369 	struct net *net;
370 	bool unregistering;
371 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
372 
373 	add_wait_queue(&netdev_unregistering_wq, &wait);
374 	for (;;) {
375 		unregistering = false;
376 		rtnl_lock();
377 		for_each_net(net) {
378 			if (net->dev_unreg_count > 0) {
379 				unregistering = true;
380 				break;
381 			}
382 		}
383 		if (!unregistering)
384 			break;
385 		__rtnl_unlock();
386 
387 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
388 	}
389 	remove_wait_queue(&netdev_unregistering_wq, &wait);
390 }
391 
392 /**
393  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
394  * @ops: struct rtnl_link_ops * to unregister
395  */
396 void rtnl_link_unregister(struct rtnl_link_ops *ops)
397 {
398 	/* Close the race with cleanup_net() */
399 	mutex_lock(&net_mutex);
400 	rtnl_lock_unregistering_all();
401 	__rtnl_link_unregister(ops);
402 	rtnl_unlock();
403 	mutex_unlock(&net_mutex);
404 }
405 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
406 
407 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
408 {
409 	struct net_device *master_dev;
410 	const struct rtnl_link_ops *ops;
411 
412 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
413 	if (!master_dev)
414 		return 0;
415 	ops = master_dev->rtnl_link_ops;
416 	if (!ops || !ops->get_slave_size)
417 		return 0;
418 	/* IFLA_INFO_SLAVE_DATA + nested data */
419 	return nla_total_size(sizeof(struct nlattr)) +
420 	       ops->get_slave_size(master_dev, dev);
421 }
422 
423 static size_t rtnl_link_get_size(const struct net_device *dev)
424 {
425 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
426 	size_t size;
427 
428 	if (!ops)
429 		return 0;
430 
431 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
432 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
433 
434 	if (ops->get_size)
435 		/* IFLA_INFO_DATA + nested data */
436 		size += nla_total_size(sizeof(struct nlattr)) +
437 			ops->get_size(dev);
438 
439 	if (ops->get_xstats_size)
440 		/* IFLA_INFO_XSTATS */
441 		size += nla_total_size(ops->get_xstats_size(dev));
442 
443 	size += rtnl_link_get_slave_info_data_size(dev);
444 
445 	return size;
446 }
447 
448 static LIST_HEAD(rtnl_af_ops);
449 
450 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
451 {
452 	const struct rtnl_af_ops *ops;
453 
454 	list_for_each_entry(ops, &rtnl_af_ops, list) {
455 		if (ops->family == family)
456 			return ops;
457 	}
458 
459 	return NULL;
460 }
461 
462 /**
463  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
464  * @ops: struct rtnl_af_ops * to register
465  *
466  * Returns 0 on success or a negative error code.
467  */
468 void rtnl_af_register(struct rtnl_af_ops *ops)
469 {
470 	rtnl_lock();
471 	list_add_tail(&ops->list, &rtnl_af_ops);
472 	rtnl_unlock();
473 }
474 EXPORT_SYMBOL_GPL(rtnl_af_register);
475 
476 /**
477  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
478  * @ops: struct rtnl_af_ops * to unregister
479  *
480  * The caller must hold the rtnl_mutex.
481  */
482 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
483 {
484 	list_del(&ops->list);
485 }
486 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
487 
488 /**
489  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
490  * @ops: struct rtnl_af_ops * to unregister
491  */
492 void rtnl_af_unregister(struct rtnl_af_ops *ops)
493 {
494 	rtnl_lock();
495 	__rtnl_af_unregister(ops);
496 	rtnl_unlock();
497 }
498 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
499 
500 static size_t rtnl_link_get_af_size(const struct net_device *dev,
501 				    u32 ext_filter_mask)
502 {
503 	struct rtnl_af_ops *af_ops;
504 	size_t size;
505 
506 	/* IFLA_AF_SPEC */
507 	size = nla_total_size(sizeof(struct nlattr));
508 
509 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
510 		if (af_ops->get_link_af_size) {
511 			/* AF_* + nested data */
512 			size += nla_total_size(sizeof(struct nlattr)) +
513 				af_ops->get_link_af_size(dev, ext_filter_mask);
514 		}
515 	}
516 
517 	return size;
518 }
519 
520 static bool rtnl_have_link_slave_info(const struct net_device *dev)
521 {
522 	struct net_device *master_dev;
523 
524 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
525 	if (master_dev && master_dev->rtnl_link_ops)
526 		return true;
527 	return false;
528 }
529 
530 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
531 				     const struct net_device *dev)
532 {
533 	struct net_device *master_dev;
534 	const struct rtnl_link_ops *ops;
535 	struct nlattr *slave_data;
536 	int err;
537 
538 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
539 	if (!master_dev)
540 		return 0;
541 	ops = master_dev->rtnl_link_ops;
542 	if (!ops)
543 		return 0;
544 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
545 		return -EMSGSIZE;
546 	if (ops->fill_slave_info) {
547 		slave_data = nla_nest_start(skb, IFLA_INFO_SLAVE_DATA);
548 		if (!slave_data)
549 			return -EMSGSIZE;
550 		err = ops->fill_slave_info(skb, master_dev, dev);
551 		if (err < 0)
552 			goto err_cancel_slave_data;
553 		nla_nest_end(skb, slave_data);
554 	}
555 	return 0;
556 
557 err_cancel_slave_data:
558 	nla_nest_cancel(skb, slave_data);
559 	return err;
560 }
561 
562 static int rtnl_link_info_fill(struct sk_buff *skb,
563 			       const struct net_device *dev)
564 {
565 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
566 	struct nlattr *data;
567 	int err;
568 
569 	if (!ops)
570 		return 0;
571 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
572 		return -EMSGSIZE;
573 	if (ops->fill_xstats) {
574 		err = ops->fill_xstats(skb, dev);
575 		if (err < 0)
576 			return err;
577 	}
578 	if (ops->fill_info) {
579 		data = nla_nest_start(skb, IFLA_INFO_DATA);
580 		if (data == NULL)
581 			return -EMSGSIZE;
582 		err = ops->fill_info(skb, dev);
583 		if (err < 0)
584 			goto err_cancel_data;
585 		nla_nest_end(skb, data);
586 	}
587 	return 0;
588 
589 err_cancel_data:
590 	nla_nest_cancel(skb, data);
591 	return err;
592 }
593 
594 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
595 {
596 	struct nlattr *linkinfo;
597 	int err = -EMSGSIZE;
598 
599 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
600 	if (linkinfo == NULL)
601 		goto out;
602 
603 	err = rtnl_link_info_fill(skb, dev);
604 	if (err < 0)
605 		goto err_cancel_link;
606 
607 	err = rtnl_link_slave_info_fill(skb, dev);
608 	if (err < 0)
609 		goto err_cancel_link;
610 
611 	nla_nest_end(skb, linkinfo);
612 	return 0;
613 
614 err_cancel_link:
615 	nla_nest_cancel(skb, linkinfo);
616 out:
617 	return err;
618 }
619 
620 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
621 {
622 	struct sock *rtnl = net->rtnl;
623 	int err = 0;
624 
625 	NETLINK_CB(skb).dst_group = group;
626 	if (echo)
627 		atomic_inc(&skb->users);
628 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
629 	if (echo)
630 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
631 	return err;
632 }
633 
634 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
635 {
636 	struct sock *rtnl = net->rtnl;
637 
638 	return nlmsg_unicast(rtnl, skb, pid);
639 }
640 EXPORT_SYMBOL(rtnl_unicast);
641 
642 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
643 		 struct nlmsghdr *nlh, gfp_t flags)
644 {
645 	struct sock *rtnl = net->rtnl;
646 	int report = 0;
647 
648 	if (nlh)
649 		report = nlmsg_report(nlh);
650 
651 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
652 }
653 EXPORT_SYMBOL(rtnl_notify);
654 
655 void rtnl_set_sk_err(struct net *net, u32 group, int error)
656 {
657 	struct sock *rtnl = net->rtnl;
658 
659 	netlink_set_err(rtnl, 0, group, error);
660 }
661 EXPORT_SYMBOL(rtnl_set_sk_err);
662 
663 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
664 {
665 	struct nlattr *mx;
666 	int i, valid = 0;
667 
668 	mx = nla_nest_start(skb, RTA_METRICS);
669 	if (mx == NULL)
670 		return -ENOBUFS;
671 
672 	for (i = 0; i < RTAX_MAX; i++) {
673 		if (metrics[i]) {
674 			if (i == RTAX_CC_ALGO - 1) {
675 				char tmp[TCP_CA_NAME_MAX], *name;
676 
677 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
678 				if (!name)
679 					continue;
680 				if (nla_put_string(skb, i + 1, name))
681 					goto nla_put_failure;
682 			} else if (i == RTAX_FEATURES - 1) {
683 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
684 
685 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
686 				if (nla_put_u32(skb, i + 1, user_features))
687 					goto nla_put_failure;
688 			} else {
689 				if (nla_put_u32(skb, i + 1, metrics[i]))
690 					goto nla_put_failure;
691 			}
692 			valid++;
693 		}
694 	}
695 
696 	if (!valid) {
697 		nla_nest_cancel(skb, mx);
698 		return 0;
699 	}
700 
701 	return nla_nest_end(skb, mx);
702 
703 nla_put_failure:
704 	nla_nest_cancel(skb, mx);
705 	return -EMSGSIZE;
706 }
707 EXPORT_SYMBOL(rtnetlink_put_metrics);
708 
709 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
710 		       long expires, u32 error)
711 {
712 	struct rta_cacheinfo ci = {
713 		.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse),
714 		.rta_used = dst->__use,
715 		.rta_clntref = atomic_read(&(dst->__refcnt)),
716 		.rta_error = error,
717 		.rta_id =  id,
718 	};
719 
720 	if (expires) {
721 		unsigned long clock;
722 
723 		clock = jiffies_to_clock_t(abs(expires));
724 		clock = min_t(unsigned long, clock, INT_MAX);
725 		ci.rta_expires = (expires > 0) ? clock : -clock;
726 	}
727 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
728 }
729 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
730 
731 static void set_operstate(struct net_device *dev, unsigned char transition)
732 {
733 	unsigned char operstate = dev->operstate;
734 
735 	switch (transition) {
736 	case IF_OPER_UP:
737 		if ((operstate == IF_OPER_DORMANT ||
738 		     operstate == IF_OPER_UNKNOWN) &&
739 		    !netif_dormant(dev))
740 			operstate = IF_OPER_UP;
741 		break;
742 
743 	case IF_OPER_DORMANT:
744 		if (operstate == IF_OPER_UP ||
745 		    operstate == IF_OPER_UNKNOWN)
746 			operstate = IF_OPER_DORMANT;
747 		break;
748 	}
749 
750 	if (dev->operstate != operstate) {
751 		write_lock_bh(&dev_base_lock);
752 		dev->operstate = operstate;
753 		write_unlock_bh(&dev_base_lock);
754 		netdev_state_change(dev);
755 	}
756 }
757 
758 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
759 {
760 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
761 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
762 }
763 
764 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
765 					   const struct ifinfomsg *ifm)
766 {
767 	unsigned int flags = ifm->ifi_flags;
768 
769 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
770 	if (ifm->ifi_change)
771 		flags = (flags & ifm->ifi_change) |
772 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
773 
774 	return flags;
775 }
776 
777 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
778 				 const struct rtnl_link_stats64 *b)
779 {
780 	a->rx_packets = b->rx_packets;
781 	a->tx_packets = b->tx_packets;
782 	a->rx_bytes = b->rx_bytes;
783 	a->tx_bytes = b->tx_bytes;
784 	a->rx_errors = b->rx_errors;
785 	a->tx_errors = b->tx_errors;
786 	a->rx_dropped = b->rx_dropped;
787 	a->tx_dropped = b->tx_dropped;
788 
789 	a->multicast = b->multicast;
790 	a->collisions = b->collisions;
791 
792 	a->rx_length_errors = b->rx_length_errors;
793 	a->rx_over_errors = b->rx_over_errors;
794 	a->rx_crc_errors = b->rx_crc_errors;
795 	a->rx_frame_errors = b->rx_frame_errors;
796 	a->rx_fifo_errors = b->rx_fifo_errors;
797 	a->rx_missed_errors = b->rx_missed_errors;
798 
799 	a->tx_aborted_errors = b->tx_aborted_errors;
800 	a->tx_carrier_errors = b->tx_carrier_errors;
801 	a->tx_fifo_errors = b->tx_fifo_errors;
802 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
803 	a->tx_window_errors = b->tx_window_errors;
804 
805 	a->rx_compressed = b->rx_compressed;
806 	a->tx_compressed = b->tx_compressed;
807 }
808 
809 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
810 {
811 	memcpy(v, b, sizeof(*b));
812 }
813 
814 /* All VF info */
815 static inline int rtnl_vfinfo_size(const struct net_device *dev,
816 				   u32 ext_filter_mask)
817 {
818 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
819 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
820 		int num_vfs = dev_num_vf(dev->dev.parent);
821 		size_t size = nla_total_size(sizeof(struct nlattr));
822 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
823 		size += num_vfs *
824 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
825 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
826 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
827 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
828 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
829 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
830 			 /* IFLA_VF_STATS_RX_PACKETS */
831 			 nla_total_size(sizeof(__u64)) +
832 			 /* IFLA_VF_STATS_TX_PACKETS */
833 			 nla_total_size(sizeof(__u64)) +
834 			 /* IFLA_VF_STATS_RX_BYTES */
835 			 nla_total_size(sizeof(__u64)) +
836 			 /* IFLA_VF_STATS_TX_BYTES */
837 			 nla_total_size(sizeof(__u64)) +
838 			 /* IFLA_VF_STATS_BROADCAST */
839 			 nla_total_size(sizeof(__u64)) +
840 			 /* IFLA_VF_STATS_MULTICAST */
841 			 nla_total_size(sizeof(__u64)) +
842 			 nla_total_size(sizeof(struct ifla_vf_trust)));
843 		return size;
844 	} else
845 		return 0;
846 }
847 
848 static size_t rtnl_port_size(const struct net_device *dev,
849 			     u32 ext_filter_mask)
850 {
851 	size_t port_size = nla_total_size(4)		/* PORT_VF */
852 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
853 		+ nla_total_size(sizeof(struct ifla_port_vsi))
854 							/* PORT_VSI_TYPE */
855 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
856 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
857 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
858 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
859 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
860 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
861 		+ port_size;
862 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
863 		+ port_size;
864 
865 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
866 	    !(ext_filter_mask & RTEXT_FILTER_VF))
867 		return 0;
868 	if (dev_num_vf(dev->dev.parent))
869 		return port_self_size + vf_ports_size +
870 			vf_port_size * dev_num_vf(dev->dev.parent);
871 	else
872 		return port_self_size;
873 }
874 
875 static noinline size_t if_nlmsg_size(const struct net_device *dev,
876 				     u32 ext_filter_mask)
877 {
878 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
879 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
880 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
881 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
882 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
883 	       + nla_total_size(sizeof(struct rtnl_link_stats))
884 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
885 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
886 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
887 	       + nla_total_size(4) /* IFLA_TXQLEN */
888 	       + nla_total_size(4) /* IFLA_WEIGHT */
889 	       + nla_total_size(4) /* IFLA_MTU */
890 	       + nla_total_size(4) /* IFLA_LINK */
891 	       + nla_total_size(4) /* IFLA_MASTER */
892 	       + nla_total_size(1) /* IFLA_CARRIER */
893 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
894 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
895 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
896 	       + nla_total_size(1) /* IFLA_OPERSTATE */
897 	       + nla_total_size(1) /* IFLA_LINKMODE */
898 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
899 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
900 	       + nla_total_size(ext_filter_mask
901 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
902 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
903 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
904 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
905 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
906 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
907 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
908 	       + nla_total_size(1); /* IFLA_PROTO_DOWN */
909 
910 }
911 
912 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
913 {
914 	struct nlattr *vf_ports;
915 	struct nlattr *vf_port;
916 	int vf;
917 	int err;
918 
919 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
920 	if (!vf_ports)
921 		return -EMSGSIZE;
922 
923 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
924 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
925 		if (!vf_port)
926 			goto nla_put_failure;
927 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
928 			goto nla_put_failure;
929 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
930 		if (err == -EMSGSIZE)
931 			goto nla_put_failure;
932 		if (err) {
933 			nla_nest_cancel(skb, vf_port);
934 			continue;
935 		}
936 		nla_nest_end(skb, vf_port);
937 	}
938 
939 	nla_nest_end(skb, vf_ports);
940 
941 	return 0;
942 
943 nla_put_failure:
944 	nla_nest_cancel(skb, vf_ports);
945 	return -EMSGSIZE;
946 }
947 
948 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
949 {
950 	struct nlattr *port_self;
951 	int err;
952 
953 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
954 	if (!port_self)
955 		return -EMSGSIZE;
956 
957 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
958 	if (err) {
959 		nla_nest_cancel(skb, port_self);
960 		return (err == -EMSGSIZE) ? err : 0;
961 	}
962 
963 	nla_nest_end(skb, port_self);
964 
965 	return 0;
966 }
967 
968 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
969 			  u32 ext_filter_mask)
970 {
971 	int err;
972 
973 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
974 	    !(ext_filter_mask & RTEXT_FILTER_VF))
975 		return 0;
976 
977 	err = rtnl_port_self_fill(skb, dev);
978 	if (err)
979 		return err;
980 
981 	if (dev_num_vf(dev->dev.parent)) {
982 		err = rtnl_vf_ports_fill(skb, dev);
983 		if (err)
984 			return err;
985 	}
986 
987 	return 0;
988 }
989 
990 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
991 {
992 	int err;
993 	struct netdev_phys_item_id ppid;
994 
995 	err = dev_get_phys_port_id(dev, &ppid);
996 	if (err) {
997 		if (err == -EOPNOTSUPP)
998 			return 0;
999 		return err;
1000 	}
1001 
1002 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1003 		return -EMSGSIZE;
1004 
1005 	return 0;
1006 }
1007 
1008 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1009 {
1010 	char name[IFNAMSIZ];
1011 	int err;
1012 
1013 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1014 	if (err) {
1015 		if (err == -EOPNOTSUPP)
1016 			return 0;
1017 		return err;
1018 	}
1019 
1020 	if (nla_put(skb, IFLA_PHYS_PORT_NAME, strlen(name), name))
1021 		return -EMSGSIZE;
1022 
1023 	return 0;
1024 }
1025 
1026 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1027 {
1028 	int err;
1029 	struct switchdev_attr attr = {
1030 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1031 		.flags = SWITCHDEV_F_NO_RECURSE,
1032 	};
1033 
1034 	err = switchdev_port_attr_get(dev, &attr);
1035 	if (err) {
1036 		if (err == -EOPNOTSUPP)
1037 			return 0;
1038 		return err;
1039 	}
1040 
1041 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1042 		    attr.u.ppid.id))
1043 		return -EMSGSIZE;
1044 
1045 	return 0;
1046 }
1047 
1048 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1049 					      struct net_device *dev)
1050 {
1051 	const struct rtnl_link_stats64 *stats;
1052 	struct rtnl_link_stats64 temp;
1053 	struct nlattr *attr;
1054 
1055 	stats = dev_get_stats(dev, &temp);
1056 
1057 	attr = nla_reserve(skb, IFLA_STATS,
1058 			   sizeof(struct rtnl_link_stats));
1059 	if (!attr)
1060 		return -EMSGSIZE;
1061 
1062 	copy_rtnl_link_stats(nla_data(attr), stats);
1063 
1064 	attr = nla_reserve(skb, IFLA_STATS64,
1065 			   sizeof(struct rtnl_link_stats64));
1066 	if (!attr)
1067 		return -EMSGSIZE;
1068 
1069 	copy_rtnl_link_stats64(nla_data(attr), stats);
1070 
1071 	return 0;
1072 }
1073 
1074 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1075 					       struct net_device *dev,
1076 					       int vfs_num,
1077 					       struct nlattr *vfinfo)
1078 {
1079 	struct ifla_vf_rss_query_en vf_rss_query_en;
1080 	struct ifla_vf_link_state vf_linkstate;
1081 	struct ifla_vf_spoofchk vf_spoofchk;
1082 	struct ifla_vf_tx_rate vf_tx_rate;
1083 	struct ifla_vf_stats vf_stats;
1084 	struct ifla_vf_trust vf_trust;
1085 	struct ifla_vf_vlan vf_vlan;
1086 	struct ifla_vf_rate vf_rate;
1087 	struct nlattr *vf, *vfstats;
1088 	struct ifla_vf_mac vf_mac;
1089 	struct ifla_vf_info ivi;
1090 
1091 	/* Not all SR-IOV capable drivers support the
1092 	 * spoofcheck and "RSS query enable" query.  Preset to
1093 	 * -1 so the user space tool can detect that the driver
1094 	 * didn't report anything.
1095 	 */
1096 	ivi.spoofchk = -1;
1097 	ivi.rss_query_en = -1;
1098 	ivi.trusted = -1;
1099 	memset(ivi.mac, 0, sizeof(ivi.mac));
1100 	/* The default value for VF link state is "auto"
1101 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1102 	 */
1103 	ivi.linkstate = 0;
1104 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1105 		return 0;
1106 
1107 	vf_mac.vf =
1108 		vf_vlan.vf =
1109 		vf_rate.vf =
1110 		vf_tx_rate.vf =
1111 		vf_spoofchk.vf =
1112 		vf_linkstate.vf =
1113 		vf_rss_query_en.vf =
1114 		vf_trust.vf = ivi.vf;
1115 
1116 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1117 	vf_vlan.vlan = ivi.vlan;
1118 	vf_vlan.qos = ivi.qos;
1119 	vf_tx_rate.rate = ivi.max_tx_rate;
1120 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1121 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1122 	vf_spoofchk.setting = ivi.spoofchk;
1123 	vf_linkstate.link_state = ivi.linkstate;
1124 	vf_rss_query_en.setting = ivi.rss_query_en;
1125 	vf_trust.setting = ivi.trusted;
1126 	vf = nla_nest_start(skb, IFLA_VF_INFO);
1127 	if (!vf) {
1128 		nla_nest_cancel(skb, vfinfo);
1129 		return -EMSGSIZE;
1130 	}
1131 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1132 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1133 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1134 		    &vf_rate) ||
1135 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1136 		    &vf_tx_rate) ||
1137 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1138 		    &vf_spoofchk) ||
1139 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1140 		    &vf_linkstate) ||
1141 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1142 		    sizeof(vf_rss_query_en),
1143 		    &vf_rss_query_en) ||
1144 	    nla_put(skb, IFLA_VF_TRUST,
1145 		    sizeof(vf_trust), &vf_trust))
1146 		return -EMSGSIZE;
1147 	memset(&vf_stats, 0, sizeof(vf_stats));
1148 	if (dev->netdev_ops->ndo_get_vf_stats)
1149 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1150 						&vf_stats);
1151 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1152 	if (!vfstats) {
1153 		nla_nest_cancel(skb, vf);
1154 		nla_nest_cancel(skb, vfinfo);
1155 		return -EMSGSIZE;
1156 	}
1157 	if (nla_put_u64(skb, IFLA_VF_STATS_RX_PACKETS,
1158 			vf_stats.rx_packets) ||
1159 	    nla_put_u64(skb, IFLA_VF_STATS_TX_PACKETS,
1160 			vf_stats.tx_packets) ||
1161 	    nla_put_u64(skb, IFLA_VF_STATS_RX_BYTES,
1162 			vf_stats.rx_bytes) ||
1163 	    nla_put_u64(skb, IFLA_VF_STATS_TX_BYTES,
1164 			vf_stats.tx_bytes) ||
1165 	    nla_put_u64(skb, IFLA_VF_STATS_BROADCAST,
1166 			vf_stats.broadcast) ||
1167 	    nla_put_u64(skb, IFLA_VF_STATS_MULTICAST,
1168 			vf_stats.multicast))
1169 		return -EMSGSIZE;
1170 	nla_nest_end(skb, vfstats);
1171 	nla_nest_end(skb, vf);
1172 	return 0;
1173 }
1174 
1175 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1176 {
1177 	struct rtnl_link_ifmap map = {
1178 		.mem_start   = dev->mem_start,
1179 		.mem_end     = dev->mem_end,
1180 		.base_addr   = dev->base_addr,
1181 		.irq         = dev->irq,
1182 		.dma         = dev->dma,
1183 		.port        = dev->if_port,
1184 	};
1185 	if (nla_put(skb, IFLA_MAP, sizeof(map), &map))
1186 		return -EMSGSIZE;
1187 
1188 	return 0;
1189 }
1190 
1191 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1192 			    int type, u32 pid, u32 seq, u32 change,
1193 			    unsigned int flags, u32 ext_filter_mask)
1194 {
1195 	struct ifinfomsg *ifm;
1196 	struct nlmsghdr *nlh;
1197 	struct nlattr *af_spec;
1198 	struct rtnl_af_ops *af_ops;
1199 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1200 
1201 	ASSERT_RTNL();
1202 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1203 	if (nlh == NULL)
1204 		return -EMSGSIZE;
1205 
1206 	ifm = nlmsg_data(nlh);
1207 	ifm->ifi_family = AF_UNSPEC;
1208 	ifm->__ifi_pad = 0;
1209 	ifm->ifi_type = dev->type;
1210 	ifm->ifi_index = dev->ifindex;
1211 	ifm->ifi_flags = dev_get_flags(dev);
1212 	ifm->ifi_change = change;
1213 
1214 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1215 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1216 	    nla_put_u8(skb, IFLA_OPERSTATE,
1217 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1218 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1219 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1220 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1221 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1222 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1223 #ifdef CONFIG_RPS
1224 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1225 #endif
1226 	    (dev->ifindex != dev_get_iflink(dev) &&
1227 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1228 	    (upper_dev &&
1229 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1230 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1231 	    (dev->qdisc &&
1232 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1233 	    (dev->ifalias &&
1234 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1235 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1236 			atomic_read(&dev->carrier_changes)) ||
1237 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1238 		goto nla_put_failure;
1239 
1240 	if (rtnl_fill_link_ifmap(skb, dev))
1241 		goto nla_put_failure;
1242 
1243 	if (dev->addr_len) {
1244 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1245 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1246 			goto nla_put_failure;
1247 	}
1248 
1249 	if (rtnl_phys_port_id_fill(skb, dev))
1250 		goto nla_put_failure;
1251 
1252 	if (rtnl_phys_port_name_fill(skb, dev))
1253 		goto nla_put_failure;
1254 
1255 	if (rtnl_phys_switch_id_fill(skb, dev))
1256 		goto nla_put_failure;
1257 
1258 	if (rtnl_fill_stats(skb, dev))
1259 		goto nla_put_failure;
1260 
1261 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1262 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1263 		goto nla_put_failure;
1264 
1265 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1266 	    ext_filter_mask & RTEXT_FILTER_VF) {
1267 		int i;
1268 		struct nlattr *vfinfo;
1269 		int num_vfs = dev_num_vf(dev->dev.parent);
1270 
1271 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1272 		if (!vfinfo)
1273 			goto nla_put_failure;
1274 		for (i = 0; i < num_vfs; i++) {
1275 			if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1276 				goto nla_put_failure;
1277 		}
1278 
1279 		nla_nest_end(skb, vfinfo);
1280 	}
1281 
1282 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1283 		goto nla_put_failure;
1284 
1285 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1286 		if (rtnl_link_fill(skb, dev) < 0)
1287 			goto nla_put_failure;
1288 	}
1289 
1290 	if (dev->rtnl_link_ops &&
1291 	    dev->rtnl_link_ops->get_link_net) {
1292 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1293 
1294 		if (!net_eq(dev_net(dev), link_net)) {
1295 			int id = peernet2id_alloc(dev_net(dev), link_net);
1296 
1297 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1298 				goto nla_put_failure;
1299 		}
1300 	}
1301 
1302 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1303 		goto nla_put_failure;
1304 
1305 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1306 		if (af_ops->fill_link_af) {
1307 			struct nlattr *af;
1308 			int err;
1309 
1310 			if (!(af = nla_nest_start(skb, af_ops->family)))
1311 				goto nla_put_failure;
1312 
1313 			err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1314 
1315 			/*
1316 			 * Caller may return ENODATA to indicate that there
1317 			 * was no data to be dumped. This is not an error, it
1318 			 * means we should trim the attribute header and
1319 			 * continue.
1320 			 */
1321 			if (err == -ENODATA)
1322 				nla_nest_cancel(skb, af);
1323 			else if (err < 0)
1324 				goto nla_put_failure;
1325 
1326 			nla_nest_end(skb, af);
1327 		}
1328 	}
1329 
1330 	nla_nest_end(skb, af_spec);
1331 
1332 	nlmsg_end(skb, nlh);
1333 	return 0;
1334 
1335 nla_put_failure:
1336 	nlmsg_cancel(skb, nlh);
1337 	return -EMSGSIZE;
1338 }
1339 
1340 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1341 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1342 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1343 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1344 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1345 	[IFLA_MTU]		= { .type = NLA_U32 },
1346 	[IFLA_LINK]		= { .type = NLA_U32 },
1347 	[IFLA_MASTER]		= { .type = NLA_U32 },
1348 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1349 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1350 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1351 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1352 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1353 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1354 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1355 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1356 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1357 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1358 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1359 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1360 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1361 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1362 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1363 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1364 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1365 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1366 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1367 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1368 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
1369 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1370 };
1371 
1372 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1373 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1374 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1375 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1376 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1377 };
1378 
1379 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1380 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1381 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1382 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1383 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1384 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1385 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
1386 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
1387 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1388 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1389 };
1390 
1391 static const struct nla_policy ifla_vf_stats_policy[IFLA_VF_STATS_MAX + 1] = {
1392 	[IFLA_VF_STATS_RX_PACKETS]	= { .type = NLA_U64 },
1393 	[IFLA_VF_STATS_TX_PACKETS]	= { .type = NLA_U64 },
1394 	[IFLA_VF_STATS_RX_BYTES]	= { .type = NLA_U64 },
1395 	[IFLA_VF_STATS_TX_BYTES]	= { .type = NLA_U64 },
1396 	[IFLA_VF_STATS_BROADCAST]	= { .type = NLA_U64 },
1397 	[IFLA_VF_STATS_MULTICAST]	= { .type = NLA_U64 },
1398 };
1399 
1400 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1401 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1402 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1403 				    .len = PORT_PROFILE_MAX },
1404 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1405 				    .len = sizeof(struct ifla_port_vsi)},
1406 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1407 				      .len = PORT_UUID_MAX },
1408 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1409 				    .len = PORT_UUID_MAX },
1410 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1411 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1412 };
1413 
1414 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1415 {
1416 	struct net *net = sock_net(skb->sk);
1417 	int h, s_h;
1418 	int idx = 0, s_idx;
1419 	struct net_device *dev;
1420 	struct hlist_head *head;
1421 	struct nlattr *tb[IFLA_MAX+1];
1422 	u32 ext_filter_mask = 0;
1423 	int err;
1424 	int hdrlen;
1425 
1426 	s_h = cb->args[0];
1427 	s_idx = cb->args[1];
1428 
1429 	cb->seq = net->dev_base_seq;
1430 
1431 	/* A hack to preserve kernel<->userspace interface.
1432 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1433 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1434 	 * what iproute2 < v3.9.0 used.
1435 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1436 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1437 	 */
1438 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1439 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1440 
1441 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1442 
1443 		if (tb[IFLA_EXT_MASK])
1444 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1445 	}
1446 
1447 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1448 		idx = 0;
1449 		head = &net->dev_index_head[h];
1450 		hlist_for_each_entry(dev, head, index_hlist) {
1451 			if (idx < s_idx)
1452 				goto cont;
1453 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1454 					       NETLINK_CB(cb->skb).portid,
1455 					       cb->nlh->nlmsg_seq, 0,
1456 					       NLM_F_MULTI,
1457 					       ext_filter_mask);
1458 			/* If we ran out of room on the first message,
1459 			 * we're in trouble
1460 			 */
1461 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1462 
1463 			if (err < 0)
1464 				goto out;
1465 
1466 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1467 cont:
1468 			idx++;
1469 		}
1470 	}
1471 out:
1472 	cb->args[1] = idx;
1473 	cb->args[0] = h;
1474 
1475 	return skb->len;
1476 }
1477 
1478 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1479 {
1480 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1481 }
1482 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1483 
1484 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1485 {
1486 	struct net *net;
1487 	/* Examine the link attributes and figure out which
1488 	 * network namespace we are talking about.
1489 	 */
1490 	if (tb[IFLA_NET_NS_PID])
1491 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1492 	else if (tb[IFLA_NET_NS_FD])
1493 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1494 	else
1495 		net = get_net(src_net);
1496 	return net;
1497 }
1498 EXPORT_SYMBOL(rtnl_link_get_net);
1499 
1500 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1501 {
1502 	if (dev) {
1503 		if (tb[IFLA_ADDRESS] &&
1504 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1505 			return -EINVAL;
1506 
1507 		if (tb[IFLA_BROADCAST] &&
1508 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1509 			return -EINVAL;
1510 	}
1511 
1512 	if (tb[IFLA_AF_SPEC]) {
1513 		struct nlattr *af;
1514 		int rem, err;
1515 
1516 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1517 			const struct rtnl_af_ops *af_ops;
1518 
1519 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1520 				return -EAFNOSUPPORT;
1521 
1522 			if (!af_ops->set_link_af)
1523 				return -EOPNOTSUPP;
1524 
1525 			if (af_ops->validate_link_af) {
1526 				err = af_ops->validate_link_af(dev, af);
1527 				if (err < 0)
1528 					return err;
1529 			}
1530 		}
1531 	}
1532 
1533 	return 0;
1534 }
1535 
1536 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1537 {
1538 	const struct net_device_ops *ops = dev->netdev_ops;
1539 	int err = -EINVAL;
1540 
1541 	if (tb[IFLA_VF_MAC]) {
1542 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1543 
1544 		err = -EOPNOTSUPP;
1545 		if (ops->ndo_set_vf_mac)
1546 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1547 						  ivm->mac);
1548 		if (err < 0)
1549 			return err;
1550 	}
1551 
1552 	if (tb[IFLA_VF_VLAN]) {
1553 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1554 
1555 		err = -EOPNOTSUPP;
1556 		if (ops->ndo_set_vf_vlan)
1557 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1558 						   ivv->qos);
1559 		if (err < 0)
1560 			return err;
1561 	}
1562 
1563 	if (tb[IFLA_VF_TX_RATE]) {
1564 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1565 		struct ifla_vf_info ivf;
1566 
1567 		err = -EOPNOTSUPP;
1568 		if (ops->ndo_get_vf_config)
1569 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1570 		if (err < 0)
1571 			return err;
1572 
1573 		err = -EOPNOTSUPP;
1574 		if (ops->ndo_set_vf_rate)
1575 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1576 						   ivf.min_tx_rate,
1577 						   ivt->rate);
1578 		if (err < 0)
1579 			return err;
1580 	}
1581 
1582 	if (tb[IFLA_VF_RATE]) {
1583 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1584 
1585 		err = -EOPNOTSUPP;
1586 		if (ops->ndo_set_vf_rate)
1587 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1588 						   ivt->min_tx_rate,
1589 						   ivt->max_tx_rate);
1590 		if (err < 0)
1591 			return err;
1592 	}
1593 
1594 	if (tb[IFLA_VF_SPOOFCHK]) {
1595 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1596 
1597 		err = -EOPNOTSUPP;
1598 		if (ops->ndo_set_vf_spoofchk)
1599 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1600 						       ivs->setting);
1601 		if (err < 0)
1602 			return err;
1603 	}
1604 
1605 	if (tb[IFLA_VF_LINK_STATE]) {
1606 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1607 
1608 		err = -EOPNOTSUPP;
1609 		if (ops->ndo_set_vf_link_state)
1610 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1611 							 ivl->link_state);
1612 		if (err < 0)
1613 			return err;
1614 	}
1615 
1616 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
1617 		struct ifla_vf_rss_query_en *ivrssq_en;
1618 
1619 		err = -EOPNOTSUPP;
1620 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1621 		if (ops->ndo_set_vf_rss_query_en)
1622 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1623 							   ivrssq_en->setting);
1624 		if (err < 0)
1625 			return err;
1626 	}
1627 
1628 	if (tb[IFLA_VF_TRUST]) {
1629 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1630 
1631 		err = -EOPNOTSUPP;
1632 		if (ops->ndo_set_vf_trust)
1633 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1634 		if (err < 0)
1635 			return err;
1636 	}
1637 
1638 	return err;
1639 }
1640 
1641 static int do_set_master(struct net_device *dev, int ifindex)
1642 {
1643 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1644 	const struct net_device_ops *ops;
1645 	int err;
1646 
1647 	if (upper_dev) {
1648 		if (upper_dev->ifindex == ifindex)
1649 			return 0;
1650 		ops = upper_dev->netdev_ops;
1651 		if (ops->ndo_del_slave) {
1652 			err = ops->ndo_del_slave(upper_dev, dev);
1653 			if (err)
1654 				return err;
1655 		} else {
1656 			return -EOPNOTSUPP;
1657 		}
1658 	}
1659 
1660 	if (ifindex) {
1661 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1662 		if (!upper_dev)
1663 			return -EINVAL;
1664 		ops = upper_dev->netdev_ops;
1665 		if (ops->ndo_add_slave) {
1666 			err = ops->ndo_add_slave(upper_dev, dev);
1667 			if (err)
1668 				return err;
1669 		} else {
1670 			return -EOPNOTSUPP;
1671 		}
1672 	}
1673 	return 0;
1674 }
1675 
1676 #define DO_SETLINK_MODIFIED	0x01
1677 /* notify flag means notify + modified. */
1678 #define DO_SETLINK_NOTIFY	0x03
1679 static int do_setlink(const struct sk_buff *skb,
1680 		      struct net_device *dev, struct ifinfomsg *ifm,
1681 		      struct nlattr **tb, char *ifname, int status)
1682 {
1683 	const struct net_device_ops *ops = dev->netdev_ops;
1684 	int err;
1685 
1686 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1687 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1688 		if (IS_ERR(net)) {
1689 			err = PTR_ERR(net);
1690 			goto errout;
1691 		}
1692 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1693 			put_net(net);
1694 			err = -EPERM;
1695 			goto errout;
1696 		}
1697 		err = dev_change_net_namespace(dev, net, ifname);
1698 		put_net(net);
1699 		if (err)
1700 			goto errout;
1701 		status |= DO_SETLINK_MODIFIED;
1702 	}
1703 
1704 	if (tb[IFLA_MAP]) {
1705 		struct rtnl_link_ifmap *u_map;
1706 		struct ifmap k_map;
1707 
1708 		if (!ops->ndo_set_config) {
1709 			err = -EOPNOTSUPP;
1710 			goto errout;
1711 		}
1712 
1713 		if (!netif_device_present(dev)) {
1714 			err = -ENODEV;
1715 			goto errout;
1716 		}
1717 
1718 		u_map = nla_data(tb[IFLA_MAP]);
1719 		k_map.mem_start = (unsigned long) u_map->mem_start;
1720 		k_map.mem_end = (unsigned long) u_map->mem_end;
1721 		k_map.base_addr = (unsigned short) u_map->base_addr;
1722 		k_map.irq = (unsigned char) u_map->irq;
1723 		k_map.dma = (unsigned char) u_map->dma;
1724 		k_map.port = (unsigned char) u_map->port;
1725 
1726 		err = ops->ndo_set_config(dev, &k_map);
1727 		if (err < 0)
1728 			goto errout;
1729 
1730 		status |= DO_SETLINK_NOTIFY;
1731 	}
1732 
1733 	if (tb[IFLA_ADDRESS]) {
1734 		struct sockaddr *sa;
1735 		int len;
1736 
1737 		len = sizeof(sa_family_t) + dev->addr_len;
1738 		sa = kmalloc(len, GFP_KERNEL);
1739 		if (!sa) {
1740 			err = -ENOMEM;
1741 			goto errout;
1742 		}
1743 		sa->sa_family = dev->type;
1744 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1745 		       dev->addr_len);
1746 		err = dev_set_mac_address(dev, sa);
1747 		kfree(sa);
1748 		if (err)
1749 			goto errout;
1750 		status |= DO_SETLINK_MODIFIED;
1751 	}
1752 
1753 	if (tb[IFLA_MTU]) {
1754 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1755 		if (err < 0)
1756 			goto errout;
1757 		status |= DO_SETLINK_MODIFIED;
1758 	}
1759 
1760 	if (tb[IFLA_GROUP]) {
1761 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1762 		status |= DO_SETLINK_NOTIFY;
1763 	}
1764 
1765 	/*
1766 	 * Interface selected by interface index but interface
1767 	 * name provided implies that a name change has been
1768 	 * requested.
1769 	 */
1770 	if (ifm->ifi_index > 0 && ifname[0]) {
1771 		err = dev_change_name(dev, ifname);
1772 		if (err < 0)
1773 			goto errout;
1774 		status |= DO_SETLINK_MODIFIED;
1775 	}
1776 
1777 	if (tb[IFLA_IFALIAS]) {
1778 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1779 				    nla_len(tb[IFLA_IFALIAS]));
1780 		if (err < 0)
1781 			goto errout;
1782 		status |= DO_SETLINK_NOTIFY;
1783 	}
1784 
1785 	if (tb[IFLA_BROADCAST]) {
1786 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1787 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1788 	}
1789 
1790 	if (ifm->ifi_flags || ifm->ifi_change) {
1791 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1792 		if (err < 0)
1793 			goto errout;
1794 	}
1795 
1796 	if (tb[IFLA_MASTER]) {
1797 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1798 		if (err)
1799 			goto errout;
1800 		status |= DO_SETLINK_MODIFIED;
1801 	}
1802 
1803 	if (tb[IFLA_CARRIER]) {
1804 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1805 		if (err)
1806 			goto errout;
1807 		status |= DO_SETLINK_MODIFIED;
1808 	}
1809 
1810 	if (tb[IFLA_TXQLEN]) {
1811 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1812 
1813 		if (dev->tx_queue_len ^ value)
1814 			status |= DO_SETLINK_NOTIFY;
1815 
1816 		dev->tx_queue_len = value;
1817 	}
1818 
1819 	if (tb[IFLA_OPERSTATE])
1820 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1821 
1822 	if (tb[IFLA_LINKMODE]) {
1823 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1824 
1825 		write_lock_bh(&dev_base_lock);
1826 		if (dev->link_mode ^ value)
1827 			status |= DO_SETLINK_NOTIFY;
1828 		dev->link_mode = value;
1829 		write_unlock_bh(&dev_base_lock);
1830 	}
1831 
1832 	if (tb[IFLA_VFINFO_LIST]) {
1833 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
1834 		struct nlattr *attr;
1835 		int rem;
1836 
1837 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1838 			if (nla_type(attr) != IFLA_VF_INFO ||
1839 			    nla_len(attr) < NLA_HDRLEN) {
1840 				err = -EINVAL;
1841 				goto errout;
1842 			}
1843 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1844 					       ifla_vf_policy);
1845 			if (err < 0)
1846 				goto errout;
1847 			err = do_setvfinfo(dev, vfinfo);
1848 			if (err < 0)
1849 				goto errout;
1850 			status |= DO_SETLINK_NOTIFY;
1851 		}
1852 	}
1853 	err = 0;
1854 
1855 	if (tb[IFLA_VF_PORTS]) {
1856 		struct nlattr *port[IFLA_PORT_MAX+1];
1857 		struct nlattr *attr;
1858 		int vf;
1859 		int rem;
1860 
1861 		err = -EOPNOTSUPP;
1862 		if (!ops->ndo_set_vf_port)
1863 			goto errout;
1864 
1865 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1866 			if (nla_type(attr) != IFLA_VF_PORT ||
1867 			    nla_len(attr) < NLA_HDRLEN) {
1868 				err = -EINVAL;
1869 				goto errout;
1870 			}
1871 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1872 					       ifla_port_policy);
1873 			if (err < 0)
1874 				goto errout;
1875 			if (!port[IFLA_PORT_VF]) {
1876 				err = -EOPNOTSUPP;
1877 				goto errout;
1878 			}
1879 			vf = nla_get_u32(port[IFLA_PORT_VF]);
1880 			err = ops->ndo_set_vf_port(dev, vf, port);
1881 			if (err < 0)
1882 				goto errout;
1883 			status |= DO_SETLINK_NOTIFY;
1884 		}
1885 	}
1886 	err = 0;
1887 
1888 	if (tb[IFLA_PORT_SELF]) {
1889 		struct nlattr *port[IFLA_PORT_MAX+1];
1890 
1891 		err = nla_parse_nested(port, IFLA_PORT_MAX,
1892 			tb[IFLA_PORT_SELF], ifla_port_policy);
1893 		if (err < 0)
1894 			goto errout;
1895 
1896 		err = -EOPNOTSUPP;
1897 		if (ops->ndo_set_vf_port)
1898 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1899 		if (err < 0)
1900 			goto errout;
1901 		status |= DO_SETLINK_NOTIFY;
1902 	}
1903 
1904 	if (tb[IFLA_AF_SPEC]) {
1905 		struct nlattr *af;
1906 		int rem;
1907 
1908 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1909 			const struct rtnl_af_ops *af_ops;
1910 
1911 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1912 				BUG();
1913 
1914 			err = af_ops->set_link_af(dev, af);
1915 			if (err < 0)
1916 				goto errout;
1917 
1918 			status |= DO_SETLINK_NOTIFY;
1919 		}
1920 	}
1921 	err = 0;
1922 
1923 	if (tb[IFLA_PROTO_DOWN]) {
1924 		err = dev_change_proto_down(dev,
1925 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
1926 		if (err)
1927 			goto errout;
1928 		status |= DO_SETLINK_NOTIFY;
1929 	}
1930 
1931 errout:
1932 	if (status & DO_SETLINK_MODIFIED) {
1933 		if (status & DO_SETLINK_NOTIFY)
1934 			netdev_state_change(dev);
1935 
1936 		if (err < 0)
1937 			net_warn_ratelimited("A link change request failed with some changes committed already. Interface %s may have been left with an inconsistent configuration, please check.\n",
1938 					     dev->name);
1939 	}
1940 
1941 	return err;
1942 }
1943 
1944 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
1945 {
1946 	struct net *net = sock_net(skb->sk);
1947 	struct ifinfomsg *ifm;
1948 	struct net_device *dev;
1949 	int err;
1950 	struct nlattr *tb[IFLA_MAX+1];
1951 	char ifname[IFNAMSIZ];
1952 
1953 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1954 	if (err < 0)
1955 		goto errout;
1956 
1957 	if (tb[IFLA_IFNAME])
1958 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1959 	else
1960 		ifname[0] = '\0';
1961 
1962 	err = -EINVAL;
1963 	ifm = nlmsg_data(nlh);
1964 	if (ifm->ifi_index > 0)
1965 		dev = __dev_get_by_index(net, ifm->ifi_index);
1966 	else if (tb[IFLA_IFNAME])
1967 		dev = __dev_get_by_name(net, ifname);
1968 	else
1969 		goto errout;
1970 
1971 	if (dev == NULL) {
1972 		err = -ENODEV;
1973 		goto errout;
1974 	}
1975 
1976 	err = validate_linkmsg(dev, tb);
1977 	if (err < 0)
1978 		goto errout;
1979 
1980 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
1981 errout:
1982 	return err;
1983 }
1984 
1985 static int rtnl_group_dellink(const struct net *net, int group)
1986 {
1987 	struct net_device *dev, *aux;
1988 	LIST_HEAD(list_kill);
1989 	bool found = false;
1990 
1991 	if (!group)
1992 		return -EPERM;
1993 
1994 	for_each_netdev(net, dev) {
1995 		if (dev->group == group) {
1996 			const struct rtnl_link_ops *ops;
1997 
1998 			found = true;
1999 			ops = dev->rtnl_link_ops;
2000 			if (!ops || !ops->dellink)
2001 				return -EOPNOTSUPP;
2002 		}
2003 	}
2004 
2005 	if (!found)
2006 		return -ENODEV;
2007 
2008 	for_each_netdev_safe(net, dev, aux) {
2009 		if (dev->group == group) {
2010 			const struct rtnl_link_ops *ops;
2011 
2012 			ops = dev->rtnl_link_ops;
2013 			ops->dellink(dev, &list_kill);
2014 		}
2015 	}
2016 	unregister_netdevice_many(&list_kill);
2017 
2018 	return 0;
2019 }
2020 
2021 int rtnl_delete_link(struct net_device *dev)
2022 {
2023 	const struct rtnl_link_ops *ops;
2024 	LIST_HEAD(list_kill);
2025 
2026 	ops = dev->rtnl_link_ops;
2027 	if (!ops || !ops->dellink)
2028 		return -EOPNOTSUPP;
2029 
2030 	ops->dellink(dev, &list_kill);
2031 	unregister_netdevice_many(&list_kill);
2032 
2033 	return 0;
2034 }
2035 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2036 
2037 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2038 {
2039 	struct net *net = sock_net(skb->sk);
2040 	struct net_device *dev;
2041 	struct ifinfomsg *ifm;
2042 	char ifname[IFNAMSIZ];
2043 	struct nlattr *tb[IFLA_MAX+1];
2044 	int err;
2045 
2046 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2047 	if (err < 0)
2048 		return err;
2049 
2050 	if (tb[IFLA_IFNAME])
2051 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2052 
2053 	ifm = nlmsg_data(nlh);
2054 	if (ifm->ifi_index > 0)
2055 		dev = __dev_get_by_index(net, ifm->ifi_index);
2056 	else if (tb[IFLA_IFNAME])
2057 		dev = __dev_get_by_name(net, ifname);
2058 	else if (tb[IFLA_GROUP])
2059 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2060 	else
2061 		return -EINVAL;
2062 
2063 	if (!dev)
2064 		return -ENODEV;
2065 
2066 	return rtnl_delete_link(dev);
2067 }
2068 
2069 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2070 {
2071 	unsigned int old_flags;
2072 	int err;
2073 
2074 	old_flags = dev->flags;
2075 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2076 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2077 		if (err < 0)
2078 			return err;
2079 	}
2080 
2081 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2082 
2083 	__dev_notify_flags(dev, old_flags, ~0U);
2084 	return 0;
2085 }
2086 EXPORT_SYMBOL(rtnl_configure_link);
2087 
2088 struct net_device *rtnl_create_link(struct net *net,
2089 	const char *ifname, unsigned char name_assign_type,
2090 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2091 {
2092 	int err;
2093 	struct net_device *dev;
2094 	unsigned int num_tx_queues = 1;
2095 	unsigned int num_rx_queues = 1;
2096 
2097 	if (tb[IFLA_NUM_TX_QUEUES])
2098 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2099 	else if (ops->get_num_tx_queues)
2100 		num_tx_queues = ops->get_num_tx_queues();
2101 
2102 	if (tb[IFLA_NUM_RX_QUEUES])
2103 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2104 	else if (ops->get_num_rx_queues)
2105 		num_rx_queues = ops->get_num_rx_queues();
2106 
2107 	err = -ENOMEM;
2108 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2109 			       ops->setup, num_tx_queues, num_rx_queues);
2110 	if (!dev)
2111 		goto err;
2112 
2113 	dev_net_set(dev, net);
2114 	dev->rtnl_link_ops = ops;
2115 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2116 
2117 	if (tb[IFLA_MTU])
2118 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2119 	if (tb[IFLA_ADDRESS]) {
2120 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2121 				nla_len(tb[IFLA_ADDRESS]));
2122 		dev->addr_assign_type = NET_ADDR_SET;
2123 	}
2124 	if (tb[IFLA_BROADCAST])
2125 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2126 				nla_len(tb[IFLA_BROADCAST]));
2127 	if (tb[IFLA_TXQLEN])
2128 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2129 	if (tb[IFLA_OPERSTATE])
2130 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2131 	if (tb[IFLA_LINKMODE])
2132 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2133 	if (tb[IFLA_GROUP])
2134 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2135 
2136 	return dev;
2137 
2138 err:
2139 	return ERR_PTR(err);
2140 }
2141 EXPORT_SYMBOL(rtnl_create_link);
2142 
2143 static int rtnl_group_changelink(const struct sk_buff *skb,
2144 		struct net *net, int group,
2145 		struct ifinfomsg *ifm,
2146 		struct nlattr **tb)
2147 {
2148 	struct net_device *dev, *aux;
2149 	int err;
2150 
2151 	for_each_netdev_safe(net, dev, aux) {
2152 		if (dev->group == group) {
2153 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2154 			if (err < 0)
2155 				return err;
2156 		}
2157 	}
2158 
2159 	return 0;
2160 }
2161 
2162 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2163 {
2164 	struct net *net = sock_net(skb->sk);
2165 	const struct rtnl_link_ops *ops;
2166 	const struct rtnl_link_ops *m_ops = NULL;
2167 	struct net_device *dev;
2168 	struct net_device *master_dev = NULL;
2169 	struct ifinfomsg *ifm;
2170 	char kind[MODULE_NAME_LEN];
2171 	char ifname[IFNAMSIZ];
2172 	struct nlattr *tb[IFLA_MAX+1];
2173 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2174 	unsigned char name_assign_type = NET_NAME_USER;
2175 	int err;
2176 
2177 #ifdef CONFIG_MODULES
2178 replay:
2179 #endif
2180 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2181 	if (err < 0)
2182 		return err;
2183 
2184 	if (tb[IFLA_IFNAME])
2185 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2186 	else
2187 		ifname[0] = '\0';
2188 
2189 	ifm = nlmsg_data(nlh);
2190 	if (ifm->ifi_index > 0)
2191 		dev = __dev_get_by_index(net, ifm->ifi_index);
2192 	else {
2193 		if (ifname[0])
2194 			dev = __dev_get_by_name(net, ifname);
2195 		else
2196 			dev = NULL;
2197 	}
2198 
2199 	if (dev) {
2200 		master_dev = netdev_master_upper_dev_get(dev);
2201 		if (master_dev)
2202 			m_ops = master_dev->rtnl_link_ops;
2203 	}
2204 
2205 	err = validate_linkmsg(dev, tb);
2206 	if (err < 0)
2207 		return err;
2208 
2209 	if (tb[IFLA_LINKINFO]) {
2210 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2211 				       tb[IFLA_LINKINFO], ifla_info_policy);
2212 		if (err < 0)
2213 			return err;
2214 	} else
2215 		memset(linkinfo, 0, sizeof(linkinfo));
2216 
2217 	if (linkinfo[IFLA_INFO_KIND]) {
2218 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2219 		ops = rtnl_link_ops_get(kind);
2220 	} else {
2221 		kind[0] = '\0';
2222 		ops = NULL;
2223 	}
2224 
2225 	if (1) {
2226 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2227 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2228 		struct nlattr **data = NULL;
2229 		struct nlattr **slave_data = NULL;
2230 		struct net *dest_net, *link_net = NULL;
2231 
2232 		if (ops) {
2233 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2234 				err = nla_parse_nested(attr, ops->maxtype,
2235 						       linkinfo[IFLA_INFO_DATA],
2236 						       ops->policy);
2237 				if (err < 0)
2238 					return err;
2239 				data = attr;
2240 			}
2241 			if (ops->validate) {
2242 				err = ops->validate(tb, data);
2243 				if (err < 0)
2244 					return err;
2245 			}
2246 		}
2247 
2248 		if (m_ops) {
2249 			if (m_ops->slave_maxtype &&
2250 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2251 				err = nla_parse_nested(slave_attr,
2252 						       m_ops->slave_maxtype,
2253 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2254 						       m_ops->slave_policy);
2255 				if (err < 0)
2256 					return err;
2257 				slave_data = slave_attr;
2258 			}
2259 			if (m_ops->slave_validate) {
2260 				err = m_ops->slave_validate(tb, slave_data);
2261 				if (err < 0)
2262 					return err;
2263 			}
2264 		}
2265 
2266 		if (dev) {
2267 			int status = 0;
2268 
2269 			if (nlh->nlmsg_flags & NLM_F_EXCL)
2270 				return -EEXIST;
2271 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
2272 				return -EOPNOTSUPP;
2273 
2274 			if (linkinfo[IFLA_INFO_DATA]) {
2275 				if (!ops || ops != dev->rtnl_link_ops ||
2276 				    !ops->changelink)
2277 					return -EOPNOTSUPP;
2278 
2279 				err = ops->changelink(dev, tb, data);
2280 				if (err < 0)
2281 					return err;
2282 				status |= DO_SETLINK_NOTIFY;
2283 			}
2284 
2285 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2286 				if (!m_ops || !m_ops->slave_changelink)
2287 					return -EOPNOTSUPP;
2288 
2289 				err = m_ops->slave_changelink(master_dev, dev,
2290 							      tb, slave_data);
2291 				if (err < 0)
2292 					return err;
2293 				status |= DO_SETLINK_NOTIFY;
2294 			}
2295 
2296 			return do_setlink(skb, dev, ifm, tb, ifname, status);
2297 		}
2298 
2299 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2300 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2301 				return rtnl_group_changelink(skb, net,
2302 						nla_get_u32(tb[IFLA_GROUP]),
2303 						ifm, tb);
2304 			return -ENODEV;
2305 		}
2306 
2307 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2308 			return -EOPNOTSUPP;
2309 
2310 		if (!ops) {
2311 #ifdef CONFIG_MODULES
2312 			if (kind[0]) {
2313 				__rtnl_unlock();
2314 				request_module("rtnl-link-%s", kind);
2315 				rtnl_lock();
2316 				ops = rtnl_link_ops_get(kind);
2317 				if (ops)
2318 					goto replay;
2319 			}
2320 #endif
2321 			return -EOPNOTSUPP;
2322 		}
2323 
2324 		if (!ops->setup)
2325 			return -EOPNOTSUPP;
2326 
2327 		if (!ifname[0]) {
2328 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2329 			name_assign_type = NET_NAME_ENUM;
2330 		}
2331 
2332 		dest_net = rtnl_link_get_net(net, tb);
2333 		if (IS_ERR(dest_net))
2334 			return PTR_ERR(dest_net);
2335 
2336 		err = -EPERM;
2337 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2338 			goto out;
2339 
2340 		if (tb[IFLA_LINK_NETNSID]) {
2341 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2342 
2343 			link_net = get_net_ns_by_id(dest_net, id);
2344 			if (!link_net) {
2345 				err =  -EINVAL;
2346 				goto out;
2347 			}
2348 			err = -EPERM;
2349 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2350 				goto out;
2351 		}
2352 
2353 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2354 				       name_assign_type, ops, tb);
2355 		if (IS_ERR(dev)) {
2356 			err = PTR_ERR(dev);
2357 			goto out;
2358 		}
2359 
2360 		dev->ifindex = ifm->ifi_index;
2361 
2362 		if (ops->newlink) {
2363 			err = ops->newlink(link_net ? : net, dev, tb, data);
2364 			/* Drivers should call free_netdev() in ->destructor
2365 			 * and unregister it on failure after registration
2366 			 * so that device could be finally freed in rtnl_unlock.
2367 			 */
2368 			if (err < 0) {
2369 				/* If device is not registered at all, free it now */
2370 				if (dev->reg_state == NETREG_UNINITIALIZED)
2371 					free_netdev(dev);
2372 				goto out;
2373 			}
2374 		} else {
2375 			err = register_netdevice(dev);
2376 			if (err < 0) {
2377 				free_netdev(dev);
2378 				goto out;
2379 			}
2380 		}
2381 		err = rtnl_configure_link(dev, ifm);
2382 		if (err < 0)
2383 			goto out_unregister;
2384 		if (link_net) {
2385 			err = dev_change_net_namespace(dev, dest_net, ifname);
2386 			if (err < 0)
2387 				goto out_unregister;
2388 		}
2389 out:
2390 		if (link_net)
2391 			put_net(link_net);
2392 		put_net(dest_net);
2393 		return err;
2394 out_unregister:
2395 		if (ops->newlink) {
2396 			LIST_HEAD(list_kill);
2397 
2398 			ops->dellink(dev, &list_kill);
2399 			unregister_netdevice_many(&list_kill);
2400 		} else {
2401 			unregister_netdevice(dev);
2402 		}
2403 		goto out;
2404 	}
2405 }
2406 
2407 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2408 {
2409 	struct net *net = sock_net(skb->sk);
2410 	struct ifinfomsg *ifm;
2411 	char ifname[IFNAMSIZ];
2412 	struct nlattr *tb[IFLA_MAX+1];
2413 	struct net_device *dev = NULL;
2414 	struct sk_buff *nskb;
2415 	int err;
2416 	u32 ext_filter_mask = 0;
2417 
2418 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2419 	if (err < 0)
2420 		return err;
2421 
2422 	if (tb[IFLA_IFNAME])
2423 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2424 
2425 	if (tb[IFLA_EXT_MASK])
2426 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2427 
2428 	ifm = nlmsg_data(nlh);
2429 	if (ifm->ifi_index > 0)
2430 		dev = __dev_get_by_index(net, ifm->ifi_index);
2431 	else if (tb[IFLA_IFNAME])
2432 		dev = __dev_get_by_name(net, ifname);
2433 	else
2434 		return -EINVAL;
2435 
2436 	if (dev == NULL)
2437 		return -ENODEV;
2438 
2439 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2440 	if (nskb == NULL)
2441 		return -ENOBUFS;
2442 
2443 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2444 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2445 	if (err < 0) {
2446 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
2447 		WARN_ON(err == -EMSGSIZE);
2448 		kfree_skb(nskb);
2449 	} else
2450 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2451 
2452 	return err;
2453 }
2454 
2455 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2456 {
2457 	struct net *net = sock_net(skb->sk);
2458 	struct net_device *dev;
2459 	struct nlattr *tb[IFLA_MAX+1];
2460 	u32 ext_filter_mask = 0;
2461 	u16 min_ifinfo_dump_size = 0;
2462 	int hdrlen;
2463 
2464 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2465 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2466 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2467 
2468 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2469 		if (tb[IFLA_EXT_MASK])
2470 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2471 	}
2472 
2473 	if (!ext_filter_mask)
2474 		return NLMSG_GOODSIZE;
2475 	/*
2476 	 * traverse the list of net devices and compute the minimum
2477 	 * buffer size based upon the filter mask.
2478 	 */
2479 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2480 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2481 					     if_nlmsg_size(dev,
2482 						           ext_filter_mask));
2483 	}
2484 
2485 	return min_ifinfo_dump_size;
2486 }
2487 
2488 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2489 {
2490 	int idx;
2491 	int s_idx = cb->family;
2492 
2493 	if (s_idx == 0)
2494 		s_idx = 1;
2495 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2496 		int type = cb->nlh->nlmsg_type-RTM_BASE;
2497 		if (idx < s_idx || idx == PF_PACKET)
2498 			continue;
2499 		if (rtnl_msg_handlers[idx] == NULL ||
2500 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
2501 			continue;
2502 		if (idx > s_idx) {
2503 			memset(&cb->args[0], 0, sizeof(cb->args));
2504 			cb->prev_seq = 0;
2505 			cb->seq = 0;
2506 		}
2507 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2508 			break;
2509 	}
2510 	cb->family = idx;
2511 
2512 	return skb->len;
2513 }
2514 
2515 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2516 				       unsigned int change, gfp_t flags)
2517 {
2518 	struct net *net = dev_net(dev);
2519 	struct sk_buff *skb;
2520 	int err = -ENOBUFS;
2521 	size_t if_info_size;
2522 
2523 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2524 	if (skb == NULL)
2525 		goto errout;
2526 
2527 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2528 	if (err < 0) {
2529 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
2530 		WARN_ON(err == -EMSGSIZE);
2531 		kfree_skb(skb);
2532 		goto errout;
2533 	}
2534 	return skb;
2535 errout:
2536 	if (err < 0)
2537 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2538 	return NULL;
2539 }
2540 
2541 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2542 {
2543 	struct net *net = dev_net(dev);
2544 
2545 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2546 }
2547 
2548 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2549 		  gfp_t flags)
2550 {
2551 	struct sk_buff *skb;
2552 
2553 	if (dev->reg_state != NETREG_REGISTERED)
2554 		return;
2555 
2556 	skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2557 	if (skb)
2558 		rtmsg_ifinfo_send(skb, dev, flags);
2559 }
2560 EXPORT_SYMBOL(rtmsg_ifinfo);
2561 
2562 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2563 				   struct net_device *dev,
2564 				   u8 *addr, u16 vid, u32 pid, u32 seq,
2565 				   int type, unsigned int flags,
2566 				   int nlflags)
2567 {
2568 	struct nlmsghdr *nlh;
2569 	struct ndmsg *ndm;
2570 
2571 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2572 	if (!nlh)
2573 		return -EMSGSIZE;
2574 
2575 	ndm = nlmsg_data(nlh);
2576 	ndm->ndm_family  = AF_BRIDGE;
2577 	ndm->ndm_pad1	 = 0;
2578 	ndm->ndm_pad2    = 0;
2579 	ndm->ndm_flags	 = flags;
2580 	ndm->ndm_type	 = 0;
2581 	ndm->ndm_ifindex = dev->ifindex;
2582 	ndm->ndm_state   = NUD_PERMANENT;
2583 
2584 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2585 		goto nla_put_failure;
2586 	if (vid)
2587 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2588 			goto nla_put_failure;
2589 
2590 	nlmsg_end(skb, nlh);
2591 	return 0;
2592 
2593 nla_put_failure:
2594 	nlmsg_cancel(skb, nlh);
2595 	return -EMSGSIZE;
2596 }
2597 
2598 static inline size_t rtnl_fdb_nlmsg_size(void)
2599 {
2600 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2601 }
2602 
2603 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type)
2604 {
2605 	struct net *net = dev_net(dev);
2606 	struct sk_buff *skb;
2607 	int err = -ENOBUFS;
2608 
2609 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2610 	if (!skb)
2611 		goto errout;
2612 
2613 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2614 				      0, 0, type, NTF_SELF, 0);
2615 	if (err < 0) {
2616 		kfree_skb(skb);
2617 		goto errout;
2618 	}
2619 
2620 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2621 	return;
2622 errout:
2623 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2624 }
2625 
2626 /**
2627  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2628  */
2629 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2630 		     struct nlattr *tb[],
2631 		     struct net_device *dev,
2632 		     const unsigned char *addr, u16 vid,
2633 		     u16 flags)
2634 {
2635 	int err = -EINVAL;
2636 
2637 	/* If aging addresses are supported device will need to
2638 	 * implement its own handler for this.
2639 	 */
2640 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2641 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2642 		return err;
2643 	}
2644 
2645 	if (vid) {
2646 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2647 		return err;
2648 	}
2649 
2650 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2651 		err = dev_uc_add_excl(dev, addr);
2652 	else if (is_multicast_ether_addr(addr))
2653 		err = dev_mc_add_excl(dev, addr);
2654 
2655 	/* Only return duplicate errors if NLM_F_EXCL is set */
2656 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2657 		err = 0;
2658 
2659 	return err;
2660 }
2661 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2662 
2663 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2664 {
2665 	u16 vid = 0;
2666 
2667 	if (vlan_attr) {
2668 		if (nla_len(vlan_attr) != sizeof(u16)) {
2669 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2670 			return -EINVAL;
2671 		}
2672 
2673 		vid = nla_get_u16(vlan_attr);
2674 
2675 		if (!vid || vid >= VLAN_VID_MASK) {
2676 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2677 				vid);
2678 			return -EINVAL;
2679 		}
2680 	}
2681 	*p_vid = vid;
2682 	return 0;
2683 }
2684 
2685 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2686 {
2687 	struct net *net = sock_net(skb->sk);
2688 	struct ndmsg *ndm;
2689 	struct nlattr *tb[NDA_MAX+1];
2690 	struct net_device *dev;
2691 	u8 *addr;
2692 	u16 vid;
2693 	int err;
2694 
2695 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2696 	if (err < 0)
2697 		return err;
2698 
2699 	ndm = nlmsg_data(nlh);
2700 	if (ndm->ndm_ifindex == 0) {
2701 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2702 		return -EINVAL;
2703 	}
2704 
2705 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2706 	if (dev == NULL) {
2707 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2708 		return -ENODEV;
2709 	}
2710 
2711 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2712 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2713 		return -EINVAL;
2714 	}
2715 
2716 	addr = nla_data(tb[NDA_LLADDR]);
2717 
2718 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2719 	if (err)
2720 		return err;
2721 
2722 	err = -EOPNOTSUPP;
2723 
2724 	/* Support fdb on master device the net/bridge default case */
2725 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2726 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2727 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2728 		const struct net_device_ops *ops = br_dev->netdev_ops;
2729 
2730 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2731 				       nlh->nlmsg_flags);
2732 		if (err)
2733 			goto out;
2734 		else
2735 			ndm->ndm_flags &= ~NTF_MASTER;
2736 	}
2737 
2738 	/* Embedded bridge, macvlan, and any other device support */
2739 	if ((ndm->ndm_flags & NTF_SELF)) {
2740 		if (dev->netdev_ops->ndo_fdb_add)
2741 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2742 							   vid,
2743 							   nlh->nlmsg_flags);
2744 		else
2745 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2746 					       nlh->nlmsg_flags);
2747 
2748 		if (!err) {
2749 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH);
2750 			ndm->ndm_flags &= ~NTF_SELF;
2751 		}
2752 	}
2753 out:
2754 	return err;
2755 }
2756 
2757 /**
2758  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2759  */
2760 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2761 		     struct nlattr *tb[],
2762 		     struct net_device *dev,
2763 		     const unsigned char *addr, u16 vid)
2764 {
2765 	int err = -EINVAL;
2766 
2767 	/* If aging addresses are supported device will need to
2768 	 * implement its own handler for this.
2769 	 */
2770 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2771 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2772 		return err;
2773 	}
2774 
2775 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2776 		err = dev_uc_del(dev, addr);
2777 	else if (is_multicast_ether_addr(addr))
2778 		err = dev_mc_del(dev, addr);
2779 
2780 	return err;
2781 }
2782 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2783 
2784 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2785 {
2786 	struct net *net = sock_net(skb->sk);
2787 	struct ndmsg *ndm;
2788 	struct nlattr *tb[NDA_MAX+1];
2789 	struct net_device *dev;
2790 	int err = -EINVAL;
2791 	__u8 *addr;
2792 	u16 vid;
2793 
2794 	if (!netlink_capable(skb, CAP_NET_ADMIN))
2795 		return -EPERM;
2796 
2797 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2798 	if (err < 0)
2799 		return err;
2800 
2801 	ndm = nlmsg_data(nlh);
2802 	if (ndm->ndm_ifindex == 0) {
2803 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2804 		return -EINVAL;
2805 	}
2806 
2807 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2808 	if (dev == NULL) {
2809 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2810 		return -ENODEV;
2811 	}
2812 
2813 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2814 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2815 		return -EINVAL;
2816 	}
2817 
2818 	addr = nla_data(tb[NDA_LLADDR]);
2819 
2820 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2821 	if (err)
2822 		return err;
2823 
2824 	err = -EOPNOTSUPP;
2825 
2826 	/* Support fdb on master device the net/bridge default case */
2827 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2828 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2829 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2830 		const struct net_device_ops *ops = br_dev->netdev_ops;
2831 
2832 		if (ops->ndo_fdb_del)
2833 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2834 
2835 		if (err)
2836 			goto out;
2837 		else
2838 			ndm->ndm_flags &= ~NTF_MASTER;
2839 	}
2840 
2841 	/* Embedded bridge, macvlan, and any other device support */
2842 	if (ndm->ndm_flags & NTF_SELF) {
2843 		if (dev->netdev_ops->ndo_fdb_del)
2844 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2845 							   vid);
2846 		else
2847 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2848 
2849 		if (!err) {
2850 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH);
2851 			ndm->ndm_flags &= ~NTF_SELF;
2852 		}
2853 	}
2854 out:
2855 	return err;
2856 }
2857 
2858 static int nlmsg_populate_fdb(struct sk_buff *skb,
2859 			      struct netlink_callback *cb,
2860 			      struct net_device *dev,
2861 			      int *idx,
2862 			      struct netdev_hw_addr_list *list)
2863 {
2864 	struct netdev_hw_addr *ha;
2865 	int err;
2866 	u32 portid, seq;
2867 
2868 	portid = NETLINK_CB(cb->skb).portid;
2869 	seq = cb->nlh->nlmsg_seq;
2870 
2871 	list_for_each_entry(ha, &list->list, list) {
2872 		if (*idx < cb->args[0])
2873 			goto skip;
2874 
2875 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2876 					      portid, seq,
2877 					      RTM_NEWNEIGH, NTF_SELF,
2878 					      NLM_F_MULTI);
2879 		if (err < 0)
2880 			return err;
2881 skip:
2882 		*idx += 1;
2883 	}
2884 	return 0;
2885 }
2886 
2887 /**
2888  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2889  * @nlh: netlink message header
2890  * @dev: netdevice
2891  *
2892  * Default netdevice operation to dump the existing unicast address list.
2893  * Returns number of addresses from list put in skb.
2894  */
2895 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2896 		      struct netlink_callback *cb,
2897 		      struct net_device *dev,
2898 		      struct net_device *filter_dev,
2899 		      int idx)
2900 {
2901 	int err;
2902 
2903 	netif_addr_lock_bh(dev);
2904 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
2905 	if (err)
2906 		goto out;
2907 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
2908 out:
2909 	netif_addr_unlock_bh(dev);
2910 	return idx;
2911 }
2912 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
2913 
2914 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
2915 {
2916 	struct net_device *dev;
2917 	struct nlattr *tb[IFLA_MAX+1];
2918 	struct net_device *br_dev = NULL;
2919 	const struct net_device_ops *ops = NULL;
2920 	const struct net_device_ops *cops = NULL;
2921 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
2922 	struct net *net = sock_net(skb->sk);
2923 	int brport_idx = 0;
2924 	int br_idx = 0;
2925 	int idx = 0;
2926 
2927 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
2928 			ifla_policy) == 0) {
2929 		if (tb[IFLA_MASTER])
2930 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
2931 	}
2932 
2933 	brport_idx = ifm->ifi_index;
2934 
2935 	if (br_idx) {
2936 		br_dev = __dev_get_by_index(net, br_idx);
2937 		if (!br_dev)
2938 			return -ENODEV;
2939 
2940 		ops = br_dev->netdev_ops;
2941 	}
2942 
2943 	for_each_netdev(net, dev) {
2944 		if (brport_idx && (dev->ifindex != brport_idx))
2945 			continue;
2946 
2947 		if (!br_idx) { /* user did not specify a specific bridge */
2948 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
2949 				br_dev = netdev_master_upper_dev_get(dev);
2950 				cops = br_dev->netdev_ops;
2951 			}
2952 
2953 		} else {
2954 			if (dev != br_dev &&
2955 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
2956 				continue;
2957 
2958 			if (br_dev != netdev_master_upper_dev_get(dev) &&
2959 			    !(dev->priv_flags & IFF_EBRIDGE))
2960 				continue;
2961 
2962 			cops = ops;
2963 		}
2964 
2965 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
2966 			if (cops && cops->ndo_fdb_dump)
2967 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
2968 							 idx);
2969 		}
2970 
2971 		if (dev->netdev_ops->ndo_fdb_dump)
2972 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
2973 							    idx);
2974 		else
2975 			idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
2976 
2977 		cops = NULL;
2978 	}
2979 
2980 	cb->args[0] = idx;
2981 	return skb->len;
2982 }
2983 
2984 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
2985 			       unsigned int attrnum, unsigned int flag)
2986 {
2987 	if (mask & flag)
2988 		return nla_put_u8(skb, attrnum, !!(flags & flag));
2989 	return 0;
2990 }
2991 
2992 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
2993 			    struct net_device *dev, u16 mode,
2994 			    u32 flags, u32 mask, int nlflags,
2995 			    u32 filter_mask,
2996 			    int (*vlan_fill)(struct sk_buff *skb,
2997 					     struct net_device *dev,
2998 					     u32 filter_mask))
2999 {
3000 	struct nlmsghdr *nlh;
3001 	struct ifinfomsg *ifm;
3002 	struct nlattr *br_afspec;
3003 	struct nlattr *protinfo;
3004 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3005 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3006 	int err = 0;
3007 
3008 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3009 	if (nlh == NULL)
3010 		return -EMSGSIZE;
3011 
3012 	ifm = nlmsg_data(nlh);
3013 	ifm->ifi_family = AF_BRIDGE;
3014 	ifm->__ifi_pad = 0;
3015 	ifm->ifi_type = dev->type;
3016 	ifm->ifi_index = dev->ifindex;
3017 	ifm->ifi_flags = dev_get_flags(dev);
3018 	ifm->ifi_change = 0;
3019 
3020 
3021 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3022 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3023 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3024 	    (br_dev &&
3025 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3026 	    (dev->addr_len &&
3027 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3028 	    (dev->ifindex != dev_get_iflink(dev) &&
3029 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3030 		goto nla_put_failure;
3031 
3032 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3033 	if (!br_afspec)
3034 		goto nla_put_failure;
3035 
3036 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3037 		nla_nest_cancel(skb, br_afspec);
3038 		goto nla_put_failure;
3039 	}
3040 
3041 	if (mode != BRIDGE_MODE_UNDEF) {
3042 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3043 			nla_nest_cancel(skb, br_afspec);
3044 			goto nla_put_failure;
3045 		}
3046 	}
3047 	if (vlan_fill) {
3048 		err = vlan_fill(skb, dev, filter_mask);
3049 		if (err) {
3050 			nla_nest_cancel(skb, br_afspec);
3051 			goto nla_put_failure;
3052 		}
3053 	}
3054 	nla_nest_end(skb, br_afspec);
3055 
3056 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3057 	if (!protinfo)
3058 		goto nla_put_failure;
3059 
3060 	if (brport_nla_put_flag(skb, flags, mask,
3061 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3062 	    brport_nla_put_flag(skb, flags, mask,
3063 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3064 	    brport_nla_put_flag(skb, flags, mask,
3065 				IFLA_BRPORT_FAST_LEAVE,
3066 				BR_MULTICAST_FAST_LEAVE) ||
3067 	    brport_nla_put_flag(skb, flags, mask,
3068 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3069 	    brport_nla_put_flag(skb, flags, mask,
3070 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3071 	    brport_nla_put_flag(skb, flags, mask,
3072 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3073 	    brport_nla_put_flag(skb, flags, mask,
3074 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3075 	    brport_nla_put_flag(skb, flags, mask,
3076 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3077 		nla_nest_cancel(skb, protinfo);
3078 		goto nla_put_failure;
3079 	}
3080 
3081 	nla_nest_end(skb, protinfo);
3082 
3083 	nlmsg_end(skb, nlh);
3084 	return 0;
3085 nla_put_failure:
3086 	nlmsg_cancel(skb, nlh);
3087 	return err ? err : -EMSGSIZE;
3088 }
3089 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3090 
3091 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3092 {
3093 	struct net *net = sock_net(skb->sk);
3094 	struct net_device *dev;
3095 	int idx = 0;
3096 	u32 portid = NETLINK_CB(cb->skb).portid;
3097 	u32 seq = cb->nlh->nlmsg_seq;
3098 	u32 filter_mask = 0;
3099 	int err;
3100 
3101 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3102 		struct nlattr *extfilt;
3103 
3104 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3105 					  IFLA_EXT_MASK);
3106 		if (extfilt) {
3107 			if (nla_len(extfilt) < sizeof(filter_mask))
3108 				return -EINVAL;
3109 
3110 			filter_mask = nla_get_u32(extfilt);
3111 		}
3112 	}
3113 
3114 	rcu_read_lock();
3115 	for_each_netdev_rcu(net, dev) {
3116 		const struct net_device_ops *ops = dev->netdev_ops;
3117 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3118 
3119 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3120 			if (idx >= cb->args[0]) {
3121 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3122 						skb, portid, seq, dev,
3123 						filter_mask, NLM_F_MULTI);
3124 				if (err < 0 && err != -EOPNOTSUPP)
3125 					break;
3126 			}
3127 			idx++;
3128 		}
3129 
3130 		if (ops->ndo_bridge_getlink) {
3131 			if (idx >= cb->args[0]) {
3132 				err = ops->ndo_bridge_getlink(skb, portid,
3133 							      seq, dev,
3134 							      filter_mask,
3135 							      NLM_F_MULTI);
3136 				if (err < 0 && err != -EOPNOTSUPP)
3137 					break;
3138 			}
3139 			idx++;
3140 		}
3141 	}
3142 	rcu_read_unlock();
3143 	cb->args[0] = idx;
3144 
3145 	return skb->len;
3146 }
3147 
3148 static inline size_t bridge_nlmsg_size(void)
3149 {
3150 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3151 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
3152 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
3153 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
3154 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
3155 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
3156 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
3157 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
3158 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
3159 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
3160 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
3161 }
3162 
3163 static int rtnl_bridge_notify(struct net_device *dev)
3164 {
3165 	struct net *net = dev_net(dev);
3166 	struct sk_buff *skb;
3167 	int err = -EOPNOTSUPP;
3168 
3169 	if (!dev->netdev_ops->ndo_bridge_getlink)
3170 		return 0;
3171 
3172 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3173 	if (!skb) {
3174 		err = -ENOMEM;
3175 		goto errout;
3176 	}
3177 
3178 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3179 	if (err < 0)
3180 		goto errout;
3181 
3182 	if (!skb->len)
3183 		goto errout;
3184 
3185 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3186 	return 0;
3187 errout:
3188 	WARN_ON(err == -EMSGSIZE);
3189 	kfree_skb(skb);
3190 	if (err)
3191 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3192 	return err;
3193 }
3194 
3195 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3196 {
3197 	struct net *net = sock_net(skb->sk);
3198 	struct ifinfomsg *ifm;
3199 	struct net_device *dev;
3200 	struct nlattr *br_spec, *attr = NULL;
3201 	int rem, err = -EOPNOTSUPP;
3202 	u16 flags = 0;
3203 	bool have_flags = false;
3204 
3205 	if (nlmsg_len(nlh) < sizeof(*ifm))
3206 		return -EINVAL;
3207 
3208 	ifm = nlmsg_data(nlh);
3209 	if (ifm->ifi_family != AF_BRIDGE)
3210 		return -EPFNOSUPPORT;
3211 
3212 	dev = __dev_get_by_index(net, ifm->ifi_index);
3213 	if (!dev) {
3214 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3215 		return -ENODEV;
3216 	}
3217 
3218 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3219 	if (br_spec) {
3220 		nla_for_each_nested(attr, br_spec, rem) {
3221 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3222 				if (nla_len(attr) < sizeof(flags))
3223 					return -EINVAL;
3224 
3225 				have_flags = true;
3226 				flags = nla_get_u16(attr);
3227 				break;
3228 			}
3229 		}
3230 	}
3231 
3232 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3233 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3234 
3235 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3236 			err = -EOPNOTSUPP;
3237 			goto out;
3238 		}
3239 
3240 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3241 		if (err)
3242 			goto out;
3243 
3244 		flags &= ~BRIDGE_FLAGS_MASTER;
3245 	}
3246 
3247 	if ((flags & BRIDGE_FLAGS_SELF)) {
3248 		if (!dev->netdev_ops->ndo_bridge_setlink)
3249 			err = -EOPNOTSUPP;
3250 		else
3251 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3252 								  flags);
3253 		if (!err) {
3254 			flags &= ~BRIDGE_FLAGS_SELF;
3255 
3256 			/* Generate event to notify upper layer of bridge
3257 			 * change
3258 			 */
3259 			err = rtnl_bridge_notify(dev);
3260 		}
3261 	}
3262 
3263 	if (have_flags)
3264 		memcpy(nla_data(attr), &flags, sizeof(flags));
3265 out:
3266 	return err;
3267 }
3268 
3269 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3270 {
3271 	struct net *net = sock_net(skb->sk);
3272 	struct ifinfomsg *ifm;
3273 	struct net_device *dev;
3274 	struct nlattr *br_spec, *attr = NULL;
3275 	int rem, err = -EOPNOTSUPP;
3276 	u16 flags = 0;
3277 	bool have_flags = false;
3278 
3279 	if (nlmsg_len(nlh) < sizeof(*ifm))
3280 		return -EINVAL;
3281 
3282 	ifm = nlmsg_data(nlh);
3283 	if (ifm->ifi_family != AF_BRIDGE)
3284 		return -EPFNOSUPPORT;
3285 
3286 	dev = __dev_get_by_index(net, ifm->ifi_index);
3287 	if (!dev) {
3288 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3289 		return -ENODEV;
3290 	}
3291 
3292 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3293 	if (br_spec) {
3294 		nla_for_each_nested(attr, br_spec, rem) {
3295 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3296 				if (nla_len(attr) < sizeof(flags))
3297 					return -EINVAL;
3298 
3299 				have_flags = true;
3300 				flags = nla_get_u16(attr);
3301 				break;
3302 			}
3303 		}
3304 	}
3305 
3306 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3307 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3308 
3309 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3310 			err = -EOPNOTSUPP;
3311 			goto out;
3312 		}
3313 
3314 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3315 		if (err)
3316 			goto out;
3317 
3318 		flags &= ~BRIDGE_FLAGS_MASTER;
3319 	}
3320 
3321 	if ((flags & BRIDGE_FLAGS_SELF)) {
3322 		if (!dev->netdev_ops->ndo_bridge_dellink)
3323 			err = -EOPNOTSUPP;
3324 		else
3325 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3326 								  flags);
3327 
3328 		if (!err) {
3329 			flags &= ~BRIDGE_FLAGS_SELF;
3330 
3331 			/* Generate event to notify upper layer of bridge
3332 			 * change
3333 			 */
3334 			err = rtnl_bridge_notify(dev);
3335 		}
3336 	}
3337 
3338 	if (have_flags)
3339 		memcpy(nla_data(attr), &flags, sizeof(flags));
3340 out:
3341 	return err;
3342 }
3343 
3344 /* Process one rtnetlink message. */
3345 
3346 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3347 {
3348 	struct net *net = sock_net(skb->sk);
3349 	rtnl_doit_func doit;
3350 	int sz_idx, kind;
3351 	int family;
3352 	int type;
3353 	int err;
3354 
3355 	type = nlh->nlmsg_type;
3356 	if (type > RTM_MAX)
3357 		return -EOPNOTSUPP;
3358 
3359 	type -= RTM_BASE;
3360 
3361 	/* All the messages must have at least 1 byte length */
3362 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3363 		return 0;
3364 
3365 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3366 	sz_idx = type>>2;
3367 	kind = type&3;
3368 
3369 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3370 		return -EPERM;
3371 
3372 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3373 		struct sock *rtnl;
3374 		rtnl_dumpit_func dumpit;
3375 		rtnl_calcit_func calcit;
3376 		u16 min_dump_alloc = 0;
3377 
3378 		dumpit = rtnl_get_dumpit(family, type);
3379 		if (dumpit == NULL)
3380 			return -EOPNOTSUPP;
3381 		calcit = rtnl_get_calcit(family, type);
3382 		if (calcit)
3383 			min_dump_alloc = calcit(skb, nlh);
3384 
3385 		__rtnl_unlock();
3386 		rtnl = net->rtnl;
3387 		{
3388 			struct netlink_dump_control c = {
3389 				.dump		= dumpit,
3390 				.min_dump_alloc	= min_dump_alloc,
3391 			};
3392 			err = netlink_dump_start(rtnl, skb, nlh, &c);
3393 		}
3394 		rtnl_lock();
3395 		return err;
3396 	}
3397 
3398 	doit = rtnl_get_doit(family, type);
3399 	if (doit == NULL)
3400 		return -EOPNOTSUPP;
3401 
3402 	return doit(skb, nlh);
3403 }
3404 
3405 static void rtnetlink_rcv(struct sk_buff *skb)
3406 {
3407 	rtnl_lock();
3408 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3409 	rtnl_unlock();
3410 }
3411 
3412 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3413 {
3414 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3415 
3416 	switch (event) {
3417 	case NETDEV_UP:
3418 	case NETDEV_DOWN:
3419 	case NETDEV_PRE_UP:
3420 	case NETDEV_POST_INIT:
3421 	case NETDEV_REGISTER:
3422 	case NETDEV_CHANGE:
3423 	case NETDEV_PRE_TYPE_CHANGE:
3424 	case NETDEV_GOING_DOWN:
3425 	case NETDEV_UNREGISTER:
3426 	case NETDEV_UNREGISTER_FINAL:
3427 	case NETDEV_RELEASE:
3428 	case NETDEV_JOIN:
3429 	case NETDEV_BONDING_INFO:
3430 		break;
3431 	default:
3432 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3433 		break;
3434 	}
3435 	return NOTIFY_DONE;
3436 }
3437 
3438 static struct notifier_block rtnetlink_dev_notifier = {
3439 	.notifier_call	= rtnetlink_event,
3440 };
3441 
3442 
3443 static int __net_init rtnetlink_net_init(struct net *net)
3444 {
3445 	struct sock *sk;
3446 	struct netlink_kernel_cfg cfg = {
3447 		.groups		= RTNLGRP_MAX,
3448 		.input		= rtnetlink_rcv,
3449 		.cb_mutex	= &rtnl_mutex,
3450 		.flags		= NL_CFG_F_NONROOT_RECV,
3451 	};
3452 
3453 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3454 	if (!sk)
3455 		return -ENOMEM;
3456 	net->rtnl = sk;
3457 	return 0;
3458 }
3459 
3460 static void __net_exit rtnetlink_net_exit(struct net *net)
3461 {
3462 	netlink_kernel_release(net->rtnl);
3463 	net->rtnl = NULL;
3464 }
3465 
3466 static struct pernet_operations rtnetlink_net_ops = {
3467 	.init = rtnetlink_net_init,
3468 	.exit = rtnetlink_net_exit,
3469 };
3470 
3471 void __init rtnetlink_init(void)
3472 {
3473 	if (register_pernet_subsys(&rtnetlink_net_ops))
3474 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
3475 
3476 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3477 
3478 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3479 		      rtnl_dump_ifinfo, rtnl_calcit);
3480 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3481 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3482 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3483 
3484 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3485 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3486 
3487 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3488 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3489 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3490 
3491 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3492 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3493 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
3494 }
3495