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/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.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>
37 #include <linux/security.h>
39 #include <asm/uaccess.h>
40 #include <asm/system.h>
41 #include <asm/string.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
46 #include <net/protocol.h>
48 #include <net/route.h>
51 #include <net/pkt_sched.h>
53 DECLARE_MUTEX(rtnl_sem);
60 int rtnl_lock_interruptible(void)
62 return down_interruptible(&rtnl_sem);
65 void rtnl_unlock(void)
72 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
74 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
76 while (RTA_OK(rta, len)) {
77 unsigned flavor = rta->rta_type;
78 if (flavor && flavor <= maxattr)
80 rta = RTA_NEXT(rta, len);
87 struct rtnetlink_link * rtnetlink_links[NPROTO];
89 static const int rtm_min[RTM_NR_FAMILIES] =
91 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
92 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
93 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
94 [RTM_FAM(RTM_NEWNEIGH)] = NLMSG_LENGTH(sizeof(struct ndmsg)),
95 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
96 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
97 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
98 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
99 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
100 [RTM_FAM(RTM_NEWPREFIX)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
101 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
102 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
105 static const int rta_max[RTM_NR_FAMILIES] =
107 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
108 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
109 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
110 [RTM_FAM(RTM_NEWNEIGH)] = NDA_MAX,
111 [RTM_FAM(RTM_NEWRULE)] = RTA_MAX,
112 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
113 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
114 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
115 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
118 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
121 int size = RTA_LENGTH(attrlen);
123 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
124 rta->rta_type = attrtype;
126 memcpy(RTA_DATA(rta), data, attrlen);
129 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
131 size_t ret = RTA_PAYLOAD(rta);
132 char *src = RTA_DATA(rta);
134 if (ret > 0 && src[ret - 1] == '\0')
137 size_t len = (ret >= size) ? size - 1 : ret;
138 memset(dest, 0, size);
139 memcpy(dest, src, len);
144 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
148 NETLINK_CB(skb).dst_groups = group;
150 atomic_inc(&skb->users);
151 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
153 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
157 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
159 struct rtattr *mx = (struct rtattr*)skb->tail;
162 RTA_PUT(skb, RTA_METRICS, 0, NULL);
163 for (i=0; i<RTAX_MAX; i++) {
165 RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
167 mx->rta_len = skb->tail - (u8*)mx;
168 if (mx->rta_len == RTA_LENGTH(0))
169 skb_trim(skb, (u8*)mx - skb->data);
173 skb_trim(skb, (u8*)mx - skb->data);
178 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
179 int type, u32 pid, u32 seq, u32 change)
182 struct nlmsghdr *nlh;
183 unsigned char *b = skb->tail;
185 nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
186 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
188 r->ifi_family = AF_UNSPEC;
189 r->ifi_type = dev->type;
190 r->ifi_index = dev->ifindex;
191 r->ifi_flags = dev_get_flags(dev);
192 r->ifi_change = change;
194 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
197 u32 txqlen = dev->tx_queue_len;
198 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
202 u32 weight = dev->weight;
203 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
207 struct rtnl_link_ifmap map = {
208 .mem_start = dev->mem_start,
209 .mem_end = dev->mem_end,
210 .base_addr = dev->base_addr,
213 .port = dev->if_port,
215 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
219 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
220 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
225 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
228 if (dev->ifindex != dev->iflink) {
229 u32 iflink = dev->iflink;
230 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
233 if (dev->qdisc_sleeping)
234 RTA_PUT(skb, IFLA_QDISC,
235 strlen(dev->qdisc_sleeping->ops->id) + 1,
236 dev->qdisc_sleeping->ops->id);
239 u32 master = dev->master->ifindex;
240 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
243 if (dev->get_stats) {
244 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
249 int n = sizeof(struct rtnl_link_stats)/4;
251 a = __RTA_PUT(skb, IFLA_STATS, n*4);
257 nlh->nlmsg_len = skb->tail - b;
262 skb_trim(skb, b - skb->data);
266 static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
269 int s_idx = cb->args[0];
270 struct net_device *dev;
272 read_lock(&dev_base_lock);
273 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
276 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
279 read_unlock(&dev_base_lock);
285 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
287 struct ifinfomsg *ifm = NLMSG_DATA(nlh);
288 struct rtattr **ida = arg;
289 struct net_device *dev;
290 int err, send_addr_notify = 0;
292 if (ifm->ifi_index >= 0)
293 dev = dev_get_by_index(ifm->ifi_index);
294 else if (ida[IFLA_IFNAME - 1]) {
295 char ifname[IFNAMSIZ];
297 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
298 IFNAMSIZ) >= IFNAMSIZ)
300 dev = dev_get_by_name(ifname);
310 dev_change_flags(dev, ifm->ifi_flags);
312 if (ida[IFLA_MAP - 1]) {
313 struct rtnl_link_ifmap *u_map;
316 if (!dev->set_config) {
321 if (!netif_device_present(dev)) {
326 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
329 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
331 k_map.mem_start = (unsigned long) u_map->mem_start;
332 k_map.mem_end = (unsigned long) u_map->mem_end;
333 k_map.base_addr = (unsigned short) u_map->base_addr;
334 k_map.irq = (unsigned char) u_map->irq;
335 k_map.dma = (unsigned char) u_map->dma;
336 k_map.port = (unsigned char) u_map->port;
338 err = dev->set_config(dev, &k_map);
344 if (ida[IFLA_ADDRESS - 1]) {
345 if (!dev->set_mac_address) {
349 if (!netif_device_present(dev)) {
353 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
356 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
359 send_addr_notify = 1;
362 if (ida[IFLA_BROADCAST - 1]) {
363 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
365 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
367 send_addr_notify = 1;
370 if (ida[IFLA_MTU - 1]) {
371 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
373 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
380 if (ida[IFLA_TXQLEN - 1]) {
381 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
384 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
387 if (ida[IFLA_WEIGHT - 1]) {
388 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
391 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
394 if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
395 char ifname[IFNAMSIZ];
397 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
398 IFNAMSIZ) >= IFNAMSIZ)
400 err = dev_change_name(dev, ifname);
408 if (send_addr_notify)
409 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
415 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
418 int s_idx = cb->family;
422 for (idx=1; idx<NPROTO; idx++) {
423 int type = cb->nlh->nlmsg_type-RTM_BASE;
424 if (idx < s_idx || idx == PF_PACKET)
426 if (rtnetlink_links[idx] == NULL ||
427 rtnetlink_links[idx][type].dumpit == NULL)
430 memset(&cb->args[0], 0, sizeof(cb->args));
431 if (rtnetlink_links[idx][type].dumpit(skb, cb))
439 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
442 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
443 sizeof(struct rtnl_link_ifmap) +
444 sizeof(struct rtnl_link_stats) + 128);
446 skb = alloc_skb(size, GFP_KERNEL);
450 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
454 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
455 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
458 static int rtnetlink_done(struct netlink_callback *cb)
463 /* Protected by RTNL sempahore. */
464 static struct rtattr **rta_buf;
465 static int rtattr_max;
467 /* Process one rtnetlink message. */
469 static __inline__ int
470 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
472 struct rtnetlink_link *link;
473 struct rtnetlink_link *link_tab;
480 /* Only requests are handled by kernel now */
481 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
484 type = nlh->nlmsg_type;
486 /* A control message: ignore them */
490 /* Unknown message: reply with EINVAL */
496 /* All the messages must have at least 1 byte length */
497 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
500 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
501 if (family >= NPROTO) {
502 *errp = -EAFNOSUPPORT;
506 link_tab = rtnetlink_links[family];
507 if (link_tab == NULL)
508 link_tab = rtnetlink_links[PF_UNSPEC];
509 link = &link_tab[type];
514 if (kind != 2 && security_netlink_recv(skb)) {
519 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
522 if (link->dumpit == NULL)
523 link = &(rtnetlink_links[PF_UNSPEC][type]);
525 if (link->dumpit == NULL)
528 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
530 rtnetlink_done)) != 0) {
533 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
540 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
542 min_len = rtm_min[sz_idx];
543 if (nlh->nlmsg_len < min_len)
546 if (nlh->nlmsg_len > min_len) {
547 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
548 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
550 while (RTA_OK(attr, attrlen)) {
551 unsigned flavor = attr->rta_type;
553 if (flavor > rta_max[sz_idx])
555 rta_buf[flavor-1] = attr;
557 attr = RTA_NEXT(attr, attrlen);
561 if (link->doit == NULL)
562 link = &(rtnetlink_links[PF_UNSPEC][type]);
563 if (link->doit == NULL)
565 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
576 * Process one packet of messages.
577 * Malformed skbs with wrong lengths of messages are discarded silently.
580 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
583 struct nlmsghdr * nlh;
585 while (skb->len >= NLMSG_SPACE(0)) {
588 nlh = (struct nlmsghdr *)skb->data;
589 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
591 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
594 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
595 /* Not error, but we must interrupt processing here:
596 * Note, that in this case we do not pull message
597 * from skb, it will be processed later.
601 netlink_ack(skb, nlh, err);
602 } else if (nlh->nlmsg_flags&NLM_F_ACK)
603 netlink_ack(skb, nlh, 0);
611 * rtnetlink input queue processing routine:
612 * - process as much as there was in the queue upon entry.
613 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
614 * that will occur, when a dump started.
617 static void rtnetlink_rcv(struct sock *sk, int len)
619 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
626 if (qlen > skb_queue_len(&sk->sk_receive_queue))
627 qlen = skb_queue_len(&sk->sk_receive_queue);
629 for (; qlen; qlen--) {
630 skb = skb_dequeue(&sk->sk_receive_queue);
631 if (rtnetlink_rcv_skb(skb)) {
633 skb_queue_head(&sk->sk_receive_queue,
648 static struct rtnetlink_link link_rtnetlink_table[RTM_NR_MSGTYPES] =
650 [RTM_GETLINK - RTM_BASE] = { .dumpit = rtnetlink_dump_ifinfo },
651 [RTM_SETLINK - RTM_BASE] = { .doit = do_setlink },
652 [RTM_GETADDR - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
653 [RTM_GETROUTE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
654 [RTM_NEWNEIGH - RTM_BASE] = { .doit = neigh_add },
655 [RTM_DELNEIGH - RTM_BASE] = { .doit = neigh_delete },
656 [RTM_GETNEIGH - RTM_BASE] = { .dumpit = neigh_dump_info },
657 [RTM_GETRULE - RTM_BASE] = { .dumpit = rtnetlink_dump_all },
660 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
662 struct net_device *dev = ptr;
664 case NETDEV_UNREGISTER:
665 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
667 case NETDEV_REGISTER:
668 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
672 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
675 case NETDEV_GOING_DOWN:
678 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
684 static struct notifier_block rtnetlink_dev_notifier = {
685 .notifier_call = rtnetlink_event,
688 void __init rtnetlink_init(void)
693 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
694 if (rta_max[i] > rtattr_max)
695 rtattr_max = rta_max[i];
696 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
698 panic("rtnetlink_init: cannot allocate rta_buf\n");
700 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
702 panic("rtnetlink_init: cannot initialize rtnetlink\n");
703 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
704 register_netdevice_notifier(&rtnetlink_dev_notifier);
705 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
706 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
709 EXPORT_SYMBOL(__rta_fill);
710 EXPORT_SYMBOL(rtattr_strlcpy);
711 EXPORT_SYMBOL(rtattr_parse);
712 EXPORT_SYMBOL(rtnetlink_links);
713 EXPORT_SYMBOL(rtnetlink_put_metrics);
715 EXPORT_SYMBOL(rtnl_lock);
716 EXPORT_SYMBOL(rtnl_lock_interruptible);
717 EXPORT_SYMBOL(rtnl_sem);
718 EXPORT_SYMBOL(rtnl_unlock);