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/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>
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>
47 #include <net/protocol.h>
49 #include <net/route.h>
52 #include <net/pkt_sched.h>
54 DECLARE_MUTEX(rtnl_sem);
61 int rtnl_lock_interruptible(void)
63 return down_interruptible(&rtnl_sem);
66 void rtnl_unlock(void)
73 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
75 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
77 while (RTA_OK(rta, len)) {
78 unsigned flavor = rta->rta_type;
79 if (flavor && flavor <= maxattr)
81 rta = RTA_NEXT(rta, len);
88 struct rtnetlink_link * rtnetlink_links[NPROTO];
90 static const int rtm_min[(RTM_MAX+1-RTM_BASE)/4] =
92 NLMSG_LENGTH(sizeof(struct ifinfomsg)),
93 NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
94 NLMSG_LENGTH(sizeof(struct rtmsg)),
95 NLMSG_LENGTH(sizeof(struct ndmsg)),
96 NLMSG_LENGTH(sizeof(struct rtmsg)),
97 NLMSG_LENGTH(sizeof(struct tcmsg)),
98 NLMSG_LENGTH(sizeof(struct tcmsg)),
99 NLMSG_LENGTH(sizeof(struct tcmsg)),
100 NLMSG_LENGTH(sizeof(struct tcamsg))
103 static const int rta_max[(RTM_MAX+1-RTM_BASE)/4] =
116 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
119 int size = RTA_LENGTH(attrlen);
121 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
122 rta->rta_type = attrtype;
124 memcpy(RTA_DATA(rta), data, attrlen);
127 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
129 size_t ret = RTA_PAYLOAD(rta);
130 char *src = RTA_DATA(rta);
132 if (ret > 0 && src[ret - 1] == '\0')
135 size_t len = (ret >= size) ? size - 1 : ret;
136 memset(dest, 0, size);
137 memcpy(dest, src, len);
142 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
146 NETLINK_CB(skb).dst_groups = group;
148 atomic_inc(&skb->users);
149 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
151 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
155 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
157 struct rtattr *mx = (struct rtattr*)skb->tail;
160 RTA_PUT(skb, RTA_METRICS, 0, NULL);
161 for (i=0; i<RTAX_MAX; i++) {
163 RTA_PUT(skb, i+1, sizeof(u32), metrics+i);
165 mx->rta_len = skb->tail - (u8*)mx;
166 if (mx->rta_len == RTA_LENGTH(0))
167 skb_trim(skb, (u8*)mx - skb->data);
171 skb_trim(skb, (u8*)mx - skb->data);
176 static int rtnetlink_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
177 int type, u32 pid, u32 seq, u32 change)
180 struct nlmsghdr *nlh;
181 unsigned char *b = skb->tail;
183 nlh = NLMSG_PUT(skb, pid, seq, type, sizeof(*r));
184 if (pid) nlh->nlmsg_flags |= NLM_F_MULTI;
186 r->ifi_family = AF_UNSPEC;
187 r->ifi_type = dev->type;
188 r->ifi_index = dev->ifindex;
189 r->ifi_flags = dev_get_flags(dev);
190 r->ifi_change = change;
192 RTA_PUT(skb, IFLA_IFNAME, strlen(dev->name)+1, dev->name);
195 u32 txqlen = dev->tx_queue_len;
196 RTA_PUT(skb, IFLA_TXQLEN, sizeof(txqlen), &txqlen);
200 u32 weight = dev->weight;
201 RTA_PUT(skb, IFLA_WEIGHT, sizeof(weight), &weight);
205 struct rtnl_link_ifmap map = {
206 .mem_start = dev->mem_start,
207 .mem_end = dev->mem_end,
208 .base_addr = dev->base_addr,
211 .port = dev->if_port,
213 RTA_PUT(skb, IFLA_MAP, sizeof(map), &map);
217 RTA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
218 RTA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
223 RTA_PUT(skb, IFLA_MTU, sizeof(mtu), &mtu);
226 if (dev->ifindex != dev->iflink) {
227 u32 iflink = dev->iflink;
228 RTA_PUT(skb, IFLA_LINK, sizeof(iflink), &iflink);
231 if (dev->qdisc_sleeping)
232 RTA_PUT(skb, IFLA_QDISC,
233 strlen(dev->qdisc_sleeping->ops->id) + 1,
234 dev->qdisc_sleeping->ops->id);
237 u32 master = dev->master->ifindex;
238 RTA_PUT(skb, IFLA_MASTER, sizeof(master), &master);
241 if (dev->get_stats) {
242 unsigned long *stats = (unsigned long*)dev->get_stats(dev);
247 int n = sizeof(struct rtnl_link_stats)/4;
249 a = __RTA_PUT(skb, IFLA_STATS, n*4);
255 nlh->nlmsg_len = skb->tail - b;
260 skb_trim(skb, b - skb->data);
264 static int rtnetlink_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
267 int s_idx = cb->args[0];
268 struct net_device *dev;
270 read_lock(&dev_base_lock);
271 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
274 if (vx_info_flags(skb->sk->sk_vx_info, VXF_HIDE_NETIF, 0) &&
275 !dev_in_nx_info(dev, skb->sk->sk_nx_info))
277 if (rtnetlink_fill_ifinfo(skb, dev, RTM_NEWLINK, NETLINK_CB(cb->skb).pid, cb->nlh->nlmsg_seq, 0) <= 0)
280 read_unlock(&dev_base_lock);
286 static int do_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
288 struct ifinfomsg *ifm = NLMSG_DATA(nlh);
289 struct rtattr **ida = arg;
290 struct net_device *dev;
291 int err, send_addr_notify = 0;
293 if (ifm->ifi_index >= 0)
294 dev = dev_get_by_index(ifm->ifi_index);
295 else if (ida[IFLA_IFNAME - 1]) {
296 char ifname[IFNAMSIZ];
298 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
299 IFNAMSIZ) >= IFNAMSIZ)
301 dev = dev_get_by_name(ifname);
311 dev_change_flags(dev, ifm->ifi_flags);
313 if (ida[IFLA_MAP - 1]) {
314 struct rtnl_link_ifmap *u_map;
317 if (!dev->set_config) {
322 if (!netif_device_present(dev)) {
327 if (ida[IFLA_MAP - 1]->rta_len != RTA_LENGTH(sizeof(*u_map)))
330 u_map = RTA_DATA(ida[IFLA_MAP - 1]);
332 k_map.mem_start = (unsigned long) u_map->mem_start;
333 k_map.mem_end = (unsigned long) u_map->mem_end;
334 k_map.base_addr = (unsigned short) u_map->base_addr;
335 k_map.irq = (unsigned char) u_map->irq;
336 k_map.dma = (unsigned char) u_map->dma;
337 k_map.port = (unsigned char) u_map->port;
339 err = dev->set_config(dev, &k_map);
345 if (ida[IFLA_ADDRESS - 1]) {
346 if (!dev->set_mac_address) {
350 if (!netif_device_present(dev)) {
354 if (ida[IFLA_ADDRESS - 1]->rta_len != RTA_LENGTH(dev->addr_len))
357 err = dev->set_mac_address(dev, RTA_DATA(ida[IFLA_ADDRESS - 1]));
360 send_addr_notify = 1;
363 if (ida[IFLA_BROADCAST - 1]) {
364 if (ida[IFLA_BROADCAST - 1]->rta_len != RTA_LENGTH(dev->addr_len))
366 memcpy(dev->broadcast, RTA_DATA(ida[IFLA_BROADCAST - 1]),
368 send_addr_notify = 1;
371 if (ida[IFLA_MTU - 1]) {
372 if (ida[IFLA_MTU - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
374 err = dev_set_mtu(dev, *((u32 *) RTA_DATA(ida[IFLA_MTU - 1])));
381 if (ida[IFLA_TXQLEN - 1]) {
382 if (ida[IFLA_TXQLEN - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
385 dev->tx_queue_len = *((u32 *) RTA_DATA(ida[IFLA_TXQLEN - 1]));
388 if (ida[IFLA_WEIGHT - 1]) {
389 if (ida[IFLA_WEIGHT - 1]->rta_len != RTA_LENGTH(sizeof(u32)))
392 dev->weight = *((u32 *) RTA_DATA(ida[IFLA_WEIGHT - 1]));
395 if (ifm->ifi_index >= 0 && ida[IFLA_IFNAME - 1]) {
396 char ifname[IFNAMSIZ];
398 if (rtattr_strlcpy(ifname, ida[IFLA_IFNAME - 1],
399 IFNAMSIZ) >= IFNAMSIZ)
401 err = dev_change_name(dev, ifname);
409 if (send_addr_notify)
410 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
416 static int rtnetlink_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
419 int s_idx = cb->family;
423 for (idx=1; idx<NPROTO; idx++) {
424 int type = cb->nlh->nlmsg_type-RTM_BASE;
425 if (idx < s_idx || idx == PF_PACKET)
427 if (rtnetlink_links[idx] == NULL ||
428 rtnetlink_links[idx][type].dumpit == NULL)
431 memset(&cb->args[0], 0, sizeof(cb->args));
432 if (rtnetlink_links[idx][type].dumpit(skb, cb))
440 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
443 int size = NLMSG_SPACE(sizeof(struct ifinfomsg) +
444 sizeof(struct rtnl_link_ifmap) +
445 sizeof(struct rtnl_link_stats) + 128);
447 if (vx_flags(VXF_HIDE_NETIF, 0) &&
448 !dev_in_nx_info(dev, current->nx_info))
450 skb = alloc_skb(size, GFP_KERNEL);
454 if (rtnetlink_fill_ifinfo(skb, dev, type, 0, 0, change) < 0) {
458 NETLINK_CB(skb).dst_groups = RTMGRP_LINK;
459 netlink_broadcast(rtnl, skb, 0, RTMGRP_LINK, GFP_KERNEL);
462 static int rtnetlink_done(struct netlink_callback *cb)
467 /* Protected by RTNL sempahore. */
468 static struct rtattr **rta_buf;
469 static int rtattr_max;
471 /* Process one rtnetlink message. */
473 static __inline__ int
474 rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh, int *errp)
476 struct rtnetlink_link *link;
477 struct rtnetlink_link *link_tab;
484 /* Only requests are handled by kernel now */
485 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
488 type = nlh->nlmsg_type;
490 /* A control message: ignore them */
494 /* Unknown message: reply with EINVAL */
500 /* All the messages must have at least 1 byte length */
501 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
504 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
505 if (family >= NPROTO) {
506 *errp = -EAFNOSUPPORT;
510 link_tab = rtnetlink_links[family];
511 if (link_tab == NULL)
512 link_tab = rtnetlink_links[PF_UNSPEC];
513 link = &link_tab[type];
518 if (kind != 2 && security_netlink_recv(skb)) {
523 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
526 if (link->dumpit == NULL)
527 link = &(rtnetlink_links[PF_UNSPEC][type]);
529 if (link->dumpit == NULL)
532 if ((*errp = netlink_dump_start(rtnl, skb, nlh,
534 rtnetlink_done)) != 0) {
537 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
544 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
546 min_len = rtm_min[sz_idx];
547 if (nlh->nlmsg_len < min_len)
550 if (nlh->nlmsg_len > min_len) {
551 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
552 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
554 while (RTA_OK(attr, attrlen)) {
555 unsigned flavor = attr->rta_type;
557 if (flavor > rta_max[sz_idx])
559 rta_buf[flavor-1] = attr;
561 attr = RTA_NEXT(attr, attrlen);
565 if (link->doit == NULL)
566 link = &(rtnetlink_links[PF_UNSPEC][type]);
567 if (link->doit == NULL)
569 err = link->doit(skb, nlh, (void *)&rta_buf[0]);
580 * Process one packet of messages.
581 * Malformed skbs with wrong lengths of messages are discarded silently.
584 static inline int rtnetlink_rcv_skb(struct sk_buff *skb)
587 struct nlmsghdr * nlh;
589 while (skb->len >= NLMSG_SPACE(0)) {
592 nlh = (struct nlmsghdr *)skb->data;
593 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
595 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
598 if (rtnetlink_rcv_msg(skb, nlh, &err)) {
599 /* Not error, but we must interrupt processing here:
600 * Note, that in this case we do not pull message
601 * from skb, it will be processed later.
605 netlink_ack(skb, nlh, err);
606 } else if (nlh->nlmsg_flags&NLM_F_ACK)
607 netlink_ack(skb, nlh, 0);
615 * rtnetlink input queue processing routine:
616 * - try to acquire shared lock. If it is failed, defer processing.
617 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
618 * that will occur, when a dump started and/or acquisition of
619 * exclusive lock failed.
622 static void rtnetlink_rcv(struct sock *sk, int len)
627 if (rtnl_shlock_nowait())
630 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
631 if (rtnetlink_rcv_skb(skb)) {
633 skb_queue_head(&sk->sk_receive_queue,
645 } while (rtnl && rtnl->sk_receive_queue.qlen);
648 static struct rtnetlink_link link_rtnetlink_table[RTM_MAX-RTM_BASE+1] =
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 }
659 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
661 struct net_device *dev = ptr;
663 case NETDEV_UNREGISTER:
664 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
666 case NETDEV_REGISTER:
667 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
671 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
674 case NETDEV_GOING_DOWN:
677 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
683 static struct notifier_block rtnetlink_dev_notifier = {
684 .notifier_call = rtnetlink_event,
687 void __init rtnetlink_init(void)
692 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
693 if (rta_max[i] > rtattr_max)
694 rtattr_max = rta_max[i];
695 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
697 panic("rtnetlink_init: cannot allocate rta_buf\n");
699 rtnl = netlink_kernel_create(NETLINK_ROUTE, rtnetlink_rcv);
701 panic("rtnetlink_init: cannot initialize rtnetlink\n");
702 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
703 register_netdevice_notifier(&rtnetlink_dev_notifier);
704 rtnetlink_links[PF_UNSPEC] = link_rtnetlink_table;
705 rtnetlink_links[PF_PACKET] = link_rtnetlink_table;
708 EXPORT_SYMBOL(__rta_fill);
709 EXPORT_SYMBOL(rtattr_strlcpy);
710 EXPORT_SYMBOL(rtattr_parse);
711 EXPORT_SYMBOL(rtnetlink_links);
712 EXPORT_SYMBOL(rtnetlink_put_metrics);
714 EXPORT_SYMBOL(rtnl_lock);
715 EXPORT_SYMBOL(rtnl_lock_interruptible);
716 EXPORT_SYMBOL(rtnl_sem);
717 EXPORT_SYMBOL(rtnl_unlock);