xref: /linux/net/core/rtnetlink.c (revision 13abf8130139c2ccd4962a7e5a8902be5e6cb5a7)
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/config.h>
20 #include <linux/errno.h>
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
31 #include <linux/mm.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/capability.h>
35 #include <linux/skbuff.h>
36 #include <linux/init.h>
37 #include <linux/security.h>
38 
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
41 #include <asm/string.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 
53 DECLARE_MUTEX(rtnl_sem);
54 
55 void rtnl_lock(void)
56 {
57 	rtnl_shlock();
58 }
59 
60 int rtnl_lock_interruptible(void)
61 {
62 	return down_interruptible(&rtnl_sem);
63 }
64 
65 void rtnl_unlock(void)
66 {
67 	rtnl_shunlock();
68 
69 	netdev_run_todo();
70 }
71 
72 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
73 {
74 	memset(tb, 0, sizeof(struct rtattr*)*maxattr);
75 
76 	while (RTA_OK(rta, len)) {
77 		unsigned flavor = rta->rta_type;
78 		if (flavor && flavor <= maxattr)
79 			tb[flavor-1] = rta;
80 		rta = RTA_NEXT(rta, len);
81 	}
82 	return 0;
83 }
84 
85 struct sock *rtnl;
86 
87 struct rtnetlink_link * rtnetlink_links[NPROTO];
88 
89 static const int rtm_min[RTM_NR_FAMILIES] =
90 {
91 	[RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
92 	[RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
93 	[RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
94 	[RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
95 	[RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct rtmsg)),
96 	[RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
97 	[RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
98 	[RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
99 	[RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
100 	[RTM_FAM(RTM_NEWPREFIX)]    = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
101 	[RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
102 	[RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
103 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NLMSG_LENGTH(sizeof(struct ndtmsg)),
104 };
105 
106 static const int rta_max[RTM_NR_FAMILIES] =
107 {
108 	[RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
109 	[RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
110 	[RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
111 	[RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
112 	[RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
113 	[RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
114 	[RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
115 	[RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
116 	[RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
117 	[RTM_FAM(RTM_NEWNEIGHTBL)]  = NDTA_MAX,
118 };
119 
120 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
121 {
122 	struct rtattr *rta;
123 	int size = RTA_LENGTH(attrlen);
124 
125 	rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
126 	rta->rta_type = attrtype;
127 	rta->rta_len = size;
128 	memcpy(RTA_DATA(rta), data, attrlen);
129 	memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
130 }
131 
132 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
133 {
134 	size_t ret = RTA_PAYLOAD(rta);
135 	char *src = RTA_DATA(rta);
136 
137 	if (ret > 0 && src[ret - 1] == '\0')
138 		ret--;
139 	if (size > 0) {
140 		size_t len = (ret >= size) ? size - 1 : ret;
141 		memset(dest, 0, size);
142 		memcpy(dest, src, len);
143 	}
144 	return ret;
145 }
146 
147 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
148 {
149 	int err = 0;
150 
151 	NETLINK_CB(skb).dst_group = group;
152 	if (echo)
153 		atomic_inc(&skb->users);
154 	netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
155 	if (echo)
156 		err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
157 	return err;
158 }
159 
160 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
161 {
162 	struct rtattr *mx = (struct rtattr*)skb->tail;
163 	int i;
164 
165 	RTA_PUT(skb, RTA_METRICS, 0, NULL);
166 	for (i=0; i<RTAX_MAX; i++) {
167 		if (metrics[i])
168 			RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
169 	}
170 	mx->rta_len = skb->tail - (u8*)mx;
171 	if (mx->rta_len == RTA_LENGTH(0))
172 		skb_trim(skb, (u8*)mx - skb->data);
173 	return 0;
174 
175 rtattr_failure:
176 	skb_trim(skb, (u8*)mx - skb->data);
177 	return -1;
178 }
179 
180 
181 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
182 				 int type, u32 pid, u32 seq, u32 change,
183 				 unsigned int flags)
184 {
185 	struct ifinfomsg *r;
186 	struct nlmsghdr  *nlh;
187 	unsigned char	 *b = skb->tail;
188 
189 	nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
190 	r = NLMSG_DATA(nlh);
191 	r->ifi_family = AF_UNSPEC;
192 	r->__ifi_pad = 0;
193 	r->ifi_type = dev->type;
194 	r->ifi_index = dev->ifindex;
195 	r->ifi_flags = dev_get_flags(dev);
196 	r->ifi_change = change;
197 
198 	RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
199 
200 	if (1) {
201 		u32 txqlen = dev->tx_queue_len;
202 		RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
203 	}
204 
205 	if (1) {
206 		u32 weight = dev->weight;
207 		RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
208 	}
209 
210 	if (1) {
211 		struct rtnl_link_ifmap map = {
212 			.mem_start   = dev->mem_start,
213 			.mem_end     = dev->mem_end,
214 			.base_addr   = dev->base_addr,
215 			.irq         = dev->irq,
216 			.dma         = dev->dma,
217 			.port        = dev->if_port,
218 		};
219 		RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
220 	}
221 
222 	if (dev->addr_len) {
223 		RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
224 		RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
225 	}
226 
227 	if (1) {
228 		u32 mtu = dev->mtu;
229 		RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
230 	}
231 
232 	if (dev->ifindex != dev->iflink) {
233 		u32 iflink = dev->iflink;
234 		RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
235 	}
236 
237 	if (dev->qdisc_sleeping)
238 		RTA_PUT(skb, IFLA_QDISC,
239 			strlen(dev->qdisc_sleeping->ops->id) + 1,
240 			dev->qdisc_sleeping->ops->id);
241 
242 	if (dev->master) {
243 		u32 master = dev->master->ifindex;
244 		RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
245 	}
246 
247 	if (dev->get_stats) {
248 		unsigned long *stats = (unsigned long*)dev->get_stats(dev);
249 		if (stats) {
250 			struct rtattr  *a;
251 			__u32	       *s;
252 			int		i;
253 			int		n = sizeof(struct rtnl_link_stats)/4;
254 
255 			a = __RTA_PUT(skb, IFLA_STATS, n*4);
256 			s = RTA_DATA(a);
257 			for (i=0; i<n; i++)
258 				s[i] = stats[i];
259 		}
260 	}
261 	nlh->nlmsg_len = skb->tail - b;
262 	return skb->len;
263 
264 nlmsg_failure:
265 rtattr_failure:
266 	skb_trim(skb, b - skb->data);
267 	return -1;
268 }
269 
270 static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
271 {
272 	int idx;
273 	int s_idx = cb->args[0];
274 	struct net_device *dev;
275 
276 	read_lock(&dev_base_lock);
277 	for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
278 		if (idx < s_idx)
279 			continue;
280 		if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
281 					  NETLINK_CB(cb->skb).pid,
282 					  cb->nlh->nlmsg_seq, 0,
283 					  NLM_F_MULTI) <= 0)
284 			break;
285 	}
286 	read_unlock(&dev_base_lock);
287 	cb->args[0] = idx;
288 
289 	return skb->len;
290 }
291 
292 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
293 {
294 	struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
295 	struct rtattr    **ida = arg;
296 	struct net_device *dev;
297 	int err, send_addr_notify = 0;
298 
299 	if (ifm->ifi_index >= 0)
300 		dev = dev_get_by_index(ifm->ifi_index);
301 	else if (ida[IFLA_IFNAME - 1]) {
302 		char ifname[IFNAMSIZ];
303 
304 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
305 		                   IFNAMSIZ) >= IFNAMSIZ)
306 			return -EINVAL;
307 		dev = dev_get_by_name(ifname);
308 	} else
309 		return -EINVAL;
310 
311 	if (!dev)
312 		return -ENODEV;
313 
314 	err = -EINVAL;
315 
316 	if (ifm->ifi_flags)
317 		dev_change_flags(dev, ifm->ifi_flags);
318 
319 	if (ida[IFLA_MAP - 1]) {
320 		struct rtnl_link_ifmap *u_map;
321 		struct ifmap k_map;
322 
323 		if (!dev->set_config) {
324 			err = -EOPNOTSUPP;
325 			goto out;
326 		}
327 
328 		if (!netif_device_present(dev)) {
329 			err = -ENODEV;
330 			goto out;
331 		}
332 
333 		if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
334 			goto out;
335 
336 		u_map = RTA_DATA(ida[IFLA_MAP - 1]);
337 
338 		k_map.mem_start = (unsigned long) u_map->mem_start;
339 		k_map.mem_end = (unsigned long) u_map->mem_end;
340 		k_map.base_addr = (unsigned short) u_map->base_addr;
341 		k_map.irq = (unsigned char) u_map->irq;
342 		k_map.dma = (unsigned char) u_map->dma;
343 		k_map.port = (unsigned char) u_map->port;
344 
345 		err = dev->set_config(dev, &k_map);
346 
347 		if (err)
348 			goto out;
349 	}
350 
351 	if (ida[IFLA_ADDRESS - 1]) {
352 		if (!dev->set_mac_address) {
353 			err = -EOPNOTSUPP;
354 			goto out;
355 		}
356 		if (!netif_device_present(dev)) {
357 			err = -ENODEV;
358 			goto out;
359 		}
360 		if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
361 			goto out;
362 
363 		err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
364 		if (err)
365 			goto out;
366 		send_addr_notify = 1;
367 	}
368 
369 	if (ida[IFLA_BROADCAST - 1]) {
370 		if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
371 			goto out;
372 		memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
373 		       dev->addr_len);
374 		send_addr_notify = 1;
375 	}
376 
377 	if (ida[IFLA_MTU - 1]) {
378 		if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
379 			goto out;
380 		err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
381 
382 		if (err)
383 			goto out;
384 
385 	}
386 
387 	if (ida[IFLA_TXQLEN - 1]) {
388 		if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
389 			goto out;
390 
391 		dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
392 	}
393 
394 	if (ida[IFLA_WEIGHT - 1]) {
395 		if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
396 			goto out;
397 
398 		dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
399 	}
400 
401 	if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
402 		char ifname[IFNAMSIZ];
403 
404 		if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
405 		                   IFNAMSIZ) >= IFNAMSIZ)
406 			goto out;
407 		err = dev_change_name(dev, ifname);
408 		if (err)
409 			goto out;
410 	}
411 
412 	err = 0;
413 
414 out:
415 	if (send_addr_notify)
416 		call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
417 
418 	dev_put(dev);
419 	return err;
420 }
421 
422 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
423 {
424 	int idx;
425 	int s_idx = cb->family;
426 
427 	if (s_idx == 0)
428 		s_idx = 1;
429 	for (idx=1; idx<NPROTO; idx++) {
430 		int type = cb->nlh->nlmsg_type-RTM_BASE;
431 		if (idx < s_idx || idx == PF_PACKET)
432 			continue;
433 		if (rtnetlink_links[idx] == NULL ||
434 		    rtnetlink_links[idx][type].dumpit == NULL)
435 			continue;
436 		if (idx > s_idx)
437 			memset(&cb->args[0], 0, sizeof(cb->args));
438 		if (rtnetlink_links[idx][type].dumpit(skb, cb))
439 			break;
440 	}
441 	cb->family = idx;
442 
443 	return skb->len;
444 }
445 
446 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
447 {
448 	struct sk_buff *skb;
449 	int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
450 			       sizeof(struct rtnl_link_ifmap) +
451 			       sizeof(struct rtnl_link_stats) + 128);
452 
453 	skb = alloc_skb(size, GFP_KERNEL);
454 	if (!skb)
455 		return;
456 
457 	if (rtnetlink_fill_ifinfo(skb, dev, type, current->pid, 0, change, 0) < 0) {
458 		kfree_skb(skb);
459 		return;
460 	}
461 	NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
462 	netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
463 }
464 
465 static int rtnetlink_done(struct netlink_callback *cb)
466 {
467 	return 0;
468 }
469 
470 /* Protected by RTNL sempahore.  */
471 static struct rtattr **rta_buf;
472 static int rtattr_max;
473 
474 /* Process one rtnetlink message. */
475 
476 static __inline__ int
477 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
478 {
479 	struct rtnetlink_link *link;
480 	struct rtnetlink_link *link_tab;
481 	int sz_idx, kind;
482 	int min_len;
483 	int family;
484 	int type;
485 	int err;
486 
487 	/* Only requests are handled by kernel now */
488 	if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
489 		return 0;
490 
491 	type = nlh->nlmsg_type;
492 
493 	/* A control message: ignore them */
494 	if (type < RTM_BASE)
495 		return 0;
496 
497 	/* Unknown message: reply with EINVAL */
498 	if (type > RTM_MAX)
499 		goto err_inval;
500 
501 	type -= RTM_BASE;
502 
503 	/* All the messages must have at least 1 byte length */
504 	if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
505 		return 0;
506 
507 	family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
508 	if (family >= NPROTO) {
509 		*errp = -EAFNOSUPPORT;
510 		return -1;
511 	}
512 
513 	link_tab = rtnetlink_links[family];
514 	if (link_tab == NULL)
515 		link_tab = rtnetlink_links[PF_UNSPEC];
516 	link = &link_tab[type];
517 
518 	sz_idx = type>>2;
519 	kind = type&3;
520 
521 	if (kind != 2 && security_netlink_recv(skb)) {
522 		*errp = -EPERM;
523 		return -1;
524 	}
525 
526 	if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
527 		u32 rlen;
528 
529 		if (link->dumpit == NULL)
530 			link = &(rtnetlink_links[PF_UNSPEC][type]);
531 
532 		if (link->dumpit == NULL)
533 			goto err_inval;
534 
535 		if ((*errp = netlink_dump_start(rtnl, skb, nlh,
536 						link->dumpit,
537 						rtnetlink_done)) != 0) {
538 			return -1;
539 		}
540 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
541 		if (rlen > skb->len)
542 			rlen = skb->len;
543 		skb_pull(skb, rlen);
544 		return -1;
545 	}
546 
547 	memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
548 
549 	min_len = rtm_min[sz_idx];
550 	if (nlh->nlmsg_len < min_len)
551 		goto err_inval;
552 
553 	if (nlh->nlmsg_len > min_len) {
554 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
555 		struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
556 
557 		while (RTA_OK(attr, attrlen)) {
558 			unsigned flavor = attr->rta_type;
559 			if (flavor) {
560 				if (flavor > rta_max[sz_idx])
561 					goto err_inval;
562 				rta_buf[flavor-1] = attr;
563 			}
564 			attr = RTA_NEXT(attr, attrlen);
565 		}
566 	}
567 
568 	if (link->doit == NULL)
569 		link = &(rtnetlink_links[PF_UNSPEC][type]);
570 	if (link->doit == NULL)
571 		goto err_inval;
572 	err = link->doit(skb, nlh, (void *)&rta_buf[0]);
573 
574 	*errp = err;
575 	return err;
576 
577 err_inval:
578 	*errp = -EINVAL;
579 	return -1;
580 }
581 
582 /*
583  * Process one packet of messages.
584  * Malformed skbs with wrong lengths of messages are discarded silently.
585  */
586 
587 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
588 {
589 	int err;
590 	struct nlmsghdr * nlh;
591 
592 	while (skb->len >= NLMSG_SPACE(0)) {
593 		u32 rlen;
594 
595 		nlh = (struct nlmsghdr *)skb->data;
596 		if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
597 			return 0;
598 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
599 		if (rlen > skb->len)
600 			rlen = skb->len;
601 		if (rtnetlink_rcv_msg(skb, nlh, &err)) {
602 			/* Not error, but we must interrupt processing here:
603 			 *   Note, that in this case we do not pull message
604 			 *   from skb, it will be processed later.
605 			 */
606 			if (err == 0)
607 				return -1;
608 			netlink_ack(skb, nlh, err);
609 		} else if (nlh->nlmsg_flags&NLM_F_ACK)
610 			netlink_ack(skb, nlh, 0);
611 		skb_pull(skb, rlen);
612 	}
613 
614 	return 0;
615 }
616 
617 /*
618  *  rtnetlink input queue processing routine:
619  *	- process as much as there was in the queue upon entry.
620  *	- feed skbs to rtnetlink_rcv_skb, until it refuse a message,
621  *	  that will occur, when a dump started.
622  */
623 
624 static void rtnetlink_rcv(struct sock *sk, int len)
625 {
626 	unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
627 
628 	do {
629 		struct sk_buff *skb;
630 
631 		rtnl_lock();
632 
633 		if (qlen > skb_queue_len(&sk->sk_receive_queue))
634 			qlen = skb_queue_len(&sk->sk_receive_queue);
635 
636 		for (; qlen; qlen--) {
637 			skb = skb_dequeue(&sk->sk_receive_queue);
638 			if (rtnetlink_rcv_skb(skb)) {
639 				if (skb->len)
640 					skb_queue_head(&sk->sk_receive_queue,
641 						       skb);
642 				else {
643 					kfree_skb(skb);
644 					qlen--;
645 				}
646 				break;
647 			}
648 			kfree_skb(skb);
649 		}
650 
651 		up(&rtnl_sem);
652 
653 		netdev_run_todo();
654 	} while (qlen);
655 }
656 
657 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
658 {
659 	[RTM_GETLINK     - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
660 	[RTM_SETLINK     - RTM_BASE] = { .doit   = do_setlink		 },
661 	[RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
662 	[RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
663 	[RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add		 },
664 	[RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete		 },
665 	[RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info	 },
666 	[RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all	 },
667 	[RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info	 },
668 	[RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set		 },
669 };
670 
671 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
672 {
673 	struct net_device *dev = ptr;
674 	switch (event) {
675 	case NETDEV_UNREGISTER:
676 		rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
677 		break;
678 	case NETDEV_REGISTER:
679 		rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
680 		break;
681 	case NETDEV_UP:
682 	case NETDEV_DOWN:
683 		rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
684 		break;
685 	case NETDEV_CHANGE:
686 	case NETDEV_GOING_DOWN:
687 		break;
688 	default:
689 		rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
690 		break;
691 	}
692 	return NOTIFY_DONE;
693 }
694 
695 static struct notifier_block rtnetlink_dev_notifier = {
696 	.notifier_call	= rtnetlink_event,
697 };
698 
699 void __init rtnetlink_init(void)
700 {
701 	int i;
702 
703 	rtattr_max = 0;
704 	for (i = 0; i < ARRAY_SIZE(rta_max); i++)
705 		if (rta_max[i] > rtattr_max)
706 			rtattr_max = rta_max[i];
707 	rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
708 	if (!rta_buf)
709 		panic("rtnetlink_init: cannot allocate rta_buf\n");
710 
711 	rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
712 	                             THIS_MODULE);
713 	if (rtnl == NULL)
714 		panic("rtnetlink_init: cannot initialize rtnetlink\n");
715 	netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
716 	register_netdevice_notifier(&rtnetlink_dev_notifier);
717 	rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
718 	rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
719 }
720 
721 EXPORT_SYMBOL(__rta_fill);
722 EXPORT_SYMBOL(rtattr_strlcpy);
723 EXPORT_SYMBOL(rtattr_parse);
724 EXPORT_SYMBOL(rtnetlink_links);
725 EXPORT_SYMBOL(rtnetlink_put_metrics);
726 EXPORT_SYMBOL(rtnl);
727 EXPORT_SYMBOL(rtnl_lock);
728 EXPORT_SYMBOL(rtnl_lock_interruptible);
729 EXPORT_SYMBOL(rtnl_sem);
730 EXPORT_SYMBOL(rtnl_unlock);
731