2 * NETLINK Kernel-user communication protocol.
4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
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)
16 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
24 #include <linux/module.h>
26 #include <linux/capability.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/signal.h>
30 #include <linux/sched.h>
31 #include <linux/errno.h>
32 #include <linux/string.h>
33 #include <linux/stat.h>
34 #include <linux/socket.h>
36 #include <linux/fcntl.h>
37 #include <linux/termios.h>
38 #include <linux/sockios.h>
39 #include <linux/net.h>
41 #include <linux/slab.h>
42 #include <asm/uaccess.h>
43 #include <linux/skbuff.h>
44 #include <linux/netdevice.h>
45 #include <linux/rtnetlink.h>
46 #include <linux/proc_fs.h>
47 #include <linux/seq_file.h>
48 #include <linux/notifier.h>
49 #include <linux/security.h>
50 #include <linux/jhash.h>
51 #include <linux/jiffies.h>
52 #include <linux/random.h>
53 #include <linux/bitops.h>
55 #include <linux/types.h>
56 #include <linux/audit.h>
57 #include <linux/mutex.h>
58 #include <linux/vmalloc.h>
59 #include <asm/cacheflush.h>
61 #include <net/net_namespace.h>
64 #include <net/netlink.h>
66 #include "af_netlink.h"
70 unsigned long masks[0];
74 #define NETLINK_CONGESTED 0x0
77 #define NETLINK_KERNEL_SOCKET 0x1
78 #define NETLINK_RECV_PKTINFO 0x2
79 #define NETLINK_BROADCAST_SEND_ERROR 0x4
80 #define NETLINK_RECV_NO_ENOBUFS 0x8
82 static inline int netlink_is_kernel(struct sock *sk)
84 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
87 struct netlink_table *nl_table;
88 EXPORT_SYMBOL_GPL(nl_table);
90 static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
92 static int netlink_dump(struct sock *sk);
93 static void netlink_skb_destructor(struct sk_buff *skb);
95 DEFINE_RWLOCK(nl_table_lock);
96 EXPORT_SYMBOL_GPL(nl_table_lock);
97 static atomic_t nl_table_users = ATOMIC_INIT(0);
99 #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
101 static ATOMIC_NOTIFIER_HEAD(netlink_chain);
103 static inline u32 netlink_group_mask(u32 group)
105 return group ? 1 << (group - 1) : 0;
108 static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
110 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
113 #ifdef CONFIG_NETLINK_MMAP
114 static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
116 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
119 static bool netlink_rx_is_mmaped(struct sock *sk)
121 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
124 static bool netlink_tx_is_mmaped(struct sock *sk)
126 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
129 static __pure struct page *pgvec_to_page(const void *addr)
131 if (is_vmalloc_addr(addr))
132 return vmalloc_to_page(addr);
134 return virt_to_page(addr);
137 static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
141 for (i = 0; i < len; i++) {
142 if (pg_vec[i] != NULL) {
143 if (is_vmalloc_addr(pg_vec[i]))
146 free_pages((unsigned long)pg_vec[i], order);
152 static void *alloc_one_pg_vec_page(unsigned long order)
155 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
156 __GFP_NOWARN | __GFP_NORETRY;
158 buffer = (void *)__get_free_pages(gfp_flags, order);
162 buffer = vzalloc((1 << order) * PAGE_SIZE);
166 gfp_flags &= ~__GFP_NORETRY;
167 return (void *)__get_free_pages(gfp_flags, order);
170 static void **alloc_pg_vec(struct netlink_sock *nlk,
171 struct nl_mmap_req *req, unsigned int order)
173 unsigned int block_nr = req->nm_block_nr;
177 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
181 for (i = 0; i < block_nr; i++) {
182 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
183 if (pg_vec[i] == NULL)
189 free_pg_vec(pg_vec, order, block_nr);
193 static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
194 bool closing, bool tx_ring)
196 struct netlink_sock *nlk = nlk_sk(sk);
197 struct netlink_ring *ring;
198 struct sk_buff_head *queue;
199 void **pg_vec = NULL;
200 unsigned int order = 0;
203 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
204 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
207 if (atomic_read(&nlk->mapped))
209 if (atomic_read(&ring->pending))
213 if (req->nm_block_nr) {
214 if (ring->pg_vec != NULL)
217 if ((int)req->nm_block_size <= 0)
219 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
221 if (req->nm_frame_size < NL_MMAP_HDRLEN)
223 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
226 ring->frames_per_block = req->nm_block_size /
228 if (ring->frames_per_block == 0)
230 if (ring->frames_per_block * req->nm_block_nr !=
234 order = get_order(req->nm_block_size);
235 pg_vec = alloc_pg_vec(nlk, req, order);
239 if (req->nm_frame_nr)
244 mutex_lock(&nlk->pg_vec_lock);
245 if (closing || atomic_read(&nlk->mapped) == 0) {
247 spin_lock_bh(&queue->lock);
249 ring->frame_max = req->nm_frame_nr - 1;
251 ring->frame_size = req->nm_frame_size;
252 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
254 swap(ring->pg_vec_len, req->nm_block_nr);
255 swap(ring->pg_vec_order, order);
256 swap(ring->pg_vec, pg_vec);
258 __skb_queue_purge(queue);
259 spin_unlock_bh(&queue->lock);
261 WARN_ON(atomic_read(&nlk->mapped));
263 mutex_unlock(&nlk->pg_vec_lock);
266 free_pg_vec(pg_vec, order, req->nm_block_nr);
270 static void netlink_mm_open(struct vm_area_struct *vma)
272 struct file *file = vma->vm_file;
273 struct socket *sock = file->private_data;
274 struct sock *sk = sock->sk;
277 atomic_inc(&nlk_sk(sk)->mapped);
280 static void netlink_mm_close(struct vm_area_struct *vma)
282 struct file *file = vma->vm_file;
283 struct socket *sock = file->private_data;
284 struct sock *sk = sock->sk;
287 atomic_dec(&nlk_sk(sk)->mapped);
290 static const struct vm_operations_struct netlink_mmap_ops = {
291 .open = netlink_mm_open,
292 .close = netlink_mm_close,
295 static int netlink_mmap(struct file *file, struct socket *sock,
296 struct vm_area_struct *vma)
298 struct sock *sk = sock->sk;
299 struct netlink_sock *nlk = nlk_sk(sk);
300 struct netlink_ring *ring;
301 unsigned long start, size, expected;
308 mutex_lock(&nlk->pg_vec_lock);
311 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
312 if (ring->pg_vec == NULL)
314 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
320 size = vma->vm_end - vma->vm_start;
321 if (size != expected)
324 start = vma->vm_start;
325 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
326 if (ring->pg_vec == NULL)
329 for (i = 0; i < ring->pg_vec_len; i++) {
331 void *kaddr = ring->pg_vec[i];
334 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
335 page = pgvec_to_page(kaddr);
336 err = vm_insert_page(vma, start, page);
345 atomic_inc(&nlk->mapped);
346 vma->vm_ops = &netlink_mmap_ops;
349 mutex_unlock(&nlk->pg_vec_lock);
353 static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
355 #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
356 struct page *p_start, *p_end;
358 /* First page is flushed through netlink_{get,set}_status */
359 p_start = pgvec_to_page(hdr + PAGE_SIZE);
360 p_end = pgvec_to_page((void *)hdr + NL_MMAP_MSG_HDRLEN + hdr->nm_len - 1);
361 while (p_start <= p_end) {
362 flush_dcache_page(p_start);
368 static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
371 flush_dcache_page(pgvec_to_page(hdr));
372 return hdr->nm_status;
375 static void netlink_set_status(struct nl_mmap_hdr *hdr,
376 enum nl_mmap_status status)
378 hdr->nm_status = status;
379 flush_dcache_page(pgvec_to_page(hdr));
383 static struct nl_mmap_hdr *
384 __netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
386 unsigned int pg_vec_pos, frame_off;
388 pg_vec_pos = pos / ring->frames_per_block;
389 frame_off = pos % ring->frames_per_block;
391 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
394 static struct nl_mmap_hdr *
395 netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
396 enum nl_mmap_status status)
398 struct nl_mmap_hdr *hdr;
400 hdr = __netlink_lookup_frame(ring, pos);
401 if (netlink_get_status(hdr) != status)
407 static struct nl_mmap_hdr *
408 netlink_current_frame(const struct netlink_ring *ring,
409 enum nl_mmap_status status)
411 return netlink_lookup_frame(ring, ring->head, status);
414 static struct nl_mmap_hdr *
415 netlink_previous_frame(const struct netlink_ring *ring,
416 enum nl_mmap_status status)
420 prev = ring->head ? ring->head - 1 : ring->frame_max;
421 return netlink_lookup_frame(ring, prev, status);
424 static void netlink_increment_head(struct netlink_ring *ring)
426 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
429 static void netlink_forward_ring(struct netlink_ring *ring)
431 unsigned int head = ring->head, pos = head;
432 const struct nl_mmap_hdr *hdr;
435 hdr = __netlink_lookup_frame(ring, pos);
436 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
438 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
440 netlink_increment_head(ring);
441 } while (ring->head != head);
444 static unsigned int netlink_poll(struct file *file, struct socket *sock,
447 struct sock *sk = sock->sk;
448 struct netlink_sock *nlk = nlk_sk(sk);
451 if (nlk->cb != NULL && nlk->rx_ring.pg_vec != NULL)
454 mask = datagram_poll(file, sock, wait);
456 spin_lock_bh(&sk->sk_receive_queue.lock);
457 if (nlk->rx_ring.pg_vec) {
458 netlink_forward_ring(&nlk->rx_ring);
459 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
460 mask |= POLLIN | POLLRDNORM;
462 spin_unlock_bh(&sk->sk_receive_queue.lock);
464 spin_lock_bh(&sk->sk_write_queue.lock);
465 if (nlk->tx_ring.pg_vec) {
466 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
467 mask |= POLLOUT | POLLWRNORM;
469 spin_unlock_bh(&sk->sk_write_queue.lock);
474 static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
476 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
479 static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
480 struct netlink_ring *ring,
481 struct nl_mmap_hdr *hdr)
486 size = ring->frame_size - NL_MMAP_HDRLEN;
487 data = (void *)hdr + NL_MMAP_HDRLEN;
491 skb_reset_tail_pointer(skb);
492 skb->end = skb->tail + size;
495 skb->destructor = netlink_skb_destructor;
496 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
497 NETLINK_CB(skb).sk = sk;
500 static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
501 u32 dst_portid, u32 dst_group,
502 struct sock_iocb *siocb)
504 struct netlink_sock *nlk = nlk_sk(sk);
505 struct netlink_ring *ring;
506 struct nl_mmap_hdr *hdr;
510 int err = 0, len = 0;
512 /* Netlink messages are validated by the receiver before processing.
513 * In order to avoid userspace changing the contents of the message
514 * after validation, the socket and the ring may only be used by a
515 * single process, otherwise we fall back to copying.
517 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
518 atomic_read(&nlk->mapped) > 1)
521 mutex_lock(&nlk->pg_vec_lock);
523 ring = &nlk->tx_ring;
524 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
527 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
529 if (!(msg->msg_flags & MSG_DONTWAIT) &&
530 atomic_read(&nlk->tx_ring.pending))
534 if (hdr->nm_len > maxlen) {
539 netlink_frame_flush_dcache(hdr);
541 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
542 skb = alloc_skb_head(GFP_KERNEL);
548 netlink_ring_setup_skb(skb, sk, ring, hdr);
549 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
550 __skb_put(skb, hdr->nm_len);
551 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
552 atomic_inc(&ring->pending);
554 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
559 __skb_put(skb, hdr->nm_len);
560 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
561 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
564 netlink_increment_head(ring);
566 NETLINK_CB(skb).portid = nlk->portid;
567 NETLINK_CB(skb).dst_group = dst_group;
568 NETLINK_CB(skb).creds = siocb->scm->creds;
570 err = security_netlink_send(sk, skb);
576 if (unlikely(dst_group)) {
577 atomic_inc(&skb->users);
578 netlink_broadcast(sk, skb, dst_portid, dst_group,
581 err = netlink_unicast(sk, skb, dst_portid,
582 msg->msg_flags & MSG_DONTWAIT);
587 } while (hdr != NULL ||
588 (!(msg->msg_flags & MSG_DONTWAIT) &&
589 atomic_read(&nlk->tx_ring.pending)));
594 mutex_unlock(&nlk->pg_vec_lock);
598 static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
600 struct nl_mmap_hdr *hdr;
602 hdr = netlink_mmap_hdr(skb);
603 hdr->nm_len = skb->len;
604 hdr->nm_group = NETLINK_CB(skb).dst_group;
605 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
606 hdr->nm_uid = NETLINK_CB(skb).creds.uid;
607 hdr->nm_gid = NETLINK_CB(skb).creds.gid;
608 netlink_frame_flush_dcache(hdr);
609 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
611 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
615 static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
617 struct netlink_sock *nlk = nlk_sk(sk);
618 struct netlink_ring *ring = &nlk->rx_ring;
619 struct nl_mmap_hdr *hdr;
621 spin_lock_bh(&sk->sk_receive_queue.lock);
622 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
624 spin_unlock_bh(&sk->sk_receive_queue.lock);
626 sk->sk_err = ENOBUFS;
627 sk->sk_error_report(sk);
630 netlink_increment_head(ring);
631 __skb_queue_tail(&sk->sk_receive_queue, skb);
632 spin_unlock_bh(&sk->sk_receive_queue.lock);
634 hdr->nm_len = skb->len;
635 hdr->nm_group = NETLINK_CB(skb).dst_group;
636 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
637 hdr->nm_uid = NETLINK_CB(skb).creds.uid;
638 hdr->nm_gid = NETLINK_CB(skb).creds.gid;
639 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
642 #else /* CONFIG_NETLINK_MMAP */
643 #define netlink_skb_is_mmaped(skb) false
644 #define netlink_rx_is_mmaped(sk) false
645 #define netlink_tx_is_mmaped(sk) false
646 #define netlink_mmap sock_no_mmap
647 #define netlink_poll datagram_poll
648 #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
649 #endif /* CONFIG_NETLINK_MMAP */
651 static void netlink_destroy_callback(struct netlink_callback *cb)
657 static void netlink_consume_callback(struct netlink_callback *cb)
659 consume_skb(cb->skb);
663 static void netlink_skb_destructor(struct sk_buff *skb)
665 #ifdef CONFIG_NETLINK_MMAP
666 struct nl_mmap_hdr *hdr;
667 struct netlink_ring *ring;
670 /* If a packet from the kernel to userspace was freed because of an
671 * error without being delivered to userspace, the kernel must reset
672 * the status. In the direction userspace to kernel, the status is
673 * always reset here after the packet was processed and freed.
675 if (netlink_skb_is_mmaped(skb)) {
676 hdr = netlink_mmap_hdr(skb);
677 sk = NETLINK_CB(skb).sk;
679 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
680 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
681 ring = &nlk_sk(sk)->tx_ring;
683 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
685 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
687 ring = &nlk_sk(sk)->rx_ring;
690 WARN_ON(atomic_read(&ring->pending) == 0);
691 atomic_dec(&ring->pending);
701 static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
703 WARN_ON(skb->sk != NULL);
705 skb->destructor = netlink_skb_destructor;
706 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
707 sk_mem_charge(sk, skb->truesize);
710 static void netlink_sock_destruct(struct sock *sk)
712 struct netlink_sock *nlk = nlk_sk(sk);
716 nlk->cb->done(nlk->cb);
718 module_put(nlk->cb->module);
719 netlink_destroy_callback(nlk->cb);
722 skb_queue_purge(&sk->sk_receive_queue);
723 #ifdef CONFIG_NETLINK_MMAP
725 struct nl_mmap_req req;
727 memset(&req, 0, sizeof(req));
728 if (nlk->rx_ring.pg_vec)
729 netlink_set_ring(sk, &req, true, false);
730 memset(&req, 0, sizeof(req));
731 if (nlk->tx_ring.pg_vec)
732 netlink_set_ring(sk, &req, true, true);
734 #endif /* CONFIG_NETLINK_MMAP */
736 if (!sock_flag(sk, SOCK_DEAD)) {
737 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
741 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
742 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
743 WARN_ON(nlk_sk(sk)->groups);
746 /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
747 * SMP. Look, when several writers sleep and reader wakes them up, all but one
748 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
749 * this, _but_ remember, it adds useless work on UP machines.
752 void netlink_table_grab(void)
753 __acquires(nl_table_lock)
757 write_lock_irq(&nl_table_lock);
759 if (atomic_read(&nl_table_users)) {
760 DECLARE_WAITQUEUE(wait, current);
762 add_wait_queue_exclusive(&nl_table_wait, &wait);
764 set_current_state(TASK_UNINTERRUPTIBLE);
765 if (atomic_read(&nl_table_users) == 0)
767 write_unlock_irq(&nl_table_lock);
769 write_lock_irq(&nl_table_lock);
772 __set_current_state(TASK_RUNNING);
773 remove_wait_queue(&nl_table_wait, &wait);
777 void netlink_table_ungrab(void)
778 __releases(nl_table_lock)
780 write_unlock_irq(&nl_table_lock);
781 wake_up(&nl_table_wait);
785 netlink_lock_table(void)
787 /* read_lock() synchronizes us to netlink_table_grab */
789 read_lock(&nl_table_lock);
790 atomic_inc(&nl_table_users);
791 read_unlock(&nl_table_lock);
795 netlink_unlock_table(void)
797 if (atomic_dec_and_test(&nl_table_users))
798 wake_up(&nl_table_wait);
801 static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
803 struct nl_portid_hash *hash = &nl_table[protocol].hash;
804 struct hlist_head *head;
807 read_lock(&nl_table_lock);
808 head = nl_portid_hashfn(hash, portid);
809 sk_for_each(sk, head) {
810 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
817 read_unlock(&nl_table_lock);
821 static struct hlist_head *nl_portid_hash_zalloc(size_t size)
823 if (size <= PAGE_SIZE)
824 return kzalloc(size, GFP_ATOMIC);
826 return (struct hlist_head *)
827 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
831 static void nl_portid_hash_free(struct hlist_head *table, size_t size)
833 if (size <= PAGE_SIZE)
836 free_pages((unsigned long)table, get_order(size));
839 static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
841 unsigned int omask, mask, shift;
843 struct hlist_head *otable, *table;
846 omask = mask = hash->mask;
847 osize = size = (mask + 1) * sizeof(*table);
851 if (++shift > hash->max_shift)
857 table = nl_portid_hash_zalloc(size);
861 otable = hash->table;
865 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
867 for (i = 0; i <= omask; i++) {
869 struct hlist_node *tmp;
871 sk_for_each_safe(sk, tmp, &otable[i])
872 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
875 nl_portid_hash_free(otable, osize);
876 hash->rehash_time = jiffies + 10 * 60 * HZ;
880 static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
882 int avg = hash->entries >> hash->shift;
884 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
887 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
888 nl_portid_hash_rehash(hash, 0);
895 static const struct proto_ops netlink_ops;
898 netlink_update_listeners(struct sock *sk)
900 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
903 struct listeners *listeners;
905 listeners = nl_deref_protected(tbl->listeners);
909 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
911 sk_for_each_bound(sk, &tbl->mc_list) {
912 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
913 mask |= nlk_sk(sk)->groups[i];
915 listeners->masks[i] = mask;
917 /* this function is only called with the netlink table "grabbed", which
918 * makes sure updates are visible before bind or setsockopt return. */
921 static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
923 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
924 struct hlist_head *head;
925 int err = -EADDRINUSE;
929 netlink_table_grab();
930 head = nl_portid_hashfn(hash, portid);
932 sk_for_each(osk, head) {
933 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
941 if (nlk_sk(sk)->portid)
945 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
948 if (len && nl_portid_hash_dilute(hash, len))
949 head = nl_portid_hashfn(hash, portid);
951 nlk_sk(sk)->portid = portid;
952 sk_add_node(sk, head);
956 netlink_table_ungrab();
960 static void netlink_remove(struct sock *sk)
962 netlink_table_grab();
963 if (sk_del_node_init(sk))
964 nl_table[sk->sk_protocol].hash.entries--;
965 if (nlk_sk(sk)->subscriptions)
966 __sk_del_bind_node(sk);
967 netlink_table_ungrab();
970 static struct proto netlink_proto = {
972 .owner = THIS_MODULE,
973 .obj_size = sizeof(struct netlink_sock),
976 static int __netlink_create(struct net *net, struct socket *sock,
977 struct mutex *cb_mutex, int protocol)
980 struct netlink_sock *nlk;
982 sock->ops = &netlink_ops;
984 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
988 sock_init_data(sock, sk);
992 nlk->cb_mutex = cb_mutex;
994 nlk->cb_mutex = &nlk->cb_def_mutex;
995 mutex_init(nlk->cb_mutex);
997 init_waitqueue_head(&nlk->wait);
998 #ifdef CONFIG_NETLINK_MMAP
999 mutex_init(&nlk->pg_vec_lock);
1002 sk->sk_destruct = netlink_sock_destruct;
1003 sk->sk_protocol = protocol;
1007 static int netlink_create(struct net *net, struct socket *sock, int protocol,
1010 struct module *module = NULL;
1011 struct mutex *cb_mutex;
1012 struct netlink_sock *nlk;
1013 void (*bind)(int group);
1016 sock->state = SS_UNCONNECTED;
1018 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1019 return -ESOCKTNOSUPPORT;
1021 if (protocol < 0 || protocol >= MAX_LINKS)
1022 return -EPROTONOSUPPORT;
1024 netlink_lock_table();
1025 #ifdef CONFIG_MODULES
1026 if (!nl_table[protocol].registered) {
1027 netlink_unlock_table();
1028 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
1029 netlink_lock_table();
1032 if (nl_table[protocol].registered &&
1033 try_module_get(nl_table[protocol].module))
1034 module = nl_table[protocol].module;
1036 err = -EPROTONOSUPPORT;
1037 cb_mutex = nl_table[protocol].cb_mutex;
1038 bind = nl_table[protocol].bind;
1039 netlink_unlock_table();
1044 err = __netlink_create(net, sock, cb_mutex, protocol);
1049 sock_prot_inuse_add(net, &netlink_proto, 1);
1052 nlk = nlk_sk(sock->sk);
1053 nlk->module = module;
1054 nlk->netlink_bind = bind;
1063 static int netlink_release(struct socket *sock)
1065 struct sock *sk = sock->sk;
1066 struct netlink_sock *nlk;
1076 * OK. Socket is unlinked, any packets that arrive now
1081 wake_up_interruptible_all(&nlk->wait);
1083 skb_queue_purge(&sk->sk_write_queue);
1086 struct netlink_notify n = {
1087 .net = sock_net(sk),
1088 .protocol = sk->sk_protocol,
1089 .portid = nlk->portid,
1091 atomic_notifier_call_chain(&netlink_chain,
1092 NETLINK_URELEASE, &n);
1095 module_put(nlk->module);
1097 netlink_table_grab();
1098 if (netlink_is_kernel(sk)) {
1099 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1100 if (--nl_table[sk->sk_protocol].registered == 0) {
1101 struct listeners *old;
1103 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1104 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1105 kfree_rcu(old, rcu);
1106 nl_table[sk->sk_protocol].module = NULL;
1107 nl_table[sk->sk_protocol].bind = NULL;
1108 nl_table[sk->sk_protocol].flags = 0;
1109 nl_table[sk->sk_protocol].registered = 0;
1111 } else if (nlk->subscriptions) {
1112 netlink_update_listeners(sk);
1114 netlink_table_ungrab();
1120 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
1126 static int netlink_autobind(struct socket *sock)
1128 struct sock *sk = sock->sk;
1129 struct net *net = sock_net(sk);
1130 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
1131 struct hlist_head *head;
1133 s32 portid = task_tgid_vnr(current);
1135 static s32 rover = -4097;
1139 netlink_table_grab();
1140 head = nl_portid_hashfn(hash, portid);
1141 sk_for_each(osk, head) {
1142 if (!net_eq(sock_net(osk), net))
1144 if (nlk_sk(osk)->portid == portid) {
1145 /* Bind collision, search negative portid values. */
1149 netlink_table_ungrab();
1153 netlink_table_ungrab();
1155 err = netlink_insert(sk, net, portid);
1156 if (err == -EADDRINUSE)
1159 /* If 2 threads race to autobind, that is fine. */
1166 static inline int netlink_capable(const struct socket *sock, unsigned int flag)
1168 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
1169 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
1173 netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1175 struct netlink_sock *nlk = nlk_sk(sk);
1177 if (nlk->subscriptions && !subscriptions)
1178 __sk_del_bind_node(sk);
1179 else if (!nlk->subscriptions && subscriptions)
1180 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1181 nlk->subscriptions = subscriptions;
1184 static int netlink_realloc_groups(struct sock *sk)
1186 struct netlink_sock *nlk = nlk_sk(sk);
1187 unsigned int groups;
1188 unsigned long *new_groups;
1191 netlink_table_grab();
1193 groups = nl_table[sk->sk_protocol].groups;
1194 if (!nl_table[sk->sk_protocol].registered) {
1199 if (nlk->ngroups >= groups)
1202 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1203 if (new_groups == NULL) {
1207 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
1208 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1210 nlk->groups = new_groups;
1211 nlk->ngroups = groups;
1213 netlink_table_ungrab();
1217 static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1220 struct sock *sk = sock->sk;
1221 struct net *net = sock_net(sk);
1222 struct netlink_sock *nlk = nlk_sk(sk);
1223 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1226 if (addr_len < sizeof(struct sockaddr_nl))
1229 if (nladdr->nl_family != AF_NETLINK)
1232 /* Only superuser is allowed to listen multicasts */
1233 if (nladdr->nl_groups) {
1234 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
1236 err = netlink_realloc_groups(sk);
1242 if (nladdr->nl_pid != nlk->portid)
1245 err = nladdr->nl_pid ?
1246 netlink_insert(sk, net, nladdr->nl_pid) :
1247 netlink_autobind(sock);
1252 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1255 netlink_table_grab();
1256 netlink_update_subscriptions(sk, nlk->subscriptions +
1257 hweight32(nladdr->nl_groups) -
1258 hweight32(nlk->groups[0]));
1259 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
1260 netlink_update_listeners(sk);
1261 netlink_table_ungrab();
1263 if (nlk->netlink_bind && nlk->groups[0]) {
1266 for (i=0; i<nlk->ngroups; i++) {
1267 if (test_bit(i, nlk->groups))
1268 nlk->netlink_bind(i);
1275 static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1276 int alen, int flags)
1279 struct sock *sk = sock->sk;
1280 struct netlink_sock *nlk = nlk_sk(sk);
1281 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1283 if (alen < sizeof(addr->sa_family))
1286 if (addr->sa_family == AF_UNSPEC) {
1287 sk->sk_state = NETLINK_UNCONNECTED;
1288 nlk->dst_portid = 0;
1292 if (addr->sa_family != AF_NETLINK)
1295 /* Only superuser is allowed to send multicasts */
1296 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
1300 err = netlink_autobind(sock);
1303 sk->sk_state = NETLINK_CONNECTED;
1304 nlk->dst_portid = nladdr->nl_pid;
1305 nlk->dst_group = ffs(nladdr->nl_groups);
1311 static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1312 int *addr_len, int peer)
1314 struct sock *sk = sock->sk;
1315 struct netlink_sock *nlk = nlk_sk(sk);
1316 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
1318 nladdr->nl_family = AF_NETLINK;
1320 *addr_len = sizeof(*nladdr);
1323 nladdr->nl_pid = nlk->dst_portid;
1324 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1326 nladdr->nl_pid = nlk->portid;
1327 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
1332 static void netlink_overrun(struct sock *sk)
1334 struct netlink_sock *nlk = nlk_sk(sk);
1336 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
1337 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
1338 sk->sk_err = ENOBUFS;
1339 sk->sk_error_report(sk);
1342 atomic_inc(&sk->sk_drops);
1345 static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1348 struct netlink_sock *nlk;
1350 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1352 return ERR_PTR(-ECONNREFUSED);
1354 /* Don't bother queuing skb if kernel socket has no input function */
1356 if (sock->sk_state == NETLINK_CONNECTED &&
1357 nlk->dst_portid != nlk_sk(ssk)->portid) {
1359 return ERR_PTR(-ECONNREFUSED);
1364 struct sock *netlink_getsockbyfilp(struct file *filp)
1366 struct inode *inode = file_inode(filp);
1369 if (!S_ISSOCK(inode->i_mode))
1370 return ERR_PTR(-ENOTSOCK);
1372 sock = SOCKET_I(inode)->sk;
1373 if (sock->sk_family != AF_NETLINK)
1374 return ERR_PTR(-EINVAL);
1381 * Attach a skb to a netlink socket.
1382 * The caller must hold a reference to the destination socket. On error, the
1383 * reference is dropped. The skb is not send to the destination, just all
1384 * all error checks are performed and memory in the queue is reserved.
1386 * < 0: error. skb freed, reference to sock dropped.
1388 * 1: repeat lookup - reference dropped while waiting for socket memory.
1390 int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
1391 long *timeo, struct sock *ssk)
1393 struct netlink_sock *nlk;
1397 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1398 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1399 !netlink_skb_is_mmaped(skb)) {
1400 DECLARE_WAITQUEUE(wait, current);
1402 if (!ssk || netlink_is_kernel(ssk))
1403 netlink_overrun(sk);
1409 __set_current_state(TASK_INTERRUPTIBLE);
1410 add_wait_queue(&nlk->wait, &wait);
1412 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1413 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1414 !sock_flag(sk, SOCK_DEAD))
1415 *timeo = schedule_timeout(*timeo);
1417 __set_current_state(TASK_RUNNING);
1418 remove_wait_queue(&nlk->wait, &wait);
1421 if (signal_pending(current)) {
1423 return sock_intr_errno(*timeo);
1427 netlink_skb_set_owner_r(skb, sk);
1431 static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1435 #ifdef CONFIG_NETLINK_MMAP
1436 if (netlink_skb_is_mmaped(skb))
1437 netlink_queue_mmaped_skb(sk, skb);
1438 else if (netlink_rx_is_mmaped(sk))
1439 netlink_ring_set_copied(sk, skb);
1441 #endif /* CONFIG_NETLINK_MMAP */
1442 skb_queue_tail(&sk->sk_receive_queue, skb);
1443 sk->sk_data_ready(sk, len);
1447 int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1449 int len = __netlink_sendskb(sk, skb);
1455 void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1461 static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1465 WARN_ON(skb->sk != NULL);
1466 if (netlink_skb_is_mmaped(skb))
1469 delta = skb->end - skb->tail;
1470 if (delta * 2 < skb->truesize)
1473 if (skb_shared(skb)) {
1474 struct sk_buff *nskb = skb_clone(skb, allocation);
1481 if (!pskb_expand_head(skb, 0, -delta, allocation))
1482 skb->truesize -= delta;
1487 static void netlink_rcv_wake(struct sock *sk)
1489 struct netlink_sock *nlk = nlk_sk(sk);
1491 if (skb_queue_empty(&sk->sk_receive_queue))
1492 clear_bit(NETLINK_CONGESTED, &nlk->state);
1493 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
1494 wake_up_interruptible(&nlk->wait);
1497 static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1501 struct netlink_sock *nlk = nlk_sk(sk);
1503 ret = -ECONNREFUSED;
1504 if (nlk->netlink_rcv != NULL) {
1506 netlink_skb_set_owner_r(skb, sk);
1507 NETLINK_CB(skb).sk = ssk;
1508 nlk->netlink_rcv(skb);
1517 int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
1518 u32 portid, int nonblock)
1524 skb = netlink_trim(skb, gfp_any());
1526 timeo = sock_sndtimeo(ssk, nonblock);
1528 sk = netlink_getsockbyportid(ssk, portid);
1533 if (netlink_is_kernel(sk))
1534 return netlink_unicast_kernel(sk, skb, ssk);
1536 if (sk_filter(sk, skb)) {
1543 err = netlink_attachskb(sk, skb, &timeo, ssk);
1549 return netlink_sendskb(sk, skb);
1551 EXPORT_SYMBOL(netlink_unicast);
1553 struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1554 u32 dst_portid, gfp_t gfp_mask)
1556 #ifdef CONFIG_NETLINK_MMAP
1557 struct sock *sk = NULL;
1558 struct sk_buff *skb;
1559 struct netlink_ring *ring;
1560 struct nl_mmap_hdr *hdr;
1561 unsigned int maxlen;
1563 sk = netlink_getsockbyportid(ssk, dst_portid);
1567 ring = &nlk_sk(sk)->rx_ring;
1568 /* fast-path without atomic ops for common case: non-mmaped receiver */
1569 if (ring->pg_vec == NULL)
1572 skb = alloc_skb_head(gfp_mask);
1576 spin_lock_bh(&sk->sk_receive_queue.lock);
1577 /* check again under lock */
1578 if (ring->pg_vec == NULL)
1581 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1585 netlink_forward_ring(ring);
1586 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1589 netlink_ring_setup_skb(skb, sk, ring, hdr);
1590 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1591 atomic_inc(&ring->pending);
1592 netlink_increment_head(ring);
1594 spin_unlock_bh(&sk->sk_receive_queue.lock);
1599 spin_unlock_bh(&sk->sk_receive_queue.lock);
1606 spin_unlock_bh(&sk->sk_receive_queue.lock);
1611 return alloc_skb(size, gfp_mask);
1613 EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1615 int netlink_has_listeners(struct sock *sk, unsigned int group)
1618 struct listeners *listeners;
1620 BUG_ON(!netlink_is_kernel(sk));
1623 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1625 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
1626 res = test_bit(group - 1, listeners->masks);
1632 EXPORT_SYMBOL_GPL(netlink_has_listeners);
1634 static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1636 struct netlink_sock *nlk = nlk_sk(sk);
1638 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
1639 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
1640 netlink_skb_set_owner_r(skb, sk);
1641 __netlink_sendskb(sk, skb);
1642 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1647 struct netlink_broadcast_data {
1648 struct sock *exclude_sk;
1653 int delivery_failure;
1657 struct sk_buff *skb, *skb2;
1658 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1662 static int do_one_broadcast(struct sock *sk,
1663 struct netlink_broadcast_data *p)
1665 struct netlink_sock *nlk = nlk_sk(sk);
1668 if (p->exclude_sk == sk)
1671 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1672 !test_bit(p->group - 1, nlk->groups))
1675 if (!net_eq(sock_net(sk), p->net))
1679 netlink_overrun(sk);
1684 if (p->skb2 == NULL) {
1685 if (skb_shared(p->skb)) {
1686 p->skb2 = skb_clone(p->skb, p->allocation);
1688 p->skb2 = skb_get(p->skb);
1690 * skb ownership may have been set when
1691 * delivered to a previous socket.
1693 skb_orphan(p->skb2);
1696 if (p->skb2 == NULL) {
1697 netlink_overrun(sk);
1698 /* Clone failed. Notify ALL listeners. */
1700 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1701 p->delivery_failure = 1;
1702 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1705 } else if (sk_filter(sk, p->skb2)) {
1708 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1709 netlink_overrun(sk);
1710 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1711 p->delivery_failure = 1;
1713 p->congested |= val;
1723 int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
1724 u32 group, gfp_t allocation,
1725 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1728 struct net *net = sock_net(ssk);
1729 struct netlink_broadcast_data info;
1732 skb = netlink_trim(skb, allocation);
1734 info.exclude_sk = ssk;
1736 info.portid = portid;
1739 info.delivery_failure = 0;
1742 info.allocation = allocation;
1745 info.tx_filter = filter;
1746 info.tx_data = filter_data;
1748 /* While we sleep in clone, do not allow to change socket list */
1750 netlink_lock_table();
1752 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1753 do_one_broadcast(sk, &info);
1757 netlink_unlock_table();
1759 if (info.delivery_failure) {
1760 kfree_skb(info.skb2);
1763 consume_skb(info.skb2);
1765 if (info.delivered) {
1766 if (info.congested && (allocation & __GFP_WAIT))
1772 EXPORT_SYMBOL(netlink_broadcast_filtered);
1774 int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
1775 u32 group, gfp_t allocation)
1777 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
1780 EXPORT_SYMBOL(netlink_broadcast);
1782 struct netlink_set_err_data {
1783 struct sock *exclude_sk;
1789 static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1791 struct netlink_sock *nlk = nlk_sk(sk);
1794 if (sk == p->exclude_sk)
1797 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
1800 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
1801 !test_bit(p->group - 1, nlk->groups))
1804 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1809 sk->sk_err = p->code;
1810 sk->sk_error_report(sk);
1816 * netlink_set_err - report error to broadcast listeners
1817 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
1818 * @portid: the PORTID of a process that we want to skip (if any)
1819 * @groups: the broadcast group that will notice the error
1820 * @code: error code, must be negative (as usual in kernelspace)
1822 * This function returns the number of broadcast listeners that have set the
1823 * NETLINK_RECV_NO_ENOBUFS socket option.
1825 int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1827 struct netlink_set_err_data info;
1831 info.exclude_sk = ssk;
1832 info.portid = portid;
1834 /* sk->sk_err wants a positive error value */
1837 read_lock(&nl_table_lock);
1839 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1840 ret += do_one_set_err(sk, &info);
1842 read_unlock(&nl_table_lock);
1845 EXPORT_SYMBOL(netlink_set_err);
1847 /* must be called with netlink table grabbed */
1848 static void netlink_update_socket_mc(struct netlink_sock *nlk,
1852 int old, new = !!is_new, subscriptions;
1854 old = test_bit(group - 1, nlk->groups);
1855 subscriptions = nlk->subscriptions - old + new;
1857 __set_bit(group - 1, nlk->groups);
1859 __clear_bit(group - 1, nlk->groups);
1860 netlink_update_subscriptions(&nlk->sk, subscriptions);
1861 netlink_update_listeners(&nlk->sk);
1864 static int netlink_setsockopt(struct socket *sock, int level, int optname,
1865 char __user *optval, unsigned int optlen)
1867 struct sock *sk = sock->sk;
1868 struct netlink_sock *nlk = nlk_sk(sk);
1869 unsigned int val = 0;
1872 if (level != SOL_NETLINK)
1873 return -ENOPROTOOPT;
1875 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1876 optlen >= sizeof(int) &&
1877 get_user(val, (unsigned int __user *)optval))
1881 case NETLINK_PKTINFO:
1883 nlk->flags |= NETLINK_RECV_PKTINFO;
1885 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1888 case NETLINK_ADD_MEMBERSHIP:
1889 case NETLINK_DROP_MEMBERSHIP: {
1890 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
1892 err = netlink_realloc_groups(sk);
1895 if (!val || val - 1 >= nlk->ngroups)
1897 netlink_table_grab();
1898 netlink_update_socket_mc(nlk, val,
1899 optname == NETLINK_ADD_MEMBERSHIP);
1900 netlink_table_ungrab();
1902 if (nlk->netlink_bind)
1903 nlk->netlink_bind(val);
1908 case NETLINK_BROADCAST_ERROR:
1910 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1912 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1915 case NETLINK_NO_ENOBUFS:
1917 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
1918 clear_bit(NETLINK_CONGESTED, &nlk->state);
1919 wake_up_interruptible(&nlk->wait);
1921 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
1925 #ifdef CONFIG_NETLINK_MMAP
1926 case NETLINK_RX_RING:
1927 case NETLINK_TX_RING: {
1928 struct nl_mmap_req req;
1930 /* Rings might consume more memory than queue limits, require
1933 if (!capable(CAP_NET_ADMIN))
1935 if (optlen < sizeof(req))
1937 if (copy_from_user(&req, optval, sizeof(req)))
1939 err = netlink_set_ring(sk, &req, false,
1940 optname == NETLINK_TX_RING);
1943 #endif /* CONFIG_NETLINK_MMAP */
1950 static int netlink_getsockopt(struct socket *sock, int level, int optname,
1951 char __user *optval, int __user *optlen)
1953 struct sock *sk = sock->sk;
1954 struct netlink_sock *nlk = nlk_sk(sk);
1957 if (level != SOL_NETLINK)
1958 return -ENOPROTOOPT;
1960 if (get_user(len, optlen))
1966 case NETLINK_PKTINFO:
1967 if (len < sizeof(int))
1970 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
1971 if (put_user(len, optlen) ||
1972 put_user(val, optval))
1976 case NETLINK_BROADCAST_ERROR:
1977 if (len < sizeof(int))
1980 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1981 if (put_user(len, optlen) ||
1982 put_user(val, optval))
1986 case NETLINK_NO_ENOBUFS:
1987 if (len < sizeof(int))
1990 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1991 if (put_user(len, optlen) ||
1992 put_user(val, optval))
2002 static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2004 struct nl_pktinfo info;
2006 info.group = NETLINK_CB(skb).dst_group;
2007 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2010 static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2011 struct msghdr *msg, size_t len)
2013 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2014 struct sock *sk = sock->sk;
2015 struct netlink_sock *nlk = nlk_sk(sk);
2016 struct sockaddr_nl *addr = msg->msg_name;
2019 struct sk_buff *skb;
2021 struct scm_cookie scm;
2023 if (msg->msg_flags&MSG_OOB)
2026 if (NULL == siocb->scm)
2029 err = scm_send(sock, msg, siocb->scm, true);
2033 if (msg->msg_namelen) {
2035 if (addr->nl_family != AF_NETLINK)
2037 dst_portid = addr->nl_pid;
2038 dst_group = ffs(addr->nl_groups);
2040 if ((dst_group || dst_portid) &&
2041 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
2044 dst_portid = nlk->dst_portid;
2045 dst_group = nlk->dst_group;
2049 err = netlink_autobind(sock);
2054 if (netlink_tx_is_mmaped(sk) &&
2055 msg->msg_iov->iov_base == NULL) {
2056 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2062 if (len > sk->sk_sndbuf - 32)
2065 skb = alloc_skb(len, GFP_KERNEL);
2069 NETLINK_CB(skb).portid = nlk->portid;
2070 NETLINK_CB(skb).dst_group = dst_group;
2071 NETLINK_CB(skb).creds = siocb->scm->creds;
2074 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
2079 err = security_netlink_send(sk, skb);
2086 atomic_inc(&skb->users);
2087 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
2089 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
2092 scm_destroy(siocb->scm);
2096 static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2097 struct msghdr *msg, size_t len,
2100 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2101 struct scm_cookie scm;
2102 struct sock *sk = sock->sk;
2103 struct netlink_sock *nlk = nlk_sk(sk);
2104 int noblock = flags&MSG_DONTWAIT;
2106 struct sk_buff *skb, *data_skb;
2114 skb = skb_recv_datagram(sk, flags, noblock, &err);
2120 #ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2121 if (unlikely(skb_shinfo(skb)->frag_list)) {
2123 * If this skb has a frag_list, then here that means that we
2124 * will have to use the frag_list skb's data for compat tasks
2125 * and the regular skb's data for normal (non-compat) tasks.
2127 * If we need to send the compat skb, assign it to the
2128 * 'data_skb' variable so that it will be used below for data
2129 * copying. We keep 'skb' for everything else, including
2130 * freeing both later.
2132 if (flags & MSG_CMSG_COMPAT)
2133 data_skb = skb_shinfo(skb)->frag_list;
2137 msg->msg_namelen = 0;
2139 copied = data_skb->len;
2141 msg->msg_flags |= MSG_TRUNC;
2145 skb_reset_transport_header(data_skb);
2146 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
2148 if (msg->msg_name) {
2149 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
2150 addr->nl_family = AF_NETLINK;
2152 addr->nl_pid = NETLINK_CB(skb).portid;
2153 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
2154 msg->msg_namelen = sizeof(*addr);
2157 if (nlk->flags & NETLINK_RECV_PKTINFO)
2158 netlink_cmsg_recv_pktinfo(msg, skb);
2160 if (NULL == siocb->scm) {
2161 memset(&scm, 0, sizeof(scm));
2164 siocb->scm->creds = *NETLINK_CREDS(skb);
2165 if (flags & MSG_TRUNC)
2166 copied = data_skb->len;
2168 skb_free_datagram(sk, skb);
2170 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
2171 ret = netlink_dump(sk);
2174 sk->sk_error_report(sk);
2178 scm_recv(sock, msg, siocb->scm, flags);
2180 netlink_rcv_wake(sk);
2181 return err ? : copied;
2184 static void netlink_data_ready(struct sock *sk, int len)
2190 * We export these functions to other modules. They provide a
2191 * complete set of kernel non-blocking support for message
2196 __netlink_kernel_create(struct net *net, int unit, struct module *module,
2197 struct netlink_kernel_cfg *cfg)
2199 struct socket *sock;
2201 struct netlink_sock *nlk;
2202 struct listeners *listeners = NULL;
2203 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2204 unsigned int groups;
2208 if (unit < 0 || unit >= MAX_LINKS)
2211 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2215 * We have to just have a reference on the net from sk, but don't
2216 * get_net it. Besides, we cannot get and then put the net here.
2217 * So we create one inside init_net and the move it to net.
2220 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2221 goto out_sock_release_nosk;
2224 sk_change_net(sk, net);
2226 if (!cfg || cfg->groups < 32)
2229 groups = cfg->groups;
2231 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2233 goto out_sock_release;
2235 sk->sk_data_ready = netlink_data_ready;
2236 if (cfg && cfg->input)
2237 nlk_sk(sk)->netlink_rcv = cfg->input;
2239 if (netlink_insert(sk, net, 0))
2240 goto out_sock_release;
2243 nlk->flags |= NETLINK_KERNEL_SOCKET;
2245 netlink_table_grab();
2246 if (!nl_table[unit].registered) {
2247 nl_table[unit].groups = groups;
2248 rcu_assign_pointer(nl_table[unit].listeners, listeners);
2249 nl_table[unit].cb_mutex = cb_mutex;
2250 nl_table[unit].module = module;
2252 nl_table[unit].bind = cfg->bind;
2253 nl_table[unit].flags = cfg->flags;
2255 nl_table[unit].registered = 1;
2258 nl_table[unit].registered++;
2260 netlink_table_ungrab();
2265 netlink_kernel_release(sk);
2268 out_sock_release_nosk:
2272 EXPORT_SYMBOL(__netlink_kernel_create);
2275 netlink_kernel_release(struct sock *sk)
2277 sk_release_kernel(sk);
2279 EXPORT_SYMBOL(netlink_kernel_release);
2281 int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
2283 struct listeners *new, *old;
2284 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
2289 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
2290 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2293 old = nl_deref_protected(tbl->listeners);
2294 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2295 rcu_assign_pointer(tbl->listeners, new);
2297 kfree_rcu(old, rcu);
2299 tbl->groups = groups;
2305 * netlink_change_ngroups - change number of multicast groups
2307 * This changes the number of multicast groups that are available
2308 * on a certain netlink family. Note that it is not possible to
2309 * change the number of groups to below 32. Also note that it does
2310 * not implicitly call netlink_clear_multicast_users() when the
2311 * number of groups is reduced.
2313 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2314 * @groups: The new number of groups.
2316 int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2320 netlink_table_grab();
2321 err = __netlink_change_ngroups(sk, groups);
2322 netlink_table_ungrab();
2327 void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2330 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2332 sk_for_each_bound(sk, &tbl->mc_list)
2333 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2337 * netlink_clear_multicast_users - kick off multicast listeners
2339 * This function removes all listeners from the given group.
2340 * @ksk: The kernel netlink socket, as returned by
2341 * netlink_kernel_create().
2342 * @group: The multicast group to clear.
2344 void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2346 netlink_table_grab();
2347 __netlink_clear_multicast_users(ksk, group);
2348 netlink_table_ungrab();
2352 __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
2354 struct nlmsghdr *nlh;
2355 int size = nlmsg_msg_size(len);
2357 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
2358 nlh->nlmsg_type = type;
2359 nlh->nlmsg_len = size;
2360 nlh->nlmsg_flags = flags;
2361 nlh->nlmsg_pid = portid;
2362 nlh->nlmsg_seq = seq;
2363 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
2364 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
2367 EXPORT_SYMBOL(__nlmsg_put);
2370 * It looks a bit ugly.
2371 * It would be better to create kernel thread.
2374 static int netlink_dump(struct sock *sk)
2376 struct netlink_sock *nlk = nlk_sk(sk);
2377 struct netlink_callback *cb;
2378 struct sk_buff *skb = NULL;
2379 struct nlmsghdr *nlh;
2380 int len, err = -ENOBUFS;
2383 mutex_lock(nlk->cb_mutex);
2391 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2393 if (!netlink_rx_is_mmaped(sk) &&
2394 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2396 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, GFP_KERNEL);
2399 netlink_skb_set_owner_r(skb, sk);
2401 len = cb->dump(skb, cb);
2404 mutex_unlock(nlk->cb_mutex);
2406 if (sk_filter(sk, skb))
2409 __netlink_sendskb(sk, skb);
2413 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2417 nl_dump_check_consistent(cb, nlh);
2419 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2421 if (sk_filter(sk, skb))
2424 __netlink_sendskb(sk, skb);
2429 mutex_unlock(nlk->cb_mutex);
2431 module_put(cb->module);
2432 netlink_consume_callback(cb);
2436 mutex_unlock(nlk->cb_mutex);
2441 int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2442 const struct nlmsghdr *nlh,
2443 struct netlink_dump_control *control)
2445 struct netlink_callback *cb;
2447 struct netlink_sock *nlk;
2450 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
2454 /* Memory mapped dump requests need to be copied to avoid looping
2455 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2456 * a reference to the skb.
2458 if (netlink_skb_is_mmaped(skb)) {
2459 skb = skb_copy(skb, GFP_KERNEL);
2465 atomic_inc(&skb->users);
2467 cb->dump = control->dump;
2468 cb->done = control->done;
2470 cb->data = control->data;
2471 cb->module = control->module;
2472 cb->min_dump_alloc = control->min_dump_alloc;
2473 atomic_inc(&skb->users);
2476 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2478 netlink_destroy_callback(cb);
2479 return -ECONNREFUSED;
2483 mutex_lock(nlk->cb_mutex);
2484 /* A dump is in progress... */
2486 mutex_unlock(nlk->cb_mutex);
2487 netlink_destroy_callback(cb);
2491 /* add reference of module which cb->dump belongs to */
2492 if (!try_module_get(cb->module)) {
2493 mutex_unlock(nlk->cb_mutex);
2494 netlink_destroy_callback(cb);
2495 ret = -EPROTONOSUPPORT;
2500 mutex_unlock(nlk->cb_mutex);
2502 ret = netlink_dump(sk);
2509 /* We successfully started a dump, by returning -EINTR we
2510 * signal not to send ACK even if it was requested.
2514 EXPORT_SYMBOL(__netlink_dump_start);
2516 void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2518 struct sk_buff *skb;
2519 struct nlmsghdr *rep;
2520 struct nlmsgerr *errmsg;
2521 size_t payload = sizeof(*errmsg);
2523 /* error messages get the original request appened */
2525 payload += nlmsg_len(nlh);
2527 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2528 NETLINK_CB(in_skb).portid, GFP_KERNEL);
2532 sk = netlink_lookup(sock_net(in_skb->sk),
2533 in_skb->sk->sk_protocol,
2534 NETLINK_CB(in_skb).portid);
2536 sk->sk_err = ENOBUFS;
2537 sk->sk_error_report(sk);
2543 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
2544 NLMSG_ERROR, payload, 0);
2545 errmsg = nlmsg_data(rep);
2546 errmsg->error = err;
2547 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
2548 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
2550 EXPORT_SYMBOL(netlink_ack);
2552 int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
2555 struct nlmsghdr *nlh;
2558 while (skb->len >= nlmsg_total_size(0)) {
2561 nlh = nlmsg_hdr(skb);
2564 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
2567 /* Only requests are handled by the kernel */
2568 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
2571 /* Skip control messages */
2572 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
2580 if (nlh->nlmsg_flags & NLM_F_ACK || err)
2581 netlink_ack(skb, nlh, err);
2584 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
2585 if (msglen > skb->len)
2587 skb_pull(skb, msglen);
2592 EXPORT_SYMBOL(netlink_rcv_skb);
2595 * nlmsg_notify - send a notification netlink message
2596 * @sk: netlink socket to use
2597 * @skb: notification message
2598 * @portid: destination netlink portid for reports or 0
2599 * @group: destination multicast group or 0
2600 * @report: 1 to report back, 0 to disable
2601 * @flags: allocation flags
2603 int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
2604 unsigned int group, int report, gfp_t flags)
2609 int exclude_portid = 0;
2612 atomic_inc(&skb->users);
2613 exclude_portid = portid;
2616 /* errors reported via destination sk->sk_err, but propagate
2617 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
2618 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
2624 err2 = nlmsg_unicast(sk, skb, portid);
2625 if (!err || err == -ESRCH)
2631 EXPORT_SYMBOL(nlmsg_notify);
2633 #ifdef CONFIG_PROC_FS
2634 struct nl_seq_iter {
2635 struct seq_net_private p;
2640 static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2642 struct nl_seq_iter *iter = seq->private;
2647 for (i = 0; i < MAX_LINKS; i++) {
2648 struct nl_portid_hash *hash = &nl_table[i].hash;
2650 for (j = 0; j <= hash->mask; j++) {
2651 sk_for_each(s, &hash->table[j]) {
2652 if (sock_net(s) != seq_file_net(seq))
2666 static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
2667 __acquires(nl_table_lock)
2669 read_lock(&nl_table_lock);
2670 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2673 static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2676 struct nl_seq_iter *iter;
2681 if (v == SEQ_START_TOKEN)
2682 return netlink_seq_socket_idx(seq, 0);
2684 iter = seq->private;
2688 } while (s && sock_net(s) != seq_file_net(seq));
2693 j = iter->hash_idx + 1;
2696 struct nl_portid_hash *hash = &nl_table[i].hash;
2698 for (; j <= hash->mask; j++) {
2699 s = sk_head(&hash->table[j]);
2700 while (s && sock_net(s) != seq_file_net(seq))
2710 } while (++i < MAX_LINKS);
2715 static void netlink_seq_stop(struct seq_file *seq, void *v)
2716 __releases(nl_table_lock)
2718 read_unlock(&nl_table_lock);
2722 static int netlink_seq_show(struct seq_file *seq, void *v)
2724 if (v == SEQ_START_TOKEN) {
2726 "sk Eth Pid Groups "
2727 "Rmem Wmem Dump Locks Drops Inode\n");
2730 struct netlink_sock *nlk = nlk_sk(s);
2732 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
2736 nlk->groups ? (u32)nlk->groups[0] : 0,
2737 sk_rmem_alloc_get(s),
2738 sk_wmem_alloc_get(s),
2740 atomic_read(&s->sk_refcnt),
2741 atomic_read(&s->sk_drops),
2749 static const struct seq_operations netlink_seq_ops = {
2750 .start = netlink_seq_start,
2751 .next = netlink_seq_next,
2752 .stop = netlink_seq_stop,
2753 .show = netlink_seq_show,
2757 static int netlink_seq_open(struct inode *inode, struct file *file)
2759 return seq_open_net(inode, file, &netlink_seq_ops,
2760 sizeof(struct nl_seq_iter));
2763 static const struct file_operations netlink_seq_fops = {
2764 .owner = THIS_MODULE,
2765 .open = netlink_seq_open,
2767 .llseek = seq_lseek,
2768 .release = seq_release_net,
2773 int netlink_register_notifier(struct notifier_block *nb)
2775 return atomic_notifier_chain_register(&netlink_chain, nb);
2777 EXPORT_SYMBOL(netlink_register_notifier);
2779 int netlink_unregister_notifier(struct notifier_block *nb)
2781 return atomic_notifier_chain_unregister(&netlink_chain, nb);
2783 EXPORT_SYMBOL(netlink_unregister_notifier);
2785 static const struct proto_ops netlink_ops = {
2786 .family = PF_NETLINK,
2787 .owner = THIS_MODULE,
2788 .release = netlink_release,
2789 .bind = netlink_bind,
2790 .connect = netlink_connect,
2791 .socketpair = sock_no_socketpair,
2792 .accept = sock_no_accept,
2793 .getname = netlink_getname,
2794 .poll = netlink_poll,
2795 .ioctl = sock_no_ioctl,
2796 .listen = sock_no_listen,
2797 .shutdown = sock_no_shutdown,
2798 .setsockopt = netlink_setsockopt,
2799 .getsockopt = netlink_getsockopt,
2800 .sendmsg = netlink_sendmsg,
2801 .recvmsg = netlink_recvmsg,
2802 .mmap = netlink_mmap,
2803 .sendpage = sock_no_sendpage,
2806 static const struct net_proto_family netlink_family_ops = {
2807 .family = PF_NETLINK,
2808 .create = netlink_create,
2809 .owner = THIS_MODULE, /* for consistency 8) */
2812 static int __net_init netlink_net_init(struct net *net)
2814 #ifdef CONFIG_PROC_FS
2815 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
2821 static void __net_exit netlink_net_exit(struct net *net)
2823 #ifdef CONFIG_PROC_FS
2824 remove_proc_entry("netlink", net->proc_net);
2828 static void __init netlink_add_usersock_entry(void)
2830 struct listeners *listeners;
2833 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
2835 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
2837 netlink_table_grab();
2839 nl_table[NETLINK_USERSOCK].groups = groups;
2840 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
2841 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2842 nl_table[NETLINK_USERSOCK].registered = 1;
2843 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
2845 netlink_table_ungrab();
2848 static struct pernet_operations __net_initdata netlink_net_ops = {
2849 .init = netlink_net_init,
2850 .exit = netlink_net_exit,
2853 static int __init netlink_proto_init(void)
2856 unsigned long limit;
2858 int err = proto_register(&netlink_proto, 0);
2863 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
2865 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
2869 if (totalram_pages >= (128 * 1024))
2870 limit = totalram_pages >> (21 - PAGE_SHIFT);
2872 limit = totalram_pages >> (23 - PAGE_SHIFT);
2874 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2875 limit = (1UL << order) / sizeof(struct hlist_head);
2876 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
2878 for (i = 0; i < MAX_LINKS; i++) {
2879 struct nl_portid_hash *hash = &nl_table[i].hash;
2881 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
2884 nl_portid_hash_free(nl_table[i].hash.table,
2885 1 * sizeof(*hash->table));
2889 hash->max_shift = order;
2892 hash->rehash_time = jiffies;
2895 netlink_add_usersock_entry();
2897 sock_register(&netlink_family_ops);
2898 register_pernet_subsys(&netlink_net_ops);
2899 /* The netlink device handler may be needed early. */
2904 panic("netlink_init: Cannot allocate nl_table\n");
2907 core_initcall(netlink_proto_init);