vserver 2.0 rc7
[linux-2.6.git] / net / core / rtnetlink.c
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 };
104
105 static const int rta_max[RTM_NR_FAMILIES] =
106 {
107         [RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
108         [RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
109         [RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
110         [RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
111         [RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
112         [RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
113         [RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
114         [RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
115         [RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
116 };
117
118 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
119 {
120         struct rtattr *rta;
121         int size = RTA_LENGTH(attrlen);
122
123         rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
124         rta->rta_type = attrtype;
125         rta->rta_len = size;
126         memcpy(RTA_DATA(rta), data, attrlen);
127 }
128
129 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
130 {
131         size_t ret = RTA_PAYLOAD(rta);
132         char *src = RTA_DATA(rta);
133
134         if (ret > 0 && src[ret - 1] == '\0')
135                 ret--;
136         if (size > 0) {
137                 size_t len = (ret >= size) ? size - 1 : ret;
138                 memset(dest, 0, size);
139                 memcpy(dest, src, len);
140         }
141         return ret;
142 }
143
144 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
145 {
146         int err = 0;
147
148         NETLINK_CB(skb).dst_groups = group;
149         if (echo)
150                 atomic_inc(&skb->users);
151         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
152         if (echo)
153                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
154         return err;
155 }
156
157 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
158 {
159         struct rtattr *mx = (struct rtattr*)skb->tail;
160         int i;
161
162         RTA_PUT(skb, RTA_METRICS, 0, NULL);
163         for (i=0; i<RTAX_MAX; i++) {
164                 if (metrics[i])
165                         RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
166         }
167         mx->rta_len = skb->tail - (u8*)mx;
168         if (mx->rta_len == RTA_LENGTH(0))
169                 skb_trim(skb, (u8*)mx - skb->data);
170         return 0;
171
172 rtattr_failure:
173         skb_trim(skb, (u8*)mx - skb->data);
174         return -1;
175 }
176
177
178 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
179                                  int type, u32 pid, u32 seq, u32 change)
180 {
181         struct ifinfomsg *r;
182         struct nlmsghdr  *nlh;
183         unsigned char    *b = skb->tail;
184
185         nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
186         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
187         r = NLMSG_DATA(nlh);
188         r->ifi_family = AF_UNSPEC;
189         r->ifi_type = dev->type;
190         r->ifi_index = dev->ifindex;
191         r->ifi_flags = dev_get_flags(dev);
192         r->ifi_change = change;
193
194         RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
195
196         if (1) {
197                 u32 txqlen = dev->tx_queue_len;
198                 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
199         }
200
201         if (1) {
202                 u32 weight = dev->weight;
203                 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
204         }
205
206         if (1) {
207                 struct rtnl_link_ifmap map = {
208                         .mem_start   = dev->mem_start,
209                         .mem_end     = dev->mem_end,
210                         .base_addr   = dev->base_addr,
211                         .irq         = dev->irq,
212                         .dma         = dev->dma,
213                         .port        = dev->if_port,
214                 };
215                 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
216         }
217
218         if (dev->addr_len) {
219                 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
220                 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
221         }
222
223         if (1) {
224                 u32 mtu = dev->mtu;
225                 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
226         }
227
228         if (dev->ifindex != dev->iflink) {
229                 u32 iflink = dev->iflink;
230                 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
231         }
232
233         if (dev->qdisc_sleeping)
234                 RTA_PUT(skb, IFLA_QDISC,
235                         strlen(dev->qdisc_sleeping->ops->id) + 1,
236                         dev->qdisc_sleeping->ops->id);
237         
238         if (dev->master) {
239                 u32 master = dev->master->ifindex;
240                 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
241         }
242
243         if (dev->get_stats) {
244                 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
245                 if (stats) {
246                         struct rtattr  *a;
247                         __u32          *s;
248                         int             i;
249                         int             n = sizeof(struct rtnl_link_stats)/4;
250
251                         a = __RTA_PUT(skb, IFLA_STATS, n*4);
252                         s = RTA_DATA(a);
253                         for (i=0; i<n; i++)
254                                 s[i] = stats[i];
255                 }
256         }
257         nlh->nlmsg_len = skb->tail - b;
258         return skb->len;
259
260 nlmsg_failure:
261 rtattr_failure:
262         skb_trim(skb, b - skb->data);
263         return -1;
264 }
265
266 static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
267 {
268         int idx;
269         int s_idx = cb->args[0];
270         struct net_device *dev;
271
272         read_lock(&dev_base_lock);
273         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
274                 if (idx < s_idx)
275                         continue;
276                 if (vx_info_flags(skb->sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
277                         !dev_in_nx_info(dev, skb->sk->sk_nx_info))
278                         continue;
279                 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
280                         break;
281         }
282         read_unlock(&dev_base_lock);
283         cb->args[0] = idx;
284
285         return skb->len;
286 }
287
288 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
289 {
290         struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
291         struct rtattr    **ida = arg;
292         struct net_device *dev;
293         int err, send_addr_notify = 0;
294
295         if (ifm->ifi_index >= 0)
296                 dev = dev_get_by_index(ifm->ifi_index);
297         else if (ida[IFLA_IFNAME - 1]) {
298                 char ifname[IFNAMSIZ];
299
300                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
301                                    IFNAMSIZ) >= IFNAMSIZ)
302                         return -EINVAL;
303                 dev = dev_get_by_name(ifname);
304         } else
305                 return -EINVAL;
306
307         if (!dev)
308                 return -ENODEV;
309
310         err = -EINVAL;
311
312         if (ifm->ifi_flags)
313                 dev_change_flags(dev, ifm->ifi_flags);
314
315         if (ida[IFLA_MAP - 1]) {
316                 struct rtnl_link_ifmap *u_map;
317                 struct ifmap k_map;
318
319                 if (!dev->set_config) {
320                         err = -EOPNOTSUPP;
321                         goto out;
322                 }
323
324                 if (!netif_device_present(dev)) {
325                         err = -ENODEV;
326                         goto out;
327                 }
328                 
329                 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
330                         goto out;
331
332                 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
333
334                 k_map.mem_start = (unsigned long) u_map->mem_start;
335                 k_map.mem_end = (unsigned long) u_map->mem_end;
336                 k_map.base_addr = (unsigned short) u_map->base_addr;
337                 k_map.irq = (unsigned char) u_map->irq;
338                 k_map.dma = (unsigned char) u_map->dma;
339                 k_map.port = (unsigned char) u_map->port;
340
341                 err = dev->set_config(dev, &k_map);
342
343                 if (err)
344                         goto out;
345         }
346
347         if (ida[IFLA_ADDRESS - 1]) {
348                 if (!dev->set_mac_address) {
349                         err = -EOPNOTSUPP;
350                         goto out;
351                 }
352                 if (!netif_device_present(dev)) {
353                         err = -ENODEV;
354                         goto out;
355                 }
356                 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
357                         goto out;
358
359                 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
360                 if (err)
361                         goto out;
362                 send_addr_notify = 1;
363         }
364
365         if (ida[IFLA_BROADCAST - 1]) {
366                 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
367                         goto out;
368                 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
369                        dev->addr_len);
370                 send_addr_notify = 1;
371         }
372
373         if (ida[IFLA_MTU - 1]) {
374                 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
375                         goto out;
376                 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
377
378                 if (err)
379                         goto out;
380
381         }
382
383         if (ida[IFLA_TXQLEN - 1]) {
384                 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
385                         goto out;
386
387                 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
388         }
389
390         if (ida[IFLA_WEIGHT - 1]) {
391                 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
392                         goto out;
393
394                 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
395         }
396
397         if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
398                 char ifname[IFNAMSIZ];
399
400                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
401                                    IFNAMSIZ) >= IFNAMSIZ)
402                         goto out;
403                 err = dev_change_name(dev, ifname);
404                 if (err)
405                         goto out;
406         }
407
408         err = 0;
409
410 out:
411         if (send_addr_notify)
412                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
413
414         dev_put(dev);
415         return err;
416 }
417
418 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
419 {
420         int idx;
421         int s_idx = cb->family;
422
423         if (s_idx == 0)
424                 s_idx = 1;
425         for (idx=1; idx<NPROTO; idx++) {
426                 int type = cb->nlh->nlmsg_type-RTM_BASE;
427                 if (idx < s_idx || idx == PF_PACKET)
428                         continue;
429                 if (rtnetlink_links[idx] == NULL ||
430                     rtnetlink_links[idx][type].dumpit == NULL)
431                         continue;
432                 if (idx > s_idx)
433                         memset(&cb->args[0], 0, sizeof(cb->args));
434                 if (rtnetlink_links[idx][type].dumpit(skb, cb))
435                         break;
436         }
437         cb->family = idx;
438
439         return skb->len;
440 }
441
442 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
443 {
444         struct sk_buff *skb;
445         int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
446                                sizeof(struct rtnl_link_ifmap) +
447                                sizeof(struct rtnl_link_stats) + 128);
448
449         if (vx_flags(VXF_HIDE_NETIF, 0) &&
450                 !dev_in_nx_info(dev, current->nx_info))
451                 return;
452         skb = alloc_skb(size, GFP_KERNEL);
453         if (!skb)
454                 return;
455
456         if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
457                 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
464 static int rtnetlink_done(struct netlink_callback *cb)
465 {
466         return 0;
467 }
468
469 /* Protected by RTNL sempahore.  */
470 static struct rtattr **rta_buf;
471 static int rtattr_max;
472
473 /* Process one rtnetlink message. */
474
475 static __inline__ int
476 rtnetlink_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
576 err_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
586 static 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:
618  *      - process as much as there was in the queue upon entry.
619  *      - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
620  *        that will occur, when a dump started.
621  */
622
623 static void rtnetlink_rcv(struct sock *sk, int len)
624 {
625         unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
626
627         do {
628                 struct sk_buff *skb;
629
630                 rtnl_lock();
631
632                 if (qlen > skb_queue_len(&sk->sk_receive_queue))
633                         qlen = skb_queue_len(&sk->sk_receive_queue);
634
635                 for (; qlen; qlen--) {
636                         skb = skb_dequeue(&sk->sk_receive_queue);
637                         if (rtnetlink_rcv_skb(skb)) {
638                                 if (skb->len)
639                                         skb_queue_head(&sk->sk_receive_queue,
640                                                        skb);
641                                 else {
642                                         kfree_skb(skb);
643                                         qlen--;
644                                 }
645                                 break;
646                         }
647                         kfree_skb(skb);
648                 }
649
650                 up(&rtnl_sem);
651
652                 netdev_run_todo();
653         } while (qlen);
654 }
655
656 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
657 {
658         [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 };
667
668 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
669 {
670         struct net_device *dev = ptr;
671         switch (event) {
672         case NETDEV_UNREGISTER:
673                 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
674                 break;
675         case NETDEV_REGISTER:
676                 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
677                 break;
678         case NETDEV_UP:
679         case NETDEV_DOWN:
680                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
681                 break;
682         case NETDEV_CHANGE:
683         case NETDEV_GOING_DOWN:
684                 break;
685         default:
686                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
687                 break;
688         }
689         return NOTIFY_DONE;
690 }
691
692 static struct notifier_block rtnetlink_dev_notifier = {
693         .notifier_call  = rtnetlink_event,
694 };
695
696 void __init rtnetlink_init(void)
697 {
698         int i;
699
700         rtattr_max = 0;
701         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
702                 if (rta_max[i] > rtattr_max)
703                         rtattr_max = rta_max[i];
704         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
705         if (!rta_buf)
706                 panic("rtnetlink_init: cannot allocate rta_buf\n");
707
708         rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
709         if (rtnl == NULL)
710                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
711         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
712         register_netdevice_notifier(&rtnetlink_dev_notifier);
713         rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
714         rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
715 }
716
717 EXPORT_SYMBOL(__rta_fill);
718 EXPORT_SYMBOL(rtattr_strlcpy);
719 EXPORT_SYMBOL(rtattr_parse);
720 EXPORT_SYMBOL(rtnetlink_links);
721 EXPORT_SYMBOL(rtnetlink_put_metrics);
722 EXPORT_SYMBOL(rtnl);
723 EXPORT_SYMBOL(rtnl_lock);
724 EXPORT_SYMBOL(rtnl_lock_interruptible);
725 EXPORT_SYMBOL(rtnl_sem);
726 EXPORT_SYMBOL(rtnl_unlock);