sched: move the __update_rq_clock() call to scheduler_tick()
[usb.git] / net / core / rtnetlink.c
blob4756d5857abfb9010a55610423f967d129ae45f4
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/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>
29 #include <linux/mm.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>
45 #include <net/ip.h>
46 #include <net/protocol.h>
47 #include <net/arp.h>
48 #include <net/route.h>
49 #include <net/udp.h>
50 #include <net/sock.h>
51 #include <net/pkt_sched.h>
52 #include <net/fib_rules.h>
53 #include <net/rtnetlink.h>
55 struct rtnl_link
57 rtnl_doit_func doit;
58 rtnl_dumpit_func dumpit;
61 static DEFINE_MUTEX(rtnl_mutex);
62 static struct sock *rtnl;
64 void rtnl_lock(void)
66 mutex_lock(&rtnl_mutex);
69 void __rtnl_unlock(void)
71 mutex_unlock(&rtnl_mutex);
74 void rtnl_unlock(void)
76 mutex_unlock(&rtnl_mutex);
77 if (rtnl && rtnl->sk_receive_queue.qlen)
78 rtnl->sk_data_ready(rtnl, 0);
79 netdev_run_todo();
82 int rtnl_trylock(void)
84 return mutex_trylock(&rtnl_mutex);
87 int rtattr_parse(struct rtattr *tb[], int maxattr, struct rtattr *rta, int len)
89 memset(tb, 0, sizeof(struct rtattr*)*maxattr);
91 while (RTA_OK(rta, len)) {
92 unsigned flavor = rta->rta_type;
93 if (flavor && flavor <= maxattr)
94 tb[flavor-1] = rta;
95 rta = RTA_NEXT(rta, len);
97 return 0;
100 int __rtattr_parse_nested_compat(struct rtattr *tb[], int maxattr,
101 struct rtattr *rta, int len)
103 if (RTA_PAYLOAD(rta) < len)
104 return -1;
105 if (RTA_PAYLOAD(rta) >= RTA_ALIGN(len) + sizeof(struct rtattr)) {
106 rta = RTA_DATA(rta) + RTA_ALIGN(len);
107 return rtattr_parse_nested(tb, maxattr, rta);
109 memset(tb, 0, sizeof(struct rtattr *) * maxattr);
110 return 0;
113 static struct rtnl_link *rtnl_msg_handlers[NPROTO];
115 static inline int rtm_msgindex(int msgtype)
117 int msgindex = msgtype - RTM_BASE;
120 * msgindex < 0 implies someone tried to register a netlink
121 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
122 * the message type has not been added to linux/rtnetlink.h
124 BUG_ON(msgindex < 0 || msgindex >= RTM_NR_MSGTYPES);
126 return msgindex;
129 static rtnl_doit_func rtnl_get_doit(int protocol, int msgindex)
131 struct rtnl_link *tab;
133 tab = rtnl_msg_handlers[protocol];
134 if (tab == NULL || tab[msgindex].doit == NULL)
135 tab = rtnl_msg_handlers[PF_UNSPEC];
137 return tab ? tab[msgindex].doit : NULL;
140 static rtnl_dumpit_func rtnl_get_dumpit(int protocol, int msgindex)
142 struct rtnl_link *tab;
144 tab = rtnl_msg_handlers[protocol];
145 if (tab == NULL || tab[msgindex].dumpit == NULL)
146 tab = rtnl_msg_handlers[PF_UNSPEC];
148 return tab ? tab[msgindex].dumpit : NULL;
152 * __rtnl_register - Register a rtnetlink message type
153 * @protocol: Protocol family or PF_UNSPEC
154 * @msgtype: rtnetlink message type
155 * @doit: Function pointer called for each request message
156 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
158 * Registers the specified function pointers (at least one of them has
159 * to be non-NULL) to be called whenever a request message for the
160 * specified protocol family and message type is received.
162 * The special protocol family PF_UNSPEC may be used to define fallback
163 * function pointers for the case when no entry for the specific protocol
164 * family exists.
166 * Returns 0 on success or a negative error code.
168 int __rtnl_register(int protocol, int msgtype,
169 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
171 struct rtnl_link *tab;
172 int msgindex;
174 BUG_ON(protocol < 0 || protocol >= NPROTO);
175 msgindex = rtm_msgindex(msgtype);
177 tab = rtnl_msg_handlers[protocol];
178 if (tab == NULL) {
179 tab = kcalloc(RTM_NR_MSGTYPES, sizeof(*tab), GFP_KERNEL);
180 if (tab == NULL)
181 return -ENOBUFS;
183 rtnl_msg_handlers[protocol] = tab;
186 if (doit)
187 tab[msgindex].doit = doit;
189 if (dumpit)
190 tab[msgindex].dumpit = dumpit;
192 return 0;
195 EXPORT_SYMBOL_GPL(__rtnl_register);
198 * rtnl_register - Register a rtnetlink message type
200 * Identical to __rtnl_register() but panics on failure. This is useful
201 * as failure of this function is very unlikely, it can only happen due
202 * to lack of memory when allocating the chain to store all message
203 * handlers for a protocol. Meant for use in init functions where lack
204 * of memory implies no sense in continueing.
206 void rtnl_register(int protocol, int msgtype,
207 rtnl_doit_func doit, rtnl_dumpit_func dumpit)
209 if (__rtnl_register(protocol, msgtype, doit, dumpit) < 0)
210 panic("Unable to register rtnetlink message handler, "
211 "protocol = %d, message type = %d\n",
212 protocol, msgtype);
215 EXPORT_SYMBOL_GPL(rtnl_register);
218 * rtnl_unregister - Unregister a rtnetlink message type
219 * @protocol: Protocol family or PF_UNSPEC
220 * @msgtype: rtnetlink message type
222 * Returns 0 on success or a negative error code.
224 int rtnl_unregister(int protocol, int msgtype)
226 int msgindex;
228 BUG_ON(protocol < 0 || protocol >= NPROTO);
229 msgindex = rtm_msgindex(msgtype);
231 if (rtnl_msg_handlers[protocol] == NULL)
232 return -ENOENT;
234 rtnl_msg_handlers[protocol][msgindex].doit = NULL;
235 rtnl_msg_handlers[protocol][msgindex].dumpit = NULL;
237 return 0;
240 EXPORT_SYMBOL_GPL(rtnl_unregister);
243 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
244 * @protocol : Protocol family or PF_UNSPEC
246 * Identical to calling rtnl_unregster() for all registered message types
247 * of a certain protocol family.
249 void rtnl_unregister_all(int protocol)
251 BUG_ON(protocol < 0 || protocol >= NPROTO);
253 kfree(rtnl_msg_handlers[protocol]);
254 rtnl_msg_handlers[protocol] = NULL;
257 EXPORT_SYMBOL_GPL(rtnl_unregister_all);
259 static LIST_HEAD(link_ops);
262 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
263 * @ops: struct rtnl_link_ops * to register
265 * The caller must hold the rtnl_mutex. This function should be used
266 * by drivers that create devices during module initialization. It
267 * must be called before registering the devices.
269 * Returns 0 on success or a negative error code.
271 int __rtnl_link_register(struct rtnl_link_ops *ops)
273 if (!ops->dellink)
274 ops->dellink = unregister_netdevice;
276 list_add_tail(&ops->list, &link_ops);
277 return 0;
280 EXPORT_SYMBOL_GPL(__rtnl_link_register);
283 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
284 * @ops: struct rtnl_link_ops * to register
286 * Returns 0 on success or a negative error code.
288 int rtnl_link_register(struct rtnl_link_ops *ops)
290 int err;
292 rtnl_lock();
293 err = __rtnl_link_register(ops);
294 rtnl_unlock();
295 return err;
298 EXPORT_SYMBOL_GPL(rtnl_link_register);
301 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
302 * @ops: struct rtnl_link_ops * to unregister
304 * The caller must hold the rtnl_mutex.
306 void __rtnl_link_unregister(struct rtnl_link_ops *ops)
308 struct net_device *dev, *n;
310 for_each_netdev_safe(dev, n) {
311 if (dev->rtnl_link_ops == ops)
312 ops->dellink(dev);
314 list_del(&ops->list);
317 EXPORT_SYMBOL_GPL(__rtnl_link_unregister);
320 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
321 * @ops: struct rtnl_link_ops * to unregister
323 void rtnl_link_unregister(struct rtnl_link_ops *ops)
325 rtnl_lock();
326 __rtnl_link_unregister(ops);
327 rtnl_unlock();
330 EXPORT_SYMBOL_GPL(rtnl_link_unregister);
332 static const struct rtnl_link_ops *rtnl_link_ops_get(const char *kind)
334 const struct rtnl_link_ops *ops;
336 list_for_each_entry(ops, &link_ops, list) {
337 if (!strcmp(ops->kind, kind))
338 return ops;
340 return NULL;
343 static size_t rtnl_link_get_size(const struct net_device *dev)
345 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
346 size_t size;
348 if (!ops)
349 return 0;
351 size = nlmsg_total_size(sizeof(struct nlattr)) + /* IFLA_LINKINFO */
352 nlmsg_total_size(strlen(ops->kind) + 1); /* IFLA_INFO_KIND */
354 if (ops->get_size)
355 /* IFLA_INFO_DATA + nested data */
356 size += nlmsg_total_size(sizeof(struct nlattr)) +
357 ops->get_size(dev);
359 if (ops->get_xstats_size)
360 size += ops->get_xstats_size(dev); /* IFLA_INFO_XSTATS */
362 return size;
365 static int rtnl_link_fill(struct sk_buff *skb, const struct net_device *dev)
367 const struct rtnl_link_ops *ops = dev->rtnl_link_ops;
368 struct nlattr *linkinfo, *data;
369 int err = -EMSGSIZE;
371 linkinfo = nla_nest_start(skb, IFLA_LINKINFO);
372 if (linkinfo == NULL)
373 goto out;
375 if (nla_put_string(skb, IFLA_INFO_KIND, ops->kind) < 0)
376 goto err_cancel_link;
377 if (ops->fill_xstats) {
378 err = ops->fill_xstats(skb, dev);
379 if (err < 0)
380 goto err_cancel_link;
382 if (ops->fill_info) {
383 data = nla_nest_start(skb, IFLA_INFO_DATA);
384 if (data == NULL)
385 goto err_cancel_link;
386 err = ops->fill_info(skb, dev);
387 if (err < 0)
388 goto err_cancel_data;
389 nla_nest_end(skb, data);
392 nla_nest_end(skb, linkinfo);
393 return 0;
395 err_cancel_data:
396 nla_nest_cancel(skb, data);
397 err_cancel_link:
398 nla_nest_cancel(skb, linkinfo);
399 out:
400 return err;
403 static const int rtm_min[RTM_NR_FAMILIES] =
405 [RTM_FAM(RTM_NEWLINK)] = NLMSG_LENGTH(sizeof(struct ifinfomsg)),
406 [RTM_FAM(RTM_NEWADDR)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg)),
407 [RTM_FAM(RTM_NEWROUTE)] = NLMSG_LENGTH(sizeof(struct rtmsg)),
408 [RTM_FAM(RTM_NEWRULE)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr)),
409 [RTM_FAM(RTM_NEWQDISC)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
410 [RTM_FAM(RTM_NEWTCLASS)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
411 [RTM_FAM(RTM_NEWTFILTER)] = NLMSG_LENGTH(sizeof(struct tcmsg)),
412 [RTM_FAM(RTM_NEWACTION)] = NLMSG_LENGTH(sizeof(struct tcamsg)),
413 [RTM_FAM(RTM_GETMULTICAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
414 [RTM_FAM(RTM_GETANYCAST)] = NLMSG_LENGTH(sizeof(struct rtgenmsg)),
417 static const int rta_max[RTM_NR_FAMILIES] =
419 [RTM_FAM(RTM_NEWLINK)] = IFLA_MAX,
420 [RTM_FAM(RTM_NEWADDR)] = IFA_MAX,
421 [RTM_FAM(RTM_NEWROUTE)] = RTA_MAX,
422 [RTM_FAM(RTM_NEWRULE)] = FRA_MAX,
423 [RTM_FAM(RTM_NEWQDISC)] = TCA_MAX,
424 [RTM_FAM(RTM_NEWTCLASS)] = TCA_MAX,
425 [RTM_FAM(RTM_NEWTFILTER)] = TCA_MAX,
426 [RTM_FAM(RTM_NEWACTION)] = TCAA_MAX,
429 void __rta_fill(struct sk_buff *skb, int attrtype, int attrlen, const void *data)
431 struct rtattr *rta;
432 int size = RTA_LENGTH(attrlen);
434 rta = (struct rtattr*)skb_put(skb, RTA_ALIGN(size));
435 rta->rta_type = attrtype;
436 rta->rta_len = size;
437 memcpy(RTA_DATA(rta), data, attrlen);
438 memset(RTA_DATA(rta) + attrlen, 0, RTA_ALIGN(size) - size);
441 size_t rtattr_strlcpy(char *dest, const struct rtattr *rta, size_t size)
443 size_t ret = RTA_PAYLOAD(rta);
444 char *src = RTA_DATA(rta);
446 if (ret > 0 && src[ret - 1] == '\0')
447 ret--;
448 if (size > 0) {
449 size_t len = (ret >= size) ? size - 1 : ret;
450 memset(dest, 0, size);
451 memcpy(dest, src, len);
453 return ret;
456 int rtnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
458 int err = 0;
460 NETLINK_CB(skb).dst_group = group;
461 if (echo)
462 atomic_inc(&skb->users);
463 netlink_broadcast(rtnl, skb, pid, group, GFP_KERNEL);
464 if (echo)
465 err = netlink_unicast(rtnl, skb, pid, MSG_DONTWAIT);
466 return err;
469 int rtnl_unicast(struct sk_buff *skb, u32 pid)
471 return nlmsg_unicast(rtnl, skb, pid);
474 int rtnl_notify(struct sk_buff *skb, u32 pid, u32 group,
475 struct nlmsghdr *nlh, gfp_t flags)
477 int report = 0;
479 if (nlh)
480 report = nlmsg_report(nlh);
482 return nlmsg_notify(rtnl, skb, pid, group, report, flags);
485 void rtnl_set_sk_err(u32 group, int error)
487 netlink_set_err(rtnl, 0, group, error);
490 int rtnetlink_put_metrics(struct sk_buff *skb, u32 *metrics)
492 struct nlattr *mx;
493 int i, valid = 0;
495 mx = nla_nest_start(skb, RTA_METRICS);
496 if (mx == NULL)
497 return -ENOBUFS;
499 for (i = 0; i < RTAX_MAX; i++) {
500 if (metrics[i]) {
501 valid++;
502 NLA_PUT_U32(skb, i+1, metrics[i]);
506 if (!valid) {
507 nla_nest_cancel(skb, mx);
508 return 0;
511 return nla_nest_end(skb, mx);
513 nla_put_failure:
514 return nla_nest_cancel(skb, mx);
517 int rtnl_put_cacheinfo(struct sk_buff *skb, struct dst_entry *dst, u32 id,
518 u32 ts, u32 tsage, long expires, u32 error)
520 struct rta_cacheinfo ci = {
521 .rta_lastuse = jiffies_to_clock_t(jiffies - dst->lastuse),
522 .rta_used = dst->__use,
523 .rta_clntref = atomic_read(&(dst->__refcnt)),
524 .rta_error = error,
525 .rta_id = id,
526 .rta_ts = ts,
527 .rta_tsage = tsage,
530 if (expires)
531 ci.rta_expires = jiffies_to_clock_t(expires);
533 return nla_put(skb, RTA_CACHEINFO, sizeof(ci), &ci);
536 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo);
538 static void set_operstate(struct net_device *dev, unsigned char transition)
540 unsigned char operstate = dev->operstate;
542 switch(transition) {
543 case IF_OPER_UP:
544 if ((operstate == IF_OPER_DORMANT ||
545 operstate == IF_OPER_UNKNOWN) &&
546 !netif_dormant(dev))
547 operstate = IF_OPER_UP;
548 break;
550 case IF_OPER_DORMANT:
551 if (operstate == IF_OPER_UP ||
552 operstate == IF_OPER_UNKNOWN)
553 operstate = IF_OPER_DORMANT;
554 break;
557 if (dev->operstate != operstate) {
558 write_lock_bh(&dev_base_lock);
559 dev->operstate = operstate;
560 write_unlock_bh(&dev_base_lock);
561 netdev_state_change(dev);
565 static void copy_rtnl_link_stats(struct rtnl_link_stats *a,
566 struct net_device_stats *b)
568 a->rx_packets = b->rx_packets;
569 a->tx_packets = b->tx_packets;
570 a->rx_bytes = b->rx_bytes;
571 a->tx_bytes = b->tx_bytes;
572 a->rx_errors = b->rx_errors;
573 a->tx_errors = b->tx_errors;
574 a->rx_dropped = b->rx_dropped;
575 a->tx_dropped = b->tx_dropped;
577 a->multicast = b->multicast;
578 a->collisions = b->collisions;
580 a->rx_length_errors = b->rx_length_errors;
581 a->rx_over_errors = b->rx_over_errors;
582 a->rx_crc_errors = b->rx_crc_errors;
583 a->rx_frame_errors = b->rx_frame_errors;
584 a->rx_fifo_errors = b->rx_fifo_errors;
585 a->rx_missed_errors = b->rx_missed_errors;
587 a->tx_aborted_errors = b->tx_aborted_errors;
588 a->tx_carrier_errors = b->tx_carrier_errors;
589 a->tx_fifo_errors = b->tx_fifo_errors;
590 a->tx_heartbeat_errors = b->tx_heartbeat_errors;
591 a->tx_window_errors = b->tx_window_errors;
593 a->rx_compressed = b->rx_compressed;
594 a->tx_compressed = b->tx_compressed;
597 static inline size_t if_nlmsg_size(const struct net_device *dev)
599 return NLMSG_ALIGN(sizeof(struct ifinfomsg))
600 + nla_total_size(IFNAMSIZ) /* IFLA_IFNAME */
601 + nla_total_size(IFNAMSIZ) /* IFLA_QDISC */
602 + nla_total_size(sizeof(struct rtnl_link_ifmap))
603 + nla_total_size(sizeof(struct rtnl_link_stats))
604 + nla_total_size(MAX_ADDR_LEN) /* IFLA_ADDRESS */
605 + nla_total_size(MAX_ADDR_LEN) /* IFLA_BROADCAST */
606 + nla_total_size(4) /* IFLA_TXQLEN */
607 + nla_total_size(4) /* IFLA_WEIGHT */
608 + nla_total_size(4) /* IFLA_MTU */
609 + nla_total_size(4) /* IFLA_LINK */
610 + nla_total_size(4) /* IFLA_MASTER */
611 + nla_total_size(1) /* IFLA_OPERSTATE */
612 + nla_total_size(1) /* IFLA_LINKMODE */
613 + rtnl_link_get_size(dev); /* IFLA_LINKINFO */
616 static int rtnl_fill_ifinfo(struct sk_buff *skb, struct net_device *dev,
617 int type, u32 pid, u32 seq, u32 change,
618 unsigned int flags)
620 struct ifinfomsg *ifm;
621 struct nlmsghdr *nlh;
623 nlh = nlmsg_put(skb, pid, seq, type, sizeof(*ifm), flags);
624 if (nlh == NULL)
625 return -EMSGSIZE;
627 ifm = nlmsg_data(nlh);
628 ifm->ifi_family = AF_UNSPEC;
629 ifm->__ifi_pad = 0;
630 ifm->ifi_type = dev->type;
631 ifm->ifi_index = dev->ifindex;
632 ifm->ifi_flags = dev_get_flags(dev);
633 ifm->ifi_change = change;
635 NLA_PUT_STRING(skb, IFLA_IFNAME, dev->name);
636 NLA_PUT_U32(skb, IFLA_TXQLEN, dev->tx_queue_len);
637 NLA_PUT_U32(skb, IFLA_WEIGHT, dev->weight);
638 NLA_PUT_U8(skb, IFLA_OPERSTATE,
639 netif_running(dev) ? dev->operstate : IF_OPER_DOWN);
640 NLA_PUT_U8(skb, IFLA_LINKMODE, dev->link_mode);
641 NLA_PUT_U32(skb, IFLA_MTU, dev->mtu);
643 if (dev->ifindex != dev->iflink)
644 NLA_PUT_U32(skb, IFLA_LINK, dev->iflink);
646 if (dev->master)
647 NLA_PUT_U32(skb, IFLA_MASTER, dev->master->ifindex);
649 if (dev->qdisc_sleeping)
650 NLA_PUT_STRING(skb, IFLA_QDISC, dev->qdisc_sleeping->ops->id);
652 if (1) {
653 struct rtnl_link_ifmap map = {
654 .mem_start = dev->mem_start,
655 .mem_end = dev->mem_end,
656 .base_addr = dev->base_addr,
657 .irq = dev->irq,
658 .dma = dev->dma,
659 .port = dev->if_port,
661 NLA_PUT(skb, IFLA_MAP, sizeof(map), &map);
664 if (dev->addr_len) {
665 NLA_PUT(skb, IFLA_ADDRESS, dev->addr_len, dev->dev_addr);
666 NLA_PUT(skb, IFLA_BROADCAST, dev->addr_len, dev->broadcast);
669 if (dev->get_stats) {
670 struct net_device_stats *stats = dev->get_stats(dev);
671 if (stats) {
672 struct nlattr *attr;
674 attr = nla_reserve(skb, IFLA_STATS,
675 sizeof(struct rtnl_link_stats));
676 if (attr == NULL)
677 goto nla_put_failure;
679 copy_rtnl_link_stats(nla_data(attr), stats);
683 if (dev->rtnl_link_ops) {
684 if (rtnl_link_fill(skb, dev) < 0)
685 goto nla_put_failure;
688 return nlmsg_end(skb, nlh);
690 nla_put_failure:
691 nlmsg_cancel(skb, nlh);
692 return -EMSGSIZE;
695 static int rtnl_dump_ifinfo(struct sk_buff *skb, struct netlink_callback *cb)
697 int idx;
698 int s_idx = cb->args[0];
699 struct net_device *dev;
701 idx = 0;
702 for_each_netdev(dev) {
703 if (idx < s_idx)
704 goto cont;
705 if (rtnl_fill_ifinfo(skb, dev, RTM_NEWLINK,
706 NETLINK_CB(cb->skb).pid,
707 cb->nlh->nlmsg_seq, 0, NLM_F_MULTI) <= 0)
708 break;
709 cont:
710 idx++;
712 cb->args[0] = idx;
714 return skb->len;
717 static const struct nla_policy ifla_policy[IFLA_MAX+1] = {
718 [IFLA_IFNAME] = { .type = NLA_STRING, .len = IFNAMSIZ-1 },
719 [IFLA_ADDRESS] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
720 [IFLA_BROADCAST] = { .type = NLA_BINARY, .len = MAX_ADDR_LEN },
721 [IFLA_MAP] = { .len = sizeof(struct rtnl_link_ifmap) },
722 [IFLA_MTU] = { .type = NLA_U32 },
723 [IFLA_TXQLEN] = { .type = NLA_U32 },
724 [IFLA_WEIGHT] = { .type = NLA_U32 },
725 [IFLA_OPERSTATE] = { .type = NLA_U8 },
726 [IFLA_LINKMODE] = { .type = NLA_U8 },
729 static const struct nla_policy ifla_info_policy[IFLA_INFO_MAX+1] = {
730 [IFLA_INFO_KIND] = { .type = NLA_STRING },
731 [IFLA_INFO_DATA] = { .type = NLA_NESTED },
734 static int do_setlink(struct net_device *dev, struct ifinfomsg *ifm,
735 struct nlattr **tb, char *ifname, int modified)
737 int send_addr_notify = 0;
738 int err;
740 if (tb[IFLA_MAP]) {
741 struct rtnl_link_ifmap *u_map;
742 struct ifmap k_map;
744 if (!dev->set_config) {
745 err = -EOPNOTSUPP;
746 goto errout;
749 if (!netif_device_present(dev)) {
750 err = -ENODEV;
751 goto errout;
754 u_map = nla_data(tb[IFLA_MAP]);
755 k_map.mem_start = (unsigned long) u_map->mem_start;
756 k_map.mem_end = (unsigned long) u_map->mem_end;
757 k_map.base_addr = (unsigned short) u_map->base_addr;
758 k_map.irq = (unsigned char) u_map->irq;
759 k_map.dma = (unsigned char) u_map->dma;
760 k_map.port = (unsigned char) u_map->port;
762 err = dev->set_config(dev, &k_map);
763 if (err < 0)
764 goto errout;
766 modified = 1;
769 if (tb[IFLA_ADDRESS]) {
770 struct sockaddr *sa;
771 int len;
773 if (!dev->set_mac_address) {
774 err = -EOPNOTSUPP;
775 goto errout;
778 if (!netif_device_present(dev)) {
779 err = -ENODEV;
780 goto errout;
783 len = sizeof(sa_family_t) + dev->addr_len;
784 sa = kmalloc(len, GFP_KERNEL);
785 if (!sa) {
786 err = -ENOMEM;
787 goto errout;
789 sa->sa_family = dev->type;
790 memcpy(sa->sa_data, nla_data(tb[IFLA_ADDRESS]),
791 dev->addr_len);
792 err = dev->set_mac_address(dev, sa);
793 kfree(sa);
794 if (err)
795 goto errout;
796 send_addr_notify = 1;
797 modified = 1;
800 if (tb[IFLA_MTU]) {
801 err = dev_set_mtu(dev, nla_get_u32(tb[IFLA_MTU]));
802 if (err < 0)
803 goto errout;
804 modified = 1;
808 * Interface selected by interface index but interface
809 * name provided implies that a name change has been
810 * requested.
812 if (ifm->ifi_index > 0 && ifname[0]) {
813 err = dev_change_name(dev, ifname);
814 if (err < 0)
815 goto errout;
816 modified = 1;
819 if (tb[IFLA_BROADCAST]) {
820 nla_memcpy(dev->broadcast, tb[IFLA_BROADCAST], dev->addr_len);
821 send_addr_notify = 1;
824 if (ifm->ifi_flags || ifm->ifi_change) {
825 unsigned int flags = ifm->ifi_flags;
827 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
828 if (ifm->ifi_change)
829 flags = (flags & ifm->ifi_change) |
830 (dev->flags & ~ifm->ifi_change);
831 dev_change_flags(dev, flags);
834 if (tb[IFLA_TXQLEN])
835 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
837 if (tb[IFLA_WEIGHT])
838 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
840 if (tb[IFLA_OPERSTATE])
841 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
843 if (tb[IFLA_LINKMODE]) {
844 write_lock_bh(&dev_base_lock);
845 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
846 write_unlock_bh(&dev_base_lock);
849 err = 0;
851 errout:
852 if (err < 0 && modified && net_ratelimit())
853 printk(KERN_WARNING "A link change request failed with "
854 "some changes comitted already. Interface %s may "
855 "have been left with an inconsistent configuration, "
856 "please check.\n", dev->name);
858 if (send_addr_notify)
859 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
860 return err;
863 static int rtnl_setlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
865 struct ifinfomsg *ifm;
866 struct net_device *dev;
867 int err;
868 struct nlattr *tb[IFLA_MAX+1];
869 char ifname[IFNAMSIZ];
871 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
872 if (err < 0)
873 goto errout;
875 if (tb[IFLA_IFNAME])
876 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
877 else
878 ifname[0] = '\0';
880 err = -EINVAL;
881 ifm = nlmsg_data(nlh);
882 if (ifm->ifi_index > 0)
883 dev = dev_get_by_index(ifm->ifi_index);
884 else if (tb[IFLA_IFNAME])
885 dev = dev_get_by_name(ifname);
886 else
887 goto errout;
889 if (dev == NULL) {
890 err = -ENODEV;
891 goto errout;
894 if (tb[IFLA_ADDRESS] &&
895 nla_len(tb[IFLA_ADDRESS]) < dev->addr_len)
896 goto errout_dev;
898 if (tb[IFLA_BROADCAST] &&
899 nla_len(tb[IFLA_BROADCAST]) < dev->addr_len)
900 goto errout_dev;
902 err = do_setlink(dev, ifm, tb, ifname, 0);
903 errout_dev:
904 dev_put(dev);
905 errout:
906 return err;
909 static int rtnl_dellink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
911 const struct rtnl_link_ops *ops;
912 struct net_device *dev;
913 struct ifinfomsg *ifm;
914 char ifname[IFNAMSIZ];
915 struct nlattr *tb[IFLA_MAX+1];
916 int err;
918 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
919 if (err < 0)
920 return err;
922 if (tb[IFLA_IFNAME])
923 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
925 ifm = nlmsg_data(nlh);
926 if (ifm->ifi_index > 0)
927 dev = __dev_get_by_index(ifm->ifi_index);
928 else if (tb[IFLA_IFNAME])
929 dev = __dev_get_by_name(ifname);
930 else
931 return -EINVAL;
933 if (!dev)
934 return -ENODEV;
936 ops = dev->rtnl_link_ops;
937 if (!ops)
938 return -EOPNOTSUPP;
940 ops->dellink(dev);
941 return 0;
944 static int rtnl_newlink(struct sk_buff *skb, struct nlmsghdr *nlh, void *arg)
946 const struct rtnl_link_ops *ops;
947 struct net_device *dev;
948 struct ifinfomsg *ifm;
949 char kind[MODULE_NAME_LEN];
950 char ifname[IFNAMSIZ];
951 struct nlattr *tb[IFLA_MAX+1];
952 struct nlattr *linkinfo[IFLA_INFO_MAX+1];
953 int err;
955 #ifdef CONFIG_KMOD
956 replay:
957 #endif
958 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
959 if (err < 0)
960 return err;
962 if (tb[IFLA_IFNAME])
963 nla_strlcpy(ifname, tb[IFLA_IFNAME], IFNAMSIZ);
964 else
965 ifname[0] = '\0';
967 ifm = nlmsg_data(nlh);
968 if (ifm->ifi_index > 0)
969 dev = __dev_get_by_index(ifm->ifi_index);
970 else if (ifname[0])
971 dev = __dev_get_by_name(ifname);
972 else
973 dev = NULL;
975 if (tb[IFLA_LINKINFO]) {
976 err = nla_parse_nested(linkinfo, IFLA_INFO_MAX,
977 tb[IFLA_LINKINFO], ifla_info_policy);
978 if (err < 0)
979 return err;
980 } else
981 memset(linkinfo, 0, sizeof(linkinfo));
983 if (linkinfo[IFLA_INFO_KIND]) {
984 nla_strlcpy(kind, linkinfo[IFLA_INFO_KIND], sizeof(kind));
985 ops = rtnl_link_ops_get(kind);
986 } else {
987 kind[0] = '\0';
988 ops = NULL;
991 if (1) {
992 struct nlattr *attr[ops ? ops->maxtype + 1 : 0], **data = NULL;
994 if (ops) {
995 if (ops->maxtype && linkinfo[IFLA_INFO_DATA]) {
996 err = nla_parse_nested(attr, ops->maxtype,
997 linkinfo[IFLA_INFO_DATA],
998 ops->policy);
999 if (err < 0)
1000 return err;
1001 data = attr;
1003 if (ops->validate) {
1004 err = ops->validate(tb, data);
1005 if (err < 0)
1006 return err;
1010 if (dev) {
1011 int modified = 0;
1013 if (nlh->nlmsg_flags & NLM_F_EXCL)
1014 return -EEXIST;
1015 if (nlh->nlmsg_flags & NLM_F_REPLACE)
1016 return -EOPNOTSUPP;
1018 if (linkinfo[IFLA_INFO_DATA]) {
1019 if (!ops || ops != dev->rtnl_link_ops ||
1020 !ops->changelink)
1021 return -EOPNOTSUPP;
1023 err = ops->changelink(dev, tb, data);
1024 if (err < 0)
1025 return err;
1026 modified = 1;
1029 return do_setlink(dev, ifm, tb, ifname, modified);
1032 if (!(nlh->nlmsg_flags & NLM_F_CREATE))
1033 return -ENODEV;
1035 if (ifm->ifi_index || ifm->ifi_flags || ifm->ifi_change)
1036 return -EOPNOTSUPP;
1037 if (tb[IFLA_MAP] || tb[IFLA_MASTER] || tb[IFLA_PROTINFO])
1038 return -EOPNOTSUPP;
1040 if (!ops) {
1041 #ifdef CONFIG_KMOD
1042 if (kind[0]) {
1043 __rtnl_unlock();
1044 request_module("rtnl-link-%s", kind);
1045 rtnl_lock();
1046 ops = rtnl_link_ops_get(kind);
1047 if (ops)
1048 goto replay;
1050 #endif
1051 return -EOPNOTSUPP;
1054 if (!ifname[0])
1055 snprintf(ifname, IFNAMSIZ, "%s%%d", ops->kind);
1056 dev = alloc_netdev(ops->priv_size, ifname, ops->setup);
1057 if (!dev)
1058 return -ENOMEM;
1060 if (strchr(dev->name, '%')) {
1061 err = dev_alloc_name(dev, dev->name);
1062 if (err < 0)
1063 goto err_free;
1065 dev->rtnl_link_ops = ops;
1067 if (tb[IFLA_MTU])
1068 dev->mtu = nla_get_u32(tb[IFLA_MTU]);
1069 if (tb[IFLA_ADDRESS])
1070 memcpy(dev->dev_addr, nla_data(tb[IFLA_ADDRESS]),
1071 nla_len(tb[IFLA_ADDRESS]));
1072 if (tb[IFLA_BROADCAST])
1073 memcpy(dev->broadcast, nla_data(tb[IFLA_BROADCAST]),
1074 nla_len(tb[IFLA_BROADCAST]));
1075 if (tb[IFLA_TXQLEN])
1076 dev->tx_queue_len = nla_get_u32(tb[IFLA_TXQLEN]);
1077 if (tb[IFLA_WEIGHT])
1078 dev->weight = nla_get_u32(tb[IFLA_WEIGHT]);
1079 if (tb[IFLA_OPERSTATE])
1080 set_operstate(dev, nla_get_u8(tb[IFLA_OPERSTATE]));
1081 if (tb[IFLA_LINKMODE])
1082 dev->link_mode = nla_get_u8(tb[IFLA_LINKMODE]);
1084 if (ops->newlink)
1085 err = ops->newlink(dev, tb, data);
1086 else
1087 err = register_netdevice(dev);
1088 err_free:
1089 if (err < 0)
1090 free_netdev(dev);
1091 return err;
1095 static int rtnl_getlink(struct sk_buff *skb, struct nlmsghdr* nlh, void *arg)
1097 struct ifinfomsg *ifm;
1098 struct nlattr *tb[IFLA_MAX+1];
1099 struct net_device *dev = NULL;
1100 struct sk_buff *nskb;
1101 int err;
1103 err = nlmsg_parse(nlh, sizeof(*ifm), tb, IFLA_MAX, ifla_policy);
1104 if (err < 0)
1105 return err;
1107 ifm = nlmsg_data(nlh);
1108 if (ifm->ifi_index > 0) {
1109 dev = dev_get_by_index(ifm->ifi_index);
1110 if (dev == NULL)
1111 return -ENODEV;
1112 } else
1113 return -EINVAL;
1115 nskb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1116 if (nskb == NULL) {
1117 err = -ENOBUFS;
1118 goto errout;
1121 err = rtnl_fill_ifinfo(nskb, dev, RTM_NEWLINK, NETLINK_CB(skb).pid,
1122 nlh->nlmsg_seq, 0, 0);
1123 if (err < 0) {
1124 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1125 WARN_ON(err == -EMSGSIZE);
1126 kfree_skb(nskb);
1127 goto errout;
1129 err = rtnl_unicast(nskb, NETLINK_CB(skb).pid);
1130 errout:
1131 dev_put(dev);
1133 return err;
1136 static int rtnl_dump_all(struct sk_buff *skb, struct netlink_callback *cb)
1138 int idx;
1139 int s_idx = cb->family;
1141 if (s_idx == 0)
1142 s_idx = 1;
1143 for (idx=1; idx<NPROTO; idx++) {
1144 int type = cb->nlh->nlmsg_type-RTM_BASE;
1145 if (idx < s_idx || idx == PF_PACKET)
1146 continue;
1147 if (rtnl_msg_handlers[idx] == NULL ||
1148 rtnl_msg_handlers[idx][type].dumpit == NULL)
1149 continue;
1150 if (idx > s_idx)
1151 memset(&cb->args[0], 0, sizeof(cb->args));
1152 if (rtnl_msg_handlers[idx][type].dumpit(skb, cb))
1153 break;
1155 cb->family = idx;
1157 return skb->len;
1160 void rtmsg_ifinfo(int type, struct net_device *dev, unsigned change)
1162 struct sk_buff *skb;
1163 int err = -ENOBUFS;
1165 skb = nlmsg_new(if_nlmsg_size(dev), GFP_KERNEL);
1166 if (skb == NULL)
1167 goto errout;
1169 err = rtnl_fill_ifinfo(skb, dev, type, 0, 0, change, 0);
1170 if (err < 0) {
1171 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1172 WARN_ON(err == -EMSGSIZE);
1173 kfree_skb(skb);
1174 goto errout;
1176 err = rtnl_notify(skb, 0, RTNLGRP_LINK, NULL, GFP_KERNEL);
1177 errout:
1178 if (err < 0)
1179 rtnl_set_sk_err(RTNLGRP_LINK, err);
1182 /* Protected by RTNL sempahore. */
1183 static struct rtattr **rta_buf;
1184 static int rtattr_max;
1186 /* Process one rtnetlink message. */
1188 static int rtnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
1190 rtnl_doit_func doit;
1191 int sz_idx, kind;
1192 int min_len;
1193 int family;
1194 int type;
1195 int err;
1197 type = nlh->nlmsg_type;
1198 if (type > RTM_MAX)
1199 return -EOPNOTSUPP;
1201 type -= RTM_BASE;
1203 /* All the messages must have at least 1 byte length */
1204 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct rtgenmsg)))
1205 return 0;
1207 family = ((struct rtgenmsg*)NLMSG_DATA(nlh))->rtgen_family;
1208 if (family >= NPROTO)
1209 return -EAFNOSUPPORT;
1211 sz_idx = type>>2;
1212 kind = type&3;
1214 if (kind != 2 && security_netlink_recv(skb, CAP_NET_ADMIN))
1215 return -EPERM;
1217 if (kind == 2 && nlh->nlmsg_flags&NLM_F_DUMP) {
1218 rtnl_dumpit_func dumpit;
1220 dumpit = rtnl_get_dumpit(family, type);
1221 if (dumpit == NULL)
1222 return -EOPNOTSUPP;
1224 __rtnl_unlock();
1225 err = netlink_dump_start(rtnl, skb, nlh, dumpit, NULL);
1226 rtnl_lock();
1227 return err;
1230 memset(rta_buf, 0, (rtattr_max * sizeof(struct rtattr *)));
1232 min_len = rtm_min[sz_idx];
1233 if (nlh->nlmsg_len < min_len)
1234 return -EINVAL;
1236 if (nlh->nlmsg_len > min_len) {
1237 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
1238 struct rtattr *attr = (void*)nlh + NLMSG_ALIGN(min_len);
1240 while (RTA_OK(attr, attrlen)) {
1241 unsigned flavor = attr->rta_type;
1242 if (flavor) {
1243 if (flavor > rta_max[sz_idx])
1244 return -EINVAL;
1245 rta_buf[flavor-1] = attr;
1247 attr = RTA_NEXT(attr, attrlen);
1251 doit = rtnl_get_doit(family, type);
1252 if (doit == NULL)
1253 return -EOPNOTSUPP;
1255 return doit(skb, nlh, (void *)&rta_buf[0]);
1258 static void rtnetlink_rcv(struct sock *sk, int len)
1260 unsigned int qlen = 0;
1262 do {
1263 mutex_lock(&rtnl_mutex);
1264 netlink_run_queue(sk, &qlen, &rtnetlink_rcv_msg);
1265 mutex_unlock(&rtnl_mutex);
1267 netdev_run_todo();
1268 } while (qlen);
1271 static int rtnetlink_event(struct notifier_block *this, unsigned long event, void *ptr)
1273 struct net_device *dev = ptr;
1274 switch (event) {
1275 case NETDEV_UNREGISTER:
1276 rtmsg_ifinfo(RTM_DELLINK, dev, ~0U);
1277 break;
1278 case NETDEV_REGISTER:
1279 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U);
1280 break;
1281 case NETDEV_UP:
1282 case NETDEV_DOWN:
1283 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING);
1284 break;
1285 case NETDEV_CHANGE:
1286 case NETDEV_GOING_DOWN:
1287 break;
1288 default:
1289 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1290 break;
1292 return NOTIFY_DONE;
1295 static struct notifier_block rtnetlink_dev_notifier = {
1296 .notifier_call = rtnetlink_event,
1299 void __init rtnetlink_init(void)
1301 int i;
1303 rtattr_max = 0;
1304 for (i = 0; i < ARRAY_SIZE(rta_max); i++)
1305 if (rta_max[i] > rtattr_max)
1306 rtattr_max = rta_max[i];
1307 rta_buf = kmalloc(rtattr_max * sizeof(struct rtattr *), GFP_KERNEL);
1308 if (!rta_buf)
1309 panic("rtnetlink_init: cannot allocate rta_buf\n");
1311 rtnl = netlink_kernel_create(NETLINK_ROUTE, RTNLGRP_MAX, rtnetlink_rcv,
1312 &rtnl_mutex, THIS_MODULE);
1313 if (rtnl == NULL)
1314 panic("rtnetlink_init: cannot initialize rtnetlink\n");
1315 netlink_set_nonroot(NETLINK_ROUTE, NL_NONROOT_RECV);
1316 register_netdevice_notifier(&rtnetlink_dev_notifier);
1318 rtnl_register(PF_UNSPEC, RTM_GETLINK, rtnl_getlink, rtnl_dump_ifinfo);
1319 rtnl_register(PF_UNSPEC, RTM_SETLINK, rtnl_setlink, NULL);
1320 rtnl_register(PF_UNSPEC, RTM_NEWLINK, rtnl_newlink, NULL);
1321 rtnl_register(PF_UNSPEC, RTM_DELLINK, rtnl_dellink, NULL);
1323 rtnl_register(PF_UNSPEC, RTM_GETADDR, NULL, rtnl_dump_all);
1324 rtnl_register(PF_UNSPEC, RTM_GETROUTE, NULL, rtnl_dump_all);
1327 EXPORT_SYMBOL(__rta_fill);
1328 EXPORT_SYMBOL(rtattr_strlcpy);
1329 EXPORT_SYMBOL(rtattr_parse);
1330 EXPORT_SYMBOL(__rtattr_parse_nested_compat);
1331 EXPORT_SYMBOL(rtnetlink_put_metrics);
1332 EXPORT_SYMBOL(rtnl_lock);
1333 EXPORT_SYMBOL(rtnl_trylock);
1334 EXPORT_SYMBOL(rtnl_unlock);
1335 EXPORT_SYMBOL(rtnl_unicast);
1336 EXPORT_SYMBOL(rtnl_notify);
1337 EXPORT_SYMBOL(rtnl_set_sk_err);