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