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.
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>
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>
45 #include <net/protocol.h>
47 #include <net/route.h>
50 #include <net/pkt_sched.h>
52 DECLARE_MUTEX(rtnl_sem);
60 void rtnl_unlock(void)
66 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
68 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
70 while (RTA_OK(rta, len)) {
71 unsigned flavor = rta->rta_type;
72 if (flavor && flavor <= maxattr)
74 rta = RTA_NEXT(rta, len);
81 struct rtnetlink_link * rtnetlink_links[NPROTO];
83 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
85 NLMSG_LENGTH(sizeof(struct ifinfomsg)),
86 NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
87 NLMSG_LENGTH(sizeof(struct rtmsg)),
88 NLMSG_LENGTH(sizeof(struct ndmsg)),
89 NLMSG_LENGTH(sizeof(struct rtmsg)),
90 NLMSG_LENGTH(sizeof(struct tcmsg)),
91 NLMSG_LENGTH(sizeof(struct tcmsg)),
92 NLMSG_LENGTH(sizeof(struct tcmsg))
95 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
107 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
110 int size = RTA_LENGTH(attrlen);
112 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
113 rta->rta_type = attrtype;
115 memcpy(RTA_DATA(rta), data, attrlen);
118 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
122 NETLINK_CB(skb).dst_groups = group;
124 atomic_inc(&skb->users);
125 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
127 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
131 int rtnetlink_put_metrics(struct sk_buff *skb, unsigned *metrics)
133 struct rtattr *mx = (struct rtattr*)skb->tail;
136 RTA_PUT(skb, RTA_METRICS, 0, NULL);
137 for (i=0; i<RTAX_MAX; i++) {
139 RTA_PUT(skb, i+1, sizeof(unsigned), metrics+i);
141 mx->rta_len = skb->tail - (u8*)mx;
142 if (mx->rta_len == RTA_LENGTH(0))
143 skb_trim(skb, (u8*)mx - skb->data);
147 skb_trim(skb, (u8*)mx - skb->data);
152 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
153 int type, u32 pid, u32 seq, u32 change)
156 struct nlmsghdr *nlh;
157 unsigned char *b = skb->tail;
159 nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
160 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
162 r->ifi_family = AF_UNSPEC;
163 r->ifi_type = dev->type;
164 r->ifi_index = dev->ifindex;
165 r->ifi_flags = dev->flags;
166 r->ifi_change = change;
168 if (!netif_running(dev) || !netif_carrier_ok(dev))
169 r->ifi_flags &= ~IFF_RUNNING;
171 r->ifi_flags |= IFF_RUNNING;
173 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
175 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
176 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
179 unsigned mtu = dev->mtu;
180 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
182 if (dev->ifindex != dev->iflink)
183 RTA_PUT(skb, IFLA_LINK, sizeof(int), &dev->iflink);
184 if (dev->qdisc_sleeping)
185 RTA_PUT(skb, IFLA_QDISC,
186 strlen(dev->qdisc_sleeping->ops->id) + 1,
187 dev->qdisc_sleeping->ops->id);
189 RTA_PUT(skb, IFLA_MASTER, sizeof(int), &dev->master->ifindex);
190 if (dev->get_stats) {
191 struct net_device_stats *stats = dev->get_stats(dev);
193 RTA_PUT(skb, IFLA_STATS, sizeof(*stats), stats);
195 nlh->nlmsg_len = skb->tail - b;
200 skb_trim(skb, b - skb->data);
204 int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
207 int s_idx = cb->args[0];
208 struct net_device *dev;
210 read_lock(&dev_base_lock);
211 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
214 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
217 read_unlock(&dev_base_lock);
223 int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
226 int s_idx = cb->family;
230 for (idx=1; idx<NPROTO; idx++) {
231 int type = cb->nlh->nlmsg_type-RTM_BASE;
232 if (idx < s_idx || idx == PF_PACKET)
234 if (rtnetlink_links[idx] == NULL ||
235 rtnetlink_links[idx][type].dumpit == NULL)
238 memset(&cb->args[0], 0, sizeof(cb->args));
239 if (rtnetlink_links[idx][type].dumpit(skb, cb))
247 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
250 int size = NLMSG_GOODSIZE;
252 skb = alloc_skb(size, GFP_KERNEL);
256 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
260 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
261 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
264 static int rtnetlink_done(struct netlink_callback *cb)
269 /* Process one rtnetlink message. */
271 static __inline__ int
272 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
274 struct rtnetlink_link *link;
275 struct rtnetlink_link *link_tab;
276 struct rtattr *rta[RTATTR_MAX];
285 /* Only requests are handled by kernel now */
286 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
289 type = nlh->nlmsg_type;
291 /* A control message: ignore them */
295 /* Unknown message: reply with EINVAL */
301 /* All the messages must have at least 1 byte length */
302 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
305 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
306 if (family >= NPROTO) {
307 *errp = -EAFNOSUPPORT;
311 link_tab = rtnetlink_links[family];
312 if (link_tab == NULL)
313 link_tab = rtnetlink_links[PF_UNSPEC];
314 link = &link_tab[type];
319 if (kind != 2 && !cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
324 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
327 if (link->dumpit == NULL)
328 link = &(rtnetlink_links[PF_UNSPEC][type]);
330 if (link->dumpit == NULL)
333 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
335 rtnetlink_done)) != 0) {
338 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
346 if (rtnl_exlock_nowait()) {
353 memset(&rta, 0, sizeof(rta));
355 min_len = rtm_min[sz_idx];
356 if (nlh->nlmsg_len < min_len)
359 if (nlh->nlmsg_len > min_len) {
360 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
361 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
363 while (RTA_OK(attr, attrlen)) {
364 unsigned flavor = attr->rta_type;
366 if (flavor > rta_max[sz_idx])
368 rta[flavor-1] = attr;
370 attr = RTA_NEXT(attr, attrlen);
374 if (link->doit == NULL)
375 link = &(rtnetlink_links[PF_UNSPEC][type]);
376 if (link->doit == NULL)
378 err = link->doit(skb, nlh, (void *)&rta);
393 * Process one packet of messages.
394 * Malformed skbs with wrong lengths of messages are discarded silently.
397 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
400 struct nlmsghdr * nlh;
402 while (skb->len >= NLMSG_SPACE(0)) {
405 nlh = (struct nlmsghdr *)skb->data;
406 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
408 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
411 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
412 /* Not error, but we must interrupt processing here:
413 * Note, that in this case we do not pull message
414 * from skb, it will be processed later.
418 netlink_ack(skb, nlh, err);
419 } else if (nlh->nlmsg_flags&NLM_F_ACK)
420 netlink_ack(skb, nlh, 0);
428 * rtnetlink input queue processing routine:
429 * - try to acquire shared lock. If it is failed, defer processing.
430 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
431 * that will occur, when a dump started and/or acquisition of
432 * exclusive lock failed.
435 static void rtnetlink_rcv(struct sock *sk, int len)
440 if (rtnl_shlock_nowait())
443 while ((skb = skb_dequeue(&sk->receive_queue)) != NULL) {
444 if (rtnetlink_rcv_skb(skb)) {
446 skb_queue_head(&sk->receive_queue, skb);
455 } while (rtnl && rtnl->receive_queue.qlen);
458 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
462 { NULL, rtnetlink_dump_ifinfo, },
467 { NULL, rtnetlink_dump_all, },
472 { NULL, rtnetlink_dump_all, },
475 { neigh_add, NULL, },
476 { neigh_delete, NULL, },
477 { NULL, neigh_dump_info, },
487 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
489 struct net_device *dev = ptr;
491 case NETDEV_UNREGISTER:
492 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
494 case NETDEV_REGISTER:
495 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
499 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
502 case NETDEV_GOING_DOWN:
505 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
511 struct notifier_block rtnetlink_dev_notifier = {
518 void __init rtnetlink_init(void)
521 printk("Initializing RT netlink socket\n");
523 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
525 panic("rtnetlink_init: cannot initialize rtnetlink\n");
526 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
527 register_netdevice_notifier(&rtnetlink_dev_notifier);
528 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
529 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;