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