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/module.h>
22 #include <linux/types.h>
23 #include <linux/socket.h>
24 #include <linux/kernel.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>
37 #include <linux/security.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>
53 DECLARE_MUTEX(rtnl_sem
);
60 int rtnl_lock_interruptible(void)
62 return down_interruptible(&rtnl_sem
);
65 void rtnl_unlock(void)
72 int rtattr_parse(struct rtattr
*tb
[], int maxattr
, struct rtattr
*rta
, int len
)
74 memset(tb
, 0, sizeof(struct rtattr
*)*maxattr
);
76 while (RTA_OK(rta
, len
)) {
77 unsigned flavor
= rta
->rta_type
;
78 if (flavor
&& flavor
<= maxattr
)
80 rta
= RTA_NEXT(rta
, len
);
87 struct rtnetlink_link
* rtnetlink_links
[NPROTO
];
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
)),
99 NLMSG_LENGTH(sizeof(struct tcamsg
))
102 static const int rta_max
[(RTM_MAX
+1-RTM_BASE
)/4] =
115 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
118 int size
= RTA_LENGTH(attrlen
);
120 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
121 rta
->rta_type
= attrtype
;
123 memcpy(RTA_DATA(rta
), data
, attrlen
);
126 size_t rtattr_strlcpy(char *dest
, const struct rtattr
*rta
, size_t size
)
128 size_t ret
= RTA_PAYLOAD(rta
);
129 char *src
= RTA_DATA(rta
);
131 if (ret
> 0 && src
[ret
- 1] == '\0')
134 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
135 memset(dest
, 0, size
);
136 memcpy(dest
, src
, len
);
141 int rtnetlink_send(struct sk_buff
*skb
, u32 pid
, unsigned group
, int echo
)
145 NETLINK_CB(skb
).dst_groups
= group
;
147 atomic_inc(&skb
->users
);
148 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
150 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
154 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
156 struct rtattr
*mx
= (struct rtattr
*)skb
->tail
;
159 RTA_PUT(skb
, RTA_METRICS
, 0, NULL
);
160 for (i
=0; i
<RTAX_MAX
; i
++) {
162 RTA_PUT(skb
, i
+1, sizeof(u32
), metrics
+i
);
164 mx
->rta_len
= skb
->tail
- (u8
*)mx
;
165 if (mx
->rta_len
== RTA_LENGTH(0))
166 skb_trim(skb
, (u8
*)mx
- skb
->data
);
170 skb_trim(skb
, (u8
*)mx
- skb
->data
);
175 static int rtnetlink_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
176 int type
, u32 pid
, u32 seq
, u32 change
)
179 struct nlmsghdr
*nlh
;
180 unsigned char *b
= skb
->tail
;
182 nlh
= NLMSG_PUT(skb
, pid
, seq
, type
, sizeof(*r
));
183 if (pid
) nlh
->nlmsg_flags
|= NLM_F_MULTI
;
185 r
->ifi_family
= AF_UNSPEC
;
186 r
->ifi_type
= dev
->type
;
187 r
->ifi_index
= dev
->ifindex
;
188 r
->ifi_flags
= dev_get_flags(dev
);
189 r
->ifi_change
= change
;
191 RTA_PUT(skb
, IFLA_IFNAME
, strlen(dev
->name
)+1, dev
->name
);
194 u32 txqlen
= dev
->tx_queue_len
;
195 RTA_PUT(skb
, IFLA_TXQLEN
, sizeof(txqlen
), &txqlen
);
199 u32 weight
= dev
->weight
;
200 RTA_PUT(skb
, IFLA_WEIGHT
, sizeof(weight
), &weight
);
204 struct rtnl_link_ifmap map
= {
205 .mem_start
= dev
->mem_start
,
206 .mem_end
= dev
->mem_end
,
207 .base_addr
= dev
->base_addr
,
210 .port
= dev
->if_port
,
212 RTA_PUT(skb
, IFLA_MAP
, sizeof(map
), &map
);
216 RTA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
217 RTA_PUT(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
);
222 RTA_PUT(skb
, IFLA_MTU
, sizeof(mtu
), &mtu
);
225 if (dev
->ifindex
!= dev
->iflink
) {
226 u32 iflink
= dev
->iflink
;
227 RTA_PUT(skb
, IFLA_LINK
, sizeof(iflink
), &iflink
);
230 if (dev
->qdisc_sleeping
)
231 RTA_PUT(skb
, IFLA_QDISC
,
232 strlen(dev
->qdisc_sleeping
->ops
->id
) + 1,
233 dev
->qdisc_sleeping
->ops
->id
);
236 u32 master
= dev
->master
->ifindex
;
237 RTA_PUT(skb
, IFLA_MASTER
, sizeof(master
), &master
);
240 if (dev
->get_stats
) {
241 unsigned long *stats
= (unsigned long*)dev
->get_stats(dev
);
246 int n
= sizeof(struct rtnl_link_stats
)/4;
248 a
= __RTA_PUT(skb
, IFLA_STATS
, n
*4);
254 nlh
->nlmsg_len
= skb
->tail
- b
;
259 skb_trim(skb
, b
- skb
->data
);
263 static int rtnetlink_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
266 int s_idx
= cb
->args
[0];
267 struct net_device
*dev
;
269 read_lock(&dev_base_lock
);
270 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
273 if (rtnetlink_fill_ifinfo(skb
, dev
, RTM_NEWLINK
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
, 0) <= 0)
276 read_unlock(&dev_base_lock
);
282 static int do_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
284 struct ifinfomsg
*ifm
= NLMSG_DATA(nlh
);
285 struct rtattr
**ida
= arg
;
286 struct net_device
*dev
;
287 int err
, send_addr_notify
= 0;
289 if (ifm
->ifi_index
>= 0)
290 dev
= dev_get_by_index(ifm
->ifi_index
);
291 else if (ida
[IFLA_IFNAME
- 1]) {
292 char ifname
[IFNAMSIZ
];
294 if (rtattr_strlcpy(ifname
, ida
[IFLA_IFNAME
- 1],
295 IFNAMSIZ
) >= IFNAMSIZ
)
297 dev
= dev_get_by_name(ifname
);
307 dev_change_flags(dev
, ifm
->ifi_flags
);
309 if (ida
[IFLA_MAP
- 1]) {
310 struct rtnl_link_ifmap
*u_map
;
313 if (!dev
->set_config
) {
318 if (!netif_device_present(dev
)) {
323 if (ida
[IFLA_MAP
- 1]->rta_len
!= RTA_LENGTH(sizeof(*u_map
)))
326 u_map
= RTA_DATA(ida
[IFLA_MAP
- 1]);
328 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
329 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
330 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
331 k_map
.irq
= (unsigned char) u_map
->irq
;
332 k_map
.dma
= (unsigned char) u_map
->dma
;
333 k_map
.port
= (unsigned char) u_map
->port
;
335 err
= dev
->set_config(dev
, &k_map
);
341 if (ida
[IFLA_ADDRESS
- 1]) {
342 if (!dev
->set_mac_address
) {
346 if (!netif_device_present(dev
)) {
350 if (ida
[IFLA_ADDRESS
- 1]->rta_len
!= RTA_LENGTH(dev
->addr_len
))
353 err
= dev
->set_mac_address(dev
, RTA_DATA(ida
[IFLA_ADDRESS
- 1]));
356 send_addr_notify
= 1;
359 if (ida
[IFLA_BROADCAST
- 1]) {
360 if (ida
[IFLA_BROADCAST
- 1]->rta_len
!= RTA_LENGTH(dev
->addr_len
))
362 memcpy(dev
->broadcast
, RTA_DATA(ida
[IFLA_BROADCAST
- 1]),
364 send_addr_notify
= 1;
367 if (ida
[IFLA_MTU
- 1]) {
368 if (ida
[IFLA_MTU
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
370 err
= dev_set_mtu(dev
, *((u32
*) RTA_DATA(ida
[IFLA_MTU
- 1])));
377 if (ida
[IFLA_TXQLEN
- 1]) {
378 if (ida
[IFLA_TXQLEN
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
381 dev
->tx_queue_len
= *((u32
*) RTA_DATA(ida
[IFLA_TXQLEN
- 1]));
384 if (ida
[IFLA_WEIGHT
- 1]) {
385 if (ida
[IFLA_WEIGHT
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
388 dev
->weight
= *((u32
*) RTA_DATA(ida
[IFLA_WEIGHT
- 1]));
391 if (ifm
->ifi_index
>= 0 && ida
[IFLA_IFNAME
- 1]) {
392 char ifname
[IFNAMSIZ
];
394 if (rtattr_strlcpy(ifname
, ida
[IFLA_IFNAME
- 1],
395 IFNAMSIZ
) >= IFNAMSIZ
)
397 err
= dev_change_name(dev
, ifname
);
405 if (send_addr_notify
)
406 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
412 static int rtnetlink_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
415 int s_idx
= cb
->family
;
419 for (idx
=1; idx
<NPROTO
; idx
++) {
420 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
421 if (idx
< s_idx
|| idx
== PF_PACKET
)
423 if (rtnetlink_links
[idx
] == NULL
||
424 rtnetlink_links
[idx
][type
].dumpit
== NULL
)
427 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
428 if (rtnetlink_links
[idx
][type
].dumpit(skb
, cb
))
436 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
439 int size
= NLMSG_SPACE(sizeof(struct ifinfomsg
) +
440 sizeof(struct rtnl_link_ifmap
) +
441 sizeof(struct rtnl_link_stats
) + 128);
443 skb
= alloc_skb(size
, GFP_KERNEL
);
447 if (rtnetlink_fill_ifinfo(skb
, dev
, type
, 0, 0, change
) < 0) {
451 NETLINK_CB(skb
).dst_groups
= RTMGRP_LINK
;
452 netlink_broadcast(rtnl
, skb
, 0, RTMGRP_LINK
, GFP_KERNEL
);
455 static int rtnetlink_done(struct netlink_callback
*cb
)
460 /* Protected by RTNL sempahore. */
461 static struct rtattr
**rta_buf
;
462 static int rtattr_max
;
464 /* Process one rtnetlink message. */
466 static __inline__
int
467 rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
469 struct rtnetlink_link
*link
;
470 struct rtnetlink_link
*link_tab
;
477 /* Only requests are handled by kernel now */
478 if (!(nlh
->nlmsg_flags
&NLM_F_REQUEST
))
481 type
= nlh
->nlmsg_type
;
483 /* A control message: ignore them */
487 /* Unknown message: reply with EINVAL */
493 /* All the messages must have at least 1 byte length */
494 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
497 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
498 if (family
>= NPROTO
) {
499 *errp
= -EAFNOSUPPORT
;
503 link_tab
= rtnetlink_links
[family
];
504 if (link_tab
== NULL
)
505 link_tab
= rtnetlink_links
[PF_UNSPEC
];
506 link
= &link_tab
[type
];
511 if (kind
!= 2 && security_netlink_recv(skb
)) {
516 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
519 if (link
->dumpit
== NULL
)
520 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
522 if (link
->dumpit
== NULL
)
525 if ((*errp
= netlink_dump_start(rtnl
, skb
, nlh
,
527 rtnetlink_done
)) != 0) {
530 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
537 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
539 min_len
= rtm_min
[sz_idx
];
540 if (nlh
->nlmsg_len
< min_len
)
543 if (nlh
->nlmsg_len
> min_len
) {
544 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
545 struct rtattr
*attr
= (void*)nlh
+ NLMSG_ALIGN(min_len
);
547 while (RTA_OK(attr
, attrlen
)) {
548 unsigned flavor
= attr
->rta_type
;
550 if (flavor
> rta_max
[sz_idx
])
552 rta_buf
[flavor
-1] = attr
;
554 attr
= RTA_NEXT(attr
, attrlen
);
558 if (link
->doit
== NULL
)
559 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
560 if (link
->doit
== NULL
)
562 err
= link
->doit(skb
, nlh
, (void *)&rta_buf
[0]);
573 * Process one packet of messages.
574 * Malformed skbs with wrong lengths of messages are discarded silently.
577 static inline int rtnetlink_rcv_skb(struct sk_buff
*skb
)
580 struct nlmsghdr
* nlh
;
582 while (skb
->len
>= NLMSG_SPACE(0)) {
585 nlh
= (struct nlmsghdr
*)skb
->data
;
586 if (nlh
->nlmsg_len
< sizeof(*nlh
) || skb
->len
< nlh
->nlmsg_len
)
588 rlen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
591 if (rtnetlink_rcv_msg(skb
, nlh
, &err
)) {
592 /* Not error, but we must interrupt processing here:
593 * Note, that in this case we do not pull message
594 * from skb, it will be processed later.
598 netlink_ack(skb
, nlh
, err
);
599 } else if (nlh
->nlmsg_flags
&NLM_F_ACK
)
600 netlink_ack(skb
, nlh
, 0);
608 * rtnetlink input queue processing routine:
609 * - try to acquire shared lock. If it is failed, defer processing.
610 * - feed skbs to rtnetlink_rcv_skb, until it refuse a message,
611 * that will occur, when a dump started and/or acquisition of
612 * exclusive lock failed.
615 static void rtnetlink_rcv(struct sock
*sk
, int len
)
620 if (rtnl_shlock_nowait())
623 while ((skb
= skb_dequeue(&sk
->sk_receive_queue
)) != NULL
) {
624 if (rtnetlink_rcv_skb(skb
)) {
626 skb_queue_head(&sk
->sk_receive_queue
,
638 } while (rtnl
&& rtnl
->sk_receive_queue
.qlen
);
641 static struct rtnetlink_link link_rtnetlink_table
[RTM_MAX
-RTM_BASE
+1] =
643 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_ifinfo
},
644 [RTM_SETLINK
- RTM_BASE
] = { .doit
= do_setlink
},
645 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
646 [RTM_GETROUTE
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
647 [RTM_NEWNEIGH
- RTM_BASE
] = { .doit
= neigh_add
},
648 [RTM_DELNEIGH
- RTM_BASE
] = { .doit
= neigh_delete
},
649 [RTM_GETNEIGH
- RTM_BASE
] = { .dumpit
= neigh_dump_info
},
650 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
653 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
655 struct net_device
*dev
= ptr
;
657 case NETDEV_UNREGISTER
:
658 rtmsg_ifinfo(RTM_DELLINK
, dev
, ~0U);
660 case NETDEV_REGISTER
:
661 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U);
665 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
);
668 case NETDEV_GOING_DOWN
:
671 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
677 static struct notifier_block rtnetlink_dev_notifier
= {
678 .notifier_call
= rtnetlink_event
,
681 void __init
rtnetlink_init(void)
686 for (i
= 0; i
< ARRAY_SIZE(rta_max
); i
++)
687 if (rta_max
[i
] > rtattr_max
)
688 rtattr_max
= rta_max
[i
];
689 rta_buf
= kmalloc(rtattr_max
* sizeof(struct rtattr
*), GFP_KERNEL
);
691 panic("rtnetlink_init: cannot allocate rta_buf\n");
693 rtnl
= netlink_kernel_create(NETLINK_ROUTE
, rtnetlink_rcv
);
695 panic("rtnetlink_init: cannot initialize rtnetlink\n");
696 netlink_set_nonroot(NETLINK_ROUTE
, NL_NONROOT_RECV
);
697 register_netdevice_notifier(&rtnetlink_dev_notifier
);
698 rtnetlink_links
[PF_UNSPEC
] = link_rtnetlink_table
;
699 rtnetlink_links
[PF_PACKET
] = link_rtnetlink_table
;
702 EXPORT_SYMBOL(__rta_fill
);
703 EXPORT_SYMBOL(rtattr_strlcpy
);
704 EXPORT_SYMBOL(rtattr_parse
);
705 EXPORT_SYMBOL(rtnetlink_links
);
706 EXPORT_SYMBOL(rtnetlink_put_metrics
);
708 EXPORT_SYMBOL(rtnl_lock
);
709 EXPORT_SYMBOL(rtnl_lock_interruptible
);
710 EXPORT_SYMBOL(rtnl_sem
);
711 EXPORT_SYMBOL(rtnl_unlock
);