• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * INET		An implementation of the TCP/IP protocol suite for the LINUX
3  *		operating system.  INET is implemented using the  BSD Socket
4  *		interface as the means of communication with the user level.
5  *
6  *		Routing netlink socket interface: protocol independent part.
7  *
8  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
9  *
10  *		This program is free software; you can redistribute it and/or
11  *		modify it under the terms of the GNU General Public License
12  *		as published by the Free Software Foundation; either version
13  *		2 of the License, or (at your option) any later version.
14  *
15  *	Fixes:
16  *	Vitaly E. Lavrov		RTA_OK arithmetics was wrong.
17  */
18 
19 #include <linux/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/mm.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.h>
38 #include <linux/nsproxy.h>
39 
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/string.h>
43 
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <net/ip.h>
47 #include <net/protocol.h>
48 #include <net/arp.h>
49 #include <net/route.h>
50 #include <net/udp.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/fib_rules.h>
54 #include <net/rtnetlink.h>
55 
56 struct rtnl_link
57 {
58 	rtnl_doit_func		doit;
59 	rtnl_dumpit_func	dumpit;
60 };
61 
62 static DEFINE_MUTEX(rtnl_mutex);
63 
rtnl_lock(void)64 void rtnl_lock(void)
65 {
66 	mutex_lock(&rtnl_mutex);
67 }
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 
rtnl_trylock(void)80 int rtnl_trylock(void)
81 {
82 	return mutex_trylock(&rtnl_mutex);
83 }
84 
rtnl_is_locked(void)85 int rtnl_is_locked(void)
86 {
87 	return mutex_is_locked(&rtnl_mutex);
88 }
89 
90 static struct rtnl_link *rtnl_msg_handlers[NPROTO];
91 
rtm_msgindex(int msgtype)92 static inline int rtm_msgindex(int msgtype)
93 {
94 	int msgindex = msgtype - RTM_BASE;
95 
96 	/*
97 	 * msgindex < 0 implies someone tried to register a netlink
98 	 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
99 	 * the message type has not been added to linux/rtnetlink.h
100 	 */
101 	BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
102 
103 	return msgindex;
104 }
105 
rtnl_get_doit(int protocol,int msgindex)106 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
107 {
108 	struct rtnl_link *tab;
109 
110 	tab = rtnl_msg_handlers[protocol];
111 	if (tab == NULL || tab[msgindex].doit == NULL)
112 		tab = rtnl_msg_handlers[PF_UNSPEC];
113 
114 	return tab ? tab[msgindex].doit : NULL;
115 }
116 
rtnl_get_dumpit(int protocol,int msgindex)117 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
118 {
119 	struct rtnl_link *tab;
120 
121 	tab = rtnl_msg_handlers[protocol];
122 	if (tab == NULL || tab[msgindex].dumpit == NULL)
123 		tab = rtnl_msg_handlers[PF_UNSPEC];
124 
125 	return tab ? tab[msgindex].dumpit : NULL;
126 }
127 
128 /**
129  * __rtnl_register - Register a rtnetlink message type
130  * @protocol: Protocol family or PF_UNSPEC
131  * @msgtype: rtnetlink message type
132  * @doit: Function pointer called for each request message
133  * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
134  *
135  * Registers the specified function pointers (at least one of them has
136  * to be non-NULL) to be called whenever a request message for the
137  * specified protocol family and message type is received.
138  *
139  * The special protocol family PF_UNSPEC may be used to define fallback
140  * function pointers for the case when no entry for the specific protocol
141  * family exists.
142  *
143  * Returns 0 on success or a negative error code.
144  */
__rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit)145 int __rtnl_register(int protocol, int msgtype,
146 		    rtnl_doit_func doit, rtnl_dumpit_func dumpit)
147 {
148 	struct rtnl_link *tab;
149 	int msgindex;
150 
151 	BUG_ON(protocol < 0 || protocol >= NPROTO);
152 	msgindex = rtm_msgindex(msgtype);
153 
154 	tab = rtnl_msg_handlers[protocol];
155 	if (tab == NULL) {
156 		tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
157 		if (tab == NULL)
158 			return -ENOBUFS;
159 
160 		rtnl_msg_handlers[protocol] = tab;
161 	}
162 
163 	if (doit)
164 		tab[msgindex].doit = doit;
165 
166 	if (dumpit)
167 		tab[msgindex].dumpit = dumpit;
168 
169 	return 0;
170 }
171 
172 EXPORT_SYMBOL_GPL(__rtnl_register);
173 
174 /**
175  * rtnl_register - Register a rtnetlink message type
176  *
177  * Identical to __rtnl_register() but panics on failure. This is useful
178  * as failure of this function is very unlikely, it can only happen due
179  * to lack of memory when allocating the chain to store all message
180  * handlers for a protocol. Meant for use in init functions where lack
181  * of memory implies no sense in continueing.
182  */
rtnl_register(int protocol,int msgtype,rtnl_doit_func doit,rtnl_dumpit_func dumpit)183 void rtnl_register(int protocol, int msgtype,
184 		   rtnl_doit_func doit, rtnl_dumpit_func dumpit)
185 {
186 	if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
187 		panic("Unable to register rtnetlink message handler, "
188 		      "protocol = %d, message type = %d\n",
189 		      protocol, msgtype);
190 }
191 
192 EXPORT_SYMBOL_GPL(rtnl_register);
193 
194 /**
195  * rtnl_unregister - Unregister a rtnetlink message type
196  * @protocol: Protocol family or PF_UNSPEC
197  * @msgtype: rtnetlink message type
198  *
199  * Returns 0 on success or a negative error code.
200  */
rtnl_unregister(int protocol,int msgtype)201 int rtnl_unregister(int protocol, int msgtype)
202 {
203 	int msgindex;
204 
205 	BUG_ON(protocol < 0 || protocol >= NPROTO);
206 	msgindex = rtm_msgindex(msgtype);
207 
208 	if (rtnl_msg_handlers[protocol] == NULL)
209 		return -ENOENT;
210 
211 	rtnl_msg_handlers[protocol][msgindex].doit = NULL;
212 	rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
213 
214 	return 0;
215 }
216 
217 EXPORT_SYMBOL_GPL(rtnl_unregister);
218 
219 /**
220  * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
221  * @protocol : Protocol family or PF_UNSPEC
222  *
223  * Identical to calling rtnl_unregster() for all registered message types
224  * of a certain protocol family.
225  */
rtnl_unregister_all(int protocol)226 void rtnl_unregister_all(int protocol)
227 {
228 	BUG_ON(protocol < 0 || protocol >= NPROTO);
229 
230 	kfree(rtnl_msg_handlers[protocol]);
231 	rtnl_msg_handlers[protocol] = NULL;
232 }
233 
234 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
235 
236 static LIST_HEAD(link_ops);
237 
238 /**
239  * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
240  * @ops: struct rtnl_link_ops * to register
241  *
242  * The caller must hold the rtnl_mutex. This function should be used
243  * by drivers that create devices during module initialization. It
244  * must be called before registering the devices.
245  *
246  * Returns 0 on success or a negative error code.
247  */
__rtnl_link_register(struct rtnl_link_ops * ops)248 int __rtnl_link_register(struct rtnl_link_ops *ops)
249 {
250 	if (!ops->dellink)
251 		ops->dellink = unregister_netdevice;
252 
253 	list_add_tail(&ops->list, &link_ops);
254 	return 0;
255 }
256 
257 EXPORT_SYMBOL_GPL(__rtnl_link_register);
258 
259 /**
260  * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
261  * @ops: struct rtnl_link_ops * to register
262  *
263  * Returns 0 on success or a negative error code.
264  */
rtnl_link_register(struct rtnl_link_ops * ops)265 int rtnl_link_register(struct rtnl_link_ops *ops)
266 {
267 	int err;
268 
269 	rtnl_lock();
270 	err = __rtnl_link_register(ops);
271 	rtnl_unlock();
272 	return err;
273 }
274 
275 EXPORT_SYMBOL_GPL(rtnl_link_register);
276 
__rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)277 static void __rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
278 {
279 	struct net_device *dev;
280 restart:
281 	for_each_netdev(net, dev) {
282 		if (dev->rtnl_link_ops == ops) {
283 			ops->dellink(dev);
284 			goto restart;
285 		}
286 	}
287 }
288 
rtnl_kill_links(struct net * net,struct rtnl_link_ops * ops)289 void rtnl_kill_links(struct net *net, struct rtnl_link_ops *ops)
290 {
291 	rtnl_lock();
292 	__rtnl_kill_links(net, ops);
293 	rtnl_unlock();
294 }
295 EXPORT_SYMBOL_GPL(rtnl_kill_links);
296 
297 /**
298  * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
299  * @ops: struct rtnl_link_ops * to unregister
300  *
301  * The caller must hold the rtnl_mutex.
302  */
__rtnl_link_unregister(struct rtnl_link_ops * ops)303 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
304 {
305 	struct net *net;
306 
307 	for_each_net(net) {
308 		__rtnl_kill_links(net, ops);
309 	}
310 	list_del(&ops->list);
311 }
312 
313 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
314 
315 /**
316  * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
317  * @ops: struct rtnl_link_ops * to unregister
318  */
rtnl_link_unregister(struct rtnl_link_ops * ops)319 void rtnl_link_unregister(struct rtnl_link_ops *ops)
320 {
321 	rtnl_lock();
322 	__rtnl_link_unregister(ops);
323 	rtnl_unlock();
324 }
325 
326 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
327 
rtnl_link_ops_get(const char * kind)328 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
329 {
330 	const struct rtnl_link_ops *ops;
331 
332 	list_for_each_entry(ops, &link_ops, list) {
333 		if (!strcmp(ops->kind, kind))
334 			return ops;
335 	}
336 	return NULL;
337 }
338 
rtnl_link_get_size(const struct net_device * dev)339 static size_t rtnl_link_get_size(const struct net_device *dev)
340 {
341 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
342 	size_t size;
343 
344 	if (!ops)
345 		return 0;
346 
347 	size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
348 	       nlmsg_total_size(strlen(ops->kind) + 1);	 /* IFLA_INFO_KIND */
349 
350 	if (ops->get_size)
351 		/* IFLA_INFO_DATA + nested data */
352 		size += nlmsg_total_size(sizeof(struct nlattr)) +
353 			ops->get_size(dev);
354 
355 	if (ops->get_xstats_size)
356 		size += ops->get_xstats_size(dev);	/* IFLA_INFO_XSTATS */
357 
358 	return size;
359 }
360 
rtnl_link_fill(struct sk_buff * skb,const struct net_device * dev)361 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
362 {
363 	const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
364 	struct nlattr *linkinfo, *data;
365 	int err = -EMSGSIZE;
366 
367 	linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
368 	if (linkinfo == NULL)
369 		goto out;
370 
371 	if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
372 		goto err_cancel_link;
373 	if (ops->fill_xstats) {
374 		err = ops->fill_xstats(skb, dev);
375 		if (err < 0)
376 			goto err_cancel_link;
377 	}
378 	if (ops->fill_info) {
379 		data = nla_nest_start(skb, IFLA_INFO_DATA);
380 		if (data == NULL)
381 			goto err_cancel_link;
382 		err = ops->fill_info(skb, dev);
383 		if (err < 0)
384 			goto err_cancel_data;
385 		nla_nest_end(skb, data);
386 	}
387 
388 	nla_nest_end(skb, linkinfo);
389 	return 0;
390 
391 err_cancel_data:
392 	nla_nest_cancel(skb, data);
393 err_cancel_link:
394 	nla_nest_cancel(skb, linkinfo);
395 out:
396 	return err;
397 }
398 
399 static const int rtm_min[RTM_NR_FAMILIES] =
400 {
401 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
402 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
403 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
404 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
405 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
406 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
407 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
408 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
409 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
410 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
411 };
412 
413 static const int rta_max[RTM_NR_FAMILIES] =
414 {
415 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
416 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
417 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
418 	[RTM_FAM(RTM_NEWRULE)]      = FRA_MAX,
419 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
420 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
421 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
422 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
423 };
424 
__rta_fill(struct sk_buff * skb,int attrtype,int attrlen,const void * data)425 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
426 {
427 	struct rtattr *rta;
428 	int size = RTA_LENGTH(attrlen);
429 
430 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
431 	rta->rta_type = attrtype;
432 	rta->rta_len = size;
433 	memcpy(RTA_DATA(rta), data, attrlen);
434 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
435 }
436 
rtnetlink_send(struct sk_buff * skb,struct net * net,u32 pid,unsigned group,int echo)437 int rtnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, unsigned group, int echo)
438 {
439 	struct sock *rtnl = net->rtnl;
440 	int err = 0;
441 
442 	NETLINK_CB(skb).dst_group = group;
443 	if (echo)
444 		atomic_inc(&skb->users);
445 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
446 	if (echo)
447 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
448 	return err;
449 }
450 
rtnl_unicast(struct sk_buff * skb,struct net * net,u32 pid)451 int rtnl_unicast(struct sk_buff *skb, struct net *net, u32 pid)
452 {
453 	struct sock *rtnl = net->rtnl;
454 
455 	return nlmsg_unicast(rtnl, skb, pid);
456 }
457 
rtnl_notify(struct sk_buff * skb,struct net * net,u32 pid,u32 group,struct nlmsghdr * nlh,gfp_t flags)458 int rtnl_notify(struct sk_buff *skb, struct net *net, u32 pid, u32 group,
459 		struct nlmsghdr *nlh, gfp_t flags)
460 {
461 	struct sock *rtnl = net->rtnl;
462 	int report = 0;
463 
464 	if (nlh)
465 		report = nlmsg_report(nlh);
466 
467 	return nlmsg_notify(rtnl, skb, pid, group, report, flags);
468 }
469 
rtnl_set_sk_err(struct net * net,u32 group,int error)470 void rtnl_set_sk_err(struct net *net, u32 group, int error)
471 {
472 	struct sock *rtnl = net->rtnl;
473 
474 	netlink_set_err(rtnl, 0, group, error);
475 }
476 
rtnetlink_put_metrics(struct sk_buff * skb,u32 * metrics)477 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
478 {
479 	struct nlattr *mx;
480 	int i, valid = 0;
481 
482 	mx = nla_nest_start(skb, RTA_METRICS);
483 	if (mx == NULL)
484 		return -ENOBUFS;
485 
486 	for (i = 0; i < RTAX_MAX; i++) {
487 		if (metrics[i]) {
488 			valid++;
489 			NLA_PUT_U32(skb, i+1, metrics[i]);
490 		}
491 	}
492 
493 	if (!valid) {
494 		nla_nest_cancel(skb, mx);
495 		return 0;
496 	}
497 
498 	return nla_nest_end(skb, mx);
499 
500 nla_put_failure:
501 	nla_nest_cancel(skb, mx);
502 	return -EMSGSIZE;
503 }
504 
rtnl_put_cacheinfo(struct sk_buff * skb,struct dst_entry * dst,u32 id,u32 ts,u32 tsage,long expires,u32 error)505 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
506 		       u32 ts, u32 tsage, long expires, u32 error)
507 {
508 	struct rta_cacheinfo ci = {
509 		.rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
510 		.rta_used = dst->__use,
511 		.rta_clntref = atomic_read(&(dst->__refcnt)),
512 		.rta_error = error,
513 		.rta_id =  id,
514 		.rta_ts = ts,
515 		.rta_tsage = tsage,
516 	};
517 
518 	if (expires)
519 		ci.rta_expires = jiffies_to_clock_t(expires);
520 
521 	return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
522 }
523 
524 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
525 
set_operstate(struct net_device * dev,unsigned char transition)526 static void set_operstate(struct net_device *dev, unsigned char transition)
527 {
528 	unsigned char operstate = dev->operstate;
529 
530 	switch(transition) {
531 	case IF_OPER_UP:
532 		if ((operstate == IF_OPER_DORMANT ||
533 		     operstate == IF_OPER_UNKNOWN) &&
534 		    !netif_dormant(dev))
535 			operstate = IF_OPER_UP;
536 		break;
537 
538 	case IF_OPER_DORMANT:
539 		if (operstate == IF_OPER_UP ||
540 		    operstate == IF_OPER_UNKNOWN)
541 			operstate = IF_OPER_DORMANT;
542 		break;
543 	}
544 
545 	if (dev->operstate != operstate) {
546 		write_lock_bh(&dev_base_lock);
547 		dev->operstate = operstate;
548 		write_unlock_bh(&dev_base_lock);
549 		netdev_state_change(dev);
550 	}
551 }
552 
copy_rtnl_link_stats(struct rtnl_link_stats * a,const struct net_device_stats * b)553 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
554 				 const struct net_device_stats *b)
555 {
556 	a->rx_packets = b->rx_packets;
557 	a->tx_packets = b->tx_packets;
558 	a->rx_bytes = b->rx_bytes;
559 	a->tx_bytes = b->tx_bytes;
560 	a->rx_errors = b->rx_errors;
561 	a->tx_errors = b->tx_errors;
562 	a->rx_dropped = b->rx_dropped;
563 	a->tx_dropped = b->tx_dropped;
564 
565 	a->multicast = b->multicast;
566 	a->collisions = b->collisions;
567 
568 	a->rx_length_errors = b->rx_length_errors;
569 	a->rx_over_errors = b->rx_over_errors;
570 	a->rx_crc_errors = b->rx_crc_errors;
571 	a->rx_frame_errors = b->rx_frame_errors;
572 	a->rx_fifo_errors = b->rx_fifo_errors;
573 	a->rx_missed_errors = b->rx_missed_errors;
574 
575 	a->tx_aborted_errors = b->tx_aborted_errors;
576 	a->tx_carrier_errors = b->tx_carrier_errors;
577 	a->tx_fifo_errors = b->tx_fifo_errors;
578 	a->tx_heartbeat_errors = b->tx_heartbeat_errors;
579 	a->tx_window_errors = b->tx_window_errors;
580 
581 	a->rx_compressed = b->rx_compressed;
582 	a->tx_compressed = b->tx_compressed;
583 };
584 
if_nlmsg_size(const struct net_device * dev)585 static inline size_t if_nlmsg_size(const struct net_device *dev)
586 {
587 	return NLMSG_ALIGN(sizeof(struct ifinfomsg))
588 	       + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
589 	       + nla_total_size(IFALIASZ) /* IFLA_IFALIAS */
590 	       + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
591 	       + nla_total_size(sizeof(struct rtnl_link_ifmap))
592 	       + nla_total_size(sizeof(struct rtnl_link_stats))
593 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
594 	       + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
595 	       + nla_total_size(4) /* IFLA_TXQLEN */
596 	       + nla_total_size(4) /* IFLA_WEIGHT */
597 	       + nla_total_size(4) /* IFLA_MTU */
598 	       + nla_total_size(4) /* IFLA_LINK */
599 	       + nla_total_size(4) /* IFLA_MASTER */
600 	       + nla_total_size(1) /* IFLA_OPERSTATE */
601 	       + nla_total_size(1) /* IFLA_LINKMODE */
602 	       + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
603 }
604 
rtnl_fill_ifinfo(struct sk_buff * skb,struct net_device * dev,int type,u32 pid,u32 seq,u32 change,unsigned int flags)605 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
606 			    int type, u32 pid, u32 seq, u32 change,
607 			    unsigned int flags)
608 {
609 	struct netdev_queue *txq;
610 	struct ifinfomsg *ifm;
611 	struct nlmsghdr *nlh;
612 	const struct net_device_stats *stats;
613 	struct nlattr *attr;
614 
615 	nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
616 	if (nlh == NULL)
617 		return -EMSGSIZE;
618 
619 	ifm = nlmsg_data(nlh);
620 	ifm->ifi_family = AF_UNSPEC;
621 	ifm->__ifi_pad = 0;
622 	ifm->ifi_type = dev->type;
623 	ifm->ifi_index = dev->ifindex;
624 	ifm->ifi_flags = dev_get_flags(dev);
625 	ifm->ifi_change = change;
626 
627 	NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
628 	NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
629 	NLA_PUT_U8(skb, IFLA_OPERSTATE,
630 		   netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
631 	NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
632 	NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
633 
634 	if (dev->ifindex != dev->iflink)
635 		NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
636 
637 	if (dev->master)
638 		NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
639 
640 	txq = netdev_get_tx_queue(dev, 0);
641 	if (txq->qdisc_sleeping)
642 		NLA_PUT_STRING(skb, IFLA_QDISC, txq->qdisc_sleeping->ops->id);
643 
644 	if (dev->ifalias)
645 		NLA_PUT_STRING(skb, IFLA_IFALIAS, dev->ifalias);
646 
647 	if (1) {
648 		struct rtnl_link_ifmap map = {
649 			.mem_start   = dev->mem_start,
650 			.mem_end     = dev->mem_end,
651 			.base_addr   = dev->base_addr,
652 			.irq         = dev->irq,
653 			.dma         = dev->dma,
654 			.port        = dev->if_port,
655 		};
656 		NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
657 	}
658 
659 	if (dev->addr_len) {
660 		NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
661 		NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
662 	}
663 
664 	attr = nla_reserve(skb, IFLA_STATS,
665 			sizeof(struct rtnl_link_stats));
666 	if (attr == NULL)
667 		goto nla_put_failure;
668 
669 	stats = dev_get_stats(dev);
670 	copy_rtnl_link_stats(nla_data(attr), stats);
671 
672 	if (dev->rtnl_link_ops) {
673 		if (rtnl_link_fill(skb, dev) < 0)
674 			goto nla_put_failure;
675 	}
676 
677 	return nlmsg_end(skb, nlh);
678 
679 nla_put_failure:
680 	nlmsg_cancel(skb, nlh);
681 	return -EMSGSIZE;
682 }
683 
rtnl_dump_ifinfo(struct sk_buff * skb,struct netlink_callback * cb)684 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
685 {
686 	struct net *net = sock_net(skb->sk);
687 	int idx;
688 	int s_idx = cb->args[0];
689 	struct net_device *dev;
690 
691 	idx = 0;
692 	for_each_netdev(net, dev) {
693 		if (idx < s_idx)
694 			goto cont;
695 		if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
696 				     NETLINK_CB(cb->skb).pid,
697 				     cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
698 			break;
699 cont:
700 		idx++;
701 	}
702 	cb->args[0] = idx;
703 
704 	return skb->len;
705 }
706 
707 const struct nla_policy ifla_policy[IFLA_MAX+1] = {
708 	[IFLA_IFNAME]		= { .type = NLA_STRING, .len = IFNAMSIZ-1 },
709 	[IFLA_ADDRESS]		= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
710 	[IFLA_BROADCAST]	= { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
711 	[IFLA_MAP]		= { .len = sizeof(struct rtnl_link_ifmap) },
712 	[IFLA_MTU]		= { .type = NLA_U32 },
713 	[IFLA_LINK]		= { .type = NLA_U32 },
714 	[IFLA_TXQLEN]		= { .type = NLA_U32 },
715 	[IFLA_WEIGHT]		= { .type = NLA_U32 },
716 	[IFLA_OPERSTATE]	= { .type = NLA_U8 },
717 	[IFLA_LINKMODE]		= { .type = NLA_U8 },
718 	[IFLA_LINKINFO]		= { .type = NLA_NESTED },
719 	[IFLA_NET_NS_PID]	= { .type = NLA_U32 },
720 	[IFLA_IFALIAS]	        = { .type = NLA_STRING, .len = IFALIASZ-1 },
721 };
722 
723 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
724 	[IFLA_INFO_KIND]	= { .type = NLA_STRING },
725 	[IFLA_INFO_DATA]	= { .type = NLA_NESTED },
726 };
727 
get_net_ns_by_pid(pid_t pid)728 static struct net *get_net_ns_by_pid(pid_t pid)
729 {
730 	struct task_struct *tsk;
731 	struct net *net;
732 
733 	/* Lookup the network namespace */
734 	net = ERR_PTR(-ESRCH);
735 	rcu_read_lock();
736 	tsk = find_task_by_vpid(pid);
737 	if (tsk) {
738 		struct nsproxy *nsproxy;
739 		nsproxy = task_nsproxy(tsk);
740 		if (nsproxy)
741 			net = get_net(nsproxy->net_ns);
742 	}
743 	rcu_read_unlock();
744 	return net;
745 }
746 
validate_linkmsg(struct net_device * dev,struct nlattr * tb[])747 static int validate_linkmsg(struct net_device *dev, struct nlattr *tb[])
748 {
749 	if (dev) {
750 		if (tb[IFLA_ADDRESS] &&
751 		    nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
752 			return -EINVAL;
753 
754 		if (tb[IFLA_BROADCAST] &&
755 		    nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
756 			return -EINVAL;
757 	}
758 
759 	return 0;
760 }
761 
do_setlink(struct net_device * dev,struct ifinfomsg * ifm,struct nlattr ** tb,char * ifname,int modified)762 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
763 		      struct nlattr **tb, char *ifname, int modified)
764 {
765 	const struct net_device_ops *ops = dev->netdev_ops;
766 	int send_addr_notify = 0;
767 	int err;
768 
769 	if (tb[IFLA_NET_NS_PID]) {
770 		struct net *net;
771 		net = get_net_ns_by_pid(nla_get_u32(tb[IFLA_NET_NS_PID]));
772 		if (IS_ERR(net)) {
773 			err = PTR_ERR(net);
774 			goto errout;
775 		}
776 		err = dev_change_net_namespace(dev, net, ifname);
777 		put_net(net);
778 		if (err)
779 			goto errout;
780 		modified = 1;
781 	}
782 
783 	if (tb[IFLA_MAP]) {
784 		struct rtnl_link_ifmap *u_map;
785 		struct ifmap k_map;
786 
787 		if (!ops->ndo_set_config) {
788 			err = -EOPNOTSUPP;
789 			goto errout;
790 		}
791 
792 		if (!netif_device_present(dev)) {
793 			err = -ENODEV;
794 			goto errout;
795 		}
796 
797 		u_map = nla_data(tb[IFLA_MAP]);
798 		k_map.mem_start = (unsigned long) u_map->mem_start;
799 		k_map.mem_end = (unsigned long) u_map->mem_end;
800 		k_map.base_addr = (unsigned short) u_map->base_addr;
801 		k_map.irq = (unsigned char) u_map->irq;
802 		k_map.dma = (unsigned char) u_map->dma;
803 		k_map.port = (unsigned char) u_map->port;
804 
805 		err = ops->ndo_set_config(dev, &k_map);
806 		if (err < 0)
807 			goto errout;
808 
809 		modified = 1;
810 	}
811 
812 	if (tb[IFLA_ADDRESS]) {
813 		struct sockaddr *sa;
814 		int len;
815 
816 		if (!ops->ndo_set_mac_address) {
817 			err = -EOPNOTSUPP;
818 			goto errout;
819 		}
820 
821 		if (!netif_device_present(dev)) {
822 			err = -ENODEV;
823 			goto errout;
824 		}
825 
826 		len = sizeof(sa_family_t) + dev->addr_len;
827 		sa = kmalloc(len, GFP_KERNEL);
828 		if (!sa) {
829 			err = -ENOMEM;
830 			goto errout;
831 		}
832 		sa->sa_family = dev->type;
833 		memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
834 		       dev->addr_len);
835 		err = ops->ndo_set_mac_address(dev, sa);
836 		kfree(sa);
837 		if (err)
838 			goto errout;
839 		send_addr_notify = 1;
840 		modified = 1;
841 	}
842 
843 	if (tb[IFLA_MTU]) {
844 		err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
845 		if (err < 0)
846 			goto errout;
847 		modified = 1;
848 	}
849 
850 	/*
851 	 * Interface selected by interface index but interface
852 	 * name provided implies that a name change has been
853 	 * requested.
854 	 */
855 	if (ifm->ifi_index > 0 && ifname[0]) {
856 		err = dev_change_name(dev, ifname);
857 		if (err < 0)
858 			goto errout;
859 		modified = 1;
860 	}
861 
862 	if (tb[IFLA_IFALIAS]) {
863 		err = dev_set_alias(dev, nla_data(tb[IFLA_IFALIAS]),
864 				    nla_len(tb[IFLA_IFALIAS]));
865 		if (err < 0)
866 			goto errout;
867 		modified = 1;
868 	}
869 
870 	if (tb[IFLA_BROADCAST]) {
871 		nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
872 		send_addr_notify = 1;
873 	}
874 
875 	if (ifm->ifi_flags || ifm->ifi_change) {
876 		unsigned int flags = ifm->ifi_flags;
877 
878 		/* bugwards compatibility: ifi_change == 0 is treated as ~0 */
879 		if (ifm->ifi_change)
880 			flags = (flags & ifm->ifi_change) |
881 				(dev->flags & ~ifm->ifi_change);
882 		err = dev_change_flags(dev, flags);
883 		if (err < 0)
884 			goto errout;
885 	}
886 
887 	if (tb[IFLA_TXQLEN])
888 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
889 
890 	if (tb[IFLA_OPERSTATE])
891 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
892 
893 	if (tb[IFLA_LINKMODE]) {
894 		write_lock_bh(&dev_base_lock);
895 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
896 		write_unlock_bh(&dev_base_lock);
897 	}
898 
899 	err = 0;
900 
901 errout:
902 	if (err < 0 && modified && net_ratelimit())
903 		printk(KERN_WARNING "A link change request failed with "
904 		       "some changes comitted already. Interface %s may "
905 		       "have been left with an inconsistent configuration, "
906 		       "please check.\n", dev->name);
907 
908 	if (send_addr_notify)
909 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
910 	return err;
911 }
912 
rtnl_setlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)913 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
914 {
915 	struct net *net = sock_net(skb->sk);
916 	struct ifinfomsg *ifm;
917 	struct net_device *dev;
918 	int err;
919 	struct nlattr *tb[IFLA_MAX+1];
920 	char ifname[IFNAMSIZ];
921 
922 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
923 	if (err < 0)
924 		goto errout;
925 
926 	if (tb[IFLA_IFNAME])
927 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
928 	else
929 		ifname[0] = '\0';
930 
931 	err = -EINVAL;
932 	ifm = nlmsg_data(nlh);
933 	if (ifm->ifi_index > 0)
934 		dev = dev_get_by_index(net, ifm->ifi_index);
935 	else if (tb[IFLA_IFNAME])
936 		dev = dev_get_by_name(net, ifname);
937 	else
938 		goto errout;
939 
940 	if (dev == NULL) {
941 		err = -ENODEV;
942 		goto errout;
943 	}
944 
945 	if ((err = validate_linkmsg(dev, tb)) < 0)
946 		goto errout_dev;
947 
948 	err = do_setlink(dev, ifm, tb, ifname, 0);
949 errout_dev:
950 	dev_put(dev);
951 errout:
952 	return err;
953 }
954 
rtnl_dellink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)955 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
956 {
957 	struct net *net = sock_net(skb->sk);
958 	const struct rtnl_link_ops *ops;
959 	struct net_device *dev;
960 	struct ifinfomsg *ifm;
961 	char ifname[IFNAMSIZ];
962 	struct nlattr *tb[IFLA_MAX+1];
963 	int err;
964 
965 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
966 	if (err < 0)
967 		return err;
968 
969 	if (tb[IFLA_IFNAME])
970 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
971 
972 	ifm = nlmsg_data(nlh);
973 	if (ifm->ifi_index > 0)
974 		dev = __dev_get_by_index(net, ifm->ifi_index);
975 	else if (tb[IFLA_IFNAME])
976 		dev = __dev_get_by_name(net, ifname);
977 	else
978 		return -EINVAL;
979 
980 	if (!dev)
981 		return -ENODEV;
982 
983 	ops = dev->rtnl_link_ops;
984 	if (!ops)
985 		return -EOPNOTSUPP;
986 
987 	ops->dellink(dev);
988 	return 0;
989 }
990 
rtnl_create_link(struct net * net,char * ifname,const struct rtnl_link_ops * ops,struct nlattr * tb[])991 struct net_device *rtnl_create_link(struct net *net, char *ifname,
992 		const struct rtnl_link_ops *ops, struct nlattr *tb[])
993 {
994 	int err;
995 	struct net_device *dev;
996 
997 	err = -ENOMEM;
998 	dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
999 	if (!dev)
1000 		goto err;
1001 
1002 	if (strchr(dev->name, '%')) {
1003 		err = dev_alloc_name(dev, dev->name);
1004 		if (err < 0)
1005 			goto err_free;
1006 	}
1007 
1008 	dev_net_set(dev, net);
1009 	dev->rtnl_link_ops = ops;
1010 
1011 	if (tb[IFLA_MTU])
1012 		dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1013 	if (tb[IFLA_ADDRESS])
1014 		memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1015 				nla_len(tb[IFLA_ADDRESS]));
1016 	if (tb[IFLA_BROADCAST])
1017 		memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1018 				nla_len(tb[IFLA_BROADCAST]));
1019 	if (tb[IFLA_TXQLEN])
1020 		dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1021 	if (tb[IFLA_OPERSTATE])
1022 		set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1023 	if (tb[IFLA_LINKMODE])
1024 		dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1025 
1026 	return dev;
1027 
1028 err_free:
1029 	free_netdev(dev);
1030 err:
1031 	return ERR_PTR(err);
1032 }
1033 
rtnl_newlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1034 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
1035 {
1036 	struct net *net = sock_net(skb->sk);
1037 	const struct rtnl_link_ops *ops;
1038 	struct net_device *dev;
1039 	struct ifinfomsg *ifm;
1040 	char kind[MODULE_NAME_LEN];
1041 	char ifname[IFNAMSIZ];
1042 	struct nlattr *tb[IFLA_MAX+1];
1043 	struct nlattr *linkinfo[IFLA_INFO_MAX+1];
1044 	int err;
1045 
1046 #ifdef CONFIG_MODULES
1047 replay:
1048 #endif
1049 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1050 	if (err < 0)
1051 		return err;
1052 
1053 	if (tb[IFLA_IFNAME])
1054 		nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
1055 	else
1056 		ifname[0] = '\0';
1057 
1058 	ifm = nlmsg_data(nlh);
1059 	if (ifm->ifi_index > 0)
1060 		dev = __dev_get_by_index(net, ifm->ifi_index);
1061 	else if (ifname[0])
1062 		dev = __dev_get_by_name(net, ifname);
1063 	else
1064 		dev = NULL;
1065 
1066 	if ((err = validate_linkmsg(dev, tb)) < 0)
1067 		return err;
1068 
1069 	if (tb[IFLA_LINKINFO]) {
1070 		err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
1071 				       tb[IFLA_LINKINFO], ifla_info_policy);
1072 		if (err < 0)
1073 			return err;
1074 	} else
1075 		memset(linkinfo, 0, sizeof(linkinfo));
1076 
1077 	if (linkinfo[IFLA_INFO_KIND]) {
1078 		nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
1079 		ops = rtnl_link_ops_get(kind);
1080 	} else {
1081 		kind[0] = '\0';
1082 		ops = NULL;
1083 	}
1084 
1085 	if (1) {
1086 		struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
1087 
1088 		if (ops) {
1089 			if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
1090 				err = nla_parse_nested(attr, ops->maxtype,
1091 						       linkinfo[IFLA_INFO_DATA],
1092 						       ops->policy);
1093 				if (err < 0)
1094 					return err;
1095 				data = attr;
1096 			}
1097 			if (ops->validate) {
1098 				err = ops->validate(tb, data);
1099 				if (err < 0)
1100 					return err;
1101 			}
1102 		}
1103 
1104 		if (dev) {
1105 			int modified = 0;
1106 
1107 			if (nlh->nlmsg_flags & NLM_F_EXCL)
1108 				return -EEXIST;
1109 			if (nlh->nlmsg_flags & NLM_F_REPLACE)
1110 				return -EOPNOTSUPP;
1111 
1112 			if (linkinfo[IFLA_INFO_DATA]) {
1113 				if (!ops || ops != dev->rtnl_link_ops ||
1114 				    !ops->changelink)
1115 					return -EOPNOTSUPP;
1116 
1117 				err = ops->changelink(dev, tb, data);
1118 				if (err < 0)
1119 					return err;
1120 				modified = 1;
1121 			}
1122 
1123 			return do_setlink(dev, ifm, tb, ifname, modified);
1124 		}
1125 
1126 		if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1127 			return -ENODEV;
1128 
1129 		if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1130 			return -EOPNOTSUPP;
1131 		if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1132 			return -EOPNOTSUPP;
1133 
1134 		if (!ops) {
1135 #ifdef CONFIG_MODULES
1136 			if (kind[0]) {
1137 				__rtnl_unlock();
1138 				request_module("rtnl-link-%s", kind);
1139 				rtnl_lock();
1140 				ops = rtnl_link_ops_get(kind);
1141 				if (ops)
1142 					goto replay;
1143 			}
1144 #endif
1145 			return -EOPNOTSUPP;
1146 		}
1147 
1148 		if (!ifname[0])
1149 			snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1150 
1151 		dev = rtnl_create_link(net, ifname, ops, tb);
1152 
1153 		if (IS_ERR(dev))
1154 			err = PTR_ERR(dev);
1155 		else if (ops->newlink)
1156 			err = ops->newlink(dev, tb, data);
1157 		else
1158 			err = register_netdevice(dev);
1159 
1160 		if (err < 0 && !IS_ERR(dev))
1161 			free_netdev(dev);
1162 		return err;
1163 	}
1164 }
1165 
rtnl_getlink(struct sk_buff * skb,struct nlmsghdr * nlh,void * arg)1166 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1167 {
1168 	struct net *net = sock_net(skb->sk);
1169 	struct ifinfomsg *ifm;
1170 	struct nlattr *tb[IFLA_MAX+1];
1171 	struct net_device *dev = NULL;
1172 	struct sk_buff *nskb;
1173 	int err;
1174 
1175 	err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1176 	if (err < 0)
1177 		return err;
1178 
1179 	ifm = nlmsg_data(nlh);
1180 	if (ifm->ifi_index > 0) {
1181 		dev = dev_get_by_index(net, ifm->ifi_index);
1182 		if (dev == NULL)
1183 			return -ENODEV;
1184 	} else
1185 		return -EINVAL;
1186 
1187 	nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1188 	if (nskb == NULL) {
1189 		err = -ENOBUFS;
1190 		goto errout;
1191 	}
1192 
1193 	err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1194 			       nlh->nlmsg_seq, 0, 0);
1195 	if (err < 0) {
1196 		/* -EMSGSIZE implies BUG in if_nlmsg_size */
1197 		WARN_ON(err == -EMSGSIZE);
1198 		kfree_skb(nskb);
1199 		goto errout;
1200 	}
1201 	err = rtnl_unicast(nskb, net, NETLINK_CB(skb).pid);
1202 errout:
1203 	dev_put(dev);
1204 
1205 	return err;
1206 }
1207 
rtnl_dump_all(struct sk_buff * skb,struct netlink_callback * cb)1208 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1209 {
1210 	int idx;
1211 	int s_idx = cb->family;
1212 
1213 	if (s_idx == 0)
1214 		s_idx = 1;
1215 	for (idx=1; idx<NPROTO; idx++) {
1216 		int type = cb->nlh->nlmsg_type-RTM_BASE;
1217 		if (idx < s_idx || idx == PF_PACKET)
1218 			continue;
1219 		if (rtnl_msg_handlers[idx] == NULL ||
1220 		    rtnl_msg_handlers[idx][type].dumpit == NULL)
1221 			continue;
1222 		if (idx > s_idx)
1223 			memset(&cb->args[0], 0, sizeof(cb->args));
1224 		if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1225 			break;
1226 	}
1227 	cb->family = idx;
1228 
1229 	return skb->len;
1230 }
1231 
rtmsg_ifinfo(int type,struct net_device * dev,unsigned change)1232 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1233 {
1234 	struct net *net = dev_net(dev);
1235 	struct sk_buff *skb;
1236 	int err = -ENOBUFS;
1237 
1238 	skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1239 	if (skb == NULL)
1240 		goto errout;
1241 
1242 	err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
1243 	if (err < 0) {
1244 		/* -EMSGSIZE implies BUG in if_nlmsg_size() */
1245 		WARN_ON(err == -EMSGSIZE);
1246 		kfree_skb(skb);
1247 		goto errout;
1248 	}
1249 	err = rtnl_notify(skb, net, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1250 errout:
1251 	if (err < 0)
1252 		rtnl_set_sk_err(net, RTNLGRP_LINK, err);
1253 }
1254 
1255 /* Protected by RTNL sempahore.  */
1256 static struct rtattr **rta_buf;
1257 static int rtattr_max;
1258 
1259 /* Process one rtnetlink message. */
1260 
rtnetlink_rcv_msg(struct sk_buff * skb,struct nlmsghdr * nlh)1261 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1262 {
1263 	struct net *net = sock_net(skb->sk);
1264 	rtnl_doit_func doit;
1265 	int sz_idx, kind;
1266 	int min_len;
1267 	int family;
1268 	int type;
1269 	int err;
1270 
1271 	type = nlh->nlmsg_type;
1272 	if (type > RTM_MAX)
1273 		return -EOPNOTSUPP;
1274 
1275 	type -= RTM_BASE;
1276 
1277 	/* All the messages must have at least 1 byte length */
1278 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1279 		return 0;
1280 
1281 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
1282 	if (family >= NPROTO)
1283 		return -EAFNOSUPPORT;
1284 
1285 	sz_idx = type>>2;
1286 	kind = type&3;
1287 
1288 	if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1289 		return -EPERM;
1290 
1291 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1292 		struct sock *rtnl;
1293 		rtnl_dumpit_func dumpit;
1294 
1295 		dumpit = rtnl_get_dumpit(family, type);
1296 		if (dumpit == NULL)
1297 			return -EOPNOTSUPP;
1298 
1299 		__rtnl_unlock();
1300 		rtnl = net->rtnl;
1301 		err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1302 		rtnl_lock();
1303 		return err;
1304 	}
1305 
1306 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1307 
1308 	min_len = rtm_min[sz_idx];
1309 	if (nlh->nlmsg_len < min_len)
1310 		return -EINVAL;
1311 
1312 	if (nlh->nlmsg_len > min_len) {
1313 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1314 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
1315 
1316 		while (RTA_OK(attr, attrlen)) {
1317 			unsigned flavor = attr->rta_type;
1318 			if (flavor) {
1319 				if (flavor > rta_max[sz_idx])
1320 					return -EINVAL;
1321 				rta_buf[flavor-1] = attr;
1322 			}
1323 			attr = RTA_NEXT(attr, attrlen);
1324 		}
1325 	}
1326 
1327 	doit = rtnl_get_doit(family, type);
1328 	if (doit == NULL)
1329 		return -EOPNOTSUPP;
1330 
1331 	return doit(skb, nlh, (void *)&rta_buf[0]);
1332 }
1333 
rtnetlink_rcv(struct sk_buff * skb)1334 static void rtnetlink_rcv(struct sk_buff *skb)
1335 {
1336 	rtnl_lock();
1337 	netlink_rcv_skb(skb, &rtnetlink_rcv_msg);
1338 	rtnl_unlock();
1339 }
1340 
rtnetlink_event(struct notifier_block * this,unsigned long event,void * ptr)1341 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1342 {
1343 	struct net_device *dev = ptr;
1344 
1345 	switch (event) {
1346 	case NETDEV_UNREGISTER:
1347 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1348 		break;
1349 	case NETDEV_REGISTER:
1350 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1351 		break;
1352 	case NETDEV_UP:
1353 	case NETDEV_DOWN:
1354 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1355 		break;
1356 	case NETDEV_CHANGE:
1357 	case NETDEV_GOING_DOWN:
1358 		break;
1359 	default:
1360 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1361 		break;
1362 	}
1363 	return NOTIFY_DONE;
1364 }
1365 
1366 static struct notifier_block rtnetlink_dev_notifier = {
1367 	.notifier_call	= rtnetlink_event,
1368 };
1369 
1370 
rtnetlink_net_init(struct net * net)1371 static int rtnetlink_net_init(struct net *net)
1372 {
1373 	struct sock *sk;
1374 	sk = netlink_kernel_create(net, NETLINK_ROUTE, RTNLGRP_MAX,
1375 				   rtnetlink_rcv, &rtnl_mutex, THIS_MODULE);
1376 	if (!sk)
1377 		return -ENOMEM;
1378 	net->rtnl = sk;
1379 	return 0;
1380 }
1381 
rtnetlink_net_exit(struct net * net)1382 static void rtnetlink_net_exit(struct net *net)
1383 {
1384 	netlink_kernel_release(net->rtnl);
1385 	net->rtnl = NULL;
1386 }
1387 
1388 static struct pernet_operations rtnetlink_net_ops = {
1389 	.init = rtnetlink_net_init,
1390 	.exit = rtnetlink_net_exit,
1391 };
1392 
rtnetlink_init(void)1393 void __init rtnetlink_init(void)
1394 {
1395 	int i;
1396 
1397 	rtattr_max = 0;
1398 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1399 		if (rta_max[i] > rtattr_max)
1400 			rtattr_max = rta_max[i];
1401 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1402 	if (!rta_buf)
1403 		panic("rtnetlink_init: cannot allocate rta_buf\n");
1404 
1405 	if (register_pernet_subsys(&rtnetlink_net_ops))
1406 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
1407 
1408 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1409 	register_netdevice_notifier(&rtnetlink_dev_notifier);
1410 
1411 	rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1412 	rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
1413 	rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1414 	rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1415 
1416 	rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1417 	rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
1418 }
1419 
1420 EXPORT_SYMBOL(__rta_fill);
1421 EXPORT_SYMBOL(rtnetlink_put_metrics);
1422 EXPORT_SYMBOL(rtnl_lock);
1423 EXPORT_SYMBOL(rtnl_trylock);
1424 EXPORT_SYMBOL(rtnl_unlock);
1425 EXPORT_SYMBOL(rtnl_is_locked);
1426 EXPORT_SYMBOL(rtnl_unicast);
1427 EXPORT_SYMBOL(rtnl_notify);
1428 EXPORT_SYMBOL(rtnl_set_sk_err);
1429 EXPORT_SYMBOL(rtnl_create_link);
1430 EXPORT_SYMBOL(ifla_policy);
1431