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/config.h>
20 #include <linux/errno.h>
21 #include <linux/types.h>
22 #include <linux/socket.h>
23 #include <linux/kernel.h>
24 #include <linux/major.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/string.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
30 #include <linux/fcntl.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/capability.h>
35 #include <linux/skbuff.h>
36 #include <linux/init.h>
38 #include <asm/uaccess.h>
39 #include <asm/system.h>
40 #include <asm/string.h>
42 #include <linux/inet.h>
43 #include <linux/netdevice.h>
45 #include <net/protocol.h>
47 #include <net/route.h>
51 #include <net/pkt_sched.h>
53 DECLARE_MUTEX(rtnl_sem
);
61 void rtnl_unlock(void)
67 int rtattr_parse(struct rtattr
*tb
[], int maxattr
, struct rtattr
*rta
, int len
)
69 memset(tb
, 0, sizeof(struct rtattr
*)*maxattr
);
71 while (RTA_OK(rta
, len
)) {
72 unsigned flavor
= rta
->rta_type
;
73 if (flavor
&& flavor
<= maxattr
)
75 rta
= RTA_NEXT(rta
, len
);
80 #ifdef CONFIG_RTNETLINK
83 struct rtnetlink_link
* rtnetlink_links
[NPROTO
];
85 #define _S 1 /* superuser privileges required */
86 #define _X 2 /* exclusive access to tables required */
87 #define _G 4 /* GET request */
89 static const int rtm_min
[(RTM_MAX
+1-RTM_BASE
)/4] =
91 NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
92 NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
93 NLMSG_LENGTH(sizeof(struct rtmsg
)),
94 NLMSG_LENGTH(sizeof(struct ndmsg
)),
95 NLMSG_LENGTH(sizeof(struct rtmsg
)),
96 NLMSG_LENGTH(sizeof(struct tcmsg
)),
97 NLMSG_LENGTH(sizeof(struct tcmsg
)),
98 NLMSG_LENGTH(sizeof(struct tcmsg
))
101 static const int rta_max
[(RTM_MAX
+1-RTM_BASE
)/4] =
113 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
116 int size
= RTA_LENGTH(attrlen
);
118 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
119 rta
->rta_type
= attrtype
;
121 memcpy(RTA_DATA(rta
), data
, attrlen
);
124 int rtnetlink_send(struct sk_buff
*skb
, u32 pid
, unsigned group
, int echo
)
128 NETLINK_CB(skb
).dst_groups
= group
;
130 atomic_inc(&skb
->users
);
131 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
133 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
137 int rtnetlink_put_metrics(struct sk_buff
*skb
, unsigned *metrics
)
139 struct rtattr
*mx
= (struct rtattr
*)skb
->tail
;
142 RTA_PUT(skb
, RTA_METRICS
, 0, NULL
);
143 for (i
=0; i
<RTAX_MAX
; i
++) {
145 RTA_PUT(skb
, i
+1, sizeof(unsigned), metrics
+i
);
147 mx
->rta_len
= skb
->tail
- (u8
*)mx
;
148 if (mx
->rta_len
== RTA_LENGTH(0))
149 skb_trim(skb
, (u8
*)mx
- skb
->data
);
153 skb_trim(skb
, (u8
*)mx
- skb
->data
);
158 static int rtnetlink_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
159 int type
, u32 pid
, u32 seq
, u32 change
)
162 struct nlmsghdr
*nlh
;
163 unsigned char *b
= skb
->tail
;
165 nlh
= NLMSG_PUT(skb
, pid
, seq
, type
, sizeof(*r
));
166 if (pid
) nlh
->nlmsg_flags
|= NLM_F_MULTI
;
168 r
->ifi_family
= AF_UNSPEC
;
169 r
->ifi_type
= dev
->type
;
170 r
->ifi_index
= dev
->ifindex
;
171 r
->ifi_flags
= dev
->flags
;
172 r
->ifi_change
= change
;
174 RTA_PUT(skb
, IFLA_IFNAME
, strlen(dev
->name
)+1, dev
->name
);
176 RTA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
177 RTA_PUT(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
);
180 unsigned mtu
= dev
->mtu
;
181 RTA_PUT(skb
, IFLA_MTU
, sizeof(mtu
), &mtu
);
183 if (dev
->ifindex
!= dev
->iflink
)
184 RTA_PUT(skb
, IFLA_LINK
, sizeof(int), &dev
->iflink
);
185 if (dev
->qdisc_sleeping
)
186 RTA_PUT(skb
, IFLA_QDISC
,
187 strlen(dev
->qdisc_sleeping
->ops
->id
) + 1,
188 dev
->qdisc_sleeping
->ops
->id
);
189 if (dev
->get_stats
) {
190 struct net_device_stats
*stats
= dev
->get_stats(dev
);
192 RTA_PUT(skb
, IFLA_STATS
, sizeof(*stats
), stats
);
194 nlh
->nlmsg_len
= skb
->tail
- b
;
199 skb_trim(skb
, b
- skb
->data
);
203 int rtnetlink_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
206 int s_idx
= cb
->args
[0];
207 struct net_device
*dev
;
209 read_lock(&dev_base_lock
);
210 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
213 if (rtnetlink_fill_ifinfo(skb
, dev
, RTM_NEWLINK
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
, 0) <= 0)
216 read_unlock(&dev_base_lock
);
222 int rtnetlink_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
225 int s_idx
= cb
->family
;
229 for (idx
=1; idx
<NPROTO
; idx
++) {
230 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
231 if (idx
< s_idx
|| idx
== PF_PACKET
)
233 if (rtnetlink_links
[idx
] == NULL
||
234 rtnetlink_links
[idx
][type
].dumpit
== NULL
)
237 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
238 if (rtnetlink_links
[idx
][type
].dumpit(skb
, cb
))
246 void rtmsg_ifinfo(int type
, struct net_device
*dev
)
249 int size
= NLMSG_GOODSIZE
;
251 skb
= alloc_skb(size
, GFP_KERNEL
);
255 if (rtnetlink_fill_ifinfo(skb
, dev
, type
, 0, 0, ~0U) < 0) {
259 NETLINK_CB(skb
).dst_groups
= RTMGRP_LINK
;
260 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_LINK
, GFP_KERNEL
);
263 static int rtnetlink_done(struct netlink_callback
*cb
)
268 /* Process one rtnetlink message. */
270 extern __inline__
int
271 rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
273 struct rtnetlink_link
*link
;
274 struct rtnetlink_link
*link_tab
;
275 struct rtattr
*rta
[RTATTR_MAX
];
284 /* Only requests are handled by kernel now */
285 if (!(nlh
->nlmsg_flags
&NLM_F_REQUEST
))
288 type
= nlh
->nlmsg_type
;
290 /* A control message: ignore them */
294 /* Unknown message: reply with EINVAL */
300 /* All the messages must have at least 1 byte length */
301 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
304 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
305 if (family
> NPROTO
) {
306 *errp
= -EAFNOSUPPORT
;
310 link_tab
= rtnetlink_links
[family
];
311 if (link_tab
== NULL
)
312 link_tab
= rtnetlink_links
[PF_UNSPEC
];
313 link
= &link_tab
[type
];
318 if (kind
!= 2 && !cap_raised(NETLINK_CB(skb
).eff_cap
, CAP_NET_ADMIN
)) {
323 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
326 if (link
->dumpit
== NULL
)
327 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
329 if (link
->dumpit
== NULL
)
332 if ((*errp
= netlink_dump_start(rtnl
, skb
, nlh
,
334 rtnetlink_done
)) != 0) {
337 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
345 if (rtnl_exlock_nowait()) {
352 memset(&rta
, 0, sizeof(rta
));
354 min_len
= rtm_min
[sz_idx
];
355 if (nlh
->nlmsg_len
< min_len
)
358 if (nlh
->nlmsg_len
> min_len
) {
359 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
360 struct rtattr
*attr
= (void*)nlh
+ NLMSG_ALIGN(min_len
);
362 while (RTA_OK(attr
, attrlen
)) {
363 unsigned flavor
= attr
->rta_type
;
365 if (flavor
> rta_max
[sz_idx
])
367 rta
[flavor
-1] = attr
;
369 attr
= RTA_NEXT(attr
, attrlen
);
373 if (link
->doit
== NULL
)
374 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
375 if (link
->doit
== NULL
)
377 err
= link
->doit(skb
, nlh
, (void *)&rta
);
392 * Process one packet of messages.
393 * Malformed skbs with wrong lengths of messages are discarded silently.
396 extern __inline__
int rtnetlink_rcv_skb(struct sk_buff
*skb
)
399 struct nlmsghdr
* nlh
;
401 while (skb
->len
>= NLMSG_SPACE(0)) {
404 nlh
= (struct nlmsghdr
*)skb
->data
;
405 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
407 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
410 if (rtnetlink_rcv_msg(skb
, nlh
, &err
)) {
411 /* Not error, but we must interrupt processing here:
412 * Note, that in this case we do not pull message
413 * from skb, it will be processed later.
417 netlink_ack(skb
, nlh
, err
);
418 } else if (nlh
->nlmsg_flags
&NLM_F_ACK
)
419 netlink_ack(skb
, nlh
, 0);
427 * rtnetlink input queue processing routine:
428 * - try to acquire shared lock. If it is failed, defer processing.
429 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
430 * that will occur, when a dump started and/or acquisition of
431 * exclusive lock failed.
434 static void rtnetlink_rcv(struct sock
*sk
, int len
)
439 if (rtnl_shlock_nowait())
442 while ((skb
= skb_dequeue(&sk
->receive_queue
)) != NULL
) {
443 if (rtnetlink_rcv_skb(skb
)) {
445 skb_queue_head(&sk
->receive_queue
, skb
);
454 } while (rtnl
&& rtnl
->receive_queue
.qlen
);
457 static struct rtnetlink_link link_rtnetlink_table
[RTM_MAX
-RTM_BASE
+1] =
461 { NULL
, rtnetlink_dump_ifinfo
, },
466 { NULL
, rtnetlink_dump_all
, },
471 { NULL
, rtnetlink_dump_all
, },
474 { neigh_add
, NULL
, },
475 { neigh_delete
, NULL
, },
476 { NULL
, neigh_dump_info
, },
486 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
488 struct net_device
*dev
= ptr
;
490 case NETDEV_UNREGISTER
:
491 rtmsg_ifinfo(RTM_DELLINK
, dev
);
494 rtmsg_ifinfo(RTM_NEWLINK
, dev
);
500 struct notifier_block rtnetlink_dev_notifier
= {
507 void __init
rtnetlink_init(void)
510 printk("Initializing RT netlink socket\n");
512 rtnl
= netlink_kernel_create(NETLINK_ROUTE
, rtnetlink_rcv
);
514 panic("rtnetlink_init: cannot initialize rtnetlink\n");
515 register_netdevice_notifier(&rtnetlink_dev_notifier
);
516 rtnetlink_links
[PF_UNSPEC
] = link_rtnetlink_table
;
517 rtnetlink_links
[PF_PACKET
] = link_rtnetlink_table
;