* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / net / core / rtnetlink.c
blobb4d8582102e918bb7ceb2dd93243ac2f9e3e5958
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/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/major.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
31 #include <linux/mm.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/capability.h>
35 #include <linux/skbuff.h>
36 #include <linux/init.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/string.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
44 #include <net/ip.h>
45 #include <net/protocol.h>
46 #include <net/arp.h>
47 #include <net/route.h>
48 #include <net/tcp.h>
49 #include <net/udp.h>
50 #include <net/sock.h>
51 #include <net/pkt_sched.h>
53 DECLARE_MUTEX(rtnl_sem);
55 void rtnl_lock(void)
57 rtnl_shlock();
58 rtnl_exlock();
61 void rtnl_unlock(void)
63 rtnl_exunlock();
64 rtnl_shunlock();
67 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
69 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
71 while (RTA_OK(rta, len)) {
72 unsigned flavor = rta->rta_type;
73 if (flavor && flavor <= maxattr)
74 tb[flavor-1] = rta;
75 rta = RTA_NEXT(rta, len);
77 return 0;
80 #ifdef CONFIG_RTNETLINK
81 struct sock *rtnl;
83 struct rtnetlink_link * rtnetlink_links[NPROTO];
85 #define _S 1 /* superuser privileges required */
86 #define _X 2 /* exclusive access to tables required */
87 #define _G 4 /* GET request */
89 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
91 NLMSG_LENGTH(sizeof(struct ifinfomsg)),
92 NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
93 NLMSG_LENGTH(sizeof(struct rtmsg)),
94 NLMSG_LENGTH(sizeof(struct ndmsg)),
95 NLMSG_LENGTH(sizeof(struct rtmsg)),
96 NLMSG_LENGTH(sizeof(struct tcmsg)),
97 NLMSG_LENGTH(sizeof(struct tcmsg)),
98 NLMSG_LENGTH(sizeof(struct tcmsg))
101 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
103 IFLA_MAX,
104 IFA_MAX,
105 RTA_MAX,
106 NDA_MAX,
107 RTA_MAX,
108 TCA_MAX,
109 TCA_MAX,
110 TCA_MAX
113 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
115 struct rtattr *rta;
116 int size = RTA_LENGTH(attrlen);
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);
124 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
126 int err = 0;
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;
137 int rtnetlink_put_metrics(struct sk_buff *skb, unsigned *metrics)
139 struct rtattr *mx = (struct rtattr*)skb->tail;
140 int i;
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(unsigned), metrics+i);
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;
152 rtattr_failure:
153 skb_trim(skb, (u8*)mx - skb->data);
154 return -1;
158 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
159 int type, u32 pid, u32 seq, u32 change)
161 struct ifinfomsg *r;
162 struct nlmsghdr *nlh;
163 unsigned char *b = skb->tail;
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;
174 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
175 if (dev->addr_len) {
176 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
177 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
179 if (1) {
180 unsigned mtu = dev->mtu;
181 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
183 if (dev->ifindex != dev->iflink)
184 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
185 if (dev->qdisc_sleeping)
186 RTA_PUT(skb, IFLA_QDISC,
187 strlen(dev->qdisc_sleeping->ops->id) + 1,
188 dev->qdisc_sleeping->ops->id);
189 if (dev->get_stats) {
190 struct net_device_stats *stats = dev->get_stats(dev);
191 if (stats)
192 RTA_PUT(skb, IFLA_STATS, sizeof(*stats), stats);
194 nlh->nlmsg_len = skb->tail - b;
195 return skb->len;
197 nlmsg_failure:
198 rtattr_failure:
199 skb_trim(skb, b - skb->data);
200 return -1;
203 int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
205 int idx;
206 int s_idx = cb->args[0];
207 struct net_device *dev;
209 read_lock(&dev_base_lock);
210 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
211 if (idx < s_idx)
212 continue;
213 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
214 break;
216 read_unlock(&dev_base_lock);
217 cb->args[0] = idx;
219 return skb->len;
222 int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
224 int idx;
225 int s_idx = cb->family;
227 if (s_idx == 0)
228 s_idx = 1;
229 for (idx=1; idx<NPROTO; idx++) {
230 int type = cb->nlh->nlmsg_type-RTM_BASE;
231 if (idx < s_idx || idx == PF_PACKET)
232 continue;
233 if (rtnetlink_links[idx] == NULL ||
234 rtnetlink_links[idx][type].dumpit == NULL)
235 continue;
236 if (idx > s_idx)
237 memset(&cb->args[0], 0, sizeof(cb->args));
238 if (rtnetlink_links[idx][type].dumpit(skb, cb))
239 break;
241 cb->family = idx;
243 return skb->len;
246 void rtmsg_ifinfo(int type, struct net_device *dev)
248 struct sk_buff *skb;
249 int size = NLMSG_GOODSIZE;
251 skb = alloc_skb(size, GFP_KERNEL);
252 if (!skb)
253 return;
255 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, ~0U) < 0) {
256 kfree_skb(skb);
257 return;
259 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
260 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
263 static int rtnetlink_done(struct netlink_callback *cb)
265 return 0;
268 /* Process one rtnetlink message. */
270 extern __inline__ int
271 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
273 struct rtnetlink_link *link;
274 struct rtnetlink_link *link_tab;
275 struct rtattr *rta[RTATTR_MAX];
277 int exclusive = 0;
278 int sz_idx, kind;
279 int min_len;
280 int family;
281 int type;
282 int err;
284 /* Only requests are handled by kernel now */
285 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
286 return 0;
288 type = nlh->nlmsg_type;
290 /* A control message: ignore them */
291 if (type < RTM_BASE)
292 return 0;
294 /* Unknown message: reply with EINVAL */
295 if (type > RTM_MAX)
296 goto err_inval;
298 type -= RTM_BASE;
300 /* All the messages must have at least 1 byte length */
301 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
302 return 0;
304 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
305 if (family > NPROTO) {
306 *errp = -EAFNOSUPPORT;
307 return -1;
310 link_tab = rtnetlink_links[family];
311 if (link_tab == NULL)
312 link_tab = rtnetlink_links[PF_UNSPEC];
313 link = &link_tab[type];
315 sz_idx = type>>2;
316 kind = type&3;
318 if (kind != 2 && !cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
319 *errp = -EPERM;
320 return -1;
323 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
324 u32 rlen;
326 if (link->dumpit == NULL)
327 link = &(rtnetlink_links[PF_UNSPEC][type]);
329 if (link->dumpit == NULL)
330 goto err_inval;
332 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
333 link->dumpit,
334 rtnetlink_done)) != 0) {
335 return -1;
337 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
338 if (rlen > skb->len)
339 rlen = skb->len;
340 skb_pull(skb, rlen);
341 return -1;
344 if (kind != 2) {
345 if (rtnl_exlock_nowait()) {
346 *errp = 0;
347 return -1;
349 exclusive = 1;
352 memset(&rta, 0, sizeof(rta));
354 min_len = rtm_min[sz_idx];
355 if (nlh->nlmsg_len < min_len)
356 goto err_inval;
358 if (nlh->nlmsg_len > min_len) {
359 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
360 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
362 while (RTA_OK(attr, attrlen)) {
363 unsigned flavor = attr->rta_type;
364 if (flavor) {
365 if (flavor > rta_max[sz_idx])
366 goto err_inval;
367 rta[flavor-1] = attr;
369 attr = RTA_NEXT(attr, attrlen);
373 if (link->doit == NULL)
374 link = &(rtnetlink_links[PF_UNSPEC][type]);
375 if (link->doit == NULL)
376 goto err_inval;
377 err = link->doit(skb, nlh, (void *)&rta);
379 if (exclusive)
380 rtnl_exunlock();
381 *errp = err;
382 return err;
384 err_inval:
385 if (exclusive)
386 rtnl_exunlock();
387 *errp = -EINVAL;
388 return -1;
392 * Process one packet of messages.
393 * Malformed skbs with wrong lengths of messages are discarded silently.
396 extern __inline__ int rtnetlink_rcv_skb(struct sk_buff *skb)
398 int err;
399 struct nlmsghdr * nlh;
401 while (skb->len >= NLMSG_SPACE(0)) {
402 u32 rlen;
404 nlh = (struct nlmsghdr *)skb->data;
405 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
406 return 0;
407 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
408 if (rlen > skb->len)
409 rlen = skb->len;
410 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
411 /* Not error, but we must interrupt processing here:
412 * Note, that in this case we do not pull message
413 * from skb, it will be processed later.
415 if (err == 0)
416 return -1;
417 netlink_ack(skb, nlh, err);
418 } else if (nlh->nlmsg_flags&NLM_F_ACK)
419 netlink_ack(skb, nlh, 0);
420 skb_pull(skb, rlen);
423 return 0;
427 * rtnetlink input queue processing routine:
428 * - try to acquire shared lock. If it is failed, defer processing.
429 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
430 * that will occur, when a dump started and/or acquisition of
431 * exclusive lock failed.
434 static void rtnetlink_rcv(struct sock *sk, int len)
436 do {
437 struct sk_buff *skb;
439 if (rtnl_shlock_nowait())
440 return;
442 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
443 if (rtnetlink_rcv_skb(skb)) {
444 if (skb->len)
445 skb_queue_head(&sk->receive_queue, skb);
446 else
447 kfree_skb(skb);
448 break;
450 kfree_skb(skb);
453 up(&rtnl_sem);
454 } while (rtnl && rtnl->receive_queue.qlen);
457 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
459 { NULL, NULL, },
460 { NULL, NULL, },
461 { NULL, rtnetlink_dump_ifinfo, },
462 { NULL, NULL, },
464 { NULL, NULL, },
465 { NULL, NULL, },
466 { NULL, rtnetlink_dump_all, },
467 { NULL, NULL, },
469 { NULL, NULL, },
470 { NULL, NULL, },
471 { NULL, rtnetlink_dump_all, },
472 { NULL, NULL, },
474 { neigh_add, NULL, },
475 { neigh_delete, NULL, },
476 { NULL, neigh_dump_info, },
477 { NULL, NULL, },
479 { NULL, NULL, },
480 { NULL, NULL, },
481 { NULL, NULL, },
482 { NULL, NULL, },
486 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
488 struct net_device *dev = ptr;
489 switch (event) {
490 case NETDEV_UNREGISTER:
491 rtmsg_ifinfo(RTM_DELLINK, dev);
492 break;
493 default:
494 rtmsg_ifinfo(RTM_NEWLINK, dev);
495 break;
497 return NOTIFY_DONE;
500 struct notifier_block rtnetlink_dev_notifier = {
501 rtnetlink_event,
502 NULL,
507 void __init rtnetlink_init(void)
509 #ifdef RTNL_DEBUG
510 printk("Initializing RT netlink socket\n");
511 #endif
512 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
513 if (rtnl == NULL)
514 panic("rtnetlink_init: cannot initialize rtnetlink\n");
515 register_netdevice_notifier(&rtnetlink_dev_notifier);
516 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
517 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
522 #endif