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