]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/rtnetlink.c
[XFRM]: Use generic netlink receive queue processor
[net-next-2.6.git] / net / core / rtnetlink.c
CommitLineData
1da177e4
LT
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>
1da177e4
LT
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
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
db46edc6 89static const int rtm_min[RTM_NR_FAMILIES] =
1da177e4 90{
f90a0a74
TG
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)),
c7fb64db 103 [RTM_FAM(RTM_NEWNEIGHTBL)] = NLMSG_LENGTH(sizeof(struct ndtmsg)),
1da177e4
LT
104};
105
db46edc6 106static const int rta_max[RTM_NR_FAMILIES] =
1da177e4 107{
f90a0a74
TG
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,
c7fb64db 117 [RTM_FAM(RTM_NEWNEIGHTBL)] = NDTA_MAX,
1da177e4
LT
118};
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);
b3563c4f 129 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
1da177e4
LT
130}
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
ac6d439d 151 NETLINK_CB(skb).dst_group = group;
1da177e4
LT
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,
b6544c0b
JHS
182 int type, u32 pid, u32 seq, u32 change,
183 unsigned int flags)
1da177e4
LT
184{
185 struct ifinfomsg *r;
186 struct nlmsghdr *nlh;
187 unsigned char *b = skb->tail;
188
b6544c0b 189 nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
1da177e4
LT
190 r = NLMSG_DATA(nlh);
191 r->ifi_family = AF_UNSPEC;
9ef1d4c7 192 r->__ifi_pad = 0;
1da177e4
LT
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
264nlmsg_failure:
265rtattr_failure:
266 skb_trim(skb, b - skb->data);
267 return -1;
268}
269
270static 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;
b6544c0b
JHS
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)
1da177e4
LT
284 break;
285 }
286 read_unlock(&dev_base_lock);
287 cb->args[0] = idx;
288
289 return skb->len;
290}
291
292static 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
414out:
415 if (send_addr_notify)
416 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
417
418 dev_put(dev);
419 return err;
420}
421
422static 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
446void 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
9ed19f33 457 if (rtnetlink_fill_ifinfo(skb, dev, type, current->pid, 0, change, 0) < 0) {
1da177e4
LT
458 kfree_skb(skb);
459 return;
460 }
ac6d439d
PM
461 NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
462 netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
1da177e4
LT
463}
464
1da177e4
LT
465/* Protected by RTNL sempahore. */
466static struct rtattr **rta_buf;
467static int rtattr_max;
468
469/* Process one rtnetlink message. */
470
471static __inline__ int
472rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
473{
474 struct rtnetlink_link *link;
475 struct rtnetlink_link *link_tab;
476 int sz_idx, kind;
477 int min_len;
478 int family;
479 int type;
480 int err;
481
482 /* Only requests are handled by kernel now */
483 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
484 return 0;
485
486 type = nlh->nlmsg_type;
487
488 /* A control message: ignore them */
489 if (type < RTM_BASE)
490 return 0;
491
492 /* Unknown message: reply with EINVAL */
493 if (type > RTM_MAX)
494 goto err_inval;
495
496 type -= RTM_BASE;
497
498 /* All the messages must have at least 1 byte length */
499 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
500 return 0;
501
502 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
503 if (family >= NPROTO) {
504 *errp = -EAFNOSUPPORT;
505 return -1;
506 }
507
508 link_tab = rtnetlink_links[family];
509 if (link_tab == NULL)
510 link_tab = rtnetlink_links[PF_UNSPEC];
511 link = &link_tab[type];
512
513 sz_idx = type>>2;
514 kind = type&3;
515
516 if (kind != 2 && security_netlink_recv(skb)) {
517 *errp = -EPERM;
518 return -1;
519 }
520
521 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
522 u32 rlen;
523
524 if (link->dumpit == NULL)
525 link = &(rtnetlink_links[PF_UNSPEC][type]);
526
527 if (link->dumpit == NULL)
528 goto err_inval;
529
530 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
a8f74b22 531 link->dumpit, NULL)) != 0) {
1da177e4
LT
532 return -1;
533 }
534 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
535 if (rlen > skb->len)
536 rlen = skb->len;
537 skb_pull(skb, rlen);
538 return -1;
539 }
540
541 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
542
543 min_len = rtm_min[sz_idx];
544 if (nlh->nlmsg_len < min_len)
545 goto err_inval;
546
547 if (nlh->nlmsg_len > min_len) {
548 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
549 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
550
551 while (RTA_OK(attr, attrlen)) {
552 unsigned flavor = attr->rta_type;
553 if (flavor) {
554 if (flavor > rta_max[sz_idx])
555 goto err_inval;
556 rta_buf[flavor-1] = attr;
557 }
558 attr = RTA_NEXT(attr, attrlen);
559 }
560 }
561
562 if (link->doit == NULL)
563 link = &(rtnetlink_links[PF_UNSPEC][type]);
564 if (link->doit == NULL)
565 goto err_inval;
566 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
567
568 *errp = err;
569 return err;
570
571err_inval:
572 *errp = -EINVAL;
573 return -1;
574}
575
576/*
577 * Process one packet of messages.
578 * Malformed skbs with wrong lengths of messages are discarded silently.
579 */
580
581static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
582{
583 int err;
584 struct nlmsghdr * nlh;
585
586 while (skb->len >= NLMSG_SPACE(0)) {
587 u32 rlen;
588
589 nlh = (struct nlmsghdr *)skb->data;
590 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
591 return 0;
592 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
593 if (rlen > skb->len)
594 rlen = skb->len;
595 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
596 /* Not error, but we must interrupt processing here:
597 * Note, that in this case we do not pull message
598 * from skb, it will be processed later.
599 */
600 if (err == 0)
601 return -1;
602 netlink_ack(skb, nlh, err);
603 } else if (nlh->nlmsg_flags&NLM_F_ACK)
604 netlink_ack(skb, nlh, 0);
605 skb_pull(skb, rlen);
606 }
607
608 return 0;
609}
610
611/*
612 * rtnetlink input queue processing routine:
2a0a6ebe 613 * - process as much as there was in the queue upon entry.
1da177e4 614 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
2a0a6ebe 615 * that will occur, when a dump started.
1da177e4
LT
616 */
617
618static void rtnetlink_rcv(struct sock *sk, int len)
619{
2a0a6ebe
HX
620 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
621
1da177e4
LT
622 do {
623 struct sk_buff *skb;
624
2a0a6ebe
HX
625 rtnl_lock();
626
627 if (qlen > skb_queue_len(&sk->sk_receive_queue))
628 qlen = skb_queue_len(&sk->sk_receive_queue);
1da177e4 629
09e14305 630 for (; qlen; qlen--) {
2a0a6ebe 631 skb = skb_dequeue(&sk->sk_receive_queue);
1da177e4 632 if (rtnetlink_rcv_skb(skb)) {
09e14305 633 if (skb->len)
1da177e4
LT
634 skb_queue_head(&sk->sk_receive_queue,
635 skb);
0f4821e7 636 else {
1da177e4 637 kfree_skb(skb);
0f4821e7
DM
638 qlen--;
639 }
1da177e4
LT
640 break;
641 }
642 kfree_skb(skb);
643 }
644
645 up(&rtnl_sem);
646
647 netdev_run_todo();
2a0a6ebe 648 } while (qlen);
1da177e4
LT
649}
650
db46edc6 651static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
1da177e4 652{
c7fb64db
TG
653 [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
654 [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
655 [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
656 [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
657 [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
658 [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
659 [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
660 [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
661 [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info },
662 [RTM_SETNEIGHTBL - RTM_BASE] = { .doit = neightbl_set },
1da177e4
LT
663};
664
665static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
666{
667 struct net_device *dev = ptr;
668 switch (event) {
669 case NETDEV_UNREGISTER:
670 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
671 break;
672 case NETDEV_REGISTER:
673 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
674 break;
675 case NETDEV_UP:
676 case NETDEV_DOWN:
677 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
678 break;
679 case NETDEV_CHANGE:
680 case NETDEV_GOING_DOWN:
681 break;
682 default:
683 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
684 break;
685 }
686 return NOTIFY_DONE;
687}
688
689static struct notifier_block rtnetlink_dev_notifier = {
690 .notifier_call = rtnetlink_event,
691};
692
693void __init rtnetlink_init(void)
694{
695 int i;
696
697 rtattr_max = 0;
698 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
699 if (rta_max[i] > rtattr_max)
700 rtattr_max = rta_max[i];
701 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
702 if (!rta_buf)
703 panic("rtnetlink_init: cannot allocate rta_buf\n");
704
06628607
PM
705 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
706 THIS_MODULE);
1da177e4
LT
707 if (rtnl == NULL)
708 panic("rtnetlink_init: cannot initialize rtnetlink\n");
709 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
710 register_netdevice_notifier(&rtnetlink_dev_notifier);
711 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
712 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
713}
714
715EXPORT_SYMBOL(__rta_fill);
716EXPORT_SYMBOL(rtattr_strlcpy);
717EXPORT_SYMBOL(rtattr_parse);
718EXPORT_SYMBOL(rtnetlink_links);
719EXPORT_SYMBOL(rtnetlink_put_metrics);
720EXPORT_SYMBOL(rtnl);
721EXPORT_SYMBOL(rtnl_lock);
722EXPORT_SYMBOL(rtnl_lock_interruptible);
723EXPORT_SYMBOL(rtnl_sem);
724EXPORT_SYMBOL(rtnl_unlock);