blob: 879237c378f8e8df85580d250d191531c250861f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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
53DECLARE_MUTEX(rtnl_sem);
54
55void rtnl_lock(void)
56{
57 rtnl_shlock();
58}
59
60int rtnl_lock_interruptible(void)
61{
62 return down_interruptible(&rtnl_sem);
63}
64
65void rtnl_unlock(void)
66{
67 rtnl_shunlock();
68
69 netdev_run_todo();
70}
71
72int 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
85struct sock *rtnl;
86
87struct rtnetlink_link * rtnetlink_links[NPROTO];
88
Thomas Grafdb46edc2005-05-03 14:29:39 -070089static const int rtm_min[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Thomas Graff90a0a72005-05-03 14:29:00 -070091 [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)),
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700103 [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104};
105
Thomas Grafdb46edc2005-05-03 14:29:39 -0700106static const int rta_max[RTM_NR_FAMILIES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Thomas Graff90a0a72005-05-03 14:29:00 -0700108 [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,
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700117 [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118};
119
120void __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);
Patrick McHardyb3563c42005-06-28 12:54:43 -0700129 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132size_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
147int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
148{
149 int err = 0;
150
151 NETLINK_CB(skb).dst_groups = 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
160int 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
175rtattr_failure:
176 skb_trim(skb, (u8*)mx - skb->data);
177 return -1;
178}
179
180
181static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700182 int type, u32 pid, u32 seq, u32 change,
183 unsigned int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 struct ifinfomsg *r;
186 struct nlmsghdr *nlh;
187 unsigned char *b = skb->tail;
188
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700189 nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 r = NLMSG_DATA(nlh);
191 r->ifi_family = AF_UNSPEC;
192 r->ifi_type = dev->type;
193 r->ifi_index = dev->ifindex;
194 r->ifi_flags = dev_get_flags(dev);
195 r->ifi_change = change;
196
197 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
198
199 if (1) {
200 u32 txqlen = dev->tx_queue_len;
201 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
202 }
203
204 if (1) {
205 u32 weight = dev->weight;
206 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
207 }
208
209 if (1) {
210 struct rtnl_link_ifmap map = {
211 .mem_start = dev->mem_start,
212 .mem_end = dev->mem_end,
213 .base_addr = dev->base_addr,
214 .irq = dev->irq,
215 .dma = dev->dma,
216 .port = dev->if_port,
217 };
218 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
219 }
220
221 if (dev->addr_len) {
222 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
223 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
224 }
225
226 if (1) {
227 u32 mtu = dev->mtu;
228 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
229 }
230
231 if (dev->ifindex != dev->iflink) {
232 u32 iflink = dev->iflink;
233 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
234 }
235
236 if (dev->qdisc_sleeping)
237 RTA_PUT(skb, IFLA_QDISC,
238 strlen(dev->qdisc_sleeping->ops->id) + 1,
239 dev->qdisc_sleeping->ops->id);
240
241 if (dev->master) {
242 u32 master = dev->master->ifindex;
243 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
244 }
245
246 if (dev->get_stats) {
247 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
248 if (stats) {
249 struct rtattr *a;
250 __u32 *s;
251 int i;
252 int n = sizeof(struct rtnl_link_stats)/4;
253
254 a = __RTA_PUT(skb, IFLA_STATS, n*4);
255 s = RTA_DATA(a);
256 for (i=0; i<n; i++)
257 s[i] = stats[i];
258 }
259 }
260 nlh->nlmsg_len = skb->tail - b;
261 return skb->len;
262
263nlmsg_failure:
264rtattr_failure:
265 skb_trim(skb, b - skb->data);
266 return -1;
267}
268
269static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
270{
271 int idx;
272 int s_idx = cb->args[0];
273 struct net_device *dev;
274
275 read_lock(&dev_base_lock);
276 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
277 if (idx < s_idx)
278 continue;
Jamal Hadi Salimb6544c02005-06-18 22:54:12 -0700279 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
280 NETLINK_CB(cb->skb).pid,
281 cb->nlh->nlmsg_seq, 0,
282 NLM_F_MULTI) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 break;
284 }
285 read_unlock(&dev_base_lock);
286 cb->args[0] = idx;
287
288 return skb->len;
289}
290
291static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
292{
293 struct ifinfomsg *ifm = NLMSG_DATA(nlh);
294 struct rtattr **ida = arg;
295 struct net_device *dev;
296 int err, send_addr_notify = 0;
297
298 if (ifm->ifi_index >= 0)
299 dev = dev_get_by_index(ifm->ifi_index);
300 else if (ida[IFLA_IFNAME - 1]) {
301 char ifname[IFNAMSIZ];
302
303 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
304 IFNAMSIZ) >= IFNAMSIZ)
305 return -EINVAL;
306 dev = dev_get_by_name(ifname);
307 } else
308 return -EINVAL;
309
310 if (!dev)
311 return -ENODEV;
312
313 err = -EINVAL;
314
315 if (ifm->ifi_flags)
316 dev_change_flags(dev, ifm->ifi_flags);
317
318 if (ida[IFLA_MAP - 1]) {
319 struct rtnl_link_ifmap *u_map;
320 struct ifmap k_map;
321
322 if (!dev->set_config) {
323 err = -EOPNOTSUPP;
324 goto out;
325 }
326
327 if (!netif_device_present(dev)) {
328 err = -ENODEV;
329 goto out;
330 }
331
332 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
333 goto out;
334
335 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
336
337 k_map.mem_start = (unsigned long) u_map->mem_start;
338 k_map.mem_end = (unsigned long) u_map->mem_end;
339 k_map.base_addr = (unsigned short) u_map->base_addr;
340 k_map.irq = (unsigned char) u_map->irq;
341 k_map.dma = (unsigned char) u_map->dma;
342 k_map.port = (unsigned char) u_map->port;
343
344 err = dev->set_config(dev, &k_map);
345
346 if (err)
347 goto out;
348 }
349
350 if (ida[IFLA_ADDRESS - 1]) {
351 if (!dev->set_mac_address) {
352 err = -EOPNOTSUPP;
353 goto out;
354 }
355 if (!netif_device_present(dev)) {
356 err = -ENODEV;
357 goto out;
358 }
359 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
360 goto out;
361
362 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
363 if (err)
364 goto out;
365 send_addr_notify = 1;
366 }
367
368 if (ida[IFLA_BROADCAST - 1]) {
369 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
370 goto out;
371 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
372 dev->addr_len);
373 send_addr_notify = 1;
374 }
375
376 if (ida[IFLA_MTU - 1]) {
377 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
378 goto out;
379 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
380
381 if (err)
382 goto out;
383
384 }
385
386 if (ida[IFLA_TXQLEN - 1]) {
387 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
388 goto out;
389
390 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
391 }
392
393 if (ida[IFLA_WEIGHT - 1]) {
394 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
395 goto out;
396
397 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
398 }
399
400 if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
401 char ifname[IFNAMSIZ];
402
403 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
404 IFNAMSIZ) >= IFNAMSIZ)
405 goto out;
406 err = dev_change_name(dev, ifname);
407 if (err)
408 goto out;
409 }
410
411 err = 0;
412
413out:
414 if (send_addr_notify)
415 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
416
417 dev_put(dev);
418 return err;
419}
420
421static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
422{
423 int idx;
424 int s_idx = cb->family;
425
426 if (s_idx == 0)
427 s_idx = 1;
428 for (idx=1; idx<NPROTO; idx++) {
429 int type = cb->nlh->nlmsg_type-RTM_BASE;
430 if (idx < s_idx || idx == PF_PACKET)
431 continue;
432 if (rtnetlink_links[idx] == NULL ||
433 rtnetlink_links[idx][type].dumpit == NULL)
434 continue;
435 if (idx > s_idx)
436 memset(&cb->args[0], 0, sizeof(cb->args));
437 if (rtnetlink_links[idx][type].dumpit(skb, cb))
438 break;
439 }
440 cb->family = idx;
441
442 return skb->len;
443}
444
445void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
446{
447 struct sk_buff *skb;
448 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
449 sizeof(struct rtnl_link_ifmap) +
450 sizeof(struct rtnl_link_stats) + 128);
451
452 skb = alloc_skb(size, GFP_KERNEL);
453 if (!skb)
454 return;
455
Jamal Hadi Salim9ed19f32005-06-18 22:55:51 -0700456 if (rtnetlink_fill_ifinfo(skb, dev, type, current->pid, 0, change, 0) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 kfree_skb(skb);
458 return;
459 }
460 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
461 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
462}
463
464static int rtnetlink_done(struct netlink_callback *cb)
465{
466 return 0;
467}
468
469/* Protected by RTNL sempahore. */
470static struct rtattr **rta_buf;
471static int rtattr_max;
472
473/* Process one rtnetlink message. */
474
475static __inline__ int
476rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
477{
478 struct rtnetlink_link *link;
479 struct rtnetlink_link *link_tab;
480 int sz_idx, kind;
481 int min_len;
482 int family;
483 int type;
484 int err;
485
486 /* Only requests are handled by kernel now */
487 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
488 return 0;
489
490 type = nlh->nlmsg_type;
491
492 /* A control message: ignore them */
493 if (type < RTM_BASE)
494 return 0;
495
496 /* Unknown message: reply with EINVAL */
497 if (type > RTM_MAX)
498 goto err_inval;
499
500 type -= RTM_BASE;
501
502 /* All the messages must have at least 1 byte length */
503 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
504 return 0;
505
506 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
507 if (family >= NPROTO) {
508 *errp = -EAFNOSUPPORT;
509 return -1;
510 }
511
512 link_tab = rtnetlink_links[family];
513 if (link_tab == NULL)
514 link_tab = rtnetlink_links[PF_UNSPEC];
515 link = &link_tab[type];
516
517 sz_idx = type>>2;
518 kind = type&3;
519
520 if (kind != 2 && security_netlink_recv(skb)) {
521 *errp = -EPERM;
522 return -1;
523 }
524
525 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
526 u32 rlen;
527
528 if (link->dumpit == NULL)
529 link = &(rtnetlink_links[PF_UNSPEC][type]);
530
531 if (link->dumpit == NULL)
532 goto err_inval;
533
534 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
535 link->dumpit,
536 rtnetlink_done)) != 0) {
537 return -1;
538 }
539 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
540 if (rlen > skb->len)
541 rlen = skb->len;
542 skb_pull(skb, rlen);
543 return -1;
544 }
545
546 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
547
548 min_len = rtm_min[sz_idx];
549 if (nlh->nlmsg_len < min_len)
550 goto err_inval;
551
552 if (nlh->nlmsg_len > min_len) {
553 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
554 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
555
556 while (RTA_OK(attr, attrlen)) {
557 unsigned flavor = attr->rta_type;
558 if (flavor) {
559 if (flavor > rta_max[sz_idx])
560 goto err_inval;
561 rta_buf[flavor-1] = attr;
562 }
563 attr = RTA_NEXT(attr, attrlen);
564 }
565 }
566
567 if (link->doit == NULL)
568 link = &(rtnetlink_links[PF_UNSPEC][type]);
569 if (link->doit == NULL)
570 goto err_inval;
571 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
572
573 *errp = err;
574 return err;
575
576err_inval:
577 *errp = -EINVAL;
578 return -1;
579}
580
581/*
582 * Process one packet of messages.
583 * Malformed skbs with wrong lengths of messages are discarded silently.
584 */
585
586static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
587{
588 int err;
589 struct nlmsghdr * nlh;
590
591 while (skb->len >= NLMSG_SPACE(0)) {
592 u32 rlen;
593
594 nlh = (struct nlmsghdr *)skb->data;
595 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
596 return 0;
597 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
598 if (rlen > skb->len)
599 rlen = skb->len;
600 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
601 /* Not error, but we must interrupt processing here:
602 * Note, that in this case we do not pull message
603 * from skb, it will be processed later.
604 */
605 if (err == 0)
606 return -1;
607 netlink_ack(skb, nlh, err);
608 } else if (nlh->nlmsg_flags&NLM_F_ACK)
609 netlink_ack(skb, nlh, 0);
610 skb_pull(skb, rlen);
611 }
612
613 return 0;
614}
615
616/*
617 * rtnetlink input queue processing routine:
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700618 * - process as much as there was in the queue upon entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700620 * that will occur, when a dump started.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622
623static void rtnetlink_rcv(struct sock *sk, int len)
624{
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700625 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 do {
628 struct sk_buff *skb;
629
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700630 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700632 if (qlen > skb_queue_len(&sk->sk_receive_queue))
633 qlen = skb_queue_len(&sk->sk_receive_queue);
634
David S. Miller09e14302005-05-03 15:30:05 -0700635 for (; qlen; qlen--) {
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700636 skb = skb_dequeue(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (rtnetlink_rcv_skb(skb)) {
David S. Miller09e14302005-05-03 15:30:05 -0700638 if (skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 skb_queue_head(&sk->sk_receive_queue,
640 skb);
David S. Miller0f4821e2005-05-03 16:15:59 -0700641 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 kfree_skb(skb);
David S. Miller0f4821e2005-05-03 16:15:59 -0700643 qlen--;
644 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 break;
646 }
647 kfree_skb(skb);
648 }
649
650 up(&rtnl_sem);
651
652 netdev_run_todo();
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700653 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
Thomas Grafdb46edc2005-05-03 14:29:39 -0700656static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
Thomas Grafc7fb64d2005-06-18 22:50:55 -0700658 [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
659 [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
660 [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
661 [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
662 [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
663 [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
664 [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
665 [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
666 [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
667 [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668};
669
670static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
671{
672 struct net_device *dev = ptr;
673 switch (event) {
674 case NETDEV_UNREGISTER:
675 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
676 break;
677 case NETDEV_REGISTER:
678 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
679 break;
680 case NETDEV_UP:
681 case NETDEV_DOWN:
682 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
683 break;
684 case NETDEV_CHANGE:
685 case NETDEV_GOING_DOWN:
686 break;
687 default:
688 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
689 break;
690 }
691 return NOTIFY_DONE;
692}
693
694static struct notifier_block rtnetlink_dev_notifier = {
695 .notifier_call = rtnetlink_event,
696};
697
698void __init rtnetlink_init(void)
699{
700 int i;
701
702 rtattr_max = 0;
703 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
704 if (rta_max[i] > rtattr_max)
705 rtattr_max = rta_max[i];
706 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
707 if (!rta_buf)
708 panic("rtnetlink_init: cannot allocate rta_buf\n");
709
710 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
711 if (rtnl == NULL)
712 panic("rtnetlink_init: cannot initialize rtnetlink\n");
713 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
714 register_netdevice_notifier(&rtnetlink_dev_notifier);
715 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
716 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
717}
718
719EXPORT_SYMBOL(__rta_fill);
720EXPORT_SYMBOL(rtattr_strlcpy);
721EXPORT_SYMBOL(rtattr_parse);
722EXPORT_SYMBOL(rtnetlink_links);
723EXPORT_SYMBOL(rtnetlink_put_metrics);
724EXPORT_SYMBOL(rtnl);
725EXPORT_SYMBOL(rtnl_lock);
726EXPORT_SYMBOL(rtnl_lock_interruptible);
727EXPORT_SYMBOL(rtnl_sem);
728EXPORT_SYMBOL(rtnl_unlock);