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>
58 rtnl_dumpit_func dumpit
;
61 static DEFINE_MUTEX(rtnl_mutex
);
62 static struct sock
*rtnl
;
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);
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
)
95 rta
= RTA_NEXT(rta
, len
);
100 int __rtattr_parse_nested_compat(struct rtattr
*tb
[], int maxattr
,
101 struct rtattr
*rta
, int len
)
103 if (RTA_PAYLOAD(rta
) < len
)
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
);
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
);
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
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
;
174 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
175 msgindex
= rtm_msgindex(msgtype
);
177 tab
= rtnl_msg_handlers
[protocol
];
179 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
183 rtnl_msg_handlers
[protocol
] = tab
;
187 tab
[msgindex
].doit
= doit
;
190 tab
[msgindex
].dumpit
= dumpit
;
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",
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
)
228 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
229 msgindex
= rtm_msgindex(msgtype
);
231 if (rtnl_msg_handlers
[protocol
] == NULL
)
234 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
235 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
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
)
274 ops
->dellink
= unregister_netdevice
;
276 list_add_tail(&ops
->list
, &link_ops
);
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
)
293 err
= __rtnl_link_register(ops
);
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
)
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
)
326 __rtnl_link_unregister(ops
);
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
))
343 static size_t rtnl_link_get_size(const struct net_device
*dev
)
345 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
351 size
= nlmsg_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
352 nlmsg_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
355 /* IFLA_INFO_DATA + nested data */
356 size
+= nlmsg_total_size(sizeof(struct nlattr
)) +
359 if (ops
->get_xstats_size
)
360 size
+= ops
->get_xstats_size(dev
); /* IFLA_INFO_XSTATS */
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
;
371 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
372 if (linkinfo
== NULL
)
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
);
380 goto err_cancel_link
;
382 if (ops
->fill_info
) {
383 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
385 goto err_cancel_link
;
386 err
= ops
->fill_info(skb
, dev
);
388 goto err_cancel_data
;
389 nla_nest_end(skb
, data
);
392 nla_nest_end(skb
, linkinfo
);
396 nla_nest_cancel(skb
, data
);
398 nla_nest_cancel(skb
, linkinfo
);
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
)
432 int size
= RTA_LENGTH(attrlen
);
434 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
435 rta
->rta_type
= attrtype
;
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')
449 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
450 memset(dest
, 0, size
);
451 memcpy(dest
, src
, len
);
456 int rtnetlink_send(struct sk_buff
*skb
, u32 pid
, unsigned group
, int echo
)
460 NETLINK_CB(skb
).dst_group
= group
;
462 atomic_inc(&skb
->users
);
463 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
465 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
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
)
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
)
495 mx
= nla_nest_start(skb
, RTA_METRICS
);
499 for (i
= 0; i
< RTAX_MAX
; i
++) {
502 NLA_PUT_U32(skb
, i
+1, metrics
[i
]);
507 nla_nest_cancel(skb
, mx
);
511 return nla_nest_end(skb
, mx
);
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
)),
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
;
544 if ((operstate
== IF_OPER_DORMANT
||
545 operstate
== IF_OPER_UNKNOWN
) &&
547 operstate
= IF_OPER_UP
;
550 case IF_OPER_DORMANT
:
551 if (operstate
== IF_OPER_UP
||
552 operstate
== IF_OPER_UNKNOWN
)
553 operstate
= IF_OPER_DORMANT
;
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
,
620 struct ifinfomsg
*ifm
;
621 struct nlmsghdr
*nlh
;
623 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
627 ifm
= nlmsg_data(nlh
);
628 ifm
->ifi_family
= AF_UNSPEC
;
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
);
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
);
653 struct rtnl_link_ifmap map
= {
654 .mem_start
= dev
->mem_start
,
655 .mem_end
= dev
->mem_end
,
656 .base_addr
= dev
->base_addr
,
659 .port
= dev
->if_port
,
661 NLA_PUT(skb
, IFLA_MAP
, sizeof(map
), &map
);
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
);
674 attr
= nla_reserve(skb
, IFLA_STATS
,
675 sizeof(struct rtnl_link_stats
));
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
);
691 nlmsg_cancel(skb
, nlh
);
695 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
698 int s_idx
= cb
->args
[0];
699 struct net_device
*dev
;
702 for_each_netdev(dev
) {
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)
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;
741 struct rtnl_link_ifmap
*u_map
;
744 if (!dev
->set_config
) {
749 if (!netif_device_present(dev
)) {
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
);
769 if (tb
[IFLA_ADDRESS
]) {
773 if (!dev
->set_mac_address
) {
778 if (!netif_device_present(dev
)) {
783 len
= sizeof(sa_family_t
) + dev
->addr_len
;
784 sa
= kmalloc(len
, GFP_KERNEL
);
789 sa
->sa_family
= dev
->type
;
790 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
792 err
= dev
->set_mac_address(dev
, sa
);
796 send_addr_notify
= 1;
801 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
808 * Interface selected by interface index but interface
809 * name provided implies that a name change has been
812 if (ifm
->ifi_index
> 0 && ifname
[0]) {
813 err
= dev_change_name(dev
, ifname
);
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 */
829 flags
= (flags
& ifm
->ifi_change
) |
830 (dev
->flags
& ~ifm
->ifi_change
);
831 dev_change_flags(dev
, flags
);
835 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
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
);
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
);
863 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
865 struct ifinfomsg
*ifm
;
866 struct net_device
*dev
;
868 struct nlattr
*tb
[IFLA_MAX
+1];
869 char ifname
[IFNAMSIZ
];
871 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
876 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
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
);
894 if (tb
[IFLA_ADDRESS
] &&
895 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
898 if (tb
[IFLA_BROADCAST
] &&
899 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
902 err
= do_setlink(dev
, ifm
, tb
, ifname
, 0);
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];
918 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
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
);
936 ops
= dev
->rtnl_link_ops
;
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];
958 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
963 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
967 ifm
= nlmsg_data(nlh
);
968 if (ifm
->ifi_index
> 0)
969 dev
= __dev_get_by_index(ifm
->ifi_index
);
971 dev
= __dev_get_by_name(ifname
);
975 if (tb
[IFLA_LINKINFO
]) {
976 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
977 tb
[IFLA_LINKINFO
], ifla_info_policy
);
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
);
992 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 0], **data
= NULL
;
995 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
996 err
= nla_parse_nested(attr
, ops
->maxtype
,
997 linkinfo
[IFLA_INFO_DATA
],
1003 if (ops
->validate
) {
1004 err
= ops
->validate(tb
, data
);
1013 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1015 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1018 if (linkinfo
[IFLA_INFO_DATA
]) {
1019 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
1023 err
= ops
->changelink(dev
, tb
, data
);
1029 return do_setlink(dev
, ifm
, tb
, ifname
, modified
);
1032 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
1035 if (ifm
->ifi_index
|| ifm
->ifi_flags
|| ifm
->ifi_change
)
1037 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
1044 request_module("rtnl-link-%s", kind
);
1046 ops
= rtnl_link_ops_get(kind
);
1055 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
1056 dev
= alloc_netdev(ops
->priv_size
, ifname
, ops
->setup
);
1060 if (strchr(dev
->name
, '%')) {
1061 err
= dev_alloc_name(dev
, dev
->name
);
1065 dev
->rtnl_link_ops
= ops
;
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
]);
1085 err
= ops
->newlink(dev
, tb
, data
);
1087 err
= register_netdevice(dev
);
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
;
1103 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1107 ifm
= nlmsg_data(nlh
);
1108 if (ifm
->ifi_index
> 0) {
1109 dev
= dev_get_by_index(ifm
->ifi_index
);
1115 nskb
= nlmsg_new(if_nlmsg_size(dev
), GFP_KERNEL
);
1121 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).pid
,
1122 nlh
->nlmsg_seq
, 0, 0);
1124 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1125 WARN_ON(err
== -EMSGSIZE
);
1129 err
= rtnl_unicast(nskb
, NETLINK_CB(skb
).pid
);
1136 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1139 int s_idx
= cb
->family
;
1143 for (idx
=1; idx
<NPROTO
; idx
++) {
1144 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
1145 if (idx
< s_idx
|| idx
== PF_PACKET
)
1147 if (rtnl_msg_handlers
[idx
] == NULL
||
1148 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
1151 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
1152 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
1160 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
1162 struct sk_buff
*skb
;
1165 skb
= nlmsg_new(if_nlmsg_size(dev
), GFP_KERNEL
);
1169 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0);
1171 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1172 WARN_ON(err
== -EMSGSIZE
);
1176 err
= rtnl_notify(skb
, 0, RTNLGRP_LINK
, NULL
, GFP_KERNEL
);
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
;
1197 type
= nlh
->nlmsg_type
;
1203 /* All the messages must have at least 1 byte length */
1204 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
1207 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
1208 if (family
>= NPROTO
)
1209 return -EAFNOSUPPORT
;
1214 if (kind
!= 2 && security_netlink_recv(skb
, CAP_NET_ADMIN
))
1217 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
1218 rtnl_dumpit_func dumpit
;
1220 dumpit
= rtnl_get_dumpit(family
, type
);
1225 err
= netlink_dump_start(rtnl
, skb
, nlh
, dumpit
, NULL
);
1230 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
1232 min_len
= rtm_min
[sz_idx
];
1233 if (nlh
->nlmsg_len
< min_len
)
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
;
1243 if (flavor
> rta_max
[sz_idx
])
1245 rta_buf
[flavor
-1] = attr
;
1247 attr
= RTA_NEXT(attr
, attrlen
);
1251 doit
= rtnl_get_doit(family
, type
);
1255 return doit(skb
, nlh
, (void *)&rta_buf
[0]);
1258 static void rtnetlink_rcv(struct sock
*sk
, int len
)
1260 unsigned int qlen
= 0;
1263 mutex_lock(&rtnl_mutex
);
1264 netlink_run_queue(sk
, &qlen
, &rtnetlink_rcv_msg
);
1265 mutex_unlock(&rtnl_mutex
);
1271 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1273 struct net_device
*dev
= ptr
;
1275 case NETDEV_UNREGISTER
:
1276 rtmsg_ifinfo(RTM_DELLINK
, dev
, ~0U);
1278 case NETDEV_REGISTER
:
1279 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U);
1283 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
);
1286 case NETDEV_GOING_DOWN
:
1289 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
1295 static struct notifier_block rtnetlink_dev_notifier
= {
1296 .notifier_call
= rtnetlink_event
,
1299 void __init
rtnetlink_init(void)
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
);
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
);
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
);