blob: 6560635fd25cd4bf25cf7bdbcce50416216c08dd [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>
Patrick McHardy9652e932013-04-17 06:47:02 +000059#include <asm/cacheflush.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010060
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020061#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010064#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Andrey Vagin0f29c762013-03-21 20:33:47 +040066#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Dumazet5c398dc2010-10-24 04:27:10 +000068struct listeners {
69 struct rcu_head rcu;
70 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000071};
72
Patrick McHardycd967e02013-04-17 06:46:56 +000073/* state bits */
74#define NETLINK_CONGESTED 0x0
75
76/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070077#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070078#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000079#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070080#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070081
David S. Miller035c4c12011-12-23 17:33:03 -050082static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070083{
84 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
85}
86
Andrey Vagin0f29c762013-03-21 20:33:47 +040087struct netlink_table *nl_table;
88EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
91
92static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000093static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andrey Vagin0f29c762013-03-21 20:33:47 +040095DEFINE_RWLOCK(nl_table_lock);
96EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static atomic_t nl_table_users = ATOMIC_INIT(0);
98
Eric Dumazet6d772ac2012-10-18 03:21:55 +000099#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
100
Alan Sterne041c682006-03-27 01:16:30 -0800101static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000103static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700104{
105 return group ? 1 << (group - 1) : 0;
106}
107
Eric W. Biederman15e47302012-09-07 20:12:54 +0000108static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000110 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000113#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000114static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
115{
116 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
117}
118
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000119static __pure struct page *pgvec_to_page(const void *addr)
120{
121 if (is_vmalloc_addr(addr))
122 return vmalloc_to_page(addr);
123 else
124 return virt_to_page(addr);
125}
126
127static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
128{
129 unsigned int i;
130
131 for (i = 0; i < len; i++) {
132 if (pg_vec[i] != NULL) {
133 if (is_vmalloc_addr(pg_vec[i]))
134 vfree(pg_vec[i]);
135 else
136 free_pages((unsigned long)pg_vec[i], order);
137 }
138 }
139 kfree(pg_vec);
140}
141
142static void *alloc_one_pg_vec_page(unsigned long order)
143{
144 void *buffer;
145 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
146 __GFP_NOWARN | __GFP_NORETRY;
147
148 buffer = (void *)__get_free_pages(gfp_flags, order);
149 if (buffer != NULL)
150 return buffer;
151
152 buffer = vzalloc((1 << order) * PAGE_SIZE);
153 if (buffer != NULL)
154 return buffer;
155
156 gfp_flags &= ~__GFP_NORETRY;
157 return (void *)__get_free_pages(gfp_flags, order);
158}
159
160static void **alloc_pg_vec(struct netlink_sock *nlk,
161 struct nl_mmap_req *req, unsigned int order)
162{
163 unsigned int block_nr = req->nm_block_nr;
164 unsigned int i;
165 void **pg_vec, *ptr;
166
167 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
168 if (pg_vec == NULL)
169 return NULL;
170
171 for (i = 0; i < block_nr; i++) {
172 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
173 if (pg_vec[i] == NULL)
174 goto err1;
175 }
176
177 return pg_vec;
178err1:
179 free_pg_vec(pg_vec, order, block_nr);
180 return NULL;
181}
182
183static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
184 bool closing, bool tx_ring)
185{
186 struct netlink_sock *nlk = nlk_sk(sk);
187 struct netlink_ring *ring;
188 struct sk_buff_head *queue;
189 void **pg_vec = NULL;
190 unsigned int order = 0;
191 int err;
192
193 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
194 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
195
196 if (!closing) {
197 if (atomic_read(&nlk->mapped))
198 return -EBUSY;
199 if (atomic_read(&ring->pending))
200 return -EBUSY;
201 }
202
203 if (req->nm_block_nr) {
204 if (ring->pg_vec != NULL)
205 return -EBUSY;
206
207 if ((int)req->nm_block_size <= 0)
208 return -EINVAL;
209 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
210 return -EINVAL;
211 if (req->nm_frame_size < NL_MMAP_HDRLEN)
212 return -EINVAL;
213 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
214 return -EINVAL;
215
216 ring->frames_per_block = req->nm_block_size /
217 req->nm_frame_size;
218 if (ring->frames_per_block == 0)
219 return -EINVAL;
220 if (ring->frames_per_block * req->nm_block_nr !=
221 req->nm_frame_nr)
222 return -EINVAL;
223
224 order = get_order(req->nm_block_size);
225 pg_vec = alloc_pg_vec(nlk, req, order);
226 if (pg_vec == NULL)
227 return -ENOMEM;
228 } else {
229 if (req->nm_frame_nr)
230 return -EINVAL;
231 }
232
233 err = -EBUSY;
234 mutex_lock(&nlk->pg_vec_lock);
235 if (closing || atomic_read(&nlk->mapped) == 0) {
236 err = 0;
237 spin_lock_bh(&queue->lock);
238
239 ring->frame_max = req->nm_frame_nr - 1;
240 ring->head = 0;
241 ring->frame_size = req->nm_frame_size;
242 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
243
244 swap(ring->pg_vec_len, req->nm_block_nr);
245 swap(ring->pg_vec_order, order);
246 swap(ring->pg_vec, pg_vec);
247
248 __skb_queue_purge(queue);
249 spin_unlock_bh(&queue->lock);
250
251 WARN_ON(atomic_read(&nlk->mapped));
252 }
253 mutex_unlock(&nlk->pg_vec_lock);
254
255 if (pg_vec)
256 free_pg_vec(pg_vec, order, req->nm_block_nr);
257 return err;
258}
259
260static void netlink_mm_open(struct vm_area_struct *vma)
261{
262 struct file *file = vma->vm_file;
263 struct socket *sock = file->private_data;
264 struct sock *sk = sock->sk;
265
266 if (sk)
267 atomic_inc(&nlk_sk(sk)->mapped);
268}
269
270static void netlink_mm_close(struct vm_area_struct *vma)
271{
272 struct file *file = vma->vm_file;
273 struct socket *sock = file->private_data;
274 struct sock *sk = sock->sk;
275
276 if (sk)
277 atomic_dec(&nlk_sk(sk)->mapped);
278}
279
280static const struct vm_operations_struct netlink_mmap_ops = {
281 .open = netlink_mm_open,
282 .close = netlink_mm_close,
283};
284
285static int netlink_mmap(struct file *file, struct socket *sock,
286 struct vm_area_struct *vma)
287{
288 struct sock *sk = sock->sk;
289 struct netlink_sock *nlk = nlk_sk(sk);
290 struct netlink_ring *ring;
291 unsigned long start, size, expected;
292 unsigned int i;
293 int err = -EINVAL;
294
295 if (vma->vm_pgoff)
296 return -EINVAL;
297
298 mutex_lock(&nlk->pg_vec_lock);
299
300 expected = 0;
301 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
302 if (ring->pg_vec == NULL)
303 continue;
304 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
305 }
306
307 if (expected == 0)
308 goto out;
309
310 size = vma->vm_end - vma->vm_start;
311 if (size != expected)
312 goto out;
313
314 start = vma->vm_start;
315 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
316 if (ring->pg_vec == NULL)
317 continue;
318
319 for (i = 0; i < ring->pg_vec_len; i++) {
320 struct page *page;
321 void *kaddr = ring->pg_vec[i];
322 unsigned int pg_num;
323
324 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
325 page = pgvec_to_page(kaddr);
326 err = vm_insert_page(vma, start, page);
327 if (err < 0)
328 goto out;
329 start += PAGE_SIZE;
330 kaddr += PAGE_SIZE;
331 }
332 }
333 }
334
335 atomic_inc(&nlk->mapped);
336 vma->vm_ops = &netlink_mmap_ops;
337 err = 0;
338out:
339 mutex_unlock(&nlk->pg_vec_lock);
340 return 0;
341}
Patrick McHardy9652e932013-04-17 06:47:02 +0000342
343static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
344{
345#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
346 struct page *p_start, *p_end;
347
348 /* First page is flushed through netlink_{get,set}_status */
349 p_start = pgvec_to_page(hdr + PAGE_SIZE);
350 p_end = pgvec_to_page((void *)hdr + NL_MMAP_MSG_HDRLEN + hdr->nm_len - 1);
351 while (p_start <= p_end) {
352 flush_dcache_page(p_start);
353 p_start++;
354 }
355#endif
356}
357
358static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
359{
360 smp_rmb();
361 flush_dcache_page(pgvec_to_page(hdr));
362 return hdr->nm_status;
363}
364
365static void netlink_set_status(struct nl_mmap_hdr *hdr,
366 enum nl_mmap_status status)
367{
368 hdr->nm_status = status;
369 flush_dcache_page(pgvec_to_page(hdr));
370 smp_wmb();
371}
372
373static struct nl_mmap_hdr *
374__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
375{
376 unsigned int pg_vec_pos, frame_off;
377
378 pg_vec_pos = pos / ring->frames_per_block;
379 frame_off = pos % ring->frames_per_block;
380
381 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
382}
383
384static struct nl_mmap_hdr *
385netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
386 enum nl_mmap_status status)
387{
388 struct nl_mmap_hdr *hdr;
389
390 hdr = __netlink_lookup_frame(ring, pos);
391 if (netlink_get_status(hdr) != status)
392 return NULL;
393
394 return hdr;
395}
396
397static struct nl_mmap_hdr *
398netlink_current_frame(const struct netlink_ring *ring,
399 enum nl_mmap_status status)
400{
401 return netlink_lookup_frame(ring, ring->head, status);
402}
403
404static struct nl_mmap_hdr *
405netlink_previous_frame(const struct netlink_ring *ring,
406 enum nl_mmap_status status)
407{
408 unsigned int prev;
409
410 prev = ring->head ? ring->head - 1 : ring->frame_max;
411 return netlink_lookup_frame(ring, prev, status);
412}
413
414static void netlink_increment_head(struct netlink_ring *ring)
415{
416 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
417}
418
419static void netlink_forward_ring(struct netlink_ring *ring)
420{
421 unsigned int head = ring->head, pos = head;
422 const struct nl_mmap_hdr *hdr;
423
424 do {
425 hdr = __netlink_lookup_frame(ring, pos);
426 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
427 break;
428 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
429 break;
430 netlink_increment_head(ring);
431 } while (ring->head != head);
432}
433
434static unsigned int netlink_poll(struct file *file, struct socket *sock,
435 poll_table *wait)
436{
437 struct sock *sk = sock->sk;
438 struct netlink_sock *nlk = nlk_sk(sk);
439 unsigned int mask;
440
441 mask = datagram_poll(file, sock, wait);
442
443 spin_lock_bh(&sk->sk_receive_queue.lock);
444 if (nlk->rx_ring.pg_vec) {
445 netlink_forward_ring(&nlk->rx_ring);
446 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
447 mask |= POLLIN | POLLRDNORM;
448 }
449 spin_unlock_bh(&sk->sk_receive_queue.lock);
450
451 spin_lock_bh(&sk->sk_write_queue.lock);
452 if (nlk->tx_ring.pg_vec) {
453 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
454 mask |= POLLOUT | POLLWRNORM;
455 }
456 spin_unlock_bh(&sk->sk_write_queue.lock);
457
458 return mask;
459}
460
461static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
462{
463 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
464}
465
466static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
467 struct netlink_ring *ring,
468 struct nl_mmap_hdr *hdr)
469{
470 unsigned int size;
471 void *data;
472
473 size = ring->frame_size - NL_MMAP_HDRLEN;
474 data = (void *)hdr + NL_MMAP_HDRLEN;
475
476 skb->head = data;
477 skb->data = data;
478 skb_reset_tail_pointer(skb);
479 skb->end = skb->tail + size;
480 skb->len = 0;
481
482 skb->destructor = netlink_skb_destructor;
483 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
484 NETLINK_CB(skb).sk = sk;
485}
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000486#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000487#define netlink_skb_is_mmaped(skb) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000488#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000489#define netlink_poll datagram_poll
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000490#endif /* CONFIG_NETLINK_MMAP */
491
Eric Dumazet658cb352012-04-22 21:30:21 +0000492static void netlink_destroy_callback(struct netlink_callback *cb)
493{
494 kfree_skb(cb->skb);
495 kfree(cb);
496}
497
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000498static void netlink_consume_callback(struct netlink_callback *cb)
499{
500 consume_skb(cb->skb);
501 kfree(cb);
502}
503
Patrick McHardycf0a0182013-04-17 06:47:00 +0000504static void netlink_skb_destructor(struct sk_buff *skb)
505{
Patrick McHardy9652e932013-04-17 06:47:02 +0000506#ifdef CONFIG_NETLINK_MMAP
507 struct nl_mmap_hdr *hdr;
508 struct netlink_ring *ring;
509 struct sock *sk;
510
511 /* If a packet from the kernel to userspace was freed because of an
512 * error without being delivered to userspace, the kernel must reset
513 * the status. In the direction userspace to kernel, the status is
514 * always reset here after the packet was processed and freed.
515 */
516 if (netlink_skb_is_mmaped(skb)) {
517 hdr = netlink_mmap_hdr(skb);
518 sk = NETLINK_CB(skb).sk;
519
520 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
521 hdr->nm_len = 0;
522 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
523 }
524 ring = &nlk_sk(sk)->rx_ring;
525
526 WARN_ON(atomic_read(&ring->pending) == 0);
527 atomic_dec(&ring->pending);
528 sock_put(sk);
529
530 skb->data = NULL;
531 }
532#endif
533 if (skb->sk != NULL)
534 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000535}
536
537static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
538{
539 WARN_ON(skb->sk != NULL);
540 skb->sk = sk;
541 skb->destructor = netlink_skb_destructor;
542 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
543 sk_mem_charge(sk, skb->truesize);
544}
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546static void netlink_sock_destruct(struct sock *sk)
547{
Herbert Xu3f660d62007-05-03 03:17:14 -0700548 struct netlink_sock *nlk = nlk_sk(sk);
549
Herbert Xu3f660d62007-05-03 03:17:14 -0700550 if (nlk->cb) {
551 if (nlk->cb->done)
552 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000553
554 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700555 netlink_destroy_callback(nlk->cb);
556 }
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000559#ifdef CONFIG_NETLINK_MMAP
560 if (1) {
561 struct nl_mmap_req req;
562
563 memset(&req, 0, sizeof(req));
564 if (nlk->rx_ring.pg_vec)
565 netlink_set_ring(sk, &req, true, false);
566 memset(&req, 0, sizeof(req));
567 if (nlk->tx_ring.pg_vec)
568 netlink_set_ring(sk, &req, true, true);
569 }
570#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800573 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 return;
575 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700576
577 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
578 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
579 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580}
581
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800582/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
583 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
585 * this, _but_ remember, it adds useless work on UP machines.
586 */
587
Johannes Bergd136f1b2009-09-12 03:03:15 +0000588void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800589 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000591 might_sleep();
592
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700593 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 if (atomic_read(&nl_table_users)) {
596 DECLARE_WAITQUEUE(wait, current);
597
598 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800599 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 set_current_state(TASK_UNINTERRUPTIBLE);
601 if (atomic_read(&nl_table_users) == 0)
602 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700603 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700605 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 }
607
608 __set_current_state(TASK_RUNNING);
609 remove_wait_queue(&nl_table_wait, &wait);
610 }
611}
612
Johannes Bergd136f1b2009-09-12 03:03:15 +0000613void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800614 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700616 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 wake_up(&nl_table_wait);
618}
619
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800620static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621netlink_lock_table(void)
622{
623 /* read_lock() synchronizes us to netlink_table_grab */
624
625 read_lock(&nl_table_lock);
626 atomic_inc(&nl_table_users);
627 read_unlock(&nl_table_lock);
628}
629
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800630static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631netlink_unlock_table(void)
632{
633 if (atomic_dec_and_test(&nl_table_users))
634 wake_up(&nl_table_wait);
635}
636
Eric W. Biederman15e47302012-09-07 20:12:54 +0000637static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000639 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 struct hlist_head *head;
641 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000644 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800645 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000646 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 sock_hold(sk);
648 goto found;
649 }
650 }
651 sk = NULL;
652found:
653 read_unlock(&nl_table_lock);
654 return sk;
655}
656
Eric W. Biederman15e47302012-09-07 20:12:54 +0000657static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800660 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 else
662 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800663 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
664 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665}
666
Eric W. Biederman15e47302012-09-07 20:12:54 +0000667static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
669 if (size <= PAGE_SIZE)
670 kfree(table);
671 else
672 free_pages((unsigned long)table, get_order(size));
673}
674
Eric W. Biederman15e47302012-09-07 20:12:54 +0000675static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
677 unsigned int omask, mask, shift;
678 size_t osize, size;
679 struct hlist_head *otable, *table;
680 int i;
681
682 omask = mask = hash->mask;
683 osize = size = (mask + 1) * sizeof(*table);
684 shift = hash->shift;
685
686 if (grow) {
687 if (++shift > hash->max_shift)
688 return 0;
689 mask = mask * 2 + 1;
690 size *= 2;
691 }
692
Eric W. Biederman15e47302012-09-07 20:12:54 +0000693 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (!table)
695 return 0;
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 otable = hash->table;
698 hash->table = table;
699 hash->mask = mask;
700 hash->shift = shift;
701 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
702
703 for (i = 0; i <= omask; i++) {
704 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800705 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Sasha Levinb67bfe02013-02-27 17:06:00 -0800707 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000708 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
Eric W. Biederman15e47302012-09-07 20:12:54 +0000711 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 hash->rehash_time = jiffies + 10 * 60 * HZ;
713 return 1;
714}
715
Eric W. Biederman15e47302012-09-07 20:12:54 +0000716static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 int avg = hash->entries >> hash->shift;
719
Eric W. Biederman15e47302012-09-07 20:12:54 +0000720 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 return 1;
722
723 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000724 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return 1;
726 }
727
728 return 0;
729}
730
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800731static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Patrick McHardy4277a082006-03-20 18:52:01 -0800733static void
734netlink_update_listeners(struct sock *sk)
735{
736 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800737 unsigned long mask;
738 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000739 struct listeners *listeners;
740
741 listeners = nl_deref_protected(tbl->listeners);
742 if (!listeners)
743 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800744
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700745 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800746 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800747 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700748 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
749 mask |= nlk_sk(sk)->groups[i];
750 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000751 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800752 }
753 /* this function is only called with the netlink table "grabbed", which
754 * makes sure updates are visible before bind or setsockopt return. */
755}
756
Eric W. Biederman15e47302012-09-07 20:12:54 +0000757static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000759 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct hlist_head *head;
761 int err = -EADDRINUSE;
762 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 int len;
764
765 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000766 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800768 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000769 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 break;
771 len++;
772 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800773 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 goto err;
775
776 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000777 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 goto err;
779
780 err = -ENOMEM;
781 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
782 goto err;
783
Eric W. Biederman15e47302012-09-07 20:12:54 +0000784 if (len && nl_portid_hash_dilute(hash, len))
785 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000787 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 sk_add_node(sk, head);
789 err = 0;
790
791err:
792 netlink_table_ungrab();
793 return err;
794}
795
796static void netlink_remove(struct sock *sk)
797{
798 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700799 if (sk_del_node_init(sk))
800 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700801 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 __sk_del_bind_node(sk);
803 netlink_table_ungrab();
804}
805
806static struct proto netlink_proto = {
807 .name = "NETLINK",
808 .owner = THIS_MODULE,
809 .obj_size = sizeof(struct netlink_sock),
810};
811
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700812static int __netlink_create(struct net *net, struct socket *sock,
813 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
815 struct sock *sk;
816 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700817
818 sock->ops = &netlink_ops;
819
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700820 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700821 if (!sk)
822 return -ENOMEM;
823
824 sock_init_data(sock, sk);
825
826 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000827 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700828 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000829 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700830 nlk->cb_mutex = &nlk->cb_def_mutex;
831 mutex_init(nlk->cb_mutex);
832 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700833 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000834#ifdef CONFIG_NETLINK_MMAP
835 mutex_init(&nlk->pg_vec_lock);
836#endif
Patrick McHardyab33a172005-08-14 19:31:36 -0700837
838 sk->sk_destruct = netlink_sock_destruct;
839 sk->sk_protocol = protocol;
840 return 0;
841}
842
Eric Paris3f378b62009-11-05 22:18:14 -0800843static int netlink_create(struct net *net, struct socket *sock, int protocol,
844 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700845{
846 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700847 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700848 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000849 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700850 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 sock->state = SS_UNCONNECTED;
853
854 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
855 return -ESOCKTNOSUPPORT;
856
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800857 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 return -EPROTONOSUPPORT;
859
Patrick McHardy77247bb2005-08-14 19:27:13 -0700860 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700861#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700862 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700863 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700864 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700865 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700866 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700867#endif
868 if (nl_table[protocol].registered &&
869 try_module_get(nl_table[protocol].module))
870 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000871 else
872 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700873 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000874 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700875 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700876
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000877 if (err < 0)
878 goto out;
879
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800880 err = __netlink_create(net, sock, cb_mutex, protocol);
881 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700882 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
David S. Miller6f756a82008-11-23 17:34:03 -0800884 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800885 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800886 local_bh_enable();
887
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700888 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700889 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000890 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -0700891out:
892 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Patrick McHardyab33a172005-08-14 19:31:36 -0700894out_module:
895 module_put(module);
896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897}
898
899static int netlink_release(struct socket *sock)
900{
901 struct sock *sk = sock->sk;
902 struct netlink_sock *nlk;
903
904 if (!sk)
905 return 0;
906
907 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -0700908 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 nlk = nlk_sk(sk);
910
Herbert Xu3f660d62007-05-03 03:17:14 -0700911 /*
912 * OK. Socket is unlinked, any packets that arrive now
913 * will be purged.
914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 sock->sk = NULL;
917 wake_up_interruptible_all(&nlk->wait);
918
919 skb_queue_purge(&sk->sk_write_queue);
920
Eric W. Biederman15e47302012-09-07 20:12:54 +0000921 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900923 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000925 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 };
Alan Sterne041c682006-03-27 01:16:30 -0800927 atomic_notifier_call_chain(&netlink_chain,
928 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +0900929 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700930
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -0800931 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700932
Patrick McHardy4277a082006-03-20 18:52:01 -0800933 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -0700934 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800935 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
936 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000937 struct listeners *old;
938
939 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
940 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
941 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800942 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +0000943 nl_table[sk->sk_protocol].bind = NULL;
944 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -0800945 nl_table[sk->sk_protocol].registered = 0;
946 }
Eric Dumazet658cb352012-04-22 21:30:21 +0000947 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800948 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000949 }
Patrick McHardy4277a082006-03-20 18:52:01 -0800950 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -0700951
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700952 kfree(nlk->groups);
953 nlk->groups = NULL;
954
Eric Dumazet37558102008-11-24 14:05:22 -0800955 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800956 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -0800957 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 sock_put(sk);
959 return 0;
960}
961
962static int netlink_autobind(struct socket *sock)
963{
964 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900965 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000966 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 struct hlist_head *head;
968 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000969 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 int err;
971 static s32 rover = -4097;
972
973retry:
974 cond_resched();
975 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000976 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800977 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +0900978 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200979 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000980 if (nlk_sk(osk)->portid == portid) {
981 /* Bind collision, search negative portid values. */
982 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (rover > -4097)
984 rover = -4097;
985 netlink_table_ungrab();
986 goto retry;
987 }
988 }
989 netlink_table_ungrab();
990
Eric W. Biederman15e47302012-09-07 20:12:54 +0000991 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 if (err == -EADDRINUSE)
993 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -0700994
995 /* If 2 threads race to autobind, that is fine. */
996 if (err == -EBUSY)
997 err = 0;
998
999 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000}
1001
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001002static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001003{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001004 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001005 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001006}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001008static void
1009netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1010{
1011 struct netlink_sock *nlk = nlk_sk(sk);
1012
1013 if (nlk->subscriptions && !subscriptions)
1014 __sk_del_bind_node(sk);
1015 else if (!nlk->subscriptions && subscriptions)
1016 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1017 nlk->subscriptions = subscriptions;
1018}
1019
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001020static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001021{
1022 struct netlink_sock *nlk = nlk_sk(sk);
1023 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001024 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001025 int err = 0;
1026
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001027 netlink_table_grab();
1028
Patrick McHardy513c2502005-09-06 15:43:59 -07001029 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001030 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001031 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001032 goto out_unlock;
1033 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001034
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001035 if (nlk->ngroups >= groups)
1036 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001037
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001038 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1039 if (new_groups == NULL) {
1040 err = -ENOMEM;
1041 goto out_unlock;
1042 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001043 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001044 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1045
1046 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001047 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001048 out_unlock:
1049 netlink_table_ungrab();
1050 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001051}
1052
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001053static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1054 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
1056 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001057 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 struct netlink_sock *nlk = nlk_sk(sk);
1059 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1060 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001061
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001062 if (addr_len < sizeof(struct sockaddr_nl))
1063 return -EINVAL;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 if (nladdr->nl_family != AF_NETLINK)
1066 return -EINVAL;
1067
1068 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -07001069 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001070 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001071 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001072 err = netlink_realloc_groups(sk);
1073 if (err)
1074 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001075 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Eric W. Biederman15e47302012-09-07 20:12:54 +00001077 if (nlk->portid) {
1078 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return -EINVAL;
1080 } else {
1081 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001082 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 netlink_autobind(sock);
1084 if (err)
1085 return err;
1086 }
1087
Patrick McHardy513c2502005-09-06 15:43:59 -07001088 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 return 0;
1090
1091 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001092 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001093 hweight32(nladdr->nl_groups) -
1094 hweight32(nlk->groups[0]));
1095 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001096 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 netlink_table_ungrab();
1098
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001099 if (nlk->netlink_bind && nlk->groups[0]) {
1100 int i;
1101
1102 for (i=0; i<nlk->ngroups; i++) {
1103 if (test_bit(i, nlk->groups))
1104 nlk->netlink_bind(i);
1105 }
1106 }
1107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 return 0;
1109}
1110
1111static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1112 int alen, int flags)
1113{
1114 int err = 0;
1115 struct sock *sk = sock->sk;
1116 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001117 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118
Changli Gao6503d962010-03-31 22:58:26 +00001119 if (alen < sizeof(addr->sa_family))
1120 return -EINVAL;
1121
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (addr->sa_family == AF_UNSPEC) {
1123 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001124 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001125 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 return 0;
1127 }
1128 if (addr->sa_family != AF_NETLINK)
1129 return -EINVAL;
1130
1131 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001132 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return -EPERM;
1134
Eric W. Biederman15e47302012-09-07 20:12:54 +00001135 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 err = netlink_autobind(sock);
1137
1138 if (err == 0) {
1139 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001141 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
1144 return err;
1145}
1146
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001147static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1148 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149{
1150 struct sock *sk = sock->sk;
1151 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001152 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001153
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 nladdr->nl_family = AF_NETLINK;
1155 nladdr->nl_pad = 0;
1156 *addr_len = sizeof(*nladdr);
1157
1158 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001159 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001160 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001162 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001163 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165 return 0;
1166}
1167
1168static void netlink_overrun(struct sock *sk)
1169{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001170 struct netlink_sock *nlk = nlk_sk(sk);
1171
1172 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
Patrick McHardycd967e02013-04-17 06:46:56 +00001173 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001174 sk->sk_err = ENOBUFS;
1175 sk->sk_error_report(sk);
1176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001178 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Eric W. Biederman15e47302012-09-07 20:12:54 +00001181static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 struct sock *sock;
1184 struct netlink_sock *nlk;
1185
Eric W. Biederman15e47302012-09-07 20:12:54 +00001186 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 if (!sock)
1188 return ERR_PTR(-ECONNREFUSED);
1189
1190 /* Don't bother queuing skb if kernel socket has no input function */
1191 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001192 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001193 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 sock_put(sock);
1195 return ERR_PTR(-ECONNREFUSED);
1196 }
1197 return sock;
1198}
1199
1200struct sock *netlink_getsockbyfilp(struct file *filp)
1201{
Al Viro496ad9a2013-01-23 17:07:38 -05001202 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 struct sock *sock;
1204
1205 if (!S_ISSOCK(inode->i_mode))
1206 return ERR_PTR(-ENOTSOCK);
1207
1208 sock = SOCKET_I(inode)->sk;
1209 if (sock->sk_family != AF_NETLINK)
1210 return ERR_PTR(-EINVAL);
1211
1212 sock_hold(sock);
1213 return sock;
1214}
1215
1216/*
1217 * Attach a skb to a netlink socket.
1218 * The caller must hold a reference to the destination socket. On error, the
1219 * reference is dropped. The skb is not send to the destination, just all
1220 * all error checks are performed and memory in the queue is reserved.
1221 * Return values:
1222 * < 0: error. skb freed, reference to sock dropped.
1223 * 0: continue
1224 * 1: repeat lookup - reference dropped while waiting for socket memory.
1225 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001226int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001227 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228{
1229 struct netlink_sock *nlk;
1230
1231 nlk = nlk_sk(sk);
1232
1233 if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001234 test_bit(NETLINK_CONGESTED, &nlk->state)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001236 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001237 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 netlink_overrun(sk);
1239 sock_put(sk);
1240 kfree_skb(skb);
1241 return -EAGAIN;
1242 }
1243
1244 __set_current_state(TASK_INTERRUPTIBLE);
1245 add_wait_queue(&nlk->wait, &wait);
1246
1247 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001248 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001250 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251
1252 __set_current_state(TASK_RUNNING);
1253 remove_wait_queue(&nlk->wait, &wait);
1254 sock_put(sk);
1255
1256 if (signal_pending(current)) {
1257 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001258 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 return 1;
1261 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001262 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001266static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 int len = skb->len;
1269
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 skb_queue_tail(&sk->sk_receive_queue, skb);
1271 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001272 return len;
1273}
1274
1275int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1276{
1277 int len = __netlink_sendskb(sk, skb);
1278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 sock_put(sk);
1280 return len;
1281}
1282
1283void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1284{
1285 kfree_skb(skb);
1286 sock_put(sk);
1287}
1288
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001289static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 int delta;
1292
Patrick McHardy1298ca42013-04-17 06:46:59 +00001293 WARN_ON(skb->sk != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001295 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (delta * 2 < skb->truesize)
1297 return skb;
1298
1299 if (skb_shared(skb)) {
1300 struct sk_buff *nskb = skb_clone(skb, allocation);
1301 if (!nskb)
1302 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001303 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 skb = nskb;
1305 }
1306
1307 if (!pskb_expand_head(skb, 0, -delta, allocation))
1308 skb->truesize -= delta;
1309
1310 return skb;
1311}
1312
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001313static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001314{
1315 struct netlink_sock *nlk = nlk_sk(sk);
1316
1317 if (skb_queue_empty(&sk->sk_receive_queue))
Patrick McHardycd967e02013-04-17 06:46:56 +00001318 clear_bit(NETLINK_CONGESTED, &nlk->state);
1319 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001320 wake_up_interruptible(&nlk->wait);
1321}
1322
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001323static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1324 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001325{
1326 int ret;
1327 struct netlink_sock *nlk = nlk_sk(sk);
1328
1329 ret = -ECONNREFUSED;
1330 if (nlk->netlink_rcv != NULL) {
1331 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001332 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001333 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001334 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001335 consume_skb(skb);
1336 } else {
1337 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001338 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001339 sock_put(sk);
1340 return ret;
1341}
1342
1343int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001344 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct sock *sk;
1347 int err;
1348 long timeo;
1349
1350 skb = netlink_trim(skb, gfp_any());
1351
1352 timeo = sock_sndtimeo(ssk, nonblock);
1353retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001354 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 if (IS_ERR(sk)) {
1356 kfree_skb(skb);
1357 return PTR_ERR(sk);
1358 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001359 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001360 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001361
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001362 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001363 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001364 kfree_skb(skb);
1365 sock_put(sk);
1366 return err;
1367 }
1368
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001369 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 if (err == 1)
1371 goto retry;
1372 if (err)
1373 return err;
1374
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001375 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001377EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
Patrick McHardy4277a082006-03-20 18:52:01 -08001379int netlink_has_listeners(struct sock *sk, unsigned int group)
1380{
1381 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001382 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001383
Denis V. Lunevaed81562007-10-10 21:14:32 -07001384 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001385
1386 rcu_read_lock();
1387 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1388
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001389 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001390 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001391
1392 rcu_read_unlock();
1393
Patrick McHardy4277a082006-03-20 18:52:01 -08001394 return res;
1395}
1396EXPORT_SYMBOL_GPL(netlink_has_listeners);
1397
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001398static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399{
1400 struct netlink_sock *nlk = nlk_sk(sk);
1401
1402 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001403 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001404 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001405 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001406 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408 return -1;
1409}
1410
1411struct netlink_broadcast_data {
1412 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001413 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001414 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 u32 group;
1416 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001417 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 int congested;
1419 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001420 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001422 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1423 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424};
1425
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001426static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct netlink_broadcast_data *p)
1428{
1429 struct netlink_sock *nlk = nlk_sk(sk);
1430 int val;
1431
1432 if (p->exclude_sk == sk)
1433 goto out;
1434
Eric W. Biederman15e47302012-09-07 20:12:54 +00001435 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001436 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 goto out;
1438
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001439 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001440 goto out;
1441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 if (p->failure) {
1443 netlink_overrun(sk);
1444 goto out;
1445 }
1446
1447 sock_hold(sk);
1448 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001449 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 p->skb2 = skb_clone(p->skb, p->allocation);
1451 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001452 p->skb2 = skb_get(p->skb);
1453 /*
1454 * skb ownership may have been set when
1455 * delivered to a previous socket.
1456 */
1457 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 }
1460 if (p->skb2 == NULL) {
1461 netlink_overrun(sk);
1462 /* Clone failed. Notify ALL listeners. */
1463 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001464 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1465 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001466 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1467 kfree_skb(p->skb2);
1468 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001469 } else if (sk_filter(sk, p->skb2)) {
1470 kfree_skb(p->skb2);
1471 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1473 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001474 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1475 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 } else {
1477 p->congested |= val;
1478 p->delivered = 1;
1479 p->skb2 = NULL;
1480 }
1481 sock_put(sk);
1482
1483out:
1484 return 0;
1485}
1486
Eric W. Biederman15e47302012-09-07 20:12:54 +00001487int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001488 u32 group, gfp_t allocation,
1489 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1490 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001492 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 struct sock *sk;
1495
1496 skb = netlink_trim(skb, allocation);
1497
1498 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001499 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001500 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 info.group = group;
1502 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001503 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 info.congested = 0;
1505 info.delivered = 0;
1506 info.allocation = allocation;
1507 info.skb = skb;
1508 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001509 info.tx_filter = filter;
1510 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
1512 /* While we sleep in clone, do not allow to change socket list */
1513
1514 netlink_lock_table();
1515
Sasha Levinb67bfe02013-02-27 17:06:00 -08001516 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 do_one_broadcast(sk, &info);
1518
Neil Horman70d4bf62010-07-20 06:45:56 +00001519 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 netlink_unlock_table();
1522
Neil Horman70d4bf62010-07-20 06:45:56 +00001523 if (info.delivery_failure) {
1524 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001525 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001526 }
1527 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (info.delivered) {
1530 if (info.congested && (allocation & __GFP_WAIT))
1531 yield();
1532 return 0;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return -ESRCH;
1535}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001536EXPORT_SYMBOL(netlink_broadcast_filtered);
1537
Eric W. Biederman15e47302012-09-07 20:12:54 +00001538int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001539 u32 group, gfp_t allocation)
1540{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001541 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001542 NULL, NULL);
1543}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001544EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546struct netlink_set_err_data {
1547 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001548 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 u32 group;
1550 int code;
1551};
1552
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001553static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554{
1555 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001556 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 if (sk == p->exclude_sk)
1559 goto out;
1560
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001561 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001562 goto out;
1563
Eric W. Biederman15e47302012-09-07 20:12:54 +00001564 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001565 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 goto out;
1567
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001568 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1569 ret = 1;
1570 goto out;
1571 }
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 sk->sk_err = p->code;
1574 sk->sk_error_report(sk);
1575out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001576 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001579/**
1580 * netlink_set_err - report error to broadcast listeners
1581 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001582 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001583 * @groups: the broadcast group that will notice the error
1584 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001585 *
1586 * This function returns the number of broadcast listeners that have set the
1587 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001588 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001589int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590{
1591 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001593 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001596 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001598 /* sk->sk_err wants a positive error value */
1599 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
1601 read_lock(&nl_table_lock);
1602
Sasha Levinb67bfe02013-02-27 17:06:00 -08001603 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001604 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001609EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Johannes Berg84659eb2007-07-18 15:47:05 -07001611/* must be called with netlink table grabbed */
1612static void netlink_update_socket_mc(struct netlink_sock *nlk,
1613 unsigned int group,
1614 int is_new)
1615{
1616 int old, new = !!is_new, subscriptions;
1617
1618 old = test_bit(group - 1, nlk->groups);
1619 subscriptions = nlk->subscriptions - old + new;
1620 if (new)
1621 __set_bit(group - 1, nlk->groups);
1622 else
1623 __clear_bit(group - 1, nlk->groups);
1624 netlink_update_subscriptions(&nlk->sk, subscriptions);
1625 netlink_update_listeners(&nlk->sk);
1626}
1627
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001628static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001629 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001630{
1631 struct sock *sk = sock->sk;
1632 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001633 unsigned int val = 0;
1634 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001635
1636 if (level != SOL_NETLINK)
1637 return -ENOPROTOOPT;
1638
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001639 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1640 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001641 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001642 return -EFAULT;
1643
1644 switch (optname) {
1645 case NETLINK_PKTINFO:
1646 if (val)
1647 nlk->flags |= NETLINK_RECV_PKTINFO;
1648 else
1649 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1650 err = 0;
1651 break;
1652 case NETLINK_ADD_MEMBERSHIP:
1653 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001654 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001655 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001656 err = netlink_realloc_groups(sk);
1657 if (err)
1658 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001659 if (!val || val - 1 >= nlk->ngroups)
1660 return -EINVAL;
1661 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001662 netlink_update_socket_mc(nlk, val,
1663 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001664 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001665
1666 if (nlk->netlink_bind)
1667 nlk->netlink_bind(val);
1668
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001669 err = 0;
1670 break;
1671 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001672 case NETLINK_BROADCAST_ERROR:
1673 if (val)
1674 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1675 else
1676 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1677 err = 0;
1678 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001679 case NETLINK_NO_ENOBUFS:
1680 if (val) {
1681 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001682 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001683 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001684 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001685 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001686 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001687 err = 0;
1688 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001689#ifdef CONFIG_NETLINK_MMAP
1690 case NETLINK_RX_RING:
1691 case NETLINK_TX_RING: {
1692 struct nl_mmap_req req;
1693
1694 /* Rings might consume more memory than queue limits, require
1695 * CAP_NET_ADMIN.
1696 */
1697 if (!capable(CAP_NET_ADMIN))
1698 return -EPERM;
1699 if (optlen < sizeof(req))
1700 return -EINVAL;
1701 if (copy_from_user(&req, optval, sizeof(req)))
1702 return -EFAULT;
1703 err = netlink_set_ring(sk, &req, false,
1704 optname == NETLINK_TX_RING);
1705 break;
1706 }
1707#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001708 default:
1709 err = -ENOPROTOOPT;
1710 }
1711 return err;
1712}
1713
1714static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001715 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001716{
1717 struct sock *sk = sock->sk;
1718 struct netlink_sock *nlk = nlk_sk(sk);
1719 int len, val, err;
1720
1721 if (level != SOL_NETLINK)
1722 return -ENOPROTOOPT;
1723
1724 if (get_user(len, optlen))
1725 return -EFAULT;
1726 if (len < 0)
1727 return -EINVAL;
1728
1729 switch (optname) {
1730 case NETLINK_PKTINFO:
1731 if (len < sizeof(int))
1732 return -EINVAL;
1733 len = sizeof(int);
1734 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001735 if (put_user(len, optlen) ||
1736 put_user(val, optval))
1737 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001738 err = 0;
1739 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001740 case NETLINK_BROADCAST_ERROR:
1741 if (len < sizeof(int))
1742 return -EINVAL;
1743 len = sizeof(int);
1744 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1745 if (put_user(len, optlen) ||
1746 put_user(val, optval))
1747 return -EFAULT;
1748 err = 0;
1749 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001750 case NETLINK_NO_ENOBUFS:
1751 if (len < sizeof(int))
1752 return -EINVAL;
1753 len = sizeof(int);
1754 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1755 if (put_user(len, optlen) ||
1756 put_user(val, optval))
1757 return -EFAULT;
1758 err = 0;
1759 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001760 default:
1761 err = -ENOPROTOOPT;
1762 }
1763 return err;
1764}
1765
1766static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1767{
1768 struct nl_pktinfo info;
1769
1770 info.group = NETLINK_CB(skb).dst_group;
1771 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1772}
1773
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1775 struct msghdr *msg, size_t len)
1776{
1777 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1778 struct sock *sk = sock->sk;
1779 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001780 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001781 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001782 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct sk_buff *skb;
1784 int err;
1785 struct scm_cookie scm;
1786
1787 if (msg->msg_flags&MSG_OOB)
1788 return -EOPNOTSUPP;
1789
Eric Dumazet16e57262011-09-19 05:52:27 +00001790 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001792
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001793 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (err < 0)
1795 return err;
1796
1797 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001798 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001800 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001801 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001802 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001803 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001804 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001805 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001806 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001808 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001809 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 }
1811
Eric W. Biederman15e47302012-09-07 20:12:54 +00001812 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 err = netlink_autobind(sock);
1814 if (err)
1815 goto out;
1816 }
1817
1818 err = -EMSGSIZE;
1819 if (len > sk->sk_sndbuf - 32)
1820 goto out;
1821 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001822 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001823 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 goto out;
1825
Eric W. Biederman15e47302012-09-07 20:12:54 +00001826 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001827 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001828 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001831 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 kfree_skb(skb);
1833 goto out;
1834 }
1835
1836 err = security_netlink_send(sk, skb);
1837 if (err) {
1838 kfree_skb(skb);
1839 goto out;
1840 }
1841
Patrick McHardyd629b832005-08-14 19:27:50 -07001842 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001844 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001846 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001849 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return err;
1851}
1852
1853static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1854 struct msghdr *msg, size_t len,
1855 int flags)
1856{
1857 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1858 struct scm_cookie scm;
1859 struct sock *sk = sock->sk;
1860 struct netlink_sock *nlk = nlk_sk(sk);
1861 int noblock = flags&MSG_DONTWAIT;
1862 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001863 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001864 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865
1866 if (flags&MSG_OOB)
1867 return -EOPNOTSUPP;
1868
1869 copied = 0;
1870
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001871 skb = skb_recv_datagram(sk, flags, noblock, &err);
1872 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 goto out;
1874
Johannes Berg68d6ac62010-08-15 21:20:44 +00001875 data_skb = skb;
1876
Johannes Berg1dacc762009-07-01 11:26:02 +00001877#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
1878 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00001879 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00001880 * If this skb has a frag_list, then here that means that we
1881 * will have to use the frag_list skb's data for compat tasks
1882 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00001883 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00001884 * If we need to send the compat skb, assign it to the
1885 * 'data_skb' variable so that it will be used below for data
1886 * copying. We keep 'skb' for everything else, including
1887 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00001888 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00001889 if (flags & MSG_CMSG_COMPAT)
1890 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00001891 }
1892#endif
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 msg->msg_namelen = 0;
1895
Johannes Berg68d6ac62010-08-15 21:20:44 +00001896 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (len < copied) {
1898 msg->msg_flags |= MSG_TRUNC;
1899 copied = len;
1900 }
1901
Johannes Berg68d6ac62010-08-15 21:20:44 +00001902 skb_reset_transport_header(data_skb);
1903 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
1905 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001906 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 addr->nl_family = AF_NETLINK;
1908 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001909 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001910 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 msg->msg_namelen = sizeof(*addr);
1912 }
1913
Patrick McHardycc9a06c2006-03-12 20:34:27 -08001914 if (nlk->flags & NETLINK_RECV_PKTINFO)
1915 netlink_cmsg_recv_pktinfo(msg, skb);
1916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (NULL == siocb->scm) {
1918 memset(&scm, 0, sizeof(scm));
1919 siocb->scm = &scm;
1920 }
1921 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07001922 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00001923 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07001924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 skb_free_datagram(sk, skb);
1926
Andrey Vaginb44d2112011-02-21 02:40:47 +00001927 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
1928 ret = netlink_dump(sk);
1929 if (ret) {
1930 sk->sk_err = ret;
1931 sk->sk_error_report(sk);
1932 }
1933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934
1935 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936out:
1937 netlink_rcv_wake(sk);
1938 return err ? : copied;
1939}
1940
1941static void netlink_data_ready(struct sock *sk, int len)
1942{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001943 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945
1946/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001947 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 * complete set of kernel non-blocking support for message
1949 * queueing.
1950 */
1951
1952struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00001953__netlink_kernel_create(struct net *net, int unit, struct module *module,
1954 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955{
1956 struct socket *sock;
1957 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001958 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001959 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001960 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
1961 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Akinobu Mitafab2caf2006-08-29 02:15:24 -07001963 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001965 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 return NULL;
1967
1968 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
1969 return NULL;
1970
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08001971 /*
1972 * We have to just have a reference on the net from sk, but don't
1973 * get_net it. Besides, we cannot get and then put the net here.
1974 * So we create one inside init_net and the move it to net.
1975 */
1976
1977 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
1978 goto out_sock_release_nosk;
1979
1980 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08001981 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001982
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001983 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08001984 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001985 else
1986 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001987
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001988 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08001989 if (!listeners)
1990 goto out_sock_release;
1991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00001993 if (cfg && cfg->input)
1994 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001996 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07001997 goto out_sock_release;
1998
1999 nlk = nlk_sk(sk);
2000 nlk->flags |= NETLINK_KERNEL_SOCKET;
2001
2002 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002003 if (!nl_table[unit].registered) {
2004 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002005 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002006 nl_table[unit].cb_mutex = cb_mutex;
2007 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002008 if (cfg) {
2009 nl_table[unit].bind = cfg->bind;
2010 nl_table[unit].flags = cfg->flags;
2011 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002012 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002013 } else {
2014 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002015 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002016 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002017 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002018 return sk;
2019
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002020out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002021 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002022 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002023 return NULL;
2024
2025out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002026 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002027 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002029EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002030
2031void
2032netlink_kernel_release(struct sock *sk)
2033{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002034 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002035}
2036EXPORT_SYMBOL(netlink_kernel_release);
2037
Johannes Bergd136f1b2009-09-12 03:03:15 +00002038int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002039{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002040 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002041 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002042
2043 if (groups < 32)
2044 groups = 32;
2045
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002046 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002047 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2048 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002049 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002050 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002051 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2052 rcu_assign_pointer(tbl->listeners, new);
2053
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002054 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002055 }
2056 tbl->groups = groups;
2057
Johannes Bergd136f1b2009-09-12 03:03:15 +00002058 return 0;
2059}
2060
2061/**
2062 * netlink_change_ngroups - change number of multicast groups
2063 *
2064 * This changes the number of multicast groups that are available
2065 * on a certain netlink family. Note that it is not possible to
2066 * change the number of groups to below 32. Also note that it does
2067 * not implicitly call netlink_clear_multicast_users() when the
2068 * number of groups is reduced.
2069 *
2070 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2071 * @groups: The new number of groups.
2072 */
2073int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2074{
2075 int err;
2076
2077 netlink_table_grab();
2078 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002079 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002080
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002081 return err;
2082}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002083
Johannes Bergb8273572009-09-24 15:44:05 -07002084void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2085{
2086 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002087 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2088
Sasha Levinb67bfe02013-02-27 17:06:00 -08002089 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002090 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2091}
2092
Johannes Berg84659eb2007-07-18 15:47:05 -07002093/**
2094 * netlink_clear_multicast_users - kick off multicast listeners
2095 *
2096 * This function removes all listeners from the given group.
2097 * @ksk: The kernel netlink socket, as returned by
2098 * netlink_kernel_create().
2099 * @group: The multicast group to clear.
2100 */
2101void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2102{
Johannes Berg84659eb2007-07-18 15:47:05 -07002103 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07002104 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07002105 netlink_table_ungrab();
2106}
Johannes Berg84659eb2007-07-18 15:47:05 -07002107
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002108struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002109__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002110{
2111 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002112 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002113
2114 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
2115 nlh->nlmsg_type = type;
2116 nlh->nlmsg_len = size;
2117 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002118 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002119 nlh->nlmsg_seq = seq;
2120 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002121 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002122 return nlh;
2123}
2124EXPORT_SYMBOL(__nlmsg_put);
2125
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126/*
2127 * It looks a bit ugly.
2128 * It would be better to create kernel thread.
2129 */
2130
2131static int netlink_dump(struct sock *sk)
2132{
2133 struct netlink_sock *nlk = nlk_sk(sk);
2134 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002135 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002137 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002138 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002140 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142 cb = nlk->cb;
2143 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002144 err = -EINVAL;
2145 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 }
2147
Greg Rosec7ac8672011-06-10 01:27:09 +00002148 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2149
2150 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
2151 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002152 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002153
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 len = cb->dump(skb, cb);
2155
2156 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002157 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002158
2159 if (sk_filter(sk, skb))
2160 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002161 else
2162 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return 0;
2164 }
2165
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002166 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2167 if (!nlh)
2168 goto errout_skb;
2169
Johannes Berg670dc282011-06-20 13:40:46 +02002170 nl_dump_check_consistent(cb, nlh);
2171
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002172 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2173
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002174 if (sk_filter(sk, skb))
2175 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002176 else
2177 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178
Thomas Grafa8f74b22005-11-10 02:25:52 +01002179 if (cb->done)
2180 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002182 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Gao feng6dc878a2012-10-04 20:15:48 +00002184 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00002185 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002187
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002188errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002189 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002190 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002191 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192}
2193
Gao feng6dc878a2012-10-04 20:15:48 +00002194int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2195 const struct nlmsghdr *nlh,
2196 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197{
2198 struct netlink_callback *cb;
2199 struct sock *sk;
2200 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002201 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002203 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (cb == NULL)
2205 return -ENOBUFS;
2206
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002207 cb->dump = control->dump;
2208 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002210 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002211 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002212 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 atomic_inc(&skb->users);
2214 cb->skb = skb;
2215
Eric W. Biederman15e47302012-09-07 20:12:54 +00002216 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 if (sk == NULL) {
2218 netlink_destroy_callback(cb);
2219 return -ECONNREFUSED;
2220 }
2221 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002222
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002223 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002224 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07002225 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002226 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00002228 ret = -EBUSY;
2229 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 }
Gao feng6dc878a2012-10-04 20:15:48 +00002231 /* add reference of module which cb->dump belongs to */
2232 if (!try_module_get(cb->module)) {
2233 mutex_unlock(nlk->cb_mutex);
2234 netlink_destroy_callback(cb);
2235 ret = -EPROTONOSUPPORT;
2236 goto out;
2237 }
2238
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002240 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Andrey Vaginb44d2112011-02-21 02:40:47 +00002242 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002245
Andrey Vaginb44d2112011-02-21 02:40:47 +00002246 if (ret)
2247 return ret;
2248
Denis V. Lunev5c582982007-10-23 20:29:25 -07002249 /* We successfully started a dump, by returning -EINTR we
2250 * signal not to send ACK even if it was requested.
2251 */
2252 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253}
Gao feng6dc878a2012-10-04 20:15:48 +00002254EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2257{
2258 struct sk_buff *skb;
2259 struct nlmsghdr *rep;
2260 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002261 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Thomas Graf339bf982006-11-10 14:10:15 -08002263 /* error messages get the original request appened */
2264 if (err)
2265 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Thomas Graf339bf982006-11-10 14:10:15 -08002267 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (!skb) {
2269 struct sock *sk;
2270
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002271 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002272 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002273 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (sk) {
2275 sk->sk_err = ENOBUFS;
2276 sk->sk_error_report(sk);
2277 sock_put(sk);
2278 }
2279 return;
2280 }
2281
Eric W. Biederman15e47302012-09-07 20:12:54 +00002282 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002283 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002284 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002286 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002287 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002289EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002291int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002292 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002293{
Thomas Graf82ace472005-11-10 02:25:53 +01002294 struct nlmsghdr *nlh;
2295 int err;
2296
2297 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002298 int msglen;
2299
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002300 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002301 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002302
Martin Murrayad8e4b72006-01-10 13:02:29 -08002303 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002304 return 0;
2305
Thomas Grafd35b6852007-03-22 23:28:46 -07002306 /* Only requests are handled by the kernel */
2307 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002308 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002309
Thomas Graf45e7ae72007-03-22 23:29:10 -07002310 /* Skip control messages */
2311 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002312 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002313
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002314 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002315 if (err == -EINTR)
2316 goto skip;
2317
2318ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002319 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002320 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002321
Denis V. Lunev5c582982007-10-23 20:29:25 -07002322skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002323 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002324 if (msglen > skb->len)
2325 msglen = skb->len;
2326 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002327 }
2328
2329 return 0;
2330}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002331EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002332
2333/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002334 * nlmsg_notify - send a notification netlink message
2335 * @sk: netlink socket to use
2336 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002337 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002338 * @group: destination multicast group or 0
2339 * @report: 1 to report back, 0 to disable
2340 * @flags: allocation flags
2341 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002342int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002343 unsigned int group, int report, gfp_t flags)
2344{
2345 int err = 0;
2346
2347 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002348 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002349
2350 if (report) {
2351 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002352 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002353 }
2354
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002355 /* errors reported via destination sk->sk_err, but propagate
2356 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002357 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002358 }
2359
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002360 if (report) {
2361 int err2;
2362
Eric W. Biederman15e47302012-09-07 20:12:54 +00002363 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002364 if (!err || err == -ESRCH)
2365 err = err2;
2366 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002367
2368 return err;
2369}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002370EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372#ifdef CONFIG_PROC_FS
2373struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002374 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 int link;
2376 int hash_idx;
2377};
2378
2379static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2380{
2381 struct nl_seq_iter *iter = seq->private;
2382 int i, j;
2383 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 loff_t off = 0;
2385
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002386 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002387 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002390 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002391 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002392 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (off == pos) {
2394 iter->link = i;
2395 iter->hash_idx = j;
2396 return s;
2397 }
2398 ++off;
2399 }
2400 }
2401 }
2402 return NULL;
2403}
2404
2405static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002406 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407{
2408 read_lock(&nl_table_lock);
2409 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2410}
2411
2412static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2413{
2414 struct sock *s;
2415 struct nl_seq_iter *iter;
2416 int i, j;
2417
2418 ++*pos;
2419
2420 if (v == SEQ_START_TOKEN)
2421 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002422
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002423 iter = seq->private;
2424 s = v;
2425 do {
2426 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002427 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 if (s)
2429 return s;
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 i = iter->link;
2432 j = iter->hash_idx + 1;
2433
2434 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002435 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 for (; j <= hash->mask; j++) {
2438 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002439 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002440 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 if (s) {
2442 iter->link = i;
2443 iter->hash_idx = j;
2444 return s;
2445 }
2446 }
2447
2448 j = 0;
2449 } while (++i < MAX_LINKS);
2450
2451 return NULL;
2452}
2453
2454static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002455 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456{
2457 read_unlock(&nl_table_lock);
2458}
2459
2460
2461static int netlink_seq_show(struct seq_file *seq, void *v)
2462{
Eric Dumazet658cb352012-04-22 21:30:21 +00002463 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 seq_puts(seq,
2465 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002466 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002467 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 struct sock *s = v;
2469 struct netlink_sock *nlk = nlk_sk(s);
2470
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002471 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 s,
2473 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002474 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002475 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002476 sk_rmem_alloc_get(s),
2477 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002479 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002480 atomic_read(&s->sk_drops),
2481 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 );
2483
2484 }
2485 return 0;
2486}
2487
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002488static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 .start = netlink_seq_start,
2490 .next = netlink_seq_next,
2491 .stop = netlink_seq_stop,
2492 .show = netlink_seq_show,
2493};
2494
2495
2496static int netlink_seq_open(struct inode *inode, struct file *file)
2497{
Denis V. Luneve372c412007-11-19 22:31:54 -08002498 return seq_open_net(inode, file, &netlink_seq_ops,
2499 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002500}
2501
Arjan van de Venda7071d2007-02-12 00:55:36 -08002502static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 .owner = THIS_MODULE,
2504 .open = netlink_seq_open,
2505 .read = seq_read,
2506 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002507 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508};
2509
2510#endif
2511
2512int netlink_register_notifier(struct notifier_block *nb)
2513{
Alan Sterne041c682006-03-27 01:16:30 -08002514 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002516EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518int netlink_unregister_notifier(struct notifier_block *nb)
2519{
Alan Sterne041c682006-03-27 01:16:30 -08002520 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002522EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002523
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002524static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 .family = PF_NETLINK,
2526 .owner = THIS_MODULE,
2527 .release = netlink_release,
2528 .bind = netlink_bind,
2529 .connect = netlink_connect,
2530 .socketpair = sock_no_socketpair,
2531 .accept = sock_no_accept,
2532 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00002533 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 .ioctl = sock_no_ioctl,
2535 .listen = sock_no_listen,
2536 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002537 .setsockopt = netlink_setsockopt,
2538 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 .sendmsg = netlink_sendmsg,
2540 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002541 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 .sendpage = sock_no_sendpage,
2543};
2544
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002545static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 .family = PF_NETLINK,
2547 .create = netlink_create,
2548 .owner = THIS_MODULE, /* for consistency 8) */
2549};
2550
Pavel Emelyanov46650792007-10-08 20:38:39 -07002551static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002552{
2553#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002554 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002555 return -ENOMEM;
2556#endif
2557 return 0;
2558}
2559
Pavel Emelyanov46650792007-10-08 20:38:39 -07002560static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002561{
2562#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002563 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002564#endif
2565}
2566
David S. Millerb963ea82010-08-30 19:08:01 -07002567static void __init netlink_add_usersock_entry(void)
2568{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002569 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002570 int groups = 32;
2571
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002572 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002573 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002574 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002575
2576 netlink_table_grab();
2577
2578 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002579 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002580 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2581 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002582 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002583
2584 netlink_table_ungrab();
2585}
2586
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002587static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002588 .init = netlink_net_init,
2589 .exit = netlink_net_exit,
2590};
2591
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592static int __init netlink_proto_init(void)
2593{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002595 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 unsigned int order;
2597 int err = proto_register(&netlink_proto, 0);
2598
2599 if (err != 0)
2600 goto out;
2601
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002602 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002604 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002605 if (!nl_table)
2606 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Jan Beulich44813742009-09-21 17:03:05 -07002608 if (totalram_pages >= (128 * 1024))
2609 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 else
Jan Beulich44813742009-09-21 17:03:05 -07002611 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002613 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2614 limit = (1UL << order) / sizeof(struct hlist_head);
2615 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616
2617 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002618 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619
Eric W. Biederman15e47302012-09-07 20:12:54 +00002620 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 if (!hash->table) {
2622 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002623 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624 1 * sizeof(*hash->table));
2625 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002626 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 hash->max_shift = order;
2629 hash->shift = 0;
2630 hash->mask = 0;
2631 hash->rehash_time = jiffies;
2632 }
2633
David S. Millerb963ea82010-08-30 19:08:01 -07002634 netlink_add_usersock_entry();
2635
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002637 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002638 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 rtnetlink_init();
2640out:
2641 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002642panic:
2643 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
2645
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646core_initcall(netlink_proto_init);