cfd2d1ef8e1ecd11143c9b6f28a13fcd7a1e0446
[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 #include <linux/mutex.h>
39
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/string.h>
43
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <net/ip.h>
47 #include <net/protocol.h>
48 #include <net/arp.h>
49 #include <net/route.h>
50 #include <net/udp.h>
51 #include <net/sock.h>
52 #include <net/pkt_sched.h>
53 #include <net/netlink.h>
54 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
55 #include <linux/wireless.h>
56 #include <net/iw_handler.h>
57 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
58
59 static DEFINE_MUTEX(rtnl_mutex);
60
61 void rtnl_lock(void)
62 {
63         mutex_lock(&rtnl_mutex);
64 }
65
66 void __rtnl_unlock(void)
67 {
68         mutex_unlock(&rtnl_mutex);
69 }
70
71 void rtnl_unlock(void)
72 {
73         mutex_unlock(&rtnl_mutex);
74         if (rtnl && rtnl->sk_receive_queue.qlen)
75                 rtnl->sk_data_ready(rtnl, 0);
76         netdev_run_todo();
77 }
78
79 int rtnl_trylock(void)
80 {
81         return mutex_trylock(&rtnl_mutex);
82 }
83
84 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
85 {
86         memset(tb, 0, sizeof(struct rtattr*)*maxattr);
87
88         while (RTA_OK(rta, len)) {
89                 unsigned flavor = rta->rta_type;
90                 if (flavor && flavor <= maxattr)
91                         tb[flavor-1] = rta;
92                 rta = RTA_NEXT(rta, len);
93         }
94         return 0;
95 }
96
97 struct sock *rtnl;
98
99 struct rtnetlink_link * rtnetlink_links[NPROTO];
100
101 static const int rtm_min[RTM_NR_FAMILIES] =
102 {
103         [RTM_FAM(RTM_NEWLINK)]      = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
104         [RTM_FAM(RTM_NEWADDR)]      = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
105         [RTM_FAM(RTM_NEWROUTE)]     = NLMSG_LENGTH(sizeof(struct rtmsg)),
106         [RTM_FAM(RTM_NEWNEIGH)]     = NLMSG_LENGTH(sizeof(struct ndmsg)),
107         [RTM_FAM(RTM_NEWRULE)]      = NLMSG_LENGTH(sizeof(struct rtmsg)),
108         [RTM_FAM(RTM_NEWQDISC)]     = NLMSG_LENGTH(sizeof(struct tcmsg)),
109         [RTM_FAM(RTM_NEWTCLASS)]    = NLMSG_LENGTH(sizeof(struct tcmsg)),
110         [RTM_FAM(RTM_NEWTFILTER)]   = NLMSG_LENGTH(sizeof(struct tcmsg)),
111         [RTM_FAM(RTM_NEWACTION)]    = NLMSG_LENGTH(sizeof(struct tcamsg)),
112         [RTM_FAM(RTM_NEWPREFIX)]    = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
113         [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
114         [RTM_FAM(RTM_GETANYCAST)]   = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
115         [RTM_FAM(RTM_NEWNEIGHTBL)]  = NLMSG_LENGTH(sizeof(struct ndtmsg)),
116 };
117
118 static const int rta_max[RTM_NR_FAMILIES] =
119 {
120         [RTM_FAM(RTM_NEWLINK)]      = IFLA_MAX,
121         [RTM_FAM(RTM_NEWADDR)]      = IFA_MAX,
122         [RTM_FAM(RTM_NEWROUTE)]     = RTA_MAX,
123         [RTM_FAM(RTM_NEWNEIGH)]     = NDA_MAX,
124         [RTM_FAM(RTM_NEWRULE)]      = RTA_MAX,
125         [RTM_FAM(RTM_NEWQDISC)]     = TCA_MAX,
126         [RTM_FAM(RTM_NEWTCLASS)]    = TCA_MAX,
127         [RTM_FAM(RTM_NEWTFILTER)]   = TCA_MAX,
128         [RTM_FAM(RTM_NEWACTION)]    = TCAA_MAX,
129         [RTM_FAM(RTM_NEWNEIGHTBL)]  = NDTA_MAX,
130 };
131
132 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
133 {
134         struct rtattr *rta;
135         int size = RTA_LENGTH(attrlen);
136
137         rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
138         rta->rta_type = attrtype;
139         rta->rta_len = size;
140         memcpy(RTA_DATA(rta), data, attrlen);
141         memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
142 }
143
144 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
145 {
146         size_t ret = RTA_PAYLOAD(rta);
147         char *src = RTA_DATA(rta);
148
149         if (ret > 0 && src[ret - 1] == '\0')
150                 ret--;
151         if (size > 0) {
152                 size_t len = (ret >= size) ? size - 1 : ret;
153                 memset(dest, 0, size);
154                 memcpy(dest, src, len);
155         }
156         return ret;
157 }
158
159 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
160 {
161         int err = 0;
162
163         NETLINK_CB(skb).dst_group = group;
164         if (echo)
165                 atomic_inc(&skb->users);
166         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
167         if (echo)
168                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
169         return err;
170 }
171
172 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
173 {
174         struct rtattr *mx = (struct rtattr*)skb->tail;
175         int i;
176
177         RTA_PUT(skb, RTA_METRICS, 0, NULL);
178         for (i=0; i<RTAX_MAX; i++) {
179                 if (metrics[i])
180                         RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
181         }
182         mx->rta_len = skb->tail - (u8*)mx;
183         if (mx->rta_len == RTA_LENGTH(0))
184                 skb_trim(skb, (u8*)mx - skb->data);
185         return 0;
186
187 rtattr_failure:
188         skb_trim(skb, (u8*)mx - skb->data);
189         return -1;
190 }
191
192
193 static void set_operstate(struct net_device *dev, unsigned char transition)
194 {
195         unsigned char operstate = dev->operstate;
196
197         switch(transition) {
198         case IF_OPER_UP:
199                 if ((operstate == IF_OPER_DORMANT ||
200                      operstate == IF_OPER_UNKNOWN) &&
201                     !netif_dormant(dev))
202                         operstate = IF_OPER_UP;
203                 break;
204
205         case IF_OPER_DORMANT:
206                 if (operstate == IF_OPER_UP ||
207                     operstate == IF_OPER_UNKNOWN)
208                         operstate = IF_OPER_DORMANT;
209                 break;
210         };
211
212         if (dev->operstate != operstate) {
213                 write_lock_bh(&dev_base_lock);
214                 dev->operstate = operstate;
215                 write_unlock_bh(&dev_base_lock);
216                 netdev_state_change(dev);
217         }
218 }
219
220 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
221                                  int type, u32 pid, u32 seq, u32 change, 
222                                  unsigned int flags)
223 {
224         struct ifinfomsg *r;
225         struct nlmsghdr  *nlh;
226         unsigned char    *b = skb->tail;
227
228         nlh = NLMSG_NEW(skb, pid, seq, type, sizeof(*r), flags);
229         r = NLMSG_DATA(nlh);
230         r->ifi_family = AF_UNSPEC;
231         r->__ifi_pad = 0;
232         r->ifi_type = dev->type;
233         r->ifi_index = dev->ifindex;
234         r->ifi_flags = dev_get_flags(dev);
235         r->ifi_change = change;
236
237         RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
238
239         if (1) {
240                 u32 txqlen = dev->tx_queue_len;
241                 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
242         }
243
244         if (1) {
245                 u32 weight = dev->weight;
246                 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
247         }
248
249         if (1) {
250                 u8 operstate = netif_running(dev)?dev->operstate:IF_OPER_DOWN;
251                 u8 link_mode = dev->link_mode;
252                 RTA_PUT(skb, IFLA_OPERSTATE, sizeof(operstate), &operstate);
253                 RTA_PUT(skb, IFLA_LINKMODE, sizeof(link_mode), &link_mode);
254         }
255
256         if (1) {
257                 struct rtnl_link_ifmap map = {
258                         .mem_start   = dev->mem_start,
259                         .mem_end     = dev->mem_end,
260                         .base_addr   = dev->base_addr,
261                         .irq         = dev->irq,
262                         .dma         = dev->dma,
263                         .port        = dev->if_port,
264                 };
265                 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
266         }
267
268         if (dev->addr_len) {
269                 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
270                 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
271         }
272
273         if (1) {
274                 u32 mtu = dev->mtu;
275                 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
276         }
277
278         if (dev->ifindex != dev->iflink) {
279                 u32 iflink = dev->iflink;
280                 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
281         }
282
283         if (dev->qdisc_sleeping)
284                 RTA_PUT(skb, IFLA_QDISC,
285                         strlen(dev->qdisc_sleeping->ops->id) + 1,
286                         dev->qdisc_sleeping->ops->id);
287         
288         if (dev->master) {
289                 u32 master = dev->master->ifindex;
290                 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
291         }
292
293         if (dev->get_stats) {
294                 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
295                 if (stats) {
296                         struct rtattr  *a;
297                         __u32          *s;
298                         int             i;
299                         int             n = sizeof(struct rtnl_link_stats)/4;
300
301                         a = __RTA_PUT(skb, IFLA_STATS, n*4);
302                         s = RTA_DATA(a);
303                         for (i=0; i<n; i++)
304                                 s[i] = stats[i];
305                 }
306         }
307         nlh->nlmsg_len = skb->tail - b;
308         return skb->len;
309
310 nlmsg_failure:
311 rtattr_failure:
312         skb_trim(skb, b - skb->data);
313         return -1;
314 }
315
316 static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
317 {
318         int idx;
319         int s_idx = cb->args[0];
320         struct net_device *dev;
321
322         read_lock(&dev_base_lock);
323         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
324                 if (idx < s_idx)
325                         continue;
326                 if (vx_info_flags(skb->sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
327                         !dev_in_nx_info(dev, skb->sk->sk_nx_info))
328                         continue;
329                 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK,
330                                           NETLINK_CB(cb->skb).pid,
331                                           cb->nlh->nlmsg_seq, 0,
332                                           NLM_F_MULTI) <= 0)
333                         break;
334         }
335         read_unlock(&dev_base_lock);
336         cb->args[0] = idx;
337
338         return skb->len;
339 }
340
341 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
342 {
343         struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
344         struct rtattr    **ida = arg;
345         struct net_device *dev;
346         int err, send_addr_notify = 0;
347
348         if (ifm->ifi_index >= 0)
349                 dev = dev_get_by_index(ifm->ifi_index);
350         else if (ida[IFLA_IFNAME - 1]) {
351                 char ifname[IFNAMSIZ];
352
353                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
354                                    IFNAMSIZ) >= IFNAMSIZ)
355                         return -EINVAL;
356                 dev = dev_get_by_name(ifname);
357         } else
358                 return -EINVAL;
359
360         if (!dev)
361                 return -ENODEV;
362
363         err = -EINVAL;
364
365         if (ifm->ifi_flags)
366                 dev_change_flags(dev, ifm->ifi_flags);
367
368         if (ida[IFLA_MAP - 1]) {
369                 struct rtnl_link_ifmap *u_map;
370                 struct ifmap k_map;
371
372                 if (!dev->set_config) {
373                         err = -EOPNOTSUPP;
374                         goto out;
375                 }
376
377                 if (!netif_device_present(dev)) {
378                         err = -ENODEV;
379                         goto out;
380                 }
381                 
382                 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
383                         goto out;
384
385                 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
386
387                 k_map.mem_start = (unsigned long) u_map->mem_start;
388                 k_map.mem_end = (unsigned long) u_map->mem_end;
389                 k_map.base_addr = (unsigned short) u_map->base_addr;
390                 k_map.irq = (unsigned char) u_map->irq;
391                 k_map.dma = (unsigned char) u_map->dma;
392                 k_map.port = (unsigned char) u_map->port;
393
394                 err = dev->set_config(dev, &k_map);
395
396                 if (err)
397                         goto out;
398         }
399
400         if (ida[IFLA_ADDRESS - 1]) {
401                 if (!dev->set_mac_address) {
402                         err = -EOPNOTSUPP;
403                         goto out;
404                 }
405                 if (!netif_device_present(dev)) {
406                         err = -ENODEV;
407                         goto out;
408                 }
409                 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
410                         goto out;
411
412                 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
413                 if (err)
414                         goto out;
415                 send_addr_notify = 1;
416         }
417
418         if (ida[IFLA_BROADCAST - 1]) {
419                 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
420                         goto out;
421                 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
422                        dev->addr_len);
423                 send_addr_notify = 1;
424         }
425
426         if (ida[IFLA_MTU - 1]) {
427                 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
428                         goto out;
429                 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
430
431                 if (err)
432                         goto out;
433
434         }
435
436         if (ida[IFLA_TXQLEN - 1]) {
437                 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
438                         goto out;
439
440                 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
441         }
442
443         if (ida[IFLA_WEIGHT - 1]) {
444                 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
445                         goto out;
446
447                 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
448         }
449
450         if (ida[IFLA_OPERSTATE - 1]) {
451                 if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
452                         goto out;
453
454                 set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
455         }
456
457         if (ida[IFLA_LINKMODE - 1]) {
458                 if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
459                         goto out;
460
461                 write_lock_bh(&dev_base_lock);
462                 dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
463                 write_unlock_bh(&dev_base_lock);
464         }
465
466         if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
467                 char ifname[IFNAMSIZ];
468
469                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
470                                    IFNAMSIZ) >= IFNAMSIZ)
471                         goto out;
472                 err = dev_change_name(dev, ifname);
473                 if (err)
474                         goto out;
475         }
476
477 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
478         if (ida[IFLA_WIRELESS - 1]) {
479
480                 /* Call Wireless Extensions.
481                  * Various stuff checked in there... */
482                 err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len);
483                 if (err)
484                         goto out;
485         }
486 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
487
488         err = 0;
489
490 out:
491         if (send_addr_notify)
492                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
493
494         dev_put(dev);
495         return err;
496 }
497
498 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
499 static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
500 {
501         struct ifinfomsg  *ifm = NLMSG_DATA(in_nlh);
502         struct rtattr    **ida = arg;
503         struct net_device *dev;
504         struct ifinfomsg *r;
505         struct nlmsghdr  *nlh;
506         int err = -ENOBUFS;
507         struct sk_buff *skb;
508         unsigned char    *b;
509         char *iw_buf = NULL;
510         int iw_buf_len = 0;
511
512         if (ifm->ifi_index >= 0)
513                 dev = dev_get_by_index(ifm->ifi_index);
514         else
515                 return -EINVAL;
516         if (!dev)
517                 return -ENODEV;
518
519 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
520         if (ida[IFLA_WIRELESS - 1]) {
521
522                 /* Call Wireless Extensions. We need to know the size before
523                  * we can alloc. Various stuff checked in there... */
524                 err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
525                 if (err)
526                         goto out;
527         }
528 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
529
530         /* Create a skb big enough to include all the data.
531          * Some requests are way bigger than 4k... Jean II */
532         skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
533                         GFP_KERNEL);
534         if (!skb)
535                 goto out;
536         b = skb->tail;
537
538         /* Put in the message the usual good stuff */
539         nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
540                         RTM_NEWLINK, sizeof(*r));
541         r = NLMSG_DATA(nlh);
542         r->ifi_family = AF_UNSPEC;
543         r->__ifi_pad = 0;
544         r->ifi_type = dev->type;
545         r->ifi_index = dev->ifindex;
546         r->ifi_flags = dev->flags;
547         r->ifi_change = 0;
548
549         /* Put the wireless payload if it exist */
550         if(iw_buf != NULL)
551                 RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
552                         iw_buf + IW_EV_POINT_OFF);
553
554         nlh->nlmsg_len = skb->tail - b;
555
556         /* Needed ? */
557         NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
558
559         err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
560         if (err > 0)
561                 err = 0;
562 out:
563         if(iw_buf != NULL)
564                 kfree(iw_buf);
565         dev_put(dev);
566         return err;
567
568 rtattr_failure:
569 nlmsg_failure:
570         kfree_skb(skb);
571         goto out;
572 }
573 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
574
575 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
576 {
577         int idx;
578         int s_idx = cb->family;
579
580         if (s_idx == 0)
581                 s_idx = 1;
582         for (idx=1; idx<NPROTO; idx++) {
583                 int type = cb->nlh->nlmsg_type-RTM_BASE;
584                 if (idx < s_idx || idx == PF_PACKET)
585                         continue;
586                 if (rtnetlink_links[idx] == NULL ||
587                     rtnetlink_links[idx][type].dumpit == NULL)
588                         continue;
589                 if (idx > s_idx)
590                         memset(&cb->args[0], 0, sizeof(cb->args));
591                 if (rtnetlink_links[idx][type].dumpit(skb, cb))
592                         break;
593         }
594         cb->family = idx;
595
596         return skb->len;
597 }
598
599 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
600 {
601         struct sk_buff *skb;
602         int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
603                                sizeof(struct rtnl_link_ifmap) +
604                                sizeof(struct rtnl_link_stats) + 128);
605
606         if (vx_flags(VXF_HIDE_NETIF, 0) &&
607                 !dev_in_nx_info(dev, current->nx_info))
608                 return;
609         skb = alloc_skb(size, GFP_KERNEL);
610         if (!skb)
611                 return;
612
613         if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
614                 kfree_skb(skb);
615                 return;
616         }
617         NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
618         netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
619 }
620
621 /* Protected by RTNL sempahore.  */
622 static struct rtattr **rta_buf;
623 static int rtattr_max;
624
625 /* Process one rtnetlink message. */
626
627 static __inline__ int
628 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
629 {
630         struct rtnetlink_link *link;
631         struct rtnetlink_link *link_tab;
632         int sz_idx, kind;
633         int min_len;
634         int family;
635         int type;
636         int err;
637
638         /* Only requests are handled by kernel now */
639         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
640                 return 0;
641
642         type = nlh->nlmsg_type;
643
644         /* A control message: ignore them */
645         if (type < RTM_BASE)
646                 return 0;
647
648         /* Unknown message: reply with EINVAL */
649         if (type > RTM_MAX)
650                 goto err_inval;
651
652         type -= RTM_BASE;
653
654         /* All the messages must have at least 1 byte length */
655         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
656                 return 0;
657
658         family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
659         if (family >= NPROTO) {
660                 *errp = -EAFNOSUPPORT;
661                 return -1;
662         }
663
664         link_tab = rtnetlink_links[family];
665         if (link_tab == NULL)
666                 link_tab = rtnetlink_links[PF_UNSPEC];
667         link = &link_tab[type];
668
669         sz_idx = type>>2;
670         kind = type&3;
671
672         if (kind != 2 && security_netlink_recv(skb)) {
673                 *errp = -EPERM;
674                 return -1;
675         }
676
677         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
678                 if (link->dumpit == NULL)
679                         link = &(rtnetlink_links[PF_UNSPEC][type]);
680
681                 if (link->dumpit == NULL)
682                         goto err_inval;
683
684                 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
685                                                 link->dumpit, NULL)) != 0) {
686                         return -1;
687                 }
688
689                 netlink_queue_skip(nlh, skb);
690                 return -1;
691         }
692
693         memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
694
695         min_len = rtm_min[sz_idx];
696         if (nlh->nlmsg_len < min_len)
697                 goto err_inval;
698
699         if (nlh->nlmsg_len > min_len) {
700                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
701                 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
702
703                 while (RTA_OK(attr, attrlen)) {
704                         unsigned flavor = attr->rta_type;
705                         if (flavor) {
706                                 if (flavor > rta_max[sz_idx])
707                                         goto err_inval;
708                                 rta_buf[flavor-1] = attr;
709                         }
710                         attr = RTA_NEXT(attr, attrlen);
711                 }
712         }
713
714         if (link->doit == NULL)
715                 link = &(rtnetlink_links[PF_UNSPEC][type]);
716         if (link->doit == NULL)
717                 goto err_inval;
718         err = link->doit(skb, nlh, (void *)&rta_buf[0]);
719
720         *errp = err;
721         return err;
722
723 err_inval:
724         *errp = -EINVAL;
725         return -1;
726 }
727
728 static void rtnetlink_rcv(struct sock *sk, int len)
729 {
730         unsigned int qlen = 0;
731
732         do {
733                 mutex_lock(&rtnl_mutex);
734                 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
735                 mutex_unlock(&rtnl_mutex);
736
737                 netdev_run_todo();
738         } while (qlen);
739 }
740
741 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
742 {
743         [RTM_GETLINK     - RTM_BASE] = {
744 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
745                                          .doit   = do_getlink,
746 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
747                                          .dumpit = rtnetlink_dump_ifinfo },
748         [RTM_SETLINK     - RTM_BASE] = { .doit   = do_setlink            },
749         [RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
750         [RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
751         [RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add             },
752         [RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete          },
753         [RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info       },
754         [RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
755         [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info    },
756         [RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set          },
757 };
758
759 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
760 {
761         struct net_device *dev = ptr;
762         switch (event) {
763         case NETDEV_UNREGISTER:
764                 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
765                 break;
766         case NETDEV_REGISTER:
767                 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
768                 break;
769         case NETDEV_UP:
770         case NETDEV_DOWN:
771                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
772                 break;
773         case NETDEV_CHANGE:
774         case NETDEV_GOING_DOWN:
775                 break;
776         default:
777                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
778                 break;
779         }
780         return NOTIFY_DONE;
781 }
782
783 static struct notifier_block rtnetlink_dev_notifier = {
784         .notifier_call  = rtnetlink_event,
785 };
786
787 void __init rtnetlink_init(void)
788 {
789         int i;
790
791         rtattr_max = 0;
792         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
793                 if (rta_max[i] > rtattr_max)
794                         rtattr_max = rta_max[i];
795         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
796         if (!rta_buf)
797                 panic("rtnetlink_init: cannot allocate rta_buf\n");
798
799         rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
800                                      THIS_MODULE);
801         if (rtnl == NULL)
802                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
803         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
804         register_netdevice_notifier(&rtnetlink_dev_notifier);
805         rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
806         rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
807 }
808
809 EXPORT_SYMBOL(__rta_fill);
810 EXPORT_SYMBOL(rtattr_strlcpy);
811 EXPORT_SYMBOL(rtattr_parse);
812 EXPORT_SYMBOL(rtnetlink_links);
813 EXPORT_SYMBOL(rtnetlink_put_metrics);
814 EXPORT_SYMBOL(rtnl);
815 EXPORT_SYMBOL(rtnl_lock);
816 EXPORT_SYMBOL(rtnl_trylock);
817 EXPORT_SYMBOL(rtnl_unlock);