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>
52 #include <net/netlink.h>
54 DECLARE_MUTEX(rtnl_sem
);
61 int rtnl_lock_interruptible(void)
63 return down_interruptible(&rtnl_sem
);
66 void rtnl_unlock(void)
73 int rtattr_parse(struct rtattr
*tb
[], int maxattr
, struct rtattr
*rta
, int len
)
75 memset(tb
, 0, sizeof(struct rtattr
*)*maxattr
);
77 while (RTA_OK(rta
, len
)) {
78 unsigned flavor
= rta
->rta_type
;
79 if (flavor
&& flavor
<= maxattr
)
81 rta
= RTA_NEXT(rta
, len
);
88 struct rtnetlink_link
* rtnetlink_links
[NPROTO
];
90 static const int rtm_min
[RTM_NR_FAMILIES
] =
92 [RTM_FAM(RTM_NEWLINK
)] = NLMSG_LENGTH(sizeof(struct ifinfomsg
)),
93 [RTM_FAM(RTM_NEWADDR
)] = NLMSG_LENGTH(sizeof(struct ifaddrmsg
)),
94 [RTM_FAM(RTM_NEWROUTE
)] = NLMSG_LENGTH(sizeof(struct rtmsg
)),
95 [RTM_FAM(RTM_NEWNEIGH
)] = NLMSG_LENGTH(sizeof(struct ndmsg
)),
96 [RTM_FAM(RTM_NEWRULE
)] = NLMSG_LENGTH(sizeof(struct rtmsg
)),
97 [RTM_FAM(RTM_NEWQDISC
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
98 [RTM_FAM(RTM_NEWTCLASS
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
99 [RTM_FAM(RTM_NEWTFILTER
)] = NLMSG_LENGTH(sizeof(struct tcmsg
)),
100 [RTM_FAM(RTM_NEWACTION
)] = NLMSG_LENGTH(sizeof(struct tcamsg
)),
101 [RTM_FAM(RTM_NEWPREFIX
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
102 [RTM_FAM(RTM_GETMULTICAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
103 [RTM_FAM(RTM_GETANYCAST
)] = NLMSG_LENGTH(sizeof(struct rtgenmsg
)),
104 [RTM_FAM(RTM_NEWNEIGHTBL
)] = NLMSG_LENGTH(sizeof(struct ndtmsg
)),
107 static const int rta_max
[RTM_NR_FAMILIES
] =
109 [RTM_FAM(RTM_NEWLINK
)] = IFLA_MAX
,
110 [RTM_FAM(RTM_NEWADDR
)] = IFA_MAX
,
111 [RTM_FAM(RTM_NEWROUTE
)] = RTA_MAX
,
112 [RTM_FAM(RTM_NEWNEIGH
)] = NDA_MAX
,
113 [RTM_FAM(RTM_NEWRULE
)] = RTA_MAX
,
114 [RTM_FAM(RTM_NEWQDISC
)] = TCA_MAX
,
115 [RTM_FAM(RTM_NEWTCLASS
)] = TCA_MAX
,
116 [RTM_FAM(RTM_NEWTFILTER
)] = TCA_MAX
,
117 [RTM_FAM(RTM_NEWACTION
)] = TCAA_MAX
,
118 [RTM_FAM(RTM_NEWNEIGHTBL
)] = NDTA_MAX
,
121 void __rta_fill(struct sk_buff
*skb
, int attrtype
, int attrlen
, const void *data
)
124 int size
= RTA_LENGTH(attrlen
);
126 rta
= (struct rtattr
*)skb_put(skb
, RTA_ALIGN(size
));
127 rta
->rta_type
= attrtype
;
129 memcpy(RTA_DATA(rta
), data
, attrlen
);
130 memset(RTA_DATA(rta
) + attrlen
, 0, RTA_ALIGN(size
) - size
);
133 size_t rtattr_strlcpy(char *dest
, const struct rtattr
*rta
, size_t size
)
135 size_t ret
= RTA_PAYLOAD(rta
);
136 char *src
= RTA_DATA(rta
);
138 if (ret
> 0 && src
[ret
- 1] == '\0')
141 size_t len
= (ret
>= size
) ? size
- 1 : ret
;
142 memset(dest
, 0, size
);
143 memcpy(dest
, src
, len
);
148 int rtnetlink_send(struct sk_buff
*skb
, u32 pid
, unsigned group
, int echo
)
152 NETLINK_CB(skb
).dst_group
= group
;
154 atomic_inc(&skb
->users
);
155 netlink_broadcast(rtnl
, skb
, pid
, group
, GFP_KERNEL
);
157 err
= netlink_unicast(rtnl
, skb
, pid
, MSG_DONTWAIT
);
161 int rtnetlink_put_metrics(struct sk_buff
*skb
, u32
*metrics
)
163 struct rtattr
*mx
= (struct rtattr
*)skb
->tail
;
166 RTA_PUT(skb
, RTA_METRICS
, 0, NULL
);
167 for (i
=0; i
<RTAX_MAX
; i
++) {
169 RTA_PUT(skb
, i
+1, sizeof(u32
), metrics
+i
);
171 mx
->rta_len
= skb
->tail
- (u8
*)mx
;
172 if (mx
->rta_len
== RTA_LENGTH(0))
173 skb_trim(skb
, (u8
*)mx
- skb
->data
);
177 skb_trim(skb
, (u8
*)mx
- skb
->data
);
182 static int rtnetlink_fill_ifinfo(struct sk_buff
*skb
, struct net_device
*dev
,
183 int type
, u32 pid
, u32 seq
, u32 change
,
187 struct nlmsghdr
*nlh
;
188 unsigned char *b
= skb
->tail
;
190 nlh
= NLMSG_NEW(skb
, pid
, seq
, type
, sizeof(*r
), flags
);
192 r
->ifi_family
= AF_UNSPEC
;
194 r
->ifi_type
= dev
->type
;
195 r
->ifi_index
= dev
->ifindex
;
196 r
->ifi_flags
= dev_get_flags(dev
);
197 r
->ifi_change
= change
;
199 RTA_PUT(skb
, IFLA_IFNAME
, strlen(dev
->name
)+1, dev
->name
);
202 u32 txqlen
= dev
->tx_queue_len
;
203 RTA_PUT(skb
, IFLA_TXQLEN
, sizeof(txqlen
), &txqlen
);
207 u32 weight
= dev
->weight
;
208 RTA_PUT(skb
, IFLA_WEIGHT
, sizeof(weight
), &weight
);
212 struct rtnl_link_ifmap map
= {
213 .mem_start
= dev
->mem_start
,
214 .mem_end
= dev
->mem_end
,
215 .base_addr
= dev
->base_addr
,
218 .port
= dev
->if_port
,
220 RTA_PUT(skb
, IFLA_MAP
, sizeof(map
), &map
);
224 RTA_PUT(skb
, IFLA_ADDRESS
, dev
->addr_len
, dev
->dev_addr
);
225 RTA_PUT(skb
, IFLA_BROADCAST
, dev
->addr_len
, dev
->broadcast
);
230 RTA_PUT(skb
, IFLA_MTU
, sizeof(mtu
), &mtu
);
233 if (dev
->ifindex
!= dev
->iflink
) {
234 u32 iflink
= dev
->iflink
;
235 RTA_PUT(skb
, IFLA_LINK
, sizeof(iflink
), &iflink
);
238 if (dev
->qdisc_sleeping
)
239 RTA_PUT(skb
, IFLA_QDISC
,
240 strlen(dev
->qdisc_sleeping
->ops
->id
) + 1,
241 dev
->qdisc_sleeping
->ops
->id
);
244 u32 master
= dev
->master
->ifindex
;
245 RTA_PUT(skb
, IFLA_MASTER
, sizeof(master
), &master
);
248 if (dev
->get_stats
) {
249 unsigned long *stats
= (unsigned long*)dev
->get_stats(dev
);
254 int n
= sizeof(struct rtnl_link_stats
)/4;
256 a
= __RTA_PUT(skb
, IFLA_STATS
, n
*4);
262 nlh
->nlmsg_len
= skb
->tail
- b
;
267 skb_trim(skb
, b
- skb
->data
);
271 static int rtnetlink_dump_ifinfo(struct sk_buff
*skb
, struct netlink_callback
*cb
)
274 int s_idx
= cb
->args
[0];
275 struct net_device
*dev
;
277 read_lock(&dev_base_lock
);
278 for (dev
=dev_base
, idx
=0; dev
; dev
= dev
->next
, idx
++) {
281 if (rtnetlink_fill_ifinfo(skb
, dev
, RTM_NEWLINK
,
282 NETLINK_CB(cb
->skb
).pid
,
283 cb
->nlh
->nlmsg_seq
, 0,
287 read_unlock(&dev_base_lock
);
293 static int do_setlink(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, void *arg
)
295 struct ifinfomsg
*ifm
= NLMSG_DATA(nlh
);
296 struct rtattr
**ida
= arg
;
297 struct net_device
*dev
;
298 int err
, send_addr_notify
= 0;
300 if (ifm
->ifi_index
>= 0)
301 dev
= dev_get_by_index(ifm
->ifi_index
);
302 else if (ida
[IFLA_IFNAME
- 1]) {
303 char ifname
[IFNAMSIZ
];
305 if (rtattr_strlcpy(ifname
, ida
[IFLA_IFNAME
- 1],
306 IFNAMSIZ
) >= IFNAMSIZ
)
308 dev
= dev_get_by_name(ifname
);
318 dev_change_flags(dev
, ifm
->ifi_flags
);
320 if (ida
[IFLA_MAP
- 1]) {
321 struct rtnl_link_ifmap
*u_map
;
324 if (!dev
->set_config
) {
329 if (!netif_device_present(dev
)) {
334 if (ida
[IFLA_MAP
- 1]->rta_len
!= RTA_LENGTH(sizeof(*u_map
)))
337 u_map
= RTA_DATA(ida
[IFLA_MAP
- 1]);
339 k_map
.mem_start
= (unsigned long) u_map
->mem_start
;
340 k_map
.mem_end
= (unsigned long) u_map
->mem_end
;
341 k_map
.base_addr
= (unsigned short) u_map
->base_addr
;
342 k_map
.irq
= (unsigned char) u_map
->irq
;
343 k_map
.dma
= (unsigned char) u_map
->dma
;
344 k_map
.port
= (unsigned char) u_map
->port
;
346 err
= dev
->set_config(dev
, &k_map
);
352 if (ida
[IFLA_ADDRESS
- 1]) {
353 if (!dev
->set_mac_address
) {
357 if (!netif_device_present(dev
)) {
361 if (ida
[IFLA_ADDRESS
- 1]->rta_len
!= RTA_LENGTH(dev
->addr_len
))
364 err
= dev
->set_mac_address(dev
, RTA_DATA(ida
[IFLA_ADDRESS
- 1]));
367 send_addr_notify
= 1;
370 if (ida
[IFLA_BROADCAST
- 1]) {
371 if (ida
[IFLA_BROADCAST
- 1]->rta_len
!= RTA_LENGTH(dev
->addr_len
))
373 memcpy(dev
->broadcast
, RTA_DATA(ida
[IFLA_BROADCAST
- 1]),
375 send_addr_notify
= 1;
378 if (ida
[IFLA_MTU
- 1]) {
379 if (ida
[IFLA_MTU
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
381 err
= dev_set_mtu(dev
, *((u32
*) RTA_DATA(ida
[IFLA_MTU
- 1])));
388 if (ida
[IFLA_TXQLEN
- 1]) {
389 if (ida
[IFLA_TXQLEN
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
392 dev
->tx_queue_len
= *((u32
*) RTA_DATA(ida
[IFLA_TXQLEN
- 1]));
395 if (ida
[IFLA_WEIGHT
- 1]) {
396 if (ida
[IFLA_WEIGHT
- 1]->rta_len
!= RTA_LENGTH(sizeof(u32
)))
399 dev
->weight
= *((u32
*) RTA_DATA(ida
[IFLA_WEIGHT
- 1]));
402 if (ifm
->ifi_index
>= 0 && ida
[IFLA_IFNAME
- 1]) {
403 char ifname
[IFNAMSIZ
];
405 if (rtattr_strlcpy(ifname
, ida
[IFLA_IFNAME
- 1],
406 IFNAMSIZ
) >= IFNAMSIZ
)
408 err
= dev_change_name(dev
, ifname
);
416 if (send_addr_notify
)
417 call_netdevice_notifiers(NETDEV_CHANGEADDR
, dev
);
423 static int rtnetlink_dump_all(struct sk_buff
*skb
, struct netlink_callback
*cb
)
426 int s_idx
= cb
->family
;
430 for (idx
=1; idx
<NPROTO
; idx
++) {
431 int type
= cb
->nlh
->nlmsg_type
-RTM_BASE
;
432 if (idx
< s_idx
|| idx
== PF_PACKET
)
434 if (rtnetlink_links
[idx
] == NULL
||
435 rtnetlink_links
[idx
][type
].dumpit
== NULL
)
438 memset(&cb
->args
[0], 0, sizeof(cb
->args
));
439 if (rtnetlink_links
[idx
][type
].dumpit(skb
, cb
))
447 void rtmsg_ifinfo(int type
, struct net_device
*dev
, unsigned change
)
450 int size
= NLMSG_SPACE(sizeof(struct ifinfomsg
) +
451 sizeof(struct rtnl_link_ifmap
) +
452 sizeof(struct rtnl_link_stats
) + 128);
454 skb
= alloc_skb(size
, GFP_KERNEL
);
458 if (rtnetlink_fill_ifinfo(skb
, dev
, type
, current
->pid
, 0, change
, 0) < 0) {
462 NETLINK_CB(skb
).dst_group
= RTNLGRP_LINK
;
463 netlink_broadcast(rtnl
, skb
, 0, RTNLGRP_LINK
, GFP_KERNEL
);
466 /* Protected by RTNL sempahore. */
467 static struct rtattr
**rta_buf
;
468 static int rtattr_max
;
470 /* Process one rtnetlink message. */
472 static __inline__
int
473 rtnetlink_rcv_msg(struct sk_buff
*skb
, struct nlmsghdr
*nlh
, int *errp
)
475 struct rtnetlink_link
*link
;
476 struct rtnetlink_link
*link_tab
;
483 /* Only requests are handled by kernel now */
484 if (!(nlh
->nlmsg_flags
&NLM_F_REQUEST
))
487 type
= nlh
->nlmsg_type
;
489 /* A control message: ignore them */
493 /* Unknown message: reply with EINVAL */
499 /* All the messages must have at least 1 byte length */
500 if (nlh
->nlmsg_len
< NLMSG_LENGTH(sizeof(struct rtgenmsg
)))
503 family
= ((struct rtgenmsg
*)NLMSG_DATA(nlh
))->rtgen_family
;
504 if (family
>= NPROTO
) {
505 *errp
= -EAFNOSUPPORT
;
509 link_tab
= rtnetlink_links
[family
];
510 if (link_tab
== NULL
)
511 link_tab
= rtnetlink_links
[PF_UNSPEC
];
512 link
= &link_tab
[type
];
517 if (kind
!= 2 && security_netlink_recv(skb
)) {
522 if (kind
== 2 && nlh
->nlmsg_flags
&NLM_F_DUMP
) {
523 if (link
->dumpit
== NULL
)
524 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
526 if (link
->dumpit
== NULL
)
529 if ((*errp
= netlink_dump_start(rtnl
, skb
, nlh
,
530 link
->dumpit
, NULL
)) != 0) {
534 netlink_queue_skip(nlh
, skb
);
538 memset(rta_buf
, 0, (rtattr_max
* sizeof(struct rtattr
*)));
540 min_len
= rtm_min
[sz_idx
];
541 if (nlh
->nlmsg_len
< min_len
)
544 if (nlh
->nlmsg_len
> min_len
) {
545 int attrlen
= nlh
->nlmsg_len
- NLMSG_ALIGN(min_len
);
546 struct rtattr
*attr
= (void*)nlh
+ NLMSG_ALIGN(min_len
);
548 while (RTA_OK(attr
, attrlen
)) {
549 unsigned flavor
= attr
->rta_type
;
551 if (flavor
> rta_max
[sz_idx
])
553 rta_buf
[flavor
-1] = attr
;
555 attr
= RTA_NEXT(attr
, attrlen
);
559 if (link
->doit
== NULL
)
560 link
= &(rtnetlink_links
[PF_UNSPEC
][type
]);
561 if (link
->doit
== NULL
)
563 err
= link
->doit(skb
, nlh
, (void *)&rta_buf
[0]);
573 static void rtnetlink_rcv(struct sock
*sk
, int len
)
575 unsigned int qlen
= 0;
579 netlink_run_queue(sk
, &qlen
, &rtnetlink_rcv_msg
);
586 static struct rtnetlink_link link_rtnetlink_table
[RTM_NR_MSGTYPES
] =
588 [RTM_GETLINK
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_ifinfo
},
589 [RTM_SETLINK
- RTM_BASE
] = { .doit
= do_setlink
},
590 [RTM_GETADDR
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
591 [RTM_GETROUTE
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
592 [RTM_NEWNEIGH
- RTM_BASE
] = { .doit
= neigh_add
},
593 [RTM_DELNEIGH
- RTM_BASE
] = { .doit
= neigh_delete
},
594 [RTM_GETNEIGH
- RTM_BASE
] = { .dumpit
= neigh_dump_info
},
595 [RTM_GETRULE
- RTM_BASE
] = { .dumpit
= rtnetlink_dump_all
},
596 [RTM_GETNEIGHTBL
- RTM_BASE
] = { .dumpit
= neightbl_dump_info
},
597 [RTM_SETNEIGHTBL
- RTM_BASE
] = { .doit
= neightbl_set
},
600 static int rtnetlink_event(struct notifier_block
*this, unsigned long event
, void *ptr
)
602 struct net_device
*dev
= ptr
;
604 case NETDEV_UNREGISTER
:
605 rtmsg_ifinfo(RTM_DELLINK
, dev
, ~0U);
607 case NETDEV_REGISTER
:
608 rtmsg_ifinfo(RTM_NEWLINK
, dev
, ~0U);
612 rtmsg_ifinfo(RTM_NEWLINK
, dev
, IFF_UP
|IFF_RUNNING
);
615 case NETDEV_GOING_DOWN
:
618 rtmsg_ifinfo(RTM_NEWLINK
, dev
, 0);
624 static struct notifier_block rtnetlink_dev_notifier
= {
625 .notifier_call
= rtnetlink_event
,
628 void __init
rtnetlink_init(void)
633 for (i
= 0; i
< ARRAY_SIZE(rta_max
); i
++)
634 if (rta_max
[i
] > rtattr_max
)
635 rtattr_max
= rta_max
[i
];
636 rta_buf
= kmalloc(rtattr_max
* sizeof(struct rtattr
*), GFP_KERNEL
);
638 panic("rtnetlink_init: cannot allocate rta_buf\n");
640 rtnl
= netlink_kernel_create(NETLINK_ROUTE
, RTNLGRP_MAX
, rtnetlink_rcv
,
643 panic("rtnetlink_init: cannot initialize rtnetlink\n");
644 netlink_set_nonroot(NETLINK_ROUTE
, NL_NONROOT_RECV
);
645 register_netdevice_notifier(&rtnetlink_dev_notifier
);
646 rtnetlink_links
[PF_UNSPEC
] = link_rtnetlink_table
;
647 rtnetlink_links
[PF_PACKET
] = link_rtnetlink_table
;
650 EXPORT_SYMBOL(__rta_fill
);
651 EXPORT_SYMBOL(rtattr_strlcpy
);
652 EXPORT_SYMBOL(rtattr_parse
);
653 EXPORT_SYMBOL(rtnetlink_links
);
654 EXPORT_SYMBOL(rtnetlink_put_metrics
);
656 EXPORT_SYMBOL(rtnl_lock
);
657 EXPORT_SYMBOL(rtnl_lock_interruptible
);
658 EXPORT_SYMBOL(rtnl_sem
);
659 EXPORT_SYMBOL(rtnl_unlock
);