blob: 1d3c7128e90ea324b6a00370bbd8755c2ea80550 [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>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070016 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/stat.h>
34#include <linux/socket.h>
35#include <linux/un.h>
36#include <linux/fcntl.h>
37#include <linux/termios.h>
38#include <linux/sockios.h>
39#include <linux/net.h>
40#include <linux/fs.h>
41#include <linux/slab.h>
42#include <asm/uaccess.h>
43#include <linux/skbuff.h>
44#include <linux/netdevice.h>
45#include <linux/rtnetlink.h>
46#include <linux/proc_fs.h>
47#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/notifier.h>
49#include <linux/security.h>
50#include <linux/jhash.h>
51#include <linux/jiffies.h>
52#include <linux/random.h>
53#include <linux/bitops.h>
54#include <linux/mm.h>
55#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010056#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070057#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000058#include <linux/vmalloc.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010059
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020060#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#include <net/sock.h>
62#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010063#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Andrey Vagin0f29c762013-03-21 20:33:47 +040065#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Eric Dumazet5c398dc2010-10-24 04:27:10 +000067struct listeners {
68 struct rcu_head rcu;
69 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000070};
71
Patrick McHardycd967e02013-04-17 06:46:56 +000072/* state bits */
73#define NETLINK_CONGESTED 0x0
74
75/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070076#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070077#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000078#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070079#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070080
David S. Miller035c4c12011-12-23 17:33:03 -050081static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070082{
83 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
84}
85
Andrey Vagin0f29c762013-03-21 20:33:47 +040086struct netlink_table *nl_table;
87EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
90
91static int netlink_dump(struct sock *sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Andrey Vagin0f29c762013-03-21 20:33:47 +040093DEFINE_RWLOCK(nl_table_lock);
94EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static atomic_t nl_table_users = ATOMIC_INIT(0);
96
Eric Dumazet6d772ac2012-10-18 03:21:55 +000097#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
98
Alan Sterne041c682006-03-27 01:16:30 -080099static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000101static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700102{
103 return group ? 1 << (group - 1) : 0;
104}
105
Eric W. Biederman15e47302012-09-07 20:12:54 +0000106static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000108 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000111#ifdef CONFIG_NETLINK_MMAP
112static __pure struct page *pgvec_to_page(const void *addr)
113{
114 if (is_vmalloc_addr(addr))
115 return vmalloc_to_page(addr);
116 else
117 return virt_to_page(addr);
118}
119
120static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
121{
122 unsigned int i;
123
124 for (i = 0; i < len; i++) {
125 if (pg_vec[i] != NULL) {
126 if (is_vmalloc_addr(pg_vec[i]))
127 vfree(pg_vec[i]);
128 else
129 free_pages((unsigned long)pg_vec[i], order);
130 }
131 }
132 kfree(pg_vec);
133}
134
135static void *alloc_one_pg_vec_page(unsigned long order)
136{
137 void *buffer;
138 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
139 __GFP_NOWARN | __GFP_NORETRY;
140
141 buffer = (void *)__get_free_pages(gfp_flags, order);
142 if (buffer != NULL)
143 return buffer;
144
145 buffer = vzalloc((1 << order) * PAGE_SIZE);
146 if (buffer != NULL)
147 return buffer;
148
149 gfp_flags &= ~__GFP_NORETRY;
150 return (void *)__get_free_pages(gfp_flags, order);
151}
152
153static void **alloc_pg_vec(struct netlink_sock *nlk,
154 struct nl_mmap_req *req, unsigned int order)
155{
156 unsigned int block_nr = req->nm_block_nr;
157 unsigned int i;
158 void **pg_vec, *ptr;
159
160 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
161 if (pg_vec == NULL)
162 return NULL;
163
164 for (i = 0; i < block_nr; i++) {
165 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
166 if (pg_vec[i] == NULL)
167 goto err1;
168 }
169
170 return pg_vec;
171err1:
172 free_pg_vec(pg_vec, order, block_nr);
173 return NULL;
174}
175
176static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
177 bool closing, bool tx_ring)
178{
179 struct netlink_sock *nlk = nlk_sk(sk);
180 struct netlink_ring *ring;
181 struct sk_buff_head *queue;
182 void **pg_vec = NULL;
183 unsigned int order = 0;
184 int err;
185
186 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
187 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
188
189 if (!closing) {
190 if (atomic_read(&nlk->mapped))
191 return -EBUSY;
192 if (atomic_read(&ring->pending))
193 return -EBUSY;
194 }
195
196 if (req->nm_block_nr) {
197 if (ring->pg_vec != NULL)
198 return -EBUSY;
199
200 if ((int)req->nm_block_size <= 0)
201 return -EINVAL;
202 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
203 return -EINVAL;
204 if (req->nm_frame_size < NL_MMAP_HDRLEN)
205 return -EINVAL;
206 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
207 return -EINVAL;
208
209 ring->frames_per_block = req->nm_block_size /
210 req->nm_frame_size;
211 if (ring->frames_per_block == 0)
212 return -EINVAL;
213 if (ring->frames_per_block * req->nm_block_nr !=
214 req->nm_frame_nr)
215 return -EINVAL;
216
217 order = get_order(req->nm_block_size);
218 pg_vec = alloc_pg_vec(nlk, req, order);
219 if (pg_vec == NULL)
220 return -ENOMEM;
221 } else {
222 if (req->nm_frame_nr)
223 return -EINVAL;
224 }
225
226 err = -EBUSY;
227 mutex_lock(&nlk->pg_vec_lock);
228 if (closing || atomic_read(&nlk->mapped) == 0) {
229 err = 0;
230 spin_lock_bh(&queue->lock);
231
232 ring->frame_max = req->nm_frame_nr - 1;
233 ring->head = 0;
234 ring->frame_size = req->nm_frame_size;
235 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
236
237 swap(ring->pg_vec_len, req->nm_block_nr);
238 swap(ring->pg_vec_order, order);
239 swap(ring->pg_vec, pg_vec);
240
241 __skb_queue_purge(queue);
242 spin_unlock_bh(&queue->lock);
243
244 WARN_ON(atomic_read(&nlk->mapped));
245 }
246 mutex_unlock(&nlk->pg_vec_lock);
247
248 if (pg_vec)
249 free_pg_vec(pg_vec, order, req->nm_block_nr);
250 return err;
251}
252
253static void netlink_mm_open(struct vm_area_struct *vma)
254{
255 struct file *file = vma->vm_file;
256 struct socket *sock = file->private_data;
257 struct sock *sk = sock->sk;
258
259 if (sk)
260 atomic_inc(&nlk_sk(sk)->mapped);
261}
262
263static void netlink_mm_close(struct vm_area_struct *vma)
264{
265 struct file *file = vma->vm_file;
266 struct socket *sock = file->private_data;
267 struct sock *sk = sock->sk;
268
269 if (sk)
270 atomic_dec(&nlk_sk(sk)->mapped);
271}
272
273static const struct vm_operations_struct netlink_mmap_ops = {
274 .open = netlink_mm_open,
275 .close = netlink_mm_close,
276};
277
278static int netlink_mmap(struct file *file, struct socket *sock,
279 struct vm_area_struct *vma)
280{
281 struct sock *sk = sock->sk;
282 struct netlink_sock *nlk = nlk_sk(sk);
283 struct netlink_ring *ring;
284 unsigned long start, size, expected;
285 unsigned int i;
286 int err = -EINVAL;
287
288 if (vma->vm_pgoff)
289 return -EINVAL;
290
291 mutex_lock(&nlk->pg_vec_lock);
292
293 expected = 0;
294 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
295 if (ring->pg_vec == NULL)
296 continue;
297 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
298 }
299
300 if (expected == 0)
301 goto out;
302
303 size = vma->vm_end - vma->vm_start;
304 if (size != expected)
305 goto out;
306
307 start = vma->vm_start;
308 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
309 if (ring->pg_vec == NULL)
310 continue;
311
312 for (i = 0; i < ring->pg_vec_len; i++) {
313 struct page *page;
314 void *kaddr = ring->pg_vec[i];
315 unsigned int pg_num;
316
317 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
318 page = pgvec_to_page(kaddr);
319 err = vm_insert_page(vma, start, page);
320 if (err < 0)
321 goto out;
322 start += PAGE_SIZE;
323 kaddr += PAGE_SIZE;
324 }
325 }
326 }
327
328 atomic_inc(&nlk->mapped);
329 vma->vm_ops = &netlink_mmap_ops;
330 err = 0;
331out:
332 mutex_unlock(&nlk->pg_vec_lock);
333 return 0;
334}
335#else /* CONFIG_NETLINK_MMAP */
336#define netlink_mmap sock_no_mmap
337#endif /* CONFIG_NETLINK_MMAP */
338
Eric Dumazet658cb352012-04-22 21:30:21 +0000339static void netlink_destroy_callback(struct netlink_callback *cb)
340{
341 kfree_skb(cb->skb);
342 kfree(cb);
343}
344
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000345static void netlink_consume_callback(struct netlink_callback *cb)
346{
347 consume_skb(cb->skb);
348 kfree(cb);
349}
350
Patrick McHardycf0a0182013-04-17 06:47:00 +0000351static void netlink_skb_destructor(struct sk_buff *skb)
352{
353 sock_rfree(skb);
354}
355
356static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
357{
358 WARN_ON(skb->sk != NULL);
359 skb->sk = sk;
360 skb->destructor = netlink_skb_destructor;
361 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
362 sk_mem_charge(sk, skb->truesize);
363}
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365static void netlink_sock_destruct(struct sock *sk)
366{
Herbert Xu3f660d62007-05-03 03:17:14 -0700367 struct netlink_sock *nlk = nlk_sk(sk);
368
Herbert Xu3f660d62007-05-03 03:17:14 -0700369 if (nlk->cb) {
370 if (nlk->cb->done)
371 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000372
373 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700374 netlink_destroy_callback(nlk->cb);
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000378#ifdef CONFIG_NETLINK_MMAP
379 if (1) {
380 struct nl_mmap_req req;
381
382 memset(&req, 0, sizeof(req));
383 if (nlk->rx_ring.pg_vec)
384 netlink_set_ring(sk, &req, true, false);
385 memset(&req, 0, sizeof(req));
386 if (nlk->tx_ring.pg_vec)
387 netlink_set_ring(sk, &req, true, true);
388 }
389#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800392 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return;
394 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700395
396 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
397 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
398 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800401/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
402 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
404 * this, _but_ remember, it adds useless work on UP machines.
405 */
406
Johannes Bergd136f1b2009-09-12 03:03:15 +0000407void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800408 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000410 might_sleep();
411
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700412 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 if (atomic_read(&nl_table_users)) {
415 DECLARE_WAITQUEUE(wait, current);
416
417 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800418 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 set_current_state(TASK_UNINTERRUPTIBLE);
420 if (atomic_read(&nl_table_users) == 0)
421 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700422 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700424 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
426
427 __set_current_state(TASK_RUNNING);
428 remove_wait_queue(&nl_table_wait, &wait);
429 }
430}
431
Johannes Bergd136f1b2009-09-12 03:03:15 +0000432void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800433 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700435 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 wake_up(&nl_table_wait);
437}
438
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800439static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440netlink_lock_table(void)
441{
442 /* read_lock() synchronizes us to netlink_table_grab */
443
444 read_lock(&nl_table_lock);
445 atomic_inc(&nl_table_users);
446 read_unlock(&nl_table_lock);
447}
448
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800449static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450netlink_unlock_table(void)
451{
452 if (atomic_dec_and_test(&nl_table_users))
453 wake_up(&nl_table_wait);
454}
455
Eric W. Biederman15e47302012-09-07 20:12:54 +0000456static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000458 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 struct hlist_head *head;
460 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000463 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800464 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000465 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 sock_hold(sk);
467 goto found;
468 }
469 }
470 sk = NULL;
471found:
472 read_unlock(&nl_table_lock);
473 return sk;
474}
475
Eric W. Biederman15e47302012-09-07 20:12:54 +0000476static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800479 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 else
481 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800482 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
483 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
485
Eric W. Biederman15e47302012-09-07 20:12:54 +0000486static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
488 if (size <= PAGE_SIZE)
489 kfree(table);
490 else
491 free_pages((unsigned long)table, get_order(size));
492}
493
Eric W. Biederman15e47302012-09-07 20:12:54 +0000494static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495{
496 unsigned int omask, mask, shift;
497 size_t osize, size;
498 struct hlist_head *otable, *table;
499 int i;
500
501 omask = mask = hash->mask;
502 osize = size = (mask + 1) * sizeof(*table);
503 shift = hash->shift;
504
505 if (grow) {
506 if (++shift > hash->max_shift)
507 return 0;
508 mask = mask * 2 + 1;
509 size *= 2;
510 }
511
Eric W. Biederman15e47302012-09-07 20:12:54 +0000512 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 if (!table)
514 return 0;
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 otable = hash->table;
517 hash->table = table;
518 hash->mask = mask;
519 hash->shift = shift;
520 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
521
522 for (i = 0; i <= omask; i++) {
523 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800524 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Sasha Levinb67bfe02013-02-27 17:06:00 -0800526 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000527 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
529
Eric W. Biederman15e47302012-09-07 20:12:54 +0000530 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 hash->rehash_time = jiffies + 10 * 60 * HZ;
532 return 1;
533}
534
Eric W. Biederman15e47302012-09-07 20:12:54 +0000535static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 int avg = hash->entries >> hash->shift;
538
Eric W. Biederman15e47302012-09-07 20:12:54 +0000539 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 1;
541
542 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000543 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return 1;
545 }
546
547 return 0;
548}
549
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800550static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Patrick McHardy4277a082006-03-20 18:52:01 -0800552static void
553netlink_update_listeners(struct sock *sk)
554{
555 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800556 unsigned long mask;
557 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000558 struct listeners *listeners;
559
560 listeners = nl_deref_protected(tbl->listeners);
561 if (!listeners)
562 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800563
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700564 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800565 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800566 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700567 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
568 mask |= nlk_sk(sk)->groups[i];
569 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000570 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800571 }
572 /* this function is only called with the netlink table "grabbed", which
573 * makes sure updates are visible before bind or setsockopt return. */
574}
575
Eric W. Biederman15e47302012-09-07 20:12:54 +0000576static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000578 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 struct hlist_head *head;
580 int err = -EADDRINUSE;
581 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 int len;
583
584 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000585 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800587 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000588 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 break;
590 len++;
591 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800592 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto err;
594
595 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000596 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 goto err;
598
599 err = -ENOMEM;
600 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
601 goto err;
602
Eric W. Biederman15e47302012-09-07 20:12:54 +0000603 if (len && nl_portid_hash_dilute(hash, len))
604 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000606 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 sk_add_node(sk, head);
608 err = 0;
609
610err:
611 netlink_table_ungrab();
612 return err;
613}
614
615static void netlink_remove(struct sock *sk)
616{
617 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700618 if (sk_del_node_init(sk))
619 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700620 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 __sk_del_bind_node(sk);
622 netlink_table_ungrab();
623}
624
625static struct proto netlink_proto = {
626 .name = "NETLINK",
627 .owner = THIS_MODULE,
628 .obj_size = sizeof(struct netlink_sock),
629};
630
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700631static int __netlink_create(struct net *net, struct socket *sock,
632 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633{
634 struct sock *sk;
635 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700636
637 sock->ops = &netlink_ops;
638
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700639 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700640 if (!sk)
641 return -ENOMEM;
642
643 sock_init_data(sock, sk);
644
645 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000646 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700647 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000648 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700649 nlk->cb_mutex = &nlk->cb_def_mutex;
650 mutex_init(nlk->cb_mutex);
651 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700652 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000653#ifdef CONFIG_NETLINK_MMAP
654 mutex_init(&nlk->pg_vec_lock);
655#endif
Patrick McHardyab33a172005-08-14 19:31:36 -0700656
657 sk->sk_destruct = netlink_sock_destruct;
658 sk->sk_protocol = protocol;
659 return 0;
660}
661
Eric Paris3f378b62009-11-05 22:18:14 -0800662static int netlink_create(struct net *net, struct socket *sock, int protocol,
663 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700664{
665 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700666 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700667 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000668 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700669 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 sock->state = SS_UNCONNECTED;
672
673 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
674 return -ESOCKTNOSUPPORT;
675
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800676 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return -EPROTONOSUPPORT;
678
Patrick McHardy77247bb2005-08-14 19:27:13 -0700679 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700680#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700681 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700682 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700683 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700684 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700685 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700686#endif
687 if (nl_table[protocol].registered &&
688 try_module_get(nl_table[protocol].module))
689 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000690 else
691 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700692 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000693 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700694 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700695
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000696 if (err < 0)
697 goto out;
698
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800699 err = __netlink_create(net, sock, cb_mutex, protocol);
700 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700701 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
David S. Miller6f756a82008-11-23 17:34:03 -0800703 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800704 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800705 local_bh_enable();
706
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700707 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700708 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000709 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700710out:
711 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
Patrick McHardyab33a172005-08-14 19:31:36 -0700713out_module:
714 module_put(module);
715 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716}
717
718static int netlink_release(struct socket *sock)
719{
720 struct sock *sk = sock->sk;
721 struct netlink_sock *nlk;
722
723 if (!sk)
724 return 0;
725
726 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700727 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 nlk = nlk_sk(sk);
729
Herbert Xu3f660d62007-05-03 03:17:14 -0700730 /*
731 * OK. Socket is unlinked, any packets that arrive now
732 * will be purged.
733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 sock->sk = NULL;
736 wake_up_interruptible_all(&nlk->wait);
737
738 skb_queue_purge(&sk->sk_write_queue);
739
Eric W. Biederman15e47302012-09-07 20:12:54 +0000740 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900742 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000744 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 };
Alan Sterne041c682006-03-27 01:16:30 -0800746 atomic_notifier_call_chain(&netlink_chain,
747 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900748 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700749
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800750 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700751
Patrick McHardy4277a082006-03-20 18:52:01 -0800752 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700753 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800754 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
755 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000756 struct listeners *old;
757
758 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
759 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
760 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800761 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000762 nl_table[sk->sk_protocol].bind = NULL;
763 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800764 nl_table[sk->sk_protocol].registered = 0;
765 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000766 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800767 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000768 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800769 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700770
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700771 kfree(nlk->groups);
772 nlk->groups = NULL;
773
Eric Dumazet37558102008-11-24 14:05:22 -0800774 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800775 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800776 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 sock_put(sk);
778 return 0;
779}
780
781static int netlink_autobind(struct socket *sock)
782{
783 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900784 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000785 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct hlist_head *head;
787 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000788 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 int err;
790 static s32 rover = -4097;
791
792retry:
793 cond_resched();
794 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000795 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800796 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900797 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200798 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000799 if (nlk_sk(osk)->portid == portid) {
800 /* Bind collision, search negative portid values. */
801 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (rover > -4097)
803 rover = -4097;
804 netlink_table_ungrab();
805 goto retry;
806 }
807 }
808 netlink_table_ungrab();
809
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (err == -EADDRINUSE)
812 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700813
814 /* If 2 threads race to autobind, that is fine. */
815 if (err == -EBUSY)
816 err = 0;
817
818 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000821static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900822{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000823 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000824 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900825}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700827static void
828netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
829{
830 struct netlink_sock *nlk = nlk_sk(sk);
831
832 if (nlk->subscriptions && !subscriptions)
833 __sk_del_bind_node(sk);
834 else if (!nlk->subscriptions && subscriptions)
835 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
836 nlk->subscriptions = subscriptions;
837}
838
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700839static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -0700840{
841 struct netlink_sock *nlk = nlk_sk(sk);
842 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700843 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700844 int err = 0;
845
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700846 netlink_table_grab();
847
Patrick McHardy513c2502005-09-06 15:43:59 -0700848 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700849 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -0700850 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700851 goto out_unlock;
852 }
Patrick McHardy513c2502005-09-06 15:43:59 -0700853
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700854 if (nlk->ngroups >= groups)
855 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -0700856
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700857 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
858 if (new_groups == NULL) {
859 err = -ENOMEM;
860 goto out_unlock;
861 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800862 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700863 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
864
865 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -0700866 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700867 out_unlock:
868 netlink_table_ungrab();
869 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700870}
871
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800872static int netlink_bind(struct socket *sock, struct sockaddr *addr,
873 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874{
875 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900876 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct netlink_sock *nlk = nlk_sk(sk);
878 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
879 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900880
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +0000881 if (addr_len < sizeof(struct sockaddr_nl))
882 return -EINVAL;
883
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 if (nladdr->nl_family != AF_NETLINK)
885 return -EINVAL;
886
887 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -0700888 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000889 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -0700890 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700891 err = netlink_realloc_groups(sk);
892 if (err)
893 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Eric W. Biederman15e47302012-09-07 20:12:54 +0000896 if (nlk->portid) {
897 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return -EINVAL;
899 } else {
900 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200901 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 netlink_autobind(sock);
903 if (err)
904 return err;
905 }
906
Patrick McHardy513c2502005-09-06 15:43:59 -0700907 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 return 0;
909
910 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700911 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900912 hweight32(nladdr->nl_groups) -
913 hweight32(nlk->groups[0]));
914 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -0800915 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 netlink_table_ungrab();
917
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000918 if (nlk->netlink_bind && nlk->groups[0]) {
919 int i;
920
921 for (i=0; i<nlk->ngroups; i++) {
922 if (test_bit(i, nlk->groups))
923 nlk->netlink_bind(i);
924 }
925 }
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 return 0;
928}
929
930static int netlink_connect(struct socket *sock, struct sockaddr *addr,
931 int alen, int flags)
932{
933 int err = 0;
934 struct sock *sk = sock->sk;
935 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800936 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Changli Gao6503d962010-03-31 22:58:26 +0000938 if (alen < sizeof(addr->sa_family))
939 return -EINVAL;
940
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (addr->sa_family == AF_UNSPEC) {
942 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000943 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -0700944 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return 0;
946 }
947 if (addr->sa_family != AF_NETLINK)
948 return -EINVAL;
949
950 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000951 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return -EPERM;
953
Eric W. Biederman15e47302012-09-07 20:12:54 +0000954 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 err = netlink_autobind(sock);
956
957 if (err == 0) {
958 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000959 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700960 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
963 return err;
964}
965
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800966static int netlink_getname(struct socket *sock, struct sockaddr *addr,
967 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968{
969 struct sock *sk = sock->sk;
970 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +0000971 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 nladdr->nl_family = AF_NETLINK;
974 nladdr->nl_pad = 0;
975 *addr_len = sizeof(*nladdr);
976
977 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000978 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -0700979 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000981 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -0700982 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 }
984 return 0;
985}
986
987static void netlink_overrun(struct sock *sk)
988{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700989 struct netlink_sock *nlk = nlk_sk(sk);
990
991 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
Patrick McHardycd967e02013-04-17 06:46:56 +0000992 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700993 sk->sk_err = ENOBUFS;
994 sk->sk_error_report(sk);
995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -0700997 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Eric W. Biederman15e47302012-09-07 20:12:54 +00001000static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 struct sock *sock;
1003 struct netlink_sock *nlk;
1004
Eric W. Biederman15e47302012-09-07 20:12:54 +00001005 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (!sock)
1007 return ERR_PTR(-ECONNREFUSED);
1008
1009 /* Don't bother queuing skb if kernel socket has no input function */
1010 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001011 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001012 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 sock_put(sock);
1014 return ERR_PTR(-ECONNREFUSED);
1015 }
1016 return sock;
1017}
1018
1019struct sock *netlink_getsockbyfilp(struct file *filp)
1020{
Al Viro496ad9a2013-01-23 17:07:38 -05001021 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 struct sock *sock;
1023
1024 if (!S_ISSOCK(inode->i_mode))
1025 return ERR_PTR(-ENOTSOCK);
1026
1027 sock = SOCKET_I(inode)->sk;
1028 if (sock->sk_family != AF_NETLINK)
1029 return ERR_PTR(-EINVAL);
1030
1031 sock_hold(sock);
1032 return sock;
1033}
1034
1035/*
1036 * Attach a skb to a netlink socket.
1037 * The caller must hold a reference to the destination socket. On error, the
1038 * reference is dropped. The skb is not send to the destination, just all
1039 * all error checks are performed and memory in the queue is reserved.
1040 * Return values:
1041 * < 0: error. skb freed, reference to sock dropped.
1042 * 0: continue
1043 * 1: repeat lookup - reference dropped while waiting for socket memory.
1044 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001045int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001046 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct netlink_sock *nlk;
1049
1050 nlk = nlk_sk(sk);
1051
1052 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001053 test_bit(NETLINK_CONGESTED, &nlk->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001055 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001056 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 netlink_overrun(sk);
1058 sock_put(sk);
1059 kfree_skb(skb);
1060 return -EAGAIN;
1061 }
1062
1063 __set_current_state(TASK_INTERRUPTIBLE);
1064 add_wait_queue(&nlk->wait, &wait);
1065
1066 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001067 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001069 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 __set_current_state(TASK_RUNNING);
1072 remove_wait_queue(&nlk->wait, &wait);
1073 sock_put(sk);
1074
1075 if (signal_pending(current)) {
1076 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001077 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 }
1079 return 1;
1080 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001081 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 return 0;
1083}
1084
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001085static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 int len = skb->len;
1088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 skb_queue_tail(&sk->sk_receive_queue, skb);
1090 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001091 return len;
1092}
1093
1094int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1095{
1096 int len = __netlink_sendskb(sk, skb);
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 sock_put(sk);
1099 return len;
1100}
1101
1102void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1103{
1104 kfree_skb(skb);
1105 sock_put(sk);
1106}
1107
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001108static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 int delta;
1111
Patrick McHardy1298ca42013-04-17 06:46:59 +00001112 WARN_ON(skb->sk != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001114 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 if (delta * 2 < skb->truesize)
1116 return skb;
1117
1118 if (skb_shared(skb)) {
1119 struct sk_buff *nskb = skb_clone(skb, allocation);
1120 if (!nskb)
1121 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001122 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 skb = nskb;
1124 }
1125
1126 if (!pskb_expand_head(skb, 0, -delta, allocation))
1127 skb->truesize -= delta;
1128
1129 return skb;
1130}
1131
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001132static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001133{
1134 struct netlink_sock *nlk = nlk_sk(sk);
1135
1136 if (skb_queue_empty(&sk->sk_receive_queue))
Patrick McHardycd967e02013-04-17 06:46:56 +00001137 clear_bit(NETLINK_CONGESTED, &nlk->state);
1138 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001139 wake_up_interruptible(&nlk->wait);
1140}
1141
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001142static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1143 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001144{
1145 int ret;
1146 struct netlink_sock *nlk = nlk_sk(sk);
1147
1148 ret = -ECONNREFUSED;
1149 if (nlk->netlink_rcv != NULL) {
1150 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001151 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001152 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001153 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001154 consume_skb(skb);
1155 } else {
1156 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001157 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001158 sock_put(sk);
1159 return ret;
1160}
1161
1162int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001163 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164{
1165 struct sock *sk;
1166 int err;
1167 long timeo;
1168
1169 skb = netlink_trim(skb, gfp_any());
1170
1171 timeo = sock_sndtimeo(ssk, nonblock);
1172retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001173 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 if (IS_ERR(sk)) {
1175 kfree_skb(skb);
1176 return PTR_ERR(sk);
1177 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001178 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001179 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001180
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001181 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001182 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001183 kfree_skb(skb);
1184 sock_put(sk);
1185 return err;
1186 }
1187
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001188 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 if (err == 1)
1190 goto retry;
1191 if (err)
1192 return err;
1193
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001194 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001196EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Patrick McHardy4277a082006-03-20 18:52:01 -08001198int netlink_has_listeners(struct sock *sk, unsigned int group)
1199{
1200 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001201 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001202
Denis V. Lunevaed81562007-10-10 21:14:32 -07001203 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001204
1205 rcu_read_lock();
1206 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1207
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001208 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001209 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001210
1211 rcu_read_unlock();
1212
Patrick McHardy4277a082006-03-20 18:52:01 -08001213 return res;
1214}
1215EXPORT_SYMBOL_GPL(netlink_has_listeners);
1216
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001217static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218{
1219 struct netlink_sock *nlk = nlk_sk(sk);
1220
1221 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001222 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001223 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001224 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001225 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 }
1227 return -1;
1228}
1229
1230struct netlink_broadcast_data {
1231 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001232 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001233 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 u32 group;
1235 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001236 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int congested;
1238 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001239 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001241 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1242 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243};
1244
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001245static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 struct netlink_broadcast_data *p)
1247{
1248 struct netlink_sock *nlk = nlk_sk(sk);
1249 int val;
1250
1251 if (p->exclude_sk == sk)
1252 goto out;
1253
Eric W. Biederman15e47302012-09-07 20:12:54 +00001254 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001255 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 goto out;
1257
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001258 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001259 goto out;
1260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 if (p->failure) {
1262 netlink_overrun(sk);
1263 goto out;
1264 }
1265
1266 sock_hold(sk);
1267 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001268 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 p->skb2 = skb_clone(p->skb, p->allocation);
1270 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001271 p->skb2 = skb_get(p->skb);
1272 /*
1273 * skb ownership may have been set when
1274 * delivered to a previous socket.
1275 */
1276 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 }
1279 if (p->skb2 == NULL) {
1280 netlink_overrun(sk);
1281 /* Clone failed. Notify ALL listeners. */
1282 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001283 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1284 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001285 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1286 kfree_skb(p->skb2);
1287 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001288 } else if (sk_filter(sk, p->skb2)) {
1289 kfree_skb(p->skb2);
1290 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1292 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001293 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1294 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 } else {
1296 p->congested |= val;
1297 p->delivered = 1;
1298 p->skb2 = NULL;
1299 }
1300 sock_put(sk);
1301
1302out:
1303 return 0;
1304}
1305
Eric W. Biederman15e47302012-09-07 20:12:54 +00001306int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001307 u32 group, gfp_t allocation,
1308 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1309 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001311 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 struct sock *sk;
1314
1315 skb = netlink_trim(skb, allocation);
1316
1317 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001318 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001319 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 info.group = group;
1321 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001322 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 info.congested = 0;
1324 info.delivered = 0;
1325 info.allocation = allocation;
1326 info.skb = skb;
1327 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001328 info.tx_filter = filter;
1329 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
1331 /* While we sleep in clone, do not allow to change socket list */
1332
1333 netlink_lock_table();
1334
Sasha Levinb67bfe02013-02-27 17:06:00 -08001335 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 do_one_broadcast(sk, &info);
1337
Neil Horman70d4bf62010-07-20 06:45:56 +00001338 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001339
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 netlink_unlock_table();
1341
Neil Horman70d4bf62010-07-20 06:45:56 +00001342 if (info.delivery_failure) {
1343 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001344 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001345 }
1346 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001347
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 if (info.delivered) {
1349 if (info.congested && (allocation & __GFP_WAIT))
1350 yield();
1351 return 0;
1352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 return -ESRCH;
1354}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001355EXPORT_SYMBOL(netlink_broadcast_filtered);
1356
Eric W. Biederman15e47302012-09-07 20:12:54 +00001357int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001358 u32 group, gfp_t allocation)
1359{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001360 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001361 NULL, NULL);
1362}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001363EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
1365struct netlink_set_err_data {
1366 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001367 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 u32 group;
1369 int code;
1370};
1371
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001372static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373{
1374 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001375 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
1377 if (sk == p->exclude_sk)
1378 goto out;
1379
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001380 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001381 goto out;
1382
Eric W. Biederman15e47302012-09-07 20:12:54 +00001383 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001384 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 goto out;
1386
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001387 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1388 ret = 1;
1389 goto out;
1390 }
1391
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 sk->sk_err = p->code;
1393 sk->sk_error_report(sk);
1394out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001395 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396}
1397
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001398/**
1399 * netlink_set_err - report error to broadcast listeners
1400 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001401 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001402 * @groups: the broadcast group that will notice the error
1403 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001404 *
1405 * This function returns the number of broadcast listeners that have set the
1406 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001407 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001408int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409{
1410 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001412 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001415 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001417 /* sk->sk_err wants a positive error value */
1418 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420 read_lock(&nl_table_lock);
1421
Sasha Levinb67bfe02013-02-27 17:06:00 -08001422 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001423 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001428EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Johannes Berg84659eb2007-07-18 15:47:05 -07001430/* must be called with netlink table grabbed */
1431static void netlink_update_socket_mc(struct netlink_sock *nlk,
1432 unsigned int group,
1433 int is_new)
1434{
1435 int old, new = !!is_new, subscriptions;
1436
1437 old = test_bit(group - 1, nlk->groups);
1438 subscriptions = nlk->subscriptions - old + new;
1439 if (new)
1440 __set_bit(group - 1, nlk->groups);
1441 else
1442 __clear_bit(group - 1, nlk->groups);
1443 netlink_update_subscriptions(&nlk->sk, subscriptions);
1444 netlink_update_listeners(&nlk->sk);
1445}
1446
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001447static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001448 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001449{
1450 struct sock *sk = sock->sk;
1451 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001452 unsigned int val = 0;
1453 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001454
1455 if (level != SOL_NETLINK)
1456 return -ENOPROTOOPT;
1457
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001458 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1459 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001460 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001461 return -EFAULT;
1462
1463 switch (optname) {
1464 case NETLINK_PKTINFO:
1465 if (val)
1466 nlk->flags |= NETLINK_RECV_PKTINFO;
1467 else
1468 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1469 err = 0;
1470 break;
1471 case NETLINK_ADD_MEMBERSHIP:
1472 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001473 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001474 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001475 err = netlink_realloc_groups(sk);
1476 if (err)
1477 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001478 if (!val || val - 1 >= nlk->ngroups)
1479 return -EINVAL;
1480 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001481 netlink_update_socket_mc(nlk, val,
1482 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001483 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001484
1485 if (nlk->netlink_bind)
1486 nlk->netlink_bind(val);
1487
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001488 err = 0;
1489 break;
1490 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001491 case NETLINK_BROADCAST_ERROR:
1492 if (val)
1493 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1494 else
1495 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1496 err = 0;
1497 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001498 case NETLINK_NO_ENOBUFS:
1499 if (val) {
1500 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001501 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001502 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001503 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001504 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001505 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001506 err = 0;
1507 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001508#ifdef CONFIG_NETLINK_MMAP
1509 case NETLINK_RX_RING:
1510 case NETLINK_TX_RING: {
1511 struct nl_mmap_req req;
1512
1513 /* Rings might consume more memory than queue limits, require
1514 * CAP_NET_ADMIN.
1515 */
1516 if (!capable(CAP_NET_ADMIN))
1517 return -EPERM;
1518 if (optlen < sizeof(req))
1519 return -EINVAL;
1520 if (copy_from_user(&req, optval, sizeof(req)))
1521 return -EFAULT;
1522 err = netlink_set_ring(sk, &req, false,
1523 optname == NETLINK_TX_RING);
1524 break;
1525 }
1526#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001527 default:
1528 err = -ENOPROTOOPT;
1529 }
1530 return err;
1531}
1532
1533static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001534 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001535{
1536 struct sock *sk = sock->sk;
1537 struct netlink_sock *nlk = nlk_sk(sk);
1538 int len, val, err;
1539
1540 if (level != SOL_NETLINK)
1541 return -ENOPROTOOPT;
1542
1543 if (get_user(len, optlen))
1544 return -EFAULT;
1545 if (len < 0)
1546 return -EINVAL;
1547
1548 switch (optname) {
1549 case NETLINK_PKTINFO:
1550 if (len < sizeof(int))
1551 return -EINVAL;
1552 len = sizeof(int);
1553 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001554 if (put_user(len, optlen) ||
1555 put_user(val, optval))
1556 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001557 err = 0;
1558 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001559 case NETLINK_BROADCAST_ERROR:
1560 if (len < sizeof(int))
1561 return -EINVAL;
1562 len = sizeof(int);
1563 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1564 if (put_user(len, optlen) ||
1565 put_user(val, optval))
1566 return -EFAULT;
1567 err = 0;
1568 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001569 case NETLINK_NO_ENOBUFS:
1570 if (len < sizeof(int))
1571 return -EINVAL;
1572 len = sizeof(int);
1573 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1574 if (put_user(len, optlen) ||
1575 put_user(val, optval))
1576 return -EFAULT;
1577 err = 0;
1578 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001579 default:
1580 err = -ENOPROTOOPT;
1581 }
1582 return err;
1583}
1584
1585static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1586{
1587 struct nl_pktinfo info;
1588
1589 info.group = NETLINK_CB(skb).dst_group;
1590 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1591}
1592
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1594 struct msghdr *msg, size_t len)
1595{
1596 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1597 struct sock *sk = sock->sk;
1598 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001599 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001600 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001601 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 struct sk_buff *skb;
1603 int err;
1604 struct scm_cookie scm;
1605
1606 if (msg->msg_flags&MSG_OOB)
1607 return -EOPNOTSUPP;
1608
Eric Dumazet16e57262011-09-19 05:52:27 +00001609 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001611
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001612 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (err < 0)
1614 return err;
1615
1616 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001617 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001619 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001620 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001621 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001622 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001623 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001624 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001627 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001628 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
Eric W. Biederman15e47302012-09-07 20:12:54 +00001631 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 err = netlink_autobind(sock);
1633 if (err)
1634 goto out;
1635 }
1636
1637 err = -EMSGSIZE;
1638 if (len > sk->sk_sndbuf - 32)
1639 goto out;
1640 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001641 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001642 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 goto out;
1644
Eric W. Biederman15e47302012-09-07 20:12:54 +00001645 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001646 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001647 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001650 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 kfree_skb(skb);
1652 goto out;
1653 }
1654
1655 err = security_netlink_send(sk, skb);
1656 if (err) {
1657 kfree_skb(skb);
1658 goto out;
1659 }
1660
Patrick McHardyd629b832005-08-14 19:27:50 -07001661 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001663 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001665 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666
1667out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001668 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return err;
1670}
1671
1672static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1673 struct msghdr *msg, size_t len,
1674 int flags)
1675{
1676 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1677 struct scm_cookie scm;
1678 struct sock *sk = sock->sk;
1679 struct netlink_sock *nlk = nlk_sk(sk);
1680 int noblock = flags&MSG_DONTWAIT;
1681 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001682 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001683 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 if (flags&MSG_OOB)
1686 return -EOPNOTSUPP;
1687
1688 copied = 0;
1689
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001690 skb = skb_recv_datagram(sk, flags, noblock, &err);
1691 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto out;
1693
Johannes Berg68d6ac62010-08-15 21:20:44 +00001694 data_skb = skb;
1695
Johannes Berg1dacc762009-07-01 11:26:02 +00001696#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1697 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001698 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001699 * If this skb has a frag_list, then here that means that we
1700 * will have to use the frag_list skb's data for compat tasks
1701 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001702 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001703 * If we need to send the compat skb, assign it to the
1704 * 'data_skb' variable so that it will be used below for data
1705 * copying. We keep 'skb' for everything else, including
1706 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001707 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001708 if (flags & MSG_CMSG_COMPAT)
1709 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001710 }
1711#endif
1712
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 msg->msg_namelen = 0;
1714
Johannes Berg68d6ac62010-08-15 21:20:44 +00001715 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 if (len < copied) {
1717 msg->msg_flags |= MSG_TRUNC;
1718 copied = len;
1719 }
1720
Johannes Berg68d6ac62010-08-15 21:20:44 +00001721 skb_reset_transport_header(data_skb);
1722 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001725 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 addr->nl_family = AF_NETLINK;
1727 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001728 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001729 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 msg->msg_namelen = sizeof(*addr);
1731 }
1732
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001733 if (nlk->flags & NETLINK_RECV_PKTINFO)
1734 netlink_cmsg_recv_pktinfo(msg, skb);
1735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (NULL == siocb->scm) {
1737 memset(&scm, 0, sizeof(scm));
1738 siocb->scm = &scm;
1739 }
1740 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001741 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001742 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 skb_free_datagram(sk, skb);
1745
Andrey Vaginb44d2112011-02-21 02:40:47 +00001746 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1747 ret = netlink_dump(sk);
1748 if (ret) {
1749 sk->sk_err = ret;
1750 sk->sk_error_report(sk);
1751 }
1752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755out:
1756 netlink_rcv_wake(sk);
1757 return err ? : copied;
1758}
1759
1760static void netlink_data_ready(struct sock *sk, int len)
1761{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001762 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}
1764
1765/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001766 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 * complete set of kernel non-blocking support for message
1768 * queueing.
1769 */
1770
1771struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001772__netlink_kernel_create(struct net *net, int unit, struct module *module,
1773 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 struct socket *sock;
1776 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001777 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001778 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001779 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1780 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001782 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001784 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 return NULL;
1786
1787 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1788 return NULL;
1789
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001790 /*
1791 * We have to just have a reference on the net from sk, but don't
1792 * get_net it. Besides, we cannot get and then put the net here.
1793 * So we create one inside init_net and the move it to net.
1794 */
1795
1796 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1797 goto out_sock_release_nosk;
1798
1799 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001800 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001801
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001802 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001803 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001804 else
1805 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001806
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001807 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001808 if (!listeners)
1809 goto out_sock_release;
1810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001812 if (cfg && cfg->input)
1813 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001815 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001816 goto out_sock_release;
1817
1818 nlk = nlk_sk(sk);
1819 nlk->flags |= NETLINK_KERNEL_SOCKET;
1820
1821 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001822 if (!nl_table[unit].registered) {
1823 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001824 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001825 nl_table[unit].cb_mutex = cb_mutex;
1826 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001827 if (cfg) {
1828 nl_table[unit].bind = cfg->bind;
1829 nl_table[unit].flags = cfg->flags;
1830 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001831 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07001832 } else {
1833 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001834 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001835 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07001836 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001837 return sk;
1838
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001839out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08001840 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08001841 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001842 return NULL;
1843
1844out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001845 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001846 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001848EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001849
1850void
1851netlink_kernel_release(struct sock *sk)
1852{
Denis V. Lunevedf02082008-02-29 11:18:32 -08001853 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08001854}
1855EXPORT_SYMBOL(netlink_kernel_release);
1856
Johannes Bergd136f1b2009-09-12 03:03:15 +00001857int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001858{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001859 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001860 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001861
1862 if (groups < 32)
1863 groups = 32;
1864
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001865 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001866 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
1867 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00001868 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001869 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001870 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
1871 rcu_assign_pointer(tbl->listeners, new);
1872
Lai Jiangshan37b6b932011-03-15 18:01:42 +08001873 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001874 }
1875 tbl->groups = groups;
1876
Johannes Bergd136f1b2009-09-12 03:03:15 +00001877 return 0;
1878}
1879
1880/**
1881 * netlink_change_ngroups - change number of multicast groups
1882 *
1883 * This changes the number of multicast groups that are available
1884 * on a certain netlink family. Note that it is not possible to
1885 * change the number of groups to below 32. Also note that it does
1886 * not implicitly call netlink_clear_multicast_users() when the
1887 * number of groups is reduced.
1888 *
1889 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
1890 * @groups: The new number of groups.
1891 */
1892int netlink_change_ngroups(struct sock *sk, unsigned int groups)
1893{
1894 int err;
1895
1896 netlink_table_grab();
1897 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001898 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00001899
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001900 return err;
1901}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001902
Johannes Bergb8273572009-09-24 15:44:05 -07001903void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1904{
1905 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07001906 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
1907
Sasha Levinb67bfe02013-02-27 17:06:00 -08001908 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07001909 netlink_update_socket_mc(nlk_sk(sk), group, 0);
1910}
1911
Johannes Berg84659eb2007-07-18 15:47:05 -07001912/**
1913 * netlink_clear_multicast_users - kick off multicast listeners
1914 *
1915 * This function removes all listeners from the given group.
1916 * @ksk: The kernel netlink socket, as returned by
1917 * netlink_kernel_create().
1918 * @group: The multicast group to clear.
1919 */
1920void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
1921{
Johannes Berg84659eb2007-07-18 15:47:05 -07001922 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07001923 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07001924 netlink_table_ungrab();
1925}
Johannes Berg84659eb2007-07-18 15:47:05 -07001926
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001927struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00001928__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001929{
1930 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00001931 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001932
1933 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
1934 nlh->nlmsg_type = type;
1935 nlh->nlmsg_len = size;
1936 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001937 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001938 nlh->nlmsg_seq = seq;
1939 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00001940 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05001941 return nlh;
1942}
1943EXPORT_SYMBOL(__nlmsg_put);
1944
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945/*
1946 * It looks a bit ugly.
1947 * It would be better to create kernel thread.
1948 */
1949
1950static int netlink_dump(struct sock *sk)
1951{
1952 struct netlink_sock *nlk = nlk_sk(sk);
1953 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001954 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001956 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00001957 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001959 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960
1961 cb = nlk->cb;
1962 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001963 err = -EINVAL;
1964 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001965 }
1966
Greg Rosec7ac8672011-06-10 01:27:09 +00001967 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
1968
1969 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
1970 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00001971 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00001972
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 len = cb->dump(skb, cb);
1974
1975 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001976 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001977
1978 if (sk_filter(sk, skb))
1979 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001980 else
1981 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 return 0;
1983 }
1984
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001985 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
1986 if (!nlh)
1987 goto errout_skb;
1988
Johannes Berg670dc282011-06-20 13:40:46 +02001989 nl_dump_check_consistent(cb, nlh);
1990
Thomas Grafbf8b79e2006-08-04 23:03:29 -07001991 memcpy(nlmsg_data(nlh), &len, sizeof(len));
1992
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001993 if (sk_filter(sk, skb))
1994 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001995 else
1996 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Thomas Grafa8f74b22005-11-10 02:25:52 +01001998 if (cb->done)
1999 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002001 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Gao feng6dc878a2012-10-04 20:15:48 +00002003 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00002004 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002006
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002007errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002008 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002009 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002010 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Gao feng6dc878a2012-10-04 20:15:48 +00002013int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2014 const struct nlmsghdr *nlh,
2015 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
2017 struct netlink_callback *cb;
2018 struct sock *sk;
2019 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002020 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002022 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (cb == NULL)
2024 return -ENOBUFS;
2025
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002026 cb->dump = control->dump;
2027 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002029 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002030 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002031 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 atomic_inc(&skb->users);
2033 cb->skb = skb;
2034
Eric W. Biederman15e47302012-09-07 20:12:54 +00002035 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (sk == NULL) {
2037 netlink_destroy_callback(cb);
2038 return -ECONNREFUSED;
2039 }
2040 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002041
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002042 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002043 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07002044 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002045 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00002047 ret = -EBUSY;
2048 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
Gao feng6dc878a2012-10-04 20:15:48 +00002050 /* add reference of module which cb->dump belongs to */
2051 if (!try_module_get(cb->module)) {
2052 mutex_unlock(nlk->cb_mutex);
2053 netlink_destroy_callback(cb);
2054 ret = -EPROTONOSUPPORT;
2055 goto out;
2056 }
2057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002059 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Andrey Vaginb44d2112011-02-21 02:40:47 +00002061 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002062out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002064
Andrey Vaginb44d2112011-02-21 02:40:47 +00002065 if (ret)
2066 return ret;
2067
Denis V. Lunev5c582982007-10-23 20:29:25 -07002068 /* We successfully started a dump, by returning -EINTR we
2069 * signal not to send ACK even if it was requested.
2070 */
2071 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072}
Gao feng6dc878a2012-10-04 20:15:48 +00002073EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074
2075void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2076{
2077 struct sk_buff *skb;
2078 struct nlmsghdr *rep;
2079 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002080 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
Thomas Graf339bf982006-11-10 14:10:15 -08002082 /* error messages get the original request appened */
2083 if (err)
2084 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Thomas Graf339bf982006-11-10 14:10:15 -08002086 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (!skb) {
2088 struct sock *sk;
2089
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002090 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002091 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002092 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 if (sk) {
2094 sk->sk_err = ENOBUFS;
2095 sk->sk_error_report(sk);
2096 sock_put(sk);
2097 }
2098 return;
2099 }
2100
Eric W. Biederman15e47302012-09-07 20:12:54 +00002101 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002102 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002103 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002105 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002106 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002108EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002110int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002111 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002112{
Thomas Graf82ace472005-11-10 02:25:53 +01002113 struct nlmsghdr *nlh;
2114 int err;
2115
2116 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002117 int msglen;
2118
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002119 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002120 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002121
Martin Murrayad8e4b72006-01-10 13:02:29 -08002122 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002123 return 0;
2124
Thomas Grafd35b6852007-03-22 23:28:46 -07002125 /* Only requests are handled by the kernel */
2126 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002127 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002128
Thomas Graf45e7ae72007-03-22 23:29:10 -07002129 /* Skip control messages */
2130 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002131 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002132
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002133 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002134 if (err == -EINTR)
2135 goto skip;
2136
2137ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002138 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002139 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002140
Denis V. Lunev5c582982007-10-23 20:29:25 -07002141skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002142 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002143 if (msglen > skb->len)
2144 msglen = skb->len;
2145 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002146 }
2147
2148 return 0;
2149}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002150EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002151
2152/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002153 * nlmsg_notify - send a notification netlink message
2154 * @sk: netlink socket to use
2155 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002156 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002157 * @group: destination multicast group or 0
2158 * @report: 1 to report back, 0 to disable
2159 * @flags: allocation flags
2160 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002161int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002162 unsigned int group, int report, gfp_t flags)
2163{
2164 int err = 0;
2165
2166 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002167 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002168
2169 if (report) {
2170 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002171 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002172 }
2173
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002174 /* errors reported via destination sk->sk_err, but propagate
2175 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002176 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002177 }
2178
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002179 if (report) {
2180 int err2;
2181
Eric W. Biederman15e47302012-09-07 20:12:54 +00002182 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002183 if (!err || err == -ESRCH)
2184 err = err2;
2185 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002186
2187 return err;
2188}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002189EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002190
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191#ifdef CONFIG_PROC_FS
2192struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002193 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int link;
2195 int hash_idx;
2196};
2197
2198static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2199{
2200 struct nl_seq_iter *iter = seq->private;
2201 int i, j;
2202 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 loff_t off = 0;
2204
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002205 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002206 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
2208 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002209 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002210 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002211 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (off == pos) {
2213 iter->link = i;
2214 iter->hash_idx = j;
2215 return s;
2216 }
2217 ++off;
2218 }
2219 }
2220 }
2221 return NULL;
2222}
2223
2224static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002225 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 read_lock(&nl_table_lock);
2228 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2229}
2230
2231static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2232{
2233 struct sock *s;
2234 struct nl_seq_iter *iter;
2235 int i, j;
2236
2237 ++*pos;
2238
2239 if (v == SEQ_START_TOKEN)
2240 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002241
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002242 iter = seq->private;
2243 s = v;
2244 do {
2245 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002246 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (s)
2248 return s;
2249
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 i = iter->link;
2251 j = iter->hash_idx + 1;
2252
2253 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002254 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 for (; j <= hash->mask; j++) {
2257 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002258 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002259 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (s) {
2261 iter->link = i;
2262 iter->hash_idx = j;
2263 return s;
2264 }
2265 }
2266
2267 j = 0;
2268 } while (++i < MAX_LINKS);
2269
2270 return NULL;
2271}
2272
2273static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002274 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275{
2276 read_unlock(&nl_table_lock);
2277}
2278
2279
2280static int netlink_seq_show(struct seq_file *seq, void *v)
2281{
Eric Dumazet658cb352012-04-22 21:30:21 +00002282 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 seq_puts(seq,
2284 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002285 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002286 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 struct sock *s = v;
2288 struct netlink_sock *nlk = nlk_sk(s);
2289
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002290 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 s,
2292 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002293 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002294 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002295 sk_rmem_alloc_get(s),
2296 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002298 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002299 atomic_read(&s->sk_drops),
2300 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 );
2302
2303 }
2304 return 0;
2305}
2306
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002307static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 .start = netlink_seq_start,
2309 .next = netlink_seq_next,
2310 .stop = netlink_seq_stop,
2311 .show = netlink_seq_show,
2312};
2313
2314
2315static int netlink_seq_open(struct inode *inode, struct file *file)
2316{
Denis V. Luneve372c412007-11-19 22:31:54 -08002317 return seq_open_net(inode, file, &netlink_seq_ops,
2318 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002319}
2320
Arjan van de Venda7071d2007-02-12 00:55:36 -08002321static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 .owner = THIS_MODULE,
2323 .open = netlink_seq_open,
2324 .read = seq_read,
2325 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002326 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327};
2328
2329#endif
2330
2331int netlink_register_notifier(struct notifier_block *nb)
2332{
Alan Sterne041c682006-03-27 01:16:30 -08002333 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002335EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337int netlink_unregister_notifier(struct notifier_block *nb)
2338{
Alan Sterne041c682006-03-27 01:16:30 -08002339 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002341EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002342
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002343static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 .family = PF_NETLINK,
2345 .owner = THIS_MODULE,
2346 .release = netlink_release,
2347 .bind = netlink_bind,
2348 .connect = netlink_connect,
2349 .socketpair = sock_no_socketpair,
2350 .accept = sock_no_accept,
2351 .getname = netlink_getname,
2352 .poll = datagram_poll,
2353 .ioctl = sock_no_ioctl,
2354 .listen = sock_no_listen,
2355 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002356 .setsockopt = netlink_setsockopt,
2357 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 .sendmsg = netlink_sendmsg,
2359 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002360 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 .sendpage = sock_no_sendpage,
2362};
2363
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002364static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 .family = PF_NETLINK,
2366 .create = netlink_create,
2367 .owner = THIS_MODULE, /* for consistency 8) */
2368};
2369
Pavel Emelyanov46650792007-10-08 20:38:39 -07002370static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002371{
2372#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002373 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002374 return -ENOMEM;
2375#endif
2376 return 0;
2377}
2378
Pavel Emelyanov46650792007-10-08 20:38:39 -07002379static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002380{
2381#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002382 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002383#endif
2384}
2385
David S. Millerb963ea82010-08-30 19:08:01 -07002386static void __init netlink_add_usersock_entry(void)
2387{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002388 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002389 int groups = 32;
2390
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002391 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002392 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002393 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002394
2395 netlink_table_grab();
2396
2397 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002398 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002399 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2400 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002401 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002402
2403 netlink_table_ungrab();
2404}
2405
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002406static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002407 .init = netlink_net_init,
2408 .exit = netlink_net_exit,
2409};
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411static int __init netlink_proto_init(void)
2412{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002414 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 unsigned int order;
2416 int err = proto_register(&netlink_proto, 0);
2417
2418 if (err != 0)
2419 goto out;
2420
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002421 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002423 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002424 if (!nl_table)
2425 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426
Jan Beulich44813742009-09-21 17:03:05 -07002427 if (totalram_pages >= (128 * 1024))
2428 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 else
Jan Beulich44813742009-09-21 17:03:05 -07002430 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002432 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2433 limit = (1UL << order) / sizeof(struct hlist_head);
2434 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435
2436 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002437 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438
Eric W. Biederman15e47302012-09-07 20:12:54 +00002439 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 if (!hash->table) {
2441 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002442 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 1 * sizeof(*hash->table));
2444 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002445 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447 hash->max_shift = order;
2448 hash->shift = 0;
2449 hash->mask = 0;
2450 hash->rehash_time = jiffies;
2451 }
2452
David S. Millerb963ea82010-08-30 19:08:01 -07002453 netlink_add_usersock_entry();
2454
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002456 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002457 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 rtnetlink_init();
2459out:
2460 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002461panic:
2462 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465core_initcall(netlink_proto_init);