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