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