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/errno.h>
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
30 #include <linux/slab.h>
31 #include <linux/interrupt.h>
32 #include <linux/capability.h>
33 #include <linux/skbuff.h>
34 #include <linux/init.h>
35 #include <linux/security.h>
36 #include <linux/mutex.h>
37 #include <linux/if_addr.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>
52 #include <net/fib_rules.h>
53 #include <net/rtnetlink.h>
54 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
55 #include <linux/wireless.h>
56 #include <net/iw_handler.h>
57 #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
62 rtnl_dumpit_func dumpit;
65 static DEFINE_MUTEX(rtnl_mutex);
66 static struct sock *rtnl;
70 mutex_lock(&rtnl_mutex);
73 void __rtnl_unlock(void)
75 mutex_unlock(&rtnl_mutex);
78 void rtnl_unlock(void)
80 mutex_unlock(&rtnl_mutex);
81 if (rtnl && rtnl->sk_receive_queue.qlen)
82 rtnl->sk_data_ready(rtnl, 0);
86 int rtnl_trylock(void)
88 return mutex_trylock(&rtnl_mutex);
91 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
93 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
95 while (RTA_OK(rta, len)) {
96 unsigned flavor = rta->rta_type;
97 if (flavor && flavor <= maxattr)
99 rta = RTA_NEXT(rta, len);
104 struct rtnl_link *rtnl_msg_handlers[NPROTO];
106 static inline int rtm_msgindex(int msgtype)
108 int msgindex = msgtype - RTM_BASE;
111 * msgindex < 0 implies someone tried to register a netlink
112 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
113 * the message type has not been added to linux/rtnetlink.h
115 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
120 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
122 struct rtnl_link *tab;
124 tab = rtnl_msg_handlers[protocol];
125 if (tab == NULL || tab[msgindex].doit == NULL)
126 tab = rtnl_msg_handlers[PF_UNSPEC];
128 return tab ? tab[msgindex].doit : NULL;
131 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
133 struct rtnl_link *tab;
135 tab = rtnl_msg_handlers[protocol];
136 if (tab == NULL || tab[msgindex].dumpit == NULL)
137 tab = rtnl_msg_handlers[PF_UNSPEC];
139 return tab ? tab[msgindex].dumpit : NULL;
143 * __rtnl_register - Register a rtnetlink message type
144 * @protocol: Protocol family or PF_UNSPEC
145 * @msgtype: rtnetlink message type
146 * @doit: Function pointer called for each request message
147 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
149 * Registers the specified function pointers (at least one of them has
150 * to be non-NULL) to be called whenever a request message for the
151 * specified protocol family and message type is received.
153 * The special protocol family PF_UNSPEC may be used to define fallback
154 * function pointers for the case when no entry for the specific protocol
157 * Returns 0 on success or a negative error code.
159 int __rtnl_register(int protocol, int msgtype,
160 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
162 struct rtnl_link *tab;
165 BUG_ON(protocol < 0 || protocol >= NPROTO);
166 msgindex = rtm_msgindex(msgtype);
168 tab = rtnl_msg_handlers[protocol];
170 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
174 rtnl_msg_handlers[protocol] = tab;
178 tab[msgindex].doit = doit;
181 tab[msgindex].dumpit = dumpit;
186 EXPORT_SYMBOL_GPL(__rtnl_register);
189 * rtnl_register - Register a rtnetlink message type
191 * Identical to __rtnl_register() but panics on failure. This is useful
192 * as failure of this function is very unlikely, it can only happen due
193 * to lack of memory when allocating the chain to store all message
194 * handlers for a protocol. Meant for use in init functions where lack
195 * of memory implies no sense in continueing.
197 void rtnl_register(int protocol, int msgtype,
198 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
200 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
201 panic("Unable to register rtnetlink message handler, "
202 "protocol = %d, message type = %d\n",
206 EXPORT_SYMBOL_GPL(rtnl_register);
209 * rtnl_unregister - Unregister a rtnetlink message type
210 * @protocol: Protocol family or PF_UNSPEC
211 * @msgtype: rtnetlink message type
213 * Returns 0 on success or a negative error code.
215 int rtnl_unregister(int protocol, int msgtype)
219 BUG_ON(protocol < 0 || protocol >= NPROTO);
220 msgindex = rtm_msgindex(msgtype);
222 if (rtnl_msg_handlers[protocol] == NULL)
225 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
226 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
231 EXPORT_SYMBOL_GPL(rtnl_unregister);
234 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
235 * @protocol : Protocol family or PF_UNSPEC
237 * Identical to calling rtnl_unregster() for all registered message types
238 * of a certain protocol family.
240 void rtnl_unregister_all(int protocol)
242 BUG_ON(protocol < 0 || protocol >= NPROTO);
244 kfree(rtnl_msg_handlers[protocol]);
245 rtnl_msg_handlers[protocol] = NULL;
248 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
250 static const int rtm_min[RTM_NR_FAMILIES] =
252 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
253 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
254 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
255 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
256 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
257 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
258 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
259 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
260 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
261 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
264 static const int rta_max[RTM_NR_FAMILIES] =
266 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
267 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
268 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
269 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
270 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
271 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
272 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
273 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
276 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
279 int size = RTA_LENGTH(attrlen);
281 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
282 rta->rta_type = attrtype;
284 memcpy(RTA_DATA(rta), data, attrlen);
285 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
288 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
290 size_t ret = RTA_PAYLOAD(rta);
291 char *src = RTA_DATA(rta);
293 if (ret > 0 && src[ret - 1] == '\0')
296 size_t len = (ret >= size) ? size - 1 : ret;
297 memset(dest, 0, size);
298 memcpy(dest, src, len);
303 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
307 NETLINK_CB(skb).dst_group = group;
309 atomic_inc(&skb->users);
310 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
312 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
316 int rtnl_unicast(struct sk_buff *skb, u32 pid)
318 return nlmsg_unicast(rtnl, skb, pid);
321 int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
322 struct nlmsghdr *nlh, gfp_t flags)
327 report = nlmsg_report(nlh);
329 return nlmsg_notify(rtnl, skb, pid, group, report, flags);
332 void rtnl_set_sk_err(u32 group, int error)
334 netlink_set_err(rtnl, 0, group, error);
337 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
342 mx = nla_nest_start(skb, RTA_METRICS);
346 for (i = 0; i < RTAX_MAX; i++) {
349 NLA_PUT_U32(skb, i+1, metrics[i]);
354 nla_nest_cancel(skb, mx);
358 return nla_nest_end(skb, mx);
361 return nla_nest_cancel(skb, mx);
364 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
365 u32 ts, u32 tsage, long expires, u32 error)
367 struct rta_cacheinfo ci = {
368 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
369 .rta_used = dst->__use,
370 .rta_clntref = atomic_read(&(dst->__refcnt)),
378 ci.rta_expires = jiffies_to_clock_t(expires);
380 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
383 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
385 static void set_operstate(struct net_device *dev, unsigned char transition)
387 unsigned char operstate = dev->operstate;
391 if ((operstate == IF_OPER_DORMANT ||
392 operstate == IF_OPER_UNKNOWN) &&
394 operstate = IF_OPER_UP;
397 case IF_OPER_DORMANT:
398 if (operstate == IF_OPER_UP ||
399 operstate == IF_OPER_UNKNOWN)
400 operstate = IF_OPER_DORMANT;
404 if (dev->operstate != operstate) {
405 write_lock_bh(&dev_base_lock);
406 dev->operstate = operstate;
407 write_unlock_bh(&dev_base_lock);
408 netdev_state_change(dev);
412 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
413 struct net_device_stats *b)
415 a->rx_packets = b->rx_packets;
416 a->tx_packets = b->tx_packets;
417 a->rx_bytes = b->rx_bytes;
418 a->tx_bytes = b->tx_bytes;
419 a->rx_errors = b->rx_errors;
420 a->tx_errors = b->tx_errors;
421 a->rx_dropped = b->rx_dropped;
422 a->tx_dropped = b->tx_dropped;
424 a->multicast = b->multicast;
425 a->collisions = b->collisions;
427 a->rx_length_errors = b->rx_length_errors;
428 a->rx_over_errors = b->rx_over_errors;
429 a->rx_crc_errors = b->rx_crc_errors;
430 a->rx_frame_errors = b->rx_frame_errors;
431 a->rx_fifo_errors = b->rx_fifo_errors;
432 a->rx_missed_errors = b->rx_missed_errors;
434 a->tx_aborted_errors = b->tx_aborted_errors;
435 a->tx_carrier_errors = b->tx_carrier_errors;
436 a->tx_fifo_errors = b->tx_fifo_errors;
437 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
438 a->tx_window_errors = b->tx_window_errors;
440 a->rx_compressed = b->rx_compressed;
441 a->tx_compressed = b->tx_compressed;
444 static inline size_t if_nlmsg_size(int iwbuflen)
446 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
447 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
448 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
449 + nla_total_size(sizeof(struct rtnl_link_ifmap))
450 + nla_total_size(sizeof(struct rtnl_link_stats))
451 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
452 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
453 + nla_total_size(4) /* IFLA_TXQLEN */
454 + nla_total_size(4) /* IFLA_WEIGHT */
455 + nla_total_size(4) /* IFLA_MTU */
456 + nla_total_size(4) /* IFLA_LINK */
457 + nla_total_size(4) /* IFLA_MASTER */
458 + nla_total_size(1) /* IFLA_OPERSTATE */
459 + nla_total_size(1) /* IFLA_LINKMODE */
460 + nla_total_size(iwbuflen);
463 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
464 void *iwbuf, int iwbuflen, int type, u32 pid,
465 u32 seq, u32 change, unsigned int flags)
467 struct ifinfomsg *ifm;
468 struct nlmsghdr *nlh;
470 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
474 ifm = nlmsg_data(nlh);
475 ifm->ifi_family = AF_UNSPEC;
477 ifm->ifi_type = dev->type;
478 ifm->ifi_index = dev->ifindex;
479 ifm->ifi_flags = dev_get_flags(dev);
480 ifm->ifi_change = change;
482 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
483 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
484 NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
485 NLA_PUT_U8(skb, IFLA_OPERSTATE,
486 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
487 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
488 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
490 if (dev->ifindex != dev->iflink)
491 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
494 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
496 if (dev->qdisc_sleeping)
497 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
500 struct rtnl_link_ifmap map = {
501 .mem_start = dev->mem_start,
502 .mem_end = dev->mem_end,
503 .base_addr = dev->base_addr,
506 .port = dev->if_port,
508 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
512 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
513 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
516 if (dev->get_stats) {
517 struct net_device_stats *stats = dev->get_stats(dev);
521 attr = nla_reserve(skb, IFLA_STATS,
522 sizeof(struct rtnl_link_stats));
524 goto nla_put_failure;
526 copy_rtnl_link_stats(nla_data(attr), stats);
531 NLA_PUT(skb, IFLA_WIRELESS, iwbuflen, iwbuf);
533 return nlmsg_end(skb, nlh);
536 nlmsg_cancel(skb, nlh);
540 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
543 int s_idx = cb->args[0];
544 struct net_device *dev;
546 for (dev=dev_base, idx=0; dev; dev = dev->next, idx++) {
549 if (rtnl_fill_ifinfo(skb, dev, NULL, 0, RTM_NEWLINK,
550 NETLINK_CB(cb->skb).pid,
551 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
559 static struct nla_policy ifla_policy[IFLA_MAX+1] __read_mostly = {
560 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
561 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
562 [IFLA_MTU] = { .type = NLA_U32 },
563 [IFLA_TXQLEN] = { .type = NLA_U32 },
564 [IFLA_WEIGHT] = { .type = NLA_U32 },
565 [IFLA_OPERSTATE] = { .type = NLA_U8 },
566 [IFLA_LINKMODE] = { .type = NLA_U8 },
569 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
571 struct ifinfomsg *ifm;
572 struct net_device *dev;
573 int err, send_addr_notify = 0, modified = 0;
574 struct nlattr *tb[IFLA_MAX+1];
575 char ifname[IFNAMSIZ];
577 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
582 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
587 ifm = nlmsg_data(nlh);
588 if (ifm->ifi_index >= 0)
589 dev = dev_get_by_index(ifm->ifi_index);
590 else if (tb[IFLA_IFNAME])
591 dev = dev_get_by_name(ifname);
600 if (tb[IFLA_ADDRESS] &&
601 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
604 if (tb[IFLA_BROADCAST] &&
605 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
609 struct rtnl_link_ifmap *u_map;
612 if (!dev->set_config) {
617 if (!netif_device_present(dev)) {
622 u_map = nla_data(tb[IFLA_MAP]);
623 k_map.mem_start = (unsigned long) u_map->mem_start;
624 k_map.mem_end = (unsigned long) u_map->mem_end;
625 k_map.base_addr = (unsigned short) u_map->base_addr;
626 k_map.irq = (unsigned char) u_map->irq;
627 k_map.dma = (unsigned char) u_map->dma;
628 k_map.port = (unsigned char) u_map->port;
630 err = dev->set_config(dev, &k_map);
637 if (tb[IFLA_ADDRESS]) {
641 if (!dev->set_mac_address) {
646 if (!netif_device_present(dev)) {
651 len = sizeof(sa_family_t) + dev->addr_len;
652 sa = kmalloc(len, GFP_KERNEL);
657 sa->sa_family = dev->type;
658 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
660 err = dev->set_mac_address(dev, sa);
664 send_addr_notify = 1;
669 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
676 * Interface selected by interface index but interface
677 * name provided implies that a name change has been
680 if (ifm->ifi_index >= 0 && ifname[0]) {
681 err = dev_change_name(dev, ifname);
687 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
688 if (tb[IFLA_WIRELESS]) {
689 /* Call Wireless Extensions.
690 * Various stuff checked in there... */
691 err = wireless_rtnetlink_set(dev, nla_data(tb[IFLA_WIRELESS]),
692 nla_len(tb[IFLA_WIRELESS]));
696 #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
698 if (tb[IFLA_BROADCAST]) {
699 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
700 send_addr_notify = 1;
705 dev_change_flags(dev, ifm->ifi_flags);
708 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
711 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
713 if (tb[IFLA_OPERSTATE])
714 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
716 if (tb[IFLA_LINKMODE]) {
717 write_lock_bh(&dev_base_lock);
718 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
719 write_unlock_bh(&dev_base_lock);
725 if (err < 0 && modified && net_ratelimit())
726 printk(KERN_WARNING "A link change request failed with "
727 "some changes comitted already. Interface %s may "
728 "have been left with an inconsistent configuration, "
729 "please check.\n", dev->name);
731 if (send_addr_notify)
732 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
739 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
741 struct ifinfomsg *ifm;
742 struct nlattr *tb[IFLA_MAX+1];
743 struct net_device *dev = NULL;
744 struct sk_buff *nskb;
745 char *iw_buf = NULL, *iw = NULL;
749 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
753 ifm = nlmsg_data(nlh);
754 if (ifm->ifi_index >= 0) {
755 dev = dev_get_by_index(ifm->ifi_index);
762 #ifdef CONFIG_NET_WIRELESS_RTNETLINK
763 if (tb[IFLA_WIRELESS]) {
764 /* Call Wireless Extensions. We need to know the size before
765 * we can alloc. Various stuff checked in there... */
766 err = wireless_rtnetlink_get(dev, nla_data(tb[IFLA_WIRELESS]),
767 nla_len(tb[IFLA_WIRELESS]),
768 &iw_buf, &iw_buf_len);
772 /* Payload is at an offset in buffer */
773 iw = iw_buf + IW_EV_POINT_OFF;
775 #endif /* CONFIG_NET_WIRELESS_RTNETLINK */
777 nskb = nlmsg_new(if_nlmsg_size(iw_buf_len), GFP_KERNEL);
783 err = rtnl_fill_ifinfo(nskb, dev, iw, iw_buf_len, RTM_NEWLINK,
784 NETLINK_CB(skb).pid, nlh->nlmsg_seq, 0, 0);
786 /* -EMSGSIZE implies BUG in if_nlmsg_size */
787 WARN_ON(err == -EMSGSIZE);
791 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
799 int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
802 int s_idx = cb->family;
806 for (idx=1; idx<NPROTO; idx++) {
807 int type = cb->nlh->nlmsg_type-RTM_BASE;
808 if (idx < s_idx || idx == PF_PACKET)
810 if (rtnl_msg_handlers[idx] == NULL ||
811 rtnl_msg_handlers[idx][type].dumpit == NULL)
814 memset(&cb->args[0], 0, sizeof(cb->args));
815 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
823 EXPORT_SYMBOL_GPL(rtnl_dump_all);
825 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
830 skb = nlmsg_new(if_nlmsg_size(0), GFP_KERNEL);
834 err = rtnl_fill_ifinfo(skb, dev, NULL, 0, type, 0, 0, change, 0);
836 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
837 WARN_ON(err == -EMSGSIZE);
841 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
844 rtnl_set_sk_err(RTNLGRP_LINK, err);
847 /* Protected by RTNL sempahore. */
848 static struct rtattr **rta_buf;
849 static int rtattr_max;
851 /* Process one rtnetlink message. */
853 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
862 type = nlh->nlmsg_type;
868 /* All the messages must have at least 1 byte length */
869 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
872 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
873 if (family >= NPROTO)
874 return -EAFNOSUPPORT;
879 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
882 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
883 rtnl_dumpit_func dumpit;
885 dumpit = rtnl_get_dumpit(family, type);
890 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
895 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
897 min_len = rtm_min[sz_idx];
898 if (nlh->nlmsg_len < min_len)
901 if (nlh->nlmsg_len > min_len) {
902 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
903 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
905 while (RTA_OK(attr, attrlen)) {
906 unsigned flavor = attr->rta_type;
908 if (flavor > rta_max[sz_idx])
910 rta_buf[flavor-1] = attr;
912 attr = RTA_NEXT(attr, attrlen);
916 doit = rtnl_get_doit(family, type);
920 return doit(skb, nlh, (void *)&rta_buf[0]);
923 static void rtnetlink_rcv(struct sock *sk, int len)
925 unsigned int qlen = 0;
928 mutex_lock(&rtnl_mutex);
929 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
930 mutex_unlock(&rtnl_mutex);
936 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
938 struct net_device *dev = ptr;
940 case NETDEV_UNREGISTER:
941 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
943 case NETDEV_REGISTER:
944 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
948 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
951 case NETDEV_GOING_DOWN:
954 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
960 static struct notifier_block rtnetlink_dev_notifier = {
961 .notifier_call = rtnetlink_event,
964 void __init rtnetlink_init(void)
969 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
970 if (rta_max[i] > rtattr_max)
971 rtattr_max = rta_max[i];
972 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
974 panic("rtnetlink_init: cannot allocate rta_buf\n");
976 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
977 &rtnl_mutex, THIS_MODULE);
979 panic("rtnetlink_init: cannot initialize rtnetlink\n");
980 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
981 register_netdevice_notifier(&rtnetlink_dev_notifier);
983 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
984 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
986 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
987 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
990 EXPORT_SYMBOL(__rta_fill);
991 EXPORT_SYMBOL(rtattr_strlcpy);
992 EXPORT_SYMBOL(rtattr_parse);
993 EXPORT_SYMBOL(rtnetlink_put_metrics);
994 EXPORT_SYMBOL(rtnl_lock);
995 EXPORT_SYMBOL(rtnl_trylock);
996 EXPORT_SYMBOL(rtnl_unlock);
997 EXPORT_SYMBOL(rtnl_unicast);
998 EXPORT_SYMBOL(rtnl_notify);
999 EXPORT_SYMBOL(rtnl_set_sk_err);