blob: 90504a0e42ab2e0e2309af3e0c19f2a417705bcb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NETLINK Kernel-user communication protocol.
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +090011 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith
13 * added netlink_proto_exit
14 * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br>
15 * use nlk_sk, as sk->protinfo is on a diet 8)
Harald Welte4fdb3bb2005-08-09 19:40:55 -070016 * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org>
17 * - inc module use count of module that owns
18 * the kernel socket in case userspace opens
19 * socket of same protocol
20 * - remove all module support, since netlink is
21 * mandatory if CONFIG_NET=y these days
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/module.h>
25
Randy Dunlap4fc268d2006-01-11 12:17:47 -080026#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/kernel.h>
28#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/signal.h>
30#include <linux/sched.h>
31#include <linux/errno.h>
32#include <linux/string.h>
33#include <linux/stat.h>
34#include <linux/socket.h>
35#include <linux/un.h>
36#include <linux/fcntl.h>
37#include <linux/termios.h>
38#include <linux/sockios.h>
39#include <linux/net.h>
40#include <linux/fs.h>
41#include <linux/slab.h>
42#include <asm/uaccess.h>
43#include <linux/skbuff.h>
44#include <linux/netdevice.h>
45#include <linux/rtnetlink.h>
46#include <linux/proc_fs.h>
47#include <linux/seq_file.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <linux/notifier.h>
49#include <linux/security.h>
50#include <linux/jhash.h>
51#include <linux/jiffies.h>
52#include <linux/random.h>
53#include <linux/bitops.h>
54#include <linux/mm.h>
55#include <linux/types.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010056#include <linux/audit.h>
Patrick McHardyaf65bdf2007-04-20 14:14:21 -070057#include <linux/mutex.h>
Patrick McHardyccdfcc32013-04-17 06:47:01 +000058#include <linux/vmalloc.h>
Patrick McHardy9652e932013-04-17 06:47:02 +000059#include <asm/cacheflush.h>
Andrew Morton54e0f522005-04-30 07:07:04 +010060
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020061#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <net/sock.h>
63#include <net/scm.h>
Thomas Graf82ace472005-11-10 02:25:53 +010064#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Andrey Vagin0f29c762013-03-21 20:33:47 +040066#include "af_netlink.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Eric Dumazet5c398dc2010-10-24 04:27:10 +000068struct listeners {
69 struct rcu_head rcu;
70 unsigned long masks[0];
Johannes Berg6c04bb12009-07-10 09:51:32 +000071};
72
Patrick McHardycd967e02013-04-17 06:46:56 +000073/* state bits */
74#define NETLINK_CONGESTED 0x0
75
76/* flags */
Patrick McHardy77247bb2005-08-14 19:27:13 -070077#define NETLINK_KERNEL_SOCKET 0x1
Patrick McHardy9a4595b2005-08-15 12:32:15 -070078#define NETLINK_RECV_PKTINFO 0x2
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +000079#define NETLINK_BROADCAST_SEND_ERROR 0x4
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -070080#define NETLINK_RECV_NO_ENOBUFS 0x8
Patrick McHardy77247bb2005-08-14 19:27:13 -070081
David S. Miller035c4c12011-12-23 17:33:03 -050082static inline int netlink_is_kernel(struct sock *sk)
Denis V. Lunevaed81562007-10-10 21:14:32 -070083{
84 return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET;
85}
86
Andrey Vagin0f29c762013-03-21 20:33:47 +040087struct netlink_table *nl_table;
88EXPORT_SYMBOL_GPL(nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait);
91
92static int netlink_dump(struct sock *sk);
Patrick McHardy9652e932013-04-17 06:47:02 +000093static void netlink_skb_destructor(struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Andrey Vagin0f29c762013-03-21 20:33:47 +040095DEFINE_RWLOCK(nl_table_lock);
96EXPORT_SYMBOL_GPL(nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097static atomic_t nl_table_users = ATOMIC_INIT(0);
98
Eric Dumazet6d772ac2012-10-18 03:21:55 +000099#define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock));
100
Alan Sterne041c682006-03-27 01:16:30 -0800101static ATOMIC_NOTIFIER_HEAD(netlink_chain);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
stephen hemmingerb57ef81f2011-12-22 08:52:02 +0000103static inline u32 netlink_group_mask(u32 group)
Patrick McHardyd629b832005-08-14 19:27:50 -0700104{
105 return group ? 1 << (group - 1) : 0;
106}
107
Eric W. Biederman15e47302012-09-07 20:12:54 +0000108static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000110 return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000113#ifdef CONFIG_NETLINK_MMAP
Patrick McHardy9652e932013-04-17 06:47:02 +0000114static bool netlink_skb_is_mmaped(const struct sk_buff *skb)
115{
116 return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED;
117}
118
Patrick McHardy5fd96122013-04-17 06:47:03 +0000119static bool netlink_tx_is_mmaped(struct sock *sk)
120{
121 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
122}
123
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000124static __pure struct page *pgvec_to_page(const void *addr)
125{
126 if (is_vmalloc_addr(addr))
127 return vmalloc_to_page(addr);
128 else
129 return virt_to_page(addr);
130}
131
132static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
133{
134 unsigned int i;
135
136 for (i = 0; i < len; i++) {
137 if (pg_vec[i] != NULL) {
138 if (is_vmalloc_addr(pg_vec[i]))
139 vfree(pg_vec[i]);
140 else
141 free_pages((unsigned long)pg_vec[i], order);
142 }
143 }
144 kfree(pg_vec);
145}
146
147static void *alloc_one_pg_vec_page(unsigned long order)
148{
149 void *buffer;
150 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
151 __GFP_NOWARN | __GFP_NORETRY;
152
153 buffer = (void *)__get_free_pages(gfp_flags, order);
154 if (buffer != NULL)
155 return buffer;
156
157 buffer = vzalloc((1 << order) * PAGE_SIZE);
158 if (buffer != NULL)
159 return buffer;
160
161 gfp_flags &= ~__GFP_NORETRY;
162 return (void *)__get_free_pages(gfp_flags, order);
163}
164
165static void **alloc_pg_vec(struct netlink_sock *nlk,
166 struct nl_mmap_req *req, unsigned int order)
167{
168 unsigned int block_nr = req->nm_block_nr;
169 unsigned int i;
170 void **pg_vec, *ptr;
171
172 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
173 if (pg_vec == NULL)
174 return NULL;
175
176 for (i = 0; i < block_nr; i++) {
177 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
178 if (pg_vec[i] == NULL)
179 goto err1;
180 }
181
182 return pg_vec;
183err1:
184 free_pg_vec(pg_vec, order, block_nr);
185 return NULL;
186}
187
188static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
189 bool closing, bool tx_ring)
190{
191 struct netlink_sock *nlk = nlk_sk(sk);
192 struct netlink_ring *ring;
193 struct sk_buff_head *queue;
194 void **pg_vec = NULL;
195 unsigned int order = 0;
196 int err;
197
198 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
199 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
200
201 if (!closing) {
202 if (atomic_read(&nlk->mapped))
203 return -EBUSY;
204 if (atomic_read(&ring->pending))
205 return -EBUSY;
206 }
207
208 if (req->nm_block_nr) {
209 if (ring->pg_vec != NULL)
210 return -EBUSY;
211
212 if ((int)req->nm_block_size <= 0)
213 return -EINVAL;
214 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
215 return -EINVAL;
216 if (req->nm_frame_size < NL_MMAP_HDRLEN)
217 return -EINVAL;
218 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
219 return -EINVAL;
220
221 ring->frames_per_block = req->nm_block_size /
222 req->nm_frame_size;
223 if (ring->frames_per_block == 0)
224 return -EINVAL;
225 if (ring->frames_per_block * req->nm_block_nr !=
226 req->nm_frame_nr)
227 return -EINVAL;
228
229 order = get_order(req->nm_block_size);
230 pg_vec = alloc_pg_vec(nlk, req, order);
231 if (pg_vec == NULL)
232 return -ENOMEM;
233 } else {
234 if (req->nm_frame_nr)
235 return -EINVAL;
236 }
237
238 err = -EBUSY;
239 mutex_lock(&nlk->pg_vec_lock);
240 if (closing || atomic_read(&nlk->mapped) == 0) {
241 err = 0;
242 spin_lock_bh(&queue->lock);
243
244 ring->frame_max = req->nm_frame_nr - 1;
245 ring->head = 0;
246 ring->frame_size = req->nm_frame_size;
247 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
248
249 swap(ring->pg_vec_len, req->nm_block_nr);
250 swap(ring->pg_vec_order, order);
251 swap(ring->pg_vec, pg_vec);
252
253 __skb_queue_purge(queue);
254 spin_unlock_bh(&queue->lock);
255
256 WARN_ON(atomic_read(&nlk->mapped));
257 }
258 mutex_unlock(&nlk->pg_vec_lock);
259
260 if (pg_vec)
261 free_pg_vec(pg_vec, order, req->nm_block_nr);
262 return err;
263}
264
265static void netlink_mm_open(struct vm_area_struct *vma)
266{
267 struct file *file = vma->vm_file;
268 struct socket *sock = file->private_data;
269 struct sock *sk = sock->sk;
270
271 if (sk)
272 atomic_inc(&nlk_sk(sk)->mapped);
273}
274
275static void netlink_mm_close(struct vm_area_struct *vma)
276{
277 struct file *file = vma->vm_file;
278 struct socket *sock = file->private_data;
279 struct sock *sk = sock->sk;
280
281 if (sk)
282 atomic_dec(&nlk_sk(sk)->mapped);
283}
284
285static const struct vm_operations_struct netlink_mmap_ops = {
286 .open = netlink_mm_open,
287 .close = netlink_mm_close,
288};
289
290static int netlink_mmap(struct file *file, struct socket *sock,
291 struct vm_area_struct *vma)
292{
293 struct sock *sk = sock->sk;
294 struct netlink_sock *nlk = nlk_sk(sk);
295 struct netlink_ring *ring;
296 unsigned long start, size, expected;
297 unsigned int i;
298 int err = -EINVAL;
299
300 if (vma->vm_pgoff)
301 return -EINVAL;
302
303 mutex_lock(&nlk->pg_vec_lock);
304
305 expected = 0;
306 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
307 if (ring->pg_vec == NULL)
308 continue;
309 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
310 }
311
312 if (expected == 0)
313 goto out;
314
315 size = vma->vm_end - vma->vm_start;
316 if (size != expected)
317 goto out;
318
319 start = vma->vm_start;
320 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
321 if (ring->pg_vec == NULL)
322 continue;
323
324 for (i = 0; i < ring->pg_vec_len; i++) {
325 struct page *page;
326 void *kaddr = ring->pg_vec[i];
327 unsigned int pg_num;
328
329 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
330 page = pgvec_to_page(kaddr);
331 err = vm_insert_page(vma, start, page);
332 if (err < 0)
333 goto out;
334 start += PAGE_SIZE;
335 kaddr += PAGE_SIZE;
336 }
337 }
338 }
339
340 atomic_inc(&nlk->mapped);
341 vma->vm_ops = &netlink_mmap_ops;
342 err = 0;
343out:
344 mutex_unlock(&nlk->pg_vec_lock);
345 return 0;
346}
Patrick McHardy9652e932013-04-17 06:47:02 +0000347
348static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
349{
350#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
351 struct page *p_start, *p_end;
352
353 /* First page is flushed through netlink_{get,set}_status */
354 p_start = pgvec_to_page(hdr + PAGE_SIZE);
355 p_end = pgvec_to_page((void *)hdr + NL_MMAP_MSG_HDRLEN + hdr->nm_len - 1);
356 while (p_start <= p_end) {
357 flush_dcache_page(p_start);
358 p_start++;
359 }
360#endif
361}
362
363static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
364{
365 smp_rmb();
366 flush_dcache_page(pgvec_to_page(hdr));
367 return hdr->nm_status;
368}
369
370static void netlink_set_status(struct nl_mmap_hdr *hdr,
371 enum nl_mmap_status status)
372{
373 hdr->nm_status = status;
374 flush_dcache_page(pgvec_to_page(hdr));
375 smp_wmb();
376}
377
378static struct nl_mmap_hdr *
379__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
380{
381 unsigned int pg_vec_pos, frame_off;
382
383 pg_vec_pos = pos / ring->frames_per_block;
384 frame_off = pos % ring->frames_per_block;
385
386 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
387}
388
389static struct nl_mmap_hdr *
390netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
391 enum nl_mmap_status status)
392{
393 struct nl_mmap_hdr *hdr;
394
395 hdr = __netlink_lookup_frame(ring, pos);
396 if (netlink_get_status(hdr) != status)
397 return NULL;
398
399 return hdr;
400}
401
402static struct nl_mmap_hdr *
403netlink_current_frame(const struct netlink_ring *ring,
404 enum nl_mmap_status status)
405{
406 return netlink_lookup_frame(ring, ring->head, status);
407}
408
409static struct nl_mmap_hdr *
410netlink_previous_frame(const struct netlink_ring *ring,
411 enum nl_mmap_status status)
412{
413 unsigned int prev;
414
415 prev = ring->head ? ring->head - 1 : ring->frame_max;
416 return netlink_lookup_frame(ring, prev, status);
417}
418
419static void netlink_increment_head(struct netlink_ring *ring)
420{
421 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
422}
423
424static void netlink_forward_ring(struct netlink_ring *ring)
425{
426 unsigned int head = ring->head, pos = head;
427 const struct nl_mmap_hdr *hdr;
428
429 do {
430 hdr = __netlink_lookup_frame(ring, pos);
431 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
432 break;
433 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
434 break;
435 netlink_increment_head(ring);
436 } while (ring->head != head);
437}
438
439static unsigned int netlink_poll(struct file *file, struct socket *sock,
440 poll_table *wait)
441{
442 struct sock *sk = sock->sk;
443 struct netlink_sock *nlk = nlk_sk(sk);
444 unsigned int mask;
445
Patrick McHardy5fd96122013-04-17 06:47:03 +0000446 if (nlk->cb != NULL && nlk->rx_ring.pg_vec != NULL)
447 netlink_dump(sk);
448
Patrick McHardy9652e932013-04-17 06:47:02 +0000449 mask = datagram_poll(file, sock, wait);
450
451 spin_lock_bh(&sk->sk_receive_queue.lock);
452 if (nlk->rx_ring.pg_vec) {
453 netlink_forward_ring(&nlk->rx_ring);
454 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
455 mask |= POLLIN | POLLRDNORM;
456 }
457 spin_unlock_bh(&sk->sk_receive_queue.lock);
458
459 spin_lock_bh(&sk->sk_write_queue.lock);
460 if (nlk->tx_ring.pg_vec) {
461 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
462 mask |= POLLOUT | POLLWRNORM;
463 }
464 spin_unlock_bh(&sk->sk_write_queue.lock);
465
466 return mask;
467}
468
469static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
470{
471 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
472}
473
474static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
475 struct netlink_ring *ring,
476 struct nl_mmap_hdr *hdr)
477{
478 unsigned int size;
479 void *data;
480
481 size = ring->frame_size - NL_MMAP_HDRLEN;
482 data = (void *)hdr + NL_MMAP_HDRLEN;
483
484 skb->head = data;
485 skb->data = data;
486 skb_reset_tail_pointer(skb);
487 skb->end = skb->tail + size;
488 skb->len = 0;
489
490 skb->destructor = netlink_skb_destructor;
491 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
492 NETLINK_CB(skb).sk = sk;
493}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000494
495static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
496 u32 dst_portid, u32 dst_group,
497 struct sock_iocb *siocb)
498{
499 struct netlink_sock *nlk = nlk_sk(sk);
500 struct netlink_ring *ring;
501 struct nl_mmap_hdr *hdr;
502 struct sk_buff *skb;
503 unsigned int maxlen;
504 bool excl = true;
505 int err = 0, len = 0;
506
507 /* Netlink messages are validated by the receiver before processing.
508 * In order to avoid userspace changing the contents of the message
509 * after validation, the socket and the ring may only be used by a
510 * single process, otherwise we fall back to copying.
511 */
512 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
513 atomic_read(&nlk->mapped) > 1)
514 excl = false;
515
516 mutex_lock(&nlk->pg_vec_lock);
517
518 ring = &nlk->tx_ring;
519 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
520
521 do {
522 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
523 if (hdr == NULL) {
524 if (!(msg->msg_flags & MSG_DONTWAIT) &&
525 atomic_read(&nlk->tx_ring.pending))
526 schedule();
527 continue;
528 }
529 if (hdr->nm_len > maxlen) {
530 err = -EINVAL;
531 goto out;
532 }
533
534 netlink_frame_flush_dcache(hdr);
535
536 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
537 skb = alloc_skb_head(GFP_KERNEL);
538 if (skb == NULL) {
539 err = -ENOBUFS;
540 goto out;
541 }
542 sock_hold(sk);
543 netlink_ring_setup_skb(skb, sk, ring, hdr);
544 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
545 __skb_put(skb, hdr->nm_len);
546 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
547 atomic_inc(&ring->pending);
548 } else {
549 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
550 if (skb == NULL) {
551 err = -ENOBUFS;
552 goto out;
553 }
554 __skb_put(skb, hdr->nm_len);
555 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
556 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
557 }
558
559 netlink_increment_head(ring);
560
561 NETLINK_CB(skb).portid = nlk->portid;
562 NETLINK_CB(skb).dst_group = dst_group;
563 NETLINK_CB(skb).creds = siocb->scm->creds;
564
565 err = security_netlink_send(sk, skb);
566 if (err) {
567 kfree_skb(skb);
568 goto out;
569 }
570
571 if (unlikely(dst_group)) {
572 atomic_inc(&skb->users);
573 netlink_broadcast(sk, skb, dst_portid, dst_group,
574 GFP_KERNEL);
575 }
576 err = netlink_unicast(sk, skb, dst_portid,
577 msg->msg_flags & MSG_DONTWAIT);
578 if (err < 0)
579 goto out;
580 len += err;
581
582 } while (hdr != NULL ||
583 (!(msg->msg_flags & MSG_DONTWAIT) &&
584 atomic_read(&nlk->tx_ring.pending)));
585
586 if (len > 0)
587 err = len;
588out:
589 mutex_unlock(&nlk->pg_vec_lock);
590 return err;
591}
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000592#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000593#define netlink_skb_is_mmaped(skb) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000594#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000595#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000596#define netlink_poll datagram_poll
Patrick McHardy5fd96122013-04-17 06:47:03 +0000597#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000598#endif /* CONFIG_NETLINK_MMAP */
599
Eric Dumazet658cb352012-04-22 21:30:21 +0000600static void netlink_destroy_callback(struct netlink_callback *cb)
601{
602 kfree_skb(cb->skb);
603 kfree(cb);
604}
605
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000606static void netlink_consume_callback(struct netlink_callback *cb)
607{
608 consume_skb(cb->skb);
609 kfree(cb);
610}
611
Patrick McHardycf0a0182013-04-17 06:47:00 +0000612static void netlink_skb_destructor(struct sk_buff *skb)
613{
Patrick McHardy9652e932013-04-17 06:47:02 +0000614#ifdef CONFIG_NETLINK_MMAP
615 struct nl_mmap_hdr *hdr;
616 struct netlink_ring *ring;
617 struct sock *sk;
618
619 /* If a packet from the kernel to userspace was freed because of an
620 * error without being delivered to userspace, the kernel must reset
621 * the status. In the direction userspace to kernel, the status is
622 * always reset here after the packet was processed and freed.
623 */
624 if (netlink_skb_is_mmaped(skb)) {
625 hdr = netlink_mmap_hdr(skb);
626 sk = NETLINK_CB(skb).sk;
627
Patrick McHardy5fd96122013-04-17 06:47:03 +0000628 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
629 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
630 ring = &nlk_sk(sk)->tx_ring;
631 } else {
632 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
633 hdr->nm_len = 0;
634 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
635 }
636 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000637 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000638
639 WARN_ON(atomic_read(&ring->pending) == 0);
640 atomic_dec(&ring->pending);
641 sock_put(sk);
642
643 skb->data = NULL;
644 }
645#endif
646 if (skb->sk != NULL)
647 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000648}
649
650static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
651{
652 WARN_ON(skb->sk != NULL);
653 skb->sk = sk;
654 skb->destructor = netlink_skb_destructor;
655 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
656 sk_mem_charge(sk, skb->truesize);
657}
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659static void netlink_sock_destruct(struct sock *sk)
660{
Herbert Xu3f660d62007-05-03 03:17:14 -0700661 struct netlink_sock *nlk = nlk_sk(sk);
662
Herbert Xu3f660d62007-05-03 03:17:14 -0700663 if (nlk->cb) {
664 if (nlk->cb->done)
665 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000666
667 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700668 netlink_destroy_callback(nlk->cb);
669 }
670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000672#ifdef CONFIG_NETLINK_MMAP
673 if (1) {
674 struct nl_mmap_req req;
675
676 memset(&req, 0, sizeof(req));
677 if (nlk->rx_ring.pg_vec)
678 netlink_set_ring(sk, &req, true, false);
679 memset(&req, 0, sizeof(req));
680 if (nlk->tx_ring.pg_vec)
681 netlink_set_ring(sk, &req, true, true);
682 }
683#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800686 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 return;
688 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700689
690 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
691 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
692 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800695/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
696 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
698 * this, _but_ remember, it adds useless work on UP machines.
699 */
700
Johannes Bergd136f1b2009-09-12 03:03:15 +0000701void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800702 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000704 might_sleep();
705
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700706 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 if (atomic_read(&nl_table_users)) {
709 DECLARE_WAITQUEUE(wait, current);
710
711 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800712 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 set_current_state(TASK_UNINTERRUPTIBLE);
714 if (atomic_read(&nl_table_users) == 0)
715 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700716 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700718 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
720
721 __set_current_state(TASK_RUNNING);
722 remove_wait_queue(&nl_table_wait, &wait);
723 }
724}
725
Johannes Bergd136f1b2009-09-12 03:03:15 +0000726void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800727 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700729 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 wake_up(&nl_table_wait);
731}
732
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800733static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734netlink_lock_table(void)
735{
736 /* read_lock() synchronizes us to netlink_table_grab */
737
738 read_lock(&nl_table_lock);
739 atomic_inc(&nl_table_users);
740 read_unlock(&nl_table_lock);
741}
742
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800743static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744netlink_unlock_table(void)
745{
746 if (atomic_dec_and_test(&nl_table_users))
747 wake_up(&nl_table_wait);
748}
749
Eric W. Biederman15e47302012-09-07 20:12:54 +0000750static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000752 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct hlist_head *head;
754 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000757 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800758 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000759 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 sock_hold(sk);
761 goto found;
762 }
763 }
764 sk = NULL;
765found:
766 read_unlock(&nl_table_lock);
767 return sk;
768}
769
Eric W. Biederman15e47302012-09-07 20:12:54 +0000770static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800773 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 else
775 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800776 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
777 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Eric W. Biederman15e47302012-09-07 20:12:54 +0000780static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 if (size <= PAGE_SIZE)
783 kfree(table);
784 else
785 free_pages((unsigned long)table, get_order(size));
786}
787
Eric W. Biederman15e47302012-09-07 20:12:54 +0000788static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 unsigned int omask, mask, shift;
791 size_t osize, size;
792 struct hlist_head *otable, *table;
793 int i;
794
795 omask = mask = hash->mask;
796 osize = size = (mask + 1) * sizeof(*table);
797 shift = hash->shift;
798
799 if (grow) {
800 if (++shift > hash->max_shift)
801 return 0;
802 mask = mask * 2 + 1;
803 size *= 2;
804 }
805
Eric W. Biederman15e47302012-09-07 20:12:54 +0000806 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 if (!table)
808 return 0;
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 otable = hash->table;
811 hash->table = table;
812 hash->mask = mask;
813 hash->shift = shift;
814 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
815
816 for (i = 0; i <= omask; i++) {
817 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800818 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Sasha Levinb67bfe02013-02-27 17:06:00 -0800820 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 }
823
Eric W. Biederman15e47302012-09-07 20:12:54 +0000824 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 hash->rehash_time = jiffies + 10 * 60 * HZ;
826 return 1;
827}
828
Eric W. Biederman15e47302012-09-07 20:12:54 +0000829static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
831 int avg = hash->entries >> hash->shift;
832
Eric W. Biederman15e47302012-09-07 20:12:54 +0000833 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 return 1;
835
836 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000837 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 return 1;
839 }
840
841 return 0;
842}
843
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800844static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Patrick McHardy4277a082006-03-20 18:52:01 -0800846static void
847netlink_update_listeners(struct sock *sk)
848{
849 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800850 unsigned long mask;
851 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000852 struct listeners *listeners;
853
854 listeners = nl_deref_protected(tbl->listeners);
855 if (!listeners)
856 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800857
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700858 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800859 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800860 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700861 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
862 mask |= nlk_sk(sk)->groups[i];
863 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000864 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800865 }
866 /* this function is only called with the netlink table "grabbed", which
867 * makes sure updates are visible before bind or setsockopt return. */
868}
869
Eric W. Biederman15e47302012-09-07 20:12:54 +0000870static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000872 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 struct hlist_head *head;
874 int err = -EADDRINUSE;
875 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 int len;
877
878 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000879 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800881 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000882 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 break;
884 len++;
885 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800886 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 goto err;
888
889 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000890 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 goto err;
892
893 err = -ENOMEM;
894 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
895 goto err;
896
Eric W. Biederman15e47302012-09-07 20:12:54 +0000897 if (len && nl_portid_hash_dilute(hash, len))
898 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000900 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 sk_add_node(sk, head);
902 err = 0;
903
904err:
905 netlink_table_ungrab();
906 return err;
907}
908
909static void netlink_remove(struct sock *sk)
910{
911 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700912 if (sk_del_node_init(sk))
913 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700914 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 __sk_del_bind_node(sk);
916 netlink_table_ungrab();
917}
918
919static struct proto netlink_proto = {
920 .name = "NETLINK",
921 .owner = THIS_MODULE,
922 .obj_size = sizeof(struct netlink_sock),
923};
924
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700925static int __netlink_create(struct net *net, struct socket *sock,
926 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927{
928 struct sock *sk;
929 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700930
931 sock->ops = &netlink_ops;
932
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700933 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700934 if (!sk)
935 return -ENOMEM;
936
937 sock_init_data(sock, sk);
938
939 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000940 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700941 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000942 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700943 nlk->cb_mutex = &nlk->cb_def_mutex;
944 mutex_init(nlk->cb_mutex);
945 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700946 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000947#ifdef CONFIG_NETLINK_MMAP
948 mutex_init(&nlk->pg_vec_lock);
949#endif
Patrick McHardyab33a172005-08-14 19:31:36 -0700950
951 sk->sk_destruct = netlink_sock_destruct;
952 sk->sk_protocol = protocol;
953 return 0;
954}
955
Eric Paris3f378b62009-11-05 22:18:14 -0800956static int netlink_create(struct net *net, struct socket *sock, int protocol,
957 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -0700958{
959 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700960 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700961 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000962 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -0700963 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 sock->state = SS_UNCONNECTED;
966
967 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
968 return -ESOCKTNOSUPPORT;
969
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800970 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return -EPROTONOSUPPORT;
972
Patrick McHardy77247bb2005-08-14 19:27:13 -0700973 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -0700974#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -0700975 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -0700976 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700977 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -0700978 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700979 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700980#endif
981 if (nl_table[protocol].registered &&
982 try_module_get(nl_table[protocol].module))
983 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000984 else
985 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700986 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000987 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -0700988 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700989
Alexey Dobriyan974c37e2010-01-30 10:05:05 +0000990 if (err < 0)
991 goto out;
992
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800993 err = __netlink_create(net, sock, cb_mutex, protocol);
994 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -0700995 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
David S. Miller6f756a82008-11-23 17:34:03 -0800997 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -0800998 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -0800999 local_bh_enable();
1000
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001001 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001002 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001003 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001004out:
1005 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Patrick McHardyab33a172005-08-14 19:31:36 -07001007out_module:
1008 module_put(module);
1009 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010}
1011
1012static int netlink_release(struct socket *sock)
1013{
1014 struct sock *sk = sock->sk;
1015 struct netlink_sock *nlk;
1016
1017 if (!sk)
1018 return 0;
1019
1020 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001021 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 nlk = nlk_sk(sk);
1023
Herbert Xu3f660d62007-05-03 03:17:14 -07001024 /*
1025 * OK. Socket is unlinked, any packets that arrive now
1026 * will be purged.
1027 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 sock->sk = NULL;
1030 wake_up_interruptible_all(&nlk->wait);
1031
1032 skb_queue_purge(&sk->sk_write_queue);
1033
Eric W. Biederman15e47302012-09-07 20:12:54 +00001034 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001036 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001038 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 };
Alan Sterne041c682006-03-27 01:16:30 -08001040 atomic_notifier_call_chain(&netlink_chain,
1041 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001042 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001043
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001044 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001045
Patrick McHardy4277a082006-03-20 18:52:01 -08001046 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -07001047 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001048 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1049 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001050 struct listeners *old;
1051
1052 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1053 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1054 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001055 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001056 nl_table[sk->sk_protocol].bind = NULL;
1057 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001058 nl_table[sk->sk_protocol].registered = 0;
1059 }
Eric Dumazet658cb352012-04-22 21:30:21 +00001060 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001061 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001062 }
Patrick McHardy4277a082006-03-20 18:52:01 -08001063 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -07001064
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001065 kfree(nlk->groups);
1066 nlk->groups = NULL;
1067
Eric Dumazet37558102008-11-24 14:05:22 -08001068 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001069 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001070 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 sock_put(sk);
1072 return 0;
1073}
1074
1075static int netlink_autobind(struct socket *sock)
1076{
1077 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001078 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001079 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct hlist_head *head;
1081 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001082 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 int err;
1084 static s32 rover = -4097;
1085
1086retry:
1087 cond_resched();
1088 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001089 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001090 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001091 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001092 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001093 if (nlk_sk(osk)->portid == portid) {
1094 /* Bind collision, search negative portid values. */
1095 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (rover > -4097)
1097 rover = -4097;
1098 netlink_table_ungrab();
1099 goto retry;
1100 }
1101 }
1102 netlink_table_ungrab();
1103
Eric W. Biederman15e47302012-09-07 20:12:54 +00001104 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 if (err == -EADDRINUSE)
1106 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001107
1108 /* If 2 threads race to autobind, that is fine. */
1109 if (err == -EBUSY)
1110 err = 0;
1111
1112 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113}
1114
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001115static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001116{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001117 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001118 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001121static void
1122netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1123{
1124 struct netlink_sock *nlk = nlk_sk(sk);
1125
1126 if (nlk->subscriptions && !subscriptions)
1127 __sk_del_bind_node(sk);
1128 else if (!nlk->subscriptions && subscriptions)
1129 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1130 nlk->subscriptions = subscriptions;
1131}
1132
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001133static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001134{
1135 struct netlink_sock *nlk = nlk_sk(sk);
1136 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001137 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001138 int err = 0;
1139
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001140 netlink_table_grab();
1141
Patrick McHardy513c2502005-09-06 15:43:59 -07001142 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001143 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001144 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001145 goto out_unlock;
1146 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001147
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001148 if (nlk->ngroups >= groups)
1149 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001150
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001151 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1152 if (new_groups == NULL) {
1153 err = -ENOMEM;
1154 goto out_unlock;
1155 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001156 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001157 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1158
1159 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001160 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001161 out_unlock:
1162 netlink_table_ungrab();
1163 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001164}
1165
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001166static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1167 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168{
1169 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001170 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 struct netlink_sock *nlk = nlk_sk(sk);
1172 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1173 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001174
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001175 if (addr_len < sizeof(struct sockaddr_nl))
1176 return -EINVAL;
1177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 if (nladdr->nl_family != AF_NETLINK)
1179 return -EINVAL;
1180
1181 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -07001182 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001183 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001184 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001185 err = netlink_realloc_groups(sk);
1186 if (err)
1187 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001188 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
Eric W. Biederman15e47302012-09-07 20:12:54 +00001190 if (nlk->portid) {
1191 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 return -EINVAL;
1193 } else {
1194 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001195 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 netlink_autobind(sock);
1197 if (err)
1198 return err;
1199 }
1200
Patrick McHardy513c2502005-09-06 15:43:59 -07001201 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 return 0;
1203
1204 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001205 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001206 hweight32(nladdr->nl_groups) -
1207 hweight32(nlk->groups[0]));
1208 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001209 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 netlink_table_ungrab();
1211
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001212 if (nlk->netlink_bind && nlk->groups[0]) {
1213 int i;
1214
1215 for (i=0; i<nlk->ngroups; i++) {
1216 if (test_bit(i, nlk->groups))
1217 nlk->netlink_bind(i);
1218 }
1219 }
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return 0;
1222}
1223
1224static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1225 int alen, int flags)
1226{
1227 int err = 0;
1228 struct sock *sk = sock->sk;
1229 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001230 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Changli Gao6503d962010-03-31 22:58:26 +00001232 if (alen < sizeof(addr->sa_family))
1233 return -EINVAL;
1234
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 if (addr->sa_family == AF_UNSPEC) {
1236 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001237 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001238 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240 }
1241 if (addr->sa_family != AF_NETLINK)
1242 return -EINVAL;
1243
1244 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001245 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return -EPERM;
1247
Eric W. Biederman15e47302012-09-07 20:12:54 +00001248 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 err = netlink_autobind(sock);
1250
1251 if (err == 0) {
1252 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001253 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001254 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
1257 return err;
1258}
1259
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001260static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1261 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262{
1263 struct sock *sk = sock->sk;
1264 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001265 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 nladdr->nl_family = AF_NETLINK;
1268 nladdr->nl_pad = 0;
1269 *addr_len = sizeof(*nladdr);
1270
1271 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001272 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001273 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001275 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001276 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 }
1278 return 0;
1279}
1280
1281static void netlink_overrun(struct sock *sk)
1282{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001283 struct netlink_sock *nlk = nlk_sk(sk);
1284
1285 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
Patrick McHardycd967e02013-04-17 06:46:56 +00001286 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001287 sk->sk_err = ENOBUFS;
1288 sk->sk_error_report(sk);
1289 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001291 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292}
1293
Eric W. Biederman15e47302012-09-07 20:12:54 +00001294static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 struct sock *sock;
1297 struct netlink_sock *nlk;
1298
Eric W. Biederman15e47302012-09-07 20:12:54 +00001299 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 if (!sock)
1301 return ERR_PTR(-ECONNREFUSED);
1302
1303 /* Don't bother queuing skb if kernel socket has no input function */
1304 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001305 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001306 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 sock_put(sock);
1308 return ERR_PTR(-ECONNREFUSED);
1309 }
1310 return sock;
1311}
1312
1313struct sock *netlink_getsockbyfilp(struct file *filp)
1314{
Al Viro496ad9a2013-01-23 17:07:38 -05001315 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 struct sock *sock;
1317
1318 if (!S_ISSOCK(inode->i_mode))
1319 return ERR_PTR(-ENOTSOCK);
1320
1321 sock = SOCKET_I(inode)->sk;
1322 if (sock->sk_family != AF_NETLINK)
1323 return ERR_PTR(-EINVAL);
1324
1325 sock_hold(sock);
1326 return sock;
1327}
1328
1329/*
1330 * Attach a skb to a netlink socket.
1331 * The caller must hold a reference to the destination socket. On error, the
1332 * reference is dropped. The skb is not send to the destination, just all
1333 * all error checks are performed and memory in the queue is reserved.
1334 * Return values:
1335 * < 0: error. skb freed, reference to sock dropped.
1336 * 0: continue
1337 * 1: repeat lookup - reference dropped while waiting for socket memory.
1338 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001339int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001340 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341{
1342 struct netlink_sock *nlk;
1343
1344 nlk = nlk_sk(sk);
1345
Patrick McHardy5fd96122013-04-17 06:47:03 +00001346 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1347 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1348 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001350 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001351 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 netlink_overrun(sk);
1353 sock_put(sk);
1354 kfree_skb(skb);
1355 return -EAGAIN;
1356 }
1357
1358 __set_current_state(TASK_INTERRUPTIBLE);
1359 add_wait_queue(&nlk->wait, &wait);
1360
1361 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001362 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001364 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 __set_current_state(TASK_RUNNING);
1367 remove_wait_queue(&nlk->wait, &wait);
1368 sock_put(sk);
1369
1370 if (signal_pending(current)) {
1371 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001372 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 }
1374 return 1;
1375 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001376 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 return 0;
1378}
1379
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001380static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 int len = skb->len;
1383
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 skb_queue_tail(&sk->sk_receive_queue, skb);
1385 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001386 return len;
1387}
1388
1389int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1390{
1391 int len = __netlink_sendskb(sk, skb);
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 sock_put(sk);
1394 return len;
1395}
1396
1397void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1398{
1399 kfree_skb(skb);
1400 sock_put(sk);
1401}
1402
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001403static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404{
1405 int delta;
1406
Patrick McHardy1298ca42013-04-17 06:46:59 +00001407 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001408 if (netlink_skb_is_mmaped(skb))
1409 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001411 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 if (delta * 2 < skb->truesize)
1413 return skb;
1414
1415 if (skb_shared(skb)) {
1416 struct sk_buff *nskb = skb_clone(skb, allocation);
1417 if (!nskb)
1418 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001419 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 skb = nskb;
1421 }
1422
1423 if (!pskb_expand_head(skb, 0, -delta, allocation))
1424 skb->truesize -= delta;
1425
1426 return skb;
1427}
1428
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001429static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001430{
1431 struct netlink_sock *nlk = nlk_sk(sk);
1432
1433 if (skb_queue_empty(&sk->sk_receive_queue))
Patrick McHardycd967e02013-04-17 06:46:56 +00001434 clear_bit(NETLINK_CONGESTED, &nlk->state);
1435 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001436 wake_up_interruptible(&nlk->wait);
1437}
1438
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001439static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1440 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001441{
1442 int ret;
1443 struct netlink_sock *nlk = nlk_sk(sk);
1444
1445 ret = -ECONNREFUSED;
1446 if (nlk->netlink_rcv != NULL) {
1447 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001448 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001449 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001450 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001451 consume_skb(skb);
1452 } else {
1453 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001454 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001455 sock_put(sk);
1456 return ret;
1457}
1458
1459int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001460 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
1462 struct sock *sk;
1463 int err;
1464 long timeo;
1465
1466 skb = netlink_trim(skb, gfp_any());
1467
1468 timeo = sock_sndtimeo(ssk, nonblock);
1469retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001470 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 if (IS_ERR(sk)) {
1472 kfree_skb(skb);
1473 return PTR_ERR(sk);
1474 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001475 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001476 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001477
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001478 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001479 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001480 kfree_skb(skb);
1481 sock_put(sk);
1482 return err;
1483 }
1484
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001485 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (err == 1)
1487 goto retry;
1488 if (err)
1489 return err;
1490
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001491 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001493EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494
Patrick McHardy4277a082006-03-20 18:52:01 -08001495int netlink_has_listeners(struct sock *sk, unsigned int group)
1496{
1497 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001498 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001499
Denis V. Lunevaed81562007-10-10 21:14:32 -07001500 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001501
1502 rcu_read_lock();
1503 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1504
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001505 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001506 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001507
1508 rcu_read_unlock();
1509
Patrick McHardy4277a082006-03-20 18:52:01 -08001510 return res;
1511}
1512EXPORT_SYMBOL_GPL(netlink_has_listeners);
1513
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001514static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515{
1516 struct netlink_sock *nlk = nlk_sk(sk);
1517
1518 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001519 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001520 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001521 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001522 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 }
1524 return -1;
1525}
1526
1527struct netlink_broadcast_data {
1528 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001529 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001530 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 u32 group;
1532 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001533 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 int congested;
1535 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001536 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001538 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1539 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540};
1541
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001542static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 struct netlink_broadcast_data *p)
1544{
1545 struct netlink_sock *nlk = nlk_sk(sk);
1546 int val;
1547
1548 if (p->exclude_sk == sk)
1549 goto out;
1550
Eric W. Biederman15e47302012-09-07 20:12:54 +00001551 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001552 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 goto out;
1554
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001555 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001556 goto out;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 if (p->failure) {
1559 netlink_overrun(sk);
1560 goto out;
1561 }
1562
1563 sock_hold(sk);
1564 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001565 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 p->skb2 = skb_clone(p->skb, p->allocation);
1567 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001568 p->skb2 = skb_get(p->skb);
1569 /*
1570 * skb ownership may have been set when
1571 * delivered to a previous socket.
1572 */
1573 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575 }
1576 if (p->skb2 == NULL) {
1577 netlink_overrun(sk);
1578 /* Clone failed. Notify ALL listeners. */
1579 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001580 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1581 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001582 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1583 kfree_skb(p->skb2);
1584 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001585 } else if (sk_filter(sk, p->skb2)) {
1586 kfree_skb(p->skb2);
1587 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1589 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001590 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1591 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 } else {
1593 p->congested |= val;
1594 p->delivered = 1;
1595 p->skb2 = NULL;
1596 }
1597 sock_put(sk);
1598
1599out:
1600 return 0;
1601}
1602
Eric W. Biederman15e47302012-09-07 20:12:54 +00001603int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001604 u32 group, gfp_t allocation,
1605 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1606 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001608 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 struct sock *sk;
1611
1612 skb = netlink_trim(skb, allocation);
1613
1614 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001615 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001616 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 info.group = group;
1618 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001619 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 info.congested = 0;
1621 info.delivered = 0;
1622 info.allocation = allocation;
1623 info.skb = skb;
1624 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001625 info.tx_filter = filter;
1626 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627
1628 /* While we sleep in clone, do not allow to change socket list */
1629
1630 netlink_lock_table();
1631
Sasha Levinb67bfe02013-02-27 17:06:00 -08001632 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 do_one_broadcast(sk, &info);
1634
Neil Horman70d4bf62010-07-20 06:45:56 +00001635 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 netlink_unlock_table();
1638
Neil Horman70d4bf62010-07-20 06:45:56 +00001639 if (info.delivery_failure) {
1640 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001641 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001642 }
1643 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (info.delivered) {
1646 if (info.congested && (allocation & __GFP_WAIT))
1647 yield();
1648 return 0;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -ESRCH;
1651}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001652EXPORT_SYMBOL(netlink_broadcast_filtered);
1653
Eric W. Biederman15e47302012-09-07 20:12:54 +00001654int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001655 u32 group, gfp_t allocation)
1656{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001657 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001658 NULL, NULL);
1659}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001660EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662struct netlink_set_err_data {
1663 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001664 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 u32 group;
1666 int code;
1667};
1668
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001669static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
1671 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001672 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 if (sk == p->exclude_sk)
1675 goto out;
1676
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001677 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001678 goto out;
1679
Eric W. Biederman15e47302012-09-07 20:12:54 +00001680 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001681 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 goto out;
1683
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001684 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1685 ret = 1;
1686 goto out;
1687 }
1688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 sk->sk_err = p->code;
1690 sk->sk_error_report(sk);
1691out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001692 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693}
1694
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001695/**
1696 * netlink_set_err - report error to broadcast listeners
1697 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001698 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001699 * @groups: the broadcast group that will notice the error
1700 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001701 *
1702 * This function returns the number of broadcast listeners that have set the
1703 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001704 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001705int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
1707 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001709 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001712 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001714 /* sk->sk_err wants a positive error value */
1715 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
1717 read_lock(&nl_table_lock);
1718
Sasha Levinb67bfe02013-02-27 17:06:00 -08001719 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001720 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
1722 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001725EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
Johannes Berg84659eb2007-07-18 15:47:05 -07001727/* must be called with netlink table grabbed */
1728static void netlink_update_socket_mc(struct netlink_sock *nlk,
1729 unsigned int group,
1730 int is_new)
1731{
1732 int old, new = !!is_new, subscriptions;
1733
1734 old = test_bit(group - 1, nlk->groups);
1735 subscriptions = nlk->subscriptions - old + new;
1736 if (new)
1737 __set_bit(group - 1, nlk->groups);
1738 else
1739 __clear_bit(group - 1, nlk->groups);
1740 netlink_update_subscriptions(&nlk->sk, subscriptions);
1741 netlink_update_listeners(&nlk->sk);
1742}
1743
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001744static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001745 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001746{
1747 struct sock *sk = sock->sk;
1748 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001749 unsigned int val = 0;
1750 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001751
1752 if (level != SOL_NETLINK)
1753 return -ENOPROTOOPT;
1754
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001755 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1756 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001757 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001758 return -EFAULT;
1759
1760 switch (optname) {
1761 case NETLINK_PKTINFO:
1762 if (val)
1763 nlk->flags |= NETLINK_RECV_PKTINFO;
1764 else
1765 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1766 err = 0;
1767 break;
1768 case NETLINK_ADD_MEMBERSHIP:
1769 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001770 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001771 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001772 err = netlink_realloc_groups(sk);
1773 if (err)
1774 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001775 if (!val || val - 1 >= nlk->ngroups)
1776 return -EINVAL;
1777 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001778 netlink_update_socket_mc(nlk, val,
1779 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001780 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001781
1782 if (nlk->netlink_bind)
1783 nlk->netlink_bind(val);
1784
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001785 err = 0;
1786 break;
1787 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001788 case NETLINK_BROADCAST_ERROR:
1789 if (val)
1790 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1791 else
1792 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1793 err = 0;
1794 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001795 case NETLINK_NO_ENOBUFS:
1796 if (val) {
1797 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001798 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001799 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001800 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001801 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001802 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001803 err = 0;
1804 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001805#ifdef CONFIG_NETLINK_MMAP
1806 case NETLINK_RX_RING:
1807 case NETLINK_TX_RING: {
1808 struct nl_mmap_req req;
1809
1810 /* Rings might consume more memory than queue limits, require
1811 * CAP_NET_ADMIN.
1812 */
1813 if (!capable(CAP_NET_ADMIN))
1814 return -EPERM;
1815 if (optlen < sizeof(req))
1816 return -EINVAL;
1817 if (copy_from_user(&req, optval, sizeof(req)))
1818 return -EFAULT;
1819 err = netlink_set_ring(sk, &req, false,
1820 optname == NETLINK_TX_RING);
1821 break;
1822 }
1823#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001824 default:
1825 err = -ENOPROTOOPT;
1826 }
1827 return err;
1828}
1829
1830static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001831 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001832{
1833 struct sock *sk = sock->sk;
1834 struct netlink_sock *nlk = nlk_sk(sk);
1835 int len, val, err;
1836
1837 if (level != SOL_NETLINK)
1838 return -ENOPROTOOPT;
1839
1840 if (get_user(len, optlen))
1841 return -EFAULT;
1842 if (len < 0)
1843 return -EINVAL;
1844
1845 switch (optname) {
1846 case NETLINK_PKTINFO:
1847 if (len < sizeof(int))
1848 return -EINVAL;
1849 len = sizeof(int);
1850 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001851 if (put_user(len, optlen) ||
1852 put_user(val, optval))
1853 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001854 err = 0;
1855 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001856 case NETLINK_BROADCAST_ERROR:
1857 if (len < sizeof(int))
1858 return -EINVAL;
1859 len = sizeof(int);
1860 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1861 if (put_user(len, optlen) ||
1862 put_user(val, optval))
1863 return -EFAULT;
1864 err = 0;
1865 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001866 case NETLINK_NO_ENOBUFS:
1867 if (len < sizeof(int))
1868 return -EINVAL;
1869 len = sizeof(int);
1870 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1871 if (put_user(len, optlen) ||
1872 put_user(val, optval))
1873 return -EFAULT;
1874 err = 0;
1875 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001876 default:
1877 err = -ENOPROTOOPT;
1878 }
1879 return err;
1880}
1881
1882static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
1883{
1884 struct nl_pktinfo info;
1885
1886 info.group = NETLINK_CB(skb).dst_group;
1887 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
1888}
1889
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
1891 struct msghdr *msg, size_t len)
1892{
1893 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1894 struct sock *sk = sock->sk;
1895 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001896 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001897 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001898 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 struct sk_buff *skb;
1900 int err;
1901 struct scm_cookie scm;
1902
1903 if (msg->msg_flags&MSG_OOB)
1904 return -EOPNOTSUPP;
1905
Eric Dumazet16e57262011-09-19 05:52:27 +00001906 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00001908
Eric Dumazete0e3cea2012-08-21 06:21:17 +00001909 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (err < 0)
1911 return err;
1912
1913 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001914 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001916 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001917 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001918 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001919 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001920 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001921 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001922 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001924 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001925 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927
Eric W. Biederman15e47302012-09-07 20:12:54 +00001928 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 err = netlink_autobind(sock);
1930 if (err)
1931 goto out;
1932 }
1933
Patrick McHardy5fd96122013-04-17 06:47:03 +00001934 if (netlink_tx_is_mmaped(sk) &&
1935 msg->msg_iov->iov_base == NULL) {
1936 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
1937 siocb);
1938 goto out;
1939 }
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 err = -EMSGSIZE;
1942 if (len > sk->sk_sndbuf - 32)
1943 goto out;
1944 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08001945 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001946 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 goto out;
1948
Eric W. Biederman15e47302012-09-07 20:12:54 +00001949 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001950 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00001951 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001954 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 kfree_skb(skb);
1956 goto out;
1957 }
1958
1959 err = security_netlink_send(sk, skb);
1960 if (err) {
1961 kfree_skb(skb);
1962 goto out;
1963 }
1964
Patrick McHardyd629b832005-08-14 19:27:50 -07001965 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001967 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00001969 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00001972 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 return err;
1974}
1975
1976static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
1977 struct msghdr *msg, size_t len,
1978 int flags)
1979{
1980 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
1981 struct scm_cookie scm;
1982 struct sock *sk = sock->sk;
1983 struct netlink_sock *nlk = nlk_sk(sk);
1984 int noblock = flags&MSG_DONTWAIT;
1985 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00001986 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00001987 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988
1989 if (flags&MSG_OOB)
1990 return -EOPNOTSUPP;
1991
1992 copied = 0;
1993
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001994 skb = skb_recv_datagram(sk, flags, noblock, &err);
1995 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 goto out;
1997
Johannes Berg68d6ac62010-08-15 21:20:44 +00001998 data_skb = skb;
1999
Johannes Berg1dacc762009-07-01 11:26:02 +00002000#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2001 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002002 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002003 * If this skb has a frag_list, then here that means that we
2004 * will have to use the frag_list skb's data for compat tasks
2005 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002006 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002007 * If we need to send the compat skb, assign it to the
2008 * 'data_skb' variable so that it will be used below for data
2009 * copying. We keep 'skb' for everything else, including
2010 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002011 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002012 if (flags & MSG_CMSG_COMPAT)
2013 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002014 }
2015#endif
2016
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 msg->msg_namelen = 0;
2018
Johannes Berg68d6ac62010-08-15 21:20:44 +00002019 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (len < copied) {
2021 msg->msg_flags |= MSG_TRUNC;
2022 copied = len;
2023 }
2024
Johannes Berg68d6ac62010-08-15 21:20:44 +00002025 skb_reset_transport_header(data_skb);
2026 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
2028 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002029 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 addr->nl_family = AF_NETLINK;
2031 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002032 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002033 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 msg->msg_namelen = sizeof(*addr);
2035 }
2036
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002037 if (nlk->flags & NETLINK_RECV_PKTINFO)
2038 netlink_cmsg_recv_pktinfo(msg, skb);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (NULL == siocb->scm) {
2041 memset(&scm, 0, sizeof(scm));
2042 siocb->scm = &scm;
2043 }
2044 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002045 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002046 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 skb_free_datagram(sk, skb);
2049
Andrey Vaginb44d2112011-02-21 02:40:47 +00002050 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
2051 ret = netlink_dump(sk);
2052 if (ret) {
2053 sk->sk_err = ret;
2054 sk->sk_error_report(sk);
2055 }
2056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057
2058 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059out:
2060 netlink_rcv_wake(sk);
2061 return err ? : copied;
2062}
2063
2064static void netlink_data_ready(struct sock *sk, int len)
2065{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002066 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
2069/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002070 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 * complete set of kernel non-blocking support for message
2072 * queueing.
2073 */
2074
2075struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002076__netlink_kernel_create(struct net *net, int unit, struct module *module,
2077 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078{
2079 struct socket *sock;
2080 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002081 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002082 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002083 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2084 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002086 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002088 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 return NULL;
2090
2091 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2092 return NULL;
2093
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002094 /*
2095 * We have to just have a reference on the net from sk, but don't
2096 * get_net it. Besides, we cannot get and then put the net here.
2097 * So we create one inside init_net and the move it to net.
2098 */
2099
2100 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2101 goto out_sock_release_nosk;
2102
2103 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002104 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002105
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002106 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002107 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002108 else
2109 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002110
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002111 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002112 if (!listeners)
2113 goto out_sock_release;
2114
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002116 if (cfg && cfg->input)
2117 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002119 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002120 goto out_sock_release;
2121
2122 nlk = nlk_sk(sk);
2123 nlk->flags |= NETLINK_KERNEL_SOCKET;
2124
2125 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002126 if (!nl_table[unit].registered) {
2127 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002128 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002129 nl_table[unit].cb_mutex = cb_mutex;
2130 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002131 if (cfg) {
2132 nl_table[unit].bind = cfg->bind;
2133 nl_table[unit].flags = cfg->flags;
2134 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002135 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002136 } else {
2137 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002138 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002139 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002140 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002141 return sk;
2142
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002143out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002144 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002145 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002146 return NULL;
2147
2148out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002149 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002150 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002152EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002153
2154void
2155netlink_kernel_release(struct sock *sk)
2156{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002157 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002158}
2159EXPORT_SYMBOL(netlink_kernel_release);
2160
Johannes Bergd136f1b2009-09-12 03:03:15 +00002161int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002162{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002163 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002164 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002165
2166 if (groups < 32)
2167 groups = 32;
2168
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002169 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002170 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2171 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002172 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002173 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002174 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2175 rcu_assign_pointer(tbl->listeners, new);
2176
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002177 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002178 }
2179 tbl->groups = groups;
2180
Johannes Bergd136f1b2009-09-12 03:03:15 +00002181 return 0;
2182}
2183
2184/**
2185 * netlink_change_ngroups - change number of multicast groups
2186 *
2187 * This changes the number of multicast groups that are available
2188 * on a certain netlink family. Note that it is not possible to
2189 * change the number of groups to below 32. Also note that it does
2190 * not implicitly call netlink_clear_multicast_users() when the
2191 * number of groups is reduced.
2192 *
2193 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2194 * @groups: The new number of groups.
2195 */
2196int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2197{
2198 int err;
2199
2200 netlink_table_grab();
2201 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002202 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002203
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002204 return err;
2205}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002206
Johannes Bergb8273572009-09-24 15:44:05 -07002207void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2208{
2209 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002210 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2211
Sasha Levinb67bfe02013-02-27 17:06:00 -08002212 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002213 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2214}
2215
Johannes Berg84659eb2007-07-18 15:47:05 -07002216/**
2217 * netlink_clear_multicast_users - kick off multicast listeners
2218 *
2219 * This function removes all listeners from the given group.
2220 * @ksk: The kernel netlink socket, as returned by
2221 * netlink_kernel_create().
2222 * @group: The multicast group to clear.
2223 */
2224void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2225{
Johannes Berg84659eb2007-07-18 15:47:05 -07002226 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07002227 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07002228 netlink_table_ungrab();
2229}
Johannes Berg84659eb2007-07-18 15:47:05 -07002230
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002231struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002232__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002233{
2234 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002235 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002236
2237 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
2238 nlh->nlmsg_type = type;
2239 nlh->nlmsg_len = size;
2240 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002241 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002242 nlh->nlmsg_seq = seq;
2243 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002244 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002245 return nlh;
2246}
2247EXPORT_SYMBOL(__nlmsg_put);
2248
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249/*
2250 * It looks a bit ugly.
2251 * It would be better to create kernel thread.
2252 */
2253
2254static int netlink_dump(struct sock *sk)
2255{
2256 struct netlink_sock *nlk = nlk_sk(sk);
2257 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002258 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002260 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002261 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002263 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265 cb = nlk->cb;
2266 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002267 err = -EINVAL;
2268 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
2270
Greg Rosec7ac8672011-06-10 01:27:09 +00002271 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2272
2273 skb = sock_rmalloc(sk, alloc_size, 0, GFP_KERNEL);
2274 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002275 goto errout_skb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002276
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 len = cb->dump(skb, cb);
2278
2279 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002280 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002281
2282 if (sk_filter(sk, skb))
2283 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002284 else
2285 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 return 0;
2287 }
2288
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002289 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2290 if (!nlh)
2291 goto errout_skb;
2292
Johannes Berg670dc282011-06-20 13:40:46 +02002293 nl_dump_check_consistent(cb, nlh);
2294
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002295 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2296
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002297 if (sk_filter(sk, skb))
2298 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002299 else
2300 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Thomas Grafa8f74b22005-11-10 02:25:52 +01002302 if (cb->done)
2303 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002305 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Gao feng6dc878a2012-10-04 20:15:48 +00002307 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00002308 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002310
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002311errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002312 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002313 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002314 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315}
2316
Gao feng6dc878a2012-10-04 20:15:48 +00002317int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2318 const struct nlmsghdr *nlh,
2319 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320{
2321 struct netlink_callback *cb;
2322 struct sock *sk;
2323 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002324 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002326 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 if (cb == NULL)
2328 return -ENOBUFS;
2329
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002330 cb->dump = control->dump;
2331 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002333 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002334 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002335 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 atomic_inc(&skb->users);
2337 cb->skb = skb;
2338
Eric W. Biederman15e47302012-09-07 20:12:54 +00002339 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 if (sk == NULL) {
2341 netlink_destroy_callback(cb);
2342 return -ECONNREFUSED;
2343 }
2344 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002345
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002346 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002347 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07002348 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002349 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00002351 ret = -EBUSY;
2352 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 }
Gao feng6dc878a2012-10-04 20:15:48 +00002354 /* add reference of module which cb->dump belongs to */
2355 if (!try_module_get(cb->module)) {
2356 mutex_unlock(nlk->cb_mutex);
2357 netlink_destroy_callback(cb);
2358 ret = -EPROTONOSUPPORT;
2359 goto out;
2360 }
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002363 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
Andrey Vaginb44d2112011-02-21 02:40:47 +00002365 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002366out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002368
Andrey Vaginb44d2112011-02-21 02:40:47 +00002369 if (ret)
2370 return ret;
2371
Denis V. Lunev5c582982007-10-23 20:29:25 -07002372 /* We successfully started a dump, by returning -EINTR we
2373 * signal not to send ACK even if it was requested.
2374 */
2375 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376}
Gao feng6dc878a2012-10-04 20:15:48 +00002377EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2380{
2381 struct sk_buff *skb;
2382 struct nlmsghdr *rep;
2383 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002384 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385
Thomas Graf339bf982006-11-10 14:10:15 -08002386 /* error messages get the original request appened */
2387 if (err)
2388 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Thomas Graf339bf982006-11-10 14:10:15 -08002390 skb = nlmsg_new(payload, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 if (!skb) {
2392 struct sock *sk;
2393
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002394 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002395 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002396 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 if (sk) {
2398 sk->sk_err = ENOBUFS;
2399 sk->sk_error_report(sk);
2400 sock_put(sk);
2401 }
2402 return;
2403 }
2404
Eric W. Biederman15e47302012-09-07 20:12:54 +00002405 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002406 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002407 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002409 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002410 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002412EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002414int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002415 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002416{
Thomas Graf82ace472005-11-10 02:25:53 +01002417 struct nlmsghdr *nlh;
2418 int err;
2419
2420 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002421 int msglen;
2422
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002423 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002424 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002425
Martin Murrayad8e4b72006-01-10 13:02:29 -08002426 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002427 return 0;
2428
Thomas Grafd35b6852007-03-22 23:28:46 -07002429 /* Only requests are handled by the kernel */
2430 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002431 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002432
Thomas Graf45e7ae72007-03-22 23:29:10 -07002433 /* Skip control messages */
2434 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002435 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002436
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002437 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002438 if (err == -EINTR)
2439 goto skip;
2440
2441ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002442 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002443 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002444
Denis V. Lunev5c582982007-10-23 20:29:25 -07002445skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002446 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002447 if (msglen > skb->len)
2448 msglen = skb->len;
2449 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002450 }
2451
2452 return 0;
2453}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002454EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002455
2456/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002457 * nlmsg_notify - send a notification netlink message
2458 * @sk: netlink socket to use
2459 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002460 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002461 * @group: destination multicast group or 0
2462 * @report: 1 to report back, 0 to disable
2463 * @flags: allocation flags
2464 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002465int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002466 unsigned int group, int report, gfp_t flags)
2467{
2468 int err = 0;
2469
2470 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002471 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002472
2473 if (report) {
2474 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002475 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002476 }
2477
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002478 /* errors reported via destination sk->sk_err, but propagate
2479 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002480 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002481 }
2482
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002483 if (report) {
2484 int err2;
2485
Eric W. Biederman15e47302012-09-07 20:12:54 +00002486 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002487 if (!err || err == -ESRCH)
2488 err = err2;
2489 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002490
2491 return err;
2492}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002493EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495#ifdef CONFIG_PROC_FS
2496struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002497 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 int link;
2499 int hash_idx;
2500};
2501
2502static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2503{
2504 struct nl_seq_iter *iter = seq->private;
2505 int i, j;
2506 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 loff_t off = 0;
2508
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002509 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002510 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511
2512 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002513 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002514 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002515 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 if (off == pos) {
2517 iter->link = i;
2518 iter->hash_idx = j;
2519 return s;
2520 }
2521 ++off;
2522 }
2523 }
2524 }
2525 return NULL;
2526}
2527
2528static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002529 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
2531 read_lock(&nl_table_lock);
2532 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2533}
2534
2535static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2536{
2537 struct sock *s;
2538 struct nl_seq_iter *iter;
2539 int i, j;
2540
2541 ++*pos;
2542
2543 if (v == SEQ_START_TOKEN)
2544 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002545
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002546 iter = seq->private;
2547 s = v;
2548 do {
2549 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002550 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 if (s)
2552 return s;
2553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 i = iter->link;
2555 j = iter->hash_idx + 1;
2556
2557 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002558 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559
2560 for (; j <= hash->mask; j++) {
2561 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002562 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002563 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (s) {
2565 iter->link = i;
2566 iter->hash_idx = j;
2567 return s;
2568 }
2569 }
2570
2571 j = 0;
2572 } while (++i < MAX_LINKS);
2573
2574 return NULL;
2575}
2576
2577static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002578 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579{
2580 read_unlock(&nl_table_lock);
2581}
2582
2583
2584static int netlink_seq_show(struct seq_file *seq, void *v)
2585{
Eric Dumazet658cb352012-04-22 21:30:21 +00002586 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587 seq_puts(seq,
2588 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002589 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002590 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 struct sock *s = v;
2592 struct netlink_sock *nlk = nlk_sk(s);
2593
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002594 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 s,
2596 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002597 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002598 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002599 sk_rmem_alloc_get(s),
2600 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002602 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002603 atomic_read(&s->sk_drops),
2604 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 );
2606
2607 }
2608 return 0;
2609}
2610
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002611static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 .start = netlink_seq_start,
2613 .next = netlink_seq_next,
2614 .stop = netlink_seq_stop,
2615 .show = netlink_seq_show,
2616};
2617
2618
2619static int netlink_seq_open(struct inode *inode, struct file *file)
2620{
Denis V. Luneve372c412007-11-19 22:31:54 -08002621 return seq_open_net(inode, file, &netlink_seq_ops,
2622 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002623}
2624
Arjan van de Venda7071d2007-02-12 00:55:36 -08002625static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 .owner = THIS_MODULE,
2627 .open = netlink_seq_open,
2628 .read = seq_read,
2629 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002630 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631};
2632
2633#endif
2634
2635int netlink_register_notifier(struct notifier_block *nb)
2636{
Alan Sterne041c682006-03-27 01:16:30 -08002637 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002639EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
2641int netlink_unregister_notifier(struct notifier_block *nb)
2642{
Alan Sterne041c682006-03-27 01:16:30 -08002643 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002645EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002646
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002647static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 .family = PF_NETLINK,
2649 .owner = THIS_MODULE,
2650 .release = netlink_release,
2651 .bind = netlink_bind,
2652 .connect = netlink_connect,
2653 .socketpair = sock_no_socketpair,
2654 .accept = sock_no_accept,
2655 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00002656 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 .ioctl = sock_no_ioctl,
2658 .listen = sock_no_listen,
2659 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002660 .setsockopt = netlink_setsockopt,
2661 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 .sendmsg = netlink_sendmsg,
2663 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002664 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 .sendpage = sock_no_sendpage,
2666};
2667
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002668static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 .family = PF_NETLINK,
2670 .create = netlink_create,
2671 .owner = THIS_MODULE, /* for consistency 8) */
2672};
2673
Pavel Emelyanov46650792007-10-08 20:38:39 -07002674static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002675{
2676#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002677 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002678 return -ENOMEM;
2679#endif
2680 return 0;
2681}
2682
Pavel Emelyanov46650792007-10-08 20:38:39 -07002683static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002684{
2685#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002686 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002687#endif
2688}
2689
David S. Millerb963ea82010-08-30 19:08:01 -07002690static void __init netlink_add_usersock_entry(void)
2691{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002692 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002693 int groups = 32;
2694
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002695 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002696 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002697 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002698
2699 netlink_table_grab();
2700
2701 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002702 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002703 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2704 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002705 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002706
2707 netlink_table_ungrab();
2708}
2709
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002710static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002711 .init = netlink_net_init,
2712 .exit = netlink_net_exit,
2713};
2714
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715static int __init netlink_proto_init(void)
2716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002718 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 unsigned int order;
2720 int err = proto_register(&netlink_proto, 0);
2721
2722 if (err != 0)
2723 goto out;
2724
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002725 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002727 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002728 if (!nl_table)
2729 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Jan Beulich44813742009-09-21 17:03:05 -07002731 if (totalram_pages >= (128 * 1024))
2732 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 else
Jan Beulich44813742009-09-21 17:03:05 -07002734 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002736 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2737 limit = (1UL << order) / sizeof(struct hlist_head);
2738 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002741 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Eric W. Biederman15e47302012-09-07 20:12:54 +00002743 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 if (!hash->table) {
2745 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002746 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 1 * sizeof(*hash->table));
2748 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002749 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 hash->max_shift = order;
2752 hash->shift = 0;
2753 hash->mask = 0;
2754 hash->rehash_time = jiffies;
2755 }
2756
David S. Millerb963ea82010-08-30 19:08:01 -07002757 netlink_add_usersock_entry();
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002760 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002761 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 rtnetlink_init();
2763out:
2764 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002765panic:
2766 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769core_initcall(netlink_proto_init);