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