This commit was manufactured by cvs2svn to create tag
[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/major.h>
26 #include <linux/sched.h>
27 #include <linux/timer.h>
28 #include <linux/string.h>
29 #include <linux/sockios.h>
30 #include <linux/net.h>
31 #include <linux/fcntl.h>
32 #include <linux/mm.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/capability.h>
36 #include <linux/skbuff.h>
37 #include <linux/init.h>
38 #include <linux/security.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
54 DECLARE_MUTEX(rtnl_sem);
55
56 void rtnl_lock(void)
57 {
58         rtnl_shlock();
59         rtnl_exlock();
60 }
61  
62 void rtnl_unlock(void)
63 {
64         rtnl_exunlock();
65         rtnl_shunlock();
66
67         netdev_run_todo();
68 }
69
70 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
71 {
72         memset(tb, 0, sizeof(struct rtattr*)*maxattr);
73
74         while (RTA_OK(rta, len)) {
75                 unsigned flavor = rta->rta_type;
76                 if (flavor && flavor <= maxattr)
77                         tb[flavor-1] = rta;
78                 rta = RTA_NEXT(rta, len);
79         }
80         return 0;
81 }
82
83 struct sock *rtnl;
84
85 struct rtnetlink_link * rtnetlink_links[NPROTO];
86
87 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
88 {
89         NLMSG_LENGTH(sizeof(struct ifinfomsg)),
90         NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
91         NLMSG_LENGTH(sizeof(struct rtmsg)),
92         NLMSG_LENGTH(sizeof(struct ndmsg)),
93         NLMSG_LENGTH(sizeof(struct rtmsg)),
94         NLMSG_LENGTH(sizeof(struct tcmsg)),
95         NLMSG_LENGTH(sizeof(struct tcmsg)),
96         NLMSG_LENGTH(sizeof(struct tcmsg)),
97         NLMSG_LENGTH(sizeof(struct tcamsg))
98 };
99
100 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
101 {
102         IFLA_MAX,
103         IFA_MAX,
104         RTA_MAX,
105         NDA_MAX,
106         RTA_MAX,
107         TCA_MAX,
108         TCA_MAX,
109         TCA_MAX,
110         TCAA_MAX
111 };
112
113 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
114 {
115         struct rtattr *rta;
116         int size = RTA_LENGTH(attrlen);
117
118         rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
119         rta->rta_type = attrtype;
120         rta->rta_len = size;
121         memcpy(RTA_DATA(rta), data, attrlen);
122 }
123
124 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
125 {
126         int err = 0;
127
128         NETLINK_CB(skb).dst_groups = group;
129         if (echo)
130                 atomic_inc(&skb->users);
131         netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
132         if (echo)
133                 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
134         return err;
135 }
136
137 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
138 {
139         struct rtattr *mx = (struct rtattr*)skb->tail;
140         int i;
141
142         RTA_PUT(skb, RTA_METRICS, 0, NULL);
143         for (i=0; i<RTAX_MAX; i++) {
144                 if (metrics[i])
145                         RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
146         }
147         mx->rta_len = skb->tail - (u8*)mx;
148         if (mx->rta_len == RTA_LENGTH(0))
149                 skb_trim(skb, (u8*)mx - skb->data);
150         return 0;
151
152 rtattr_failure:
153         skb_trim(skb, (u8*)mx - skb->data);
154         return -1;
155 }
156
157
158 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
159                                  int type, u32 pid, u32 seq, u32 change)
160 {
161         struct ifinfomsg *r;
162         struct nlmsghdr  *nlh;
163         unsigned char    *b = skb->tail;
164
165         nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
166         if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
167         r = NLMSG_DATA(nlh);
168         r->ifi_family = AF_UNSPEC;
169         r->ifi_type = dev->type;
170         r->ifi_index = dev->ifindex;
171         r->ifi_flags = dev->flags;
172         r->ifi_change = change;
173
174         if (!netif_running(dev) || !netif_carrier_ok(dev))
175                 r->ifi_flags &= ~IFF_RUNNING;
176         else
177                 r->ifi_flags |= IFF_RUNNING;
178
179         RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
180         if (dev->addr_len) {
181                 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
182                 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
183         }
184         if (1) {
185                 unsigned mtu = dev->mtu;
186                 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
187         }
188         if (dev->ifindex != dev->iflink)
189                 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
190         if (dev->qdisc_sleeping)
191                 RTA_PUT(skb, IFLA_QDISC,
192                         strlen(dev->qdisc_sleeping->ops->id) + 1,
193                         dev->qdisc_sleeping->ops->id);
194         if (dev->master)
195                 RTA_PUT(skb, IFLA_MASTER, sizeof(int), &dev->master->ifindex);
196         if (dev->get_stats) {
197                 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
198                 if (stats) {
199                         struct rtattr  *a;
200                         __u32          *s;
201                         int             i;
202                         int             n = sizeof(struct rtnl_link_stats)/4;
203
204                         a = __RTA_PUT(skb, IFLA_STATS, n*4);
205                         s = RTA_DATA(a);
206                         for (i=0; i<n; i++)
207                                 s[i] = stats[i];
208                 }
209         }
210         nlh->nlmsg_len = skb->tail - b;
211         return skb->len;
212
213 nlmsg_failure:
214 rtattr_failure:
215         skb_trim(skb, b - skb->data);
216         return -1;
217 }
218
219 int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
220 {
221         int idx;
222         int s_idx = cb->args[0];
223         struct net_device *dev;
224
225         read_lock(&dev_base_lock);
226         for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
227                 if (idx < s_idx)
228                         continue;
229                 if (!dev_in_nx_info(dev, current->nx_info))
230                         continue;
231                 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
232                         break;
233         }
234         read_unlock(&dev_base_lock);
235         cb->args[0] = idx;
236
237         return skb->len;
238 }
239
240 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
241 {
242         struct ifinfomsg  *ifm = NLMSG_DATA(nlh);
243         struct rtattr    **ida = arg;
244         struct net_device *dev;
245         int err;
246
247         dev = dev_get_by_index(ifm->ifi_index);
248         if (!dev)
249                 return -ENODEV;
250
251         err = -EINVAL;
252
253         if (ida[IFLA_ADDRESS - 1]) {
254                 if (!dev->set_mac_address) {
255                         err = -EOPNOTSUPP;
256                         goto out;
257                 }
258                 if (!netif_device_present(dev)) {
259                         err = -ENODEV;
260                         goto out;
261                 }
262                 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
263                         goto out;
264
265                 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
266                 if (err)
267                         goto out;
268         }
269
270         if (ida[IFLA_BROADCAST - 1]) {
271                 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
272                         goto out;
273                 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
274                        dev->addr_len);
275         }
276
277         err = 0;
278
279 out:
280         if (!err)
281                 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
282
283         dev_put(dev);
284         return err;
285 }
286
287 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
288 {
289         int idx;
290         int s_idx = cb->family;
291
292         if (s_idx == 0)
293                 s_idx = 1;
294         for (idx=1; idx<NPROTO; idx++) {
295                 int type = cb->nlh->nlmsg_type-RTM_BASE;
296                 if (idx < s_idx || idx == PF_PACKET)
297                         continue;
298                 if (rtnetlink_links[idx] == NULL ||
299                     rtnetlink_links[idx][type].dumpit == NULL)
300                         continue;
301                 if (idx > s_idx)
302                         memset(&cb->args[0], 0, sizeof(cb->args));
303                 if (rtnetlink_links[idx][type].dumpit(skb, cb))
304                         break;
305         }
306         cb->family = idx;
307
308         return skb->len;
309 }
310
311 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
312 {
313         struct sk_buff *skb;
314         int size = NLMSG_GOODSIZE;
315
316         if (!dev_in_nx_info(dev, current->nx_info))
317                 return;
318         skb = alloc_skb(size, GFP_KERNEL);
319         if (!skb)
320                 return;
321
322         if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
323                 kfree_skb(skb);
324                 return;
325         }
326         NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
327         netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
328 }
329
330 static int rtnetlink_done(struct netlink_callback *cb)
331 {
332         return 0;
333 }
334
335 /* Process one rtnetlink message. */
336
337 static __inline__ int
338 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
339 {
340         struct rtnetlink_link *link;
341         struct rtnetlink_link *link_tab;
342         struct rtattr   *rta[RTATTR_MAX];
343
344         int exclusive = 0;
345         int sz_idx, kind;
346         int min_len;
347         int family;
348         int type;
349         int err;
350
351         /* Only requests are handled by kernel now */
352         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
353                 return 0;
354
355         type = nlh->nlmsg_type;
356
357         /* A control message: ignore them */
358         if (type < RTM_BASE)
359                 return 0;
360
361         /* Unknown message: reply with EINVAL */
362         if (type > RTM_MAX)
363                 goto err_inval;
364
365         type -= RTM_BASE;
366
367         /* All the messages must have at least 1 byte length */
368         if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
369                 return 0;
370
371         family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
372         if (family >= NPROTO) {
373                 *errp = -EAFNOSUPPORT;
374                 return -1;
375         }
376
377         link_tab = rtnetlink_links[family];
378         if (link_tab == NULL)
379                 link_tab = rtnetlink_links[PF_UNSPEC];
380         link = &link_tab[type];
381
382         sz_idx = type>>2;
383         kind = type&3;
384
385         if (kind != 2 && security_netlink_recv(skb)) {
386                 *errp = -EPERM;
387                 return -1;
388         }
389
390         if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
391                 u32 rlen;
392
393                 if (link->dumpit == NULL)
394                         link = &(rtnetlink_links[PF_UNSPEC][type]);
395
396                 if (link->dumpit == NULL)
397                         goto err_inval;
398
399                 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
400                                                 link->dumpit,
401                                                 rtnetlink_done)) != 0) {
402                         return -1;
403                 }
404                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
405                 if (rlen > skb->len)
406                         rlen = skb->len;
407                 skb_pull(skb, rlen);
408                 return -1;
409         }
410
411         if (kind != 2) {
412                 if (rtnl_exlock_nowait()) {
413                         *errp = 0;
414                         return -1;
415                 }
416                 exclusive = 1;
417         }
418
419         memset(&rta, 0, sizeof(rta));
420
421         min_len = rtm_min[sz_idx];
422         if (nlh->nlmsg_len < min_len)
423                 goto err_inval;
424
425         if (nlh->nlmsg_len > min_len) {
426                 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
427                 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
428
429                 while (RTA_OK(attr, attrlen)) {
430                         unsigned flavor = attr->rta_type;
431                         if (flavor) {
432                                 if (flavor > rta_max[sz_idx])
433                                         goto err_inval;
434                                 rta[flavor-1] = attr;
435                         }
436                         attr = RTA_NEXT(attr, attrlen);
437                 }
438         }
439
440         if (link->doit == NULL)
441                 link = &(rtnetlink_links[PF_UNSPEC][type]);
442         if (link->doit == NULL)
443                 goto err_inval;
444         err = link->doit(skb, nlh, (void *)&rta);
445
446         if (exclusive)
447                 rtnl_exunlock();
448         *errp = err;
449         return err;
450
451 err_inval:
452         if (exclusive)
453                 rtnl_exunlock();
454         *errp = -EINVAL;
455         return -1;
456 }
457
458 /* 
459  * Process one packet of messages.
460  * Malformed skbs with wrong lengths of messages are discarded silently.
461  */
462
463 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
464 {
465         int err;
466         struct nlmsghdr * nlh;
467
468         while (skb->len >= NLMSG_SPACE(0)) {
469                 u32 rlen;
470
471                 nlh = (struct nlmsghdr *)skb->data;
472                 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
473                         return 0;
474                 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
475                 if (rlen > skb->len)
476                         rlen = skb->len;
477                 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
478                         /* Not error, but we must interrupt processing here:
479                          *   Note, that in this case we do not pull message
480                          *   from skb, it will be processed later.
481                          */
482                         if (err == 0)
483                                 return -1;
484                         netlink_ack(skb, nlh, err);
485                 } else if (nlh->nlmsg_flags&NLM_F_ACK)
486                         netlink_ack(skb, nlh, 0);
487                 skb_pull(skb, rlen);
488         }
489
490         return 0;
491 }
492
493 /*
494  *  rtnetlink input queue processing routine:
495  *      - try to acquire shared lock. If it is failed, defer processing.
496  *      - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
497  *        that will occur, when a dump started and/or acquisition of
498  *        exclusive lock failed.
499  */
500
501 static void rtnetlink_rcv(struct sock *sk, int len)
502 {
503         do {
504                 struct sk_buff *skb;
505
506                 if (rtnl_shlock_nowait())
507                         return;
508
509                 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
510                         if (rtnetlink_rcv_skb(skb)) {
511                                 if (skb->len)
512                                         skb_queue_head(&sk->sk_receive_queue,
513                                                        skb);
514                                 else
515                                         kfree_skb(skb);
516                                 break;
517                         }
518                         kfree_skb(skb);
519                 }
520
521                 up(&rtnl_sem);
522
523                 netdev_run_todo();
524         } while (rtnl && rtnl->sk_receive_queue.qlen);
525 }
526
527 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
528 {
529         [RTM_GETLINK  - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
530         [RTM_SETLINK  - RTM_BASE] = { .doit   = do_setlink            },
531         [RTM_GETADDR  - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
532         [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all    },
533         [RTM_NEWNEIGH - RTM_BASE] = { .doit   = neigh_add             },
534         [RTM_DELNEIGH - RTM_BASE] = { .doit   = neigh_delete          },
535         [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info       }
536 };
537
538 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
539 {
540         struct net_device *dev = ptr;
541         switch (event) {
542         case NETDEV_UNREGISTER:
543                 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
544                 break;
545         case NETDEV_REGISTER:
546                 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
547                 break;
548         case NETDEV_UP:
549         case NETDEV_DOWN:
550                 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
551                 break;
552         case NETDEV_CHANGE:
553         case NETDEV_GOING_DOWN:
554                 break;
555         default:
556                 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
557                 break;
558         }
559         return NOTIFY_DONE;
560 }
561
562 static struct notifier_block rtnetlink_dev_notifier = {
563         .notifier_call  = rtnetlink_event,
564 };
565
566 void __init rtnetlink_init(void)
567 {
568         rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
569         if (rtnl == NULL)
570                 panic("rtnetlink_init: cannot initialize rtnetlink\n");
571         netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
572         register_netdevice_notifier(&rtnetlink_dev_notifier);
573         rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
574         rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
575 }
576
577 EXPORT_SYMBOL(__rta_fill);
578 EXPORT_SYMBOL(rtattr_parse);
579 EXPORT_SYMBOL(rtnetlink_dump_ifinfo);
580 EXPORT_SYMBOL(rtnetlink_links);
581 EXPORT_SYMBOL(rtnetlink_put_metrics);
582 EXPORT_SYMBOL(rtnl);
583 EXPORT_SYMBOL(rtnl_lock);
584 EXPORT_SYMBOL(rtnl_sem);
585 EXPORT_SYMBOL(rtnl_unlock);