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