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