2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 * Patrick McHardy <kaber@trash.net>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
13 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
14 * added netlink_proto_exit
15 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
16 * use nlk_sk, as sk->protinfo is on a diet 8)
17 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
18 * - inc module use count of module that owns
19 * the kernel socket in case userspace opens
20 * socket of same protocol
21 * - remove all module support, since netlink is
22 * mandatory if CONFIG_NET=y these days
25 #include <linux/module.h>
27 #include <linux/capability.h>
28 #include <linux/kernel.h>
29 #include <linux/init.h>
30 #include <linux/signal.h>
31 #include <linux/sched.h>
32 #include <linux/errno.h>
33 #include <linux/string.h>
34 #include <linux/stat.h>
35 #include <linux/socket.h>
37 #include <linux/fcntl.h>
38 #include <linux/termios.h>
39 #include <linux/sockios.h>
40 #include <linux/net.h>
42 #include <linux/slab.h>
43 #include <asm/uaccess.h>
44 #include <linux/skbuff.h>
45 #include <linux/netdevice.h>
46 #include <linux/rtnetlink.h>
47 #include <linux/proc_fs.h>
48 #include <linux/seq_file.h>
49 #include <linux/notifier.h>
50 #include <linux/security.h>
51 #include <linux/jhash.h>
52 #include <linux/jiffies.h>
53 #include <linux/random.h>
54 #include <linux/bitops.h>
56 #include <linux/types.h>
57 #include <linux/audit.h>
58 #include <linux/mutex.h>
59 #include <linux/vmalloc.h>
60 #include <asm/cacheflush.h>
62 #include <net/net_namespace.h>
65 #include <net/netlink.h>
67 #include "af_netlink.h"
71 unsigned long masks
[0];
75 #define NETLINK_CONGESTED 0x0
78 #define NETLINK_KERNEL_SOCKET 0x1
79 #define NETLINK_RECV_PKTINFO 0x2
80 #define NETLINK_BROADCAST_SEND_ERROR 0x4
81 #define NETLINK_RECV_NO_ENOBUFS 0x8
83 static inline int netlink_is_kernel(struct sock
*sk
)
85 return nlk_sk(sk
)->flags
& NETLINK_KERNEL_SOCKET
;
88 struct netlink_table
*nl_table
;
89 EXPORT_SYMBOL_GPL(nl_table
);
91 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait
);
93 static int netlink_dump(struct sock
*sk
);
94 static void netlink_skb_destructor(struct sk_buff
*skb
);
96 DEFINE_RWLOCK(nl_table_lock
);
97 EXPORT_SYMBOL_GPL(nl_table_lock
);
98 static atomic_t nl_table_users
= ATOMIC_INIT(0);
100 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
102 static ATOMIC_NOTIFIER_HEAD(netlink_chain
);
104 static inline u32
netlink_group_mask(u32 group
)
106 return group
? 1 << (group
- 1) : 0;
109 static inline struct hlist_head
*nl_portid_hashfn(struct nl_portid_hash
*hash
, u32 portid
)
111 return &hash
->table
[jhash_1word(portid
, hash
->rnd
) & hash
->mask
];
114 static void netlink_overrun(struct sock
*sk
)
116 struct netlink_sock
*nlk
= nlk_sk(sk
);
118 if (!(nlk
->flags
& NETLINK_RECV_NO_ENOBUFS
)) {
119 if (!test_and_set_bit(NETLINK_CONGESTED
, &nlk_sk(sk
)->state
)) {
120 sk
->sk_err
= ENOBUFS
;
121 sk
->sk_error_report(sk
);
124 atomic_inc(&sk
->sk_drops
);
127 static void netlink_rcv_wake(struct sock
*sk
)
129 struct netlink_sock
*nlk
= nlk_sk(sk
);
131 if (skb_queue_empty(&sk
->sk_receive_queue
))
132 clear_bit(NETLINK_CONGESTED
, &nlk
->state
);
133 if (!test_bit(NETLINK_CONGESTED
, &nlk
->state
))
134 wake_up_interruptible(&nlk
->wait
);
137 #ifdef CONFIG_NETLINK_MMAP
138 static bool netlink_skb_is_mmaped(const struct sk_buff
*skb
)
140 return NETLINK_CB(skb
).flags
& NETLINK_SKB_MMAPED
;
143 static bool netlink_rx_is_mmaped(struct sock
*sk
)
145 return nlk_sk(sk
)->rx_ring
.pg_vec
!= NULL
;
148 static bool netlink_tx_is_mmaped(struct sock
*sk
)
150 return nlk_sk(sk
)->tx_ring
.pg_vec
!= NULL
;
153 static __pure
struct page
*pgvec_to_page(const void *addr
)
155 if (is_vmalloc_addr(addr
))
156 return vmalloc_to_page(addr
);
158 return virt_to_page(addr
);
161 static void free_pg_vec(void **pg_vec
, unsigned int order
, unsigned int len
)
165 for (i
= 0; i
< len
; i
++) {
166 if (pg_vec
[i
] != NULL
) {
167 if (is_vmalloc_addr(pg_vec
[i
]))
170 free_pages((unsigned long)pg_vec
[i
], order
);
176 static void *alloc_one_pg_vec_page(unsigned long order
)
179 gfp_t gfp_flags
= GFP_KERNEL
| __GFP_COMP
| __GFP_ZERO
|
180 __GFP_NOWARN
| __GFP_NORETRY
;
182 buffer
= (void *)__get_free_pages(gfp_flags
, order
);
186 buffer
= vzalloc((1 << order
) * PAGE_SIZE
);
190 gfp_flags
&= ~__GFP_NORETRY
;
191 return (void *)__get_free_pages(gfp_flags
, order
);
194 static void **alloc_pg_vec(struct netlink_sock
*nlk
,
195 struct nl_mmap_req
*req
, unsigned int order
)
197 unsigned int block_nr
= req
->nm_block_nr
;
201 pg_vec
= kcalloc(block_nr
, sizeof(void *), GFP_KERNEL
);
205 for (i
= 0; i
< block_nr
; i
++) {
206 pg_vec
[i
] = ptr
= alloc_one_pg_vec_page(order
);
207 if (pg_vec
[i
] == NULL
)
213 free_pg_vec(pg_vec
, order
, block_nr
);
217 static int netlink_set_ring(struct sock
*sk
, struct nl_mmap_req
*req
,
218 bool closing
, bool tx_ring
)
220 struct netlink_sock
*nlk
= nlk_sk(sk
);
221 struct netlink_ring
*ring
;
222 struct sk_buff_head
*queue
;
223 void **pg_vec
= NULL
;
224 unsigned int order
= 0;
227 ring
= tx_ring
? &nlk
->tx_ring
: &nlk
->rx_ring
;
228 queue
= tx_ring
? &sk
->sk_write_queue
: &sk
->sk_receive_queue
;
231 if (atomic_read(&nlk
->mapped
))
233 if (atomic_read(&ring
->pending
))
237 if (req
->nm_block_nr
) {
238 if (ring
->pg_vec
!= NULL
)
241 if ((int)req
->nm_block_size
<= 0)
243 if (!IS_ALIGNED(req
->nm_block_size
, PAGE_SIZE
))
245 if (req
->nm_frame_size
< NL_MMAP_HDRLEN
)
247 if (!IS_ALIGNED(req
->nm_frame_size
, NL_MMAP_MSG_ALIGNMENT
))
250 ring
->frames_per_block
= req
->nm_block_size
/
252 if (ring
->frames_per_block
== 0)
254 if (ring
->frames_per_block
* req
->nm_block_nr
!=
258 order
= get_order(req
->nm_block_size
);
259 pg_vec
= alloc_pg_vec(nlk
, req
, order
);
263 if (req
->nm_frame_nr
)
268 mutex_lock(&nlk
->pg_vec_lock
);
269 if (closing
|| atomic_read(&nlk
->mapped
) == 0) {
271 spin_lock_bh(&queue
->lock
);
273 ring
->frame_max
= req
->nm_frame_nr
- 1;
275 ring
->frame_size
= req
->nm_frame_size
;
276 ring
->pg_vec_pages
= req
->nm_block_size
/ PAGE_SIZE
;
278 swap(ring
->pg_vec_len
, req
->nm_block_nr
);
279 swap(ring
->pg_vec_order
, order
);
280 swap(ring
->pg_vec
, pg_vec
);
282 __skb_queue_purge(queue
);
283 spin_unlock_bh(&queue
->lock
);
285 WARN_ON(atomic_read(&nlk
->mapped
));
287 mutex_unlock(&nlk
->pg_vec_lock
);
290 free_pg_vec(pg_vec
, order
, req
->nm_block_nr
);
294 static void netlink_mm_open(struct vm_area_struct
*vma
)
296 struct file
*file
= vma
->vm_file
;
297 struct socket
*sock
= file
->private_data
;
298 struct sock
*sk
= sock
->sk
;
301 atomic_inc(&nlk_sk(sk
)->mapped
);
304 static void netlink_mm_close(struct vm_area_struct
*vma
)
306 struct file
*file
= vma
->vm_file
;
307 struct socket
*sock
= file
->private_data
;
308 struct sock
*sk
= sock
->sk
;
311 atomic_dec(&nlk_sk(sk
)->mapped
);
314 static const struct vm_operations_struct netlink_mmap_ops
= {
315 .open
= netlink_mm_open
,
316 .close
= netlink_mm_close
,
319 static int netlink_mmap(struct file
*file
, struct socket
*sock
,
320 struct vm_area_struct
*vma
)
322 struct sock
*sk
= sock
->sk
;
323 struct netlink_sock
*nlk
= nlk_sk(sk
);
324 struct netlink_ring
*ring
;
325 unsigned long start
, size
, expected
;
332 mutex_lock(&nlk
->pg_vec_lock
);
335 for (ring
= &nlk
->rx_ring
; ring
<= &nlk
->tx_ring
; ring
++) {
336 if (ring
->pg_vec
== NULL
)
338 expected
+= ring
->pg_vec_len
* ring
->pg_vec_pages
* PAGE_SIZE
;
344 size
= vma
->vm_end
- vma
->vm_start
;
345 if (size
!= expected
)
348 start
= vma
->vm_start
;
349 for (ring
= &nlk
->rx_ring
; ring
<= &nlk
->tx_ring
; ring
++) {
350 if (ring
->pg_vec
== NULL
)
353 for (i
= 0; i
< ring
->pg_vec_len
; i
++) {
355 void *kaddr
= ring
->pg_vec
[i
];
358 for (pg_num
= 0; pg_num
< ring
->pg_vec_pages
; pg_num
++) {
359 page
= pgvec_to_page(kaddr
);
360 err
= vm_insert_page(vma
, start
, page
);
369 atomic_inc(&nlk
->mapped
);
370 vma
->vm_ops
= &netlink_mmap_ops
;
373 mutex_unlock(&nlk
->pg_vec_lock
);
377 static void netlink_frame_flush_dcache(const struct nl_mmap_hdr
*hdr
)
379 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
380 struct page
*p_start
, *p_end
;
382 /* First page is flushed through netlink_{get,set}_status */
383 p_start
= pgvec_to_page(hdr
+ PAGE_SIZE
);
384 p_end
= pgvec_to_page((void *)hdr
+ NL_MMAP_HDRLEN
+ hdr
->nm_len
- 1);
385 while (p_start
<= p_end
) {
386 flush_dcache_page(p_start
);
392 static enum nl_mmap_status
netlink_get_status(const struct nl_mmap_hdr
*hdr
)
395 flush_dcache_page(pgvec_to_page(hdr
));
396 return hdr
->nm_status
;
399 static void netlink_set_status(struct nl_mmap_hdr
*hdr
,
400 enum nl_mmap_status status
)
402 hdr
->nm_status
= status
;
403 flush_dcache_page(pgvec_to_page(hdr
));
407 static struct nl_mmap_hdr
*
408 __netlink_lookup_frame(const struct netlink_ring
*ring
, unsigned int pos
)
410 unsigned int pg_vec_pos
, frame_off
;
412 pg_vec_pos
= pos
/ ring
->frames_per_block
;
413 frame_off
= pos
% ring
->frames_per_block
;
415 return ring
->pg_vec
[pg_vec_pos
] + (frame_off
* ring
->frame_size
);
418 static struct nl_mmap_hdr
*
419 netlink_lookup_frame(const struct netlink_ring
*ring
, unsigned int pos
,
420 enum nl_mmap_status status
)
422 struct nl_mmap_hdr
*hdr
;
424 hdr
= __netlink_lookup_frame(ring
, pos
);
425 if (netlink_get_status(hdr
) != status
)
431 static struct nl_mmap_hdr
*
432 netlink_current_frame(const struct netlink_ring
*ring
,
433 enum nl_mmap_status status
)
435 return netlink_lookup_frame(ring
, ring
->head
, status
);
438 static struct nl_mmap_hdr
*
439 netlink_previous_frame(const struct netlink_ring
*ring
,
440 enum nl_mmap_status status
)
444 prev
= ring
->head
? ring
->head
- 1 : ring
->frame_max
;
445 return netlink_lookup_frame(ring
, prev
, status
);
448 static void netlink_increment_head(struct netlink_ring
*ring
)
450 ring
->head
= ring
->head
!= ring
->frame_max
? ring
->head
+ 1 : 0;
453 static void netlink_forward_ring(struct netlink_ring
*ring
)
455 unsigned int head
= ring
->head
, pos
= head
;
456 const struct nl_mmap_hdr
*hdr
;
459 hdr
= __netlink_lookup_frame(ring
, pos
);
460 if (hdr
->nm_status
== NL_MMAP_STATUS_UNUSED
)
462 if (hdr
->nm_status
!= NL_MMAP_STATUS_SKIP
)
464 netlink_increment_head(ring
);
465 } while (ring
->head
!= head
);
468 static bool netlink_dump_space(struct netlink_sock
*nlk
)
470 struct netlink_ring
*ring
= &nlk
->rx_ring
;
471 struct nl_mmap_hdr
*hdr
;
474 hdr
= netlink_current_frame(ring
, NL_MMAP_STATUS_UNUSED
);
478 n
= ring
->head
+ ring
->frame_max
/ 2;
479 if (n
> ring
->frame_max
)
480 n
-= ring
->frame_max
;
482 hdr
= __netlink_lookup_frame(ring
, n
);
484 return hdr
->nm_status
== NL_MMAP_STATUS_UNUSED
;
487 static unsigned int netlink_poll(struct file
*file
, struct socket
*sock
,
490 struct sock
*sk
= sock
->sk
;
491 struct netlink_sock
*nlk
= nlk_sk(sk
);
495 if (nlk
->rx_ring
.pg_vec
!= NULL
) {
496 /* Memory mapped sockets don't call recvmsg(), so flow control
497 * for dumps is performed here. A dump is allowed to continue
498 * if at least half the ring is unused.
500 while (nlk
->cb
!= NULL
&& netlink_dump_space(nlk
)) {
501 err
= netlink_dump(sk
);
504 sk
->sk_error_report(sk
);
508 netlink_rcv_wake(sk
);
511 mask
= datagram_poll(file
, sock
, wait
);
513 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
514 if (nlk
->rx_ring
.pg_vec
) {
515 netlink_forward_ring(&nlk
->rx_ring
);
516 if (!netlink_previous_frame(&nlk
->rx_ring
, NL_MMAP_STATUS_UNUSED
))
517 mask
|= POLLIN
| POLLRDNORM
;
519 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
521 spin_lock_bh(&sk
->sk_write_queue
.lock
);
522 if (nlk
->tx_ring
.pg_vec
) {
523 if (netlink_current_frame(&nlk
->tx_ring
, NL_MMAP_STATUS_UNUSED
))
524 mask
|= POLLOUT
| POLLWRNORM
;
526 spin_unlock_bh(&sk
->sk_write_queue
.lock
);
531 static struct nl_mmap_hdr
*netlink_mmap_hdr(struct sk_buff
*skb
)
533 return (struct nl_mmap_hdr
*)(skb
->head
- NL_MMAP_HDRLEN
);
536 static void netlink_ring_setup_skb(struct sk_buff
*skb
, struct sock
*sk
,
537 struct netlink_ring
*ring
,
538 struct nl_mmap_hdr
*hdr
)
543 size
= ring
->frame_size
- NL_MMAP_HDRLEN
;
544 data
= (void *)hdr
+ NL_MMAP_HDRLEN
;
548 skb_reset_tail_pointer(skb
);
549 skb
->end
= skb
->tail
+ size
;
552 skb
->destructor
= netlink_skb_destructor
;
553 NETLINK_CB(skb
).flags
|= NETLINK_SKB_MMAPED
;
554 NETLINK_CB(skb
).sk
= sk
;
557 static int netlink_mmap_sendmsg(struct sock
*sk
, struct msghdr
*msg
,
558 u32 dst_portid
, u32 dst_group
,
559 struct sock_iocb
*siocb
)
561 struct netlink_sock
*nlk
= nlk_sk(sk
);
562 struct netlink_ring
*ring
;
563 struct nl_mmap_hdr
*hdr
;
567 int err
= 0, len
= 0;
569 /* Netlink messages are validated by the receiver before processing.
570 * In order to avoid userspace changing the contents of the message
571 * after validation, the socket and the ring may only be used by a
572 * single process, otherwise we fall back to copying.
574 if (atomic_long_read(&sk
->sk_socket
->file
->f_count
) > 2 ||
575 atomic_read(&nlk
->mapped
) > 1)
578 mutex_lock(&nlk
->pg_vec_lock
);
580 ring
= &nlk
->tx_ring
;
581 maxlen
= ring
->frame_size
- NL_MMAP_HDRLEN
;
584 hdr
= netlink_current_frame(ring
, NL_MMAP_STATUS_VALID
);
586 if (!(msg
->msg_flags
& MSG_DONTWAIT
) &&
587 atomic_read(&nlk
->tx_ring
.pending
))
591 if (hdr
->nm_len
> maxlen
) {
596 netlink_frame_flush_dcache(hdr
);
598 if (likely(dst_portid
== 0 && dst_group
== 0 && excl
)) {
599 skb
= alloc_skb_head(GFP_KERNEL
);
605 netlink_ring_setup_skb(skb
, sk
, ring
, hdr
);
606 NETLINK_CB(skb
).flags
|= NETLINK_SKB_TX
;
607 __skb_put(skb
, hdr
->nm_len
);
608 netlink_set_status(hdr
, NL_MMAP_STATUS_RESERVED
);
609 atomic_inc(&ring
->pending
);
611 skb
= alloc_skb(hdr
->nm_len
, GFP_KERNEL
);
616 __skb_put(skb
, hdr
->nm_len
);
617 memcpy(skb
->data
, (void *)hdr
+ NL_MMAP_HDRLEN
, hdr
->nm_len
);
618 netlink_set_status(hdr
, NL_MMAP_STATUS_UNUSED
);
621 netlink_increment_head(ring
);
623 NETLINK_CB(skb
).portid
= nlk
->portid
;
624 NETLINK_CB(skb
).dst_group
= dst_group
;
625 NETLINK_CB(skb
).creds
= siocb
->scm
->creds
;
627 err
= security_netlink_send(sk
, skb
);
633 if (unlikely(dst_group
)) {
634 atomic_inc(&skb
->users
);
635 netlink_broadcast(sk
, skb
, dst_portid
, dst_group
,
638 err
= netlink_unicast(sk
, skb
, dst_portid
,
639 msg
->msg_flags
& MSG_DONTWAIT
);
644 } while (hdr
!= NULL
||
645 (!(msg
->msg_flags
& MSG_DONTWAIT
) &&
646 atomic_read(&nlk
->tx_ring
.pending
)));
651 mutex_unlock(&nlk
->pg_vec_lock
);
655 static void netlink_queue_mmaped_skb(struct sock
*sk
, struct sk_buff
*skb
)
657 struct nl_mmap_hdr
*hdr
;
659 hdr
= netlink_mmap_hdr(skb
);
660 hdr
->nm_len
= skb
->len
;
661 hdr
->nm_group
= NETLINK_CB(skb
).dst_group
;
662 hdr
->nm_pid
= NETLINK_CB(skb
).creds
.pid
;
663 hdr
->nm_uid
= from_kuid(sk_user_ns(sk
), NETLINK_CB(skb
).creds
.uid
);
664 hdr
->nm_gid
= from_kgid(sk_user_ns(sk
), NETLINK_CB(skb
).creds
.gid
);
665 netlink_frame_flush_dcache(hdr
);
666 netlink_set_status(hdr
, NL_MMAP_STATUS_VALID
);
668 NETLINK_CB(skb
).flags
|= NETLINK_SKB_DELIVERED
;
672 static void netlink_ring_set_copied(struct sock
*sk
, struct sk_buff
*skb
)
674 struct netlink_sock
*nlk
= nlk_sk(sk
);
675 struct netlink_ring
*ring
= &nlk
->rx_ring
;
676 struct nl_mmap_hdr
*hdr
;
678 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
679 hdr
= netlink_current_frame(ring
, NL_MMAP_STATUS_UNUSED
);
681 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
686 netlink_increment_head(ring
);
687 __skb_queue_tail(&sk
->sk_receive_queue
, skb
);
688 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
690 hdr
->nm_len
= skb
->len
;
691 hdr
->nm_group
= NETLINK_CB(skb
).dst_group
;
692 hdr
->nm_pid
= NETLINK_CB(skb
).creds
.pid
;
693 hdr
->nm_uid
= from_kuid(sk_user_ns(sk
), NETLINK_CB(skb
).creds
.uid
);
694 hdr
->nm_gid
= from_kgid(sk_user_ns(sk
), NETLINK_CB(skb
).creds
.gid
);
695 netlink_set_status(hdr
, NL_MMAP_STATUS_COPY
);
698 #else /* CONFIG_NETLINK_MMAP */
699 #define netlink_skb_is_mmaped(skb) false
700 #define netlink_rx_is_mmaped(sk) false
701 #define netlink_tx_is_mmaped(sk) false
702 #define netlink_mmap sock_no_mmap
703 #define netlink_poll datagram_poll
704 #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
705 #endif /* CONFIG_NETLINK_MMAP */
707 static void netlink_destroy_callback(struct netlink_callback
*cb
)
713 static void netlink_consume_callback(struct netlink_callback
*cb
)
715 consume_skb(cb
->skb
);
719 static void netlink_skb_destructor(struct sk_buff
*skb
)
721 #ifdef CONFIG_NETLINK_MMAP
722 struct nl_mmap_hdr
*hdr
;
723 struct netlink_ring
*ring
;
726 /* If a packet from the kernel to userspace was freed because of an
727 * error without being delivered to userspace, the kernel must reset
728 * the status. In the direction userspace to kernel, the status is
729 * always reset here after the packet was processed and freed.
731 if (netlink_skb_is_mmaped(skb
)) {
732 hdr
= netlink_mmap_hdr(skb
);
733 sk
= NETLINK_CB(skb
).sk
;
735 if (NETLINK_CB(skb
).flags
& NETLINK_SKB_TX
) {
736 netlink_set_status(hdr
, NL_MMAP_STATUS_UNUSED
);
737 ring
= &nlk_sk(sk
)->tx_ring
;
739 if (!(NETLINK_CB(skb
).flags
& NETLINK_SKB_DELIVERED
)) {
741 netlink_set_status(hdr
, NL_MMAP_STATUS_VALID
);
743 ring
= &nlk_sk(sk
)->rx_ring
;
746 WARN_ON(atomic_read(&ring
->pending
) == 0);
747 atomic_dec(&ring
->pending
);
757 static void netlink_skb_set_owner_r(struct sk_buff
*skb
, struct sock
*sk
)
759 WARN_ON(skb
->sk
!= NULL
);
761 skb
->destructor
= netlink_skb_destructor
;
762 atomic_add(skb
->truesize
, &sk
->sk_rmem_alloc
);
763 sk_mem_charge(sk
, skb
->truesize
);
766 static void netlink_sock_destruct(struct sock
*sk
)
768 struct netlink_sock
*nlk
= nlk_sk(sk
);
772 nlk
->cb
->done(nlk
->cb
);
774 module_put(nlk
->cb
->module
);
775 netlink_destroy_callback(nlk
->cb
);
778 skb_queue_purge(&sk
->sk_receive_queue
);
779 #ifdef CONFIG_NETLINK_MMAP
781 struct nl_mmap_req req
;
783 memset(&req
, 0, sizeof(req
));
784 if (nlk
->rx_ring
.pg_vec
)
785 netlink_set_ring(sk
, &req
, true, false);
786 memset(&req
, 0, sizeof(req
));
787 if (nlk
->tx_ring
.pg_vec
)
788 netlink_set_ring(sk
, &req
, true, true);
790 #endif /* CONFIG_NETLINK_MMAP */
792 if (!sock_flag(sk
, SOCK_DEAD
)) {
793 printk(KERN_ERR
"Freeing alive netlink socket %p\n", sk
);
797 WARN_ON(atomic_read(&sk
->sk_rmem_alloc
));
798 WARN_ON(atomic_read(&sk
->sk_wmem_alloc
));
799 WARN_ON(nlk_sk(sk
)->groups
);
802 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
803 * SMP. Look, when several writers sleep and reader wakes them up, all but one
804 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
805 * this, _but_ remember, it adds useless work on UP machines.
808 void netlink_table_grab(void)
809 __acquires(nl_table_lock
)
813 write_lock_irq(&nl_table_lock
);
815 if (atomic_read(&nl_table_users
)) {
816 DECLARE_WAITQUEUE(wait
, current
);
818 add_wait_queue_exclusive(&nl_table_wait
, &wait
);
820 set_current_state(TASK_UNINTERRUPTIBLE
);
821 if (atomic_read(&nl_table_users
) == 0)
823 write_unlock_irq(&nl_table_lock
);
825 write_lock_irq(&nl_table_lock
);
828 __set_current_state(TASK_RUNNING
);
829 remove_wait_queue(&nl_table_wait
, &wait
);
833 void netlink_table_ungrab(void)
834 __releases(nl_table_lock
)
836 write_unlock_irq(&nl_table_lock
);
837 wake_up(&nl_table_wait
);
841 netlink_lock_table(void)
843 /* read_lock() synchronizes us to netlink_table_grab */
845 read_lock(&nl_table_lock
);
846 atomic_inc(&nl_table_users
);
847 read_unlock(&nl_table_lock
);
851 netlink_unlock_table(void)
853 if (atomic_dec_and_test(&nl_table_users
))
854 wake_up(&nl_table_wait
);
857 static struct sock
*netlink_lookup(struct net
*net
, int protocol
, u32 portid
)
859 struct nl_portid_hash
*hash
= &nl_table
[protocol
].hash
;
860 struct hlist_head
*head
;
863 read_lock(&nl_table_lock
);
864 head
= nl_portid_hashfn(hash
, portid
);
865 sk_for_each(sk
, head
) {
866 if (net_eq(sock_net(sk
), net
) && (nlk_sk(sk
)->portid
== portid
)) {
873 read_unlock(&nl_table_lock
);
877 static struct hlist_head
*nl_portid_hash_zalloc(size_t size
)
879 if (size
<= PAGE_SIZE
)
880 return kzalloc(size
, GFP_ATOMIC
);
882 return (struct hlist_head
*)
883 __get_free_pages(GFP_ATOMIC
| __GFP_ZERO
,
887 static void nl_portid_hash_free(struct hlist_head
*table
, size_t size
)
889 if (size
<= PAGE_SIZE
)
892 free_pages((unsigned long)table
, get_order(size
));
895 static int nl_portid_hash_rehash(struct nl_portid_hash
*hash
, int grow
)
897 unsigned int omask
, mask
, shift
;
899 struct hlist_head
*otable
, *table
;
902 omask
= mask
= hash
->mask
;
903 osize
= size
= (mask
+ 1) * sizeof(*table
);
907 if (++shift
> hash
->max_shift
)
913 table
= nl_portid_hash_zalloc(size
);
917 otable
= hash
->table
;
921 get_random_bytes(&hash
->rnd
, sizeof(hash
->rnd
));
923 for (i
= 0; i
<= omask
; i
++) {
925 struct hlist_node
*tmp
;
927 sk_for_each_safe(sk
, tmp
, &otable
[i
])
928 __sk_add_node(sk
, nl_portid_hashfn(hash
, nlk_sk(sk
)->portid
));
931 nl_portid_hash_free(otable
, osize
);
932 hash
->rehash_time
= jiffies
+ 10 * 60 * HZ
;
936 static inline int nl_portid_hash_dilute(struct nl_portid_hash
*hash
, int len
)
938 int avg
= hash
->entries
>> hash
->shift
;
940 if (unlikely(avg
> 1) && nl_portid_hash_rehash(hash
, 1))
943 if (unlikely(len
> avg
) && time_after(jiffies
, hash
->rehash_time
)) {
944 nl_portid_hash_rehash(hash
, 0);
951 static const struct proto_ops netlink_ops
;
954 netlink_update_listeners(struct sock
*sk
)
956 struct netlink_table
*tbl
= &nl_table
[sk
->sk_protocol
];
959 struct listeners
*listeners
;
961 listeners
= nl_deref_protected(tbl
->listeners
);
965 for (i
= 0; i
< NLGRPLONGS(tbl
->groups
); i
++) {
967 sk_for_each_bound(sk
, &tbl
->mc_list
) {
968 if (i
< NLGRPLONGS(nlk_sk(sk
)->ngroups
))
969 mask
|= nlk_sk(sk
)->groups
[i
];
971 listeners
->masks
[i
] = mask
;
973 /* this function is only called with the netlink table "grabbed", which
974 * makes sure updates are visible before bind or setsockopt return. */
977 static int netlink_insert(struct sock
*sk
, struct net
*net
, u32 portid
)
979 struct nl_portid_hash
*hash
= &nl_table
[sk
->sk_protocol
].hash
;
980 struct hlist_head
*head
;
981 int err
= -EADDRINUSE
;
985 netlink_table_grab();
986 head
= nl_portid_hashfn(hash
, portid
);
988 sk_for_each(osk
, head
) {
989 if (net_eq(sock_net(osk
), net
) && (nlk_sk(osk
)->portid
== portid
))
997 if (nlk_sk(sk
)->portid
)
1001 if (BITS_PER_LONG
> 32 && unlikely(hash
->entries
>= UINT_MAX
))
1004 if (len
&& nl_portid_hash_dilute(hash
, len
))
1005 head
= nl_portid_hashfn(hash
, portid
);
1007 nlk_sk(sk
)->portid
= portid
;
1008 sk_add_node(sk
, head
);
1012 netlink_table_ungrab();
1016 static void netlink_remove(struct sock
*sk
)
1018 netlink_table_grab();
1019 if (sk_del_node_init(sk
))
1020 nl_table
[sk
->sk_protocol
].hash
.entries
--;
1021 if (nlk_sk(sk
)->subscriptions
)
1022 __sk_del_bind_node(sk
);
1023 netlink_table_ungrab();
1026 static struct proto netlink_proto
= {
1028 .owner
= THIS_MODULE
,
1029 .obj_size
= sizeof(struct netlink_sock
),
1032 static int __netlink_create(struct net
*net
, struct socket
*sock
,
1033 struct mutex
*cb_mutex
, int protocol
)
1036 struct netlink_sock
*nlk
;
1038 sock
->ops
= &netlink_ops
;
1040 sk
= sk_alloc(net
, PF_NETLINK
, GFP_KERNEL
, &netlink_proto
);
1044 sock_init_data(sock
, sk
);
1048 nlk
->cb_mutex
= cb_mutex
;
1050 nlk
->cb_mutex
= &nlk
->cb_def_mutex
;
1051 mutex_init(nlk
->cb_mutex
);
1053 init_waitqueue_head(&nlk
->wait
);
1054 #ifdef CONFIG_NETLINK_MMAP
1055 mutex_init(&nlk
->pg_vec_lock
);
1058 sk
->sk_destruct
= netlink_sock_destruct
;
1059 sk
->sk_protocol
= protocol
;
1063 static int netlink_create(struct net
*net
, struct socket
*sock
, int protocol
,
1066 struct module
*module
= NULL
;
1067 struct mutex
*cb_mutex
;
1068 struct netlink_sock
*nlk
;
1069 void (*bind
)(int group
);
1072 sock
->state
= SS_UNCONNECTED
;
1074 if (sock
->type
!= SOCK_RAW
&& sock
->type
!= SOCK_DGRAM
)
1075 return -ESOCKTNOSUPPORT
;
1077 if (protocol
< 0 || protocol
>= MAX_LINKS
)
1078 return -EPROTONOSUPPORT
;
1080 netlink_lock_table();
1081 #ifdef CONFIG_MODULES
1082 if (!nl_table
[protocol
].registered
) {
1083 netlink_unlock_table();
1084 request_module("net-pf-%d-proto-%d", PF_NETLINK
, protocol
);
1085 netlink_lock_table();
1088 if (nl_table
[protocol
].registered
&&
1089 try_module_get(nl_table
[protocol
].module
))
1090 module
= nl_table
[protocol
].module
;
1092 err
= -EPROTONOSUPPORT
;
1093 cb_mutex
= nl_table
[protocol
].cb_mutex
;
1094 bind
= nl_table
[protocol
].bind
;
1095 netlink_unlock_table();
1100 err
= __netlink_create(net
, sock
, cb_mutex
, protocol
);
1105 sock_prot_inuse_add(net
, &netlink_proto
, 1);
1108 nlk
= nlk_sk(sock
->sk
);
1109 nlk
->module
= module
;
1110 nlk
->netlink_bind
= bind
;
1119 static int netlink_release(struct socket
*sock
)
1121 struct sock
*sk
= sock
->sk
;
1122 struct netlink_sock
*nlk
;
1132 * OK. Socket is unlinked, any packets that arrive now
1137 wake_up_interruptible_all(&nlk
->wait
);
1139 skb_queue_purge(&sk
->sk_write_queue
);
1142 struct netlink_notify n
= {
1143 .net
= sock_net(sk
),
1144 .protocol
= sk
->sk_protocol
,
1145 .portid
= nlk
->portid
,
1147 atomic_notifier_call_chain(&netlink_chain
,
1148 NETLINK_URELEASE
, &n
);
1151 module_put(nlk
->module
);
1153 netlink_table_grab();
1154 if (netlink_is_kernel(sk
)) {
1155 BUG_ON(nl_table
[sk
->sk_protocol
].registered
== 0);
1156 if (--nl_table
[sk
->sk_protocol
].registered
== 0) {
1157 struct listeners
*old
;
1159 old
= nl_deref_protected(nl_table
[sk
->sk_protocol
].listeners
);
1160 RCU_INIT_POINTER(nl_table
[sk
->sk_protocol
].listeners
, NULL
);
1161 kfree_rcu(old
, rcu
);
1162 nl_table
[sk
->sk_protocol
].module
= NULL
;
1163 nl_table
[sk
->sk_protocol
].bind
= NULL
;
1164 nl_table
[sk
->sk_protocol
].flags
= 0;
1165 nl_table
[sk
->sk_protocol
].registered
= 0;
1167 } else if (nlk
->subscriptions
) {
1168 netlink_update_listeners(sk
);
1170 netlink_table_ungrab();
1176 sock_prot_inuse_add(sock_net(sk
), &netlink_proto
, -1);
1182 static int netlink_autobind(struct socket
*sock
)
1184 struct sock
*sk
= sock
->sk
;
1185 struct net
*net
= sock_net(sk
);
1186 struct nl_portid_hash
*hash
= &nl_table
[sk
->sk_protocol
].hash
;
1187 struct hlist_head
*head
;
1189 s32 portid
= task_tgid_vnr(current
);
1191 static s32 rover
= -4097;
1195 netlink_table_grab();
1196 head
= nl_portid_hashfn(hash
, portid
);
1197 sk_for_each(osk
, head
) {
1198 if (!net_eq(sock_net(osk
), net
))
1200 if (nlk_sk(osk
)->portid
== portid
) {
1201 /* Bind collision, search negative portid values. */
1205 netlink_table_ungrab();
1209 netlink_table_ungrab();
1211 err
= netlink_insert(sk
, net
, portid
);
1212 if (err
== -EADDRINUSE
)
1215 /* If 2 threads race to autobind, that is fine. */
1223 * __netlink_ns_capable - General netlink message capability test
1224 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
1225 * @user_ns: The user namespace of the capability to use
1226 * @cap: The capability to use
1228 * Test to see if the opener of the socket we received the message
1229 * from had when the netlink socket was created and the sender of the
1230 * message has has the capability @cap in the user namespace @user_ns.
1232 bool __netlink_ns_capable(const struct netlink_skb_parms
*nsp
,
1233 struct user_namespace
*user_ns
, int cap
)
1235 return ((nsp
->flags
& NETLINK_SKB_DST
) ||
1236 file_ns_capable(nsp
->sk
->sk_socket
->file
, user_ns
, cap
)) &&
1237 ns_capable(user_ns
, cap
);
1239 EXPORT_SYMBOL(__netlink_ns_capable
);
1242 * netlink_ns_capable - General netlink message capability test
1243 * @skb: socket buffer holding a netlink command from userspace
1244 * @user_ns: The user namespace of the capability to use
1245 * @cap: The capability to use
1247 * Test to see if the opener of the socket we received the message
1248 * from had when the netlink socket was created and the sender of the
1249 * message has has the capability @cap in the user namespace @user_ns.
1251 bool netlink_ns_capable(const struct sk_buff
*skb
,
1252 struct user_namespace
*user_ns
, int cap
)
1254 return __netlink_ns_capable(&NETLINK_CB(skb
), user_ns
, cap
);
1256 EXPORT_SYMBOL(netlink_ns_capable
);
1259 * netlink_capable - Netlink global message capability test
1260 * @skb: socket buffer holding a netlink command from userspace
1261 * @cap: The capability to use
1263 * Test to see if the opener of the socket we received the message
1264 * from had when the netlink socket was created and the sender of the
1265 * message has has the capability @cap in all user namespaces.
1267 bool netlink_capable(const struct sk_buff
*skb
, int cap
)
1269 return netlink_ns_capable(skb
, &init_user_ns
, cap
);
1271 EXPORT_SYMBOL(netlink_capable
);
1274 * netlink_net_capable - Netlink network namespace message capability test
1275 * @skb: socket buffer holding a netlink command from userspace
1276 * @cap: The capability to use
1278 * Test to see if the opener of the socket we received the message
1279 * from had when the netlink socket was created and the sender of the
1280 * message has has the capability @cap over the network namespace of
1281 * the socket we received the message from.
1283 bool netlink_net_capable(const struct sk_buff
*skb
, int cap
)
1285 return netlink_ns_capable(skb
, sock_net(skb
->sk
)->user_ns
, cap
);
1287 EXPORT_SYMBOL(netlink_net_capable
);
1289 static inline int netlink_allowed(const struct socket
*sock
, unsigned int flag
)
1291 return (nl_table
[sock
->sk
->sk_protocol
].flags
& flag
) ||
1292 ns_capable(sock_net(sock
->sk
)->user_ns
, CAP_NET_ADMIN
);
1296 netlink_update_subscriptions(struct sock
*sk
, unsigned int subscriptions
)
1298 struct netlink_sock
*nlk
= nlk_sk(sk
);
1300 if (nlk
->subscriptions
&& !subscriptions
)
1301 __sk_del_bind_node(sk
);
1302 else if (!nlk
->subscriptions
&& subscriptions
)
1303 sk_add_bind_node(sk
, &nl_table
[sk
->sk_protocol
].mc_list
);
1304 nlk
->subscriptions
= subscriptions
;
1307 static int netlink_realloc_groups(struct sock
*sk
)
1309 struct netlink_sock
*nlk
= nlk_sk(sk
);
1310 unsigned int groups
;
1311 unsigned long *new_groups
;
1314 netlink_table_grab();
1316 groups
= nl_table
[sk
->sk_protocol
].groups
;
1317 if (!nl_table
[sk
->sk_protocol
].registered
) {
1322 if (nlk
->ngroups
>= groups
)
1325 new_groups
= krealloc(nlk
->groups
, NLGRPSZ(groups
), GFP_ATOMIC
);
1326 if (new_groups
== NULL
) {
1330 memset((char *)new_groups
+ NLGRPSZ(nlk
->ngroups
), 0,
1331 NLGRPSZ(groups
) - NLGRPSZ(nlk
->ngroups
));
1333 nlk
->groups
= new_groups
;
1334 nlk
->ngroups
= groups
;
1336 netlink_table_ungrab();
1340 static int netlink_bind(struct socket
*sock
, struct sockaddr
*addr
,
1343 struct sock
*sk
= sock
->sk
;
1344 struct net
*net
= sock_net(sk
);
1345 struct netlink_sock
*nlk
= nlk_sk(sk
);
1346 struct sockaddr_nl
*nladdr
= (struct sockaddr_nl
*)addr
;
1349 if (addr_len
< sizeof(struct sockaddr_nl
))
1352 if (nladdr
->nl_family
!= AF_NETLINK
)
1355 /* Only superuser is allowed to listen multicasts */
1356 if (nladdr
->nl_groups
) {
1357 if (!netlink_allowed(sock
, NL_CFG_F_NONROOT_RECV
))
1359 err
= netlink_realloc_groups(sk
);
1365 if (nladdr
->nl_pid
!= nlk
->portid
)
1368 err
= nladdr
->nl_pid
?
1369 netlink_insert(sk
, net
, nladdr
->nl_pid
) :
1370 netlink_autobind(sock
);
1375 if (!nladdr
->nl_groups
&& (nlk
->groups
== NULL
|| !(u32
)nlk
->groups
[0]))
1378 netlink_table_grab();
1379 netlink_update_subscriptions(sk
, nlk
->subscriptions
+
1380 hweight32(nladdr
->nl_groups
) -
1381 hweight32(nlk
->groups
[0]));
1382 nlk
->groups
[0] = (nlk
->groups
[0] & ~0xffffffffUL
) | nladdr
->nl_groups
;
1383 netlink_update_listeners(sk
);
1384 netlink_table_ungrab();
1386 if (nlk
->netlink_bind
&& nlk
->groups
[0]) {
1389 for (i
=0; i
<nlk
->ngroups
; i
++) {
1390 if (test_bit(i
, nlk
->groups
))
1391 nlk
->netlink_bind(i
);
1398 static int netlink_connect(struct socket
*sock
, struct sockaddr
*addr
,
1399 int alen
, int flags
)
1402 struct sock
*sk
= sock
->sk
;
1403 struct netlink_sock
*nlk
= nlk_sk(sk
);
1404 struct sockaddr_nl
*nladdr
= (struct sockaddr_nl
*)addr
;
1406 if (alen
< sizeof(addr
->sa_family
))
1409 if (addr
->sa_family
== AF_UNSPEC
) {
1410 sk
->sk_state
= NETLINK_UNCONNECTED
;
1411 nlk
->dst_portid
= 0;
1415 if (addr
->sa_family
!= AF_NETLINK
)
1418 /* Only superuser is allowed to send multicasts */
1419 if (nladdr
->nl_groups
&& !netlink_allowed(sock
, NL_CFG_F_NONROOT_SEND
))
1423 err
= netlink_autobind(sock
);
1426 sk
->sk_state
= NETLINK_CONNECTED
;
1427 nlk
->dst_portid
= nladdr
->nl_pid
;
1428 nlk
->dst_group
= ffs(nladdr
->nl_groups
);
1434 static int netlink_getname(struct socket
*sock
, struct sockaddr
*addr
,
1435 int *addr_len
, int peer
)
1437 struct sock
*sk
= sock
->sk
;
1438 struct netlink_sock
*nlk
= nlk_sk(sk
);
1439 DECLARE_SOCKADDR(struct sockaddr_nl
*, nladdr
, addr
);
1441 nladdr
->nl_family
= AF_NETLINK
;
1443 *addr_len
= sizeof(*nladdr
);
1446 nladdr
->nl_pid
= nlk
->dst_portid
;
1447 nladdr
->nl_groups
= netlink_group_mask(nlk
->dst_group
);
1449 nladdr
->nl_pid
= nlk
->portid
;
1450 nladdr
->nl_groups
= nlk
->groups
? nlk
->groups
[0] : 0;
1455 static struct sock
*netlink_getsockbyportid(struct sock
*ssk
, u32 portid
)
1458 struct netlink_sock
*nlk
;
1460 sock
= netlink_lookup(sock_net(ssk
), ssk
->sk_protocol
, portid
);
1462 return ERR_PTR(-ECONNREFUSED
);
1464 /* Don't bother queuing skb if kernel socket has no input function */
1466 if (sock
->sk_state
== NETLINK_CONNECTED
&&
1467 nlk
->dst_portid
!= nlk_sk(ssk
)->portid
) {
1469 return ERR_PTR(-ECONNREFUSED
);
1474 struct sock
*netlink_getsockbyfilp(struct file
*filp
)
1476 struct inode
*inode
= file_inode(filp
);
1479 if (!S_ISSOCK(inode
->i_mode
))
1480 return ERR_PTR(-ENOTSOCK
);
1482 sock
= SOCKET_I(inode
)->sk
;
1483 if (sock
->sk_family
!= AF_NETLINK
)
1484 return ERR_PTR(-EINVAL
);
1491 * Attach a skb to a netlink socket.
1492 * The caller must hold a reference to the destination socket. On error, the
1493 * reference is dropped. The skb is not send to the destination, just all
1494 * all error checks are performed and memory in the queue is reserved.
1496 * < 0: error. skb freed, reference to sock dropped.
1498 * 1: repeat lookup - reference dropped while waiting for socket memory.
1500 int netlink_attachskb(struct sock
*sk
, struct sk_buff
*skb
,
1501 long *timeo
, struct sock
*ssk
)
1503 struct netlink_sock
*nlk
;
1507 if ((atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
||
1508 test_bit(NETLINK_CONGESTED
, &nlk
->state
)) &&
1509 !netlink_skb_is_mmaped(skb
)) {
1510 DECLARE_WAITQUEUE(wait
, current
);
1512 if (!ssk
|| netlink_is_kernel(ssk
))
1513 netlink_overrun(sk
);
1519 __set_current_state(TASK_INTERRUPTIBLE
);
1520 add_wait_queue(&nlk
->wait
, &wait
);
1522 if ((atomic_read(&sk
->sk_rmem_alloc
) > sk
->sk_rcvbuf
||
1523 test_bit(NETLINK_CONGESTED
, &nlk
->state
)) &&
1524 !sock_flag(sk
, SOCK_DEAD
))
1525 *timeo
= schedule_timeout(*timeo
);
1527 __set_current_state(TASK_RUNNING
);
1528 remove_wait_queue(&nlk
->wait
, &wait
);
1531 if (signal_pending(current
)) {
1533 return sock_intr_errno(*timeo
);
1537 netlink_skb_set_owner_r(skb
, sk
);
1541 static int __netlink_sendskb(struct sock
*sk
, struct sk_buff
*skb
)
1545 #ifdef CONFIG_NETLINK_MMAP
1546 if (netlink_skb_is_mmaped(skb
))
1547 netlink_queue_mmaped_skb(sk
, skb
);
1548 else if (netlink_rx_is_mmaped(sk
))
1549 netlink_ring_set_copied(sk
, skb
);
1551 #endif /* CONFIG_NETLINK_MMAP */
1552 skb_queue_tail(&sk
->sk_receive_queue
, skb
);
1553 sk
->sk_data_ready(sk
, len
);
1557 int netlink_sendskb(struct sock
*sk
, struct sk_buff
*skb
)
1559 int len
= __netlink_sendskb(sk
, skb
);
1565 void netlink_detachskb(struct sock
*sk
, struct sk_buff
*skb
)
1571 static struct sk_buff
*netlink_trim(struct sk_buff
*skb
, gfp_t allocation
)
1575 WARN_ON(skb
->sk
!= NULL
);
1576 if (netlink_skb_is_mmaped(skb
))
1579 delta
= skb
->end
- skb
->tail
;
1580 if (delta
* 2 < skb
->truesize
)
1583 if (skb_shared(skb
)) {
1584 struct sk_buff
*nskb
= skb_clone(skb
, allocation
);
1591 if (!pskb_expand_head(skb
, 0, -delta
, allocation
))
1592 skb
->truesize
-= delta
;
1597 static int netlink_unicast_kernel(struct sock
*sk
, struct sk_buff
*skb
,
1601 struct netlink_sock
*nlk
= nlk_sk(sk
);
1603 ret
= -ECONNREFUSED
;
1604 if (nlk
->netlink_rcv
!= NULL
) {
1606 netlink_skb_set_owner_r(skb
, sk
);
1607 NETLINK_CB(skb
).sk
= ssk
;
1608 nlk
->netlink_rcv(skb
);
1617 int netlink_unicast(struct sock
*ssk
, struct sk_buff
*skb
,
1618 u32 portid
, int nonblock
)
1624 skb
= netlink_trim(skb
, gfp_any());
1626 timeo
= sock_sndtimeo(ssk
, nonblock
);
1628 sk
= netlink_getsockbyportid(ssk
, portid
);
1633 if (netlink_is_kernel(sk
))
1634 return netlink_unicast_kernel(sk
, skb
, ssk
);
1636 if (sk_filter(sk
, skb
)) {
1643 err
= netlink_attachskb(sk
, skb
, &timeo
, ssk
);
1649 return netlink_sendskb(sk
, skb
);
1651 EXPORT_SYMBOL(netlink_unicast
);
1653 struct sk_buff
*netlink_alloc_skb(struct sock
*ssk
, unsigned int size
,
1654 u32 dst_portid
, gfp_t gfp_mask
)
1656 #ifdef CONFIG_NETLINK_MMAP
1657 struct sock
*sk
= NULL
;
1658 struct sk_buff
*skb
;
1659 struct netlink_ring
*ring
;
1660 struct nl_mmap_hdr
*hdr
;
1661 unsigned int maxlen
;
1663 sk
= netlink_getsockbyportid(ssk
, dst_portid
);
1667 ring
= &nlk_sk(sk
)->rx_ring
;
1668 /* fast-path without atomic ops for common case: non-mmaped receiver */
1669 if (ring
->pg_vec
== NULL
)
1672 skb
= alloc_skb_head(gfp_mask
);
1676 spin_lock_bh(&sk
->sk_receive_queue
.lock
);
1677 /* check again under lock */
1678 if (ring
->pg_vec
== NULL
)
1681 maxlen
= ring
->frame_size
- NL_MMAP_HDRLEN
;
1685 netlink_forward_ring(ring
);
1686 hdr
= netlink_current_frame(ring
, NL_MMAP_STATUS_UNUSED
);
1689 netlink_ring_setup_skb(skb
, sk
, ring
, hdr
);
1690 netlink_set_status(hdr
, NL_MMAP_STATUS_RESERVED
);
1691 atomic_inc(&ring
->pending
);
1692 netlink_increment_head(ring
);
1694 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1699 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1700 netlink_overrun(sk
);
1707 spin_unlock_bh(&sk
->sk_receive_queue
.lock
);
1712 return alloc_skb(size
, gfp_mask
);
1714 EXPORT_SYMBOL_GPL(netlink_alloc_skb
);
1716 int netlink_has_listeners(struct sock
*sk
, unsigned int group
)
1719 struct listeners
*listeners
;
1721 BUG_ON(!netlink_is_kernel(sk
));
1724 listeners
= rcu_dereference(nl_table
[sk
->sk_protocol
].listeners
);
1726 if (listeners
&& group
- 1 < nl_table
[sk
->sk_protocol
].groups
)
1727 res
= test_bit(group
- 1, listeners
->masks
);
1733 EXPORT_SYMBOL_GPL(netlink_has_listeners
);
1735 static int netlink_broadcast_deliver(struct sock
*sk
, struct sk_buff
*skb
)
1737 struct netlink_sock
*nlk
= nlk_sk(sk
);
1739 if (atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
&&
1740 !test_bit(NETLINK_CONGESTED
, &nlk
->state
)) {
1741 netlink_skb_set_owner_r(skb
, sk
);
1742 __netlink_sendskb(sk
, skb
);
1743 return atomic_read(&sk
->sk_rmem_alloc
) > (sk
->sk_rcvbuf
>> 1);
1748 struct netlink_broadcast_data
{
1749 struct sock
*exclude_sk
;
1754 int delivery_failure
;
1758 struct sk_buff
*skb
, *skb2
;
1759 int (*tx_filter
)(struct sock
*dsk
, struct sk_buff
*skb
, void *data
);
1763 static int do_one_broadcast(struct sock
*sk
,
1764 struct netlink_broadcast_data
*p
)
1766 struct netlink_sock
*nlk
= nlk_sk(sk
);
1769 if (p
->exclude_sk
== sk
)
1772 if (nlk
->portid
== p
->portid
|| p
->group
- 1 >= nlk
->ngroups
||
1773 !test_bit(p
->group
- 1, nlk
->groups
))
1776 if (!net_eq(sock_net(sk
), p
->net
))
1780 netlink_overrun(sk
);
1785 if (p
->skb2
== NULL
) {
1786 if (skb_shared(p
->skb
)) {
1787 p
->skb2
= skb_clone(p
->skb
, p
->allocation
);
1789 p
->skb2
= skb_get(p
->skb
);
1791 * skb ownership may have been set when
1792 * delivered to a previous socket.
1794 skb_orphan(p
->skb2
);
1797 if (p
->skb2
== NULL
) {
1798 netlink_overrun(sk
);
1799 /* Clone failed. Notify ALL listeners. */
1801 if (nlk
->flags
& NETLINK_BROADCAST_SEND_ERROR
)
1802 p
->delivery_failure
= 1;
1803 } else if (p
->tx_filter
&& p
->tx_filter(sk
, p
->skb2
, p
->tx_data
)) {
1806 } else if (sk_filter(sk
, p
->skb2
)) {
1809 } else if ((val
= netlink_broadcast_deliver(sk
, p
->skb2
)) < 0) {
1810 netlink_overrun(sk
);
1811 if (nlk
->flags
& NETLINK_BROADCAST_SEND_ERROR
)
1812 p
->delivery_failure
= 1;
1814 p
->congested
|= val
;
1824 int netlink_broadcast_filtered(struct sock
*ssk
, struct sk_buff
*skb
, u32 portid
,
1825 u32 group
, gfp_t allocation
,
1826 int (*filter
)(struct sock
*dsk
, struct sk_buff
*skb
, void *data
),
1829 struct net
*net
= sock_net(ssk
);
1830 struct netlink_broadcast_data info
;
1833 skb
= netlink_trim(skb
, allocation
);
1835 info
.exclude_sk
= ssk
;
1837 info
.portid
= portid
;
1840 info
.delivery_failure
= 0;
1843 info
.allocation
= allocation
;
1846 info
.tx_filter
= filter
;
1847 info
.tx_data
= filter_data
;
1849 /* While we sleep in clone, do not allow to change socket list */
1851 netlink_lock_table();
1853 sk_for_each_bound(sk
, &nl_table
[ssk
->sk_protocol
].mc_list
)
1854 do_one_broadcast(sk
, &info
);
1858 netlink_unlock_table();
1860 if (info
.delivery_failure
) {
1861 kfree_skb(info
.skb2
);
1864 consume_skb(info
.skb2
);
1866 if (info
.delivered
) {
1867 if (info
.congested
&& (allocation
& __GFP_WAIT
))
1873 EXPORT_SYMBOL(netlink_broadcast_filtered
);
1875 int netlink_broadcast(struct sock
*ssk
, struct sk_buff
*skb
, u32 portid
,
1876 u32 group
, gfp_t allocation
)
1878 return netlink_broadcast_filtered(ssk
, skb
, portid
, group
, allocation
,
1881 EXPORT_SYMBOL(netlink_broadcast
);
1883 struct netlink_set_err_data
{
1884 struct sock
*exclude_sk
;
1890 static int do_one_set_err(struct sock
*sk
, struct netlink_set_err_data
*p
)
1892 struct netlink_sock
*nlk
= nlk_sk(sk
);
1895 if (sk
== p
->exclude_sk
)
1898 if (!net_eq(sock_net(sk
), sock_net(p
->exclude_sk
)))
1901 if (nlk
->portid
== p
->portid
|| p
->group
- 1 >= nlk
->ngroups
||
1902 !test_bit(p
->group
- 1, nlk
->groups
))
1905 if (p
->code
== ENOBUFS
&& nlk
->flags
& NETLINK_RECV_NO_ENOBUFS
) {
1910 sk
->sk_err
= p
->code
;
1911 sk
->sk_error_report(sk
);
1917 * netlink_set_err - report error to broadcast listeners
1918 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1919 * @portid: the PORTID of a process that we want to skip (if any)
1920 * @groups: the broadcast group that will notice the error
1921 * @code: error code, must be negative (as usual in kernelspace)
1923 * This function returns the number of broadcast listeners that have set the
1924 * NETLINK_RECV_NO_ENOBUFS socket option.
1926 int netlink_set_err(struct sock
*ssk
, u32 portid
, u32 group
, int code
)
1928 struct netlink_set_err_data info
;
1932 info
.exclude_sk
= ssk
;
1933 info
.portid
= portid
;
1935 /* sk->sk_err wants a positive error value */
1938 read_lock(&nl_table_lock
);
1940 sk_for_each_bound(sk
, &nl_table
[ssk
->sk_protocol
].mc_list
)
1941 ret
+= do_one_set_err(sk
, &info
);
1943 read_unlock(&nl_table_lock
);
1946 EXPORT_SYMBOL(netlink_set_err
);
1948 /* must be called with netlink table grabbed */
1949 static void netlink_update_socket_mc(struct netlink_sock
*nlk
,
1953 int old
, new = !!is_new
, subscriptions
;
1955 old
= test_bit(group
- 1, nlk
->groups
);
1956 subscriptions
= nlk
->subscriptions
- old
+ new;
1958 __set_bit(group
- 1, nlk
->groups
);
1960 __clear_bit(group
- 1, nlk
->groups
);
1961 netlink_update_subscriptions(&nlk
->sk
, subscriptions
);
1962 netlink_update_listeners(&nlk
->sk
);
1965 static int netlink_setsockopt(struct socket
*sock
, int level
, int optname
,
1966 char __user
*optval
, unsigned int optlen
)
1968 struct sock
*sk
= sock
->sk
;
1969 struct netlink_sock
*nlk
= nlk_sk(sk
);
1970 unsigned int val
= 0;
1973 if (level
!= SOL_NETLINK
)
1974 return -ENOPROTOOPT
;
1976 if (optname
!= NETLINK_RX_RING
&& optname
!= NETLINK_TX_RING
&&
1977 optlen
>= sizeof(int) &&
1978 get_user(val
, (unsigned int __user
*)optval
))
1982 case NETLINK_PKTINFO
:
1984 nlk
->flags
|= NETLINK_RECV_PKTINFO
;
1986 nlk
->flags
&= ~NETLINK_RECV_PKTINFO
;
1989 case NETLINK_ADD_MEMBERSHIP
:
1990 case NETLINK_DROP_MEMBERSHIP
: {
1991 if (!netlink_allowed(sock
, NL_CFG_F_NONROOT_RECV
))
1993 err
= netlink_realloc_groups(sk
);
1996 if (!val
|| val
- 1 >= nlk
->ngroups
)
1998 netlink_table_grab();
1999 netlink_update_socket_mc(nlk
, val
,
2000 optname
== NETLINK_ADD_MEMBERSHIP
);
2001 netlink_table_ungrab();
2003 if (nlk
->netlink_bind
)
2004 nlk
->netlink_bind(val
);
2009 case NETLINK_BROADCAST_ERROR
:
2011 nlk
->flags
|= NETLINK_BROADCAST_SEND_ERROR
;
2013 nlk
->flags
&= ~NETLINK_BROADCAST_SEND_ERROR
;
2016 case NETLINK_NO_ENOBUFS
:
2018 nlk
->flags
|= NETLINK_RECV_NO_ENOBUFS
;
2019 clear_bit(NETLINK_CONGESTED
, &nlk
->state
);
2020 wake_up_interruptible(&nlk
->wait
);
2022 nlk
->flags
&= ~NETLINK_RECV_NO_ENOBUFS
;
2026 #ifdef CONFIG_NETLINK_MMAP
2027 case NETLINK_RX_RING
:
2028 case NETLINK_TX_RING
: {
2029 struct nl_mmap_req req
;
2031 /* Rings might consume more memory than queue limits, require
2034 if (!capable(CAP_NET_ADMIN
))
2036 if (optlen
< sizeof(req
))
2038 if (copy_from_user(&req
, optval
, sizeof(req
)))
2040 err
= netlink_set_ring(sk
, &req
, false,
2041 optname
== NETLINK_TX_RING
);
2044 #endif /* CONFIG_NETLINK_MMAP */
2051 static int netlink_getsockopt(struct socket
*sock
, int level
, int optname
,
2052 char __user
*optval
, int __user
*optlen
)
2054 struct sock
*sk
= sock
->sk
;
2055 struct netlink_sock
*nlk
= nlk_sk(sk
);
2058 if (level
!= SOL_NETLINK
)
2059 return -ENOPROTOOPT
;
2061 if (get_user(len
, optlen
))
2067 case NETLINK_PKTINFO
:
2068 if (len
< sizeof(int))
2071 val
= nlk
->flags
& NETLINK_RECV_PKTINFO
? 1 : 0;
2072 if (put_user(len
, optlen
) ||
2073 put_user(val
, optval
))
2077 case NETLINK_BROADCAST_ERROR
:
2078 if (len
< sizeof(int))
2081 val
= nlk
->flags
& NETLINK_BROADCAST_SEND_ERROR
? 1 : 0;
2082 if (put_user(len
, optlen
) ||
2083 put_user(val
, optval
))
2087 case NETLINK_NO_ENOBUFS
:
2088 if (len
< sizeof(int))
2091 val
= nlk
->flags
& NETLINK_RECV_NO_ENOBUFS
? 1 : 0;
2092 if (put_user(len
, optlen
) ||
2093 put_user(val
, optval
))
2103 static void netlink_cmsg_recv_pktinfo(struct msghdr
*msg
, struct sk_buff
*skb
)
2105 struct nl_pktinfo info
;
2107 info
.group
= NETLINK_CB(skb
).dst_group
;
2108 put_cmsg(msg
, SOL_NETLINK
, NETLINK_PKTINFO
, sizeof(info
), &info
);
2111 static int netlink_sendmsg(struct kiocb
*kiocb
, struct socket
*sock
,
2112 struct msghdr
*msg
, size_t len
)
2114 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
2115 struct sock
*sk
= sock
->sk
;
2116 struct netlink_sock
*nlk
= nlk_sk(sk
);
2117 struct sockaddr_nl
*addr
= msg
->msg_name
;
2120 struct sk_buff
*skb
;
2122 struct scm_cookie scm
;
2123 u32 netlink_skb_flags
= 0;
2125 if (msg
->msg_flags
&MSG_OOB
)
2128 if (NULL
== siocb
->scm
)
2131 err
= scm_send(sock
, msg
, siocb
->scm
, true);
2135 if (msg
->msg_namelen
) {
2137 if (addr
->nl_family
!= AF_NETLINK
)
2139 dst_portid
= addr
->nl_pid
;
2140 dst_group
= ffs(addr
->nl_groups
);
2142 if ((dst_group
|| dst_portid
) &&
2143 !netlink_allowed(sock
, NL_CFG_F_NONROOT_SEND
))
2145 netlink_skb_flags
|= NETLINK_SKB_DST
;
2147 dst_portid
= nlk
->dst_portid
;
2148 dst_group
= nlk
->dst_group
;
2152 err
= netlink_autobind(sock
);
2157 if (netlink_tx_is_mmaped(sk
) &&
2158 msg
->msg_iov
->iov_base
== NULL
) {
2159 err
= netlink_mmap_sendmsg(sk
, msg
, dst_portid
, dst_group
,
2165 if (len
> sk
->sk_sndbuf
- 32)
2168 skb
= alloc_skb(len
, GFP_KERNEL
);
2172 NETLINK_CB(skb
).portid
= nlk
->portid
;
2173 NETLINK_CB(skb
).dst_group
= dst_group
;
2174 NETLINK_CB(skb
).creds
= siocb
->scm
->creds
;
2175 NETLINK_CB(skb
).flags
= netlink_skb_flags
;
2178 if (memcpy_fromiovec(skb_put(skb
, len
), msg
->msg_iov
, len
)) {
2183 err
= security_netlink_send(sk
, skb
);
2190 atomic_inc(&skb
->users
);
2191 netlink_broadcast(sk
, skb
, dst_portid
, dst_group
, GFP_KERNEL
);
2193 err
= netlink_unicast(sk
, skb
, dst_portid
, msg
->msg_flags
&MSG_DONTWAIT
);
2196 scm_destroy(siocb
->scm
);
2200 static int netlink_recvmsg(struct kiocb
*kiocb
, struct socket
*sock
,
2201 struct msghdr
*msg
, size_t len
,
2204 struct sock_iocb
*siocb
= kiocb_to_siocb(kiocb
);
2205 struct scm_cookie scm
;
2206 struct sock
*sk
= sock
->sk
;
2207 struct netlink_sock
*nlk
= nlk_sk(sk
);
2208 int noblock
= flags
&MSG_DONTWAIT
;
2210 struct sk_buff
*skb
, *data_skb
;
2218 skb
= skb_recv_datagram(sk
, flags
, noblock
, &err
);
2224 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2225 if (unlikely(skb_shinfo(skb
)->frag_list
)) {
2227 * If this skb has a frag_list, then here that means that we
2228 * will have to use the frag_list skb's data for compat tasks
2229 * and the regular skb's data for normal (non-compat) tasks.
2231 * If we need to send the compat skb, assign it to the
2232 * 'data_skb' variable so that it will be used below for data
2233 * copying. We keep 'skb' for everything else, including
2234 * freeing both later.
2236 if (flags
& MSG_CMSG_COMPAT
)
2237 data_skb
= skb_shinfo(skb
)->frag_list
;
2241 copied
= data_skb
->len
;
2243 msg
->msg_flags
|= MSG_TRUNC
;
2247 skb_reset_transport_header(data_skb
);
2248 err
= skb_copy_datagram_iovec(data_skb
, 0, msg
->msg_iov
, copied
);
2250 if (msg
->msg_name
) {
2251 struct sockaddr_nl
*addr
= (struct sockaddr_nl
*)msg
->msg_name
;
2252 addr
->nl_family
= AF_NETLINK
;
2254 addr
->nl_pid
= NETLINK_CB(skb
).portid
;
2255 addr
->nl_groups
= netlink_group_mask(NETLINK_CB(skb
).dst_group
);
2256 msg
->msg_namelen
= sizeof(*addr
);
2259 if (nlk
->flags
& NETLINK_RECV_PKTINFO
)
2260 netlink_cmsg_recv_pktinfo(msg
, skb
);
2262 if (NULL
== siocb
->scm
) {
2263 memset(&scm
, 0, sizeof(scm
));
2266 siocb
->scm
->creds
= *NETLINK_CREDS(skb
);
2267 if (flags
& MSG_TRUNC
)
2268 copied
= data_skb
->len
;
2270 skb_free_datagram(sk
, skb
);
2272 if (nlk
->cb
&& atomic_read(&sk
->sk_rmem_alloc
) <= sk
->sk_rcvbuf
/ 2) {
2273 ret
= netlink_dump(sk
);
2276 sk
->sk_error_report(sk
);
2280 scm_recv(sock
, msg
, siocb
->scm
, flags
);
2282 netlink_rcv_wake(sk
);
2283 return err
? : copied
;
2286 static void netlink_data_ready(struct sock
*sk
, int len
)
2292 * We export these functions to other modules. They provide a
2293 * complete set of kernel non-blocking support for message
2298 __netlink_kernel_create(struct net
*net
, int unit
, struct module
*module
,
2299 struct netlink_kernel_cfg
*cfg
)
2301 struct socket
*sock
;
2303 struct netlink_sock
*nlk
;
2304 struct listeners
*listeners
= NULL
;
2305 struct mutex
*cb_mutex
= cfg
? cfg
->cb_mutex
: NULL
;
2306 unsigned int groups
;
2310 if (unit
< 0 || unit
>= MAX_LINKS
)
2313 if (sock_create_lite(PF_NETLINK
, SOCK_DGRAM
, unit
, &sock
))
2317 * We have to just have a reference on the net from sk, but don't
2318 * get_net it. Besides, we cannot get and then put the net here.
2319 * So we create one inside init_net and the move it to net.
2322 if (__netlink_create(&init_net
, sock
, cb_mutex
, unit
) < 0)
2323 goto out_sock_release_nosk
;
2326 sk_change_net(sk
, net
);
2328 if (!cfg
|| cfg
->groups
< 32)
2331 groups
= cfg
->groups
;
2333 listeners
= kzalloc(sizeof(*listeners
) + NLGRPSZ(groups
), GFP_KERNEL
);
2335 goto out_sock_release
;
2337 sk
->sk_data_ready
= netlink_data_ready
;
2338 if (cfg
&& cfg
->input
)
2339 nlk_sk(sk
)->netlink_rcv
= cfg
->input
;
2341 if (netlink_insert(sk
, net
, 0))
2342 goto out_sock_release
;
2345 nlk
->flags
|= NETLINK_KERNEL_SOCKET
;
2347 netlink_table_grab();
2348 if (!nl_table
[unit
].registered
) {
2349 nl_table
[unit
].groups
= groups
;
2350 rcu_assign_pointer(nl_table
[unit
].listeners
, listeners
);
2351 nl_table
[unit
].cb_mutex
= cb_mutex
;
2352 nl_table
[unit
].module
= module
;
2354 nl_table
[unit
].bind
= cfg
->bind
;
2355 nl_table
[unit
].flags
= cfg
->flags
;
2357 nl_table
[unit
].registered
= 1;
2360 nl_table
[unit
].registered
++;
2362 netlink_table_ungrab();
2367 netlink_kernel_release(sk
);
2370 out_sock_release_nosk
:
2374 EXPORT_SYMBOL(__netlink_kernel_create
);
2377 netlink_kernel_release(struct sock
*sk
)
2379 sk_release_kernel(sk
);
2381 EXPORT_SYMBOL(netlink_kernel_release
);
2383 int __netlink_change_ngroups(struct sock
*sk
, unsigned int groups
)
2385 struct listeners
*new, *old
;
2386 struct netlink_table
*tbl
= &nl_table
[sk
->sk_protocol
];
2391 if (NLGRPSZ(tbl
->groups
) < NLGRPSZ(groups
)) {
2392 new = kzalloc(sizeof(*new) + NLGRPSZ(groups
), GFP_ATOMIC
);
2395 old
= nl_deref_protected(tbl
->listeners
);
2396 memcpy(new->masks
, old
->masks
, NLGRPSZ(tbl
->groups
));
2397 rcu_assign_pointer(tbl
->listeners
, new);
2399 kfree_rcu(old
, rcu
);
2401 tbl
->groups
= groups
;
2407 * netlink_change_ngroups - change number of multicast groups
2409 * This changes the number of multicast groups that are available
2410 * on a certain netlink family. Note that it is not possible to
2411 * change the number of groups to below 32. Also note that it does
2412 * not implicitly call netlink_clear_multicast_users() when the
2413 * number of groups is reduced.
2415 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2416 * @groups: The new number of groups.
2418 int netlink_change_ngroups(struct sock
*sk
, unsigned int groups
)
2422 netlink_table_grab();
2423 err
= __netlink_change_ngroups(sk
, groups
);
2424 netlink_table_ungrab();
2429 void __netlink_clear_multicast_users(struct sock
*ksk
, unsigned int group
)
2432 struct netlink_table
*tbl
= &nl_table
[ksk
->sk_protocol
];
2434 sk_for_each_bound(sk
, &tbl
->mc_list
)
2435 netlink_update_socket_mc(nlk_sk(sk
), group
, 0);
2439 * netlink_clear_multicast_users - kick off multicast listeners
2441 * This function removes all listeners from the given group.
2442 * @ksk: The kernel netlink socket, as returned by
2443 * netlink_kernel_create().
2444 * @group: The multicast group to clear.
2446 void netlink_clear_multicast_users(struct sock
*ksk
, unsigned int group
)
2448 netlink_table_grab();
2449 __netlink_clear_multicast_users(ksk
, group
);
2450 netlink_table_ungrab();
2454 __nlmsg_put(struct sk_buff
*skb
, u32 portid
, u32 seq
, int type
, int len
, int flags
)
2456 struct nlmsghdr
*nlh
;
2457 int size
= nlmsg_msg_size(len
);
2459 nlh
= (struct nlmsghdr
*)skb_put(skb
, NLMSG_ALIGN(size
));
2460 nlh
->nlmsg_type
= type
;
2461 nlh
->nlmsg_len
= size
;
2462 nlh
->nlmsg_flags
= flags
;
2463 nlh
->nlmsg_pid
= portid
;
2464 nlh
->nlmsg_seq
= seq
;
2465 if (!__builtin_constant_p(size
) || NLMSG_ALIGN(size
) - size
!= 0)
2466 memset(nlmsg_data(nlh
) + len
, 0, NLMSG_ALIGN(size
) - size
);
2469 EXPORT_SYMBOL(__nlmsg_put
);
2472 * It looks a bit ugly.
2473 * It would be better to create kernel thread.
2476 static int netlink_dump(struct sock
*sk
)
2478 struct netlink_sock
*nlk
= nlk_sk(sk
);
2479 struct netlink_callback
*cb
;
2480 struct sk_buff
*skb
= NULL
;
2481 struct nlmsghdr
*nlh
;
2482 int len
, err
= -ENOBUFS
;
2485 mutex_lock(nlk
->cb_mutex
);
2493 alloc_size
= max_t(int, cb
->min_dump_alloc
, NLMSG_GOODSIZE
);
2495 if (!netlink_rx_is_mmaped(sk
) &&
2496 atomic_read(&sk
->sk_rmem_alloc
) >= sk
->sk_rcvbuf
)
2498 skb
= netlink_alloc_skb(sk
, alloc_size
, nlk
->portid
, GFP_KERNEL
);
2501 netlink_skb_set_owner_r(skb
, sk
);
2503 len
= cb
->dump(skb
, cb
);
2506 mutex_unlock(nlk
->cb_mutex
);
2508 if (sk_filter(sk
, skb
))
2511 __netlink_sendskb(sk
, skb
);
2515 nlh
= nlmsg_put_answer(skb
, cb
, NLMSG_DONE
, sizeof(len
), NLM_F_MULTI
);
2519 nl_dump_check_consistent(cb
, nlh
);
2521 memcpy(nlmsg_data(nlh
), &len
, sizeof(len
));
2523 if (sk_filter(sk
, skb
))
2526 __netlink_sendskb(sk
, skb
);
2531 mutex_unlock(nlk
->cb_mutex
);
2533 module_put(cb
->module
);
2534 netlink_consume_callback(cb
);
2538 mutex_unlock(nlk
->cb_mutex
);
2543 int __netlink_dump_start(struct sock
*ssk
, struct sk_buff
*skb
,
2544 const struct nlmsghdr
*nlh
,
2545 struct netlink_dump_control
*control
)
2547 struct netlink_callback
*cb
;
2549 struct netlink_sock
*nlk
;
2552 cb
= kzalloc(sizeof(*cb
), GFP_KERNEL
);
2556 /* Memory mapped dump requests need to be copied to avoid looping
2557 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2558 * a reference to the skb.
2560 if (netlink_skb_is_mmaped(skb
)) {
2561 skb
= skb_copy(skb
, GFP_KERNEL
);
2567 atomic_inc(&skb
->users
);
2569 cb
->dump
= control
->dump
;
2570 cb
->done
= control
->done
;
2572 cb
->data
= control
->data
;
2573 cb
->module
= control
->module
;
2574 cb
->min_dump_alloc
= control
->min_dump_alloc
;
2577 sk
= netlink_lookup(sock_net(ssk
), ssk
->sk_protocol
, NETLINK_CB(skb
).portid
);
2579 netlink_destroy_callback(cb
);
2580 return -ECONNREFUSED
;
2584 mutex_lock(nlk
->cb_mutex
);
2585 /* A dump is in progress... */
2587 mutex_unlock(nlk
->cb_mutex
);
2588 netlink_destroy_callback(cb
);
2592 /* add reference of module which cb->dump belongs to */
2593 if (!try_module_get(cb
->module
)) {
2594 mutex_unlock(nlk
->cb_mutex
);
2595 netlink_destroy_callback(cb
);
2596 ret
= -EPROTONOSUPPORT
;
2601 mutex_unlock(nlk
->cb_mutex
);
2603 ret
= netlink_dump(sk
);
2610 /* We successfully started a dump, by returning -EINTR we
2611 * signal not to send ACK even if it was requested.
2615 EXPORT_SYMBOL(__netlink_dump_start
);
2617 void netlink_ack(struct sk_buff
*in_skb
, struct nlmsghdr
*nlh
, int err
)
2619 struct sk_buff
*skb
;
2620 struct nlmsghdr
*rep
;
2621 struct nlmsgerr
*errmsg
;
2622 size_t payload
= sizeof(*errmsg
);
2624 /* error messages get the original request appened */
2626 payload
+= nlmsg_len(nlh
);
2628 skb
= netlink_alloc_skb(in_skb
->sk
, nlmsg_total_size(payload
),
2629 NETLINK_CB(in_skb
).portid
, GFP_KERNEL
);
2633 sk
= netlink_lookup(sock_net(in_skb
->sk
),
2634 in_skb
->sk
->sk_protocol
,
2635 NETLINK_CB(in_skb
).portid
);
2637 sk
->sk_err
= ENOBUFS
;
2638 sk
->sk_error_report(sk
);
2644 rep
= __nlmsg_put(skb
, NETLINK_CB(in_skb
).portid
, nlh
->nlmsg_seq
,
2645 NLMSG_ERROR
, payload
, 0);
2646 errmsg
= nlmsg_data(rep
);
2647 errmsg
->error
= err
;
2648 memcpy(&errmsg
->msg
, nlh
, err
? nlh
->nlmsg_len
: sizeof(*nlh
));
2649 netlink_unicast(in_skb
->sk
, skb
, NETLINK_CB(in_skb
).portid
, MSG_DONTWAIT
);
2651 EXPORT_SYMBOL(netlink_ack
);
2653 int netlink_rcv_skb(struct sk_buff
*skb
, int (*cb
)(struct sk_buff
*,
2656 struct nlmsghdr
*nlh
;
2659 while (skb
->len
>= nlmsg_total_size(0)) {
2662 nlh
= nlmsg_hdr(skb
);
2665 if (nlh
->nlmsg_len
< NLMSG_HDRLEN
|| skb
->len
< nlh
->nlmsg_len
)
2668 /* Only requests are handled by the kernel */
2669 if (!(nlh
->nlmsg_flags
& NLM_F_REQUEST
))
2672 /* Skip control messages */
2673 if (nlh
->nlmsg_type
< NLMSG_MIN_TYPE
)
2681 if (nlh
->nlmsg_flags
& NLM_F_ACK
|| err
)
2682 netlink_ack(skb
, nlh
, err
);
2685 msglen
= NLMSG_ALIGN(nlh
->nlmsg_len
);
2686 if (msglen
> skb
->len
)
2688 skb_pull(skb
, msglen
);
2693 EXPORT_SYMBOL(netlink_rcv_skb
);
2696 * nlmsg_notify - send a notification netlink message
2697 * @sk: netlink socket to use
2698 * @skb: notification message
2699 * @portid: destination netlink portid for reports or 0
2700 * @group: destination multicast group or 0
2701 * @report: 1 to report back, 0 to disable
2702 * @flags: allocation flags
2704 int nlmsg_notify(struct sock
*sk
, struct sk_buff
*skb
, u32 portid
,
2705 unsigned int group
, int report
, gfp_t flags
)
2710 int exclude_portid
= 0;
2713 atomic_inc(&skb
->users
);
2714 exclude_portid
= portid
;
2717 /* errors reported via destination sk->sk_err, but propagate
2718 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
2719 err
= nlmsg_multicast(sk
, skb
, exclude_portid
, group
, flags
);
2725 err2
= nlmsg_unicast(sk
, skb
, portid
);
2726 if (!err
|| err
== -ESRCH
)
2732 EXPORT_SYMBOL(nlmsg_notify
);
2734 #ifdef CONFIG_PROC_FS
2735 struct nl_seq_iter
{
2736 struct seq_net_private p
;
2741 static struct sock
*netlink_seq_socket_idx(struct seq_file
*seq
, loff_t pos
)
2743 struct nl_seq_iter
*iter
= seq
->private;
2748 for (i
= 0; i
< MAX_LINKS
; i
++) {
2749 struct nl_portid_hash
*hash
= &nl_table
[i
].hash
;
2751 for (j
= 0; j
<= hash
->mask
; j
++) {
2752 sk_for_each(s
, &hash
->table
[j
]) {
2753 if (sock_net(s
) != seq_file_net(seq
))
2767 static void *netlink_seq_start(struct seq_file
*seq
, loff_t
*pos
)
2768 __acquires(nl_table_lock
)
2770 read_lock(&nl_table_lock
);
2771 return *pos
? netlink_seq_socket_idx(seq
, *pos
- 1) : SEQ_START_TOKEN
;
2774 static void *netlink_seq_next(struct seq_file
*seq
, void *v
, loff_t
*pos
)
2777 struct nl_seq_iter
*iter
;
2782 if (v
== SEQ_START_TOKEN
)
2783 return netlink_seq_socket_idx(seq
, 0);
2785 iter
= seq
->private;
2789 } while (s
&& sock_net(s
) != seq_file_net(seq
));
2794 j
= iter
->hash_idx
+ 1;
2797 struct nl_portid_hash
*hash
= &nl_table
[i
].hash
;
2799 for (; j
<= hash
->mask
; j
++) {
2800 s
= sk_head(&hash
->table
[j
]);
2801 while (s
&& sock_net(s
) != seq_file_net(seq
))
2811 } while (++i
< MAX_LINKS
);
2816 static void netlink_seq_stop(struct seq_file
*seq
, void *v
)
2817 __releases(nl_table_lock
)
2819 read_unlock(&nl_table_lock
);
2823 static int netlink_seq_show(struct seq_file
*seq
, void *v
)
2825 if (v
== SEQ_START_TOKEN
) {
2827 "sk Eth Pid Groups "
2828 "Rmem Wmem Dump Locks Drops Inode\n");
2831 struct netlink_sock
*nlk
= nlk_sk(s
);
2833 seq_printf(seq
, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2837 nlk
->groups
? (u32
)nlk
->groups
[0] : 0,
2838 sk_rmem_alloc_get(s
),
2839 sk_wmem_alloc_get(s
),
2841 atomic_read(&s
->sk_refcnt
),
2842 atomic_read(&s
->sk_drops
),
2850 static const struct seq_operations netlink_seq_ops
= {
2851 .start
= netlink_seq_start
,
2852 .next
= netlink_seq_next
,
2853 .stop
= netlink_seq_stop
,
2854 .show
= netlink_seq_show
,
2858 static int netlink_seq_open(struct inode
*inode
, struct file
*file
)
2860 return seq_open_net(inode
, file
, &netlink_seq_ops
,
2861 sizeof(struct nl_seq_iter
));
2864 static const struct file_operations netlink_seq_fops
= {
2865 .owner
= THIS_MODULE
,
2866 .open
= netlink_seq_open
,
2868 .llseek
= seq_lseek
,
2869 .release
= seq_release_net
,
2874 int netlink_register_notifier(struct notifier_block
*nb
)
2876 return atomic_notifier_chain_register(&netlink_chain
, nb
);
2878 EXPORT_SYMBOL(netlink_register_notifier
);
2880 int netlink_unregister_notifier(struct notifier_block
*nb
)
2882 return atomic_notifier_chain_unregister(&netlink_chain
, nb
);
2884 EXPORT_SYMBOL(netlink_unregister_notifier
);
2886 static const struct proto_ops netlink_ops
= {
2887 .family
= PF_NETLINK
,
2888 .owner
= THIS_MODULE
,
2889 .release
= netlink_release
,
2890 .bind
= netlink_bind
,
2891 .connect
= netlink_connect
,
2892 .socketpair
= sock_no_socketpair
,
2893 .accept
= sock_no_accept
,
2894 .getname
= netlink_getname
,
2895 .poll
= netlink_poll
,
2896 .ioctl
= sock_no_ioctl
,
2897 .listen
= sock_no_listen
,
2898 .shutdown
= sock_no_shutdown
,
2899 .setsockopt
= netlink_setsockopt
,
2900 .getsockopt
= netlink_getsockopt
,
2901 .sendmsg
= netlink_sendmsg
,
2902 .recvmsg
= netlink_recvmsg
,
2903 .mmap
= netlink_mmap
,
2904 .sendpage
= sock_no_sendpage
,
2907 static const struct net_proto_family netlink_family_ops
= {
2908 .family
= PF_NETLINK
,
2909 .create
= netlink_create
,
2910 .owner
= THIS_MODULE
, /* for consistency 8) */
2913 static int __net_init
netlink_net_init(struct net
*net
)
2915 #ifdef CONFIG_PROC_FS
2916 if (!proc_create("netlink", 0, net
->proc_net
, &netlink_seq_fops
))
2922 static void __net_exit
netlink_net_exit(struct net
*net
)
2924 #ifdef CONFIG_PROC_FS
2925 remove_proc_entry("netlink", net
->proc_net
);
2929 static void __init
netlink_add_usersock_entry(void)
2931 struct listeners
*listeners
;
2934 listeners
= kzalloc(sizeof(*listeners
) + NLGRPSZ(groups
), GFP_KERNEL
);
2936 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2938 netlink_table_grab();
2940 nl_table
[NETLINK_USERSOCK
].groups
= groups
;
2941 rcu_assign_pointer(nl_table
[NETLINK_USERSOCK
].listeners
, listeners
);
2942 nl_table
[NETLINK_USERSOCK
].module
= THIS_MODULE
;
2943 nl_table
[NETLINK_USERSOCK
].registered
= 1;
2944 nl_table
[NETLINK_USERSOCK
].flags
= NL_CFG_F_NONROOT_SEND
;
2946 netlink_table_ungrab();
2949 static struct pernet_operations __net_initdata netlink_net_ops
= {
2950 .init
= netlink_net_init
,
2951 .exit
= netlink_net_exit
,
2954 static int __init
netlink_proto_init(void)
2957 unsigned long limit
;
2959 int err
= proto_register(&netlink_proto
, 0);
2964 BUILD_BUG_ON(sizeof(struct netlink_skb_parms
) > FIELD_SIZEOF(struct sk_buff
, cb
));
2966 nl_table
= kcalloc(MAX_LINKS
, sizeof(*nl_table
), GFP_KERNEL
);
2970 if (totalram_pages
>= (128 * 1024))
2971 limit
= totalram_pages
>> (21 - PAGE_SHIFT
);
2973 limit
= totalram_pages
>> (23 - PAGE_SHIFT
);
2975 order
= get_bitmask_order(limit
) - 1 + PAGE_SHIFT
;
2976 limit
= (1UL << order
) / sizeof(struct hlist_head
);
2977 order
= get_bitmask_order(min(limit
, (unsigned long)UINT_MAX
)) - 1;
2979 for (i
= 0; i
< MAX_LINKS
; i
++) {
2980 struct nl_portid_hash
*hash
= &nl_table
[i
].hash
;
2982 hash
->table
= nl_portid_hash_zalloc(1 * sizeof(*hash
->table
));
2985 nl_portid_hash_free(nl_table
[i
].hash
.table
,
2986 1 * sizeof(*hash
->table
));
2990 hash
->max_shift
= order
;
2993 hash
->rehash_time
= jiffies
;
2996 netlink_add_usersock_entry();
2998 sock_register(&netlink_family_ops
);
2999 register_pernet_subsys(&netlink_net_ops
);
3000 /* The netlink device handler may be needed early. */
3005 panic("netlink_init: Cannot allocate nl_table\n");
3008 core_initcall(netlink_proto_init
);