blob: 1b38f7fe12f1f8e09006752333ca7ef5026294c7 [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>
Patrick McHardy9652e932013-04-17 06:47:02 +000061#include <asm/cacheflush.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010062
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020063#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/sock.h>
65#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010066#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Andrey Vagin0f29c762013-03-21 20:33:47 +040068#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Eric Dumazet5c398dc2010-10-24 04:27:10 +000070struct listeners {
71 struct rcu_head rcu;
72 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000073};
74
Patrick McHardycd967e02013-04-17 06:46:56 +000075/* state bits */
76#define NETLINK_CONGESTED 0x0
77
78/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070079#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070080#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000081#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070082#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070083
David S. Miller035c4c12011-12-23 17:33:03 -050084static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070085{
86 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
87}
88
Andrey Vagin0f29c762013-03-21 20:33:47 +040089struct netlink_table *nl_table;
90EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
93
94static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000095static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Andrey Vagin0f29c762013-03-21 20:33:47 +040097DEFINE_RWLOCK(nl_table_lock);
98EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static atomic_t nl_table_users = ATOMIC_INIT(0);
100
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000101#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
102
Alan Sterne041c682006-03-27 01:16:30 -0800103static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200105static DEFINE_SPINLOCK(netlink_tap_lock);
106static struct list_head netlink_tap_all __read_mostly;
107
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000108static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700109{
110 return group ? 1 << (group - 1) : 0;
111}
112
Eric W. Biederman15e47302012-09-07 20:12:54 +0000113static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000115 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200118int netlink_add_tap(struct netlink_tap *nt)
119{
120 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
121 return -EINVAL;
122
123 spin_lock(&netlink_tap_lock);
124 list_add_rcu(&nt->list, &netlink_tap_all);
125 spin_unlock(&netlink_tap_lock);
126
127 if (nt->module)
128 __module_get(nt->module);
129
130 return 0;
131}
132EXPORT_SYMBOL_GPL(netlink_add_tap);
133
stephen hemminger2173f8d2013-12-30 10:49:22 -0800134static int __netlink_remove_tap(struct netlink_tap *nt)
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200135{
136 bool found = false;
137 struct netlink_tap *tmp;
138
139 spin_lock(&netlink_tap_lock);
140
141 list_for_each_entry(tmp, &netlink_tap_all, list) {
142 if (nt == tmp) {
143 list_del_rcu(&nt->list);
144 found = true;
145 goto out;
146 }
147 }
148
149 pr_warn("__netlink_remove_tap: %p not found\n", nt);
150out:
151 spin_unlock(&netlink_tap_lock);
152
153 if (found && nt->module)
154 module_put(nt->module);
155
156 return found ? 0 : -ENODEV;
157}
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200158
159int netlink_remove_tap(struct netlink_tap *nt)
160{
161 int ret;
162
163 ret = __netlink_remove_tap(nt);
164 synchronize_net();
165
166 return ret;
167}
168EXPORT_SYMBOL_GPL(netlink_remove_tap);
169
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200170static bool netlink_filter_tap(const struct sk_buff *skb)
171{
172 struct sock *sk = skb->sk;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200173
174 /* We take the more conservative approach and
175 * whitelist socket protocols that may pass.
176 */
177 switch (sk->sk_protocol) {
178 case NETLINK_ROUTE:
179 case NETLINK_USERSOCK:
180 case NETLINK_SOCK_DIAG:
181 case NETLINK_NFLOG:
182 case NETLINK_XFRM:
183 case NETLINK_FIB_LOOKUP:
184 case NETLINK_NETFILTER:
185 case NETLINK_GENERIC:
Varka Bhadram498044b2014-07-16 10:59:47 +0530186 return true;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200187 }
188
Varka Bhadram498044b2014-07-16 10:59:47 +0530189 return false;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200190}
191
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200192static int __netlink_deliver_tap_skb(struct sk_buff *skb,
193 struct net_device *dev)
194{
195 struct sk_buff *nskb;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200196 struct sock *sk = skb->sk;
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200197 int ret = -ENOMEM;
198
199 dev_hold(dev);
200 nskb = skb_clone(skb, GFP_ATOMIC);
201 if (nskb) {
202 nskb->dev = dev;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200203 nskb->protocol = htons((u16) sk->sk_protocol);
Daniel Borkmann604d13c2013-12-23 14:35:56 +0100204 nskb->pkt_type = netlink_is_kernel(sk) ?
205 PACKET_KERNEL : PACKET_USER;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200206
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200207 ret = dev_queue_xmit(nskb);
208 if (unlikely(ret > 0))
209 ret = net_xmit_errno(ret);
210 }
211
212 dev_put(dev);
213 return ret;
214}
215
216static void __netlink_deliver_tap(struct sk_buff *skb)
217{
218 int ret;
219 struct netlink_tap *tmp;
220
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200221 if (!netlink_filter_tap(skb))
222 return;
223
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200224 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
225 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
226 if (unlikely(ret))
227 break;
228 }
229}
230
231static void netlink_deliver_tap(struct sk_buff *skb)
232{
233 rcu_read_lock();
234
235 if (unlikely(!list_empty(&netlink_tap_all)))
236 __netlink_deliver_tap(skb);
237
238 rcu_read_unlock();
239}
240
Daniel Borkmann73bfd372013-12-23 14:35:55 +0100241static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
242 struct sk_buff *skb)
243{
244 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
245 netlink_deliver_tap(skb);
246}
247
Patrick McHardycd1df522013-04-17 06:47:05 +0000248static void netlink_overrun(struct sock *sk)
249{
250 struct netlink_sock *nlk = nlk_sk(sk);
251
252 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
253 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
254 sk->sk_err = ENOBUFS;
255 sk->sk_error_report(sk);
256 }
257 }
258 atomic_inc(&sk->sk_drops);
259}
260
261static void netlink_rcv_wake(struct sock *sk)
262{
263 struct netlink_sock *nlk = nlk_sk(sk);
264
265 if (skb_queue_empty(&sk->sk_receive_queue))
266 clear_bit(NETLINK_CONGESTED, &nlk->state);
267 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
268 wake_up_interruptible(&nlk->wait);
269}
270
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000271#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000272static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
273{
274 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
275}
276
Patrick McHardyf9c22882013-04-17 06:47:04 +0000277static bool netlink_rx_is_mmaped(struct sock *sk)
278{
279 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
280}
281
Patrick McHardy5fd96122013-04-17 06:47:03 +0000282static bool netlink_tx_is_mmaped(struct sock *sk)
283{
284 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
285}
286
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000287static __pure struct page *pgvec_to_page(const void *addr)
288{
289 if (is_vmalloc_addr(addr))
290 return vmalloc_to_page(addr);
291 else
292 return virt_to_page(addr);
293}
294
295static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
296{
297 unsigned int i;
298
299 for (i = 0; i < len; i++) {
300 if (pg_vec[i] != NULL) {
301 if (is_vmalloc_addr(pg_vec[i]))
302 vfree(pg_vec[i]);
303 else
304 free_pages((unsigned long)pg_vec[i], order);
305 }
306 }
307 kfree(pg_vec);
308}
309
310static void *alloc_one_pg_vec_page(unsigned long order)
311{
312 void *buffer;
313 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
314 __GFP_NOWARN | __GFP_NORETRY;
315
316 buffer = (void *)__get_free_pages(gfp_flags, order);
317 if (buffer != NULL)
318 return buffer;
319
320 buffer = vzalloc((1 << order) * PAGE_SIZE);
321 if (buffer != NULL)
322 return buffer;
323
324 gfp_flags &= ~__GFP_NORETRY;
325 return (void *)__get_free_pages(gfp_flags, order);
326}
327
328static void **alloc_pg_vec(struct netlink_sock *nlk,
329 struct nl_mmap_req *req, unsigned int order)
330{
331 unsigned int block_nr = req->nm_block_nr;
332 unsigned int i;
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200333 void **pg_vec;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000334
335 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
336 if (pg_vec == NULL)
337 return NULL;
338
339 for (i = 0; i < block_nr; i++) {
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200340 pg_vec[i] = alloc_one_pg_vec_page(order);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000341 if (pg_vec[i] == NULL)
342 goto err1;
343 }
344
345 return pg_vec;
346err1:
347 free_pg_vec(pg_vec, order, block_nr);
348 return NULL;
349}
350
351static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
352 bool closing, bool tx_ring)
353{
354 struct netlink_sock *nlk = nlk_sk(sk);
355 struct netlink_ring *ring;
356 struct sk_buff_head *queue;
357 void **pg_vec = NULL;
358 unsigned int order = 0;
359 int err;
360
361 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
362 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
363
364 if (!closing) {
365 if (atomic_read(&nlk->mapped))
366 return -EBUSY;
367 if (atomic_read(&ring->pending))
368 return -EBUSY;
369 }
370
371 if (req->nm_block_nr) {
372 if (ring->pg_vec != NULL)
373 return -EBUSY;
374
375 if ((int)req->nm_block_size <= 0)
376 return -EINVAL;
377 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
378 return -EINVAL;
379 if (req->nm_frame_size < NL_MMAP_HDRLEN)
380 return -EINVAL;
381 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
382 return -EINVAL;
383
384 ring->frames_per_block = req->nm_block_size /
385 req->nm_frame_size;
386 if (ring->frames_per_block == 0)
387 return -EINVAL;
388 if (ring->frames_per_block * req->nm_block_nr !=
389 req->nm_frame_nr)
390 return -EINVAL;
391
392 order = get_order(req->nm_block_size);
393 pg_vec = alloc_pg_vec(nlk, req, order);
394 if (pg_vec == NULL)
395 return -ENOMEM;
396 } else {
397 if (req->nm_frame_nr)
398 return -EINVAL;
399 }
400
401 err = -EBUSY;
402 mutex_lock(&nlk->pg_vec_lock);
403 if (closing || atomic_read(&nlk->mapped) == 0) {
404 err = 0;
405 spin_lock_bh(&queue->lock);
406
407 ring->frame_max = req->nm_frame_nr - 1;
408 ring->head = 0;
409 ring->frame_size = req->nm_frame_size;
410 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
411
412 swap(ring->pg_vec_len, req->nm_block_nr);
413 swap(ring->pg_vec_order, order);
414 swap(ring->pg_vec, pg_vec);
415
416 __skb_queue_purge(queue);
417 spin_unlock_bh(&queue->lock);
418
419 WARN_ON(atomic_read(&nlk->mapped));
420 }
421 mutex_unlock(&nlk->pg_vec_lock);
422
423 if (pg_vec)
424 free_pg_vec(pg_vec, order, req->nm_block_nr);
425 return err;
426}
427
428static void netlink_mm_open(struct vm_area_struct *vma)
429{
430 struct file *file = vma->vm_file;
431 struct socket *sock = file->private_data;
432 struct sock *sk = sock->sk;
433
434 if (sk)
435 atomic_inc(&nlk_sk(sk)->mapped);
436}
437
438static void netlink_mm_close(struct vm_area_struct *vma)
439{
440 struct file *file = vma->vm_file;
441 struct socket *sock = file->private_data;
442 struct sock *sk = sock->sk;
443
444 if (sk)
445 atomic_dec(&nlk_sk(sk)->mapped);
446}
447
448static const struct vm_operations_struct netlink_mmap_ops = {
449 .open = netlink_mm_open,
450 .close = netlink_mm_close,
451};
452
453static int netlink_mmap(struct file *file, struct socket *sock,
454 struct vm_area_struct *vma)
455{
456 struct sock *sk = sock->sk;
457 struct netlink_sock *nlk = nlk_sk(sk);
458 struct netlink_ring *ring;
459 unsigned long start, size, expected;
460 unsigned int i;
461 int err = -EINVAL;
462
463 if (vma->vm_pgoff)
464 return -EINVAL;
465
466 mutex_lock(&nlk->pg_vec_lock);
467
468 expected = 0;
469 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
470 if (ring->pg_vec == NULL)
471 continue;
472 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
473 }
474
475 if (expected == 0)
476 goto out;
477
478 size = vma->vm_end - vma->vm_start;
479 if (size != expected)
480 goto out;
481
482 start = vma->vm_start;
483 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
484 if (ring->pg_vec == NULL)
485 continue;
486
487 for (i = 0; i < ring->pg_vec_len; i++) {
488 struct page *page;
489 void *kaddr = ring->pg_vec[i];
490 unsigned int pg_num;
491
492 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
493 page = pgvec_to_page(kaddr);
494 err = vm_insert_page(vma, start, page);
495 if (err < 0)
496 goto out;
497 start += PAGE_SIZE;
498 kaddr += PAGE_SIZE;
499 }
500 }
501 }
502
503 atomic_inc(&nlk->mapped);
504 vma->vm_ops = &netlink_mmap_ops;
505 err = 0;
506out:
507 mutex_unlock(&nlk->pg_vec_lock);
Patrick McHardy7cdbac72013-06-11 02:52:47 -0700508 return err;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000509}
Patrick McHardy9652e932013-04-17 06:47:02 +0000510
511static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
512{
513#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
514 struct page *p_start, *p_end;
515
516 /* First page is flushed through netlink_{get,set}_status */
517 p_start = pgvec_to_page(hdr + PAGE_SIZE);
Stephen Rothwell1d5085c2013-04-23 17:40:35 +1000518 p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
Patrick McHardy9652e932013-04-17 06:47:02 +0000519 while (p_start <= p_end) {
520 flush_dcache_page(p_start);
521 p_start++;
522 }
523#endif
524}
525
526static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
527{
528 smp_rmb();
529 flush_dcache_page(pgvec_to_page(hdr));
530 return hdr->nm_status;
531}
532
533static void netlink_set_status(struct nl_mmap_hdr *hdr,
534 enum nl_mmap_status status)
535{
536 hdr->nm_status = status;
537 flush_dcache_page(pgvec_to_page(hdr));
538 smp_wmb();
539}
540
541static struct nl_mmap_hdr *
542__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
543{
544 unsigned int pg_vec_pos, frame_off;
545
546 pg_vec_pos = pos / ring->frames_per_block;
547 frame_off = pos % ring->frames_per_block;
548
549 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
550}
551
552static struct nl_mmap_hdr *
553netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
554 enum nl_mmap_status status)
555{
556 struct nl_mmap_hdr *hdr;
557
558 hdr = __netlink_lookup_frame(ring, pos);
559 if (netlink_get_status(hdr) != status)
560 return NULL;
561
562 return hdr;
563}
564
565static struct nl_mmap_hdr *
566netlink_current_frame(const struct netlink_ring *ring,
567 enum nl_mmap_status status)
568{
569 return netlink_lookup_frame(ring, ring->head, status);
570}
571
572static struct nl_mmap_hdr *
573netlink_previous_frame(const struct netlink_ring *ring,
574 enum nl_mmap_status status)
575{
576 unsigned int prev;
577
578 prev = ring->head ? ring->head - 1 : ring->frame_max;
579 return netlink_lookup_frame(ring, prev, status);
580}
581
582static void netlink_increment_head(struct netlink_ring *ring)
583{
584 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
585}
586
587static void netlink_forward_ring(struct netlink_ring *ring)
588{
589 unsigned int head = ring->head, pos = head;
590 const struct nl_mmap_hdr *hdr;
591
592 do {
593 hdr = __netlink_lookup_frame(ring, pos);
594 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
595 break;
596 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
597 break;
598 netlink_increment_head(ring);
599 } while (ring->head != head);
600}
601
Patrick McHardycd1df522013-04-17 06:47:05 +0000602static bool netlink_dump_space(struct netlink_sock *nlk)
603{
604 struct netlink_ring *ring = &nlk->rx_ring;
605 struct nl_mmap_hdr *hdr;
606 unsigned int n;
607
608 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
609 if (hdr == NULL)
610 return false;
611
612 n = ring->head + ring->frame_max / 2;
613 if (n > ring->frame_max)
614 n -= ring->frame_max;
615
616 hdr = __netlink_lookup_frame(ring, n);
617
618 return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
619}
620
Patrick McHardy9652e932013-04-17 06:47:02 +0000621static unsigned int netlink_poll(struct file *file, struct socket *sock,
622 poll_table *wait)
623{
624 struct sock *sk = sock->sk;
625 struct netlink_sock *nlk = nlk_sk(sk);
626 unsigned int mask;
Patrick McHardycd1df522013-04-17 06:47:05 +0000627 int err;
Patrick McHardy9652e932013-04-17 06:47:02 +0000628
Patrick McHardycd1df522013-04-17 06:47:05 +0000629 if (nlk->rx_ring.pg_vec != NULL) {
630 /* Memory mapped sockets don't call recvmsg(), so flow control
631 * for dumps is performed here. A dump is allowed to continue
632 * if at least half the ring is unused.
633 */
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700634 while (nlk->cb_running && netlink_dump_space(nlk)) {
Patrick McHardycd1df522013-04-17 06:47:05 +0000635 err = netlink_dump(sk);
636 if (err < 0) {
Ben Pfaffac30ef82014-07-09 10:31:22 -0700637 sk->sk_err = -err;
Patrick McHardycd1df522013-04-17 06:47:05 +0000638 sk->sk_error_report(sk);
639 break;
640 }
641 }
642 netlink_rcv_wake(sk);
643 }
Patrick McHardy5fd96122013-04-17 06:47:03 +0000644
Patrick McHardy9652e932013-04-17 06:47:02 +0000645 mask = datagram_poll(file, sock, wait);
646
647 spin_lock_bh(&sk->sk_receive_queue.lock);
648 if (nlk->rx_ring.pg_vec) {
649 netlink_forward_ring(&nlk->rx_ring);
650 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
651 mask |= POLLIN | POLLRDNORM;
652 }
653 spin_unlock_bh(&sk->sk_receive_queue.lock);
654
655 spin_lock_bh(&sk->sk_write_queue.lock);
656 if (nlk->tx_ring.pg_vec) {
657 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
658 mask |= POLLOUT | POLLWRNORM;
659 }
660 spin_unlock_bh(&sk->sk_write_queue.lock);
661
662 return mask;
663}
664
665static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
666{
667 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
668}
669
670static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
671 struct netlink_ring *ring,
672 struct nl_mmap_hdr *hdr)
673{
674 unsigned int size;
675 void *data;
676
677 size = ring->frame_size - NL_MMAP_HDRLEN;
678 data = (void *)hdr + NL_MMAP_HDRLEN;
679
680 skb->head = data;
681 skb->data = data;
682 skb_reset_tail_pointer(skb);
683 skb->end = skb->tail + size;
684 skb->len = 0;
685
686 skb->destructor = netlink_skb_destructor;
687 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
688 NETLINK_CB(skb).sk = sk;
689}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000690
691static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
692 u32 dst_portid, u32 dst_group,
693 struct sock_iocb *siocb)
694{
695 struct netlink_sock *nlk = nlk_sk(sk);
696 struct netlink_ring *ring;
697 struct nl_mmap_hdr *hdr;
698 struct sk_buff *skb;
699 unsigned int maxlen;
700 bool excl = true;
701 int err = 0, len = 0;
702
703 /* Netlink messages are validated by the receiver before processing.
704 * In order to avoid userspace changing the contents of the message
705 * after validation, the socket and the ring may only be used by a
706 * single process, otherwise we fall back to copying.
707 */
708 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
709 atomic_read(&nlk->mapped) > 1)
710 excl = false;
711
712 mutex_lock(&nlk->pg_vec_lock);
713
714 ring = &nlk->tx_ring;
715 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
716
717 do {
718 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
719 if (hdr == NULL) {
720 if (!(msg->msg_flags & MSG_DONTWAIT) &&
721 atomic_read(&nlk->tx_ring.pending))
722 schedule();
723 continue;
724 }
725 if (hdr->nm_len > maxlen) {
726 err = -EINVAL;
727 goto out;
728 }
729
730 netlink_frame_flush_dcache(hdr);
731
732 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
733 skb = alloc_skb_head(GFP_KERNEL);
734 if (skb == NULL) {
735 err = -ENOBUFS;
736 goto out;
737 }
738 sock_hold(sk);
739 netlink_ring_setup_skb(skb, sk, ring, hdr);
740 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
741 __skb_put(skb, hdr->nm_len);
742 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
743 atomic_inc(&ring->pending);
744 } else {
745 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
746 if (skb == NULL) {
747 err = -ENOBUFS;
748 goto out;
749 }
750 __skb_put(skb, hdr->nm_len);
751 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
752 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
753 }
754
755 netlink_increment_head(ring);
756
757 NETLINK_CB(skb).portid = nlk->portid;
758 NETLINK_CB(skb).dst_group = dst_group;
759 NETLINK_CB(skb).creds = siocb->scm->creds;
760
761 err = security_netlink_send(sk, skb);
762 if (err) {
763 kfree_skb(skb);
764 goto out;
765 }
766
767 if (unlikely(dst_group)) {
768 atomic_inc(&skb->users);
769 netlink_broadcast(sk, skb, dst_portid, dst_group,
770 GFP_KERNEL);
771 }
772 err = netlink_unicast(sk, skb, dst_portid,
773 msg->msg_flags & MSG_DONTWAIT);
774 if (err < 0)
775 goto out;
776 len += err;
777
778 } while (hdr != NULL ||
779 (!(msg->msg_flags & MSG_DONTWAIT) &&
780 atomic_read(&nlk->tx_ring.pending)));
781
782 if (len > 0)
783 err = len;
784out:
785 mutex_unlock(&nlk->pg_vec_lock);
786 return err;
787}
Patrick McHardyf9c22882013-04-17 06:47:04 +0000788
789static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
790{
791 struct nl_mmap_hdr *hdr;
792
793 hdr = netlink_mmap_hdr(skb);
794 hdr->nm_len = skb->len;
795 hdr->nm_group = NETLINK_CB(skb).dst_group;
796 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200797 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
798 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000799 netlink_frame_flush_dcache(hdr);
800 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
801
802 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
803 kfree_skb(skb);
804}
805
806static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
807{
808 struct netlink_sock *nlk = nlk_sk(sk);
809 struct netlink_ring *ring = &nlk->rx_ring;
810 struct nl_mmap_hdr *hdr;
811
812 spin_lock_bh(&sk->sk_receive_queue.lock);
813 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
814 if (hdr == NULL) {
815 spin_unlock_bh(&sk->sk_receive_queue.lock);
816 kfree_skb(skb);
Patrick McHardycd1df522013-04-17 06:47:05 +0000817 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000818 return;
819 }
820 netlink_increment_head(ring);
821 __skb_queue_tail(&sk->sk_receive_queue, skb);
822 spin_unlock_bh(&sk->sk_receive_queue.lock);
823
824 hdr->nm_len = skb->len;
825 hdr->nm_group = NETLINK_CB(skb).dst_group;
826 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200827 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
828 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000829 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
830}
831
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000832#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000833#define netlink_skb_is_mmaped(skb) false
Patrick McHardyf9c22882013-04-17 06:47:04 +0000834#define netlink_rx_is_mmaped(sk) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000835#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000836#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000837#define netlink_poll datagram_poll
Patrick McHardy5fd96122013-04-17 06:47:03 +0000838#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000839#endif /* CONFIG_NETLINK_MMAP */
840
Patrick McHardycf0a0182013-04-17 06:47:00 +0000841static void netlink_skb_destructor(struct sk_buff *skb)
842{
Patrick McHardy9652e932013-04-17 06:47:02 +0000843#ifdef CONFIG_NETLINK_MMAP
844 struct nl_mmap_hdr *hdr;
845 struct netlink_ring *ring;
846 struct sock *sk;
847
848 /* If a packet from the kernel to userspace was freed because of an
849 * error without being delivered to userspace, the kernel must reset
850 * the status. In the direction userspace to kernel, the status is
851 * always reset here after the packet was processed and freed.
852 */
853 if (netlink_skb_is_mmaped(skb)) {
854 hdr = netlink_mmap_hdr(skb);
855 sk = NETLINK_CB(skb).sk;
856
Patrick McHardy5fd96122013-04-17 06:47:03 +0000857 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
858 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
859 ring = &nlk_sk(sk)->tx_ring;
860 } else {
861 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
862 hdr->nm_len = 0;
863 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
864 }
865 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000866 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000867
868 WARN_ON(atomic_read(&ring->pending) == 0);
869 atomic_dec(&ring->pending);
870 sock_put(sk);
871
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000872 skb->head = NULL;
Patrick McHardy9652e932013-04-17 06:47:02 +0000873 }
874#endif
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000875 if (is_vmalloc_addr(skb->head)) {
Pablo Neira3a365152013-06-28 03:04:23 +0200876 if (!skb->cloned ||
877 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
878 vfree(skb->head);
879
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000880 skb->head = NULL;
881 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000882 if (skb->sk != NULL)
883 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000884}
885
886static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
887{
888 WARN_ON(skb->sk != NULL);
889 skb->sk = sk;
890 skb->destructor = netlink_skb_destructor;
891 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
892 sk_mem_charge(sk, skb->truesize);
893}
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895static void netlink_sock_destruct(struct sock *sk)
896{
Herbert Xu3f660d62007-05-03 03:17:14 -0700897 struct netlink_sock *nlk = nlk_sk(sk);
898
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700899 if (nlk->cb_running) {
900 if (nlk->cb.done)
901 nlk->cb.done(&nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000902
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700903 module_put(nlk->cb.module);
904 kfree_skb(nlk->cb.skb);
Herbert Xu3f660d62007-05-03 03:17:14 -0700905 }
906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000908#ifdef CONFIG_NETLINK_MMAP
909 if (1) {
910 struct nl_mmap_req req;
911
912 memset(&req, 0, sizeof(req));
913 if (nlk->rx_ring.pg_vec)
914 netlink_set_ring(sk, &req, true, false);
915 memset(&req, 0, sizeof(req));
916 if (nlk->tx_ring.pg_vec)
917 netlink_set_ring(sk, &req, true, true);
918 }
919#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800922 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 return;
924 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700925
926 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
927 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
928 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800931/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
932 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
934 * this, _but_ remember, it adds useless work on UP machines.
935 */
936
Johannes Bergd136f1b2009-09-12 03:03:15 +0000937void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800938 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000940 might_sleep();
941
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700942 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943
944 if (atomic_read(&nl_table_users)) {
945 DECLARE_WAITQUEUE(wait, current);
946
947 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800948 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 set_current_state(TASK_UNINTERRUPTIBLE);
950 if (atomic_read(&nl_table_users) == 0)
951 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700952 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700954 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 }
956
957 __set_current_state(TASK_RUNNING);
958 remove_wait_queue(&nl_table_wait, &wait);
959 }
960}
961
Johannes Bergd136f1b2009-09-12 03:03:15 +0000962void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800963 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700965 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 wake_up(&nl_table_wait);
967}
968
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800969static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970netlink_lock_table(void)
971{
972 /* read_lock() synchronizes us to netlink_table_grab */
973
974 read_lock(&nl_table_lock);
975 atomic_inc(&nl_table_users);
976 read_unlock(&nl_table_lock);
977}
978
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800979static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980netlink_unlock_table(void)
981{
982 if (atomic_dec_and_test(&nl_table_users))
983 wake_up(&nl_table_wait);
984}
985
Gao fengda12c902013-06-06 14:49:11 +0800986static bool netlink_compare(struct net *net, struct sock *sk)
987{
988 return net_eq(sock_net(sk), net);
989}
990
Eric W. Biederman15e47302012-09-07 20:12:54 +0000991static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Gao fengda12c902013-06-06 14:49:11 +0800993 struct netlink_table *table = &nl_table[protocol];
994 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 struct hlist_head *head;
996 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000999 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001000 sk_for_each(sk, head) {
Gao fengda12c902013-06-06 14:49:11 +08001001 if (table->compare(net, sk) &&
1002 (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 sock_hold(sk);
1004 goto found;
1005 }
1006 }
1007 sk = NULL;
1008found:
1009 read_unlock(&nl_table_lock);
1010 return sk;
1011}
1012
Eric W. Biederman15e47302012-09-07 20:12:54 +00001013static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014{
1015 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -08001016 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 else
1018 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -08001019 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
1020 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Eric W. Biederman15e47302012-09-07 20:12:54 +00001023static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024{
1025 if (size <= PAGE_SIZE)
1026 kfree(table);
1027 else
1028 free_pages((unsigned long)table, get_order(size));
1029}
1030
Eric W. Biederman15e47302012-09-07 20:12:54 +00001031static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032{
1033 unsigned int omask, mask, shift;
1034 size_t osize, size;
1035 struct hlist_head *otable, *table;
1036 int i;
1037
1038 omask = mask = hash->mask;
1039 osize = size = (mask + 1) * sizeof(*table);
1040 shift = hash->shift;
1041
1042 if (grow) {
1043 if (++shift > hash->max_shift)
1044 return 0;
1045 mask = mask * 2 + 1;
1046 size *= 2;
1047 }
1048
Eric W. Biederman15e47302012-09-07 20:12:54 +00001049 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 if (!table)
1051 return 0;
1052
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 otable = hash->table;
1054 hash->table = table;
1055 hash->mask = mask;
1056 hash->shift = shift;
1057 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
1058
1059 for (i = 0; i <= omask; i++) {
1060 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001061 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Sasha Levinb67bfe02013-02-27 17:06:00 -08001063 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +00001064 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
1066
Eric W. Biederman15e47302012-09-07 20:12:54 +00001067 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 hash->rehash_time = jiffies + 10 * 60 * HZ;
1069 return 1;
1070}
1071
Eric W. Biederman15e47302012-09-07 20:12:54 +00001072static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073{
1074 int avg = hash->entries >> hash->shift;
1075
Eric W. Biederman15e47302012-09-07 20:12:54 +00001076 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return 1;
1078
1079 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001080 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 return 1;
1082 }
1083
1084 return 0;
1085}
1086
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001087static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Patrick McHardy4277a082006-03-20 18:52:01 -08001089static void
1090netlink_update_listeners(struct sock *sk)
1091{
1092 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -08001093 unsigned long mask;
1094 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001095 struct listeners *listeners;
1096
1097 listeners = nl_deref_protected(tbl->listeners);
1098 if (!listeners)
1099 return;
Patrick McHardy4277a082006-03-20 18:52:01 -08001100
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001101 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001102 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001103 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001104 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
1105 mask |= nlk_sk(sk)->groups[i];
1106 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001107 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -08001108 }
1109 /* this function is only called with the netlink table "grabbed", which
1110 * makes sure updates are visible before bind or setsockopt return. */
1111}
1112
Eric W. Biederman15e47302012-09-07 20:12:54 +00001113static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114{
Gao fengda12c902013-06-06 14:49:11 +08001115 struct netlink_table *table = &nl_table[sk->sk_protocol];
1116 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 struct hlist_head *head;
1118 int err = -EADDRINUSE;
1119 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int len;
1121
1122 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001123 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001125 sk_for_each(osk, head) {
Gao fengda12c902013-06-06 14:49:11 +08001126 if (table->compare(net, osk) &&
1127 (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 break;
1129 len++;
1130 }
Sasha Levinb67bfe02013-02-27 17:06:00 -08001131 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 goto err;
1133
1134 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001135 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 goto err;
1137
1138 err = -ENOMEM;
1139 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
1140 goto err;
1141
Eric W. Biederman15e47302012-09-07 20:12:54 +00001142 if (len && nl_portid_hash_dilute(hash, len))
1143 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001145 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 sk_add_node(sk, head);
1147 err = 0;
1148
1149err:
1150 netlink_table_ungrab();
1151 return err;
1152}
1153
1154static void netlink_remove(struct sock *sk)
1155{
1156 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -07001157 if (sk_del_node_init(sk))
1158 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001159 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 __sk_del_bind_node(sk);
1161 netlink_table_ungrab();
1162}
1163
1164static struct proto netlink_proto = {
1165 .name = "NETLINK",
1166 .owner = THIS_MODULE,
1167 .obj_size = sizeof(struct netlink_sock),
1168};
1169
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001170static int __netlink_create(struct net *net, struct socket *sock,
1171 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172{
1173 struct sock *sk;
1174 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -07001175
1176 sock->ops = &netlink_ops;
1177
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001178 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -07001179 if (!sk)
1180 return -ENOMEM;
1181
1182 sock_init_data(sock, sk);
1183
1184 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001185 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001186 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +00001187 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001188 nlk->cb_mutex = &nlk->cb_def_mutex;
1189 mutex_init(nlk->cb_mutex);
1190 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001191 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001192#ifdef CONFIG_NETLINK_MMAP
1193 mutex_init(&nlk->pg_vec_lock);
1194#endif
Patrick McHardyab33a172005-08-14 19:31:36 -07001195
1196 sk->sk_destruct = netlink_sock_destruct;
1197 sk->sk_protocol = protocol;
1198 return 0;
1199}
1200
Eric Paris3f378b62009-11-05 22:18:14 -08001201static int netlink_create(struct net *net, struct socket *sock, int protocol,
1202 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -07001203{
1204 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001205 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001206 struct netlink_sock *nlk;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001207 int (*bind)(int group);
1208 void (*unbind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -07001209 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210
1211 sock->state = SS_UNCONNECTED;
1212
1213 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1214 return -ESOCKTNOSUPPORT;
1215
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001216 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EPROTONOSUPPORT;
1218
Patrick McHardy77247bb2005-08-14 19:27:13 -07001219 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -07001220#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -07001221 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -07001222 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001223 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001224 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001225 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001226#endif
1227 if (nl_table[protocol].registered &&
1228 try_module_get(nl_table[protocol].module))
1229 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001230 else
1231 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001232 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001233 bind = nl_table[protocol].bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001234 unbind = nl_table[protocol].unbind;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001235 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001236
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001237 if (err < 0)
1238 goto out;
1239
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001240 err = __netlink_create(net, sock, cb_mutex, protocol);
1241 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -07001242 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
David S. Miller6f756a82008-11-23 17:34:03 -08001244 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001245 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -08001246 local_bh_enable();
1247
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001248 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001249 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001250 nlk->netlink_bind = bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001251 nlk->netlink_unbind = unbind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001252out:
1253 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Patrick McHardyab33a172005-08-14 19:31:36 -07001255out_module:
1256 module_put(module);
1257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258}
1259
1260static int netlink_release(struct socket *sock)
1261{
1262 struct sock *sk = sock->sk;
1263 struct netlink_sock *nlk;
1264
1265 if (!sk)
1266 return 0;
1267
1268 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001269 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 nlk = nlk_sk(sk);
1271
Herbert Xu3f660d62007-05-03 03:17:14 -07001272 /*
1273 * OK. Socket is unlinked, any packets that arrive now
1274 * will be purged.
1275 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 sock->sk = NULL;
1278 wake_up_interruptible_all(&nlk->wait);
1279
1280 skb_queue_purge(&sk->sk_write_queue);
1281
Eric W. Biederman15e47302012-09-07 20:12:54 +00001282 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001284 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001286 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 };
Alan Sterne041c682006-03-27 01:16:30 -08001288 atomic_notifier_call_chain(&netlink_chain,
1289 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001290 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001291
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001292 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001293
Patrick McHardy4277a082006-03-20 18:52:01 -08001294 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -07001295 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001296 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1297 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001298 struct listeners *old;
1299
1300 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1301 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1302 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001303 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001304 nl_table[sk->sk_protocol].bind = NULL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001305 nl_table[sk->sk_protocol].unbind = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001306 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001307 nl_table[sk->sk_protocol].registered = 0;
1308 }
Eric Dumazet658cb352012-04-22 21:30:21 +00001309 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001310 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001311 }
Patrick McHardy4277a082006-03-20 18:52:01 -08001312 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -07001313
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001314 kfree(nlk->groups);
1315 nlk->groups = NULL;
1316
Eric Dumazet37558102008-11-24 14:05:22 -08001317 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001318 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001319 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 sock_put(sk);
1321 return 0;
1322}
1323
1324static int netlink_autobind(struct socket *sock)
1325{
1326 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001327 struct net *net = sock_net(sk);
Gao fengda12c902013-06-06 14:49:11 +08001328 struct netlink_table *table = &nl_table[sk->sk_protocol];
1329 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 struct hlist_head *head;
1331 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001332 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 int err;
1334 static s32 rover = -4097;
1335
1336retry:
1337 cond_resched();
1338 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001339 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001340 sk_for_each(osk, head) {
Gao fengda12c902013-06-06 14:49:11 +08001341 if (!table->compare(net, osk))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001342 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001343 if (nlk_sk(osk)->portid == portid) {
1344 /* Bind collision, search negative portid values. */
1345 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 if (rover > -4097)
1347 rover = -4097;
1348 netlink_table_ungrab();
1349 goto retry;
1350 }
1351 }
1352 netlink_table_ungrab();
1353
Eric W. Biederman15e47302012-09-07 20:12:54 +00001354 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (err == -EADDRINUSE)
1356 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001357
1358 /* If 2 threads race to autobind, that is fine. */
1359 if (err == -EBUSY)
1360 err = 0;
1361
1362 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001365/**
1366 * __netlink_ns_capable - General netlink message capability test
1367 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
1368 * @user_ns: The user namespace of the capability to use
1369 * @cap: The capability to use
1370 *
1371 * Test to see if the opener of the socket we received the message
1372 * from had when the netlink socket was created and the sender of the
1373 * message has has the capability @cap in the user namespace @user_ns.
1374 */
1375bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
1376 struct user_namespace *user_ns, int cap)
1377{
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001378 return ((nsp->flags & NETLINK_SKB_DST) ||
1379 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
1380 ns_capable(user_ns, cap);
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001381}
1382EXPORT_SYMBOL(__netlink_ns_capable);
1383
1384/**
1385 * netlink_ns_capable - General netlink message capability test
1386 * @skb: socket buffer holding a netlink command from userspace
1387 * @user_ns: The user namespace of the capability to use
1388 * @cap: The capability to use
1389 *
1390 * Test to see if the opener of the socket we received the message
1391 * from had when the netlink socket was created and the sender of the
1392 * message has has the capability @cap in the user namespace @user_ns.
1393 */
1394bool netlink_ns_capable(const struct sk_buff *skb,
1395 struct user_namespace *user_ns, int cap)
1396{
1397 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
1398}
1399EXPORT_SYMBOL(netlink_ns_capable);
1400
1401/**
1402 * netlink_capable - Netlink global message capability test
1403 * @skb: socket buffer holding a netlink command from userspace
1404 * @cap: The capability to use
1405 *
1406 * Test to see if the opener of the socket we received the message
1407 * from had when the netlink socket was created and the sender of the
1408 * message has has the capability @cap in all user namespaces.
1409 */
1410bool netlink_capable(const struct sk_buff *skb, int cap)
1411{
1412 return netlink_ns_capable(skb, &init_user_ns, cap);
1413}
1414EXPORT_SYMBOL(netlink_capable);
1415
1416/**
1417 * netlink_net_capable - Netlink network namespace message capability test
1418 * @skb: socket buffer holding a netlink command from userspace
1419 * @cap: The capability to use
1420 *
1421 * Test to see if the opener of the socket we received the message
1422 * from had when the netlink socket was created and the sender of the
1423 * message has has the capability @cap over the network namespace of
1424 * the socket we received the message from.
1425 */
1426bool netlink_net_capable(const struct sk_buff *skb, int cap)
1427{
1428 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
1429}
1430EXPORT_SYMBOL(netlink_net_capable);
1431
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001432static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001433{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001434 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001435 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001436}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001438static void
1439netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1440{
1441 struct netlink_sock *nlk = nlk_sk(sk);
1442
1443 if (nlk->subscriptions && !subscriptions)
1444 __sk_del_bind_node(sk);
1445 else if (!nlk->subscriptions && subscriptions)
1446 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1447 nlk->subscriptions = subscriptions;
1448}
1449
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001450static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001451{
1452 struct netlink_sock *nlk = nlk_sk(sk);
1453 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001454 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001455 int err = 0;
1456
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001457 netlink_table_grab();
1458
Patrick McHardy513c2502005-09-06 15:43:59 -07001459 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001460 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001461 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001462 goto out_unlock;
1463 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001464
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001465 if (nlk->ngroups >= groups)
1466 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001467
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001468 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1469 if (new_groups == NULL) {
1470 err = -ENOMEM;
1471 goto out_unlock;
1472 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001473 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001474 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1475
1476 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001477 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001478 out_unlock:
1479 netlink_table_ungrab();
1480 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001481}
1482
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001483static void netlink_unbind(int group, long unsigned int groups,
1484 struct netlink_sock *nlk)
1485{
1486 int undo;
1487
1488 if (!nlk->netlink_unbind)
1489 return;
1490
1491 for (undo = 0; undo < group; undo++)
1492 if (test_bit(group, &groups))
1493 nlk->netlink_unbind(undo);
1494}
1495
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001496static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1497 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001500 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 struct netlink_sock *nlk = nlk_sk(sk);
1502 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1503 int err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001504 long unsigned int groups = nladdr->nl_groups;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001505
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001506 if (addr_len < sizeof(struct sockaddr_nl))
1507 return -EINVAL;
1508
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (nladdr->nl_family != AF_NETLINK)
1510 return -EINVAL;
1511
1512 /* Only superuser is allowed to listen multicasts */
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001513 if (groups) {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001514 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001515 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001516 err = netlink_realloc_groups(sk);
1517 if (err)
1518 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001521 if (nlk->portid)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001522 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 return -EINVAL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001524
1525 if (nlk->netlink_bind && groups) {
1526 int group;
1527
1528 for (group = 0; group < nlk->ngroups; group++) {
1529 if (!test_bit(group, &groups))
1530 continue;
1531 err = nlk->netlink_bind(group);
1532 if (!err)
1533 continue;
1534 netlink_unbind(group, groups, nlk);
1535 return err;
1536 }
1537 }
1538
1539 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001541 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 netlink_autobind(sock);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001543 if (err) {
1544 netlink_unbind(nlk->ngroups - 1, groups, nlk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 return err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 }
1548
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001549 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 return 0;
1551
1552 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001553 netlink_update_subscriptions(sk, nlk->subscriptions +
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001554 hweight32(groups) -
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001555 hweight32(nlk->groups[0]));
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001556 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001557 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 netlink_table_ungrab();
1559
1560 return 0;
1561}
1562
1563static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1564 int alen, int flags)
1565{
1566 int err = 0;
1567 struct sock *sk = sock->sk;
1568 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001569 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Changli Gao6503d962010-03-31 22:58:26 +00001571 if (alen < sizeof(addr->sa_family))
1572 return -EINVAL;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 if (addr->sa_family == AF_UNSPEC) {
1575 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001576 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001577 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return 0;
1579 }
1580 if (addr->sa_family != AF_NETLINK)
1581 return -EINVAL;
1582
Mike Pecovnik46833a82014-02-24 21:11:16 +01001583 if ((nladdr->nl_groups || nladdr->nl_pid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001584 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return -EPERM;
1586
Eric W. Biederman15e47302012-09-07 20:12:54 +00001587 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 err = netlink_autobind(sock);
1589
1590 if (err == 0) {
1591 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001592 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001593 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
1595
1596 return err;
1597}
1598
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001599static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1600 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601{
1602 struct sock *sk = sock->sk;
1603 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001604 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001605
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 nladdr->nl_family = AF_NETLINK;
1607 nladdr->nl_pad = 0;
1608 *addr_len = sizeof(*nladdr);
1609
1610 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001611 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001612 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001614 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001615 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 }
1617 return 0;
1618}
1619
Eric W. Biederman15e47302012-09-07 20:12:54 +00001620static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 struct sock *sock;
1623 struct netlink_sock *nlk;
1624
Eric W. Biederman15e47302012-09-07 20:12:54 +00001625 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 if (!sock)
1627 return ERR_PTR(-ECONNREFUSED);
1628
1629 /* Don't bother queuing skb if kernel socket has no input function */
1630 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001631 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001632 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 sock_put(sock);
1634 return ERR_PTR(-ECONNREFUSED);
1635 }
1636 return sock;
1637}
1638
1639struct sock *netlink_getsockbyfilp(struct file *filp)
1640{
Al Viro496ad9a2013-01-23 17:07:38 -05001641 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 struct sock *sock;
1643
1644 if (!S_ISSOCK(inode->i_mode))
1645 return ERR_PTR(-ENOTSOCK);
1646
1647 sock = SOCKET_I(inode)->sk;
1648 if (sock->sk_family != AF_NETLINK)
1649 return ERR_PTR(-EINVAL);
1650
1651 sock_hold(sock);
1652 return sock;
1653}
1654
Pablo Neira3a365152013-06-28 03:04:23 +02001655static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1656 int broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001657{
1658 struct sk_buff *skb;
1659 void *data;
1660
Pablo Neira3a365152013-06-28 03:04:23 +02001661 if (size <= NLMSG_GOODSIZE || broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001662 return alloc_skb(size, GFP_KERNEL);
1663
Pablo Neira3a365152013-06-28 03:04:23 +02001664 size = SKB_DATA_ALIGN(size) +
1665 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001666
1667 data = vmalloc(size);
1668 if (data == NULL)
Pablo Neira3a365152013-06-28 03:04:23 +02001669 return NULL;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001670
Pablo Neira3a365152013-06-28 03:04:23 +02001671 skb = build_skb(data, size);
1672 if (skb == NULL)
1673 vfree(data);
1674 else {
1675 skb->head_frag = 0;
1676 skb->destructor = netlink_skb_destructor;
1677 }
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001678
1679 return skb;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001680}
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682/*
1683 * Attach a skb to a netlink socket.
1684 * The caller must hold a reference to the destination socket. On error, the
1685 * reference is dropped. The skb is not send to the destination, just all
1686 * all error checks are performed and memory in the queue is reserved.
1687 * Return values:
1688 * < 0: error. skb freed, reference to sock dropped.
1689 * 0: continue
1690 * 1: repeat lookup - reference dropped while waiting for socket memory.
1691 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001692int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001693 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
1695 struct netlink_sock *nlk;
1696
1697 nlk = nlk_sk(sk);
1698
Patrick McHardy5fd96122013-04-17 06:47:03 +00001699 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1700 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1701 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001703 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001704 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 netlink_overrun(sk);
1706 sock_put(sk);
1707 kfree_skb(skb);
1708 return -EAGAIN;
1709 }
1710
1711 __set_current_state(TASK_INTERRUPTIBLE);
1712 add_wait_queue(&nlk->wait, &wait);
1713
1714 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001715 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001717 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
1719 __set_current_state(TASK_RUNNING);
1720 remove_wait_queue(&nlk->wait, &wait);
1721 sock_put(sk);
1722
1723 if (signal_pending(current)) {
1724 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001725 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 }
1727 return 1;
1728 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001729 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return 0;
1731}
1732
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001733static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 int len = skb->len;
1736
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02001737 netlink_deliver_tap(skb);
1738
Patrick McHardyf9c22882013-04-17 06:47:04 +00001739#ifdef CONFIG_NETLINK_MMAP
1740 if (netlink_skb_is_mmaped(skb))
1741 netlink_queue_mmaped_skb(sk, skb);
1742 else if (netlink_rx_is_mmaped(sk))
1743 netlink_ring_set_copied(sk, skb);
1744 else
1745#endif /* CONFIG_NETLINK_MMAP */
1746 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -04001747 sk->sk_data_ready(sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001748 return len;
1749}
1750
1751int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1752{
1753 int len = __netlink_sendskb(sk, skb);
1754
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 sock_put(sk);
1756 return len;
1757}
1758
1759void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1760{
1761 kfree_skb(skb);
1762 sock_put(sk);
1763}
1764
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001765static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766{
1767 int delta;
1768
Patrick McHardy1298ca42013-04-17 06:46:59 +00001769 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001770 if (netlink_skb_is_mmaped(skb))
1771 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001773 delta = skb->end - skb->tail;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001774 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return skb;
1776
1777 if (skb_shared(skb)) {
1778 struct sk_buff *nskb = skb_clone(skb, allocation);
1779 if (!nskb)
1780 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001781 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 skb = nskb;
1783 }
1784
1785 if (!pskb_expand_head(skb, 0, -delta, allocation))
1786 skb->truesize -= delta;
1787
1788 return skb;
1789}
1790
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001791static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1792 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001793{
1794 int ret;
1795 struct netlink_sock *nlk = nlk_sk(sk);
1796
1797 ret = -ECONNREFUSED;
1798 if (nlk->netlink_rcv != NULL) {
1799 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001800 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001801 NETLINK_CB(skb).sk = ssk;
Daniel Borkmann73bfd372013-12-23 14:35:55 +01001802 netlink_deliver_tap_kernel(sk, ssk, skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001803 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001804 consume_skb(skb);
1805 } else {
1806 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001807 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001808 sock_put(sk);
1809 return ret;
1810}
1811
1812int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001813 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814{
1815 struct sock *sk;
1816 int err;
1817 long timeo;
1818
1819 skb = netlink_trim(skb, gfp_any());
1820
1821 timeo = sock_sndtimeo(ssk, nonblock);
1822retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001823 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (IS_ERR(sk)) {
1825 kfree_skb(skb);
1826 return PTR_ERR(sk);
1827 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001828 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001829 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001830
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001831 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001832 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001833 kfree_skb(skb);
1834 sock_put(sk);
1835 return err;
1836 }
1837
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001838 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (err == 1)
1840 goto retry;
1841 if (err)
1842 return err;
1843
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001844 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001846EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Patrick McHardyf9c22882013-04-17 06:47:04 +00001848struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1849 u32 dst_portid, gfp_t gfp_mask)
1850{
1851#ifdef CONFIG_NETLINK_MMAP
1852 struct sock *sk = NULL;
1853 struct sk_buff *skb;
1854 struct netlink_ring *ring;
1855 struct nl_mmap_hdr *hdr;
1856 unsigned int maxlen;
1857
1858 sk = netlink_getsockbyportid(ssk, dst_portid);
1859 if (IS_ERR(sk))
1860 goto out;
1861
1862 ring = &nlk_sk(sk)->rx_ring;
1863 /* fast-path without atomic ops for common case: non-mmaped receiver */
1864 if (ring->pg_vec == NULL)
1865 goto out_put;
1866
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001867 if (ring->frame_size - NL_MMAP_HDRLEN < size)
1868 goto out_put;
1869
Patrick McHardyf9c22882013-04-17 06:47:04 +00001870 skb = alloc_skb_head(gfp_mask);
1871 if (skb == NULL)
1872 goto err1;
1873
1874 spin_lock_bh(&sk->sk_receive_queue.lock);
1875 /* check again under lock */
1876 if (ring->pg_vec == NULL)
1877 goto out_free;
1878
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001879 /* check again under lock */
Patrick McHardyf9c22882013-04-17 06:47:04 +00001880 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1881 if (maxlen < size)
1882 goto out_free;
1883
1884 netlink_forward_ring(ring);
1885 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1886 if (hdr == NULL)
1887 goto err2;
1888 netlink_ring_setup_skb(skb, sk, ring, hdr);
1889 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1890 atomic_inc(&ring->pending);
1891 netlink_increment_head(ring);
1892
1893 spin_unlock_bh(&sk->sk_receive_queue.lock);
1894 return skb;
1895
1896err2:
1897 kfree_skb(skb);
1898 spin_unlock_bh(&sk->sk_receive_queue.lock);
Patrick McHardycd1df522013-04-17 06:47:05 +00001899 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +00001900err1:
1901 sock_put(sk);
1902 return NULL;
1903
1904out_free:
1905 kfree_skb(skb);
1906 spin_unlock_bh(&sk->sk_receive_queue.lock);
1907out_put:
1908 sock_put(sk);
1909out:
1910#endif
1911 return alloc_skb(size, gfp_mask);
1912}
1913EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1914
Patrick McHardy4277a082006-03-20 18:52:01 -08001915int netlink_has_listeners(struct sock *sk, unsigned int group)
1916{
1917 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001918 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001919
Denis V. Lunevaed81562007-10-10 21:14:32 -07001920 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001921
1922 rcu_read_lock();
1923 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1924
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001925 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001926 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001927
1928 rcu_read_unlock();
1929
Patrick McHardy4277a082006-03-20 18:52:01 -08001930 return res;
1931}
1932EXPORT_SYMBOL_GPL(netlink_has_listeners);
1933
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001934static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 struct netlink_sock *nlk = nlk_sk(sk);
1937
1938 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001939 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001940 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001941 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001942 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944 return -1;
1945}
1946
1947struct netlink_broadcast_data {
1948 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001949 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001950 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 u32 group;
1952 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001953 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 int congested;
1955 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001956 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001958 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1959 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960};
1961
Rami Rosen46c95212014-07-01 21:17:35 +03001962static void do_one_broadcast(struct sock *sk,
1963 struct netlink_broadcast_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct netlink_sock *nlk = nlk_sk(sk);
1966 int val;
1967
1968 if (p->exclude_sk == sk)
Rami Rosen46c95212014-07-01 21:17:35 +03001969 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Eric W. Biederman15e47302012-09-07 20:12:54 +00001971 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001972 !test_bit(p->group - 1, nlk->groups))
Rami Rosen46c95212014-07-01 21:17:35 +03001973 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001975 if (!net_eq(sock_net(sk), p->net))
Rami Rosen46c95212014-07-01 21:17:35 +03001976 return;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 if (p->failure) {
1979 netlink_overrun(sk);
Rami Rosen46c95212014-07-01 21:17:35 +03001980 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 }
1982
1983 sock_hold(sk);
1984 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001985 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 p->skb2 = skb_clone(p->skb, p->allocation);
1987 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001988 p->skb2 = skb_get(p->skb);
1989 /*
1990 * skb ownership may have been set when
1991 * delivered to a previous socket.
1992 */
1993 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995 }
1996 if (p->skb2 == NULL) {
1997 netlink_overrun(sk);
1998 /* Clone failed. Notify ALL listeners. */
1999 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002000 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
2001 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002002 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
2003 kfree_skb(p->skb2);
2004 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002005 } else if (sk_filter(sk, p->skb2)) {
2006 kfree_skb(p->skb2);
2007 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
2009 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002010 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
2011 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 } else {
2013 p->congested |= val;
2014 p->delivered = 1;
2015 p->skb2 = NULL;
2016 }
2017 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018}
2019
Eric W. Biederman15e47302012-09-07 20:12:54 +00002020int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002021 u32 group, gfp_t allocation,
2022 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
2023 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002025 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 struct sock *sk;
2028
2029 skb = netlink_trim(skb, allocation);
2030
2031 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002032 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002033 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 info.group = group;
2035 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002036 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 info.congested = 0;
2038 info.delivered = 0;
2039 info.allocation = allocation;
2040 info.skb = skb;
2041 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002042 info.tx_filter = filter;
2043 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 /* While we sleep in clone, do not allow to change socket list */
2046
2047 netlink_lock_table();
2048
Sasha Levinb67bfe02013-02-27 17:06:00 -08002049 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 do_one_broadcast(sk, &info);
2051
Neil Horman70d4bf62010-07-20 06:45:56 +00002052 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 netlink_unlock_table();
2055
Neil Horman70d4bf62010-07-20 06:45:56 +00002056 if (info.delivery_failure) {
2057 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002058 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002059 }
2060 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 if (info.delivered) {
2063 if (info.congested && (allocation & __GFP_WAIT))
2064 yield();
2065 return 0;
2066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return -ESRCH;
2068}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002069EXPORT_SYMBOL(netlink_broadcast_filtered);
2070
Eric W. Biederman15e47302012-09-07 20:12:54 +00002071int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002072 u32 group, gfp_t allocation)
2073{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002074 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002075 NULL, NULL);
2076}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002077EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079struct netlink_set_err_data {
2080 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002081 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 u32 group;
2083 int code;
2084};
2085
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00002086static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087{
2088 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002089 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091 if (sk == p->exclude_sk)
2092 goto out;
2093
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002094 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002095 goto out;
2096
Eric W. Biederman15e47302012-09-07 20:12:54 +00002097 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07002098 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 goto out;
2100
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002101 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
2102 ret = 1;
2103 goto out;
2104 }
2105
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 sk->sk_err = p->code;
2107 sk->sk_error_report(sk);
2108out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002109 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002112/**
2113 * netlink_set_err - report error to broadcast listeners
2114 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00002115 * @portid: the PORTID of a process that we want to skip (if any)
Johannes Berg840e93f22013-11-19 10:35:40 +01002116 * @group: the broadcast group that will notice the error
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002117 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002118 *
2119 * This function returns the number of broadcast listeners that have set the
2120 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002121 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002122int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123{
2124 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002126 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127
2128 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002129 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002131 /* sk->sk_err wants a positive error value */
2132 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 read_lock(&nl_table_lock);
2135
Sasha Levinb67bfe02013-02-27 17:06:00 -08002136 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002137 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002140 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01002142EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
Johannes Berg84659eb2007-07-18 15:47:05 -07002144/* must be called with netlink table grabbed */
2145static void netlink_update_socket_mc(struct netlink_sock *nlk,
2146 unsigned int group,
2147 int is_new)
2148{
2149 int old, new = !!is_new, subscriptions;
2150
2151 old = test_bit(group - 1, nlk->groups);
2152 subscriptions = nlk->subscriptions - old + new;
2153 if (new)
2154 __set_bit(group - 1, nlk->groups);
2155 else
2156 __clear_bit(group - 1, nlk->groups);
2157 netlink_update_subscriptions(&nlk->sk, subscriptions);
2158 netlink_update_listeners(&nlk->sk);
2159}
2160
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002161static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002162 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002163{
2164 struct sock *sk = sock->sk;
2165 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07002166 unsigned int val = 0;
2167 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002168
2169 if (level != SOL_NETLINK)
2170 return -ENOPROTOOPT;
2171
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002172 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
2173 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07002174 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002175 return -EFAULT;
2176
2177 switch (optname) {
2178 case NETLINK_PKTINFO:
2179 if (val)
2180 nlk->flags |= NETLINK_RECV_PKTINFO;
2181 else
2182 nlk->flags &= ~NETLINK_RECV_PKTINFO;
2183 err = 0;
2184 break;
2185 case NETLINK_ADD_MEMBERSHIP:
2186 case NETLINK_DROP_MEMBERSHIP: {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002187 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002188 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002189 err = netlink_realloc_groups(sk);
2190 if (err)
2191 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002192 if (!val || val - 1 >= nlk->ngroups)
2193 return -EINVAL;
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002194 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
Richard Guy Briggs4f520902014-04-22 21:31:54 -04002195 err = nlk->netlink_bind(val);
2196 if (err)
2197 return err;
2198 }
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002199 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07002200 netlink_update_socket_mc(nlk, val,
2201 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002202 netlink_table_ungrab();
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002203 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
2204 nlk->netlink_unbind(val);
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00002205
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002206 err = 0;
2207 break;
2208 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002209 case NETLINK_BROADCAST_ERROR:
2210 if (val)
2211 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
2212 else
2213 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
2214 err = 0;
2215 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002216 case NETLINK_NO_ENOBUFS:
2217 if (val) {
2218 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00002219 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002220 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00002221 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002222 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002223 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002224 err = 0;
2225 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002226#ifdef CONFIG_NETLINK_MMAP
2227 case NETLINK_RX_RING:
2228 case NETLINK_TX_RING: {
2229 struct nl_mmap_req req;
2230
2231 /* Rings might consume more memory than queue limits, require
2232 * CAP_NET_ADMIN.
2233 */
2234 if (!capable(CAP_NET_ADMIN))
2235 return -EPERM;
2236 if (optlen < sizeof(req))
2237 return -EINVAL;
2238 if (copy_from_user(&req, optval, sizeof(req)))
2239 return -EFAULT;
2240 err = netlink_set_ring(sk, &req, false,
2241 optname == NETLINK_TX_RING);
2242 break;
2243 }
2244#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002245 default:
2246 err = -ENOPROTOOPT;
2247 }
2248 return err;
2249}
2250
2251static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002252 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002253{
2254 struct sock *sk = sock->sk;
2255 struct netlink_sock *nlk = nlk_sk(sk);
2256 int len, val, err;
2257
2258 if (level != SOL_NETLINK)
2259 return -ENOPROTOOPT;
2260
2261 if (get_user(len, optlen))
2262 return -EFAULT;
2263 if (len < 0)
2264 return -EINVAL;
2265
2266 switch (optname) {
2267 case NETLINK_PKTINFO:
2268 if (len < sizeof(int))
2269 return -EINVAL;
2270 len = sizeof(int);
2271 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08002272 if (put_user(len, optlen) ||
2273 put_user(val, optval))
2274 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002275 err = 0;
2276 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002277 case NETLINK_BROADCAST_ERROR:
2278 if (len < sizeof(int))
2279 return -EINVAL;
2280 len = sizeof(int);
2281 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
2282 if (put_user(len, optlen) ||
2283 put_user(val, optval))
2284 return -EFAULT;
2285 err = 0;
2286 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002287 case NETLINK_NO_ENOBUFS:
2288 if (len < sizeof(int))
2289 return -EINVAL;
2290 len = sizeof(int);
2291 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
2292 if (put_user(len, optlen) ||
2293 put_user(val, optval))
2294 return -EFAULT;
2295 err = 0;
2296 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002297 default:
2298 err = -ENOPROTOOPT;
2299 }
2300 return err;
2301}
2302
2303static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2304{
2305 struct nl_pktinfo info;
2306
2307 info.group = NETLINK_CB(skb).dst_group;
2308 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2312 struct msghdr *msg, size_t len)
2313{
2314 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2315 struct sock *sk = sock->sk;
2316 struct netlink_sock *nlk = nlk_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002317 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002318 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002319 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 struct sk_buff *skb;
2321 int err;
2322 struct scm_cookie scm;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002323 u32 netlink_skb_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 if (msg->msg_flags&MSG_OOB)
2326 return -EOPNOTSUPP;
2327
Eric Dumazet16e57262011-09-19 05:52:27 +00002328 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00002330
Eric Dumazete0e3cea2012-08-21 06:21:17 +00002331 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 if (err < 0)
2333 return err;
2334
2335 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002336 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002338 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002339 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002340 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002341 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002342 if ((dst_group || dst_portid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002343 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002344 goto out;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002345 netlink_skb_flags |= NETLINK_SKB_DST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002347 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002348 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
2350
Eric W. Biederman15e47302012-09-07 20:12:54 +00002351 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 err = netlink_autobind(sock);
2353 if (err)
2354 goto out;
2355 }
2356
Patrick McHardy5fd96122013-04-17 06:47:03 +00002357 if (netlink_tx_is_mmaped(sk) &&
2358 msg->msg_iov->iov_base == NULL) {
2359 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2360 siocb);
2361 goto out;
2362 }
2363
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 err = -EMSGSIZE;
2365 if (len > sk->sk_sndbuf - 32)
2366 goto out;
2367 err = -ENOBUFS;
Pablo Neira3a365152013-06-28 03:04:23 +02002368 skb = netlink_alloc_large_skb(len, dst_group);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002369 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 goto out;
2371
Eric W. Biederman15e47302012-09-07 20:12:54 +00002372 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002373 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00002374 NETLINK_CB(skb).creds = siocb->scm->creds;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002375 NETLINK_CB(skb).flags = netlink_skb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002378 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 kfree_skb(skb);
2380 goto out;
2381 }
2382
2383 err = security_netlink_send(sk, skb);
2384 if (err) {
2385 kfree_skb(skb);
2386 goto out;
2387 }
2388
Patrick McHardyd629b832005-08-14 19:27:50 -07002389 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002391 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002393 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394
2395out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002396 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 return err;
2398}
2399
2400static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2401 struct msghdr *msg, size_t len,
2402 int flags)
2403{
2404 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2405 struct scm_cookie scm;
2406 struct sock *sk = sock->sk;
2407 struct netlink_sock *nlk = nlk_sk(sk);
2408 int noblock = flags&MSG_DONTWAIT;
2409 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00002410 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002411 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 if (flags&MSG_OOB)
2414 return -EOPNOTSUPP;
2415
2416 copied = 0;
2417
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002418 skb = skb_recv_datagram(sk, flags, noblock, &err);
2419 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 goto out;
2421
Johannes Berg68d6ac62010-08-15 21:20:44 +00002422 data_skb = skb;
2423
Johannes Berg1dacc762009-07-01 11:26:02 +00002424#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2425 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002426 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002427 * If this skb has a frag_list, then here that means that we
2428 * will have to use the frag_list skb's data for compat tasks
2429 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002430 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002431 * If we need to send the compat skb, assign it to the
2432 * 'data_skb' variable so that it will be used below for data
2433 * copying. We keep 'skb' for everything else, including
2434 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002435 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002436 if (flags & MSG_CMSG_COMPAT)
2437 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002438 }
2439#endif
2440
Eric Dumazet9063e212014-03-07 12:02:33 -08002441 /* Record the max length of recvmsg() calls for future allocations */
2442 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
2443 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
2444 16384);
2445
Johannes Berg68d6ac62010-08-15 21:20:44 +00002446 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 if (len < copied) {
2448 msg->msg_flags |= MSG_TRUNC;
2449 copied = len;
2450 }
2451
Johannes Berg68d6ac62010-08-15 21:20:44 +00002452 skb_reset_transport_header(data_skb);
2453 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454
2455 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002456 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 addr->nl_family = AF_NETLINK;
2458 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002459 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002460 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 msg->msg_namelen = sizeof(*addr);
2462 }
2463
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002464 if (nlk->flags & NETLINK_RECV_PKTINFO)
2465 netlink_cmsg_recv_pktinfo(msg, skb);
2466
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 if (NULL == siocb->scm) {
2468 memset(&scm, 0, sizeof(scm));
2469 siocb->scm = &scm;
2470 }
2471 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002472 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002473 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002474
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 skb_free_datagram(sk, skb);
2476
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002477 if (nlk->cb_running &&
2478 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
Andrey Vaginb44d2112011-02-21 02:40:47 +00002479 ret = netlink_dump(sk);
2480 if (ret) {
Ben Pfaffac30ef82014-07-09 10:31:22 -07002481 sk->sk_err = -ret;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002482 sk->sk_error_report(sk);
2483 }
2484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
2486 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487out:
2488 netlink_rcv_wake(sk);
2489 return err ? : copied;
2490}
2491
David S. Miller676d2362014-04-11 16:15:36 -04002492static void netlink_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002494 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
2497/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002498 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 * complete set of kernel non-blocking support for message
2500 * queueing.
2501 */
2502
2503struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002504__netlink_kernel_create(struct net *net, int unit, struct module *module,
2505 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506{
2507 struct socket *sock;
2508 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002509 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002510 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002511 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2512 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002514 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002516 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return NULL;
2518
2519 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2520 return NULL;
2521
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002522 /*
2523 * We have to just have a reference on the net from sk, but don't
2524 * get_net it. Besides, we cannot get and then put the net here.
2525 * So we create one inside init_net and the move it to net.
2526 */
2527
2528 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2529 goto out_sock_release_nosk;
2530
2531 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002532 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002533
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002534 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002535 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002536 else
2537 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002538
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002539 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002540 if (!listeners)
2541 goto out_sock_release;
2542
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002544 if (cfg && cfg->input)
2545 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002547 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002548 goto out_sock_release;
2549
2550 nlk = nlk_sk(sk);
2551 nlk->flags |= NETLINK_KERNEL_SOCKET;
2552
2553 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002554 if (!nl_table[unit].registered) {
2555 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002556 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002557 nl_table[unit].cb_mutex = cb_mutex;
2558 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002559 if (cfg) {
2560 nl_table[unit].bind = cfg->bind;
2561 nl_table[unit].flags = cfg->flags;
Gao fengda12c902013-06-06 14:49:11 +08002562 if (cfg->compare)
2563 nl_table[unit].compare = cfg->compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002564 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002565 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002566 } else {
2567 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002568 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002569 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002570 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002571 return sk;
2572
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002573out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002574 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002575 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002576 return NULL;
2577
2578out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002579 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002580 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002582EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002583
2584void
2585netlink_kernel_release(struct sock *sk)
2586{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002587 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002588}
2589EXPORT_SYMBOL(netlink_kernel_release);
2590
Johannes Bergd136f1b2009-09-12 03:03:15 +00002591int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002592{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002593 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002594 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002595
2596 if (groups < 32)
2597 groups = 32;
2598
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002599 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002600 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2601 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002602 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002603 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002604 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2605 rcu_assign_pointer(tbl->listeners, new);
2606
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002607 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002608 }
2609 tbl->groups = groups;
2610
Johannes Bergd136f1b2009-09-12 03:03:15 +00002611 return 0;
2612}
2613
2614/**
2615 * netlink_change_ngroups - change number of multicast groups
2616 *
2617 * This changes the number of multicast groups that are available
2618 * on a certain netlink family. Note that it is not possible to
2619 * change the number of groups to below 32. Also note that it does
2620 * not implicitly call netlink_clear_multicast_users() when the
2621 * number of groups is reduced.
2622 *
2623 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2624 * @groups: The new number of groups.
2625 */
2626int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2627{
2628 int err;
2629
2630 netlink_table_grab();
2631 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002632 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002633
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002634 return err;
2635}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002636
Johannes Bergb8273572009-09-24 15:44:05 -07002637void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2638{
2639 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002640 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2641
Sasha Levinb67bfe02013-02-27 17:06:00 -08002642 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002643 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2644}
2645
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002646struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002647__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002648{
2649 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002650 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002651
Wang Yufen23b45672014-02-17 16:53:32 +08002652 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002653 nlh->nlmsg_type = type;
2654 nlh->nlmsg_len = size;
2655 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002656 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002657 nlh->nlmsg_seq = seq;
2658 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002659 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002660 return nlh;
2661}
2662EXPORT_SYMBOL(__nlmsg_put);
2663
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664/*
2665 * It looks a bit ugly.
2666 * It would be better to create kernel thread.
2667 */
2668
2669static int netlink_dump(struct sock *sk)
2670{
2671 struct netlink_sock *nlk = nlk_sk(sk);
2672 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002673 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002675 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002676 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002678 mutex_lock(nlk->cb_mutex);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002679 if (!nlk->cb_running) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002680 err = -EINVAL;
2681 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002684 cb = &nlk->cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002685 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2686
Patrick McHardyf9c22882013-04-17 06:47:04 +00002687 if (!netlink_rx_is_mmaped(sk) &&
2688 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2689 goto errout_skb;
Eric Dumazet9063e212014-03-07 12:02:33 -08002690
2691 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2692 * required, but it makes sense to _attempt_ a 16K bytes allocation
2693 * to reduce number of system calls on dump operations, if user
2694 * ever provided a big enough buffer.
2695 */
2696 if (alloc_size < nlk->max_recvmsg_len) {
2697 skb = netlink_alloc_skb(sk,
2698 nlk->max_recvmsg_len,
2699 nlk->portid,
2700 GFP_KERNEL |
2701 __GFP_NOWARN |
2702 __GFP_NORETRY);
2703 /* available room should be exact amount to avoid MSG_TRUNC */
2704 if (skb)
2705 skb_reserve(skb, skb_tailroom(skb) -
2706 nlk->max_recvmsg_len);
2707 }
2708 if (!skb)
2709 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
2710 GFP_KERNEL);
Greg Rosec7ac8672011-06-10 01:27:09 +00002711 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002712 goto errout_skb;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002713 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 len = cb->dump(skb, cb);
2716
2717 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002718 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002719
2720 if (sk_filter(sk, skb))
2721 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002722 else
2723 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return 0;
2725 }
2726
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002727 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2728 if (!nlh)
2729 goto errout_skb;
2730
Johannes Berg670dc282011-06-20 13:40:46 +02002731 nl_dump_check_consistent(cb, nlh);
2732
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002733 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2734
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002735 if (sk_filter(sk, skb))
2736 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002737 else
2738 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Thomas Grafa8f74b22005-11-10 02:25:52 +01002740 if (cb->done)
2741 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002743 nlk->cb_running = false;
2744 mutex_unlock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002745 module_put(cb->module);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002746 consume_skb(cb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002748
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002749errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002750 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002751 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002752 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753}
2754
Gao feng6dc878a2012-10-04 20:15:48 +00002755int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2756 const struct nlmsghdr *nlh,
2757 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758{
2759 struct netlink_callback *cb;
2760 struct sock *sk;
2761 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002762 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
Patrick McHardyf9c22882013-04-17 06:47:04 +00002764 /* Memory mapped dump requests need to be copied to avoid looping
2765 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2766 * a reference to the skb.
2767 */
2768 if (netlink_skb_is_mmaped(skb)) {
2769 skb = skb_copy(skb, GFP_KERNEL);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002770 if (skb == NULL)
Patrick McHardyf9c22882013-04-17 06:47:04 +00002771 return -ENOBUFS;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002772 } else
2773 atomic_inc(&skb->users);
2774
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002775 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2776 if (sk == NULL) {
2777 ret = -ECONNREFUSED;
2778 goto error_free;
2779 }
2780
2781 nlk = nlk_sk(sk);
2782 mutex_lock(nlk->cb_mutex);
2783 /* A dump is in progress... */
2784 if (nlk->cb_running) {
2785 ret = -EBUSY;
2786 goto error_unlock;
2787 }
2788 /* add reference of module which cb->dump belongs to */
2789 if (!try_module_get(control->module)) {
2790 ret = -EPROTONOSUPPORT;
2791 goto error_unlock;
2792 }
2793
2794 cb = &nlk->cb;
2795 memset(cb, 0, sizeof(*cb));
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002796 cb->dump = control->dump;
2797 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002799 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002800 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002801 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 cb->skb = skb;
2803
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002804 nlk->cb_running = true;
Gao feng6dc878a2012-10-04 20:15:48 +00002805
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002806 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
Andrey Vaginb44d2112011-02-21 02:40:47 +00002808 ret = netlink_dump(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002810
Andrey Vaginb44d2112011-02-21 02:40:47 +00002811 if (ret)
2812 return ret;
2813
Denis V. Lunev5c582982007-10-23 20:29:25 -07002814 /* We successfully started a dump, by returning -EINTR we
2815 * signal not to send ACK even if it was requested.
2816 */
2817 return -EINTR;
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002818
2819error_unlock:
2820 sock_put(sk);
2821 mutex_unlock(nlk->cb_mutex);
2822error_free:
2823 kfree_skb(skb);
2824 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825}
Gao feng6dc878a2012-10-04 20:15:48 +00002826EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827
2828void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2829{
2830 struct sk_buff *skb;
2831 struct nlmsghdr *rep;
2832 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002833 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
Thomas Graf339bf982006-11-10 14:10:15 -08002835 /* error messages get the original request appened */
2836 if (err)
2837 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Patrick McHardyf9c22882013-04-17 06:47:04 +00002839 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2840 NETLINK_CB(in_skb).portid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 if (!skb) {
2842 struct sock *sk;
2843
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002844 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002845 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002846 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 if (sk) {
2848 sk->sk_err = ENOBUFS;
2849 sk->sk_error_report(sk);
2850 sock_put(sk);
2851 }
2852 return;
2853 }
2854
Eric W. Biederman15e47302012-09-07 20:12:54 +00002855 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002856 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002857 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002859 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002860 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002862EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002864int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002865 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002866{
Thomas Graf82ace472005-11-10 02:25:53 +01002867 struct nlmsghdr *nlh;
2868 int err;
2869
2870 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002871 int msglen;
2872
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002873 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002874 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002875
Martin Murrayad8e4b72006-01-10 13:02:29 -08002876 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002877 return 0;
2878
Thomas Grafd35b6852007-03-22 23:28:46 -07002879 /* Only requests are handled by the kernel */
2880 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002881 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002882
Thomas Graf45e7ae72007-03-22 23:29:10 -07002883 /* Skip control messages */
2884 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002885 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002886
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002887 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002888 if (err == -EINTR)
2889 goto skip;
2890
2891ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002892 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002893 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002894
Denis V. Lunev5c582982007-10-23 20:29:25 -07002895skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002896 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002897 if (msglen > skb->len)
2898 msglen = skb->len;
2899 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002900 }
2901
2902 return 0;
2903}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002904EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002905
2906/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002907 * nlmsg_notify - send a notification netlink message
2908 * @sk: netlink socket to use
2909 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002910 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002911 * @group: destination multicast group or 0
2912 * @report: 1 to report back, 0 to disable
2913 * @flags: allocation flags
2914 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002915int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002916 unsigned int group, int report, gfp_t flags)
2917{
2918 int err = 0;
2919
2920 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002921 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002922
2923 if (report) {
2924 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002925 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002926 }
2927
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002928 /* errors reported via destination sk->sk_err, but propagate
2929 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002930 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002931 }
2932
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002933 if (report) {
2934 int err2;
2935
Eric W. Biederman15e47302012-09-07 20:12:54 +00002936 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002937 if (!err || err == -ESRCH)
2938 err = err2;
2939 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002940
2941 return err;
2942}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002943EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945#ifdef CONFIG_PROC_FS
2946struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002947 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 int link;
2949 int hash_idx;
2950};
2951
2952static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2953{
2954 struct nl_seq_iter *iter = seq->private;
2955 int i, j;
2956 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 loff_t off = 0;
2958
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002959 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002960 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002963 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002964 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002965 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 if (off == pos) {
2967 iter->link = i;
2968 iter->hash_idx = j;
2969 return s;
2970 }
2971 ++off;
2972 }
2973 }
2974 }
2975 return NULL;
2976}
2977
2978static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002979 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980{
2981 read_lock(&nl_table_lock);
2982 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2983}
2984
2985static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2986{
2987 struct sock *s;
2988 struct nl_seq_iter *iter;
Gao fengda12c902013-06-06 14:49:11 +08002989 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 int i, j;
2991
2992 ++*pos;
2993
2994 if (v == SEQ_START_TOKEN)
2995 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002996
Gao fengda12c902013-06-06 14:49:11 +08002997 net = seq_file_net(seq);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002998 iter = seq->private;
2999 s = v;
3000 do {
3001 s = sk_next(s);
Gao fengda12c902013-06-06 14:49:11 +08003002 } while (s && !nl_table[s->sk_protocol].compare(net, s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 if (s)
3004 return s;
3005
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 i = iter->link;
3007 j = iter->hash_idx + 1;
3008
3009 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00003010 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011
3012 for (; j <= hash->mask; j++) {
3013 s = sk_head(&hash->table[j]);
Gao fengda12c902013-06-06 14:49:11 +08003014
3015 while (s && !nl_table[s->sk_protocol].compare(net, s))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003016 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (s) {
3018 iter->link = i;
3019 iter->hash_idx = j;
3020 return s;
3021 }
3022 }
3023
3024 j = 0;
3025 } while (++i < MAX_LINKS);
3026
3027 return NULL;
3028}
3029
3030static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08003031 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032{
3033 read_unlock(&nl_table_lock);
3034}
3035
3036
3037static int netlink_seq_show(struct seq_file *seq, void *v)
3038{
Eric Dumazet658cb352012-04-22 21:30:21 +00003039 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 seq_puts(seq,
3041 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00003042 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00003043 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 struct sock *s = v;
3045 struct netlink_sock *nlk = nlk_sk(s);
3046
Pravin B Shelar16b304f2013-08-15 15:31:06 -07003047 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 s,
3049 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003050 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07003051 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07003052 sk_rmem_alloc_get(s),
3053 sk_wmem_alloc_get(s),
Pravin B Shelar16b304f2013-08-15 15:31:06 -07003054 nlk->cb_running,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07003055 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00003056 atomic_read(&s->sk_drops),
3057 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 );
3059
3060 }
3061 return 0;
3062}
3063
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003064static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 .start = netlink_seq_start,
3066 .next = netlink_seq_next,
3067 .stop = netlink_seq_stop,
3068 .show = netlink_seq_show,
3069};
3070
3071
3072static int netlink_seq_open(struct inode *inode, struct file *file)
3073{
Denis V. Luneve372c412007-11-19 22:31:54 -08003074 return seq_open_net(inode, file, &netlink_seq_ops,
3075 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003076}
3077
Arjan van de Venda7071d2007-02-12 00:55:36 -08003078static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 .owner = THIS_MODULE,
3080 .open = netlink_seq_open,
3081 .read = seq_read,
3082 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003083 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084};
3085
3086#endif
3087
3088int netlink_register_notifier(struct notifier_block *nb)
3089{
Alan Sterne041c682006-03-27 01:16:30 -08003090 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003092EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
3094int netlink_unregister_notifier(struct notifier_block *nb)
3095{
Alan Sterne041c682006-03-27 01:16:30 -08003096 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003098EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003099
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003100static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 .family = PF_NETLINK,
3102 .owner = THIS_MODULE,
3103 .release = netlink_release,
3104 .bind = netlink_bind,
3105 .connect = netlink_connect,
3106 .socketpair = sock_no_socketpair,
3107 .accept = sock_no_accept,
3108 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00003109 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 .ioctl = sock_no_ioctl,
3111 .listen = sock_no_listen,
3112 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07003113 .setsockopt = netlink_setsockopt,
3114 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 .sendmsg = netlink_sendmsg,
3116 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00003117 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 .sendpage = sock_no_sendpage,
3119};
3120
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003121static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 .family = PF_NETLINK,
3123 .create = netlink_create,
3124 .owner = THIS_MODULE, /* for consistency 8) */
3125};
3126
Pavel Emelyanov46650792007-10-08 20:38:39 -07003127static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003128{
3129#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003130 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003131 return -ENOMEM;
3132#endif
3133 return 0;
3134}
3135
Pavel Emelyanov46650792007-10-08 20:38:39 -07003136static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003137{
3138#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003139 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003140#endif
3141}
3142
David S. Millerb963ea82010-08-30 19:08:01 -07003143static void __init netlink_add_usersock_entry(void)
3144{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003145 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07003146 int groups = 32;
3147
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003148 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07003149 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003150 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07003151
3152 netlink_table_grab();
3153
3154 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003155 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07003156 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
3157 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003158 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07003159
3160 netlink_table_ungrab();
3161}
3162
Denis V. Lunev022cbae2007-11-13 03:23:50 -08003163static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003164 .init = netlink_net_init,
3165 .exit = netlink_net_exit,
3166};
3167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168static int __init netlink_proto_init(void)
3169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07003171 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 unsigned int order;
3173 int err = proto_register(&netlink_proto, 0);
3174
3175 if (err != 0)
3176 goto out;
3177
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00003178 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07003180 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003181 if (!nl_table)
3182 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Jan Beulich44813742009-09-21 17:03:05 -07003184 if (totalram_pages >= (128 * 1024))
3185 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 else
Jan Beulich44813742009-09-21 17:03:05 -07003187 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Denis Cheng26ff5dd2007-09-16 16:36:02 -07003189 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
3190 limit = (1UL << order) / sizeof(struct hlist_head);
3191 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00003194 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195
Eric W. Biederman15e47302012-09-07 20:12:54 +00003196 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if (!hash->table) {
3198 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00003199 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 1 * sizeof(*hash->table));
3201 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003202 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 hash->max_shift = order;
3205 hash->shift = 0;
3206 hash->mask = 0;
3207 hash->rehash_time = jiffies;
Gao fengca15feb2013-06-13 10:05:38 +08003208
3209 nl_table[i].compare = netlink_compare;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 }
3211
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02003212 INIT_LIST_HEAD(&netlink_tap_all);
3213
David S. Millerb963ea82010-08-30 19:08:01 -07003214 netlink_add_usersock_entry();
3215
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003217 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003218 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 rtnetlink_init();
3220out:
3221 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003222panic:
3223 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224}
3225
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226core_initcall(netlink_proto_init);