2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@redhat.com>
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.
12 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
19 #include <linux/config.h>
20 #include <linux/module.h>
22 #include <linux/kernel.h>
23 #include <linux/init.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/errno.h>
27 #include <linux/string.h>
28 #include <linux/stat.h>
29 #include <linux/socket.h>
31 #include <linux/fcntl.h>
32 #include <linux/termios.h>
33 #include <linux/sockios.h>
34 #include <linux/net.h>
36 #include <linux/slab.h>
37 #include <asm/uaccess.h>
38 #include <linux/skbuff.h>
39 #include <linux/netdevice.h>
40 #include <linux/rtnetlink.h>
41 #include <linux/proc_fs.h>
42 #include <linux/seq_file.h>
43 #include <linux/smp_lock.h>
44 #include <linux/notifier.h>
45 #include <linux/security.h>
46 #include <linux/jhash.h>
47 #include <linux/jiffies.h>
48 #include <linux/random.h>
49 #include <linux/bitops.h>
51 #include <linux/types.h>
52 #include <linux/audit.h>
60 /* struct sock has to be the first member of netlink_sock */
65 unsigned int dst_groups
;
67 wait_queue_head_t wait
;
68 struct netlink_callback
*cb
;
70 void (*data_ready
)(struct sock
*sk
, int bytes
);
73 static inline struct netlink_sock
*nlk_sk(struct sock
*sk
)
75 return (struct netlink_sock
*)sk
;
79 struct hlist_head
*table
;
80 unsigned long rehash_time
;
86 unsigned int max_shift
;
91 struct netlink_table
{
92 struct nl_pid_hash hash
;
93 struct hlist_head mc_list
;
94 unsigned int nl_nonroot
;
97 static struct netlink_table
*nl_table
;
99 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait
);
101 static int netlink_dump(struct sock
*sk
);
102 static void netlink_destroy_callback(struct netlink_callback
*cb
);
104 static DEFINE_RWLOCK(nl_table_lock
);
105 static atomic_t nl_table_users
= ATOMIC_INIT(0);
107 static struct notifier_block
*netlink_chain
;
109 static struct hlist_head
*nl_pid_hashfn(struct nl_pid_hash
*hash
, u32 pid
)
111 return &hash
->table
[jhash_1word(pid
, hash
->rnd
) & hash
->mask
];
114 static void netlink_sock_destruct(struct sock
*sk
)
116 skb_queue_purge(&sk
->sk_receive_queue
);
118 if (!sock_flag(sk
, SOCK_DEAD
)) {
119 printk("Freeing alive netlink socket %p\n", sk
);
122 BUG_TRAP(!atomic_read(&sk
->sk_rmem_alloc
));
123 BUG_TRAP(!atomic_read(&sk
->sk_wmem_alloc
));
124 BUG_TRAP(!nlk_sk(sk
)->cb
);
127 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP.
128 * Look, when several writers sleep and reader wakes them up, all but one
129 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
130 * this, _but_ remember, it adds useless work on UP machines.
133 static void netlink_table_grab(void)
135 write_lock_bh(&nl_table_lock
);
137 if (atomic_read(&nl_table_users
)) {
138 DECLARE_WAITQUEUE(wait
, current
);
140 add_wait_queue_exclusive(&nl_table_wait
, &wait
);
142 set_current_state(TASK_UNINTERRUPTIBLE
);
143 if (atomic_read(&nl_table_users
) == 0)
145 write_unlock_bh(&nl_table_lock
);
147 write_lock_bh(&nl_table_lock
);
150 __set_current_state(TASK_RUNNING
);
151 remove_wait_queue(&nl_table_wait
, &wait
);
155 static __inline__
void netlink_table_ungrab(void)
157 write_unlock_bh(&nl_table_lock
);
158 wake_up(&nl_table_wait
);
161 static __inline__
void
162 netlink_lock_table(void)
164 /* read_lock() synchronizes us to netlink_table_grab */
166 read_lock(&nl_table_lock
);
167 atomic_inc(&nl_table_users
);
168 read_unlock(&nl_table_lock
);
171 static __inline__
void
172 netlink_unlock_table(void)
174 if (atomic_dec_and_test(&nl_table_users
))
175 wake_up(&nl_table_wait
);
178 static __inline__
struct sock
*netlink_lookup(int protocol
, u32 pid
)
180 struct nl_pid_hash
*hash
= &nl_table
[protocol
].hash
;
181 struct hlist_head
*head
;
183 struct hlist_node
*node
;
185 read_lock(&nl_table_lock
);
186 head
= nl_pid_hashfn(hash
, pid
);
187 sk_for_each(sk
, node
, head
) {
188 if (nlk_sk(sk
)->pid
== pid
) {
195 read_unlock(&nl_table_lock
);
199 static inline struct hlist_head
*nl_pid_hash_alloc(size_t size
)
201 if (size
<= PAGE_SIZE
)
202 return kmalloc(size
, GFP_ATOMIC
);
204 return (struct hlist_head
*)
205 __get_free_pages(GFP_ATOMIC
, get_order(size
));
208 static inline void nl_pid_hash_free(struct hlist_head
*table
, size_t size
)
210 if (size
<= PAGE_SIZE
)
213 free_pages((unsigned long)table
, get_order(size
));
216 static int nl_pid_hash_rehash(struct nl_pid_hash
*hash
, int grow
)
218 unsigned int omask
, mask
, shift
;
220 struct hlist_head
*otable
, *table
;
223 omask
= mask
= hash
->mask
;
224 osize
= size
= (mask
+ 1) * sizeof(*table
);
228 if (++shift
> hash
->max_shift
)
234 table
= nl_pid_hash_alloc(size
);
238 memset(table
, 0, size
);
239 otable
= hash
->table
;
243 get_random_bytes(&hash
->rnd
, sizeof(hash
->rnd
));
245 for (i
= 0; i
<= omask
; i
++) {
247 struct hlist_node
*node
, *tmp
;
249 sk_for_each_safe(sk
, node
, tmp
, &otable
[i
])
250 __sk_add_node(sk
, nl_pid_hashfn(hash
, nlk_sk(sk
)->pid
));
253 nl_pid_hash_free(otable
, osize
);
254 hash
->rehash_time
= jiffies
+ 10 * 60 * HZ
;
258 static inline int nl_pid_hash_dilute(struct nl_pid_hash
*hash
, int len
)
260 int avg
= hash
->entries
>> hash
->shift
;
262 if (unlikely(avg
> 1) && nl_pid_hash_rehash(hash
, 1))
265 if (unlikely(len
> avg
) && time_after(jiffies
, hash
->rehash_time
)) {
266 nl_pid_hash_rehash(hash
, 0);
273 static struct proto_ops netlink_ops
;
275 static int netlink_insert(struct sock
*sk
, u32 pid
)
277 struct nl_pid_hash
*hash
= &nl_table
[sk
->sk_protocol
].hash
;
278 struct hlist_head
*head
;
279 int err
= -EADDRINUSE
;
281 struct hlist_node
*node
;
284 netlink_table_grab();
285 head
= nl_pid_hashfn(hash
, pid
);
287 sk_for_each(osk
, node
, head
) {
288 if (nlk_sk(osk
)->pid
== pid
)
300 if (BITS_PER_LONG
> 32 && unlikely(hash
->entries
>= UINT_MAX
))
303 if (len
&& nl_pid_hash_dilute(hash
, len
))
304 head
= nl_pid_hashfn(hash
, pid
);
306 nlk_sk(sk
)->pid
= pid
;
307 sk_add_node(sk
, head
);
311 netlink_table_ungrab();
315 static void netlink_remove(struct sock
*sk
)
317 netlink_table_grab();
318 if (sk_del_node_init(sk
))
319 nl_table
[sk
->sk_protocol
].hash
.entries
--;
320 if (nlk_sk(sk
)->groups
)
321 __sk_del_bind_node(sk
);
322 netlink_table_ungrab();
325 static struct proto netlink_proto
= {
327 .owner
= THIS_MODULE
,
328 .obj_size
= sizeof(struct netlink_sock
),
331 static int netlink_create(struct socket
*sock
, int protocol
)
334 struct netlink_sock
*nlk
;
336 sock
->state
= SS_UNCONNECTED
;
338 if (sock
->type
!= SOCK_RAW
&& sock
->type
!= SOCK_DGRAM
)
339 return -ESOCKTNOSUPPORT
;
341 if (protocol
<0 || protocol
>= MAX_LINKS
)
342 return -EPROTONOSUPPORT
;
344 sock
->ops
= &netlink_ops
;
346 sk
= sk_alloc(PF_NETLINK
, GFP_KERNEL
, &netlink_proto
, 1);
350 sock_init_data(sock
, sk
);
354 spin_lock_init(&nlk
->cb_lock
);
355 init_waitqueue_head(&nlk
->wait
);
356 sk
->sk_destruct
= netlink_sock_destruct
;
358 sk
->sk_protocol
= protocol
;
362 static int netlink_release(struct socket
*sock
)
364 struct sock
*sk
= sock
->sk
;
365 struct netlink_sock
*nlk
;
373 spin_lock(&nlk
->cb_lock
);
375 nlk
->cb
->done(nlk
->cb
);
376 netlink_destroy_callback(nlk
->cb
);
379 spin_unlock(&nlk
->cb_lock
);
381 /* OK. Socket is unlinked, and, therefore,
382 no new packets will arrive */
386 wake_up_interruptible_all(&nlk
->wait
);
388 skb_queue_purge(&sk
->sk_write_queue
);
390 if (nlk
->pid
&& !nlk
->groups
) {
391 struct netlink_notify n
= {
392 .protocol
= sk
->sk_protocol
,
395 notifier_call_chain(&netlink_chain
, NETLINK_URELEASE
, &n
);
402 static int netlink_autobind(struct socket
*sock
)
404 struct sock
*sk
= sock
->sk
;
405 struct nl_pid_hash
*hash
= &nl_table
[sk
->sk_protocol
].hash
;
406 struct hlist_head
*head
;
408 struct hlist_node
*node
;
409 s32 pid
= current
->pid
;
411 static s32 rover
= -4097;
415 netlink_table_grab();
416 head
= nl_pid_hashfn(hash
, pid
);
417 sk_for_each(osk
, node
, head
) {
418 if (nlk_sk(osk
)->pid
== pid
) {
419 /* Bind collision, search negative pid values. */
423 netlink_table_ungrab();
427 netlink_table_ungrab();
429 err
= netlink_insert(sk
, pid
);
430 if (err
== -EADDRINUSE
)
433 /* If 2 threads race to autobind, that is fine. */
440 static inline int netlink_capable(struct socket
*sock
, unsigned int flag
)
442 return (nl_table
[sock
->sk
->sk_protocol
].nl_nonroot
& flag
) ||
443 capable(CAP_NET_ADMIN
);
446 static int netlink_bind(struct socket
*sock
, struct sockaddr
*addr
, int addr_len
)
448 struct sock
*sk
= sock
->sk
;
449 struct netlink_sock
*nlk
= nlk_sk(sk
);
450 struct sockaddr_nl
*nladdr
= (struct sockaddr_nl
*)addr
;
453 if (nladdr
->nl_family
!= AF_NETLINK
)
456 /* Only superuser is allowed to listen multicasts */
457 if (nladdr
->nl_groups
&& !netlink_capable(sock
, NL_NONROOT_RECV
))
461 if (nladdr
->nl_pid
!= nlk
->pid
)
464 err
= nladdr
->nl_pid
?
465 netlink_insert(sk
, nladdr
->nl_pid
) :
466 netlink_autobind(sock
);
471 if (!nladdr
->nl_groups
&& !nlk
->groups
)
474 netlink_table_grab();
475 if (nlk
->groups
&& !nladdr
->nl_groups
)
476 __sk_del_bind_node(sk
);
477 else if (!nlk
->groups
&& nladdr
->nl_groups
)
478 sk_add_bind_node(sk
, &nl_table
[sk
->sk_protocol
].mc_list
);
479 nlk
->groups
= nladdr
->nl_groups
;
480 netlink_table_ungrab();
485 static int netlink_connect(struct socket
*sock
, struct sockaddr
*addr
,
489 struct sock
*sk
= sock
->sk
;
490 struct netlink_sock
*nlk
= nlk_sk(sk
);
491 struct sockaddr_nl
*nladdr
=(struct sockaddr_nl
*)addr
;
493 if (addr
->sa_family
== AF_UNSPEC
) {
494 sk
->sk_state
= NETLINK_UNCONNECTED
;
499 if (addr
->sa_family
!= AF_NETLINK
)
502 /* Only superuser is allowed to send multicasts */
503 if (nladdr
->nl_groups
&& !netlink_capable(sock
, NL_NONROOT_SEND
))
507 err
= netlink_autobind(sock
);
510 sk
->sk_state
= NETLINK_CONNECTED
;
511 nlk
->dst_pid
= nladdr
->nl_pid
;
512 nlk
->dst_groups
= nladdr
->nl_groups
;
518 static int netlink_getname(struct socket
*sock
, struct sockaddr
*addr
, int *addr_len
, int peer
)
520 struct sock
*sk
= sock
->sk
;
521 struct netlink_sock
*nlk
= nlk_sk(sk
);
522 struct sockaddr_nl
*nladdr
=(struct sockaddr_nl
*)addr
;
524 nladdr
->nl_family
= AF_NETLINK
;
526 *addr_len
= sizeof(*nladdr
);
529 nladdr
->nl_pid
= nlk
->dst_pid
;
530 nladdr
->nl_groups
= nlk
->dst_groups
;
532 nladdr
->nl_pid
= nlk
->pid
;
533 nladdr
->nl_groups
= nlk
->groups
;
538 static void netlink_overrun(struct sock
*sk
)
540 if (!test_and_set_bit(0, &nlk_sk(sk
)->state
)) {
541 sk
->sk_err
= ENOBUFS
;
542 sk
->sk_error_report(sk
);
546 static struct sock
*netlink_getsockbypid(struct sock
*ssk
, u32 pid
)
548 int protocol
= ssk
->sk_protocol
;
550 struct netlink_sock
*nlk
;
552 sock
= netlink_lookup(protocol
, pid
);
554 return ERR_PTR(-ECONNREFUSED
);
556 /* Don't bother queuing skb if kernel socket has no input function */
558 if ((nlk
->pid
== 0 && !nlk
->data_ready
) ||
559 (sock
->sk_state
== NETLINK_CONNECTED
&&
560 nlk
->dst_pid
!= nlk_sk(ssk
)->pid
)) {
562 return ERR_PTR(-ECONNREFUSED
);
567 struct sock
*netlink_getsockbyfilp(struct file
*filp
)
569 struct inode
*inode
= filp
->f_dentry
->d_inode
;
572 if (!S_ISSOCK(inode
->i_mode
))
573 return ERR_PTR(-ENOTSOCK
);
575 sock
= SOCKET_I(inode
)->sk
;
576 if (sock
->sk_family
!= AF_NETLINK
)
577 return ERR_PTR(-EINVAL
);
584 * Attach a skb to a netlink socket.
585 * The caller must hold a reference to the destination socket. On error, the
586 * reference is dropped. The skb is not send to the destination, just all
587 * all error checks are performed and memory in the queue is reserved.
589 * < 0: error. skb freed, reference to sock dropped.
591 * 1: repeat lookup - reference dropped while waiting for socket memory.
593 int netlink_attachskb(struct sock
*sk
, struct sk_buff
*skb
, int nonblock
, long timeo
)
595 struct netlink_sock
*nlk
;
599 if (atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
||
600 test_bit(0, &nlk
->state
)) {
601 DECLARE_WAITQUEUE(wait
, current
);
610 __set_current_state(TASK_INTERRUPTIBLE
);
611 add_wait_queue(&nlk
->wait
, &wait
);
613 if ((atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
||
614 test_bit(0, &nlk
->state
)) &&
615 !sock_flag(sk
, SOCK_DEAD
))
616 timeo
= schedule_timeout(timeo
);
618 __set_current_state(TASK_RUNNING
);
619 remove_wait_queue(&nlk
->wait
, &wait
);
622 if (signal_pending(current
)) {
624 return sock_intr_errno(timeo
);
628 skb_set_owner_r(skb
, sk
);
632 int netlink_sendskb(struct sock
*sk
, struct sk_buff
*skb
, int protocol
)
634 struct netlink_sock
*nlk
;
639 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
640 sk
->sk_data_ready(sk
, len
);
645 void netlink_detachskb(struct sock
*sk
, struct sk_buff
*skb
)
651 static inline struct sk_buff
*netlink_trim(struct sk_buff
*skb
, int allocation
)
657 delta
= skb
->end
- skb
->tail
;
658 if (delta
* 2 < skb
->truesize
)
661 if (skb_shared(skb
)) {
662 struct sk_buff
*nskb
= skb_clone(skb
, allocation
);
669 if (!pskb_expand_head(skb
, 0, -delta
, allocation
))
670 skb
->truesize
-= delta
;
675 int netlink_unicast(struct sock
*ssk
, struct sk_buff
*skb
, u32 pid
, int nonblock
)
681 skb
= netlink_trim(skb
, gfp_any());
683 timeo
= sock_sndtimeo(ssk
, nonblock
);
685 sk
= netlink_getsockbypid(ssk
, pid
);
690 err
= netlink_attachskb(sk
, skb
, nonblock
, timeo
);
696 return netlink_sendskb(sk
, skb
, ssk
->sk_protocol
);
699 static __inline__
int netlink_broadcast_deliver(struct sock
*sk
, struct sk_buff
*skb
)
701 struct netlink_sock
*nlk
= nlk_sk(sk
);
703 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
&&
704 !test_bit(0, &nlk
->state
)) {
705 skb_set_owner_r(skb
, sk
);
706 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
707 sk
->sk_data_ready(sk
, skb
->len
);
708 return atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
;
713 struct netlink_broadcast_data
{
714 struct sock
*exclude_sk
;
721 struct sk_buff
*skb
, *skb2
;
724 static inline int do_one_broadcast(struct sock
*sk
,
725 struct netlink_broadcast_data
*p
)
727 struct netlink_sock
*nlk
= nlk_sk(sk
);
730 if (p
->exclude_sk
== sk
)
733 if (nlk
->pid
== p
->pid
|| !(nlk
->groups
& p
->group
))
742 if (p
->skb2
== NULL
) {
743 if (skb_shared(p
->skb
)) {
744 p
->skb2
= skb_clone(p
->skb
, p
->allocation
);
746 p
->skb2
= skb_get(p
->skb
);
748 * skb ownership may have been set when
749 * delivered to a previous socket.
754 if (p
->skb2
== NULL
) {
756 /* Clone failed. Notify ALL listeners. */
758 } else if ((val
= netlink_broadcast_deliver(sk
, p
->skb2
)) < 0) {
771 int netlink_broadcast(struct sock
*ssk
, struct sk_buff
*skb
, u32 pid
,
772 u32 group
, int allocation
)
774 struct netlink_broadcast_data info
;
775 struct hlist_node
*node
;
778 skb
= netlink_trim(skb
, allocation
);
780 info
.exclude_sk
= ssk
;
786 info
.allocation
= allocation
;
790 /* While we sleep in clone, do not allow to change socket list */
792 netlink_lock_table();
794 sk_for_each_bound(sk
, node
, &nl_table
[ssk
->sk_protocol
].mc_list
)
795 do_one_broadcast(sk
, &info
);
799 netlink_unlock_table();
802 kfree_skb(info
.skb2
);
804 if (info
.delivered
) {
805 if (info
.congested
&& (allocation
& __GFP_WAIT
))
814 struct netlink_set_err_data
{
815 struct sock
*exclude_sk
;
821 static inline int do_one_set_err(struct sock
*sk
,
822 struct netlink_set_err_data
*p
)
824 struct netlink_sock
*nlk
= nlk_sk(sk
);
826 if (sk
== p
->exclude_sk
)
829 if (nlk
->pid
== p
->pid
|| !(nlk
->groups
& p
->group
))
832 sk
->sk_err
= p
->code
;
833 sk
->sk_error_report(sk
);
838 void netlink_set_err(struct sock
*ssk
, u32 pid
, u32 group
, int code
)
840 struct netlink_set_err_data info
;
841 struct hlist_node
*node
;
844 info
.exclude_sk
= ssk
;
849 read_lock(&nl_table_lock
);
851 sk_for_each_bound(sk
, node
, &nl_table
[ssk
->sk_protocol
].mc_list
)
852 do_one_set_err(sk
, &info
);
854 read_unlock(&nl_table_lock
);
857 static inline void netlink_rcv_wake(struct sock
*sk
)
859 struct netlink_sock
*nlk
= nlk_sk(sk
);
861 if (!skb_queue_len(&sk
->sk_receive_queue
))
862 clear_bit(0, &nlk
->state
);
863 if (!test_bit(0, &nlk
->state
))
864 wake_up_interruptible(&nlk
->wait
);
867 static int netlink_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
868 struct msghdr
*msg
, size_t len
)
870 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
871 struct sock
*sk
= sock
->sk
;
872 struct netlink_sock
*nlk
= nlk_sk(sk
);
873 struct sockaddr_nl
*addr
=msg
->msg_name
;
878 struct scm_cookie scm
;
880 if (msg
->msg_flags
&MSG_OOB
)
883 if (NULL
== siocb
->scm
)
885 err
= scm_send(sock
, msg
, siocb
->scm
);
889 if (msg
->msg_namelen
) {
890 if (addr
->nl_family
!= AF_NETLINK
)
892 dst_pid
= addr
->nl_pid
;
893 dst_groups
= addr
->nl_groups
;
894 if (dst_groups
&& !netlink_capable(sock
, NL_NONROOT_SEND
))
897 dst_pid
= nlk
->dst_pid
;
898 dst_groups
= nlk
->dst_groups
;
902 err
= netlink_autobind(sock
);
908 if (len
> sk
->sk_sndbuf
- 32)
911 skb
= alloc_skb(len
, GFP_KERNEL
);
915 NETLINK_CB(skb
).pid
= nlk
->pid
;
916 NETLINK_CB(skb
).groups
= nlk
->groups
;
917 NETLINK_CB(skb
).dst_pid
= dst_pid
;
918 NETLINK_CB(skb
).dst_groups
= dst_groups
;
919 NETLINK_CB(skb
).loginuid
= audit_get_loginuid(current
->audit_context
);
920 memcpy(NETLINK_CREDS(skb
), &siocb
->scm
->creds
, sizeof(struct ucred
));
922 /* What can I do? Netlink is asynchronous, so that
923 we will have to save current capabilities to
924 check them, when this message will be delivered
925 to corresponding kernel module. --ANK (980802)
929 if (memcpy_fromiovec(skb_put(skb
,len
), msg
->msg_iov
, len
)) {
934 err
= security_netlink_send(sk
, skb
);
941 atomic_inc(&skb
->users
);
942 netlink_broadcast(sk
, skb
, dst_pid
, dst_groups
, GFP_KERNEL
);
944 err
= netlink_unicast(sk
, skb
, dst_pid
, msg
->msg_flags
&MSG_DONTWAIT
);
950 static int netlink_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
951 struct msghdr
*msg
, size_t len
,
954 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
955 struct scm_cookie scm
;
956 struct sock
*sk
= sock
->sk
;
957 struct netlink_sock
*nlk
= nlk_sk(sk
);
958 int noblock
= flags
&MSG_DONTWAIT
;
968 skb
= skb_recv_datagram(sk
,flags
,noblock
,&err
);
972 msg
->msg_namelen
= 0;
976 msg
->msg_flags
|= MSG_TRUNC
;
980 skb
->h
.raw
= skb
->data
;
981 err
= skb_copy_datagram_iovec(skb
, 0, msg
->msg_iov
, copied
);
984 struct sockaddr_nl
*addr
= (struct sockaddr_nl
*)msg
->msg_name
;
985 addr
->nl_family
= AF_NETLINK
;
987 addr
->nl_pid
= NETLINK_CB(skb
).pid
;
988 addr
->nl_groups
= NETLINK_CB(skb
).dst_groups
;
989 msg
->msg_namelen
= sizeof(*addr
);
992 if (NULL
== siocb
->scm
) {
993 memset(&scm
, 0, sizeof(scm
));
996 siocb
->scm
->creds
= *NETLINK_CREDS(skb
);
997 skb_free_datagram(sk
, skb
);
999 if (nlk
->cb
&& atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
/ 2)
1002 scm_recv(sock
, msg
, siocb
->scm
, flags
);
1005 netlink_rcv_wake(sk
);
1006 return err
? : copied
;
1009 static void netlink_data_ready(struct sock
*sk
, int len
)
1011 struct netlink_sock
*nlk
= nlk_sk(sk
);
1013 if (nlk
->data_ready
)
1014 nlk
->data_ready(sk
, len
);
1015 netlink_rcv_wake(sk
);
1019 * We export these functions to other modules. They provide a
1020 * complete set of kernel non-blocking support for message
1025 netlink_kernel_create(int unit
, void (*input
)(struct sock
*sk
, int len
))
1027 struct socket
*sock
;
1033 if (unit
<0 || unit
>=MAX_LINKS
)
1036 if (sock_create_lite(PF_NETLINK
, SOCK_DGRAM
, unit
, &sock
))
1039 if (netlink_create(sock
, unit
) < 0) {
1044 sk
->sk_data_ready
= netlink_data_ready
;
1046 nlk_sk(sk
)->data_ready
= input
;
1048 if (netlink_insert(sk
, 0)) {
1055 void netlink_set_nonroot(int protocol
, unsigned int flags
)
1057 if ((unsigned int)protocol
< MAX_LINKS
)
1058 nl_table
[protocol
].nl_nonroot
= flags
;
1061 static void netlink_destroy_callback(struct netlink_callback
*cb
)
1069 * It looks a bit ugly.
1070 * It would be better to create kernel thread.
1073 static int netlink_dump(struct sock
*sk
)
1075 struct netlink_sock
*nlk
= nlk_sk(sk
);
1076 struct netlink_callback
*cb
;
1077 struct sk_buff
*skb
;
1078 struct nlmsghdr
*nlh
;
1081 skb
= sock_rmalloc(sk
, NLMSG_GOODSIZE
, 0, GFP_KERNEL
);
1085 spin_lock(&nlk
->cb_lock
);
1089 spin_unlock(&nlk
->cb_lock
);
1094 len
= cb
->dump(skb
, cb
);
1097 spin_unlock(&nlk
->cb_lock
);
1098 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
1099 sk
->sk_data_ready(sk
, len
);
1103 nlh
= __nlmsg_put(skb
, NETLINK_CB(cb
->skb
).pid
, cb
->nlh
->nlmsg_seq
, NLMSG_DONE
, sizeof(int));
1104 nlh
->nlmsg_flags
|= NLM_F_MULTI
;
1105 memcpy(NLMSG_DATA(nlh
), &len
, sizeof(len
));
1106 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
1107 sk
->sk_data_ready(sk
, skb
->len
);
1111 spin_unlock(&nlk
->cb_lock
);
1113 netlink_destroy_callback(cb
);
1117 int netlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
1118 struct nlmsghdr
*nlh
,
1119 int (*dump
)(struct sk_buff
*skb
, struct netlink_callback
*),
1120 int (*done
)(struct netlink_callback
*))
1122 struct netlink_callback
*cb
;
1124 struct netlink_sock
*nlk
;
1126 cb
= kmalloc(sizeof(*cb
), GFP_KERNEL
);
1130 memset(cb
, 0, sizeof(*cb
));
1134 atomic_inc(&skb
->users
);
1137 sk
= netlink_lookup(ssk
->sk_protocol
, NETLINK_CB(skb
).pid
);
1139 netlink_destroy_callback(cb
);
1140 return -ECONNREFUSED
;
1143 /* A dump is in progress... */
1144 spin_lock(&nlk
->cb_lock
);
1146 spin_unlock(&nlk
->cb_lock
);
1147 netlink_destroy_callback(cb
);
1152 spin_unlock(&nlk
->cb_lock
);
1159 void netlink_ack(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
, int err
)
1161 struct sk_buff
*skb
;
1162 struct nlmsghdr
*rep
;
1163 struct nlmsgerr
*errmsg
;
1167 size
= NLMSG_SPACE(sizeof(struct nlmsgerr
));
1169 size
= NLMSG_SPACE(4 + NLMSG_ALIGN(nlh
->nlmsg_len
));
1171 skb
= alloc_skb(size
, GFP_KERNEL
);
1175 sk
= netlink_lookup(in_skb
->sk
->sk_protocol
,
1176 NETLINK_CB(in_skb
).pid
);
1178 sk
->sk_err
= ENOBUFS
;
1179 sk
->sk_error_report(sk
);
1185 rep
= __nlmsg_put(skb
, NETLINK_CB(in_skb
).pid
, nlh
->nlmsg_seq
,
1186 NLMSG_ERROR
, sizeof(struct nlmsgerr
));
1187 errmsg
= NLMSG_DATA(rep
);
1188 errmsg
->error
= err
;
1189 memcpy(&errmsg
->msg
, nlh
, err
? nlh
->nlmsg_len
: sizeof(struct nlmsghdr
));
1190 netlink_unicast(in_skb
->sk
, skb
, NETLINK_CB(in_skb
).pid
, MSG_DONTWAIT
);
1194 #ifdef CONFIG_PROC_FS
1195 struct nl_seq_iter
{
1200 static struct sock
*netlink_seq_socket_idx(struct seq_file
*seq
, loff_t pos
)
1202 struct nl_seq_iter
*iter
= seq
->private;
1205 struct hlist_node
*node
;
1208 for (i
=0; i
<MAX_LINKS
; i
++) {
1209 struct nl_pid_hash
*hash
= &nl_table
[i
].hash
;
1211 for (j
= 0; j
<= hash
->mask
; j
++) {
1212 sk_for_each(s
, node
, &hash
->table
[j
]) {
1225 static void *netlink_seq_start(struct seq_file
*seq
, loff_t
*pos
)
1227 read_lock(&nl_table_lock
);
1228 return *pos
? netlink_seq_socket_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
1231 static void *netlink_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
1234 struct nl_seq_iter
*iter
;
1239 if (v
== SEQ_START_TOKEN
)
1240 return netlink_seq_socket_idx(seq
, 0);
1246 iter
= seq
->private;
1248 j
= iter
->hash_idx
+ 1;
1251 struct nl_pid_hash
*hash
= &nl_table
[i
].hash
;
1253 for (; j
<= hash
->mask
; j
++) {
1254 s
= sk_head(&hash
->table
[j
]);
1263 } while (++i
< MAX_LINKS
);
1268 static void netlink_seq_stop(struct seq_file
*seq
, void *v
)
1270 read_unlock(&nl_table_lock
);
1274 static int netlink_seq_show(struct seq_file
*seq
, void *v
)
1276 if (v
== SEQ_START_TOKEN
)
1278 "sk Eth Pid Groups "
1279 "Rmem Wmem Dump Locks\n");
1282 struct netlink_sock
*nlk
= nlk_sk(s
);
1284 seq_printf(seq
, "%p %-3d %-6d %08x %-8d %-8d %p %d\n",
1289 atomic_read(&s
->sk_rmem_alloc
),
1290 atomic_read(&s
->sk_wmem_alloc
),
1292 atomic_read(&s
->sk_refcnt
)
1299 static struct seq_operations netlink_seq_ops
= {
1300 .start
= netlink_seq_start
,
1301 .next
= netlink_seq_next
,
1302 .stop
= netlink_seq_stop
,
1303 .show
= netlink_seq_show
,
1307 static int netlink_seq_open(struct inode
*inode
, struct file
*file
)
1309 struct seq_file
*seq
;
1310 struct nl_seq_iter
*iter
;
1313 iter
= kmalloc(sizeof(*iter
), GFP_KERNEL
);
1317 err
= seq_open(file
, &netlink_seq_ops
);
1323 memset(iter
, 0, sizeof(*iter
));
1324 seq
= file
->private_data
;
1325 seq
->private = iter
;
1329 static struct file_operations netlink_seq_fops
= {
1330 .owner
= THIS_MODULE
,
1331 .open
= netlink_seq_open
,
1333 .llseek
= seq_lseek
,
1334 .release
= seq_release_private
,
1339 int netlink_register_notifier(struct notifier_block
*nb
)
1341 return notifier_chain_register(&netlink_chain
, nb
);
1344 int netlink_unregister_notifier(struct notifier_block
*nb
)
1346 return notifier_chain_unregister(&netlink_chain
, nb
);
1349 static struct proto_ops netlink_ops
= {
1350 .family
= PF_NETLINK
,
1351 .owner
= THIS_MODULE
,
1352 .release
= netlink_release
,
1353 .bind
= netlink_bind
,
1354 .connect
= netlink_connect
,
1355 .socketpair
= sock_no_socketpair
,
1356 .accept
= sock_no_accept
,
1357 .getname
= netlink_getname
,
1358 .poll
= datagram_poll
,
1359 .ioctl
= sock_no_ioctl
,
1360 .listen
= sock_no_listen
,
1361 .shutdown
= sock_no_shutdown
,
1362 .setsockopt
= sock_no_setsockopt
,
1363 .getsockopt
= sock_no_getsockopt
,
1364 .sendmsg
= netlink_sendmsg
,
1365 .recvmsg
= netlink_recvmsg
,
1366 .mmap
= sock_no_mmap
,
1367 .sendpage
= sock_no_sendpage
,
1370 static struct net_proto_family netlink_family_ops
= {
1371 .family
= PF_NETLINK
,
1372 .create
= netlink_create
,
1373 .owner
= THIS_MODULE
, /* for consistency 8) */
1376 extern void netlink_skb_parms_too_large(void);
1378 static int __init
netlink_proto_init(void)
1380 struct sk_buff
*dummy_skb
;
1384 int err
= proto_register(&netlink_proto
, 0);
1389 if (sizeof(struct netlink_skb_parms
) > sizeof(dummy_skb
->cb
))
1390 netlink_skb_parms_too_large();
1392 nl_table
= kmalloc(sizeof(*nl_table
) * MAX_LINKS
, GFP_KERNEL
);
1395 printk(KERN_CRIT
"netlink_init: Cannot allocate nl_table\n");
1399 memset(nl_table
, 0, sizeof(*nl_table
) * MAX_LINKS
);
1401 if (num_physpages
>= (128 * 1024))
1402 max
= num_physpages
>> (21 - PAGE_SHIFT
);
1404 max
= num_physpages
>> (23 - PAGE_SHIFT
);
1406 order
= get_bitmask_order(max
) - 1 + PAGE_SHIFT
;
1407 max
= (1UL << order
) / sizeof(struct hlist_head
);
1408 order
= get_bitmask_order(max
> UINT_MAX
? UINT_MAX
: max
) - 1;
1410 for (i
= 0; i
< MAX_LINKS
; i
++) {
1411 struct nl_pid_hash
*hash
= &nl_table
[i
].hash
;
1413 hash
->table
= nl_pid_hash_alloc(1 * sizeof(*hash
->table
));
1416 nl_pid_hash_free(nl_table
[i
].hash
.table
,
1417 1 * sizeof(*hash
->table
));
1421 memset(hash
->table
, 0, 1 * sizeof(*hash
->table
));
1422 hash
->max_shift
= order
;
1425 hash
->rehash_time
= jiffies
;
1428 sock_register(&netlink_family_ops
);
1429 #ifdef CONFIG_PROC_FS
1430 proc_net_fops_create("netlink", 0, &netlink_seq_fops
);
1432 /* The netlink device handler may be needed early. */
1438 static void __exit
netlink_proto_exit(void)
1440 sock_unregister(PF_NETLINK
);
1441 proc_net_remove("netlink");
1444 proto_unregister(&netlink_proto
);
1447 core_initcall(netlink_proto_init
);
1448 module_exit(netlink_proto_exit
);
1450 MODULE_LICENSE("GPL");
1452 MODULE_ALIAS_NETPROTO(PF_NETLINK
);
1454 EXPORT_SYMBOL(netlink_ack
);
1455 EXPORT_SYMBOL(netlink_broadcast
);
1456 EXPORT_SYMBOL(netlink_dump_start
);
1457 EXPORT_SYMBOL(netlink_kernel_create
);
1458 EXPORT_SYMBOL(netlink_register_notifier
);
1459 EXPORT_SYMBOL(netlink_set_err
);
1460 EXPORT_SYMBOL(netlink_set_nonroot
);
1461 EXPORT_SYMBOL(netlink_unicast
);
1462 EXPORT_SYMBOL(netlink_unregister_notifier
);