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