xref: /openbmc/linux/net/core/rtnetlink.c (revision bdd91001)
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 	a->rx_nohandler = b->rx_nohandler;
809 }
810 
811 /* All VF info */
812 static inline int rtnl_vfinfo_size(const struct net_device *dev,
813 				   u32 ext_filter_mask)
814 {
815 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
816 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
817 		int num_vfs = dev_num_vf(dev->dev.parent);
818 		size_t size = nla_total_size(sizeof(struct nlattr));
819 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
820 		size += num_vfs *
821 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
822 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
823 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
824 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
825 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
826 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
827 			 /* IFLA_VF_STATS_RX_PACKETS */
828 			 nla_total_size_64bit(sizeof(__u64)) +
829 			 /* IFLA_VF_STATS_TX_PACKETS */
830 			 nla_total_size_64bit(sizeof(__u64)) +
831 			 /* IFLA_VF_STATS_RX_BYTES */
832 			 nla_total_size_64bit(sizeof(__u64)) +
833 			 /* IFLA_VF_STATS_TX_BYTES */
834 			 nla_total_size_64bit(sizeof(__u64)) +
835 			 /* IFLA_VF_STATS_BROADCAST */
836 			 nla_total_size_64bit(sizeof(__u64)) +
837 			 /* IFLA_VF_STATS_MULTICAST */
838 			 nla_total_size_64bit(sizeof(__u64)) +
839 			 nla_total_size(sizeof(struct ifla_vf_trust)));
840 		return size;
841 	} else
842 		return 0;
843 }
844 
845 static size_t rtnl_port_size(const struct net_device *dev,
846 			     u32 ext_filter_mask)
847 {
848 	size_t port_size = nla_total_size(4)		/* PORT_VF */
849 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
850 		+ nla_total_size(sizeof(struct ifla_port_vsi))
851 							/* PORT_VSI_TYPE */
852 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
853 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
854 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
855 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
856 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
857 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
858 		+ port_size;
859 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
860 		+ port_size;
861 
862 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
863 	    !(ext_filter_mask & RTEXT_FILTER_VF))
864 		return 0;
865 	if (dev_num_vf(dev->dev.parent))
866 		return port_self_size + vf_ports_size +
867 			vf_port_size * dev_num_vf(dev->dev.parent);
868 	else
869 		return port_self_size;
870 }
871 
872 static noinline size_t if_nlmsg_size(const struct net_device *dev,
873 				     u32 ext_filter_mask)
874 {
875 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
876 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
877 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
878 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
879 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
880 	       + nla_total_size(sizeof(struct rtnl_link_stats))
881 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
882 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
883 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
884 	       + nla_total_size(4) /* IFLA_TXQLEN */
885 	       + nla_total_size(4) /* IFLA_WEIGHT */
886 	       + nla_total_size(4) /* IFLA_MTU */
887 	       + nla_total_size(4) /* IFLA_LINK */
888 	       + nla_total_size(4) /* IFLA_MASTER */
889 	       + nla_total_size(1) /* IFLA_CARRIER */
890 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
891 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
892 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
893 	       + nla_total_size(4) /* IFLA_MAX_GSO_SEGS */
894 	       + nla_total_size(4) /* IFLA_MAX_GSO_SIZE */
895 	       + nla_total_size(1) /* IFLA_OPERSTATE */
896 	       + nla_total_size(1) /* IFLA_LINKMODE */
897 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
898 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
899 	       + nla_total_size(ext_filter_mask
900 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
901 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
902 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
903 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
904 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
905 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
906 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
907 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
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 		.orig_dev = dev,
1031 		.id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
1032 		.flags = SWITCHDEV_F_NO_RECURSE,
1033 	};
1034 
1035 	err = switchdev_port_attr_get(dev, &attr);
1036 	if (err) {
1037 		if (err == -EOPNOTSUPP)
1038 			return 0;
1039 		return err;
1040 	}
1041 
1042 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, attr.u.ppid.id_len,
1043 		    attr.u.ppid.id))
1044 		return -EMSGSIZE;
1045 
1046 	return 0;
1047 }
1048 
1049 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1050 					      struct net_device *dev)
1051 {
1052 	struct rtnl_link_stats64 *sp;
1053 	struct nlattr *attr;
1054 
1055 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
1056 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1057 	if (!attr)
1058 		return -EMSGSIZE;
1059 
1060 	sp = nla_data(attr);
1061 	dev_get_stats(dev, sp);
1062 
1063 	attr = nla_reserve(skb, IFLA_STATS,
1064 			   sizeof(struct rtnl_link_stats));
1065 	if (!attr)
1066 		return -EMSGSIZE;
1067 
1068 	copy_rtnl_link_stats(nla_data(attr), sp);
1069 
1070 	return 0;
1071 }
1072 
1073 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1074 					       struct net_device *dev,
1075 					       int vfs_num,
1076 					       struct nlattr *vfinfo)
1077 {
1078 	struct ifla_vf_rss_query_en vf_rss_query_en;
1079 	struct ifla_vf_link_state vf_linkstate;
1080 	struct ifla_vf_spoofchk vf_spoofchk;
1081 	struct ifla_vf_tx_rate vf_tx_rate;
1082 	struct ifla_vf_stats vf_stats;
1083 	struct ifla_vf_trust vf_trust;
1084 	struct ifla_vf_vlan vf_vlan;
1085 	struct ifla_vf_rate vf_rate;
1086 	struct nlattr *vf, *vfstats;
1087 	struct ifla_vf_mac vf_mac;
1088 	struct ifla_vf_info ivi;
1089 
1090 	/* Not all SR-IOV capable drivers support the
1091 	 * spoofcheck and "RSS query enable" query.  Preset to
1092 	 * -1 so the user space tool can detect that the driver
1093 	 * didn't report anything.
1094 	 */
1095 	ivi.spoofchk = -1;
1096 	ivi.rss_query_en = -1;
1097 	ivi.trusted = -1;
1098 	memset(ivi.mac, 0, sizeof(ivi.mac));
1099 	/* The default value for VF link state is "auto"
1100 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1101 	 */
1102 	ivi.linkstate = 0;
1103 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1104 		return 0;
1105 
1106 	vf_mac.vf =
1107 		vf_vlan.vf =
1108 		vf_rate.vf =
1109 		vf_tx_rate.vf =
1110 		vf_spoofchk.vf =
1111 		vf_linkstate.vf =
1112 		vf_rss_query_en.vf =
1113 		vf_trust.vf = ivi.vf;
1114 
1115 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1116 	vf_vlan.vlan = ivi.vlan;
1117 	vf_vlan.qos = ivi.qos;
1118 	vf_tx_rate.rate = ivi.max_tx_rate;
1119 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1120 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1121 	vf_spoofchk.setting = ivi.spoofchk;
1122 	vf_linkstate.link_state = ivi.linkstate;
1123 	vf_rss_query_en.setting = ivi.rss_query_en;
1124 	vf_trust.setting = ivi.trusted;
1125 	vf = nla_nest_start(skb, IFLA_VF_INFO);
1126 	if (!vf) {
1127 		nla_nest_cancel(skb, vfinfo);
1128 		return -EMSGSIZE;
1129 	}
1130 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1131 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1132 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1133 		    &vf_rate) ||
1134 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1135 		    &vf_tx_rate) ||
1136 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1137 		    &vf_spoofchk) ||
1138 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1139 		    &vf_linkstate) ||
1140 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1141 		    sizeof(vf_rss_query_en),
1142 		    &vf_rss_query_en) ||
1143 	    nla_put(skb, IFLA_VF_TRUST,
1144 		    sizeof(vf_trust), &vf_trust))
1145 		return -EMSGSIZE;
1146 	memset(&vf_stats, 0, sizeof(vf_stats));
1147 	if (dev->netdev_ops->ndo_get_vf_stats)
1148 		dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1149 						&vf_stats);
1150 	vfstats = nla_nest_start(skb, IFLA_VF_STATS);
1151 	if (!vfstats) {
1152 		nla_nest_cancel(skb, vf);
1153 		nla_nest_cancel(skb, vfinfo);
1154 		return -EMSGSIZE;
1155 	}
1156 	if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1157 			      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1158 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1159 			      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1160 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1161 			      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1162 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1163 			      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1164 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1165 			      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1166 	    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1167 			      vf_stats.multicast, IFLA_VF_STATS_PAD))
1168 		return -EMSGSIZE;
1169 	nla_nest_end(skb, vfstats);
1170 	nla_nest_end(skb, vf);
1171 	return 0;
1172 }
1173 
1174 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1175 {
1176 	struct rtnl_link_ifmap map;
1177 
1178 	memset(&map, 0, sizeof(map));
1179 	map.mem_start   = dev->mem_start;
1180 	map.mem_end     = dev->mem_end;
1181 	map.base_addr   = dev->base_addr;
1182 	map.irq         = dev->irq;
1183 	map.dma         = dev->dma;
1184 	map.port        = dev->if_port;
1185 
1186 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1187 		return -EMSGSIZE;
1188 
1189 	return 0;
1190 }
1191 
1192 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
1193 			    int type, u32 pid, u32 seq, u32 change,
1194 			    unsigned int flags, u32 ext_filter_mask)
1195 {
1196 	struct ifinfomsg *ifm;
1197 	struct nlmsghdr *nlh;
1198 	struct nlattr *af_spec;
1199 	struct rtnl_af_ops *af_ops;
1200 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1201 
1202 	ASSERT_RTNL();
1203 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1204 	if (nlh == NULL)
1205 		return -EMSGSIZE;
1206 
1207 	ifm = nlmsg_data(nlh);
1208 	ifm->ifi_family = AF_UNSPEC;
1209 	ifm->__ifi_pad = 0;
1210 	ifm->ifi_type = dev->type;
1211 	ifm->ifi_index = dev->ifindex;
1212 	ifm->ifi_flags = dev_get_flags(dev);
1213 	ifm->ifi_change = change;
1214 
1215 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1216 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1217 	    nla_put_u8(skb, IFLA_OPERSTATE,
1218 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1219 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1220 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1221 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1222 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1223 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1224 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1225 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1226 #ifdef CONFIG_RPS
1227 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1228 #endif
1229 	    (dev->ifindex != dev_get_iflink(dev) &&
1230 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))) ||
1231 	    (upper_dev &&
1232 	     nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex)) ||
1233 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1234 	    (dev->qdisc &&
1235 	     nla_put_string(skb, IFLA_QDISC, dev->qdisc->ops->id)) ||
1236 	    (dev->ifalias &&
1237 	     nla_put_string(skb, IFLA_IFALIAS, dev->ifalias)) ||
1238 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1239 			atomic_read(&dev->carrier_changes)) ||
1240 	    nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1241 		goto nla_put_failure;
1242 
1243 	if (rtnl_fill_link_ifmap(skb, dev))
1244 		goto nla_put_failure;
1245 
1246 	if (dev->addr_len) {
1247 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1248 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1249 			goto nla_put_failure;
1250 	}
1251 
1252 	if (rtnl_phys_port_id_fill(skb, dev))
1253 		goto nla_put_failure;
1254 
1255 	if (rtnl_phys_port_name_fill(skb, dev))
1256 		goto nla_put_failure;
1257 
1258 	if (rtnl_phys_switch_id_fill(skb, dev))
1259 		goto nla_put_failure;
1260 
1261 	if (rtnl_fill_stats(skb, dev))
1262 		goto nla_put_failure;
1263 
1264 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF) &&
1265 	    nla_put_u32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent)))
1266 		goto nla_put_failure;
1267 
1268 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent &&
1269 	    ext_filter_mask & RTEXT_FILTER_VF) {
1270 		int i;
1271 		struct nlattr *vfinfo;
1272 		int num_vfs = dev_num_vf(dev->dev.parent);
1273 
1274 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
1275 		if (!vfinfo)
1276 			goto nla_put_failure;
1277 		for (i = 0; i < num_vfs; i++) {
1278 			if (rtnl_fill_vfinfo(skb, dev, i, vfinfo))
1279 				goto nla_put_failure;
1280 		}
1281 
1282 		nla_nest_end(skb, vfinfo);
1283 	}
1284 
1285 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1286 		goto nla_put_failure;
1287 
1288 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1289 		if (rtnl_link_fill(skb, dev) < 0)
1290 			goto nla_put_failure;
1291 	}
1292 
1293 	if (dev->rtnl_link_ops &&
1294 	    dev->rtnl_link_ops->get_link_net) {
1295 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1296 
1297 		if (!net_eq(dev_net(dev), link_net)) {
1298 			int id = peernet2id_alloc(dev_net(dev), link_net);
1299 
1300 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1301 				goto nla_put_failure;
1302 		}
1303 	}
1304 
1305 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1306 		goto nla_put_failure;
1307 
1308 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1309 		if (af_ops->fill_link_af) {
1310 			struct nlattr *af;
1311 			int err;
1312 
1313 			if (!(af = nla_nest_start(skb, af_ops->family)))
1314 				goto nla_put_failure;
1315 
1316 			err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1317 
1318 			/*
1319 			 * Caller may return ENODATA to indicate that there
1320 			 * was no data to be dumped. This is not an error, it
1321 			 * means we should trim the attribute header and
1322 			 * continue.
1323 			 */
1324 			if (err == -ENODATA)
1325 				nla_nest_cancel(skb, af);
1326 			else if (err < 0)
1327 				goto nla_put_failure;
1328 
1329 			nla_nest_end(skb, af);
1330 		}
1331 	}
1332 
1333 	nla_nest_end(skb, af_spec);
1334 
1335 	nlmsg_end(skb, nlh);
1336 	return 0;
1337 
1338 nla_put_failure:
1339 	nlmsg_cancel(skb, nlh);
1340 	return -EMSGSIZE;
1341 }
1342 
1343 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1344 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1345 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1346 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1347 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1348 	[IFLA_MTU]		= { .type = NLA_U32 },
1349 	[IFLA_LINK]		= { .type = NLA_U32 },
1350 	[IFLA_MASTER]		= { .type = NLA_U32 },
1351 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1352 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1353 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1354 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1355 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1356 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1357 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1358 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1359 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1360 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1361 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1362 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1363 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1364 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1365 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1366 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1367 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1368 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1369 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1370 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1371 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
1372 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1373 };
1374 
1375 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1376 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1377 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1378 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1379 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1380 };
1381 
1382 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1383 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1384 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1385 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1386 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1387 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1388 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
1389 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
1390 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1391 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1392 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1393 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1394 };
1395 
1396 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1397 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1398 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1399 				    .len = PORT_PROFILE_MAX },
1400 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1401 				    .len = sizeof(struct ifla_port_vsi)},
1402 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1403 				      .len = PORT_UUID_MAX },
1404 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1405 				    .len = PORT_UUID_MAX },
1406 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1407 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1408 };
1409 
1410 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1411 {
1412 	const struct rtnl_link_ops *ops = NULL;
1413 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1414 
1415 	if (nla_parse_nested(linfo, IFLA_INFO_MAX, nla, ifla_info_policy) < 0)
1416 		return NULL;
1417 
1418 	if (linfo[IFLA_INFO_KIND]) {
1419 		char kind[MODULE_NAME_LEN];
1420 
1421 		nla_strlcpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
1422 		ops = rtnl_link_ops_get(kind);
1423 	}
1424 
1425 	return ops;
1426 }
1427 
1428 static bool link_master_filtered(struct net_device *dev, int master_idx)
1429 {
1430 	struct net_device *master;
1431 
1432 	if (!master_idx)
1433 		return false;
1434 
1435 	master = netdev_master_upper_dev_get(dev);
1436 	if (!master || master->ifindex != master_idx)
1437 		return true;
1438 
1439 	return false;
1440 }
1441 
1442 static bool link_kind_filtered(const struct net_device *dev,
1443 			       const struct rtnl_link_ops *kind_ops)
1444 {
1445 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
1446 		return true;
1447 
1448 	return false;
1449 }
1450 
1451 static bool link_dump_filtered(struct net_device *dev,
1452 			       int master_idx,
1453 			       const struct rtnl_link_ops *kind_ops)
1454 {
1455 	if (link_master_filtered(dev, master_idx) ||
1456 	    link_kind_filtered(dev, kind_ops))
1457 		return true;
1458 
1459 	return false;
1460 }
1461 
1462 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1463 {
1464 	struct net *net = sock_net(skb->sk);
1465 	int h, s_h;
1466 	int idx = 0, s_idx;
1467 	struct net_device *dev;
1468 	struct hlist_head *head;
1469 	struct nlattr *tb[IFLA_MAX+1];
1470 	u32 ext_filter_mask = 0;
1471 	const struct rtnl_link_ops *kind_ops = NULL;
1472 	unsigned int flags = NLM_F_MULTI;
1473 	int master_idx = 0;
1474 	int err;
1475 	int hdrlen;
1476 
1477 	s_h = cb->args[0];
1478 	s_idx = cb->args[1];
1479 
1480 	cb->seq = net->dev_base_seq;
1481 
1482 	/* A hack to preserve kernel<->userspace interface.
1483 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
1484 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
1485 	 * what iproute2 < v3.9.0 used.
1486 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
1487 	 * attribute, its netlink message is shorter than struct ifinfomsg.
1488 	 */
1489 	hdrlen = nlmsg_len(cb->nlh) < sizeof(struct ifinfomsg) ?
1490 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
1491 
1492 	if (nlmsg_parse(cb->nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
1493 
1494 		if (tb[IFLA_EXT_MASK])
1495 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1496 
1497 		if (tb[IFLA_MASTER])
1498 			master_idx = nla_get_u32(tb[IFLA_MASTER]);
1499 
1500 		if (tb[IFLA_LINKINFO])
1501 			kind_ops = linkinfo_to_kind_ops(tb[IFLA_LINKINFO]);
1502 
1503 		if (master_idx || kind_ops)
1504 			flags |= NLM_F_DUMP_FILTERED;
1505 	}
1506 
1507 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1508 		idx = 0;
1509 		head = &net->dev_index_head[h];
1510 		hlist_for_each_entry(dev, head, index_hlist) {
1511 			if (link_dump_filtered(dev, master_idx, kind_ops))
1512 				continue;
1513 			if (idx < s_idx)
1514 				goto cont;
1515 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1516 					       NETLINK_CB(cb->skb).portid,
1517 					       cb->nlh->nlmsg_seq, 0,
1518 					       flags,
1519 					       ext_filter_mask);
1520 			/* If we ran out of room on the first message,
1521 			 * we're in trouble
1522 			 */
1523 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1524 
1525 			if (err < 0)
1526 				goto out;
1527 
1528 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1529 cont:
1530 			idx++;
1531 		}
1532 	}
1533 out:
1534 	cb->args[1] = idx;
1535 	cb->args[0] = h;
1536 
1537 	return skb->len;
1538 }
1539 
1540 int rtnl_nla_parse_ifla(struct nlattr **tb, const struct nlattr *head, int len)
1541 {
1542 	return nla_parse(tb, IFLA_MAX, head, len, ifla_policy);
1543 }
1544 EXPORT_SYMBOL(rtnl_nla_parse_ifla);
1545 
1546 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1547 {
1548 	struct net *net;
1549 	/* Examine the link attributes and figure out which
1550 	 * network namespace we are talking about.
1551 	 */
1552 	if (tb[IFLA_NET_NS_PID])
1553 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1554 	else if (tb[IFLA_NET_NS_FD])
1555 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1556 	else
1557 		net = get_net(src_net);
1558 	return net;
1559 }
1560 EXPORT_SYMBOL(rtnl_link_get_net);
1561 
1562 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1563 {
1564 	if (dev) {
1565 		if (tb[IFLA_ADDRESS] &&
1566 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1567 			return -EINVAL;
1568 
1569 		if (tb[IFLA_BROADCAST] &&
1570 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1571 			return -EINVAL;
1572 	}
1573 
1574 	if (tb[IFLA_AF_SPEC]) {
1575 		struct nlattr *af;
1576 		int rem, err;
1577 
1578 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1579 			const struct rtnl_af_ops *af_ops;
1580 
1581 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1582 				return -EAFNOSUPPORT;
1583 
1584 			if (!af_ops->set_link_af)
1585 				return -EOPNOTSUPP;
1586 
1587 			if (af_ops->validate_link_af) {
1588 				err = af_ops->validate_link_af(dev, af);
1589 				if (err < 0)
1590 					return err;
1591 			}
1592 		}
1593 	}
1594 
1595 	return 0;
1596 }
1597 
1598 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
1599 				  int guid_type)
1600 {
1601 	const struct net_device_ops *ops = dev->netdev_ops;
1602 
1603 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
1604 }
1605 
1606 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
1607 {
1608 	if (dev->type != ARPHRD_INFINIBAND)
1609 		return -EOPNOTSUPP;
1610 
1611 	return handle_infiniband_guid(dev, ivt, guid_type);
1612 }
1613 
1614 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
1615 {
1616 	const struct net_device_ops *ops = dev->netdev_ops;
1617 	int err = -EINVAL;
1618 
1619 	if (tb[IFLA_VF_MAC]) {
1620 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
1621 
1622 		err = -EOPNOTSUPP;
1623 		if (ops->ndo_set_vf_mac)
1624 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
1625 						  ivm->mac);
1626 		if (err < 0)
1627 			return err;
1628 	}
1629 
1630 	if (tb[IFLA_VF_VLAN]) {
1631 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
1632 
1633 		err = -EOPNOTSUPP;
1634 		if (ops->ndo_set_vf_vlan)
1635 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
1636 						   ivv->qos);
1637 		if (err < 0)
1638 			return err;
1639 	}
1640 
1641 	if (tb[IFLA_VF_TX_RATE]) {
1642 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
1643 		struct ifla_vf_info ivf;
1644 
1645 		err = -EOPNOTSUPP;
1646 		if (ops->ndo_get_vf_config)
1647 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
1648 		if (err < 0)
1649 			return err;
1650 
1651 		err = -EOPNOTSUPP;
1652 		if (ops->ndo_set_vf_rate)
1653 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1654 						   ivf.min_tx_rate,
1655 						   ivt->rate);
1656 		if (err < 0)
1657 			return err;
1658 	}
1659 
1660 	if (tb[IFLA_VF_RATE]) {
1661 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
1662 
1663 		err = -EOPNOTSUPP;
1664 		if (ops->ndo_set_vf_rate)
1665 			err = ops->ndo_set_vf_rate(dev, ivt->vf,
1666 						   ivt->min_tx_rate,
1667 						   ivt->max_tx_rate);
1668 		if (err < 0)
1669 			return err;
1670 	}
1671 
1672 	if (tb[IFLA_VF_SPOOFCHK]) {
1673 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
1674 
1675 		err = -EOPNOTSUPP;
1676 		if (ops->ndo_set_vf_spoofchk)
1677 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1678 						       ivs->setting);
1679 		if (err < 0)
1680 			return err;
1681 	}
1682 
1683 	if (tb[IFLA_VF_LINK_STATE]) {
1684 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
1685 
1686 		err = -EOPNOTSUPP;
1687 		if (ops->ndo_set_vf_link_state)
1688 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
1689 							 ivl->link_state);
1690 		if (err < 0)
1691 			return err;
1692 	}
1693 
1694 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
1695 		struct ifla_vf_rss_query_en *ivrssq_en;
1696 
1697 		err = -EOPNOTSUPP;
1698 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
1699 		if (ops->ndo_set_vf_rss_query_en)
1700 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
1701 							   ivrssq_en->setting);
1702 		if (err < 0)
1703 			return err;
1704 	}
1705 
1706 	if (tb[IFLA_VF_TRUST]) {
1707 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
1708 
1709 		err = -EOPNOTSUPP;
1710 		if (ops->ndo_set_vf_trust)
1711 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
1712 		if (err < 0)
1713 			return err;
1714 	}
1715 
1716 	if (tb[IFLA_VF_IB_NODE_GUID]) {
1717 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
1718 
1719 		if (!ops->ndo_set_vf_guid)
1720 			return -EOPNOTSUPP;
1721 
1722 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
1723 	}
1724 
1725 	if (tb[IFLA_VF_IB_PORT_GUID]) {
1726 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
1727 
1728 		if (!ops->ndo_set_vf_guid)
1729 			return -EOPNOTSUPP;
1730 
1731 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
1732 	}
1733 
1734 	return err;
1735 }
1736 
1737 static int do_set_master(struct net_device *dev, int ifindex)
1738 {
1739 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
1740 	const struct net_device_ops *ops;
1741 	int err;
1742 
1743 	if (upper_dev) {
1744 		if (upper_dev->ifindex == ifindex)
1745 			return 0;
1746 		ops = upper_dev->netdev_ops;
1747 		if (ops->ndo_del_slave) {
1748 			err = ops->ndo_del_slave(upper_dev, dev);
1749 			if (err)
1750 				return err;
1751 		} else {
1752 			return -EOPNOTSUPP;
1753 		}
1754 	}
1755 
1756 	if (ifindex) {
1757 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
1758 		if (!upper_dev)
1759 			return -EINVAL;
1760 		ops = upper_dev->netdev_ops;
1761 		if (ops->ndo_add_slave) {
1762 			err = ops->ndo_add_slave(upper_dev, dev);
1763 			if (err)
1764 				return err;
1765 		} else {
1766 			return -EOPNOTSUPP;
1767 		}
1768 	}
1769 	return 0;
1770 }
1771 
1772 #define DO_SETLINK_MODIFIED	0x01
1773 /* notify flag means notify + modified. */
1774 #define DO_SETLINK_NOTIFY	0x03
1775 static int do_setlink(const struct sk_buff *skb,
1776 		      struct net_device *dev, struct ifinfomsg *ifm,
1777 		      struct nlattr **tb, char *ifname, int status)
1778 {
1779 	const struct net_device_ops *ops = dev->netdev_ops;
1780 	int err;
1781 
1782 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1783 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1784 		if (IS_ERR(net)) {
1785 			err = PTR_ERR(net);
1786 			goto errout;
1787 		}
1788 		if (!netlink_ns_capable(skb, net->user_ns, CAP_NET_ADMIN)) {
1789 			put_net(net);
1790 			err = -EPERM;
1791 			goto errout;
1792 		}
1793 		err = dev_change_net_namespace(dev, net, ifname);
1794 		put_net(net);
1795 		if (err)
1796 			goto errout;
1797 		status |= DO_SETLINK_MODIFIED;
1798 	}
1799 
1800 	if (tb[IFLA_MAP]) {
1801 		struct rtnl_link_ifmap *u_map;
1802 		struct ifmap k_map;
1803 
1804 		if (!ops->ndo_set_config) {
1805 			err = -EOPNOTSUPP;
1806 			goto errout;
1807 		}
1808 
1809 		if (!netif_device_present(dev)) {
1810 			err = -ENODEV;
1811 			goto errout;
1812 		}
1813 
1814 		u_map = nla_data(tb[IFLA_MAP]);
1815 		k_map.mem_start = (unsigned long) u_map->mem_start;
1816 		k_map.mem_end = (unsigned long) u_map->mem_end;
1817 		k_map.base_addr = (unsigned short) u_map->base_addr;
1818 		k_map.irq = (unsigned char) u_map->irq;
1819 		k_map.dma = (unsigned char) u_map->dma;
1820 		k_map.port = (unsigned char) u_map->port;
1821 
1822 		err = ops->ndo_set_config(dev, &k_map);
1823 		if (err < 0)
1824 			goto errout;
1825 
1826 		status |= DO_SETLINK_NOTIFY;
1827 	}
1828 
1829 	if (tb[IFLA_ADDRESS]) {
1830 		struct sockaddr *sa;
1831 		int len;
1832 
1833 		len = sizeof(sa_family_t) + dev->addr_len;
1834 		sa = kmalloc(len, GFP_KERNEL);
1835 		if (!sa) {
1836 			err = -ENOMEM;
1837 			goto errout;
1838 		}
1839 		sa->sa_family = dev->type;
1840 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1841 		       dev->addr_len);
1842 		err = dev_set_mac_address(dev, sa);
1843 		kfree(sa);
1844 		if (err)
1845 			goto errout;
1846 		status |= DO_SETLINK_MODIFIED;
1847 	}
1848 
1849 	if (tb[IFLA_MTU]) {
1850 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1851 		if (err < 0)
1852 			goto errout;
1853 		status |= DO_SETLINK_MODIFIED;
1854 	}
1855 
1856 	if (tb[IFLA_GROUP]) {
1857 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1858 		status |= DO_SETLINK_NOTIFY;
1859 	}
1860 
1861 	/*
1862 	 * Interface selected by interface index but interface
1863 	 * name provided implies that a name change has been
1864 	 * requested.
1865 	 */
1866 	if (ifm->ifi_index > 0 && ifname[0]) {
1867 		err = dev_change_name(dev, ifname);
1868 		if (err < 0)
1869 			goto errout;
1870 		status |= DO_SETLINK_MODIFIED;
1871 	}
1872 
1873 	if (tb[IFLA_IFALIAS]) {
1874 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1875 				    nla_len(tb[IFLA_IFALIAS]));
1876 		if (err < 0)
1877 			goto errout;
1878 		status |= DO_SETLINK_NOTIFY;
1879 	}
1880 
1881 	if (tb[IFLA_BROADCAST]) {
1882 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1883 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1884 	}
1885 
1886 	if (ifm->ifi_flags || ifm->ifi_change) {
1887 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1888 		if (err < 0)
1889 			goto errout;
1890 	}
1891 
1892 	if (tb[IFLA_MASTER]) {
1893 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1894 		if (err)
1895 			goto errout;
1896 		status |= DO_SETLINK_MODIFIED;
1897 	}
1898 
1899 	if (tb[IFLA_CARRIER]) {
1900 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
1901 		if (err)
1902 			goto errout;
1903 		status |= DO_SETLINK_MODIFIED;
1904 	}
1905 
1906 	if (tb[IFLA_TXQLEN]) {
1907 		unsigned long value = nla_get_u32(tb[IFLA_TXQLEN]);
1908 
1909 		if (dev->tx_queue_len ^ value)
1910 			status |= DO_SETLINK_NOTIFY;
1911 
1912 		dev->tx_queue_len = value;
1913 	}
1914 
1915 	if (tb[IFLA_OPERSTATE])
1916 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1917 
1918 	if (tb[IFLA_LINKMODE]) {
1919 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
1920 
1921 		write_lock_bh(&dev_base_lock);
1922 		if (dev->link_mode ^ value)
1923 			status |= DO_SETLINK_NOTIFY;
1924 		dev->link_mode = value;
1925 		write_unlock_bh(&dev_base_lock);
1926 	}
1927 
1928 	if (tb[IFLA_VFINFO_LIST]) {
1929 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
1930 		struct nlattr *attr;
1931 		int rem;
1932 
1933 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1934 			if (nla_type(attr) != IFLA_VF_INFO ||
1935 			    nla_len(attr) < NLA_HDRLEN) {
1936 				err = -EINVAL;
1937 				goto errout;
1938 			}
1939 			err = nla_parse_nested(vfinfo, IFLA_VF_MAX, attr,
1940 					       ifla_vf_policy);
1941 			if (err < 0)
1942 				goto errout;
1943 			err = do_setvfinfo(dev, vfinfo);
1944 			if (err < 0)
1945 				goto errout;
1946 			status |= DO_SETLINK_NOTIFY;
1947 		}
1948 	}
1949 	err = 0;
1950 
1951 	if (tb[IFLA_VF_PORTS]) {
1952 		struct nlattr *port[IFLA_PORT_MAX+1];
1953 		struct nlattr *attr;
1954 		int vf;
1955 		int rem;
1956 
1957 		err = -EOPNOTSUPP;
1958 		if (!ops->ndo_set_vf_port)
1959 			goto errout;
1960 
1961 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1962 			if (nla_type(attr) != IFLA_VF_PORT ||
1963 			    nla_len(attr) < NLA_HDRLEN) {
1964 				err = -EINVAL;
1965 				goto errout;
1966 			}
1967 			err = nla_parse_nested(port, IFLA_PORT_MAX, attr,
1968 					       ifla_port_policy);
1969 			if (err < 0)
1970 				goto errout;
1971 			if (!port[IFLA_PORT_VF]) {
1972 				err = -EOPNOTSUPP;
1973 				goto errout;
1974 			}
1975 			vf = nla_get_u32(port[IFLA_PORT_VF]);
1976 			err = ops->ndo_set_vf_port(dev, vf, port);
1977 			if (err < 0)
1978 				goto errout;
1979 			status |= DO_SETLINK_NOTIFY;
1980 		}
1981 	}
1982 	err = 0;
1983 
1984 	if (tb[IFLA_PORT_SELF]) {
1985 		struct nlattr *port[IFLA_PORT_MAX+1];
1986 
1987 		err = nla_parse_nested(port, IFLA_PORT_MAX,
1988 			tb[IFLA_PORT_SELF], ifla_port_policy);
1989 		if (err < 0)
1990 			goto errout;
1991 
1992 		err = -EOPNOTSUPP;
1993 		if (ops->ndo_set_vf_port)
1994 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1995 		if (err < 0)
1996 			goto errout;
1997 		status |= DO_SETLINK_NOTIFY;
1998 	}
1999 
2000 	if (tb[IFLA_AF_SPEC]) {
2001 		struct nlattr *af;
2002 		int rem;
2003 
2004 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2005 			const struct rtnl_af_ops *af_ops;
2006 
2007 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
2008 				BUG();
2009 
2010 			err = af_ops->set_link_af(dev, af);
2011 			if (err < 0)
2012 				goto errout;
2013 
2014 			status |= DO_SETLINK_NOTIFY;
2015 		}
2016 	}
2017 	err = 0;
2018 
2019 	if (tb[IFLA_PROTO_DOWN]) {
2020 		err = dev_change_proto_down(dev,
2021 					    nla_get_u8(tb[IFLA_PROTO_DOWN]));
2022 		if (err)
2023 			goto errout;
2024 		status |= DO_SETLINK_NOTIFY;
2025 	}
2026 
2027 errout:
2028 	if (status & DO_SETLINK_MODIFIED) {
2029 		if (status & DO_SETLINK_NOTIFY)
2030 			netdev_state_change(dev);
2031 
2032 		if (err < 0)
2033 			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",
2034 					     dev->name);
2035 	}
2036 
2037 	return err;
2038 }
2039 
2040 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2041 {
2042 	struct net *net = sock_net(skb->sk);
2043 	struct ifinfomsg *ifm;
2044 	struct net_device *dev;
2045 	int err;
2046 	struct nlattr *tb[IFLA_MAX+1];
2047 	char ifname[IFNAMSIZ];
2048 
2049 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2050 	if (err < 0)
2051 		goto errout;
2052 
2053 	if (tb[IFLA_IFNAME])
2054 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2055 	else
2056 		ifname[0] = '\0';
2057 
2058 	err = -EINVAL;
2059 	ifm = nlmsg_data(nlh);
2060 	if (ifm->ifi_index > 0)
2061 		dev = __dev_get_by_index(net, ifm->ifi_index);
2062 	else if (tb[IFLA_IFNAME])
2063 		dev = __dev_get_by_name(net, ifname);
2064 	else
2065 		goto errout;
2066 
2067 	if (dev == NULL) {
2068 		err = -ENODEV;
2069 		goto errout;
2070 	}
2071 
2072 	err = validate_linkmsg(dev, tb);
2073 	if (err < 0)
2074 		goto errout;
2075 
2076 	err = do_setlink(skb, dev, ifm, tb, ifname, 0);
2077 errout:
2078 	return err;
2079 }
2080 
2081 static int rtnl_group_dellink(const struct net *net, int group)
2082 {
2083 	struct net_device *dev, *aux;
2084 	LIST_HEAD(list_kill);
2085 	bool found = false;
2086 
2087 	if (!group)
2088 		return -EPERM;
2089 
2090 	for_each_netdev(net, dev) {
2091 		if (dev->group == group) {
2092 			const struct rtnl_link_ops *ops;
2093 
2094 			found = true;
2095 			ops = dev->rtnl_link_ops;
2096 			if (!ops || !ops->dellink)
2097 				return -EOPNOTSUPP;
2098 		}
2099 	}
2100 
2101 	if (!found)
2102 		return -ENODEV;
2103 
2104 	for_each_netdev_safe(net, dev, aux) {
2105 		if (dev->group == group) {
2106 			const struct rtnl_link_ops *ops;
2107 
2108 			ops = dev->rtnl_link_ops;
2109 			ops->dellink(dev, &list_kill);
2110 		}
2111 	}
2112 	unregister_netdevice_many(&list_kill);
2113 
2114 	return 0;
2115 }
2116 
2117 int rtnl_delete_link(struct net_device *dev)
2118 {
2119 	const struct rtnl_link_ops *ops;
2120 	LIST_HEAD(list_kill);
2121 
2122 	ops = dev->rtnl_link_ops;
2123 	if (!ops || !ops->dellink)
2124 		return -EOPNOTSUPP;
2125 
2126 	ops->dellink(dev, &list_kill);
2127 	unregister_netdevice_many(&list_kill);
2128 
2129 	return 0;
2130 }
2131 EXPORT_SYMBOL_GPL(rtnl_delete_link);
2132 
2133 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
2134 {
2135 	struct net *net = sock_net(skb->sk);
2136 	struct net_device *dev;
2137 	struct ifinfomsg *ifm;
2138 	char ifname[IFNAMSIZ];
2139 	struct nlattr *tb[IFLA_MAX+1];
2140 	int err;
2141 
2142 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2143 	if (err < 0)
2144 		return err;
2145 
2146 	if (tb[IFLA_IFNAME])
2147 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2148 
2149 	ifm = nlmsg_data(nlh);
2150 	if (ifm->ifi_index > 0)
2151 		dev = __dev_get_by_index(net, ifm->ifi_index);
2152 	else if (tb[IFLA_IFNAME])
2153 		dev = __dev_get_by_name(net, ifname);
2154 	else if (tb[IFLA_GROUP])
2155 		return rtnl_group_dellink(net, nla_get_u32(tb[IFLA_GROUP]));
2156 	else
2157 		return -EINVAL;
2158 
2159 	if (!dev)
2160 		return -ENODEV;
2161 
2162 	return rtnl_delete_link(dev);
2163 }
2164 
2165 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
2166 {
2167 	unsigned int old_flags;
2168 	int err;
2169 
2170 	old_flags = dev->flags;
2171 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
2172 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
2173 		if (err < 0)
2174 			return err;
2175 	}
2176 
2177 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
2178 
2179 	__dev_notify_flags(dev, old_flags, ~0U);
2180 	return 0;
2181 }
2182 EXPORT_SYMBOL(rtnl_configure_link);
2183 
2184 struct net_device *rtnl_create_link(struct net *net,
2185 	const char *ifname, unsigned char name_assign_type,
2186 	const struct rtnl_link_ops *ops, struct nlattr *tb[])
2187 {
2188 	int err;
2189 	struct net_device *dev;
2190 	unsigned int num_tx_queues = 1;
2191 	unsigned int num_rx_queues = 1;
2192 
2193 	if (tb[IFLA_NUM_TX_QUEUES])
2194 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
2195 	else if (ops->get_num_tx_queues)
2196 		num_tx_queues = ops->get_num_tx_queues();
2197 
2198 	if (tb[IFLA_NUM_RX_QUEUES])
2199 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
2200 	else if (ops->get_num_rx_queues)
2201 		num_rx_queues = ops->get_num_rx_queues();
2202 
2203 	err = -ENOMEM;
2204 	dev = alloc_netdev_mqs(ops->priv_size, ifname, name_assign_type,
2205 			       ops->setup, num_tx_queues, num_rx_queues);
2206 	if (!dev)
2207 		goto err;
2208 
2209 	dev_net_set(dev, net);
2210 	dev->rtnl_link_ops = ops;
2211 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
2212 
2213 	if (tb[IFLA_MTU])
2214 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
2215 	if (tb[IFLA_ADDRESS]) {
2216 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
2217 				nla_len(tb[IFLA_ADDRESS]));
2218 		dev->addr_assign_type = NET_ADDR_SET;
2219 	}
2220 	if (tb[IFLA_BROADCAST])
2221 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
2222 				nla_len(tb[IFLA_BROADCAST]));
2223 	if (tb[IFLA_TXQLEN])
2224 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
2225 	if (tb[IFLA_OPERSTATE])
2226 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2227 	if (tb[IFLA_LINKMODE])
2228 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
2229 	if (tb[IFLA_GROUP])
2230 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2231 
2232 	return dev;
2233 
2234 err:
2235 	return ERR_PTR(err);
2236 }
2237 EXPORT_SYMBOL(rtnl_create_link);
2238 
2239 static int rtnl_group_changelink(const struct sk_buff *skb,
2240 		struct net *net, int group,
2241 		struct ifinfomsg *ifm,
2242 		struct nlattr **tb)
2243 {
2244 	struct net_device *dev, *aux;
2245 	int err;
2246 
2247 	for_each_netdev_safe(net, dev, aux) {
2248 		if (dev->group == group) {
2249 			err = do_setlink(skb, dev, ifm, tb, NULL, 0);
2250 			if (err < 0)
2251 				return err;
2252 		}
2253 	}
2254 
2255 	return 0;
2256 }
2257 
2258 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh)
2259 {
2260 	struct net *net = sock_net(skb->sk);
2261 	const struct rtnl_link_ops *ops;
2262 	const struct rtnl_link_ops *m_ops = NULL;
2263 	struct net_device *dev;
2264 	struct net_device *master_dev = NULL;
2265 	struct ifinfomsg *ifm;
2266 	char kind[MODULE_NAME_LEN];
2267 	char ifname[IFNAMSIZ];
2268 	struct nlattr *tb[IFLA_MAX+1];
2269 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
2270 	unsigned char name_assign_type = NET_NAME_USER;
2271 	int err;
2272 
2273 #ifdef CONFIG_MODULES
2274 replay:
2275 #endif
2276 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2277 	if (err < 0)
2278 		return err;
2279 
2280 	if (tb[IFLA_IFNAME])
2281 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2282 	else
2283 		ifname[0] = '\0';
2284 
2285 	ifm = nlmsg_data(nlh);
2286 	if (ifm->ifi_index > 0)
2287 		dev = __dev_get_by_index(net, ifm->ifi_index);
2288 	else {
2289 		if (ifname[0])
2290 			dev = __dev_get_by_name(net, ifname);
2291 		else
2292 			dev = NULL;
2293 	}
2294 
2295 	if (dev) {
2296 		master_dev = netdev_master_upper_dev_get(dev);
2297 		if (master_dev)
2298 			m_ops = master_dev->rtnl_link_ops;
2299 	}
2300 
2301 	err = validate_linkmsg(dev, tb);
2302 	if (err < 0)
2303 		return err;
2304 
2305 	if (tb[IFLA_LINKINFO]) {
2306 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
2307 				       tb[IFLA_LINKINFO], ifla_info_policy);
2308 		if (err < 0)
2309 			return err;
2310 	} else
2311 		memset(linkinfo, 0, sizeof(linkinfo));
2312 
2313 	if (linkinfo[IFLA_INFO_KIND]) {
2314 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
2315 		ops = rtnl_link_ops_get(kind);
2316 	} else {
2317 		kind[0] = '\0';
2318 		ops = NULL;
2319 	}
2320 
2321 	if (1) {
2322 		struct nlattr *attr[ops ? ops->maxtype + 1 : 1];
2323 		struct nlattr *slave_attr[m_ops ? m_ops->slave_maxtype + 1 : 1];
2324 		struct nlattr **data = NULL;
2325 		struct nlattr **slave_data = NULL;
2326 		struct net *dest_net, *link_net = NULL;
2327 
2328 		if (ops) {
2329 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
2330 				err = nla_parse_nested(attr, ops->maxtype,
2331 						       linkinfo[IFLA_INFO_DATA],
2332 						       ops->policy);
2333 				if (err < 0)
2334 					return err;
2335 				data = attr;
2336 			}
2337 			if (ops->validate) {
2338 				err = ops->validate(tb, data);
2339 				if (err < 0)
2340 					return err;
2341 			}
2342 		}
2343 
2344 		if (m_ops) {
2345 			if (m_ops->slave_maxtype &&
2346 			    linkinfo[IFLA_INFO_SLAVE_DATA]) {
2347 				err = nla_parse_nested(slave_attr,
2348 						       m_ops->slave_maxtype,
2349 						       linkinfo[IFLA_INFO_SLAVE_DATA],
2350 						       m_ops->slave_policy);
2351 				if (err < 0)
2352 					return err;
2353 				slave_data = slave_attr;
2354 			}
2355 			if (m_ops->slave_validate) {
2356 				err = m_ops->slave_validate(tb, slave_data);
2357 				if (err < 0)
2358 					return err;
2359 			}
2360 		}
2361 
2362 		if (dev) {
2363 			int status = 0;
2364 
2365 			if (nlh->nlmsg_flags & NLM_F_EXCL)
2366 				return -EEXIST;
2367 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
2368 				return -EOPNOTSUPP;
2369 
2370 			if (linkinfo[IFLA_INFO_DATA]) {
2371 				if (!ops || ops != dev->rtnl_link_ops ||
2372 				    !ops->changelink)
2373 					return -EOPNOTSUPP;
2374 
2375 				err = ops->changelink(dev, tb, data);
2376 				if (err < 0)
2377 					return err;
2378 				status |= DO_SETLINK_NOTIFY;
2379 			}
2380 
2381 			if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
2382 				if (!m_ops || !m_ops->slave_changelink)
2383 					return -EOPNOTSUPP;
2384 
2385 				err = m_ops->slave_changelink(master_dev, dev,
2386 							      tb, slave_data);
2387 				if (err < 0)
2388 					return err;
2389 				status |= DO_SETLINK_NOTIFY;
2390 			}
2391 
2392 			return do_setlink(skb, dev, ifm, tb, ifname, status);
2393 		}
2394 
2395 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
2396 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
2397 				return rtnl_group_changelink(skb, net,
2398 						nla_get_u32(tb[IFLA_GROUP]),
2399 						ifm, tb);
2400 			return -ENODEV;
2401 		}
2402 
2403 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
2404 			return -EOPNOTSUPP;
2405 
2406 		if (!ops) {
2407 #ifdef CONFIG_MODULES
2408 			if (kind[0]) {
2409 				__rtnl_unlock();
2410 				request_module("rtnl-link-%s", kind);
2411 				rtnl_lock();
2412 				ops = rtnl_link_ops_get(kind);
2413 				if (ops)
2414 					goto replay;
2415 			}
2416 #endif
2417 			return -EOPNOTSUPP;
2418 		}
2419 
2420 		if (!ops->setup)
2421 			return -EOPNOTSUPP;
2422 
2423 		if (!ifname[0]) {
2424 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
2425 			name_assign_type = NET_NAME_ENUM;
2426 		}
2427 
2428 		dest_net = rtnl_link_get_net(net, tb);
2429 		if (IS_ERR(dest_net))
2430 			return PTR_ERR(dest_net);
2431 
2432 		err = -EPERM;
2433 		if (!netlink_ns_capable(skb, dest_net->user_ns, CAP_NET_ADMIN))
2434 			goto out;
2435 
2436 		if (tb[IFLA_LINK_NETNSID]) {
2437 			int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
2438 
2439 			link_net = get_net_ns_by_id(dest_net, id);
2440 			if (!link_net) {
2441 				err =  -EINVAL;
2442 				goto out;
2443 			}
2444 			err = -EPERM;
2445 			if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
2446 				goto out;
2447 		}
2448 
2449 		dev = rtnl_create_link(link_net ? : dest_net, ifname,
2450 				       name_assign_type, ops, tb);
2451 		if (IS_ERR(dev)) {
2452 			err = PTR_ERR(dev);
2453 			goto out;
2454 		}
2455 
2456 		dev->ifindex = ifm->ifi_index;
2457 
2458 		if (ops->newlink) {
2459 			err = ops->newlink(link_net ? : net, dev, tb, data);
2460 			/* Drivers should call free_netdev() in ->destructor
2461 			 * and unregister it on failure after registration
2462 			 * so that device could be finally freed in rtnl_unlock.
2463 			 */
2464 			if (err < 0) {
2465 				/* If device is not registered at all, free it now */
2466 				if (dev->reg_state == NETREG_UNINITIALIZED)
2467 					free_netdev(dev);
2468 				goto out;
2469 			}
2470 		} else {
2471 			err = register_netdevice(dev);
2472 			if (err < 0) {
2473 				free_netdev(dev);
2474 				goto out;
2475 			}
2476 		}
2477 		err = rtnl_configure_link(dev, ifm);
2478 		if (err < 0)
2479 			goto out_unregister;
2480 		if (link_net) {
2481 			err = dev_change_net_namespace(dev, dest_net, ifname);
2482 			if (err < 0)
2483 				goto out_unregister;
2484 		}
2485 out:
2486 		if (link_net)
2487 			put_net(link_net);
2488 		put_net(dest_net);
2489 		return err;
2490 out_unregister:
2491 		if (ops->newlink) {
2492 			LIST_HEAD(list_kill);
2493 
2494 			ops->dellink(dev, &list_kill);
2495 			unregister_netdevice_many(&list_kill);
2496 		} else {
2497 			unregister_netdevice(dev);
2498 		}
2499 		goto out;
2500 	}
2501 }
2502 
2503 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh)
2504 {
2505 	struct net *net = sock_net(skb->sk);
2506 	struct ifinfomsg *ifm;
2507 	char ifname[IFNAMSIZ];
2508 	struct nlattr *tb[IFLA_MAX+1];
2509 	struct net_device *dev = NULL;
2510 	struct sk_buff *nskb;
2511 	int err;
2512 	u32 ext_filter_mask = 0;
2513 
2514 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
2515 	if (err < 0)
2516 		return err;
2517 
2518 	if (tb[IFLA_IFNAME])
2519 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2520 
2521 	if (tb[IFLA_EXT_MASK])
2522 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2523 
2524 	ifm = nlmsg_data(nlh);
2525 	if (ifm->ifi_index > 0)
2526 		dev = __dev_get_by_index(net, ifm->ifi_index);
2527 	else if (tb[IFLA_IFNAME])
2528 		dev = __dev_get_by_name(net, ifname);
2529 	else
2530 		return -EINVAL;
2531 
2532 	if (dev == NULL)
2533 		return -ENODEV;
2534 
2535 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
2536 	if (nskb == NULL)
2537 		return -ENOBUFS;
2538 
2539 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).portid,
2540 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
2541 	if (err < 0) {
2542 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
2543 		WARN_ON(err == -EMSGSIZE);
2544 		kfree_skb(nskb);
2545 	} else
2546 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
2547 
2548 	return err;
2549 }
2550 
2551 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
2552 {
2553 	struct net *net = sock_net(skb->sk);
2554 	struct net_device *dev;
2555 	struct nlattr *tb[IFLA_MAX+1];
2556 	u32 ext_filter_mask = 0;
2557 	u16 min_ifinfo_dump_size = 0;
2558 	int hdrlen;
2559 
2560 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
2561 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2562 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2563 
2564 	if (nlmsg_parse(nlh, hdrlen, tb, IFLA_MAX, ifla_policy) >= 0) {
2565 		if (tb[IFLA_EXT_MASK])
2566 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
2567 	}
2568 
2569 	if (!ext_filter_mask)
2570 		return NLMSG_GOODSIZE;
2571 	/*
2572 	 * traverse the list of net devices and compute the minimum
2573 	 * buffer size based upon the filter mask.
2574 	 */
2575 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
2576 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
2577 					     if_nlmsg_size(dev,
2578 						           ext_filter_mask));
2579 	}
2580 
2581 	return min_ifinfo_dump_size;
2582 }
2583 
2584 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
2585 {
2586 	int idx;
2587 	int s_idx = cb->family;
2588 
2589 	if (s_idx == 0)
2590 		s_idx = 1;
2591 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
2592 		int type = cb->nlh->nlmsg_type-RTM_BASE;
2593 		if (idx < s_idx || idx == PF_PACKET)
2594 			continue;
2595 		if (rtnl_msg_handlers[idx] == NULL ||
2596 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
2597 			continue;
2598 		if (idx > s_idx) {
2599 			memset(&cb->args[0], 0, sizeof(cb->args));
2600 			cb->prev_seq = 0;
2601 			cb->seq = 0;
2602 		}
2603 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
2604 			break;
2605 	}
2606 	cb->family = idx;
2607 
2608 	return skb->len;
2609 }
2610 
2611 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
2612 				       unsigned int change, gfp_t flags)
2613 {
2614 	struct net *net = dev_net(dev);
2615 	struct sk_buff *skb;
2616 	int err = -ENOBUFS;
2617 	size_t if_info_size;
2618 
2619 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), flags);
2620 	if (skb == NULL)
2621 		goto errout;
2622 
2623 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
2624 	if (err < 0) {
2625 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
2626 		WARN_ON(err == -EMSGSIZE);
2627 		kfree_skb(skb);
2628 		goto errout;
2629 	}
2630 	return skb;
2631 errout:
2632 	if (err < 0)
2633 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
2634 	return NULL;
2635 }
2636 
2637 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
2638 {
2639 	struct net *net = dev_net(dev);
2640 
2641 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
2642 }
2643 
2644 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
2645 		  gfp_t flags)
2646 {
2647 	struct sk_buff *skb;
2648 
2649 	if (dev->reg_state != NETREG_REGISTERED)
2650 		return;
2651 
2652 	skb = rtmsg_ifinfo_build_skb(type, dev, change, flags);
2653 	if (skb)
2654 		rtmsg_ifinfo_send(skb, dev, flags);
2655 }
2656 EXPORT_SYMBOL(rtmsg_ifinfo);
2657 
2658 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
2659 				   struct net_device *dev,
2660 				   u8 *addr, u16 vid, u32 pid, u32 seq,
2661 				   int type, unsigned int flags,
2662 				   int nlflags, u16 ndm_state)
2663 {
2664 	struct nlmsghdr *nlh;
2665 	struct ndmsg *ndm;
2666 
2667 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
2668 	if (!nlh)
2669 		return -EMSGSIZE;
2670 
2671 	ndm = nlmsg_data(nlh);
2672 	ndm->ndm_family  = AF_BRIDGE;
2673 	ndm->ndm_pad1	 = 0;
2674 	ndm->ndm_pad2    = 0;
2675 	ndm->ndm_flags	 = flags;
2676 	ndm->ndm_type	 = 0;
2677 	ndm->ndm_ifindex = dev->ifindex;
2678 	ndm->ndm_state   = ndm_state;
2679 
2680 	if (nla_put(skb, NDA_LLADDR, ETH_ALEN, addr))
2681 		goto nla_put_failure;
2682 	if (vid)
2683 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
2684 			goto nla_put_failure;
2685 
2686 	nlmsg_end(skb, nlh);
2687 	return 0;
2688 
2689 nla_put_failure:
2690 	nlmsg_cancel(skb, nlh);
2691 	return -EMSGSIZE;
2692 }
2693 
2694 static inline size_t rtnl_fdb_nlmsg_size(void)
2695 {
2696 	return NLMSG_ALIGN(sizeof(struct ndmsg)) + nla_total_size(ETH_ALEN);
2697 }
2698 
2699 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
2700 			    u16 ndm_state)
2701 {
2702 	struct net *net = dev_net(dev);
2703 	struct sk_buff *skb;
2704 	int err = -ENOBUFS;
2705 
2706 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(), GFP_ATOMIC);
2707 	if (!skb)
2708 		goto errout;
2709 
2710 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
2711 				      0, 0, type, NTF_SELF, 0, ndm_state);
2712 	if (err < 0) {
2713 		kfree_skb(skb);
2714 		goto errout;
2715 	}
2716 
2717 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
2718 	return;
2719 errout:
2720 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
2721 }
2722 
2723 /**
2724  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
2725  */
2726 int ndo_dflt_fdb_add(struct ndmsg *ndm,
2727 		     struct nlattr *tb[],
2728 		     struct net_device *dev,
2729 		     const unsigned char *addr, u16 vid,
2730 		     u16 flags)
2731 {
2732 	int err = -EINVAL;
2733 
2734 	/* If aging addresses are supported device will need to
2735 	 * implement its own handler for this.
2736 	 */
2737 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
2738 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2739 		return err;
2740 	}
2741 
2742 	if (vid) {
2743 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
2744 		return err;
2745 	}
2746 
2747 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2748 		err = dev_uc_add_excl(dev, addr);
2749 	else if (is_multicast_ether_addr(addr))
2750 		err = dev_mc_add_excl(dev, addr);
2751 
2752 	/* Only return duplicate errors if NLM_F_EXCL is set */
2753 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
2754 		err = 0;
2755 
2756 	return err;
2757 }
2758 EXPORT_SYMBOL(ndo_dflt_fdb_add);
2759 
2760 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid)
2761 {
2762 	u16 vid = 0;
2763 
2764 	if (vlan_attr) {
2765 		if (nla_len(vlan_attr) != sizeof(u16)) {
2766 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan\n");
2767 			return -EINVAL;
2768 		}
2769 
2770 		vid = nla_get_u16(vlan_attr);
2771 
2772 		if (!vid || vid >= VLAN_VID_MASK) {
2773 			pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid vlan id %d\n",
2774 				vid);
2775 			return -EINVAL;
2776 		}
2777 	}
2778 	*p_vid = vid;
2779 	return 0;
2780 }
2781 
2782 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh)
2783 {
2784 	struct net *net = sock_net(skb->sk);
2785 	struct ndmsg *ndm;
2786 	struct nlattr *tb[NDA_MAX+1];
2787 	struct net_device *dev;
2788 	u8 *addr;
2789 	u16 vid;
2790 	int err;
2791 
2792 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2793 	if (err < 0)
2794 		return err;
2795 
2796 	ndm = nlmsg_data(nlh);
2797 	if (ndm->ndm_ifindex == 0) {
2798 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid ifindex\n");
2799 		return -EINVAL;
2800 	}
2801 
2802 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2803 	if (dev == NULL) {
2804 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with unknown ifindex\n");
2805 		return -ENODEV;
2806 	}
2807 
2808 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2809 		pr_info("PF_BRIDGE: RTM_NEWNEIGH with invalid address\n");
2810 		return -EINVAL;
2811 	}
2812 
2813 	addr = nla_data(tb[NDA_LLADDR]);
2814 
2815 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2816 	if (err)
2817 		return err;
2818 
2819 	err = -EOPNOTSUPP;
2820 
2821 	/* Support fdb on master device the net/bridge default case */
2822 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2823 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2824 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2825 		const struct net_device_ops *ops = br_dev->netdev_ops;
2826 
2827 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
2828 				       nlh->nlmsg_flags);
2829 		if (err)
2830 			goto out;
2831 		else
2832 			ndm->ndm_flags &= ~NTF_MASTER;
2833 	}
2834 
2835 	/* Embedded bridge, macvlan, and any other device support */
2836 	if ((ndm->ndm_flags & NTF_SELF)) {
2837 		if (dev->netdev_ops->ndo_fdb_add)
2838 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
2839 							   vid,
2840 							   nlh->nlmsg_flags);
2841 		else
2842 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
2843 					       nlh->nlmsg_flags);
2844 
2845 		if (!err) {
2846 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
2847 					ndm->ndm_state);
2848 			ndm->ndm_flags &= ~NTF_SELF;
2849 		}
2850 	}
2851 out:
2852 	return err;
2853 }
2854 
2855 /**
2856  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
2857  */
2858 int ndo_dflt_fdb_del(struct ndmsg *ndm,
2859 		     struct nlattr *tb[],
2860 		     struct net_device *dev,
2861 		     const unsigned char *addr, u16 vid)
2862 {
2863 	int err = -EINVAL;
2864 
2865 	/* If aging addresses are supported device will need to
2866 	 * implement its own handler for this.
2867 	 */
2868 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
2869 		pr_info("%s: FDB only supports static addresses\n", dev->name);
2870 		return err;
2871 	}
2872 
2873 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
2874 		err = dev_uc_del(dev, addr);
2875 	else if (is_multicast_ether_addr(addr))
2876 		err = dev_mc_del(dev, addr);
2877 
2878 	return err;
2879 }
2880 EXPORT_SYMBOL(ndo_dflt_fdb_del);
2881 
2882 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh)
2883 {
2884 	struct net *net = sock_net(skb->sk);
2885 	struct ndmsg *ndm;
2886 	struct nlattr *tb[NDA_MAX+1];
2887 	struct net_device *dev;
2888 	int err = -EINVAL;
2889 	__u8 *addr;
2890 	u16 vid;
2891 
2892 	if (!netlink_capable(skb, CAP_NET_ADMIN))
2893 		return -EPERM;
2894 
2895 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, NULL);
2896 	if (err < 0)
2897 		return err;
2898 
2899 	ndm = nlmsg_data(nlh);
2900 	if (ndm->ndm_ifindex == 0) {
2901 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid ifindex\n");
2902 		return -EINVAL;
2903 	}
2904 
2905 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
2906 	if (dev == NULL) {
2907 		pr_info("PF_BRIDGE: RTM_DELNEIGH with unknown ifindex\n");
2908 		return -ENODEV;
2909 	}
2910 
2911 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
2912 		pr_info("PF_BRIDGE: RTM_DELNEIGH with invalid address\n");
2913 		return -EINVAL;
2914 	}
2915 
2916 	addr = nla_data(tb[NDA_LLADDR]);
2917 
2918 	err = fdb_vid_parse(tb[NDA_VLAN], &vid);
2919 	if (err)
2920 		return err;
2921 
2922 	err = -EOPNOTSUPP;
2923 
2924 	/* Support fdb on master device the net/bridge default case */
2925 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
2926 	    (dev->priv_flags & IFF_BRIDGE_PORT)) {
2927 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
2928 		const struct net_device_ops *ops = br_dev->netdev_ops;
2929 
2930 		if (ops->ndo_fdb_del)
2931 			err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid);
2932 
2933 		if (err)
2934 			goto out;
2935 		else
2936 			ndm->ndm_flags &= ~NTF_MASTER;
2937 	}
2938 
2939 	/* Embedded bridge, macvlan, and any other device support */
2940 	if (ndm->ndm_flags & NTF_SELF) {
2941 		if (dev->netdev_ops->ndo_fdb_del)
2942 			err = dev->netdev_ops->ndo_fdb_del(ndm, tb, dev, addr,
2943 							   vid);
2944 		else
2945 			err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
2946 
2947 		if (!err) {
2948 			rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
2949 					ndm->ndm_state);
2950 			ndm->ndm_flags &= ~NTF_SELF;
2951 		}
2952 	}
2953 out:
2954 	return err;
2955 }
2956 
2957 static int nlmsg_populate_fdb(struct sk_buff *skb,
2958 			      struct netlink_callback *cb,
2959 			      struct net_device *dev,
2960 			      int *idx,
2961 			      struct netdev_hw_addr_list *list)
2962 {
2963 	struct netdev_hw_addr *ha;
2964 	int err;
2965 	u32 portid, seq;
2966 
2967 	portid = NETLINK_CB(cb->skb).portid;
2968 	seq = cb->nlh->nlmsg_seq;
2969 
2970 	list_for_each_entry(ha, &list->list, list) {
2971 		if (*idx < cb->args[0])
2972 			goto skip;
2973 
2974 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
2975 					      portid, seq,
2976 					      RTM_NEWNEIGH, NTF_SELF,
2977 					      NLM_F_MULTI, NUD_PERMANENT);
2978 		if (err < 0)
2979 			return err;
2980 skip:
2981 		*idx += 1;
2982 	}
2983 	return 0;
2984 }
2985 
2986 /**
2987  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
2988  * @nlh: netlink message header
2989  * @dev: netdevice
2990  *
2991  * Default netdevice operation to dump the existing unicast address list.
2992  * Returns number of addresses from list put in skb.
2993  */
2994 int ndo_dflt_fdb_dump(struct sk_buff *skb,
2995 		      struct netlink_callback *cb,
2996 		      struct net_device *dev,
2997 		      struct net_device *filter_dev,
2998 		      int idx)
2999 {
3000 	int err;
3001 
3002 	netif_addr_lock_bh(dev);
3003 	err = nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->uc);
3004 	if (err)
3005 		goto out;
3006 	nlmsg_populate_fdb(skb, cb, dev, &idx, &dev->mc);
3007 out:
3008 	netif_addr_unlock_bh(dev);
3009 	cb->args[1] = err;
3010 	return idx;
3011 }
3012 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
3013 
3014 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
3015 {
3016 	struct net_device *dev;
3017 	struct nlattr *tb[IFLA_MAX+1];
3018 	struct net_device *br_dev = NULL;
3019 	const struct net_device_ops *ops = NULL;
3020 	const struct net_device_ops *cops = NULL;
3021 	struct ifinfomsg *ifm = nlmsg_data(cb->nlh);
3022 	struct net *net = sock_net(skb->sk);
3023 	int brport_idx = 0;
3024 	int br_idx = 0;
3025 	int idx = 0;
3026 
3027 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
3028 			ifla_policy) == 0) {
3029 		if (tb[IFLA_MASTER])
3030 			br_idx = nla_get_u32(tb[IFLA_MASTER]);
3031 	}
3032 
3033 	brport_idx = ifm->ifi_index;
3034 
3035 	if (br_idx) {
3036 		br_dev = __dev_get_by_index(net, br_idx);
3037 		if (!br_dev)
3038 			return -ENODEV;
3039 
3040 		ops = br_dev->netdev_ops;
3041 	}
3042 
3043 	cb->args[1] = 0;
3044 	for_each_netdev(net, dev) {
3045 		if (brport_idx && (dev->ifindex != brport_idx))
3046 			continue;
3047 
3048 		if (!br_idx) { /* user did not specify a specific bridge */
3049 			if (dev->priv_flags & IFF_BRIDGE_PORT) {
3050 				br_dev = netdev_master_upper_dev_get(dev);
3051 				cops = br_dev->netdev_ops;
3052 			}
3053 
3054 		} else {
3055 			if (dev != br_dev &&
3056 			    !(dev->priv_flags & IFF_BRIDGE_PORT))
3057 				continue;
3058 
3059 			if (br_dev != netdev_master_upper_dev_get(dev) &&
3060 			    !(dev->priv_flags & IFF_EBRIDGE))
3061 				continue;
3062 
3063 			cops = ops;
3064 		}
3065 
3066 		if (dev->priv_flags & IFF_BRIDGE_PORT) {
3067 			if (cops && cops->ndo_fdb_dump)
3068 				idx = cops->ndo_fdb_dump(skb, cb, br_dev, dev,
3069 							 idx);
3070 		}
3071 		if (cb->args[1] == -EMSGSIZE)
3072 			break;
3073 
3074 		if (dev->netdev_ops->ndo_fdb_dump)
3075 			idx = dev->netdev_ops->ndo_fdb_dump(skb, cb, dev, NULL,
3076 							    idx);
3077 		else
3078 			idx = ndo_dflt_fdb_dump(skb, cb, dev, NULL, idx);
3079 		if (cb->args[1] == -EMSGSIZE)
3080 			break;
3081 
3082 		cops = NULL;
3083 	}
3084 
3085 	cb->args[0] = idx;
3086 	return skb->len;
3087 }
3088 
3089 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
3090 			       unsigned int attrnum, unsigned int flag)
3091 {
3092 	if (mask & flag)
3093 		return nla_put_u8(skb, attrnum, !!(flags & flag));
3094 	return 0;
3095 }
3096 
3097 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
3098 			    struct net_device *dev, u16 mode,
3099 			    u32 flags, u32 mask, int nlflags,
3100 			    u32 filter_mask,
3101 			    int (*vlan_fill)(struct sk_buff *skb,
3102 					     struct net_device *dev,
3103 					     u32 filter_mask))
3104 {
3105 	struct nlmsghdr *nlh;
3106 	struct ifinfomsg *ifm;
3107 	struct nlattr *br_afspec;
3108 	struct nlattr *protinfo;
3109 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
3110 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3111 	int err = 0;
3112 
3113 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
3114 	if (nlh == NULL)
3115 		return -EMSGSIZE;
3116 
3117 	ifm = nlmsg_data(nlh);
3118 	ifm->ifi_family = AF_BRIDGE;
3119 	ifm->__ifi_pad = 0;
3120 	ifm->ifi_type = dev->type;
3121 	ifm->ifi_index = dev->ifindex;
3122 	ifm->ifi_flags = dev_get_flags(dev);
3123 	ifm->ifi_change = 0;
3124 
3125 
3126 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
3127 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
3128 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
3129 	    (br_dev &&
3130 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
3131 	    (dev->addr_len &&
3132 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
3133 	    (dev->ifindex != dev_get_iflink(dev) &&
3134 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
3135 		goto nla_put_failure;
3136 
3137 	br_afspec = nla_nest_start(skb, IFLA_AF_SPEC);
3138 	if (!br_afspec)
3139 		goto nla_put_failure;
3140 
3141 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
3142 		nla_nest_cancel(skb, br_afspec);
3143 		goto nla_put_failure;
3144 	}
3145 
3146 	if (mode != BRIDGE_MODE_UNDEF) {
3147 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
3148 			nla_nest_cancel(skb, br_afspec);
3149 			goto nla_put_failure;
3150 		}
3151 	}
3152 	if (vlan_fill) {
3153 		err = vlan_fill(skb, dev, filter_mask);
3154 		if (err) {
3155 			nla_nest_cancel(skb, br_afspec);
3156 			goto nla_put_failure;
3157 		}
3158 	}
3159 	nla_nest_end(skb, br_afspec);
3160 
3161 	protinfo = nla_nest_start(skb, IFLA_PROTINFO | NLA_F_NESTED);
3162 	if (!protinfo)
3163 		goto nla_put_failure;
3164 
3165 	if (brport_nla_put_flag(skb, flags, mask,
3166 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
3167 	    brport_nla_put_flag(skb, flags, mask,
3168 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
3169 	    brport_nla_put_flag(skb, flags, mask,
3170 				IFLA_BRPORT_FAST_LEAVE,
3171 				BR_MULTICAST_FAST_LEAVE) ||
3172 	    brport_nla_put_flag(skb, flags, mask,
3173 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
3174 	    brport_nla_put_flag(skb, flags, mask,
3175 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
3176 	    brport_nla_put_flag(skb, flags, mask,
3177 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
3178 	    brport_nla_put_flag(skb, flags, mask,
3179 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
3180 	    brport_nla_put_flag(skb, flags, mask,
3181 				IFLA_BRPORT_PROXYARP, BR_PROXYARP)) {
3182 		nla_nest_cancel(skb, protinfo);
3183 		goto nla_put_failure;
3184 	}
3185 
3186 	nla_nest_end(skb, protinfo);
3187 
3188 	nlmsg_end(skb, nlh);
3189 	return 0;
3190 nla_put_failure:
3191 	nlmsg_cancel(skb, nlh);
3192 	return err ? err : -EMSGSIZE;
3193 }
3194 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
3195 
3196 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
3197 {
3198 	struct net *net = sock_net(skb->sk);
3199 	struct net_device *dev;
3200 	int idx = 0;
3201 	u32 portid = NETLINK_CB(cb->skb).portid;
3202 	u32 seq = cb->nlh->nlmsg_seq;
3203 	u32 filter_mask = 0;
3204 	int err;
3205 
3206 	if (nlmsg_len(cb->nlh) > sizeof(struct ifinfomsg)) {
3207 		struct nlattr *extfilt;
3208 
3209 		extfilt = nlmsg_find_attr(cb->nlh, sizeof(struct ifinfomsg),
3210 					  IFLA_EXT_MASK);
3211 		if (extfilt) {
3212 			if (nla_len(extfilt) < sizeof(filter_mask))
3213 				return -EINVAL;
3214 
3215 			filter_mask = nla_get_u32(extfilt);
3216 		}
3217 	}
3218 
3219 	rcu_read_lock();
3220 	for_each_netdev_rcu(net, dev) {
3221 		const struct net_device_ops *ops = dev->netdev_ops;
3222 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3223 
3224 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
3225 			if (idx >= cb->args[0]) {
3226 				err = br_dev->netdev_ops->ndo_bridge_getlink(
3227 						skb, portid, seq, dev,
3228 						filter_mask, NLM_F_MULTI);
3229 				if (err < 0 && err != -EOPNOTSUPP)
3230 					break;
3231 			}
3232 			idx++;
3233 		}
3234 
3235 		if (ops->ndo_bridge_getlink) {
3236 			if (idx >= cb->args[0]) {
3237 				err = ops->ndo_bridge_getlink(skb, portid,
3238 							      seq, dev,
3239 							      filter_mask,
3240 							      NLM_F_MULTI);
3241 				if (err < 0 && err != -EOPNOTSUPP)
3242 					break;
3243 			}
3244 			idx++;
3245 		}
3246 	}
3247 	rcu_read_unlock();
3248 	cb->args[0] = idx;
3249 
3250 	return skb->len;
3251 }
3252 
3253 static inline size_t bridge_nlmsg_size(void)
3254 {
3255 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
3256 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
3257 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
3258 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
3259 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
3260 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
3261 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
3262 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
3263 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
3264 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
3265 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
3266 }
3267 
3268 static int rtnl_bridge_notify(struct net_device *dev)
3269 {
3270 	struct net *net = dev_net(dev);
3271 	struct sk_buff *skb;
3272 	int err = -EOPNOTSUPP;
3273 
3274 	if (!dev->netdev_ops->ndo_bridge_getlink)
3275 		return 0;
3276 
3277 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
3278 	if (!skb) {
3279 		err = -ENOMEM;
3280 		goto errout;
3281 	}
3282 
3283 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
3284 	if (err < 0)
3285 		goto errout;
3286 
3287 	if (!skb->len)
3288 		goto errout;
3289 
3290 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
3291 	return 0;
3292 errout:
3293 	WARN_ON(err == -EMSGSIZE);
3294 	kfree_skb(skb);
3295 	if (err)
3296 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3297 	return err;
3298 }
3299 
3300 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh)
3301 {
3302 	struct net *net = sock_net(skb->sk);
3303 	struct ifinfomsg *ifm;
3304 	struct net_device *dev;
3305 	struct nlattr *br_spec, *attr = NULL;
3306 	int rem, err = -EOPNOTSUPP;
3307 	u16 flags = 0;
3308 	bool have_flags = false;
3309 
3310 	if (nlmsg_len(nlh) < sizeof(*ifm))
3311 		return -EINVAL;
3312 
3313 	ifm = nlmsg_data(nlh);
3314 	if (ifm->ifi_family != AF_BRIDGE)
3315 		return -EPFNOSUPPORT;
3316 
3317 	dev = __dev_get_by_index(net, ifm->ifi_index);
3318 	if (!dev) {
3319 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3320 		return -ENODEV;
3321 	}
3322 
3323 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3324 	if (br_spec) {
3325 		nla_for_each_nested(attr, br_spec, rem) {
3326 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3327 				if (nla_len(attr) < sizeof(flags))
3328 					return -EINVAL;
3329 
3330 				have_flags = true;
3331 				flags = nla_get_u16(attr);
3332 				break;
3333 			}
3334 		}
3335 	}
3336 
3337 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3338 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3339 
3340 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
3341 			err = -EOPNOTSUPP;
3342 			goto out;
3343 		}
3344 
3345 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags);
3346 		if (err)
3347 			goto out;
3348 
3349 		flags &= ~BRIDGE_FLAGS_MASTER;
3350 	}
3351 
3352 	if ((flags & BRIDGE_FLAGS_SELF)) {
3353 		if (!dev->netdev_ops->ndo_bridge_setlink)
3354 			err = -EOPNOTSUPP;
3355 		else
3356 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
3357 								  flags);
3358 		if (!err) {
3359 			flags &= ~BRIDGE_FLAGS_SELF;
3360 
3361 			/* Generate event to notify upper layer of bridge
3362 			 * change
3363 			 */
3364 			err = rtnl_bridge_notify(dev);
3365 		}
3366 	}
3367 
3368 	if (have_flags)
3369 		memcpy(nla_data(attr), &flags, sizeof(flags));
3370 out:
3371 	return err;
3372 }
3373 
3374 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh)
3375 {
3376 	struct net *net = sock_net(skb->sk);
3377 	struct ifinfomsg *ifm;
3378 	struct net_device *dev;
3379 	struct nlattr *br_spec, *attr = NULL;
3380 	int rem, err = -EOPNOTSUPP;
3381 	u16 flags = 0;
3382 	bool have_flags = false;
3383 
3384 	if (nlmsg_len(nlh) < sizeof(*ifm))
3385 		return -EINVAL;
3386 
3387 	ifm = nlmsg_data(nlh);
3388 	if (ifm->ifi_family != AF_BRIDGE)
3389 		return -EPFNOSUPPORT;
3390 
3391 	dev = __dev_get_by_index(net, ifm->ifi_index);
3392 	if (!dev) {
3393 		pr_info("PF_BRIDGE: RTM_SETLINK with unknown ifindex\n");
3394 		return -ENODEV;
3395 	}
3396 
3397 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
3398 	if (br_spec) {
3399 		nla_for_each_nested(attr, br_spec, rem) {
3400 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
3401 				if (nla_len(attr) < sizeof(flags))
3402 					return -EINVAL;
3403 
3404 				have_flags = true;
3405 				flags = nla_get_u16(attr);
3406 				break;
3407 			}
3408 		}
3409 	}
3410 
3411 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
3412 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
3413 
3414 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
3415 			err = -EOPNOTSUPP;
3416 			goto out;
3417 		}
3418 
3419 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
3420 		if (err)
3421 			goto out;
3422 
3423 		flags &= ~BRIDGE_FLAGS_MASTER;
3424 	}
3425 
3426 	if ((flags & BRIDGE_FLAGS_SELF)) {
3427 		if (!dev->netdev_ops->ndo_bridge_dellink)
3428 			err = -EOPNOTSUPP;
3429 		else
3430 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
3431 								  flags);
3432 
3433 		if (!err) {
3434 			flags &= ~BRIDGE_FLAGS_SELF;
3435 
3436 			/* Generate event to notify upper layer of bridge
3437 			 * change
3438 			 */
3439 			err = rtnl_bridge_notify(dev);
3440 		}
3441 	}
3442 
3443 	if (have_flags)
3444 		memcpy(nla_data(attr), &flags, sizeof(flags));
3445 out:
3446 	return err;
3447 }
3448 
3449 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
3450 {
3451 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
3452 	       (!idxattr || idxattr == attrid);
3453 }
3454 
3455 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
3456 			       int type, u32 pid, u32 seq, u32 change,
3457 			       unsigned int flags, unsigned int filter_mask,
3458 			       int *idxattr, int *prividx)
3459 {
3460 	struct if_stats_msg *ifsm;
3461 	struct nlmsghdr *nlh;
3462 	struct nlattr *attr;
3463 	int s_prividx = *prividx;
3464 
3465 	ASSERT_RTNL();
3466 
3467 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
3468 	if (!nlh)
3469 		return -EMSGSIZE;
3470 
3471 	ifsm = nlmsg_data(nlh);
3472 	ifsm->ifindex = dev->ifindex;
3473 	ifsm->filter_mask = filter_mask;
3474 
3475 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
3476 		struct rtnl_link_stats64 *sp;
3477 
3478 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
3479 					 sizeof(struct rtnl_link_stats64),
3480 					 IFLA_STATS_UNSPEC);
3481 		if (!attr)
3482 			goto nla_put_failure;
3483 
3484 		sp = nla_data(attr);
3485 		dev_get_stats(dev, sp);
3486 	}
3487 
3488 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
3489 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3490 
3491 		if (ops && ops->fill_linkxstats) {
3492 			int err;
3493 
3494 			*idxattr = IFLA_STATS_LINK_XSTATS;
3495 			attr = nla_nest_start(skb,
3496 					      IFLA_STATS_LINK_XSTATS);
3497 			if (!attr)
3498 				goto nla_put_failure;
3499 
3500 			err = ops->fill_linkxstats(skb, dev, prividx);
3501 			nla_nest_end(skb, attr);
3502 			if (err)
3503 				goto nla_put_failure;
3504 			*idxattr = 0;
3505 		}
3506 	}
3507 
3508 	nlmsg_end(skb, nlh);
3509 
3510 	return 0;
3511 
3512 nla_put_failure:
3513 	/* not a multi message or no progress mean a real error */
3514 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
3515 		nlmsg_cancel(skb, nlh);
3516 	else
3517 		nlmsg_end(skb, nlh);
3518 
3519 	return -EMSGSIZE;
3520 }
3521 
3522 static const struct nla_policy ifla_stats_policy[IFLA_STATS_MAX + 1] = {
3523 	[IFLA_STATS_LINK_64]	= { .len = sizeof(struct rtnl_link_stats64) },
3524 };
3525 
3526 static size_t if_nlmsg_stats_size(const struct net_device *dev,
3527 				  u32 filter_mask)
3528 {
3529 	size_t size = 0;
3530 
3531 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
3532 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
3533 
3534 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
3535 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
3536 
3537 		if (ops && ops->get_linkxstats_size) {
3538 			size += nla_total_size(ops->get_linkxstats_size(dev));
3539 			/* for IFLA_STATS_LINK_XSTATS */
3540 			size += nla_total_size(0);
3541 		}
3542 	}
3543 
3544 	return size;
3545 }
3546 
3547 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh)
3548 {
3549 	struct net *net = sock_net(skb->sk);
3550 	struct net_device *dev = NULL;
3551 	int idxattr = 0, prividx = 0;
3552 	struct if_stats_msg *ifsm;
3553 	struct sk_buff *nskb;
3554 	u32 filter_mask;
3555 	int err;
3556 
3557 	ifsm = nlmsg_data(nlh);
3558 	if (ifsm->ifindex > 0)
3559 		dev = __dev_get_by_index(net, ifsm->ifindex);
3560 	else
3561 		return -EINVAL;
3562 
3563 	if (!dev)
3564 		return -ENODEV;
3565 
3566 	filter_mask = ifsm->filter_mask;
3567 	if (!filter_mask)
3568 		return -EINVAL;
3569 
3570 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, filter_mask), GFP_KERNEL);
3571 	if (!nskb)
3572 		return -ENOBUFS;
3573 
3574 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
3575 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
3576 				  0, filter_mask, &idxattr, &prividx);
3577 	if (err < 0) {
3578 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
3579 		WARN_ON(err == -EMSGSIZE);
3580 		kfree_skb(nskb);
3581 	} else {
3582 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3583 	}
3584 
3585 	return err;
3586 }
3587 
3588 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
3589 {
3590 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
3591 	struct net *net = sock_net(skb->sk);
3592 	unsigned int flags = NLM_F_MULTI;
3593 	struct if_stats_msg *ifsm;
3594 	struct hlist_head *head;
3595 	struct net_device *dev;
3596 	u32 filter_mask = 0;
3597 	int idx = 0;
3598 
3599 	s_h = cb->args[0];
3600 	s_idx = cb->args[1];
3601 	s_idxattr = cb->args[2];
3602 	s_prividx = cb->args[3];
3603 
3604 	cb->seq = net->dev_base_seq;
3605 
3606 	ifsm = nlmsg_data(cb->nlh);
3607 	filter_mask = ifsm->filter_mask;
3608 	if (!filter_mask)
3609 		return -EINVAL;
3610 
3611 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
3612 		idx = 0;
3613 		head = &net->dev_index_head[h];
3614 		hlist_for_each_entry(dev, head, index_hlist) {
3615 			if (idx < s_idx)
3616 				goto cont;
3617 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
3618 						  NETLINK_CB(cb->skb).portid,
3619 						  cb->nlh->nlmsg_seq, 0,
3620 						  flags, filter_mask,
3621 						  &s_idxattr, &s_prividx);
3622 			/* If we ran out of room on the first message,
3623 			 * we're in trouble
3624 			 */
3625 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
3626 
3627 			if (err < 0)
3628 				goto out;
3629 			s_prividx = 0;
3630 			s_idxattr = 0;
3631 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
3632 cont:
3633 			idx++;
3634 		}
3635 	}
3636 out:
3637 	cb->args[3] = s_prividx;
3638 	cb->args[2] = s_idxattr;
3639 	cb->args[1] = idx;
3640 	cb->args[0] = h;
3641 
3642 	return skb->len;
3643 }
3644 
3645 /* Process one rtnetlink message. */
3646 
3647 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
3648 {
3649 	struct net *net = sock_net(skb->sk);
3650 	rtnl_doit_func doit;
3651 	int kind;
3652 	int family;
3653 	int type;
3654 	int err;
3655 
3656 	type = nlh->nlmsg_type;
3657 	if (type > RTM_MAX)
3658 		return -EOPNOTSUPP;
3659 
3660 	type -= RTM_BASE;
3661 
3662 	/* All the messages must have at least 1 byte length */
3663 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
3664 		return 0;
3665 
3666 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
3667 	kind = type&3;
3668 
3669 	if (kind != 2 && !netlink_net_capable(skb, CAP_NET_ADMIN))
3670 		return -EPERM;
3671 
3672 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
3673 		struct sock *rtnl;
3674 		rtnl_dumpit_func dumpit;
3675 		rtnl_calcit_func calcit;
3676 		u16 min_dump_alloc = 0;
3677 
3678 		dumpit = rtnl_get_dumpit(family, type);
3679 		if (dumpit == NULL)
3680 			return -EOPNOTSUPP;
3681 		calcit = rtnl_get_calcit(family, type);
3682 		if (calcit)
3683 			min_dump_alloc = calcit(skb, nlh);
3684 
3685 		__rtnl_unlock();
3686 		rtnl = net->rtnl;
3687 		{
3688 			struct netlink_dump_control c = {
3689 				.dump		= dumpit,
3690 				.min_dump_alloc	= min_dump_alloc,
3691 			};
3692 			err = netlink_dump_start(rtnl, skb, nlh, &c);
3693 		}
3694 		rtnl_lock();
3695 		return err;
3696 	}
3697 
3698 	doit = rtnl_get_doit(family, type);
3699 	if (doit == NULL)
3700 		return -EOPNOTSUPP;
3701 
3702 	return doit(skb, nlh);
3703 }
3704 
3705 static void rtnetlink_rcv(struct sk_buff *skb)
3706 {
3707 	rtnl_lock();
3708 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
3709 	rtnl_unlock();
3710 }
3711 
3712 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
3713 {
3714 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
3715 
3716 	switch (event) {
3717 	case NETDEV_UP:
3718 	case NETDEV_DOWN:
3719 	case NETDEV_PRE_UP:
3720 	case NETDEV_POST_INIT:
3721 	case NETDEV_REGISTER:
3722 	case NETDEV_CHANGE:
3723 	case NETDEV_PRE_TYPE_CHANGE:
3724 	case NETDEV_GOING_DOWN:
3725 	case NETDEV_UNREGISTER:
3726 	case NETDEV_UNREGISTER_FINAL:
3727 	case NETDEV_RELEASE:
3728 	case NETDEV_JOIN:
3729 	case NETDEV_BONDING_INFO:
3730 		break;
3731 	default:
3732 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
3733 		break;
3734 	}
3735 	return NOTIFY_DONE;
3736 }
3737 
3738 static struct notifier_block rtnetlink_dev_notifier = {
3739 	.notifier_call	= rtnetlink_event,
3740 };
3741 
3742 
3743 static int __net_init rtnetlink_net_init(struct net *net)
3744 {
3745 	struct sock *sk;
3746 	struct netlink_kernel_cfg cfg = {
3747 		.groups		= RTNLGRP_MAX,
3748 		.input		= rtnetlink_rcv,
3749 		.cb_mutex	= &rtnl_mutex,
3750 		.flags		= NL_CFG_F_NONROOT_RECV,
3751 	};
3752 
3753 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
3754 	if (!sk)
3755 		return -ENOMEM;
3756 	net->rtnl = sk;
3757 	return 0;
3758 }
3759 
3760 static void __net_exit rtnetlink_net_exit(struct net *net)
3761 {
3762 	netlink_kernel_release(net->rtnl);
3763 	net->rtnl = NULL;
3764 }
3765 
3766 static struct pernet_operations rtnetlink_net_ops = {
3767 	.init = rtnetlink_net_init,
3768 	.exit = rtnetlink_net_exit,
3769 };
3770 
3771 void __init rtnetlink_init(void)
3772 {
3773 	if (register_pernet_subsys(&rtnetlink_net_ops))
3774 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
3775 
3776 	register_netdevice_notifier(&rtnetlink_dev_notifier);
3777 
3778 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
3779 		      rtnl_dump_ifinfo, rtnl_calcit);
3780 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
3781 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
3782 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
3783 
3784 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
3785 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
3786 
3787 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, NULL);
3788 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL, NULL);
3789 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, NULL, rtnl_fdb_dump, NULL);
3790 
3791 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, NULL);
3792 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, NULL);
3793 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, NULL);
3794 
3795 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
3796 		      NULL);
3797 }
3798