initial commit with v2.6.9
[linux-2.6.9-moxart.git] / net / core / rtnetlink.c
blob52641b0a5de8ceabf99e716851697c042f9e405e
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.
6 * Routing netlink socket interface: protocol independent part.
8 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
15 * Fixes:
16 * Vitaly E. Lavrov RTA_OK arithmetics was wrong.
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>
40 #include <asm/uaccess.h>
41 #include <asm/system.h>
42 #include <asm/string.h>
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>
54 DECLARE_MUTEX(rtnl_sem);
56 void rtnl_lock(void)
58 rtnl_shlock();
61 void rtnl_unlock(void)
63 rtnl_shunlock();
65 netdev_run_todo();
68 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
70 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
72 while (RTA_OK(rta, len)) {
73 unsigned flavor = rta->rta_type;
74 if (flavor && flavor <= maxattr)
75 tb[flavor-1] = rta;
76 rta = RTA_NEXT(rta, len);
78 return 0;
81 struct sock *rtnl;
83 struct rtnetlink_link * rtnetlink_links[NPROTO];
85 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
87 NLMSG_LENGTH(sizeof(struct ifinfomsg)),
88 NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
89 NLMSG_LENGTH(sizeof(struct rtmsg)),
90 NLMSG_LENGTH(sizeof(struct ndmsg)),
91 NLMSG_LENGTH(sizeof(struct rtmsg)),
92 NLMSG_LENGTH(sizeof(struct tcmsg)),
93 NLMSG_LENGTH(sizeof(struct tcmsg)),
94 NLMSG_LENGTH(sizeof(struct tcmsg)),
95 NLMSG_LENGTH(sizeof(struct tcamsg))
98 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
100 IFLA_MAX,
101 IFA_MAX,
102 RTA_MAX,
103 NDA_MAX,
104 RTA_MAX,
105 TCA_MAX,
106 TCA_MAX,
107 TCA_MAX,
108 TCAA_MAX
111 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
113 struct rtattr *rta;
114 int size = RTA_LENGTH(attrlen);
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);
122 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
124 int err = 0;
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;
135 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
137 struct rtattr *mx = (struct rtattr*)skb->tail;
138 int i;
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);
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;
150 rtattr_failure:
151 skb_trim(skb, (u8*)mx - skb->data);
152 return -1;
156 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
157 int type, u32 pid, u32 seq, u32 change)
159 struct ifinfomsg *r;
160 struct nlmsghdr *nlh;
161 unsigned char *b = skb->tail;
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_get_flags(dev);
170 r->ifi_change = change;
172 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
174 if (1) {
175 u32 txqlen = dev->tx_queue_len;
176 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
179 if (1) {
180 u32 weight = dev->weight;
181 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
184 if (1) {
185 struct rtnl_link_ifmap map = {
186 .mem_start = dev->mem_start,
187 .mem_end = dev->mem_end,
188 .base_addr = dev->base_addr,
189 .irq = dev->irq,
190 .dma = dev->dma,
191 .port = dev->if_port,
193 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
196 if (dev->addr_len) {
197 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
198 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
201 if (1) {
202 u32 mtu = dev->mtu;
203 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
206 if (dev->ifindex != dev->iflink) {
207 u32 iflink = dev->iflink;
208 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
211 if (dev->qdisc_sleeping)
212 RTA_PUT(skb, IFLA_QDISC,
213 strlen(dev->qdisc_sleeping->ops->id) + 1,
214 dev->qdisc_sleeping->ops->id);
216 if (dev->master) {
217 u32 master = dev->master->ifindex;
218 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
221 if (dev->get_stats) {
222 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
223 if (stats) {
224 struct rtattr *a;
225 __u32 *s;
226 int i;
227 int n = sizeof(struct rtnl_link_stats)/4;
229 a = __RTA_PUT(skb, IFLA_STATS, n*4);
230 s = RTA_DATA(a);
231 for (i=0; i<n; i++)
232 s[i] = stats[i];
235 nlh->nlmsg_len = skb->tail - b;
236 return skb->len;
238 nlmsg_failure:
239 rtattr_failure:
240 skb_trim(skb, b - skb->data);
241 return -1;
244 int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
246 int idx;
247 int s_idx = cb->args[0];
248 struct net_device *dev;
250 read_lock(&dev_base_lock);
251 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
252 if (idx < s_idx)
253 continue;
254 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
255 break;
257 read_unlock(&dev_base_lock);
258 cb->args[0] = idx;
260 return skb->len;
263 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
265 struct ifinfomsg *ifm = NLMSG_DATA(nlh);
266 struct rtattr **ida = arg;
267 struct net_device *dev;
268 int err, send_addr_notify = 0;
270 dev = dev_get_by_index(ifm->ifi_index);
271 if (!dev)
272 return -ENODEV;
274 err = -EINVAL;
276 if (ifm->ifi_flags)
277 dev_change_flags(dev, ifm->ifi_flags);
279 if (ida[IFLA_MAP - 1]) {
280 struct rtnl_link_ifmap *u_map;
281 struct ifmap k_map;
283 if (!dev->set_config) {
284 err = -EOPNOTSUPP;
285 goto out;
288 if (!netif_device_present(dev)) {
289 err = -ENODEV;
290 goto out;
293 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
294 goto out;
296 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
298 k_map.mem_start = (unsigned long) u_map->mem_start;
299 k_map.mem_end = (unsigned long) u_map->mem_end;
300 k_map.base_addr = (unsigned short) u_map->base_addr;
301 k_map.irq = (unsigned char) u_map->irq;
302 k_map.dma = (unsigned char) u_map->dma;
303 k_map.port = (unsigned char) u_map->port;
305 err = dev->set_config(dev, &k_map);
307 if (err)
308 goto out;
311 if (ida[IFLA_ADDRESS - 1]) {
312 if (!dev->set_mac_address) {
313 err = -EOPNOTSUPP;
314 goto out;
316 if (!netif_device_present(dev)) {
317 err = -ENODEV;
318 goto out;
320 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
321 goto out;
323 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
324 if (err)
325 goto out;
326 send_addr_notify = 1;
329 if (ida[IFLA_BROADCAST - 1]) {
330 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
331 goto out;
332 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
333 dev->addr_len);
334 send_addr_notify = 1;
337 if (ida[IFLA_MTU - 1]) {
338 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
339 goto out;
340 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
342 if (err)
343 goto out;
347 if (ida[IFLA_TXQLEN - 1]) {
348 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
349 goto out;
351 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
354 if (ida[IFLA_WEIGHT - 1]) {
355 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
356 goto out;
358 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
361 if (ida[IFLA_IFNAME - 1]) {
362 char ifname[IFNAMSIZ];
364 if (ida[IFLA_IFNAME - 1]->rta_len > RTA_LENGTH(sizeof(ifname)))
365 goto out;
367 memset(ifname, 0, sizeof(ifname));
368 memcpy(ifname, RTA_DATA(ida[IFLA_IFNAME - 1]),
369 RTA_PAYLOAD(ida[IFLA_IFNAME - 1]));
370 ifname[IFNAMSIZ - 1] = '\0';
372 err = dev_change_name(dev, ifname);
374 if (err)
375 goto out;
378 err = 0;
380 out:
381 if (send_addr_notify)
382 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
384 dev_put(dev);
385 return err;
388 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
390 int idx;
391 int s_idx = cb->family;
393 if (s_idx == 0)
394 s_idx = 1;
395 for (idx=1; idx<NPROTO; idx++) {
396 int type = cb->nlh->nlmsg_type-RTM_BASE;
397 if (idx < s_idx || idx == PF_PACKET)
398 continue;
399 if (rtnetlink_links[idx] == NULL ||
400 rtnetlink_links[idx][type].dumpit == NULL)
401 continue;
402 if (idx > s_idx)
403 memset(&cb->args[0], 0, sizeof(cb->args));
404 if (rtnetlink_links[idx][type].dumpit(skb, cb))
405 break;
407 cb->family = idx;
409 return skb->len;
412 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
414 struct sk_buff *skb;
415 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
416 sizeof(struct rtnl_link_ifmap) +
417 sizeof(struct rtnl_link_stats) + 128);
419 skb = alloc_skb(size, GFP_KERNEL);
420 if (!skb)
421 return;
423 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
424 kfree_skb(skb);
425 return;
427 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
428 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
431 static int rtnetlink_done(struct netlink_callback *cb)
433 return 0;
436 /* Protected by RTNL sempahore. */
437 static struct rtattr **rta_buf;
438 static int rtattr_max;
440 /* Process one rtnetlink message. */
442 static __inline__ int
443 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
445 struct rtnetlink_link *link;
446 struct rtnetlink_link *link_tab;
447 int sz_idx, kind;
448 int min_len;
449 int family;
450 int type;
451 int err;
453 /* Only requests are handled by kernel now */
454 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
455 return 0;
457 type = nlh->nlmsg_type;
459 /* A control message: ignore them */
460 if (type < RTM_BASE)
461 return 0;
463 /* Unknown message: reply with EINVAL */
464 if (type > RTM_MAX)
465 goto err_inval;
467 type -= RTM_BASE;
469 /* All the messages must have at least 1 byte length */
470 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
471 return 0;
473 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
474 if (family >= NPROTO) {
475 *errp = -EAFNOSUPPORT;
476 return -1;
479 link_tab = rtnetlink_links[family];
480 if (link_tab == NULL)
481 link_tab = rtnetlink_links[PF_UNSPEC];
482 link = &link_tab[type];
484 sz_idx = type>>2;
485 kind = type&3;
487 if (kind != 2 && security_netlink_recv(skb)) {
488 *errp = -EPERM;
489 return -1;
492 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
493 u32 rlen;
495 if (link->dumpit == NULL)
496 link = &(rtnetlink_links[PF_UNSPEC][type]);
498 if (link->dumpit == NULL)
499 goto err_inval;
501 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
502 link->dumpit,
503 rtnetlink_done)) != 0) {
504 return -1;
506 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
507 if (rlen > skb->len)
508 rlen = skb->len;
509 skb_pull(skb, rlen);
510 return -1;
513 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
515 min_len = rtm_min[sz_idx];
516 if (nlh->nlmsg_len < min_len)
517 goto err_inval;
519 if (nlh->nlmsg_len > min_len) {
520 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
521 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
523 while (RTA_OK(attr, attrlen)) {
524 unsigned flavor = attr->rta_type;
525 if (flavor) {
526 if (flavor > rta_max[sz_idx])
527 goto err_inval;
528 rta_buf[flavor-1] = attr;
530 attr = RTA_NEXT(attr, attrlen);
534 if (link->doit == NULL)
535 link = &(rtnetlink_links[PF_UNSPEC][type]);
536 if (link->doit == NULL)
537 goto err_inval;
538 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
540 *errp = err;
541 return err;
543 err_inval:
544 *errp = -EINVAL;
545 return -1;
549 * Process one packet of messages.
550 * Malformed skbs with wrong lengths of messages are discarded silently.
553 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
555 int err;
556 struct nlmsghdr * nlh;
558 while (skb->len >= NLMSG_SPACE(0)) {
559 u32 rlen;
561 nlh = (struct nlmsghdr *)skb->data;
562 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
563 return 0;
564 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
565 if (rlen > skb->len)
566 rlen = skb->len;
567 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
568 /* Not error, but we must interrupt processing here:
569 * Note, that in this case we do not pull message
570 * from skb, it will be processed later.
572 if (err == 0)
573 return -1;
574 netlink_ack(skb, nlh, err);
575 } else if (nlh->nlmsg_flags&NLM_F_ACK)
576 netlink_ack(skb, nlh, 0);
577 skb_pull(skb, rlen);
580 return 0;
584 * rtnetlink input queue processing routine:
585 * - try to acquire shared lock. If it is failed, defer processing.
586 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
587 * that will occur, when a dump started and/or acquisition of
588 * exclusive lock failed.
591 static void rtnetlink_rcv(struct sock *sk, int len)
593 do {
594 struct sk_buff *skb;
596 if (rtnl_shlock_nowait())
597 return;
599 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
600 if (rtnetlink_rcv_skb(skb)) {
601 if (skb->len)
602 skb_queue_head(&sk->sk_receive_queue,
603 skb);
604 else
605 kfree_skb(skb);
606 break;
608 kfree_skb(skb);
611 up(&rtnl_sem);
613 netdev_run_todo();
614 } while (rtnl && rtnl->sk_receive_queue.qlen);
617 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
619 [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
620 [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
621 [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
622 [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
623 [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
624 [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
625 [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info }
628 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
630 struct net_device *dev = ptr;
631 switch (event) {
632 case NETDEV_UNREGISTER:
633 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
634 break;
635 case NETDEV_REGISTER:
636 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
637 break;
638 case NETDEV_UP:
639 case NETDEV_DOWN:
640 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
641 break;
642 case NETDEV_CHANGE:
643 case NETDEV_GOING_DOWN:
644 break;
645 default:
646 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
647 break;
649 return NOTIFY_DONE;
652 static struct notifier_block rtnetlink_dev_notifier = {
653 .notifier_call = rtnetlink_event,
656 void __init rtnetlink_init(void)
658 int i;
660 rtattr_max = 0;
661 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
662 if (rta_max[i] > rtattr_max)
663 rtattr_max = rta_max[i];
664 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
665 if (!rta_buf)
666 panic("rtnetlink_init: cannot allocate rta_buf\n");
668 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
669 if (rtnl == NULL)
670 panic("rtnetlink_init: cannot initialize rtnetlink\n");
671 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
672 register_netdevice_notifier(&rtnetlink_dev_notifier);
673 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
674 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
677 EXPORT_SYMBOL(__rta_fill);
678 EXPORT_SYMBOL(rtattr_parse);
679 EXPORT_SYMBOL(rtnetlink_dump_ifinfo);
680 EXPORT_SYMBOL(rtnetlink_links);
681 EXPORT_SYMBOL(rtnetlink_put_metrics);
682 EXPORT_SYMBOL(rtnl);
683 EXPORT_SYMBOL(rtnl_lock);
684 EXPORT_SYMBOL(rtnl_sem);
685 EXPORT_SYMBOL(rtnl_unlock);