2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@cymru.net>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 #include <linux/config.h>
15 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/major.h>
19 #include <linux/signal.h>
20 #include <linux/sched.h>
21 #include <linux/errno.h>
22 #include <linux/string.h>
23 #include <linux/stat.h>
24 #include <linux/socket.h>
26 #include <linux/fcntl.h>
27 #include <linux/termios.h>
28 #include <linux/sockios.h>
29 #include <linux/net.h>
31 #include <linux/malloc.h>
32 #include <asm/uaccess.h>
33 #include <linux/skbuff.h>
34 #include <linux/netdevice.h>
35 #include <linux/netlink.h>
36 #include <linux/proc_fs.h>
42 #if defined(CONFIG_NETLINK_DEV) || defined(CONFIG_NETLINK_DEV_MODULE)
43 #define NL_EMULATE_DEV
46 static struct sock
*nl_table
[MAX_LINKS
];
47 static atomic_t nl_table_lock
[MAX_LINKS
];
48 static struct wait_queue
*nl_table_wait
;
51 static struct socket
*netlink_kernel
[MAX_LINKS
];
54 static int netlink_dump(struct sock
*sk
);
55 static void netlink_destroy_callback(struct netlink_callback
*cb
);
57 /* Netlink table lock. It protects against sk list changes
58 during uninterruptible sleeps in netlink_broadcast.
60 These lock MUST NOT be used from bh/irq on SMP kernels, because
61 It would result in race in netlink_wait_on_table.
64 extern __inline__
void
65 netlink_wait_on_table(int protocol
)
67 while (atomic_read(&nl_table_lock
[protocol
]))
68 sleep_on(&nl_table_wait
);
71 extern __inline__
void
72 netlink_lock_table(int protocol
)
74 atomic_inc(&nl_table_lock
[protocol
]);
77 extern __inline__
void
78 netlink_unlock_table(int protocol
)
81 /* F...g gcc does not eat it! */
83 if (atomic_dec_and_test(&nl_table_lock
[protocol
]))
84 wake_up(&nl_table_wait
);
86 atomic_dec(&nl_table_lock
[protocol
]);
87 if (!atomic_read(&nl_table_lock
[protocol
]))
88 wake_up(&nl_table_wait
);
92 static __inline__
void netlink_lock(struct sock
*sk
)
94 atomic_inc(&sk
->protinfo
.af_netlink
.locks
);
97 static __inline__
void netlink_unlock(struct sock
*sk
)
99 atomic_dec(&sk
->protinfo
.af_netlink
.locks
);
102 static __inline__
int netlink_locked(struct sock
*sk
)
104 return atomic_read(&sk
->protinfo
.af_netlink
.locks
);
107 static __inline__
struct sock
*netlink_lookup(int protocol
, u32 pid
)
111 for (sk
=nl_table
[protocol
]; sk
; sk
=sk
->next
) {
112 if (sk
->protinfo
.af_netlink
.pid
== pid
) {
121 extern struct proto_ops netlink_ops
;
123 static void netlink_insert(struct sock
*sk
)
125 sk
->next
= nl_table
[sk
->protocol
];
126 nl_table
[sk
->protocol
] = sk
;
129 static void netlink_remove(struct sock
*sk
)
132 for (skp
= &nl_table
[sk
->protocol
]; *skp
; skp
= &((*skp
)->next
)) {
142 static int netlink_create(struct socket
*sock
, int protocol
)
146 sock
->state
= SS_UNCONNECTED
;
148 if (sock
->type
!= SOCK_RAW
&& sock
->type
!= SOCK_DGRAM
)
149 return -ESOCKTNOSUPPORT
;
151 if (protocol
<0 || protocol
>= MAX_LINKS
)
152 return -EPROTONOSUPPORT
;
154 sock
->ops
= &netlink_ops
;
156 sk
= sk_alloc(PF_NETLINK
, GFP_KERNEL
, 1);
160 sock_init_data(sock
,sk
);
163 sk
->protocol
=protocol
;
167 static int netlink_release(struct socket
*sock
, struct socket
*peer
)
169 struct sock
*sk
= sock
->sk
;
174 /* Wait on table before removing socket */
175 netlink_wait_on_table(sk
->protocol
);
178 if (sk
->protinfo
.af_netlink
.cb
) {
180 sk
->protinfo
.af_netlink
.cb
->done(sk
->protinfo
.af_netlink
.cb
);
181 netlink_destroy_callback(sk
->protinfo
.af_netlink
.cb
);
182 sk
->protinfo
.af_netlink
.cb
= NULL
;
185 /* OK. Socket is unlinked, and, therefore,
186 no new packets will arrive */
187 sk
->state_change(sk
);
190 skb_queue_purge(&sk
->receive_queue
);
191 skb_queue_purge(&sk
->write_queue
);
193 /* IMPORTANT! It is the major unpleasant feature of this
194 transport (and AF_UNIX datagram, when it will be repaired).
196 Someone could wait on our sock->wait now.
197 We cannot release socket until waiter will remove itself
198 from wait queue. I choose the most conservetive way of solving
201 We waked up this queue above, so that we need only to wait
202 when the readers release us.
205 while (netlink_locked(sk
)) {
206 current
->counter
= 0;
215 if (atomic_read(&sk
->rmem_alloc
) || atomic_read(&sk
->wmem_alloc
)) {
216 printk(KERN_DEBUG
"netlink_release: impossible event. Please, report.\n");
224 static int netlink_autobind(struct socket
*sock
)
226 struct sock
*sk
= sock
->sk
;
229 sk
->protinfo
.af_netlink
.groups
= 0;
230 sk
->protinfo
.af_netlink
.pid
= current
->pid
;
233 for (osk
=nl_table
[sk
->protocol
]; osk
; osk
=osk
->next
) {
234 if (osk
->protinfo
.af_netlink
.pid
== sk
->protinfo
.af_netlink
.pid
) {
235 /* Bind collision, search negative pid values. */
236 if (sk
->protinfo
.af_netlink
.pid
> 0)
237 sk
->protinfo
.af_netlink
.pid
= -4096;
238 sk
->protinfo
.af_netlink
.pid
--;
247 static int netlink_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
249 struct sock
*sk
= sock
->sk
;
251 struct sockaddr_nl
*nladdr
=(struct sockaddr_nl
*)addr
;
253 if (nladdr
->nl_family
!= AF_NETLINK
)
256 /* Only superuser is allowed to listen multicasts */
257 if (nladdr
->nl_groups
&& !capable(CAP_NET_ADMIN
))
260 if (sk
->protinfo
.af_netlink
.pid
) {
261 if (nladdr
->nl_pid
!= sk
->protinfo
.af_netlink
.pid
)
263 sk
->protinfo
.af_netlink
.groups
= nladdr
->nl_groups
;
267 if (nladdr
->nl_pid
== 0) {
268 netlink_autobind(sock
);
269 sk
->protinfo
.af_netlink
.groups
= nladdr
->nl_groups
;
273 for (osk
=nl_table
[sk
->protocol
]; osk
; osk
=osk
->next
) {
274 if (osk
->protinfo
.af_netlink
.pid
== nladdr
->nl_pid
)
278 sk
->protinfo
.af_netlink
.pid
= nladdr
->nl_pid
;
279 sk
->protinfo
.af_netlink
.groups
= nladdr
->nl_groups
;
284 static int netlink_connect(struct socket
*sock
, struct sockaddr
*addr
,
287 struct sock
*sk
= sock
->sk
;
288 struct sockaddr_nl
*nladdr
=(struct sockaddr_nl
*)addr
;
290 if (addr
->sa_family
== AF_UNSPEC
)
292 sk
->protinfo
.af_netlink
.dst_pid
= 0;
293 sk
->protinfo
.af_netlink
.dst_groups
= 0;
296 if (addr
->sa_family
!= AF_NETLINK
)
299 /* Only superuser is allowed to send multicasts */
300 if (nladdr
->nl_groups
&& !capable(CAP_NET_ADMIN
))
303 sk
->protinfo
.af_netlink
.dst_pid
= nladdr
->nl_pid
;
304 sk
->protinfo
.af_netlink
.dst_groups
= nladdr
->nl_groups
;
306 if (!sk
->protinfo
.af_netlink
.pid
)
307 netlink_autobind(sock
);
311 static int netlink_getname(struct socket
*sock
, struct sockaddr
*addr
, int *addr_len
, int peer
)
313 struct sock
*sk
= sock
->sk
;
314 struct sockaddr_nl
*nladdr
=(struct sockaddr_nl
*)addr
;
316 nladdr
->nl_family
= AF_NETLINK
;
317 *addr_len
= sizeof(*nladdr
);
320 nladdr
->nl_pid
= sk
->protinfo
.af_netlink
.dst_pid
;
321 nladdr
->nl_groups
= sk
->protinfo
.af_netlink
.dst_groups
;
323 nladdr
->nl_pid
= sk
->protinfo
.af_netlink
.pid
;
324 nladdr
->nl_groups
= sk
->protinfo
.af_netlink
.groups
;
329 int netlink_unicast(struct sock
*ssk
, struct sk_buff
*skb
, u32 pid
, int nonblock
)
333 int protocol
= ssk
->protocol
;
334 DECLARE_WAITQUEUE(wait
, current
);
337 for (sk
= nl_table
[protocol
]; sk
; sk
= sk
->next
) {
338 if (sk
->protinfo
.af_netlink
.pid
!= pid
)
343 #ifdef NL_EMULATE_DEV
344 if (sk
->protinfo
.af_netlink
.handler
) {
346 len
= sk
->protinfo
.af_netlink
.handler(protocol
, skb
);
353 add_wait_queue(sk
->sleep
, &wait
);
354 current
->state
= TASK_INTERRUPTIBLE
;
357 if (atomic_read(&sk
->rmem_alloc
) > sk
->rcvbuf
) {
366 current
->state
= TASK_RUNNING
;
367 remove_wait_queue(sk
->sleep
, &wait
);
370 if (signal_pending(current
)) {
378 current
->state
= TASK_RUNNING
;
379 remove_wait_queue(sk
->sleep
, &wait
);
383 skb_set_owner_r(skb
, sk
);
384 skb_queue_tail(&sk
->receive_queue
, skb
);
385 sk
->data_ready(sk
, len
);
390 return -ECONNREFUSED
;
393 static __inline__
int netlink_broadcast_deliver(struct sock
*sk
, struct sk_buff
*skb
)
395 #ifdef NL_EMULATE_DEV
396 if (sk
->protinfo
.af_netlink
.handler
) {
398 sk
->protinfo
.af_netlink
.handler(sk
->protocol
, skb
);
402 if (atomic_read(&sk
->rmem_alloc
) <= sk
->rcvbuf
) {
403 Nprintk("broadcast_deliver %d\n", skb
->len
);
405 skb_set_owner_r(skb
, sk
);
406 skb_queue_tail(&sk
->receive_queue
, skb
);
407 sk
->data_ready(sk
, skb
->len
);
413 void netlink_broadcast(struct sock
*ssk
, struct sk_buff
*skb
, u32 pid
,
414 u32 group
, int allocation
)
417 struct sk_buff
*skb2
= NULL
;
418 int protocol
= ssk
->protocol
;
421 /* While we sleep in clone, do not allow to change socket list */
423 if (allocation
== GFP_KERNEL
)
424 netlink_lock_table(protocol
);
426 for (sk
= nl_table
[protocol
]; sk
; sk
= sk
->next
) {
430 if (sk
->protinfo
.af_netlink
.pid
== pid
||
431 !(sk
->protinfo
.af_netlink
.groups
&group
))
436 sk
->state_change(sk
);
442 if (atomic_read(&skb
->users
) != 1) {
443 skb2
= skb_clone(skb
, allocation
);
446 atomic_inc(&skb
->users
);
451 sk
->state_change(sk
);
452 /* Clone failed. Notify ALL listeners. */
454 } else if (netlink_broadcast_deliver(sk
, skb2
)) {
456 sk
->state_change(sk
);
462 if (allocation
== GFP_KERNEL
)
463 netlink_unlock_table(protocol
);
470 void netlink_set_err(struct sock
*ssk
, u32 pid
, u32 group
, int code
)
473 int protocol
= ssk
->protocol
;
476 for (sk
= nl_table
[protocol
]; sk
; sk
= sk
->next
) {
480 if (sk
->protinfo
.af_netlink
.pid
== pid
||
481 !(sk
->protinfo
.af_netlink
.groups
&group
))
485 sk
->state_change(sk
);
489 static int netlink_sendmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
490 struct scm_cookie
*scm
)
492 struct sock
*sk
= sock
->sk
;
493 struct sockaddr_nl
*addr
=msg
->msg_name
;
498 if (msg
->msg_flags
&MSG_OOB
)
501 if (msg
->msg_flags
&~(MSG_DONTWAIT
|MSG_NOSIGNAL
|MSG_ERRQUEUE
))
504 if (msg
->msg_namelen
) {
505 if (addr
->nl_family
!= AF_NETLINK
)
507 dst_pid
= addr
->nl_pid
;
508 dst_groups
= addr
->nl_groups
;
509 if (dst_groups
&& !capable(CAP_NET_ADMIN
))
512 dst_pid
= sk
->protinfo
.af_netlink
.dst_pid
;
513 dst_groups
= sk
->protinfo
.af_netlink
.dst_groups
;
516 if (!sk
->protinfo
.af_netlink
.pid
)
517 netlink_autobind(sock
);
519 skb
= sock_wmalloc(sk
, len
, 0, GFP_KERNEL
);
523 NETLINK_CB(skb
).pid
= sk
->protinfo
.af_netlink
.pid
;
524 NETLINK_CB(skb
).groups
= sk
->protinfo
.af_netlink
.groups
;
525 NETLINK_CB(skb
).dst_pid
= dst_pid
;
526 NETLINK_CB(skb
).dst_groups
= dst_groups
;
527 memcpy(NETLINK_CREDS(skb
), &scm
->creds
, sizeof(struct ucred
));
529 /* What can I do? Netlink is asynchronous, so that
530 we will have to save current capabilities to
531 check them, when this message will be delivered
532 to corresponding kernel module. --ANK (980802)
534 NETLINK_CB(skb
).eff_cap
= current
->cap_effective
;
536 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
)) {
542 atomic_inc(&skb
->users
);
543 netlink_broadcast(sk
, skb
, dst_pid
, dst_groups
, GFP_KERNEL
);
545 return netlink_unicast(sk
, skb
, dst_pid
, msg
->msg_flags
&MSG_DONTWAIT
);
548 static int netlink_recvmsg(struct socket
*sock
, struct msghdr
*msg
, int len
,
549 int flags
, struct scm_cookie
*scm
)
551 struct sock
*sk
= sock
->sk
;
552 int noblock
= flags
&MSG_DONTWAIT
;
557 if (flags
&(MSG_OOB
|MSG_PEEK
))
560 skb
= skb_recv_datagram(sk
,flags
,noblock
,&err
);
564 msg
->msg_namelen
= 0;
568 msg
->msg_flags
|= MSG_TRUNC
;
572 skb
->h
.raw
= skb
->data
;
573 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
576 struct sockaddr_nl
*addr
= (struct sockaddr_nl
*)msg
->msg_name
;
577 addr
->nl_family
= AF_NETLINK
;
578 addr
->nl_pid
= NETLINK_CB(skb
).pid
;
579 addr
->nl_groups
= NETLINK_CB(skb
).dst_groups
;
580 msg
->msg_namelen
= sizeof(*addr
);
583 scm
->creds
= *NETLINK_CREDS(skb
);
584 skb_free_datagram(sk
, skb
);
586 if (sk
->protinfo
.af_netlink
.cb
587 && atomic_read(&sk
->rmem_alloc
) <= sk
->rcvbuf
/2)
589 return err
? : copied
;
593 * We export these functions to other modules. They provide a
594 * complete set of kernel non-blocking support for message
599 netlink_kernel_create(int unit
, void (*input
)(struct sock
*sk
, int len
))
604 if (unit
<0 || unit
>=MAX_LINKS
)
607 if (!(sock
= sock_alloc()))
610 sock
->type
= SOCK_RAW
;
612 if (netlink_create(sock
, unit
) < 0) {
618 sk
->data_ready
= input
;
624 static void netlink_destroy_callback(struct netlink_callback
*cb
)
632 * It looks a bit ugly.
633 * It would be better to create kernel thread.
636 static int netlink_dump(struct sock
*sk
)
638 struct netlink_callback
*cb
;
640 struct nlmsghdr
*nlh
;
643 skb
= sock_rmalloc(sk
, NLMSG_GOODSIZE
, 0, GFP_KERNEL
);
647 cb
= sk
->protinfo
.af_netlink
.cb
;
649 len
= cb
->dump(skb
, cb
);
652 skb_queue_tail(&sk
->receive_queue
, skb
);
653 sk
->data_ready(sk
, len
);
657 nlh
= __nlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
, NLMSG_DONE
, sizeof(int));
658 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
659 memcpy(NLMSG_DATA(nlh
), &len
, sizeof(len
));
660 skb_queue_tail(&sk
->receive_queue
, skb
);
661 sk
->data_ready(sk
, skb
->len
);
664 sk
->protinfo
.af_netlink
.cb
= NULL
;
665 netlink_destroy_callback(cb
);
670 int netlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
671 struct nlmsghdr
*nlh
,
672 int (*dump
)(struct sk_buff
*skb
, struct netlink_callback
*),
673 int (*done
)(struct netlink_callback
*))
675 struct netlink_callback
*cb
;
678 cb
= kmalloc(sizeof(*cb
), GFP_KERNEL
);
682 memset(cb
, 0, sizeof(*cb
));
686 atomic_inc(&skb
->users
);
689 sk
= netlink_lookup(ssk
->protocol
, NETLINK_CB(skb
).pid
);
691 netlink_destroy_callback(cb
);
692 return -ECONNREFUSED
;
694 /* A dump is in progress... */
695 if (sk
->protinfo
.af_netlink
.cb
) {
696 netlink_destroy_callback(cb
);
700 sk
->protinfo
.af_netlink
.cb
= cb
;
705 void netlink_ack(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
, int err
)
708 struct nlmsghdr
*rep
;
709 struct nlmsgerr
*errmsg
;
713 size
= NLMSG_SPACE(sizeof(struct nlmsgerr
));
715 size
= NLMSG_SPACE(4 + NLMSG_ALIGN(nlh
->nlmsg_len
));
717 skb
= alloc_skb(size
, GFP_KERNEL
);
721 rep
= __nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, nlh
->nlmsg_seq
,
722 NLMSG_ERROR
, sizeof(struct nlmsgerr
));
723 errmsg
= NLMSG_DATA(rep
);
725 memcpy(&errmsg
->msg
, nlh
, err
? nlh
->nlmsg_len
: sizeof(struct nlmsghdr
));
726 netlink_unicast(in_skb
->sk
, skb
, NETLINK_CB(in_skb
).pid
, MSG_DONTWAIT
);
730 #ifdef NL_EMULATE_DEV
732 * Backward compatibility.
735 int netlink_attach(int unit
, int (*function
)(int, struct sk_buff
*skb
))
737 struct sock
*sk
= netlink_kernel_create(unit
, NULL
);
740 sk
->protinfo
.af_netlink
.handler
= function
;
741 netlink_kernel
[unit
] = sk
->socket
;
745 void netlink_detach(int unit
)
747 struct socket
*sock
= netlink_kernel
[unit
];
749 netlink_kernel
[unit
] = NULL
;
755 int netlink_post(int unit
, struct sk_buff
*skb
)
757 struct socket
*sock
= netlink_kernel
[unit
];
760 memset(skb
->cb
, 0, sizeof(skb
->cb
));
761 netlink_broadcast(sock
->sk
, skb
, 0, ~0, GFP_ATOMIC
);
770 #ifdef CONFIG_PROC_FS
771 static int netlink_read_proc(char *buffer
, char **start
, off_t offset
,
772 int length
, int *eof
, void *data
)
780 len
+= sprintf(buffer
,"sk Eth Pid Groups "
781 "Rmem Wmem Dump Locks\n");
783 for (i
=0; i
<MAX_LINKS
; i
++) {
784 for (s
= nl_table
[i
]; s
; s
= s
->next
) {
785 len
+=sprintf(buffer
+len
,"%p %-3d %-6d %08x %-8d %-8d %p %d",
788 s
->protinfo
.af_netlink
.pid
,
789 s
->protinfo
.af_netlink
.groups
,
790 atomic_read(&s
->rmem_alloc
),
791 atomic_read(&s
->wmem_alloc
),
792 s
->protinfo
.af_netlink
.cb
,
793 atomic_read(&s
->protinfo
.af_netlink
.locks
)
803 if(pos
>offset
+length
)
810 *start
=buffer
+(offset
-begin
);
820 struct proto_ops netlink_ops
= {
841 struct net_proto_family netlink_family_ops
= {
846 void netlink_proto_init(struct net_proto
*pro
)
848 #ifdef CONFIG_PROC_FS
849 struct proc_dir_entry
*ent
;
851 struct sk_buff
*dummy_skb
;
853 if (sizeof(struct netlink_skb_parms
) > sizeof(dummy_skb
->cb
)) {
854 printk(KERN_CRIT
"netlink_proto_init: panic\n");
857 sock_register(&netlink_family_ops
);
858 #ifdef CONFIG_PROC_FS
859 ent
= create_proc_entry("net/netlink", 0, 0);
860 ent
->read_proc
= netlink_read_proc
;