]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/core/rtnetlink.c
[IPv4]: Move interface address bits to linux/if_addr.h
[mv-sheeva.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/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 int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
340 {
341         struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
342         struct rtattr    **ida = arg;
343         struct net_device *dev;
344         int err, send_addr_notify = 0;
345
346         if (ifm->ifi_index >= 0)
347                 dev = dev_get_by_index(ifm->ifi_index);
348         else if (ida[IFLA_IFNAME - 1]) {
349                 char ifname[IFNAMSIZ];
350
351                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
352                                    IFNAMSIZ) >= IFNAMSIZ)
353                         return -EINVAL;
354                 dev = dev_get_by_name(ifname);
355         } else
356                 return -EINVAL;
357
358         if (!dev)
359                 return -ENODEV;
360
361         err = -EINVAL;
362
363         if (ifm->ifi_flags)
364                 dev_change_flags(dev, ifm->ifi_flags);
365
366         if (ida[IFLA_MAP - 1]) {
367                 struct rtnl_link_ifmap *u_map;
368                 struct ifmap k_map;
369
370                 if (!dev->set_config) {
371                         err = -EOPNOTSUPP;
372                         goto out;
373                 }
374
375                 if (!netif_device_present(dev)) {
376                         err = -ENODEV;
377                         goto out;
378                 }
379                 
380                 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
381                         goto out;
382
383                 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
384
385                 k_map.mem_start = (unsigned long) u_map->mem_start;
386                 k_map.mem_end = (unsigned long) u_map->mem_end;
387                 k_map.base_addr = (unsigned short) u_map->base_addr;
388                 k_map.irq = (unsigned char) u_map->irq;
389                 k_map.dma = (unsigned char) u_map->dma;
390                 k_map.port = (unsigned char) u_map->port;
391
392                 err = dev->set_config(dev, &k_map);
393
394                 if (err)
395                         goto out;
396         }
397
398         if (ida[IFLA_ADDRESS - 1]) {
399                 struct sockaddr *sa;
400                 int len;
401
402                 if (!dev->set_mac_address) {
403                         err = -EOPNOTSUPP;
404                         goto out;
405                 }
406                 if (!netif_device_present(dev)) {
407                         err = -ENODEV;
408                         goto out;
409                 }
410                 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
411                         goto out;
412
413                 len = sizeof(sa_family_t) + dev->addr_len;
414                 sa = kmalloc(len, GFP_KERNEL);
415                 if (!sa) {
416                         err = -ENOMEM;
417                         goto out;
418                 }
419                 sa->sa_family = dev->type;
420                 memcpy(sa->sa_data, RTA_DATA(ida[IFLA_ADDRESS - 1]),
421                        dev->addr_len);
422                 err = dev->set_mac_address(dev, sa);
423                 kfree(sa);
424                 if (err)
425                         goto out;
426                 send_addr_notify = 1;
427         }
428
429         if (ida[IFLA_BROADCAST - 1]) {
430                 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
431                         goto out;
432                 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
433                        dev->addr_len);
434                 send_addr_notify = 1;
435         }
436
437         if (ida[IFLA_MTU - 1]) {
438                 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
439                         goto out;
440                 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
441
442                 if (err)
443                         goto out;
444
445         }
446
447         if (ida[IFLA_TXQLEN - 1]) {
448                 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
449                         goto out;
450
451                 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
452         }
453
454         if (ida[IFLA_WEIGHT - 1]) {
455                 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
456                         goto out;
457
458                 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
459         }
460
461         if (ida[IFLA_OPERSTATE - 1]) {
462                 if (ida[IFLA_OPERSTATE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
463                         goto out;
464
465                 set_operstate(dev, *((u8 *) RTA_DATA(ida[IFLA_OPERSTATE - 1])));
466         }
467
468         if (ida[IFLA_LINKMODE - 1]) {
469                 if (ida[IFLA_LINKMODE - 1]->rta_len != RTA_LENGTH(sizeof(u8)))
470                         goto out;
471
472                 write_lock_bh(&dev_base_lock);
473                 dev->link_mode = *((u8 *) RTA_DATA(ida[IFLA_LINKMODE - 1]));
474                 write_unlock_bh(&dev_base_lock);
475         }
476
477         if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
478                 char ifname[IFNAMSIZ];
479
480                 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
481                                    IFNAMSIZ) >= IFNAMSIZ)
482                         goto out;
483                 err = dev_change_name(dev, ifname);
484                 if (err)
485                         goto out;
486         }
487
488 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
489         if (ida[IFLA_WIRELESS - 1]) {
490
491                 /* Call Wireless Extensions.
492                  * Various stuff checked in there... */
493                 err = wireless_rtnetlink_set(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len);
494                 if (err)
495                         goto out;
496         }
497 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
498
499         err = 0;
500
501 out:
502         if (send_addr_notify)
503                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
504
505         dev_put(dev);
506         return err;
507 }
508
509 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
510 static int do_getlink(struct sk_buff *in_skb, struct nlmsghdr* in_nlh, void *arg)
511 {
512         struct ifinfomsg  *ifm = NLMSG_DATA(in_nlh);
513         struct rtattr    **ida = arg;
514         struct net_device *dev;
515         struct ifinfomsg *r;
516         struct nlmsghdr  *nlh;
517         int err = -ENOBUFS;
518         struct sk_buff *skb;
519         unsigned char    *b;
520         char *iw_buf = NULL;
521         int iw_buf_len = 0;
522
523         if (ifm->ifi_index >= 0)
524                 dev = dev_get_by_index(ifm->ifi_index);
525         else
526                 return -EINVAL;
527         if (!dev)
528                 return -ENODEV;
529
530 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
531         if (ida[IFLA_WIRELESS - 1]) {
532
533                 /* Call Wireless Extensions. We need to know the size before
534                  * we can alloc. Various stuff checked in there... */
535                 err = wireless_rtnetlink_get(dev, RTA_DATA(ida[IFLA_WIRELESS - 1]), ida[IFLA_WIRELESS - 1]->rta_len, &iw_buf, &iw_buf_len);
536                 if (err)
537                         goto out;
538         }
539 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
540
541         /* Create a skb big enough to include all the data.
542          * Some requests are way bigger than 4k... Jean II */
543         skb = alloc_skb((NLMSG_LENGTH(sizeof(*r))) + (RTA_SPACE(iw_buf_len)),
544                         GFP_KERNEL);
545         if (!skb)
546                 goto out;
547         b = skb->tail;
548
549         /* Put in the message the usual good stuff */
550         nlh = NLMSG_PUT(skb, NETLINK_CB(in_skb).pid, in_nlh->nlmsg_seq,
551                         RTM_NEWLINK, sizeof(*r));
552         r = NLMSG_DATA(nlh);
553         r->ifi_family = AF_UNSPEC;
554         r->__ifi_pad = 0;
555         r->ifi_type = dev->type;
556         r->ifi_index = dev->ifindex;
557         r->ifi_flags = dev->flags;
558         r->ifi_change = 0;
559
560         /* Put the wireless payload if it exist */
561         if(iw_buf != NULL)
562                 RTA_PUT(skb, IFLA_WIRELESS, iw_buf_len,
563                         iw_buf + IW_EV_POINT_OFF);
564
565         nlh->nlmsg_len = skb->tail - b;
566
567         /* Needed ? */
568         NETLINK_CB(skb).dst_pid = NETLINK_CB(in_skb).pid;
569
570         err = netlink_unicast(rtnl, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
571         if (err > 0)
572                 err = 0;
573 out:
574         if(iw_buf != NULL)
575                 kfree(iw_buf);
576         dev_put(dev);
577         return err;
578
579 rtattr_failure:
580 nlmsg_failure:
581         kfree_skb(skb);
582         goto out;
583 }
584 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
585
586 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
587 {
588         int idx;
589         int s_idx = cb->family;
590
591         if (s_idx == 0)
592                 s_idx = 1;
593         for (idx=1; idx<NPROTO; idx++) {
594                 int type = cb->nlh->nlmsg_type-RTM_BASE;
595                 if (idx < s_idx || idx == PF_PACKET)
596                         continue;
597                 if (rtnetlink_links[idx] == NULL ||
598                     rtnetlink_links[idx][type].dumpit == NULL)
599                         continue;
600                 if (idx > s_idx)
601                         memset(&cb->args[0], 0, sizeof(cb->args));
602                 if (rtnetlink_links[idx][type].dumpit(skb, cb))
603                         break;
604         }
605         cb->family = idx;
606
607         return skb->len;
608 }
609
610 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
611 {
612         struct sk_buff *skb;
613         int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
614                                sizeof(struct rtnl_link_ifmap) +
615                                sizeof(struct rtnl_link_stats) + 128);
616
617         skb = alloc_skb(size, GFP_KERNEL);
618         if (!skb)
619                 return;
620
621         if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change, 0) < 0) {
622                 kfree_skb(skb);
623                 return;
624         }
625         NETLINK_CB(skb).dst_group = RTNLGRP_LINK;
626         netlink_broadcast(rtnl, skb, 0, RTNLGRP_LINK, GFP_KERNEL);
627 }
628
629 /* Protected by RTNL sempahore.  */
630 static struct rtattr **rta_buf;
631 static int rtattr_max;
632
633 /* Process one rtnetlink message. */
634
635 static __inline__ int
636 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
637 {
638         struct rtnetlink_link *link;
639         struct rtnetlink_link *link_tab;
640         int sz_idx, kind;
641         int min_len;
642         int family;
643         int type;
644         int err;
645
646         /* Only requests are handled by kernel now */
647         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
648                 return 0;
649
650         type = nlh->nlmsg_type;
651
652         /* A control message: ignore them */
653         if (type < RTM_BASE)
654                 return 0;
655
656         /* Unknown message: reply with EINVAL */
657         if (type > RTM_MAX)
658                 goto err_inval;
659
660         type -= RTM_BASE;
661
662         /* All the messages must have at least 1 byte length */
663         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
664                 return 0;
665
666         family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
667         if (family >= NPROTO) {
668                 *errp = -EAFNOSUPPORT;
669                 return -1;
670         }
671
672         link_tab = rtnetlink_links[family];
673         if (link_tab == NULL)
674                 link_tab = rtnetlink_links[PF_UNSPEC];
675         link = &link_tab[type];
676
677         sz_idx = type>>2;
678         kind = type&3;
679
680         if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN)) {
681                 *errp = -EPERM;
682                 return -1;
683         }
684
685         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
686                 if (link->dumpit == NULL)
687                         link = &(rtnetlink_links[PF_UNSPEC][type]);
688
689                 if (link->dumpit == NULL)
690                         goto err_inval;
691
692                 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
693                                                 link->dumpit, NULL)) != 0) {
694                         return -1;
695                 }
696
697                 netlink_queue_skip(nlh, skb);
698                 return -1;
699         }
700
701         memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
702
703         min_len = rtm_min[sz_idx];
704         if (nlh->nlmsg_len < min_len)
705                 goto err_inval;
706
707         if (nlh->nlmsg_len > min_len) {
708                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
709                 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
710
711                 while (RTA_OK(attr, attrlen)) {
712                         unsigned flavor = attr->rta_type;
713                         if (flavor) {
714                                 if (flavor > rta_max[sz_idx])
715                                         goto err_inval;
716                                 rta_buf[flavor-1] = attr;
717                         }
718                         attr = RTA_NEXT(attr, attrlen);
719                 }
720         }
721
722         if (link->doit == NULL)
723                 link = &(rtnetlink_links[PF_UNSPEC][type]);
724         if (link->doit == NULL)
725                 goto err_inval;
726         err = link->doit(skb, nlh, (void *)&rta_buf[0]);
727
728         *errp = err;
729         return err;
730
731 err_inval:
732         *errp = -EINVAL;
733         return -1;
734 }
735
736 static void rtnetlink_rcv(struct sock *sk, int len)
737 {
738         unsigned int qlen = 0;
739
740         do {
741                 mutex_lock(&rtnl_mutex);
742                 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
743                 mutex_unlock(&rtnl_mutex);
744
745                 netdev_run_todo();
746         } while (qlen);
747 }
748
749 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
750 {
751         [RTM_GETLINK     - RTM_BASE] = {
752 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
753                                          .doit   = do_getlink,
754 #endif  /* CONFIG_NET_WIRELESS_RTNETLINK */
755                                          .dumpit = rtnetlink_dump_ifinfo },
756         [RTM_SETLINK     - RTM_BASE] = { .doit   = do_setlink            },
757         [RTM_GETADDR     - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
758         [RTM_GETROUTE    - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
759         [RTM_NEWNEIGH    - RTM_BASE] = { .doit   = neigh_add             },
760         [RTM_DELNEIGH    - RTM_BASE] = { .doit   = neigh_delete          },
761         [RTM_GETNEIGH    - RTM_BASE] = { .dumpit = neigh_dump_info       },
762 #ifdef CONFIG_FIB_RULES
763         [RTM_NEWRULE     - RTM_BASE] = { .doit   = fib_nl_newrule        },
764         [RTM_DELRULE     - RTM_BASE] = { .doit   = fib_nl_delrule        },
765 #endif
766         [RTM_GETRULE     - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
767         [RTM_GETNEIGHTBL - RTM_BASE] = { .dumpit = neightbl_dump_info    },
768         [RTM_SETNEIGHTBL - RTM_BASE] = { .doit   = neightbl_set          },
769 };
770
771 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
772 {
773         struct net_device *dev = ptr;
774         switch (event) {
775         case NETDEV_UNREGISTER:
776                 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
777                 break;
778         case NETDEV_REGISTER:
779                 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
780                 break;
781         case NETDEV_UP:
782         case NETDEV_DOWN:
783                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
784                 break;
785         case NETDEV_CHANGE:
786         case NETDEV_GOING_DOWN:
787                 break;
788         default:
789                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
790                 break;
791         }
792         return NOTIFY_DONE;
793 }
794
795 static struct notifier_block rtnetlink_dev_notifier = {
796         .notifier_call  = rtnetlink_event,
797 };
798
799 void __init rtnetlink_init(void)
800 {
801         int i;
802
803         rtattr_max = 0;
804         for (i = 0; i < ARRAY_SIZE(rta_max); i++)
805                 if (rta_max[i] > rtattr_max)
806                         rtattr_max = rta_max[i];
807         rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
808         if (!rta_buf)
809                 panic("rtnetlink_init: cannot allocate rta_buf\n");
810
811         rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
812                                      THIS_MODULE);
813         if (rtnl == NULL)
814                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
815         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
816         register_netdevice_notifier(&rtnetlink_dev_notifier);
817         rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
818         rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
819 }
820
821 EXPORT_SYMBOL(__rta_fill);
822 EXPORT_SYMBOL(rtattr_strlcpy);
823 EXPORT_SYMBOL(rtattr_parse);
824 EXPORT_SYMBOL(rtnetlink_links);
825 EXPORT_SYMBOL(rtnetlink_put_metrics);
826 EXPORT_SYMBOL(rtnl);
827 EXPORT_SYMBOL(rtnl_lock);
828 EXPORT_SYMBOL(rtnl_trylock);
829 EXPORT_SYMBOL(rtnl_unlock);