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 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
;
182 EXPORT_SYMBOL_GPL(__rtnl_register
);
185 * rtnl_register - Register a rtnetlink message type
187 * Identical to __rtnl_register() but panics on failure. This is useful
188 * as failure of this function is very unlikely, it can only happen due
189 * to lack of memory when allocating the chain to store all message
190 * handlers for a protocol. Meant for use in init functions where lack
191 * of memory implies no sense in continueing.
193 void rtnl_register(int protocol
, int msgtype
,
194 rtnl_doit_func doit
, rtnl_dumpit_func dumpit
)
196 if (__rtnl_register(protocol
, msgtype
, doit
, dumpit
) < 0)
197 panic("Unable to register rtnetlink message handler, "
198 "protocol = %d, message type = %d\n",
202 EXPORT_SYMBOL_GPL(rtnl_register
);
205 * rtnl_unregister - Unregister a rtnetlink message type
206 * @protocol: Protocol family or PF_UNSPEC
207 * @msgtype: rtnetlink message type
209 * Returns 0 on success or a negative error code.
211 int rtnl_unregister(int protocol
, int msgtype
)
215 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
216 msgindex
= rtm_msgindex(msgtype
);
218 if (rtnl_msg_handlers
[protocol
] == NULL
)
221 rtnl_msg_handlers
[protocol
][msgindex
].doit
= NULL
;
222 rtnl_msg_handlers
[protocol
][msgindex
].dumpit
= NULL
;
227 EXPORT_SYMBOL_GPL(rtnl_unregister
);
230 * rtnl_unregister_all - Unregister all rtnetlink message type of a protocol
231 * @protocol : Protocol family or PF_UNSPEC
233 * Identical to calling rtnl_unregster() for all registered message types
234 * of a certain protocol family.
236 void rtnl_unregister_all(int protocol
)
238 BUG_ON(protocol
< 0 || protocol
>= NPROTO
);
240 kfree(rtnl_msg_handlers
[protocol
]);
241 rtnl_msg_handlers
[protocol
] = NULL
;
244 EXPORT_SYMBOL_GPL(rtnl_unregister_all
);
246 static const int rtm_min
[RTM_NR_FAMILIES
] =
248 [RTM_FAM(RTM_NEWLINK
)] = NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
249 [RTM_FAM(RTM_NEWADDR
)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
250 [RTM_FAM(RTM_NEWROUTE
)] = NLMSG_LENGTH(sizeof(struct rtmsg
)),
251 [RTM_FAM(RTM_NEWRULE
)] = NLMSG_LENGTH(sizeof(struct fib_rule_hdr
)),
252 [RTM_FAM(RTM_NEWQDISC
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
253 [RTM_FAM(RTM_NEWTCLASS
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
254 [RTM_FAM(RTM_NEWTFILTER
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
255 [RTM_FAM(RTM_NEWACTION
)] = NLMSG_LENGTH(sizeof(struct tcamsg
)),
256 [RTM_FAM(RTM_GETMULTICAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
257 [RTM_FAM(RTM_GETANYCAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
260 static const int rta_max
[RTM_NR_FAMILIES
] =
262 [RTM_FAM(RTM_NEWLINK
)] = IFLA_MAX
,
263 [RTM_FAM(RTM_NEWADDR
)] = IFA_MAX
,
264 [RTM_FAM(RTM_NEWROUTE
)] = RTA_MAX
,
265 [RTM_FAM(RTM_NEWRULE
)] = FRA_MAX
,
266 [RTM_FAM(RTM_NEWQDISC
)] = TCA_MAX
,
267 [RTM_FAM(RTM_NEWTCLASS
)] = TCA_MAX
,
268 [RTM_FAM(RTM_NEWTFILTER
)] = TCA_MAX
,
269 [RTM_FAM(RTM_NEWACTION
)] = TCAA_MAX
,
272 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
275 int size
= RTA_LENGTH(attrlen
);
277 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
278 rta
->rta_type
= attrtype
;
280 memcpy(RTA_DATA(rta
), data
, attrlen
);
281 memset(RTA_DATA(rta
) + attrlen
, 0, RTA_ALIGN(size
) - size
);
284 size_t rtattr_strlcpy(char *dest
, const struct rtattr
*rta
, size_t size
)
286 size_t ret
= RTA_PAYLOAD(rta
);
287 char *src
= RTA_DATA(rta
);
289 if (ret
> 0 && src
[ret
- 1] == '\0')
292 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
293 memset(dest
, 0, size
);
294 memcpy(dest
, src
, len
);
299 int rtnetlink_send(struct sk_buff
*skb
, u32 pid
, unsigned group
, int echo
)
303 NETLINK_CB(skb
).dst_group
= group
;
305 atomic_inc(&skb
->users
);
306 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
308 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
312 int rtnl_unicast(struct sk_buff
*skb
, u32 pid
)
314 return nlmsg_unicast(rtnl
, skb
, pid
);
317 int rtnl_notify(struct sk_buff
*skb
, u32 pid
, u32 group
,
318 struct nlmsghdr
*nlh
, gfp_t flags
)
323 report
= nlmsg_report(nlh
);
325 return nlmsg_notify(rtnl
, skb
, pid
, group
, report
, flags
);
328 void rtnl_set_sk_err(u32 group
, int error
)
330 netlink_set_err(rtnl
, 0, group
, error
);
333 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
338 mx
= nla_nest_start(skb
, RTA_METRICS
);
342 for (i
= 0; i
< RTAX_MAX
; i
++) {
345 NLA_PUT_U32(skb
, i
+1, metrics
[i
]);
350 nla_nest_cancel(skb
, mx
);
354 return nla_nest_end(skb
, mx
);
357 return nla_nest_cancel(skb
, mx
);
360 int rtnl_put_cacheinfo(struct sk_buff
*skb
, struct dst_entry
*dst
, u32 id
,
361 u32 ts
, u32 tsage
, long expires
, u32 error
)
363 struct rta_cacheinfo ci
= {
364 .rta_lastuse
= jiffies_to_clock_t(jiffies
- dst
->lastuse
),
365 .rta_used
= dst
->__use
,
366 .rta_clntref
= atomic_read(&(dst
->__refcnt
)),
374 ci
.rta_expires
= jiffies_to_clock_t(expires
);
376 return nla_put(skb
, RTA_CACHEINFO
, sizeof(ci
), &ci
);
379 EXPORT_SYMBOL_GPL(rtnl_put_cacheinfo
);
381 static void set_operstate(struct net_device
*dev
, unsigned char transition
)
383 unsigned char operstate
= dev
->operstate
;
387 if ((operstate
== IF_OPER_DORMANT
||
388 operstate
== IF_OPER_UNKNOWN
) &&
390 operstate
= IF_OPER_UP
;
393 case IF_OPER_DORMANT
:
394 if (operstate
== IF_OPER_UP
||
395 operstate
== IF_OPER_UNKNOWN
)
396 operstate
= IF_OPER_DORMANT
;
400 if (dev
->operstate
!= operstate
) {
401 write_lock_bh(&dev_base_lock
);
402 dev
->operstate
= operstate
;
403 write_unlock_bh(&dev_base_lock
);
404 netdev_state_change(dev
);
408 static void copy_rtnl_link_stats(struct rtnl_link_stats
*a
,
409 struct net_device_stats
*b
)
411 a
->rx_packets
= b
->rx_packets
;
412 a
->tx_packets
= b
->tx_packets
;
413 a
->rx_bytes
= b
->rx_bytes
;
414 a
->tx_bytes
= b
->tx_bytes
;
415 a
->rx_errors
= b
->rx_errors
;
416 a
->tx_errors
= b
->tx_errors
;
417 a
->rx_dropped
= b
->rx_dropped
;
418 a
->tx_dropped
= b
->tx_dropped
;
420 a
->multicast
= b
->multicast
;
421 a
->collisions
= b
->collisions
;
423 a
->rx_length_errors
= b
->rx_length_errors
;
424 a
->rx_over_errors
= b
->rx_over_errors
;
425 a
->rx_crc_errors
= b
->rx_crc_errors
;
426 a
->rx_frame_errors
= b
->rx_frame_errors
;
427 a
->rx_fifo_errors
= b
->rx_fifo_errors
;
428 a
->rx_missed_errors
= b
->rx_missed_errors
;
430 a
->tx_aborted_errors
= b
->tx_aborted_errors
;
431 a
->tx_carrier_errors
= b
->tx_carrier_errors
;
432 a
->tx_fifo_errors
= b
->tx_fifo_errors
;
433 a
->tx_heartbeat_errors
= b
->tx_heartbeat_errors
;
434 a
->tx_window_errors
= b
->tx_window_errors
;
436 a
->rx_compressed
= b
->rx_compressed
;
437 a
->tx_compressed
= b
->tx_compressed
;
440 static inline size_t if_nlmsg_size(void)
442 return NLMSG_ALIGN(sizeof(struct ifinfomsg
))
443 + nla_total_size(IFNAMSIZ
) /* IFLA_IFNAME */
444 + nla_total_size(IFNAMSIZ
) /* IFLA_QDISC */
445 + nla_total_size(sizeof(struct rtnl_link_ifmap
))
446 + nla_total_size(sizeof(struct rtnl_link_stats
))
447 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_ADDRESS */
448 + nla_total_size(MAX_ADDR_LEN
) /* IFLA_BROADCAST */
449 + nla_total_size(4) /* IFLA_TXQLEN */
450 + nla_total_size(4) /* IFLA_WEIGHT */
451 + nla_total_size(4) /* IFLA_MTU */
452 + nla_total_size(4) /* IFLA_LINK */
453 + nla_total_size(4) /* IFLA_MASTER */
454 + nla_total_size(1) /* IFLA_OPERSTATE */
455 + nla_total_size(1); /* IFLA_LINKMODE */
458 static int rtnl_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
459 int type
, u32 pid
, u32 seq
, u32 change
,
462 struct ifinfomsg
*ifm
;
463 struct nlmsghdr
*nlh
;
465 nlh
= nlmsg_put(skb
, pid
, seq
, type
, sizeof(*ifm
), flags
);
469 ifm
= nlmsg_data(nlh
);
470 ifm
->ifi_family
= AF_UNSPEC
;
472 ifm
->ifi_type
= dev
->type
;
473 ifm
->ifi_index
= dev
->ifindex
;
474 ifm
->ifi_flags
= dev_get_flags(dev
);
475 ifm
->ifi_change
= change
;
477 NLA_PUT_STRING(skb
, IFLA_IFNAME
, dev
->name
);
478 NLA_PUT_U32(skb
, IFLA_TXQLEN
, dev
->tx_queue_len
);
479 NLA_PUT_U32(skb
, IFLA_WEIGHT
, dev
->weight
);
480 NLA_PUT_U8(skb
, IFLA_OPERSTATE
,
481 netif_running(dev
) ? dev
->operstate
: IF_OPER_DOWN
);
482 NLA_PUT_U8(skb
, IFLA_LINKMODE
, dev
->link_mode
);
483 NLA_PUT_U32(skb
, IFLA_MTU
, dev
->mtu
);
485 if (dev
->ifindex
!= dev
->iflink
)
486 NLA_PUT_U32(skb
, IFLA_LINK
, dev
->iflink
);
489 NLA_PUT_U32(skb
, IFLA_MASTER
, dev
->master
->ifindex
);
491 if (dev
->qdisc_sleeping
)
492 NLA_PUT_STRING(skb
, IFLA_QDISC
, dev
->qdisc_sleeping
->ops
->id
);
495 struct rtnl_link_ifmap map
= {
496 .mem_start
= dev
->mem_start
,
497 .mem_end
= dev
->mem_end
,
498 .base_addr
= dev
->base_addr
,
501 .port
= dev
->if_port
,
503 NLA_PUT(skb
, IFLA_MAP
, sizeof(map
), &map
);
507 NLA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
508 NLA_PUT(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
);
511 if (dev
->get_stats
) {
512 struct net_device_stats
*stats
= dev
->get_stats(dev
);
516 attr
= nla_reserve(skb
, IFLA_STATS
,
517 sizeof(struct rtnl_link_stats
));
519 goto nla_put_failure
;
521 copy_rtnl_link_stats(nla_data(attr
), stats
);
525 return nlmsg_end(skb
, nlh
);
528 nlmsg_cancel(skb
, nlh
);
532 static int rtnl_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
535 int s_idx
= cb
->args
[0];
536 struct net_device
*dev
;
539 for_each_netdev(dev
) {
542 if (rtnl_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
543 NETLINK_CB(cb
->skb
).pid
,
544 cb
->nlh
->nlmsg_seq
, 0, NLM_F_MULTI
) <= 0)
554 static const struct nla_policy ifla_policy
[IFLA_MAX
+1] = {
555 [IFLA_IFNAME
] = { .type
= NLA_STRING
, .len
= IFNAMSIZ
-1 },
556 [IFLA_MAP
] = { .len
= sizeof(struct rtnl_link_ifmap
) },
557 [IFLA_MTU
] = { .type
= NLA_U32
},
558 [IFLA_TXQLEN
] = { .type
= NLA_U32
},
559 [IFLA_WEIGHT
] = { .type
= NLA_U32
},
560 [IFLA_OPERSTATE
] = { .type
= NLA_U8
},
561 [IFLA_LINKMODE
] = { .type
= NLA_U8
},
564 static int do_setlink(struct net_device
*dev
, struct ifinfomsg
*ifm
,
565 struct nlattr
**tb
, char *ifname
)
567 int modified
= 0, send_addr_notify
= 0;
571 struct rtnl_link_ifmap
*u_map
;
574 if (!dev
->set_config
) {
579 if (!netif_device_present(dev
)) {
584 u_map
= nla_data(tb
[IFLA_MAP
]);
585 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
586 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
587 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
588 k_map
.irq
= (unsigned char) u_map
->irq
;
589 k_map
.dma
= (unsigned char) u_map
->dma
;
590 k_map
.port
= (unsigned char) u_map
->port
;
592 err
= dev
->set_config(dev
, &k_map
);
599 if (tb
[IFLA_ADDRESS
]) {
603 if (!dev
->set_mac_address
) {
608 if (!netif_device_present(dev
)) {
613 len
= sizeof(sa_family_t
) + dev
->addr_len
;
614 sa
= kmalloc(len
, GFP_KERNEL
);
619 sa
->sa_family
= dev
->type
;
620 memcpy(sa
->sa_data
, nla_data(tb
[IFLA_ADDRESS
]),
622 err
= dev
->set_mac_address(dev
, sa
);
626 send_addr_notify
= 1;
631 err
= dev_set_mtu(dev
, nla_get_u32(tb
[IFLA_MTU
]));
638 * Interface selected by interface index but interface
639 * name provided implies that a name change has been
642 if (ifm
->ifi_index
> 0 && ifname
[0]) {
643 err
= dev_change_name(dev
, ifname
);
649 if (tb
[IFLA_BROADCAST
]) {
650 nla_memcpy(dev
->broadcast
, tb
[IFLA_BROADCAST
], dev
->addr_len
);
651 send_addr_notify
= 1;
654 if (ifm
->ifi_flags
|| ifm
->ifi_change
) {
655 unsigned int flags
= ifm
->ifi_flags
;
657 /* bugwards compatibility: ifi_change == 0 is treated as ~0 */
659 flags
= (flags
& ifm
->ifi_change
) |
660 (dev
->flags
& ~ifm
->ifi_change
);
661 dev_change_flags(dev
, flags
);
665 dev
->tx_queue_len
= nla_get_u32(tb
[IFLA_TXQLEN
]);
668 dev
->weight
= nla_get_u32(tb
[IFLA_WEIGHT
]);
670 if (tb
[IFLA_OPERSTATE
])
671 set_operstate(dev
, nla_get_u8(tb
[IFLA_OPERSTATE
]));
673 if (tb
[IFLA_LINKMODE
]) {
674 write_lock_bh(&dev_base_lock
);
675 dev
->link_mode
= nla_get_u8(tb
[IFLA_LINKMODE
]);
676 write_unlock_bh(&dev_base_lock
);
682 if (err
< 0 && modified
&& net_ratelimit())
683 printk(KERN_WARNING
"A link change request failed with "
684 "some changes comitted already. Interface %s may "
685 "have been left with an inconsistent configuration, "
686 "please check.\n", dev
->name
);
688 if (send_addr_notify
)
689 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
693 static int rtnl_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
695 struct ifinfomsg
*ifm
;
696 struct net_device
*dev
;
698 struct nlattr
*tb
[IFLA_MAX
+1];
699 char ifname
[IFNAMSIZ
];
701 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
706 nla_strlcpy(ifname
, tb
[IFLA_IFNAME
], IFNAMSIZ
);
711 ifm
= nlmsg_data(nlh
);
712 if (ifm
->ifi_index
> 0)
713 dev
= dev_get_by_index(ifm
->ifi_index
);
714 else if (tb
[IFLA_IFNAME
])
715 dev
= dev_get_by_name(ifname
);
724 if (tb
[IFLA_ADDRESS
] &&
725 nla_len(tb
[IFLA_ADDRESS
]) < dev
->addr_len
)
728 if (tb
[IFLA_BROADCAST
] &&
729 nla_len(tb
[IFLA_BROADCAST
]) < dev
->addr_len
)
732 err
= do_setlink(dev
, ifm
, tb
, ifname
);
739 static int rtnl_getlink(struct sk_buff
*skb
, struct nlmsghdr
* nlh
, void *arg
)
741 struct ifinfomsg
*ifm
;
742 struct nlattr
*tb
[IFLA_MAX
+1];
743 struct net_device
*dev
= NULL
;
744 struct sk_buff
*nskb
;
747 err
= nlmsg_parse(nlh
, sizeof(*ifm
), tb
, IFLA_MAX
, ifla_policy
);
751 ifm
= nlmsg_data(nlh
);
752 if (ifm
->ifi_index
> 0) {
753 dev
= dev_get_by_index(ifm
->ifi_index
);
759 nskb
= nlmsg_new(if_nlmsg_size(), GFP_KERNEL
);
765 err
= rtnl_fill_ifinfo(nskb
, dev
, RTM_NEWLINK
, NETLINK_CB(skb
).pid
,
766 nlh
->nlmsg_seq
, 0, 0);
768 /* -EMSGSIZE implies BUG in if_nlmsg_size */
769 WARN_ON(err
== -EMSGSIZE
);
773 err
= rtnl_unicast(nskb
, NETLINK_CB(skb
).pid
);
780 static int rtnl_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
783 int s_idx
= cb
->family
;
787 for (idx
=1; idx
<NPROTO
; idx
++) {
788 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
789 if (idx
< s_idx
|| idx
== PF_PACKET
)
791 if (rtnl_msg_handlers
[idx
] == NULL
||
792 rtnl_msg_handlers
[idx
][type
].dumpit
== NULL
)
795 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
796 if (rtnl_msg_handlers
[idx
][type
].dumpit(skb
, cb
))
804 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
809 skb
= nlmsg_new(if_nlmsg_size(), GFP_KERNEL
);
813 err
= rtnl_fill_ifinfo(skb
, dev
, type
, 0, 0, change
, 0);
815 /* -EMSGSIZE implies BUG in if_nlmsg_size() */
816 WARN_ON(err
== -EMSGSIZE
);
820 err
= rtnl_notify(skb
, 0, RTNLGRP_LINK
, NULL
, GFP_KERNEL
);
823 rtnl_set_sk_err(RTNLGRP_LINK
, err
);
826 /* Protected by RTNL sempahore. */
827 static struct rtattr
**rta_buf
;
828 static int rtattr_max
;
830 /* Process one rtnetlink message. */
832 static int rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
841 type
= nlh
->nlmsg_type
;
847 /* All the messages must have at least 1 byte length */
848 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
851 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
852 if (family
>= NPROTO
)
853 return -EAFNOSUPPORT
;
858 if (kind
!= 2 && security_netlink_recv(skb
, CAP_NET_ADMIN
))
861 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
862 rtnl_dumpit_func dumpit
;
864 dumpit
= rtnl_get_dumpit(family
, type
);
869 err
= netlink_dump_start(rtnl
, skb
, nlh
, dumpit
, NULL
);
874 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
876 min_len
= rtm_min
[sz_idx
];
877 if (nlh
->nlmsg_len
< min_len
)
880 if (nlh
->nlmsg_len
> min_len
) {
881 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
882 struct rtattr
*attr
= (void*)nlh
+ NLMSG_ALIGN(min_len
);
884 while (RTA_OK(attr
, attrlen
)) {
885 unsigned flavor
= attr
->rta_type
;
887 if (flavor
> rta_max
[sz_idx
])
889 rta_buf
[flavor
-1] = attr
;
891 attr
= RTA_NEXT(attr
, attrlen
);
895 doit
= rtnl_get_doit(family
, type
);
899 return doit(skb
, nlh
, (void *)&rta_buf
[0]);
902 static void rtnetlink_rcv(struct sock
*sk
, int len
)
904 unsigned int qlen
= 0;
907 mutex_lock(&rtnl_mutex
);
908 netlink_run_queue(sk
, &qlen
, &rtnetlink_rcv_msg
);
909 mutex_unlock(&rtnl_mutex
);
915 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
917 struct net_device
*dev
= ptr
;
919 case NETDEV_UNREGISTER
:
920 rtmsg_ifinfo(RTM_DELLINK
, dev
, ~0U);
922 case NETDEV_REGISTER
:
923 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U);
927 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
);
930 case NETDEV_GOING_DOWN
:
933 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
939 static struct notifier_block rtnetlink_dev_notifier
= {
940 .notifier_call
= rtnetlink_event
,
943 void __init
rtnetlink_init(void)
948 for (i
= 0; i
< ARRAY_SIZE(rta_max
); i
++)
949 if (rta_max
[i
] > rtattr_max
)
950 rtattr_max
= rta_max
[i
];
951 rta_buf
= kmalloc(rtattr_max
* sizeof(struct rtattr
*), GFP_KERNEL
);
953 panic("rtnetlink_init: cannot allocate rta_buf\n");
955 rtnl
= netlink_kernel_create(NETLINK_ROUTE
, RTNLGRP_MAX
, rtnetlink_rcv
,
956 &rtnl_mutex
, THIS_MODULE
);
958 panic("rtnetlink_init: cannot initialize rtnetlink\n");
959 netlink_set_nonroot(NETLINK_ROUTE
, NL_NONROOT_RECV
);
960 register_netdevice_notifier(&rtnetlink_dev_notifier
);
962 rtnl_register(PF_UNSPEC
, RTM_GETLINK
, rtnl_getlink
, rtnl_dump_ifinfo
);
963 rtnl_register(PF_UNSPEC
, RTM_SETLINK
, rtnl_setlink
, NULL
);
965 rtnl_register(PF_UNSPEC
, RTM_GETADDR
, NULL
, rtnl_dump_all
);
966 rtnl_register(PF_UNSPEC
, RTM_GETROUTE
, NULL
, rtnl_dump_all
);
969 EXPORT_SYMBOL(__rta_fill
);
970 EXPORT_SYMBOL(rtattr_strlcpy
);
971 EXPORT_SYMBOL(rtattr_parse
);
972 EXPORT_SYMBOL(rtnetlink_put_metrics
);
973 EXPORT_SYMBOL(rtnl_lock
);
974 EXPORT_SYMBOL(rtnl_trylock
);
975 EXPORT_SYMBOL(rtnl_unlock
);
976 EXPORT_SYMBOL(rtnl_unicast
);
977 EXPORT_SYMBOL(rtnl_notify
);
978 EXPORT_SYMBOL(rtnl_set_sk_err
);