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