blob: 9b6b115e008f904070cfcde5a6d7b9ebdc033387 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
Patrick McHardycd1df522013-04-17 06:47:05 +00006 * Patrick McHardy <kaber@trash.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
14 * added netlink_proto_exit
15 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
16 * use nlk_sk, as sk->protinfo is on a diet 8)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070017 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
18 * - inc module use count of module that owns
19 * the kernel socket in case userspace opens
20 * socket of same protocol
21 * - remove all module support, since netlink is
22 * mandatory if CONFIG_NET=y these days
Linus Torvalds1da177e2005-04-16 15:20:36 -070023 */
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/module.h>
26
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/kernel.h>
29#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/signal.h>
31#include <linux/sched.h>
32#include <linux/errno.h>
33#include <linux/string.h>
34#include <linux/stat.h>
35#include <linux/socket.h>
36#include <linux/un.h>
37#include <linux/fcntl.h>
38#include <linux/termios.h>
39#include <linux/sockios.h>
40#include <linux/net.h>
41#include <linux/fs.h>
42#include <linux/slab.h>
43#include <asm/uaccess.h>
44#include <linux/skbuff.h>
45#include <linux/netdevice.h>
46#include <linux/rtnetlink.h>
47#include <linux/proc_fs.h>
48#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/notifier.h>
50#include <linux/security.h>
51#include <linux/jhash.h>
52#include <linux/jiffies.h>
53#include <linux/random.h>
54#include <linux/bitops.h>
55#include <linux/mm.h>
56#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010057#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070058#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000059#include <linux/vmalloc.h>
Patrick McHardy9652e932013-04-17 06:47:02 +000060#include <asm/cacheflush.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010061
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020062#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#include <net/sock.h>
64#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010065#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Andrey Vagin0f29c762013-03-21 20:33:47 +040067#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Eric Dumazet5c398dc2010-10-24 04:27:10 +000069struct listeners {
70 struct rcu_head rcu;
71 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000072};
73
Patrick McHardycd967e02013-04-17 06:46:56 +000074/* state bits */
75#define NETLINK_CONGESTED 0x0
76
77/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070078#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070079#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000080#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070081#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070082
David S. Miller035c4c12011-12-23 17:33:03 -050083static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070084{
85 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
86}
87
Andrey Vagin0f29c762013-03-21 20:33:47 +040088struct netlink_table *nl_table;
89EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
91static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
92
93static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000094static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Andrey Vagin0f29c762013-03-21 20:33:47 +040096DEFINE_RWLOCK(nl_table_lock);
97EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098static atomic_t nl_table_users = ATOMIC_INIT(0);
99
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000100#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
101
Alan Sterne041c682006-03-27 01:16:30 -0800102static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000104static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700105{
106 return group ? 1 << (group - 1) : 0;
107}
108
Eric W. Biederman15e47302012-09-07 20:12:54 +0000109static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000111 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Patrick McHardycd1df522013-04-17 06:47:05 +0000114static void netlink_overrun(struct sock *sk)
115{
116 struct netlink_sock *nlk = nlk_sk(sk);
117
118 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
119 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
120 sk->sk_err = ENOBUFS;
121 sk->sk_error_report(sk);
122 }
123 }
124 atomic_inc(&sk->sk_drops);
125}
126
127static void netlink_rcv_wake(struct sock *sk)
128{
129 struct netlink_sock *nlk = nlk_sk(sk);
130
131 if (skb_queue_empty(&sk->sk_receive_queue))
132 clear_bit(NETLINK_CONGESTED, &nlk->state);
133 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
134 wake_up_interruptible(&nlk->wait);
135}
136
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000137#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000138static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
139{
140 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
141}
142
Patrick McHardyf9c22882013-04-17 06:47:04 +0000143static bool netlink_rx_is_mmaped(struct sock *sk)
144{
145 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
146}
147
Patrick McHardy5fd96122013-04-17 06:47:03 +0000148static bool netlink_tx_is_mmaped(struct sock *sk)
149{
150 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
151}
152
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000153static __pure struct page *pgvec_to_page(const void *addr)
154{
155 if (is_vmalloc_addr(addr))
156 return vmalloc_to_page(addr);
157 else
158 return virt_to_page(addr);
159}
160
161static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
162{
163 unsigned int i;
164
165 for (i = 0; i < len; i++) {
166 if (pg_vec[i] != NULL) {
167 if (is_vmalloc_addr(pg_vec[i]))
168 vfree(pg_vec[i]);
169 else
170 free_pages((unsigned long)pg_vec[i], order);
171 }
172 }
173 kfree(pg_vec);
174}
175
176static void *alloc_one_pg_vec_page(unsigned long order)
177{
178 void *buffer;
179 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
180 __GFP_NOWARN | __GFP_NORETRY;
181
182 buffer = (void *)__get_free_pages(gfp_flags, order);
183 if (buffer != NULL)
184 return buffer;
185
186 buffer = vzalloc((1 << order) * PAGE_SIZE);
187 if (buffer != NULL)
188 return buffer;
189
190 gfp_flags &= ~__GFP_NORETRY;
191 return (void *)__get_free_pages(gfp_flags, order);
192}
193
194static void **alloc_pg_vec(struct netlink_sock *nlk,
195 struct nl_mmap_req *req, unsigned int order)
196{
197 unsigned int block_nr = req->nm_block_nr;
198 unsigned int i;
199 void **pg_vec, *ptr;
200
201 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
202 if (pg_vec == NULL)
203 return NULL;
204
205 for (i = 0; i < block_nr; i++) {
206 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
207 if (pg_vec[i] == NULL)
208 goto err1;
209 }
210
211 return pg_vec;
212err1:
213 free_pg_vec(pg_vec, order, block_nr);
214 return NULL;
215}
216
217static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
218 bool closing, bool tx_ring)
219{
220 struct netlink_sock *nlk = nlk_sk(sk);
221 struct netlink_ring *ring;
222 struct sk_buff_head *queue;
223 void **pg_vec = NULL;
224 unsigned int order = 0;
225 int err;
226
227 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
228 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
229
230 if (!closing) {
231 if (atomic_read(&nlk->mapped))
232 return -EBUSY;
233 if (atomic_read(&ring->pending))
234 return -EBUSY;
235 }
236
237 if (req->nm_block_nr) {
238 if (ring->pg_vec != NULL)
239 return -EBUSY;
240
241 if ((int)req->nm_block_size <= 0)
242 return -EINVAL;
243 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
244 return -EINVAL;
245 if (req->nm_frame_size < NL_MMAP_HDRLEN)
246 return -EINVAL;
247 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
248 return -EINVAL;
249
250 ring->frames_per_block = req->nm_block_size /
251 req->nm_frame_size;
252 if (ring->frames_per_block == 0)
253 return -EINVAL;
254 if (ring->frames_per_block * req->nm_block_nr !=
255 req->nm_frame_nr)
256 return -EINVAL;
257
258 order = get_order(req->nm_block_size);
259 pg_vec = alloc_pg_vec(nlk, req, order);
260 if (pg_vec == NULL)
261 return -ENOMEM;
262 } else {
263 if (req->nm_frame_nr)
264 return -EINVAL;
265 }
266
267 err = -EBUSY;
268 mutex_lock(&nlk->pg_vec_lock);
269 if (closing || atomic_read(&nlk->mapped) == 0) {
270 err = 0;
271 spin_lock_bh(&queue->lock);
272
273 ring->frame_max = req->nm_frame_nr - 1;
274 ring->head = 0;
275 ring->frame_size = req->nm_frame_size;
276 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
277
278 swap(ring->pg_vec_len, req->nm_block_nr);
279 swap(ring->pg_vec_order, order);
280 swap(ring->pg_vec, pg_vec);
281
282 __skb_queue_purge(queue);
283 spin_unlock_bh(&queue->lock);
284
285 WARN_ON(atomic_read(&nlk->mapped));
286 }
287 mutex_unlock(&nlk->pg_vec_lock);
288
289 if (pg_vec)
290 free_pg_vec(pg_vec, order, req->nm_block_nr);
291 return err;
292}
293
294static void netlink_mm_open(struct vm_area_struct *vma)
295{
296 struct file *file = vma->vm_file;
297 struct socket *sock = file->private_data;
298 struct sock *sk = sock->sk;
299
300 if (sk)
301 atomic_inc(&nlk_sk(sk)->mapped);
302}
303
304static void netlink_mm_close(struct vm_area_struct *vma)
305{
306 struct file *file = vma->vm_file;
307 struct socket *sock = file->private_data;
308 struct sock *sk = sock->sk;
309
310 if (sk)
311 atomic_dec(&nlk_sk(sk)->mapped);
312}
313
314static const struct vm_operations_struct netlink_mmap_ops = {
315 .open = netlink_mm_open,
316 .close = netlink_mm_close,
317};
318
319static int netlink_mmap(struct file *file, struct socket *sock,
320 struct vm_area_struct *vma)
321{
322 struct sock *sk = sock->sk;
323 struct netlink_sock *nlk = nlk_sk(sk);
324 struct netlink_ring *ring;
325 unsigned long start, size, expected;
326 unsigned int i;
327 int err = -EINVAL;
328
329 if (vma->vm_pgoff)
330 return -EINVAL;
331
332 mutex_lock(&nlk->pg_vec_lock);
333
334 expected = 0;
335 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
336 if (ring->pg_vec == NULL)
337 continue;
338 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
339 }
340
341 if (expected == 0)
342 goto out;
343
344 size = vma->vm_end - vma->vm_start;
345 if (size != expected)
346 goto out;
347
348 start = vma->vm_start;
349 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
350 if (ring->pg_vec == NULL)
351 continue;
352
353 for (i = 0; i < ring->pg_vec_len; i++) {
354 struct page *page;
355 void *kaddr = ring->pg_vec[i];
356 unsigned int pg_num;
357
358 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
359 page = pgvec_to_page(kaddr);
360 err = vm_insert_page(vma, start, page);
361 if (err < 0)
362 goto out;
363 start += PAGE_SIZE;
364 kaddr += PAGE_SIZE;
365 }
366 }
367 }
368
369 atomic_inc(&nlk->mapped);
370 vma->vm_ops = &netlink_mmap_ops;
371 err = 0;
372out:
373 mutex_unlock(&nlk->pg_vec_lock);
374 return 0;
375}
Patrick McHardy9652e932013-04-17 06:47:02 +0000376
377static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
378{
379#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
380 struct page *p_start, *p_end;
381
382 /* First page is flushed through netlink_{get,set}_status */
383 p_start = pgvec_to_page(hdr + PAGE_SIZE);
Stephen Rothwell1d5085c2013-04-23 17:40:35 +1000384 p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1);
Patrick McHardy9652e932013-04-17 06:47:02 +0000385 while (p_start <= p_end) {
386 flush_dcache_page(p_start);
387 p_start++;
388 }
389#endif
390}
391
392static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
393{
394 smp_rmb();
395 flush_dcache_page(pgvec_to_page(hdr));
396 return hdr->nm_status;
397}
398
399static void netlink_set_status(struct nl_mmap_hdr *hdr,
400 enum nl_mmap_status status)
401{
402 hdr->nm_status = status;
403 flush_dcache_page(pgvec_to_page(hdr));
404 smp_wmb();
405}
406
407static struct nl_mmap_hdr *
408__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
409{
410 unsigned int pg_vec_pos, frame_off;
411
412 pg_vec_pos = pos / ring->frames_per_block;
413 frame_off = pos % ring->frames_per_block;
414
415 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
416}
417
418static struct nl_mmap_hdr *
419netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
420 enum nl_mmap_status status)
421{
422 struct nl_mmap_hdr *hdr;
423
424 hdr = __netlink_lookup_frame(ring, pos);
425 if (netlink_get_status(hdr) != status)
426 return NULL;
427
428 return hdr;
429}
430
431static struct nl_mmap_hdr *
432netlink_current_frame(const struct netlink_ring *ring,
433 enum nl_mmap_status status)
434{
435 return netlink_lookup_frame(ring, ring->head, status);
436}
437
438static struct nl_mmap_hdr *
439netlink_previous_frame(const struct netlink_ring *ring,
440 enum nl_mmap_status status)
441{
442 unsigned int prev;
443
444 prev = ring->head ? ring->head - 1 : ring->frame_max;
445 return netlink_lookup_frame(ring, prev, status);
446}
447
448static void netlink_increment_head(struct netlink_ring *ring)
449{
450 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
451}
452
453static void netlink_forward_ring(struct netlink_ring *ring)
454{
455 unsigned int head = ring->head, pos = head;
456 const struct nl_mmap_hdr *hdr;
457
458 do {
459 hdr = __netlink_lookup_frame(ring, pos);
460 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
461 break;
462 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
463 break;
464 netlink_increment_head(ring);
465 } while (ring->head != head);
466}
467
Patrick McHardycd1df522013-04-17 06:47:05 +0000468static bool netlink_dump_space(struct netlink_sock *nlk)
469{
470 struct netlink_ring *ring = &nlk->rx_ring;
471 struct nl_mmap_hdr *hdr;
472 unsigned int n;
473
474 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
475 if (hdr == NULL)
476 return false;
477
478 n = ring->head + ring->frame_max / 2;
479 if (n > ring->frame_max)
480 n -= ring->frame_max;
481
482 hdr = __netlink_lookup_frame(ring, n);
483
484 return hdr->nm_status == NL_MMAP_STATUS_UNUSED;
485}
486
Patrick McHardy9652e932013-04-17 06:47:02 +0000487static unsigned int netlink_poll(struct file *file, struct socket *sock,
488 poll_table *wait)
489{
490 struct sock *sk = sock->sk;
491 struct netlink_sock *nlk = nlk_sk(sk);
492 unsigned int mask;
Patrick McHardycd1df522013-04-17 06:47:05 +0000493 int err;
Patrick McHardy9652e932013-04-17 06:47:02 +0000494
Patrick McHardycd1df522013-04-17 06:47:05 +0000495 if (nlk->rx_ring.pg_vec != NULL) {
496 /* Memory mapped sockets don't call recvmsg(), so flow control
497 * for dumps is performed here. A dump is allowed to continue
498 * if at least half the ring is unused.
499 */
500 while (nlk->cb != NULL && netlink_dump_space(nlk)) {
501 err = netlink_dump(sk);
502 if (err < 0) {
503 sk->sk_err = err;
504 sk->sk_error_report(sk);
505 break;
506 }
507 }
508 netlink_rcv_wake(sk);
509 }
Patrick McHardy5fd96122013-04-17 06:47:03 +0000510
Patrick McHardy9652e932013-04-17 06:47:02 +0000511 mask = datagram_poll(file, sock, wait);
512
513 spin_lock_bh(&sk->sk_receive_queue.lock);
514 if (nlk->rx_ring.pg_vec) {
515 netlink_forward_ring(&nlk->rx_ring);
516 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
517 mask |= POLLIN | POLLRDNORM;
518 }
519 spin_unlock_bh(&sk->sk_receive_queue.lock);
520
521 spin_lock_bh(&sk->sk_write_queue.lock);
522 if (nlk->tx_ring.pg_vec) {
523 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
524 mask |= POLLOUT | POLLWRNORM;
525 }
526 spin_unlock_bh(&sk->sk_write_queue.lock);
527
528 return mask;
529}
530
531static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
532{
533 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
534}
535
536static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
537 struct netlink_ring *ring,
538 struct nl_mmap_hdr *hdr)
539{
540 unsigned int size;
541 void *data;
542
543 size = ring->frame_size - NL_MMAP_HDRLEN;
544 data = (void *)hdr + NL_MMAP_HDRLEN;
545
546 skb->head = data;
547 skb->data = data;
548 skb_reset_tail_pointer(skb);
549 skb->end = skb->tail + size;
550 skb->len = 0;
551
552 skb->destructor = netlink_skb_destructor;
553 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
554 NETLINK_CB(skb).sk = sk;
555}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000556
557static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
558 u32 dst_portid, u32 dst_group,
559 struct sock_iocb *siocb)
560{
561 struct netlink_sock *nlk = nlk_sk(sk);
562 struct netlink_ring *ring;
563 struct nl_mmap_hdr *hdr;
564 struct sk_buff *skb;
565 unsigned int maxlen;
566 bool excl = true;
567 int err = 0, len = 0;
568
569 /* Netlink messages are validated by the receiver before processing.
570 * In order to avoid userspace changing the contents of the message
571 * after validation, the socket and the ring may only be used by a
572 * single process, otherwise we fall back to copying.
573 */
574 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
575 atomic_read(&nlk->mapped) > 1)
576 excl = false;
577
578 mutex_lock(&nlk->pg_vec_lock);
579
580 ring = &nlk->tx_ring;
581 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
582
583 do {
584 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
585 if (hdr == NULL) {
586 if (!(msg->msg_flags & MSG_DONTWAIT) &&
587 atomic_read(&nlk->tx_ring.pending))
588 schedule();
589 continue;
590 }
591 if (hdr->nm_len > maxlen) {
592 err = -EINVAL;
593 goto out;
594 }
595
596 netlink_frame_flush_dcache(hdr);
597
598 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
599 skb = alloc_skb_head(GFP_KERNEL);
600 if (skb == NULL) {
601 err = -ENOBUFS;
602 goto out;
603 }
604 sock_hold(sk);
605 netlink_ring_setup_skb(skb, sk, ring, hdr);
606 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
607 __skb_put(skb, hdr->nm_len);
608 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
609 atomic_inc(&ring->pending);
610 } else {
611 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
612 if (skb == NULL) {
613 err = -ENOBUFS;
614 goto out;
615 }
616 __skb_put(skb, hdr->nm_len);
617 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
618 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
619 }
620
621 netlink_increment_head(ring);
622
623 NETLINK_CB(skb).portid = nlk->portid;
624 NETLINK_CB(skb).dst_group = dst_group;
625 NETLINK_CB(skb).creds = siocb->scm->creds;
626
627 err = security_netlink_send(sk, skb);
628 if (err) {
629 kfree_skb(skb);
630 goto out;
631 }
632
633 if (unlikely(dst_group)) {
634 atomic_inc(&skb->users);
635 netlink_broadcast(sk, skb, dst_portid, dst_group,
636 GFP_KERNEL);
637 }
638 err = netlink_unicast(sk, skb, dst_portid,
639 msg->msg_flags & MSG_DONTWAIT);
640 if (err < 0)
641 goto out;
642 len += err;
643
644 } while (hdr != NULL ||
645 (!(msg->msg_flags & MSG_DONTWAIT) &&
646 atomic_read(&nlk->tx_ring.pending)));
647
648 if (len > 0)
649 err = len;
650out:
651 mutex_unlock(&nlk->pg_vec_lock);
652 return err;
653}
Patrick McHardyf9c22882013-04-17 06:47:04 +0000654
655static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
656{
657 struct nl_mmap_hdr *hdr;
658
659 hdr = netlink_mmap_hdr(skb);
660 hdr->nm_len = skb->len;
661 hdr->nm_group = NETLINK_CB(skb).dst_group;
662 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200663 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
664 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000665 netlink_frame_flush_dcache(hdr);
666 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
667
668 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
669 kfree_skb(skb);
670}
671
672static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
673{
674 struct netlink_sock *nlk = nlk_sk(sk);
675 struct netlink_ring *ring = &nlk->rx_ring;
676 struct nl_mmap_hdr *hdr;
677
678 spin_lock_bh(&sk->sk_receive_queue.lock);
679 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
680 if (hdr == NULL) {
681 spin_unlock_bh(&sk->sk_receive_queue.lock);
682 kfree_skb(skb);
Patrick McHardycd1df522013-04-17 06:47:05 +0000683 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000684 return;
685 }
686 netlink_increment_head(ring);
687 __skb_queue_tail(&sk->sk_receive_queue, skb);
688 spin_unlock_bh(&sk->sk_receive_queue.lock);
689
690 hdr->nm_len = skb->len;
691 hdr->nm_group = NETLINK_CB(skb).dst_group;
692 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
Nicolas Dichtel1bf93102013-04-24 10:36:23 +0200693 hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid);
694 hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid);
Patrick McHardyf9c22882013-04-17 06:47:04 +0000695 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
696}
697
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000698#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000699#define netlink_skb_is_mmaped(skb) false
Patrick McHardyf9c22882013-04-17 06:47:04 +0000700#define netlink_rx_is_mmaped(sk) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000701#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000702#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000703#define netlink_poll datagram_poll
Patrick McHardy5fd96122013-04-17 06:47:03 +0000704#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000705#endif /* CONFIG_NETLINK_MMAP */
706
Eric Dumazet658cb352012-04-22 21:30:21 +0000707static void netlink_destroy_callback(struct netlink_callback *cb)
708{
709 kfree_skb(cb->skb);
710 kfree(cb);
711}
712
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000713static void netlink_consume_callback(struct netlink_callback *cb)
714{
715 consume_skb(cb->skb);
716 kfree(cb);
717}
718
Patrick McHardycf0a0182013-04-17 06:47:00 +0000719static void netlink_skb_destructor(struct sk_buff *skb)
720{
Patrick McHardy9652e932013-04-17 06:47:02 +0000721#ifdef CONFIG_NETLINK_MMAP
722 struct nl_mmap_hdr *hdr;
723 struct netlink_ring *ring;
724 struct sock *sk;
725
726 /* If a packet from the kernel to userspace was freed because of an
727 * error without being delivered to userspace, the kernel must reset
728 * the status. In the direction userspace to kernel, the status is
729 * always reset here after the packet was processed and freed.
730 */
731 if (netlink_skb_is_mmaped(skb)) {
732 hdr = netlink_mmap_hdr(skb);
733 sk = NETLINK_CB(skb).sk;
734
Patrick McHardy5fd96122013-04-17 06:47:03 +0000735 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
736 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
737 ring = &nlk_sk(sk)->tx_ring;
738 } else {
739 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
740 hdr->nm_len = 0;
741 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
742 }
743 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000744 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000745
746 WARN_ON(atomic_read(&ring->pending) == 0);
747 atomic_dec(&ring->pending);
748 sock_put(sk);
749
Pablo Neira5e71d9d2013-06-03 09:28:43 +0000750 skb->head = NULL;
Patrick McHardy9652e932013-04-17 06:47:02 +0000751 }
752#endif
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +0000753 if (is_vmalloc_addr(skb->head)) {
754 vfree(skb->head);
755 skb->head = NULL;
756 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000757 if (skb->sk != NULL)
758 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000759}
760
761static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
762{
763 WARN_ON(skb->sk != NULL);
764 skb->sk = sk;
765 skb->destructor = netlink_skb_destructor;
766 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
767 sk_mem_charge(sk, skb->truesize);
768}
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770static void netlink_sock_destruct(struct sock *sk)
771{
Herbert Xu3f660d62007-05-03 03:17:14 -0700772 struct netlink_sock *nlk = nlk_sk(sk);
773
Herbert Xu3f660d62007-05-03 03:17:14 -0700774 if (nlk->cb) {
775 if (nlk->cb->done)
776 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000777
778 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700779 netlink_destroy_callback(nlk->cb);
780 }
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000783#ifdef CONFIG_NETLINK_MMAP
784 if (1) {
785 struct nl_mmap_req req;
786
787 memset(&req, 0, sizeof(req));
788 if (nlk->rx_ring.pg_vec)
789 netlink_set_ring(sk, &req, true, false);
790 memset(&req, 0, sizeof(req));
791 if (nlk->tx_ring.pg_vec)
792 netlink_set_ring(sk, &req, true, true);
793 }
794#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800797 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 return;
799 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700800
801 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
802 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
803 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800806/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
807 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
809 * this, _but_ remember, it adds useless work on UP machines.
810 */
811
Johannes Bergd136f1b2009-09-12 03:03:15 +0000812void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800813 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000815 might_sleep();
816
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700817 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 if (atomic_read(&nl_table_users)) {
820 DECLARE_WAITQUEUE(wait, current);
821
822 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800823 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 set_current_state(TASK_UNINTERRUPTIBLE);
825 if (atomic_read(&nl_table_users) == 0)
826 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700827 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700829 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 }
831
832 __set_current_state(TASK_RUNNING);
833 remove_wait_queue(&nl_table_wait, &wait);
834 }
835}
836
Johannes Bergd136f1b2009-09-12 03:03:15 +0000837void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800838 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700840 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 wake_up(&nl_table_wait);
842}
843
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800844static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845netlink_lock_table(void)
846{
847 /* read_lock() synchronizes us to netlink_table_grab */
848
849 read_lock(&nl_table_lock);
850 atomic_inc(&nl_table_users);
851 read_unlock(&nl_table_lock);
852}
853
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800854static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855netlink_unlock_table(void)
856{
857 if (atomic_dec_and_test(&nl_table_users))
858 wake_up(&nl_table_wait);
859}
860
Gao fengda12c902013-06-06 14:49:11 +0800861static bool netlink_compare(struct net *net, struct sock *sk)
862{
863 return net_eq(sock_net(sk), net);
864}
865
Eric W. Biederman15e47302012-09-07 20:12:54 +0000866static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
Gao fengda12c902013-06-06 14:49:11 +0800868 struct netlink_table *table = &nl_table[protocol];
869 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 struct hlist_head *head;
871 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000874 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800875 sk_for_each(sk, head) {
Gao fengda12c902013-06-06 14:49:11 +0800876 if (table->compare(net, sk) &&
877 (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 sock_hold(sk);
879 goto found;
880 }
881 }
882 sk = NULL;
883found:
884 read_unlock(&nl_table_lock);
885 return sk;
886}
887
Eric W. Biederman15e47302012-09-07 20:12:54 +0000888static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800891 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 else
893 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800894 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
895 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896}
897
Eric W. Biederman15e47302012-09-07 20:12:54 +0000898static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899{
900 if (size <= PAGE_SIZE)
901 kfree(table);
902 else
903 free_pages((unsigned long)table, get_order(size));
904}
905
Eric W. Biederman15e47302012-09-07 20:12:54 +0000906static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 unsigned int omask, mask, shift;
909 size_t osize, size;
910 struct hlist_head *otable, *table;
911 int i;
912
913 omask = mask = hash->mask;
914 osize = size = (mask + 1) * sizeof(*table);
915 shift = hash->shift;
916
917 if (grow) {
918 if (++shift > hash->max_shift)
919 return 0;
920 mask = mask * 2 + 1;
921 size *= 2;
922 }
923
Eric W. Biederman15e47302012-09-07 20:12:54 +0000924 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!table)
926 return 0;
927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 otable = hash->table;
929 hash->table = table;
930 hash->mask = mask;
931 hash->shift = shift;
932 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
933
934 for (i = 0; i <= omask; i++) {
935 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800936 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937
Sasha Levinb67bfe02013-02-27 17:06:00 -0800938 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000939 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Eric W. Biederman15e47302012-09-07 20:12:54 +0000942 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 hash->rehash_time = jiffies + 10 * 60 * HZ;
944 return 1;
945}
946
Eric W. Biederman15e47302012-09-07 20:12:54 +0000947static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
949 int avg = hash->entries >> hash->shift;
950
Eric W. Biederman15e47302012-09-07 20:12:54 +0000951 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 return 1;
953
954 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000955 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 return 1;
957 }
958
959 return 0;
960}
961
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800962static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Patrick McHardy4277a082006-03-20 18:52:01 -0800964static void
965netlink_update_listeners(struct sock *sk)
966{
967 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800968 unsigned long mask;
969 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000970 struct listeners *listeners;
971
972 listeners = nl_deref_protected(tbl->listeners);
973 if (!listeners)
974 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800975
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700976 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800977 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800978 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700979 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
980 mask |= nlk_sk(sk)->groups[i];
981 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000982 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800983 }
984 /* this function is only called with the netlink table "grabbed", which
985 * makes sure updates are visible before bind or setsockopt return. */
986}
987
Eric W. Biederman15e47302012-09-07 20:12:54 +0000988static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Gao fengda12c902013-06-06 14:49:11 +0800990 struct netlink_table *table = &nl_table[sk->sk_protocol];
991 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct hlist_head *head;
993 int err = -EADDRINUSE;
994 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 int len;
996
997 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000998 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -08001000 sk_for_each(osk, head) {
Gao fengda12c902013-06-06 14:49:11 +08001001 if (table->compare(net, osk) &&
1002 (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 break;
1004 len++;
1005 }
Sasha Levinb67bfe02013-02-27 17:06:00 -08001006 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 goto err;
1008
1009 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001010 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto err;
1012
1013 err = -ENOMEM;
1014 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
1015 goto err;
1016
Eric W. Biederman15e47302012-09-07 20:12:54 +00001017 if (len && nl_portid_hash_dilute(hash, len))
1018 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001020 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 sk_add_node(sk, head);
1022 err = 0;
1023
1024err:
1025 netlink_table_ungrab();
1026 return err;
1027}
1028
1029static void netlink_remove(struct sock *sk)
1030{
1031 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -07001032 if (sk_del_node_init(sk))
1033 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001034 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 __sk_del_bind_node(sk);
1036 netlink_table_ungrab();
1037}
1038
1039static struct proto netlink_proto = {
1040 .name = "NETLINK",
1041 .owner = THIS_MODULE,
1042 .obj_size = sizeof(struct netlink_sock),
1043};
1044
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -07001045static int __netlink_create(struct net *net, struct socket *sock,
1046 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct sock *sk;
1049 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -07001050
1051 sock->ops = &netlink_ops;
1052
Pavel Emelyanov6257ff22007-11-01 00:39:31 -07001053 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -07001054 if (!sk)
1055 return -ENOMEM;
1056
1057 sock_init_data(sock, sk);
1058
1059 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001060 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001061 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +00001062 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -07001063 nlk->cb_mutex = &nlk->cb_def_mutex;
1064 mutex_init(nlk->cb_mutex);
1065 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001066 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001067#ifdef CONFIG_NETLINK_MMAP
1068 mutex_init(&nlk->pg_vec_lock);
1069#endif
Patrick McHardyab33a172005-08-14 19:31:36 -07001070
1071 sk->sk_destruct = netlink_sock_destruct;
1072 sk->sk_protocol = protocol;
1073 return 0;
1074}
1075
Eric Paris3f378b62009-11-05 22:18:14 -08001076static int netlink_create(struct net *net, struct socket *sock, int protocol,
1077 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -07001078{
1079 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001080 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001081 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001082 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -07001083 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 sock->state = SS_UNCONNECTED;
1086
1087 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1088 return -ESOCKTNOSUPPORT;
1089
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001090 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 return -EPROTONOSUPPORT;
1092
Patrick McHardy77247bb2005-08-14 19:27:13 -07001093 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -07001094#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -07001095 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -07001096 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001097 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001098 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001099 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001100#endif
1101 if (nl_table[protocol].registered &&
1102 try_module_get(nl_table[protocol].module))
1103 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001104 else
1105 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001106 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001107 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001108 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001109
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001110 if (err < 0)
1111 goto out;
1112
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001113 err = __netlink_create(net, sock, cb_mutex, protocol);
1114 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -07001115 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
David S. Miller6f756a82008-11-23 17:34:03 -08001117 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001118 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -08001119 local_bh_enable();
1120
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001121 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001122 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001123 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001124out:
1125 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
Patrick McHardyab33a172005-08-14 19:31:36 -07001127out_module:
1128 module_put(module);
1129 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
1132static int netlink_release(struct socket *sock)
1133{
1134 struct sock *sk = sock->sk;
1135 struct netlink_sock *nlk;
1136
1137 if (!sk)
1138 return 0;
1139
1140 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001141 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 nlk = nlk_sk(sk);
1143
Herbert Xu3f660d62007-05-03 03:17:14 -07001144 /*
1145 * OK. Socket is unlinked, any packets that arrive now
1146 * will be purged.
1147 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 sock->sk = NULL;
1150 wake_up_interruptible_all(&nlk->wait);
1151
1152 skb_queue_purge(&sk->sk_write_queue);
1153
Eric W. Biederman15e47302012-09-07 20:12:54 +00001154 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001156 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001158 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 };
Alan Sterne041c682006-03-27 01:16:30 -08001160 atomic_notifier_call_chain(&netlink_chain,
1161 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001162 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001163
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001164 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001165
Patrick McHardy4277a082006-03-20 18:52:01 -08001166 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -07001167 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001168 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1169 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001170 struct listeners *old;
1171
1172 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1173 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1174 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001175 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001176 nl_table[sk->sk_protocol].bind = NULL;
Gao fengda12c902013-06-06 14:49:11 +08001177 nl_table[sk->sk_protocol].compare = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001178 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001179 nl_table[sk->sk_protocol].registered = 0;
1180 }
Eric Dumazet658cb352012-04-22 21:30:21 +00001181 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001182 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001183 }
Patrick McHardy4277a082006-03-20 18:52:01 -08001184 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -07001185
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001186 kfree(nlk->groups);
1187 nlk->groups = NULL;
1188
Eric Dumazet37558102008-11-24 14:05:22 -08001189 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001190 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001191 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 sock_put(sk);
1193 return 0;
1194}
1195
1196static int netlink_autobind(struct socket *sock)
1197{
1198 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001199 struct net *net = sock_net(sk);
Gao fengda12c902013-06-06 14:49:11 +08001200 struct netlink_table *table = &nl_table[sk->sk_protocol];
1201 struct nl_portid_hash *hash = &table->hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 struct hlist_head *head;
1203 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001204 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err;
1206 static s32 rover = -4097;
1207
1208retry:
1209 cond_resched();
1210 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001211 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001212 sk_for_each(osk, head) {
Gao fengda12c902013-06-06 14:49:11 +08001213 if (!table->compare(net, osk))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001214 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001215 if (nlk_sk(osk)->portid == portid) {
1216 /* Bind collision, search negative portid values. */
1217 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 if (rover > -4097)
1219 rover = -4097;
1220 netlink_table_ungrab();
1221 goto retry;
1222 }
1223 }
1224 netlink_table_ungrab();
1225
Eric W. Biederman15e47302012-09-07 20:12:54 +00001226 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (err == -EADDRINUSE)
1228 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001229
1230 /* If 2 threads race to autobind, that is fine. */
1231 if (err == -EBUSY)
1232 err = 0;
1233
1234 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001237static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001238{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001239 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001240 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001241}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001243static void
1244netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1245{
1246 struct netlink_sock *nlk = nlk_sk(sk);
1247
1248 if (nlk->subscriptions && !subscriptions)
1249 __sk_del_bind_node(sk);
1250 else if (!nlk->subscriptions && subscriptions)
1251 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1252 nlk->subscriptions = subscriptions;
1253}
1254
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001255static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001256{
1257 struct netlink_sock *nlk = nlk_sk(sk);
1258 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001259 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001260 int err = 0;
1261
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001262 netlink_table_grab();
1263
Patrick McHardy513c2502005-09-06 15:43:59 -07001264 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001265 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001266 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001267 goto out_unlock;
1268 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001269
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001270 if (nlk->ngroups >= groups)
1271 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001272
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001273 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1274 if (new_groups == NULL) {
1275 err = -ENOMEM;
1276 goto out_unlock;
1277 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001278 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001279 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1280
1281 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001282 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001283 out_unlock:
1284 netlink_table_ungrab();
1285 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001286}
1287
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001288static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1289 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
1291 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001292 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 struct netlink_sock *nlk = nlk_sk(sk);
1294 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1295 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001296
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001297 if (addr_len < sizeof(struct sockaddr_nl))
1298 return -EINVAL;
1299
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (nladdr->nl_family != AF_NETLINK)
1301 return -EINVAL;
1302
1303 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -07001304 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001305 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001306 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001307 err = netlink_realloc_groups(sk);
1308 if (err)
1309 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001310 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
Eric W. Biederman15e47302012-09-07 20:12:54 +00001312 if (nlk->portid) {
1313 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return -EINVAL;
1315 } else {
1316 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001317 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 netlink_autobind(sock);
1319 if (err)
1320 return err;
1321 }
1322
Patrick McHardy513c2502005-09-06 15:43:59 -07001323 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 return 0;
1325
1326 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001327 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001328 hweight32(nladdr->nl_groups) -
1329 hweight32(nlk->groups[0]));
1330 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001331 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 netlink_table_ungrab();
1333
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001334 if (nlk->netlink_bind && nlk->groups[0]) {
1335 int i;
1336
1337 for (i=0; i<nlk->ngroups; i++) {
1338 if (test_bit(i, nlk->groups))
1339 nlk->netlink_bind(i);
1340 }
1341 }
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return 0;
1344}
1345
1346static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1347 int alen, int flags)
1348{
1349 int err = 0;
1350 struct sock *sk = sock->sk;
1351 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001352 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
Changli Gao6503d962010-03-31 22:58:26 +00001354 if (alen < sizeof(addr->sa_family))
1355 return -EINVAL;
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 if (addr->sa_family == AF_UNSPEC) {
1358 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001359 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001360 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 return 0;
1362 }
1363 if (addr->sa_family != AF_NETLINK)
1364 return -EINVAL;
1365
1366 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001367 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 return -EPERM;
1369
Eric W. Biederman15e47302012-09-07 20:12:54 +00001370 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 err = netlink_autobind(sock);
1372
1373 if (err == 0) {
1374 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001375 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001376 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 }
1378
1379 return err;
1380}
1381
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001382static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1383 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 struct sock *sk = sock->sk;
1386 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001387 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001388
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 nladdr->nl_family = AF_NETLINK;
1390 nladdr->nl_pad = 0;
1391 *addr_len = sizeof(*nladdr);
1392
1393 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001394 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001395 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001397 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001398 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 }
1400 return 0;
1401}
1402
Eric W. Biederman15e47302012-09-07 20:12:54 +00001403static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct sock *sock;
1406 struct netlink_sock *nlk;
1407
Eric W. Biederman15e47302012-09-07 20:12:54 +00001408 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 if (!sock)
1410 return ERR_PTR(-ECONNREFUSED);
1411
1412 /* Don't bother queuing skb if kernel socket has no input function */
1413 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001414 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001415 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 sock_put(sock);
1417 return ERR_PTR(-ECONNREFUSED);
1418 }
1419 return sock;
1420}
1421
1422struct sock *netlink_getsockbyfilp(struct file *filp)
1423{
Al Viro496ad9a2013-01-23 17:07:38 -05001424 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 struct sock *sock;
1426
1427 if (!S_ISSOCK(inode->i_mode))
1428 return ERR_PTR(-ENOTSOCK);
1429
1430 sock = SOCKET_I(inode)->sk;
1431 if (sock->sk_family != AF_NETLINK)
1432 return ERR_PTR(-EINVAL);
1433
1434 sock_hold(sock);
1435 return sock;
1436}
1437
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001438static struct sk_buff *netlink_alloc_large_skb(unsigned int size)
1439{
1440 struct sk_buff *skb;
1441 void *data;
1442
1443 if (size <= NLMSG_GOODSIZE)
1444 return alloc_skb(size, GFP_KERNEL);
1445
1446 skb = alloc_skb_head(GFP_KERNEL);
1447 if (skb == NULL)
1448 return NULL;
1449
1450 data = vmalloc(size);
1451 if (data == NULL)
1452 goto err;
1453
1454 skb->head = data;
1455 skb->data = data;
1456 skb_reset_tail_pointer(skb);
1457 skb->end = skb->tail + size;
1458 skb->len = 0;
1459 skb->destructor = netlink_skb_destructor;
1460
1461 return skb;
1462err:
1463 kfree_skb(skb);
1464 return NULL;
1465}
1466
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467/*
1468 * Attach a skb to a netlink socket.
1469 * The caller must hold a reference to the destination socket. On error, the
1470 * reference is dropped. The skb is not send to the destination, just all
1471 * all error checks are performed and memory in the queue is reserved.
1472 * Return values:
1473 * < 0: error. skb freed, reference to sock dropped.
1474 * 0: continue
1475 * 1: repeat lookup - reference dropped while waiting for socket memory.
1476 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001477int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001478 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
1480 struct netlink_sock *nlk;
1481
1482 nlk = nlk_sk(sk);
1483
Patrick McHardy5fd96122013-04-17 06:47:03 +00001484 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1485 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1486 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001488 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001489 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 netlink_overrun(sk);
1491 sock_put(sk);
1492 kfree_skb(skb);
1493 return -EAGAIN;
1494 }
1495
1496 __set_current_state(TASK_INTERRUPTIBLE);
1497 add_wait_queue(&nlk->wait, &wait);
1498
1499 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001500 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001502 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
1504 __set_current_state(TASK_RUNNING);
1505 remove_wait_queue(&nlk->wait, &wait);
1506 sock_put(sk);
1507
1508 if (signal_pending(current)) {
1509 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001510 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
1512 return 1;
1513 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001514 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 return 0;
1516}
1517
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001518static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 int len = skb->len;
1521
Patrick McHardyf9c22882013-04-17 06:47:04 +00001522#ifdef CONFIG_NETLINK_MMAP
1523 if (netlink_skb_is_mmaped(skb))
1524 netlink_queue_mmaped_skb(sk, skb);
1525 else if (netlink_rx_is_mmaped(sk))
1526 netlink_ring_set_copied(sk, skb);
1527 else
1528#endif /* CONFIG_NETLINK_MMAP */
1529 skb_queue_tail(&sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001531 return len;
1532}
1533
1534int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1535{
1536 int len = __netlink_sendskb(sk, skb);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 sock_put(sk);
1539 return len;
1540}
1541
1542void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1543{
1544 kfree_skb(skb);
1545 sock_put(sk);
1546}
1547
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001548static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549{
1550 int delta;
1551
Patrick McHardy1298ca42013-04-17 06:46:59 +00001552 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001553 if (netlink_skb_is_mmaped(skb))
1554 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001556 delta = skb->end - skb->tail;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00001557 if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return skb;
1559
1560 if (skb_shared(skb)) {
1561 struct sk_buff *nskb = skb_clone(skb, allocation);
1562 if (!nskb)
1563 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001564 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 skb = nskb;
1566 }
1567
1568 if (!pskb_expand_head(skb, 0, -delta, allocation))
1569 skb->truesize -= delta;
1570
1571 return skb;
1572}
1573
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001574static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1575 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001576{
1577 int ret;
1578 struct netlink_sock *nlk = nlk_sk(sk);
1579
1580 ret = -ECONNREFUSED;
1581 if (nlk->netlink_rcv != NULL) {
1582 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001583 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001584 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001585 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001586 consume_skb(skb);
1587 } else {
1588 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001589 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001590 sock_put(sk);
1591 return ret;
1592}
1593
1594int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001595 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596{
1597 struct sock *sk;
1598 int err;
1599 long timeo;
1600
1601 skb = netlink_trim(skb, gfp_any());
1602
1603 timeo = sock_sndtimeo(ssk, nonblock);
1604retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001605 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 if (IS_ERR(sk)) {
1607 kfree_skb(skb);
1608 return PTR_ERR(sk);
1609 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001610 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001611 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001612
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001613 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001614 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001615 kfree_skb(skb);
1616 sock_put(sk);
1617 return err;
1618 }
1619
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001620 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 if (err == 1)
1622 goto retry;
1623 if (err)
1624 return err;
1625
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001626 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001628EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
Patrick McHardyf9c22882013-04-17 06:47:04 +00001630struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1631 u32 dst_portid, gfp_t gfp_mask)
1632{
1633#ifdef CONFIG_NETLINK_MMAP
1634 struct sock *sk = NULL;
1635 struct sk_buff *skb;
1636 struct netlink_ring *ring;
1637 struct nl_mmap_hdr *hdr;
1638 unsigned int maxlen;
1639
1640 sk = netlink_getsockbyportid(ssk, dst_portid);
1641 if (IS_ERR(sk))
1642 goto out;
1643
1644 ring = &nlk_sk(sk)->rx_ring;
1645 /* fast-path without atomic ops for common case: non-mmaped receiver */
1646 if (ring->pg_vec == NULL)
1647 goto out_put;
1648
1649 skb = alloc_skb_head(gfp_mask);
1650 if (skb == NULL)
1651 goto err1;
1652
1653 spin_lock_bh(&sk->sk_receive_queue.lock);
1654 /* check again under lock */
1655 if (ring->pg_vec == NULL)
1656 goto out_free;
1657
1658 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1659 if (maxlen < size)
1660 goto out_free;
1661
1662 netlink_forward_ring(ring);
1663 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1664 if (hdr == NULL)
1665 goto err2;
1666 netlink_ring_setup_skb(skb, sk, ring, hdr);
1667 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1668 atomic_inc(&ring->pending);
1669 netlink_increment_head(ring);
1670
1671 spin_unlock_bh(&sk->sk_receive_queue.lock);
1672 return skb;
1673
1674err2:
1675 kfree_skb(skb);
1676 spin_unlock_bh(&sk->sk_receive_queue.lock);
Patrick McHardycd1df522013-04-17 06:47:05 +00001677 netlink_overrun(sk);
Patrick McHardyf9c22882013-04-17 06:47:04 +00001678err1:
1679 sock_put(sk);
1680 return NULL;
1681
1682out_free:
1683 kfree_skb(skb);
1684 spin_unlock_bh(&sk->sk_receive_queue.lock);
1685out_put:
1686 sock_put(sk);
1687out:
1688#endif
1689 return alloc_skb(size, gfp_mask);
1690}
1691EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1692
Patrick McHardy4277a082006-03-20 18:52:01 -08001693int netlink_has_listeners(struct sock *sk, unsigned int group)
1694{
1695 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001696 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001697
Denis V. Lunevaed81562007-10-10 21:14:32 -07001698 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001699
1700 rcu_read_lock();
1701 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1702
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001703 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001704 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001705
1706 rcu_read_unlock();
1707
Patrick McHardy4277a082006-03-20 18:52:01 -08001708 return res;
1709}
1710EXPORT_SYMBOL_GPL(netlink_has_listeners);
1711
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001712static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 struct netlink_sock *nlk = nlk_sk(sk);
1715
1716 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001717 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001718 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001719 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001720 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722 return -1;
1723}
1724
1725struct netlink_broadcast_data {
1726 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001727 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001728 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 u32 group;
1730 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001731 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 int congested;
1733 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001734 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001736 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1737 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738};
1739
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001740static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 struct netlink_broadcast_data *p)
1742{
1743 struct netlink_sock *nlk = nlk_sk(sk);
1744 int val;
1745
1746 if (p->exclude_sk == sk)
1747 goto out;
1748
Eric W. Biederman15e47302012-09-07 20:12:54 +00001749 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001750 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 goto out;
1752
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001753 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001754 goto out;
1755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (p->failure) {
1757 netlink_overrun(sk);
1758 goto out;
1759 }
1760
1761 sock_hold(sk);
1762 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001763 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 p->skb2 = skb_clone(p->skb, p->allocation);
1765 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001766 p->skb2 = skb_get(p->skb);
1767 /*
1768 * skb ownership may have been set when
1769 * delivered to a previous socket.
1770 */
1771 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
1773 }
1774 if (p->skb2 == NULL) {
1775 netlink_overrun(sk);
1776 /* Clone failed. Notify ALL listeners. */
1777 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001778 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1779 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001780 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1781 kfree_skb(p->skb2);
1782 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001783 } else if (sk_filter(sk, p->skb2)) {
1784 kfree_skb(p->skb2);
1785 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1787 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001788 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1789 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 } else {
1791 p->congested |= val;
1792 p->delivered = 1;
1793 p->skb2 = NULL;
1794 }
1795 sock_put(sk);
1796
1797out:
1798 return 0;
1799}
1800
Eric W. Biederman15e47302012-09-07 20:12:54 +00001801int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001802 u32 group, gfp_t allocation,
1803 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1804 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001806 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 struct sock *sk;
1809
1810 skb = netlink_trim(skb, allocation);
1811
1812 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001813 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001814 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 info.group = group;
1816 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001817 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 info.congested = 0;
1819 info.delivered = 0;
1820 info.allocation = allocation;
1821 info.skb = skb;
1822 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001823 info.tx_filter = filter;
1824 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
1826 /* While we sleep in clone, do not allow to change socket list */
1827
1828 netlink_lock_table();
1829
Sasha Levinb67bfe02013-02-27 17:06:00 -08001830 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 do_one_broadcast(sk, &info);
1832
Neil Horman70d4bf62010-07-20 06:45:56 +00001833 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 netlink_unlock_table();
1836
Neil Horman70d4bf62010-07-20 06:45:56 +00001837 if (info.delivery_failure) {
1838 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001839 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001840 }
1841 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001842
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (info.delivered) {
1844 if (info.congested && (allocation & __GFP_WAIT))
1845 yield();
1846 return 0;
1847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 return -ESRCH;
1849}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001850EXPORT_SYMBOL(netlink_broadcast_filtered);
1851
Eric W. Biederman15e47302012-09-07 20:12:54 +00001852int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001853 u32 group, gfp_t allocation)
1854{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001855 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001856 NULL, NULL);
1857}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001858EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
1860struct netlink_set_err_data {
1861 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001862 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 u32 group;
1864 int code;
1865};
1866
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001867static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868{
1869 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001870 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
1872 if (sk == p->exclude_sk)
1873 goto out;
1874
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001875 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001876 goto out;
1877
Eric W. Biederman15e47302012-09-07 20:12:54 +00001878 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001879 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 goto out;
1881
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001882 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1883 ret = 1;
1884 goto out;
1885 }
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 sk->sk_err = p->code;
1888 sk->sk_error_report(sk);
1889out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001890 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001893/**
1894 * netlink_set_err - report error to broadcast listeners
1895 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001896 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001897 * @groups: the broadcast group that will notice the error
1898 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001899 *
1900 * This function returns the number of broadcast listeners that have set the
1901 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001902 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001903int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904{
1905 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001907 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
1909 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001910 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001912 /* sk->sk_err wants a positive error value */
1913 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 read_lock(&nl_table_lock);
1916
Sasha Levinb67bfe02013-02-27 17:06:00 -08001917 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001918 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001921 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001923EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Johannes Berg84659eb2007-07-18 15:47:05 -07001925/* must be called with netlink table grabbed */
1926static void netlink_update_socket_mc(struct netlink_sock *nlk,
1927 unsigned int group,
1928 int is_new)
1929{
1930 int old, new = !!is_new, subscriptions;
1931
1932 old = test_bit(group - 1, nlk->groups);
1933 subscriptions = nlk->subscriptions - old + new;
1934 if (new)
1935 __set_bit(group - 1, nlk->groups);
1936 else
1937 __clear_bit(group - 1, nlk->groups);
1938 netlink_update_subscriptions(&nlk->sk, subscriptions);
1939 netlink_update_listeners(&nlk->sk);
1940}
1941
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001942static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001943 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001944{
1945 struct sock *sk = sock->sk;
1946 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001947 unsigned int val = 0;
1948 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001949
1950 if (level != SOL_NETLINK)
1951 return -ENOPROTOOPT;
1952
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001953 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1954 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001955 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001956 return -EFAULT;
1957
1958 switch (optname) {
1959 case NETLINK_PKTINFO:
1960 if (val)
1961 nlk->flags |= NETLINK_RECV_PKTINFO;
1962 else
1963 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1964 err = 0;
1965 break;
1966 case NETLINK_ADD_MEMBERSHIP:
1967 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001968 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001969 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001970 err = netlink_realloc_groups(sk);
1971 if (err)
1972 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001973 if (!val || val - 1 >= nlk->ngroups)
1974 return -EINVAL;
1975 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001976 netlink_update_socket_mc(nlk, val,
1977 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001978 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001979
1980 if (nlk->netlink_bind)
1981 nlk->netlink_bind(val);
1982
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001983 err = 0;
1984 break;
1985 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001986 case NETLINK_BROADCAST_ERROR:
1987 if (val)
1988 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1989 else
1990 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1991 err = 0;
1992 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001993 case NETLINK_NO_ENOBUFS:
1994 if (val) {
1995 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001996 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001997 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001998 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001999 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00002000 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002001 err = 0;
2002 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002003#ifdef CONFIG_NETLINK_MMAP
2004 case NETLINK_RX_RING:
2005 case NETLINK_TX_RING: {
2006 struct nl_mmap_req req;
2007
2008 /* Rings might consume more memory than queue limits, require
2009 * CAP_NET_ADMIN.
2010 */
2011 if (!capable(CAP_NET_ADMIN))
2012 return -EPERM;
2013 if (optlen < sizeof(req))
2014 return -EINVAL;
2015 if (copy_from_user(&req, optval, sizeof(req)))
2016 return -EFAULT;
2017 err = netlink_set_ring(sk, &req, false,
2018 optname == NETLINK_TX_RING);
2019 break;
2020 }
2021#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002022 default:
2023 err = -ENOPROTOOPT;
2024 }
2025 return err;
2026}
2027
2028static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002029 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002030{
2031 struct sock *sk = sock->sk;
2032 struct netlink_sock *nlk = nlk_sk(sk);
2033 int len, val, err;
2034
2035 if (level != SOL_NETLINK)
2036 return -ENOPROTOOPT;
2037
2038 if (get_user(len, optlen))
2039 return -EFAULT;
2040 if (len < 0)
2041 return -EINVAL;
2042
2043 switch (optname) {
2044 case NETLINK_PKTINFO:
2045 if (len < sizeof(int))
2046 return -EINVAL;
2047 len = sizeof(int);
2048 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08002049 if (put_user(len, optlen) ||
2050 put_user(val, optval))
2051 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002052 err = 0;
2053 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00002054 case NETLINK_BROADCAST_ERROR:
2055 if (len < sizeof(int))
2056 return -EINVAL;
2057 len = sizeof(int);
2058 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
2059 if (put_user(len, optlen) ||
2060 put_user(val, optval))
2061 return -EFAULT;
2062 err = 0;
2063 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002064 case NETLINK_NO_ENOBUFS:
2065 if (len < sizeof(int))
2066 return -EINVAL;
2067 len = sizeof(int);
2068 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
2069 if (put_user(len, optlen) ||
2070 put_user(val, optval))
2071 return -EFAULT;
2072 err = 0;
2073 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002074 default:
2075 err = -ENOPROTOOPT;
2076 }
2077 return err;
2078}
2079
2080static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2081{
2082 struct nl_pktinfo info;
2083
2084 info.group = NETLINK_CB(skb).dst_group;
2085 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2086}
2087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2089 struct msghdr *msg, size_t len)
2090{
2091 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2092 struct sock *sk = sock->sk;
2093 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002094 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002095 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002096 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 struct sk_buff *skb;
2098 int err;
2099 struct scm_cookie scm;
2100
2101 if (msg->msg_flags&MSG_OOB)
2102 return -EOPNOTSUPP;
2103
Eric Dumazet16e57262011-09-19 05:52:27 +00002104 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00002106
Eric Dumazete0e3cea2012-08-21 06:21:17 +00002107 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 if (err < 0)
2109 return err;
2110
2111 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002112 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002114 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002115 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002116 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002117 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002118 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002119 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002120 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002122 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002123 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Eric W. Biederman15e47302012-09-07 20:12:54 +00002126 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 err = netlink_autobind(sock);
2128 if (err)
2129 goto out;
2130 }
2131
Patrick McHardy5fd96122013-04-17 06:47:03 +00002132 if (netlink_tx_is_mmaped(sk) &&
2133 msg->msg_iov->iov_base == NULL) {
2134 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2135 siocb);
2136 goto out;
2137 }
2138
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 err = -EMSGSIZE;
2140 if (len > sk->sk_sndbuf - 32)
2141 goto out;
2142 err = -ENOBUFS;
Pablo Neira Ayusoc05cdb12013-06-03 09:46:28 +00002143 skb = netlink_alloc_large_skb(len);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002144 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 goto out;
2146
Eric W. Biederman15e47302012-09-07 20:12:54 +00002147 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002148 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00002149 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002152 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 kfree_skb(skb);
2154 goto out;
2155 }
2156
2157 err = security_netlink_send(sk, skb);
2158 if (err) {
2159 kfree_skb(skb);
2160 goto out;
2161 }
2162
Patrick McHardyd629b832005-08-14 19:27:50 -07002163 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002165 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002167 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002170 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 return err;
2172}
2173
2174static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2175 struct msghdr *msg, size_t len,
2176 int flags)
2177{
2178 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2179 struct scm_cookie scm;
2180 struct sock *sk = sock->sk;
2181 struct netlink_sock *nlk = nlk_sk(sk);
2182 int noblock = flags&MSG_DONTWAIT;
2183 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00002184 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002185 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186
2187 if (flags&MSG_OOB)
2188 return -EOPNOTSUPP;
2189
2190 copied = 0;
2191
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002192 skb = skb_recv_datagram(sk, flags, noblock, &err);
2193 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 goto out;
2195
Johannes Berg68d6ac62010-08-15 21:20:44 +00002196 data_skb = skb;
2197
Johannes Berg1dacc762009-07-01 11:26:02 +00002198#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2199 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002200 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002201 * If this skb has a frag_list, then here that means that we
2202 * will have to use the frag_list skb's data for compat tasks
2203 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002204 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002205 * If we need to send the compat skb, assign it to the
2206 * 'data_skb' variable so that it will be used below for data
2207 * copying. We keep 'skb' for everything else, including
2208 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002209 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002210 if (flags & MSG_CMSG_COMPAT)
2211 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002212 }
2213#endif
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 msg->msg_namelen = 0;
2216
Johannes Berg68d6ac62010-08-15 21:20:44 +00002217 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (len < copied) {
2219 msg->msg_flags |= MSG_TRUNC;
2220 copied = len;
2221 }
2222
Johannes Berg68d6ac62010-08-15 21:20:44 +00002223 skb_reset_transport_header(data_skb);
2224 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
2226 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002227 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 addr->nl_family = AF_NETLINK;
2229 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002230 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002231 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 msg->msg_namelen = sizeof(*addr);
2233 }
2234
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002235 if (nlk->flags & NETLINK_RECV_PKTINFO)
2236 netlink_cmsg_recv_pktinfo(msg, skb);
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (NULL == siocb->scm) {
2239 memset(&scm, 0, sizeof(scm));
2240 siocb->scm = &scm;
2241 }
2242 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002243 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002244 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002245
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 skb_free_datagram(sk, skb);
2247
Andrey Vaginb44d2112011-02-21 02:40:47 +00002248 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
2249 ret = netlink_dump(sk);
2250 if (ret) {
2251 sk->sk_err = ret;
2252 sk->sk_error_report(sk);
2253 }
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257out:
2258 netlink_rcv_wake(sk);
2259 return err ? : copied;
2260}
2261
2262static void netlink_data_ready(struct sock *sk, int len)
2263{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002264 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265}
2266
2267/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002268 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 * complete set of kernel non-blocking support for message
2270 * queueing.
2271 */
2272
2273struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002274__netlink_kernel_create(struct net *net, int unit, struct module *module,
2275 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct socket *sock;
2278 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002279 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002280 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002281 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2282 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002284 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002286 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return NULL;
2288
2289 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2290 return NULL;
2291
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002292 /*
2293 * We have to just have a reference on the net from sk, but don't
2294 * get_net it. Besides, we cannot get and then put the net here.
2295 * So we create one inside init_net and the move it to net.
2296 */
2297
2298 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2299 goto out_sock_release_nosk;
2300
2301 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002302 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002303
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002304 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002305 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002306 else
2307 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002308
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002309 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002310 if (!listeners)
2311 goto out_sock_release;
2312
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002314 if (cfg && cfg->input)
2315 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002317 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002318 goto out_sock_release;
2319
2320 nlk = nlk_sk(sk);
2321 nlk->flags |= NETLINK_KERNEL_SOCKET;
2322
2323 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002324 if (!nl_table[unit].registered) {
2325 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002326 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002327 nl_table[unit].cb_mutex = cb_mutex;
2328 nl_table[unit].module = module;
Gao fengda12c902013-06-06 14:49:11 +08002329 nl_table[unit].compare = netlink_compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002330 if (cfg) {
2331 nl_table[unit].bind = cfg->bind;
2332 nl_table[unit].flags = cfg->flags;
Gao fengda12c902013-06-06 14:49:11 +08002333 if (cfg->compare)
2334 nl_table[unit].compare = cfg->compare;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002335 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002336 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002337 } else {
2338 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002339 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002340 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002341 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002342 return sk;
2343
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002344out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002345 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002346 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002347 return NULL;
2348
2349out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002350 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002351 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002353EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002354
2355void
2356netlink_kernel_release(struct sock *sk)
2357{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002358 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002359}
2360EXPORT_SYMBOL(netlink_kernel_release);
2361
Johannes Bergd136f1b2009-09-12 03:03:15 +00002362int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002363{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002364 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002365 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002366
2367 if (groups < 32)
2368 groups = 32;
2369
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002370 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002371 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2372 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002373 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002374 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002375 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2376 rcu_assign_pointer(tbl->listeners, new);
2377
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002378 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002379 }
2380 tbl->groups = groups;
2381
Johannes Bergd136f1b2009-09-12 03:03:15 +00002382 return 0;
2383}
2384
2385/**
2386 * netlink_change_ngroups - change number of multicast groups
2387 *
2388 * This changes the number of multicast groups that are available
2389 * on a certain netlink family. Note that it is not possible to
2390 * change the number of groups to below 32. Also note that it does
2391 * not implicitly call netlink_clear_multicast_users() when the
2392 * number of groups is reduced.
2393 *
2394 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2395 * @groups: The new number of groups.
2396 */
2397int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2398{
2399 int err;
2400
2401 netlink_table_grab();
2402 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002403 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002404
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002405 return err;
2406}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002407
Johannes Bergb8273572009-09-24 15:44:05 -07002408void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2409{
2410 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002411 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2412
Sasha Levinb67bfe02013-02-27 17:06:00 -08002413 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002414 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2415}
2416
Johannes Berg84659eb2007-07-18 15:47:05 -07002417/**
2418 * netlink_clear_multicast_users - kick off multicast listeners
2419 *
2420 * This function removes all listeners from the given group.
2421 * @ksk: The kernel netlink socket, as returned by
2422 * netlink_kernel_create().
2423 * @group: The multicast group to clear.
2424 */
2425void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2426{
Johannes Berg84659eb2007-07-18 15:47:05 -07002427 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07002428 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07002429 netlink_table_ungrab();
2430}
Johannes Berg84659eb2007-07-18 15:47:05 -07002431
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002432struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002433__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002434{
2435 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002436 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002437
2438 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
2439 nlh->nlmsg_type = type;
2440 nlh->nlmsg_len = size;
2441 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002442 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002443 nlh->nlmsg_seq = seq;
2444 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002445 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002446 return nlh;
2447}
2448EXPORT_SYMBOL(__nlmsg_put);
2449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450/*
2451 * It looks a bit ugly.
2452 * It would be better to create kernel thread.
2453 */
2454
2455static int netlink_dump(struct sock *sk)
2456{
2457 struct netlink_sock *nlk = nlk_sk(sk);
2458 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002459 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002461 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002462 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002464 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
2466 cb = nlk->cb;
2467 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002468 err = -EINVAL;
2469 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
2471
Greg Rosec7ac8672011-06-10 01:27:09 +00002472 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2473
Patrick McHardyf9c22882013-04-17 06:47:04 +00002474 if (!netlink_rx_is_mmaped(sk) &&
2475 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2476 goto errout_skb;
2477 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, GFP_KERNEL);
Greg Rosec7ac8672011-06-10 01:27:09 +00002478 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002479 goto errout_skb;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002480 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 len = cb->dump(skb, cb);
2483
2484 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002485 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002486
2487 if (sk_filter(sk, skb))
2488 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002489 else
2490 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 return 0;
2492 }
2493
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002494 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2495 if (!nlh)
2496 goto errout_skb;
2497
Johannes Berg670dc282011-06-20 13:40:46 +02002498 nl_dump_check_consistent(cb, nlh);
2499
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002500 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2501
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002502 if (sk_filter(sk, skb))
2503 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002504 else
2505 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506
Thomas Grafa8f74b22005-11-10 02:25:52 +01002507 if (cb->done)
2508 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002510 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
Gao feng6dc878a2012-10-04 20:15:48 +00002512 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00002513 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002515
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002516errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002517 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002518 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002519 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520}
2521
Gao feng6dc878a2012-10-04 20:15:48 +00002522int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2523 const struct nlmsghdr *nlh,
2524 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525{
2526 struct netlink_callback *cb;
2527 struct sock *sk;
2528 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002531 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (cb == NULL)
2533 return -ENOBUFS;
2534
Patrick McHardyf9c22882013-04-17 06:47:04 +00002535 /* Memory mapped dump requests need to be copied to avoid looping
2536 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2537 * a reference to the skb.
2538 */
2539 if (netlink_skb_is_mmaped(skb)) {
2540 skb = skb_copy(skb, GFP_KERNEL);
2541 if (skb == NULL) {
2542 kfree(cb);
2543 return -ENOBUFS;
2544 }
2545 } else
2546 atomic_inc(&skb->users);
2547
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002548 cb->dump = control->dump;
2549 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002551 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002552 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002553 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 cb->skb = skb;
2555
Eric W. Biederman15e47302012-09-07 20:12:54 +00002556 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 if (sk == NULL) {
2558 netlink_destroy_callback(cb);
2559 return -ECONNREFUSED;
2560 }
2561 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002562
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002563 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002564 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07002565 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002566 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00002568 ret = -EBUSY;
2569 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 }
Gao feng6dc878a2012-10-04 20:15:48 +00002571 /* add reference of module which cb->dump belongs to */
2572 if (!try_module_get(cb->module)) {
2573 mutex_unlock(nlk->cb_mutex);
2574 netlink_destroy_callback(cb);
2575 ret = -EPROTONOSUPPORT;
2576 goto out;
2577 }
2578
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002580 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
Andrey Vaginb44d2112011-02-21 02:40:47 +00002582 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002585
Andrey Vaginb44d2112011-02-21 02:40:47 +00002586 if (ret)
2587 return ret;
2588
Denis V. Lunev5c582982007-10-23 20:29:25 -07002589 /* We successfully started a dump, by returning -EINTR we
2590 * signal not to send ACK even if it was requested.
2591 */
2592 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002593}
Gao feng6dc878a2012-10-04 20:15:48 +00002594EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595
2596void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2597{
2598 struct sk_buff *skb;
2599 struct nlmsghdr *rep;
2600 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002601 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Thomas Graf339bf982006-11-10 14:10:15 -08002603 /* error messages get the original request appened */
2604 if (err)
2605 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Patrick McHardyf9c22882013-04-17 06:47:04 +00002607 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2608 NETLINK_CB(in_skb).portid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (!skb) {
2610 struct sock *sk;
2611
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002612 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002613 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002614 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 if (sk) {
2616 sk->sk_err = ENOBUFS;
2617 sk->sk_error_report(sk);
2618 sock_put(sk);
2619 }
2620 return;
2621 }
2622
Eric W. Biederman15e47302012-09-07 20:12:54 +00002623 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002624 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002625 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002627 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002628 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002630EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002632int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002633 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002634{
Thomas Graf82ace472005-11-10 02:25:53 +01002635 struct nlmsghdr *nlh;
2636 int err;
2637
2638 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002639 int msglen;
2640
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002641 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002642 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002643
Martin Murrayad8e4b72006-01-10 13:02:29 -08002644 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002645 return 0;
2646
Thomas Grafd35b6852007-03-22 23:28:46 -07002647 /* Only requests are handled by the kernel */
2648 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002649 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002650
Thomas Graf45e7ae72007-03-22 23:29:10 -07002651 /* Skip control messages */
2652 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002653 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002654
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002655 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002656 if (err == -EINTR)
2657 goto skip;
2658
2659ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002660 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002661 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002662
Denis V. Lunev5c582982007-10-23 20:29:25 -07002663skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002664 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002665 if (msglen > skb->len)
2666 msglen = skb->len;
2667 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002668 }
2669
2670 return 0;
2671}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002672EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002673
2674/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002675 * nlmsg_notify - send a notification netlink message
2676 * @sk: netlink socket to use
2677 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002678 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002679 * @group: destination multicast group or 0
2680 * @report: 1 to report back, 0 to disable
2681 * @flags: allocation flags
2682 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002683int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002684 unsigned int group, int report, gfp_t flags)
2685{
2686 int err = 0;
2687
2688 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002689 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002690
2691 if (report) {
2692 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002693 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002694 }
2695
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002696 /* errors reported via destination sk->sk_err, but propagate
2697 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002698 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002699 }
2700
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002701 if (report) {
2702 int err2;
2703
Eric W. Biederman15e47302012-09-07 20:12:54 +00002704 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002705 if (!err || err == -ESRCH)
2706 err = err2;
2707 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002708
2709 return err;
2710}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002711EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713#ifdef CONFIG_PROC_FS
2714struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002715 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 int link;
2717 int hash_idx;
2718};
2719
2720static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2721{
2722 struct nl_seq_iter *iter = seq->private;
2723 int i, j;
2724 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 loff_t off = 0;
2726
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002727 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002728 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
2730 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002731 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002732 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002733 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 if (off == pos) {
2735 iter->link = i;
2736 iter->hash_idx = j;
2737 return s;
2738 }
2739 ++off;
2740 }
2741 }
2742 }
2743 return NULL;
2744}
2745
2746static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002747 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
2749 read_lock(&nl_table_lock);
2750 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2751}
2752
2753static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2754{
2755 struct sock *s;
2756 struct nl_seq_iter *iter;
Gao fengda12c902013-06-06 14:49:11 +08002757 struct net *net;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 int i, j;
2759
2760 ++*pos;
2761
2762 if (v == SEQ_START_TOKEN)
2763 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002764
Gao fengda12c902013-06-06 14:49:11 +08002765 net = seq_file_net(seq);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002766 iter = seq->private;
2767 s = v;
2768 do {
2769 s = sk_next(s);
Gao fengda12c902013-06-06 14:49:11 +08002770 } while (s && !nl_table[s->sk_protocol].compare(net, s));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 if (s)
2772 return s;
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 i = iter->link;
2775 j = iter->hash_idx + 1;
2776
2777 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002778 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 for (; j <= hash->mask; j++) {
2781 s = sk_head(&hash->table[j]);
Gao fengda12c902013-06-06 14:49:11 +08002782
2783 while (s && !nl_table[s->sk_protocol].compare(net, s))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002784 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 if (s) {
2786 iter->link = i;
2787 iter->hash_idx = j;
2788 return s;
2789 }
2790 }
2791
2792 j = 0;
2793 } while (++i < MAX_LINKS);
2794
2795 return NULL;
2796}
2797
2798static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002799 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800{
2801 read_unlock(&nl_table_lock);
2802}
2803
2804
2805static int netlink_seq_show(struct seq_file *seq, void *v)
2806{
Eric Dumazet658cb352012-04-22 21:30:21 +00002807 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 seq_puts(seq,
2809 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002810 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002811 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 struct sock *s = v;
2813 struct netlink_sock *nlk = nlk_sk(s);
2814
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002815 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 s,
2817 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002818 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002819 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002820 sk_rmem_alloc_get(s),
2821 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002823 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002824 atomic_read(&s->sk_drops),
2825 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 );
2827
2828 }
2829 return 0;
2830}
2831
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002832static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 .start = netlink_seq_start,
2834 .next = netlink_seq_next,
2835 .stop = netlink_seq_stop,
2836 .show = netlink_seq_show,
2837};
2838
2839
2840static int netlink_seq_open(struct inode *inode, struct file *file)
2841{
Denis V. Luneve372c412007-11-19 22:31:54 -08002842 return seq_open_net(inode, file, &netlink_seq_ops,
2843 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002844}
2845
Arjan van de Venda7071d2007-02-12 00:55:36 -08002846static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 .owner = THIS_MODULE,
2848 .open = netlink_seq_open,
2849 .read = seq_read,
2850 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002851 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852};
2853
2854#endif
2855
2856int netlink_register_notifier(struct notifier_block *nb)
2857{
Alan Sterne041c682006-03-27 01:16:30 -08002858 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002860EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862int netlink_unregister_notifier(struct notifier_block *nb)
2863{
Alan Sterne041c682006-03-27 01:16:30 -08002864 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002866EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002867
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002868static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 .family = PF_NETLINK,
2870 .owner = THIS_MODULE,
2871 .release = netlink_release,
2872 .bind = netlink_bind,
2873 .connect = netlink_connect,
2874 .socketpair = sock_no_socketpair,
2875 .accept = sock_no_accept,
2876 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00002877 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 .ioctl = sock_no_ioctl,
2879 .listen = sock_no_listen,
2880 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002881 .setsockopt = netlink_setsockopt,
2882 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .sendmsg = netlink_sendmsg,
2884 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002885 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 .sendpage = sock_no_sendpage,
2887};
2888
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002889static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 .family = PF_NETLINK,
2891 .create = netlink_create,
2892 .owner = THIS_MODULE, /* for consistency 8) */
2893};
2894
Pavel Emelyanov46650792007-10-08 20:38:39 -07002895static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002896{
2897#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002898 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002899 return -ENOMEM;
2900#endif
2901 return 0;
2902}
2903
Pavel Emelyanov46650792007-10-08 20:38:39 -07002904static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002905{
2906#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002907 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002908#endif
2909}
2910
David S. Millerb963ea82010-08-30 19:08:01 -07002911static void __init netlink_add_usersock_entry(void)
2912{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002913 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002914 int groups = 32;
2915
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002916 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002917 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002918 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002919
2920 netlink_table_grab();
2921
2922 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002923 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002924 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2925 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002926 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002927
2928 netlink_table_ungrab();
2929}
2930
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002931static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002932 .init = netlink_net_init,
2933 .exit = netlink_net_exit,
2934};
2935
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936static int __init netlink_proto_init(void)
2937{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002939 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 unsigned int order;
2941 int err = proto_register(&netlink_proto, 0);
2942
2943 if (err != 0)
2944 goto out;
2945
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002946 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002948 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002949 if (!nl_table)
2950 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Jan Beulich44813742009-09-21 17:03:05 -07002952 if (totalram_pages >= (128 * 1024))
2953 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 else
Jan Beulich44813742009-09-21 17:03:05 -07002955 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002957 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2958 limit = (1UL << order) / sizeof(struct hlist_head);
2959 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
2961 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002962 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Eric W. Biederman15e47302012-09-07 20:12:54 +00002964 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (!hash->table) {
2966 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002967 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 1 * sizeof(*hash->table));
2969 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002970 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 hash->max_shift = order;
2973 hash->shift = 0;
2974 hash->mask = 0;
2975 hash->rehash_time = jiffies;
2976 }
2977
David S. Millerb963ea82010-08-30 19:08:01 -07002978 netlink_add_usersock_entry();
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002981 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002982 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 rtnetlink_init();
2984out:
2985 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002986panic:
2987 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988}
2989
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990core_initcall(netlink_proto_init);