blob: 4856d975492d9a82ef3bf8cdc4376c22643062bf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Patrick McHardycd1df522013-04-17 06:47:05 +00006 * Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
14 * added netlink_proto_exit
15 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
16 * use nlk_sk, as sk->protinfo is on a diet 8)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070017 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
18 * - inc module use count of module that owns
19 * the kernel socket in case userspace opens
20 * socket of same protocol
21 * - remove all module support, since netlink is
22 * mandatory if CONFIG_NET=y these days
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/errno.h>
33#include <linux/string.h>
34#include <linux/stat.h>
35#include <linux/socket.h>
36#include <linux/un.h>
37#include <linux/fcntl.h>
38#include <linux/termios.h>
39#include <linux/sockios.h>
40#include <linux/net.h>
41#include <linux/fs.h>
42#include <linux/slab.h>
43#include <asm/uaccess.h>
44#include <linux/skbuff.h>
45#include <linux/netdevice.h>
46#include <linux/rtnetlink.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/notifier.h>
50#include <linux/security.h>
51#include <linux/jhash.h>
52#include <linux/jiffies.h>
53#include <linux/random.h>
54#include <linux/bitops.h>
55#include <linux/mm.h>
56#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010057#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070058#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000059#include <linux/vmalloc.h>
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +020060#include <linux/if_arp.h>
Thomas Grafe3416942014-08-02 11:47:45 +020061#include <linux/rhashtable.h>
Patrick McHardy9652e932013-04-17 06:47:02 +000062#include <asm/cacheflush.h>
Thomas Grafe3416942014-08-02 11:47:45 +020063#include <linux/hash.h>
Johannes Bergee1c24422015-01-16 11:37:14 +010064#include <linux/genetlink.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010065
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020066#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <net/sock.h>
68#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010069#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Andrey Vagin0f29c762013-03-21 20:33:47 +040071#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Eric Dumazet5c398dc2010-10-24 04:27:10 +000073struct listeners {
74 struct rcu_head rcu;
75 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000076};
77
Patrick McHardycd967e02013-04-17 06:46:56 +000078/* state bits */
79#define NETLINK_CONGESTED 0x0
80
81/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070082#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070083#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000084#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070085#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070086
David S. Miller035c4c12011-12-23 17:33:03 -050087static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070088{
89 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
90}
91
Eric Dumazet91dd93f2015-05-12 17:24:50 -070092struct netlink_table *nl_table __read_mostly;
Andrey Vagin0f29c762013-03-21 20:33:47 +040093EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
96
97static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000098static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Thomas Graf78fd1d02014-10-21 22:05:38 +0200100/* nl_table locking explained:
Thomas Graf21e49022015-01-02 23:00:22 +0100101 * Lookup and traversal are protected with an RCU read-side lock. Insertion
Ying Xuec5adde92015-01-12 14:52:23 +0800102 * and removal are protected with per bucket lock while using RCU list
Thomas Graf21e49022015-01-02 23:00:22 +0100103 * modification primitives and may run in parallel to RCU protected lookups.
104 * Destruction of the Netlink socket may only occur *after* nl_table_lock has
105 * been acquired * either during or after the socket has been removed from
106 * the list and after an RCU grace period.
Thomas Graf78fd1d02014-10-21 22:05:38 +0200107 */
Andrey Vagin0f29c762013-03-21 20:33:47 +0400108DEFINE_RWLOCK(nl_table_lock);
109EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static atomic_t nl_table_users = ATOMIC_INIT(0);
111
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000112#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
113
Alan Sterne041c682006-03-27 01:16:30 -0800114static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200116static DEFINE_SPINLOCK(netlink_tap_lock);
117static struct list_head netlink_tap_all __read_mostly;
118
Herbert Xuc428ecd2015-03-20 21:57:01 +1100119static const struct rhashtable_params netlink_rhashtable_params;
120
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000121static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700122{
123 return group ? 1 << (group - 1) : 0;
124}
125
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200126int netlink_add_tap(struct netlink_tap *nt)
127{
128 if (unlikely(nt->dev->type != ARPHRD_NETLINK))
129 return -EINVAL;
130
131 spin_lock(&netlink_tap_lock);
132 list_add_rcu(&nt->list, &netlink_tap_all);
133 spin_unlock(&netlink_tap_lock);
134
Markus Elfringfcd4d352014-11-18 21:03:13 +0100135 __module_get(nt->module);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200136
137 return 0;
138}
139EXPORT_SYMBOL_GPL(netlink_add_tap);
140
stephen hemminger2173f8d2013-12-30 10:49:22 -0800141static int __netlink_remove_tap(struct netlink_tap *nt)
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200142{
143 bool found = false;
144 struct netlink_tap *tmp;
145
146 spin_lock(&netlink_tap_lock);
147
148 list_for_each_entry(tmp, &netlink_tap_all, list) {
149 if (nt == tmp) {
150 list_del_rcu(&nt->list);
151 found = true;
152 goto out;
153 }
154 }
155
156 pr_warn("__netlink_remove_tap: %p not found\n", nt);
157out:
158 spin_unlock(&netlink_tap_lock);
159
160 if (found && nt->module)
161 module_put(nt->module);
162
163 return found ? 0 : -ENODEV;
164}
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200165
166int netlink_remove_tap(struct netlink_tap *nt)
167{
168 int ret;
169
170 ret = __netlink_remove_tap(nt);
171 synchronize_net();
172
173 return ret;
174}
175EXPORT_SYMBOL_GPL(netlink_remove_tap);
176
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200177static bool netlink_filter_tap(const struct sk_buff *skb)
178{
179 struct sock *sk = skb->sk;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200180
181 /* We take the more conservative approach and
182 * whitelist socket protocols that may pass.
183 */
184 switch (sk->sk_protocol) {
185 case NETLINK_ROUTE:
186 case NETLINK_USERSOCK:
187 case NETLINK_SOCK_DIAG:
188 case NETLINK_NFLOG:
189 case NETLINK_XFRM:
190 case NETLINK_FIB_LOOKUP:
191 case NETLINK_NETFILTER:
192 case NETLINK_GENERIC:
Varka Bhadram498044b2014-07-16 10:59:47 +0530193 return true;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200194 }
195
Varka Bhadram498044b2014-07-16 10:59:47 +0530196 return false;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200197}
198
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200199static int __netlink_deliver_tap_skb(struct sk_buff *skb,
200 struct net_device *dev)
201{
202 struct sk_buff *nskb;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200203 struct sock *sk = skb->sk;
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200204 int ret = -ENOMEM;
205
206 dev_hold(dev);
207 nskb = skb_clone(skb, GFP_ATOMIC);
208 if (nskb) {
209 nskb->dev = dev;
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200210 nskb->protocol = htons((u16) sk->sk_protocol);
Daniel Borkmann604d13c2013-12-23 14:35:56 +0100211 nskb->pkt_type = netlink_is_kernel(sk) ?
212 PACKET_KERNEL : PACKET_USER;
Daniel Borkmann4e48ed82014-08-07 22:22:47 +0200213 skb_reset_network_header(nskb);
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200214 ret = dev_queue_xmit(nskb);
215 if (unlikely(ret > 0))
216 ret = net_xmit_errno(ret);
217 }
218
219 dev_put(dev);
220 return ret;
221}
222
223static void __netlink_deliver_tap(struct sk_buff *skb)
224{
225 int ret;
226 struct netlink_tap *tmp;
227
Daniel Borkmann5ffd5cd2013-09-05 17:48:47 +0200228 if (!netlink_filter_tap(skb))
229 return;
230
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +0200231 list_for_each_entry_rcu(tmp, &netlink_tap_all, list) {
232 ret = __netlink_deliver_tap_skb(skb, tmp->dev);
233 if (unlikely(ret))
234 break;
235 }
236}
237
238static void netlink_deliver_tap(struct sk_buff *skb)
239{
240 rcu_read_lock();
241
242 if (unlikely(!list_empty(&netlink_tap_all)))
243 __netlink_deliver_tap(skb);
244
245 rcu_read_unlock();
246}
247
Daniel Borkmann73bfd372013-12-23 14:35:55 +0100248static void netlink_deliver_tap_kernel(struct sock *dst, struct sock *src,
249 struct sk_buff *skb)
250{
251 if (!(netlink_is_kernel(dst) && netlink_is_kernel(src)))
252 netlink_deliver_tap(skb);
253}
254
Patrick McHardycd1df522013-04-17 06:47:05 +0000255static void netlink_overrun(struct sock *sk)
256{
257 struct netlink_sock *nlk = nlk_sk(sk);
258
259 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
260 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
261 sk->sk_err = ENOBUFS;
262 sk->sk_error_report(sk);
263 }
264 }
265 atomic_inc(&sk->sk_drops);
266}
267
268static void netlink_rcv_wake(struct sock *sk)
269{
270 struct netlink_sock *nlk = nlk_sk(sk);
271
272 if (skb_queue_empty(&sk->sk_receive_queue))
273 clear_bit(NETLINK_CONGESTED, &nlk->state);
274 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
275 wake_up_interruptible(&nlk->wait);
276}
277
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000278#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000279static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
280{
281 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
282}
283
Patrick McHardyf9c22882013-04-17 06:47:04 +0000284static bool netlink_rx_is_mmaped(struct sock *sk)
285{
286 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
287}
288
Patrick McHardy5fd96122013-04-17 06:47:03 +0000289static bool netlink_tx_is_mmaped(struct sock *sk)
290{
291 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
292}
293
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000294static __pure struct page *pgvec_to_page(const void *addr)
295{
296 if (is_vmalloc_addr(addr))
297 return vmalloc_to_page(addr);
298 else
299 return virt_to_page(addr);
300}
301
302static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
303{
304 unsigned int i;
305
306 for (i = 0; i < len; i++) {
307 if (pg_vec[i] != NULL) {
308 if (is_vmalloc_addr(pg_vec[i]))
309 vfree(pg_vec[i]);
310 else
311 free_pages((unsigned long)pg_vec[i], order);
312 }
313 }
314 kfree(pg_vec);
315}
316
317static void *alloc_one_pg_vec_page(unsigned long order)
318{
319 void *buffer;
320 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
321 __GFP_NOWARN | __GFP_NORETRY;
322
323 buffer = (void *)__get_free_pages(gfp_flags, order);
324 if (buffer != NULL)
325 return buffer;
326
327 buffer = vzalloc((1 << order) * PAGE_SIZE);
328 if (buffer != NULL)
329 return buffer;
330
331 gfp_flags &= ~__GFP_NORETRY;
332 return (void *)__get_free_pages(gfp_flags, order);
333}
334
335static void **alloc_pg_vec(struct netlink_sock *nlk,
336 struct nl_mmap_req *req, unsigned int order)
337{
338 unsigned int block_nr = req->nm_block_nr;
339 unsigned int i;
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200340 void **pg_vec;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000341
342 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
343 if (pg_vec == NULL)
344 return NULL;
345
346 for (i = 0; i < block_nr; i++) {
Daniel Borkmann8a849bb2013-08-02 17:32:39 +0200347 pg_vec[i] = alloc_one_pg_vec_page(order);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000348 if (pg_vec[i] == NULL)
349 goto err1;
350 }
351
352 return pg_vec;
353err1:
354 free_pg_vec(pg_vec, order, block_nr);
355 return NULL;
356}
357
Florian Westphalb265c302015-07-21 16:33:50 +0200358
359static void
360__netlink_set_ring(struct sock *sk, struct nl_mmap_req *req, bool tx_ring, void **pg_vec,
361 unsigned int order)
362{
363 struct netlink_sock *nlk = nlk_sk(sk);
364 struct sk_buff_head *queue;
365 struct netlink_ring *ring;
366
367 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
368 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
369
370 spin_lock_bh(&queue->lock);
371
372 ring->frame_max = req->nm_frame_nr - 1;
373 ring->head = 0;
374 ring->frame_size = req->nm_frame_size;
375 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
376
377 swap(ring->pg_vec_len, req->nm_block_nr);
378 swap(ring->pg_vec_order, order);
379 swap(ring->pg_vec, pg_vec);
380
381 __skb_queue_purge(queue);
382 spin_unlock_bh(&queue->lock);
383
384 WARN_ON(atomic_read(&nlk->mapped));
385
386 if (pg_vec)
387 free_pg_vec(pg_vec, order, req->nm_block_nr);
388}
389
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000390static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
Florian Westphalb265c302015-07-21 16:33:50 +0200391 bool tx_ring)
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000392{
393 struct netlink_sock *nlk = nlk_sk(sk);
394 struct netlink_ring *ring;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000395 void **pg_vec = NULL;
396 unsigned int order = 0;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000397
398 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000399
Florian Westphalb265c302015-07-21 16:33:50 +0200400 if (atomic_read(&nlk->mapped))
401 return -EBUSY;
402 if (atomic_read(&ring->pending))
403 return -EBUSY;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000404
405 if (req->nm_block_nr) {
406 if (ring->pg_vec != NULL)
407 return -EBUSY;
408
409 if ((int)req->nm_block_size <= 0)
410 return -EINVAL;
Tobias Klauser74e83b22014-07-31 12:17:08 +0200411 if (!PAGE_ALIGNED(req->nm_block_size))
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000412 return -EINVAL;
413 if (req->nm_frame_size < NL_MMAP_HDRLEN)
414 return -EINVAL;
415 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
416 return -EINVAL;
417
418 ring->frames_per_block = req->nm_block_size /
419 req->nm_frame_size;
420 if (ring->frames_per_block == 0)
421 return -EINVAL;
422 if (ring->frames_per_block * req->nm_block_nr !=
423 req->nm_frame_nr)
424 return -EINVAL;
425
426 order = get_order(req->nm_block_size);
427 pg_vec = alloc_pg_vec(nlk, req, order);
428 if (pg_vec == NULL)
429 return -ENOMEM;
430 } else {
431 if (req->nm_frame_nr)
432 return -EINVAL;
433 }
434
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000435 mutex_lock(&nlk->pg_vec_lock);
Florian Westphalb265c302015-07-21 16:33:50 +0200436 if (atomic_read(&nlk->mapped) == 0) {
437 __netlink_set_ring(sk, req, tx_ring, pg_vec, order);
438 mutex_unlock(&nlk->pg_vec_lock);
439 return 0;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000440 }
Florian Westphalb265c302015-07-21 16:33:50 +0200441
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000442 mutex_unlock(&nlk->pg_vec_lock);
443
444 if (pg_vec)
445 free_pg_vec(pg_vec, order, req->nm_block_nr);
Florian Westphalb265c302015-07-21 16:33:50 +0200446
447 return -EBUSY;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000448}
449
450static void netlink_mm_open(struct vm_area_struct *vma)
451{
452 struct file *file = vma->vm_file;
453 struct socket *sock = file->private_data;
454 struct sock *sk = sock->sk;
455
456 if (sk)
457 atomic_inc(&nlk_sk(sk)->mapped);
458}
459
460static void netlink_mm_close(struct vm_area_struct *vma)
461{
462 struct file *file = vma->vm_file;
463 struct socket *sock = file->private_data;
464 struct sock *sk = sock->sk;
465
466 if (sk)
467 atomic_dec(&nlk_sk(sk)->mapped);
468}
469
470static const struct vm_operations_struct netlink_mmap_ops = {
471 .open = netlink_mm_open,
472 .close = netlink_mm_close,
473};
474
475static int netlink_mmap(struct file *file, struct socket *sock,
476 struct vm_area_struct *vma)
477{
478 struct sock *sk = sock->sk;
479 struct netlink_sock *nlk = nlk_sk(sk);
480 struct netlink_ring *ring;
481 unsigned long start, size, expected;
482 unsigned int i;
483 int err = -EINVAL;
484
485 if (vma->vm_pgoff)
486 return -EINVAL;
487
488 mutex_lock(&nlk->pg_vec_lock);
489
490 expected = 0;
491 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
492 if (ring->pg_vec == NULL)
493 continue;
494 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
495 }
496
497 if (expected == 0)
498 goto out;
499
500 size = vma->vm_end - vma->vm_start;
501 if (size != expected)
502 goto out;
503
504 start = vma->vm_start;
505 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
506 if (ring->pg_vec == NULL)
507 continue;
508
509 for (i = 0; i < ring->pg_vec_len; i++) {
510 struct page *page;
511 void *kaddr = ring->pg_vec[i];
512 unsigned int pg_num;
513
514 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
515 page = pgvec_to_page(kaddr);
516 err = vm_insert_page(vma, start, page);
517 if (err < 0)
518 goto out;
519 start += PAGE_SIZE;
520 kaddr += PAGE_SIZE;
521 }
522 }
523 }
524
525 atomic_inc(&nlk->mapped);
526 vma->vm_ops = &netlink_mmap_ops;
527 err = 0;
528out:
529 mutex_unlock(&nlk->pg_vec_lock);
Patrick McHardy7cdbac72013-06-11 02:52:47 -0700530 return err;
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000531}
Patrick McHardy9652e932013-04-17 06:47:02 +0000532
David Miller4682a032014-12-16 17:58:17 -0500533static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr, unsigned int nm_len)
Patrick McHardy9652e932013-04-17 06:47:02 +0000534{
535#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
536 struct page *p_start, *p_end;
537
538 /* First page is flushed through netlink_{get,set}_status */
539 p_start = pgvec_to_page(hdr + PAGE_SIZE);
David Miller4682a032014-12-16 17:58:17 -0500540 p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + nm_len - 1);
Patrick McHardy9652e932013-04-17 06:47:02 +0000541 while (p_start <= p_end) {
542 flush_dcache_page(p_start);
543 p_start++;
544 }
545#endif
546}
547
548static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
549{
550 smp_rmb();
551 flush_dcache_page(pgvec_to_page(hdr));
552 return hdr->nm_status;
553}
554
555static void netlink_set_status(struct nl_mmap_hdr *hdr,
556 enum nl_mmap_status status)
557{
Thomas Grafa18e6a12014-12-18 10:30:26 +0000558 smp_mb();
Patrick McHardy9652e932013-04-17 06:47:02 +0000559 hdr->nm_status = status;
560 flush_dcache_page(pgvec_to_page(hdr));
Patrick McHardy9652e932013-04-17 06:47:02 +0000561}
562
563static struct nl_mmap_hdr *
564__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
565{
566 unsigned int pg_vec_pos, frame_off;
567
568 pg_vec_pos = pos / ring->frames_per_block;
569 frame_off = pos % ring->frames_per_block;
570
571 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
572}
573
574static struct nl_mmap_hdr *
575netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
576 enum nl_mmap_status status)
577{
578 struct nl_mmap_hdr *hdr;
579
580 hdr = __netlink_lookup_frame(ring, pos);
581 if (netlink_get_status(hdr) != status)
582 return NULL;
583
584 return hdr;
585}
586
587static struct nl_mmap_hdr *
588netlink_current_frame(const struct netlink_ring *ring,
589 enum nl_mmap_status status)
590{
591 return netlink_lookup_frame(ring, ring->head, status);
592}
593
594static struct nl_mmap_hdr *
595netlink_previous_frame(const struct netlink_ring *ring,
596 enum nl_mmap_status status)
597{
598 unsigned int prev;
599
600 prev = ring->head ? ring->head - 1 : ring->frame_max;
601 return netlink_lookup_frame(ring, prev, status);
602}
603
604static void netlink_increment_head(struct netlink_ring *ring)
605{
606 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
607}
608
609static void netlink_forward_ring(struct netlink_ring *ring)
610{
611 unsigned int head = ring->head, pos = head;
612 const struct nl_mmap_hdr *hdr;
613
614 do {
615 hdr = __netlink_lookup_frame(ring, pos);
616 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
617 break;
618 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
619 break;
620 netlink_increment_head(ring);
621 } while (ring->head != head);
622}
623
Patrick McHardycd1df522013-04-17 06:47:05 +0000624static bool netlink_dump_space(struct netlink_sock *nlk)
625{
626 struct netlink_ring *ring = &nlk->rx_ring;
627 struct nl_mmap_hdr *hdr;
628 unsigned int n;
629
630 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
631 if (hdr == NULL)
632 return false;
633
634 n = ring->head + ring->frame_max / 2;
635 if (n > ring->frame_max)
636 n -= ring->frame_max;
637
638 hdr = __netlink_lookup_frame(ring, n);
639
640 return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
641}
642
Patrick McHardy9652e932013-04-17 06:47:02 +0000643static unsigned int netlink_poll(struct file *file, struct socket *sock,
644 poll_table *wait)
645{
646 struct sock *sk = sock->sk;
647 struct netlink_sock *nlk = nlk_sk(sk);
648 unsigned int mask;
Patrick McHardycd1df522013-04-17 06:47:05 +0000649 int err;
Patrick McHardy9652e932013-04-17 06:47:02 +0000650
Patrick McHardycd1df522013-04-17 06:47:05 +0000651 if (nlk->rx_ring.pg_vec != NULL) {
652 /* Memory mapped sockets don't call recvmsg(), so flow control
653 * for dumps is performed here. A dump is allowed to continue
654 * if at least half the ring is unused.
655 */
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700656 while (nlk->cb_running && netlink_dump_space(nlk)) {
Patrick McHardycd1df522013-04-17 06:47:05 +0000657 err = netlink_dump(sk);
658 if (err < 0) {
Ben Pfaffac30ef82014-07-09 10:31:22 -0700659 sk->sk_err = -err;
Patrick McHardycd1df522013-04-17 06:47:05 +0000660 sk->sk_error_report(sk);
661 break;
662 }
663 }
664 netlink_rcv_wake(sk);
665 }
Patrick McHardy5fd96122013-04-17 06:47:03 +0000666
Patrick McHardy9652e932013-04-17 06:47:02 +0000667 mask = datagram_poll(file, sock, wait);
668
669 spin_lock_bh(&sk->sk_receive_queue.lock);
670 if (nlk->rx_ring.pg_vec) {
671 netlink_forward_ring(&nlk->rx_ring);
672 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
673 mask |= POLLIN | POLLRDNORM;
674 }
675 spin_unlock_bh(&sk->sk_receive_queue.lock);
676
677 spin_lock_bh(&sk->sk_write_queue.lock);
678 if (nlk->tx_ring.pg_vec) {
679 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
680 mask |= POLLOUT | POLLWRNORM;
681 }
682 spin_unlock_bh(&sk->sk_write_queue.lock);
683
684 return mask;
685}
686
687static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
688{
689 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
690}
691
692static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
693 struct netlink_ring *ring,
694 struct nl_mmap_hdr *hdr)
695{
696 unsigned int size;
697 void *data;
698
699 size = ring->frame_size - NL_MMAP_HDRLEN;
700 data = (void *)hdr + NL_MMAP_HDRLEN;
701
702 skb->head = data;
703 skb->data = data;
704 skb_reset_tail_pointer(skb);
705 skb->end = skb->tail + size;
706 skb->len = 0;
707
708 skb->destructor = netlink_skb_destructor;
709 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
710 NETLINK_CB(skb).sk = sk;
711}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000712
713static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
714 u32 dst_portid, u32 dst_group,
Christoph Hellwig7cc05662015-01-28 18:04:53 +0100715 struct scm_cookie *scm)
Patrick McHardy5fd96122013-04-17 06:47:03 +0000716{
717 struct netlink_sock *nlk = nlk_sk(sk);
718 struct netlink_ring *ring;
719 struct nl_mmap_hdr *hdr;
720 struct sk_buff *skb;
721 unsigned int maxlen;
Patrick McHardy5fd96122013-04-17 06:47:03 +0000722 int err = 0, len = 0;
723
Patrick McHardy5fd96122013-04-17 06:47:03 +0000724 mutex_lock(&nlk->pg_vec_lock);
725
726 ring = &nlk->tx_ring;
727 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
728
729 do {
David Miller4682a032014-12-16 17:58:17 -0500730 unsigned int nm_len;
731
Patrick McHardy5fd96122013-04-17 06:47:03 +0000732 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
733 if (hdr == NULL) {
734 if (!(msg->msg_flags & MSG_DONTWAIT) &&
735 atomic_read(&nlk->tx_ring.pending))
736 schedule();
737 continue;
738 }
David Miller4682a032014-12-16 17:58:17 -0500739
740 nm_len = ACCESS_ONCE(hdr->nm_len);
741 if (nm_len > maxlen) {
Patrick McHardy5fd96122013-04-17 06:47:03 +0000742 err = -EINVAL;
743 goto out;
744 }
745
David Miller4682a032014-12-16 17:58:17 -0500746 netlink_frame_flush_dcache(hdr, nm_len);
Patrick McHardy5fd96122013-04-17 06:47:03 +0000747
David Miller4682a032014-12-16 17:58:17 -0500748 skb = alloc_skb(nm_len, GFP_KERNEL);
749 if (skb == NULL) {
750 err = -ENOBUFS;
751 goto out;
Patrick McHardy5fd96122013-04-17 06:47:03 +0000752 }
David Miller4682a032014-12-16 17:58:17 -0500753 __skb_put(skb, nm_len);
754 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, nm_len);
755 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
Patrick McHardy5fd96122013-04-17 06:47:03 +0000756
757 netlink_increment_head(ring);
758
759 NETLINK_CB(skb).portid = nlk->portid;
760 NETLINK_CB(skb).dst_group = dst_group;
Christoph Hellwig7cc05662015-01-28 18:04:53 +0100761 NETLINK_CB(skb).creds = scm->creds;
Patrick McHardy5fd96122013-04-17 06:47:03 +0000762
763 err = security_netlink_send(sk, skb);
764 if (err) {
765 kfree_skb(skb);
766 goto out;
767 }
768
769 if (unlikely(dst_group)) {
770 atomic_inc(&skb->users);
771 netlink_broadcast(sk, skb, dst_portid, dst_group,
772 GFP_KERNEL);
773 }
774 err = netlink_unicast(sk, skb, dst_portid,
775 msg->msg_flags & MSG_DONTWAIT);
776 if (err < 0)
777 goto out;
778 len += err;
779
780 } while (hdr != NULL ||
781 (!(msg->msg_flags & MSG_DONTWAIT) &&
782 atomic_read(&nlk->tx_ring.pending)));
783
784 if (len > 0)
785 err = len;
786out:
787 mutex_unlock(&nlk->pg_vec_lock);
788 return err;
789}
Patrick McHardyf9c22882013-04-17 06:47:04 +0000790
791static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
792{
793 struct nl_mmap_hdr *hdr;
794
795 hdr = netlink_mmap_hdr(skb);
796 hdr->nm_len = skb->len;
797 hdr->nm_group = NETLINK_CB(skb).dst_group;
798 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200799 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
800 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
David Miller4682a032014-12-16 17:58:17 -0500801 netlink_frame_flush_dcache(hdr, hdr->nm_len);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000802 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
803
804 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
805 kfree_skb(skb);
806}
807
808static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
809{
810 struct netlink_sock *nlk = nlk_sk(sk);
811 struct netlink_ring *ring = &nlk->rx_ring;
812 struct nl_mmap_hdr *hdr;
813
814 spin_lock_bh(&sk->sk_receive_queue.lock);
815 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
816 if (hdr == NULL) {
817 spin_unlock_bh(&sk->sk_receive_queue.lock);
818 kfree_skb(skb);
Patrick McHardycd1df522013-04-17 06:47:05 +0000819 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000820 return;
821 }
822 netlink_increment_head(ring);
823 __skb_queue_tail(&sk->sk_receive_queue, skb);
824 spin_unlock_bh(&sk->sk_receive_queue.lock);
825
826 hdr->nm_len = skb->len;
827 hdr->nm_group = NETLINK_CB(skb).dst_group;
828 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200829 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
830 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000831 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
832}
833
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000834#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000835#define netlink_skb_is_mmaped(skb) false
Patrick McHardyf9c22882013-04-17 06:47:04 +0000836#define netlink_rx_is_mmaped(sk) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000837#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000838#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000839#define netlink_poll datagram_poll
Christoph Hellwig7cc05662015-01-28 18:04:53 +0100840#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, scm) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000841#endif /* CONFIG_NETLINK_MMAP */
842
Patrick McHardycf0a0182013-04-17 06:47:00 +0000843static void netlink_skb_destructor(struct sk_buff *skb)
844{
Patrick McHardy9652e932013-04-17 06:47:02 +0000845#ifdef CONFIG_NETLINK_MMAP
846 struct nl_mmap_hdr *hdr;
847 struct netlink_ring *ring;
848 struct sock *sk;
849
850 /* If a packet from the kernel to userspace was freed because of an
851 * error without being delivered to userspace, the kernel must reset
852 * the status. In the direction userspace to kernel, the status is
853 * always reset here after the packet was processed and freed.
854 */
855 if (netlink_skb_is_mmaped(skb)) {
856 hdr = netlink_mmap_hdr(skb);
857 sk = NETLINK_CB(skb).sk;
858
Patrick McHardy5fd96122013-04-17 06:47:03 +0000859 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
860 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
861 ring = &nlk_sk(sk)->tx_ring;
862 } else {
863 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
864 hdr->nm_len = 0;
865 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
866 }
867 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000868 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000869
870 WARN_ON(atomic_read(&ring->pending) == 0);
871 atomic_dec(&ring->pending);
872 sock_put(sk);
873
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000874 skb->head = NULL;
Patrick McHardy9652e932013-04-17 06:47:02 +0000875 }
876#endif
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000877 if (is_vmalloc_addr(skb->head)) {
Pablo Neira3a365152013-06-28 03:04:23 +0200878 if (!skb->cloned ||
879 !atomic_dec_return(&(skb_shinfo(skb)->dataref)))
880 vfree(skb->head);
881
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000882 skb->head = NULL;
883 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000884 if (skb->sk != NULL)
885 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000886}
887
888static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
889{
890 WARN_ON(skb->sk != NULL);
891 skb->sk = sk;
892 skb->destructor = netlink_skb_destructor;
893 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
894 sk_mem_charge(sk, skb->truesize);
895}
896
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897static void netlink_sock_destruct(struct sock *sk)
898{
Herbert Xu3f660d62007-05-03 03:17:14 -0700899 struct netlink_sock *nlk = nlk_sk(sk);
900
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700901 if (nlk->cb_running) {
902 if (nlk->cb.done)
903 nlk->cb.done(&nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000904
Pravin B Shelar16b304f2013-08-15 15:31:06 -0700905 module_put(nlk->cb.module);
906 kfree_skb(nlk->cb.skb);
Herbert Xu3f660d62007-05-03 03:17:14 -0700907 }
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000910#ifdef CONFIG_NETLINK_MMAP
911 if (1) {
912 struct nl_mmap_req req;
913
914 memset(&req, 0, sizeof(req));
915 if (nlk->rx_ring.pg_vec)
Florian Westphalb265c302015-07-21 16:33:50 +0200916 __netlink_set_ring(sk, &req, false, NULL, 0);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000917 memset(&req, 0, sizeof(req));
918 if (nlk->tx_ring.pg_vec)
Florian Westphalb265c302015-07-21 16:33:50 +0200919 __netlink_set_ring(sk, &req, true, NULL, 0);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000920 }
921#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800924 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return;
926 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700927
928 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
929 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
930 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931}
932
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800933/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
934 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
936 * this, _but_ remember, it adds useless work on UP machines.
937 */
938
Johannes Bergd136f1b2009-09-12 03:03:15 +0000939void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800940 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000942 might_sleep();
943
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700944 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
946 if (atomic_read(&nl_table_users)) {
947 DECLARE_WAITQUEUE(wait, current);
948
949 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800950 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 set_current_state(TASK_UNINTERRUPTIBLE);
952 if (atomic_read(&nl_table_users) == 0)
953 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700954 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700956 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 }
958
959 __set_current_state(TASK_RUNNING);
960 remove_wait_queue(&nl_table_wait, &wait);
961 }
962}
963
Johannes Bergd136f1b2009-09-12 03:03:15 +0000964void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800965 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700967 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 wake_up(&nl_table_wait);
969}
970
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800971static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972netlink_lock_table(void)
973{
974 /* read_lock() synchronizes us to netlink_table_grab */
975
976 read_lock(&nl_table_lock);
977 atomic_inc(&nl_table_users);
978 read_unlock(&nl_table_lock);
979}
980
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800981static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982netlink_unlock_table(void)
983{
984 if (atomic_dec_and_test(&nl_table_users))
985 wake_up(&nl_table_wait);
986}
987
Thomas Grafe3416942014-08-02 11:47:45 +0200988struct netlink_compare_arg
Gao fengda12c902013-06-06 14:49:11 +0800989{
Herbert Xuc428ecd2015-03-20 21:57:01 +1100990 possible_net_t pnet;
Thomas Grafe3416942014-08-02 11:47:45 +0200991 u32 portid;
992};
993
Herbert Xu8f2ddaa2015-03-21 14:14:03 +1100994/* Doing sizeof directly may yield 4 extra bytes on 64-bit. */
995#define netlink_compare_arg_len \
996 (offsetof(struct netlink_compare_arg, portid) + sizeof(u32))
Thomas Grafe3416942014-08-02 11:47:45 +0200997
Herbert Xuc428ecd2015-03-20 21:57:01 +1100998static inline int netlink_compare(struct rhashtable_compare_arg *arg,
999 const void *ptr)
1000{
1001 const struct netlink_compare_arg *x = arg->key;
1002 const struct netlink_sock *nlk = ptr;
1003
1004 return nlk->portid != x->portid ||
1005 !net_eq(sock_net(&nlk->sk), read_pnet(&x->pnet));
1006}
1007
1008static void netlink_compare_arg_init(struct netlink_compare_arg *arg,
1009 struct net *net, u32 portid)
1010{
1011 memset(arg, 0, sizeof(*arg));
1012 write_pnet(&arg->pnet, net);
1013 arg->portid = portid;
Thomas Grafe3416942014-08-02 11:47:45 +02001014}
1015
1016static struct sock *__netlink_lookup(struct netlink_table *table, u32 portid,
1017 struct net *net)
1018{
Herbert Xuc428ecd2015-03-20 21:57:01 +11001019 struct netlink_compare_arg arg;
Thomas Grafe3416942014-08-02 11:47:45 +02001020
Herbert Xuc428ecd2015-03-20 21:57:01 +11001021 netlink_compare_arg_init(&arg, net, portid);
1022 return rhashtable_lookup_fast(&table->hash, &arg,
1023 netlink_rhashtable_params);
Gao fengda12c902013-06-06 14:49:11 +08001024}
1025
Herbert Xuc428ecd2015-03-20 21:57:01 +11001026static int __netlink_insert(struct netlink_table *table, struct sock *sk)
Ying Xuec5adde92015-01-12 14:52:23 +08001027{
Herbert Xuc428ecd2015-03-20 21:57:01 +11001028 struct netlink_compare_arg arg;
Ying Xuec5adde92015-01-12 14:52:23 +08001029
Herbert Xuc428ecd2015-03-20 21:57:01 +11001030 netlink_compare_arg_init(&arg, sock_net(sk), nlk_sk(sk)->portid);
1031 return rhashtable_lookup_insert_key(&table->hash, &arg,
1032 &nlk_sk(sk)->node,
1033 netlink_rhashtable_params);
Ying Xuec5adde92015-01-12 14:52:23 +08001034}
1035
Eric W. Biederman15e47302012-09-07 20:12:54 +00001036static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037{
Gao fengda12c902013-06-06 14:49:11 +08001038 struct netlink_table *table = &nl_table[protocol];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Thomas Grafe3416942014-08-02 11:47:45 +02001041 rcu_read_lock();
1042 sk = __netlink_lookup(table, portid, net);
1043 if (sk)
1044 sock_hold(sk);
1045 rcu_read_unlock();
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 return sk;
1048}
1049
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08001050static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Patrick McHardy4277a082006-03-20 18:52:01 -08001052static void
1053netlink_update_listeners(struct sock *sk)
1054{
1055 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -08001056 unsigned long mask;
1057 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001058 struct listeners *listeners;
1059
1060 listeners = nl_deref_protected(tbl->listeners);
1061 if (!listeners)
1062 return;
Patrick McHardy4277a082006-03-20 18:52:01 -08001063
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001064 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001065 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001066 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001067 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
1068 mask |= nlk_sk(sk)->groups[i];
1069 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001070 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -08001071 }
1072 /* this function is only called with the netlink table "grabbed", which
1073 * makes sure updates are visible before bind or setsockopt return. */
1074}
1075
Herbert Xu8ea65f42015-01-26 14:02:56 +11001076static int netlink_insert(struct sock *sk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Gao fengda12c902013-06-06 14:49:11 +08001078 struct netlink_table *table = &nl_table[sk->sk_protocol];
Herbert Xu919d9db2015-01-16 17:23:48 +11001079 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
Ying Xuec5adde92015-01-12 14:52:23 +08001081 lock_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082
1083 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001084 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 goto err;
1086
1087 err = -ENOMEM;
Thomas Graf97defe12015-01-02 23:00:20 +01001088 if (BITS_PER_LONG > 32 &&
1089 unlikely(atomic_read(&table->hash.nelems) >= UINT_MAX))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 goto err;
1091
Eric W. Biederman15e47302012-09-07 20:12:54 +00001092 nlk_sk(sk)->portid = portid;
Thomas Grafe3416942014-08-02 11:47:45 +02001093 sock_hold(sk);
Herbert Xu919d9db2015-01-16 17:23:48 +11001094
Herbert Xuc428ecd2015-03-20 21:57:01 +11001095 err = __netlink_insert(table, sk);
1096 if (err) {
Daniel Borkmannd3976172015-08-07 00:26:41 +02001097 /* In case the hashtable backend returns with -EBUSY
1098 * from here, it must not escape to the caller.
1099 */
1100 if (unlikely(err == -EBUSY))
1101 err = -EOVERFLOW;
Herbert Xuc428ecd2015-03-20 21:57:01 +11001102 if (err == -EEXIST)
1103 err = -EADDRINUSE;
Herbert Xuc0bb07d2015-05-16 21:50:28 +08001104 nlk_sk(sk)->portid = 0;
Ying Xuec5adde92015-01-12 14:52:23 +08001105 sock_put(sk);
Herbert Xu919d9db2015-01-16 17:23:48 +11001106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108err:
Ying Xuec5adde92015-01-12 14:52:23 +08001109 release_sock(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return err;
1111}
1112
1113static void netlink_remove(struct sock *sk)
1114{
Thomas Grafe3416942014-08-02 11:47:45 +02001115 struct netlink_table *table;
1116
Thomas Grafe3416942014-08-02 11:47:45 +02001117 table = &nl_table[sk->sk_protocol];
Herbert Xuc428ecd2015-03-20 21:57:01 +11001118 if (!rhashtable_remove_fast(&table->hash, &nlk_sk(sk)->node,
1119 netlink_rhashtable_params)) {
Thomas Grafe3416942014-08-02 11:47:45 +02001120 WARN_ON(atomic_read(&sk->sk_refcnt) == 1);
1121 __sock_put(sk);
1122 }
Thomas Grafe3416942014-08-02 11:47:45 +02001123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 netlink_table_grab();
Johannes Bergb10dcb32014-12-22 18:56:37 +01001125 if (nlk_sk(sk)->subscriptions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 __sk_del_bind_node(sk);
Johannes Bergb10dcb32014-12-22 18:56:37 +01001127 netlink_update_listeners(sk);
1128 }
Johannes Bergee1c24422015-01-16 11:37:14 +01001129 if (sk->sk_protocol == NETLINK_GENERIC)
1130 atomic_inc(&genl_sk_destructing_cnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 netlink_table_ungrab();
1132}
1133
1134static struct proto netlink_proto = {
1135 .name = "NETLINK",
1136 .owner = THIS_MODULE,
1137 .obj_size = sizeof(struct netlink_sock),
1138};
1139
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001140static int __netlink_create(struct net *net, struct socket *sock,
1141 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142{
1143 struct sock *sk;
1144 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -07001145
1146 sock->ops = &netlink_ops;
1147
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001148 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -07001149 if (!sk)
1150 return -ENOMEM;
1151
1152 sock_init_data(sock, sk);
1153
1154 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001155 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001156 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +00001157 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001158 nlk->cb_mutex = &nlk->cb_def_mutex;
1159 mutex_init(nlk->cb_mutex);
1160 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001161 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001162#ifdef CONFIG_NETLINK_MMAP
1163 mutex_init(&nlk->pg_vec_lock);
1164#endif
Patrick McHardyab33a172005-08-14 19:31:36 -07001165
1166 sk->sk_destruct = netlink_sock_destruct;
1167 sk->sk_protocol = protocol;
1168 return 0;
1169}
1170
Eric Paris3f378b62009-11-05 22:18:14 -08001171static int netlink_create(struct net *net, struct socket *sock, int protocol,
1172 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -07001173{
1174 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001175 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001176 struct netlink_sock *nlk;
Johannes Berg023e2cf2014-12-23 21:00:06 +01001177 int (*bind)(struct net *net, int group);
1178 void (*unbind)(struct net *net, int group);
Patrick McHardyab33a172005-08-14 19:31:36 -07001179 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 sock->state = SS_UNCONNECTED;
1182
1183 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1184 return -ESOCKTNOSUPPORT;
1185
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001186 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -EPROTONOSUPPORT;
1188
Patrick McHardy77247bb2005-08-14 19:27:13 -07001189 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -07001190#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -07001191 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -07001192 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001193 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001194 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001195 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001196#endif
1197 if (nl_table[protocol].registered &&
1198 try_module_get(nl_table[protocol].module))
1199 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001200 else
1201 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001202 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001203 bind = nl_table[protocol].bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001204 unbind = nl_table[protocol].unbind;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001205 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001206
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001207 if (err < 0)
1208 goto out;
1209
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001210 err = __netlink_create(net, sock, cb_mutex, protocol);
1211 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -07001212 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
David S. Miller6f756a82008-11-23 17:34:03 -08001214 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001215 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -08001216 local_bh_enable();
1217
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001218 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001219 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001220 nlk->netlink_bind = bind;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001221 nlk->netlink_unbind = unbind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001222out:
1223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Patrick McHardyab33a172005-08-14 19:31:36 -07001225out_module:
1226 module_put(module);
1227 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228}
1229
Thomas Graf21e49022015-01-02 23:00:22 +01001230static void deferred_put_nlk_sk(struct rcu_head *head)
1231{
1232 struct netlink_sock *nlk = container_of(head, struct netlink_sock, rcu);
1233
1234 sock_put(&nlk->sk);
1235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237static int netlink_release(struct socket *sock)
1238{
1239 struct sock *sk = sock->sk;
1240 struct netlink_sock *nlk;
1241
1242 if (!sk)
1243 return 0;
1244
1245 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001246 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 nlk = nlk_sk(sk);
1248
Herbert Xu3f660d62007-05-03 03:17:14 -07001249 /*
1250 * OK. Socket is unlinked, any packets that arrive now
1251 * will be purged.
1252 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Johannes Bergee1c24422015-01-16 11:37:14 +01001254 /* must not acquire netlink_table_lock in any way again before unbind
1255 * and notifying genetlink is done as otherwise it might deadlock
1256 */
1257 if (nlk->netlink_unbind) {
1258 int i;
1259
1260 for (i = 0; i < nlk->ngroups; i++)
1261 if (test_bit(i, nlk->groups))
1262 nlk->netlink_unbind(sock_net(sk), i + 1);
1263 }
1264 if (sk->sk_protocol == NETLINK_GENERIC &&
1265 atomic_dec_return(&genl_sk_destructing_cnt) == 0)
1266 wake_up(&genl_sk_destructing_waitq);
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 sock->sk = NULL;
1269 wake_up_interruptible_all(&nlk->wait);
1270
1271 skb_queue_purge(&sk->sk_write_queue);
1272
Eric W. Biederman15e47302012-09-07 20:12:54 +00001273 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001275 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001277 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 };
Alan Sterne041c682006-03-27 01:16:30 -08001279 atomic_notifier_call_chain(&netlink_chain,
1280 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001281 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001282
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001283 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001284
Denis V. Lunevaed81562007-10-10 21:14:32 -07001285 if (netlink_is_kernel(sk)) {
Johannes Bergb10dcb32014-12-22 18:56:37 +01001286 netlink_table_grab();
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001287 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1288 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001289 struct listeners *old;
1290
1291 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1292 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1293 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001294 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001295 nl_table[sk->sk_protocol].bind = NULL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001296 nl_table[sk->sk_protocol].unbind = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001297 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001298 nl_table[sk->sk_protocol].registered = 0;
1299 }
Johannes Bergb10dcb32014-12-22 18:56:37 +01001300 netlink_table_ungrab();
Eric Dumazet658cb352012-04-22 21:30:21 +00001301 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001302
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001303 kfree(nlk->groups);
1304 nlk->groups = NULL;
1305
Eric Dumazet37558102008-11-24 14:05:22 -08001306 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001307 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001308 local_bh_enable();
Thomas Graf21e49022015-01-02 23:00:22 +01001309 call_rcu(&nlk->rcu, deferred_put_nlk_sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312
1313static int netlink_autobind(struct socket *sock)
1314{
1315 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001316 struct net *net = sock_net(sk);
Gao fengda12c902013-06-06 14:49:11 +08001317 struct netlink_table *table = &nl_table[sk->sk_protocol];
Eric W. Biederman15e47302012-09-07 20:12:54 +00001318 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 int err;
1320 static s32 rover = -4097;
1321
1322retry:
1323 cond_resched();
Thomas Grafe3416942014-08-02 11:47:45 +02001324 rcu_read_lock();
1325 if (__netlink_lookup(table, portid, net)) {
1326 /* Bind collision, search negative portid values. */
1327 portid = rover--;
1328 if (rover > -4097)
1329 rover = -4097;
1330 rcu_read_unlock();
1331 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
Thomas Grafe3416942014-08-02 11:47:45 +02001333 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Herbert Xu8ea65f42015-01-26 14:02:56 +11001335 err = netlink_insert(sk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 if (err == -EADDRINUSE)
1337 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001338
1339 /* If 2 threads race to autobind, that is fine. */
1340 if (err == -EBUSY)
1341 err = 0;
1342
1343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344}
1345
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001346/**
1347 * __netlink_ns_capable - General netlink message capability test
1348 * @nsp: NETLINK_CB of the socket buffer holding a netlink command from userspace.
1349 * @user_ns: The user namespace of the capability to use
1350 * @cap: The capability to use
1351 *
1352 * Test to see if the opener of the socket we received the message
1353 * from had when the netlink socket was created and the sender of the
1354 * message has has the capability @cap in the user namespace @user_ns.
1355 */
1356bool __netlink_ns_capable(const struct netlink_skb_parms *nsp,
1357 struct user_namespace *user_ns, int cap)
1358{
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07001359 return ((nsp->flags & NETLINK_SKB_DST) ||
1360 file_ns_capable(nsp->sk->sk_socket->file, user_ns, cap)) &&
1361 ns_capable(user_ns, cap);
Eric W. Biedermanaa4cf942014-04-23 14:28:03 -07001362}
1363EXPORT_SYMBOL(__netlink_ns_capable);
1364
1365/**
1366 * netlink_ns_capable - General netlink message capability test
1367 * @skb: 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 sk_buff *skb,
1376 struct user_namespace *user_ns, int cap)
1377{
1378 return __netlink_ns_capable(&NETLINK_CB(skb), user_ns, cap);
1379}
1380EXPORT_SYMBOL(netlink_ns_capable);
1381
1382/**
1383 * netlink_capable - Netlink global message capability test
1384 * @skb: socket buffer holding a netlink command from userspace
1385 * @cap: The capability to use
1386 *
1387 * Test to see if the opener of the socket we received the message
1388 * from had when the netlink socket was created and the sender of the
1389 * message has has the capability @cap in all user namespaces.
1390 */
1391bool netlink_capable(const struct sk_buff *skb, int cap)
1392{
1393 return netlink_ns_capable(skb, &init_user_ns, cap);
1394}
1395EXPORT_SYMBOL(netlink_capable);
1396
1397/**
1398 * netlink_net_capable - Netlink network namespace message capability test
1399 * @skb: socket buffer holding a netlink command from userspace
1400 * @cap: The capability to use
1401 *
1402 * Test to see if the opener of the socket we received the message
1403 * from had when the netlink socket was created and the sender of the
1404 * message has has the capability @cap over the network namespace of
1405 * the socket we received the message from.
1406 */
1407bool netlink_net_capable(const struct sk_buff *skb, int cap)
1408{
1409 return netlink_ns_capable(skb, sock_net(skb->sk)->user_ns, cap);
1410}
1411EXPORT_SYMBOL(netlink_net_capable);
1412
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001413static inline int netlink_allowed(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001414{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001415 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001416 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001417}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001419static void
1420netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1421{
1422 struct netlink_sock *nlk = nlk_sk(sk);
1423
1424 if (nlk->subscriptions && !subscriptions)
1425 __sk_del_bind_node(sk);
1426 else if (!nlk->subscriptions && subscriptions)
1427 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1428 nlk->subscriptions = subscriptions;
1429}
1430
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001431static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001432{
1433 struct netlink_sock *nlk = nlk_sk(sk);
1434 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001435 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001436 int err = 0;
1437
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001438 netlink_table_grab();
1439
Patrick McHardy513c2502005-09-06 15:43:59 -07001440 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001441 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001442 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001443 goto out_unlock;
1444 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001445
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001446 if (nlk->ngroups >= groups)
1447 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001448
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001449 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1450 if (new_groups == NULL) {
1451 err = -ENOMEM;
1452 goto out_unlock;
1453 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001454 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001455 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1456
1457 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001458 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001459 out_unlock:
1460 netlink_table_ungrab();
1461 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001462}
1463
Johannes Berg02c81ab2014-12-22 18:56:35 +01001464static void netlink_undo_bind(int group, long unsigned int groups,
Johannes Berg023e2cf2014-12-23 21:00:06 +01001465 struct sock *sk)
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001466{
Johannes Berg023e2cf2014-12-23 21:00:06 +01001467 struct netlink_sock *nlk = nlk_sk(sk);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001468 int undo;
1469
1470 if (!nlk->netlink_unbind)
1471 return;
1472
1473 for (undo = 0; undo < group; undo++)
Hiroaki SHIMODA6251edd2014-11-13 04:24:10 +09001474 if (test_bit(undo, &groups))
Pablo Neira8b7c36d2015-01-29 10:51:53 +01001475 nlk->netlink_unbind(sock_net(sk), undo + 1);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001476}
1477
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001478static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1479 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480{
1481 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001482 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 struct netlink_sock *nlk = nlk_sk(sk);
1484 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1485 int err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001486 long unsigned int groups = nladdr->nl_groups;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001487
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001488 if (addr_len < sizeof(struct sockaddr_nl))
1489 return -EINVAL;
1490
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 if (nladdr->nl_family != AF_NETLINK)
1492 return -EINVAL;
1493
1494 /* Only superuser is allowed to listen multicasts */
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001495 if (groups) {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001496 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001497 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001498 err = netlink_realloc_groups(sk);
1499 if (err)
1500 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001503 if (nlk->portid)
Eric W. Biederman15e47302012-09-07 20:12:54 +00001504 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505 return -EINVAL;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001506
1507 if (nlk->netlink_bind && groups) {
1508 int group;
1509
1510 for (group = 0; group < nlk->ngroups; group++) {
1511 if (!test_bit(group, &groups))
1512 continue;
Pablo Neira8b7c36d2015-01-29 10:51:53 +01001513 err = nlk->netlink_bind(net, group + 1);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001514 if (!err)
1515 continue;
Johannes Berg023e2cf2014-12-23 21:00:06 +01001516 netlink_undo_bind(group, groups, sk);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001517 return err;
1518 }
1519 }
1520
1521 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 err = nladdr->nl_pid ?
Herbert Xu8ea65f42015-01-26 14:02:56 +11001523 netlink_insert(sk, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 netlink_autobind(sock);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001525 if (err) {
Johannes Berg023e2cf2014-12-23 21:00:06 +01001526 netlink_undo_bind(nlk->ngroups, groups, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 return err;
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
1530
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001531 if (!groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 return 0;
1533
1534 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001535 netlink_update_subscriptions(sk, nlk->subscriptions +
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001536 hweight32(groups) -
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001537 hweight32(nlk->groups[0]));
Richard Guy Briggs4f520902014-04-22 21:31:54 -04001538 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001539 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 netlink_table_ungrab();
1541
1542 return 0;
1543}
1544
1545static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1546 int alen, int flags)
1547{
1548 int err = 0;
1549 struct sock *sk = sock->sk;
1550 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001551 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Changli Gao6503d962010-03-31 22:58:26 +00001553 if (alen < sizeof(addr->sa_family))
1554 return -EINVAL;
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 if (addr->sa_family == AF_UNSPEC) {
1557 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001558 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001559 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 return 0;
1561 }
1562 if (addr->sa_family != AF_NETLINK)
1563 return -EINVAL;
1564
Mike Pecovnik46833a82014-02-24 21:11:16 +01001565 if ((nladdr->nl_groups || nladdr->nl_pid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07001566 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 return -EPERM;
1568
Eric W. Biederman15e47302012-09-07 20:12:54 +00001569 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 err = netlink_autobind(sock);
1571
1572 if (err == 0) {
1573 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001574 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001575 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 }
1577
1578 return err;
1579}
1580
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001581static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1582 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
1584 struct sock *sk = sock->sk;
1585 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001586 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 nladdr->nl_family = AF_NETLINK;
1589 nladdr->nl_pad = 0;
1590 *addr_len = sizeof(*nladdr);
1591
1592 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001593 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001594 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001596 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001597 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
1599 return 0;
1600}
1601
Eric W. Biederman15e47302012-09-07 20:12:54 +00001602static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 struct sock *sock;
1605 struct netlink_sock *nlk;
1606
Eric W. Biederman15e47302012-09-07 20:12:54 +00001607 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (!sock)
1609 return ERR_PTR(-ECONNREFUSED);
1610
1611 /* Don't bother queuing skb if kernel socket has no input function */
1612 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001613 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001614 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 sock_put(sock);
1616 return ERR_PTR(-ECONNREFUSED);
1617 }
1618 return sock;
1619}
1620
1621struct sock *netlink_getsockbyfilp(struct file *filp)
1622{
Al Viro496ad9a2013-01-23 17:07:38 -05001623 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 struct sock *sock;
1625
1626 if (!S_ISSOCK(inode->i_mode))
1627 return ERR_PTR(-ENOTSOCK);
1628
1629 sock = SOCKET_I(inode)->sk;
1630 if (sock->sk_family != AF_NETLINK)
1631 return ERR_PTR(-EINVAL);
1632
1633 sock_hold(sock);
1634 return sock;
1635}
1636
Pablo Neira3a365152013-06-28 03:04:23 +02001637static struct sk_buff *netlink_alloc_large_skb(unsigned int size,
1638 int broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001639{
1640 struct sk_buff *skb;
1641 void *data;
1642
Pablo Neira3a365152013-06-28 03:04:23 +02001643 if (size <= NLMSG_GOODSIZE || broadcast)
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001644 return alloc_skb(size, GFP_KERNEL);
1645
Pablo Neira3a365152013-06-28 03:04:23 +02001646 size = SKB_DATA_ALIGN(size) +
1647 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001648
1649 data = vmalloc(size);
1650 if (data == NULL)
Pablo Neira3a365152013-06-28 03:04:23 +02001651 return NULL;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001652
Eric Dumazet2ea2f622015-04-24 16:05:01 -07001653 skb = __build_skb(data, size);
Pablo Neira3a365152013-06-28 03:04:23 +02001654 if (skb == NULL)
1655 vfree(data);
Eric Dumazet2ea2f622015-04-24 16:05:01 -07001656 else
Pablo Neira3a365152013-06-28 03:04:23 +02001657 skb->destructor = netlink_skb_destructor;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001658
1659 return skb;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001660}
1661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662/*
1663 * Attach a skb to a netlink socket.
1664 * The caller must hold a reference to the destination socket. On error, the
1665 * reference is dropped. The skb is not send to the destination, just all
1666 * all error checks are performed and memory in the queue is reserved.
1667 * Return values:
1668 * < 0: error. skb freed, reference to sock dropped.
1669 * 0: continue
1670 * 1: repeat lookup - reference dropped while waiting for socket memory.
1671 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001672int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001673 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674{
1675 struct netlink_sock *nlk;
1676
1677 nlk = nlk_sk(sk);
1678
Patrick McHardy5fd96122013-04-17 06:47:03 +00001679 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1680 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1681 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001683 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001684 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 netlink_overrun(sk);
1686 sock_put(sk);
1687 kfree_skb(skb);
1688 return -EAGAIN;
1689 }
1690
1691 __set_current_state(TASK_INTERRUPTIBLE);
1692 add_wait_queue(&nlk->wait, &wait);
1693
1694 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001695 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001697 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 __set_current_state(TASK_RUNNING);
1700 remove_wait_queue(&nlk->wait, &wait);
1701 sock_put(sk);
1702
1703 if (signal_pending(current)) {
1704 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001705 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
1707 return 1;
1708 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001709 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 return 0;
1711}
1712
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001713static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 int len = skb->len;
1716
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02001717 netlink_deliver_tap(skb);
1718
Patrick McHardyf9c22882013-04-17 06:47:04 +00001719#ifdef CONFIG_NETLINK_MMAP
1720 if (netlink_skb_is_mmaped(skb))
1721 netlink_queue_mmaped_skb(sk, skb);
1722 else if (netlink_rx_is_mmaped(sk))
1723 netlink_ring_set_copied(sk, skb);
1724 else
1725#endif /* CONFIG_NETLINK_MMAP */
1726 skb_queue_tail(&sk->sk_receive_queue, skb);
David S. Miller676d2362014-04-11 16:15:36 -04001727 sk->sk_data_ready(sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001728 return len;
1729}
1730
1731int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1732{
1733 int len = __netlink_sendskb(sk, skb);
1734
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 sock_put(sk);
1736 return len;
1737}
1738
1739void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1740{
1741 kfree_skb(skb);
1742 sock_put(sk);
1743}
1744
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001745static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
1747 int delta;
1748
Patrick McHardy1298ca42013-04-17 06:46:59 +00001749 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001750 if (netlink_skb_is_mmaped(skb))
1751 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001753 delta = skb->end - skb->tail;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001754 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return skb;
1756
1757 if (skb_shared(skb)) {
1758 struct sk_buff *nskb = skb_clone(skb, allocation);
1759 if (!nskb)
1760 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001761 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 skb = nskb;
1763 }
1764
1765 if (!pskb_expand_head(skb, 0, -delta, allocation))
1766 skb->truesize -= delta;
1767
1768 return skb;
1769}
1770
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001771static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1772 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001773{
1774 int ret;
1775 struct netlink_sock *nlk = nlk_sk(sk);
1776
1777 ret = -ECONNREFUSED;
1778 if (nlk->netlink_rcv != NULL) {
1779 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001780 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001781 NETLINK_CB(skb).sk = ssk;
Daniel Borkmann73bfd372013-12-23 14:35:55 +01001782 netlink_deliver_tap_kernel(sk, ssk, skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001783 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001784 consume_skb(skb);
1785 } else {
1786 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001787 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001788 sock_put(sk);
1789 return ret;
1790}
1791
1792int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001793 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 struct sock *sk;
1796 int err;
1797 long timeo;
1798
1799 skb = netlink_trim(skb, gfp_any());
1800
1801 timeo = sock_sndtimeo(ssk, nonblock);
1802retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001803 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (IS_ERR(sk)) {
1805 kfree_skb(skb);
1806 return PTR_ERR(sk);
1807 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001808 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001809 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001810
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001811 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001812 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001813 kfree_skb(skb);
1814 sock_put(sk);
1815 return err;
1816 }
1817
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001818 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 if (err == 1)
1820 goto retry;
1821 if (err)
1822 return err;
1823
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001824 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001826EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Patrick McHardyf9c22882013-04-17 06:47:04 +00001828struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1829 u32 dst_portid, gfp_t gfp_mask)
1830{
1831#ifdef CONFIG_NETLINK_MMAP
1832 struct sock *sk = NULL;
1833 struct sk_buff *skb;
1834 struct netlink_ring *ring;
1835 struct nl_mmap_hdr *hdr;
1836 unsigned int maxlen;
1837
1838 sk = netlink_getsockbyportid(ssk, dst_portid);
1839 if (IS_ERR(sk))
1840 goto out;
1841
1842 ring = &nlk_sk(sk)->rx_ring;
1843 /* fast-path without atomic ops for common case: non-mmaped receiver */
1844 if (ring->pg_vec == NULL)
1845 goto out_put;
1846
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001847 if (ring->frame_size - NL_MMAP_HDRLEN < size)
1848 goto out_put;
1849
Patrick McHardyf9c22882013-04-17 06:47:04 +00001850 skb = alloc_skb_head(gfp_mask);
1851 if (skb == NULL)
1852 goto err1;
1853
1854 spin_lock_bh(&sk->sk_receive_queue.lock);
1855 /* check again under lock */
1856 if (ring->pg_vec == NULL)
1857 goto out_free;
1858
Thomas Grafaae9f0e2013-11-30 13:21:31 +01001859 /* check again under lock */
Patrick McHardyf9c22882013-04-17 06:47:04 +00001860 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1861 if (maxlen < size)
1862 goto out_free;
1863
1864 netlink_forward_ring(ring);
1865 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1866 if (hdr == NULL)
1867 goto err2;
1868 netlink_ring_setup_skb(skb, sk, ring, hdr);
1869 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1870 atomic_inc(&ring->pending);
1871 netlink_increment_head(ring);
1872
1873 spin_unlock_bh(&sk->sk_receive_queue.lock);
1874 return skb;
1875
1876err2:
1877 kfree_skb(skb);
1878 spin_unlock_bh(&sk->sk_receive_queue.lock);
Patrick McHardycd1df522013-04-17 06:47:05 +00001879 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +00001880err1:
1881 sock_put(sk);
1882 return NULL;
1883
1884out_free:
1885 kfree_skb(skb);
1886 spin_unlock_bh(&sk->sk_receive_queue.lock);
1887out_put:
1888 sock_put(sk);
1889out:
1890#endif
1891 return alloc_skb(size, gfp_mask);
1892}
1893EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1894
Patrick McHardy4277a082006-03-20 18:52:01 -08001895int netlink_has_listeners(struct sock *sk, unsigned int group)
1896{
1897 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001898 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001899
Denis V. Lunevaed81562007-10-10 21:14:32 -07001900 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001901
1902 rcu_read_lock();
1903 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1904
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001905 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001906 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001907
1908 rcu_read_unlock();
1909
Patrick McHardy4277a082006-03-20 18:52:01 -08001910 return res;
1911}
1912EXPORT_SYMBOL_GPL(netlink_has_listeners);
1913
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001914static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 struct netlink_sock *nlk = nlk_sk(sk);
1917
1918 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001919 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001920 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001921 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001922 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924 return -1;
1925}
1926
1927struct netlink_broadcast_data {
1928 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001929 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001930 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 u32 group;
1932 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001933 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 int congested;
1935 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001936 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001938 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1939 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940};
1941
Rami Rosen46c95212014-07-01 21:17:35 +03001942static void do_one_broadcast(struct sock *sk,
1943 struct netlink_broadcast_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 struct netlink_sock *nlk = nlk_sk(sk);
1946 int val;
1947
1948 if (p->exclude_sk == sk)
Rami Rosen46c95212014-07-01 21:17:35 +03001949 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Eric W. Biederman15e47302012-09-07 20:12:54 +00001951 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001952 !test_bit(p->group - 1, nlk->groups))
Rami Rosen46c95212014-07-01 21:17:35 +03001953 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001955 if (!net_eq(sock_net(sk), p->net))
Rami Rosen46c95212014-07-01 21:17:35 +03001956 return;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001957
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (p->failure) {
1959 netlink_overrun(sk);
Rami Rosen46c95212014-07-01 21:17:35 +03001960 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
1962
1963 sock_hold(sk);
1964 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001965 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 p->skb2 = skb_clone(p->skb, p->allocation);
1967 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001968 p->skb2 = skb_get(p->skb);
1969 /*
1970 * skb ownership may have been set when
1971 * delivered to a previous socket.
1972 */
1973 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 }
1976 if (p->skb2 == NULL) {
1977 netlink_overrun(sk);
1978 /* Clone failed. Notify ALL listeners. */
1979 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001980 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1981 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001982 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1983 kfree_skb(p->skb2);
1984 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001985 } else if (sk_filter(sk, p->skb2)) {
1986 kfree_skb(p->skb2);
1987 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1989 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001990 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1991 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 } else {
1993 p->congested |= val;
1994 p->delivered = 1;
1995 p->skb2 = NULL;
1996 }
1997 sock_put(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998}
1999
Eric W. Biederman15e47302012-09-07 20:12:54 +00002000int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002001 u32 group, gfp_t allocation,
2002 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
2003 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002005 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 struct sock *sk;
2008
2009 skb = netlink_trim(skb, allocation);
2010
2011 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002012 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002013 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 info.group = group;
2015 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002016 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 info.congested = 0;
2018 info.delivered = 0;
2019 info.allocation = allocation;
2020 info.skb = skb;
2021 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002022 info.tx_filter = filter;
2023 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /* While we sleep in clone, do not allow to change socket list */
2026
2027 netlink_lock_table();
2028
Sasha Levinb67bfe02013-02-27 17:06:00 -08002029 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 do_one_broadcast(sk, &info);
2031
Neil Horman70d4bf62010-07-20 06:45:56 +00002032 consume_skb(skb);
Tommy S. Christensenaa1c6a6f2005-05-19 13:07:32 -07002033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 netlink_unlock_table();
2035
Neil Horman70d4bf62010-07-20 06:45:56 +00002036 if (info.delivery_failure) {
2037 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002038 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002039 }
2040 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08002041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (info.delivered) {
2043 if (info.congested && (allocation & __GFP_WAIT))
2044 yield();
2045 return 0;
2046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return -ESRCH;
2048}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002049EXPORT_SYMBOL(netlink_broadcast_filtered);
2050
Eric W. Biederman15e47302012-09-07 20:12:54 +00002051int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002052 u32 group, gfp_t allocation)
2053{
Eric W. Biederman15e47302012-09-07 20:12:54 +00002054 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07002055 NULL, NULL);
2056}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002057EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
2059struct netlink_set_err_data {
2060 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002061 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 u32 group;
2063 int code;
2064};
2065
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00002066static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067{
2068 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002069 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
2071 if (sk == p->exclude_sk)
2072 goto out;
2073
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08002074 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002075 goto out;
2076
Eric W. Biederman15e47302012-09-07 20:12:54 +00002077 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07002078 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 goto out;
2080
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002081 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
2082 ret = 1;
2083 goto out;
2084 }
2085
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 sk->sk_err = p->code;
2087 sk->sk_error_report(sk);
2088out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002089 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090}
2091
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002092/**
2093 * netlink_set_err - report error to broadcast listeners
2094 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00002095 * @portid: the PORTID of a process that we want to skip (if any)
Johannes Berg840e93f22013-11-19 10:35:40 +01002096 * @group: the broadcast group that will notice the error
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002097 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002098 *
2099 * This function returns the number of broadcast listeners that have set the
2100 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002101 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002102int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103{
2104 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002106 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002109 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08002111 /* sk->sk_err wants a positive error value */
2112 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113
2114 read_lock(&nl_table_lock);
2115
Sasha Levinb67bfe02013-02-27 17:06:00 -08002116 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002117 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00002120 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01002122EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Johannes Berg84659eb2007-07-18 15:47:05 -07002124/* must be called with netlink table grabbed */
2125static void netlink_update_socket_mc(struct netlink_sock *nlk,
2126 unsigned int group,
2127 int is_new)
2128{
2129 int old, new = !!is_new, subscriptions;
2130
2131 old = test_bit(group - 1, nlk->groups);
2132 subscriptions = nlk->subscriptions - old + new;
2133 if (new)
2134 __set_bit(group - 1, nlk->groups);
2135 else
2136 __clear_bit(group - 1, nlk->groups);
2137 netlink_update_subscriptions(&nlk->sk, subscriptions);
2138 netlink_update_listeners(&nlk->sk);
2139}
2140
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002141static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07002142 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002143{
2144 struct sock *sk = sock->sk;
2145 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07002146 unsigned int val = 0;
2147 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002148
2149 if (level != SOL_NETLINK)
2150 return -ENOPROTOOPT;
2151
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002152 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
2153 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07002154 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002155 return -EFAULT;
2156
2157 switch (optname) {
2158 case NETLINK_PKTINFO:
2159 if (val)
2160 nlk->flags |= NETLINK_RECV_PKTINFO;
2161 else
2162 nlk->flags &= ~NETLINK_RECV_PKTINFO;
2163 err = 0;
2164 break;
2165 case NETLINK_ADD_MEMBERSHIP:
2166 case NETLINK_DROP_MEMBERSHIP: {
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002167 if (!netlink_allowed(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002168 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002169 err = netlink_realloc_groups(sk);
2170 if (err)
2171 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002172 if (!val || val - 1 >= nlk->ngroups)
2173 return -EINVAL;
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002174 if (optname == NETLINK_ADD_MEMBERSHIP && nlk->netlink_bind) {
Johannes Berg023e2cf2014-12-23 21:00:06 +01002175 err = nlk->netlink_bind(sock_net(sk), val);
Richard Guy Briggs4f520902014-04-22 21:31:54 -04002176 if (err)
2177 return err;
2178 }
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002179 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07002180 netlink_update_socket_mc(nlk, val,
2181 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002182 netlink_table_ungrab();
Richard Guy Briggs7774d5e2014-04-22 21:31:55 -04002183 if (optname == NETLINK_DROP_MEMBERSHIP && nlk->netlink_unbind)
Johannes Berg023e2cf2014-12-23 21:00:06 +01002184 nlk->netlink_unbind(sock_net(sk), val);
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00002185
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002186 err = 0;
2187 break;
2188 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002189 case NETLINK_BROADCAST_ERROR:
2190 if (val)
2191 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
2192 else
2193 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
2194 err = 0;
2195 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002196 case NETLINK_NO_ENOBUFS:
2197 if (val) {
2198 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00002199 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002200 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00002201 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002202 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002203 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002204 err = 0;
2205 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002206#ifdef CONFIG_NETLINK_MMAP
2207 case NETLINK_RX_RING:
2208 case NETLINK_TX_RING: {
2209 struct nl_mmap_req req;
2210
2211 /* Rings might consume more memory than queue limits, require
2212 * CAP_NET_ADMIN.
2213 */
2214 if (!capable(CAP_NET_ADMIN))
2215 return -EPERM;
2216 if (optlen < sizeof(req))
2217 return -EINVAL;
2218 if (copy_from_user(&req, optval, sizeof(req)))
2219 return -EFAULT;
Florian Westphalb265c302015-07-21 16:33:50 +02002220 err = netlink_set_ring(sk, &req,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002221 optname == NETLINK_TX_RING);
2222 break;
2223 }
2224#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002225 default:
2226 err = -ENOPROTOOPT;
2227 }
2228 return err;
2229}
2230
2231static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002232 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002233{
2234 struct sock *sk = sock->sk;
2235 struct netlink_sock *nlk = nlk_sk(sk);
2236 int len, val, err;
2237
2238 if (level != SOL_NETLINK)
2239 return -ENOPROTOOPT;
2240
2241 if (get_user(len, optlen))
2242 return -EFAULT;
2243 if (len < 0)
2244 return -EINVAL;
2245
2246 switch (optname) {
2247 case NETLINK_PKTINFO:
2248 if (len < sizeof(int))
2249 return -EINVAL;
2250 len = sizeof(int);
2251 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08002252 if (put_user(len, optlen) ||
2253 put_user(val, optval))
2254 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002255 err = 0;
2256 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002257 case NETLINK_BROADCAST_ERROR:
2258 if (len < sizeof(int))
2259 return -EINVAL;
2260 len = sizeof(int);
2261 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
2262 if (put_user(len, optlen) ||
2263 put_user(val, optval))
2264 return -EFAULT;
2265 err = 0;
2266 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002267 case NETLINK_NO_ENOBUFS:
2268 if (len < sizeof(int))
2269 return -EINVAL;
2270 len = sizeof(int);
2271 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
2272 if (put_user(len, optlen) ||
2273 put_user(val, optval))
2274 return -EFAULT;
2275 err = 0;
2276 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002277 default:
2278 err = -ENOPROTOOPT;
2279 }
2280 return err;
2281}
2282
2283static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2284{
2285 struct nl_pktinfo info;
2286
2287 info.group = NETLINK_CB(skb).dst_group;
2288 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2289}
2290
Ying Xue1b784142015-03-02 15:37:48 +08002291static int netlink_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct sock *sk = sock->sk;
2294 struct netlink_sock *nlk = nlk_sk(sk);
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002295 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002296 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002297 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 struct sk_buff *skb;
2299 int err;
2300 struct scm_cookie scm;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002301 u32 netlink_skb_flags = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 if (msg->msg_flags&MSG_OOB)
2304 return -EOPNOTSUPP;
2305
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002306 err = scm_send(sock, msg, &scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 if (err < 0)
2308 return err;
2309
2310 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002311 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002313 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002314 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002315 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002316 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002317 if ((dst_group || dst_portid) &&
Eric W. Biederman5187cd02014-04-23 14:25:48 -07002318 !netlink_allowed(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002319 goto out;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002320 netlink_skb_flags |= NETLINK_SKB_DST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002322 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002323 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 }
2325
Eric W. Biederman15e47302012-09-07 20:12:54 +00002326 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 err = netlink_autobind(sock);
2328 if (err)
2329 goto out;
2330 }
2331
Al Viroa8866ff2014-12-12 23:02:36 -05002332 /* It's a really convoluted way for userland to ask for mmaped
2333 * sendmsg(), but that's what we've got...
2334 */
Patrick McHardy5fd96122013-04-17 06:47:03 +00002335 if (netlink_tx_is_mmaped(sk) &&
Al Viroa8866ff2014-12-12 23:02:36 -05002336 msg->msg_iter.type == ITER_IOVEC &&
2337 msg->msg_iter.nr_segs == 1 &&
Al Viroc0371da2014-11-24 10:42:55 -05002338 msg->msg_iter.iov->iov_base == NULL) {
Patrick McHardy5fd96122013-04-17 06:47:03 +00002339 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002340 &scm);
Patrick McHardy5fd96122013-04-17 06:47:03 +00002341 goto out;
2342 }
2343
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 err = -EMSGSIZE;
2345 if (len > sk->sk_sndbuf - 32)
2346 goto out;
2347 err = -ENOBUFS;
Pablo Neira3a365152013-06-28 03:04:23 +02002348 skb = netlink_alloc_large_skb(len, dst_group);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002349 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 goto out;
2351
Eric W. Biederman15e47302012-09-07 20:12:54 +00002352 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002353 NETLINK_CB(skb).dst_group = dst_group;
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002354 NETLINK_CB(skb).creds = scm.creds;
Eric W. Biederman2d7a85f2014-05-30 11:04:00 -07002355 NETLINK_CB(skb).flags = netlink_skb_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 err = -EFAULT;
Al Viro6ce8e9c2014-04-06 21:25:44 -04002358 if (memcpy_from_msg(skb_put(skb, len), msg, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 kfree_skb(skb);
2360 goto out;
2361 }
2362
2363 err = security_netlink_send(sk, skb);
2364 if (err) {
2365 kfree_skb(skb);
2366 goto out;
2367 }
2368
Patrick McHardyd629b832005-08-14 19:27:50 -07002369 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002371 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002373 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375out:
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002376 scm_destroy(&scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 return err;
2378}
2379
Ying Xue1b784142015-03-02 15:37:48 +08002380static int netlink_recvmsg(struct socket *sock, struct msghdr *msg, size_t len,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 int flags)
2382{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 struct scm_cookie scm;
2384 struct sock *sk = sock->sk;
2385 struct netlink_sock *nlk = nlk_sk(sk);
2386 int noblock = flags&MSG_DONTWAIT;
2387 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00002388 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002389 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 if (flags&MSG_OOB)
2392 return -EOPNOTSUPP;
2393
2394 copied = 0;
2395
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002396 skb = skb_recv_datagram(sk, flags, noblock, &err);
2397 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 goto out;
2399
Johannes Berg68d6ac62010-08-15 21:20:44 +00002400 data_skb = skb;
2401
Johannes Berg1dacc762009-07-01 11:26:02 +00002402#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2403 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002404 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002405 * If this skb has a frag_list, then here that means that we
2406 * will have to use the frag_list skb's data for compat tasks
2407 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002408 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002409 * If we need to send the compat skb, assign it to the
2410 * 'data_skb' variable so that it will be used below for data
2411 * copying. We keep 'skb' for everything else, including
2412 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002413 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002414 if (flags & MSG_CMSG_COMPAT)
2415 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002416 }
2417#endif
2418
Eric Dumazet9063e212014-03-07 12:02:33 -08002419 /* Record the max length of recvmsg() calls for future allocations */
2420 nlk->max_recvmsg_len = max(nlk->max_recvmsg_len, len);
2421 nlk->max_recvmsg_len = min_t(size_t, nlk->max_recvmsg_len,
2422 16384);
2423
Johannes Berg68d6ac62010-08-15 21:20:44 +00002424 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 if (len < copied) {
2426 msg->msg_flags |= MSG_TRUNC;
2427 copied = len;
2428 }
2429
Johannes Berg68d6ac62010-08-15 21:20:44 +00002430 skb_reset_transport_header(data_skb);
David S. Miller51f3d022014-11-05 16:46:40 -05002431 err = skb_copy_datagram_msg(data_skb, 0, msg, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432
2433 if (msg->msg_name) {
Steffen Hurrle342dfc32014-01-17 22:53:15 +01002434 DECLARE_SOCKADDR(struct sockaddr_nl *, addr, msg->msg_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 addr->nl_family = AF_NETLINK;
2436 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002437 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002438 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 msg->msg_namelen = sizeof(*addr);
2440 }
2441
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002442 if (nlk->flags & NETLINK_RECV_PKTINFO)
2443 netlink_cmsg_recv_pktinfo(msg, skb);
2444
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002445 memset(&scm, 0, sizeof(scm));
2446 scm.creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002447 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002448 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 skb_free_datagram(sk, skb);
2451
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002452 if (nlk->cb_running &&
2453 atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
Andrey Vaginb44d2112011-02-21 02:40:47 +00002454 ret = netlink_dump(sk);
2455 if (ret) {
Ben Pfaffac30ef82014-07-09 10:31:22 -07002456 sk->sk_err = -ret;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002457 sk->sk_error_report(sk);
2458 }
2459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460
Christoph Hellwig7cc05662015-01-28 18:04:53 +01002461 scm_recv(sock, msg, &scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462out:
2463 netlink_rcv_wake(sk);
2464 return err ? : copied;
2465}
2466
David S. Miller676d2362014-04-11 16:15:36 -04002467static void netlink_data_ready(struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002469 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470}
2471
2472/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002473 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 * complete set of kernel non-blocking support for message
2475 * queueing.
2476 */
2477
2478struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002479__netlink_kernel_create(struct net *net, int unit, struct module *module,
2480 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481{
2482 struct socket *sock;
2483 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002484 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002485 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002486 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2487 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002489 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002491 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return NULL;
2493
2494 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2495 return NULL;
2496
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002497 /*
2498 * We have to just have a reference on the net from sk, but don't
2499 * get_net it. Besides, we cannot get and then put the net here.
2500 * So we create one inside init_net and the move it to net.
2501 */
2502
2503 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2504 goto out_sock_release_nosk;
2505
2506 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002507 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002508
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002509 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002510 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002511 else
2512 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002513
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002514 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002515 if (!listeners)
2516 goto out_sock_release;
2517
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002519 if (cfg && cfg->input)
2520 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Herbert Xu8ea65f42015-01-26 14:02:56 +11002522 if (netlink_insert(sk, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002523 goto out_sock_release;
2524
2525 nlk = nlk_sk(sk);
2526 nlk->flags |= NETLINK_KERNEL_SOCKET;
2527
2528 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002529 if (!nl_table[unit].registered) {
2530 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002531 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002532 nl_table[unit].cb_mutex = cb_mutex;
2533 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002534 if (cfg) {
2535 nl_table[unit].bind = cfg->bind;
Hiroaki SHIMODA6251edd2014-11-13 04:24:10 +09002536 nl_table[unit].unbind = cfg->unbind;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002537 nl_table[unit].flags = cfg->flags;
Gao fengda12c902013-06-06 14:49:11 +08002538 if (cfg->compare)
2539 nl_table[unit].compare = cfg->compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002540 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002541 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002542 } else {
2543 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002544 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002545 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002546 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002547 return sk;
2548
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002549out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002550 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002551 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002552 return NULL;
2553
2554out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002555 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002556 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002558EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002559
2560void
2561netlink_kernel_release(struct sock *sk)
2562{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002563 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002564}
2565EXPORT_SYMBOL(netlink_kernel_release);
2566
Johannes Bergd136f1b2009-09-12 03:03:15 +00002567int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002568{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002569 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002570 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002571
2572 if (groups < 32)
2573 groups = 32;
2574
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002575 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002576 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2577 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002578 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002579 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002580 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2581 rcu_assign_pointer(tbl->listeners, new);
2582
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002583 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002584 }
2585 tbl->groups = groups;
2586
Johannes Bergd136f1b2009-09-12 03:03:15 +00002587 return 0;
2588}
2589
2590/**
2591 * netlink_change_ngroups - change number of multicast groups
2592 *
2593 * This changes the number of multicast groups that are available
2594 * on a certain netlink family. Note that it is not possible to
2595 * change the number of groups to below 32. Also note that it does
2596 * not implicitly call netlink_clear_multicast_users() when the
2597 * number of groups is reduced.
2598 *
2599 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2600 * @groups: The new number of groups.
2601 */
2602int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2603{
2604 int err;
2605
2606 netlink_table_grab();
2607 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002608 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002609
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002610 return err;
2611}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002612
Johannes Bergb8273572009-09-24 15:44:05 -07002613void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2614{
2615 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002616 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2617
Sasha Levinb67bfe02013-02-27 17:06:00 -08002618 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002619 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2620}
2621
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002622struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002623__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002624{
2625 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002626 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002627
Wang Yufen23b45672014-02-17 16:53:32 +08002628 nlh = (struct nlmsghdr *)skb_put(skb, NLMSG_ALIGN(size));
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002629 nlh->nlmsg_type = type;
2630 nlh->nlmsg_len = size;
2631 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002632 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002633 nlh->nlmsg_seq = seq;
2634 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002635 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002636 return nlh;
2637}
2638EXPORT_SYMBOL(__nlmsg_put);
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640/*
2641 * It looks a bit ugly.
2642 * It would be better to create kernel thread.
2643 */
2644
2645static int netlink_dump(struct sock *sk)
2646{
2647 struct netlink_sock *nlk = nlk_sk(sk);
2648 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002649 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002651 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002652 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002654 mutex_lock(nlk->cb_mutex);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002655 if (!nlk->cb_running) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002656 err = -EINVAL;
2657 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002660 cb = &nlk->cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002661 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2662
Patrick McHardyf9c22882013-04-17 06:47:04 +00002663 if (!netlink_rx_is_mmaped(sk) &&
2664 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2665 goto errout_skb;
Eric Dumazet9063e212014-03-07 12:02:33 -08002666
2667 /* NLMSG_GOODSIZE is small to avoid high order allocations being
2668 * required, but it makes sense to _attempt_ a 16K bytes allocation
2669 * to reduce number of system calls on dump operations, if user
2670 * ever provided a big enough buffer.
2671 */
2672 if (alloc_size < nlk->max_recvmsg_len) {
2673 skb = netlink_alloc_skb(sk,
2674 nlk->max_recvmsg_len,
2675 nlk->portid,
2676 GFP_KERNEL |
2677 __GFP_NOWARN |
2678 __GFP_NORETRY);
2679 /* available room should be exact amount to avoid MSG_TRUNC */
2680 if (skb)
2681 skb_reserve(skb, skb_tailroom(skb) -
2682 nlk->max_recvmsg_len);
2683 }
2684 if (!skb)
2685 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid,
2686 GFP_KERNEL);
Greg Rosec7ac8672011-06-10 01:27:09 +00002687 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002688 goto errout_skb;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002689 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 len = cb->dump(skb, cb);
2692
2693 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002694 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002695
2696 if (sk_filter(sk, skb))
2697 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002698 else
2699 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 return 0;
2701 }
2702
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002703 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2704 if (!nlh)
2705 goto errout_skb;
2706
Johannes Berg670dc282011-06-20 13:40:46 +02002707 nl_dump_check_consistent(cb, nlh);
2708
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002709 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2710
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002711 if (sk_filter(sk, skb))
2712 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002713 else
2714 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715
Thomas Grafa8f74b22005-11-10 02:25:52 +01002716 if (cb->done)
2717 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002719 nlk->cb_running = false;
2720 mutex_unlock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002721 module_put(cb->module);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002722 consume_skb(cb->skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002724
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002725errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002726 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002727 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002728 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729}
2730
Gao feng6dc878a2012-10-04 20:15:48 +00002731int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2732 const struct nlmsghdr *nlh,
2733 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734{
2735 struct netlink_callback *cb;
2736 struct sock *sk;
2737 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002738 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Patrick McHardyf9c22882013-04-17 06:47:04 +00002740 /* Memory mapped dump requests need to be copied to avoid looping
2741 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2742 * a reference to the skb.
2743 */
2744 if (netlink_skb_is_mmaped(skb)) {
2745 skb = skb_copy(skb, GFP_KERNEL);
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002746 if (skb == NULL)
Patrick McHardyf9c22882013-04-17 06:47:04 +00002747 return -ENOBUFS;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002748 } else
2749 atomic_inc(&skb->users);
2750
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002751 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
2752 if (sk == NULL) {
2753 ret = -ECONNREFUSED;
2754 goto error_free;
2755 }
2756
2757 nlk = nlk_sk(sk);
2758 mutex_lock(nlk->cb_mutex);
2759 /* A dump is in progress... */
2760 if (nlk->cb_running) {
2761 ret = -EBUSY;
2762 goto error_unlock;
2763 }
2764 /* add reference of module which cb->dump belongs to */
2765 if (!try_module_get(control->module)) {
2766 ret = -EPROTONOSUPPORT;
2767 goto error_unlock;
2768 }
2769
2770 cb = &nlk->cb;
2771 memset(cb, 0, sizeof(*cb));
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002772 cb->dump = control->dump;
2773 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002775 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002776 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002777 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 cb->skb = skb;
2779
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002780 nlk->cb_running = true;
Gao feng6dc878a2012-10-04 20:15:48 +00002781
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002782 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783
Andrey Vaginb44d2112011-02-21 02:40:47 +00002784 ret = netlink_dump(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002786
Andrey Vaginb44d2112011-02-21 02:40:47 +00002787 if (ret)
2788 return ret;
2789
Denis V. Lunev5c582982007-10-23 20:29:25 -07002790 /* We successfully started a dump, by returning -EINTR we
2791 * signal not to send ACK even if it was requested.
2792 */
2793 return -EINTR;
Pravin B Shelar16b304f2013-08-15 15:31:06 -07002794
2795error_unlock:
2796 sock_put(sk);
2797 mutex_unlock(nlk->cb_mutex);
2798error_free:
2799 kfree_skb(skb);
2800 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801}
Gao feng6dc878a2012-10-04 20:15:48 +00002802EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2805{
2806 struct sk_buff *skb;
2807 struct nlmsghdr *rep;
2808 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002809 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Thomas Graf339bf982006-11-10 14:10:15 -08002811 /* error messages get the original request appened */
2812 if (err)
2813 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814
Patrick McHardyf9c22882013-04-17 06:47:04 +00002815 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2816 NETLINK_CB(in_skb).portid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (!skb) {
2818 struct sock *sk;
2819
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002820 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002821 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002822 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 if (sk) {
2824 sk->sk_err = ENOBUFS;
2825 sk->sk_error_report(sk);
2826 sock_put(sk);
2827 }
2828 return;
2829 }
2830
Eric W. Biederman15e47302012-09-07 20:12:54 +00002831 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002832 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002833 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002835 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002836 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002838EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002840int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002841 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002842{
Thomas Graf82ace472005-11-10 02:25:53 +01002843 struct nlmsghdr *nlh;
2844 int err;
2845
2846 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002847 int msglen;
2848
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002849 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002850 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002851
Martin Murrayad8e4b72006-01-10 13:02:29 -08002852 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002853 return 0;
2854
Thomas Grafd35b6852007-03-22 23:28:46 -07002855 /* Only requests are handled by the kernel */
2856 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002857 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002858
Thomas Graf45e7ae72007-03-22 23:29:10 -07002859 /* Skip control messages */
2860 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002861 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002862
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002863 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002864 if (err == -EINTR)
2865 goto skip;
2866
2867ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002868 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002869 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002870
Denis V. Lunev5c582982007-10-23 20:29:25 -07002871skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002872 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002873 if (msglen > skb->len)
2874 msglen = skb->len;
2875 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002876 }
2877
2878 return 0;
2879}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002880EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002881
2882/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002883 * nlmsg_notify - send a notification netlink message
2884 * @sk: netlink socket to use
2885 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002886 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002887 * @group: destination multicast group or 0
2888 * @report: 1 to report back, 0 to disable
2889 * @flags: allocation flags
2890 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002891int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002892 unsigned int group, int report, gfp_t flags)
2893{
2894 int err = 0;
2895
2896 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002897 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002898
2899 if (report) {
2900 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002901 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002902 }
2903
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002904 /* errors reported via destination sk->sk_err, but propagate
2905 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002906 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002907 }
2908
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002909 if (report) {
2910 int err2;
2911
Eric W. Biederman15e47302012-09-07 20:12:54 +00002912 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002913 if (!err || err == -ESRCH)
2914 err = err2;
2915 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002916
2917 return err;
2918}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002919EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921#ifdef CONFIG_PROC_FS
2922struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002923 struct seq_net_private p;
Herbert Xu56d28b12015-02-04 07:33:24 +11002924 struct rhashtable_iter hti;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 int link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926};
2927
Herbert Xu56d28b12015-02-04 07:33:24 +11002928static int netlink_walk_start(struct nl_seq_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929{
Herbert Xu56d28b12015-02-04 07:33:24 +11002930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Herbert Xu56d28b12015-02-04 07:33:24 +11002932 err = rhashtable_walk_init(&nl_table[iter->link].hash, &iter->hti);
2933 if (err) {
2934 iter->link = MAX_LINKS;
2935 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 }
Herbert Xu56d28b12015-02-04 07:33:24 +11002937
2938 err = rhashtable_walk_start(&iter->hti);
2939 return err == -EAGAIN ? 0 : err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940}
2941
Herbert Xu56d28b12015-02-04 07:33:24 +11002942static void netlink_walk_stop(struct nl_seq_iter *iter)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Herbert Xu56d28b12015-02-04 07:33:24 +11002944 rhashtable_walk_stop(&iter->hti);
2945 rhashtable_walk_exit(&iter->hti);
2946}
2947
2948static void *__netlink_seq_next(struct seq_file *seq)
2949{
2950 struct nl_seq_iter *iter = seq->private;
2951 struct netlink_sock *nlk;
2952
2953 do {
2954 for (;;) {
2955 int err;
2956
2957 nlk = rhashtable_walk_next(&iter->hti);
2958
2959 if (IS_ERR(nlk)) {
2960 if (PTR_ERR(nlk) == -EAGAIN)
2961 continue;
2962
2963 return nlk;
2964 }
2965
2966 if (nlk)
2967 break;
2968
2969 netlink_walk_stop(iter);
2970 if (++iter->link >= MAX_LINKS)
2971 return NULL;
2972
2973 err = netlink_walk_start(iter);
2974 if (err)
2975 return ERR_PTR(err);
2976 }
2977 } while (sock_net(&nlk->sk) != seq_file_net(seq));
2978
2979 return nlk;
2980}
2981
2982static void *netlink_seq_start(struct seq_file *seq, loff_t *posp)
2983{
2984 struct nl_seq_iter *iter = seq->private;
2985 void *obj = SEQ_START_TOKEN;
2986 loff_t pos;
2987 int err;
2988
2989 iter->link = 0;
2990
2991 err = netlink_walk_start(iter);
2992 if (err)
2993 return ERR_PTR(err);
2994
2995 for (pos = *posp; pos && obj && !IS_ERR(obj); pos--)
2996 obj = __netlink_seq_next(seq);
2997
2998 return obj;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999}
3000
3001static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 ++*pos;
Herbert Xu56d28b12015-02-04 07:33:24 +11003004 return __netlink_seq_next(seq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
3007static void netlink_seq_stop(struct seq_file *seq, void *v)
3008{
Herbert Xu56d28b12015-02-04 07:33:24 +11003009 struct nl_seq_iter *iter = seq->private;
3010
3011 if (iter->link >= MAX_LINKS)
3012 return;
3013
3014 netlink_walk_stop(iter);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015}
3016
3017
3018static int netlink_seq_show(struct seq_file *seq, void *v)
3019{
Eric Dumazet658cb352012-04-22 21:30:21 +00003020 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 seq_puts(seq,
3022 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00003023 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00003024 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 struct sock *s = v;
3026 struct netlink_sock *nlk = nlk_sk(s);
3027
Pravin B Shelar16b304f2013-08-15 15:31:06 -07003028 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %d %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 s,
3030 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00003031 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07003032 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07003033 sk_rmem_alloc_get(s),
3034 sk_wmem_alloc_get(s),
Pravin B Shelar16b304f2013-08-15 15:31:06 -07003035 nlk->cb_running,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07003036 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00003037 atomic_read(&s->sk_drops),
3038 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 );
3040
3041 }
3042 return 0;
3043}
3044
Philippe De Muyter56b3d972007-07-10 23:07:31 -07003045static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 .start = netlink_seq_start,
3047 .next = netlink_seq_next,
3048 .stop = netlink_seq_stop,
3049 .show = netlink_seq_show,
3050};
3051
3052
3053static int netlink_seq_open(struct inode *inode, struct file *file)
3054{
Denis V. Luneve372c412007-11-19 22:31:54 -08003055 return seq_open_net(inode, file, &netlink_seq_ops,
3056 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003057}
3058
Arjan van de Venda7071d2007-02-12 00:55:36 -08003059static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 .owner = THIS_MODULE,
3061 .open = netlink_seq_open,
3062 .read = seq_read,
3063 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08003064 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065};
3066
3067#endif
3068
3069int netlink_register_notifier(struct notifier_block *nb)
3070{
Alan Sterne041c682006-03-27 01:16:30 -08003071 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003073EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
3075int netlink_unregister_notifier(struct notifier_block *nb)
3076{
Alan Sterne041c682006-03-27 01:16:30 -08003077 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08003079EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003080
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08003081static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 .family = PF_NETLINK,
3083 .owner = THIS_MODULE,
3084 .release = netlink_release,
3085 .bind = netlink_bind,
3086 .connect = netlink_connect,
3087 .socketpair = sock_no_socketpair,
3088 .accept = sock_no_accept,
3089 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00003090 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 .ioctl = sock_no_ioctl,
3092 .listen = sock_no_listen,
3093 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07003094 .setsockopt = netlink_setsockopt,
3095 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 .sendmsg = netlink_sendmsg,
3097 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00003098 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 .sendpage = sock_no_sendpage,
3100};
3101
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00003102static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 .family = PF_NETLINK,
3104 .create = netlink_create,
3105 .owner = THIS_MODULE, /* for consistency 8) */
3106};
3107
Pavel Emelyanov46650792007-10-08 20:38:39 -07003108static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003109{
3110#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00003111 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003112 return -ENOMEM;
3113#endif
3114 return 0;
3115}
3116
Pavel Emelyanov46650792007-10-08 20:38:39 -07003117static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003118{
3119#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003120 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003121#endif
3122}
3123
David S. Millerb963ea82010-08-30 19:08:01 -07003124static void __init netlink_add_usersock_entry(void)
3125{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003126 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07003127 int groups = 32;
3128
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003129 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07003130 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003131 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07003132
3133 netlink_table_grab();
3134
3135 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00003136 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07003137 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
3138 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00003139 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07003140
3141 netlink_table_ungrab();
3142}
3143
Denis V. Lunev022cbae2007-11-13 03:23:50 -08003144static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003145 .init = netlink_net_init,
3146 .exit = netlink_net_exit,
3147};
3148
Patrick McHardy49f7b332015-03-25 13:07:45 +00003149static inline u32 netlink_hash(const void *data, u32 len, u32 seed)
Herbert Xuc428ecd2015-03-20 21:57:01 +11003150{
3151 const struct netlink_sock *nlk = data;
3152 struct netlink_compare_arg arg;
3153
3154 netlink_compare_arg_init(&arg, sock_net(&nlk->sk), nlk->portid);
Herbert Xu11b58ba2015-03-24 00:50:22 +11003155 return jhash2((u32 *)&arg, netlink_compare_arg_len / sizeof(u32), seed);
Herbert Xuc428ecd2015-03-20 21:57:01 +11003156}
3157
3158static const struct rhashtable_params netlink_rhashtable_params = {
3159 .head_offset = offsetof(struct netlink_sock, node),
3160 .key_len = netlink_compare_arg_len,
Herbert Xuc428ecd2015-03-20 21:57:01 +11003161 .obj_hashfn = netlink_hash,
3162 .obj_cmpfn = netlink_compare,
Thomas Grafb5e2c152015-03-24 20:42:19 +00003163 .automatic_shrinking = true,
Herbert Xuc428ecd2015-03-20 21:57:01 +11003164};
3165
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166static int __init netlink_proto_init(void)
3167{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 int err = proto_register(&netlink_proto, 0);
3170
3171 if (err != 0)
3172 goto out;
3173
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00003174 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07003176 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003177 if (!nl_table)
3178 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 for (i = 0; i < MAX_LINKS; i++) {
Herbert Xuc428ecd2015-03-20 21:57:01 +11003181 if (rhashtable_init(&nl_table[i].hash,
3182 &netlink_rhashtable_params) < 0) {
Thomas Grafe3416942014-08-02 11:47:45 +02003183 while (--i > 0)
3184 rhashtable_destroy(&nl_table[i].hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003186 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189
Daniel Borkmannbcbde0d2013-06-21 19:38:07 +02003190 INIT_LIST_HEAD(&netlink_tap_all);
3191
David S. Millerb963ea82010-08-30 19:08:01 -07003192 netlink_add_usersock_entry();
3193
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02003195 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09003196 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 rtnetlink_init();
3198out:
3199 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07003200panic:
3201 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204core_initcall(netlink_proto_init);