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>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
45 #include <net/protocol.h>
47 #include <net/route.h>
50 #include <net/pkt_sched.h>
51 #include <net/fib_rules.h>
52 #include <net/rtnetlink.h>
53 #include <net/net_namespace.h>
57 rtnl_dumpit_func dumpit
;
60 static DEFINE_MUTEX(rtnl_mutex
);
64 mutex_lock(&rtnl_mutex
);
66 EXPORT_SYMBOL(rtnl_lock
);
68 void __rtnl_unlock(void)
70 mutex_unlock(&rtnl_mutex
);
73 void rtnl_unlock(void)
75 /* This fellow will unlock it for us. */
78 EXPORT_SYMBOL(rtnl_unlock
);
80 int rtnl_trylock(void)
82 return mutex_trylock(&rtnl_mutex
);
84 EXPORT_SYMBOL(rtnl_trylock
);
86 int rtnl_is_locked(void)
88 return mutex_is_locked(&rtnl_mutex
);
90 EXPORT_SYMBOL(rtnl_is_locked
);
92 #ifdef CONFIG_PROVE_LOCKING
93 int lockdep_rtnl_is_held(void)
95 return lockdep_is_held(&rtnl_mutex
);
97 EXPORT_SYMBOL(lockdep_rtnl_is_held
);
98 #endif /* #ifdef CONFIG_PROVE_LOCKING */
100 static struct rtnl_link
*rtnl_msg_handlers
[NPROTO
];
102 static inline int rtm_msgindex(int msgtype
)
104 int msgindex
= msgtype
- RTM_BASE
;
107 * msgindex < 0 implies someone tried to register a netlink
108 * control code. msgindex >= RTM_NR_MSGTYPES may indicate that
109 * the message type has not been added to linux/rtnetlink.h
111 BUG_ON(msgindex
< 0 || msgindex
>= RTM_NR_MSGTYPES
);
116 static rtnl_doit_func
rtnl_get_doit(int protocol
, int msgindex
)
118 struct rtnl_link
*tab
;
120 tab
= rtnl_msg_handlers
[protocol
];
121 if (tab
== NULL
|| tab
[msgindex
].doit
== NULL
)
122 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
124 return tab
? tab
[msgindex
].doit
: NULL
;
127 static rtnl_dumpit_func
rtnl_get_dumpit(int protocol
, int msgindex
)
129 struct rtnl_link
*tab
;
131 tab
= rtnl_msg_handlers
[protocol
];
132 if (tab
== NULL
|| tab
[msgindex
].dumpit
== NULL
)
133 tab
= rtnl_msg_handlers
[PF_UNSPEC
];
135 return tab
? tab
[msgindex
].dumpit
: NULL
;
139 * __rtnl_register - Register a rtnetlink message type
140 * @protocol: Protocol family or PF_UNSPEC
141 * @msgtype: rtnetlink message type
142 * @doit: Function pointer called for each request message
143 * @dumpit: Function pointer called for each dump request (NLM_F_DUMP) message
145 * Registers the specified function pointers (at least one of them has
146 * to be non-NULL) to be called whenever a request message for the
147 * specified protocol family and message type is received.
149 * The special protocol family PF_UNSPEC may be used to define fallback
150 * function pointers for the case when no entry for the specific protocol
153 * Returns 0 on success or a negative error code.
155 int __rtnl_register(int protocol
, int msgtype
,
156 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
)
158 struct rtnl_link
*tab
;
161 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
162 msgindex
= rtm_msgindex(msgtype
);
164 tab
= rtnl_msg_handlers
[protocol
];
166 tab
= kcalloc(RTM_NR_MSGTYPES
, sizeof(*tab
), GFP_KERNEL
);
170 rtnl_msg_handlers
[protocol
] = tab
;
174 tab
[msgindex
].doit
= doit
;
177 tab
[msgindex
].dumpit
= dumpit
;
181 EXPORT_SYMBOL_GPL(__rtnl_register
);
184 * rtnl_register - Register a rtnetlink message type
186 * Identical to __rtnl_register() but panics on failure. This is useful
187 * as failure of this function is very unlikely, it can only happen due
188 * to lack of memory when allocating the chain to store all message
189 * handlers for a protocol. Meant for use in init functions where lack
190 * of memory implies no sense in continueing.
192 void rtnl_register(int protocol
, int msgtype
,
193 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
)
195 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
) < 0)
196 panic("Unable to register rtnetlink message handler, "
197 "protocol = %d, message type = %d\n",
200 EXPORT_SYMBOL_GPL(rtnl_register
);
203 * rtnl_unregister - Unregister a rtnetlink message type
204 * @protocol: Protocol family or PF_UNSPEC
205 * @msgtype: rtnetlink message type
207 * Returns 0 on success or a negative error code.
209 int rtnl_unregister(int protocol
, int msgtype
)
213 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
214 msgindex
= rtm_msgindex(msgtype
);
216 if (rtnl_msg_handlers
[protocol
] == NULL
)
219 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
220 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
224 EXPORT_SYMBOL_GPL(rtnl_unregister
);
227 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
228 * @protocol : Protocol family or PF_UNSPEC
230 * Identical to calling rtnl_unregster() for all registered message types
231 * of a certain protocol family.
233 void rtnl_unregister_all(int protocol
)
235 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
237 kfree(rtnl_msg_handlers
[protocol
]);
238 rtnl_msg_handlers
[protocol
] = NULL
;
240 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
242 static LIST_HEAD(link_ops
);
245 * __rtnl_link_register - Register rtnl_link_ops with rtnetlink.
246 * @ops: struct rtnl_link_ops * to register
248 * The caller must hold the rtnl_mutex. This function should be used
249 * by drivers that create devices during module initialization. It
250 * must be called before registering the devices.
252 * Returns 0 on success or a negative error code.
254 int __rtnl_link_register(struct rtnl_link_ops
*ops
)
257 ops
->dellink
= unregister_netdevice_queue
;
259 list_add_tail(&ops
->list
, &link_ops
);
262 EXPORT_SYMBOL_GPL(__rtnl_link_register
);
265 * rtnl_link_register - Register rtnl_link_ops with rtnetlink.
266 * @ops: struct rtnl_link_ops * to register
268 * Returns 0 on success or a negative error code.
270 int rtnl_link_register(struct rtnl_link_ops
*ops
)
275 err
= __rtnl_link_register(ops
);
279 EXPORT_SYMBOL_GPL(rtnl_link_register
);
281 static void __rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
283 struct net_device
*dev
;
284 LIST_HEAD(list_kill
);
286 for_each_netdev(net
, dev
) {
287 if (dev
->rtnl_link_ops
== ops
)
288 ops
->dellink(dev
, &list_kill
);
290 unregister_netdevice_many(&list_kill
);
293 void rtnl_kill_links(struct net
*net
, struct rtnl_link_ops
*ops
)
296 __rtnl_kill_links(net
, ops
);
299 EXPORT_SYMBOL_GPL(rtnl_kill_links
);
302 * __rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
303 * @ops: struct rtnl_link_ops * to unregister
305 * The caller must hold the rtnl_mutex.
307 void __rtnl_link_unregister(struct rtnl_link_ops
*ops
)
312 __rtnl_kill_links(net
, ops
);
314 list_del(&ops
->list
);
316 EXPORT_SYMBOL_GPL(__rtnl_link_unregister
);
319 * rtnl_link_unregister - Unregister rtnl_link_ops from rtnetlink.
320 * @ops: struct rtnl_link_ops * to unregister
322 void rtnl_link_unregister(struct rtnl_link_ops
*ops
)
325 __rtnl_link_unregister(ops
);
328 EXPORT_SYMBOL_GPL(rtnl_link_unregister
);
330 static const struct rtnl_link_ops
*rtnl_link_ops_get(const char *kind
)
332 const struct rtnl_link_ops
*ops
;
334 list_for_each_entry(ops
, &link_ops
, list
) {
335 if (!strcmp(ops
->kind
, kind
))
341 static size_t rtnl_link_get_size(const struct net_device
*dev
)
343 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
349 size
= nlmsg_total_size(sizeof(struct nlattr
)) + /* IFLA_LINKINFO */
350 nlmsg_total_size(strlen(ops
->kind
) + 1); /* IFLA_INFO_KIND */
353 /* IFLA_INFO_DATA + nested data */
354 size
+= nlmsg_total_size(sizeof(struct nlattr
)) +
357 if (ops
->get_xstats_size
)
358 size
+= ops
->get_xstats_size(dev
); /* IFLA_INFO_XSTATS */
363 static int rtnl_link_fill(struct sk_buff
*skb
, const struct net_device
*dev
)
365 const struct rtnl_link_ops
*ops
= dev
->rtnl_link_ops
;
366 struct nlattr
*linkinfo
, *data
;
369 linkinfo
= nla_nest_start(skb
, IFLA_LINKINFO
);
370 if (linkinfo
== NULL
)
373 if (nla_put_string(skb
, IFLA_INFO_KIND
, ops
->kind
) < 0)
374 goto err_cancel_link
;
375 if (ops
->fill_xstats
) {
376 err
= ops
->fill_xstats(skb
, dev
);
378 goto err_cancel_link
;
380 if (ops
->fill_info
) {
381 data
= nla_nest_start(skb
, IFLA_INFO_DATA
);
383 goto err_cancel_link
;
384 err
= ops
->fill_info(skb
, dev
);
386 goto err_cancel_data
;
387 nla_nest_end(skb
, data
);
390 nla_nest_end(skb
, linkinfo
);
394 nla_nest_cancel(skb
, data
);
396 nla_nest_cancel(skb
, linkinfo
);
401 static const int rtm_min
[RTM_NR_FAMILIES
] =
403 [RTM_FAM(RTM_NEWLINK
)] = NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
404 [RTM_FAM(RTM_NEWADDR
)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
405 [RTM_FAM(RTM_NEWROUTE
)] = NLMSG_LENGTH(sizeof(struct rtmsg
)),
406 [RTM_FAM(RTM_NEWRULE
)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
407 [RTM_FAM(RTM_NEWQDISC
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
408 [RTM_FAM(RTM_NEWTCLASS
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
409 [RTM_FAM(RTM_NEWTFILTER
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
410 [RTM_FAM(RTM_NEWACTION
)] = NLMSG_LENGTH(sizeof(struct tcamsg
)),
411 [RTM_FAM(RTM_GETMULTICAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
412 [RTM_FAM(RTM_GETANYCAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
415 static const int rta_max
[RTM_NR_FAMILIES
] =
417 [RTM_FAM(RTM_NEWLINK
)] = IFLA_MAX
,
418 [RTM_FAM(RTM_NEWADDR
)] = IFA_MAX
,
419 [RTM_FAM(RTM_NEWROUTE
)] = RTA_MAX
,
420 [RTM_FAM(RTM_NEWRULE
)] = FRA_MAX
,
421 [RTM_FAM(RTM_NEWQDISC
)] = TCA_MAX
,
422 [RTM_FAM(RTM_NEWTCLASS
)] = TCA_MAX
,
423 [RTM_FAM(RTM_NEWTFILTER
)] = TCA_MAX
,
424 [RTM_FAM(RTM_NEWACTION
)] = TCAA_MAX
,
427 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
430 int size
= RTA_LENGTH(attrlen
);
432 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
433 rta
->rta_type
= attrtype
;
435 memcpy(RTA_DATA(rta
), data
, attrlen
);
436 memset(RTA_DATA(rta
) + attrlen
, 0, RTA_ALIGN(size
) - size
);
438 EXPORT_SYMBOL(__rta_fill
);
440 int rtnetlink_send(struct sk_buff
*skb
, struct net
*net
, u32 pid
, unsigned group
, int echo
)
442 struct sock
*rtnl
= net
->rtnl
;
445 NETLINK_CB(skb
).dst_group
= group
;
447 atomic_inc(&skb
->users
);
448 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
450 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
454 int rtnl_unicast(struct sk_buff
*skb
, struct net
*net
, u32 pid
)
456 struct sock
*rtnl
= net
->rtnl
;
458 return nlmsg_unicast(rtnl
, skb
, pid
);
460 EXPORT_SYMBOL(rtnl_unicast
);
462 void rtnl_notify(struct sk_buff
*skb
, struct net
*net
, u32 pid
, u32 group
,
463 struct nlmsghdr
*nlh
, gfp_t flags
)
465 struct sock
*rtnl
= net
->rtnl
;
469 report
= nlmsg_report(nlh
);
471 nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
473 EXPORT_SYMBOL(rtnl_notify
);
475 void rtnl_set_sk_err(struct net
*net
, u32 group
, int error
)
477 struct sock
*rtnl
= net
->rtnl
;
479 netlink_set_err(rtnl
, 0, group
, error
);
481 EXPORT_SYMBOL(rtnl_set_sk_err
);
483 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
488 mx
= nla_nest_start(skb
, RTA_METRICS
);
492 for (i
= 0; i
< RTAX_MAX
; i
++) {
495 NLA_PUT_U32(skb
, i
+1, metrics
[i
]);
500 nla_nest_cancel(skb
, mx
);
504 return nla_nest_end(skb
, mx
);
507 nla_nest_cancel(skb
, mx
);
510 EXPORT_SYMBOL(rtnetlink_put_metrics
);
512 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
513 u32 ts
, u32 tsage
, long expires
, u32 error
)
515 struct rta_cacheinfo ci
= {
516 .rta_lastuse
= jiffies_to_clock_t(jiffies
- dst
->lastuse
),
517 .rta_used
= dst
->__use
,
518 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
526 ci
.rta_expires
= jiffies_to_clock_t(expires
);
528 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
530 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
532 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
534 unsigned char operstate
= dev
->operstate
;
536 switch (transition
) {
538 if ((operstate
== IF_OPER_DORMANT
||
539 operstate
== IF_OPER_UNKNOWN
) &&
541 operstate
= IF_OPER_UP
;
544 case IF_OPER_DORMANT
:
545 if (operstate
== IF_OPER_UP
||
546 operstate
== IF_OPER_UNKNOWN
)
547 operstate
= IF_OPER_DORMANT
;
551 if (dev
->operstate
!= operstate
) {
552 write_lock_bh(&dev_base_lock
);
553 dev
->operstate
= operstate
;
554 write_unlock_bh(&dev_base_lock
);
555 netdev_state_change(dev
);
559 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
560 const struct net_device_stats
*b
)
562 a
->rx_packets
= b
->rx_packets
;
563 a
->tx_packets
= b
->tx_packets
;
564 a
->rx_bytes
= b
->rx_bytes
;
565 a
->tx_bytes
= b
->tx_bytes
;
566 a
->rx_errors
= b
->rx_errors
;
567 a
->tx_errors
= b
->tx_errors
;
568 a
->rx_dropped
= b
->rx_dropped
;
569 a
->tx_dropped
= b
->tx_dropped
;
571 a
->multicast
= b
->multicast
;
572 a
->collisions
= b
->collisions
;
574 a
->rx_length_errors
= b
->rx_length_errors
;
575 a
->rx_over_errors
= b
->rx_over_errors
;
576 a
->rx_crc_errors
= b
->rx_crc_errors
;
577 a
->rx_frame_errors
= b
->rx_frame_errors
;
578 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
579 a
->rx_missed_errors
= b
->rx_missed_errors
;
581 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
582 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
583 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
584 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
585 a
->tx_window_errors
= b
->tx_window_errors
;
587 a
->rx_compressed
= b
->rx_compressed
;
588 a
->tx_compressed
= b
->tx_compressed
;
591 static inline size_t if_nlmsg_size(const struct net_device
*dev
)
593 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
594 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
595 + nla_total_size(IFALIASZ
) /* IFLA_IFALIAS */
596 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
597 + nla_total_size(sizeof(struct rtnl_link_ifmap
))
598 + nla_total_size(sizeof(struct rtnl_link_stats
))
599 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
600 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
601 + nla_total_size(4) /* IFLA_TXQLEN */
602 + nla_total_size(4) /* IFLA_WEIGHT */
603 + nla_total_size(4) /* IFLA_MTU */
604 + nla_total_size(4) /* IFLA_LINK */
605 + nla_total_size(4) /* IFLA_MASTER */
606 + nla_total_size(1) /* IFLA_OPERSTATE */
607 + nla_total_size(1) /* IFLA_LINKMODE */
608 + rtnl_link_get_size(dev
); /* IFLA_LINKINFO */
611 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
612 int type
, u32 pid
, u32 seq
, u32 change
,
615 struct ifinfomsg
*ifm
;
616 struct nlmsghdr
*nlh
;
617 const struct net_device_stats
*stats
;
620 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
624 ifm
= nlmsg_data(nlh
);
625 ifm
->ifi_family
= AF_UNSPEC
;
627 ifm
->ifi_type
= dev
->type
;
628 ifm
->ifi_index
= dev
->ifindex
;
629 ifm
->ifi_flags
= dev_get_flags(dev
);
630 ifm
->ifi_change
= change
;
632 NLA_PUT_STRING(skb
, IFLA_IFNAME
, dev
->name
);
633 NLA_PUT_U32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
);
634 NLA_PUT_U8(skb
, IFLA_OPERSTATE
,
635 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
);
636 NLA_PUT_U8(skb
, IFLA_LINKMODE
, dev
->link_mode
);
637 NLA_PUT_U32(skb
, IFLA_MTU
, dev
->mtu
);
639 if (dev
->ifindex
!= dev
->iflink
)
640 NLA_PUT_U32(skb
, IFLA_LINK
, dev
->iflink
);
643 NLA_PUT_U32(skb
, IFLA_MASTER
, dev
->master
->ifindex
);
646 NLA_PUT_STRING(skb
, IFLA_QDISC
, dev
->qdisc
->ops
->id
);
649 NLA_PUT_STRING(skb
, IFLA_IFALIAS
, dev
->ifalias
);
652 struct rtnl_link_ifmap map
= {
653 .mem_start
= dev
->mem_start
,
654 .mem_end
= dev
->mem_end
,
655 .base_addr
= dev
->base_addr
,
658 .port
= dev
->if_port
,
660 NLA_PUT(skb
, IFLA_MAP
, sizeof(map
), &map
);
664 NLA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
665 NLA_PUT(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
);
668 attr
= nla_reserve(skb
, IFLA_STATS
,
669 sizeof(struct rtnl_link_stats
));
671 goto nla_put_failure
;
673 stats
= dev_get_stats(dev
);
674 copy_rtnl_link_stats(nla_data(attr
), stats
);
676 if (dev
->rtnl_link_ops
) {
677 if (rtnl_link_fill(skb
, dev
) < 0)
678 goto nla_put_failure
;
681 return nlmsg_end(skb
, nlh
);
684 nlmsg_cancel(skb
, nlh
);
688 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
690 struct net
*net
= sock_net(skb
->sk
);
693 struct net_device
*dev
;
694 struct hlist_head
*head
;
695 struct hlist_node
*node
;
700 for (h
= s_h
; h
< NETDEV_HASHENTRIES
; h
++, s_idx
= 0) {
702 head
= &net
->dev_index_head
[h
];
703 hlist_for_each_entry(dev
, node
, head
, index_hlist
) {
706 if (rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
707 NETLINK_CB(cb
->skb
).pid
,
708 cb
->nlh
->nlmsg_seq
, 0,
722 const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
723 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
724 [IFLA_ADDRESS
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
725 [IFLA_BROADCAST
] = { .type
= NLA_BINARY
, .len
= MAX_ADDR_LEN
},
726 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
727 [IFLA_MTU
] = { .type
= NLA_U32
},
728 [IFLA_LINK
] = { .type
= NLA_U32
},
729 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
730 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
731 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
732 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
733 [IFLA_LINKINFO
] = { .type
= NLA_NESTED
},
734 [IFLA_NET_NS_PID
] = { .type
= NLA_U32
},
735 [IFLA_IFALIAS
] = { .type
= NLA_STRING
, .len
= IFALIASZ
-1 },
737 EXPORT_SYMBOL(ifla_policy
);
739 static const struct nla_policy ifla_info_policy
[IFLA_INFO_MAX
+1] = {
740 [IFLA_INFO_KIND
] = { .type
= NLA_STRING
},
741 [IFLA_INFO_DATA
] = { .type
= NLA_NESTED
},
744 struct net
*rtnl_link_get_net(struct net
*src_net
, struct nlattr
*tb
[])
747 /* Examine the link attributes and figure out which
748 * network namespace we are talking about.
750 if (tb
[IFLA_NET_NS_PID
])
751 net
= get_net_ns_by_pid(nla_get_u32(tb
[IFLA_NET_NS_PID
]));
753 net
= get_net(src_net
);
756 EXPORT_SYMBOL(rtnl_link_get_net
);
758 static int validate_linkmsg(struct net_device
*dev
, struct nlattr
*tb
[])
761 if (tb
[IFLA_ADDRESS
] &&
762 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
765 if (tb
[IFLA_BROADCAST
] &&
766 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
773 static int do_setlink(struct net_device
*dev
, struct ifinfomsg
*ifm
,
774 struct nlattr
**tb
, char *ifname
, int modified
)
776 const struct net_device_ops
*ops
= dev
->netdev_ops
;
777 int send_addr_notify
= 0;
780 if (tb
[IFLA_NET_NS_PID
]) {
781 struct net
*net
= rtnl_link_get_net(dev_net(dev
), tb
);
786 err
= dev_change_net_namespace(dev
, net
, ifname
);
794 struct rtnl_link_ifmap
*u_map
;
797 if (!ops
->ndo_set_config
) {
802 if (!netif_device_present(dev
)) {
807 u_map
= nla_data(tb
[IFLA_MAP
]);
808 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
809 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
810 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
811 k_map
.irq
= (unsigned char) u_map
->irq
;
812 k_map
.dma
= (unsigned char) u_map
->dma
;
813 k_map
.port
= (unsigned char) u_map
->port
;
815 err
= ops
->ndo_set_config(dev
, &k_map
);
822 if (tb
[IFLA_ADDRESS
]) {
826 if (!ops
->ndo_set_mac_address
) {
831 if (!netif_device_present(dev
)) {
836 len
= sizeof(sa_family_t
) + dev
->addr_len
;
837 sa
= kmalloc(len
, GFP_KERNEL
);
842 sa
->sa_family
= dev
->type
;
843 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
845 err
= ops
->ndo_set_mac_address(dev
, sa
);
849 send_addr_notify
= 1;
854 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
861 * Interface selected by interface index but interface
862 * name provided implies that a name change has been
865 if (ifm
->ifi_index
> 0 && ifname
[0]) {
866 err
= dev_change_name(dev
, ifname
);
872 if (tb
[IFLA_IFALIAS
]) {
873 err
= dev_set_alias(dev
, nla_data(tb
[IFLA_IFALIAS
]),
874 nla_len(tb
[IFLA_IFALIAS
]));
880 if (tb
[IFLA_BROADCAST
]) {
881 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
882 send_addr_notify
= 1;
885 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
886 unsigned int flags
= ifm
->ifi_flags
;
888 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
890 flags
= (flags
& ifm
->ifi_change
) |
891 (dev
->flags
& ~ifm
->ifi_change
);
892 err
= dev_change_flags(dev
, flags
);
898 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
900 if (tb
[IFLA_OPERSTATE
])
901 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
903 if (tb
[IFLA_LINKMODE
]) {
904 write_lock_bh(&dev_base_lock
);
905 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
906 write_unlock_bh(&dev_base_lock
);
912 if (err
< 0 && modified
&& net_ratelimit())
913 printk(KERN_WARNING
"A link change request failed with "
914 "some changes comitted already. Interface %s may "
915 "have been left with an inconsistent configuration, "
916 "please check.\n", dev
->name
);
918 if (send_addr_notify
)
919 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
923 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
925 struct net
*net
= sock_net(skb
->sk
);
926 struct ifinfomsg
*ifm
;
927 struct net_device
*dev
;
929 struct nlattr
*tb
[IFLA_MAX
+1];
930 char ifname
[IFNAMSIZ
];
932 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
937 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
942 ifm
= nlmsg_data(nlh
);
943 if (ifm
->ifi_index
> 0)
944 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
945 else if (tb
[IFLA_IFNAME
])
946 dev
= __dev_get_by_name(net
, ifname
);
955 err
= validate_linkmsg(dev
, tb
);
959 err
= do_setlink(dev
, ifm
, tb
, ifname
, 0);
964 static int rtnl_dellink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
966 struct net
*net
= sock_net(skb
->sk
);
967 const struct rtnl_link_ops
*ops
;
968 struct net_device
*dev
;
969 struct ifinfomsg
*ifm
;
970 char ifname
[IFNAMSIZ
];
971 struct nlattr
*tb
[IFLA_MAX
+1];
974 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
979 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
981 ifm
= nlmsg_data(nlh
);
982 if (ifm
->ifi_index
> 0)
983 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
984 else if (tb
[IFLA_IFNAME
])
985 dev
= __dev_get_by_name(net
, ifname
);
992 ops
= dev
->rtnl_link_ops
;
996 ops
->dellink(dev
, NULL
);
1000 struct net_device
*rtnl_create_link(struct net
*src_net
, struct net
*net
,
1001 char *ifname
, const struct rtnl_link_ops
*ops
, struct nlattr
*tb
[])
1004 struct net_device
*dev
;
1005 unsigned int num_queues
= 1;
1006 unsigned int real_num_queues
= 1;
1008 if (ops
->get_tx_queues
) {
1009 err
= ops
->get_tx_queues(src_net
, tb
, &num_queues
,
1015 dev
= alloc_netdev_mq(ops
->priv_size
, ifname
, ops
->setup
, num_queues
);
1019 dev_net_set(dev
, net
);
1020 dev
->rtnl_link_ops
= ops
;
1021 dev
->real_num_tx_queues
= real_num_queues
;
1023 if (strchr(dev
->name
, '%')) {
1024 err
= dev_alloc_name(dev
, dev
->name
);
1030 dev
->mtu
= nla_get_u32(tb
[IFLA_MTU
]);
1031 if (tb
[IFLA_ADDRESS
])
1032 memcpy(dev
->dev_addr
, nla_data(tb
[IFLA_ADDRESS
]),
1033 nla_len(tb
[IFLA_ADDRESS
]));
1034 if (tb
[IFLA_BROADCAST
])
1035 memcpy(dev
->broadcast
, nla_data(tb
[IFLA_BROADCAST
]),
1036 nla_len(tb
[IFLA_BROADCAST
]));
1037 if (tb
[IFLA_TXQLEN
])
1038 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
1039 if (tb
[IFLA_OPERSTATE
])
1040 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
1041 if (tb
[IFLA_LINKMODE
])
1042 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
1049 return ERR_PTR(err
);
1051 EXPORT_SYMBOL(rtnl_create_link
);
1053 static int rtnl_newlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
1055 struct net
*net
= sock_net(skb
->sk
);
1056 const struct rtnl_link_ops
*ops
;
1057 struct net_device
*dev
;
1058 struct ifinfomsg
*ifm
;
1059 char kind
[MODULE_NAME_LEN
];
1060 char ifname
[IFNAMSIZ
];
1061 struct nlattr
*tb
[IFLA_MAX
+1];
1062 struct nlattr
*linkinfo
[IFLA_INFO_MAX
+1];
1065 #ifdef CONFIG_MODULES
1068 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1072 if (tb
[IFLA_IFNAME
])
1073 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1077 ifm
= nlmsg_data(nlh
);
1078 if (ifm
->ifi_index
> 0)
1079 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1081 dev
= __dev_get_by_name(net
, ifname
);
1085 err
= validate_linkmsg(dev
, tb
);
1089 if (tb
[IFLA_LINKINFO
]) {
1090 err
= nla_parse_nested(linkinfo
, IFLA_INFO_MAX
,
1091 tb
[IFLA_LINKINFO
], ifla_info_policy
);
1095 memset(linkinfo
, 0, sizeof(linkinfo
));
1097 if (linkinfo
[IFLA_INFO_KIND
]) {
1098 nla_strlcpy(kind
, linkinfo
[IFLA_INFO_KIND
], sizeof(kind
));
1099 ops
= rtnl_link_ops_get(kind
);
1106 struct nlattr
*attr
[ops
? ops
->maxtype
+ 1 : 0], **data
= NULL
;
1107 struct net
*dest_net
;
1110 if (ops
->maxtype
&& linkinfo
[IFLA_INFO_DATA
]) {
1111 err
= nla_parse_nested(attr
, ops
->maxtype
,
1112 linkinfo
[IFLA_INFO_DATA
],
1118 if (ops
->validate
) {
1119 err
= ops
->validate(tb
, data
);
1128 if (nlh
->nlmsg_flags
& NLM_F_EXCL
)
1130 if (nlh
->nlmsg_flags
& NLM_F_REPLACE
)
1133 if (linkinfo
[IFLA_INFO_DATA
]) {
1134 if (!ops
|| ops
!= dev
->rtnl_link_ops
||
1138 err
= ops
->changelink(dev
, tb
, data
);
1144 return do_setlink(dev
, ifm
, tb
, ifname
, modified
);
1147 if (!(nlh
->nlmsg_flags
& NLM_F_CREATE
))
1150 if (ifm
->ifi_index
|| ifm
->ifi_flags
|| ifm
->ifi_change
)
1152 if (tb
[IFLA_MAP
] || tb
[IFLA_MASTER
] || tb
[IFLA_PROTINFO
])
1156 #ifdef CONFIG_MODULES
1159 request_module("rtnl-link-%s", kind
);
1161 ops
= rtnl_link_ops_get(kind
);
1170 snprintf(ifname
, IFNAMSIZ
, "%s%%d", ops
->kind
);
1172 dest_net
= rtnl_link_get_net(net
, tb
);
1173 dev
= rtnl_create_link(net
, dest_net
, ifname
, ops
, tb
);
1177 else if (ops
->newlink
)
1178 err
= ops
->newlink(net
, dev
, tb
, data
);
1180 err
= register_netdevice(dev
);
1181 if (err
< 0 && !IS_ERR(dev
))
1189 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
1191 struct net
*net
= sock_net(skb
->sk
);
1192 struct ifinfomsg
*ifm
;
1193 char ifname
[IFNAMSIZ
];
1194 struct nlattr
*tb
[IFLA_MAX
+1];
1195 struct net_device
*dev
= NULL
;
1196 struct sk_buff
*nskb
;
1199 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
1203 if (tb
[IFLA_IFNAME
])
1204 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
1206 ifm
= nlmsg_data(nlh
);
1207 if (ifm
->ifi_index
> 0)
1208 dev
= __dev_get_by_index(net
, ifm
->ifi_index
);
1209 else if (tb
[IFLA_IFNAME
])
1210 dev
= __dev_get_by_name(net
, ifname
);
1217 nskb
= nlmsg_new(if_nlmsg_size(dev
), GFP_KERNEL
);
1221 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).pid
,
1222 nlh
->nlmsg_seq
, 0, 0);
1224 /* -EMSGSIZE implies BUG in if_nlmsg_size */
1225 WARN_ON(err
== -EMSGSIZE
);
1228 err
= rtnl_unicast(nskb
, net
, NETLINK_CB(skb
).pid
);
1233 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1236 int s_idx
= cb
->family
;
1240 for (idx
= 1; idx
< NPROTO
; idx
++) {
1241 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
1242 if (idx
< s_idx
|| idx
== PF_PACKET
)
1244 if (rtnl_msg_handlers
[idx
] == NULL
||
1245 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
1248 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
1249 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
1257 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
1259 struct net
*net
= dev_net(dev
);
1260 struct sk_buff
*skb
;
1263 skb
= nlmsg_new(if_nlmsg_size(dev
), GFP_KERNEL
);
1267 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0);
1269 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
1270 WARN_ON(err
== -EMSGSIZE
);
1274 rtnl_notify(skb
, net
, 0, RTNLGRP_LINK
, NULL
, GFP_KERNEL
);
1278 rtnl_set_sk_err(net
, RTNLGRP_LINK
, err
);
1281 /* Protected by RTNL sempahore. */
1282 static struct rtattr
**rta_buf
;
1283 static int rtattr_max
;
1285 /* Process one rtnetlink message. */
1287 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1289 struct net
*net
= sock_net(skb
->sk
);
1290 rtnl_doit_func doit
;
1297 type
= nlh
->nlmsg_type
;
1303 /* All the messages must have at least 1 byte length */
1304 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
1307 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
1308 if (family
>= NPROTO
)
1309 return -EAFNOSUPPORT
;
1314 if (kind
!= 2 && security_netlink_recv(skb
, CAP_NET_ADMIN
))
1317 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
1319 rtnl_dumpit_func dumpit
;
1321 dumpit
= rtnl_get_dumpit(family
, type
);
1327 err
= netlink_dump_start(rtnl
, skb
, nlh
, dumpit
, NULL
);
1332 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
1334 min_len
= rtm_min
[sz_idx
];
1335 if (nlh
->nlmsg_len
< min_len
)
1338 if (nlh
->nlmsg_len
> min_len
) {
1339 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
1340 struct rtattr
*attr
= (void *)nlh
+ NLMSG_ALIGN(min_len
);
1342 while (RTA_OK(attr
, attrlen
)) {
1343 unsigned flavor
= attr
->rta_type
;
1345 if (flavor
> rta_max
[sz_idx
])
1347 rta_buf
[flavor
-1] = attr
;
1349 attr
= RTA_NEXT(attr
, attrlen
);
1353 doit
= rtnl_get_doit(family
, type
);
1357 return doit(skb
, nlh
, (void *)&rta_buf
[0]);
1360 static void rtnetlink_rcv(struct sk_buff
*skb
)
1363 netlink_rcv_skb(skb
, &rtnetlink_rcv_msg
);
1367 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
1369 struct net_device
*dev
= ptr
;
1372 case NETDEV_UNREGISTER
:
1373 rtmsg_ifinfo(RTM_DELLINK
, dev
, ~0U);
1377 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
);
1379 case NETDEV_POST_INIT
:
1380 case NETDEV_REGISTER
:
1382 case NETDEV_GOING_DOWN
:
1383 case NETDEV_UNREGISTER_BATCH
:
1386 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
1392 static struct notifier_block rtnetlink_dev_notifier
= {
1393 .notifier_call
= rtnetlink_event
,
1397 static int rtnetlink_net_init(struct net
*net
)
1400 sk
= netlink_kernel_create(net
, NETLINK_ROUTE
, RTNLGRP_MAX
,
1401 rtnetlink_rcv
, &rtnl_mutex
, THIS_MODULE
);
1408 static void rtnetlink_net_exit(struct net
*net
)
1410 netlink_kernel_release(net
->rtnl
);
1414 static struct pernet_operations rtnetlink_net_ops
= {
1415 .init
= rtnetlink_net_init
,
1416 .exit
= rtnetlink_net_exit
,
1419 void __init
rtnetlink_init(void)
1424 for (i
= 0; i
< ARRAY_SIZE(rta_max
); i
++)
1425 if (rta_max
[i
] > rtattr_max
)
1426 rtattr_max
= rta_max
[i
];
1427 rta_buf
= kmalloc(rtattr_max
* sizeof(struct rtattr
*), GFP_KERNEL
);
1429 panic("rtnetlink_init: cannot allocate rta_buf\n");
1431 if (register_pernet_subsys(&rtnetlink_net_ops
))
1432 panic("rtnetlink_init: cannot initialize rtnetlink\n");
1434 netlink_set_nonroot(NETLINK_ROUTE
, NL_NONROOT_RECV
);
1435 register_netdevice_notifier(&rtnetlink_dev_notifier
);
1437 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
, rtnl_dump_ifinfo
);
1438 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
);
1439 rtnl_register(PF_UNSPEC
, RTM_NEWLINK
, rtnl_newlink
, NULL
);
1440 rtnl_register(PF_UNSPEC
, RTM_DELLINK
, rtnl_dellink
, NULL
);
1442 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
);
1443 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
);