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/pci.h>
39 
40 #include <asm/uaccess.h>
41 
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <net/ip.h>
45 #include <net/protocol.h>
46 #include <net/arp.h>
47 #include <net/route.h>
48 #include <net/udp.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 #include <net/fib_rules.h>
52 #include <net/rtnetlink.h>
53 #include <net/net_namespace.h>
54 
55 struct rtnl_link {
56 	rtnl_doit_func		doit;
57 	rtnl_dumpit_func	dumpit;
58 	rtnl_calcit_func 	calcit;
59 };
60 
61 static DEFINE_MUTEX(rtnl_mutex);
62 
rtnl_lock(void)63 void rtnl_lock(void)
64 {
65 	mutex_lock(&rtnl_mutex);
66 }
67 EXPORT_SYMBOL(rtnl_lock);
68 
__rtnl_unlock(void)69 void __rtnl_unlock(void)
70 {
71 	mutex_unlock(&rtnl_mutex);
72 }
73 
rtnl_unlock(void)74 void rtnl_unlock(void)
75 {
76 	/* This fellow will unlock it for us. */
77 	netdev_run_todo();
78 }
79 EXPORT_SYMBOL(rtnl_unlock);
80 
rtnl_trylock(void)81 int rtnl_trylock(void)
82 {
83 	return mutex_trylock(&rtnl_mutex);
84 }
85 EXPORT_SYMBOL(rtnl_trylock);
86 
rtnl_is_locked(void)87 int rtnl_is_locked(void)
88 {
89 	return mutex_is_locked(&rtnl_mutex);
90 }
91 EXPORT_SYMBOL(rtnl_is_locked);
92 
93 #ifdef CONFIG_PROVE_LOCKING
lockdep_rtnl_is_held(void)94 int lockdep_rtnl_is_held(void)
95 {
96 	return lockdep_is_held(&rtnl_mutex);
97 }
98 EXPORT_SYMBOL(lockdep_rtnl_is_held);
99 #endif /* #ifdef CONFIG_PROVE_LOCKING */
100 
101 static struct rtnl_link *rtnl_msg_handlers[RTNL_FAMILY_MAX + 1];
102 
rtm_msgindex(int msgtype)103 static inline int rtm_msgindex(int msgtype)
104 {
105 	int msgindex = msgtype - RTM_BASE;
106 
107 	/*
108 	 * msgindex < 0 implies someone tried to register a netlink
109 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
110 	 * the message type has not been added to linux/rtnetlink.h
111 	 */
112 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
113 
114 	return msgindex;
115 }
116 
rtnl_get_doit(int protocol,int msgindex)117 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
118 {
119 	struct rtnl_link *tab;
120 
121 	if (protocol <= RTNL_FAMILY_MAX)
122 		tab = rtnl_msg_handlers[protocol];
123 	else
124 		tab = NULL;
125 
126 	if (tab == NULL || tab[msgindex].doit == NULL)
127 		tab = rtnl_msg_handlers[PF_UNSPEC];
128 
129 	return tab ? tab[msgindex].doit : NULL;
130 }
131 
rtnl_get_dumpit(int protocol,int msgindex)132 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
133 {
134 	struct rtnl_link *tab;
135 
136 	if (protocol <= RTNL_FAMILY_MAX)
137 		tab = rtnl_msg_handlers[protocol];
138 	else
139 		tab = NULL;
140 
141 	if (tab == NULL || tab[msgindex].dumpit == NULL)
142 		tab = rtnl_msg_handlers[PF_UNSPEC];
143 
144 	return tab ? tab[msgindex].dumpit : NULL;
145 }
146 
rtnl_get_calcit(int protocol,int msgindex)147 static rtnl_calcit_func rtnl_get_calcit(int protocol, int msgindex)
148 {
149 	struct rtnl_link *tab;
150 
151 	if (protocol <= RTNL_FAMILY_MAX)
152 		tab = rtnl_msg_handlers[protocol];
153 	else
154 		tab = NULL;
155 
156 	if (tab == NULL || tab[msgindex].calcit == NULL)
157 		tab = rtnl_msg_handlers[PF_UNSPEC];
158 
159 	return tab ? tab[msgindex].calcit : NULL;
160 }
161 
162 /**
163  * __rtnl_register - Register a rtnetlink message type
164  * @protocol: Protocol family or PF_UNSPEC
165  * @msgtype: rtnetlink message type
166  * @doit: Function pointer called for each request message
167  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
168  * @calcit: Function pointer to calc size of dump message
169  *
170  * Registers the specified function pointers (at least one of them has
171  * to be non-NULL) to be called whenever a request message for the
172  * specified protocol family and message type is received.
173  *
174  * The special protocol family PF_UNSPEC may be used to define fallback
175  * function pointers for the case when no entry for the specific protocol
176  * family exists.
177  *
178  * Returns 0 on success or a negative error code.
179  */
__rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,rtnl_calcit_func calcit)180 int __rtnl_register(int protocol, int msgtype,
181 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit,
182 		    rtnl_calcit_func calcit)
183 {
184 	struct rtnl_link *tab;
185 	int msgindex;
186 
187 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
188 	msgindex = rtm_msgindex(msgtype);
189 
190 	tab = rtnl_msg_handlers[protocol];
191 	if (tab == NULL) {
192 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
193 		if (tab == NULL)
194 			return -ENOBUFS;
195 
196 		rtnl_msg_handlers[protocol] = tab;
197 	}
198 
199 	if (doit)
200 		tab[msgindex].doit = doit;
201 
202 	if (dumpit)
203 		tab[msgindex].dumpit = dumpit;
204 
205 	if (calcit)
206 		tab[msgindex].calcit = calcit;
207 
208 	return 0;
209 }
210 EXPORT_SYMBOL_GPL(__rtnl_register);
211 
212 /**
213  * rtnl_register - Register a rtnetlink message type
214  *
215  * Identical to __rtnl_register() but panics on failure. This is useful
216  * as failure of this function is very unlikely, it can only happen due
217  * to lack of memory when allocating the chain to store all message
218  * handlers for a protocol. Meant for use in init functions where lack
219  * of memory implies no sense in continuing.
220  */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit,rtnl_calcit_func calcit)221 void rtnl_register(int protocol, int msgtype,
222 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit,
223 		   rtnl_calcit_func calcit)
224 {
225 	if (__rtnl_register(protocol, msgtype, doit, dumpit, calcit) < 0)
226 		panic("Unable to register rtnetlink message handler, "
227 		      "protocol = %d, message type = %d\n",
228 		      protocol, msgtype);
229 }
230 EXPORT_SYMBOL_GPL(rtnl_register);
231 
232 /**
233  * rtnl_unregister - Unregister a rtnetlink message type
234  * @protocol: Protocol family or PF_UNSPEC
235  * @msgtype: rtnetlink message type
236  *
237  * Returns 0 on success or a negative error code.
238  */
rtnl_unregister(int protocol,int msgtype)239 int rtnl_unregister(int protocol, int msgtype)
240 {
241 	int msgindex;
242 
243 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
244 	msgindex = rtm_msgindex(msgtype);
245 
246 	if (rtnl_msg_handlers[protocol] == NULL)
247 		return -ENOENT;
248 
249 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
250 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
251 
252 	return 0;
253 }
254 EXPORT_SYMBOL_GPL(rtnl_unregister);
255 
256 /**
257  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
258  * @protocol : Protocol family or PF_UNSPEC
259  *
260  * Identical to calling rtnl_unregster() for all registered message types
261  * of a certain protocol family.
262  */
rtnl_unregister_all(int protocol)263 void rtnl_unregister_all(int protocol)
264 {
265 	BUG_ON(protocol < 0 || protocol > RTNL_FAMILY_MAX);
266 
267 	kfree(rtnl_msg_handlers[protocol]);
268 	rtnl_msg_handlers[protocol] = NULL;
269 }
270 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
271 
272 static LIST_HEAD(link_ops);
273 
rtnl_link_ops_get(const char * kind)274 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
275 {
276 	const struct rtnl_link_ops *ops;
277 
278 	list_for_each_entry(ops, &link_ops, list) {
279 		if (!strcmp(ops->kind, kind))
280 			return ops;
281 	}
282 	return NULL;
283 }
284 
285 /**
286  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
287  * @ops: struct rtnl_link_ops * to register
288  *
289  * The caller must hold the rtnl_mutex. This function should be used
290  * by drivers that create devices during module initialization. It
291  * must be called before registering the devices.
292  *
293  * Returns 0 on success or a negative error code.
294  */
__rtnl_link_register(struct rtnl_link_ops * ops)295 int __rtnl_link_register(struct rtnl_link_ops *ops)
296 {
297 	if (rtnl_link_ops_get(ops->kind))
298 		return -EEXIST;
299 
300 	if (!ops->dellink)
301 		ops->dellink = unregister_netdevice_queue;
302 
303 	list_add_tail(&ops->list, &link_ops);
304 	return 0;
305 }
306 EXPORT_SYMBOL_GPL(__rtnl_link_register);
307 
308 /**
309  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
310  * @ops: struct rtnl_link_ops * to register
311  *
312  * Returns 0 on success or a negative error code.
313  */
rtnl_link_register(struct rtnl_link_ops * ops)314 int rtnl_link_register(struct rtnl_link_ops *ops)
315 {
316 	int err;
317 
318 	rtnl_lock();
319 	err = __rtnl_link_register(ops);
320 	rtnl_unlock();
321 	return err;
322 }
323 EXPORT_SYMBOL_GPL(rtnl_link_register);
324 
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)325 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
326 {
327 	struct net_device *dev;
328 	LIST_HEAD(list_kill);
329 
330 	for_each_netdev(net, dev) {
331 		if (dev->rtnl_link_ops == ops)
332 			ops->dellink(dev, &list_kill);
333 	}
334 	unregister_netdevice_many(&list_kill);
335 }
336 
337 /**
338  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
339  * @ops: struct rtnl_link_ops * to unregister
340  *
341  * The caller must hold the rtnl_mutex.
342  */
__rtnl_link_unregister(struct rtnl_link_ops * ops)343 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
344 {
345 	struct net *net;
346 
347 	for_each_net(net) {
348 		__rtnl_kill_links(net, ops);
349 	}
350 	list_del(&ops->list);
351 }
352 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
353 
354 /**
355  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
356  * @ops: struct rtnl_link_ops * to unregister
357  */
rtnl_link_unregister(struct rtnl_link_ops * ops)358 void rtnl_link_unregister(struct rtnl_link_ops *ops)
359 {
360 	rtnl_lock();
361 	__rtnl_link_unregister(ops);
362 	rtnl_unlock();
363 }
364 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
365 
rtnl_link_get_size(const struct net_device * dev)366 static size_t rtnl_link_get_size(const struct net_device *dev)
367 {
368 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
369 	size_t size;
370 
371 	if (!ops)
372 		return 0;
373 
374 	size = nla_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
375 	       nla_total_size(strlen(ops->kind) + 1);  /* IFLA_INFO_KIND */
376 
377 	if (ops->get_size)
378 		/* IFLA_INFO_DATA + nested data */
379 		size += nla_total_size(sizeof(struct nlattr)) +
380 			ops->get_size(dev);
381 
382 	if (ops->get_xstats_size)
383 		/* IFLA_INFO_XSTATS */
384 		size += nla_total_size(ops->get_xstats_size(dev));
385 
386 	return size;
387 }
388 
389 static LIST_HEAD(rtnl_af_ops);
390 
rtnl_af_lookup(const int family)391 static const struct rtnl_af_ops *rtnl_af_lookup(const int family)
392 {
393 	const struct rtnl_af_ops *ops;
394 
395 	list_for_each_entry(ops, &rtnl_af_ops, list) {
396 		if (ops->family == family)
397 			return ops;
398 	}
399 
400 	return NULL;
401 }
402 
403 /**
404  * __rtnl_af_register - Register rtnl_af_ops with rtnetlink.
405  * @ops: struct rtnl_af_ops * to register
406  *
407  * The caller must hold the rtnl_mutex.
408  *
409  * Returns 0 on success or a negative error code.
410  */
__rtnl_af_register(struct rtnl_af_ops * ops)411 int __rtnl_af_register(struct rtnl_af_ops *ops)
412 {
413 	list_add_tail(&ops->list, &rtnl_af_ops);
414 	return 0;
415 }
416 EXPORT_SYMBOL_GPL(__rtnl_af_register);
417 
418 /**
419  * rtnl_af_register - Register rtnl_af_ops with rtnetlink.
420  * @ops: struct rtnl_af_ops * to register
421  *
422  * Returns 0 on success or a negative error code.
423  */
rtnl_af_register(struct rtnl_af_ops * ops)424 int rtnl_af_register(struct rtnl_af_ops *ops)
425 {
426 	int err;
427 
428 	rtnl_lock();
429 	err = __rtnl_af_register(ops);
430 	rtnl_unlock();
431 	return err;
432 }
433 EXPORT_SYMBOL_GPL(rtnl_af_register);
434 
435 /**
436  * __rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
437  * @ops: struct rtnl_af_ops * to unregister
438  *
439  * The caller must hold the rtnl_mutex.
440  */
__rtnl_af_unregister(struct rtnl_af_ops * ops)441 void __rtnl_af_unregister(struct rtnl_af_ops *ops)
442 {
443 	list_del(&ops->list);
444 }
445 EXPORT_SYMBOL_GPL(__rtnl_af_unregister);
446 
447 /**
448  * rtnl_af_unregister - Unregister rtnl_af_ops from rtnetlink.
449  * @ops: struct rtnl_af_ops * to unregister
450  */
rtnl_af_unregister(struct rtnl_af_ops * ops)451 void rtnl_af_unregister(struct rtnl_af_ops *ops)
452 {
453 	rtnl_lock();
454 	__rtnl_af_unregister(ops);
455 	rtnl_unlock();
456 }
457 EXPORT_SYMBOL_GPL(rtnl_af_unregister);
458 
rtnl_link_get_af_size(const struct net_device * dev)459 static size_t rtnl_link_get_af_size(const struct net_device *dev)
460 {
461 	struct rtnl_af_ops *af_ops;
462 	size_t size;
463 
464 	/* IFLA_AF_SPEC */
465 	size = nla_total_size(sizeof(struct nlattr));
466 
467 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
468 		if (af_ops->get_link_af_size) {
469 			/* AF_* + nested data */
470 			size += nla_total_size(sizeof(struct nlattr)) +
471 				af_ops->get_link_af_size(dev);
472 		}
473 	}
474 
475 	return size;
476 }
477 
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)478 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
479 {
480 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
481 	struct nlattr *linkinfo, *data;
482 	int err = -EMSGSIZE;
483 
484 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
485 	if (linkinfo == NULL)
486 		goto out;
487 
488 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
489 		goto err_cancel_link;
490 	if (ops->fill_xstats) {
491 		err = ops->fill_xstats(skb, dev);
492 		if (err < 0)
493 			goto err_cancel_link;
494 	}
495 	if (ops->fill_info) {
496 		data = nla_nest_start(skb, IFLA_INFO_DATA);
497 		if (data == NULL)
498 			goto err_cancel_link;
499 		err = ops->fill_info(skb, dev);
500 		if (err < 0)
501 			goto err_cancel_data;
502 		nla_nest_end(skb, data);
503 	}
504 
505 	nla_nest_end(skb, linkinfo);
506 	return 0;
507 
508 err_cancel_data:
509 	nla_nest_cancel(skb, data);
510 err_cancel_link:
511 	nla_nest_cancel(skb, linkinfo);
512 out:
513 	return err;
514 }
515 
516 static const int rtm_min[RTM_NR_FAMILIES] =
517 {
518 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
519 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
520 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
521 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
522 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
523 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
524 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
525 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
526 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
527 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
528 };
529 
530 static const int rta_max[RTM_NR_FAMILIES] =
531 {
532 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
533 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
534 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
535 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
536 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
537 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
538 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
539 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
540 };
541 
__rta_fill(struct sk_buff * skb,int attrtype,int attrlen,const void * data)542 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
543 {
544 	struct rtattr *rta;
545 	int size = RTA_LENGTH(attrlen);
546 
547 	rta = (struct rtattr *)skb_put(skb, RTA_ALIGN(size));
548 	rta->rta_type = attrtype;
549 	rta->rta_len = size;
550 	memcpy(RTA_DATA(rta), data, attrlen);
551 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
552 }
553 EXPORT_SYMBOL(__rta_fill);
554 
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned group,int echo)555 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
556 {
557 	struct sock *rtnl = net->rtnl;
558 	int err = 0;
559 
560 	NETLINK_CB(skb).dst_group = group;
561 	if (echo)
562 		atomic_inc(&skb->users);
563 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
564 	if (echo)
565 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
566 	return err;
567 }
568 
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)569 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
570 {
571 	struct sock *rtnl = net->rtnl;
572 
573 	return nlmsg_unicast(rtnl, skb, pid);
574 }
575 EXPORT_SYMBOL(rtnl_unicast);
576 
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,struct nlmsghdr * nlh,gfp_t flags)577 void rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
578 		 struct nlmsghdr *nlh, gfp_t flags)
579 {
580 	struct sock *rtnl = net->rtnl;
581 	int report = 0;
582 
583 	if (nlh)
584 		report = nlmsg_report(nlh);
585 
586 	nlmsg_notify(rtnl, skb, pid, group, report, flags);
587 }
588 EXPORT_SYMBOL(rtnl_notify);
589 
rtnl_set_sk_err(struct net * net,u32 group,int error)590 void rtnl_set_sk_err(struct net *net, u32 group, int error)
591 {
592 	struct sock *rtnl = net->rtnl;
593 
594 	netlink_set_err(rtnl, 0, group, error);
595 }
596 EXPORT_SYMBOL(rtnl_set_sk_err);
597 
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)598 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
599 {
600 	struct nlattr *mx;
601 	int i, valid = 0;
602 
603 	mx = nla_nest_start(skb, RTA_METRICS);
604 	if (mx == NULL)
605 		return -ENOBUFS;
606 
607 	for (i = 0; i < RTAX_MAX; i++) {
608 		if (metrics[i]) {
609 			valid++;
610 			NLA_PUT_U32(skb, i+1, metrics[i]);
611 		}
612 	}
613 
614 	if (!valid) {
615 		nla_nest_cancel(skb, mx);
616 		return 0;
617 	}
618 
619 	return nla_nest_end(skb, mx);
620 
621 nla_put_failure:
622 	nla_nest_cancel(skb, mx);
623 	return -EMSGSIZE;
624 }
625 EXPORT_SYMBOL(rtnetlink_put_metrics);
626 
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,u32 ts,u32 tsage,long expires,u32 error)627 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
628 		       u32 ts, u32 tsage, long expires, u32 error)
629 {
630 	struct rta_cacheinfo ci = {
631 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
632 		.rta_used = dst->__use,
633 		.rta_clntref = atomic_read(&(dst->__refcnt)),
634 		.rta_error = error,
635 		.rta_id =  id,
636 		.rta_ts = ts,
637 		.rta_tsage = tsage,
638 	};
639 
640 	if (expires)
641 		ci.rta_expires = jiffies_to_clock_t(expires);
642 
643 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
644 }
645 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
646 
set_operstate(struct net_device * dev,unsigned char transition)647 static void set_operstate(struct net_device *dev, unsigned char transition)
648 {
649 	unsigned char operstate = dev->operstate;
650 
651 	switch (transition) {
652 	case IF_OPER_UP:
653 		if ((operstate == IF_OPER_DORMANT ||
654 		     operstate == IF_OPER_UNKNOWN) &&
655 		    !netif_dormant(dev))
656 			operstate = IF_OPER_UP;
657 		break;
658 
659 	case IF_OPER_DORMANT:
660 		if (operstate == IF_OPER_UP ||
661 		    operstate == IF_OPER_UNKNOWN)
662 			operstate = IF_OPER_DORMANT;
663 		break;
664 	}
665 
666 	if (dev->operstate != operstate) {
667 		write_lock_bh(&dev_base_lock);
668 		dev->operstate = operstate;
669 		write_unlock_bh(&dev_base_lock);
670 		netdev_state_change(dev);
671 	}
672 }
673 
rtnl_dev_get_flags(const struct net_device * dev)674 static unsigned int rtnl_dev_get_flags(const struct net_device *dev)
675 {
676 	return (dev->flags & ~(IFF_PROMISC | IFF_ALLMULTI)) |
677 	       (dev->gflags & (IFF_PROMISC | IFF_ALLMULTI));
678 }
679 
rtnl_dev_combine_flags(const struct net_device * dev,const struct ifinfomsg * ifm)680 static unsigned int rtnl_dev_combine_flags(const struct net_device *dev,
681 					   const struct ifinfomsg *ifm)
682 {
683 	unsigned int flags = ifm->ifi_flags;
684 
685 	/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
686 	if (ifm->ifi_change)
687 		flags = (flags & ifm->ifi_change) |
688 			(rtnl_dev_get_flags(dev) & ~ifm->ifi_change);
689 
690 	return flags;
691 }
692 
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct rtnl_link_stats64 * b)693 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
694 				 const struct rtnl_link_stats64 *b)
695 {
696 	a->rx_packets = b->rx_packets;
697 	a->tx_packets = b->tx_packets;
698 	a->rx_bytes = b->rx_bytes;
699 	a->tx_bytes = b->tx_bytes;
700 	a->rx_errors = b->rx_errors;
701 	a->tx_errors = b->tx_errors;
702 	a->rx_dropped = b->rx_dropped;
703 	a->tx_dropped = b->tx_dropped;
704 
705 	a->multicast = b->multicast;
706 	a->collisions = b->collisions;
707 
708 	a->rx_length_errors = b->rx_length_errors;
709 	a->rx_over_errors = b->rx_over_errors;
710 	a->rx_crc_errors = b->rx_crc_errors;
711 	a->rx_frame_errors = b->rx_frame_errors;
712 	a->rx_fifo_errors = b->rx_fifo_errors;
713 	a->rx_missed_errors = b->rx_missed_errors;
714 
715 	a->tx_aborted_errors = b->tx_aborted_errors;
716 	a->tx_carrier_errors = b->tx_carrier_errors;
717 	a->tx_fifo_errors = b->tx_fifo_errors;
718 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
719 	a->tx_window_errors = b->tx_window_errors;
720 
721 	a->rx_compressed = b->rx_compressed;
722 	a->tx_compressed = b->tx_compressed;
723 }
724 
copy_rtnl_link_stats64(void * v,const struct rtnl_link_stats64 * b)725 static void copy_rtnl_link_stats64(void *v, const struct rtnl_link_stats64 *b)
726 {
727 	memcpy(v, b, sizeof(*b));
728 }
729 
730 /* All VF info */
rtnl_vfinfo_size(const struct net_device * dev,u32 ext_filter_mask)731 static inline int rtnl_vfinfo_size(const struct net_device *dev,
732 				   u32 ext_filter_mask)
733 {
734 	if (dev->dev.parent && dev_is_pci(dev->dev.parent) &&
735 	    (ext_filter_mask & RTEXT_FILTER_VF)) {
736 		int num_vfs = dev_num_vf(dev->dev.parent);
737 		size_t size = nla_total_size(sizeof(struct nlattr));
738 		size += nla_total_size(num_vfs * sizeof(struct nlattr));
739 		size += num_vfs *
740 			(nla_total_size(sizeof(struct ifla_vf_mac)) +
741 			 nla_total_size(sizeof(struct ifla_vf_vlan)) +
742 			 nla_total_size(sizeof(struct ifla_vf_tx_rate)) +
743 			 nla_total_size(sizeof(struct ifla_vf_spoofchk)));
744 		return size;
745 	} else
746 		return 0;
747 }
748 
rtnl_port_size(const struct net_device * dev,u32 ext_filter_mask)749 static size_t rtnl_port_size(const struct net_device *dev,
750 			     u32 ext_filter_mask)
751 {
752 	size_t port_size = nla_total_size(4)		/* PORT_VF */
753 		+ nla_total_size(PORT_PROFILE_MAX)	/* PORT_PROFILE */
754 		+ nla_total_size(sizeof(struct ifla_port_vsi))
755 							/* PORT_VSI_TYPE */
756 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_INSTANCE_UUID */
757 		+ nla_total_size(PORT_UUID_MAX)		/* PORT_HOST_UUID */
758 		+ nla_total_size(1)			/* PROT_VDP_REQUEST */
759 		+ nla_total_size(2);			/* PORT_VDP_RESPONSE */
760 	size_t vf_ports_size = nla_total_size(sizeof(struct nlattr));
761 	size_t vf_port_size = nla_total_size(sizeof(struct nlattr))
762 		+ port_size;
763 	size_t port_self_size = nla_total_size(sizeof(struct nlattr))
764 		+ port_size;
765 
766 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
767 	    !(ext_filter_mask & RTEXT_FILTER_VF))
768 		return 0;
769 	if (dev_num_vf(dev->dev.parent))
770 		return port_self_size + vf_ports_size +
771 			vf_port_size * dev_num_vf(dev->dev.parent);
772 	else
773 		return port_self_size;
774 }
775 
if_nlmsg_size(const struct net_device * dev,u32 ext_filter_mask)776 static noinline size_t if_nlmsg_size(const struct net_device *dev,
777 				     u32 ext_filter_mask)
778 {
779 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
780 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
781 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
782 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
783 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
784 	       + nla_total_size(sizeof(struct rtnl_link_stats))
785 	       + nla_total_size(sizeof(struct rtnl_link_stats64))
786 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
787 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
788 	       + nla_total_size(4) /* IFLA_TXQLEN */
789 	       + nla_total_size(4) /* IFLA_WEIGHT */
790 	       + nla_total_size(4) /* IFLA_MTU */
791 	       + nla_total_size(4) /* IFLA_LINK */
792 	       + nla_total_size(4) /* IFLA_MASTER */
793 	       + nla_total_size(1) /* IFLA_OPERSTATE */
794 	       + nla_total_size(1) /* IFLA_LINKMODE */
795 	       + nla_total_size(ext_filter_mask
796 			        & RTEXT_FILTER_VF ? 4 : 0) /* IFLA_NUM_VF */
797 	       + rtnl_vfinfo_size(dev, ext_filter_mask) /* IFLA_VFINFO_LIST */
798 	       + rtnl_port_size(dev, ext_filter_mask) /* IFLA_VF_PORTS + IFLA_PORT_SELF */
799 	       + rtnl_link_get_size(dev) /* IFLA_LINKINFO */
800 	       + rtnl_link_get_af_size(dev); /* IFLA_AF_SPEC */
801 }
802 
rtnl_vf_ports_fill(struct sk_buff * skb,struct net_device * dev)803 static int rtnl_vf_ports_fill(struct sk_buff *skb, struct net_device *dev)
804 {
805 	struct nlattr *vf_ports;
806 	struct nlattr *vf_port;
807 	int vf;
808 	int err;
809 
810 	vf_ports = nla_nest_start(skb, IFLA_VF_PORTS);
811 	if (!vf_ports)
812 		return -EMSGSIZE;
813 
814 	for (vf = 0; vf < dev_num_vf(dev->dev.parent); vf++) {
815 		vf_port = nla_nest_start(skb, IFLA_VF_PORT);
816 		if (!vf_port)
817 			goto nla_put_failure;
818 		NLA_PUT_U32(skb, IFLA_PORT_VF, vf);
819 		err = dev->netdev_ops->ndo_get_vf_port(dev, vf, skb);
820 		if (err == -EMSGSIZE)
821 			goto nla_put_failure;
822 		if (err) {
823 			nla_nest_cancel(skb, vf_port);
824 			continue;
825 		}
826 		nla_nest_end(skb, vf_port);
827 	}
828 
829 	nla_nest_end(skb, vf_ports);
830 
831 	return 0;
832 
833 nla_put_failure:
834 	nla_nest_cancel(skb, vf_ports);
835 	return -EMSGSIZE;
836 }
837 
rtnl_port_self_fill(struct sk_buff * skb,struct net_device * dev)838 static int rtnl_port_self_fill(struct sk_buff *skb, struct net_device *dev)
839 {
840 	struct nlattr *port_self;
841 	int err;
842 
843 	port_self = nla_nest_start(skb, IFLA_PORT_SELF);
844 	if (!port_self)
845 		return -EMSGSIZE;
846 
847 	err = dev->netdev_ops->ndo_get_vf_port(dev, PORT_SELF_VF, skb);
848 	if (err) {
849 		nla_nest_cancel(skb, port_self);
850 		return (err == -EMSGSIZE) ? err : 0;
851 	}
852 
853 	nla_nest_end(skb, port_self);
854 
855 	return 0;
856 }
857 
rtnl_port_fill(struct sk_buff * skb,struct net_device * dev,u32 ext_filter_mask)858 static int rtnl_port_fill(struct sk_buff *skb, struct net_device *dev,
859 			  u32 ext_filter_mask)
860 {
861 	int err;
862 
863 	if (!dev->netdev_ops->ndo_get_vf_port || !dev->dev.parent ||
864 	    !(ext_filter_mask & RTEXT_FILTER_VF))
865 		return 0;
866 
867 	err = rtnl_port_self_fill(skb, dev);
868 	if (err)
869 		return err;
870 
871 	if (dev_num_vf(dev->dev.parent)) {
872 		err = rtnl_vf_ports_fill(skb, dev);
873 		if (err)
874 			return err;
875 	}
876 
877 	return 0;
878 }
879 
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)880 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
881 			    int type, u32 pid, u32 seq, u32 change,
882 			    unsigned int flags, u32 ext_filter_mask)
883 {
884 	struct ifinfomsg *ifm;
885 	struct nlmsghdr *nlh;
886 	struct rtnl_link_stats64 temp;
887 	const struct rtnl_link_stats64 *stats;
888 	struct nlattr *attr, *af_spec;
889 	struct rtnl_af_ops *af_ops;
890 
891 	ASSERT_RTNL();
892 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
893 	if (nlh == NULL)
894 		return -EMSGSIZE;
895 
896 	ifm = nlmsg_data(nlh);
897 	ifm->ifi_family = AF_UNSPEC;
898 	ifm->__ifi_pad = 0;
899 	ifm->ifi_type = dev->type;
900 	ifm->ifi_index = dev->ifindex;
901 	ifm->ifi_flags = dev_get_flags(dev);
902 	ifm->ifi_change = change;
903 
904 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
905 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
906 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
907 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
908 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
909 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
910 	NLA_PUT_U32(skb, IFLA_GROUP, dev->group);
911 
912 	if (dev->ifindex != dev->iflink)
913 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
914 
915 	if (dev->master)
916 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
917 
918 	if (dev->qdisc)
919 		NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc->ops->id);
920 
921 	if (dev->ifalias)
922 		NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
923 
924 	if (1) {
925 		struct rtnl_link_ifmap map = {
926 			.mem_start   = dev->mem_start,
927 			.mem_end     = dev->mem_end,
928 			.base_addr   = dev->base_addr,
929 			.irq         = dev->irq,
930 			.dma         = dev->dma,
931 			.port        = dev->if_port,
932 		};
933 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
934 	}
935 
936 	if (dev->addr_len) {
937 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
938 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
939 	}
940 
941 	attr = nla_reserve(skb, IFLA_STATS,
942 			sizeof(struct rtnl_link_stats));
943 	if (attr == NULL)
944 		goto nla_put_failure;
945 
946 	stats = dev_get_stats(dev, &temp);
947 	copy_rtnl_link_stats(nla_data(attr), stats);
948 
949 	attr = nla_reserve(skb, IFLA_STATS64,
950 			sizeof(struct rtnl_link_stats64));
951 	if (attr == NULL)
952 		goto nla_put_failure;
953 	copy_rtnl_link_stats64(nla_data(attr), stats);
954 
955 	if (dev->dev.parent && (ext_filter_mask & RTEXT_FILTER_VF))
956 		NLA_PUT_U32(skb, IFLA_NUM_VF, dev_num_vf(dev->dev.parent));
957 
958 	if (dev->netdev_ops->ndo_get_vf_config && dev->dev.parent
959 	    && (ext_filter_mask & RTEXT_FILTER_VF)) {
960 		int i;
961 
962 		struct nlattr *vfinfo, *vf;
963 		int num_vfs = dev_num_vf(dev->dev.parent);
964 
965 		vfinfo = nla_nest_start(skb, IFLA_VFINFO_LIST);
966 		if (!vfinfo)
967 			goto nla_put_failure;
968 		for (i = 0; i < num_vfs; i++) {
969 			struct ifla_vf_info ivi;
970 			struct ifla_vf_mac vf_mac;
971 			struct ifla_vf_vlan vf_vlan;
972 			struct ifla_vf_tx_rate vf_tx_rate;
973 			struct ifla_vf_spoofchk vf_spoofchk;
974 
975 			/*
976 			 * Not all SR-IOV capable drivers support the
977 			 * spoofcheck query.  Preset to -1 so the user
978 			 * space tool can detect that the driver didn't
979 			 * report anything.
980 			 */
981 			ivi.spoofchk = -1;
982 			memset(ivi.mac, 0, sizeof(ivi.mac));
983 			if (dev->netdev_ops->ndo_get_vf_config(dev, i, &ivi))
984 				break;
985 			vf_mac.vf =
986 				vf_vlan.vf =
987 				vf_tx_rate.vf =
988 				vf_spoofchk.vf = ivi.vf;
989 
990 			memcpy(vf_mac.mac, ivi.mac, sizeof(ivi.mac));
991 			vf_vlan.vlan = ivi.vlan;
992 			vf_vlan.qos = ivi.qos;
993 			vf_tx_rate.rate = ivi.tx_rate;
994 			vf_spoofchk.setting = ivi.spoofchk;
995 			vf = nla_nest_start(skb, IFLA_VF_INFO);
996 			if (!vf) {
997 				nla_nest_cancel(skb, vfinfo);
998 				goto nla_put_failure;
999 			}
1000 			NLA_PUT(skb, IFLA_VF_MAC, sizeof(vf_mac), &vf_mac);
1001 			NLA_PUT(skb, IFLA_VF_VLAN, sizeof(vf_vlan), &vf_vlan);
1002 			NLA_PUT(skb, IFLA_VF_TX_RATE, sizeof(vf_tx_rate),
1003 				&vf_tx_rate);
1004 			NLA_PUT(skb, IFLA_VF_SPOOFCHK, sizeof(vf_spoofchk),
1005 				&vf_spoofchk);
1006 			nla_nest_end(skb, vf);
1007 		}
1008 		nla_nest_end(skb, vfinfo);
1009 	}
1010 
1011 	if (rtnl_port_fill(skb, dev, ext_filter_mask))
1012 		goto nla_put_failure;
1013 
1014 	if (dev->rtnl_link_ops) {
1015 		if (rtnl_link_fill(skb, dev) < 0)
1016 			goto nla_put_failure;
1017 	}
1018 
1019 	if (!(af_spec = nla_nest_start(skb, IFLA_AF_SPEC)))
1020 		goto nla_put_failure;
1021 
1022 	list_for_each_entry(af_ops, &rtnl_af_ops, list) {
1023 		if (af_ops->fill_link_af) {
1024 			struct nlattr *af;
1025 			int err;
1026 
1027 			if (!(af = nla_nest_start(skb, af_ops->family)))
1028 				goto nla_put_failure;
1029 
1030 			err = af_ops->fill_link_af(skb, dev);
1031 
1032 			/*
1033 			 * Caller may return ENODATA to indicate that there
1034 			 * was no data to be dumped. This is not an error, it
1035 			 * means we should trim the attribute header and
1036 			 * continue.
1037 			 */
1038 			if (err == -ENODATA)
1039 				nla_nest_cancel(skb, af);
1040 			else if (err < 0)
1041 				goto nla_put_failure;
1042 
1043 			nla_nest_end(skb, af);
1044 		}
1045 	}
1046 
1047 	nla_nest_end(skb, af_spec);
1048 
1049 	return nlmsg_end(skb, nlh);
1050 
1051 nla_put_failure:
1052 	nlmsg_cancel(skb, nlh);
1053 	return -EMSGSIZE;
1054 }
1055 
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)1056 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
1057 {
1058 	struct net *net = sock_net(skb->sk);
1059 	int h, s_h;
1060 	int idx = 0, s_idx;
1061 	struct net_device *dev;
1062 	struct hlist_head *head;
1063 	struct hlist_node *node;
1064 	struct nlattr *tb[IFLA_MAX+1];
1065 	u32 ext_filter_mask = 0;
1066 	int err;
1067 
1068 	s_h = cb->args[0];
1069 	s_idx = cb->args[1];
1070 
1071 	rcu_read_lock();
1072 	cb->seq = net->dev_base_seq;
1073 
1074 	if (nlmsg_parse(cb->nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1075 			ifla_policy) >= 0) {
1076 
1077 		if (tb[IFLA_EXT_MASK])
1078 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1079 	}
1080 
1081 	for (h = s_h; h < NETDEV_HASHENTRIES; h++, s_idx = 0) {
1082 		idx = 0;
1083 		head = &net->dev_index_head[h];
1084 		hlist_for_each_entry_rcu(dev, node, head, index_hlist) {
1085 			if (idx < s_idx)
1086 				goto cont;
1087 			err = rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
1088 					       NETLINK_CB(cb->skb).pid,
1089 					       cb->nlh->nlmsg_seq, 0,
1090 					       NLM_F_MULTI,
1091 					       ext_filter_mask);
1092 			/* If we ran out of room on the first message,
1093 			 * we're in trouble
1094 			 */
1095 			WARN_ON((err == -EMSGSIZE) && (skb->len == 0));
1096 
1097 			if (err <= 0)
1098 				goto out;
1099 
1100 			nl_dump_check_consistent(cb, nlmsg_hdr(skb));
1101 cont:
1102 			idx++;
1103 		}
1104 	}
1105 out:
1106 	rcu_read_unlock();
1107 	cb->args[1] = idx;
1108 	cb->args[0] = h;
1109 
1110 	return skb->len;
1111 }
1112 
1113 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
1114 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
1115 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1116 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
1117 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
1118 	[IFLA_MTU]		= { .type = NLA_U32 },
1119 	[IFLA_LINK]		= { .type = NLA_U32 },
1120 	[IFLA_MASTER]		= { .type = NLA_U32 },
1121 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
1122 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
1123 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
1124 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
1125 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
1126 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
1127 	[IFLA_NET_NS_FD]	= { .type = NLA_U32 },
1128 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
1129 	[IFLA_VFINFO_LIST]	= {. type = NLA_NESTED },
1130 	[IFLA_VF_PORTS]		= { .type = NLA_NESTED },
1131 	[IFLA_PORT_SELF]	= { .type = NLA_NESTED },
1132 	[IFLA_AF_SPEC]		= { .type = NLA_NESTED },
1133 	[IFLA_EXT_MASK]		= { .type = NLA_U32 },
1134 };
1135 EXPORT_SYMBOL(ifla_policy);
1136 
1137 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
1138 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
1139 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
1140 };
1141 
1142 static const struct nla_policy ifla_vfinfo_policy[IFLA_VF_INFO_MAX+1] = {
1143 	[IFLA_VF_INFO]		= { .type = NLA_NESTED },
1144 };
1145 
1146 static const struct nla_policy ifla_vf_policy[IFLA_VF_MAX+1] = {
1147 	[IFLA_VF_MAC]		= { .type = NLA_BINARY,
1148 				    .len = sizeof(struct ifla_vf_mac) },
1149 	[IFLA_VF_VLAN]		= { .type = NLA_BINARY,
1150 				    .len = sizeof(struct ifla_vf_vlan) },
1151 	[IFLA_VF_TX_RATE]	= { .type = NLA_BINARY,
1152 				    .len = sizeof(struct ifla_vf_tx_rate) },
1153 	[IFLA_VF_SPOOFCHK]	= { .type = NLA_BINARY,
1154 				    .len = sizeof(struct ifla_vf_spoofchk) },
1155 };
1156 
1157 static const struct nla_policy ifla_port_policy[IFLA_PORT_MAX+1] = {
1158 	[IFLA_PORT_VF]		= { .type = NLA_U32 },
1159 	[IFLA_PORT_PROFILE]	= { .type = NLA_STRING,
1160 				    .len = PORT_PROFILE_MAX },
1161 	[IFLA_PORT_VSI_TYPE]	= { .type = NLA_BINARY,
1162 				    .len = sizeof(struct ifla_port_vsi)},
1163 	[IFLA_PORT_INSTANCE_UUID] = { .type = NLA_BINARY,
1164 				      .len = PORT_UUID_MAX },
1165 	[IFLA_PORT_HOST_UUID]	= { .type = NLA_STRING,
1166 				    .len = PORT_UUID_MAX },
1167 	[IFLA_PORT_REQUEST]	= { .type = NLA_U8, },
1168 	[IFLA_PORT_RESPONSE]	= { .type = NLA_U16, },
1169 };
1170 
rtnl_link_get_net(struct net * src_net,struct nlattr * tb[])1171 struct net *rtnl_link_get_net(struct net *src_net, struct nlattr *tb[])
1172 {
1173 	struct net *net;
1174 	/* Examine the link attributes and figure out which
1175 	 * network namespace we are talking about.
1176 	 */
1177 	if (tb[IFLA_NET_NS_PID])
1178 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
1179 	else if (tb[IFLA_NET_NS_FD])
1180 		net = get_net_ns_by_fd(nla_get_u32(tb[IFLA_NET_NS_FD]));
1181 	else
1182 		net = get_net(src_net);
1183 	return net;
1184 }
1185 EXPORT_SYMBOL(rtnl_link_get_net);
1186 
validate_linkmsg(struct net_device * dev,struct nlattr * tb[])1187 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
1188 {
1189 	if (dev) {
1190 		if (tb[IFLA_ADDRESS] &&
1191 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
1192 			return -EINVAL;
1193 
1194 		if (tb[IFLA_BROADCAST] &&
1195 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
1196 			return -EINVAL;
1197 	}
1198 
1199 	if (tb[IFLA_AF_SPEC]) {
1200 		struct nlattr *af;
1201 		int rem, err;
1202 
1203 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1204 			const struct rtnl_af_ops *af_ops;
1205 
1206 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1207 				return -EAFNOSUPPORT;
1208 
1209 			if (!af_ops->set_link_af)
1210 				return -EOPNOTSUPP;
1211 
1212 			if (af_ops->validate_link_af) {
1213 				err = af_ops->validate_link_af(dev, af);
1214 				if (err < 0)
1215 					return err;
1216 			}
1217 		}
1218 	}
1219 
1220 	return 0;
1221 }
1222 
do_setvfinfo(struct net_device * dev,struct nlattr * attr)1223 static int do_setvfinfo(struct net_device *dev, struct nlattr *attr)
1224 {
1225 	int rem, err = -EINVAL;
1226 	struct nlattr *vf;
1227 	const struct net_device_ops *ops = dev->netdev_ops;
1228 
1229 	nla_for_each_nested(vf, attr, rem) {
1230 		switch (nla_type(vf)) {
1231 		case IFLA_VF_MAC: {
1232 			struct ifla_vf_mac *ivm;
1233 			ivm = nla_data(vf);
1234 			err = -EOPNOTSUPP;
1235 			if (ops->ndo_set_vf_mac)
1236 				err = ops->ndo_set_vf_mac(dev, ivm->vf,
1237 							  ivm->mac);
1238 			break;
1239 		}
1240 		case IFLA_VF_VLAN: {
1241 			struct ifla_vf_vlan *ivv;
1242 			ivv = nla_data(vf);
1243 			err = -EOPNOTSUPP;
1244 			if (ops->ndo_set_vf_vlan)
1245 				err = ops->ndo_set_vf_vlan(dev, ivv->vf,
1246 							   ivv->vlan,
1247 							   ivv->qos);
1248 			break;
1249 		}
1250 		case IFLA_VF_TX_RATE: {
1251 			struct ifla_vf_tx_rate *ivt;
1252 			ivt = nla_data(vf);
1253 			err = -EOPNOTSUPP;
1254 			if (ops->ndo_set_vf_tx_rate)
1255 				err = ops->ndo_set_vf_tx_rate(dev, ivt->vf,
1256 							      ivt->rate);
1257 			break;
1258 		}
1259 		case IFLA_VF_SPOOFCHK: {
1260 			struct ifla_vf_spoofchk *ivs;
1261 			ivs = nla_data(vf);
1262 			err = -EOPNOTSUPP;
1263 			if (ops->ndo_set_vf_spoofchk)
1264 				err = ops->ndo_set_vf_spoofchk(dev, ivs->vf,
1265 							       ivs->setting);
1266 			break;
1267 		}
1268 		default:
1269 			err = -EINVAL;
1270 			break;
1271 		}
1272 		if (err)
1273 			break;
1274 	}
1275 	return err;
1276 }
1277 
do_set_master(struct net_device * dev,int ifindex)1278 static int do_set_master(struct net_device *dev, int ifindex)
1279 {
1280 	struct net_device *master_dev;
1281 	const struct net_device_ops *ops;
1282 	int err;
1283 
1284 	if (dev->master) {
1285 		if (dev->master->ifindex == ifindex)
1286 			return 0;
1287 		ops = dev->master->netdev_ops;
1288 		if (ops->ndo_del_slave) {
1289 			err = ops->ndo_del_slave(dev->master, dev);
1290 			if (err)
1291 				return err;
1292 		} else {
1293 			return -EOPNOTSUPP;
1294 		}
1295 	}
1296 
1297 	if (ifindex) {
1298 		master_dev = __dev_get_by_index(dev_net(dev), ifindex);
1299 		if (!master_dev)
1300 			return -EINVAL;
1301 		ops = master_dev->netdev_ops;
1302 		if (ops->ndo_add_slave) {
1303 			err = ops->ndo_add_slave(master_dev, dev);
1304 			if (err)
1305 				return err;
1306 		} else {
1307 			return -EOPNOTSUPP;
1308 		}
1309 	}
1310 	return 0;
1311 }
1312 
do_setlink(struct net_device * dev,struct ifinfomsg * ifm,struct nlattr ** tb,char * ifname,int modified)1313 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
1314 		      struct nlattr **tb, char *ifname, int modified)
1315 {
1316 	const struct net_device_ops *ops = dev->netdev_ops;
1317 	int send_addr_notify = 0;
1318 	int err;
1319 
1320 	if (tb[IFLA_NET_NS_PID] || tb[IFLA_NET_NS_FD]) {
1321 		struct net *net = rtnl_link_get_net(dev_net(dev), tb);
1322 		if (IS_ERR(net)) {
1323 			err = PTR_ERR(net);
1324 			goto errout;
1325 		}
1326 		err = dev_change_net_namespace(dev, net, ifname);
1327 		put_net(net);
1328 		if (err)
1329 			goto errout;
1330 		modified = 1;
1331 	}
1332 
1333 	if (tb[IFLA_MAP]) {
1334 		struct rtnl_link_ifmap *u_map;
1335 		struct ifmap k_map;
1336 
1337 		if (!ops->ndo_set_config) {
1338 			err = -EOPNOTSUPP;
1339 			goto errout;
1340 		}
1341 
1342 		if (!netif_device_present(dev)) {
1343 			err = -ENODEV;
1344 			goto errout;
1345 		}
1346 
1347 		u_map = nla_data(tb[IFLA_MAP]);
1348 		k_map.mem_start = (unsigned long) u_map->mem_start;
1349 		k_map.mem_end = (unsigned long) u_map->mem_end;
1350 		k_map.base_addr = (unsigned short) u_map->base_addr;
1351 		k_map.irq = (unsigned char) u_map->irq;
1352 		k_map.dma = (unsigned char) u_map->dma;
1353 		k_map.port = (unsigned char) u_map->port;
1354 
1355 		err = ops->ndo_set_config(dev, &k_map);
1356 		if (err < 0)
1357 			goto errout;
1358 
1359 		modified = 1;
1360 	}
1361 
1362 	if (tb[IFLA_ADDRESS]) {
1363 		struct sockaddr *sa;
1364 		int len;
1365 
1366 		if (!ops->ndo_set_mac_address) {
1367 			err = -EOPNOTSUPP;
1368 			goto errout;
1369 		}
1370 
1371 		if (!netif_device_present(dev)) {
1372 			err = -ENODEV;
1373 			goto errout;
1374 		}
1375 
1376 		len = sizeof(sa_family_t) + dev->addr_len;
1377 		sa = kmalloc(len, GFP_KERNEL);
1378 		if (!sa) {
1379 			err = -ENOMEM;
1380 			goto errout;
1381 		}
1382 		sa->sa_family = dev->type;
1383 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
1384 		       dev->addr_len);
1385 		err = ops->ndo_set_mac_address(dev, sa);
1386 		kfree(sa);
1387 		if (err)
1388 			goto errout;
1389 		send_addr_notify = 1;
1390 		modified = 1;
1391 		add_device_randomness(dev->dev_addr, dev->addr_len);
1392 	}
1393 
1394 	if (tb[IFLA_MTU]) {
1395 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
1396 		if (err < 0)
1397 			goto errout;
1398 		modified = 1;
1399 	}
1400 
1401 	if (tb[IFLA_GROUP]) {
1402 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1403 		modified = 1;
1404 	}
1405 
1406 	/*
1407 	 * Interface selected by interface index but interface
1408 	 * name provided implies that a name change has been
1409 	 * requested.
1410 	 */
1411 	if (ifm->ifi_index > 0 && ifname[0]) {
1412 		err = dev_change_name(dev, ifname);
1413 		if (err < 0)
1414 			goto errout;
1415 		modified = 1;
1416 	}
1417 
1418 	if (tb[IFLA_IFALIAS]) {
1419 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
1420 				    nla_len(tb[IFLA_IFALIAS]));
1421 		if (err < 0)
1422 			goto errout;
1423 		modified = 1;
1424 	}
1425 
1426 	if (tb[IFLA_BROADCAST]) {
1427 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
1428 		send_addr_notify = 1;
1429 	}
1430 
1431 	if (ifm->ifi_flags || ifm->ifi_change) {
1432 		err = dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1433 		if (err < 0)
1434 			goto errout;
1435 	}
1436 
1437 	if (tb[IFLA_MASTER]) {
1438 		err = do_set_master(dev, nla_get_u32(tb[IFLA_MASTER]));
1439 		if (err)
1440 			goto errout;
1441 		modified = 1;
1442 	}
1443 
1444 	if (tb[IFLA_TXQLEN])
1445 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1446 
1447 	if (tb[IFLA_OPERSTATE])
1448 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1449 
1450 	if (tb[IFLA_LINKMODE]) {
1451 		write_lock_bh(&dev_base_lock);
1452 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1453 		write_unlock_bh(&dev_base_lock);
1454 	}
1455 
1456 	if (tb[IFLA_VFINFO_LIST]) {
1457 		struct nlattr *attr;
1458 		int rem;
1459 		nla_for_each_nested(attr, tb[IFLA_VFINFO_LIST], rem) {
1460 			if (nla_type(attr) != IFLA_VF_INFO) {
1461 				err = -EINVAL;
1462 				goto errout;
1463 			}
1464 			err = do_setvfinfo(dev, attr);
1465 			if (err < 0)
1466 				goto errout;
1467 			modified = 1;
1468 		}
1469 	}
1470 	err = 0;
1471 
1472 	if (tb[IFLA_VF_PORTS]) {
1473 		struct nlattr *port[IFLA_PORT_MAX+1];
1474 		struct nlattr *attr;
1475 		int vf;
1476 		int rem;
1477 
1478 		err = -EOPNOTSUPP;
1479 		if (!ops->ndo_set_vf_port)
1480 			goto errout;
1481 
1482 		nla_for_each_nested(attr, tb[IFLA_VF_PORTS], rem) {
1483 			if (nla_type(attr) != IFLA_VF_PORT)
1484 				continue;
1485 			err = nla_parse_nested(port, IFLA_PORT_MAX,
1486 				attr, ifla_port_policy);
1487 			if (err < 0)
1488 				goto errout;
1489 			if (!port[IFLA_PORT_VF]) {
1490 				err = -EOPNOTSUPP;
1491 				goto errout;
1492 			}
1493 			vf = nla_get_u32(port[IFLA_PORT_VF]);
1494 			err = ops->ndo_set_vf_port(dev, vf, port);
1495 			if (err < 0)
1496 				goto errout;
1497 			modified = 1;
1498 		}
1499 	}
1500 	err = 0;
1501 
1502 	if (tb[IFLA_PORT_SELF]) {
1503 		struct nlattr *port[IFLA_PORT_MAX+1];
1504 
1505 		err = nla_parse_nested(port, IFLA_PORT_MAX,
1506 			tb[IFLA_PORT_SELF], ifla_port_policy);
1507 		if (err < 0)
1508 			goto errout;
1509 
1510 		err = -EOPNOTSUPP;
1511 		if (ops->ndo_set_vf_port)
1512 			err = ops->ndo_set_vf_port(dev, PORT_SELF_VF, port);
1513 		if (err < 0)
1514 			goto errout;
1515 		modified = 1;
1516 	}
1517 
1518 	if (tb[IFLA_AF_SPEC]) {
1519 		struct nlattr *af;
1520 		int rem;
1521 
1522 		nla_for_each_nested(af, tb[IFLA_AF_SPEC], rem) {
1523 			const struct rtnl_af_ops *af_ops;
1524 
1525 			if (!(af_ops = rtnl_af_lookup(nla_type(af))))
1526 				BUG();
1527 
1528 			err = af_ops->set_link_af(dev, af);
1529 			if (err < 0)
1530 				goto errout;
1531 
1532 			modified = 1;
1533 		}
1534 	}
1535 	err = 0;
1536 
1537 errout:
1538 	if (err < 0 && modified && net_ratelimit())
1539 		printk(KERN_WARNING "A link change request failed with "
1540 		       "some changes committed already. Interface %s may "
1541 		       "have been left with an inconsistent configuration, "
1542 		       "please check.\n", dev->name);
1543 
1544 	if (send_addr_notify)
1545 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
1546 
1547 	return err;
1548 }
1549 
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1550 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1551 {
1552 	struct net *net = sock_net(skb->sk);
1553 	struct ifinfomsg *ifm;
1554 	struct net_device *dev;
1555 	int err;
1556 	struct nlattr *tb[IFLA_MAX+1];
1557 	char ifname[IFNAMSIZ];
1558 
1559 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1560 	if (err < 0)
1561 		goto errout;
1562 
1563 	if (tb[IFLA_IFNAME])
1564 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1565 	else
1566 		ifname[0] = '\0';
1567 
1568 	err = -EINVAL;
1569 	ifm = nlmsg_data(nlh);
1570 	if (ifm->ifi_index > 0)
1571 		dev = __dev_get_by_index(net, ifm->ifi_index);
1572 	else if (tb[IFLA_IFNAME])
1573 		dev = __dev_get_by_name(net, ifname);
1574 	else
1575 		goto errout;
1576 
1577 	if (dev == NULL) {
1578 		err = -ENODEV;
1579 		goto errout;
1580 	}
1581 
1582 	err = validate_linkmsg(dev, tb);
1583 	if (err < 0)
1584 		goto errout;
1585 
1586 	err = do_setlink(dev, ifm, tb, ifname, 0);
1587 errout:
1588 	return err;
1589 }
1590 
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1591 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1592 {
1593 	struct net *net = sock_net(skb->sk);
1594 	const struct rtnl_link_ops *ops;
1595 	struct net_device *dev;
1596 	struct ifinfomsg *ifm;
1597 	char ifname[IFNAMSIZ];
1598 	struct nlattr *tb[IFLA_MAX+1];
1599 	int err;
1600 	LIST_HEAD(list_kill);
1601 
1602 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1603 	if (err < 0)
1604 		return err;
1605 
1606 	if (tb[IFLA_IFNAME])
1607 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1608 
1609 	ifm = nlmsg_data(nlh);
1610 	if (ifm->ifi_index > 0)
1611 		dev = __dev_get_by_index(net, ifm->ifi_index);
1612 	else if (tb[IFLA_IFNAME])
1613 		dev = __dev_get_by_name(net, ifname);
1614 	else
1615 		return -EINVAL;
1616 
1617 	if (!dev)
1618 		return -ENODEV;
1619 
1620 	ops = dev->rtnl_link_ops;
1621 	if (!ops)
1622 		return -EOPNOTSUPP;
1623 
1624 	ops->dellink(dev, &list_kill);
1625 	unregister_netdevice_many(&list_kill);
1626 	list_del(&list_kill);
1627 	return 0;
1628 }
1629 
rtnl_configure_link(struct net_device * dev,const struct ifinfomsg * ifm)1630 int rtnl_configure_link(struct net_device *dev, const struct ifinfomsg *ifm)
1631 {
1632 	unsigned int old_flags;
1633 	int err;
1634 
1635 	old_flags = dev->flags;
1636 	if (ifm && (ifm->ifi_flags || ifm->ifi_change)) {
1637 		err = __dev_change_flags(dev, rtnl_dev_combine_flags(dev, ifm));
1638 		if (err < 0)
1639 			return err;
1640 	}
1641 
1642 	dev->rtnl_link_state = RTNL_LINK_INITIALIZED;
1643 	rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1644 
1645 	__dev_notify_flags(dev, old_flags);
1646 	return 0;
1647 }
1648 EXPORT_SYMBOL(rtnl_configure_link);
1649 
rtnl_create_link(struct net * src_net,struct net * net,char * ifname,const struct rtnl_link_ops * ops,struct nlattr * tb[])1650 struct net_device *rtnl_create_link(struct net *src_net, struct net *net,
1651 	char *ifname, const struct rtnl_link_ops *ops, struct nlattr *tb[])
1652 {
1653 	int err;
1654 	struct net_device *dev;
1655 	unsigned int num_queues = 1;
1656 	unsigned int real_num_queues = 1;
1657 
1658 	if (ops->get_tx_queues) {
1659 		err = ops->get_tx_queues(src_net, tb, &num_queues,
1660 					 &real_num_queues);
1661 		if (err)
1662 			goto err;
1663 	}
1664 	err = -ENOMEM;
1665 	dev = alloc_netdev_mq(ops->priv_size, ifname, ops->setup, num_queues);
1666 	if (!dev)
1667 		goto err;
1668 
1669 	dev_net_set(dev, net);
1670 	dev->rtnl_link_ops = ops;
1671 	dev->rtnl_link_state = RTNL_LINK_INITIALIZING;
1672 
1673 	if (tb[IFLA_MTU])
1674 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1675 	if (tb[IFLA_ADDRESS])
1676 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1677 				nla_len(tb[IFLA_ADDRESS]));
1678 	if (tb[IFLA_BROADCAST])
1679 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1680 				nla_len(tb[IFLA_BROADCAST]));
1681 	if (tb[IFLA_TXQLEN])
1682 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1683 	if (tb[IFLA_OPERSTATE])
1684 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1685 	if (tb[IFLA_LINKMODE])
1686 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1687 	if (tb[IFLA_GROUP])
1688 		dev_set_group(dev, nla_get_u32(tb[IFLA_GROUP]));
1689 
1690 	return dev;
1691 
1692 err:
1693 	return ERR_PTR(err);
1694 }
1695 EXPORT_SYMBOL(rtnl_create_link);
1696 
rtnl_group_changelink(struct net * net,int group,struct ifinfomsg * ifm,struct nlattr ** tb)1697 static int rtnl_group_changelink(struct net *net, int group,
1698 		struct ifinfomsg *ifm,
1699 		struct nlattr **tb)
1700 {
1701 	struct net_device *dev;
1702 	int err;
1703 
1704 	for_each_netdev(net, dev) {
1705 		if (dev->group == group) {
1706 			err = do_setlink(dev, ifm, tb, NULL, 0);
1707 			if (err < 0)
1708 				return err;
1709 		}
1710 	}
1711 
1712 	return 0;
1713 }
1714 
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1715 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1716 {
1717 	struct net *net = sock_net(skb->sk);
1718 	const struct rtnl_link_ops *ops;
1719 	struct net_device *dev;
1720 	struct ifinfomsg *ifm;
1721 	char kind[MODULE_NAME_LEN];
1722 	char ifname[IFNAMSIZ];
1723 	struct nlattr *tb[IFLA_MAX+1];
1724 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1725 	int err;
1726 
1727 #ifdef CONFIG_MODULES
1728 replay:
1729 #endif
1730 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1731 	if (err < 0)
1732 		return err;
1733 
1734 	if (tb[IFLA_IFNAME])
1735 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1736 	else
1737 		ifname[0] = '\0';
1738 
1739 	ifm = nlmsg_data(nlh);
1740 	if (ifm->ifi_index > 0)
1741 		dev = __dev_get_by_index(net, ifm->ifi_index);
1742 	else {
1743 		if (ifname[0])
1744 			dev = __dev_get_by_name(net, ifname);
1745 		else
1746 			dev = NULL;
1747 	}
1748 
1749 	err = validate_linkmsg(dev, tb);
1750 	if (err < 0)
1751 		return err;
1752 
1753 	if (tb[IFLA_LINKINFO]) {
1754 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1755 				       tb[IFLA_LINKINFO], ifla_info_policy);
1756 		if (err < 0)
1757 			return err;
1758 	} else
1759 		memset(linkinfo, 0, sizeof(linkinfo));
1760 
1761 	if (linkinfo[IFLA_INFO_KIND]) {
1762 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1763 		ops = rtnl_link_ops_get(kind);
1764 	} else {
1765 		kind[0] = '\0';
1766 		ops = NULL;
1767 	}
1768 
1769 	if (1) {
1770 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1771 		struct net *dest_net;
1772 
1773 		if (ops) {
1774 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1775 				err = nla_parse_nested(attr, ops->maxtype,
1776 						       linkinfo[IFLA_INFO_DATA],
1777 						       ops->policy);
1778 				if (err < 0)
1779 					return err;
1780 				data = attr;
1781 			}
1782 			if (ops->validate) {
1783 				err = ops->validate(tb, data);
1784 				if (err < 0)
1785 					return err;
1786 			}
1787 		}
1788 
1789 		if (dev) {
1790 			int modified = 0;
1791 
1792 			if (nlh->nlmsg_flags & NLM_F_EXCL)
1793 				return -EEXIST;
1794 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
1795 				return -EOPNOTSUPP;
1796 
1797 			if (linkinfo[IFLA_INFO_DATA]) {
1798 				if (!ops || ops != dev->rtnl_link_ops ||
1799 				    !ops->changelink)
1800 					return -EOPNOTSUPP;
1801 
1802 				err = ops->changelink(dev, tb, data);
1803 				if (err < 0)
1804 					return err;
1805 				modified = 1;
1806 			}
1807 
1808 			return do_setlink(dev, ifm, tb, ifname, modified);
1809 		}
1810 
1811 		if (!(nlh->nlmsg_flags & NLM_F_CREATE)) {
1812 			if (ifm->ifi_index == 0 && tb[IFLA_GROUP])
1813 				return rtnl_group_changelink(net,
1814 						nla_get_u32(tb[IFLA_GROUP]),
1815 						ifm, tb);
1816 			return -ENODEV;
1817 		}
1818 
1819 		if (ifm->ifi_index)
1820 			return -EOPNOTSUPP;
1821 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1822 			return -EOPNOTSUPP;
1823 
1824 		if (!ops) {
1825 #ifdef CONFIG_MODULES
1826 			if (kind[0]) {
1827 				__rtnl_unlock();
1828 				request_module("rtnl-link-%s", kind);
1829 				rtnl_lock();
1830 				ops = rtnl_link_ops_get(kind);
1831 				if (ops)
1832 					goto replay;
1833 			}
1834 #endif
1835 			return -EOPNOTSUPP;
1836 		}
1837 
1838 		if (!ifname[0])
1839 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1840 
1841 		dest_net = rtnl_link_get_net(net, tb);
1842 		if (IS_ERR(dest_net))
1843 			return PTR_ERR(dest_net);
1844 
1845 		dev = rtnl_create_link(net, dest_net, ifname, ops, tb);
1846 
1847 		if (IS_ERR(dev))
1848 			err = PTR_ERR(dev);
1849 		else if (ops->newlink)
1850 			err = ops->newlink(net, dev, tb, data);
1851 		else
1852 			err = register_netdevice(dev);
1853 
1854 		if (err < 0 && !IS_ERR(dev))
1855 			free_netdev(dev);
1856 		if (err < 0)
1857 			goto out;
1858 
1859 		err = rtnl_configure_link(dev, ifm);
1860 		if (err < 0)
1861 			unregister_netdevice(dev);
1862 out:
1863 		put_net(dest_net);
1864 		return err;
1865 	}
1866 }
1867 
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1868 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1869 {
1870 	struct net *net = sock_net(skb->sk);
1871 	struct ifinfomsg *ifm;
1872 	char ifname[IFNAMSIZ];
1873 	struct nlattr *tb[IFLA_MAX+1];
1874 	struct net_device *dev = NULL;
1875 	struct sk_buff *nskb;
1876 	int err;
1877 	u32 ext_filter_mask = 0;
1878 
1879 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1880 	if (err < 0)
1881 		return err;
1882 
1883 	if (tb[IFLA_IFNAME])
1884 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1885 
1886 	if (tb[IFLA_EXT_MASK])
1887 		ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1888 
1889 	ifm = nlmsg_data(nlh);
1890 	if (ifm->ifi_index > 0)
1891 		dev = __dev_get_by_index(net, ifm->ifi_index);
1892 	else if (tb[IFLA_IFNAME])
1893 		dev = __dev_get_by_name(net, ifname);
1894 	else
1895 		return -EINVAL;
1896 
1897 	if (dev == NULL)
1898 		return -ENODEV;
1899 
1900 	nskb = nlmsg_new(if_nlmsg_size(dev, ext_filter_mask), GFP_KERNEL);
1901 	if (nskb == NULL)
1902 		return -ENOBUFS;
1903 
1904 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1905 			       nlh->nlmsg_seq, 0, 0, ext_filter_mask);
1906 	if (err < 0) {
1907 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
1908 		WARN_ON(err == -EMSGSIZE);
1909 		kfree_skb(nskb);
1910 	} else
1911 		err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1912 
1913 	return err;
1914 }
1915 
rtnl_calcit(struct sk_buff * skb,struct nlmsghdr * nlh)1916 static u16 rtnl_calcit(struct sk_buff *skb, struct nlmsghdr *nlh)
1917 {
1918 	struct net *net = sock_net(skb->sk);
1919 	struct net_device *dev;
1920 	struct nlattr *tb[IFLA_MAX+1];
1921 	u32 ext_filter_mask = 0;
1922 	u16 min_ifinfo_dump_size = 0;
1923 
1924 	if (nlmsg_parse(nlh, sizeof(struct ifinfomsg), tb, IFLA_MAX,
1925 			ifla_policy) >= 0) {
1926 		if (tb[IFLA_EXT_MASK])
1927 			ext_filter_mask = nla_get_u32(tb[IFLA_EXT_MASK]);
1928 	}
1929 
1930 	if (!ext_filter_mask)
1931 		return NLMSG_GOODSIZE;
1932 	/*
1933 	 * traverse the list of net devices and compute the minimum
1934 	 * buffer size based upon the filter mask.
1935 	 */
1936 	list_for_each_entry(dev, &net->dev_base_head, dev_list) {
1937 		min_ifinfo_dump_size = max_t(u16, min_ifinfo_dump_size,
1938 					     if_nlmsg_size(dev,
1939 						           ext_filter_mask));
1940 	}
1941 
1942 	return min_ifinfo_dump_size;
1943 }
1944 
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)1945 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1946 {
1947 	int idx;
1948 	int s_idx = cb->family;
1949 
1950 	if (s_idx == 0)
1951 		s_idx = 1;
1952 	for (idx = 1; idx <= RTNL_FAMILY_MAX; idx++) {
1953 		int type = cb->nlh->nlmsg_type-RTM_BASE;
1954 		if (idx < s_idx || idx == PF_PACKET)
1955 			continue;
1956 		if (rtnl_msg_handlers[idx] == NULL ||
1957 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
1958 			continue;
1959 		if (idx > s_idx)
1960 			memset(&cb->args[0], 0, sizeof(cb->args));
1961 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1962 			break;
1963 	}
1964 	cb->family = idx;
1965 
1966 	return skb->len;
1967 }
1968 
rtmsg_ifinfo(int type,struct net_device * dev,unsigned change)1969 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1970 {
1971 	struct net *net = dev_net(dev);
1972 	struct sk_buff *skb;
1973 	int err = -ENOBUFS;
1974 	size_t if_info_size;
1975 
1976 	skb = nlmsg_new((if_info_size = if_nlmsg_size(dev, 0)), GFP_KERNEL);
1977 	if (skb == NULL)
1978 		goto errout;
1979 
1980 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0, 0);
1981 	if (err < 0) {
1982 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
1983 		WARN_ON(err == -EMSGSIZE);
1984 		kfree_skb(skb);
1985 		goto errout;
1986 	}
1987 	rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1988 	return;
1989 errout:
1990 	if (err < 0)
1991 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1992 }
1993 
1994 /* Protected by RTNL sempahore.  */
1995 static struct rtattr **rta_buf;
1996 static int rtattr_max;
1997 
1998 /* Process one rtnetlink message. */
1999 
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh)2000 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
2001 {
2002 	struct net *net = sock_net(skb->sk);
2003 	rtnl_doit_func doit;
2004 	int sz_idx, kind;
2005 	int min_len;
2006 	int family;
2007 	int type;
2008 	int err;
2009 
2010 	type = nlh->nlmsg_type;
2011 	if (type > RTM_MAX)
2012 		return -EOPNOTSUPP;
2013 
2014 	type -= RTM_BASE;
2015 
2016 	/* All the messages must have at least 1 byte length */
2017 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
2018 		return 0;
2019 
2020 	family = ((struct rtgenmsg *)NLMSG_DATA(nlh))->rtgen_family;
2021 	sz_idx = type>>2;
2022 	kind = type&3;
2023 
2024 	if (kind != 2 && !capable(CAP_NET_ADMIN))
2025 		return -EPERM;
2026 
2027 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
2028 		struct sock *rtnl;
2029 		rtnl_dumpit_func dumpit;
2030 		rtnl_calcit_func calcit;
2031 		u16 min_dump_alloc = 0;
2032 
2033 		dumpit = rtnl_get_dumpit(family, type);
2034 		if (dumpit == NULL)
2035 			return -EOPNOTSUPP;
2036 		calcit = rtnl_get_calcit(family, type);
2037 		if (calcit)
2038 			min_dump_alloc = calcit(skb, nlh);
2039 
2040 		__rtnl_unlock();
2041 		rtnl = net->rtnl;
2042 		{
2043 			struct netlink_dump_control c = {
2044 				.dump		= dumpit,
2045 				.min_dump_alloc	= min_dump_alloc,
2046 			};
2047 			err = netlink_dump_start(rtnl, skb, nlh, &c);
2048 		}
2049 		rtnl_lock();
2050 		return err;
2051 	}
2052 
2053 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
2054 
2055 	min_len = rtm_min[sz_idx];
2056 	if (nlh->nlmsg_len < min_len)
2057 		return -EINVAL;
2058 
2059 	if (nlh->nlmsg_len > min_len) {
2060 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
2061 		struct rtattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
2062 
2063 		while (RTA_OK(attr, attrlen)) {
2064 			unsigned int flavor = attr->rta_type & NLA_TYPE_MASK;
2065 			if (flavor) {
2066 				if (flavor > rta_max[sz_idx])
2067 					return -EINVAL;
2068 				rta_buf[flavor-1] = attr;
2069 			}
2070 			attr = RTA_NEXT(attr, attrlen);
2071 		}
2072 	}
2073 
2074 	doit = rtnl_get_doit(family, type);
2075 	if (doit == NULL)
2076 		return -EOPNOTSUPP;
2077 
2078 	return doit(skb, nlh, (void *)&rta_buf[0]);
2079 }
2080 
rtnetlink_rcv(struct sk_buff * skb)2081 static void rtnetlink_rcv(struct sk_buff *skb)
2082 {
2083 	rtnl_lock();
2084 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
2085 	rtnl_unlock();
2086 }
2087 
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)2088 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
2089 {
2090 	struct net_device *dev = ptr;
2091 
2092 	switch (event) {
2093 	case NETDEV_UP:
2094 	case NETDEV_DOWN:
2095 	case NETDEV_PRE_UP:
2096 	case NETDEV_POST_INIT:
2097 	case NETDEV_REGISTER:
2098 	case NETDEV_CHANGE:
2099 	case NETDEV_PRE_TYPE_CHANGE:
2100 	case NETDEV_GOING_DOWN:
2101 	case NETDEV_UNREGISTER:
2102 	case NETDEV_UNREGISTER_BATCH:
2103 	case NETDEV_RELEASE:
2104 	case NETDEV_JOIN:
2105 		break;
2106 	default:
2107 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
2108 		break;
2109 	}
2110 	return NOTIFY_DONE;
2111 }
2112 
2113 static struct notifier_block rtnetlink_dev_notifier = {
2114 	.notifier_call	= rtnetlink_event,
2115 };
2116 
2117 
rtnetlink_net_init(struct net * net)2118 static int __net_init rtnetlink_net_init(struct net *net)
2119 {
2120 	struct sock *sk;
2121 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
2122 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
2123 	if (!sk)
2124 		return -ENOMEM;
2125 	net->rtnl = sk;
2126 	return 0;
2127 }
2128 
rtnetlink_net_exit(struct net * net)2129 static void __net_exit rtnetlink_net_exit(struct net *net)
2130 {
2131 	netlink_kernel_release(net->rtnl);
2132 	net->rtnl = NULL;
2133 }
2134 
2135 static struct pernet_operations rtnetlink_net_ops = {
2136 	.init = rtnetlink_net_init,
2137 	.exit = rtnetlink_net_exit,
2138 };
2139 
rtnetlink_init(void)2140 void __init rtnetlink_init(void)
2141 {
2142 	int i;
2143 
2144 	rtattr_max = 0;
2145 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
2146 		if (rta_max[i] > rtattr_max)
2147 			rtattr_max = rta_max[i];
2148 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
2149 	if (!rta_buf)
2150 		panic("rtnetlink_init: cannot allocate rta_buf\n");
2151 
2152 	if (register_pernet_subsys(&rtnetlink_net_ops))
2153 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
2154 
2155 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
2156 	register_netdevice_notifier(&rtnetlink_dev_notifier);
2157 
2158 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink,
2159 		      rtnl_dump_ifinfo, rtnl_calcit);
2160 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL, NULL);
2161 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL, NULL);
2162 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL, NULL);
2163 
2164 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all, NULL);
2165 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all, NULL);
2166 }
2167 
2168