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