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