blob: a324b4b34c909094f1b1aac14e986e3777bccd5d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Patrick McHardycd1df522013-04-17 06:47:05 +00006 * Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * 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)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070017 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#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>
Andrew Morton54e0f522005-04-30 07:07:04 +010057#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070058#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000059#include <linux/vmalloc.h>
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +020060#include <linux/if_arp.h>
Thomas Grafe3416942014-08-02 11:47:45 +020061#include <linux/rhashtable.h>
Patrick McHardy9652e932013-04-17 06:47:02 +000062#include <asm/cacheflush.h>
Thomas Grafe3416942014-08-02 11:47:45 +020063#include <linux/hash.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010064
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020065#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/sock.h>
67#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010068#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Andrey Vagin0f29c762013-03-21 20:33:47 +040070#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Eric Dumazet5c398dc2010-10-24 04:27:10 +000072struct listeners {
73 struct rcu_head rcu;
74 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000075};
76
Patrick McHardycd967e02013-04-17 06:46:56 +000077/* state bits */
78#define NETLINK_CONGESTED 0x0
79
80/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070081#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070082#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000083#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070084#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070085
David S. Miller035c4c12011-12-23 17:33:03 -050086static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070087{
88 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
89}
90
Andrey Vagin0f29c762013-03-21 20:33:47 +040091struct netlink_table *nl_table;
92EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
95
96static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000097static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Andrey Vagin0f29c762013-03-21 20:33:47 +040099DEFINE_RWLOCK(nl_table_lock);
100EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101static atomic_t nl_table_users = ATOMIC_INIT(0);
102
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000103#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
104
Thomas Grafe3416942014-08-02 11:47:45 +0200105/* Protects netlink socket hash table mutations */
106DEFINE_MUTEX(nl_sk_hash_lock);
Thomas Graf6c8f7e72014-08-07 00:18:47 +0100107EXPORT_SYMBOL_GPL(nl_sk_hash_lock);
Thomas Grafe3416942014-08-02 11:47:45 +0200108
109static int lockdep_nl_sk_hash_is_held(void)
110{
111#ifdef CONFIG_LOCKDEP
112 return (debug_locks) ? lockdep_is_held(&nl_sk_hash_lock) : 1;
113#else
114 return 1;
115#endif
116}
117
Alan Sterne041c682006-03-27 01:16:30 -0800118static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200120static DEFINE_SPINLOCK(netlink_tap_lock);
121static struct list_head netlink_tap_all __read_mostly;
122
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000123static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700124{
125 return group ? 1 << (group - 1) : 0;
126}
127
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200128int netlink_add_tap(struct netlink_tap *nt)
129{
130 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
131 return -EINVAL;
132
133 spin_lock(&netlink_tap_lock);
134 list_add_rcu(&nt->list, &netlink_tap_all);
135 spin_unlock(&netlink_tap_lock);
136
137 if (nt->module)
138 __module_get(nt->module);
139
140 return 0;
141}
142EXPORT_SYMBOL_GPL(netlink_add_tap);
143
stephen hemminger2173f8d2013-12-30 10:49:22 -0800144static int __netlink_remove_tap(struct netlink_tap *nt)
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200145{
146 bool found = false;
147 struct netlink_tap *tmp;
148
149 spin_lock(&netlink_tap_lock);
150
151 list_for_each_entry(tmp, &netlink_tap_all, list) {
152 if (nt == tmp) {
153 list_del_rcu(&nt->list);
154 found = true;
155 goto out;
156 }
157 }
158
159 pr_warn("__netlink_remove_tap: %p not found\n", nt);
160out:
161 spin_unlock(&netlink_tap_lock);
162
163 if (found && nt->module)
164 module_put(nt->module);
165
166 return found ? 0 : -ENODEV;
167}
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200168
169int netlink_remove_tap(struct netlink_tap *nt)
170{
171 int ret;
172
173 ret = __netlink_remove_tap(nt);
174 synchronize_net();
175
176 return ret;
177}
178EXPORT_SYMBOL_GPL(netlink_remove_tap);
179
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200180static bool netlink_filter_tap(const struct sk_buff *skb)
181{
182 struct sock *sk = skb->sk;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200183
184 /* We take the more conservative approach and
185 * whitelist socket protocols that may pass.
186 */
187 switch (sk->sk_protocol) {
188 case NETLINK_ROUTE:
189 case NETLINK_USERSOCK:
190 case NETLINK_SOCK_DIAG:
191 case NETLINK_NFLOG:
192 case NETLINK_XFRM:
193 case NETLINK_FIB_LOOKUP:
194 case NETLINK_NETFILTER:
195 case NETLINK_GENERIC:
Varka Bhadram498044b2014-07-16 10:59:47 +0530196 return true;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200197 }
198
Varka Bhadram498044b2014-07-16 10:59:47 +0530199 return false;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200200}
201
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200202static int __netlink_deliver_tap_skb(struct sk_buff *skb,
203 struct net_device *dev)
204{
205 struct sk_buff *nskb;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200206 struct sock *sk = skb->sk;
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200207 int ret = -ENOMEM;
208
209 dev_hold(dev);
210 nskb = skb_clone(skb, GFP_ATOMIC);
211 if (nskb) {
212 nskb->dev = dev;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200213 nskb->protocol = htons((u16) sk->sk_protocol);
Daniel Borkmann604d13c2013-12-23 14:35:56 +0100214 nskb->pkt_type = netlink_is_kernel(sk) ?
215 PACKET_KERNEL : PACKET_USER;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200216
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200217 ret = dev_queue_xmit(nskb);
218 if (unlikely(ret > 0))
219 ret = net_xmit_errno(ret);
220 }
221
222 dev_put(dev);
223 return ret;
224}
225
226static void __netlink_deliver_tap(struct sk_buff *skb)
227{
228 int ret;
229 struct netlink_tap *tmp;
230
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200231 if (!netlink_filter_tap(skb))
232 return;
233
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200234 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
235 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
236 if (unlikely(ret))
237 break;
238 }
239}
240
241static void netlink_deliver_tap(struct sk_buff *skb)
242{
243 rcu_read_lock();
244
245 if (unlikely(!list_empty(&netlink_tap_all)))
246 __netlink_deliver_tap(skb);
247
248 rcu_read_unlock();
249}
250
Daniel Borkmann73bfd372013-12-23 14:35:55 +0100251static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
252 struct sk_buff *skb)
253{
254 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
255 netlink_deliver_tap(skb);
256}
257
Patrick McHardycd1df522013-04-17 06:47:05 +0000258static void netlink_overrun(struct sock *sk)
259{
260 struct netlink_sock *nlk = nlk_sk(sk);
261
262 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
263 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
264 sk->sk_err = ENOBUFS;
265 sk->sk_error_report(sk);
266 }
267 }
268 atomic_inc(&sk->sk_drops);
269}
270
271static void netlink_rcv_wake(struct sock *sk)
272{
273 struct netlink_sock *nlk = nlk_sk(sk);
274
275 if (skb_queue_empty(&sk->sk_receive_queue))
276 clear_bit(NETLINK_CONGESTED, &nlk->state);
277 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
278 wake_up_interruptible(&nlk->wait);
279}
280
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000281#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000282static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
283{
284 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
285}
286
Patrick McHardyf9c22882013-04-17 06:47:04 +0000287static bool netlink_rx_is_mmaped(struct sock *sk)
288{
289 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
290}
291
Patrick McHardy5fd96122013-04-17 06:47:03 +0000292static bool netlink_tx_is_mmaped(struct sock *sk)
293{
294 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
295}
296
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000297static __pure struct page *pgvec_to_page(const void *addr)
298{
299 if (is_vmalloc_addr(addr))
300 return vmalloc_to_page(addr);
301 else
302 return virt_to_page(addr);
303}
304
305static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
306{
307 unsigned int i;
308
309 for (i = 0; i < len; i++) {
310 if (pg_vec[i] != NULL) {
311 if (is_vmalloc_addr(pg_vec[i]))
312 vfree(pg_vec[i]);
313 else
314 free_pages((unsigned long)pg_vec[i], order);
315 }
316 }
317 kfree(pg_vec);
318}
319
320static void *alloc_one_pg_vec_page(unsigned long order)
321{
322 void *buffer;
323 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
324 __GFP_NOWARN | __GFP_NORETRY;
325
326 buffer = (void *)__get_free_pages(gfp_flags, order);
327 if (buffer != NULL)
328 return buffer;
329
330 buffer = vzalloc((1 << order) * PAGE_SIZE);
331 if (buffer != NULL)
332 return buffer;
333
334 gfp_flags &= ~__GFP_NORETRY;
335 return (void *)__get_free_pages(gfp_flags, order);
336}
337
338static void **alloc_pg_vec(struct netlink_sock *nlk,
339 struct nl_mmap_req *req, unsigned int order)
340{
341 unsigned int block_nr = req->nm_block_nr;
342 unsigned int i;
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200343 void **pg_vec;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000344
345 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
346 if (pg_vec == NULL)
347 return NULL;
348
349 for (i = 0; i < block_nr; i++) {
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200350 pg_vec[i] = alloc_one_pg_vec_page(order);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000351 if (pg_vec[i] == NULL)
352 goto err1;
353 }
354
355 return pg_vec;
356err1:
357 free_pg_vec(pg_vec, order, block_nr);
358 return NULL;
359}
360
361static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
362 bool closing, bool tx_ring)
363{
364 struct netlink_sock *nlk = nlk_sk(sk);
365 struct netlink_ring *ring;
366 struct sk_buff_head *queue;
367 void **pg_vec = NULL;
368 unsigned int order = 0;
369 int err;
370
371 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
372 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
373
374 if (!closing) {
375 if (atomic_read(&nlk->mapped))
376 return -EBUSY;
377 if (atomic_read(&ring->pending))
378 return -EBUSY;
379 }
380
381 if (req->nm_block_nr) {
382 if (ring->pg_vec != NULL)
383 return -EBUSY;
384
385 if ((int)req->nm_block_size <= 0)
386 return -EINVAL;
Tobias Klauser74e83b22014-07-31 12:17:08 +0200387 if (!PAGE_ALIGNED(req->nm_block_size))
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000388 return -EINVAL;
389 if (req->nm_frame_size < NL_MMAP_HDRLEN)
390 return -EINVAL;
391 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
392 return -EINVAL;
393
394 ring->frames_per_block = req->nm_block_size /
395 req->nm_frame_size;
396 if (ring->frames_per_block == 0)
397 return -EINVAL;
398 if (ring->frames_per_block * req->nm_block_nr !=
399 req->nm_frame_nr)
400 return -EINVAL;
401
402 order = get_order(req->nm_block_size);
403 pg_vec = alloc_pg_vec(nlk, req, order);
404 if (pg_vec == NULL)
405 return -ENOMEM;
406 } else {
407 if (req->nm_frame_nr)
408 return -EINVAL;
409 }
410
411 err = -EBUSY;
412 mutex_lock(&nlk->pg_vec_lock);
413 if (closing || atomic_read(&nlk->mapped) == 0) {
414 err = 0;
415 spin_lock_bh(&queue->lock);
416
417 ring->frame_max = req->nm_frame_nr - 1;
418 ring->head = 0;
419 ring->frame_size = req->nm_frame_size;
420 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
421
422 swap(ring->pg_vec_len, req->nm_block_nr);
423 swap(ring->pg_vec_order, order);
424 swap(ring->pg_vec, pg_vec);
425
426 __skb_queue_purge(queue);
427 spin_unlock_bh(&queue->lock);
428
429 WARN_ON(atomic_read(&nlk->mapped));
430 }
431 mutex_unlock(&nlk->pg_vec_lock);
432
433 if (pg_vec)
434 free_pg_vec(pg_vec, order, req->nm_block_nr);
435 return err;
436}
437
438static void netlink_mm_open(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_inc(&nlk_sk(sk)->mapped);
446}
447
448static void netlink_mm_close(struct vm_area_struct *vma)
449{
450 struct file *file = vma->vm_file;
451 struct socket *sock = file->private_data;
452 struct sock *sk = sock->sk;
453
454 if (sk)
455 atomic_dec(&nlk_sk(sk)->mapped);
456}
457
458static const struct vm_operations_struct netlink_mmap_ops = {
459 .open = netlink_mm_open,
460 .close = netlink_mm_close,
461};
462
463static int netlink_mmap(struct file *file, struct socket *sock,
464 struct vm_area_struct *vma)
465{
466 struct sock *sk = sock->sk;
467 struct netlink_sock *nlk = nlk_sk(sk);
468 struct netlink_ring *ring;
469 unsigned long start, size, expected;
470 unsigned int i;
471 int err = -EINVAL;
472
473 if (vma->vm_pgoff)
474 return -EINVAL;
475
476 mutex_lock(&nlk->pg_vec_lock);
477
478 expected = 0;
479 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
480 if (ring->pg_vec == NULL)
481 continue;
482 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
483 }
484
485 if (expected == 0)
486 goto out;
487
488 size = vma->vm_end - vma->vm_start;
489 if (size != expected)
490 goto out;
491
492 start = vma->vm_start;
493 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
494 if (ring->pg_vec == NULL)
495 continue;
496
497 for (i = 0; i < ring->pg_vec_len; i++) {
498 struct page *page;
499 void *kaddr = ring->pg_vec[i];
500 unsigned int pg_num;
501
502 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
503 page = pgvec_to_page(kaddr);
504 err = vm_insert_page(vma, start, page);
505 if (err < 0)
506 goto out;
507 start += PAGE_SIZE;
508 kaddr += PAGE_SIZE;
509 }
510 }
511 }
512
513 atomic_inc(&nlk->mapped);
514 vma->vm_ops = &netlink_mmap_ops;
515 err = 0;
516out:
517 mutex_unlock(&nlk->pg_vec_lock);
Patrick McHardy7cdbac72013-06-11 02:52:47 -0700518 return err;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000519}
Patrick McHardy9652e932013-04-17 06:47:02 +0000520
521static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
522{
523#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
524 struct page *p_start, *p_end;
525
526 /* First page is flushed through netlink_{get,set}_status */
527 p_start = pgvec_to_page(hdr + PAGE_SIZE);
Stephen Rothwell1d5085c2013-04-23 17:40:35 +1000528 p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
Patrick McHardy9652e932013-04-17 06:47:02 +0000529 while (p_start <= p_end) {
530 flush_dcache_page(p_start);
531 p_start++;
532 }
533#endif
534}
535
536static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
537{
538 smp_rmb();
539 flush_dcache_page(pgvec_to_page(hdr));
540 return hdr->nm_status;
541}
542
543static void netlink_set_status(struct nl_mmap_hdr *hdr,
544 enum nl_mmap_status status)
545{
546 hdr->nm_status = status;
547 flush_dcache_page(pgvec_to_page(hdr));
548 smp_wmb();
549}
550
551static struct nl_mmap_hdr *
552__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
553{
554 unsigned int pg_vec_pos, frame_off;
555
556 pg_vec_pos = pos / ring->frames_per_block;
557 frame_off = pos % ring->frames_per_block;
558
559 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
560}
561
562static struct nl_mmap_hdr *
563netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
564 enum nl_mmap_status status)
565{
566 struct nl_mmap_hdr *hdr;
567
568 hdr = __netlink_lookup_frame(ring, pos);
569 if (netlink_get_status(hdr) != status)
570 return NULL;
571
572 return hdr;
573}
574
575static struct nl_mmap_hdr *
576netlink_current_frame(const struct netlink_ring *ring,
577 enum nl_mmap_status status)
578{
579 return netlink_lookup_frame(ring, ring->head, status);
580}
581
582static struct nl_mmap_hdr *
583netlink_previous_frame(const struct netlink_ring *ring,
584 enum nl_mmap_status status)
585{
586 unsigned int prev;
587
588 prev = ring->head ? ring->head - 1 : ring->frame_max;
589 return netlink_lookup_frame(ring, prev, status);
590}
591
592static void netlink_increment_head(struct netlink_ring *ring)
593{
594 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
595}
596
597static void netlink_forward_ring(struct netlink_ring *ring)
598{
599 unsigned int head = ring->head, pos = head;
600 const struct nl_mmap_hdr *hdr;
601
602 do {
603 hdr = __netlink_lookup_frame(ring, pos);
604 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
605 break;
606 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
607 break;
608 netlink_increment_head(ring);
609 } while (ring->head != head);
610}
611
Patrick McHardycd1df522013-04-17 06:47:05 +0000612static bool netlink_dump_space(struct netlink_sock *nlk)
613{
614 struct netlink_ring *ring = &nlk->rx_ring;
615 struct nl_mmap_hdr *hdr;
616 unsigned int n;
617
618 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
619 if (hdr == NULL)
620 return false;
621
622 n = ring->head + ring->frame_max / 2;
623 if (n > ring->frame_max)
624 n -= ring->frame_max;
625
626 hdr = __netlink_lookup_frame(ring, n);
627
628 return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
629}
630
Patrick McHardy9652e932013-04-17 06:47:02 +0000631static unsigned int netlink_poll(struct file *file, struct socket *sock,
632 poll_table *wait)
633{
634 struct sock *sk = sock->sk;
635 struct netlink_sock *nlk = nlk_sk(sk);
636 unsigned int mask;
Patrick McHardycd1df522013-04-17 06:47:05 +0000637 int err;
Patrick McHardy9652e932013-04-17 06:47:02 +0000638
Patrick McHardycd1df522013-04-17 06:47:05 +0000639 if (nlk->rx_ring.pg_vec != NULL) {
640 /* Memory mapped sockets don't call recvmsg(), so flow control
641 * for dumps is performed here. A dump is allowed to continue
642 * if at least half the ring is unused.
643 */
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700644 while (nlk->cb_running && netlink_dump_space(nlk)) {
Patrick McHardycd1df522013-04-17 06:47:05 +0000645 err = netlink_dump(sk);
646 if (err < 0) {
Ben Pfaffac30ef82014-07-09 10:31:22 -0700647 sk->sk_err = -err;
Patrick McHardycd1df522013-04-17 06:47:05 +0000648 sk->sk_error_report(sk);
649 break;
650 }
651 }
652 netlink_rcv_wake(sk);
653 }
Patrick McHardy5fd96122013-04-17 06:47:03 +0000654
Patrick McHardy9652e932013-04-17 06:47:02 +0000655 mask = datagram_poll(file, sock, wait);
656
657 spin_lock_bh(&sk->sk_receive_queue.lock);
658 if (nlk->rx_ring.pg_vec) {
659 netlink_forward_ring(&nlk->rx_ring);
660 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
661 mask |= POLLIN | POLLRDNORM;
662 }
663 spin_unlock_bh(&sk->sk_receive_queue.lock);
664
665 spin_lock_bh(&sk->sk_write_queue.lock);
666 if (nlk->tx_ring.pg_vec) {
667 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
668 mask |= POLLOUT | POLLWRNORM;
669 }
670 spin_unlock_bh(&sk->sk_write_queue.lock);
671
672 return mask;
673}
674
675static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
676{
677 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
678}
679
680static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
681 struct netlink_ring *ring,
682 struct nl_mmap_hdr *hdr)
683{
684 unsigned int size;
685 void *data;
686
687 size = ring->frame_size - NL_MMAP_HDRLEN;
688 data = (void *)hdr + NL_MMAP_HDRLEN;
689
690 skb->head = data;
691 skb->data = data;
692 skb_reset_tail_pointer(skb);
693 skb->end = skb->tail + size;
694 skb->len = 0;
695
696 skb->destructor = netlink_skb_destructor;
697 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
698 NETLINK_CB(skb).sk = sk;
699}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000700
701static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
702 u32 dst_portid, u32 dst_group,
703 struct sock_iocb *siocb)
704{
705 struct netlink_sock *nlk = nlk_sk(sk);
706 struct netlink_ring *ring;
707 struct nl_mmap_hdr *hdr;
708 struct sk_buff *skb;
709 unsigned int maxlen;
710 bool excl = true;
711 int err = 0, len = 0;
712
713 /* Netlink messages are validated by the receiver before processing.
714 * In order to avoid userspace changing the contents of the message
715 * after validation, the socket and the ring may only be used by a
716 * single process, otherwise we fall back to copying.
717 */
718 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
719 atomic_read(&nlk->mapped) > 1)
720 excl = false;
721
722 mutex_lock(&nlk->pg_vec_lock);
723
724 ring = &nlk->tx_ring;
725 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
726
727 do {
728 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
729 if (hdr == NULL) {
730 if (!(msg->msg_flags & MSG_DONTWAIT) &&
731 atomic_read(&nlk->tx_ring.pending))
732 schedule();
733 continue;
734 }
735 if (hdr->nm_len > maxlen) {
736 err = -EINVAL;
737 goto out;
738 }
739
740 netlink_frame_flush_dcache(hdr);
741
742 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
743 skb = alloc_skb_head(GFP_KERNEL);
744 if (skb == NULL) {
745 err = -ENOBUFS;
746 goto out;
747 }
748 sock_hold(sk);
749 netlink_ring_setup_skb(skb, sk, ring, hdr);
750 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
751 __skb_put(skb, hdr->nm_len);
752 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
753 atomic_inc(&ring->pending);
754 } else {
755 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
756 if (skb == NULL) {
757 err = -ENOBUFS;
758 goto out;
759 }
760 __skb_put(skb, hdr->nm_len);
761 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
762 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
763 }
764
765 netlink_increment_head(ring);
766
767 NETLINK_CB(skb).portid = nlk->portid;
768 NETLINK_CB(skb).dst_group = dst_group;
769 NETLINK_CB(skb).creds = siocb->scm->creds;
770
771 err = security_netlink_send(sk, skb);
772 if (err) {
773 kfree_skb(skb);
774 goto out;
775 }
776
777 if (unlikely(dst_group)) {
778 atomic_inc(&skb->users);
779 netlink_broadcast(sk, skb, dst_portid, dst_group,
780 GFP_KERNEL);
781 }
782 err = netlink_unicast(sk, skb, dst_portid,
783 msg->msg_flags & MSG_DONTWAIT);
784 if (err < 0)
785 goto out;
786 len += err;
787
788 } while (hdr != NULL ||
789 (!(msg->msg_flags & MSG_DONTWAIT) &&
790 atomic_read(&nlk->tx_ring.pending)));
791
792 if (len > 0)
793 err = len;
794out:
795 mutex_unlock(&nlk->pg_vec_lock);
796 return err;
797}
Patrick McHardyf9c22882013-04-17 06:47:04 +0000798
799static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
800{
801 struct nl_mmap_hdr *hdr;
802
803 hdr = netlink_mmap_hdr(skb);
804 hdr->nm_len = skb->len;
805 hdr->nm_group = NETLINK_CB(skb).dst_group;
806 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200807 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
808 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000809 netlink_frame_flush_dcache(hdr);
810 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
811
812 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
813 kfree_skb(skb);
814}
815
816static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
817{
818 struct netlink_sock *nlk = nlk_sk(sk);
819 struct netlink_ring *ring = &nlk->rx_ring;
820 struct nl_mmap_hdr *hdr;
821
822 spin_lock_bh(&sk->sk_receive_queue.lock);
823 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
824 if (hdr == NULL) {
825 spin_unlock_bh(&sk->sk_receive_queue.lock);
826 kfree_skb(skb);
Patrick McHardycd1df522013-04-17 06:47:05 +0000827 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000828 return;
829 }
830 netlink_increment_head(ring);
831 __skb_queue_tail(&sk->sk_receive_queue, skb);
832 spin_unlock_bh(&sk->sk_receive_queue.lock);
833
834 hdr->nm_len = skb->len;
835 hdr->nm_group = NETLINK_CB(skb).dst_group;
836 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200837 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
838 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000839 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
840}
841
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000842#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000843#define netlink_skb_is_mmaped(skb) false
Patrick McHardyf9c22882013-04-17 06:47:04 +0000844#define netlink_rx_is_mmaped(sk) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000845#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000846#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000847#define netlink_poll datagram_poll
Patrick McHardy5fd96122013-04-17 06:47:03 +0000848#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000849#endif /* CONFIG_NETLINK_MMAP */
850
Patrick McHardycf0a0182013-04-17 06:47:00 +0000851static void netlink_skb_destructor(struct sk_buff *skb)
852{
Patrick McHardy9652e932013-04-17 06:47:02 +0000853#ifdef CONFIG_NETLINK_MMAP
854 struct nl_mmap_hdr *hdr;
855 struct netlink_ring *ring;
856 struct sock *sk;
857
858 /* If a packet from the kernel to userspace was freed because of an
859 * error without being delivered to userspace, the kernel must reset
860 * the status. In the direction userspace to kernel, the status is
861 * always reset here after the packet was processed and freed.
862 */
863 if (netlink_skb_is_mmaped(skb)) {
864 hdr = netlink_mmap_hdr(skb);
865 sk = NETLINK_CB(skb).sk;
866
Patrick McHardy5fd96122013-04-17 06:47:03 +0000867 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
868 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
869 ring = &nlk_sk(sk)->tx_ring;
870 } else {
871 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
872 hdr->nm_len = 0;
873 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
874 }
875 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000876 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000877
878 WARN_ON(atomic_read(&ring->pending) == 0);
879 atomic_dec(&ring->pending);
880 sock_put(sk);
881
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000882 skb->head = NULL;
Patrick McHardy9652e932013-04-17 06:47:02 +0000883 }
884#endif
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000885 if (is_vmalloc_addr(skb->head)) {
Pablo Neira3a365152013-06-28 03:04:23 +0200886 if (!skb->cloned ||
887 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
888 vfree(skb->head);
889
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000890 skb->head = NULL;
891 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000892 if (skb->sk != NULL)
893 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000894}
895
896static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
897{
898 WARN_ON(skb->sk != NULL);
899 skb->sk = sk;
900 skb->destructor = netlink_skb_destructor;
901 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
902 sk_mem_charge(sk, skb->truesize);
903}
904
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905static void netlink_sock_destruct(struct sock *sk)
906{
Herbert Xu3f660d62007-05-03 03:17:14 -0700907 struct netlink_sock *nlk = nlk_sk(sk);
908
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700909 if (nlk->cb_running) {
910 if (nlk->cb.done)
911 nlk->cb.done(&nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000912
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700913 module_put(nlk->cb.module);
914 kfree_skb(nlk->cb.skb);
Herbert Xu3f660d62007-05-03 03:17:14 -0700915 }
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000918#ifdef CONFIG_NETLINK_MMAP
919 if (1) {
920 struct nl_mmap_req req;
921
922 memset(&req, 0, sizeof(req));
923 if (nlk->rx_ring.pg_vec)
924 netlink_set_ring(sk, &req, true, false);
925 memset(&req, 0, sizeof(req));
926 if (nlk->tx_ring.pg_vec)
927 netlink_set_ring(sk, &req, true, true);
928 }
929#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800932 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 return;
934 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700935
936 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
937 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
938 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939}
940
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800941/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
942 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
944 * this, _but_ remember, it adds useless work on UP machines.
945 */
946
Johannes Bergd136f1b2009-09-12 03:03:15 +0000947void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800948 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000950 might_sleep();
951
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700952 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 if (atomic_read(&nl_table_users)) {
955 DECLARE_WAITQUEUE(wait, current);
956
957 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800958 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 set_current_state(TASK_UNINTERRUPTIBLE);
960 if (atomic_read(&nl_table_users) == 0)
961 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700962 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700964 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966
967 __set_current_state(TASK_RUNNING);
968 remove_wait_queue(&nl_table_wait, &wait);
969 }
970}
971
Johannes Bergd136f1b2009-09-12 03:03:15 +0000972void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800973 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700975 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 wake_up(&nl_table_wait);
977}
978
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800979static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980netlink_lock_table(void)
981{
982 /* read_lock() synchronizes us to netlink_table_grab */
983
984 read_lock(&nl_table_lock);
985 atomic_inc(&nl_table_users);
986 read_unlock(&nl_table_lock);
987}
988
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800989static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990netlink_unlock_table(void)
991{
992 if (atomic_dec_and_test(&nl_table_users))
993 wake_up(&nl_table_wait);
994}
995
Thomas Grafe3416942014-08-02 11:47:45 +0200996struct netlink_compare_arg
Gao fengda12c902013-06-06 14:49:11 +0800997{
Thomas Grafe3416942014-08-02 11:47:45 +0200998 struct net *net;
999 u32 portid;
1000};
1001
1002static bool netlink_compare(void *ptr, void *arg)
1003{
1004 struct netlink_compare_arg *x = arg;
1005 struct sock *sk = ptr;
1006
1007 return nlk_sk(sk)->portid == x->portid &&
1008 net_eq(sock_net(sk), x->net);
1009}
1010
1011static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
1012 struct net *net)
1013{
1014 struct netlink_compare_arg arg = {
1015 .net = net,
1016 .portid = portid,
1017 };
1018 u32 hash;
1019
1020 hash = rhashtable_hashfn(&table->hash, &portid, sizeof(portid));
1021
1022 return rhashtable_lookup_compare(&table->hash, hash,
1023 &netlink_compare, &arg);
Gao fengda12c902013-06-06 14:49:11 +08001024}
1025
Eric W. Biederman15e47302012-09-07 20:12:54 +00001026static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Gao fengda12c902013-06-06 14:49:11 +08001028 struct netlink_table *table = &nl_table[protocol];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
Thomas Grafe3416942014-08-02 11:47:45 +02001031 rcu_read_lock();
1032 sk = __netlink_lookup(table, portid, net);
1033 if (sk)
1034 sock_hold(sk);
1035 rcu_read_unlock();
1036
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 return sk;
1038}
1039
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001040static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Patrick McHardy4277a082006-03-20 18:52:01 -08001042static void
1043netlink_update_listeners(struct sock *sk)
1044{
1045 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -08001046 unsigned long mask;
1047 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001048 struct listeners *listeners;
1049
1050 listeners = nl_deref_protected(tbl->listeners);
1051 if (!listeners)
1052 return;
Patrick McHardy4277a082006-03-20 18:52:01 -08001053
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001054 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001055 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001056 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001057 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
1058 mask |= nlk_sk(sk)->groups[i];
1059 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001060 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -08001061 }
1062 /* this function is only called with the netlink table "grabbed", which
1063 * makes sure updates are visible before bind or setsockopt return. */
1064}
1065
Eric W. Biederman15e47302012-09-07 20:12:54 +00001066static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
Gao fengda12c902013-06-06 14:49:11 +08001068 struct netlink_table *table = &nl_table[sk->sk_protocol];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 int err = -EADDRINUSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Thomas Grafe3416942014-08-02 11:47:45 +02001071 mutex_lock(&nl_sk_hash_lock);
1072 if (__netlink_lookup(table, portid, net))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 goto err;
1074
1075 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto err;
1078
1079 err = -ENOMEM;
Thomas Grafe3416942014-08-02 11:47:45 +02001080 if (BITS_PER_LONG > 32 && unlikely(table->hash.nelems >= UINT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 goto err;
1082
Eric W. Biederman15e47302012-09-07 20:12:54 +00001083 nlk_sk(sk)->portid = portid;
Thomas Grafe3416942014-08-02 11:47:45 +02001084 sock_hold(sk);
1085 rhashtable_insert(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087err:
Thomas Grafe3416942014-08-02 11:47:45 +02001088 mutex_unlock(&nl_sk_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return err;
1090}
1091
1092static void netlink_remove(struct sock *sk)
1093{
Thomas Grafe3416942014-08-02 11:47:45 +02001094 struct netlink_table *table;
1095
1096 mutex_lock(&nl_sk_hash_lock);
1097 table = &nl_table[sk->sk_protocol];
1098 if (rhashtable_remove(&table->hash, &nlk_sk(sk)->node, GFP_KERNEL)) {
1099 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
1100 __sock_put(sk);
1101 }
1102 mutex_unlock(&nl_sk_hash_lock);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001105 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 __sk_del_bind_node(sk);
1107 netlink_table_ungrab();
1108}
1109
1110static struct proto netlink_proto = {
1111 .name = "NETLINK",
1112 .owner = THIS_MODULE,
1113 .obj_size = sizeof(struct netlink_sock),
1114};
1115
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001116static int __netlink_create(struct net *net, struct socket *sock,
1117 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct sock *sk;
1120 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -07001121
1122 sock->ops = &netlink_ops;
1123
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001124 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -07001125 if (!sk)
1126 return -ENOMEM;
1127
1128 sock_init_data(sock, sk);
1129
1130 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001131 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001132 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +00001133 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001134 nlk->cb_mutex = &nlk->cb_def_mutex;
1135 mutex_init(nlk->cb_mutex);
1136 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001137 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001138#ifdef CONFIG_NETLINK_MMAP
1139 mutex_init(&nlk->pg_vec_lock);
1140#endif
Patrick McHardyab33a172005-08-14 19:31:36 -07001141
1142 sk->sk_destruct = netlink_sock_destruct;
1143 sk->sk_protocol = protocol;
1144 return 0;
1145}
1146
Eric Paris3f378b62009-11-05 22:18:14 -08001147static int netlink_create(struct net *net, struct socket *sock, int protocol,
1148 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -07001149{
1150 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001151 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001152 struct netlink_sock *nlk;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001153 int (*bind)(int group);
1154 void (*unbind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -07001155 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 sock->state = SS_UNCONNECTED;
1158
1159 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1160 return -ESOCKTNOSUPPORT;
1161
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001162 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 return -EPROTONOSUPPORT;
1164
Patrick McHardy77247bb2005-08-14 19:27:13 -07001165 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -07001166#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -07001167 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -07001168 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001169 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001170 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001171 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001172#endif
1173 if (nl_table[protocol].registered &&
1174 try_module_get(nl_table[protocol].module))
1175 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001176 else
1177 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001178 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001179 bind = nl_table[protocol].bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001180 unbind = nl_table[protocol].unbind;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001181 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001182
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001183 if (err < 0)
1184 goto out;
1185
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001186 err = __netlink_create(net, sock, cb_mutex, protocol);
1187 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -07001188 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
David S. Miller6f756a82008-11-23 17:34:03 -08001190 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001191 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -08001192 local_bh_enable();
1193
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001194 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001195 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001196 nlk->netlink_bind = bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001197 nlk->netlink_unbind = unbind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001198out:
1199 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Patrick McHardyab33a172005-08-14 19:31:36 -07001201out_module:
1202 module_put(module);
1203 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204}
1205
1206static int netlink_release(struct socket *sock)
1207{
1208 struct sock *sk = sock->sk;
1209 struct netlink_sock *nlk;
1210
1211 if (!sk)
1212 return 0;
1213
1214 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001215 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 nlk = nlk_sk(sk);
1217
Herbert Xu3f660d62007-05-03 03:17:14 -07001218 /*
1219 * OK. Socket is unlinked, any packets that arrive now
1220 * will be purged.
1221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 sock->sk = NULL;
1224 wake_up_interruptible_all(&nlk->wait);
1225
1226 skb_queue_purge(&sk->sk_write_queue);
1227
Eric W. Biederman15e47302012-09-07 20:12:54 +00001228 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001230 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001232 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 };
Alan Sterne041c682006-03-27 01:16:30 -08001234 atomic_notifier_call_chain(&netlink_chain,
1235 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001236 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001237
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001238 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001239
Patrick McHardy4277a082006-03-20 18:52:01 -08001240 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -07001241 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001242 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1243 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001244 struct listeners *old;
1245
1246 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1247 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1248 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001249 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001250 nl_table[sk->sk_protocol].bind = NULL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001251 nl_table[sk->sk_protocol].unbind = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001252 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001253 nl_table[sk->sk_protocol].registered = 0;
1254 }
Eric Dumazet658cb352012-04-22 21:30:21 +00001255 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001256 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001257 }
Patrick McHardy4277a082006-03-20 18:52:01 -08001258 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -07001259
Thomas Grafe3416942014-08-02 11:47:45 +02001260 /* Wait for readers to complete */
1261 synchronize_net();
1262
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001263 kfree(nlk->groups);
1264 nlk->groups = NULL;
1265
Eric Dumazet37558102008-11-24 14:05:22 -08001266 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001267 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001268 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 sock_put(sk);
1270 return 0;
1271}
1272
1273static int netlink_autobind(struct socket *sock)
1274{
1275 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001276 struct net *net = sock_net(sk);
Gao fengda12c902013-06-06 14:49:11 +08001277 struct netlink_table *table = &nl_table[sk->sk_protocol];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001278 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 int err;
1280 static s32 rover = -4097;
1281
1282retry:
1283 cond_resched();
Thomas Grafe3416942014-08-02 11:47:45 +02001284 rcu_read_lock();
1285 if (__netlink_lookup(table, portid, net)) {
1286 /* Bind collision, search negative portid values. */
1287 portid = rover--;
1288 if (rover > -4097)
1289 rover = -4097;
1290 rcu_read_unlock();
1291 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Thomas Grafe3416942014-08-02 11:47:45 +02001293 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Eric W. Biederman15e47302012-09-07 20:12:54 +00001295 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (err == -EADDRINUSE)
1297 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001298
1299 /* If 2 threads race to autobind, that is fine. */
1300 if (err == -EBUSY)
1301 err = 0;
1302
1303 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304}
1305
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001306/**
1307 * __netlink_ns_capable - General netlink message capability test
1308 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
1309 * @user_ns: The user namespace of the capability to use
1310 * @cap: The capability to use
1311 *
1312 * Test to see if the opener of the socket we received the message
1313 * from had when the netlink socket was created and the sender of the
1314 * message has has the capability @cap in the user namespace @user_ns.
1315 */
1316bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
1317 struct user_namespace *user_ns, int cap)
1318{
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001319 return ((nsp->flags & NETLINK_SKB_DST) ||
1320 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
1321 ns_capable(user_ns, cap);
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001322}
1323EXPORT_SYMBOL(__netlink_ns_capable);
1324
1325/**
1326 * netlink_ns_capable - General netlink message capability test
1327 * @skb: socket buffer holding a netlink command from userspace
1328 * @user_ns: The user namespace of the capability to use
1329 * @cap: The capability to use
1330 *
1331 * Test to see if the opener of the socket we received the message
1332 * from had when the netlink socket was created and the sender of the
1333 * message has has the capability @cap in the user namespace @user_ns.
1334 */
1335bool netlink_ns_capable(const struct sk_buff *skb,
1336 struct user_namespace *user_ns, int cap)
1337{
1338 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
1339}
1340EXPORT_SYMBOL(netlink_ns_capable);
1341
1342/**
1343 * netlink_capable - Netlink global message capability test
1344 * @skb: socket buffer holding a netlink command from userspace
1345 * @cap: The capability to use
1346 *
1347 * Test to see if the opener of the socket we received the message
1348 * from had when the netlink socket was created and the sender of the
1349 * message has has the capability @cap in all user namespaces.
1350 */
1351bool netlink_capable(const struct sk_buff *skb, int cap)
1352{
1353 return netlink_ns_capable(skb, &init_user_ns, cap);
1354}
1355EXPORT_SYMBOL(netlink_capable);
1356
1357/**
1358 * netlink_net_capable - Netlink network namespace message capability test
1359 * @skb: socket buffer holding a netlink command from userspace
1360 * @cap: The capability to use
1361 *
1362 * Test to see if the opener of the socket we received the message
1363 * from had when the netlink socket was created and the sender of the
1364 * message has has the capability @cap over the network namespace of
1365 * the socket we received the message from.
1366 */
1367bool netlink_net_capable(const struct sk_buff *skb, int cap)
1368{
1369 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
1370}
1371EXPORT_SYMBOL(netlink_net_capable);
1372
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001373static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001374{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001375 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001376 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001379static void
1380netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1381{
1382 struct netlink_sock *nlk = nlk_sk(sk);
1383
1384 if (nlk->subscriptions && !subscriptions)
1385 __sk_del_bind_node(sk);
1386 else if (!nlk->subscriptions && subscriptions)
1387 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1388 nlk->subscriptions = subscriptions;
1389}
1390
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001391static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001392{
1393 struct netlink_sock *nlk = nlk_sk(sk);
1394 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001395 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001396 int err = 0;
1397
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001398 netlink_table_grab();
1399
Patrick McHardy513c2502005-09-06 15:43:59 -07001400 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001401 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001402 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001403 goto out_unlock;
1404 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001405
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001406 if (nlk->ngroups >= groups)
1407 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001408
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001409 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1410 if (new_groups == NULL) {
1411 err = -ENOMEM;
1412 goto out_unlock;
1413 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001414 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001415 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1416
1417 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001418 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001419 out_unlock:
1420 netlink_table_ungrab();
1421 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001422}
1423
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001424static void netlink_unbind(int group, long unsigned int groups,
1425 struct netlink_sock *nlk)
1426{
1427 int undo;
1428
1429 if (!nlk->netlink_unbind)
1430 return;
1431
1432 for (undo = 0; undo < group; undo++)
1433 if (test_bit(group, &groups))
1434 nlk->netlink_unbind(undo);
1435}
1436
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001437static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1438 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
1440 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001441 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 struct netlink_sock *nlk = nlk_sk(sk);
1443 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1444 int err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001445 long unsigned int groups = nladdr->nl_groups;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001446
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001447 if (addr_len < sizeof(struct sockaddr_nl))
1448 return -EINVAL;
1449
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (nladdr->nl_family != AF_NETLINK)
1451 return -EINVAL;
1452
1453 /* Only superuser is allowed to listen multicasts */
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001454 if (groups) {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001455 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001456 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001457 err = netlink_realloc_groups(sk);
1458 if (err)
1459 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001460 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001462 if (nlk->portid)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001463 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EINVAL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001465
1466 if (nlk->netlink_bind && groups) {
1467 int group;
1468
1469 for (group = 0; group < nlk->ngroups; group++) {
1470 if (!test_bit(group, &groups))
1471 continue;
1472 err = nlk->netlink_bind(group);
1473 if (!err)
1474 continue;
1475 netlink_unbind(group, groups, nlk);
1476 return err;
1477 }
1478 }
1479
1480 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001482 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 netlink_autobind(sock);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001484 if (err) {
1485 netlink_unbind(nlk->ngroups - 1, groups, nlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 return err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001487 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
1489
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001490 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492
1493 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001494 netlink_update_subscriptions(sk, nlk->subscriptions +
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001495 hweight32(groups) -
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001496 hweight32(nlk->groups[0]));
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001497 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001498 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 netlink_table_ungrab();
1500
1501 return 0;
1502}
1503
1504static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1505 int alen, int flags)
1506{
1507 int err = 0;
1508 struct sock *sk = sock->sk;
1509 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001510 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Changli Gao6503d962010-03-31 22:58:26 +00001512 if (alen < sizeof(addr->sa_family))
1513 return -EINVAL;
1514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 if (addr->sa_family == AF_UNSPEC) {
1516 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001517 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001518 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 return 0;
1520 }
1521 if (addr->sa_family != AF_NETLINK)
1522 return -EINVAL;
1523
Mike Pecovnik46833a82014-02-24 21:11:16 +01001524 if ((nladdr->nl_groups || nladdr->nl_pid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001525 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 return -EPERM;
1527
Eric W. Biederman15e47302012-09-07 20:12:54 +00001528 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 err = netlink_autobind(sock);
1530
1531 if (err == 0) {
1532 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001533 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001534 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 }
1536
1537 return err;
1538}
1539
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001540static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1541 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542{
1543 struct sock *sk = sock->sk;
1544 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001545 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 nladdr->nl_family = AF_NETLINK;
1548 nladdr->nl_pad = 0;
1549 *addr_len = sizeof(*nladdr);
1550
1551 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001552 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001553 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001555 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001556 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 }
1558 return 0;
1559}
1560
Eric W. Biederman15e47302012-09-07 20:12:54 +00001561static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct sock *sock;
1564 struct netlink_sock *nlk;
1565
Eric W. Biederman15e47302012-09-07 20:12:54 +00001566 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 if (!sock)
1568 return ERR_PTR(-ECONNREFUSED);
1569
1570 /* Don't bother queuing skb if kernel socket has no input function */
1571 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001572 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001573 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 sock_put(sock);
1575 return ERR_PTR(-ECONNREFUSED);
1576 }
1577 return sock;
1578}
1579
1580struct sock *netlink_getsockbyfilp(struct file *filp)
1581{
Al Viro496ad9a2013-01-23 17:07:38 -05001582 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 struct sock *sock;
1584
1585 if (!S_ISSOCK(inode->i_mode))
1586 return ERR_PTR(-ENOTSOCK);
1587
1588 sock = SOCKET_I(inode)->sk;
1589 if (sock->sk_family != AF_NETLINK)
1590 return ERR_PTR(-EINVAL);
1591
1592 sock_hold(sock);
1593 return sock;
1594}
1595
Pablo Neira3a365152013-06-28 03:04:23 +02001596static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1597 int broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001598{
1599 struct sk_buff *skb;
1600 void *data;
1601
Pablo Neira3a365152013-06-28 03:04:23 +02001602 if (size <= NLMSG_GOODSIZE || broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001603 return alloc_skb(size, GFP_KERNEL);
1604
Pablo Neira3a365152013-06-28 03:04:23 +02001605 size = SKB_DATA_ALIGN(size) +
1606 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001607
1608 data = vmalloc(size);
1609 if (data == NULL)
Pablo Neira3a365152013-06-28 03:04:23 +02001610 return NULL;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001611
Pablo Neira3a365152013-06-28 03:04:23 +02001612 skb = build_skb(data, size);
1613 if (skb == NULL)
1614 vfree(data);
1615 else {
1616 skb->head_frag = 0;
1617 skb->destructor = netlink_skb_destructor;
1618 }
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001619
1620 return skb;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001621}
1622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623/*
1624 * Attach a skb to a netlink socket.
1625 * The caller must hold a reference to the destination socket. On error, the
1626 * reference is dropped. The skb is not send to the destination, just all
1627 * all error checks are performed and memory in the queue is reserved.
1628 * Return values:
1629 * < 0: error. skb freed, reference to sock dropped.
1630 * 0: continue
1631 * 1: repeat lookup - reference dropped while waiting for socket memory.
1632 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001633int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001634 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct netlink_sock *nlk;
1637
1638 nlk = nlk_sk(sk);
1639
Patrick McHardy5fd96122013-04-17 06:47:03 +00001640 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1641 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1642 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001644 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001645 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 netlink_overrun(sk);
1647 sock_put(sk);
1648 kfree_skb(skb);
1649 return -EAGAIN;
1650 }
1651
1652 __set_current_state(TASK_INTERRUPTIBLE);
1653 add_wait_queue(&nlk->wait, &wait);
1654
1655 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001656 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001658 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 __set_current_state(TASK_RUNNING);
1661 remove_wait_queue(&nlk->wait, &wait);
1662 sock_put(sk);
1663
1664 if (signal_pending(current)) {
1665 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001666 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
1668 return 1;
1669 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001670 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 return 0;
1672}
1673
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001674static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 int len = skb->len;
1677
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02001678 netlink_deliver_tap(skb);
1679
Patrick McHardyf9c22882013-04-17 06:47:04 +00001680#ifdef CONFIG_NETLINK_MMAP
1681 if (netlink_skb_is_mmaped(skb))
1682 netlink_queue_mmaped_skb(sk, skb);
1683 else if (netlink_rx_is_mmaped(sk))
1684 netlink_ring_set_copied(sk, skb);
1685 else
1686#endif /* CONFIG_NETLINK_MMAP */
1687 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -04001688 sk->sk_data_ready(sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001689 return len;
1690}
1691
1692int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1693{
1694 int len = __netlink_sendskb(sk, skb);
1695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 sock_put(sk);
1697 return len;
1698}
1699
1700void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1701{
1702 kfree_skb(skb);
1703 sock_put(sk);
1704}
1705
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001706static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707{
1708 int delta;
1709
Patrick McHardy1298ca42013-04-17 06:46:59 +00001710 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001711 if (netlink_skb_is_mmaped(skb))
1712 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001714 delta = skb->end - skb->tail;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001715 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 return skb;
1717
1718 if (skb_shared(skb)) {
1719 struct sk_buff *nskb = skb_clone(skb, allocation);
1720 if (!nskb)
1721 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001722 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 skb = nskb;
1724 }
1725
1726 if (!pskb_expand_head(skb, 0, -delta, allocation))
1727 skb->truesize -= delta;
1728
1729 return skb;
1730}
1731
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001732static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1733 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001734{
1735 int ret;
1736 struct netlink_sock *nlk = nlk_sk(sk);
1737
1738 ret = -ECONNREFUSED;
1739 if (nlk->netlink_rcv != NULL) {
1740 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001741 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001742 NETLINK_CB(skb).sk = ssk;
Daniel Borkmann73bfd372013-12-23 14:35:55 +01001743 netlink_deliver_tap_kernel(sk, ssk, skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001744 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001745 consume_skb(skb);
1746 } else {
1747 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001748 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001749 sock_put(sk);
1750 return ret;
1751}
1752
1753int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001754 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755{
1756 struct sock *sk;
1757 int err;
1758 long timeo;
1759
1760 skb = netlink_trim(skb, gfp_any());
1761
1762 timeo = sock_sndtimeo(ssk, nonblock);
1763retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001764 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (IS_ERR(sk)) {
1766 kfree_skb(skb);
1767 return PTR_ERR(sk);
1768 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001769 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001770 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001771
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001772 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001773 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001774 kfree_skb(skb);
1775 sock_put(sk);
1776 return err;
1777 }
1778
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001779 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 if (err == 1)
1781 goto retry;
1782 if (err)
1783 return err;
1784
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001785 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001787EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Patrick McHardyf9c22882013-04-17 06:47:04 +00001789struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1790 u32 dst_portid, gfp_t gfp_mask)
1791{
1792#ifdef CONFIG_NETLINK_MMAP
1793 struct sock *sk = NULL;
1794 struct sk_buff *skb;
1795 struct netlink_ring *ring;
1796 struct nl_mmap_hdr *hdr;
1797 unsigned int maxlen;
1798
1799 sk = netlink_getsockbyportid(ssk, dst_portid);
1800 if (IS_ERR(sk))
1801 goto out;
1802
1803 ring = &nlk_sk(sk)->rx_ring;
1804 /* fast-path without atomic ops for common case: non-mmaped receiver */
1805 if (ring->pg_vec == NULL)
1806 goto out_put;
1807
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001808 if (ring->frame_size - NL_MMAP_HDRLEN < size)
1809 goto out_put;
1810
Patrick McHardyf9c22882013-04-17 06:47:04 +00001811 skb = alloc_skb_head(gfp_mask);
1812 if (skb == NULL)
1813 goto err1;
1814
1815 spin_lock_bh(&sk->sk_receive_queue.lock);
1816 /* check again under lock */
1817 if (ring->pg_vec == NULL)
1818 goto out_free;
1819
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001820 /* check again under lock */
Patrick McHardyf9c22882013-04-17 06:47:04 +00001821 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1822 if (maxlen < size)
1823 goto out_free;
1824
1825 netlink_forward_ring(ring);
1826 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1827 if (hdr == NULL)
1828 goto err2;
1829 netlink_ring_setup_skb(skb, sk, ring, hdr);
1830 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1831 atomic_inc(&ring->pending);
1832 netlink_increment_head(ring);
1833
1834 spin_unlock_bh(&sk->sk_receive_queue.lock);
1835 return skb;
1836
1837err2:
1838 kfree_skb(skb);
1839 spin_unlock_bh(&sk->sk_receive_queue.lock);
Patrick McHardycd1df522013-04-17 06:47:05 +00001840 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +00001841err1:
1842 sock_put(sk);
1843 return NULL;
1844
1845out_free:
1846 kfree_skb(skb);
1847 spin_unlock_bh(&sk->sk_receive_queue.lock);
1848out_put:
1849 sock_put(sk);
1850out:
1851#endif
1852 return alloc_skb(size, gfp_mask);
1853}
1854EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1855
Patrick McHardy4277a082006-03-20 18:52:01 -08001856int netlink_has_listeners(struct sock *sk, unsigned int group)
1857{
1858 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001859 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001860
Denis V. Lunevaed81562007-10-10 21:14:32 -07001861 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001862
1863 rcu_read_lock();
1864 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1865
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001866 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001867 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001868
1869 rcu_read_unlock();
1870
Patrick McHardy4277a082006-03-20 18:52:01 -08001871 return res;
1872}
1873EXPORT_SYMBOL_GPL(netlink_has_listeners);
1874
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001875static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876{
1877 struct netlink_sock *nlk = nlk_sk(sk);
1878
1879 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001880 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001881 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001882 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001883 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885 return -1;
1886}
1887
1888struct netlink_broadcast_data {
1889 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001890 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001891 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 u32 group;
1893 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001894 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int congested;
1896 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001897 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001899 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1900 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901};
1902
Rami Rosen46c95212014-07-01 21:17:35 +03001903static void do_one_broadcast(struct sock *sk,
1904 struct netlink_broadcast_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 struct netlink_sock *nlk = nlk_sk(sk);
1907 int val;
1908
1909 if (p->exclude_sk == sk)
Rami Rosen46c95212014-07-01 21:17:35 +03001910 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
Eric W. Biederman15e47302012-09-07 20:12:54 +00001912 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001913 !test_bit(p->group - 1, nlk->groups))
Rami Rosen46c95212014-07-01 21:17:35 +03001914 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001916 if (!net_eq(sock_net(sk), p->net))
Rami Rosen46c95212014-07-01 21:17:35 +03001917 return;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 if (p->failure) {
1920 netlink_overrun(sk);
Rami Rosen46c95212014-07-01 21:17:35 +03001921 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 }
1923
1924 sock_hold(sk);
1925 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001926 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 p->skb2 = skb_clone(p->skb, p->allocation);
1928 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001929 p->skb2 = skb_get(p->skb);
1930 /*
1931 * skb ownership may have been set when
1932 * delivered to a previous socket.
1933 */
1934 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 }
1936 }
1937 if (p->skb2 == NULL) {
1938 netlink_overrun(sk);
1939 /* Clone failed. Notify ALL listeners. */
1940 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001941 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1942 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001943 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1944 kfree_skb(p->skb2);
1945 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001946 } else if (sk_filter(sk, p->skb2)) {
1947 kfree_skb(p->skb2);
1948 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1950 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001951 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1952 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 } else {
1954 p->congested |= val;
1955 p->delivered = 1;
1956 p->skb2 = NULL;
1957 }
1958 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959}
1960
Eric W. Biederman15e47302012-09-07 20:12:54 +00001961int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001962 u32 group, gfp_t allocation,
1963 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1964 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001966 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 struct sock *sk;
1969
1970 skb = netlink_trim(skb, allocation);
1971
1972 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001973 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001974 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 info.group = group;
1976 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001977 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 info.congested = 0;
1979 info.delivered = 0;
1980 info.allocation = allocation;
1981 info.skb = skb;
1982 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001983 info.tx_filter = filter;
1984 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 /* While we sleep in clone, do not allow to change socket list */
1987
1988 netlink_lock_table();
1989
Sasha Levinb67bfe02013-02-27 17:06:00 -08001990 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 do_one_broadcast(sk, &info);
1992
Neil Horman70d4bf62010-07-20 06:45:56 +00001993 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 netlink_unlock_table();
1996
Neil Horman70d4bf62010-07-20 06:45:56 +00001997 if (info.delivery_failure) {
1998 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001999 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002000 }
2001 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (info.delivered) {
2004 if (info.congested && (allocation & __GFP_WAIT))
2005 yield();
2006 return 0;
2007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return -ESRCH;
2009}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002010EXPORT_SYMBOL(netlink_broadcast_filtered);
2011
Eric W. Biederman15e47302012-09-07 20:12:54 +00002012int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002013 u32 group, gfp_t allocation)
2014{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002015 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002016 NULL, NULL);
2017}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002018EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020struct netlink_set_err_data {
2021 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002022 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 u32 group;
2024 int code;
2025};
2026
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00002027static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028{
2029 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002030 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031
2032 if (sk == p->exclude_sk)
2033 goto out;
2034
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002035 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002036 goto out;
2037
Eric W. Biederman15e47302012-09-07 20:12:54 +00002038 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07002039 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 goto out;
2041
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002042 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
2043 ret = 1;
2044 goto out;
2045 }
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 sk->sk_err = p->code;
2048 sk->sk_error_report(sk);
2049out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002050 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051}
2052
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002053/**
2054 * netlink_set_err - report error to broadcast listeners
2055 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00002056 * @portid: the PORTID of a process that we want to skip (if any)
Johannes Berg840e93f22013-11-19 10:35:40 +01002057 * @group: the broadcast group that will notice the error
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002058 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002059 *
2060 * This function returns the number of broadcast listeners that have set the
2061 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002062 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002063int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002067 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
2069 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002070 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002072 /* sk->sk_err wants a positive error value */
2073 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075 read_lock(&nl_table_lock);
2076
Sasha Levinb67bfe02013-02-27 17:06:00 -08002077 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002078 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002081 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01002083EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
Johannes Berg84659eb2007-07-18 15:47:05 -07002085/* must be called with netlink table grabbed */
2086static void netlink_update_socket_mc(struct netlink_sock *nlk,
2087 unsigned int group,
2088 int is_new)
2089{
2090 int old, new = !!is_new, subscriptions;
2091
2092 old = test_bit(group - 1, nlk->groups);
2093 subscriptions = nlk->subscriptions - old + new;
2094 if (new)
2095 __set_bit(group - 1, nlk->groups);
2096 else
2097 __clear_bit(group - 1, nlk->groups);
2098 netlink_update_subscriptions(&nlk->sk, subscriptions);
2099 netlink_update_listeners(&nlk->sk);
2100}
2101
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002102static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002103 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002104{
2105 struct sock *sk = sock->sk;
2106 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07002107 unsigned int val = 0;
2108 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002109
2110 if (level != SOL_NETLINK)
2111 return -ENOPROTOOPT;
2112
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002113 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
2114 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07002115 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002116 return -EFAULT;
2117
2118 switch (optname) {
2119 case NETLINK_PKTINFO:
2120 if (val)
2121 nlk->flags |= NETLINK_RECV_PKTINFO;
2122 else
2123 nlk->flags &= ~NETLINK_RECV_PKTINFO;
2124 err = 0;
2125 break;
2126 case NETLINK_ADD_MEMBERSHIP:
2127 case NETLINK_DROP_MEMBERSHIP: {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002128 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002129 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002130 err = netlink_realloc_groups(sk);
2131 if (err)
2132 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002133 if (!val || val - 1 >= nlk->ngroups)
2134 return -EINVAL;
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002135 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
Richard Guy Briggs4f520902014-04-22 21:31:54 -04002136 err = nlk->netlink_bind(val);
2137 if (err)
2138 return err;
2139 }
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002140 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07002141 netlink_update_socket_mc(nlk, val,
2142 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002143 netlink_table_ungrab();
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002144 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
2145 nlk->netlink_unbind(val);
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00002146
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002147 err = 0;
2148 break;
2149 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002150 case NETLINK_BROADCAST_ERROR:
2151 if (val)
2152 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
2153 else
2154 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
2155 err = 0;
2156 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002157 case NETLINK_NO_ENOBUFS:
2158 if (val) {
2159 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00002160 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002161 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00002162 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002163 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002164 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002165 err = 0;
2166 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002167#ifdef CONFIG_NETLINK_MMAP
2168 case NETLINK_RX_RING:
2169 case NETLINK_TX_RING: {
2170 struct nl_mmap_req req;
2171
2172 /* Rings might consume more memory than queue limits, require
2173 * CAP_NET_ADMIN.
2174 */
2175 if (!capable(CAP_NET_ADMIN))
2176 return -EPERM;
2177 if (optlen < sizeof(req))
2178 return -EINVAL;
2179 if (copy_from_user(&req, optval, sizeof(req)))
2180 return -EFAULT;
2181 err = netlink_set_ring(sk, &req, false,
2182 optname == NETLINK_TX_RING);
2183 break;
2184 }
2185#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002186 default:
2187 err = -ENOPROTOOPT;
2188 }
2189 return err;
2190}
2191
2192static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002193 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002194{
2195 struct sock *sk = sock->sk;
2196 struct netlink_sock *nlk = nlk_sk(sk);
2197 int len, val, err;
2198
2199 if (level != SOL_NETLINK)
2200 return -ENOPROTOOPT;
2201
2202 if (get_user(len, optlen))
2203 return -EFAULT;
2204 if (len < 0)
2205 return -EINVAL;
2206
2207 switch (optname) {
2208 case NETLINK_PKTINFO:
2209 if (len < sizeof(int))
2210 return -EINVAL;
2211 len = sizeof(int);
2212 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08002213 if (put_user(len, optlen) ||
2214 put_user(val, optval))
2215 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002216 err = 0;
2217 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002218 case NETLINK_BROADCAST_ERROR:
2219 if (len < sizeof(int))
2220 return -EINVAL;
2221 len = sizeof(int);
2222 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
2223 if (put_user(len, optlen) ||
2224 put_user(val, optval))
2225 return -EFAULT;
2226 err = 0;
2227 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002228 case NETLINK_NO_ENOBUFS:
2229 if (len < sizeof(int))
2230 return -EINVAL;
2231 len = sizeof(int);
2232 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
2233 if (put_user(len, optlen) ||
2234 put_user(val, optval))
2235 return -EFAULT;
2236 err = 0;
2237 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002238 default:
2239 err = -ENOPROTOOPT;
2240 }
2241 return err;
2242}
2243
2244static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2245{
2246 struct nl_pktinfo info;
2247
2248 info.group = NETLINK_CB(skb).dst_group;
2249 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2250}
2251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2253 struct msghdr *msg, size_t len)
2254{
2255 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2256 struct sock *sk = sock->sk;
2257 struct netlink_sock *nlk = nlk_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002258 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002259 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002260 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 struct sk_buff *skb;
2262 int err;
2263 struct scm_cookie scm;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002264 u32 netlink_skb_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
2266 if (msg->msg_flags&MSG_OOB)
2267 return -EOPNOTSUPP;
2268
Eric Dumazet16e57262011-09-19 05:52:27 +00002269 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00002271
Eric Dumazete0e3cea2012-08-21 06:21:17 +00002272 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (err < 0)
2274 return err;
2275
2276 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002277 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002279 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002280 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002281 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002282 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002283 if ((dst_group || dst_portid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002284 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002285 goto out;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002286 netlink_skb_flags |= NETLINK_SKB_DST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002288 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002289 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 }
2291
Eric W. Biederman15e47302012-09-07 20:12:54 +00002292 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 err = netlink_autobind(sock);
2294 if (err)
2295 goto out;
2296 }
2297
Patrick McHardy5fd96122013-04-17 06:47:03 +00002298 if (netlink_tx_is_mmaped(sk) &&
2299 msg->msg_iov->iov_base == NULL) {
2300 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2301 siocb);
2302 goto out;
2303 }
2304
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 err = -EMSGSIZE;
2306 if (len > sk->sk_sndbuf - 32)
2307 goto out;
2308 err = -ENOBUFS;
Pablo Neira3a365152013-06-28 03:04:23 +02002309 skb = netlink_alloc_large_skb(len, dst_group);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002310 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 goto out;
2312
Eric W. Biederman15e47302012-09-07 20:12:54 +00002313 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002314 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00002315 NETLINK_CB(skb).creds = siocb->scm->creds;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002316 NETLINK_CB(skb).flags = netlink_skb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002319 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 kfree_skb(skb);
2321 goto out;
2322 }
2323
2324 err = security_netlink_send(sk, skb);
2325 if (err) {
2326 kfree_skb(skb);
2327 goto out;
2328 }
2329
Patrick McHardyd629b832005-08-14 19:27:50 -07002330 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002332 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002334 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
2336out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002337 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return err;
2339}
2340
2341static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2342 struct msghdr *msg, size_t len,
2343 int flags)
2344{
2345 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2346 struct scm_cookie scm;
2347 struct sock *sk = sock->sk;
2348 struct netlink_sock *nlk = nlk_sk(sk);
2349 int noblock = flags&MSG_DONTWAIT;
2350 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00002351 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002352 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353
2354 if (flags&MSG_OOB)
2355 return -EOPNOTSUPP;
2356
2357 copied = 0;
2358
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002359 skb = skb_recv_datagram(sk, flags, noblock, &err);
2360 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 goto out;
2362
Johannes Berg68d6ac62010-08-15 21:20:44 +00002363 data_skb = skb;
2364
Johannes Berg1dacc762009-07-01 11:26:02 +00002365#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2366 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002367 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002368 * If this skb has a frag_list, then here that means that we
2369 * will have to use the frag_list skb's data for compat tasks
2370 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002371 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002372 * If we need to send the compat skb, assign it to the
2373 * 'data_skb' variable so that it will be used below for data
2374 * copying. We keep 'skb' for everything else, including
2375 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002376 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002377 if (flags & MSG_CMSG_COMPAT)
2378 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002379 }
2380#endif
2381
Eric Dumazet9063e212014-03-07 12:02:33 -08002382 /* Record the max length of recvmsg() calls for future allocations */
2383 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
2384 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
2385 16384);
2386
Johannes Berg68d6ac62010-08-15 21:20:44 +00002387 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 if (len < copied) {
2389 msg->msg_flags |= MSG_TRUNC;
2390 copied = len;
2391 }
2392
Johannes Berg68d6ac62010-08-15 21:20:44 +00002393 skb_reset_transport_header(data_skb);
2394 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
2396 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002397 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 addr->nl_family = AF_NETLINK;
2399 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002400 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002401 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 msg->msg_namelen = sizeof(*addr);
2403 }
2404
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002405 if (nlk->flags & NETLINK_RECV_PKTINFO)
2406 netlink_cmsg_recv_pktinfo(msg, skb);
2407
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 if (NULL == siocb->scm) {
2409 memset(&scm, 0, sizeof(scm));
2410 siocb->scm = &scm;
2411 }
2412 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002413 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002414 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 skb_free_datagram(sk, skb);
2417
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002418 if (nlk->cb_running &&
2419 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
Andrey Vaginb44d2112011-02-21 02:40:47 +00002420 ret = netlink_dump(sk);
2421 if (ret) {
Ben Pfaffac30ef82014-07-09 10:31:22 -07002422 sk->sk_err = -ret;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002423 sk->sk_error_report(sk);
2424 }
2425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
2427 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428out:
2429 netlink_rcv_wake(sk);
2430 return err ? : copied;
2431}
2432
David S. Miller676d2362014-04-11 16:15:36 -04002433static void netlink_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002435 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436}
2437
2438/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002439 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 * complete set of kernel non-blocking support for message
2441 * queueing.
2442 */
2443
2444struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002445__netlink_kernel_create(struct net *net, int unit, struct module *module,
2446 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448 struct socket *sock;
2449 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002450 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002451 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002452 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2453 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002455 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002457 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 return NULL;
2459
2460 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2461 return NULL;
2462
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002463 /*
2464 * We have to just have a reference on the net from sk, but don't
2465 * get_net it. Besides, we cannot get and then put the net here.
2466 * So we create one inside init_net and the move it to net.
2467 */
2468
2469 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2470 goto out_sock_release_nosk;
2471
2472 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002473 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002474
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002475 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002476 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002477 else
2478 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002479
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002480 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002481 if (!listeners)
2482 goto out_sock_release;
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002485 if (cfg && cfg->input)
2486 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002488 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002489 goto out_sock_release;
2490
2491 nlk = nlk_sk(sk);
2492 nlk->flags |= NETLINK_KERNEL_SOCKET;
2493
2494 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002495 if (!nl_table[unit].registered) {
2496 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002497 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002498 nl_table[unit].cb_mutex = cb_mutex;
2499 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002500 if (cfg) {
2501 nl_table[unit].bind = cfg->bind;
2502 nl_table[unit].flags = cfg->flags;
Gao fengda12c902013-06-06 14:49:11 +08002503 if (cfg->compare)
2504 nl_table[unit].compare = cfg->compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002505 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002506 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002507 } else {
2508 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002509 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002510 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002511 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002512 return sk;
2513
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002514out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002515 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002516 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002517 return NULL;
2518
2519out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002520 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002521 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002523EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002524
2525void
2526netlink_kernel_release(struct sock *sk)
2527{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002528 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002529}
2530EXPORT_SYMBOL(netlink_kernel_release);
2531
Johannes Bergd136f1b2009-09-12 03:03:15 +00002532int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002533{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002534 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002535 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002536
2537 if (groups < 32)
2538 groups = 32;
2539
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002540 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002541 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2542 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002543 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002544 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002545 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2546 rcu_assign_pointer(tbl->listeners, new);
2547
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002548 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002549 }
2550 tbl->groups = groups;
2551
Johannes Bergd136f1b2009-09-12 03:03:15 +00002552 return 0;
2553}
2554
2555/**
2556 * netlink_change_ngroups - change number of multicast groups
2557 *
2558 * This changes the number of multicast groups that are available
2559 * on a certain netlink family. Note that it is not possible to
2560 * change the number of groups to below 32. Also note that it does
2561 * not implicitly call netlink_clear_multicast_users() when the
2562 * number of groups is reduced.
2563 *
2564 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2565 * @groups: The new number of groups.
2566 */
2567int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2568{
2569 int err;
2570
2571 netlink_table_grab();
2572 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002573 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002574
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002575 return err;
2576}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002577
Johannes Bergb8273572009-09-24 15:44:05 -07002578void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2579{
2580 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002581 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2582
Sasha Levinb67bfe02013-02-27 17:06:00 -08002583 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002584 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2585}
2586
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002587struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002588__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002589{
2590 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002591 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002592
Wang Yufen23b45672014-02-17 16:53:32 +08002593 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002594 nlh->nlmsg_type = type;
2595 nlh->nlmsg_len = size;
2596 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002597 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002598 nlh->nlmsg_seq = seq;
2599 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002600 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002601 return nlh;
2602}
2603EXPORT_SYMBOL(__nlmsg_put);
2604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605/*
2606 * It looks a bit ugly.
2607 * It would be better to create kernel thread.
2608 */
2609
2610static int netlink_dump(struct sock *sk)
2611{
2612 struct netlink_sock *nlk = nlk_sk(sk);
2613 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002614 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002616 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002617 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002619 mutex_lock(nlk->cb_mutex);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002620 if (!nlk->cb_running) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002621 err = -EINVAL;
2622 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 }
2624
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002625 cb = &nlk->cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002626 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2627
Patrick McHardyf9c22882013-04-17 06:47:04 +00002628 if (!netlink_rx_is_mmaped(sk) &&
2629 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2630 goto errout_skb;
Eric Dumazet9063e212014-03-07 12:02:33 -08002631
2632 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2633 * required, but it makes sense to _attempt_ a 16K bytes allocation
2634 * to reduce number of system calls on dump operations, if user
2635 * ever provided a big enough buffer.
2636 */
2637 if (alloc_size < nlk->max_recvmsg_len) {
2638 skb = netlink_alloc_skb(sk,
2639 nlk->max_recvmsg_len,
2640 nlk->portid,
2641 GFP_KERNEL |
2642 __GFP_NOWARN |
2643 __GFP_NORETRY);
2644 /* available room should be exact amount to avoid MSG_TRUNC */
2645 if (skb)
2646 skb_reserve(skb, skb_tailroom(skb) -
2647 nlk->max_recvmsg_len);
2648 }
2649 if (!skb)
2650 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
2651 GFP_KERNEL);
Greg Rosec7ac8672011-06-10 01:27:09 +00002652 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002653 goto errout_skb;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002654 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002655
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 len = cb->dump(skb, cb);
2657
2658 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002659 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002660
2661 if (sk_filter(sk, skb))
2662 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002663 else
2664 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 return 0;
2666 }
2667
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002668 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2669 if (!nlh)
2670 goto errout_skb;
2671
Johannes Berg670dc282011-06-20 13:40:46 +02002672 nl_dump_check_consistent(cb, nlh);
2673
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002674 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2675
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002676 if (sk_filter(sk, skb))
2677 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002678 else
2679 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Thomas Grafa8f74b22005-11-10 02:25:52 +01002681 if (cb->done)
2682 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002684 nlk->cb_running = false;
2685 mutex_unlock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002686 module_put(cb->module);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002687 consume_skb(cb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002689
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002690errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002691 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002692 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002693 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694}
2695
Gao feng6dc878a2012-10-04 20:15:48 +00002696int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2697 const struct nlmsghdr *nlh,
2698 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct netlink_callback *cb;
2701 struct sock *sk;
2702 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002703 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Patrick McHardyf9c22882013-04-17 06:47:04 +00002705 /* Memory mapped dump requests need to be copied to avoid looping
2706 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2707 * a reference to the skb.
2708 */
2709 if (netlink_skb_is_mmaped(skb)) {
2710 skb = skb_copy(skb, GFP_KERNEL);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002711 if (skb == NULL)
Patrick McHardyf9c22882013-04-17 06:47:04 +00002712 return -ENOBUFS;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002713 } else
2714 atomic_inc(&skb->users);
2715
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002716 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2717 if (sk == NULL) {
2718 ret = -ECONNREFUSED;
2719 goto error_free;
2720 }
2721
2722 nlk = nlk_sk(sk);
2723 mutex_lock(nlk->cb_mutex);
2724 /* A dump is in progress... */
2725 if (nlk->cb_running) {
2726 ret = -EBUSY;
2727 goto error_unlock;
2728 }
2729 /* add reference of module which cb->dump belongs to */
2730 if (!try_module_get(control->module)) {
2731 ret = -EPROTONOSUPPORT;
2732 goto error_unlock;
2733 }
2734
2735 cb = &nlk->cb;
2736 memset(cb, 0, sizeof(*cb));
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002737 cb->dump = control->dump;
2738 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002740 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002741 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002742 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 cb->skb = skb;
2744
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002745 nlk->cb_running = true;
Gao feng6dc878a2012-10-04 20:15:48 +00002746
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002747 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Andrey Vaginb44d2112011-02-21 02:40:47 +00002749 ret = netlink_dump(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002751
Andrey Vaginb44d2112011-02-21 02:40:47 +00002752 if (ret)
2753 return ret;
2754
Denis V. Lunev5c582982007-10-23 20:29:25 -07002755 /* We successfully started a dump, by returning -EINTR we
2756 * signal not to send ACK even if it was requested.
2757 */
2758 return -EINTR;
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002759
2760error_unlock:
2761 sock_put(sk);
2762 mutex_unlock(nlk->cb_mutex);
2763error_free:
2764 kfree_skb(skb);
2765 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766}
Gao feng6dc878a2012-10-04 20:15:48 +00002767EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2770{
2771 struct sk_buff *skb;
2772 struct nlmsghdr *rep;
2773 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002774 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Thomas Graf339bf982006-11-10 14:10:15 -08002776 /* error messages get the original request appened */
2777 if (err)
2778 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Patrick McHardyf9c22882013-04-17 06:47:04 +00002780 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2781 NETLINK_CB(in_skb).portid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 if (!skb) {
2783 struct sock *sk;
2784
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002785 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002786 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002787 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 if (sk) {
2789 sk->sk_err = ENOBUFS;
2790 sk->sk_error_report(sk);
2791 sock_put(sk);
2792 }
2793 return;
2794 }
2795
Eric W. Biederman15e47302012-09-07 20:12:54 +00002796 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002797 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002798 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002800 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002801 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002803EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002805int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002806 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002807{
Thomas Graf82ace472005-11-10 02:25:53 +01002808 struct nlmsghdr *nlh;
2809 int err;
2810
2811 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002812 int msglen;
2813
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002814 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002815 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002816
Martin Murrayad8e4b72006-01-10 13:02:29 -08002817 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002818 return 0;
2819
Thomas Grafd35b6852007-03-22 23:28:46 -07002820 /* Only requests are handled by the kernel */
2821 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002822 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002823
Thomas Graf45e7ae72007-03-22 23:29:10 -07002824 /* Skip control messages */
2825 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002826 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002827
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002828 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002829 if (err == -EINTR)
2830 goto skip;
2831
2832ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002833 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002834 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002835
Denis V. Lunev5c582982007-10-23 20:29:25 -07002836skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002837 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002838 if (msglen > skb->len)
2839 msglen = skb->len;
2840 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002841 }
2842
2843 return 0;
2844}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002845EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002846
2847/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002848 * nlmsg_notify - send a notification netlink message
2849 * @sk: netlink socket to use
2850 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002851 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002852 * @group: destination multicast group or 0
2853 * @report: 1 to report back, 0 to disable
2854 * @flags: allocation flags
2855 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002856int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002857 unsigned int group, int report, gfp_t flags)
2858{
2859 int err = 0;
2860
2861 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002862 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002863
2864 if (report) {
2865 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002866 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002867 }
2868
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002869 /* errors reported via destination sk->sk_err, but propagate
2870 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002871 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002872 }
2873
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002874 if (report) {
2875 int err2;
2876
Eric W. Biederman15e47302012-09-07 20:12:54 +00002877 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002878 if (!err || err == -ESRCH)
2879 err = err2;
2880 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002881
2882 return err;
2883}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002884EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002885
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886#ifdef CONFIG_PROC_FS
2887struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002888 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 int link;
2890 int hash_idx;
2891};
2892
2893static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2894{
2895 struct nl_seq_iter *iter = seq->private;
2896 int i, j;
Thomas Grafe3416942014-08-02 11:47:45 +02002897 struct netlink_sock *nlk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 loff_t off = 0;
2900
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002901 for (i = 0; i < MAX_LINKS; i++) {
Thomas Grafe3416942014-08-02 11:47:45 +02002902 struct rhashtable *ht = &nl_table[i].hash;
Eric Dumazet67a24ac2014-08-05 07:50:07 +02002903 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904
Thomas Grafe3416942014-08-02 11:47:45 +02002905 for (j = 0; j < tbl->size; j++) {
2906 rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) {
2907 s = (struct sock *)nlk;
2908
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002909 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002910 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 if (off == pos) {
2912 iter->link = i;
2913 iter->hash_idx = j;
2914 return s;
2915 }
2916 ++off;
2917 }
2918 }
2919 }
2920 return NULL;
2921}
2922
2923static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
2924{
Thomas Grafe3416942014-08-02 11:47:45 +02002925 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2927}
2928
2929static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2930{
Thomas Grafe3416942014-08-02 11:47:45 +02002931 struct netlink_sock *nlk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 struct nl_seq_iter *iter;
Gao fengda12c902013-06-06 14:49:11 +08002933 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 int i, j;
2935
2936 ++*pos;
2937
2938 if (v == SEQ_START_TOKEN)
2939 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002940
Gao fengda12c902013-06-06 14:49:11 +08002941 net = seq_file_net(seq);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002942 iter = seq->private;
Thomas Grafe3416942014-08-02 11:47:45 +02002943 nlk = v;
2944
2945 rht_for_each_entry_rcu(nlk, nlk->node.next, node)
2946 if (net_eq(sock_net((struct sock *)nlk), net))
2947 return nlk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 i = iter->link;
2950 j = iter->hash_idx + 1;
2951
2952 do {
Thomas Grafe3416942014-08-02 11:47:45 +02002953 struct rhashtable *ht = &nl_table[i].hash;
Eric Dumazet67a24ac2014-08-05 07:50:07 +02002954 const struct bucket_table *tbl = rht_dereference_rcu(ht->tbl, ht);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
Thomas Grafe3416942014-08-02 11:47:45 +02002956 for (; j < tbl->size; j++) {
2957 rht_for_each_entry_rcu(nlk, tbl->buckets[j], node) {
2958 if (net_eq(sock_net((struct sock *)nlk), net)) {
2959 iter->link = i;
2960 iter->hash_idx = j;
2961 return nlk;
2962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 }
2964 }
2965
2966 j = 0;
2967 } while (++i < MAX_LINKS);
2968
2969 return NULL;
2970}
2971
2972static void netlink_seq_stop(struct seq_file *seq, void *v)
2973{
Thomas Grafe3416942014-08-02 11:47:45 +02002974 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975}
2976
2977
2978static int netlink_seq_show(struct seq_file *seq, void *v)
2979{
Eric Dumazet658cb352012-04-22 21:30:21 +00002980 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 seq_puts(seq,
2982 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002983 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002984 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 struct sock *s = v;
2986 struct netlink_sock *nlk = nlk_sk(s);
2987
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002988 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 s,
2990 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002991 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002992 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002993 sk_rmem_alloc_get(s),
2994 sk_wmem_alloc_get(s),
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002995 nlk->cb_running,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002996 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002997 atomic_read(&s->sk_drops),
2998 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 );
3000
3001 }
3002 return 0;
3003}
3004
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003005static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 .start = netlink_seq_start,
3007 .next = netlink_seq_next,
3008 .stop = netlink_seq_stop,
3009 .show = netlink_seq_show,
3010};
3011
3012
3013static int netlink_seq_open(struct inode *inode, struct file *file)
3014{
Denis V. Luneve372c412007-11-19 22:31:54 -08003015 return seq_open_net(inode, file, &netlink_seq_ops,
3016 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003017}
3018
Arjan van de Venda7071d2007-02-12 00:55:36 -08003019static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 .owner = THIS_MODULE,
3021 .open = netlink_seq_open,
3022 .read = seq_read,
3023 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003024 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025};
3026
3027#endif
3028
3029int netlink_register_notifier(struct notifier_block *nb)
3030{
Alan Sterne041c682006-03-27 01:16:30 -08003031 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003033EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034
3035int netlink_unregister_notifier(struct notifier_block *nb)
3036{
Alan Sterne041c682006-03-27 01:16:30 -08003037 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003039EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003040
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003041static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 .family = PF_NETLINK,
3043 .owner = THIS_MODULE,
3044 .release = netlink_release,
3045 .bind = netlink_bind,
3046 .connect = netlink_connect,
3047 .socketpair = sock_no_socketpair,
3048 .accept = sock_no_accept,
3049 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00003050 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 .ioctl = sock_no_ioctl,
3052 .listen = sock_no_listen,
3053 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07003054 .setsockopt = netlink_setsockopt,
3055 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056 .sendmsg = netlink_sendmsg,
3057 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00003058 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 .sendpage = sock_no_sendpage,
3060};
3061
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003062static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 .family = PF_NETLINK,
3064 .create = netlink_create,
3065 .owner = THIS_MODULE, /* for consistency 8) */
3066};
3067
Pavel Emelyanov46650792007-10-08 20:38:39 -07003068static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003069{
3070#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003071 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003072 return -ENOMEM;
3073#endif
3074 return 0;
3075}
3076
Pavel Emelyanov46650792007-10-08 20:38:39 -07003077static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003078{
3079#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003080 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003081#endif
3082}
3083
David S. Millerb963ea82010-08-30 19:08:01 -07003084static void __init netlink_add_usersock_entry(void)
3085{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003086 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07003087 int groups = 32;
3088
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003089 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07003090 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003091 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07003092
3093 netlink_table_grab();
3094
3095 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003096 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07003097 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
3098 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003099 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07003100
3101 netlink_table_ungrab();
3102}
3103
Denis V. Lunev022cbae2007-11-13 03:23:50 -08003104static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003105 .init = netlink_net_init,
3106 .exit = netlink_net_exit,
3107};
3108
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109static int __init netlink_proto_init(void)
3110{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112 int err = proto_register(&netlink_proto, 0);
Thomas Grafe3416942014-08-02 11:47:45 +02003113 struct rhashtable_params ht_params = {
3114 .head_offset = offsetof(struct netlink_sock, node),
3115 .key_offset = offsetof(struct netlink_sock, portid),
3116 .key_len = sizeof(u32), /* portid */
3117 .hashfn = arch_fast_hash,
3118 .max_shift = 16, /* 64K */
3119 .grow_decision = rht_grow_above_75,
3120 .shrink_decision = rht_shrink_below_30,
3121 .mutex_is_held = lockdep_nl_sk_hash_is_held,
3122 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123
3124 if (err != 0)
3125 goto out;
3126
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00003127 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07003129 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003130 if (!nl_table)
3131 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 for (i = 0; i < MAX_LINKS; i++) {
Thomas Grafe3416942014-08-02 11:47:45 +02003134 if (rhashtable_init(&nl_table[i].hash, &ht_params) < 0) {
3135 while (--i > 0)
3136 rhashtable_destroy(&nl_table[i].hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003138 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 }
3141
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02003142 INIT_LIST_HEAD(&netlink_tap_all);
3143
David S. Millerb963ea82010-08-30 19:08:01 -07003144 netlink_add_usersock_entry();
3145
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003147 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003148 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149 rtnetlink_init();
3150out:
3151 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003152panic:
3153 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154}
3155
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156core_initcall(netlink_proto_init);