• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * INET		An implementation of the TCP/IP protocol suite for the LINUX
4  *		operating system.  INET is implemented using the  BSD Socket
5  *		interface as the means of communication with the user level.
6  *
7  *		Routing netlink socket interface: protocol independent part.
8  *
9  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10  *
11  *	Fixes:
12  *	Vitaly E. Lavrov		RTA_OK arithmetic was wrong.
13  */
14 
15 #include <linux/bitops.h>
16 #include <linux/errno.h>
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/timer.h>
22 #include <linux/string.h>
23 #include <linux/sockios.h>
24 #include <linux/net.h>
25 #include <linux/fcntl.h>
26 #include <linux/mm.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
29 #include <linux/capability.h>
30 #include <linux/skbuff.h>
31 #include <linux/init.h>
32 #include <linux/security.h>
33 #include <linux/mutex.h>
34 #include <linux/if_addr.h>
35 #include <linux/if_bridge.h>
36 #include <linux/if_vlan.h>
37 #include <linux/pci.h>
38 #include <linux/etherdevice.h>
39 #include <linux/bpf.h>
40 
41 #include <linux/uaccess.h>
42 
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <net/ip.h>
46 #include <net/protocol.h>
47 #include <net/arp.h>
48 #include <net/route.h>
49 #include <net/udp.h>
50 #include <net/tcp.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 #include <net/net_namespace.h>
56 
57 #include "dev.h"
58 
59 #define RTNL_MAX_TYPE		50
60 #define RTNL_SLAVE_MAX_TYPE	40
61 
62 struct rtnl_link {
63 	rtnl_doit_func		doit;
64 	rtnl_dumpit_func	dumpit;
65 	struct module		*owner;
66 	unsigned int		flags;
67 	struct rcu_head		rcu;
68 };
69 
70 static DEFINE_MUTEX(rtnl_mutex);
71 
rtnl_lock(void)72 void rtnl_lock(void)
73 {
74 	mutex_lock(&rtnl_mutex);
75 }
76 EXPORT_SYMBOL(rtnl_lock);
77 
rtnl_lock_killable(void)78 int rtnl_lock_killable(void)
79 {
80 	return mutex_lock_killable(&rtnl_mutex);
81 }
82 EXPORT_SYMBOL(rtnl_lock_killable);
83 
84 static struct sk_buff *defer_kfree_skb_list;
rtnl_kfree_skbs(struct sk_buff * head,struct sk_buff * tail)85 void rtnl_kfree_skbs(struct sk_buff *head, struct sk_buff *tail)
86 {
87 	if (head && tail) {
88 		tail->next = defer_kfree_skb_list;
89 		defer_kfree_skb_list = head;
90 	}
91 }
92 EXPORT_SYMBOL(rtnl_kfree_skbs);
93 
__rtnl_unlock(void)94 void __rtnl_unlock(void)
95 {
96 	struct sk_buff *head = defer_kfree_skb_list;
97 
98 	defer_kfree_skb_list = NULL;
99 
100 	/* Ensure that we didn't actually add any TODO item when __rtnl_unlock()
101 	 * is used. In some places, e.g. in cfg80211, we have code that will do
102 	 * something like
103 	 *   rtnl_lock()
104 	 *   wiphy_lock()
105 	 *   ...
106 	 *   rtnl_unlock()
107 	 *
108 	 * and because netdev_run_todo() acquires the RTNL for items on the list
109 	 * we could cause a situation such as this:
110 	 * Thread 1			Thread 2
111 	 *				  rtnl_lock()
112 	 *				  unregister_netdevice()
113 	 *				  __rtnl_unlock()
114 	 * rtnl_lock()
115 	 * wiphy_lock()
116 	 * rtnl_unlock()
117 	 *   netdev_run_todo()
118 	 *     __rtnl_unlock()
119 	 *
120 	 *     // list not empty now
121 	 *     // because of thread 2
122 	 *				  rtnl_lock()
123 	 *     while (!list_empty(...))
124 	 *       rtnl_lock()
125 	 *				  wiphy_lock()
126 	 * **** DEADLOCK ****
127 	 *
128 	 * However, usage of __rtnl_unlock() is rare, and so we can ensure that
129 	 * it's not used in cases where something is added to do the list.
130 	 */
131 	WARN_ON(!list_empty(&net_todo_list));
132 
133 	mutex_unlock(&rtnl_mutex);
134 
135 	while (head) {
136 		struct sk_buff *next = head->next;
137 
138 		kfree_skb(head);
139 		cond_resched();
140 		head = next;
141 	}
142 }
143 
rtnl_unlock(void)144 void rtnl_unlock(void)
145 {
146 	/* This fellow will unlock it for us. */
147 	netdev_run_todo();
148 }
149 EXPORT_SYMBOL(rtnl_unlock);
150 
rtnl_trylock(void)151 int rtnl_trylock(void)
152 {
153 	return mutex_trylock(&rtnl_mutex);
154 }
155 EXPORT_SYMBOL(rtnl_trylock);
156 
rtnl_is_locked(void)157 int rtnl_is_locked(void)
158 {
159 	return mutex_is_locked(&rtnl_mutex);
160 }
161 EXPORT_SYMBOL(rtnl_is_locked);
162 
refcount_dec_and_rtnl_lock(refcount_t * r)163 bool refcount_dec_and_rtnl_lock(refcount_t *r)
164 {
165 	return refcount_dec_and_mutex_lock(r, &rtnl_mutex);
166 }
167 EXPORT_SYMBOL(refcount_dec_and_rtnl_lock);
168 
169 #ifdef CONFIG_PROVE_LOCKING
lockdep_rtnl_is_held(void)170 bool lockdep_rtnl_is_held(void)
171 {
172 	return lockdep_is_held(&rtnl_mutex);
173 }
174 EXPORT_SYMBOL(lockdep_rtnl_is_held);
175 #endif /* #ifdef CONFIG_PROVE_LOCKING */
176 
177 static struct rtnl_link __rcu *__rcu *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
178 
rtm_msgindex(int msgtype)179 static inline int rtm_msgindex(int msgtype)
180 {
181 	int msgindex = msgtype - RTM_BASE;
182 
183 	/*
184 	 * msgindex < 0 implies someone tried to register a netlink
185 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
186 	 * the message type has not been added to linux/rtnetlink.h
187 	 */
188 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
189 
190 	return msgindex;
191 }
192 
rtnl_get_link(int protocol,int msgtype)193 static struct rtnl_link *rtnl_get_link(int protocol, int msgtype)
194 {
195 	struct rtnl_link __rcu **tab;
196 
197 	if (protocol >= ARRAY_SIZE(rtnl_msg_handlers))
198 		protocol = PF_UNSPEC;
199 
200 	tab = rcu_dereference_rtnl(rtnl_msg_handlers[protocol]);
201 	if (!tab)
202 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[PF_UNSPEC]);
203 
204 	return rcu_dereference_rtnl(tab[msgtype]);
205 }
206 
rtnl_register_internal(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)207 static int rtnl_register_internal(struct module *owner,
208 				  int protocol, int msgtype,
209 				  rtnl_doit_func doit, rtnl_dumpit_func dumpit,
210 				  unsigned int flags)
211 {
212 	struct rtnl_link *link, *old;
213 	struct rtnl_link __rcu **tab;
214 	int msgindex;
215 	int ret = -ENOBUFS;
216 
217 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
218 	msgindex = rtm_msgindex(msgtype);
219 
220 	rtnl_lock();
221 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
222 	if (tab == NULL) {
223 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(void *), GFP_KERNEL);
224 		if (!tab)
225 			goto unlock;
226 
227 		/* ensures we see the 0 stores */
228 		rcu_assign_pointer(rtnl_msg_handlers[protocol], tab);
229 	}
230 
231 	old = rtnl_dereference(tab[msgindex]);
232 	if (old) {
233 		link = kmemdup(old, sizeof(*old), GFP_KERNEL);
234 		if (!link)
235 			goto unlock;
236 	} else {
237 		link = kzalloc(sizeof(*link), GFP_KERNEL);
238 		if (!link)
239 			goto unlock;
240 	}
241 
242 	WARN_ON(link->owner && link->owner != owner);
243 	link->owner = owner;
244 
245 	WARN_ON(doit && link->doit && link->doit != doit);
246 	if (doit)
247 		link->doit = doit;
248 	WARN_ON(dumpit && link->dumpit && link->dumpit != dumpit);
249 	if (dumpit)
250 		link->dumpit = dumpit;
251 
252 	WARN_ON(rtnl_msgtype_kind(msgtype) != RTNL_KIND_DEL &&
253 		(flags & RTNL_FLAG_BULK_DEL_SUPPORTED));
254 	link->flags |= flags;
255 
256 	/* publish protocol:msgtype */
257 	rcu_assign_pointer(tab[msgindex], link);
258 	ret = 0;
259 	if (old)
260 		kfree_rcu(old, rcu);
261 unlock:
262 	rtnl_unlock();
263 	return ret;
264 }
265 
266 /**
267  * rtnl_register_module - Register a rtnetlink message type
268  *
269  * @owner: module registering the hook (THIS_MODULE)
270  * @protocol: Protocol family or PF_UNSPEC
271  * @msgtype: rtnetlink message type
272  * @doit: Function pointer called for each request message
273  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
274  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
275  *
276  * Like rtnl_register, but for use by removable modules.
277  */
rtnl_register_module(struct module * owner,int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)278 int rtnl_register_module(struct module *owner,
279 			 int protocol, int msgtype,
280 			 rtnl_doit_func doit, rtnl_dumpit_func dumpit,
281 			 unsigned int flags)
282 {
283 	return rtnl_register_internal(owner, protocol, msgtype,
284 				      doit, dumpit, flags);
285 }
286 EXPORT_SYMBOL_GPL(rtnl_register_module);
287 
288 /**
289  * rtnl_register - Register a rtnetlink message type
290  * @protocol: Protocol family or PF_UNSPEC
291  * @msgtype: rtnetlink message type
292  * @doit: Function pointer called for each request message
293  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
294  * @flags: rtnl_link_flags to modify behaviour of doit/dumpit functions
295  *
296  * Registers the specified function pointers (at least one of them has
297  * to be non-NULL) to be called whenever a request message for the
298  * specified protocol family and message type is received.
299  *
300  * The special protocol family PF_UNSPEC may be used to define fallback
301  * function pointers for the case when no entry for the specific protocol
302  * family exists.
303  */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,unsigned int flags)304 void rtnl_register(int protocol, int msgtype,
305 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
306 		   unsigned int flags)
307 {
308 	int err;
309 
310 	err = rtnl_register_internal(NULL, protocol, msgtype, doit, dumpit,
311 				     flags);
312 	if (err)
313 		pr_err("Unable to register rtnetlink message handler, "
314 		       "protocol = %d, message type = %d\n", protocol, msgtype);
315 }
316 
317 /**
318  * rtnl_unregister - Unregister a rtnetlink message type
319  * @protocol: Protocol family or PF_UNSPEC
320  * @msgtype: rtnetlink message type
321  *
322  * Returns 0 on success or a negative error code.
323  */
rtnl_unregister(int protocol,int msgtype)324 int rtnl_unregister(int protocol, int msgtype)
325 {
326 	struct rtnl_link __rcu **tab;
327 	struct rtnl_link *link;
328 	int msgindex;
329 
330 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
331 	msgindex = rtm_msgindex(msgtype);
332 
333 	rtnl_lock();
334 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
335 	if (!tab) {
336 		rtnl_unlock();
337 		return -ENOENT;
338 	}
339 
340 	link = rtnl_dereference(tab[msgindex]);
341 	RCU_INIT_POINTER(tab[msgindex], NULL);
342 	rtnl_unlock();
343 
344 	kfree_rcu(link, rcu);
345 
346 	return 0;
347 }
348 EXPORT_SYMBOL_GPL(rtnl_unregister);
349 
350 /**
351  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
352  * @protocol : Protocol family or PF_UNSPEC
353  *
354  * Identical to calling rtnl_unregster() for all registered message types
355  * of a certain protocol family.
356  */
rtnl_unregister_all(int protocol)357 void rtnl_unregister_all(int protocol)
358 {
359 	struct rtnl_link __rcu **tab;
360 	struct rtnl_link *link;
361 	int msgindex;
362 
363 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
364 
365 	rtnl_lock();
366 	tab = rtnl_dereference(rtnl_msg_handlers[protocol]);
367 	if (!tab) {
368 		rtnl_unlock();
369 		return;
370 	}
371 	RCU_INIT_POINTER(rtnl_msg_handlers[protocol], NULL);
372 	for (msgindex = 0; msgindex < RTM_NR_MSGTYPES; msgindex++) {
373 		link = rtnl_dereference(tab[msgindex]);
374 		if (!link)
375 			continue;
376 
377 		RCU_INIT_POINTER(tab[msgindex], NULL);
378 		kfree_rcu(link, rcu);
379 	}
380 	rtnl_unlock();
381 
382 	synchronize_net();
383 
384 	kfree(tab);
385 }
386 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
387 
388 static LIST_HEAD(link_ops);
389 
rtnl_link_ops_get(const char * kind)390 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
391 {
392 	const struct rtnl_link_ops *ops;
393 
394 	list_for_each_entry(ops, &link_ops, list) {
395 		if (!strcmp(ops->kind, kind))
396 			return ops;
397 	}
398 	return NULL;
399 }
400 
401 /**
402  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
403  * @ops: struct rtnl_link_ops * to register
404  *
405  * The caller must hold the rtnl_mutex. This function should be used
406  * by drivers that create devices during module initialization. It
407  * must be called before registering the devices.
408  *
409  * Returns 0 on success or a negative error code.
410  */
__rtnl_link_register(struct rtnl_link_ops * ops)411 int __rtnl_link_register(struct rtnl_link_ops *ops)
412 {
413 	if (rtnl_link_ops_get(ops->kind))
414 		return -EEXIST;
415 
416 	/* The check for alloc/setup is here because if ops
417 	 * does not have that filled up, it is not possible
418 	 * to use the ops for creating device. So do not
419 	 * fill up dellink as well. That disables rtnl_dellink.
420 	 */
421 	if ((ops->alloc || ops->setup) && !ops->dellink)
422 		ops->dellink = unregister_netdevice_queue;
423 
424 	list_add_tail(&ops->list, &link_ops);
425 	return 0;
426 }
427 EXPORT_SYMBOL_GPL(__rtnl_link_register);
428 
429 /**
430  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
431  * @ops: struct rtnl_link_ops * to register
432  *
433  * Returns 0 on success or a negative error code.
434  */
rtnl_link_register(struct rtnl_link_ops * ops)435 int rtnl_link_register(struct rtnl_link_ops *ops)
436 {
437 	int err;
438 
439 	/* Sanity-check max sizes to avoid stack buffer overflow. */
440 	if (WARN_ON(ops->maxtype > RTNL_MAX_TYPE ||
441 		    ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE))
442 		return -EINVAL;
443 
444 	rtnl_lock();
445 	err = __rtnl_link_register(ops);
446 	rtnl_unlock();
447 	return err;
448 }
449 EXPORT_SYMBOL_GPL(rtnl_link_register);
450 
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)451 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
452 {
453 	struct net_device *dev;
454 	LIST_HEAD(list_kill);
455 
456 	for_each_netdev(net, dev) {
457 		if (dev->rtnl_link_ops == ops)
458 			ops->dellink(dev, &list_kill);
459 	}
460 	unregister_netdevice_many(&list_kill);
461 }
462 
463 /**
464  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
465  * @ops: struct rtnl_link_ops * to unregister
466  *
467  * The caller must hold the rtnl_mutex and guarantee net_namespace_list
468  * integrity (hold pernet_ops_rwsem for writing to close the race
469  * with setup_net() and cleanup_net()).
470  */
__rtnl_link_unregister(struct rtnl_link_ops * ops)471 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
472 {
473 	struct net *net;
474 
475 	for_each_net(net) {
476 		__rtnl_kill_links(net, ops);
477 	}
478 	list_del(&ops->list);
479 }
480 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
481 
482 /* Return with the rtnl_lock held when there are no network
483  * devices unregistering in any network namespace.
484  */
rtnl_lock_unregistering_all(void)485 static void rtnl_lock_unregistering_all(void)
486 {
487 	struct net *net;
488 	bool unregistering;
489 	DEFINE_WAIT_FUNC(wait, woken_wake_function);
490 
491 	add_wait_queue(&netdev_unregistering_wq, &wait);
492 	for (;;) {
493 		unregistering = false;
494 		rtnl_lock();
495 		/* We held write locked pernet_ops_rwsem, and parallel
496 		 * setup_net() and cleanup_net() are not possible.
497 		 */
498 		for_each_net(net) {
499 			if (atomic_read(&net->dev_unreg_count) > 0) {
500 				unregistering = true;
501 				break;
502 			}
503 		}
504 		if (!unregistering)
505 			break;
506 		__rtnl_unlock();
507 
508 		wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
509 	}
510 	remove_wait_queue(&netdev_unregistering_wq, &wait);
511 }
512 
513 /**
514  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
515  * @ops: struct rtnl_link_ops * to unregister
516  */
rtnl_link_unregister(struct rtnl_link_ops * ops)517 void rtnl_link_unregister(struct rtnl_link_ops *ops)
518 {
519 	/* Close the race with setup_net() and cleanup_net() */
520 	down_write(&pernet_ops_rwsem);
521 	rtnl_lock_unregistering_all();
522 	__rtnl_link_unregister(ops);
523 	rtnl_unlock();
524 	up_write(&pernet_ops_rwsem);
525 }
526 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
527 
rtnl_link_get_slave_info_data_size(const struct net_device * dev)528 static size_t rtnl_link_get_slave_info_data_size(const struct net_device *dev)
529 {
530 	struct net_device *master_dev;
531 	const struct rtnl_link_ops *ops;
532 	size_t size = 0;
533 
534 	rcu_read_lock();
535 
536 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
537 	if (!master_dev)
538 		goto out;
539 
540 	ops = master_dev->rtnl_link_ops;
541 	if (!ops || !ops->get_slave_size)
542 		goto out;
543 	/* IFLA_INFO_SLAVE_DATA + nested data */
544 	size = nla_total_size(sizeof(struct nlattr)) +
545 	       ops->get_slave_size(master_dev, dev);
546 
547 out:
548 	rcu_read_unlock();
549 	return size;
550 }
551 
rtnl_link_get_size(const struct net_device * dev)552 static size_t rtnl_link_get_size(const struct net_device *dev)
553 {
554 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
555 	size_t size;
556 
557 	if (!ops)
558 		return 0;
559 
560 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
561 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
562 
563 	if (ops->get_size)
564 		/* IFLA_INFO_DATA + nested data */
565 		size += nla_total_size(sizeof(struct nlattr)) +
566 			ops->get_size(dev);
567 
568 	if (ops->get_xstats_size)
569 		/* IFLA_INFO_XSTATS */
570 		size += nla_total_size(ops->get_xstats_size(dev));
571 
572 	size += rtnl_link_get_slave_info_data_size(dev);
573 
574 	return size;
575 }
576 
577 static LIST_HEAD(rtnl_af_ops);
578 
rtnl_af_lookup(const int family)579 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
580 {
581 	const struct rtnl_af_ops *ops;
582 
583 	ASSERT_RTNL();
584 
585 	list_for_each_entry(ops, &rtnl_af_ops, list) {
586 		if (ops->family == family)
587 			return ops;
588 	}
589 
590 	return NULL;
591 }
592 
593 /**
594  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
595  * @ops: struct rtnl_af_ops * to register
596  *
597  * Returns 0 on success or a negative error code.
598  */
rtnl_af_register(struct rtnl_af_ops * ops)599 void rtnl_af_register(struct rtnl_af_ops *ops)
600 {
601 	rtnl_lock();
602 	list_add_tail_rcu(&ops->list, &rtnl_af_ops);
603 	rtnl_unlock();
604 }
605 EXPORT_SYMBOL_GPL(rtnl_af_register);
606 
607 /**
608  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
609  * @ops: struct rtnl_af_ops * to unregister
610  */
rtnl_af_unregister(struct rtnl_af_ops * ops)611 void rtnl_af_unregister(struct rtnl_af_ops *ops)
612 {
613 	rtnl_lock();
614 	list_del_rcu(&ops->list);
615 	rtnl_unlock();
616 
617 	synchronize_rcu();
618 }
619 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
620 
rtnl_link_get_af_size(const struct net_device * dev,u32 ext_filter_mask)621 static size_t rtnl_link_get_af_size(const struct net_device *dev,
622 				    u32 ext_filter_mask)
623 {
624 	struct rtnl_af_ops *af_ops;
625 	size_t size;
626 
627 	/* IFLA_AF_SPEC */
628 	size = nla_total_size(sizeof(struct nlattr));
629 
630 	rcu_read_lock();
631 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
632 		if (af_ops->get_link_af_size) {
633 			/* AF_* + nested data */
634 			size += nla_total_size(sizeof(struct nlattr)) +
635 				af_ops->get_link_af_size(dev, ext_filter_mask);
636 		}
637 	}
638 	rcu_read_unlock();
639 
640 	return size;
641 }
642 
rtnl_have_link_slave_info(const struct net_device * dev)643 static bool rtnl_have_link_slave_info(const struct net_device *dev)
644 {
645 	struct net_device *master_dev;
646 	bool ret = false;
647 
648 	rcu_read_lock();
649 
650 	master_dev = netdev_master_upper_dev_get_rcu((struct net_device *)dev);
651 	if (master_dev && master_dev->rtnl_link_ops)
652 		ret = true;
653 	rcu_read_unlock();
654 	return ret;
655 }
656 
rtnl_link_slave_info_fill(struct sk_buff * skb,const struct net_device * dev)657 static int rtnl_link_slave_info_fill(struct sk_buff *skb,
658 				     const struct net_device *dev)
659 {
660 	struct net_device *master_dev;
661 	const struct rtnl_link_ops *ops;
662 	struct nlattr *slave_data;
663 	int err;
664 
665 	master_dev = netdev_master_upper_dev_get((struct net_device *) dev);
666 	if (!master_dev)
667 		return 0;
668 	ops = master_dev->rtnl_link_ops;
669 	if (!ops)
670 		return 0;
671 	if (nla_put_string(skb, IFLA_INFO_SLAVE_KIND, ops->kind) < 0)
672 		return -EMSGSIZE;
673 	if (ops->fill_slave_info) {
674 		slave_data = nla_nest_start_noflag(skb, IFLA_INFO_SLAVE_DATA);
675 		if (!slave_data)
676 			return -EMSGSIZE;
677 		err = ops->fill_slave_info(skb, master_dev, dev);
678 		if (err < 0)
679 			goto err_cancel_slave_data;
680 		nla_nest_end(skb, slave_data);
681 	}
682 	return 0;
683 
684 err_cancel_slave_data:
685 	nla_nest_cancel(skb, slave_data);
686 	return err;
687 }
688 
rtnl_link_info_fill(struct sk_buff * skb,const struct net_device * dev)689 static int rtnl_link_info_fill(struct sk_buff *skb,
690 			       const struct net_device *dev)
691 {
692 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
693 	struct nlattr *data;
694 	int err;
695 
696 	if (!ops)
697 		return 0;
698 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
699 		return -EMSGSIZE;
700 	if (ops->fill_xstats) {
701 		err = ops->fill_xstats(skb, dev);
702 		if (err < 0)
703 			return err;
704 	}
705 	if (ops->fill_info) {
706 		data = nla_nest_start_noflag(skb, IFLA_INFO_DATA);
707 		if (data == NULL)
708 			return -EMSGSIZE;
709 		err = ops->fill_info(skb, dev);
710 		if (err < 0)
711 			goto err_cancel_data;
712 		nla_nest_end(skb, data);
713 	}
714 	return 0;
715 
716 err_cancel_data:
717 	nla_nest_cancel(skb, data);
718 	return err;
719 }
720 
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)721 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
722 {
723 	struct nlattr *linkinfo;
724 	int err = -EMSGSIZE;
725 
726 	linkinfo = nla_nest_start_noflag(skb, IFLA_LINKINFO);
727 	if (linkinfo == NULL)
728 		goto out;
729 
730 	err = rtnl_link_info_fill(skb, dev);
731 	if (err < 0)
732 		goto err_cancel_link;
733 
734 	err = rtnl_link_slave_info_fill(skb, dev);
735 	if (err < 0)
736 		goto err_cancel_link;
737 
738 	nla_nest_end(skb, linkinfo);
739 	return 0;
740 
741 err_cancel_link:
742 	nla_nest_cancel(skb, linkinfo);
743 out:
744 	return err;
745 }
746 
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned int group,int echo)747 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned int group, int echo)
748 {
749 	struct sock *rtnl = net->rtnl;
750 
751 	return nlmsg_notify(rtnl, skb, pid, group, echo, GFP_KERNEL);
752 }
753 
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)754 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
755 {
756 	struct sock *rtnl = net->rtnl;
757 
758 	return nlmsg_unicast(rtnl, skb, pid);
759 }
760 EXPORT_SYMBOL(rtnl_unicast);
761 
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,struct nlmsghdr * nlh,gfp_t flags)762 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
763 		 struct nlmsghdr *nlh, gfp_t flags)
764 {
765 	struct sock *rtnl = net->rtnl;
766 
767 	nlmsg_notify(rtnl, skb, pid, group, nlmsg_report(nlh), flags);
768 }
769 EXPORT_SYMBOL(rtnl_notify);
770 
rtnl_set_sk_err(struct net * net,u32 group,int error)771 void rtnl_set_sk_err(struct net *net, u32 group, int error)
772 {
773 	struct sock *rtnl = net->rtnl;
774 
775 	netlink_set_err(rtnl, 0, group, error);
776 }
777 EXPORT_SYMBOL(rtnl_set_sk_err);
778 
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)779 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
780 {
781 	struct nlattr *mx;
782 	int i, valid = 0;
783 
784 	/* nothing is dumped for dst_default_metrics, so just skip the loop */
785 	if (metrics == dst_default_metrics.metrics)
786 		return 0;
787 
788 	mx = nla_nest_start_noflag(skb, RTA_METRICS);
789 	if (mx == NULL)
790 		return -ENOBUFS;
791 
792 	for (i = 0; i < RTAX_MAX; i++) {
793 		if (metrics[i]) {
794 			if (i == RTAX_CC_ALGO - 1) {
795 				char tmp[TCP_CA_NAME_MAX], *name;
796 
797 				name = tcp_ca_get_name_by_key(metrics[i], tmp);
798 				if (!name)
799 					continue;
800 				if (nla_put_string(skb, i + 1, name))
801 					goto nla_put_failure;
802 			} else if (i == RTAX_FEATURES - 1) {
803 				u32 user_features = metrics[i] & RTAX_FEATURE_MASK;
804 
805 				if (!user_features)
806 					continue;
807 				BUILD_BUG_ON(RTAX_FEATURE_MASK & DST_FEATURE_MASK);
808 				if (nla_put_u32(skb, i + 1, user_features))
809 					goto nla_put_failure;
810 			} else {
811 				if (nla_put_u32(skb, i + 1, metrics[i]))
812 					goto nla_put_failure;
813 			}
814 			valid++;
815 		}
816 	}
817 
818 	if (!valid) {
819 		nla_nest_cancel(skb, mx);
820 		return 0;
821 	}
822 
823 	return nla_nest_end(skb, mx);
824 
825 nla_put_failure:
826 	nla_nest_cancel(skb, mx);
827 	return -EMSGSIZE;
828 }
829 EXPORT_SYMBOL(rtnetlink_put_metrics);
830 
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,long expires,u32 error)831 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
832 		       long expires, u32 error)
833 {
834 	struct rta_cacheinfo ci = {
835 		.rta_error = error,
836 		.rta_id =  id,
837 	};
838 
839 	if (dst) {
840 		ci.rta_lastuse = jiffies_delta_to_clock_t(jiffies - dst->lastuse);
841 		ci.rta_used = dst->__use;
842 		ci.rta_clntref = atomic_read(&dst->__refcnt);
843 	}
844 	if (expires) {
845 		unsigned long clock;
846 
847 		clock = jiffies_to_clock_t(abs(expires));
848 		clock = min_t(unsigned long, clock, INT_MAX);
849 		ci.rta_expires = (expires > 0) ? clock : -clock;
850 	}
851 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
852 }
853 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
854 
set_operstate(struct net_device * dev,unsigned char transition)855 static void set_operstate(struct net_device *dev, unsigned char transition)
856 {
857 	unsigned char operstate = dev->operstate;
858 
859 	switch (transition) {
860 	case IF_OPER_UP:
861 		if ((operstate == IF_OPER_DORMANT ||
862 		     operstate == IF_OPER_TESTING ||
863 		     operstate == IF_OPER_UNKNOWN) &&
864 		    !netif_dormant(dev) && !netif_testing(dev))
865 			operstate = IF_OPER_UP;
866 		break;
867 
868 	case IF_OPER_TESTING:
869 		if (netif_oper_up(dev))
870 			operstate = IF_OPER_TESTING;
871 		break;
872 
873 	case IF_OPER_DORMANT:
874 		if (netif_oper_up(dev))
875 			operstate = IF_OPER_DORMANT;
876 		break;
877 	}
878 
879 	if (dev->operstate != operstate) {
880 		write_lock(&dev_base_lock);
881 		dev->operstate = operstate;
882 		write_unlock(&dev_base_lock);
883 		netdev_state_change(dev);
884 	}
885 }
886 
rtnl_dev_get_flags(const struct net_device * dev)887 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
888 {
889 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
890 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
891 }
892 
rtnl_dev_combine_flags(const struct net_device * dev,const struct ifinfomsg * ifm)893 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
894 					   const struct ifinfomsg *ifm)
895 {
896 	unsigned int flags = ifm->ifi_flags;
897 
898 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
899 	if (ifm->ifi_change)
900 		flags = (flags & ifm->ifi_change) |
901 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
902 
903 	return flags;
904 }
905 
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct rtnl_link_stats64 * b)906 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
907 				 const struct rtnl_link_stats64 *b)
908 {
909 	a->rx_packets = b->rx_packets;
910 	a->tx_packets = b->tx_packets;
911 	a->rx_bytes = b->rx_bytes;
912 	a->tx_bytes = b->tx_bytes;
913 	a->rx_errors = b->rx_errors;
914 	a->tx_errors = b->tx_errors;
915 	a->rx_dropped = b->rx_dropped;
916 	a->tx_dropped = b->tx_dropped;
917 
918 	a->multicast = b->multicast;
919 	a->collisions = b->collisions;
920 
921 	a->rx_length_errors = b->rx_length_errors;
922 	a->rx_over_errors = b->rx_over_errors;
923 	a->rx_crc_errors = b->rx_crc_errors;
924 	a->rx_frame_errors = b->rx_frame_errors;
925 	a->rx_fifo_errors = b->rx_fifo_errors;
926 	a->rx_missed_errors = b->rx_missed_errors;
927 
928 	a->tx_aborted_errors = b->tx_aborted_errors;
929 	a->tx_carrier_errors = b->tx_carrier_errors;
930 	a->tx_fifo_errors = b->tx_fifo_errors;
931 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
932 	a->tx_window_errors = b->tx_window_errors;
933 
934 	a->rx_compressed = b->rx_compressed;
935 	a->tx_compressed = b->tx_compressed;
936 
937 	a->rx_nohandler = b->rx_nohandler;
938 }
939 
940 /* All VF info */
rtnl_vfinfo_size(const struct net_device * dev,u32 ext_filter_mask)941 static inline int rtnl_vfinfo_size(const struct net_device *dev,
942 				   u32 ext_filter_mask)
943 {
944 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF)) {
945 		int num_vfs = dev_num_vf(dev->dev.parent);
946 		size_t size = nla_total_size(0);
947 		size += num_vfs *
948 			(nla_total_size(0) +
949 			 nla_total_size(sizeof(struct ifla_vf_mac)) +
950 			 nla_total_size(sizeof(struct ifla_vf_broadcast)) +
951 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
952 			 nla_total_size(0) + /* nest IFLA_VF_VLAN_LIST */
953 			 nla_total_size(MAX_VLAN_LIST_LEN *
954 					sizeof(struct ifla_vf_vlan_info)) +
955 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)) +
956 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
957 			 nla_total_size(sizeof(struct ifla_vf_rate)) +
958 			 nla_total_size(sizeof(struct ifla_vf_link_state)) +
959 			 nla_total_size(sizeof(struct ifla_vf_rss_query_en)) +
960 			 nla_total_size(sizeof(struct ifla_vf_trust)));
961 		if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
962 			size += num_vfs *
963 				(nla_total_size(0) + /* nest IFLA_VF_STATS */
964 				 /* IFLA_VF_STATS_RX_PACKETS */
965 				 nla_total_size_64bit(sizeof(__u64)) +
966 				 /* IFLA_VF_STATS_TX_PACKETS */
967 				 nla_total_size_64bit(sizeof(__u64)) +
968 				 /* IFLA_VF_STATS_RX_BYTES */
969 				 nla_total_size_64bit(sizeof(__u64)) +
970 				 /* IFLA_VF_STATS_TX_BYTES */
971 				 nla_total_size_64bit(sizeof(__u64)) +
972 				 /* IFLA_VF_STATS_BROADCAST */
973 				 nla_total_size_64bit(sizeof(__u64)) +
974 				 /* IFLA_VF_STATS_MULTICAST */
975 				 nla_total_size_64bit(sizeof(__u64)) +
976 				 /* IFLA_VF_STATS_RX_DROPPED */
977 				 nla_total_size_64bit(sizeof(__u64)) +
978 				 /* IFLA_VF_STATS_TX_DROPPED */
979 				 nla_total_size_64bit(sizeof(__u64)));
980 		}
981 		return size;
982 	} else
983 		return 0;
984 }
985 
rtnl_port_size(const struct net_device * dev,u32 ext_filter_mask)986 static size_t rtnl_port_size(const struct net_device *dev,
987 			     u32 ext_filter_mask)
988 {
989 	size_t port_size = nla_total_size(4)		/* PORT_VF */
990 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
991 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
992 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
993 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
994 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
995 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
996 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
997 		+ port_size;
998 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
999 		+ port_size;
1000 
1001 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1002 	    !(ext_filter_mask & RTEXT_FILTER_VF))
1003 		return 0;
1004 	if (dev_num_vf(dev->dev.parent))
1005 		return port_self_size + vf_ports_size +
1006 			vf_port_size * dev_num_vf(dev->dev.parent);
1007 	else
1008 		return port_self_size;
1009 }
1010 
rtnl_xdp_size(void)1011 static size_t rtnl_xdp_size(void)
1012 {
1013 	size_t xdp_size = nla_total_size(0) +	/* nest IFLA_XDP */
1014 			  nla_total_size(1) +	/* XDP_ATTACHED */
1015 			  nla_total_size(4) +	/* XDP_PROG_ID (or 1st mode) */
1016 			  nla_total_size(4);	/* XDP_<mode>_PROG_ID */
1017 
1018 	return xdp_size;
1019 }
1020 
rtnl_prop_list_size(const struct net_device * dev)1021 static size_t rtnl_prop_list_size(const struct net_device *dev)
1022 {
1023 	struct netdev_name_node *name_node;
1024 	size_t size;
1025 
1026 	if (list_empty(&dev->name_node->list))
1027 		return 0;
1028 	size = nla_total_size(0);
1029 	list_for_each_entry(name_node, &dev->name_node->list, list)
1030 		size += nla_total_size(ALTIFNAMSIZ);
1031 	return size;
1032 }
1033 
rtnl_proto_down_size(const struct net_device * dev)1034 static size_t rtnl_proto_down_size(const struct net_device *dev)
1035 {
1036 	size_t size = nla_total_size(1);
1037 
1038 	if (dev->proto_down_reason)
1039 		size += nla_total_size(0) + nla_total_size(4);
1040 
1041 	return size;
1042 }
1043 
if_nlmsg_size(const struct net_device * dev,u32 ext_filter_mask)1044 static noinline size_t if_nlmsg_size(const struct net_device *dev,
1045 				     u32 ext_filter_mask)
1046 {
1047 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
1048 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
1049 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
1050 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
1051 	       + nla_total_size_64bit(sizeof(struct rtnl_link_ifmap))
1052 	       + nla_total_size(sizeof(struct rtnl_link_stats))
1053 	       + nla_total_size_64bit(sizeof(struct rtnl_link_stats64))
1054 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
1055 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
1056 	       + nla_total_size(4) /* IFLA_TXQLEN */
1057 	       + nla_total_size(4) /* IFLA_WEIGHT */
1058 	       + nla_total_size(4) /* IFLA_MTU */
1059 	       + nla_total_size(4) /* IFLA_LINK */
1060 	       + nla_total_size(4) /* IFLA_MASTER */
1061 	       + nla_total_size(1) /* IFLA_CARRIER */
1062 	       + nla_total_size(4) /* IFLA_PROMISCUITY */
1063 	       + nla_total_size(4) /* IFLA_ALLMULTI */
1064 	       + nla_total_size(4) /* IFLA_NUM_TX_QUEUES */
1065 	       + nla_total_size(4) /* IFLA_NUM_RX_QUEUES */
1066 	       + nla_total_size(4) /* IFLA_GSO_MAX_SEGS */
1067 	       + nla_total_size(4) /* IFLA_GSO_MAX_SIZE */
1068 	       + nla_total_size(4) /* IFLA_GRO_MAX_SIZE */
1069 	       + nla_total_size(4) /* IFLA_TSO_MAX_SIZE */
1070 	       + nla_total_size(4) /* IFLA_TSO_MAX_SEGS */
1071 	       + nla_total_size(1) /* IFLA_OPERSTATE */
1072 	       + nla_total_size(1) /* IFLA_LINKMODE */
1073 	       + nla_total_size(4) /* IFLA_CARRIER_CHANGES */
1074 	       + nla_total_size(4) /* IFLA_LINK_NETNSID */
1075 	       + nla_total_size(4) /* IFLA_GROUP */
1076 	       + nla_total_size(ext_filter_mask
1077 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
1078 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
1079 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
1080 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
1081 	       + rtnl_link_get_af_size(dev, ext_filter_mask) /* IFLA_AF_SPEC */
1082 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_PORT_ID */
1083 	       + nla_total_size(MAX_PHYS_ITEM_ID_LEN) /* IFLA_PHYS_SWITCH_ID */
1084 	       + nla_total_size(IFNAMSIZ) /* IFLA_PHYS_PORT_NAME */
1085 	       + rtnl_xdp_size() /* IFLA_XDP */
1086 	       + nla_total_size(4)  /* IFLA_EVENT */
1087 	       + nla_total_size(4)  /* IFLA_NEW_NETNSID */
1088 	       + nla_total_size(4)  /* IFLA_NEW_IFINDEX */
1089 	       + rtnl_proto_down_size(dev)  /* proto down */
1090 	       + nla_total_size(4)  /* IFLA_TARGET_NETNSID */
1091 	       + nla_total_size(4)  /* IFLA_CARRIER_UP_COUNT */
1092 	       + nla_total_size(4)  /* IFLA_CARRIER_DOWN_COUNT */
1093 	       + nla_total_size(4)  /* IFLA_MIN_MTU */
1094 	       + nla_total_size(4)  /* IFLA_MAX_MTU */
1095 	       + rtnl_prop_list_size(dev)
1096 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_PERM_ADDRESS */
1097 	       + 0;
1098 }
1099 
rtnl_vf_ports_fill(struct sk_buff * skb,struct net_device * dev)1100 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
1101 {
1102 	struct nlattr *vf_ports;
1103 	struct nlattr *vf_port;
1104 	int vf;
1105 	int err;
1106 
1107 	vf_ports = nla_nest_start_noflag(skb, IFLA_VF_PORTS);
1108 	if (!vf_ports)
1109 		return -EMSGSIZE;
1110 
1111 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
1112 		vf_port = nla_nest_start_noflag(skb, IFLA_VF_PORT);
1113 		if (!vf_port)
1114 			goto nla_put_failure;
1115 		if (nla_put_u32(skb, IFLA_PORT_VF, vf))
1116 			goto nla_put_failure;
1117 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
1118 		if (err == -EMSGSIZE)
1119 			goto nla_put_failure;
1120 		if (err) {
1121 			nla_nest_cancel(skb, vf_port);
1122 			continue;
1123 		}
1124 		nla_nest_end(skb, vf_port);
1125 	}
1126 
1127 	nla_nest_end(skb, vf_ports);
1128 
1129 	return 0;
1130 
1131 nla_put_failure:
1132 	nla_nest_cancel(skb, vf_ports);
1133 	return -EMSGSIZE;
1134 }
1135 
rtnl_port_self_fill(struct sk_buff * skb,struct net_device * dev)1136 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
1137 {
1138 	struct nlattr *port_self;
1139 	int err;
1140 
1141 	port_self = nla_nest_start_noflag(skb, IFLA_PORT_SELF);
1142 	if (!port_self)
1143 		return -EMSGSIZE;
1144 
1145 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
1146 	if (err) {
1147 		nla_nest_cancel(skb, port_self);
1148 		return (err == -EMSGSIZE) ? err : 0;
1149 	}
1150 
1151 	nla_nest_end(skb, port_self);
1152 
1153 	return 0;
1154 }
1155 
rtnl_port_fill(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1156 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
1157 			  u32 ext_filter_mask)
1158 {
1159 	int err;
1160 
1161 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
1162 	    !(ext_filter_mask & RTEXT_FILTER_VF))
1163 		return 0;
1164 
1165 	err = rtnl_port_self_fill(skb, dev);
1166 	if (err)
1167 		return err;
1168 
1169 	if (dev_num_vf(dev->dev.parent)) {
1170 		err = rtnl_vf_ports_fill(skb, dev);
1171 		if (err)
1172 			return err;
1173 	}
1174 
1175 	return 0;
1176 }
1177 
rtnl_phys_port_id_fill(struct sk_buff * skb,struct net_device * dev)1178 static int rtnl_phys_port_id_fill(struct sk_buff *skb, struct net_device *dev)
1179 {
1180 	int err;
1181 	struct netdev_phys_item_id ppid;
1182 
1183 	err = dev_get_phys_port_id(dev, &ppid);
1184 	if (err) {
1185 		if (err == -EOPNOTSUPP)
1186 			return 0;
1187 		return err;
1188 	}
1189 
1190 	if (nla_put(skb, IFLA_PHYS_PORT_ID, ppid.id_len, ppid.id))
1191 		return -EMSGSIZE;
1192 
1193 	return 0;
1194 }
1195 
rtnl_phys_port_name_fill(struct sk_buff * skb,struct net_device * dev)1196 static int rtnl_phys_port_name_fill(struct sk_buff *skb, struct net_device *dev)
1197 {
1198 	char name[IFNAMSIZ];
1199 	int err;
1200 
1201 	err = dev_get_phys_port_name(dev, name, sizeof(name));
1202 	if (err) {
1203 		if (err == -EOPNOTSUPP)
1204 			return 0;
1205 		return err;
1206 	}
1207 
1208 	if (nla_put_string(skb, IFLA_PHYS_PORT_NAME, name))
1209 		return -EMSGSIZE;
1210 
1211 	return 0;
1212 }
1213 
rtnl_phys_switch_id_fill(struct sk_buff * skb,struct net_device * dev)1214 static int rtnl_phys_switch_id_fill(struct sk_buff *skb, struct net_device *dev)
1215 {
1216 	struct netdev_phys_item_id ppid = { };
1217 	int err;
1218 
1219 	err = dev_get_port_parent_id(dev, &ppid, false);
1220 	if (err) {
1221 		if (err == -EOPNOTSUPP)
1222 			return 0;
1223 		return err;
1224 	}
1225 
1226 	if (nla_put(skb, IFLA_PHYS_SWITCH_ID, ppid.id_len, ppid.id))
1227 		return -EMSGSIZE;
1228 
1229 	return 0;
1230 }
1231 
rtnl_fill_stats(struct sk_buff * skb,struct net_device * dev)1232 static noinline_for_stack int rtnl_fill_stats(struct sk_buff *skb,
1233 					      struct net_device *dev)
1234 {
1235 	struct rtnl_link_stats64 *sp;
1236 	struct nlattr *attr;
1237 
1238 	attr = nla_reserve_64bit(skb, IFLA_STATS64,
1239 				 sizeof(struct rtnl_link_stats64), IFLA_PAD);
1240 	if (!attr)
1241 		return -EMSGSIZE;
1242 
1243 	sp = nla_data(attr);
1244 	dev_get_stats(dev, sp);
1245 
1246 	attr = nla_reserve(skb, IFLA_STATS,
1247 			   sizeof(struct rtnl_link_stats));
1248 	if (!attr)
1249 		return -EMSGSIZE;
1250 
1251 	copy_rtnl_link_stats(nla_data(attr), sp);
1252 
1253 	return 0;
1254 }
1255 
rtnl_fill_vfinfo(struct sk_buff * skb,struct net_device * dev,int vfs_num,struct nlattr * vfinfo,u32 ext_filter_mask)1256 static noinline_for_stack int rtnl_fill_vfinfo(struct sk_buff *skb,
1257 					       struct net_device *dev,
1258 					       int vfs_num,
1259 					       struct nlattr *vfinfo,
1260 					       u32 ext_filter_mask)
1261 {
1262 	struct ifla_vf_rss_query_en vf_rss_query_en;
1263 	struct nlattr *vf, *vfstats, *vfvlanlist;
1264 	struct ifla_vf_link_state vf_linkstate;
1265 	struct ifla_vf_vlan_info vf_vlan_info;
1266 	struct ifla_vf_spoofchk vf_spoofchk;
1267 	struct ifla_vf_tx_rate vf_tx_rate;
1268 	struct ifla_vf_stats vf_stats;
1269 	struct ifla_vf_trust vf_trust;
1270 	struct ifla_vf_vlan vf_vlan;
1271 	struct ifla_vf_rate vf_rate;
1272 	struct ifla_vf_mac vf_mac;
1273 	struct ifla_vf_broadcast vf_broadcast;
1274 	struct ifla_vf_info ivi;
1275 	struct ifla_vf_guid node_guid;
1276 	struct ifla_vf_guid port_guid;
1277 
1278 	memset(&ivi, 0, sizeof(ivi));
1279 
1280 	/* Not all SR-IOV capable drivers support the
1281 	 * spoofcheck and "RSS query enable" query.  Preset to
1282 	 * -1 so the user space tool can detect that the driver
1283 	 * didn't report anything.
1284 	 */
1285 	ivi.spoofchk = -1;
1286 	ivi.rss_query_en = -1;
1287 	ivi.trusted = -1;
1288 	/* The default value for VF link state is "auto"
1289 	 * IFLA_VF_LINK_STATE_AUTO which equals zero
1290 	 */
1291 	ivi.linkstate = 0;
1292 	/* VLAN Protocol by default is 802.1Q */
1293 	ivi.vlan_proto = htons(ETH_P_8021Q);
1294 	if (dev->netdev_ops->ndo_get_vf_config(dev, vfs_num, &ivi))
1295 		return 0;
1296 
1297 	memset(&vf_vlan_info, 0, sizeof(vf_vlan_info));
1298 	memset(&node_guid, 0, sizeof(node_guid));
1299 	memset(&port_guid, 0, sizeof(port_guid));
1300 
1301 	vf_mac.vf =
1302 		vf_vlan.vf =
1303 		vf_vlan_info.vf =
1304 		vf_rate.vf =
1305 		vf_tx_rate.vf =
1306 		vf_spoofchk.vf =
1307 		vf_linkstate.vf =
1308 		vf_rss_query_en.vf =
1309 		vf_trust.vf =
1310 		node_guid.vf =
1311 		port_guid.vf = ivi.vf;
1312 
1313 	memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
1314 	memcpy(vf_broadcast.broadcast, dev->broadcast, dev->addr_len);
1315 	vf_vlan.vlan = ivi.vlan;
1316 	vf_vlan.qos = ivi.qos;
1317 	vf_vlan_info.vlan = ivi.vlan;
1318 	vf_vlan_info.qos = ivi.qos;
1319 	vf_vlan_info.vlan_proto = ivi.vlan_proto;
1320 	vf_tx_rate.rate = ivi.max_tx_rate;
1321 	vf_rate.min_tx_rate = ivi.min_tx_rate;
1322 	vf_rate.max_tx_rate = ivi.max_tx_rate;
1323 	vf_spoofchk.setting = ivi.spoofchk;
1324 	vf_linkstate.link_state = ivi.linkstate;
1325 	vf_rss_query_en.setting = ivi.rss_query_en;
1326 	vf_trust.setting = ivi.trusted;
1327 	vf = nla_nest_start_noflag(skb, IFLA_VF_INFO);
1328 	if (!vf)
1329 		goto nla_put_vfinfo_failure;
1330 	if (nla_put(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac) ||
1331 	    nla_put(skb, IFLA_VF_BROADCAST, sizeof(vf_broadcast), &vf_broadcast) ||
1332 	    nla_put(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan) ||
1333 	    nla_put(skb, IFLA_VF_RATE, sizeof(vf_rate),
1334 		    &vf_rate) ||
1335 	    nla_put(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1336 		    &vf_tx_rate) ||
1337 	    nla_put(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1338 		    &vf_spoofchk) ||
1339 	    nla_put(skb, IFLA_VF_LINK_STATE, sizeof(vf_linkstate),
1340 		    &vf_linkstate) ||
1341 	    nla_put(skb, IFLA_VF_RSS_QUERY_EN,
1342 		    sizeof(vf_rss_query_en),
1343 		    &vf_rss_query_en) ||
1344 	    nla_put(skb, IFLA_VF_TRUST,
1345 		    sizeof(vf_trust), &vf_trust))
1346 		goto nla_put_vf_failure;
1347 
1348 	if (dev->netdev_ops->ndo_get_vf_guid &&
1349 	    !dev->netdev_ops->ndo_get_vf_guid(dev, vfs_num, &node_guid,
1350 					      &port_guid)) {
1351 		if (nla_put(skb, IFLA_VF_IB_NODE_GUID, sizeof(node_guid),
1352 			    &node_guid) ||
1353 		    nla_put(skb, IFLA_VF_IB_PORT_GUID, sizeof(port_guid),
1354 			    &port_guid))
1355 			goto nla_put_vf_failure;
1356 	}
1357 	vfvlanlist = nla_nest_start_noflag(skb, IFLA_VF_VLAN_LIST);
1358 	if (!vfvlanlist)
1359 		goto nla_put_vf_failure;
1360 	if (nla_put(skb, IFLA_VF_VLAN_INFO, sizeof(vf_vlan_info),
1361 		    &vf_vlan_info)) {
1362 		nla_nest_cancel(skb, vfvlanlist);
1363 		goto nla_put_vf_failure;
1364 	}
1365 	nla_nest_end(skb, vfvlanlist);
1366 	if (~ext_filter_mask & RTEXT_FILTER_SKIP_STATS) {
1367 		memset(&vf_stats, 0, sizeof(vf_stats));
1368 		if (dev->netdev_ops->ndo_get_vf_stats)
1369 			dev->netdev_ops->ndo_get_vf_stats(dev, vfs_num,
1370 							  &vf_stats);
1371 		vfstats = nla_nest_start_noflag(skb, IFLA_VF_STATS);
1372 		if (!vfstats)
1373 			goto nla_put_vf_failure;
1374 		if (nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_PACKETS,
1375 				      vf_stats.rx_packets, IFLA_VF_STATS_PAD) ||
1376 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_PACKETS,
1377 				      vf_stats.tx_packets, IFLA_VF_STATS_PAD) ||
1378 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_BYTES,
1379 				      vf_stats.rx_bytes, IFLA_VF_STATS_PAD) ||
1380 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_BYTES,
1381 				      vf_stats.tx_bytes, IFLA_VF_STATS_PAD) ||
1382 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_BROADCAST,
1383 				      vf_stats.broadcast, IFLA_VF_STATS_PAD) ||
1384 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_MULTICAST,
1385 				      vf_stats.multicast, IFLA_VF_STATS_PAD) ||
1386 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_RX_DROPPED,
1387 				      vf_stats.rx_dropped, IFLA_VF_STATS_PAD) ||
1388 		    nla_put_u64_64bit(skb, IFLA_VF_STATS_TX_DROPPED,
1389 				      vf_stats.tx_dropped, IFLA_VF_STATS_PAD)) {
1390 			nla_nest_cancel(skb, vfstats);
1391 			goto nla_put_vf_failure;
1392 		}
1393 		nla_nest_end(skb, vfstats);
1394 	}
1395 	nla_nest_end(skb, vf);
1396 	return 0;
1397 
1398 nla_put_vf_failure:
1399 	nla_nest_cancel(skb, vf);
1400 nla_put_vfinfo_failure:
1401 	nla_nest_cancel(skb, vfinfo);
1402 	return -EMSGSIZE;
1403 }
1404 
rtnl_fill_vf(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)1405 static noinline_for_stack int rtnl_fill_vf(struct sk_buff *skb,
1406 					   struct net_device *dev,
1407 					   u32 ext_filter_mask)
1408 {
1409 	struct nlattr *vfinfo;
1410 	int i, num_vfs;
1411 
1412 	if (!dev->dev.parent || ((ext_filter_mask & RTEXT_FILTER_VF) == 0))
1413 		return 0;
1414 
1415 	num_vfs = dev_num_vf(dev->dev.parent);
1416 	if (nla_put_u32(skb, IFLA_NUM_VF, num_vfs))
1417 		return -EMSGSIZE;
1418 
1419 	if (!dev->netdev_ops->ndo_get_vf_config)
1420 		return 0;
1421 
1422 	vfinfo = nla_nest_start_noflag(skb, IFLA_VFINFO_LIST);
1423 	if (!vfinfo)
1424 		return -EMSGSIZE;
1425 
1426 	for (i = 0; i < num_vfs; i++) {
1427 		if (rtnl_fill_vfinfo(skb, dev, i, vfinfo, ext_filter_mask))
1428 			return -EMSGSIZE;
1429 	}
1430 
1431 	nla_nest_end(skb, vfinfo);
1432 	return 0;
1433 }
1434 
rtnl_fill_link_ifmap(struct sk_buff * skb,struct net_device * dev)1435 static int rtnl_fill_link_ifmap(struct sk_buff *skb, struct net_device *dev)
1436 {
1437 	struct rtnl_link_ifmap map;
1438 
1439 	memset(&map, 0, sizeof(map));
1440 	map.mem_start   = dev->mem_start;
1441 	map.mem_end     = dev->mem_end;
1442 	map.base_addr   = dev->base_addr;
1443 	map.irq         = dev->irq;
1444 	map.dma         = dev->dma;
1445 	map.port        = dev->if_port;
1446 
1447 	if (nla_put_64bit(skb, IFLA_MAP, sizeof(map), &map, IFLA_PAD))
1448 		return -EMSGSIZE;
1449 
1450 	return 0;
1451 }
1452 
rtnl_xdp_prog_skb(struct net_device * dev)1453 static u32 rtnl_xdp_prog_skb(struct net_device *dev)
1454 {
1455 	const struct bpf_prog *generic_xdp_prog;
1456 
1457 	ASSERT_RTNL();
1458 
1459 	generic_xdp_prog = rtnl_dereference(dev->xdp_prog);
1460 	if (!generic_xdp_prog)
1461 		return 0;
1462 	return generic_xdp_prog->aux->id;
1463 }
1464 
rtnl_xdp_prog_drv(struct net_device * dev)1465 static u32 rtnl_xdp_prog_drv(struct net_device *dev)
1466 {
1467 	return dev_xdp_prog_id(dev, XDP_MODE_DRV);
1468 }
1469 
rtnl_xdp_prog_hw(struct net_device * dev)1470 static u32 rtnl_xdp_prog_hw(struct net_device *dev)
1471 {
1472 	return dev_xdp_prog_id(dev, XDP_MODE_HW);
1473 }
1474 
rtnl_xdp_report_one(struct sk_buff * skb,struct net_device * dev,u32 * prog_id,u8 * mode,u8 tgt_mode,u32 attr,u32 (* get_prog_id)(struct net_device * dev))1475 static int rtnl_xdp_report_one(struct sk_buff *skb, struct net_device *dev,
1476 			       u32 *prog_id, u8 *mode, u8 tgt_mode, u32 attr,
1477 			       u32 (*get_prog_id)(struct net_device *dev))
1478 {
1479 	u32 curr_id;
1480 	int err;
1481 
1482 	curr_id = get_prog_id(dev);
1483 	if (!curr_id)
1484 		return 0;
1485 
1486 	*prog_id = curr_id;
1487 	err = nla_put_u32(skb, attr, curr_id);
1488 	if (err)
1489 		return err;
1490 
1491 	if (*mode != XDP_ATTACHED_NONE)
1492 		*mode = XDP_ATTACHED_MULTI;
1493 	else
1494 		*mode = tgt_mode;
1495 
1496 	return 0;
1497 }
1498 
rtnl_xdp_fill(struct sk_buff * skb,struct net_device * dev)1499 static int rtnl_xdp_fill(struct sk_buff *skb, struct net_device *dev)
1500 {
1501 	struct nlattr *xdp;
1502 	u32 prog_id;
1503 	int err;
1504 	u8 mode;
1505 
1506 	xdp = nla_nest_start_noflag(skb, IFLA_XDP);
1507 	if (!xdp)
1508 		return -EMSGSIZE;
1509 
1510 	prog_id = 0;
1511 	mode = XDP_ATTACHED_NONE;
1512 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_SKB,
1513 				  IFLA_XDP_SKB_PROG_ID, rtnl_xdp_prog_skb);
1514 	if (err)
1515 		goto err_cancel;
1516 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_DRV,
1517 				  IFLA_XDP_DRV_PROG_ID, rtnl_xdp_prog_drv);
1518 	if (err)
1519 		goto err_cancel;
1520 	err = rtnl_xdp_report_one(skb, dev, &prog_id, &mode, XDP_ATTACHED_HW,
1521 				  IFLA_XDP_HW_PROG_ID, rtnl_xdp_prog_hw);
1522 	if (err)
1523 		goto err_cancel;
1524 
1525 	err = nla_put_u8(skb, IFLA_XDP_ATTACHED, mode);
1526 	if (err)
1527 		goto err_cancel;
1528 
1529 	if (prog_id && mode != XDP_ATTACHED_MULTI) {
1530 		err = nla_put_u32(skb, IFLA_XDP_PROG_ID, prog_id);
1531 		if (err)
1532 			goto err_cancel;
1533 	}
1534 
1535 	nla_nest_end(skb, xdp);
1536 	return 0;
1537 
1538 err_cancel:
1539 	nla_nest_cancel(skb, xdp);
1540 	return err;
1541 }
1542 
rtnl_get_event(unsigned long event)1543 static u32 rtnl_get_event(unsigned long event)
1544 {
1545 	u32 rtnl_event_type = IFLA_EVENT_NONE;
1546 
1547 	switch (event) {
1548 	case NETDEV_REBOOT:
1549 		rtnl_event_type = IFLA_EVENT_REBOOT;
1550 		break;
1551 	case NETDEV_FEAT_CHANGE:
1552 		rtnl_event_type = IFLA_EVENT_FEATURES;
1553 		break;
1554 	case NETDEV_BONDING_FAILOVER:
1555 		rtnl_event_type = IFLA_EVENT_BONDING_FAILOVER;
1556 		break;
1557 	case NETDEV_NOTIFY_PEERS:
1558 		rtnl_event_type = IFLA_EVENT_NOTIFY_PEERS;
1559 		break;
1560 	case NETDEV_RESEND_IGMP:
1561 		rtnl_event_type = IFLA_EVENT_IGMP_RESEND;
1562 		break;
1563 	case NETDEV_CHANGEINFODATA:
1564 		rtnl_event_type = IFLA_EVENT_BONDING_OPTIONS;
1565 		break;
1566 	default:
1567 		break;
1568 	}
1569 
1570 	return rtnl_event_type;
1571 }
1572 
put_master_ifindex(struct sk_buff * skb,struct net_device * dev)1573 static int put_master_ifindex(struct sk_buff *skb, struct net_device *dev)
1574 {
1575 	const struct net_device *upper_dev;
1576 	int ret = 0;
1577 
1578 	rcu_read_lock();
1579 
1580 	upper_dev = netdev_master_upper_dev_get_rcu(dev);
1581 	if (upper_dev)
1582 		ret = nla_put_u32(skb, IFLA_MASTER, upper_dev->ifindex);
1583 
1584 	rcu_read_unlock();
1585 	return ret;
1586 }
1587 
nla_put_iflink(struct sk_buff * skb,const struct net_device * dev,bool force)1588 static int nla_put_iflink(struct sk_buff *skb, const struct net_device *dev,
1589 			  bool force)
1590 {
1591 	int ifindex = dev_get_iflink(dev);
1592 
1593 	if (force || dev->ifindex != ifindex)
1594 		return nla_put_u32(skb, IFLA_LINK, ifindex);
1595 
1596 	return 0;
1597 }
1598 
nla_put_ifalias(struct sk_buff * skb,struct net_device * dev)1599 static noinline_for_stack int nla_put_ifalias(struct sk_buff *skb,
1600 					      struct net_device *dev)
1601 {
1602 	char buf[IFALIASZ];
1603 	int ret;
1604 
1605 	ret = dev_get_alias(dev, buf, sizeof(buf));
1606 	return ret > 0 ? nla_put_string(skb, IFLA_IFALIAS, buf) : 0;
1607 }
1608 
rtnl_fill_link_netnsid(struct sk_buff * skb,const struct net_device * dev,struct net * src_net,gfp_t gfp)1609 static int rtnl_fill_link_netnsid(struct sk_buff *skb,
1610 				  const struct net_device *dev,
1611 				  struct net *src_net, gfp_t gfp)
1612 {
1613 	bool put_iflink = false;
1614 
1615 	if (dev->rtnl_link_ops && dev->rtnl_link_ops->get_link_net) {
1616 		struct net *link_net = dev->rtnl_link_ops->get_link_net(dev);
1617 
1618 		if (!net_eq(dev_net(dev), link_net)) {
1619 			int id = peernet2id_alloc(src_net, link_net, gfp);
1620 
1621 			if (nla_put_s32(skb, IFLA_LINK_NETNSID, id))
1622 				return -EMSGSIZE;
1623 
1624 			put_iflink = true;
1625 		}
1626 	}
1627 
1628 	return nla_put_iflink(skb, dev, put_iflink);
1629 }
1630 
rtnl_fill_link_af(struct sk_buff * skb,const struct net_device * dev,u32 ext_filter_mask)1631 static int rtnl_fill_link_af(struct sk_buff *skb,
1632 			     const struct net_device *dev,
1633 			     u32 ext_filter_mask)
1634 {
1635 	const struct rtnl_af_ops *af_ops;
1636 	struct nlattr *af_spec;
1637 
1638 	af_spec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
1639 	if (!af_spec)
1640 		return -EMSGSIZE;
1641 
1642 	list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
1643 		struct nlattr *af;
1644 		int err;
1645 
1646 		if (!af_ops->fill_link_af)
1647 			continue;
1648 
1649 		af = nla_nest_start_noflag(skb, af_ops->family);
1650 		if (!af)
1651 			return -EMSGSIZE;
1652 
1653 		err = af_ops->fill_link_af(skb, dev, ext_filter_mask);
1654 		/*
1655 		 * Caller may return ENODATA to indicate that there
1656 		 * was no data to be dumped. This is not an error, it
1657 		 * means we should trim the attribute header and
1658 		 * continue.
1659 		 */
1660 		if (err == -ENODATA)
1661 			nla_nest_cancel(skb, af);
1662 		else if (err < 0)
1663 			return -EMSGSIZE;
1664 
1665 		nla_nest_end(skb, af);
1666 	}
1667 
1668 	nla_nest_end(skb, af_spec);
1669 	return 0;
1670 }
1671 
rtnl_fill_alt_ifnames(struct sk_buff * skb,const struct net_device * dev)1672 static int rtnl_fill_alt_ifnames(struct sk_buff *skb,
1673 				 const struct net_device *dev)
1674 {
1675 	struct netdev_name_node *name_node;
1676 	int count = 0;
1677 
1678 	list_for_each_entry(name_node, &dev->name_node->list, list) {
1679 		if (nla_put_string(skb, IFLA_ALT_IFNAME, name_node->name))
1680 			return -EMSGSIZE;
1681 		count++;
1682 	}
1683 	return count;
1684 }
1685 
rtnl_fill_prop_list(struct sk_buff * skb,const struct net_device * dev)1686 static int rtnl_fill_prop_list(struct sk_buff *skb,
1687 			       const struct net_device *dev)
1688 {
1689 	struct nlattr *prop_list;
1690 	int ret;
1691 
1692 	prop_list = nla_nest_start(skb, IFLA_PROP_LIST);
1693 	if (!prop_list)
1694 		return -EMSGSIZE;
1695 
1696 	ret = rtnl_fill_alt_ifnames(skb, dev);
1697 	if (ret <= 0)
1698 		goto nest_cancel;
1699 
1700 	nla_nest_end(skb, prop_list);
1701 	return 0;
1702 
1703 nest_cancel:
1704 	nla_nest_cancel(skb, prop_list);
1705 	return ret;
1706 }
1707 
rtnl_fill_proto_down(struct sk_buff * skb,const struct net_device * dev)1708 static int rtnl_fill_proto_down(struct sk_buff *skb,
1709 				const struct net_device *dev)
1710 {
1711 	struct nlattr *pr;
1712 	u32 preason;
1713 
1714 	if (nla_put_u8(skb, IFLA_PROTO_DOWN, dev->proto_down))
1715 		goto nla_put_failure;
1716 
1717 	preason = dev->proto_down_reason;
1718 	if (!preason)
1719 		return 0;
1720 
1721 	pr = nla_nest_start(skb, IFLA_PROTO_DOWN_REASON);
1722 	if (!pr)
1723 		return -EMSGSIZE;
1724 
1725 	if (nla_put_u32(skb, IFLA_PROTO_DOWN_REASON_VALUE, preason)) {
1726 		nla_nest_cancel(skb, pr);
1727 		goto nla_put_failure;
1728 	}
1729 
1730 	nla_nest_end(skb, pr);
1731 	return 0;
1732 
1733 nla_put_failure:
1734 	return -EMSGSIZE;
1735 }
1736 
rtnl_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,struct net * src_net,int type,u32 pid,u32 seq,u32 change,unsigned int flags,u32 ext_filter_mask,u32 event,int * new_nsid,int new_ifindex,int tgt_netnsid,gfp_t gfp)1737 static int rtnl_fill_ifinfo(struct sk_buff *skb,
1738 			    struct net_device *dev, struct net *src_net,
1739 			    int type, u32 pid, u32 seq, u32 change,
1740 			    unsigned int flags, u32 ext_filter_mask,
1741 			    u32 event, int *new_nsid, int new_ifindex,
1742 			    int tgt_netnsid, gfp_t gfp)
1743 {
1744 	struct ifinfomsg *ifm;
1745 	struct nlmsghdr *nlh;
1746 	struct Qdisc *qdisc;
1747 
1748 	ASSERT_RTNL();
1749 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
1750 	if (nlh == NULL)
1751 		return -EMSGSIZE;
1752 
1753 	ifm = nlmsg_data(nlh);
1754 	ifm->ifi_family = AF_UNSPEC;
1755 	ifm->__ifi_pad = 0;
1756 	ifm->ifi_type = dev->type;
1757 	ifm->ifi_index = dev->ifindex;
1758 	ifm->ifi_flags = dev_get_flags(dev);
1759 	ifm->ifi_change = change;
1760 
1761 	if (tgt_netnsid >= 0 && nla_put_s32(skb, IFLA_TARGET_NETNSID, tgt_netnsid))
1762 		goto nla_put_failure;
1763 
1764 	qdisc = rtnl_dereference(dev->qdisc);
1765 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
1766 	    nla_put_u32(skb, IFLA_TXQLEN, dev->tx_queue_len) ||
1767 	    nla_put_u8(skb, IFLA_OPERSTATE,
1768 		       netif_running(dev) ? dev->operstate : IF_OPER_DOWN) ||
1769 	    nla_put_u8(skb, IFLA_LINKMODE, dev->link_mode) ||
1770 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
1771 	    nla_put_u32(skb, IFLA_MIN_MTU, dev->min_mtu) ||
1772 	    nla_put_u32(skb, IFLA_MAX_MTU, dev->max_mtu) ||
1773 	    nla_put_u32(skb, IFLA_GROUP, dev->group) ||
1774 	    nla_put_u32(skb, IFLA_PROMISCUITY, dev->promiscuity) ||
1775 	    nla_put_u32(skb, IFLA_ALLMULTI, dev->allmulti) ||
1776 	    nla_put_u32(skb, IFLA_NUM_TX_QUEUES, dev->num_tx_queues) ||
1777 	    nla_put_u32(skb, IFLA_GSO_MAX_SEGS, dev->gso_max_segs) ||
1778 	    nla_put_u32(skb, IFLA_GSO_MAX_SIZE, dev->gso_max_size) ||
1779 	    nla_put_u32(skb, IFLA_GRO_MAX_SIZE, dev->gro_max_size) ||
1780 	    nla_put_u32(skb, IFLA_TSO_MAX_SIZE, dev->tso_max_size) ||
1781 	    nla_put_u32(skb, IFLA_TSO_MAX_SEGS, dev->tso_max_segs) ||
1782 #ifdef CONFIG_RPS
1783 	    nla_put_u32(skb, IFLA_NUM_RX_QUEUES, dev->num_rx_queues) ||
1784 #endif
1785 	    put_master_ifindex(skb, dev) ||
1786 	    nla_put_u8(skb, IFLA_CARRIER, netif_carrier_ok(dev)) ||
1787 	    (qdisc &&
1788 	     nla_put_string(skb, IFLA_QDISC, qdisc->ops->id)) ||
1789 	    nla_put_ifalias(skb, dev) ||
1790 	    nla_put_u32(skb, IFLA_CARRIER_CHANGES,
1791 			atomic_read(&dev->carrier_up_count) +
1792 			atomic_read(&dev->carrier_down_count)) ||
1793 	    nla_put_u32(skb, IFLA_CARRIER_UP_COUNT,
1794 			atomic_read(&dev->carrier_up_count)) ||
1795 	    nla_put_u32(skb, IFLA_CARRIER_DOWN_COUNT,
1796 			atomic_read(&dev->carrier_down_count)))
1797 		goto nla_put_failure;
1798 
1799 	if (rtnl_fill_proto_down(skb, dev))
1800 		goto nla_put_failure;
1801 
1802 	if (event != IFLA_EVENT_NONE) {
1803 		if (nla_put_u32(skb, IFLA_EVENT, event))
1804 			goto nla_put_failure;
1805 	}
1806 
1807 	if (rtnl_fill_link_ifmap(skb, dev))
1808 		goto nla_put_failure;
1809 
1810 	if (dev->addr_len) {
1811 		if (nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr) ||
1812 		    nla_put(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast))
1813 			goto nla_put_failure;
1814 	}
1815 
1816 	if (rtnl_phys_port_id_fill(skb, dev))
1817 		goto nla_put_failure;
1818 
1819 	if (rtnl_phys_port_name_fill(skb, dev))
1820 		goto nla_put_failure;
1821 
1822 	if (rtnl_phys_switch_id_fill(skb, dev))
1823 		goto nla_put_failure;
1824 
1825 	if (rtnl_fill_stats(skb, dev))
1826 		goto nla_put_failure;
1827 
1828 	if (rtnl_fill_vf(skb, dev, ext_filter_mask))
1829 		goto nla_put_failure;
1830 
1831 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1832 		goto nla_put_failure;
1833 
1834 	if (rtnl_xdp_fill(skb, dev))
1835 		goto nla_put_failure;
1836 
1837 	if (dev->rtnl_link_ops || rtnl_have_link_slave_info(dev)) {
1838 		if (rtnl_link_fill(skb, dev) < 0)
1839 			goto nla_put_failure;
1840 	}
1841 
1842 	if (rtnl_fill_link_netnsid(skb, dev, src_net, gfp))
1843 		goto nla_put_failure;
1844 
1845 	if (new_nsid &&
1846 	    nla_put_s32(skb, IFLA_NEW_NETNSID, *new_nsid) < 0)
1847 		goto nla_put_failure;
1848 	if (new_ifindex &&
1849 	    nla_put_s32(skb, IFLA_NEW_IFINDEX, new_ifindex) < 0)
1850 		goto nla_put_failure;
1851 
1852 	if (memchr_inv(dev->perm_addr, '\0', dev->addr_len) &&
1853 	    nla_put(skb, IFLA_PERM_ADDRESS, dev->addr_len, dev->perm_addr))
1854 		goto nla_put_failure;
1855 
1856 	rcu_read_lock();
1857 	if (rtnl_fill_link_af(skb, dev, ext_filter_mask))
1858 		goto nla_put_failure_rcu;
1859 	rcu_read_unlock();
1860 
1861 	if (rtnl_fill_prop_list(skb, dev))
1862 		goto nla_put_failure;
1863 
1864 	if (dev->dev.parent &&
1865 	    nla_put_string(skb, IFLA_PARENT_DEV_NAME,
1866 			   dev_name(dev->dev.parent)))
1867 		goto nla_put_failure;
1868 
1869 	if (dev->dev.parent && dev->dev.parent->bus &&
1870 	    nla_put_string(skb, IFLA_PARENT_DEV_BUS_NAME,
1871 			   dev->dev.parent->bus->name))
1872 		goto nla_put_failure;
1873 
1874 	nlmsg_end(skb, nlh);
1875 	return 0;
1876 
1877 nla_put_failure_rcu:
1878 	rcu_read_unlock();
1879 nla_put_failure:
1880 	nlmsg_cancel(skb, nlh);
1881 	return -EMSGSIZE;
1882 }
1883 
1884 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1885 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1886 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1887 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1888 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1889 	[IFLA_MTU]		= { .type = NLA_U32 },
1890 	[IFLA_LINK]		= { .type = NLA_U32 },
1891 	[IFLA_MASTER]		= { .type = NLA_U32 },
1892 	[IFLA_CARRIER]		= { .type = NLA_U8 },
1893 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1894 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1895 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1896 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1897 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1898 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1899 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1900 	/* IFLA_IFALIAS is a string, but policy is set to NLA_BINARY to
1901 	 * allow 0-length string (needed to remove an alias).
1902 	 */
1903 	[IFLA_IFALIAS]	        = { .type = NLA_BINARY, .len = IFALIASZ - 1 },
1904 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1905 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1906 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1907 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1908 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1909 	[IFLA_PROMISCUITY]	= { .type = NLA_U32 },
1910 	[IFLA_NUM_TX_QUEUES]	= { .type = NLA_U32 },
1911 	[IFLA_NUM_RX_QUEUES]	= { .type = NLA_U32 },
1912 	[IFLA_GSO_MAX_SEGS]	= { .type = NLA_U32 },
1913 	[IFLA_GSO_MAX_SIZE]	= { .type = NLA_U32 },
1914 	[IFLA_PHYS_PORT_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1915 	[IFLA_CARRIER_CHANGES]	= { .type = NLA_U32 },  /* ignored */
1916 	[IFLA_PHYS_SWITCH_ID]	= { .type = NLA_BINARY, .len = MAX_PHYS_ITEM_ID_LEN },
1917 	[IFLA_LINK_NETNSID]	= { .type = NLA_S32 },
1918 	[IFLA_PROTO_DOWN]	= { .type = NLA_U8 },
1919 	[IFLA_XDP]		= { .type = NLA_NESTED },
1920 	[IFLA_EVENT]		= { .type = NLA_U32 },
1921 	[IFLA_GROUP]		= { .type = NLA_U32 },
1922 	[IFLA_TARGET_NETNSID]	= { .type = NLA_S32 },
1923 	[IFLA_CARRIER_UP_COUNT]	= { .type = NLA_U32 },
1924 	[IFLA_CARRIER_DOWN_COUNT] = { .type = NLA_U32 },
1925 	[IFLA_MIN_MTU]		= { .type = NLA_U32 },
1926 	[IFLA_MAX_MTU]		= { .type = NLA_U32 },
1927 	[IFLA_PROP_LIST]	= { .type = NLA_NESTED },
1928 	[IFLA_ALT_IFNAME]	= { .type = NLA_STRING,
1929 				    .len = ALTIFNAMSIZ - 1 },
1930 	[IFLA_PERM_ADDRESS]	= { .type = NLA_REJECT },
1931 	[IFLA_PROTO_DOWN_REASON] = { .type = NLA_NESTED },
1932 	[IFLA_NEW_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
1933 	[IFLA_PARENT_DEV_NAME]	= { .type = NLA_NUL_STRING },
1934 	[IFLA_GRO_MAX_SIZE]	= { .type = NLA_U32 },
1935 	[IFLA_TSO_MAX_SIZE]	= { .type = NLA_REJECT },
1936 	[IFLA_TSO_MAX_SEGS]	= { .type = NLA_REJECT },
1937 	[IFLA_ALLMULTI]		= { .type = NLA_REJECT },
1938 };
1939 
1940 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1941 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1942 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1943 	[IFLA_INFO_SLAVE_KIND]	= { .type = NLA_STRING },
1944 	[IFLA_INFO_SLAVE_DATA]	= { .type = NLA_NESTED },
1945 };
1946 
1947 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1948 	[IFLA_VF_MAC]		= { .len = sizeof(struct ifla_vf_mac) },
1949 	[IFLA_VF_BROADCAST]	= { .type = NLA_REJECT },
1950 	[IFLA_VF_VLAN]		= { .len = sizeof(struct ifla_vf_vlan) },
1951 	[IFLA_VF_VLAN_LIST]     = { .type = NLA_NESTED },
1952 	[IFLA_VF_TX_RATE]	= { .len = sizeof(struct ifla_vf_tx_rate) },
1953 	[IFLA_VF_SPOOFCHK]	= { .len = sizeof(struct ifla_vf_spoofchk) },
1954 	[IFLA_VF_RATE]		= { .len = sizeof(struct ifla_vf_rate) },
1955 	[IFLA_VF_LINK_STATE]	= { .len = sizeof(struct ifla_vf_link_state) },
1956 	[IFLA_VF_RSS_QUERY_EN]	= { .len = sizeof(struct ifla_vf_rss_query_en) },
1957 	[IFLA_VF_STATS]		= { .type = NLA_NESTED },
1958 	[IFLA_VF_TRUST]		= { .len = sizeof(struct ifla_vf_trust) },
1959 	[IFLA_VF_IB_NODE_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1960 	[IFLA_VF_IB_PORT_GUID]	= { .len = sizeof(struct ifla_vf_guid) },
1961 };
1962 
1963 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1964 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1965 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1966 				    .len = PORT_PROFILE_MAX },
1967 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1968 				      .len = PORT_UUID_MAX },
1969 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1970 				    .len = PORT_UUID_MAX },
1971 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1972 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1973 
1974 	/* Unused, but we need to keep it here since user space could
1975 	 * fill it. It's also broken with regard to NLA_BINARY use in
1976 	 * combination with structs.
1977 	 */
1978 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1979 				    .len = sizeof(struct ifla_port_vsi) },
1980 };
1981 
1982 static const struct nla_policy ifla_xdp_policy[IFLA_XDP_MAX + 1] = {
1983 	[IFLA_XDP_UNSPEC]	= { .strict_start_type = IFLA_XDP_EXPECTED_FD },
1984 	[IFLA_XDP_FD]		= { .type = NLA_S32 },
1985 	[IFLA_XDP_EXPECTED_FD]	= { .type = NLA_S32 },
1986 	[IFLA_XDP_ATTACHED]	= { .type = NLA_U8 },
1987 	[IFLA_XDP_FLAGS]	= { .type = NLA_U32 },
1988 	[IFLA_XDP_PROG_ID]	= { .type = NLA_U32 },
1989 };
1990 
linkinfo_to_kind_ops(const struct nlattr * nla)1991 static const struct rtnl_link_ops *linkinfo_to_kind_ops(const struct nlattr *nla)
1992 {
1993 	const struct rtnl_link_ops *ops = NULL;
1994 	struct nlattr *linfo[IFLA_INFO_MAX + 1];
1995 
1996 	if (nla_parse_nested_deprecated(linfo, IFLA_INFO_MAX, nla, ifla_info_policy, NULL) < 0)
1997 		return NULL;
1998 
1999 	if (linfo[IFLA_INFO_KIND]) {
2000 		char kind[MODULE_NAME_LEN];
2001 
2002 		nla_strscpy(kind, linfo[IFLA_INFO_KIND], sizeof(kind));
2003 		ops = rtnl_link_ops_get(kind);
2004 	}
2005 
2006 	return ops;
2007 }
2008 
link_master_filtered(struct net_device * dev,int master_idx)2009 static bool link_master_filtered(struct net_device *dev, int master_idx)
2010 {
2011 	struct net_device *master;
2012 
2013 	if (!master_idx)
2014 		return false;
2015 
2016 	master = netdev_master_upper_dev_get(dev);
2017 
2018 	/* 0 is already used to denote IFLA_MASTER wasn't passed, therefore need
2019 	 * another invalid value for ifindex to denote "no master".
2020 	 */
2021 	if (master_idx == -1)
2022 		return !!master;
2023 
2024 	if (!master || master->ifindex != master_idx)
2025 		return true;
2026 
2027 	return false;
2028 }
2029 
link_kind_filtered(const struct net_device * dev,const struct rtnl_link_ops * kind_ops)2030 static bool link_kind_filtered(const struct net_device *dev,
2031 			       const struct rtnl_link_ops *kind_ops)
2032 {
2033 	if (kind_ops && dev->rtnl_link_ops != kind_ops)
2034 		return true;
2035 
2036 	return false;
2037 }
2038 
link_dump_filtered(struct net_device * dev,int master_idx,const struct rtnl_link_ops * kind_ops)2039 static bool link_dump_filtered(struct net_device *dev,
2040 			       int master_idx,
2041 			       const struct rtnl_link_ops *kind_ops)
2042 {
2043 	if (link_master_filtered(dev, master_idx) ||
2044 	    link_kind_filtered(dev, kind_ops))
2045 		return true;
2046 
2047 	return false;
2048 }
2049 
2050 /**
2051  * rtnl_get_net_ns_capable - Get netns if sufficiently privileged.
2052  * @sk: netlink socket
2053  * @netnsid: network namespace identifier
2054  *
2055  * Returns the network namespace identified by netnsid on success or an error
2056  * pointer on failure.
2057  */
rtnl_get_net_ns_capable(struct sock * sk,int netnsid)2058 struct net *rtnl_get_net_ns_capable(struct sock *sk, int netnsid)
2059 {
2060 	struct net *net;
2061 
2062 	net = get_net_ns_by_id(sock_net(sk), netnsid);
2063 	if (!net)
2064 		return ERR_PTR(-EINVAL);
2065 
2066 	/* For now, the caller is required to have CAP_NET_ADMIN in
2067 	 * the user namespace owning the target net ns.
2068 	 */
2069 	if (!sk_ns_capable(sk, net->user_ns, CAP_NET_ADMIN)) {
2070 		put_net(net);
2071 		return ERR_PTR(-EACCES);
2072 	}
2073 	return net;
2074 }
2075 EXPORT_SYMBOL_GPL(rtnl_get_net_ns_capable);
2076 
rtnl_valid_dump_ifinfo_req(const struct nlmsghdr * nlh,bool strict_check,struct nlattr ** tb,struct netlink_ext_ack * extack)2077 static int rtnl_valid_dump_ifinfo_req(const struct nlmsghdr *nlh,
2078 				      bool strict_check, struct nlattr **tb,
2079 				      struct netlink_ext_ack *extack)
2080 {
2081 	int hdrlen;
2082 
2083 	if (strict_check) {
2084 		struct ifinfomsg *ifm;
2085 
2086 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2087 			NL_SET_ERR_MSG(extack, "Invalid header for link dump");
2088 			return -EINVAL;
2089 		}
2090 
2091 		ifm = nlmsg_data(nlh);
2092 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2093 		    ifm->ifi_change) {
2094 			NL_SET_ERR_MSG(extack, "Invalid values in header for link dump request");
2095 			return -EINVAL;
2096 		}
2097 		if (ifm->ifi_index) {
2098 			NL_SET_ERR_MSG(extack, "Filter by device index not supported for link dumps");
2099 			return -EINVAL;
2100 		}
2101 
2102 		return nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb,
2103 						     IFLA_MAX, ifla_policy,
2104 						     extack);
2105 	}
2106 
2107 	/* A hack to preserve kernel<->userspace interface.
2108 	 * The correct header is ifinfomsg. It is consistent with rtnl_getlink.
2109 	 * However, before Linux v3.9 the code here assumed rtgenmsg and that's
2110 	 * what iproute2 < v3.9.0 used.
2111 	 * We can detect the old iproute2. Even including the IFLA_EXT_MASK
2112 	 * attribute, its netlink message is shorter than struct ifinfomsg.
2113 	 */
2114 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
2115 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
2116 
2117 	return nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy,
2118 				      extack);
2119 }
2120 
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)2121 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
2122 {
2123 	struct netlink_ext_ack *extack = cb->extack;
2124 	const struct nlmsghdr *nlh = cb->nlh;
2125 	struct net *net = sock_net(skb->sk);
2126 	struct net *tgt_net = net;
2127 	int h, s_h;
2128 	int idx = 0, s_idx;
2129 	struct net_device *dev;
2130 	struct hlist_head *head;
2131 	struct nlattr *tb[IFLA_MAX+1];
2132 	u32 ext_filter_mask = 0;
2133 	const struct rtnl_link_ops *kind_ops = NULL;
2134 	unsigned int flags = NLM_F_MULTI;
2135 	int master_idx = 0;
2136 	int netnsid = -1;
2137 	int err, i;
2138 
2139 	s_h = cb->args[0];
2140 	s_idx = cb->args[1];
2141 
2142 	err = rtnl_valid_dump_ifinfo_req(nlh, cb->strict_check, tb, extack);
2143 	if (err < 0) {
2144 		if (cb->strict_check)
2145 			return err;
2146 
2147 		goto walk_entries;
2148 	}
2149 
2150 	for (i = 0; i <= IFLA_MAX; ++i) {
2151 		if (!tb[i])
2152 			continue;
2153 
2154 		/* new attributes should only be added with strict checking */
2155 		switch (i) {
2156 		case IFLA_TARGET_NETNSID:
2157 			netnsid = nla_get_s32(tb[i]);
2158 			tgt_net = rtnl_get_net_ns_capable(skb->sk, netnsid);
2159 			if (IS_ERR(tgt_net)) {
2160 				NL_SET_ERR_MSG(extack, "Invalid target network namespace id");
2161 				return PTR_ERR(tgt_net);
2162 			}
2163 			break;
2164 		case IFLA_EXT_MASK:
2165 			ext_filter_mask = nla_get_u32(tb[i]);
2166 			break;
2167 		case IFLA_MASTER:
2168 			master_idx = nla_get_u32(tb[i]);
2169 			break;
2170 		case IFLA_LINKINFO:
2171 			kind_ops = linkinfo_to_kind_ops(tb[i]);
2172 			break;
2173 		default:
2174 			if (cb->strict_check) {
2175 				NL_SET_ERR_MSG(extack, "Unsupported attribute in link dump request");
2176 				return -EINVAL;
2177 			}
2178 		}
2179 	}
2180 
2181 	if (master_idx || kind_ops)
2182 		flags |= NLM_F_DUMP_FILTERED;
2183 
2184 walk_entries:
2185 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
2186 		idx = 0;
2187 		head = &tgt_net->dev_index_head[h];
2188 		hlist_for_each_entry(dev, head, index_hlist) {
2189 			if (link_dump_filtered(dev, master_idx, kind_ops))
2190 				goto cont;
2191 			if (idx < s_idx)
2192 				goto cont;
2193 			err = rtnl_fill_ifinfo(skb, dev, net,
2194 					       RTM_NEWLINK,
2195 					       NETLINK_CB(cb->skb).portid,
2196 					       nlh->nlmsg_seq, 0, flags,
2197 					       ext_filter_mask, 0, NULL, 0,
2198 					       netnsid, GFP_KERNEL);
2199 
2200 			if (err < 0) {
2201 				if (likely(skb->len))
2202 					goto out;
2203 
2204 				goto out_err;
2205 			}
2206 cont:
2207 			idx++;
2208 		}
2209 	}
2210 out:
2211 	err = skb->len;
2212 out_err:
2213 	cb->args[1] = idx;
2214 	cb->args[0] = h;
2215 	cb->seq = tgt_net->dev_base_seq;
2216 	nl_dump_check_consistent(cb, nlmsg_hdr(skb));
2217 	if (netnsid >= 0)
2218 		put_net(tgt_net);
2219 
2220 	return err;
2221 }
2222 
rtnl_nla_parse_ifinfomsg(struct nlattr ** tb,const struct nlattr * nla_peer,struct netlink_ext_ack * exterr)2223 int rtnl_nla_parse_ifinfomsg(struct nlattr **tb, const struct nlattr *nla_peer,
2224 			     struct netlink_ext_ack *exterr)
2225 {
2226 	const struct ifinfomsg *ifmp;
2227 	const struct nlattr *attrs;
2228 	size_t len;
2229 
2230 	ifmp = nla_data(nla_peer);
2231 	attrs = nla_data(nla_peer) + sizeof(struct ifinfomsg);
2232 	len = nla_len(nla_peer) - sizeof(struct ifinfomsg);
2233 
2234 	if (ifmp->ifi_index < 0) {
2235 		NL_SET_ERR_MSG_ATTR(exterr, nla_peer,
2236 				    "ifindex can't be negative");
2237 		return -EINVAL;
2238 	}
2239 
2240 	return nla_parse_deprecated(tb, IFLA_MAX, attrs, len, ifla_policy,
2241 				    exterr);
2242 }
2243 EXPORT_SYMBOL(rtnl_nla_parse_ifinfomsg);
2244 
rtnl_link_get_net(struct net * src_net,struct nlattr * tb[])2245 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
2246 {
2247 	struct net *net;
2248 	/* Examine the link attributes and figure out which
2249 	 * network namespace we are talking about.
2250 	 */
2251 	if (tb[IFLA_NET_NS_PID])
2252 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
2253 	else if (tb[IFLA_NET_NS_FD])
2254 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
2255 	else
2256 		net = get_net(src_net);
2257 	return net;
2258 }
2259 EXPORT_SYMBOL(rtnl_link_get_net);
2260 
2261 /* Figure out which network namespace we are talking about by
2262  * examining the link attributes in the following order:
2263  *
2264  * 1. IFLA_NET_NS_PID
2265  * 2. IFLA_NET_NS_FD
2266  * 3. IFLA_TARGET_NETNSID
2267  */
rtnl_link_get_net_by_nlattr(struct net * src_net,struct nlattr * tb[])2268 static struct net *rtnl_link_get_net_by_nlattr(struct net *src_net,
2269 					       struct nlattr *tb[])
2270 {
2271 	struct net *net;
2272 
2273 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD])
2274 		return rtnl_link_get_net(src_net, tb);
2275 
2276 	if (!tb[IFLA_TARGET_NETNSID])
2277 		return get_net(src_net);
2278 
2279 	net = get_net_ns_by_id(src_net, nla_get_u32(tb[IFLA_TARGET_NETNSID]));
2280 	if (!net)
2281 		return ERR_PTR(-EINVAL);
2282 
2283 	return net;
2284 }
2285 
rtnl_link_get_net_capable(const struct sk_buff * skb,struct net * src_net,struct nlattr * tb[],int cap)2286 static struct net *rtnl_link_get_net_capable(const struct sk_buff *skb,
2287 					     struct net *src_net,
2288 					     struct nlattr *tb[], int cap)
2289 {
2290 	struct net *net;
2291 
2292 	net = rtnl_link_get_net_by_nlattr(src_net, tb);
2293 	if (IS_ERR(net))
2294 		return net;
2295 
2296 	if (!netlink_ns_capable(skb, net->user_ns, cap)) {
2297 		put_net(net);
2298 		return ERR_PTR(-EPERM);
2299 	}
2300 
2301 	return net;
2302 }
2303 
2304 /* Verify that rtnetlink requests do not pass additional properties
2305  * potentially referring to different network namespaces.
2306  */
rtnl_ensure_unique_netns(struct nlattr * tb[],struct netlink_ext_ack * extack,bool netns_id_only)2307 static int rtnl_ensure_unique_netns(struct nlattr *tb[],
2308 				    struct netlink_ext_ack *extack,
2309 				    bool netns_id_only)
2310 {
2311 
2312 	if (netns_id_only) {
2313 		if (!tb[IFLA_NET_NS_PID] && !tb[IFLA_NET_NS_FD])
2314 			return 0;
2315 
2316 		NL_SET_ERR_MSG(extack, "specified netns attribute not supported");
2317 		return -EOPNOTSUPP;
2318 	}
2319 
2320 	if (tb[IFLA_TARGET_NETNSID] && (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]))
2321 		goto invalid_attr;
2322 
2323 	if (tb[IFLA_NET_NS_PID] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_FD]))
2324 		goto invalid_attr;
2325 
2326 	if (tb[IFLA_NET_NS_FD] && (tb[IFLA_TARGET_NETNSID] || tb[IFLA_NET_NS_PID]))
2327 		goto invalid_attr;
2328 
2329 	return 0;
2330 
2331 invalid_attr:
2332 	NL_SET_ERR_MSG(extack, "multiple netns identifying attributes specified");
2333 	return -EINVAL;
2334 }
2335 
rtnl_set_vf_rate(struct net_device * dev,int vf,int min_tx_rate,int max_tx_rate)2336 static	int rtnl_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate,
2337 			     int max_tx_rate)
2338 {
2339 	const struct net_device_ops *ops = dev->netdev_ops;
2340 
2341 	if (!ops->ndo_set_vf_rate)
2342 		return -EOPNOTSUPP;
2343 	if (max_tx_rate && max_tx_rate < min_tx_rate)
2344 		return -EINVAL;
2345 
2346 	return ops->ndo_set_vf_rate(dev, vf, min_tx_rate, max_tx_rate);
2347 }
2348 
validate_linkmsg(struct net_device * dev,struct nlattr * tb[],struct netlink_ext_ack * extack)2349 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[],
2350 			    struct netlink_ext_ack *extack)
2351 {
2352 	if (dev) {
2353 		if (tb[IFLA_ADDRESS] &&
2354 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
2355 			return -EINVAL;
2356 
2357 		if (tb[IFLA_BROADCAST] &&
2358 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
2359 			return -EINVAL;
2360 	}
2361 
2362 	if (tb[IFLA_AF_SPEC]) {
2363 		struct nlattr *af;
2364 		int rem, err;
2365 
2366 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2367 			const struct rtnl_af_ops *af_ops;
2368 
2369 			af_ops = rtnl_af_lookup(nla_type(af));
2370 			if (!af_ops)
2371 				return -EAFNOSUPPORT;
2372 
2373 			if (!af_ops->set_link_af)
2374 				return -EOPNOTSUPP;
2375 
2376 			if (af_ops->validate_link_af) {
2377 				err = af_ops->validate_link_af(dev, af, extack);
2378 				if (err < 0)
2379 					return err;
2380 			}
2381 		}
2382 	}
2383 
2384 	return 0;
2385 }
2386 
handle_infiniband_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2387 static int handle_infiniband_guid(struct net_device *dev, struct ifla_vf_guid *ivt,
2388 				  int guid_type)
2389 {
2390 	const struct net_device_ops *ops = dev->netdev_ops;
2391 
2392 	return ops->ndo_set_vf_guid(dev, ivt->vf, ivt->guid, guid_type);
2393 }
2394 
handle_vf_guid(struct net_device * dev,struct ifla_vf_guid * ivt,int guid_type)2395 static int handle_vf_guid(struct net_device *dev, struct ifla_vf_guid *ivt, int guid_type)
2396 {
2397 	if (dev->type != ARPHRD_INFINIBAND)
2398 		return -EOPNOTSUPP;
2399 
2400 	return handle_infiniband_guid(dev, ivt, guid_type);
2401 }
2402 
do_setvfinfo(struct net_device * dev,struct nlattr ** tb)2403 static int do_setvfinfo(struct net_device *dev, struct nlattr **tb)
2404 {
2405 	const struct net_device_ops *ops = dev->netdev_ops;
2406 	int err = -EINVAL;
2407 
2408 	if (tb[IFLA_VF_MAC]) {
2409 		struct ifla_vf_mac *ivm = nla_data(tb[IFLA_VF_MAC]);
2410 
2411 		if (ivm->vf >= INT_MAX)
2412 			return -EINVAL;
2413 		err = -EOPNOTSUPP;
2414 		if (ops->ndo_set_vf_mac)
2415 			err = ops->ndo_set_vf_mac(dev, ivm->vf,
2416 						  ivm->mac);
2417 		if (err < 0)
2418 			return err;
2419 	}
2420 
2421 	if (tb[IFLA_VF_VLAN]) {
2422 		struct ifla_vf_vlan *ivv = nla_data(tb[IFLA_VF_VLAN]);
2423 
2424 		if (ivv->vf >= INT_MAX)
2425 			return -EINVAL;
2426 		err = -EOPNOTSUPP;
2427 		if (ops->ndo_set_vf_vlan)
2428 			err = ops->ndo_set_vf_vlan(dev, ivv->vf, ivv->vlan,
2429 						   ivv->qos,
2430 						   htons(ETH_P_8021Q));
2431 		if (err < 0)
2432 			return err;
2433 	}
2434 
2435 	if (tb[IFLA_VF_VLAN_LIST]) {
2436 		struct ifla_vf_vlan_info *ivvl[MAX_VLAN_LIST_LEN];
2437 		struct nlattr *attr;
2438 		int rem, len = 0;
2439 
2440 		err = -EOPNOTSUPP;
2441 		if (!ops->ndo_set_vf_vlan)
2442 			return err;
2443 
2444 		nla_for_each_nested(attr, tb[IFLA_VF_VLAN_LIST], rem) {
2445 			if (nla_type(attr) != IFLA_VF_VLAN_INFO ||
2446 			    nla_len(attr) < NLA_HDRLEN) {
2447 				return -EINVAL;
2448 			}
2449 			if (len >= MAX_VLAN_LIST_LEN)
2450 				return -EOPNOTSUPP;
2451 			ivvl[len] = nla_data(attr);
2452 
2453 			len++;
2454 		}
2455 		if (len == 0)
2456 			return -EINVAL;
2457 
2458 		if (ivvl[0]->vf >= INT_MAX)
2459 			return -EINVAL;
2460 		err = ops->ndo_set_vf_vlan(dev, ivvl[0]->vf, ivvl[0]->vlan,
2461 					   ivvl[0]->qos, ivvl[0]->vlan_proto);
2462 		if (err < 0)
2463 			return err;
2464 	}
2465 
2466 	if (tb[IFLA_VF_TX_RATE]) {
2467 		struct ifla_vf_tx_rate *ivt = nla_data(tb[IFLA_VF_TX_RATE]);
2468 		struct ifla_vf_info ivf;
2469 
2470 		if (ivt->vf >= INT_MAX)
2471 			return -EINVAL;
2472 		err = -EOPNOTSUPP;
2473 		if (ops->ndo_get_vf_config)
2474 			err = ops->ndo_get_vf_config(dev, ivt->vf, &ivf);
2475 		if (err < 0)
2476 			return err;
2477 
2478 		err = rtnl_set_vf_rate(dev, ivt->vf,
2479 				       ivf.min_tx_rate, ivt->rate);
2480 		if (err < 0)
2481 			return err;
2482 	}
2483 
2484 	if (tb[IFLA_VF_RATE]) {
2485 		struct ifla_vf_rate *ivt = nla_data(tb[IFLA_VF_RATE]);
2486 
2487 		if (ivt->vf >= INT_MAX)
2488 			return -EINVAL;
2489 
2490 		err = rtnl_set_vf_rate(dev, ivt->vf,
2491 				       ivt->min_tx_rate, ivt->max_tx_rate);
2492 		if (err < 0)
2493 			return err;
2494 	}
2495 
2496 	if (tb[IFLA_VF_SPOOFCHK]) {
2497 		struct ifla_vf_spoofchk *ivs = nla_data(tb[IFLA_VF_SPOOFCHK]);
2498 
2499 		if (ivs->vf >= INT_MAX)
2500 			return -EINVAL;
2501 		err = -EOPNOTSUPP;
2502 		if (ops->ndo_set_vf_spoofchk)
2503 			err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
2504 						       ivs->setting);
2505 		if (err < 0)
2506 			return err;
2507 	}
2508 
2509 	if (tb[IFLA_VF_LINK_STATE]) {
2510 		struct ifla_vf_link_state *ivl = nla_data(tb[IFLA_VF_LINK_STATE]);
2511 
2512 		if (ivl->vf >= INT_MAX)
2513 			return -EINVAL;
2514 		err = -EOPNOTSUPP;
2515 		if (ops->ndo_set_vf_link_state)
2516 			err = ops->ndo_set_vf_link_state(dev, ivl->vf,
2517 							 ivl->link_state);
2518 		if (err < 0)
2519 			return err;
2520 	}
2521 
2522 	if (tb[IFLA_VF_RSS_QUERY_EN]) {
2523 		struct ifla_vf_rss_query_en *ivrssq_en;
2524 
2525 		err = -EOPNOTSUPP;
2526 		ivrssq_en = nla_data(tb[IFLA_VF_RSS_QUERY_EN]);
2527 		if (ivrssq_en->vf >= INT_MAX)
2528 			return -EINVAL;
2529 		if (ops->ndo_set_vf_rss_query_en)
2530 			err = ops->ndo_set_vf_rss_query_en(dev, ivrssq_en->vf,
2531 							   ivrssq_en->setting);
2532 		if (err < 0)
2533 			return err;
2534 	}
2535 
2536 	if (tb[IFLA_VF_TRUST]) {
2537 		struct ifla_vf_trust *ivt = nla_data(tb[IFLA_VF_TRUST]);
2538 
2539 		if (ivt->vf >= INT_MAX)
2540 			return -EINVAL;
2541 		err = -EOPNOTSUPP;
2542 		if (ops->ndo_set_vf_trust)
2543 			err = ops->ndo_set_vf_trust(dev, ivt->vf, ivt->setting);
2544 		if (err < 0)
2545 			return err;
2546 	}
2547 
2548 	if (tb[IFLA_VF_IB_NODE_GUID]) {
2549 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_NODE_GUID]);
2550 
2551 		if (ivt->vf >= INT_MAX)
2552 			return -EINVAL;
2553 		if (!ops->ndo_set_vf_guid)
2554 			return -EOPNOTSUPP;
2555 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_NODE_GUID);
2556 	}
2557 
2558 	if (tb[IFLA_VF_IB_PORT_GUID]) {
2559 		struct ifla_vf_guid *ivt = nla_data(tb[IFLA_VF_IB_PORT_GUID]);
2560 
2561 		if (ivt->vf >= INT_MAX)
2562 			return -EINVAL;
2563 		if (!ops->ndo_set_vf_guid)
2564 			return -EOPNOTSUPP;
2565 
2566 		return handle_vf_guid(dev, ivt, IFLA_VF_IB_PORT_GUID);
2567 	}
2568 
2569 	return err;
2570 }
2571 
do_set_master(struct net_device * dev,int ifindex,struct netlink_ext_ack * extack)2572 static int do_set_master(struct net_device *dev, int ifindex,
2573 			 struct netlink_ext_ack *extack)
2574 {
2575 	struct net_device *upper_dev = netdev_master_upper_dev_get(dev);
2576 	const struct net_device_ops *ops;
2577 	int err;
2578 
2579 	if (upper_dev) {
2580 		if (upper_dev->ifindex == ifindex)
2581 			return 0;
2582 		ops = upper_dev->netdev_ops;
2583 		if (ops->ndo_del_slave) {
2584 			err = ops->ndo_del_slave(upper_dev, dev);
2585 			if (err)
2586 				return err;
2587 		} else {
2588 			return -EOPNOTSUPP;
2589 		}
2590 	}
2591 
2592 	if (ifindex) {
2593 		upper_dev = __dev_get_by_index(dev_net(dev), ifindex);
2594 		if (!upper_dev)
2595 			return -EINVAL;
2596 		ops = upper_dev->netdev_ops;
2597 		if (ops->ndo_add_slave) {
2598 			err = ops->ndo_add_slave(upper_dev, dev, extack);
2599 			if (err)
2600 				return err;
2601 		} else {
2602 			return -EOPNOTSUPP;
2603 		}
2604 	}
2605 	return 0;
2606 }
2607 
2608 static const struct nla_policy ifla_proto_down_reason_policy[IFLA_PROTO_DOWN_REASON_VALUE + 1] = {
2609 	[IFLA_PROTO_DOWN_REASON_MASK]	= { .type = NLA_U32 },
2610 	[IFLA_PROTO_DOWN_REASON_VALUE]	= { .type = NLA_U32 },
2611 };
2612 
do_set_proto_down(struct net_device * dev,struct nlattr * nl_proto_down,struct nlattr * nl_proto_down_reason,struct netlink_ext_ack * extack)2613 static int do_set_proto_down(struct net_device *dev,
2614 			     struct nlattr *nl_proto_down,
2615 			     struct nlattr *nl_proto_down_reason,
2616 			     struct netlink_ext_ack *extack)
2617 {
2618 	struct nlattr *pdreason[IFLA_PROTO_DOWN_REASON_MAX + 1];
2619 	unsigned long mask = 0;
2620 	u32 value;
2621 	bool proto_down;
2622 	int err;
2623 
2624 	if (!(dev->priv_flags & IFF_CHANGE_PROTO_DOWN)) {
2625 		NL_SET_ERR_MSG(extack,  "Protodown not supported by device");
2626 		return -EOPNOTSUPP;
2627 	}
2628 
2629 	if (nl_proto_down_reason) {
2630 		err = nla_parse_nested_deprecated(pdreason,
2631 						  IFLA_PROTO_DOWN_REASON_MAX,
2632 						  nl_proto_down_reason,
2633 						  ifla_proto_down_reason_policy,
2634 						  NULL);
2635 		if (err < 0)
2636 			return err;
2637 
2638 		if (!pdreason[IFLA_PROTO_DOWN_REASON_VALUE]) {
2639 			NL_SET_ERR_MSG(extack, "Invalid protodown reason value");
2640 			return -EINVAL;
2641 		}
2642 
2643 		value = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_VALUE]);
2644 
2645 		if (pdreason[IFLA_PROTO_DOWN_REASON_MASK])
2646 			mask = nla_get_u32(pdreason[IFLA_PROTO_DOWN_REASON_MASK]);
2647 
2648 		dev_change_proto_down_reason(dev, mask, value);
2649 	}
2650 
2651 	if (nl_proto_down) {
2652 		proto_down = nla_get_u8(nl_proto_down);
2653 
2654 		/* Don't turn off protodown if there are active reasons */
2655 		if (!proto_down && dev->proto_down_reason) {
2656 			NL_SET_ERR_MSG(extack, "Cannot clear protodown, active reasons");
2657 			return -EBUSY;
2658 		}
2659 		err = dev_change_proto_down(dev,
2660 					    proto_down);
2661 		if (err)
2662 			return err;
2663 	}
2664 
2665 	return 0;
2666 }
2667 
2668 #define DO_SETLINK_MODIFIED	0x01
2669 /* notify flag means notify + modified. */
2670 #define DO_SETLINK_NOTIFY	0x03
do_setlink(const struct sk_buff * skb,struct net_device * dev,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb,int status)2671 static int do_setlink(const struct sk_buff *skb,
2672 		      struct net_device *dev, struct ifinfomsg *ifm,
2673 		      struct netlink_ext_ack *extack,
2674 		      struct nlattr **tb, int status)
2675 {
2676 	const struct net_device_ops *ops = dev->netdev_ops;
2677 	char ifname[IFNAMSIZ];
2678 	int err;
2679 
2680 	err = validate_linkmsg(dev, tb, extack);
2681 	if (err < 0)
2682 		return err;
2683 
2684 	if (tb[IFLA_IFNAME])
2685 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
2686 	else
2687 		ifname[0] = '\0';
2688 
2689 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD] || tb[IFLA_TARGET_NETNSID]) {
2690 		const char *pat = ifname[0] ? ifname : NULL;
2691 		struct net *net;
2692 		int new_ifindex;
2693 
2694 		net = rtnl_link_get_net_capable(skb, dev_net(dev),
2695 						tb, CAP_NET_ADMIN);
2696 		if (IS_ERR(net)) {
2697 			err = PTR_ERR(net);
2698 			goto errout;
2699 		}
2700 
2701 		if (tb[IFLA_NEW_IFINDEX])
2702 			new_ifindex = nla_get_s32(tb[IFLA_NEW_IFINDEX]);
2703 		else
2704 			new_ifindex = 0;
2705 
2706 		err = __dev_change_net_namespace(dev, net, pat, new_ifindex);
2707 		put_net(net);
2708 		if (err)
2709 			goto errout;
2710 		status |= DO_SETLINK_MODIFIED;
2711 	}
2712 
2713 	if (tb[IFLA_MAP]) {
2714 		struct rtnl_link_ifmap *u_map;
2715 		struct ifmap k_map;
2716 
2717 		if (!ops->ndo_set_config) {
2718 			err = -EOPNOTSUPP;
2719 			goto errout;
2720 		}
2721 
2722 		if (!netif_device_present(dev)) {
2723 			err = -ENODEV;
2724 			goto errout;
2725 		}
2726 
2727 		u_map = nla_data(tb[IFLA_MAP]);
2728 		k_map.mem_start = (unsigned long) u_map->mem_start;
2729 		k_map.mem_end = (unsigned long) u_map->mem_end;
2730 		k_map.base_addr = (unsigned short) u_map->base_addr;
2731 		k_map.irq = (unsigned char) u_map->irq;
2732 		k_map.dma = (unsigned char) u_map->dma;
2733 		k_map.port = (unsigned char) u_map->port;
2734 
2735 		err = ops->ndo_set_config(dev, &k_map);
2736 		if (err < 0)
2737 			goto errout;
2738 
2739 		status |= DO_SETLINK_NOTIFY;
2740 	}
2741 
2742 	if (tb[IFLA_ADDRESS]) {
2743 		struct sockaddr *sa;
2744 		int len;
2745 
2746 		len = sizeof(sa_family_t) + max_t(size_t, dev->addr_len,
2747 						  sizeof(*sa));
2748 		sa = kmalloc(len, GFP_KERNEL);
2749 		if (!sa) {
2750 			err = -ENOMEM;
2751 			goto errout;
2752 		}
2753 		sa->sa_family = dev->type;
2754 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
2755 		       dev->addr_len);
2756 		err = dev_set_mac_address_user(dev, sa, extack);
2757 		kfree(sa);
2758 		if (err)
2759 			goto errout;
2760 		status |= DO_SETLINK_MODIFIED;
2761 	}
2762 
2763 	if (tb[IFLA_MTU]) {
2764 		err = dev_set_mtu_ext(dev, nla_get_u32(tb[IFLA_MTU]), extack);
2765 		if (err < 0)
2766 			goto errout;
2767 		status |= DO_SETLINK_MODIFIED;
2768 	}
2769 
2770 	if (tb[IFLA_GROUP]) {
2771 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
2772 		status |= DO_SETLINK_NOTIFY;
2773 	}
2774 
2775 	/*
2776 	 * Interface selected by interface index but interface
2777 	 * name provided implies that a name change has been
2778 	 * requested.
2779 	 */
2780 	if (ifm->ifi_index > 0 && ifname[0]) {
2781 		err = dev_change_name(dev, ifname);
2782 		if (err < 0)
2783 			goto errout;
2784 		status |= DO_SETLINK_MODIFIED;
2785 	}
2786 
2787 	if (tb[IFLA_IFALIAS]) {
2788 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
2789 				    nla_len(tb[IFLA_IFALIAS]));
2790 		if (err < 0)
2791 			goto errout;
2792 		status |= DO_SETLINK_NOTIFY;
2793 	}
2794 
2795 	if (tb[IFLA_BROADCAST]) {
2796 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
2797 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
2798 	}
2799 
2800 	if (ifm->ifi_flags || ifm->ifi_change) {
2801 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
2802 				       extack);
2803 		if (err < 0)
2804 			goto errout;
2805 	}
2806 
2807 	if (tb[IFLA_MASTER]) {
2808 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
2809 		if (err)
2810 			goto errout;
2811 		status |= DO_SETLINK_MODIFIED;
2812 	}
2813 
2814 	if (tb[IFLA_CARRIER]) {
2815 		err = dev_change_carrier(dev, nla_get_u8(tb[IFLA_CARRIER]));
2816 		if (err)
2817 			goto errout;
2818 		status |= DO_SETLINK_MODIFIED;
2819 	}
2820 
2821 	if (tb[IFLA_TXQLEN]) {
2822 		unsigned int value = nla_get_u32(tb[IFLA_TXQLEN]);
2823 
2824 		err = dev_change_tx_queue_len(dev, value);
2825 		if (err)
2826 			goto errout;
2827 		status |= DO_SETLINK_MODIFIED;
2828 	}
2829 
2830 	if (tb[IFLA_GSO_MAX_SIZE]) {
2831 		u32 max_size = nla_get_u32(tb[IFLA_GSO_MAX_SIZE]);
2832 
2833 		if (max_size > dev->tso_max_size) {
2834 			err = -EINVAL;
2835 			goto errout;
2836 		}
2837 
2838 		if (dev->gso_max_size ^ max_size) {
2839 			netif_set_gso_max_size(dev, max_size);
2840 			status |= DO_SETLINK_MODIFIED;
2841 		}
2842 	}
2843 
2844 	if (tb[IFLA_GSO_MAX_SEGS]) {
2845 		u32 max_segs = nla_get_u32(tb[IFLA_GSO_MAX_SEGS]);
2846 
2847 		if (max_segs > GSO_MAX_SEGS || max_segs > dev->tso_max_segs) {
2848 			err = -EINVAL;
2849 			goto errout;
2850 		}
2851 
2852 		if (dev->gso_max_segs ^ max_segs) {
2853 			netif_set_gso_max_segs(dev, max_segs);
2854 			status |= DO_SETLINK_MODIFIED;
2855 		}
2856 	}
2857 
2858 	if (tb[IFLA_GRO_MAX_SIZE]) {
2859 		u32 gro_max_size = nla_get_u32(tb[IFLA_GRO_MAX_SIZE]);
2860 
2861 		if (dev->gro_max_size ^ gro_max_size) {
2862 			netif_set_gro_max_size(dev, gro_max_size);
2863 			status |= DO_SETLINK_MODIFIED;
2864 		}
2865 	}
2866 
2867 	if (tb[IFLA_OPERSTATE])
2868 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
2869 
2870 	if (tb[IFLA_LINKMODE]) {
2871 		unsigned char value = nla_get_u8(tb[IFLA_LINKMODE]);
2872 
2873 		write_lock(&dev_base_lock);
2874 		if (dev->link_mode ^ value)
2875 			status |= DO_SETLINK_NOTIFY;
2876 		dev->link_mode = value;
2877 		write_unlock(&dev_base_lock);
2878 	}
2879 
2880 	if (tb[IFLA_VFINFO_LIST]) {
2881 		struct nlattr *vfinfo[IFLA_VF_MAX + 1];
2882 		struct nlattr *attr;
2883 		int rem;
2884 
2885 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
2886 			if (nla_type(attr) != IFLA_VF_INFO ||
2887 			    nla_len(attr) < NLA_HDRLEN) {
2888 				err = -EINVAL;
2889 				goto errout;
2890 			}
2891 			err = nla_parse_nested_deprecated(vfinfo, IFLA_VF_MAX,
2892 							  attr,
2893 							  ifla_vf_policy,
2894 							  NULL);
2895 			if (err < 0)
2896 				goto errout;
2897 			err = do_setvfinfo(dev, vfinfo);
2898 			if (err < 0)
2899 				goto errout;
2900 			status |= DO_SETLINK_NOTIFY;
2901 		}
2902 	}
2903 	err = 0;
2904 
2905 	if (tb[IFLA_VF_PORTS]) {
2906 		struct nlattr *port[IFLA_PORT_MAX+1];
2907 		struct nlattr *attr;
2908 		int vf;
2909 		int rem;
2910 
2911 		err = -EOPNOTSUPP;
2912 		if (!ops->ndo_set_vf_port)
2913 			goto errout;
2914 
2915 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
2916 			if (nla_type(attr) != IFLA_VF_PORT ||
2917 			    nla_len(attr) < NLA_HDRLEN) {
2918 				err = -EINVAL;
2919 				goto errout;
2920 			}
2921 			err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2922 							  attr,
2923 							  ifla_port_policy,
2924 							  NULL);
2925 			if (err < 0)
2926 				goto errout;
2927 			if (!port[IFLA_PORT_VF]) {
2928 				err = -EOPNOTSUPP;
2929 				goto errout;
2930 			}
2931 			vf = nla_get_u32(port[IFLA_PORT_VF]);
2932 			err = ops->ndo_set_vf_port(dev, vf, port);
2933 			if (err < 0)
2934 				goto errout;
2935 			status |= DO_SETLINK_NOTIFY;
2936 		}
2937 	}
2938 	err = 0;
2939 
2940 	if (tb[IFLA_PORT_SELF]) {
2941 		struct nlattr *port[IFLA_PORT_MAX+1];
2942 
2943 		err = nla_parse_nested_deprecated(port, IFLA_PORT_MAX,
2944 						  tb[IFLA_PORT_SELF],
2945 						  ifla_port_policy, NULL);
2946 		if (err < 0)
2947 			goto errout;
2948 
2949 		err = -EOPNOTSUPP;
2950 		if (ops->ndo_set_vf_port)
2951 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
2952 		if (err < 0)
2953 			goto errout;
2954 		status |= DO_SETLINK_NOTIFY;
2955 	}
2956 
2957 	if (tb[IFLA_AF_SPEC]) {
2958 		struct nlattr *af;
2959 		int rem;
2960 
2961 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
2962 			const struct rtnl_af_ops *af_ops;
2963 
2964 			BUG_ON(!(af_ops = rtnl_af_lookup(nla_type(af))));
2965 
2966 			err = af_ops->set_link_af(dev, af, extack);
2967 			if (err < 0)
2968 				goto errout;
2969 
2970 			status |= DO_SETLINK_NOTIFY;
2971 		}
2972 	}
2973 	err = 0;
2974 
2975 	if (tb[IFLA_PROTO_DOWN] || tb[IFLA_PROTO_DOWN_REASON]) {
2976 		err = do_set_proto_down(dev, tb[IFLA_PROTO_DOWN],
2977 					tb[IFLA_PROTO_DOWN_REASON], extack);
2978 		if (err)
2979 			goto errout;
2980 		status |= DO_SETLINK_NOTIFY;
2981 	}
2982 
2983 	if (tb[IFLA_XDP]) {
2984 		struct nlattr *xdp[IFLA_XDP_MAX + 1];
2985 		u32 xdp_flags = 0;
2986 
2987 		err = nla_parse_nested_deprecated(xdp, IFLA_XDP_MAX,
2988 						  tb[IFLA_XDP],
2989 						  ifla_xdp_policy, NULL);
2990 		if (err < 0)
2991 			goto errout;
2992 
2993 		if (xdp[IFLA_XDP_ATTACHED] || xdp[IFLA_XDP_PROG_ID]) {
2994 			err = -EINVAL;
2995 			goto errout;
2996 		}
2997 
2998 		if (xdp[IFLA_XDP_FLAGS]) {
2999 			xdp_flags = nla_get_u32(xdp[IFLA_XDP_FLAGS]);
3000 			if (xdp_flags & ~XDP_FLAGS_MASK) {
3001 				err = -EINVAL;
3002 				goto errout;
3003 			}
3004 			if (hweight32(xdp_flags & XDP_FLAGS_MODES) > 1) {
3005 				err = -EINVAL;
3006 				goto errout;
3007 			}
3008 		}
3009 
3010 		if (xdp[IFLA_XDP_FD]) {
3011 			int expected_fd = -1;
3012 
3013 			if (xdp_flags & XDP_FLAGS_REPLACE) {
3014 				if (!xdp[IFLA_XDP_EXPECTED_FD]) {
3015 					err = -EINVAL;
3016 					goto errout;
3017 				}
3018 				expected_fd =
3019 					nla_get_s32(xdp[IFLA_XDP_EXPECTED_FD]);
3020 			}
3021 
3022 			err = dev_change_xdp_fd(dev, extack,
3023 						nla_get_s32(xdp[IFLA_XDP_FD]),
3024 						expected_fd,
3025 						xdp_flags);
3026 			if (err)
3027 				goto errout;
3028 			status |= DO_SETLINK_NOTIFY;
3029 		}
3030 	}
3031 
3032 errout:
3033 	if (status & DO_SETLINK_MODIFIED) {
3034 		if ((status & DO_SETLINK_NOTIFY) == DO_SETLINK_NOTIFY)
3035 			netdev_state_change(dev);
3036 
3037 		if (err < 0)
3038 			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",
3039 					     dev->name);
3040 	}
3041 
3042 	return err;
3043 }
3044 
rtnl_dev_get(struct net * net,struct nlattr * tb[])3045 static struct net_device *rtnl_dev_get(struct net *net,
3046 				       struct nlattr *tb[])
3047 {
3048 	char ifname[ALTIFNAMSIZ];
3049 
3050 	if (tb[IFLA_IFNAME])
3051 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3052 	else if (tb[IFLA_ALT_IFNAME])
3053 		nla_strscpy(ifname, tb[IFLA_ALT_IFNAME], ALTIFNAMSIZ);
3054 	else
3055 		return NULL;
3056 
3057 	return __dev_get_by_name(net, ifname);
3058 }
3059 
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3060 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3061 			struct netlink_ext_ack *extack)
3062 {
3063 	struct net *net = sock_net(skb->sk);
3064 	struct ifinfomsg *ifm;
3065 	struct net_device *dev;
3066 	int err;
3067 	struct nlattr *tb[IFLA_MAX+1];
3068 
3069 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3070 				     ifla_policy, extack);
3071 	if (err < 0)
3072 		goto errout;
3073 
3074 	err = rtnl_ensure_unique_netns(tb, extack, false);
3075 	if (err < 0)
3076 		goto errout;
3077 
3078 	err = -EINVAL;
3079 	ifm = nlmsg_data(nlh);
3080 	if (ifm->ifi_index > 0)
3081 		dev = __dev_get_by_index(net, ifm->ifi_index);
3082 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3083 		dev = rtnl_dev_get(net, tb);
3084 	else
3085 		goto errout;
3086 
3087 	if (dev == NULL) {
3088 		err = -ENODEV;
3089 		goto errout;
3090 	}
3091 
3092 	err = do_setlink(skb, dev, ifm, extack, tb, 0);
3093 errout:
3094 	return err;
3095 }
3096 
rtnl_group_dellink(const struct net * net,int group)3097 static int rtnl_group_dellink(const struct net *net, int group)
3098 {
3099 	struct net_device *dev, *aux;
3100 	LIST_HEAD(list_kill);
3101 	bool found = false;
3102 
3103 	if (!group)
3104 		return -EPERM;
3105 
3106 	for_each_netdev(net, dev) {
3107 		if (dev->group == group) {
3108 			const struct rtnl_link_ops *ops;
3109 
3110 			found = true;
3111 			ops = dev->rtnl_link_ops;
3112 			if (!ops || !ops->dellink)
3113 				return -EOPNOTSUPP;
3114 		}
3115 	}
3116 
3117 	if (!found)
3118 		return -ENODEV;
3119 
3120 	for_each_netdev_safe(net, dev, aux) {
3121 		if (dev->group == group) {
3122 			const struct rtnl_link_ops *ops;
3123 
3124 			ops = dev->rtnl_link_ops;
3125 			ops->dellink(dev, &list_kill);
3126 		}
3127 	}
3128 	unregister_netdevice_many(&list_kill);
3129 
3130 	return 0;
3131 }
3132 
rtnl_delete_link(struct net_device * dev)3133 int rtnl_delete_link(struct net_device *dev)
3134 {
3135 	const struct rtnl_link_ops *ops;
3136 	LIST_HEAD(list_kill);
3137 
3138 	ops = dev->rtnl_link_ops;
3139 	if (!ops || !ops->dellink)
3140 		return -EOPNOTSUPP;
3141 
3142 	ops->dellink(dev, &list_kill);
3143 	unregister_netdevice_many(&list_kill);
3144 
3145 	return 0;
3146 }
3147 EXPORT_SYMBOL_GPL(rtnl_delete_link);
3148 
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3149 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
3150 			struct netlink_ext_ack *extack)
3151 {
3152 	struct net *net = sock_net(skb->sk);
3153 	struct net *tgt_net = net;
3154 	struct net_device *dev = NULL;
3155 	struct ifinfomsg *ifm;
3156 	struct nlattr *tb[IFLA_MAX+1];
3157 	int err;
3158 	int netnsid = -1;
3159 
3160 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3161 				     ifla_policy, extack);
3162 	if (err < 0)
3163 		return err;
3164 
3165 	err = rtnl_ensure_unique_netns(tb, extack, true);
3166 	if (err < 0)
3167 		return err;
3168 
3169 	if (tb[IFLA_TARGET_NETNSID]) {
3170 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3171 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3172 		if (IS_ERR(tgt_net))
3173 			return PTR_ERR(tgt_net);
3174 	}
3175 
3176 	err = -EINVAL;
3177 	ifm = nlmsg_data(nlh);
3178 	if (ifm->ifi_index > 0)
3179 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3180 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3181 		dev = rtnl_dev_get(net, tb);
3182 	else if (tb[IFLA_GROUP])
3183 		err = rtnl_group_dellink(tgt_net, nla_get_u32(tb[IFLA_GROUP]));
3184 	else
3185 		goto out;
3186 
3187 	if (!dev) {
3188 		if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME] || ifm->ifi_index > 0)
3189 			err = -ENODEV;
3190 
3191 		goto out;
3192 	}
3193 
3194 	err = rtnl_delete_link(dev);
3195 
3196 out:
3197 	if (netnsid >= 0)
3198 		put_net(tgt_net);
3199 
3200 	return err;
3201 }
3202 
rtnl_configure_link(struct net_device * dev,const struct ifinfomsg * ifm)3203 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
3204 {
3205 	unsigned int old_flags;
3206 	int err;
3207 
3208 	old_flags = dev->flags;
3209 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
3210 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm),
3211 					 NULL);
3212 		if (err < 0)
3213 			return err;
3214 	}
3215 
3216 	if (dev->rtnl_link_state == RTNL_LINK_INITIALIZED) {
3217 		__dev_notify_flags(dev, old_flags, (old_flags ^ dev->flags));
3218 	} else {
3219 		dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
3220 		__dev_notify_flags(dev, old_flags, ~0U);
3221 	}
3222 	return 0;
3223 }
3224 EXPORT_SYMBOL(rtnl_configure_link);
3225 
rtnl_create_link(struct net * net,const char * ifname,unsigned char name_assign_type,const struct rtnl_link_ops * ops,struct nlattr * tb[],struct netlink_ext_ack * extack)3226 struct net_device *rtnl_create_link(struct net *net, const char *ifname,
3227 				    unsigned char name_assign_type,
3228 				    const struct rtnl_link_ops *ops,
3229 				    struct nlattr *tb[],
3230 				    struct netlink_ext_ack *extack)
3231 {
3232 	struct net_device *dev;
3233 	unsigned int num_tx_queues = 1;
3234 	unsigned int num_rx_queues = 1;
3235 	int err;
3236 
3237 	if (tb[IFLA_NUM_TX_QUEUES])
3238 		num_tx_queues = nla_get_u32(tb[IFLA_NUM_TX_QUEUES]);
3239 	else if (ops->get_num_tx_queues)
3240 		num_tx_queues = ops->get_num_tx_queues();
3241 
3242 	if (tb[IFLA_NUM_RX_QUEUES])
3243 		num_rx_queues = nla_get_u32(tb[IFLA_NUM_RX_QUEUES]);
3244 	else if (ops->get_num_rx_queues)
3245 		num_rx_queues = ops->get_num_rx_queues();
3246 
3247 	if (num_tx_queues < 1 || num_tx_queues > 4096) {
3248 		NL_SET_ERR_MSG(extack, "Invalid number of transmit queues");
3249 		return ERR_PTR(-EINVAL);
3250 	}
3251 
3252 	if (num_rx_queues < 1 || num_rx_queues > 4096) {
3253 		NL_SET_ERR_MSG(extack, "Invalid number of receive queues");
3254 		return ERR_PTR(-EINVAL);
3255 	}
3256 
3257 	if (ops->alloc) {
3258 		dev = ops->alloc(tb, ifname, name_assign_type,
3259 				 num_tx_queues, num_rx_queues);
3260 		if (IS_ERR(dev))
3261 			return dev;
3262 	} else {
3263 		dev = alloc_netdev_mqs(ops->priv_size, ifname,
3264 				       name_assign_type, ops->setup,
3265 				       num_tx_queues, num_rx_queues);
3266 	}
3267 
3268 	if (!dev)
3269 		return ERR_PTR(-ENOMEM);
3270 
3271 	err = validate_linkmsg(dev, tb, extack);
3272 	if (err < 0) {
3273 		free_netdev(dev);
3274 		return ERR_PTR(err);
3275 	}
3276 
3277 	dev_net_set(dev, net);
3278 	dev->rtnl_link_ops = ops;
3279 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
3280 
3281 	if (tb[IFLA_MTU]) {
3282 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3283 
3284 		err = dev_validate_mtu(dev, mtu, extack);
3285 		if (err) {
3286 			free_netdev(dev);
3287 			return ERR_PTR(err);
3288 		}
3289 		dev->mtu = mtu;
3290 	}
3291 	if (tb[IFLA_ADDRESS]) {
3292 		__dev_addr_set(dev, nla_data(tb[IFLA_ADDRESS]),
3293 			       nla_len(tb[IFLA_ADDRESS]));
3294 		dev->addr_assign_type = NET_ADDR_SET;
3295 	}
3296 	if (tb[IFLA_BROADCAST])
3297 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
3298 				nla_len(tb[IFLA_BROADCAST]));
3299 	if (tb[IFLA_TXQLEN])
3300 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
3301 	if (tb[IFLA_OPERSTATE])
3302 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
3303 	if (tb[IFLA_LINKMODE])
3304 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
3305 	if (tb[IFLA_GROUP])
3306 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
3307 	if (tb[IFLA_GSO_MAX_SIZE])
3308 		netif_set_gso_max_size(dev, nla_get_u32(tb[IFLA_GSO_MAX_SIZE]));
3309 	if (tb[IFLA_GSO_MAX_SEGS])
3310 		netif_set_gso_max_segs(dev, nla_get_u32(tb[IFLA_GSO_MAX_SEGS]));
3311 	if (tb[IFLA_GRO_MAX_SIZE])
3312 		netif_set_gro_max_size(dev, nla_get_u32(tb[IFLA_GRO_MAX_SIZE]));
3313 
3314 	return dev;
3315 }
3316 EXPORT_SYMBOL(rtnl_create_link);
3317 
rtnl_group_changelink(const struct sk_buff * skb,struct net * net,int group,struct ifinfomsg * ifm,struct netlink_ext_ack * extack,struct nlattr ** tb)3318 static int rtnl_group_changelink(const struct sk_buff *skb,
3319 		struct net *net, int group,
3320 		struct ifinfomsg *ifm,
3321 		struct netlink_ext_ack *extack,
3322 		struct nlattr **tb)
3323 {
3324 	struct net_device *dev, *aux;
3325 	int err;
3326 
3327 	for_each_netdev_safe(net, dev, aux) {
3328 		if (dev->group == group) {
3329 			err = do_setlink(skb, dev, ifm, extack, tb, 0);
3330 			if (err < 0)
3331 				return err;
3332 		}
3333 	}
3334 
3335 	return 0;
3336 }
3337 
rtnl_newlink_create(struct sk_buff * skb,struct ifinfomsg * ifm,const struct rtnl_link_ops * ops,struct nlattr ** tb,struct nlattr ** data,struct netlink_ext_ack * extack)3338 static int rtnl_newlink_create(struct sk_buff *skb, struct ifinfomsg *ifm,
3339 			       const struct rtnl_link_ops *ops,
3340 			       struct nlattr **tb, struct nlattr **data,
3341 			       struct netlink_ext_ack *extack)
3342 {
3343 	unsigned char name_assign_type = NET_NAME_USER;
3344 	struct net *net = sock_net(skb->sk);
3345 	struct net *dest_net, *link_net;
3346 	struct net_device *dev;
3347 	char ifname[IFNAMSIZ];
3348 	int err;
3349 
3350 	if (!ops->alloc && !ops->setup)
3351 		return -EOPNOTSUPP;
3352 
3353 	if (tb[IFLA_IFNAME]) {
3354 		nla_strscpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
3355 	} else {
3356 		snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
3357 		name_assign_type = NET_NAME_ENUM;
3358 	}
3359 
3360 	dest_net = rtnl_link_get_net_capable(skb, net, tb, CAP_NET_ADMIN);
3361 	if (IS_ERR(dest_net))
3362 		return PTR_ERR(dest_net);
3363 
3364 	if (tb[IFLA_LINK_NETNSID]) {
3365 		int id = nla_get_s32(tb[IFLA_LINK_NETNSID]);
3366 
3367 		link_net = get_net_ns_by_id(dest_net, id);
3368 		if (!link_net) {
3369 			NL_SET_ERR_MSG(extack, "Unknown network namespace id");
3370 			err =  -EINVAL;
3371 			goto out;
3372 		}
3373 		err = -EPERM;
3374 		if (!netlink_ns_capable(skb, link_net->user_ns, CAP_NET_ADMIN))
3375 			goto out;
3376 	} else {
3377 		link_net = NULL;
3378 	}
3379 
3380 	dev = rtnl_create_link(link_net ? : dest_net, ifname,
3381 			       name_assign_type, ops, tb, extack);
3382 	if (IS_ERR(dev)) {
3383 		err = PTR_ERR(dev);
3384 		goto out;
3385 	}
3386 
3387 	dev->ifindex = ifm->ifi_index;
3388 
3389 	if (ops->newlink)
3390 		err = ops->newlink(link_net ? : net, dev, tb, data, extack);
3391 	else
3392 		err = register_netdevice(dev);
3393 	if (err < 0) {
3394 		free_netdev(dev);
3395 		goto out;
3396 	}
3397 
3398 	err = rtnl_configure_link(dev, ifm);
3399 	if (err < 0)
3400 		goto out_unregister;
3401 	if (link_net) {
3402 		err = dev_change_net_namespace(dev, dest_net, ifname);
3403 		if (err < 0)
3404 			goto out_unregister;
3405 	}
3406 	if (tb[IFLA_MASTER]) {
3407 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]), extack);
3408 		if (err)
3409 			goto out_unregister;
3410 	}
3411 out:
3412 	if (link_net)
3413 		put_net(link_net);
3414 	put_net(dest_net);
3415 	return err;
3416 out_unregister:
3417 	if (ops->newlink) {
3418 		LIST_HEAD(list_kill);
3419 
3420 		ops->dellink(dev, &list_kill);
3421 		unregister_netdevice_many(&list_kill);
3422 	} else {
3423 		unregister_netdevice(dev);
3424 	}
3425 	goto out;
3426 }
3427 
3428 struct rtnl_newlink_tbs {
3429 	struct nlattr *tb[IFLA_MAX + 1];
3430 	struct nlattr *attr[RTNL_MAX_TYPE + 1];
3431 	struct nlattr *slave_attr[RTNL_SLAVE_MAX_TYPE + 1];
3432 };
3433 
__rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct rtnl_newlink_tbs * tbs,struct netlink_ext_ack * extack)3434 static int __rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3435 			  struct rtnl_newlink_tbs *tbs,
3436 			  struct netlink_ext_ack *extack)
3437 {
3438 	struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
3439 	struct nlattr ** const tb = tbs->tb;
3440 	const struct rtnl_link_ops *m_ops;
3441 	struct net_device *master_dev;
3442 	struct net *net = sock_net(skb->sk);
3443 	const struct rtnl_link_ops *ops;
3444 	struct nlattr **slave_data;
3445 	char kind[MODULE_NAME_LEN];
3446 	struct net_device *dev;
3447 	struct ifinfomsg *ifm;
3448 	struct nlattr **data;
3449 	bool link_specified;
3450 	int err;
3451 
3452 #ifdef CONFIG_MODULES
3453 replay:
3454 #endif
3455 	err = nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3456 				     ifla_policy, extack);
3457 	if (err < 0)
3458 		return err;
3459 
3460 	err = rtnl_ensure_unique_netns(tb, extack, false);
3461 	if (err < 0)
3462 		return err;
3463 
3464 	ifm = nlmsg_data(nlh);
3465 	if (ifm->ifi_index > 0) {
3466 		link_specified = true;
3467 		dev = __dev_get_by_index(net, ifm->ifi_index);
3468 	} else if (ifm->ifi_index < 0) {
3469 		NL_SET_ERR_MSG(extack, "ifindex can't be negative");
3470 		return -EINVAL;
3471 	} else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME]) {
3472 		link_specified = true;
3473 		dev = rtnl_dev_get(net, tb);
3474 	} else {
3475 		link_specified = false;
3476 		dev = NULL;
3477 	}
3478 
3479 	master_dev = NULL;
3480 	m_ops = NULL;
3481 	if (dev) {
3482 		master_dev = netdev_master_upper_dev_get(dev);
3483 		if (master_dev)
3484 			m_ops = master_dev->rtnl_link_ops;
3485 	}
3486 
3487 	err = validate_linkmsg(dev, tb, extack);
3488 	if (err < 0)
3489 		return err;
3490 
3491 	if (tb[IFLA_LINKINFO]) {
3492 		err = nla_parse_nested_deprecated(linkinfo, IFLA_INFO_MAX,
3493 						  tb[IFLA_LINKINFO],
3494 						  ifla_info_policy, NULL);
3495 		if (err < 0)
3496 			return err;
3497 	} else
3498 		memset(linkinfo, 0, sizeof(linkinfo));
3499 
3500 	if (linkinfo[IFLA_INFO_KIND]) {
3501 		nla_strscpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
3502 		ops = rtnl_link_ops_get(kind);
3503 	} else {
3504 		kind[0] = '\0';
3505 		ops = NULL;
3506 	}
3507 
3508 	data = NULL;
3509 	if (ops) {
3510 		if (ops->maxtype > RTNL_MAX_TYPE)
3511 			return -EINVAL;
3512 
3513 		if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
3514 			err = nla_parse_nested_deprecated(tbs->attr, ops->maxtype,
3515 							  linkinfo[IFLA_INFO_DATA],
3516 							  ops->policy, extack);
3517 			if (err < 0)
3518 				return err;
3519 			data = tbs->attr;
3520 		}
3521 		if (ops->validate) {
3522 			err = ops->validate(tb, data, extack);
3523 			if (err < 0)
3524 				return err;
3525 		}
3526 	}
3527 
3528 	slave_data = NULL;
3529 	if (m_ops) {
3530 		if (m_ops->slave_maxtype > RTNL_SLAVE_MAX_TYPE)
3531 			return -EINVAL;
3532 
3533 		if (m_ops->slave_maxtype &&
3534 		    linkinfo[IFLA_INFO_SLAVE_DATA]) {
3535 			err = nla_parse_nested_deprecated(tbs->slave_attr,
3536 							  m_ops->slave_maxtype,
3537 							  linkinfo[IFLA_INFO_SLAVE_DATA],
3538 							  m_ops->slave_policy,
3539 							  extack);
3540 			if (err < 0)
3541 				return err;
3542 			slave_data = tbs->slave_attr;
3543 		}
3544 	}
3545 
3546 	if (dev) {
3547 		int status = 0;
3548 
3549 		if (nlh->nlmsg_flags & NLM_F_EXCL)
3550 			return -EEXIST;
3551 		if (nlh->nlmsg_flags & NLM_F_REPLACE)
3552 			return -EOPNOTSUPP;
3553 
3554 		if (linkinfo[IFLA_INFO_DATA]) {
3555 			if (!ops || ops != dev->rtnl_link_ops ||
3556 			    !ops->changelink)
3557 				return -EOPNOTSUPP;
3558 
3559 			err = ops->changelink(dev, tb, data, extack);
3560 			if (err < 0)
3561 				return err;
3562 			status |= DO_SETLINK_NOTIFY;
3563 		}
3564 
3565 		if (linkinfo[IFLA_INFO_SLAVE_DATA]) {
3566 			if (!m_ops || !m_ops->slave_changelink)
3567 				return -EOPNOTSUPP;
3568 
3569 			err = m_ops->slave_changelink(master_dev, dev, tb,
3570 						      slave_data, extack);
3571 			if (err < 0)
3572 				return err;
3573 			status |= DO_SETLINK_NOTIFY;
3574 		}
3575 
3576 		return do_setlink(skb, dev, ifm, extack, tb, status);
3577 	}
3578 
3579 	if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
3580 		/* No dev found and NLM_F_CREATE not set. Requested dev does not exist,
3581 		 * or it's for a group
3582 		*/
3583 		if (link_specified)
3584 			return -ENODEV;
3585 		if (tb[IFLA_GROUP])
3586 			return rtnl_group_changelink(skb, net,
3587 						nla_get_u32(tb[IFLA_GROUP]),
3588 						ifm, extack, tb);
3589 		return -ENODEV;
3590 	}
3591 
3592 	if (tb[IFLA_MAP] || tb[IFLA_PROTINFO])
3593 		return -EOPNOTSUPP;
3594 
3595 	if (!ops) {
3596 #ifdef CONFIG_MODULES
3597 		if (kind[0]) {
3598 			__rtnl_unlock();
3599 			request_module("rtnl-link-%s", kind);
3600 			rtnl_lock();
3601 			ops = rtnl_link_ops_get(kind);
3602 			if (ops)
3603 				goto replay;
3604 		}
3605 #endif
3606 		NL_SET_ERR_MSG(extack, "Unknown device type");
3607 		return -EOPNOTSUPP;
3608 	}
3609 
3610 	return rtnl_newlink_create(skb, ifm, ops, tb, data, extack);
3611 }
3612 
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3613 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3614 			struct netlink_ext_ack *extack)
3615 {
3616 	struct rtnl_newlink_tbs *tbs;
3617 	int ret;
3618 
3619 	tbs = kmalloc(sizeof(*tbs), GFP_KERNEL);
3620 	if (!tbs)
3621 		return -ENOMEM;
3622 
3623 	ret = __rtnl_newlink(skb, nlh, tbs, extack);
3624 	kfree(tbs);
3625 	return ret;
3626 }
3627 
rtnl_valid_getlink_req(struct sk_buff * skb,const struct nlmsghdr * nlh,struct nlattr ** tb,struct netlink_ext_ack * extack)3628 static int rtnl_valid_getlink_req(struct sk_buff *skb,
3629 				  const struct nlmsghdr *nlh,
3630 				  struct nlattr **tb,
3631 				  struct netlink_ext_ack *extack)
3632 {
3633 	struct ifinfomsg *ifm;
3634 	int i, err;
3635 
3636 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
3637 		NL_SET_ERR_MSG(extack, "Invalid header for get link");
3638 		return -EINVAL;
3639 	}
3640 
3641 	if (!netlink_strict_get_check(skb))
3642 		return nlmsg_parse_deprecated(nlh, sizeof(*ifm), tb, IFLA_MAX,
3643 					      ifla_policy, extack);
3644 
3645 	ifm = nlmsg_data(nlh);
3646 	if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
3647 	    ifm->ifi_change) {
3648 		NL_SET_ERR_MSG(extack, "Invalid values in header for get link request");
3649 		return -EINVAL;
3650 	}
3651 
3652 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(*ifm), tb, IFLA_MAX,
3653 					    ifla_policy, extack);
3654 	if (err)
3655 		return err;
3656 
3657 	for (i = 0; i <= IFLA_MAX; i++) {
3658 		if (!tb[i])
3659 			continue;
3660 
3661 		switch (i) {
3662 		case IFLA_IFNAME:
3663 		case IFLA_ALT_IFNAME:
3664 		case IFLA_EXT_MASK:
3665 		case IFLA_TARGET_NETNSID:
3666 			break;
3667 		default:
3668 			NL_SET_ERR_MSG(extack, "Unsupported attribute in get link request");
3669 			return -EINVAL;
3670 		}
3671 	}
3672 
3673 	return 0;
3674 }
3675 
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3676 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr *nlh,
3677 			struct netlink_ext_ack *extack)
3678 {
3679 	struct net *net = sock_net(skb->sk);
3680 	struct net *tgt_net = net;
3681 	struct ifinfomsg *ifm;
3682 	struct nlattr *tb[IFLA_MAX+1];
3683 	struct net_device *dev = NULL;
3684 	struct sk_buff *nskb;
3685 	int netnsid = -1;
3686 	int err;
3687 	u32 ext_filter_mask = 0;
3688 
3689 	err = rtnl_valid_getlink_req(skb, nlh, tb, extack);
3690 	if (err < 0)
3691 		return err;
3692 
3693 	err = rtnl_ensure_unique_netns(tb, extack, true);
3694 	if (err < 0)
3695 		return err;
3696 
3697 	if (tb[IFLA_TARGET_NETNSID]) {
3698 		netnsid = nla_get_s32(tb[IFLA_TARGET_NETNSID]);
3699 		tgt_net = rtnl_get_net_ns_capable(NETLINK_CB(skb).sk, netnsid);
3700 		if (IS_ERR(tgt_net))
3701 			return PTR_ERR(tgt_net);
3702 	}
3703 
3704 	if (tb[IFLA_EXT_MASK])
3705 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3706 
3707 	err = -EINVAL;
3708 	ifm = nlmsg_data(nlh);
3709 	if (ifm->ifi_index > 0)
3710 		dev = __dev_get_by_index(tgt_net, ifm->ifi_index);
3711 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3712 		dev = rtnl_dev_get(tgt_net, tb);
3713 	else
3714 		goto out;
3715 
3716 	err = -ENODEV;
3717 	if (dev == NULL)
3718 		goto out;
3719 
3720 	err = -ENOBUFS;
3721 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
3722 	if (nskb == NULL)
3723 		goto out;
3724 
3725 	err = rtnl_fill_ifinfo(nskb, dev, net,
3726 			       RTM_NEWLINK, NETLINK_CB(skb).portid,
3727 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask,
3728 			       0, NULL, 0, netnsid, GFP_KERNEL);
3729 	if (err < 0) {
3730 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
3731 		WARN_ON(err == -EMSGSIZE);
3732 		kfree_skb(nskb);
3733 	} else
3734 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
3735 out:
3736 	if (netnsid >= 0)
3737 		put_net(tgt_net);
3738 
3739 	return err;
3740 }
3741 
rtnl_alt_ifname(int cmd,struct net_device * dev,struct nlattr * attr,bool * changed,struct netlink_ext_ack * extack)3742 static int rtnl_alt_ifname(int cmd, struct net_device *dev, struct nlattr *attr,
3743 			   bool *changed, struct netlink_ext_ack *extack)
3744 {
3745 	char *alt_ifname;
3746 	size_t size;
3747 	int err;
3748 
3749 	err = nla_validate(attr, attr->nla_len, IFLA_MAX, ifla_policy, extack);
3750 	if (err)
3751 		return err;
3752 
3753 	if (cmd == RTM_NEWLINKPROP) {
3754 		size = rtnl_prop_list_size(dev);
3755 		size += nla_total_size(ALTIFNAMSIZ);
3756 		if (size >= U16_MAX) {
3757 			NL_SET_ERR_MSG(extack,
3758 				       "effective property list too long");
3759 			return -EINVAL;
3760 		}
3761 	}
3762 
3763 	alt_ifname = nla_strdup(attr, GFP_KERNEL_ACCOUNT);
3764 	if (!alt_ifname)
3765 		return -ENOMEM;
3766 
3767 	if (cmd == RTM_NEWLINKPROP) {
3768 		err = netdev_name_node_alt_create(dev, alt_ifname);
3769 		if (!err)
3770 			alt_ifname = NULL;
3771 	} else if (cmd == RTM_DELLINKPROP) {
3772 		err = netdev_name_node_alt_destroy(dev, alt_ifname);
3773 	} else {
3774 		WARN_ON_ONCE(1);
3775 		err = -EINVAL;
3776 	}
3777 
3778 	kfree(alt_ifname);
3779 	if (!err)
3780 		*changed = true;
3781 	return err;
3782 }
3783 
rtnl_linkprop(int cmd,struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3784 static int rtnl_linkprop(int cmd, struct sk_buff *skb, struct nlmsghdr *nlh,
3785 			 struct netlink_ext_ack *extack)
3786 {
3787 	struct net *net = sock_net(skb->sk);
3788 	struct nlattr *tb[IFLA_MAX + 1];
3789 	struct net_device *dev;
3790 	struct ifinfomsg *ifm;
3791 	bool changed = false;
3792 	struct nlattr *attr;
3793 	int err, rem;
3794 
3795 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy, extack);
3796 	if (err)
3797 		return err;
3798 
3799 	err = rtnl_ensure_unique_netns(tb, extack, true);
3800 	if (err)
3801 		return err;
3802 
3803 	ifm = nlmsg_data(nlh);
3804 	if (ifm->ifi_index > 0)
3805 		dev = __dev_get_by_index(net, ifm->ifi_index);
3806 	else if (tb[IFLA_IFNAME] || tb[IFLA_ALT_IFNAME])
3807 		dev = rtnl_dev_get(net, tb);
3808 	else
3809 		return -EINVAL;
3810 
3811 	if (!dev)
3812 		return -ENODEV;
3813 
3814 	if (!tb[IFLA_PROP_LIST])
3815 		return 0;
3816 
3817 	nla_for_each_nested(attr, tb[IFLA_PROP_LIST], rem) {
3818 		switch (nla_type(attr)) {
3819 		case IFLA_ALT_IFNAME:
3820 			err = rtnl_alt_ifname(cmd, dev, attr, &changed, extack);
3821 			if (err)
3822 				return err;
3823 			break;
3824 		}
3825 	}
3826 
3827 	if (changed)
3828 		netdev_state_change(dev);
3829 	return 0;
3830 }
3831 
rtnl_newlinkprop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3832 static int rtnl_newlinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3833 			    struct netlink_ext_ack *extack)
3834 {
3835 	return rtnl_linkprop(RTM_NEWLINKPROP, skb, nlh, extack);
3836 }
3837 
rtnl_dellinkprop(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)3838 static int rtnl_dellinkprop(struct sk_buff *skb, struct nlmsghdr *nlh,
3839 			    struct netlink_ext_ack *extack)
3840 {
3841 	return rtnl_linkprop(RTM_DELLINKPROP, skb, nlh, extack);
3842 }
3843 
rtnl_calcit(struct sk_buff * skb,struct nlmsghdr * nlh)3844 static u32 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
3845 {
3846 	struct net *net = sock_net(skb->sk);
3847 	size_t min_ifinfo_dump_size = 0;
3848 	struct nlattr *tb[IFLA_MAX+1];
3849 	u32 ext_filter_mask = 0;
3850 	struct net_device *dev;
3851 	int hdrlen;
3852 
3853 	/* Same kernel<->userspace interface hack as in rtnl_dump_ifinfo. */
3854 	hdrlen = nlmsg_len(nlh) < sizeof(struct ifinfomsg) ?
3855 		 sizeof(struct rtgenmsg) : sizeof(struct ifinfomsg);
3856 
3857 	if (nlmsg_parse_deprecated(nlh, hdrlen, tb, IFLA_MAX, ifla_policy, NULL) >= 0) {
3858 		if (tb[IFLA_EXT_MASK])
3859 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
3860 	}
3861 
3862 	if (!ext_filter_mask)
3863 		return NLMSG_GOODSIZE;
3864 	/*
3865 	 * traverse the list of net devices and compute the minimum
3866 	 * buffer size based upon the filter mask.
3867 	 */
3868 	rcu_read_lock();
3869 	for_each_netdev_rcu(net, dev) {
3870 		min_ifinfo_dump_size = max(min_ifinfo_dump_size,
3871 					   if_nlmsg_size(dev, ext_filter_mask));
3872 	}
3873 	rcu_read_unlock();
3874 
3875 	return nlmsg_total_size(min_ifinfo_dump_size);
3876 }
3877 
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)3878 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
3879 {
3880 	int idx;
3881 	int s_idx = cb->family;
3882 	int type = cb->nlh->nlmsg_type - RTM_BASE;
3883 	int ret = 0;
3884 
3885 	if (s_idx == 0)
3886 		s_idx = 1;
3887 
3888 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
3889 		struct rtnl_link __rcu **tab;
3890 		struct rtnl_link *link;
3891 		rtnl_dumpit_func dumpit;
3892 
3893 		if (idx < s_idx || idx == PF_PACKET)
3894 			continue;
3895 
3896 		if (type < 0 || type >= RTM_NR_MSGTYPES)
3897 			continue;
3898 
3899 		tab = rcu_dereference_rtnl(rtnl_msg_handlers[idx]);
3900 		if (!tab)
3901 			continue;
3902 
3903 		link = rcu_dereference_rtnl(tab[type]);
3904 		if (!link)
3905 			continue;
3906 
3907 		dumpit = link->dumpit;
3908 		if (!dumpit)
3909 			continue;
3910 
3911 		if (idx > s_idx) {
3912 			memset(&cb->args[0], 0, sizeof(cb->args));
3913 			cb->prev_seq = 0;
3914 			cb->seq = 0;
3915 		}
3916 		ret = dumpit(skb, cb);
3917 		if (ret)
3918 			break;
3919 	}
3920 	cb->family = idx;
3921 
3922 	return skb->len ? : ret;
3923 }
3924 
rtmsg_ifinfo_build_skb(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex)3925 struct sk_buff *rtmsg_ifinfo_build_skb(int type, struct net_device *dev,
3926 				       unsigned int change,
3927 				       u32 event, gfp_t flags, int *new_nsid,
3928 				       int new_ifindex)
3929 {
3930 	struct net *net = dev_net(dev);
3931 	struct sk_buff *skb;
3932 	int err = -ENOBUFS;
3933 
3934 	skb = nlmsg_new(if_nlmsg_size(dev, 0), flags);
3935 	if (skb == NULL)
3936 		goto errout;
3937 
3938 	err = rtnl_fill_ifinfo(skb, dev, dev_net(dev),
3939 			       type, 0, 0, change, 0, 0, event,
3940 			       new_nsid, new_ifindex, -1, flags);
3941 	if (err < 0) {
3942 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
3943 		WARN_ON(err == -EMSGSIZE);
3944 		kfree_skb(skb);
3945 		goto errout;
3946 	}
3947 	return skb;
3948 errout:
3949 	if (err < 0)
3950 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
3951 	return NULL;
3952 }
3953 
rtmsg_ifinfo_send(struct sk_buff * skb,struct net_device * dev,gfp_t flags)3954 void rtmsg_ifinfo_send(struct sk_buff *skb, struct net_device *dev, gfp_t flags)
3955 {
3956 	struct net *net = dev_net(dev);
3957 
3958 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, flags);
3959 }
3960 
rtmsg_ifinfo_event(int type,struct net_device * dev,unsigned int change,u32 event,gfp_t flags,int * new_nsid,int new_ifindex)3961 static void rtmsg_ifinfo_event(int type, struct net_device *dev,
3962 			       unsigned int change, u32 event,
3963 			       gfp_t flags, int *new_nsid, int new_ifindex)
3964 {
3965 	struct sk_buff *skb;
3966 
3967 	if (dev->reg_state != NETREG_REGISTERED)
3968 		return;
3969 
3970 	skb = rtmsg_ifinfo_build_skb(type, dev, change, event, flags, new_nsid,
3971 				     new_ifindex);
3972 	if (skb)
3973 		rtmsg_ifinfo_send(skb, dev, flags);
3974 }
3975 
rtmsg_ifinfo(int type,struct net_device * dev,unsigned int change,gfp_t flags)3976 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned int change,
3977 		  gfp_t flags)
3978 {
3979 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3980 			   NULL, 0);
3981 }
3982 
rtmsg_ifinfo_newnet(int type,struct net_device * dev,unsigned int change,gfp_t flags,int * new_nsid,int new_ifindex)3983 void rtmsg_ifinfo_newnet(int type, struct net_device *dev, unsigned int change,
3984 			 gfp_t flags, int *new_nsid, int new_ifindex)
3985 {
3986 	rtmsg_ifinfo_event(type, dev, change, rtnl_get_event(0), flags,
3987 			   new_nsid, new_ifindex);
3988 }
3989 
nlmsg_populate_fdb_fill(struct sk_buff * skb,struct net_device * dev,u8 * addr,u16 vid,u32 pid,u32 seq,int type,unsigned int flags,int nlflags,u16 ndm_state)3990 static int nlmsg_populate_fdb_fill(struct sk_buff *skb,
3991 				   struct net_device *dev,
3992 				   u8 *addr, u16 vid, u32 pid, u32 seq,
3993 				   int type, unsigned int flags,
3994 				   int nlflags, u16 ndm_state)
3995 {
3996 	struct nlmsghdr *nlh;
3997 	struct ndmsg *ndm;
3998 
3999 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ndm), nlflags);
4000 	if (!nlh)
4001 		return -EMSGSIZE;
4002 
4003 	ndm = nlmsg_data(nlh);
4004 	ndm->ndm_family  = AF_BRIDGE;
4005 	ndm->ndm_pad1	 = 0;
4006 	ndm->ndm_pad2    = 0;
4007 	ndm->ndm_flags	 = flags;
4008 	ndm->ndm_type	 = 0;
4009 	ndm->ndm_ifindex = dev->ifindex;
4010 	ndm->ndm_state   = ndm_state;
4011 
4012 	if (nla_put(skb, NDA_LLADDR, dev->addr_len, addr))
4013 		goto nla_put_failure;
4014 	if (vid)
4015 		if (nla_put(skb, NDA_VLAN, sizeof(u16), &vid))
4016 			goto nla_put_failure;
4017 
4018 	nlmsg_end(skb, nlh);
4019 	return 0;
4020 
4021 nla_put_failure:
4022 	nlmsg_cancel(skb, nlh);
4023 	return -EMSGSIZE;
4024 }
4025 
rtnl_fdb_nlmsg_size(const struct net_device * dev)4026 static inline size_t rtnl_fdb_nlmsg_size(const struct net_device *dev)
4027 {
4028 	return NLMSG_ALIGN(sizeof(struct ndmsg)) +
4029 	       nla_total_size(dev->addr_len) +	/* NDA_LLADDR */
4030 	       nla_total_size(sizeof(u16)) +	/* NDA_VLAN */
4031 	       0;
4032 }
4033 
rtnl_fdb_notify(struct net_device * dev,u8 * addr,u16 vid,int type,u16 ndm_state)4034 static void rtnl_fdb_notify(struct net_device *dev, u8 *addr, u16 vid, int type,
4035 			    u16 ndm_state)
4036 {
4037 	struct net *net = dev_net(dev);
4038 	struct sk_buff *skb;
4039 	int err = -ENOBUFS;
4040 
4041 	skb = nlmsg_new(rtnl_fdb_nlmsg_size(dev), GFP_ATOMIC);
4042 	if (!skb)
4043 		goto errout;
4044 
4045 	err = nlmsg_populate_fdb_fill(skb, dev, addr, vid,
4046 				      0, 0, type, NTF_SELF, 0, ndm_state);
4047 	if (err < 0) {
4048 		kfree_skb(skb);
4049 		goto errout;
4050 	}
4051 
4052 	rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
4053 	return;
4054 errout:
4055 	rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
4056 }
4057 
4058 /*
4059  * ndo_dflt_fdb_add - default netdevice operation to add an FDB entry
4060  */
ndo_dflt_fdb_add(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid,u16 flags)4061 int ndo_dflt_fdb_add(struct ndmsg *ndm,
4062 		     struct nlattr *tb[],
4063 		     struct net_device *dev,
4064 		     const unsigned char *addr, u16 vid,
4065 		     u16 flags)
4066 {
4067 	int err = -EINVAL;
4068 
4069 	/* If aging addresses are supported device will need to
4070 	 * implement its own handler for this.
4071 	 */
4072 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
4073 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4074 		return err;
4075 	}
4076 
4077 	if (vid) {
4078 		netdev_info(dev, "vlans aren't supported yet for dev_uc|mc_add()\n");
4079 		return err;
4080 	}
4081 
4082 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4083 		err = dev_uc_add_excl(dev, addr);
4084 	else if (is_multicast_ether_addr(addr))
4085 		err = dev_mc_add_excl(dev, addr);
4086 
4087 	/* Only return duplicate errors if NLM_F_EXCL is set */
4088 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
4089 		err = 0;
4090 
4091 	return err;
4092 }
4093 EXPORT_SYMBOL(ndo_dflt_fdb_add);
4094 
fdb_vid_parse(struct nlattr * vlan_attr,u16 * p_vid,struct netlink_ext_ack * extack)4095 static int fdb_vid_parse(struct nlattr *vlan_attr, u16 *p_vid,
4096 			 struct netlink_ext_ack *extack)
4097 {
4098 	u16 vid = 0;
4099 
4100 	if (vlan_attr) {
4101 		if (nla_len(vlan_attr) != sizeof(u16)) {
4102 			NL_SET_ERR_MSG(extack, "invalid vlan attribute size");
4103 			return -EINVAL;
4104 		}
4105 
4106 		vid = nla_get_u16(vlan_attr);
4107 
4108 		if (!vid || vid >= VLAN_VID_MASK) {
4109 			NL_SET_ERR_MSG(extack, "invalid vlan id");
4110 			return -EINVAL;
4111 		}
4112 	}
4113 	*p_vid = vid;
4114 	return 0;
4115 }
4116 
rtnl_fdb_add(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4117 static int rtnl_fdb_add(struct sk_buff *skb, struct nlmsghdr *nlh,
4118 			struct netlink_ext_ack *extack)
4119 {
4120 	struct net *net = sock_net(skb->sk);
4121 	struct ndmsg *ndm;
4122 	struct nlattr *tb[NDA_MAX+1];
4123 	struct net_device *dev;
4124 	u8 *addr;
4125 	u16 vid;
4126 	int err;
4127 
4128 	err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX, NULL,
4129 				     extack);
4130 	if (err < 0)
4131 		return err;
4132 
4133 	ndm = nlmsg_data(nlh);
4134 	if (ndm->ndm_ifindex == 0) {
4135 		NL_SET_ERR_MSG(extack, "invalid ifindex");
4136 		return -EINVAL;
4137 	}
4138 
4139 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4140 	if (dev == NULL) {
4141 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4142 		return -ENODEV;
4143 	}
4144 
4145 	if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4146 		NL_SET_ERR_MSG(extack, "invalid address");
4147 		return -EINVAL;
4148 	}
4149 
4150 	if (dev->type != ARPHRD_ETHER) {
4151 		NL_SET_ERR_MSG(extack, "FDB add only supported for Ethernet devices");
4152 		return -EINVAL;
4153 	}
4154 
4155 	addr = nla_data(tb[NDA_LLADDR]);
4156 
4157 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4158 	if (err)
4159 		return err;
4160 
4161 	err = -EOPNOTSUPP;
4162 
4163 	/* Support fdb on master device the net/bridge default case */
4164 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4165 	    netif_is_bridge_port(dev)) {
4166 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4167 		const struct net_device_ops *ops = br_dev->netdev_ops;
4168 
4169 		err = ops->ndo_fdb_add(ndm, tb, dev, addr, vid,
4170 				       nlh->nlmsg_flags, extack);
4171 		if (err)
4172 			goto out;
4173 		else
4174 			ndm->ndm_flags &= ~NTF_MASTER;
4175 	}
4176 
4177 	/* Embedded bridge, macvlan, and any other device support */
4178 	if ((ndm->ndm_flags & NTF_SELF)) {
4179 		if (dev->netdev_ops->ndo_fdb_add)
4180 			err = dev->netdev_ops->ndo_fdb_add(ndm, tb, dev, addr,
4181 							   vid,
4182 							   nlh->nlmsg_flags,
4183 							   extack);
4184 		else
4185 			err = ndo_dflt_fdb_add(ndm, tb, dev, addr, vid,
4186 					       nlh->nlmsg_flags);
4187 
4188 		if (!err) {
4189 			rtnl_fdb_notify(dev, addr, vid, RTM_NEWNEIGH,
4190 					ndm->ndm_state);
4191 			ndm->ndm_flags &= ~NTF_SELF;
4192 		}
4193 	}
4194 out:
4195 	return err;
4196 }
4197 
4198 /*
4199  * ndo_dflt_fdb_del - default netdevice operation to delete an FDB entry
4200  */
ndo_dflt_fdb_del(struct ndmsg * ndm,struct nlattr * tb[],struct net_device * dev,const unsigned char * addr,u16 vid)4201 int ndo_dflt_fdb_del(struct ndmsg *ndm,
4202 		     struct nlattr *tb[],
4203 		     struct net_device *dev,
4204 		     const unsigned char *addr, u16 vid)
4205 {
4206 	int err = -EINVAL;
4207 
4208 	/* If aging addresses are supported device will need to
4209 	 * implement its own handler for this.
4210 	 */
4211 	if (!(ndm->ndm_state & NUD_PERMANENT)) {
4212 		netdev_info(dev, "default FDB implementation only supports local addresses\n");
4213 		return err;
4214 	}
4215 
4216 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
4217 		err = dev_uc_del(dev, addr);
4218 	else if (is_multicast_ether_addr(addr))
4219 		err = dev_mc_del(dev, addr);
4220 
4221 	return err;
4222 }
4223 EXPORT_SYMBOL(ndo_dflt_fdb_del);
4224 
4225 static const struct nla_policy fdb_del_bulk_policy[NDA_MAX + 1] = {
4226 	[NDA_VLAN]	= { .type = NLA_U16 },
4227 	[NDA_IFINDEX]	= NLA_POLICY_MIN(NLA_S32, 1),
4228 	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16  },
4229 	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
4230 };
4231 
rtnl_fdb_del(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4232 static int rtnl_fdb_del(struct sk_buff *skb, struct nlmsghdr *nlh,
4233 			struct netlink_ext_ack *extack)
4234 {
4235 	bool del_bulk = !!(nlh->nlmsg_flags & NLM_F_BULK);
4236 	struct net *net = sock_net(skb->sk);
4237 	const struct net_device_ops *ops;
4238 	struct ndmsg *ndm;
4239 	struct nlattr *tb[NDA_MAX+1];
4240 	struct net_device *dev;
4241 	__u8 *addr = NULL;
4242 	int err;
4243 	u16 vid;
4244 
4245 	if (!netlink_capable(skb, CAP_NET_ADMIN))
4246 		return -EPERM;
4247 
4248 	if (!del_bulk) {
4249 		err = nlmsg_parse_deprecated(nlh, sizeof(*ndm), tb, NDA_MAX,
4250 					     NULL, extack);
4251 	} else {
4252 		err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX,
4253 				  fdb_del_bulk_policy, extack);
4254 	}
4255 	if (err < 0)
4256 		return err;
4257 
4258 	ndm = nlmsg_data(nlh);
4259 	if (ndm->ndm_ifindex == 0) {
4260 		NL_SET_ERR_MSG(extack, "invalid ifindex");
4261 		return -EINVAL;
4262 	}
4263 
4264 	dev = __dev_get_by_index(net, ndm->ndm_ifindex);
4265 	if (dev == NULL) {
4266 		NL_SET_ERR_MSG(extack, "unknown ifindex");
4267 		return -ENODEV;
4268 	}
4269 
4270 	if (!del_bulk) {
4271 		if (!tb[NDA_LLADDR] || nla_len(tb[NDA_LLADDR]) != ETH_ALEN) {
4272 			NL_SET_ERR_MSG(extack, "invalid address");
4273 			return -EINVAL;
4274 		}
4275 		addr = nla_data(tb[NDA_LLADDR]);
4276 	}
4277 
4278 	if (dev->type != ARPHRD_ETHER) {
4279 		NL_SET_ERR_MSG(extack, "FDB delete only supported for Ethernet devices");
4280 		return -EINVAL;
4281 	}
4282 
4283 	err = fdb_vid_parse(tb[NDA_VLAN], &vid, extack);
4284 	if (err)
4285 		return err;
4286 
4287 	err = -EOPNOTSUPP;
4288 
4289 	/* Support fdb on master device the net/bridge default case */
4290 	if ((!ndm->ndm_flags || ndm->ndm_flags & NTF_MASTER) &&
4291 	    netif_is_bridge_port(dev)) {
4292 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4293 
4294 		ops = br_dev->netdev_ops;
4295 		if (!del_bulk) {
4296 			if (ops->ndo_fdb_del)
4297 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4298 		} else {
4299 			if (ops->ndo_fdb_del_bulk)
4300 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4301 							    extack);
4302 		}
4303 
4304 		if (err)
4305 			goto out;
4306 		else
4307 			ndm->ndm_flags &= ~NTF_MASTER;
4308 	}
4309 
4310 	/* Embedded bridge, macvlan, and any other device support */
4311 	if (ndm->ndm_flags & NTF_SELF) {
4312 		ops = dev->netdev_ops;
4313 		if (!del_bulk) {
4314 			if (ops->ndo_fdb_del)
4315 				err = ops->ndo_fdb_del(ndm, tb, dev, addr, vid, extack);
4316 			else
4317 				err = ndo_dflt_fdb_del(ndm, tb, dev, addr, vid);
4318 		} else {
4319 			/* in case err was cleared by NTF_MASTER call */
4320 			err = -EOPNOTSUPP;
4321 			if (ops->ndo_fdb_del_bulk)
4322 				err = ops->ndo_fdb_del_bulk(ndm, tb, dev, vid,
4323 							    extack);
4324 		}
4325 
4326 		if (!err) {
4327 			if (!del_bulk)
4328 				rtnl_fdb_notify(dev, addr, vid, RTM_DELNEIGH,
4329 						ndm->ndm_state);
4330 			ndm->ndm_flags &= ~NTF_SELF;
4331 		}
4332 	}
4333 out:
4334 	return err;
4335 }
4336 
nlmsg_populate_fdb(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,int * idx,struct netdev_hw_addr_list * list)4337 static int nlmsg_populate_fdb(struct sk_buff *skb,
4338 			      struct netlink_callback *cb,
4339 			      struct net_device *dev,
4340 			      int *idx,
4341 			      struct netdev_hw_addr_list *list)
4342 {
4343 	struct netdev_hw_addr *ha;
4344 	int err;
4345 	u32 portid, seq;
4346 
4347 	portid = NETLINK_CB(cb->skb).portid;
4348 	seq = cb->nlh->nlmsg_seq;
4349 
4350 	list_for_each_entry(ha, &list->list, list) {
4351 		if (*idx < cb->args[2])
4352 			goto skip;
4353 
4354 		err = nlmsg_populate_fdb_fill(skb, dev, ha->addr, 0,
4355 					      portid, seq,
4356 					      RTM_NEWNEIGH, NTF_SELF,
4357 					      NLM_F_MULTI, NUD_PERMANENT);
4358 		if (err < 0)
4359 			return err;
4360 skip:
4361 		*idx += 1;
4362 	}
4363 	return 0;
4364 }
4365 
4366 /**
4367  * ndo_dflt_fdb_dump - default netdevice operation to dump an FDB table.
4368  * @skb: socket buffer to store message in
4369  * @cb: netlink callback
4370  * @dev: netdevice
4371  * @filter_dev: ignored
4372  * @idx: the number of FDB table entries dumped is added to *@idx
4373  *
4374  * Default netdevice operation to dump the existing unicast address list.
4375  * Returns number of addresses from list put in skb.
4376  */
ndo_dflt_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb,struct net_device * dev,struct net_device * filter_dev,int * idx)4377 int ndo_dflt_fdb_dump(struct sk_buff *skb,
4378 		      struct netlink_callback *cb,
4379 		      struct net_device *dev,
4380 		      struct net_device *filter_dev,
4381 		      int *idx)
4382 {
4383 	int err;
4384 
4385 	if (dev->type != ARPHRD_ETHER)
4386 		return -EINVAL;
4387 
4388 	netif_addr_lock_bh(dev);
4389 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->uc);
4390 	if (err)
4391 		goto out;
4392 	err = nlmsg_populate_fdb(skb, cb, dev, idx, &dev->mc);
4393 out:
4394 	netif_addr_unlock_bh(dev);
4395 	return err;
4396 }
4397 EXPORT_SYMBOL(ndo_dflt_fdb_dump);
4398 
valid_fdb_dump_strict(const struct nlmsghdr * nlh,int * br_idx,int * brport_idx,struct netlink_ext_ack * extack)4399 static int valid_fdb_dump_strict(const struct nlmsghdr *nlh,
4400 				 int *br_idx, int *brport_idx,
4401 				 struct netlink_ext_ack *extack)
4402 {
4403 	struct nlattr *tb[NDA_MAX + 1];
4404 	struct ndmsg *ndm;
4405 	int err, i;
4406 
4407 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4408 		NL_SET_ERR_MSG(extack, "Invalid header for fdb dump request");
4409 		return -EINVAL;
4410 	}
4411 
4412 	ndm = nlmsg_data(nlh);
4413 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
4414 	    ndm->ndm_flags || ndm->ndm_type) {
4415 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb dump request");
4416 		return -EINVAL;
4417 	}
4418 
4419 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4420 					    NDA_MAX, NULL, extack);
4421 	if (err < 0)
4422 		return err;
4423 
4424 	*brport_idx = ndm->ndm_ifindex;
4425 	for (i = 0; i <= NDA_MAX; ++i) {
4426 		if (!tb[i])
4427 			continue;
4428 
4429 		switch (i) {
4430 		case NDA_IFINDEX:
4431 			if (nla_len(tb[i]) != sizeof(u32)) {
4432 				NL_SET_ERR_MSG(extack, "Invalid IFINDEX attribute in fdb dump request");
4433 				return -EINVAL;
4434 			}
4435 			*brport_idx = nla_get_u32(tb[NDA_IFINDEX]);
4436 			break;
4437 		case NDA_MASTER:
4438 			if (nla_len(tb[i]) != sizeof(u32)) {
4439 				NL_SET_ERR_MSG(extack, "Invalid MASTER attribute in fdb dump request");
4440 				return -EINVAL;
4441 			}
4442 			*br_idx = nla_get_u32(tb[NDA_MASTER]);
4443 			break;
4444 		default:
4445 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb dump request");
4446 			return -EINVAL;
4447 		}
4448 	}
4449 
4450 	return 0;
4451 }
4452 
valid_fdb_dump_legacy(const struct nlmsghdr * nlh,int * br_idx,int * brport_idx,struct netlink_ext_ack * extack)4453 static int valid_fdb_dump_legacy(const struct nlmsghdr *nlh,
4454 				 int *br_idx, int *brport_idx,
4455 				 struct netlink_ext_ack *extack)
4456 {
4457 	struct nlattr *tb[IFLA_MAX+1];
4458 	int err;
4459 
4460 	/* A hack to preserve kernel<->userspace interface.
4461 	 * Before Linux v4.12 this code accepted ndmsg since iproute2 v3.3.0.
4462 	 * However, ndmsg is shorter than ifinfomsg thus nlmsg_parse() bails.
4463 	 * So, check for ndmsg with an optional u32 attribute (not used here).
4464 	 * Fortunately these sizes don't conflict with the size of ifinfomsg
4465 	 * with an optional attribute.
4466 	 */
4467 	if (nlmsg_len(nlh) != sizeof(struct ndmsg) &&
4468 	    (nlmsg_len(nlh) != sizeof(struct ndmsg) +
4469 	     nla_attr_size(sizeof(u32)))) {
4470 		struct ifinfomsg *ifm;
4471 
4472 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4473 					     tb, IFLA_MAX, ifla_policy,
4474 					     extack);
4475 		if (err < 0) {
4476 			return -EINVAL;
4477 		} else if (err == 0) {
4478 			if (tb[IFLA_MASTER])
4479 				*br_idx = nla_get_u32(tb[IFLA_MASTER]);
4480 		}
4481 
4482 		ifm = nlmsg_data(nlh);
4483 		*brport_idx = ifm->ifi_index;
4484 	}
4485 	return 0;
4486 }
4487 
rtnl_fdb_dump(struct sk_buff * skb,struct netlink_callback * cb)4488 static int rtnl_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb)
4489 {
4490 	struct net_device *dev;
4491 	struct net_device *br_dev = NULL;
4492 	const struct net_device_ops *ops = NULL;
4493 	const struct net_device_ops *cops = NULL;
4494 	struct net *net = sock_net(skb->sk);
4495 	struct hlist_head *head;
4496 	int brport_idx = 0;
4497 	int br_idx = 0;
4498 	int h, s_h;
4499 	int idx = 0, s_idx;
4500 	int err = 0;
4501 	int fidx = 0;
4502 
4503 	if (cb->strict_check)
4504 		err = valid_fdb_dump_strict(cb->nlh, &br_idx, &brport_idx,
4505 					    cb->extack);
4506 	else
4507 		err = valid_fdb_dump_legacy(cb->nlh, &br_idx, &brport_idx,
4508 					    cb->extack);
4509 	if (err < 0)
4510 		return err;
4511 
4512 	if (br_idx) {
4513 		br_dev = __dev_get_by_index(net, br_idx);
4514 		if (!br_dev)
4515 			return -ENODEV;
4516 
4517 		ops = br_dev->netdev_ops;
4518 	}
4519 
4520 	s_h = cb->args[0];
4521 	s_idx = cb->args[1];
4522 
4523 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
4524 		idx = 0;
4525 		head = &net->dev_index_head[h];
4526 		hlist_for_each_entry(dev, head, index_hlist) {
4527 
4528 			if (brport_idx && (dev->ifindex != brport_idx))
4529 				continue;
4530 
4531 			if (!br_idx) { /* user did not specify a specific bridge */
4532 				if (netif_is_bridge_port(dev)) {
4533 					br_dev = netdev_master_upper_dev_get(dev);
4534 					cops = br_dev->netdev_ops;
4535 				}
4536 			} else {
4537 				if (dev != br_dev &&
4538 				    !netif_is_bridge_port(dev))
4539 					continue;
4540 
4541 				if (br_dev != netdev_master_upper_dev_get(dev) &&
4542 				    !netif_is_bridge_master(dev))
4543 					continue;
4544 				cops = ops;
4545 			}
4546 
4547 			if (idx < s_idx)
4548 				goto cont;
4549 
4550 			if (netif_is_bridge_port(dev)) {
4551 				if (cops && cops->ndo_fdb_dump) {
4552 					err = cops->ndo_fdb_dump(skb, cb,
4553 								br_dev, dev,
4554 								&fidx);
4555 					if (err == -EMSGSIZE)
4556 						goto out;
4557 				}
4558 			}
4559 
4560 			if (dev->netdev_ops->ndo_fdb_dump)
4561 				err = dev->netdev_ops->ndo_fdb_dump(skb, cb,
4562 								    dev, NULL,
4563 								    &fidx);
4564 			else
4565 				err = ndo_dflt_fdb_dump(skb, cb, dev, NULL,
4566 							&fidx);
4567 			if (err == -EMSGSIZE)
4568 				goto out;
4569 
4570 			cops = NULL;
4571 
4572 			/* reset fdb offset to 0 for rest of the interfaces */
4573 			cb->args[2] = 0;
4574 			fidx = 0;
4575 cont:
4576 			idx++;
4577 		}
4578 	}
4579 
4580 out:
4581 	cb->args[0] = h;
4582 	cb->args[1] = idx;
4583 	cb->args[2] = fidx;
4584 
4585 	return skb->len;
4586 }
4587 
valid_fdb_get_strict(const struct nlmsghdr * nlh,struct nlattr ** tb,u8 * ndm_flags,int * br_idx,int * brport_idx,u8 ** addr,u16 * vid,struct netlink_ext_ack * extack)4588 static int valid_fdb_get_strict(const struct nlmsghdr *nlh,
4589 				struct nlattr **tb, u8 *ndm_flags,
4590 				int *br_idx, int *brport_idx, u8 **addr,
4591 				u16 *vid, struct netlink_ext_ack *extack)
4592 {
4593 	struct ndmsg *ndm;
4594 	int err, i;
4595 
4596 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ndm))) {
4597 		NL_SET_ERR_MSG(extack, "Invalid header for fdb get request");
4598 		return -EINVAL;
4599 	}
4600 
4601 	ndm = nlmsg_data(nlh);
4602 	if (ndm->ndm_pad1  || ndm->ndm_pad2  || ndm->ndm_state ||
4603 	    ndm->ndm_type) {
4604 		NL_SET_ERR_MSG(extack, "Invalid values in header for fdb get request");
4605 		return -EINVAL;
4606 	}
4607 
4608 	if (ndm->ndm_flags & ~(NTF_MASTER | NTF_SELF)) {
4609 		NL_SET_ERR_MSG(extack, "Invalid flags in header for fdb get request");
4610 		return -EINVAL;
4611 	}
4612 
4613 	err = nlmsg_parse_deprecated_strict(nlh, sizeof(struct ndmsg), tb,
4614 					    NDA_MAX, nda_policy, extack);
4615 	if (err < 0)
4616 		return err;
4617 
4618 	*ndm_flags = ndm->ndm_flags;
4619 	*brport_idx = ndm->ndm_ifindex;
4620 	for (i = 0; i <= NDA_MAX; ++i) {
4621 		if (!tb[i])
4622 			continue;
4623 
4624 		switch (i) {
4625 		case NDA_MASTER:
4626 			*br_idx = nla_get_u32(tb[i]);
4627 			break;
4628 		case NDA_LLADDR:
4629 			if (nla_len(tb[i]) != ETH_ALEN) {
4630 				NL_SET_ERR_MSG(extack, "Invalid address in fdb get request");
4631 				return -EINVAL;
4632 			}
4633 			*addr = nla_data(tb[i]);
4634 			break;
4635 		case NDA_VLAN:
4636 			err = fdb_vid_parse(tb[i], vid, extack);
4637 			if (err)
4638 				return err;
4639 			break;
4640 		case NDA_VNI:
4641 			break;
4642 		default:
4643 			NL_SET_ERR_MSG(extack, "Unsupported attribute in fdb get request");
4644 			return -EINVAL;
4645 		}
4646 	}
4647 
4648 	return 0;
4649 }
4650 
rtnl_fdb_get(struct sk_buff * in_skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)4651 static int rtnl_fdb_get(struct sk_buff *in_skb, struct nlmsghdr *nlh,
4652 			struct netlink_ext_ack *extack)
4653 {
4654 	struct net_device *dev = NULL, *br_dev = NULL;
4655 	const struct net_device_ops *ops = NULL;
4656 	struct net *net = sock_net(in_skb->sk);
4657 	struct nlattr *tb[NDA_MAX + 1];
4658 	struct sk_buff *skb;
4659 	int brport_idx = 0;
4660 	u8 ndm_flags = 0;
4661 	int br_idx = 0;
4662 	u8 *addr = NULL;
4663 	u16 vid = 0;
4664 	int err;
4665 
4666 	err = valid_fdb_get_strict(nlh, tb, &ndm_flags, &br_idx,
4667 				   &brport_idx, &addr, &vid, extack);
4668 	if (err < 0)
4669 		return err;
4670 
4671 	if (!addr) {
4672 		NL_SET_ERR_MSG(extack, "Missing lookup address for fdb get request");
4673 		return -EINVAL;
4674 	}
4675 
4676 	if (brport_idx) {
4677 		dev = __dev_get_by_index(net, brport_idx);
4678 		if (!dev) {
4679 			NL_SET_ERR_MSG(extack, "Unknown device ifindex");
4680 			return -ENODEV;
4681 		}
4682 	}
4683 
4684 	if (br_idx) {
4685 		if (dev) {
4686 			NL_SET_ERR_MSG(extack, "Master and device are mutually exclusive");
4687 			return -EINVAL;
4688 		}
4689 
4690 		br_dev = __dev_get_by_index(net, br_idx);
4691 		if (!br_dev) {
4692 			NL_SET_ERR_MSG(extack, "Invalid master ifindex");
4693 			return -EINVAL;
4694 		}
4695 		ops = br_dev->netdev_ops;
4696 	}
4697 
4698 	if (dev) {
4699 		if (!ndm_flags || (ndm_flags & NTF_MASTER)) {
4700 			if (!netif_is_bridge_port(dev)) {
4701 				NL_SET_ERR_MSG(extack, "Device is not a bridge port");
4702 				return -EINVAL;
4703 			}
4704 			br_dev = netdev_master_upper_dev_get(dev);
4705 			if (!br_dev) {
4706 				NL_SET_ERR_MSG(extack, "Master of device not found");
4707 				return -EINVAL;
4708 			}
4709 			ops = br_dev->netdev_ops;
4710 		} else {
4711 			if (!(ndm_flags & NTF_SELF)) {
4712 				NL_SET_ERR_MSG(extack, "Missing NTF_SELF");
4713 				return -EINVAL;
4714 			}
4715 			ops = dev->netdev_ops;
4716 		}
4717 	}
4718 
4719 	if (!br_dev && !dev) {
4720 		NL_SET_ERR_MSG(extack, "No device specified");
4721 		return -ENODEV;
4722 	}
4723 
4724 	if (!ops || !ops->ndo_fdb_get) {
4725 		NL_SET_ERR_MSG(extack, "Fdb get operation not supported by device");
4726 		return -EOPNOTSUPP;
4727 	}
4728 
4729 	skb = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL);
4730 	if (!skb)
4731 		return -ENOBUFS;
4732 
4733 	if (br_dev)
4734 		dev = br_dev;
4735 	err = ops->ndo_fdb_get(skb, tb, dev, addr, vid,
4736 			       NETLINK_CB(in_skb).portid,
4737 			       nlh->nlmsg_seq, extack);
4738 	if (err)
4739 		goto out;
4740 
4741 	return rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
4742 out:
4743 	kfree_skb(skb);
4744 	return err;
4745 }
4746 
brport_nla_put_flag(struct sk_buff * skb,u32 flags,u32 mask,unsigned int attrnum,unsigned int flag)4747 static int brport_nla_put_flag(struct sk_buff *skb, u32 flags, u32 mask,
4748 			       unsigned int attrnum, unsigned int flag)
4749 {
4750 	if (mask & flag)
4751 		return nla_put_u8(skb, attrnum, !!(flags & flag));
4752 	return 0;
4753 }
4754 
ndo_dflt_bridge_getlink(struct sk_buff * skb,u32 pid,u32 seq,struct net_device * dev,u16 mode,u32 flags,u32 mask,int nlflags,u32 filter_mask,int (* vlan_fill)(struct sk_buff * skb,struct net_device * dev,u32 filter_mask))4755 int ndo_dflt_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
4756 			    struct net_device *dev, u16 mode,
4757 			    u32 flags, u32 mask, int nlflags,
4758 			    u32 filter_mask,
4759 			    int (*vlan_fill)(struct sk_buff *skb,
4760 					     struct net_device *dev,
4761 					     u32 filter_mask))
4762 {
4763 	struct nlmsghdr *nlh;
4764 	struct ifinfomsg *ifm;
4765 	struct nlattr *br_afspec;
4766 	struct nlattr *protinfo;
4767 	u8 operstate = netif_running(dev) ? dev->operstate : IF_OPER_DOWN;
4768 	struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4769 	int err = 0;
4770 
4771 	nlh = nlmsg_put(skb, pid, seq, RTM_NEWLINK, sizeof(*ifm), nlflags);
4772 	if (nlh == NULL)
4773 		return -EMSGSIZE;
4774 
4775 	ifm = nlmsg_data(nlh);
4776 	ifm->ifi_family = AF_BRIDGE;
4777 	ifm->__ifi_pad = 0;
4778 	ifm->ifi_type = dev->type;
4779 	ifm->ifi_index = dev->ifindex;
4780 	ifm->ifi_flags = dev_get_flags(dev);
4781 	ifm->ifi_change = 0;
4782 
4783 
4784 	if (nla_put_string(skb, IFLA_IFNAME, dev->name) ||
4785 	    nla_put_u32(skb, IFLA_MTU, dev->mtu) ||
4786 	    nla_put_u8(skb, IFLA_OPERSTATE, operstate) ||
4787 	    (br_dev &&
4788 	     nla_put_u32(skb, IFLA_MASTER, br_dev->ifindex)) ||
4789 	    (dev->addr_len &&
4790 	     nla_put(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr)) ||
4791 	    (dev->ifindex != dev_get_iflink(dev) &&
4792 	     nla_put_u32(skb, IFLA_LINK, dev_get_iflink(dev))))
4793 		goto nla_put_failure;
4794 
4795 	br_afspec = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
4796 	if (!br_afspec)
4797 		goto nla_put_failure;
4798 
4799 	if (nla_put_u16(skb, IFLA_BRIDGE_FLAGS, BRIDGE_FLAGS_SELF)) {
4800 		nla_nest_cancel(skb, br_afspec);
4801 		goto nla_put_failure;
4802 	}
4803 
4804 	if (mode != BRIDGE_MODE_UNDEF) {
4805 		if (nla_put_u16(skb, IFLA_BRIDGE_MODE, mode)) {
4806 			nla_nest_cancel(skb, br_afspec);
4807 			goto nla_put_failure;
4808 		}
4809 	}
4810 	if (vlan_fill) {
4811 		err = vlan_fill(skb, dev, filter_mask);
4812 		if (err) {
4813 			nla_nest_cancel(skb, br_afspec);
4814 			goto nla_put_failure;
4815 		}
4816 	}
4817 	nla_nest_end(skb, br_afspec);
4818 
4819 	protinfo = nla_nest_start(skb, IFLA_PROTINFO);
4820 	if (!protinfo)
4821 		goto nla_put_failure;
4822 
4823 	if (brport_nla_put_flag(skb, flags, mask,
4824 				IFLA_BRPORT_MODE, BR_HAIRPIN_MODE) ||
4825 	    brport_nla_put_flag(skb, flags, mask,
4826 				IFLA_BRPORT_GUARD, BR_BPDU_GUARD) ||
4827 	    brport_nla_put_flag(skb, flags, mask,
4828 				IFLA_BRPORT_FAST_LEAVE,
4829 				BR_MULTICAST_FAST_LEAVE) ||
4830 	    brport_nla_put_flag(skb, flags, mask,
4831 				IFLA_BRPORT_PROTECT, BR_ROOT_BLOCK) ||
4832 	    brport_nla_put_flag(skb, flags, mask,
4833 				IFLA_BRPORT_LEARNING, BR_LEARNING) ||
4834 	    brport_nla_put_flag(skb, flags, mask,
4835 				IFLA_BRPORT_LEARNING_SYNC, BR_LEARNING_SYNC) ||
4836 	    brport_nla_put_flag(skb, flags, mask,
4837 				IFLA_BRPORT_UNICAST_FLOOD, BR_FLOOD) ||
4838 	    brport_nla_put_flag(skb, flags, mask,
4839 				IFLA_BRPORT_PROXYARP, BR_PROXYARP) ||
4840 	    brport_nla_put_flag(skb, flags, mask,
4841 				IFLA_BRPORT_MCAST_FLOOD, BR_MCAST_FLOOD) ||
4842 	    brport_nla_put_flag(skb, flags, mask,
4843 				IFLA_BRPORT_BCAST_FLOOD, BR_BCAST_FLOOD)) {
4844 		nla_nest_cancel(skb, protinfo);
4845 		goto nla_put_failure;
4846 	}
4847 
4848 	nla_nest_end(skb, protinfo);
4849 
4850 	nlmsg_end(skb, nlh);
4851 	return 0;
4852 nla_put_failure:
4853 	nlmsg_cancel(skb, nlh);
4854 	return err ? err : -EMSGSIZE;
4855 }
4856 EXPORT_SYMBOL_GPL(ndo_dflt_bridge_getlink);
4857 
valid_bridge_getlink_req(const struct nlmsghdr * nlh,bool strict_check,u32 * filter_mask,struct netlink_ext_ack * extack)4858 static int valid_bridge_getlink_req(const struct nlmsghdr *nlh,
4859 				    bool strict_check, u32 *filter_mask,
4860 				    struct netlink_ext_ack *extack)
4861 {
4862 	struct nlattr *tb[IFLA_MAX+1];
4863 	int err, i;
4864 
4865 	if (strict_check) {
4866 		struct ifinfomsg *ifm;
4867 
4868 		if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
4869 			NL_SET_ERR_MSG(extack, "Invalid header for bridge link dump");
4870 			return -EINVAL;
4871 		}
4872 
4873 		ifm = nlmsg_data(nlh);
4874 		if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
4875 		    ifm->ifi_change || ifm->ifi_index) {
4876 			NL_SET_ERR_MSG(extack, "Invalid values in header for bridge link dump request");
4877 			return -EINVAL;
4878 		}
4879 
4880 		err = nlmsg_parse_deprecated_strict(nlh,
4881 						    sizeof(struct ifinfomsg),
4882 						    tb, IFLA_MAX, ifla_policy,
4883 						    extack);
4884 	} else {
4885 		err = nlmsg_parse_deprecated(nlh, sizeof(struct ifinfomsg),
4886 					     tb, IFLA_MAX, ifla_policy,
4887 					     extack);
4888 	}
4889 	if (err < 0)
4890 		return err;
4891 
4892 	/* new attributes should only be added with strict checking */
4893 	for (i = 0; i <= IFLA_MAX; ++i) {
4894 		if (!tb[i])
4895 			continue;
4896 
4897 		switch (i) {
4898 		case IFLA_EXT_MASK:
4899 			*filter_mask = nla_get_u32(tb[i]);
4900 			break;
4901 		default:
4902 			if (strict_check) {
4903 				NL_SET_ERR_MSG(extack, "Unsupported attribute in bridge link dump request");
4904 				return -EINVAL;
4905 			}
4906 		}
4907 	}
4908 
4909 	return 0;
4910 }
4911 
rtnl_bridge_getlink(struct sk_buff * skb,struct netlink_callback * cb)4912 static int rtnl_bridge_getlink(struct sk_buff *skb, struct netlink_callback *cb)
4913 {
4914 	const struct nlmsghdr *nlh = cb->nlh;
4915 	struct net *net = sock_net(skb->sk);
4916 	struct net_device *dev;
4917 	int idx = 0;
4918 	u32 portid = NETLINK_CB(cb->skb).portid;
4919 	u32 seq = nlh->nlmsg_seq;
4920 	u32 filter_mask = 0;
4921 	int err;
4922 
4923 	err = valid_bridge_getlink_req(nlh, cb->strict_check, &filter_mask,
4924 				       cb->extack);
4925 	if (err < 0 && cb->strict_check)
4926 		return err;
4927 
4928 	rcu_read_lock();
4929 	for_each_netdev_rcu(net, dev) {
4930 		const struct net_device_ops *ops = dev->netdev_ops;
4931 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
4932 
4933 		if (br_dev && br_dev->netdev_ops->ndo_bridge_getlink) {
4934 			if (idx >= cb->args[0]) {
4935 				err = br_dev->netdev_ops->ndo_bridge_getlink(
4936 						skb, portid, seq, dev,
4937 						filter_mask, NLM_F_MULTI);
4938 				if (err < 0 && err != -EOPNOTSUPP) {
4939 					if (likely(skb->len))
4940 						break;
4941 
4942 					goto out_err;
4943 				}
4944 			}
4945 			idx++;
4946 		}
4947 
4948 		if (ops->ndo_bridge_getlink) {
4949 			if (idx >= cb->args[0]) {
4950 				err = ops->ndo_bridge_getlink(skb, portid,
4951 							      seq, dev,
4952 							      filter_mask,
4953 							      NLM_F_MULTI);
4954 				if (err < 0 && err != -EOPNOTSUPP) {
4955 					if (likely(skb->len))
4956 						break;
4957 
4958 					goto out_err;
4959 				}
4960 			}
4961 			idx++;
4962 		}
4963 	}
4964 	err = skb->len;
4965 out_err:
4966 	rcu_read_unlock();
4967 	cb->args[0] = idx;
4968 
4969 	return err;
4970 }
4971 
bridge_nlmsg_size(void)4972 static inline size_t bridge_nlmsg_size(void)
4973 {
4974 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
4975 		+ nla_total_size(IFNAMSIZ)	/* IFLA_IFNAME */
4976 		+ nla_total_size(MAX_ADDR_LEN)	/* IFLA_ADDRESS */
4977 		+ nla_total_size(sizeof(u32))	/* IFLA_MASTER */
4978 		+ nla_total_size(sizeof(u32))	/* IFLA_MTU */
4979 		+ nla_total_size(sizeof(u32))	/* IFLA_LINK */
4980 		+ nla_total_size(sizeof(u32))	/* IFLA_OPERSTATE */
4981 		+ nla_total_size(sizeof(u8))	/* IFLA_PROTINFO */
4982 		+ nla_total_size(sizeof(struct nlattr))	/* IFLA_AF_SPEC */
4983 		+ nla_total_size(sizeof(u16))	/* IFLA_BRIDGE_FLAGS */
4984 		+ nla_total_size(sizeof(u16));	/* IFLA_BRIDGE_MODE */
4985 }
4986 
rtnl_bridge_notify(struct net_device * dev)4987 static int rtnl_bridge_notify(struct net_device *dev)
4988 {
4989 	struct net *net = dev_net(dev);
4990 	struct sk_buff *skb;
4991 	int err = -EOPNOTSUPP;
4992 
4993 	if (!dev->netdev_ops->ndo_bridge_getlink)
4994 		return 0;
4995 
4996 	skb = nlmsg_new(bridge_nlmsg_size(), GFP_ATOMIC);
4997 	if (!skb) {
4998 		err = -ENOMEM;
4999 		goto errout;
5000 	}
5001 
5002 	err = dev->netdev_ops->ndo_bridge_getlink(skb, 0, 0, dev, 0, 0);
5003 	if (err < 0)
5004 		goto errout;
5005 
5006 	/* Notification info is only filled for bridge ports, not the bridge
5007 	 * device itself. Therefore, a zero notification length is valid and
5008 	 * should not result in an error.
5009 	 */
5010 	if (!skb->len)
5011 		goto errout;
5012 
5013 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_ATOMIC);
5014 	return 0;
5015 errout:
5016 	WARN_ON(err == -EMSGSIZE);
5017 	kfree_skb(skb);
5018 	if (err)
5019 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
5020 	return err;
5021 }
5022 
rtnl_bridge_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5023 static int rtnl_bridge_setlink(struct sk_buff *skb, struct nlmsghdr *nlh,
5024 			       struct netlink_ext_ack *extack)
5025 {
5026 	struct net *net = sock_net(skb->sk);
5027 	struct ifinfomsg *ifm;
5028 	struct net_device *dev;
5029 	struct nlattr *br_spec, *attr = NULL;
5030 	int rem, err = -EOPNOTSUPP;
5031 	u16 flags = 0;
5032 	bool have_flags = false;
5033 
5034 	if (nlmsg_len(nlh) < sizeof(*ifm))
5035 		return -EINVAL;
5036 
5037 	ifm = nlmsg_data(nlh);
5038 	if (ifm->ifi_family != AF_BRIDGE)
5039 		return -EPFNOSUPPORT;
5040 
5041 	dev = __dev_get_by_index(net, ifm->ifi_index);
5042 	if (!dev) {
5043 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5044 		return -ENODEV;
5045 	}
5046 
5047 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5048 	if (br_spec) {
5049 		nla_for_each_nested(attr, br_spec, rem) {
5050 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS && !have_flags) {
5051 				if (nla_len(attr) < sizeof(flags))
5052 					return -EINVAL;
5053 
5054 				have_flags = true;
5055 				flags = nla_get_u16(attr);
5056 			}
5057 
5058 			if (nla_type(attr) == IFLA_BRIDGE_MODE) {
5059 				if (nla_len(attr) < sizeof(u16))
5060 					return -EINVAL;
5061 			}
5062 		}
5063 	}
5064 
5065 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5066 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5067 
5068 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_setlink) {
5069 			err = -EOPNOTSUPP;
5070 			goto out;
5071 		}
5072 
5073 		err = br_dev->netdev_ops->ndo_bridge_setlink(dev, nlh, flags,
5074 							     extack);
5075 		if (err)
5076 			goto out;
5077 
5078 		flags &= ~BRIDGE_FLAGS_MASTER;
5079 	}
5080 
5081 	if ((flags & BRIDGE_FLAGS_SELF)) {
5082 		if (!dev->netdev_ops->ndo_bridge_setlink)
5083 			err = -EOPNOTSUPP;
5084 		else
5085 			err = dev->netdev_ops->ndo_bridge_setlink(dev, nlh,
5086 								  flags,
5087 								  extack);
5088 		if (!err) {
5089 			flags &= ~BRIDGE_FLAGS_SELF;
5090 
5091 			/* Generate event to notify upper layer of bridge
5092 			 * change
5093 			 */
5094 			err = rtnl_bridge_notify(dev);
5095 		}
5096 	}
5097 
5098 	if (have_flags)
5099 		memcpy(nla_data(attr), &flags, sizeof(flags));
5100 out:
5101 	return err;
5102 }
5103 
rtnl_bridge_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5104 static int rtnl_bridge_dellink(struct sk_buff *skb, struct nlmsghdr *nlh,
5105 			       struct netlink_ext_ack *extack)
5106 {
5107 	struct net *net = sock_net(skb->sk);
5108 	struct ifinfomsg *ifm;
5109 	struct net_device *dev;
5110 	struct nlattr *br_spec, *attr = NULL;
5111 	int rem, err = -EOPNOTSUPP;
5112 	u16 flags = 0;
5113 	bool have_flags = false;
5114 
5115 	if (nlmsg_len(nlh) < sizeof(*ifm))
5116 		return -EINVAL;
5117 
5118 	ifm = nlmsg_data(nlh);
5119 	if (ifm->ifi_family != AF_BRIDGE)
5120 		return -EPFNOSUPPORT;
5121 
5122 	dev = __dev_get_by_index(net, ifm->ifi_index);
5123 	if (!dev) {
5124 		NL_SET_ERR_MSG(extack, "unknown ifindex");
5125 		return -ENODEV;
5126 	}
5127 
5128 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
5129 	if (br_spec) {
5130 		nla_for_each_nested(attr, br_spec, rem) {
5131 			if (nla_type(attr) == IFLA_BRIDGE_FLAGS) {
5132 				if (nla_len(attr) < sizeof(flags))
5133 					return -EINVAL;
5134 
5135 				have_flags = true;
5136 				flags = nla_get_u16(attr);
5137 				break;
5138 			}
5139 		}
5140 	}
5141 
5142 	if (!flags || (flags & BRIDGE_FLAGS_MASTER)) {
5143 		struct net_device *br_dev = netdev_master_upper_dev_get(dev);
5144 
5145 		if (!br_dev || !br_dev->netdev_ops->ndo_bridge_dellink) {
5146 			err = -EOPNOTSUPP;
5147 			goto out;
5148 		}
5149 
5150 		err = br_dev->netdev_ops->ndo_bridge_dellink(dev, nlh, flags);
5151 		if (err)
5152 			goto out;
5153 
5154 		flags &= ~BRIDGE_FLAGS_MASTER;
5155 	}
5156 
5157 	if ((flags & BRIDGE_FLAGS_SELF)) {
5158 		if (!dev->netdev_ops->ndo_bridge_dellink)
5159 			err = -EOPNOTSUPP;
5160 		else
5161 			err = dev->netdev_ops->ndo_bridge_dellink(dev, nlh,
5162 								  flags);
5163 
5164 		if (!err) {
5165 			flags &= ~BRIDGE_FLAGS_SELF;
5166 
5167 			/* Generate event to notify upper layer of bridge
5168 			 * change
5169 			 */
5170 			err = rtnl_bridge_notify(dev);
5171 		}
5172 	}
5173 
5174 	if (have_flags)
5175 		memcpy(nla_data(attr), &flags, sizeof(flags));
5176 out:
5177 	return err;
5178 }
5179 
stats_attr_valid(unsigned int mask,int attrid,int idxattr)5180 static bool stats_attr_valid(unsigned int mask, int attrid, int idxattr)
5181 {
5182 	return (mask & IFLA_STATS_FILTER_BIT(attrid)) &&
5183 	       (!idxattr || idxattr == attrid);
5184 }
5185 
5186 static bool
rtnl_offload_xstats_have_ndo(const struct net_device * dev,int attr_id)5187 rtnl_offload_xstats_have_ndo(const struct net_device *dev, int attr_id)
5188 {
5189 	return dev->netdev_ops &&
5190 	       dev->netdev_ops->ndo_has_offload_stats &&
5191 	       dev->netdev_ops->ndo_get_offload_stats &&
5192 	       dev->netdev_ops->ndo_has_offload_stats(dev, attr_id);
5193 }
5194 
5195 static unsigned int
rtnl_offload_xstats_get_size_ndo(const struct net_device * dev,int attr_id)5196 rtnl_offload_xstats_get_size_ndo(const struct net_device *dev, int attr_id)
5197 {
5198 	return rtnl_offload_xstats_have_ndo(dev, attr_id) ?
5199 	       sizeof(struct rtnl_link_stats64) : 0;
5200 }
5201 
5202 static int
rtnl_offload_xstats_fill_ndo(struct net_device * dev,int attr_id,struct sk_buff * skb)5203 rtnl_offload_xstats_fill_ndo(struct net_device *dev, int attr_id,
5204 			     struct sk_buff *skb)
5205 {
5206 	unsigned int size = rtnl_offload_xstats_get_size_ndo(dev, attr_id);
5207 	struct nlattr *attr = NULL;
5208 	void *attr_data;
5209 	int err;
5210 
5211 	if (!size)
5212 		return -ENODATA;
5213 
5214 	attr = nla_reserve_64bit(skb, attr_id, size,
5215 				 IFLA_OFFLOAD_XSTATS_UNSPEC);
5216 	if (!attr)
5217 		return -EMSGSIZE;
5218 
5219 	attr_data = nla_data(attr);
5220 	memset(attr_data, 0, size);
5221 
5222 	err = dev->netdev_ops->ndo_get_offload_stats(attr_id, dev, attr_data);
5223 	if (err)
5224 		return err;
5225 
5226 	return 0;
5227 }
5228 
5229 static unsigned int
rtnl_offload_xstats_get_size_stats(const struct net_device * dev,enum netdev_offload_xstats_type type)5230 rtnl_offload_xstats_get_size_stats(const struct net_device *dev,
5231 				   enum netdev_offload_xstats_type type)
5232 {
5233 	bool enabled = netdev_offload_xstats_enabled(dev, type);
5234 
5235 	return enabled ? sizeof(struct rtnl_hw_stats64) : 0;
5236 }
5237 
5238 struct rtnl_offload_xstats_request_used {
5239 	bool request;
5240 	bool used;
5241 };
5242 
5243 static int
rtnl_offload_xstats_get_stats(struct net_device * dev,enum netdev_offload_xstats_type type,struct rtnl_offload_xstats_request_used * ru,struct rtnl_hw_stats64 * stats,struct netlink_ext_ack * extack)5244 rtnl_offload_xstats_get_stats(struct net_device *dev,
5245 			      enum netdev_offload_xstats_type type,
5246 			      struct rtnl_offload_xstats_request_used *ru,
5247 			      struct rtnl_hw_stats64 *stats,
5248 			      struct netlink_ext_ack *extack)
5249 {
5250 	bool request;
5251 	bool used;
5252 	int err;
5253 
5254 	request = netdev_offload_xstats_enabled(dev, type);
5255 	if (!request) {
5256 		used = false;
5257 		goto out;
5258 	}
5259 
5260 	err = netdev_offload_xstats_get(dev, type, stats, &used, extack);
5261 	if (err)
5262 		return err;
5263 
5264 out:
5265 	if (ru) {
5266 		ru->request = request;
5267 		ru->used = used;
5268 	}
5269 	return 0;
5270 }
5271 
5272 static int
rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff * skb,int attr_id,struct rtnl_offload_xstats_request_used * ru)5273 rtnl_offload_xstats_fill_hw_s_info_one(struct sk_buff *skb, int attr_id,
5274 				       struct rtnl_offload_xstats_request_used *ru)
5275 {
5276 	struct nlattr *nest;
5277 
5278 	nest = nla_nest_start(skb, attr_id);
5279 	if (!nest)
5280 		return -EMSGSIZE;
5281 
5282 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST, ru->request))
5283 		goto nla_put_failure;
5284 
5285 	if (nla_put_u8(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED, ru->used))
5286 		goto nla_put_failure;
5287 
5288 	nla_nest_end(skb, nest);
5289 	return 0;
5290 
5291 nla_put_failure:
5292 	nla_nest_cancel(skb, nest);
5293 	return -EMSGSIZE;
5294 }
5295 
5296 static int
rtnl_offload_xstats_fill_hw_s_info(struct sk_buff * skb,struct net_device * dev,struct netlink_ext_ack * extack)5297 rtnl_offload_xstats_fill_hw_s_info(struct sk_buff *skb, struct net_device *dev,
5298 				   struct netlink_ext_ack *extack)
5299 {
5300 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5301 	struct rtnl_offload_xstats_request_used ru_l3;
5302 	struct nlattr *nest;
5303 	int err;
5304 
5305 	err = rtnl_offload_xstats_get_stats(dev, t_l3, &ru_l3, NULL, extack);
5306 	if (err)
5307 		return err;
5308 
5309 	nest = nla_nest_start(skb, IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5310 	if (!nest)
5311 		return -EMSGSIZE;
5312 
5313 	if (rtnl_offload_xstats_fill_hw_s_info_one(skb,
5314 						   IFLA_OFFLOAD_XSTATS_L3_STATS,
5315 						   &ru_l3))
5316 		goto nla_put_failure;
5317 
5318 	nla_nest_end(skb, nest);
5319 	return 0;
5320 
5321 nla_put_failure:
5322 	nla_nest_cancel(skb, nest);
5323 	return -EMSGSIZE;
5324 }
5325 
rtnl_offload_xstats_fill(struct sk_buff * skb,struct net_device * dev,int * prividx,u32 off_filter_mask,struct netlink_ext_ack * extack)5326 static int rtnl_offload_xstats_fill(struct sk_buff *skb, struct net_device *dev,
5327 				    int *prividx, u32 off_filter_mask,
5328 				    struct netlink_ext_ack *extack)
5329 {
5330 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5331 	int attr_id_hw_s_info = IFLA_OFFLOAD_XSTATS_HW_S_INFO;
5332 	int attr_id_l3_stats = IFLA_OFFLOAD_XSTATS_L3_STATS;
5333 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5334 	bool have_data = false;
5335 	int err;
5336 
5337 	if (*prividx <= attr_id_cpu_hit &&
5338 	    (off_filter_mask &
5339 	     IFLA_STATS_FILTER_BIT(attr_id_cpu_hit))) {
5340 		err = rtnl_offload_xstats_fill_ndo(dev, attr_id_cpu_hit, skb);
5341 		if (!err) {
5342 			have_data = true;
5343 		} else if (err != -ENODATA) {
5344 			*prividx = attr_id_cpu_hit;
5345 			return err;
5346 		}
5347 	}
5348 
5349 	if (*prividx <= attr_id_hw_s_info &&
5350 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_hw_s_info))) {
5351 		*prividx = attr_id_hw_s_info;
5352 
5353 		err = rtnl_offload_xstats_fill_hw_s_info(skb, dev, extack);
5354 		if (err)
5355 			return err;
5356 
5357 		have_data = true;
5358 		*prividx = 0;
5359 	}
5360 
5361 	if (*prividx <= attr_id_l3_stats &&
5362 	    (off_filter_mask & IFLA_STATS_FILTER_BIT(attr_id_l3_stats))) {
5363 		unsigned int size_l3;
5364 		struct nlattr *attr;
5365 
5366 		*prividx = attr_id_l3_stats;
5367 
5368 		size_l3 = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5369 		if (!size_l3)
5370 			goto skip_l3_stats;
5371 		attr = nla_reserve_64bit(skb, attr_id_l3_stats, size_l3,
5372 					 IFLA_OFFLOAD_XSTATS_UNSPEC);
5373 		if (!attr)
5374 			return -EMSGSIZE;
5375 
5376 		err = rtnl_offload_xstats_get_stats(dev, t_l3, NULL,
5377 						    nla_data(attr), extack);
5378 		if (err)
5379 			return err;
5380 
5381 		have_data = true;
5382 skip_l3_stats:
5383 		*prividx = 0;
5384 	}
5385 
5386 	if (!have_data)
5387 		return -ENODATA;
5388 
5389 	*prividx = 0;
5390 	return 0;
5391 }
5392 
5393 static unsigned int
rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device * dev,enum netdev_offload_xstats_type type)5394 rtnl_offload_xstats_get_size_hw_s_info_one(const struct net_device *dev,
5395 					   enum netdev_offload_xstats_type type)
5396 {
5397 	return nla_total_size(0) +
5398 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_REQUEST */
5399 		nla_total_size(sizeof(u8)) +
5400 		/* IFLA_OFFLOAD_XSTATS_HW_S_INFO_USED */
5401 		nla_total_size(sizeof(u8)) +
5402 		0;
5403 }
5404 
5405 static unsigned int
rtnl_offload_xstats_get_size_hw_s_info(const struct net_device * dev)5406 rtnl_offload_xstats_get_size_hw_s_info(const struct net_device *dev)
5407 {
5408 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5409 
5410 	return nla_total_size(0) +
5411 		/* IFLA_OFFLOAD_XSTATS_L3_STATS */
5412 		rtnl_offload_xstats_get_size_hw_s_info_one(dev, t_l3) +
5413 		0;
5414 }
5415 
rtnl_offload_xstats_get_size(const struct net_device * dev,u32 off_filter_mask)5416 static int rtnl_offload_xstats_get_size(const struct net_device *dev,
5417 					u32 off_filter_mask)
5418 {
5419 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5420 	int attr_id_cpu_hit = IFLA_OFFLOAD_XSTATS_CPU_HIT;
5421 	int nla_size = 0;
5422 	int size;
5423 
5424 	if (off_filter_mask &
5425 	    IFLA_STATS_FILTER_BIT(attr_id_cpu_hit)) {
5426 		size = rtnl_offload_xstats_get_size_ndo(dev, attr_id_cpu_hit);
5427 		nla_size += nla_total_size_64bit(size);
5428 	}
5429 
5430 	if (off_filter_mask &
5431 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO))
5432 		nla_size += rtnl_offload_xstats_get_size_hw_s_info(dev);
5433 
5434 	if (off_filter_mask &
5435 	    IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_L3_STATS)) {
5436 		size = rtnl_offload_xstats_get_size_stats(dev, t_l3);
5437 		nla_size += nla_total_size_64bit(size);
5438 	}
5439 
5440 	if (nla_size != 0)
5441 		nla_size += nla_total_size(0);
5442 
5443 	return nla_size;
5444 }
5445 
5446 struct rtnl_stats_dump_filters {
5447 	/* mask[0] filters outer attributes. Then individual nests have their
5448 	 * filtering mask at the index of the nested attribute.
5449 	 */
5450 	u32 mask[IFLA_STATS_MAX + 1];
5451 };
5452 
rtnl_fill_statsinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change,unsigned int flags,const struct rtnl_stats_dump_filters * filters,int * idxattr,int * prividx,struct netlink_ext_ack * extack)5453 static int rtnl_fill_statsinfo(struct sk_buff *skb, struct net_device *dev,
5454 			       int type, u32 pid, u32 seq, u32 change,
5455 			       unsigned int flags,
5456 			       const struct rtnl_stats_dump_filters *filters,
5457 			       int *idxattr, int *prividx,
5458 			       struct netlink_ext_ack *extack)
5459 {
5460 	unsigned int filter_mask = filters->mask[0];
5461 	struct if_stats_msg *ifsm;
5462 	struct nlmsghdr *nlh;
5463 	struct nlattr *attr;
5464 	int s_prividx = *prividx;
5465 	int err;
5466 
5467 	ASSERT_RTNL();
5468 
5469 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifsm), flags);
5470 	if (!nlh)
5471 		return -EMSGSIZE;
5472 
5473 	ifsm = nlmsg_data(nlh);
5474 	ifsm->family = PF_UNSPEC;
5475 	ifsm->pad1 = 0;
5476 	ifsm->pad2 = 0;
5477 	ifsm->ifindex = dev->ifindex;
5478 	ifsm->filter_mask = filter_mask;
5479 
5480 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, *idxattr)) {
5481 		struct rtnl_link_stats64 *sp;
5482 
5483 		attr = nla_reserve_64bit(skb, IFLA_STATS_LINK_64,
5484 					 sizeof(struct rtnl_link_stats64),
5485 					 IFLA_STATS_UNSPEC);
5486 		if (!attr) {
5487 			err = -EMSGSIZE;
5488 			goto nla_put_failure;
5489 		}
5490 
5491 		sp = nla_data(attr);
5492 		dev_get_stats(dev, sp);
5493 	}
5494 
5495 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, *idxattr)) {
5496 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5497 
5498 		if (ops && ops->fill_linkxstats) {
5499 			*idxattr = IFLA_STATS_LINK_XSTATS;
5500 			attr = nla_nest_start_noflag(skb,
5501 						     IFLA_STATS_LINK_XSTATS);
5502 			if (!attr) {
5503 				err = -EMSGSIZE;
5504 				goto nla_put_failure;
5505 			}
5506 
5507 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5508 			nla_nest_end(skb, attr);
5509 			if (err)
5510 				goto nla_put_failure;
5511 			*idxattr = 0;
5512 		}
5513 	}
5514 
5515 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE,
5516 			     *idxattr)) {
5517 		const struct rtnl_link_ops *ops = NULL;
5518 		const struct net_device *master;
5519 
5520 		master = netdev_master_upper_dev_get(dev);
5521 		if (master)
5522 			ops = master->rtnl_link_ops;
5523 		if (ops && ops->fill_linkxstats) {
5524 			*idxattr = IFLA_STATS_LINK_XSTATS_SLAVE;
5525 			attr = nla_nest_start_noflag(skb,
5526 						     IFLA_STATS_LINK_XSTATS_SLAVE);
5527 			if (!attr) {
5528 				err = -EMSGSIZE;
5529 				goto nla_put_failure;
5530 			}
5531 
5532 			err = ops->fill_linkxstats(skb, dev, prividx, *idxattr);
5533 			nla_nest_end(skb, attr);
5534 			if (err)
5535 				goto nla_put_failure;
5536 			*idxattr = 0;
5537 		}
5538 	}
5539 
5540 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS,
5541 			     *idxattr)) {
5542 		u32 off_filter_mask;
5543 
5544 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5545 		*idxattr = IFLA_STATS_LINK_OFFLOAD_XSTATS;
5546 		attr = nla_nest_start_noflag(skb,
5547 					     IFLA_STATS_LINK_OFFLOAD_XSTATS);
5548 		if (!attr) {
5549 			err = -EMSGSIZE;
5550 			goto nla_put_failure;
5551 		}
5552 
5553 		err = rtnl_offload_xstats_fill(skb, dev, prividx,
5554 					       off_filter_mask, extack);
5555 		if (err == -ENODATA)
5556 			nla_nest_cancel(skb, attr);
5557 		else
5558 			nla_nest_end(skb, attr);
5559 
5560 		if (err && err != -ENODATA)
5561 			goto nla_put_failure;
5562 		*idxattr = 0;
5563 	}
5564 
5565 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, *idxattr)) {
5566 		struct rtnl_af_ops *af_ops;
5567 
5568 		*idxattr = IFLA_STATS_AF_SPEC;
5569 		attr = nla_nest_start_noflag(skb, IFLA_STATS_AF_SPEC);
5570 		if (!attr) {
5571 			err = -EMSGSIZE;
5572 			goto nla_put_failure;
5573 		}
5574 
5575 		rcu_read_lock();
5576 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5577 			if (af_ops->fill_stats_af) {
5578 				struct nlattr *af;
5579 
5580 				af = nla_nest_start_noflag(skb,
5581 							   af_ops->family);
5582 				if (!af) {
5583 					rcu_read_unlock();
5584 					err = -EMSGSIZE;
5585 					goto nla_put_failure;
5586 				}
5587 				err = af_ops->fill_stats_af(skb, dev);
5588 
5589 				if (err == -ENODATA) {
5590 					nla_nest_cancel(skb, af);
5591 				} else if (err < 0) {
5592 					rcu_read_unlock();
5593 					goto nla_put_failure;
5594 				}
5595 
5596 				nla_nest_end(skb, af);
5597 			}
5598 		}
5599 		rcu_read_unlock();
5600 
5601 		nla_nest_end(skb, attr);
5602 
5603 		*idxattr = 0;
5604 	}
5605 
5606 	nlmsg_end(skb, nlh);
5607 
5608 	return 0;
5609 
5610 nla_put_failure:
5611 	/* not a multi message or no progress mean a real error */
5612 	if (!(flags & NLM_F_MULTI) || s_prividx == *prividx)
5613 		nlmsg_cancel(skb, nlh);
5614 	else
5615 		nlmsg_end(skb, nlh);
5616 
5617 	return err;
5618 }
5619 
if_nlmsg_stats_size(const struct net_device * dev,const struct rtnl_stats_dump_filters * filters)5620 static size_t if_nlmsg_stats_size(const struct net_device *dev,
5621 				  const struct rtnl_stats_dump_filters *filters)
5622 {
5623 	size_t size = NLMSG_ALIGN(sizeof(struct if_stats_msg));
5624 	unsigned int filter_mask = filters->mask[0];
5625 
5626 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_64, 0))
5627 		size += nla_total_size_64bit(sizeof(struct rtnl_link_stats64));
5628 
5629 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS, 0)) {
5630 		const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
5631 		int attr = IFLA_STATS_LINK_XSTATS;
5632 
5633 		if (ops && ops->get_linkxstats_size) {
5634 			size += nla_total_size(ops->get_linkxstats_size(dev,
5635 									attr));
5636 			/* for IFLA_STATS_LINK_XSTATS */
5637 			size += nla_total_size(0);
5638 		}
5639 	}
5640 
5641 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_XSTATS_SLAVE, 0)) {
5642 		struct net_device *_dev = (struct net_device *)dev;
5643 		const struct rtnl_link_ops *ops = NULL;
5644 		const struct net_device *master;
5645 
5646 		/* netdev_master_upper_dev_get can't take const */
5647 		master = netdev_master_upper_dev_get(_dev);
5648 		if (master)
5649 			ops = master->rtnl_link_ops;
5650 		if (ops && ops->get_linkxstats_size) {
5651 			int attr = IFLA_STATS_LINK_XSTATS_SLAVE;
5652 
5653 			size += nla_total_size(ops->get_linkxstats_size(dev,
5654 									attr));
5655 			/* for IFLA_STATS_LINK_XSTATS_SLAVE */
5656 			size += nla_total_size(0);
5657 		}
5658 	}
5659 
5660 	if (stats_attr_valid(filter_mask, IFLA_STATS_LINK_OFFLOAD_XSTATS, 0)) {
5661 		u32 off_filter_mask;
5662 
5663 		off_filter_mask = filters->mask[IFLA_STATS_LINK_OFFLOAD_XSTATS];
5664 		size += rtnl_offload_xstats_get_size(dev, off_filter_mask);
5665 	}
5666 
5667 	if (stats_attr_valid(filter_mask, IFLA_STATS_AF_SPEC, 0)) {
5668 		struct rtnl_af_ops *af_ops;
5669 
5670 		/* for IFLA_STATS_AF_SPEC */
5671 		size += nla_total_size(0);
5672 
5673 		rcu_read_lock();
5674 		list_for_each_entry_rcu(af_ops, &rtnl_af_ops, list) {
5675 			if (af_ops->get_stats_af_size) {
5676 				size += nla_total_size(
5677 					af_ops->get_stats_af_size(dev));
5678 
5679 				/* for AF_* */
5680 				size += nla_total_size(0);
5681 			}
5682 		}
5683 		rcu_read_unlock();
5684 	}
5685 
5686 	return size;
5687 }
5688 
5689 #define RTNL_STATS_OFFLOAD_XSTATS_VALID ((1 << __IFLA_OFFLOAD_XSTATS_MAX) - 1)
5690 
5691 static const struct nla_policy
5692 rtnl_stats_get_policy_filters[IFLA_STATS_MAX + 1] = {
5693 	[IFLA_STATS_LINK_OFFLOAD_XSTATS] =
5694 		    NLA_POLICY_MASK(NLA_U32, RTNL_STATS_OFFLOAD_XSTATS_VALID),
5695 };
5696 
5697 static const struct nla_policy
5698 rtnl_stats_get_policy[IFLA_STATS_GETSET_MAX + 1] = {
5699 	[IFLA_STATS_GET_FILTERS] =
5700 		    NLA_POLICY_NESTED(rtnl_stats_get_policy_filters),
5701 };
5702 
5703 static const struct nla_policy
5704 ifla_stats_set_policy[IFLA_STATS_GETSET_MAX + 1] = {
5705 	[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS] = NLA_POLICY_MAX(NLA_U8, 1),
5706 };
5707 
rtnl_stats_get_parse_filters(struct nlattr * ifla_filters,struct rtnl_stats_dump_filters * filters,struct netlink_ext_ack * extack)5708 static int rtnl_stats_get_parse_filters(struct nlattr *ifla_filters,
5709 					struct rtnl_stats_dump_filters *filters,
5710 					struct netlink_ext_ack *extack)
5711 {
5712 	struct nlattr *tb[IFLA_STATS_MAX + 1];
5713 	int err;
5714 	int at;
5715 
5716 	err = nla_parse_nested(tb, IFLA_STATS_MAX, ifla_filters,
5717 			       rtnl_stats_get_policy_filters, extack);
5718 	if (err < 0)
5719 		return err;
5720 
5721 	for (at = 1; at <= IFLA_STATS_MAX; at++) {
5722 		if (tb[at]) {
5723 			if (!(filters->mask[0] & IFLA_STATS_FILTER_BIT(at))) {
5724 				NL_SET_ERR_MSG(extack, "Filtered attribute not enabled in filter_mask");
5725 				return -EINVAL;
5726 			}
5727 			filters->mask[at] = nla_get_u32(tb[at]);
5728 		}
5729 	}
5730 
5731 	return 0;
5732 }
5733 
rtnl_stats_get_parse(const struct nlmsghdr * nlh,u32 filter_mask,struct rtnl_stats_dump_filters * filters,struct netlink_ext_ack * extack)5734 static int rtnl_stats_get_parse(const struct nlmsghdr *nlh,
5735 				u32 filter_mask,
5736 				struct rtnl_stats_dump_filters *filters,
5737 				struct netlink_ext_ack *extack)
5738 {
5739 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5740 	int err;
5741 	int i;
5742 
5743 	filters->mask[0] = filter_mask;
5744 	for (i = 1; i < ARRAY_SIZE(filters->mask); i++)
5745 		filters->mask[i] = -1U;
5746 
5747 	err = nlmsg_parse(nlh, sizeof(struct if_stats_msg), tb,
5748 			  IFLA_STATS_GETSET_MAX, rtnl_stats_get_policy, extack);
5749 	if (err < 0)
5750 		return err;
5751 
5752 	if (tb[IFLA_STATS_GET_FILTERS]) {
5753 		err = rtnl_stats_get_parse_filters(tb[IFLA_STATS_GET_FILTERS],
5754 						   filters, extack);
5755 		if (err)
5756 			return err;
5757 	}
5758 
5759 	return 0;
5760 }
5761 
rtnl_valid_stats_req(const struct nlmsghdr * nlh,bool strict_check,bool is_dump,struct netlink_ext_ack * extack)5762 static int rtnl_valid_stats_req(const struct nlmsghdr *nlh, bool strict_check,
5763 				bool is_dump, struct netlink_ext_ack *extack)
5764 {
5765 	struct if_stats_msg *ifsm;
5766 
5767 	if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifsm))) {
5768 		NL_SET_ERR_MSG(extack, "Invalid header for stats dump");
5769 		return -EINVAL;
5770 	}
5771 
5772 	if (!strict_check)
5773 		return 0;
5774 
5775 	ifsm = nlmsg_data(nlh);
5776 
5777 	/* only requests using strict checks can pass data to influence
5778 	 * the dump. The legacy exception is filter_mask.
5779 	 */
5780 	if (ifsm->pad1 || ifsm->pad2 || (is_dump && ifsm->ifindex)) {
5781 		NL_SET_ERR_MSG(extack, "Invalid values in header for stats dump request");
5782 		return -EINVAL;
5783 	}
5784 	if (ifsm->filter_mask >= IFLA_STATS_FILTER_BIT(IFLA_STATS_MAX + 1)) {
5785 		NL_SET_ERR_MSG(extack, "Invalid stats requested through filter mask");
5786 		return -EINVAL;
5787 	}
5788 
5789 	return 0;
5790 }
5791 
rtnl_stats_get(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5792 static int rtnl_stats_get(struct sk_buff *skb, struct nlmsghdr *nlh,
5793 			  struct netlink_ext_ack *extack)
5794 {
5795 	struct rtnl_stats_dump_filters filters;
5796 	struct net *net = sock_net(skb->sk);
5797 	struct net_device *dev = NULL;
5798 	int idxattr = 0, prividx = 0;
5799 	struct if_stats_msg *ifsm;
5800 	struct sk_buff *nskb;
5801 	int err;
5802 
5803 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5804 				   false, extack);
5805 	if (err)
5806 		return err;
5807 
5808 	ifsm = nlmsg_data(nlh);
5809 	if (ifsm->ifindex > 0)
5810 		dev = __dev_get_by_index(net, ifsm->ifindex);
5811 	else
5812 		return -EINVAL;
5813 
5814 	if (!dev)
5815 		return -ENODEV;
5816 
5817 	if (!ifsm->filter_mask) {
5818 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats get");
5819 		return -EINVAL;
5820 	}
5821 
5822 	err = rtnl_stats_get_parse(nlh, ifsm->filter_mask, &filters, extack);
5823 	if (err)
5824 		return err;
5825 
5826 	nskb = nlmsg_new(if_nlmsg_stats_size(dev, &filters), GFP_KERNEL);
5827 	if (!nskb)
5828 		return -ENOBUFS;
5829 
5830 	err = rtnl_fill_statsinfo(nskb, dev, RTM_NEWSTATS,
5831 				  NETLINK_CB(skb).portid, nlh->nlmsg_seq, 0,
5832 				  0, &filters, &idxattr, &prividx, extack);
5833 	if (err < 0) {
5834 		/* -EMSGSIZE implies BUG in if_nlmsg_stats_size */
5835 		WARN_ON(err == -EMSGSIZE);
5836 		kfree_skb(nskb);
5837 	} else {
5838 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).portid);
5839 	}
5840 
5841 	return err;
5842 }
5843 
rtnl_stats_dump(struct sk_buff * skb,struct netlink_callback * cb)5844 static int rtnl_stats_dump(struct sk_buff *skb, struct netlink_callback *cb)
5845 {
5846 	struct netlink_ext_ack *extack = cb->extack;
5847 	int h, s_h, err, s_idx, s_idxattr, s_prividx;
5848 	struct rtnl_stats_dump_filters filters;
5849 	struct net *net = sock_net(skb->sk);
5850 	unsigned int flags = NLM_F_MULTI;
5851 	struct if_stats_msg *ifsm;
5852 	struct hlist_head *head;
5853 	struct net_device *dev;
5854 	int idx = 0;
5855 
5856 	s_h = cb->args[0];
5857 	s_idx = cb->args[1];
5858 	s_idxattr = cb->args[2];
5859 	s_prividx = cb->args[3];
5860 
5861 	cb->seq = net->dev_base_seq;
5862 
5863 	err = rtnl_valid_stats_req(cb->nlh, cb->strict_check, true, extack);
5864 	if (err)
5865 		return err;
5866 
5867 	ifsm = nlmsg_data(cb->nlh);
5868 	if (!ifsm->filter_mask) {
5869 		NL_SET_ERR_MSG(extack, "Filter mask must be set for stats dump");
5870 		return -EINVAL;
5871 	}
5872 
5873 	err = rtnl_stats_get_parse(cb->nlh, ifsm->filter_mask, &filters,
5874 				   extack);
5875 	if (err)
5876 		return err;
5877 
5878 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
5879 		idx = 0;
5880 		head = &net->dev_index_head[h];
5881 		hlist_for_each_entry(dev, head, index_hlist) {
5882 			if (idx < s_idx)
5883 				goto cont;
5884 			err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS,
5885 						  NETLINK_CB(cb->skb).portid,
5886 						  cb->nlh->nlmsg_seq, 0,
5887 						  flags, &filters,
5888 						  &s_idxattr, &s_prividx,
5889 						  extack);
5890 			/* If we ran out of room on the first message,
5891 			 * we're in trouble
5892 			 */
5893 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
5894 
5895 			if (err < 0)
5896 				goto out;
5897 			s_prividx = 0;
5898 			s_idxattr = 0;
5899 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
5900 cont:
5901 			idx++;
5902 		}
5903 	}
5904 out:
5905 	cb->args[3] = s_prividx;
5906 	cb->args[2] = s_idxattr;
5907 	cb->args[1] = idx;
5908 	cb->args[0] = h;
5909 
5910 	return skb->len;
5911 }
5912 
rtnl_offload_xstats_notify(struct net_device * dev)5913 void rtnl_offload_xstats_notify(struct net_device *dev)
5914 {
5915 	struct rtnl_stats_dump_filters response_filters = {};
5916 	struct net *net = dev_net(dev);
5917 	int idxattr = 0, prividx = 0;
5918 	struct sk_buff *skb;
5919 	int err = -ENOBUFS;
5920 
5921 	ASSERT_RTNL();
5922 
5923 	response_filters.mask[0] |=
5924 		IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
5925 	response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
5926 		IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
5927 
5928 	skb = nlmsg_new(if_nlmsg_stats_size(dev, &response_filters),
5929 			GFP_KERNEL);
5930 	if (!skb)
5931 		goto errout;
5932 
5933 	err = rtnl_fill_statsinfo(skb, dev, RTM_NEWSTATS, 0, 0, 0, 0,
5934 				  &response_filters, &idxattr, &prividx, NULL);
5935 	if (err < 0) {
5936 		kfree_skb(skb);
5937 		goto errout;
5938 	}
5939 
5940 	rtnl_notify(skb, net, 0, RTNLGRP_STATS, NULL, GFP_KERNEL);
5941 	return;
5942 
5943 errout:
5944 	rtnl_set_sk_err(net, RTNLGRP_STATS, err);
5945 }
5946 EXPORT_SYMBOL(rtnl_offload_xstats_notify);
5947 
rtnl_stats_set(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)5948 static int rtnl_stats_set(struct sk_buff *skb, struct nlmsghdr *nlh,
5949 			  struct netlink_ext_ack *extack)
5950 {
5951 	enum netdev_offload_xstats_type t_l3 = NETDEV_OFFLOAD_XSTATS_TYPE_L3;
5952 	struct rtnl_stats_dump_filters response_filters = {};
5953 	struct nlattr *tb[IFLA_STATS_GETSET_MAX + 1];
5954 	struct net *net = sock_net(skb->sk);
5955 	struct net_device *dev = NULL;
5956 	struct if_stats_msg *ifsm;
5957 	bool notify = false;
5958 	int err;
5959 
5960 	err = rtnl_valid_stats_req(nlh, netlink_strict_get_check(skb),
5961 				   false, extack);
5962 	if (err)
5963 		return err;
5964 
5965 	ifsm = nlmsg_data(nlh);
5966 	if (ifsm->family != AF_UNSPEC) {
5967 		NL_SET_ERR_MSG(extack, "Address family should be AF_UNSPEC");
5968 		return -EINVAL;
5969 	}
5970 
5971 	if (ifsm->ifindex > 0)
5972 		dev = __dev_get_by_index(net, ifsm->ifindex);
5973 	else
5974 		return -EINVAL;
5975 
5976 	if (!dev)
5977 		return -ENODEV;
5978 
5979 	if (ifsm->filter_mask) {
5980 		NL_SET_ERR_MSG(extack, "Filter mask must be 0 for stats set");
5981 		return -EINVAL;
5982 	}
5983 
5984 	err = nlmsg_parse(nlh, sizeof(*ifsm), tb, IFLA_STATS_GETSET_MAX,
5985 			  ifla_stats_set_policy, extack);
5986 	if (err < 0)
5987 		return err;
5988 
5989 	if (tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]) {
5990 		u8 req = nla_get_u8(tb[IFLA_STATS_SET_OFFLOAD_XSTATS_L3_STATS]);
5991 
5992 		if (req)
5993 			err = netdev_offload_xstats_enable(dev, t_l3, extack);
5994 		else
5995 			err = netdev_offload_xstats_disable(dev, t_l3);
5996 
5997 		if (!err)
5998 			notify = true;
5999 		else if (err != -EALREADY)
6000 			return err;
6001 
6002 		response_filters.mask[0] |=
6003 			IFLA_STATS_FILTER_BIT(IFLA_STATS_LINK_OFFLOAD_XSTATS);
6004 		response_filters.mask[IFLA_STATS_LINK_OFFLOAD_XSTATS] |=
6005 			IFLA_STATS_FILTER_BIT(IFLA_OFFLOAD_XSTATS_HW_S_INFO);
6006 	}
6007 
6008 	if (notify)
6009 		rtnl_offload_xstats_notify(dev);
6010 
6011 	return 0;
6012 }
6013 
6014 /* Process one rtnetlink message. */
6015 
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh,struct netlink_ext_ack * extack)6016 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
6017 			     struct netlink_ext_ack *extack)
6018 {
6019 	struct net *net = sock_net(skb->sk);
6020 	struct rtnl_link *link;
6021 	enum rtnl_kinds kind;
6022 	struct module *owner;
6023 	int err = -EOPNOTSUPP;
6024 	rtnl_doit_func doit;
6025 	unsigned int flags;
6026 	int family;
6027 	int type;
6028 
6029 	type = nlh->nlmsg_type;
6030 	if (type > RTM_MAX)
6031 		return -EOPNOTSUPP;
6032 
6033 	type -= RTM_BASE;
6034 
6035 	/* All the messages must have at least 1 byte length */
6036 	if (nlmsg_len(nlh) < sizeof(struct rtgenmsg))
6037 		return 0;
6038 
6039 	family = ((struct rtgenmsg *)nlmsg_data(nlh))->rtgen_family;
6040 	kind = rtnl_msgtype_kind(type);
6041 
6042 	if (kind != RTNL_KIND_GET && !netlink_net_capable(skb, CAP_NET_ADMIN))
6043 		return -EPERM;
6044 
6045 	rcu_read_lock();
6046 	if (kind == RTNL_KIND_GET && (nlh->nlmsg_flags & NLM_F_DUMP)) {
6047 		struct sock *rtnl;
6048 		rtnl_dumpit_func dumpit;
6049 		u32 min_dump_alloc = 0;
6050 
6051 		link = rtnl_get_link(family, type);
6052 		if (!link || !link->dumpit) {
6053 			family = PF_UNSPEC;
6054 			link = rtnl_get_link(family, type);
6055 			if (!link || !link->dumpit)
6056 				goto err_unlock;
6057 		}
6058 		owner = link->owner;
6059 		dumpit = link->dumpit;
6060 
6061 		if (type == RTM_GETLINK - RTM_BASE)
6062 			min_dump_alloc = rtnl_calcit(skb, nlh);
6063 
6064 		err = 0;
6065 		/* need to do this before rcu_read_unlock() */
6066 		if (!try_module_get(owner))
6067 			err = -EPROTONOSUPPORT;
6068 
6069 		rcu_read_unlock();
6070 
6071 		rtnl = net->rtnl;
6072 		if (err == 0) {
6073 			struct netlink_dump_control c = {
6074 				.dump		= dumpit,
6075 				.min_dump_alloc	= min_dump_alloc,
6076 				.module		= owner,
6077 			};
6078 			err = netlink_dump_start(rtnl, skb, nlh, &c);
6079 			/* netlink_dump_start() will keep a reference on
6080 			 * module if dump is still in progress.
6081 			 */
6082 			module_put(owner);
6083 		}
6084 		return err;
6085 	}
6086 
6087 	link = rtnl_get_link(family, type);
6088 	if (!link || !link->doit) {
6089 		family = PF_UNSPEC;
6090 		link = rtnl_get_link(PF_UNSPEC, type);
6091 		if (!link || !link->doit)
6092 			goto out_unlock;
6093 	}
6094 
6095 	owner = link->owner;
6096 	if (!try_module_get(owner)) {
6097 		err = -EPROTONOSUPPORT;
6098 		goto out_unlock;
6099 	}
6100 
6101 	flags = link->flags;
6102 	if (kind == RTNL_KIND_DEL && (nlh->nlmsg_flags & NLM_F_BULK) &&
6103 	    !(flags & RTNL_FLAG_BULK_DEL_SUPPORTED)) {
6104 		NL_SET_ERR_MSG(extack, "Bulk delete is not supported");
6105 		module_put(owner);
6106 		goto err_unlock;
6107 	}
6108 
6109 	if (flags & RTNL_FLAG_DOIT_UNLOCKED) {
6110 		doit = link->doit;
6111 		rcu_read_unlock();
6112 		if (doit)
6113 			err = doit(skb, nlh, extack);
6114 		module_put(owner);
6115 		return err;
6116 	}
6117 	rcu_read_unlock();
6118 
6119 	rtnl_lock();
6120 	link = rtnl_get_link(family, type);
6121 	if (link && link->doit)
6122 		err = link->doit(skb, nlh, extack);
6123 	rtnl_unlock();
6124 
6125 	module_put(owner);
6126 
6127 	return err;
6128 
6129 out_unlock:
6130 	rcu_read_unlock();
6131 	return err;
6132 
6133 err_unlock:
6134 	rcu_read_unlock();
6135 	return -EOPNOTSUPP;
6136 }
6137 
rtnetlink_rcv(struct sk_buff * skb)6138 static void rtnetlink_rcv(struct sk_buff *skb)
6139 {
6140 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
6141 }
6142 
rtnetlink_bind(struct net * net,int group)6143 static int rtnetlink_bind(struct net *net, int group)
6144 {
6145 	switch (group) {
6146 	case RTNLGRP_IPV4_MROUTE_R:
6147 	case RTNLGRP_IPV6_MROUTE_R:
6148 		if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
6149 			return -EPERM;
6150 		break;
6151 	}
6152 	return 0;
6153 }
6154 
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)6155 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
6156 {
6157 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
6158 
6159 	switch (event) {
6160 	case NETDEV_REBOOT:
6161 	case NETDEV_CHANGEMTU:
6162 	case NETDEV_CHANGEADDR:
6163 	case NETDEV_CHANGENAME:
6164 	case NETDEV_FEAT_CHANGE:
6165 	case NETDEV_BONDING_FAILOVER:
6166 	case NETDEV_POST_TYPE_CHANGE:
6167 	case NETDEV_NOTIFY_PEERS:
6168 	case NETDEV_CHANGEUPPER:
6169 	case NETDEV_RESEND_IGMP:
6170 	case NETDEV_CHANGEINFODATA:
6171 	case NETDEV_CHANGELOWERSTATE:
6172 	case NETDEV_CHANGE_TX_QUEUE_LEN:
6173 		rtmsg_ifinfo_event(RTM_NEWLINK, dev, 0, rtnl_get_event(event),
6174 				   GFP_KERNEL, NULL, 0);
6175 		break;
6176 	default:
6177 		break;
6178 	}
6179 	return NOTIFY_DONE;
6180 }
6181 
6182 static struct notifier_block rtnetlink_dev_notifier = {
6183 	.notifier_call	= rtnetlink_event,
6184 };
6185 
6186 
rtnetlink_net_init(struct net * net)6187 static int __net_init rtnetlink_net_init(struct net *net)
6188 {
6189 	struct sock *sk;
6190 	struct netlink_kernel_cfg cfg = {
6191 		.groups		= RTNLGRP_MAX,
6192 		.input		= rtnetlink_rcv,
6193 		.cb_mutex	= &rtnl_mutex,
6194 		.flags		= NL_CFG_F_NONROOT_RECV,
6195 		.bind		= rtnetlink_bind,
6196 	};
6197 
6198 	sk = netlink_kernel_create(net, NETLINK_ROUTE, &cfg);
6199 	if (!sk)
6200 		return -ENOMEM;
6201 	net->rtnl = sk;
6202 	return 0;
6203 }
6204 
rtnetlink_net_exit(struct net * net)6205 static void __net_exit rtnetlink_net_exit(struct net *net)
6206 {
6207 	netlink_kernel_release(net->rtnl);
6208 	net->rtnl = NULL;
6209 }
6210 
6211 static struct pernet_operations rtnetlink_net_ops = {
6212 	.init = rtnetlink_net_init,
6213 	.exit = rtnetlink_net_exit,
6214 };
6215 
rtnetlink_init(void)6216 void __init rtnetlink_init(void)
6217 {
6218 	if (register_pernet_subsys(&rtnetlink_net_ops))
6219 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
6220 
6221 	register_netdevice_notifier(&rtnetlink_dev_notifier);
6222 
6223 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
6224 		      rtnl_dump_ifinfo, 0);
6225 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, 0);
6226 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, 0);
6227 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, 0);
6228 
6229 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, 0);
6230 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, 0);
6231 	rtnl_register(PF_UNSPEC, RTM_GETNETCONF, NULL, rtnl_dump_all, 0);
6232 
6233 	rtnl_register(PF_UNSPEC, RTM_NEWLINKPROP, rtnl_newlinkprop, NULL, 0);
6234 	rtnl_register(PF_UNSPEC, RTM_DELLINKPROP, rtnl_dellinkprop, NULL, 0);
6235 
6236 	rtnl_register(PF_BRIDGE, RTM_NEWNEIGH, rtnl_fdb_add, NULL, 0);
6237 	rtnl_register(PF_BRIDGE, RTM_DELNEIGH, rtnl_fdb_del, NULL,
6238 		      RTNL_FLAG_BULK_DEL_SUPPORTED);
6239 	rtnl_register(PF_BRIDGE, RTM_GETNEIGH, rtnl_fdb_get, rtnl_fdb_dump, 0);
6240 
6241 	rtnl_register(PF_BRIDGE, RTM_GETLINK, NULL, rtnl_bridge_getlink, 0);
6242 	rtnl_register(PF_BRIDGE, RTM_DELLINK, rtnl_bridge_dellink, NULL, 0);
6243 	rtnl_register(PF_BRIDGE, RTM_SETLINK, rtnl_bridge_setlink, NULL, 0);
6244 
6245 	rtnl_register(PF_UNSPEC, RTM_GETSTATS, rtnl_stats_get, rtnl_stats_dump,
6246 		      0);
6247 	rtnl_register(PF_UNSPEC, RTM_SETSTATS, rtnl_stats_set, NULL, 0);
6248 }
6249