lib: Resizable, Scalable, Concurrent Hash Table
[linux-block.git] / net / netlink / af_netlink.c
CommitLineData
1da177e4
LT
1/*
2 * NETLINK Kernel-user communication protocol.
3 *
113aa838 4 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
1da177e4 5 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
cd1df525 6 * Patrick McHardy <kaber@trash.net>
1da177e4
LT
7 *
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.
746fac4d 12 *
1da177e4
LT
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)
4fdb3bb7
HW
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
1da177e4
LT
23 */
24
1da177e4
LT
25#include <linux/module.h>
26
4fc268d2 27#include <linux/capability.h>
1da177e4
LT
28#include <linux/kernel.h>
29#include <linux/init.h>
1da177e4
LT
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>
36#include <linux/un.h>
37#include <linux/fcntl.h>
38#include <linux/termios.h>
39#include <linux/sockios.h>
40#include <linux/net.h>
41#include <linux/fs.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>
1da177e4
LT
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>
55#include <linux/mm.h>
56#include <linux/types.h>
54e0f520 57#include <linux/audit.h>
af65bdfc 58#include <linux/mutex.h>
ccdfcc39 59#include <linux/vmalloc.h>
bcbde0d4 60#include <linux/if_arp.h>
9652e931 61#include <asm/cacheflush.h>
54e0f520 62
457c4cbc 63#include <net/net_namespace.h>
1da177e4
LT
64#include <net/sock.h>
65#include <net/scm.h>
82ace47a 66#include <net/netlink.h>
1da177e4 67
0f29c768 68#include "af_netlink.h"
1da177e4 69
5c398dc8
ED
70struct listeners {
71 struct rcu_head rcu;
72 unsigned long masks[0];
6c04bb18
JB
73};
74
cd967e05
PM
75/* state bits */
76#define NETLINK_CONGESTED 0x0
77
78/* flags */
77247bbb 79#define NETLINK_KERNEL_SOCKET 0x1
9a4595bc 80#define NETLINK_RECV_PKTINFO 0x2
be0c22a4 81#define NETLINK_BROADCAST_SEND_ERROR 0x4
38938bfe 82#define NETLINK_RECV_NO_ENOBUFS 0x8
77247bbb 83
035c4c16 84static inline int netlink_is_kernel(struct sock *sk)
aed81560
DL
85{
86 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
87}
88
0f29c768
AV
89struct netlink_table *nl_table;
90EXPORT_SYMBOL_GPL(nl_table);
1da177e4
LT
91
92static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
93
94static int netlink_dump(struct sock *sk);
9652e931 95static void netlink_skb_destructor(struct sk_buff *skb);
1da177e4 96
0f29c768
AV
97DEFINE_RWLOCK(nl_table_lock);
98EXPORT_SYMBOL_GPL(nl_table_lock);
1da177e4
LT
99static atomic_t nl_table_users = ATOMIC_INIT(0);
100
6d772ac5
ED
101#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
102
e041c683 103static ATOMIC_NOTIFIER_HEAD(netlink_chain);
1da177e4 104
bcbde0d4
DB
105static DEFINE_SPINLOCK(netlink_tap_lock);
106static struct list_head netlink_tap_all __read_mostly;
107
b57ef81f 108static inline u32 netlink_group_mask(u32 group)
d629b836
PM
109{
110 return group ? 1 << (group - 1) : 0;
111}
112
15e47304 113static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
1da177e4 114{
15e47304 115 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
1da177e4
LT
116}
117
bcbde0d4
DB
118int netlink_add_tap(struct netlink_tap *nt)
119{
120 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
121 return -EINVAL;
122
123 spin_lock(&netlink_tap_lock);
124 list_add_rcu(&nt->list, &netlink_tap_all);
125 spin_unlock(&netlink_tap_lock);
126
127 if (nt->module)
128 __module_get(nt->module);
129
130 return 0;
131}
132EXPORT_SYMBOL_GPL(netlink_add_tap);
133
2173f8d9 134static int __netlink_remove_tap(struct netlink_tap *nt)
bcbde0d4
DB
135{
136 bool found = false;
137 struct netlink_tap *tmp;
138
139 spin_lock(&netlink_tap_lock);
140
141 list_for_each_entry(tmp, &netlink_tap_all, list) {
142 if (nt == tmp) {
143 list_del_rcu(&nt->list);
144 found = true;
145 goto out;
146 }
147 }
148
149 pr_warn("__netlink_remove_tap: %p not found\n", nt);
150out:
151 spin_unlock(&netlink_tap_lock);
152
153 if (found && nt->module)
154 module_put(nt->module);
155
156 return found ? 0 : -ENODEV;
157}
bcbde0d4
DB
158
159int netlink_remove_tap(struct netlink_tap *nt)
160{
161 int ret;
162
163 ret = __netlink_remove_tap(nt);
164 synchronize_net();
165
166 return ret;
167}
168EXPORT_SYMBOL_GPL(netlink_remove_tap);
169
5ffd5cdd
DB
170static bool netlink_filter_tap(const struct sk_buff *skb)
171{
172 struct sock *sk = skb->sk;
5ffd5cdd
DB
173
174 /* We take the more conservative approach and
175 * whitelist socket protocols that may pass.
176 */
177 switch (sk->sk_protocol) {
178 case NETLINK_ROUTE:
179 case NETLINK_USERSOCK:
180 case NETLINK_SOCK_DIAG:
181 case NETLINK_NFLOG:
182 case NETLINK_XFRM:
183 case NETLINK_FIB_LOOKUP:
184 case NETLINK_NETFILTER:
185 case NETLINK_GENERIC:
498044bb 186 return true;
5ffd5cdd
DB
187 }
188
498044bb 189 return false;
5ffd5cdd
DB
190}
191
bcbde0d4
DB
192static int __netlink_deliver_tap_skb(struct sk_buff *skb,
193 struct net_device *dev)
194{
195 struct sk_buff *nskb;
5ffd5cdd 196 struct sock *sk = skb->sk;
bcbde0d4
DB
197 int ret = -ENOMEM;
198
199 dev_hold(dev);
200 nskb = skb_clone(skb, GFP_ATOMIC);
201 if (nskb) {
202 nskb->dev = dev;
5ffd5cdd 203 nskb->protocol = htons((u16) sk->sk_protocol);
604d13c9
DB
204 nskb->pkt_type = netlink_is_kernel(sk) ?
205 PACKET_KERNEL : PACKET_USER;
5ffd5cdd 206
bcbde0d4
DB
207 ret = dev_queue_xmit(nskb);
208 if (unlikely(ret > 0))
209 ret = net_xmit_errno(ret);
210 }
211
212 dev_put(dev);
213 return ret;
214}
215
216static void __netlink_deliver_tap(struct sk_buff *skb)
217{
218 int ret;
219 struct netlink_tap *tmp;
220
5ffd5cdd
DB
221 if (!netlink_filter_tap(skb))
222 return;
223
bcbde0d4
DB
224 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
225 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
226 if (unlikely(ret))
227 break;
228 }
229}
230
231static void netlink_deliver_tap(struct sk_buff *skb)
232{
233 rcu_read_lock();
234
235 if (unlikely(!list_empty(&netlink_tap_all)))
236 __netlink_deliver_tap(skb);
237
238 rcu_read_unlock();
239}
240
73bfd370
DB
241static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
242 struct sk_buff *skb)
243{
244 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
245 netlink_deliver_tap(skb);
246}
247
cd1df525
PM
248static void netlink_overrun(struct sock *sk)
249{
250 struct netlink_sock *nlk = nlk_sk(sk);
251
252 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
253 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
254 sk->sk_err = ENOBUFS;
255 sk->sk_error_report(sk);
256 }
257 }
258 atomic_inc(&sk->sk_drops);
259}
260
261static void netlink_rcv_wake(struct sock *sk)
262{
263 struct netlink_sock *nlk = nlk_sk(sk);
264
265 if (skb_queue_empty(&sk->sk_receive_queue))
266 clear_bit(NETLINK_CONGESTED, &nlk->state);
267 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
268 wake_up_interruptible(&nlk->wait);
269}
270
ccdfcc39 271#ifdef CONFIG_NETLINK_MMAP
9652e931
PM
272static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
273{
274 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
275}
276
f9c22888
PM
277static bool netlink_rx_is_mmaped(struct sock *sk)
278{
279 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
280}
281
5fd96123
PM
282static bool netlink_tx_is_mmaped(struct sock *sk)
283{
284 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
285}
286
ccdfcc39
PM
287static __pure struct page *pgvec_to_page(const void *addr)
288{
289 if (is_vmalloc_addr(addr))
290 return vmalloc_to_page(addr);
291 else
292 return virt_to_page(addr);
293}
294
295static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
296{
297 unsigned int i;
298
299 for (i = 0; i < len; i++) {
300 if (pg_vec[i] != NULL) {
301 if (is_vmalloc_addr(pg_vec[i]))
302 vfree(pg_vec[i]);
303 else
304 free_pages((unsigned long)pg_vec[i], order);
305 }
306 }
307 kfree(pg_vec);
308}
309
310static void *alloc_one_pg_vec_page(unsigned long order)
311{
312 void *buffer;
313 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
314 __GFP_NOWARN | __GFP_NORETRY;
315
316 buffer = (void *)__get_free_pages(gfp_flags, order);
317 if (buffer != NULL)
318 return buffer;
319
320 buffer = vzalloc((1 << order) * PAGE_SIZE);
321 if (buffer != NULL)
322 return buffer;
323
324 gfp_flags &= ~__GFP_NORETRY;
325 return (void *)__get_free_pages(gfp_flags, order);
326}
327
328static void **alloc_pg_vec(struct netlink_sock *nlk,
329 struct nl_mmap_req *req, unsigned int order)
330{
331 unsigned int block_nr = req->nm_block_nr;
332 unsigned int i;
8a849bb7 333 void **pg_vec;
ccdfcc39
PM
334
335 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
336 if (pg_vec == NULL)
337 return NULL;
338
339 for (i = 0; i < block_nr; i++) {
8a849bb7 340 pg_vec[i] = alloc_one_pg_vec_page(order);
ccdfcc39
PM
341 if (pg_vec[i] == NULL)
342 goto err1;
343 }
344
345 return pg_vec;
346err1:
347 free_pg_vec(pg_vec, order, block_nr);
348 return NULL;
349}
350
351static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
352 bool closing, bool tx_ring)
353{
354 struct netlink_sock *nlk = nlk_sk(sk);
355 struct netlink_ring *ring;
356 struct sk_buff_head *queue;
357 void **pg_vec = NULL;
358 unsigned int order = 0;
359 int err;
360
361 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
362 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
363
364 if (!closing) {
365 if (atomic_read(&nlk->mapped))
366 return -EBUSY;
367 if (atomic_read(&ring->pending))
368 return -EBUSY;
369 }
370
371 if (req->nm_block_nr) {
372 if (ring->pg_vec != NULL)
373 return -EBUSY;
374
375 if ((int)req->nm_block_size <= 0)
376 return -EINVAL;
74e83b23 377 if (!PAGE_ALIGNED(req->nm_block_size))
ccdfcc39
PM
378 return -EINVAL;
379 if (req->nm_frame_size < NL_MMAP_HDRLEN)
380 return -EINVAL;
381 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
382 return -EINVAL;
383
384 ring->frames_per_block = req->nm_block_size /
385 req->nm_frame_size;
386 if (ring->frames_per_block == 0)
387 return -EINVAL;
388 if (ring->frames_per_block * req->nm_block_nr !=
389 req->nm_frame_nr)
390 return -EINVAL;
391
392 order = get_order(req->nm_block_size);
393 pg_vec = alloc_pg_vec(nlk, req, order);
394 if (pg_vec == NULL)
395 return -ENOMEM;
396 } else {
397 if (req->nm_frame_nr)
398 return -EINVAL;
399 }
400
401 err = -EBUSY;
402 mutex_lock(&nlk->pg_vec_lock);
403 if (closing || atomic_read(&nlk->mapped) == 0) {
404 err = 0;
405 spin_lock_bh(&queue->lock);
406
407 ring->frame_max = req->nm_frame_nr - 1;
408 ring->head = 0;
409 ring->frame_size = req->nm_frame_size;
410 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
411
412 swap(ring->pg_vec_len, req->nm_block_nr);
413 swap(ring->pg_vec_order, order);
414 swap(ring->pg_vec, pg_vec);
415
416 __skb_queue_purge(queue);
417 spin_unlock_bh(&queue->lock);
418
419 WARN_ON(atomic_read(&nlk->mapped));
420 }
421 mutex_unlock(&nlk->pg_vec_lock);
422
423 if (pg_vec)
424 free_pg_vec(pg_vec, order, req->nm_block_nr);
425 return err;
426}
427
428static void netlink_mm_open(struct vm_area_struct *vma)
429{
430 struct file *file = vma->vm_file;
431 struct socket *sock = file->private_data;
432 struct sock *sk = sock->sk;
433
434 if (sk)
435 atomic_inc(&nlk_sk(sk)->mapped);
436}
437
438static void netlink_mm_close(struct vm_area_struct *vma)
439{
440 struct file *file = vma->vm_file;
441 struct socket *sock = file->private_data;
442 struct sock *sk = sock->sk;
443
444 if (sk)
445 atomic_dec(&nlk_sk(sk)->mapped);
446}
447
448static const struct vm_operations_struct netlink_mmap_ops = {
449 .open = netlink_mm_open,
450 .close = netlink_mm_close,
451};
452
453static int netlink_mmap(struct file *file, struct socket *sock,
454 struct vm_area_struct *vma)
455{
456 struct sock *sk = sock->sk;
457 struct netlink_sock *nlk = nlk_sk(sk);
458 struct netlink_ring *ring;
459 unsigned long start, size, expected;
460 unsigned int i;
461 int err = -EINVAL;
462
463 if (vma->vm_pgoff)
464 return -EINVAL;
465
466 mutex_lock(&nlk->pg_vec_lock);
467
468 expected = 0;
469 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
470 if (ring->pg_vec == NULL)
471 continue;
472 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
473 }
474
475 if (expected == 0)
476 goto out;
477
478 size = vma->vm_end - vma->vm_start;
479 if (size != expected)
480 goto out;
481
482 start = vma->vm_start;
483 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
484 if (ring->pg_vec == NULL)
485 continue;
486
487 for (i = 0; i < ring->pg_vec_len; i++) {
488 struct page *page;
489 void *kaddr = ring->pg_vec[i];
490 unsigned int pg_num;
491
492 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
493 page = pgvec_to_page(kaddr);
494 err = vm_insert_page(vma, start, page);
495 if (err < 0)
496 goto out;
497 start += PAGE_SIZE;
498 kaddr += PAGE_SIZE;
499 }
500 }
501 }
502
503 atomic_inc(&nlk->mapped);
504 vma->vm_ops = &netlink_mmap_ops;
505 err = 0;
506out:
507 mutex_unlock(&nlk->pg_vec_lock);
7cdbac71 508 return err;
ccdfcc39 509}
9652e931
PM
510
511static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
512{
513#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
514 struct page *p_start, *p_end;
515
516 /* First page is flushed through netlink_{get,set}_status */
517 p_start = pgvec_to_page(hdr + PAGE_SIZE);
1d5085cb 518 p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
9652e931
PM
519 while (p_start <= p_end) {
520 flush_dcache_page(p_start);
521 p_start++;
522 }
523#endif
524}
525
526static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
527{
528 smp_rmb();
529 flush_dcache_page(pgvec_to_page(hdr));
530 return hdr->nm_status;
531}
532
533static void netlink_set_status(struct nl_mmap_hdr *hdr,
534 enum nl_mmap_status status)
535{
536 hdr->nm_status = status;
537 flush_dcache_page(pgvec_to_page(hdr));
538 smp_wmb();
539}
540
541static struct nl_mmap_hdr *
542__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
543{
544 unsigned int pg_vec_pos, frame_off;
545
546 pg_vec_pos = pos / ring->frames_per_block;
547 frame_off = pos % ring->frames_per_block;
548
549 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
550}
551
552static struct nl_mmap_hdr *
553netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
554 enum nl_mmap_status status)
555{
556 struct nl_mmap_hdr *hdr;
557
558 hdr = __netlink_lookup_frame(ring, pos);
559 if (netlink_get_status(hdr) != status)
560 return NULL;
561
562 return hdr;
563}
564
565static struct nl_mmap_hdr *
566netlink_current_frame(const struct netlink_ring *ring,
567 enum nl_mmap_status status)
568{
569 return netlink_lookup_frame(ring, ring->head, status);
570}
571
572static struct nl_mmap_hdr *
573netlink_previous_frame(const struct netlink_ring *ring,
574 enum nl_mmap_status status)
575{
576 unsigned int prev;
577
578 prev = ring->head ? ring->head - 1 : ring->frame_max;
579 return netlink_lookup_frame(ring, prev, status);
580}
581
582static void netlink_increment_head(struct netlink_ring *ring)
583{
584 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
585}
586
587static void netlink_forward_ring(struct netlink_ring *ring)
588{
589 unsigned int head = ring->head, pos = head;
590 const struct nl_mmap_hdr *hdr;
591
592 do {
593 hdr = __netlink_lookup_frame(ring, pos);
594 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
595 break;
596 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
597 break;
598 netlink_increment_head(ring);
599 } while (ring->head != head);
600}
601
cd1df525
PM
602static bool netlink_dump_space(struct netlink_sock *nlk)
603{
604 struct netlink_ring *ring = &nlk->rx_ring;
605 struct nl_mmap_hdr *hdr;
606 unsigned int n;
607
608 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
609 if (hdr == NULL)
610 return false;
611
612 n = ring->head + ring->frame_max / 2;
613 if (n > ring->frame_max)
614 n -= ring->frame_max;
615
616 hdr = __netlink_lookup_frame(ring, n);
617
618 return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
619}
620
9652e931
PM
621static unsigned int netlink_poll(struct file *file, struct socket *sock,
622 poll_table *wait)
623{
624 struct sock *sk = sock->sk;
625 struct netlink_sock *nlk = nlk_sk(sk);
626 unsigned int mask;
cd1df525 627 int err;
9652e931 628
cd1df525
PM
629 if (nlk->rx_ring.pg_vec != NULL) {
630 /* Memory mapped sockets don't call recvmsg(), so flow control
631 * for dumps is performed here. A dump is allowed to continue
632 * if at least half the ring is unused.
633 */
16b304f3 634 while (nlk->cb_running && netlink_dump_space(nlk)) {
cd1df525
PM
635 err = netlink_dump(sk);
636 if (err < 0) {
ac30ef83 637 sk->sk_err = -err;
cd1df525
PM
638 sk->sk_error_report(sk);
639 break;
640 }
641 }
642 netlink_rcv_wake(sk);
643 }
5fd96123 644
9652e931
PM
645 mask = datagram_poll(file, sock, wait);
646
647 spin_lock_bh(&sk->sk_receive_queue.lock);
648 if (nlk->rx_ring.pg_vec) {
649 netlink_forward_ring(&nlk->rx_ring);
650 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
651 mask |= POLLIN | POLLRDNORM;
652 }
653 spin_unlock_bh(&sk->sk_receive_queue.lock);
654
655 spin_lock_bh(&sk->sk_write_queue.lock);
656 if (nlk->tx_ring.pg_vec) {
657 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
658 mask |= POLLOUT | POLLWRNORM;
659 }
660 spin_unlock_bh(&sk->sk_write_queue.lock);
661
662 return mask;
663}
664
665static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
666{
667 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
668}
669
670static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
671 struct netlink_ring *ring,
672 struct nl_mmap_hdr *hdr)
673{
674 unsigned int size;
675 void *data;
676
677 size = ring->frame_size - NL_MMAP_HDRLEN;
678 data = (void *)hdr + NL_MMAP_HDRLEN;
679
680 skb->head = data;
681 skb->data = data;
682 skb_reset_tail_pointer(skb);
683 skb->end = skb->tail + size;
684 skb->len = 0;
685
686 skb->destructor = netlink_skb_destructor;
687 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
688 NETLINK_CB(skb).sk = sk;
689}
5fd96123
PM
690
691static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
692 u32 dst_portid, u32 dst_group,
693 struct sock_iocb *siocb)
694{
695 struct netlink_sock *nlk = nlk_sk(sk);
696 struct netlink_ring *ring;
697 struct nl_mmap_hdr *hdr;
698 struct sk_buff *skb;
699 unsigned int maxlen;
700 bool excl = true;
701 int err = 0, len = 0;
702
703 /* Netlink messages are validated by the receiver before processing.
704 * In order to avoid userspace changing the contents of the message
705 * after validation, the socket and the ring may only be used by a
706 * single process, otherwise we fall back to copying.
707 */
708 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
709 atomic_read(&nlk->mapped) > 1)
710 excl = false;
711
712 mutex_lock(&nlk->pg_vec_lock);
713
714 ring = &nlk->tx_ring;
715 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
716
717 do {
718 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
719 if (hdr == NULL) {
720 if (!(msg->msg_flags & MSG_DONTWAIT) &&
721 atomic_read(&nlk->tx_ring.pending))
722 schedule();
723 continue;
724 }
725 if (hdr->nm_len > maxlen) {
726 err = -EINVAL;
727 goto out;
728 }
729
730 netlink_frame_flush_dcache(hdr);
731
732 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
733 skb = alloc_skb_head(GFP_KERNEL);
734 if (skb == NULL) {
735 err = -ENOBUFS;
736 goto out;
737 }
738 sock_hold(sk);
739 netlink_ring_setup_skb(skb, sk, ring, hdr);
740 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
741 __skb_put(skb, hdr->nm_len);
742 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
743 atomic_inc(&ring->pending);
744 } else {
745 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
746 if (skb == NULL) {
747 err = -ENOBUFS;
748 goto out;
749 }
750 __skb_put(skb, hdr->nm_len);
751 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
752 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
753 }
754
755 netlink_increment_head(ring);
756
757 NETLINK_CB(skb).portid = nlk->portid;
758 NETLINK_CB(skb).dst_group = dst_group;
759 NETLINK_CB(skb).creds = siocb->scm->creds;
760
761 err = security_netlink_send(sk, skb);
762 if (err) {
763 kfree_skb(skb);
764 goto out;
765 }
766
767 if (unlikely(dst_group)) {
768 atomic_inc(&skb->users);
769 netlink_broadcast(sk, skb, dst_portid, dst_group,
770 GFP_KERNEL);
771 }
772 err = netlink_unicast(sk, skb, dst_portid,
773 msg->msg_flags & MSG_DONTWAIT);
774 if (err < 0)
775 goto out;
776 len += err;
777
778 } while (hdr != NULL ||
779 (!(msg->msg_flags & MSG_DONTWAIT) &&
780 atomic_read(&nlk->tx_ring.pending)));
781
782 if (len > 0)
783 err = len;
784out:
785 mutex_unlock(&nlk->pg_vec_lock);
786 return err;
787}
f9c22888
PM
788
789static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
790{
791 struct nl_mmap_hdr *hdr;
792
793 hdr = netlink_mmap_hdr(skb);
794 hdr->nm_len = skb->len;
795 hdr->nm_group = NETLINK_CB(skb).dst_group;
796 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
1bf9310a
ND
797 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
798 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
f9c22888
PM
799 netlink_frame_flush_dcache(hdr);
800 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
801
802 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
803 kfree_skb(skb);
804}
805
806static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
807{
808 struct netlink_sock *nlk = nlk_sk(sk);
809 struct netlink_ring *ring = &nlk->rx_ring;
810 struct nl_mmap_hdr *hdr;
811
812 spin_lock_bh(&sk->sk_receive_queue.lock);
813 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
814 if (hdr == NULL) {
815 spin_unlock_bh(&sk->sk_receive_queue.lock);
816 kfree_skb(skb);
cd1df525 817 netlink_overrun(sk);
f9c22888
PM
818 return;
819 }
820 netlink_increment_head(ring);
821 __skb_queue_tail(&sk->sk_receive_queue, skb);
822 spin_unlock_bh(&sk->sk_receive_queue.lock);
823
824 hdr->nm_len = skb->len;
825 hdr->nm_group = NETLINK_CB(skb).dst_group;
826 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
1bf9310a
ND
827 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
828 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
f9c22888
PM
829 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
830}
831
ccdfcc39 832#else /* CONFIG_NETLINK_MMAP */
9652e931 833#define netlink_skb_is_mmaped(skb) false
f9c22888 834#define netlink_rx_is_mmaped(sk) false
5fd96123 835#define netlink_tx_is_mmaped(sk) false
ccdfcc39 836#define netlink_mmap sock_no_mmap
9652e931 837#define netlink_poll datagram_poll
5fd96123 838#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
ccdfcc39
PM
839#endif /* CONFIG_NETLINK_MMAP */
840
cf0a018a
PM
841static void netlink_skb_destructor(struct sk_buff *skb)
842{
9652e931
PM
843#ifdef CONFIG_NETLINK_MMAP
844 struct nl_mmap_hdr *hdr;
845 struct netlink_ring *ring;
846 struct sock *sk;
847
848 /* If a packet from the kernel to userspace was freed because of an
849 * error without being delivered to userspace, the kernel must reset
850 * the status. In the direction userspace to kernel, the status is
851 * always reset here after the packet was processed and freed.
852 */
853 if (netlink_skb_is_mmaped(skb)) {
854 hdr = netlink_mmap_hdr(skb);
855 sk = NETLINK_CB(skb).sk;
856
5fd96123
PM
857 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
858 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
859 ring = &nlk_sk(sk)->tx_ring;
860 } else {
861 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
862 hdr->nm_len = 0;
863 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
864 }
865 ring = &nlk_sk(sk)->rx_ring;
9652e931 866 }
9652e931
PM
867
868 WARN_ON(atomic_read(&ring->pending) == 0);
869 atomic_dec(&ring->pending);
870 sock_put(sk);
871
5e71d9d7 872 skb->head = NULL;
9652e931
PM
873 }
874#endif
c05cdb1b 875 if (is_vmalloc_addr(skb->head)) {
3a36515f
PN
876 if (!skb->cloned ||
877 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
878 vfree(skb->head);
879
c05cdb1b
PNA
880 skb->head = NULL;
881 }
9652e931
PM
882 if (skb->sk != NULL)
883 sock_rfree(skb);
cf0a018a
PM
884}
885
886static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
887{
888 WARN_ON(skb->sk != NULL);
889 skb->sk = sk;
890 skb->destructor = netlink_skb_destructor;
891 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
892 sk_mem_charge(sk, skb->truesize);
893}
894
1da177e4
LT
895static void netlink_sock_destruct(struct sock *sk)
896{
3f660d66
HX
897 struct netlink_sock *nlk = nlk_sk(sk);
898
16b304f3
PS
899 if (nlk->cb_running) {
900 if (nlk->cb.done)
901 nlk->cb.done(&nlk->cb);
6dc878a8 902
16b304f3
PS
903 module_put(nlk->cb.module);
904 kfree_skb(nlk->cb.skb);
3f660d66
HX
905 }
906
1da177e4 907 skb_queue_purge(&sk->sk_receive_queue);
ccdfcc39
PM
908#ifdef CONFIG_NETLINK_MMAP
909 if (1) {
910 struct nl_mmap_req req;
911
912 memset(&req, 0, sizeof(req));
913 if (nlk->rx_ring.pg_vec)
914 netlink_set_ring(sk, &req, true, false);
915 memset(&req, 0, sizeof(req));
916 if (nlk->tx_ring.pg_vec)
917 netlink_set_ring(sk, &req, true, true);
918 }
919#endif /* CONFIG_NETLINK_MMAP */
1da177e4
LT
920
921 if (!sock_flag(sk, SOCK_DEAD)) {
6ac552fd 922 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
1da177e4
LT
923 return;
924 }
547b792c
IJ
925
926 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
927 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
928 WARN_ON(nlk_sk(sk)->groups);
1da177e4
LT
929}
930
6ac552fd
PM
931/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
932 * SMP. Look, when several writers sleep and reader wakes them up, all but one
1da177e4
LT
933 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
934 * this, _but_ remember, it adds useless work on UP machines.
935 */
936
d136f1bd 937void netlink_table_grab(void)
9a429c49 938 __acquires(nl_table_lock)
1da177e4 939{
d136f1bd
JB
940 might_sleep();
941
6abd219c 942 write_lock_irq(&nl_table_lock);
1da177e4
LT
943
944 if (atomic_read(&nl_table_users)) {
945 DECLARE_WAITQUEUE(wait, current);
946
947 add_wait_queue_exclusive(&nl_table_wait, &wait);
6ac552fd 948 for (;;) {
1da177e4
LT
949 set_current_state(TASK_UNINTERRUPTIBLE);
950 if (atomic_read(&nl_table_users) == 0)
951 break;
6abd219c 952 write_unlock_irq(&nl_table_lock);
1da177e4 953 schedule();
6abd219c 954 write_lock_irq(&nl_table_lock);
1da177e4
LT
955 }
956
957 __set_current_state(TASK_RUNNING);
958 remove_wait_queue(&nl_table_wait, &wait);
959 }
960}
961
d136f1bd 962void netlink_table_ungrab(void)
9a429c49 963 __releases(nl_table_lock)
1da177e4 964{
6abd219c 965 write_unlock_irq(&nl_table_lock);
1da177e4
LT
966 wake_up(&nl_table_wait);
967}
968
6ac552fd 969static inline void
1da177e4
LT
970netlink_lock_table(void)
971{
972 /* read_lock() synchronizes us to netlink_table_grab */
973
974 read_lock(&nl_table_lock);
975 atomic_inc(&nl_table_users);
976 read_unlock(&nl_table_lock);
977}
978
6ac552fd 979static inline void
1da177e4
LT
980netlink_unlock_table(void)
981{
982 if (atomic_dec_and_test(&nl_table_users))
983 wake_up(&nl_table_wait);
984}
985
da12c90e
G
986static bool netlink_compare(struct net *net, struct sock *sk)
987{
988 return net_eq(sock_net(sk), net);
989}
990
15e47304 991static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
1da177e4 992{
da12c90e
G
993 struct netlink_table *table = &nl_table[protocol];
994 struct nl_portid_hash *hash = &table->hash;
1da177e4
LT
995 struct hlist_head *head;
996 struct sock *sk;
1da177e4
LT
997
998 read_lock(&nl_table_lock);
15e47304 999 head = nl_portid_hashfn(hash, portid);
b67bfe0d 1000 sk_for_each(sk, head) {
da12c90e
G
1001 if (table->compare(net, sk) &&
1002 (nlk_sk(sk)->portid == portid)) {
1da177e4
LT
1003 sock_hold(sk);
1004 goto found;
1005 }
1006 }
1007 sk = NULL;
1008found:
1009 read_unlock(&nl_table_lock);
1010 return sk;
1011}
1012
15e47304 1013static struct hlist_head *nl_portid_hash_zalloc(size_t size)
1da177e4
LT
1014{
1015 if (size <= PAGE_SIZE)
ea72912c 1016 return kzalloc(size, GFP_ATOMIC);
1da177e4
LT
1017 else
1018 return (struct hlist_head *)
ea72912c
ED
1019 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
1020 get_order(size));
1da177e4
LT
1021}
1022
15e47304 1023static void nl_portid_hash_free(struct hlist_head *table, size_t size)
1da177e4
LT
1024{
1025 if (size <= PAGE_SIZE)
1026 kfree(table);
1027 else
1028 free_pages((unsigned long)table, get_order(size));
1029}
1030
15e47304 1031static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
1da177e4
LT
1032{
1033 unsigned int omask, mask, shift;
1034 size_t osize, size;
1035 struct hlist_head *otable, *table;
1036 int i;
1037
1038 omask = mask = hash->mask;
1039 osize = size = (mask + 1) * sizeof(*table);
1040 shift = hash->shift;
1041
1042 if (grow) {
1043 if (++shift > hash->max_shift)
1044 return 0;
1045 mask = mask * 2 + 1;
1046 size *= 2;
1047 }
1048
15e47304 1049 table = nl_portid_hash_zalloc(size);
1da177e4
LT
1050 if (!table)
1051 return 0;
1052
1da177e4
LT
1053 otable = hash->table;
1054 hash->table = table;
1055 hash->mask = mask;
1056 hash->shift = shift;
1057 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
1058
1059 for (i = 0; i <= omask; i++) {
1060 struct sock *sk;
b67bfe0d 1061 struct hlist_node *tmp;
1da177e4 1062
b67bfe0d 1063 sk_for_each_safe(sk, tmp, &otable[i])
15e47304 1064 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
1da177e4
LT
1065 }
1066
15e47304 1067 nl_portid_hash_free(otable, osize);
1da177e4
LT
1068 hash->rehash_time = jiffies + 10 * 60 * HZ;
1069 return 1;
1070}
1071
15e47304 1072static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
1da177e4
LT
1073{
1074 int avg = hash->entries >> hash->shift;
1075
15e47304 1076 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
1da177e4
LT
1077 return 1;
1078
1079 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
15e47304 1080 nl_portid_hash_rehash(hash, 0);
1da177e4
LT
1081 return 1;
1082 }
1083
1084 return 0;
1085}
1086
90ddc4f0 1087static const struct proto_ops netlink_ops;
1da177e4 1088
4277a083
PM
1089static void
1090netlink_update_listeners(struct sock *sk)
1091{
1092 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
4277a083
PM
1093 unsigned long mask;
1094 unsigned int i;
6d772ac5
ED
1095 struct listeners *listeners;
1096
1097 listeners = nl_deref_protected(tbl->listeners);
1098 if (!listeners)
1099 return;
4277a083 1100
b4ff4f04 1101 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
4277a083 1102 mask = 0;
b67bfe0d 1103 sk_for_each_bound(sk, &tbl->mc_list) {
b4ff4f04
JB
1104 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
1105 mask |= nlk_sk(sk)->groups[i];
1106 }
6d772ac5 1107 listeners->masks[i] = mask;
4277a083
PM
1108 }
1109 /* this function is only called with the netlink table "grabbed", which
1110 * makes sure updates are visible before bind or setsockopt return. */
1111}
1112
15e47304 1113static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
1da177e4 1114{
da12c90e
G
1115 struct netlink_table *table = &nl_table[sk->sk_protocol];
1116 struct nl_portid_hash *hash = &table->hash;
1da177e4
LT
1117 struct hlist_head *head;
1118 int err = -EADDRINUSE;
1119 struct sock *osk;
1da177e4
LT
1120 int len;
1121
1122 netlink_table_grab();
15e47304 1123 head = nl_portid_hashfn(hash, portid);
1da177e4 1124 len = 0;
b67bfe0d 1125 sk_for_each(osk, head) {
da12c90e
G
1126 if (table->compare(net, osk) &&
1127 (nlk_sk(osk)->portid == portid))
1da177e4
LT
1128 break;
1129 len++;
1130 }
b67bfe0d 1131 if (osk)
1da177e4
LT
1132 goto err;
1133
1134 err = -EBUSY;
15e47304 1135 if (nlk_sk(sk)->portid)
1da177e4
LT
1136 goto err;
1137
1138 err = -ENOMEM;
1139 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
1140 goto err;
1141
15e47304
EB
1142 if (len && nl_portid_hash_dilute(hash, len))
1143 head = nl_portid_hashfn(hash, portid);
1da177e4 1144 hash->entries++;
15e47304 1145 nlk_sk(sk)->portid = portid;
1da177e4
LT
1146 sk_add_node(sk, head);
1147 err = 0;
1148
1149err:
1150 netlink_table_ungrab();
1151 return err;
1152}
1153
1154static void netlink_remove(struct sock *sk)
1155{
1156 netlink_table_grab();
d470e3b4
DM
1157 if (sk_del_node_init(sk))
1158 nl_table[sk->sk_protocol].hash.entries--;
f7fa9b10 1159 if (nlk_sk(sk)->subscriptions)
1da177e4
LT
1160 __sk_del_bind_node(sk);
1161 netlink_table_ungrab();
1162}
1163
1164static struct proto netlink_proto = {
1165 .name = "NETLINK",
1166 .owner = THIS_MODULE,
1167 .obj_size = sizeof(struct netlink_sock),
1168};
1169
1b8d7ae4
EB
1170static int __netlink_create(struct net *net, struct socket *sock,
1171 struct mutex *cb_mutex, int protocol)
1da177e4
LT
1172{
1173 struct sock *sk;
1174 struct netlink_sock *nlk;
ab33a171
PM
1175
1176 sock->ops = &netlink_ops;
1177
6257ff21 1178 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
ab33a171
PM
1179 if (!sk)
1180 return -ENOMEM;
1181
1182 sock_init_data(sock, sk);
1183
1184 nlk = nlk_sk(sk);
658cb354 1185 if (cb_mutex) {
ffa4d721 1186 nlk->cb_mutex = cb_mutex;
658cb354 1187 } else {
ffa4d721
PM
1188 nlk->cb_mutex = &nlk->cb_def_mutex;
1189 mutex_init(nlk->cb_mutex);
1190 }
ab33a171 1191 init_waitqueue_head(&nlk->wait);
ccdfcc39
PM
1192#ifdef CONFIG_NETLINK_MMAP
1193 mutex_init(&nlk->pg_vec_lock);
1194#endif
ab33a171
PM
1195
1196 sk->sk_destruct = netlink_sock_destruct;
1197 sk->sk_protocol = protocol;
1198 return 0;
1199}
1200
3f378b68
EP
1201static int netlink_create(struct net *net, struct socket *sock, int protocol,
1202 int kern)
ab33a171
PM
1203{
1204 struct module *module = NULL;
af65bdfc 1205 struct mutex *cb_mutex;
f7fa9b10 1206 struct netlink_sock *nlk;
4f520900
RGB
1207 int (*bind)(int group);
1208 void (*unbind)(int group);
ab33a171 1209 int err = 0;
1da177e4
LT
1210
1211 sock->state = SS_UNCONNECTED;
1212
1213 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1214 return -ESOCKTNOSUPPORT;
1215
6ac552fd 1216 if (protocol < 0 || protocol >= MAX_LINKS)
1da177e4
LT
1217 return -EPROTONOSUPPORT;
1218
77247bbb 1219 netlink_lock_table();
95a5afca 1220#ifdef CONFIG_MODULES
ab33a171 1221 if (!nl_table[protocol].registered) {
77247bbb 1222 netlink_unlock_table();
4fdb3bb7 1223 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
77247bbb 1224 netlink_lock_table();
4fdb3bb7 1225 }
ab33a171
PM
1226#endif
1227 if (nl_table[protocol].registered &&
1228 try_module_get(nl_table[protocol].module))
1229 module = nl_table[protocol].module;
974c37e9
AD
1230 else
1231 err = -EPROTONOSUPPORT;
af65bdfc 1232 cb_mutex = nl_table[protocol].cb_mutex;
03292745 1233 bind = nl_table[protocol].bind;
4f520900 1234 unbind = nl_table[protocol].unbind;
77247bbb 1235 netlink_unlock_table();
4fdb3bb7 1236
974c37e9
AD
1237 if (err < 0)
1238 goto out;
1239
6ac552fd
PM
1240 err = __netlink_create(net, sock, cb_mutex, protocol);
1241 if (err < 0)
f7fa9b10
PM
1242 goto out_module;
1243
6f756a8c 1244 local_bh_disable();
c1fd3b94 1245 sock_prot_inuse_add(net, &netlink_proto, 1);
6f756a8c
DM
1246 local_bh_enable();
1247
f7fa9b10 1248 nlk = nlk_sk(sock->sk);
f7fa9b10 1249 nlk->module = module;
03292745 1250 nlk->netlink_bind = bind;
4f520900 1251 nlk->netlink_unbind = unbind;
ab33a171
PM
1252out:
1253 return err;
1da177e4 1254
ab33a171
PM
1255out_module:
1256 module_put(module);
1257 goto out;
1da177e4
LT
1258}
1259
1260static int netlink_release(struct socket *sock)
1261{
1262 struct sock *sk = sock->sk;
1263 struct netlink_sock *nlk;
1264
1265 if (!sk)
1266 return 0;
1267
1268 netlink_remove(sk);
ac57b3a9 1269 sock_orphan(sk);
1da177e4
LT
1270 nlk = nlk_sk(sk);
1271
3f660d66
HX
1272 /*
1273 * OK. Socket is unlinked, any packets that arrive now
1274 * will be purged.
1275 */
1da177e4 1276
1da177e4
LT
1277 sock->sk = NULL;
1278 wake_up_interruptible_all(&nlk->wait);
1279
1280 skb_queue_purge(&sk->sk_write_queue);
1281
15e47304 1282 if (nlk->portid) {
1da177e4 1283 struct netlink_notify n = {
3b1e0a65 1284 .net = sock_net(sk),
1da177e4 1285 .protocol = sk->sk_protocol,
15e47304 1286 .portid = nlk->portid,
1da177e4 1287 };
e041c683
AS
1288 atomic_notifier_call_chain(&netlink_chain,
1289 NETLINK_URELEASE, &n);
746fac4d 1290 }
4fdb3bb7 1291
5e7c001c 1292 module_put(nlk->module);
4fdb3bb7 1293
4277a083 1294 netlink_table_grab();
aed81560 1295 if (netlink_is_kernel(sk)) {
869e58f8
DL
1296 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1297 if (--nl_table[sk->sk_protocol].registered == 0) {
6d772ac5
ED
1298 struct listeners *old;
1299
1300 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1301 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1302 kfree_rcu(old, rcu);
869e58f8 1303 nl_table[sk->sk_protocol].module = NULL;
9785e10a 1304 nl_table[sk->sk_protocol].bind = NULL;
4f520900 1305 nl_table[sk->sk_protocol].unbind = NULL;
9785e10a 1306 nl_table[sk->sk_protocol].flags = 0;
869e58f8
DL
1307 nl_table[sk->sk_protocol].registered = 0;
1308 }
658cb354 1309 } else if (nlk->subscriptions) {
4277a083 1310 netlink_update_listeners(sk);
658cb354 1311 }
4277a083 1312 netlink_table_ungrab();
77247bbb 1313
f7fa9b10
PM
1314 kfree(nlk->groups);
1315 nlk->groups = NULL;
1316
3755810c 1317 local_bh_disable();
c1fd3b94 1318 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
3755810c 1319 local_bh_enable();
1da177e4
LT
1320 sock_put(sk);
1321 return 0;
1322}
1323
1324static int netlink_autobind(struct socket *sock)
1325{
1326 struct sock *sk = sock->sk;
3b1e0a65 1327 struct net *net = sock_net(sk);
da12c90e
G
1328 struct netlink_table *table = &nl_table[sk->sk_protocol];
1329 struct nl_portid_hash *hash = &table->hash;
1da177e4
LT
1330 struct hlist_head *head;
1331 struct sock *osk;
15e47304 1332 s32 portid = task_tgid_vnr(current);
1da177e4
LT
1333 int err;
1334 static s32 rover = -4097;
1335
1336retry:
1337 cond_resched();
1338 netlink_table_grab();
15e47304 1339 head = nl_portid_hashfn(hash, portid);
b67bfe0d 1340 sk_for_each(osk, head) {
da12c90e 1341 if (!table->compare(net, osk))
b4b51029 1342 continue;
15e47304
EB
1343 if (nlk_sk(osk)->portid == portid) {
1344 /* Bind collision, search negative portid values. */
1345 portid = rover--;
1da177e4
LT
1346 if (rover > -4097)
1347 rover = -4097;
1348 netlink_table_ungrab();
1349 goto retry;
1350 }
1351 }
1352 netlink_table_ungrab();
1353
15e47304 1354 err = netlink_insert(sk, net, portid);
1da177e4
LT
1355 if (err == -EADDRINUSE)
1356 goto retry;
d470e3b4
DM
1357
1358 /* If 2 threads race to autobind, that is fine. */
1359 if (err == -EBUSY)
1360 err = 0;
1361
1362 return err;
1da177e4
LT
1363}
1364
aa4cf945
EB
1365/**
1366 * __netlink_ns_capable - General netlink message capability test
1367 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
1368 * @user_ns: The user namespace of the capability to use
1369 * @cap: The capability to use
1370 *
1371 * Test to see if the opener of the socket we received the message
1372 * from had when the netlink socket was created and the sender of the
1373 * message has has the capability @cap in the user namespace @user_ns.
1374 */
1375bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
1376 struct user_namespace *user_ns, int cap)
1377{
2d7a85f4
EB
1378 return ((nsp->flags & NETLINK_SKB_DST) ||
1379 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
1380 ns_capable(user_ns, cap);
aa4cf945
EB
1381}
1382EXPORT_SYMBOL(__netlink_ns_capable);
1383
1384/**
1385 * netlink_ns_capable - General netlink message capability test
1386 * @skb: socket buffer holding a netlink command from userspace
1387 * @user_ns: The user namespace of the capability to use
1388 * @cap: The capability to use
1389 *
1390 * Test to see if the opener of the socket we received the message
1391 * from had when the netlink socket was created and the sender of the
1392 * message has has the capability @cap in the user namespace @user_ns.
1393 */
1394bool netlink_ns_capable(const struct sk_buff *skb,
1395 struct user_namespace *user_ns, int cap)
1396{
1397 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
1398}
1399EXPORT_SYMBOL(netlink_ns_capable);
1400
1401/**
1402 * netlink_capable - Netlink global message capability test
1403 * @skb: socket buffer holding a netlink command from userspace
1404 * @cap: The capability to use
1405 *
1406 * Test to see if the opener of the socket we received the message
1407 * from had when the netlink socket was created and the sender of the
1408 * message has has the capability @cap in all user namespaces.
1409 */
1410bool netlink_capable(const struct sk_buff *skb, int cap)
1411{
1412 return netlink_ns_capable(skb, &init_user_ns, cap);
1413}
1414EXPORT_SYMBOL(netlink_capable);
1415
1416/**
1417 * netlink_net_capable - Netlink network namespace message capability test
1418 * @skb: socket buffer holding a netlink command from userspace
1419 * @cap: The capability to use
1420 *
1421 * Test to see if the opener of the socket we received the message
1422 * from had when the netlink socket was created and the sender of the
1423 * message has has the capability @cap over the network namespace of
1424 * the socket we received the message from.
1425 */
1426bool netlink_net_capable(const struct sk_buff *skb, int cap)
1427{
1428 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
1429}
1430EXPORT_SYMBOL(netlink_net_capable);
1431
5187cd05 1432static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
746fac4d 1433{
9785e10a 1434 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
df008c91 1435 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
746fac4d 1436}
1da177e4 1437
f7fa9b10
PM
1438static void
1439netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1440{
1441 struct netlink_sock *nlk = nlk_sk(sk);
1442
1443 if (nlk->subscriptions && !subscriptions)
1444 __sk_del_bind_node(sk);
1445 else if (!nlk->subscriptions && subscriptions)
1446 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1447 nlk->subscriptions = subscriptions;
1448}
1449
b4ff4f04 1450static int netlink_realloc_groups(struct sock *sk)
513c2500
PM
1451{
1452 struct netlink_sock *nlk = nlk_sk(sk);
1453 unsigned int groups;
b4ff4f04 1454 unsigned long *new_groups;
513c2500
PM
1455 int err = 0;
1456
b4ff4f04
JB
1457 netlink_table_grab();
1458
513c2500 1459 groups = nl_table[sk->sk_protocol].groups;
b4ff4f04 1460 if (!nl_table[sk->sk_protocol].registered) {
513c2500 1461 err = -ENOENT;
b4ff4f04
JB
1462 goto out_unlock;
1463 }
513c2500 1464
b4ff4f04
JB
1465 if (nlk->ngroups >= groups)
1466 goto out_unlock;
513c2500 1467
b4ff4f04
JB
1468 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1469 if (new_groups == NULL) {
1470 err = -ENOMEM;
1471 goto out_unlock;
1472 }
6ac552fd 1473 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
b4ff4f04
JB
1474 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1475
1476 nlk->groups = new_groups;
513c2500 1477 nlk->ngroups = groups;
b4ff4f04
JB
1478 out_unlock:
1479 netlink_table_ungrab();
1480 return err;
513c2500
PM
1481}
1482
4f520900
RGB
1483static void netlink_unbind(int group, long unsigned int groups,
1484 struct netlink_sock *nlk)
1485{
1486 int undo;
1487
1488 if (!nlk->netlink_unbind)
1489 return;
1490
1491 for (undo = 0; undo < group; undo++)
1492 if (test_bit(group, &groups))
1493 nlk->netlink_unbind(undo);
1494}
1495
6ac552fd
PM
1496static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1497 int addr_len)
1da177e4
LT
1498{
1499 struct sock *sk = sock->sk;
3b1e0a65 1500 struct net *net = sock_net(sk);
1da177e4
LT
1501 struct netlink_sock *nlk = nlk_sk(sk);
1502 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1503 int err;
4f520900 1504 long unsigned int groups = nladdr->nl_groups;
746fac4d 1505
4e4b5376
HFS
1506 if (addr_len < sizeof(struct sockaddr_nl))
1507 return -EINVAL;
1508
1da177e4
LT
1509 if (nladdr->nl_family != AF_NETLINK)
1510 return -EINVAL;
1511
1512 /* Only superuser is allowed to listen multicasts */
4f520900 1513 if (groups) {
5187cd05 1514 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
513c2500 1515 return -EPERM;
b4ff4f04
JB
1516 err = netlink_realloc_groups(sk);
1517 if (err)
1518 return err;
513c2500 1519 }
1da177e4 1520
4f520900 1521 if (nlk->portid)
15e47304 1522 if (nladdr->nl_pid != nlk->portid)
1da177e4 1523 return -EINVAL;
4f520900
RGB
1524
1525 if (nlk->netlink_bind && groups) {
1526 int group;
1527
1528 for (group = 0; group < nlk->ngroups; group++) {
1529 if (!test_bit(group, &groups))
1530 continue;
1531 err = nlk->netlink_bind(group);
1532 if (!err)
1533 continue;
1534 netlink_unbind(group, groups, nlk);
1535 return err;
1536 }
1537 }
1538
1539 if (!nlk->portid) {
1da177e4 1540 err = nladdr->nl_pid ?
b4b51029 1541 netlink_insert(sk, net, nladdr->nl_pid) :
1da177e4 1542 netlink_autobind(sock);
4f520900
RGB
1543 if (err) {
1544 netlink_unbind(nlk->ngroups - 1, groups, nlk);
1da177e4 1545 return err;
4f520900 1546 }
1da177e4
LT
1547 }
1548
4f520900 1549 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
1da177e4
LT
1550 return 0;
1551
1552 netlink_table_grab();
f7fa9b10 1553 netlink_update_subscriptions(sk, nlk->subscriptions +
4f520900 1554 hweight32(groups) -
746fac4d 1555 hweight32(nlk->groups[0]));
4f520900 1556 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
4277a083 1557 netlink_update_listeners(sk);
1da177e4
LT
1558 netlink_table_ungrab();
1559
1560 return 0;
1561}
1562
1563static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1564 int alen, int flags)
1565{
1566 int err = 0;
1567 struct sock *sk = sock->sk;
1568 struct netlink_sock *nlk = nlk_sk(sk);
6ac552fd 1569 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1da177e4 1570
6503d961
CG
1571 if (alen < sizeof(addr->sa_family))
1572 return -EINVAL;
1573
1da177e4
LT
1574 if (addr->sa_family == AF_UNSPEC) {
1575 sk->sk_state = NETLINK_UNCONNECTED;
15e47304 1576 nlk->dst_portid = 0;
d629b836 1577 nlk->dst_group = 0;
1da177e4
LT
1578 return 0;
1579 }
1580 if (addr->sa_family != AF_NETLINK)
1581 return -EINVAL;
1582
46833a86 1583 if ((nladdr->nl_groups || nladdr->nl_pid) &&
5187cd05 1584 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
1da177e4
LT
1585 return -EPERM;
1586
15e47304 1587 if (!nlk->portid)
1da177e4
LT
1588 err = netlink_autobind(sock);
1589
1590 if (err == 0) {
1591 sk->sk_state = NETLINK_CONNECTED;
15e47304 1592 nlk->dst_portid = nladdr->nl_pid;
d629b836 1593 nlk->dst_group = ffs(nladdr->nl_groups);
1da177e4
LT
1594 }
1595
1596 return err;
1597}
1598
6ac552fd
PM
1599static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1600 int *addr_len, int peer)
1da177e4
LT
1601{
1602 struct sock *sk = sock->sk;
1603 struct netlink_sock *nlk = nlk_sk(sk);
13cfa97b 1604 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
746fac4d 1605
1da177e4
LT
1606 nladdr->nl_family = AF_NETLINK;
1607 nladdr->nl_pad = 0;
1608 *addr_len = sizeof(*nladdr);
1609
1610 if (peer) {
15e47304 1611 nladdr->nl_pid = nlk->dst_portid;
d629b836 1612 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
1da177e4 1613 } else {
15e47304 1614 nladdr->nl_pid = nlk->portid;
513c2500 1615 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
1da177e4
LT
1616 }
1617 return 0;
1618}
1619
15e47304 1620static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
1da177e4 1621{
1da177e4
LT
1622 struct sock *sock;
1623 struct netlink_sock *nlk;
1624
15e47304 1625 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
1da177e4
LT
1626 if (!sock)
1627 return ERR_PTR(-ECONNREFUSED);
1628
1629 /* Don't bother queuing skb if kernel socket has no input function */
1630 nlk = nlk_sk(sock);
cd40b7d3 1631 if (sock->sk_state == NETLINK_CONNECTED &&
15e47304 1632 nlk->dst_portid != nlk_sk(ssk)->portid) {
1da177e4
LT
1633 sock_put(sock);
1634 return ERR_PTR(-ECONNREFUSED);
1635 }
1636 return sock;
1637}
1638
1639struct sock *netlink_getsockbyfilp(struct file *filp)
1640{
496ad9aa 1641 struct inode *inode = file_inode(filp);
1da177e4
LT
1642 struct sock *sock;
1643
1644 if (!S_ISSOCK(inode->i_mode))
1645 return ERR_PTR(-ENOTSOCK);
1646
1647 sock = SOCKET_I(inode)->sk;
1648 if (sock->sk_family != AF_NETLINK)
1649 return ERR_PTR(-EINVAL);
1650
1651 sock_hold(sock);
1652 return sock;
1653}
1654
3a36515f
PN
1655static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1656 int broadcast)
c05cdb1b
PNA
1657{
1658 struct sk_buff *skb;
1659 void *data;
1660
3a36515f 1661 if (size <= NLMSG_GOODSIZE || broadcast)
c05cdb1b
PNA
1662 return alloc_skb(size, GFP_KERNEL);
1663
3a36515f
PN
1664 size = SKB_DATA_ALIGN(size) +
1665 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
c05cdb1b
PNA
1666
1667 data = vmalloc(size);
1668 if (data == NULL)
3a36515f 1669 return NULL;
c05cdb1b 1670
3a36515f
PN
1671 skb = build_skb(data, size);
1672 if (skb == NULL)
1673 vfree(data);
1674 else {
1675 skb->head_frag = 0;
1676 skb->destructor = netlink_skb_destructor;
1677 }
c05cdb1b
PNA
1678
1679 return skb;
c05cdb1b
PNA
1680}
1681
1da177e4
LT
1682/*
1683 * Attach a skb to a netlink socket.
1684 * The caller must hold a reference to the destination socket. On error, the
1685 * reference is dropped. The skb is not send to the destination, just all
1686 * all error checks are performed and memory in the queue is reserved.
1687 * Return values:
1688 * < 0: error. skb freed, reference to sock dropped.
1689 * 0: continue
1690 * 1: repeat lookup - reference dropped while waiting for socket memory.
1691 */
9457afee 1692int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
c3d8d1e3 1693 long *timeo, struct sock *ssk)
1da177e4
LT
1694{
1695 struct netlink_sock *nlk;
1696
1697 nlk = nlk_sk(sk);
1698
5fd96123
PM
1699 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1700 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1701 !netlink_skb_is_mmaped(skb)) {
1da177e4 1702 DECLARE_WAITQUEUE(wait, current);
c3d8d1e3 1703 if (!*timeo) {
aed81560 1704 if (!ssk || netlink_is_kernel(ssk))
1da177e4
LT
1705 netlink_overrun(sk);
1706 sock_put(sk);
1707 kfree_skb(skb);
1708 return -EAGAIN;
1709 }
1710
1711 __set_current_state(TASK_INTERRUPTIBLE);
1712 add_wait_queue(&nlk->wait, &wait);
1713
1714 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
cd967e05 1715 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1da177e4 1716 !sock_flag(sk, SOCK_DEAD))
c3d8d1e3 1717 *timeo = schedule_timeout(*timeo);
1da177e4
LT
1718
1719 __set_current_state(TASK_RUNNING);
1720 remove_wait_queue(&nlk->wait, &wait);
1721 sock_put(sk);
1722
1723 if (signal_pending(current)) {
1724 kfree_skb(skb);
c3d8d1e3 1725 return sock_intr_errno(*timeo);
1da177e4
LT
1726 }
1727 return 1;
1728 }
cf0a018a 1729 netlink_skb_set_owner_r(skb, sk);
1da177e4
LT
1730 return 0;
1731}
1732
4a7e7c2a 1733static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1da177e4 1734{
1da177e4
LT
1735 int len = skb->len;
1736
bcbde0d4
DB
1737 netlink_deliver_tap(skb);
1738
f9c22888
PM
1739#ifdef CONFIG_NETLINK_MMAP
1740 if (netlink_skb_is_mmaped(skb))
1741 netlink_queue_mmaped_skb(sk, skb);
1742 else if (netlink_rx_is_mmaped(sk))
1743 netlink_ring_set_copied(sk, skb);
1744 else
1745#endif /* CONFIG_NETLINK_MMAP */
1746 skb_queue_tail(&sk->sk_receive_queue, skb);
676d2369 1747 sk->sk_data_ready(sk);
4a7e7c2a
ED
1748 return len;
1749}
1750
1751int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1752{
1753 int len = __netlink_sendskb(sk, skb);
1754
1da177e4
LT
1755 sock_put(sk);
1756 return len;
1757}
1758
1759void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1760{
1761 kfree_skb(skb);
1762 sock_put(sk);
1763}
1764
b57ef81f 1765static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
1da177e4
LT
1766{
1767 int delta;
1768
1298ca46 1769 WARN_ON(skb->sk != NULL);
5fd96123
PM
1770 if (netlink_skb_is_mmaped(skb))
1771 return skb;
1da177e4 1772
4305b541 1773 delta = skb->end - skb->tail;
c05cdb1b 1774 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
1da177e4
LT
1775 return skb;
1776
1777 if (skb_shared(skb)) {
1778 struct sk_buff *nskb = skb_clone(skb, allocation);
1779 if (!nskb)
1780 return skb;
8460c00f 1781 consume_skb(skb);
1da177e4
LT
1782 skb = nskb;
1783 }
1784
1785 if (!pskb_expand_head(skb, 0, -delta, allocation))
1786 skb->truesize -= delta;
1787
1788 return skb;
1789}
1790
3fbc2905
EB
1791static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1792 struct sock *ssk)
cd40b7d3
DL
1793{
1794 int ret;
1795 struct netlink_sock *nlk = nlk_sk(sk);
1796
1797 ret = -ECONNREFUSED;
1798 if (nlk->netlink_rcv != NULL) {
1799 ret = skb->len;
cf0a018a 1800 netlink_skb_set_owner_r(skb, sk);
e32123e5 1801 NETLINK_CB(skb).sk = ssk;
73bfd370 1802 netlink_deliver_tap_kernel(sk, ssk, skb);
cd40b7d3 1803 nlk->netlink_rcv(skb);
bfb253c9
ED
1804 consume_skb(skb);
1805 } else {
1806 kfree_skb(skb);
cd40b7d3 1807 }
cd40b7d3
DL
1808 sock_put(sk);
1809 return ret;
1810}
1811
1812int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
15e47304 1813 u32 portid, int nonblock)
1da177e4
LT
1814{
1815 struct sock *sk;
1816 int err;
1817 long timeo;
1818
1819 skb = netlink_trim(skb, gfp_any());
1820
1821 timeo = sock_sndtimeo(ssk, nonblock);
1822retry:
15e47304 1823 sk = netlink_getsockbyportid(ssk, portid);
1da177e4
LT
1824 if (IS_ERR(sk)) {
1825 kfree_skb(skb);
1826 return PTR_ERR(sk);
1827 }
cd40b7d3 1828 if (netlink_is_kernel(sk))
3fbc2905 1829 return netlink_unicast_kernel(sk, skb, ssk);
cd40b7d3 1830
b1153f29 1831 if (sk_filter(sk, skb)) {
84874607 1832 err = skb->len;
b1153f29
SH
1833 kfree_skb(skb);
1834 sock_put(sk);
1835 return err;
1836 }
1837
9457afee 1838 err = netlink_attachskb(sk, skb, &timeo, ssk);
1da177e4
LT
1839 if (err == 1)
1840 goto retry;
1841 if (err)
1842 return err;
1843
7ee015e0 1844 return netlink_sendskb(sk, skb);
1da177e4 1845}
6ac552fd 1846EXPORT_SYMBOL(netlink_unicast);
1da177e4 1847
f9c22888
PM
1848struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1849 u32 dst_portid, gfp_t gfp_mask)
1850{
1851#ifdef CONFIG_NETLINK_MMAP
1852 struct sock *sk = NULL;
1853 struct sk_buff *skb;
1854 struct netlink_ring *ring;
1855 struct nl_mmap_hdr *hdr;
1856 unsigned int maxlen;
1857
1858 sk = netlink_getsockbyportid(ssk, dst_portid);
1859 if (IS_ERR(sk))
1860 goto out;
1861
1862 ring = &nlk_sk(sk)->rx_ring;
1863 /* fast-path without atomic ops for common case: non-mmaped receiver */
1864 if (ring->pg_vec == NULL)
1865 goto out_put;
1866
aae9f0e2
TG
1867 if (ring->frame_size - NL_MMAP_HDRLEN < size)
1868 goto out_put;
1869
f9c22888
PM
1870 skb = alloc_skb_head(gfp_mask);
1871 if (skb == NULL)
1872 goto err1;
1873
1874 spin_lock_bh(&sk->sk_receive_queue.lock);
1875 /* check again under lock */
1876 if (ring->pg_vec == NULL)
1877 goto out_free;
1878
aae9f0e2 1879 /* check again under lock */
f9c22888
PM
1880 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1881 if (maxlen < size)
1882 goto out_free;
1883
1884 netlink_forward_ring(ring);
1885 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1886 if (hdr == NULL)
1887 goto err2;
1888 netlink_ring_setup_skb(skb, sk, ring, hdr);
1889 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1890 atomic_inc(&ring->pending);
1891 netlink_increment_head(ring);
1892
1893 spin_unlock_bh(&sk->sk_receive_queue.lock);
1894 return skb;
1895
1896err2:
1897 kfree_skb(skb);
1898 spin_unlock_bh(&sk->sk_receive_queue.lock);
cd1df525 1899 netlink_overrun(sk);
f9c22888
PM
1900err1:
1901 sock_put(sk);
1902 return NULL;
1903
1904out_free:
1905 kfree_skb(skb);
1906 spin_unlock_bh(&sk->sk_receive_queue.lock);
1907out_put:
1908 sock_put(sk);
1909out:
1910#endif
1911 return alloc_skb(size, gfp_mask);
1912}
1913EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1914
4277a083
PM
1915int netlink_has_listeners(struct sock *sk, unsigned int group)
1916{
1917 int res = 0;
5c398dc8 1918 struct listeners *listeners;
4277a083 1919
aed81560 1920 BUG_ON(!netlink_is_kernel(sk));
b4ff4f04
JB
1921
1922 rcu_read_lock();
1923 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1924
6d772ac5 1925 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
5c398dc8 1926 res = test_bit(group - 1, listeners->masks);
b4ff4f04
JB
1927
1928 rcu_read_unlock();
1929
4277a083
PM
1930 return res;
1931}
1932EXPORT_SYMBOL_GPL(netlink_has_listeners);
1933
b57ef81f 1934static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
1da177e4
LT
1935{
1936 struct netlink_sock *nlk = nlk_sk(sk);
1937
1938 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
cd967e05 1939 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
cf0a018a 1940 netlink_skb_set_owner_r(skb, sk);
4a7e7c2a 1941 __netlink_sendskb(sk, skb);
2c645800 1942 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
1da177e4
LT
1943 }
1944 return -1;
1945}
1946
1947struct netlink_broadcast_data {
1948 struct sock *exclude_sk;
b4b51029 1949 struct net *net;
15e47304 1950 u32 portid;
1da177e4
LT
1951 u32 group;
1952 int failure;
ff491a73 1953 int delivery_failure;
1da177e4
LT
1954 int congested;
1955 int delivered;
7d877f3b 1956 gfp_t allocation;
1da177e4 1957 struct sk_buff *skb, *skb2;
910a7e90
EB
1958 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1959 void *tx_data;
1da177e4
LT
1960};
1961
46c9521f
RR
1962static void do_one_broadcast(struct sock *sk,
1963 struct netlink_broadcast_data *p)
1da177e4
LT
1964{
1965 struct netlink_sock *nlk = nlk_sk(sk);
1966 int val;
1967
1968 if (p->exclude_sk == sk)
46c9521f 1969 return;
1da177e4 1970
15e47304 1971 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 1972 !test_bit(p->group - 1, nlk->groups))
46c9521f 1973 return;
1da177e4 1974
878628fb 1975 if (!net_eq(sock_net(sk), p->net))
46c9521f 1976 return;
b4b51029 1977
1da177e4
LT
1978 if (p->failure) {
1979 netlink_overrun(sk);
46c9521f 1980 return;
1da177e4
LT
1981 }
1982
1983 sock_hold(sk);
1984 if (p->skb2 == NULL) {
68acc024 1985 if (skb_shared(p->skb)) {
1da177e4
LT
1986 p->skb2 = skb_clone(p->skb, p->allocation);
1987 } else {
68acc024
TC
1988 p->skb2 = skb_get(p->skb);
1989 /*
1990 * skb ownership may have been set when
1991 * delivered to a previous socket.
1992 */
1993 skb_orphan(p->skb2);
1da177e4
LT
1994 }
1995 }
1996 if (p->skb2 == NULL) {
1997 netlink_overrun(sk);
1998 /* Clone failed. Notify ALL listeners. */
1999 p->failure = 1;
be0c22a4
PNA
2000 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
2001 p->delivery_failure = 1;
910a7e90
EB
2002 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
2003 kfree_skb(p->skb2);
2004 p->skb2 = NULL;
b1153f29
SH
2005 } else if (sk_filter(sk, p->skb2)) {
2006 kfree_skb(p->skb2);
2007 p->skb2 = NULL;
1da177e4
LT
2008 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
2009 netlink_overrun(sk);
be0c22a4
PNA
2010 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
2011 p->delivery_failure = 1;
1da177e4
LT
2012 } else {
2013 p->congested |= val;
2014 p->delivered = 1;
2015 p->skb2 = NULL;
2016 }
2017 sock_put(sk);
1da177e4
LT
2018}
2019
15e47304 2020int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
2021 u32 group, gfp_t allocation,
2022 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
2023 void *filter_data)
1da177e4 2024{
3b1e0a65 2025 struct net *net = sock_net(ssk);
1da177e4 2026 struct netlink_broadcast_data info;
1da177e4
LT
2027 struct sock *sk;
2028
2029 skb = netlink_trim(skb, allocation);
2030
2031 info.exclude_sk = ssk;
b4b51029 2032 info.net = net;
15e47304 2033 info.portid = portid;
1da177e4
LT
2034 info.group = group;
2035 info.failure = 0;
ff491a73 2036 info.delivery_failure = 0;
1da177e4
LT
2037 info.congested = 0;
2038 info.delivered = 0;
2039 info.allocation = allocation;
2040 info.skb = skb;
2041 info.skb2 = NULL;
910a7e90
EB
2042 info.tx_filter = filter;
2043 info.tx_data = filter_data;
1da177e4
LT
2044
2045 /* While we sleep in clone, do not allow to change socket list */
2046
2047 netlink_lock_table();
2048
b67bfe0d 2049 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1da177e4
LT
2050 do_one_broadcast(sk, &info);
2051
70d4bf6d 2052 consume_skb(skb);
aa1c6a6f 2053
1da177e4
LT
2054 netlink_unlock_table();
2055
70d4bf6d
NH
2056 if (info.delivery_failure) {
2057 kfree_skb(info.skb2);
ff491a73 2058 return -ENOBUFS;
658cb354
ED
2059 }
2060 consume_skb(info.skb2);
ff491a73 2061
1da177e4
LT
2062 if (info.delivered) {
2063 if (info.congested && (allocation & __GFP_WAIT))
2064 yield();
2065 return 0;
2066 }
1da177e4
LT
2067 return -ESRCH;
2068}
910a7e90
EB
2069EXPORT_SYMBOL(netlink_broadcast_filtered);
2070
15e47304 2071int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
910a7e90
EB
2072 u32 group, gfp_t allocation)
2073{
15e47304 2074 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
910a7e90
EB
2075 NULL, NULL);
2076}
6ac552fd 2077EXPORT_SYMBOL(netlink_broadcast);
1da177e4
LT
2078
2079struct netlink_set_err_data {
2080 struct sock *exclude_sk;
15e47304 2081 u32 portid;
1da177e4
LT
2082 u32 group;
2083 int code;
2084};
2085
b57ef81f 2086static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
1da177e4
LT
2087{
2088 struct netlink_sock *nlk = nlk_sk(sk);
1a50307b 2089 int ret = 0;
1da177e4
LT
2090
2091 if (sk == p->exclude_sk)
2092 goto out;
2093
09ad9bc7 2094 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
b4b51029
EB
2095 goto out;
2096
15e47304 2097 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
f7fa9b10 2098 !test_bit(p->group - 1, nlk->groups))
1da177e4
LT
2099 goto out;
2100
1a50307b
PNA
2101 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
2102 ret = 1;
2103 goto out;
2104 }
2105
1da177e4
LT
2106 sk->sk_err = p->code;
2107 sk->sk_error_report(sk);
2108out:
1a50307b 2109 return ret;
1da177e4
LT
2110}
2111
4843b93c
PNA
2112/**
2113 * netlink_set_err - report error to broadcast listeners
2114 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
15e47304 2115 * @portid: the PORTID of a process that we want to skip (if any)
840e93f2 2116 * @group: the broadcast group that will notice the error
4843b93c 2117 * @code: error code, must be negative (as usual in kernelspace)
1a50307b
PNA
2118 *
2119 * This function returns the number of broadcast listeners that have set the
2120 * NETLINK_RECV_NO_ENOBUFS socket option.
4843b93c 2121 */
15e47304 2122int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
1da177e4
LT
2123{
2124 struct netlink_set_err_data info;
1da177e4 2125 struct sock *sk;
1a50307b 2126 int ret = 0;
1da177e4
LT
2127
2128 info.exclude_sk = ssk;
15e47304 2129 info.portid = portid;
1da177e4 2130 info.group = group;
4843b93c
PNA
2131 /* sk->sk_err wants a positive error value */
2132 info.code = -code;
1da177e4
LT
2133
2134 read_lock(&nl_table_lock);
2135
b67bfe0d 2136 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
1a50307b 2137 ret += do_one_set_err(sk, &info);
1da177e4
LT
2138
2139 read_unlock(&nl_table_lock);
1a50307b 2140 return ret;
1da177e4 2141}
dd5b6ce6 2142EXPORT_SYMBOL(netlink_set_err);
1da177e4 2143
84659eb5
JB
2144/* must be called with netlink table grabbed */
2145static void netlink_update_socket_mc(struct netlink_sock *nlk,
2146 unsigned int group,
2147 int is_new)
2148{
2149 int old, new = !!is_new, subscriptions;
2150
2151 old = test_bit(group - 1, nlk->groups);
2152 subscriptions = nlk->subscriptions - old + new;
2153 if (new)
2154 __set_bit(group - 1, nlk->groups);
2155 else
2156 __clear_bit(group - 1, nlk->groups);
2157 netlink_update_subscriptions(&nlk->sk, subscriptions);
2158 netlink_update_listeners(&nlk->sk);
2159}
2160
9a4595bc 2161static int netlink_setsockopt(struct socket *sock, int level, int optname,
b7058842 2162 char __user *optval, unsigned int optlen)
9a4595bc
PM
2163{
2164 struct sock *sk = sock->sk;
2165 struct netlink_sock *nlk = nlk_sk(sk);
eb496534
JB
2166 unsigned int val = 0;
2167 int err;
9a4595bc
PM
2168
2169 if (level != SOL_NETLINK)
2170 return -ENOPROTOOPT;
2171
ccdfcc39
PM
2172 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
2173 optlen >= sizeof(int) &&
eb496534 2174 get_user(val, (unsigned int __user *)optval))
9a4595bc
PM
2175 return -EFAULT;
2176
2177 switch (optname) {
2178 case NETLINK_PKTINFO:
2179 if (val)
2180 nlk->flags |= NETLINK_RECV_PKTINFO;
2181 else
2182 nlk->flags &= ~NETLINK_RECV_PKTINFO;
2183 err = 0;
2184 break;
2185 case NETLINK_ADD_MEMBERSHIP:
2186 case NETLINK_DROP_MEMBERSHIP: {
5187cd05 2187 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
9a4595bc 2188 return -EPERM;
b4ff4f04
JB
2189 err = netlink_realloc_groups(sk);
2190 if (err)
2191 return err;
9a4595bc
PM
2192 if (!val || val - 1 >= nlk->ngroups)
2193 return -EINVAL;
7774d5e0 2194 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
4f520900
RGB
2195 err = nlk->netlink_bind(val);
2196 if (err)
2197 return err;
2198 }
9a4595bc 2199 netlink_table_grab();
84659eb5
JB
2200 netlink_update_socket_mc(nlk, val,
2201 optname == NETLINK_ADD_MEMBERSHIP);
9a4595bc 2202 netlink_table_ungrab();
7774d5e0
RGB
2203 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
2204 nlk->netlink_unbind(val);
03292745 2205
9a4595bc
PM
2206 err = 0;
2207 break;
2208 }
be0c22a4
PNA
2209 case NETLINK_BROADCAST_ERROR:
2210 if (val)
2211 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
2212 else
2213 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
2214 err = 0;
2215 break;
38938bfe
PNA
2216 case NETLINK_NO_ENOBUFS:
2217 if (val) {
2218 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
cd967e05 2219 clear_bit(NETLINK_CONGESTED, &nlk->state);
38938bfe 2220 wake_up_interruptible(&nlk->wait);
658cb354 2221 } else {
38938bfe 2222 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
658cb354 2223 }
38938bfe
PNA
2224 err = 0;
2225 break;
ccdfcc39
PM
2226#ifdef CONFIG_NETLINK_MMAP
2227 case NETLINK_RX_RING:
2228 case NETLINK_TX_RING: {
2229 struct nl_mmap_req req;
2230
2231 /* Rings might consume more memory than queue limits, require
2232 * CAP_NET_ADMIN.
2233 */
2234 if (!capable(CAP_NET_ADMIN))
2235 return -EPERM;
2236 if (optlen < sizeof(req))
2237 return -EINVAL;
2238 if (copy_from_user(&req, optval, sizeof(req)))
2239 return -EFAULT;
2240 err = netlink_set_ring(sk, &req, false,
2241 optname == NETLINK_TX_RING);
2242 break;
2243 }
2244#endif /* CONFIG_NETLINK_MMAP */
9a4595bc
PM
2245 default:
2246 err = -ENOPROTOOPT;
2247 }
2248 return err;
2249}
2250
2251static int netlink_getsockopt(struct socket *sock, int level, int optname,
746fac4d 2252 char __user *optval, int __user *optlen)
9a4595bc
PM
2253{
2254 struct sock *sk = sock->sk;
2255 struct netlink_sock *nlk = nlk_sk(sk);
2256 int len, val, err;
2257
2258 if (level != SOL_NETLINK)
2259 return -ENOPROTOOPT;
2260
2261 if (get_user(len, optlen))
2262 return -EFAULT;
2263 if (len < 0)
2264 return -EINVAL;
2265
2266 switch (optname) {
2267 case NETLINK_PKTINFO:
2268 if (len < sizeof(int))
2269 return -EINVAL;
2270 len = sizeof(int);
2271 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
a27b58fe
HC
2272 if (put_user(len, optlen) ||
2273 put_user(val, optval))
2274 return -EFAULT;
9a4595bc
PM
2275 err = 0;
2276 break;
be0c22a4
PNA
2277 case NETLINK_BROADCAST_ERROR:
2278 if (len < sizeof(int))
2279 return -EINVAL;
2280 len = sizeof(int);
2281 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
2282 if (put_user(len, optlen) ||
2283 put_user(val, optval))
2284 return -EFAULT;
2285 err = 0;
2286 break;
38938bfe
PNA
2287 case NETLINK_NO_ENOBUFS:
2288 if (len < sizeof(int))
2289 return -EINVAL;
2290 len = sizeof(int);
2291 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
2292 if (put_user(len, optlen) ||
2293 put_user(val, optval))
2294 return -EFAULT;
2295 err = 0;
2296 break;
9a4595bc
PM
2297 default:
2298 err = -ENOPROTOOPT;
2299 }
2300 return err;
2301}
2302
2303static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2304{
2305 struct nl_pktinfo info;
2306
2307 info.group = NETLINK_CB(skb).dst_group;
2308 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2309}
2310
1da177e4
LT
2311static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2312 struct msghdr *msg, size_t len)
2313{
2314 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2315 struct sock *sk = sock->sk;
2316 struct netlink_sock *nlk = nlk_sk(sk);
342dfc30 2317 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
15e47304 2318 u32 dst_portid;
d629b836 2319 u32 dst_group;
1da177e4
LT
2320 struct sk_buff *skb;
2321 int err;
2322 struct scm_cookie scm;
2d7a85f4 2323 u32 netlink_skb_flags = 0;
1da177e4
LT
2324
2325 if (msg->msg_flags&MSG_OOB)
2326 return -EOPNOTSUPP;
2327
16e57262 2328 if (NULL == siocb->scm)
1da177e4 2329 siocb->scm = &scm;
16e57262 2330
e0e3cea4 2331 err = scm_send(sock, msg, siocb->scm, true);
1da177e4
LT
2332 if (err < 0)
2333 return err;
2334
2335 if (msg->msg_namelen) {
b47030c7 2336 err = -EINVAL;
1da177e4 2337 if (addr->nl_family != AF_NETLINK)
b47030c7 2338 goto out;
15e47304 2339 dst_portid = addr->nl_pid;
d629b836 2340 dst_group = ffs(addr->nl_groups);
b47030c7 2341 err = -EPERM;
15e47304 2342 if ((dst_group || dst_portid) &&
5187cd05 2343 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
b47030c7 2344 goto out;
2d7a85f4 2345 netlink_skb_flags |= NETLINK_SKB_DST;
1da177e4 2346 } else {
15e47304 2347 dst_portid = nlk->dst_portid;
d629b836 2348 dst_group = nlk->dst_group;
1da177e4
LT
2349 }
2350
15e47304 2351 if (!nlk->portid) {
1da177e4
LT
2352 err = netlink_autobind(sock);
2353 if (err)
2354 goto out;
2355 }
2356
5fd96123
PM
2357 if (netlink_tx_is_mmaped(sk) &&
2358 msg->msg_iov->iov_base == NULL) {
2359 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2360 siocb);
2361 goto out;
2362 }
2363
1da177e4
LT
2364 err = -EMSGSIZE;
2365 if (len > sk->sk_sndbuf - 32)
2366 goto out;
2367 err = -ENOBUFS;
3a36515f 2368 skb = netlink_alloc_large_skb(len, dst_group);
6ac552fd 2369 if (skb == NULL)
1da177e4
LT
2370 goto out;
2371
15e47304 2372 NETLINK_CB(skb).portid = nlk->portid;
d629b836 2373 NETLINK_CB(skb).dst_group = dst_group;
dbe9a417 2374 NETLINK_CB(skb).creds = siocb->scm->creds;
2d7a85f4 2375 NETLINK_CB(skb).flags = netlink_skb_flags;
1da177e4 2376
1da177e4 2377 err = -EFAULT;
6ac552fd 2378 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
1da177e4
LT
2379 kfree_skb(skb);
2380 goto out;
2381 }
2382
2383 err = security_netlink_send(sk, skb);
2384 if (err) {
2385 kfree_skb(skb);
2386 goto out;
2387 }
2388
d629b836 2389 if (dst_group) {
1da177e4 2390 atomic_inc(&skb->users);
15e47304 2391 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
1da177e4 2392 }
15e47304 2393 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
1da177e4
LT
2394
2395out:
b47030c7 2396 scm_destroy(siocb->scm);
1da177e4
LT
2397 return err;
2398}
2399
2400static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2401 struct msghdr *msg, size_t len,
2402 int flags)
2403{
2404 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2405 struct scm_cookie scm;
2406 struct sock *sk = sock->sk;
2407 struct netlink_sock *nlk = nlk_sk(sk);
2408 int noblock = flags&MSG_DONTWAIT;
2409 size_t copied;
68d6ac6d 2410 struct sk_buff *skb, *data_skb;
b44d211e 2411 int err, ret;
1da177e4
LT
2412
2413 if (flags&MSG_OOB)
2414 return -EOPNOTSUPP;
2415
2416 copied = 0;
2417
6ac552fd
PM
2418 skb = skb_recv_datagram(sk, flags, noblock, &err);
2419 if (skb == NULL)
1da177e4
LT
2420 goto out;
2421
68d6ac6d
JB
2422 data_skb = skb;
2423
1dacc76d
JB
2424#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2425 if (unlikely(skb_shinfo(skb)->frag_list)) {
1dacc76d 2426 /*
68d6ac6d
JB
2427 * If this skb has a frag_list, then here that means that we
2428 * will have to use the frag_list skb's data for compat tasks
2429 * and the regular skb's data for normal (non-compat) tasks.
1dacc76d 2430 *
68d6ac6d
JB
2431 * If we need to send the compat skb, assign it to the
2432 * 'data_skb' variable so that it will be used below for data
2433 * copying. We keep 'skb' for everything else, including
2434 * freeing both later.
1dacc76d 2435 */
68d6ac6d
JB
2436 if (flags & MSG_CMSG_COMPAT)
2437 data_skb = skb_shinfo(skb)->frag_list;
1dacc76d
JB
2438 }
2439#endif
2440
9063e21f
ED
2441 /* Record the max length of recvmsg() calls for future allocations */
2442 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
2443 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
2444 16384);
2445
68d6ac6d 2446 copied = data_skb->len;
1da177e4
LT
2447 if (len < copied) {
2448 msg->msg_flags |= MSG_TRUNC;
2449 copied = len;
2450 }
2451
68d6ac6d
JB
2452 skb_reset_transport_header(data_skb);
2453 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
1da177e4
LT
2454
2455 if (msg->msg_name) {
342dfc30 2456 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
1da177e4
LT
2457 addr->nl_family = AF_NETLINK;
2458 addr->nl_pad = 0;
15e47304 2459 addr->nl_pid = NETLINK_CB(skb).portid;
d629b836 2460 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
1da177e4
LT
2461 msg->msg_namelen = sizeof(*addr);
2462 }
2463
cc9a06cd
PM
2464 if (nlk->flags & NETLINK_RECV_PKTINFO)
2465 netlink_cmsg_recv_pktinfo(msg, skb);
2466
1da177e4
LT
2467 if (NULL == siocb->scm) {
2468 memset(&scm, 0, sizeof(scm));
2469 siocb->scm = &scm;
2470 }
2471 siocb->scm->creds = *NETLINK_CREDS(skb);
188ccb55 2472 if (flags & MSG_TRUNC)
68d6ac6d 2473 copied = data_skb->len;
daa3766e 2474
1da177e4
LT
2475 skb_free_datagram(sk, skb);
2476
16b304f3
PS
2477 if (nlk->cb_running &&
2478 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
b44d211e
AV
2479 ret = netlink_dump(sk);
2480 if (ret) {
ac30ef83 2481 sk->sk_err = -ret;
b44d211e
AV
2482 sk->sk_error_report(sk);
2483 }
2484 }
1da177e4
LT
2485
2486 scm_recv(sock, msg, siocb->scm, flags);
1da177e4
LT
2487out:
2488 netlink_rcv_wake(sk);
2489 return err ? : copied;
2490}
2491
676d2369 2492static void netlink_data_ready(struct sock *sk)
1da177e4 2493{
cd40b7d3 2494 BUG();
1da177e4
LT
2495}
2496
2497/*
746fac4d 2498 * We export these functions to other modules. They provide a
1da177e4
LT
2499 * complete set of kernel non-blocking support for message
2500 * queueing.
2501 */
2502
2503struct sock *
9f00d977
PNA
2504__netlink_kernel_create(struct net *net, int unit, struct module *module,
2505 struct netlink_kernel_cfg *cfg)
1da177e4
LT
2506{
2507 struct socket *sock;
2508 struct sock *sk;
77247bbb 2509 struct netlink_sock *nlk;
5c398dc8 2510 struct listeners *listeners = NULL;
a31f2d17
PNA
2511 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2512 unsigned int groups;
1da177e4 2513
fab2caf6 2514 BUG_ON(!nl_table);
1da177e4 2515
6ac552fd 2516 if (unit < 0 || unit >= MAX_LINKS)
1da177e4
LT
2517 return NULL;
2518
2519 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2520 return NULL;
2521
23fe1866
PE
2522 /*
2523 * We have to just have a reference on the net from sk, but don't
2524 * get_net it. Besides, we cannot get and then put the net here.
2525 * So we create one inside init_net and the move it to net.
2526 */
2527
2528 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2529 goto out_sock_release_nosk;
2530
2531 sk = sock->sk;
edf02087 2532 sk_change_net(sk, net);
4fdb3bb7 2533
a31f2d17 2534 if (!cfg || cfg->groups < 32)
4277a083 2535 groups = 32;
a31f2d17
PNA
2536 else
2537 groups = cfg->groups;
4277a083 2538
5c398dc8 2539 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
4277a083
PM
2540 if (!listeners)
2541 goto out_sock_release;
2542
1da177e4 2543 sk->sk_data_ready = netlink_data_ready;
a31f2d17
PNA
2544 if (cfg && cfg->input)
2545 nlk_sk(sk)->netlink_rcv = cfg->input;
1da177e4 2546
b4b51029 2547 if (netlink_insert(sk, net, 0))
77247bbb 2548 goto out_sock_release;
4fdb3bb7 2549
77247bbb
PM
2550 nlk = nlk_sk(sk);
2551 nlk->flags |= NETLINK_KERNEL_SOCKET;
4fdb3bb7 2552
4fdb3bb7 2553 netlink_table_grab();
b4b51029
EB
2554 if (!nl_table[unit].registered) {
2555 nl_table[unit].groups = groups;
5c398dc8 2556 rcu_assign_pointer(nl_table[unit].listeners, listeners);
b4b51029
EB
2557 nl_table[unit].cb_mutex = cb_mutex;
2558 nl_table[unit].module = module;
9785e10a
PNA
2559 if (cfg) {
2560 nl_table[unit].bind = cfg->bind;
2561 nl_table[unit].flags = cfg->flags;
da12c90e
G
2562 if (cfg->compare)
2563 nl_table[unit].compare = cfg->compare;
9785e10a 2564 }
b4b51029 2565 nl_table[unit].registered = 1;
f937f1f4
JJ
2566 } else {
2567 kfree(listeners);
869e58f8 2568 nl_table[unit].registered++;
b4b51029 2569 }
4fdb3bb7 2570 netlink_table_ungrab();
77247bbb
PM
2571 return sk;
2572
4fdb3bb7 2573out_sock_release:
4277a083 2574 kfree(listeners);
9dfbec1f 2575 netlink_kernel_release(sk);
23fe1866
PE
2576 return NULL;
2577
2578out_sock_release_nosk:
4fdb3bb7 2579 sock_release(sock);
77247bbb 2580 return NULL;
1da177e4 2581}
9f00d977 2582EXPORT_SYMBOL(__netlink_kernel_create);
b7c6ba6e
DL
2583
2584void
2585netlink_kernel_release(struct sock *sk)
2586{
edf02087 2587 sk_release_kernel(sk);
b7c6ba6e
DL
2588}
2589EXPORT_SYMBOL(netlink_kernel_release);
2590
d136f1bd 2591int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
b4ff4f04 2592{
5c398dc8 2593 struct listeners *new, *old;
b4ff4f04 2594 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
b4ff4f04
JB
2595
2596 if (groups < 32)
2597 groups = 32;
2598
b4ff4f04 2599 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
5c398dc8
ED
2600 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2601 if (!new)
d136f1bd 2602 return -ENOMEM;
6d772ac5 2603 old = nl_deref_protected(tbl->listeners);
5c398dc8
ED
2604 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2605 rcu_assign_pointer(tbl->listeners, new);
2606
37b6b935 2607 kfree_rcu(old, rcu);
b4ff4f04
JB
2608 }
2609 tbl->groups = groups;
2610
d136f1bd
JB
2611 return 0;
2612}
2613
2614/**
2615 * netlink_change_ngroups - change number of multicast groups
2616 *
2617 * This changes the number of multicast groups that are available
2618 * on a certain netlink family. Note that it is not possible to
2619 * change the number of groups to below 32. Also note that it does
2620 * not implicitly call netlink_clear_multicast_users() when the
2621 * number of groups is reduced.
2622 *
2623 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2624 * @groups: The new number of groups.
2625 */
2626int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2627{
2628 int err;
2629
2630 netlink_table_grab();
2631 err = __netlink_change_ngroups(sk, groups);
b4ff4f04 2632 netlink_table_ungrab();
d136f1bd 2633
b4ff4f04
JB
2634 return err;
2635}
b4ff4f04 2636
b8273570
JB
2637void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2638{
2639 struct sock *sk;
b8273570
JB
2640 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2641
b67bfe0d 2642 sk_for_each_bound(sk, &tbl->mc_list)
b8273570
JB
2643 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2644}
2645
a46621a3 2646struct nlmsghdr *
15e47304 2647__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
a46621a3
DV
2648{
2649 struct nlmsghdr *nlh;
573ce260 2650 int size = nlmsg_msg_size(len);
a46621a3 2651
23b45672 2652 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
a46621a3
DV
2653 nlh->nlmsg_type = type;
2654 nlh->nlmsg_len = size;
2655 nlh->nlmsg_flags = flags;
15e47304 2656 nlh->nlmsg_pid = portid;
a46621a3
DV
2657 nlh->nlmsg_seq = seq;
2658 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
573ce260 2659 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
a46621a3
DV
2660 return nlh;
2661}
2662EXPORT_SYMBOL(__nlmsg_put);
2663
1da177e4
LT
2664/*
2665 * It looks a bit ugly.
2666 * It would be better to create kernel thread.
2667 */
2668
2669static int netlink_dump(struct sock *sk)
2670{
2671 struct netlink_sock *nlk = nlk_sk(sk);
2672 struct netlink_callback *cb;
c7ac8679 2673 struct sk_buff *skb = NULL;
1da177e4 2674 struct nlmsghdr *nlh;
bf8b79e4 2675 int len, err = -ENOBUFS;
c7ac8679 2676 int alloc_size;
1da177e4 2677
af65bdfc 2678 mutex_lock(nlk->cb_mutex);
16b304f3 2679 if (!nlk->cb_running) {
bf8b79e4
TG
2680 err = -EINVAL;
2681 goto errout_skb;
1da177e4
LT
2682 }
2683
16b304f3 2684 cb = &nlk->cb;
c7ac8679
GR
2685 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2686
f9c22888
PM
2687 if (!netlink_rx_is_mmaped(sk) &&
2688 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2689 goto errout_skb;
9063e21f
ED
2690
2691 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2692 * required, but it makes sense to _attempt_ a 16K bytes allocation
2693 * to reduce number of system calls on dump operations, if user
2694 * ever provided a big enough buffer.
2695 */
2696 if (alloc_size < nlk->max_recvmsg_len) {
2697 skb = netlink_alloc_skb(sk,
2698 nlk->max_recvmsg_len,
2699 nlk->portid,
2700 GFP_KERNEL |
2701 __GFP_NOWARN |
2702 __GFP_NORETRY);
2703 /* available room should be exact amount to avoid MSG_TRUNC */
2704 if (skb)
2705 skb_reserve(skb, skb_tailroom(skb) -
2706 nlk->max_recvmsg_len);
2707 }
2708 if (!skb)
2709 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
2710 GFP_KERNEL);
c7ac8679 2711 if (!skb)
c63d6ea3 2712 goto errout_skb;
f9c22888 2713 netlink_skb_set_owner_r(skb, sk);
c7ac8679 2714
1da177e4
LT
2715 len = cb->dump(skb, cb);
2716
2717 if (len > 0) {
af65bdfc 2718 mutex_unlock(nlk->cb_mutex);
b1153f29
SH
2719
2720 if (sk_filter(sk, skb))
2721 kfree_skb(skb);
4a7e7c2a
ED
2722 else
2723 __netlink_sendskb(sk, skb);
1da177e4
LT
2724 return 0;
2725 }
2726
bf8b79e4
TG
2727 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2728 if (!nlh)
2729 goto errout_skb;
2730
670dc283
JB
2731 nl_dump_check_consistent(cb, nlh);
2732
bf8b79e4
TG
2733 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2734
b1153f29
SH
2735 if (sk_filter(sk, skb))
2736 kfree_skb(skb);
4a7e7c2a
ED
2737 else
2738 __netlink_sendskb(sk, skb);
1da177e4 2739
a8f74b22
TG
2740 if (cb->done)
2741 cb->done(cb);
1da177e4 2742
16b304f3
PS
2743 nlk->cb_running = false;
2744 mutex_unlock(nlk->cb_mutex);
6dc878a8 2745 module_put(cb->module);
16b304f3 2746 consume_skb(cb->skb);
1da177e4 2747 return 0;
1797754e 2748
bf8b79e4 2749errout_skb:
af65bdfc 2750 mutex_unlock(nlk->cb_mutex);
bf8b79e4 2751 kfree_skb(skb);
bf8b79e4 2752 return err;
1da177e4
LT
2753}
2754
6dc878a8
G
2755int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2756 const struct nlmsghdr *nlh,
2757 struct netlink_dump_control *control)
1da177e4
LT
2758{
2759 struct netlink_callback *cb;
2760 struct sock *sk;
2761 struct netlink_sock *nlk;
b44d211e 2762 int ret;
1da177e4 2763
f9c22888
PM
2764 /* Memory mapped dump requests need to be copied to avoid looping
2765 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2766 * a reference to the skb.
2767 */
2768 if (netlink_skb_is_mmaped(skb)) {
2769 skb = skb_copy(skb, GFP_KERNEL);
16b304f3 2770 if (skb == NULL)
f9c22888 2771 return -ENOBUFS;
f9c22888
PM
2772 } else
2773 atomic_inc(&skb->users);
2774
15e47304 2775 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
1da177e4 2776 if (sk == NULL) {
16b304f3
PS
2777 ret = -ECONNREFUSED;
2778 goto error_free;
1da177e4 2779 }
6dc878a8 2780
16b304f3 2781 nlk = nlk_sk(sk);
af65bdfc 2782 mutex_lock(nlk->cb_mutex);
6dc878a8 2783 /* A dump is in progress... */
16b304f3 2784 if (nlk->cb_running) {
6dc878a8 2785 ret = -EBUSY;
16b304f3 2786 goto error_unlock;
1da177e4 2787 }
6dc878a8 2788 /* add reference of module which cb->dump belongs to */
16b304f3 2789 if (!try_module_get(control->module)) {
6dc878a8 2790 ret = -EPROTONOSUPPORT;
16b304f3 2791 goto error_unlock;
6dc878a8
G
2792 }
2793
16b304f3
PS
2794 cb = &nlk->cb;
2795 memset(cb, 0, sizeof(*cb));
2796 cb->dump = control->dump;
2797 cb->done = control->done;
2798 cb->nlh = nlh;
2799 cb->data = control->data;
2800 cb->module = control->module;
2801 cb->min_dump_alloc = control->min_dump_alloc;
2802 cb->skb = skb;
2803
2804 nlk->cb_running = true;
2805
af65bdfc 2806 mutex_unlock(nlk->cb_mutex);
1da177e4 2807
b44d211e 2808 ret = netlink_dump(sk);
1da177e4 2809 sock_put(sk);
5c58298c 2810
b44d211e
AV
2811 if (ret)
2812 return ret;
2813
5c58298c
DL
2814 /* We successfully started a dump, by returning -EINTR we
2815 * signal not to send ACK even if it was requested.
2816 */
2817 return -EINTR;
16b304f3
PS
2818
2819error_unlock:
2820 sock_put(sk);
2821 mutex_unlock(nlk->cb_mutex);
2822error_free:
2823 kfree_skb(skb);
2824 return ret;
1da177e4 2825}
6dc878a8 2826EXPORT_SYMBOL(__netlink_dump_start);
1da177e4
LT
2827
2828void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2829{
2830 struct sk_buff *skb;
2831 struct nlmsghdr *rep;
2832 struct nlmsgerr *errmsg;
339bf98f 2833 size_t payload = sizeof(*errmsg);
1da177e4 2834
339bf98f
TG
2835 /* error messages get the original request appened */
2836 if (err)
2837 payload += nlmsg_len(nlh);
1da177e4 2838
f9c22888
PM
2839 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2840 NETLINK_CB(in_skb).portid, GFP_KERNEL);
1da177e4
LT
2841 if (!skb) {
2842 struct sock *sk;
2843
3b1e0a65 2844 sk = netlink_lookup(sock_net(in_skb->sk),
b4b51029 2845 in_skb->sk->sk_protocol,
15e47304 2846 NETLINK_CB(in_skb).portid);
1da177e4
LT
2847 if (sk) {
2848 sk->sk_err = ENOBUFS;
2849 sk->sk_error_report(sk);
2850 sock_put(sk);
2851 }
2852 return;
2853 }
2854
15e47304 2855 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
5dba93ae 2856 NLMSG_ERROR, payload, 0);
bf8b79e4 2857 errmsg = nlmsg_data(rep);
1da177e4 2858 errmsg->error = err;
bf8b79e4 2859 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
15e47304 2860 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
1da177e4 2861}
6ac552fd 2862EXPORT_SYMBOL(netlink_ack);
1da177e4 2863
cd40b7d3 2864int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
1d00a4eb 2865 struct nlmsghdr *))
82ace47a 2866{
82ace47a
TG
2867 struct nlmsghdr *nlh;
2868 int err;
2869
2870 while (skb->len >= nlmsg_total_size(0)) {
cd40b7d3
DL
2871 int msglen;
2872
b529ccf2 2873 nlh = nlmsg_hdr(skb);
d35b6856 2874 err = 0;
82ace47a 2875
ad8e4b75 2876 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
82ace47a
TG
2877 return 0;
2878
d35b6856
TG
2879 /* Only requests are handled by the kernel */
2880 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
5c58298c 2881 goto ack;
45e7ae7f
TG
2882
2883 /* Skip control messages */
2884 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
5c58298c 2885 goto ack;
d35b6856 2886
1d00a4eb 2887 err = cb(skb, nlh);
5c58298c
DL
2888 if (err == -EINTR)
2889 goto skip;
2890
2891ack:
d35b6856 2892 if (nlh->nlmsg_flags & NLM_F_ACK || err)
82ace47a 2893 netlink_ack(skb, nlh, err);
82ace47a 2894
5c58298c 2895skip:
6ac552fd 2896 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
cd40b7d3
DL
2897 if (msglen > skb->len)
2898 msglen = skb->len;
2899 skb_pull(skb, msglen);
82ace47a
TG
2900 }
2901
2902 return 0;
2903}
6ac552fd 2904EXPORT_SYMBOL(netlink_rcv_skb);
82ace47a 2905
d387f6ad
TG
2906/**
2907 * nlmsg_notify - send a notification netlink message
2908 * @sk: netlink socket to use
2909 * @skb: notification message
15e47304 2910 * @portid: destination netlink portid for reports or 0
d387f6ad
TG
2911 * @group: destination multicast group or 0
2912 * @report: 1 to report back, 0 to disable
2913 * @flags: allocation flags
2914 */
15e47304 2915int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
d387f6ad
TG
2916 unsigned int group, int report, gfp_t flags)
2917{
2918 int err = 0;
2919
2920 if (group) {
15e47304 2921 int exclude_portid = 0;
d387f6ad
TG
2922
2923 if (report) {
2924 atomic_inc(&skb->users);
15e47304 2925 exclude_portid = portid;
d387f6ad
TG
2926 }
2927
1ce85fe4
PNA
2928 /* errors reported via destination sk->sk_err, but propagate
2929 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
15e47304 2930 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
d387f6ad
TG
2931 }
2932
1ce85fe4
PNA
2933 if (report) {
2934 int err2;
2935
15e47304 2936 err2 = nlmsg_unicast(sk, skb, portid);
1ce85fe4
PNA
2937 if (!err || err == -ESRCH)
2938 err = err2;
2939 }
d387f6ad
TG
2940
2941 return err;
2942}
6ac552fd 2943EXPORT_SYMBOL(nlmsg_notify);
d387f6ad 2944
1da177e4
LT
2945#ifdef CONFIG_PROC_FS
2946struct nl_seq_iter {
e372c414 2947 struct seq_net_private p;
1da177e4
LT
2948 int link;
2949 int hash_idx;
2950};
2951
2952static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2953{
2954 struct nl_seq_iter *iter = seq->private;
2955 int i, j;
2956 struct sock *s;
1da177e4
LT
2957 loff_t off = 0;
2958
6ac552fd 2959 for (i = 0; i < MAX_LINKS; i++) {
15e47304 2960 struct nl_portid_hash *hash = &nl_table[i].hash;
1da177e4
LT
2961
2962 for (j = 0; j <= hash->mask; j++) {
b67bfe0d 2963 sk_for_each(s, &hash->table[j]) {
1218854a 2964 if (sock_net(s) != seq_file_net(seq))
b4b51029 2965 continue;
1da177e4
LT
2966 if (off == pos) {
2967 iter->link = i;
2968 iter->hash_idx = j;
2969 return s;
2970 }
2971 ++off;
2972 }
2973 }
2974 }
2975 return NULL;
2976}
2977
2978static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
9a429c49 2979 __acquires(nl_table_lock)
1da177e4
LT
2980{
2981 read_lock(&nl_table_lock);
2982 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2983}
2984
2985static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2986{
2987 struct sock *s;
2988 struct nl_seq_iter *iter;
da12c90e 2989 struct net *net;
1da177e4
LT
2990 int i, j;
2991
2992 ++*pos;
2993
2994 if (v == SEQ_START_TOKEN)
2995 return netlink_seq_socket_idx(seq, 0);
746fac4d 2996
da12c90e 2997 net = seq_file_net(seq);
b4b51029
EB
2998 iter = seq->private;
2999 s = v;
3000 do {
3001 s = sk_next(s);
da12c90e 3002 } while (s && !nl_table[s->sk_protocol].compare(net, s));
1da177e4
LT
3003 if (s)
3004 return s;
3005
1da177e4
LT
3006 i = iter->link;
3007 j = iter->hash_idx + 1;
3008
3009 do {
15e47304 3010 struct nl_portid_hash *hash = &nl_table[i].hash;
1da177e4
LT
3011
3012 for (; j <= hash->mask; j++) {
3013 s = sk_head(&hash->table[j]);
da12c90e
G
3014
3015 while (s && !nl_table[s->sk_protocol].compare(net, s))
b4b51029 3016 s = sk_next(s);
1da177e4
LT
3017 if (s) {
3018 iter->link = i;
3019 iter->hash_idx = j;
3020 return s;
3021 }
3022 }
3023
3024 j = 0;
3025 } while (++i < MAX_LINKS);
3026
3027 return NULL;
3028}
3029
3030static void netlink_seq_stop(struct seq_file *seq, void *v)
9a429c49 3031 __releases(nl_table_lock)
1da177e4
LT
3032{
3033 read_unlock(&nl_table_lock);
3034}
3035
3036
3037static int netlink_seq_show(struct seq_file *seq, void *v)
3038{
658cb354 3039 if (v == SEQ_START_TOKEN) {
1da177e4
LT
3040 seq_puts(seq,
3041 "sk Eth Pid Groups "
cf0aa4e0 3042 "Rmem Wmem Dump Locks Drops Inode\n");
658cb354 3043 } else {
1da177e4
LT
3044 struct sock *s = v;
3045 struct netlink_sock *nlk = nlk_sk(s);
3046
16b304f3 3047 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
1da177e4
LT
3048 s,
3049 s->sk_protocol,
15e47304 3050 nlk->portid,
513c2500 3051 nlk->groups ? (u32)nlk->groups[0] : 0,
31e6d363
ED
3052 sk_rmem_alloc_get(s),
3053 sk_wmem_alloc_get(s),
16b304f3 3054 nlk->cb_running,
38938bfe 3055 atomic_read(&s->sk_refcnt),
cf0aa4e0
MY
3056 atomic_read(&s->sk_drops),
3057 sock_i_ino(s)
1da177e4
LT
3058 );
3059
3060 }
3061 return 0;
3062}
3063
56b3d975 3064static const struct seq_operations netlink_seq_ops = {
1da177e4
LT
3065 .start = netlink_seq_start,
3066 .next = netlink_seq_next,
3067 .stop = netlink_seq_stop,
3068 .show = netlink_seq_show,
3069};
3070
3071
3072static int netlink_seq_open(struct inode *inode, struct file *file)
3073{
e372c414
DL
3074 return seq_open_net(inode, file, &netlink_seq_ops,
3075 sizeof(struct nl_seq_iter));
b4b51029
EB
3076}
3077
da7071d7 3078static const struct file_operations netlink_seq_fops = {
1da177e4
LT
3079 .owner = THIS_MODULE,
3080 .open = netlink_seq_open,
3081 .read = seq_read,
3082 .llseek = seq_lseek,
e372c414 3083 .release = seq_release_net,
1da177e4
LT
3084};
3085
3086#endif
3087
3088int netlink_register_notifier(struct notifier_block *nb)
3089{
e041c683 3090 return atomic_notifier_chain_register(&netlink_chain, nb);
1da177e4 3091}
6ac552fd 3092EXPORT_SYMBOL(netlink_register_notifier);
1da177e4
LT
3093
3094int netlink_unregister_notifier(struct notifier_block *nb)
3095{
e041c683 3096 return atomic_notifier_chain_unregister(&netlink_chain, nb);
1da177e4 3097}
6ac552fd 3098EXPORT_SYMBOL(netlink_unregister_notifier);
746fac4d 3099
90ddc4f0 3100static const struct proto_ops netlink_ops = {
1da177e4
LT
3101 .family = PF_NETLINK,
3102 .owner = THIS_MODULE,
3103 .release = netlink_release,
3104 .bind = netlink_bind,
3105 .connect = netlink_connect,
3106 .socketpair = sock_no_socketpair,
3107 .accept = sock_no_accept,
3108 .getname = netlink_getname,
9652e931 3109 .poll = netlink_poll,
1da177e4
LT
3110 .ioctl = sock_no_ioctl,
3111 .listen = sock_no_listen,
3112 .shutdown = sock_no_shutdown,
9a4595bc
PM
3113 .setsockopt = netlink_setsockopt,
3114 .getsockopt = netlink_getsockopt,
1da177e4
LT
3115 .sendmsg = netlink_sendmsg,
3116 .recvmsg = netlink_recvmsg,
ccdfcc39 3117 .mmap = netlink_mmap,
1da177e4
LT
3118 .sendpage = sock_no_sendpage,
3119};
3120
ec1b4cf7 3121static const struct net_proto_family netlink_family_ops = {
1da177e4
LT
3122 .family = PF_NETLINK,
3123 .create = netlink_create,
3124 .owner = THIS_MODULE, /* for consistency 8) */
3125};
3126
4665079c 3127static int __net_init netlink_net_init(struct net *net)
b4b51029
EB
3128{
3129#ifdef CONFIG_PROC_FS
d4beaa66 3130 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
b4b51029
EB
3131 return -ENOMEM;
3132#endif
3133 return 0;
3134}
3135
4665079c 3136static void __net_exit netlink_net_exit(struct net *net)
b4b51029
EB
3137{
3138#ifdef CONFIG_PROC_FS
ece31ffd 3139 remove_proc_entry("netlink", net->proc_net);
b4b51029
EB
3140#endif
3141}
3142
b963ea89
DM
3143static void __init netlink_add_usersock_entry(void)
3144{
5c398dc8 3145 struct listeners *listeners;
b963ea89
DM
3146 int groups = 32;
3147
5c398dc8 3148 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
b963ea89 3149 if (!listeners)
5c398dc8 3150 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
b963ea89
DM
3151
3152 netlink_table_grab();
3153
3154 nl_table[NETLINK_USERSOCK].groups = groups;
5c398dc8 3155 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
b963ea89
DM
3156 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
3157 nl_table[NETLINK_USERSOCK].registered = 1;
9785e10a 3158 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
b963ea89
DM
3159
3160 netlink_table_ungrab();
3161}
3162
022cbae6 3163static struct pernet_operations __net_initdata netlink_net_ops = {
b4b51029
EB
3164 .init = netlink_net_init,
3165 .exit = netlink_net_exit,
3166};
3167
1da177e4
LT
3168static int __init netlink_proto_init(void)
3169{
1da177e4 3170 int i;
26ff5ddc 3171 unsigned long limit;
1da177e4
LT
3172 unsigned int order;
3173 int err = proto_register(&netlink_proto, 0);
3174
3175 if (err != 0)
3176 goto out;
3177
fab25745 3178 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
1da177e4 3179
0da974f4 3180 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
fab2caf6
AM
3181 if (!nl_table)
3182 goto panic;
1da177e4 3183
4481374c
JB
3184 if (totalram_pages >= (128 * 1024))
3185 limit = totalram_pages >> (21 - PAGE_SHIFT);
1da177e4 3186 else
4481374c 3187 limit = totalram_pages >> (23 - PAGE_SHIFT);
1da177e4 3188
26ff5ddc
DC
3189 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
3190 limit = (1UL << order) / sizeof(struct hlist_head);
3191 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
1da177e4
LT
3192
3193 for (i = 0; i < MAX_LINKS; i++) {
15e47304 3194 struct nl_portid_hash *hash = &nl_table[i].hash;
1da177e4 3195
15e47304 3196 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
1da177e4
LT
3197 if (!hash->table) {
3198 while (i-- > 0)
15e47304 3199 nl_portid_hash_free(nl_table[i].hash.table,
1da177e4
LT
3200 1 * sizeof(*hash->table));
3201 kfree(nl_table);
fab2caf6 3202 goto panic;
1da177e4 3203 }
1da177e4
LT
3204 hash->max_shift = order;
3205 hash->shift = 0;
3206 hash->mask = 0;
3207 hash->rehash_time = jiffies;
ca15febf
G
3208
3209 nl_table[i].compare = netlink_compare;
1da177e4
LT
3210 }
3211
bcbde0d4
DB
3212 INIT_LIST_HEAD(&netlink_tap_all);
3213
b963ea89
DM
3214 netlink_add_usersock_entry();
3215
1da177e4 3216 sock_register(&netlink_family_ops);
b4b51029 3217 register_pernet_subsys(&netlink_net_ops);
746fac4d 3218 /* The netlink device handler may be needed early. */
1da177e4
LT
3219 rtnetlink_init();
3220out:
3221 return err;
fab2caf6
AM
3222panic:
3223 panic("netlink_init: Cannot allocate nl_table\n");
1da177e4
LT
3224}
3225
1da177e4 3226core_initcall(netlink_proto_init);