blob: d120b5d4d86a094c4d6f46e126cbbfed0e67e711 [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 McHardyf9c22882013-04-17 06:47:04 +0000119static bool netlink_rx_is_mmaped(struct sock *sk)
120{
121 return nlk_sk(sk)->rx_ring.pg_vec != NULL;
122}
123
Patrick McHardy5fd96122013-04-17 06:47:03 +0000124static bool netlink_tx_is_mmaped(struct sock *sk)
125{
126 return nlk_sk(sk)->tx_ring.pg_vec != NULL;
127}
128
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000129static __pure struct page *pgvec_to_page(const void *addr)
130{
131 if (is_vmalloc_addr(addr))
132 return vmalloc_to_page(addr);
133 else
134 return virt_to_page(addr);
135}
136
137static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len)
138{
139 unsigned int i;
140
141 for (i = 0; i < len; i++) {
142 if (pg_vec[i] != NULL) {
143 if (is_vmalloc_addr(pg_vec[i]))
144 vfree(pg_vec[i]);
145 else
146 free_pages((unsigned long)pg_vec[i], order);
147 }
148 }
149 kfree(pg_vec);
150}
151
152static void *alloc_one_pg_vec_page(unsigned long order)
153{
154 void *buffer;
155 gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO |
156 __GFP_NOWARN | __GFP_NORETRY;
157
158 buffer = (void *)__get_free_pages(gfp_flags, order);
159 if (buffer != NULL)
160 return buffer;
161
162 buffer = vzalloc((1 << order) * PAGE_SIZE);
163 if (buffer != NULL)
164 return buffer;
165
166 gfp_flags &= ~__GFP_NORETRY;
167 return (void *)__get_free_pages(gfp_flags, order);
168}
169
170static void **alloc_pg_vec(struct netlink_sock *nlk,
171 struct nl_mmap_req *req, unsigned int order)
172{
173 unsigned int block_nr = req->nm_block_nr;
174 unsigned int i;
175 void **pg_vec, *ptr;
176
177 pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL);
178 if (pg_vec == NULL)
179 return NULL;
180
181 for (i = 0; i < block_nr; i++) {
182 pg_vec[i] = ptr = alloc_one_pg_vec_page(order);
183 if (pg_vec[i] == NULL)
184 goto err1;
185 }
186
187 return pg_vec;
188err1:
189 free_pg_vec(pg_vec, order, block_nr);
190 return NULL;
191}
192
193static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req,
194 bool closing, bool tx_ring)
195{
196 struct netlink_sock *nlk = nlk_sk(sk);
197 struct netlink_ring *ring;
198 struct sk_buff_head *queue;
199 void **pg_vec = NULL;
200 unsigned int order = 0;
201 int err;
202
203 ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring;
204 queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue;
205
206 if (!closing) {
207 if (atomic_read(&nlk->mapped))
208 return -EBUSY;
209 if (atomic_read(&ring->pending))
210 return -EBUSY;
211 }
212
213 if (req->nm_block_nr) {
214 if (ring->pg_vec != NULL)
215 return -EBUSY;
216
217 if ((int)req->nm_block_size <= 0)
218 return -EINVAL;
219 if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE))
220 return -EINVAL;
221 if (req->nm_frame_size < NL_MMAP_HDRLEN)
222 return -EINVAL;
223 if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT))
224 return -EINVAL;
225
226 ring->frames_per_block = req->nm_block_size /
227 req->nm_frame_size;
228 if (ring->frames_per_block == 0)
229 return -EINVAL;
230 if (ring->frames_per_block * req->nm_block_nr !=
231 req->nm_frame_nr)
232 return -EINVAL;
233
234 order = get_order(req->nm_block_size);
235 pg_vec = alloc_pg_vec(nlk, req, order);
236 if (pg_vec == NULL)
237 return -ENOMEM;
238 } else {
239 if (req->nm_frame_nr)
240 return -EINVAL;
241 }
242
243 err = -EBUSY;
244 mutex_lock(&nlk->pg_vec_lock);
245 if (closing || atomic_read(&nlk->mapped) == 0) {
246 err = 0;
247 spin_lock_bh(&queue->lock);
248
249 ring->frame_max = req->nm_frame_nr - 1;
250 ring->head = 0;
251 ring->frame_size = req->nm_frame_size;
252 ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE;
253
254 swap(ring->pg_vec_len, req->nm_block_nr);
255 swap(ring->pg_vec_order, order);
256 swap(ring->pg_vec, pg_vec);
257
258 __skb_queue_purge(queue);
259 spin_unlock_bh(&queue->lock);
260
261 WARN_ON(atomic_read(&nlk->mapped));
262 }
263 mutex_unlock(&nlk->pg_vec_lock);
264
265 if (pg_vec)
266 free_pg_vec(pg_vec, order, req->nm_block_nr);
267 return err;
268}
269
270static void netlink_mm_open(struct vm_area_struct *vma)
271{
272 struct file *file = vma->vm_file;
273 struct socket *sock = file->private_data;
274 struct sock *sk = sock->sk;
275
276 if (sk)
277 atomic_inc(&nlk_sk(sk)->mapped);
278}
279
280static void netlink_mm_close(struct vm_area_struct *vma)
281{
282 struct file *file = vma->vm_file;
283 struct socket *sock = file->private_data;
284 struct sock *sk = sock->sk;
285
286 if (sk)
287 atomic_dec(&nlk_sk(sk)->mapped);
288}
289
290static const struct vm_operations_struct netlink_mmap_ops = {
291 .open = netlink_mm_open,
292 .close = netlink_mm_close,
293};
294
295static int netlink_mmap(struct file *file, struct socket *sock,
296 struct vm_area_struct *vma)
297{
298 struct sock *sk = sock->sk;
299 struct netlink_sock *nlk = nlk_sk(sk);
300 struct netlink_ring *ring;
301 unsigned long start, size, expected;
302 unsigned int i;
303 int err = -EINVAL;
304
305 if (vma->vm_pgoff)
306 return -EINVAL;
307
308 mutex_lock(&nlk->pg_vec_lock);
309
310 expected = 0;
311 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
312 if (ring->pg_vec == NULL)
313 continue;
314 expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE;
315 }
316
317 if (expected == 0)
318 goto out;
319
320 size = vma->vm_end - vma->vm_start;
321 if (size != expected)
322 goto out;
323
324 start = vma->vm_start;
325 for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) {
326 if (ring->pg_vec == NULL)
327 continue;
328
329 for (i = 0; i < ring->pg_vec_len; i++) {
330 struct page *page;
331 void *kaddr = ring->pg_vec[i];
332 unsigned int pg_num;
333
334 for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) {
335 page = pgvec_to_page(kaddr);
336 err = vm_insert_page(vma, start, page);
337 if (err < 0)
338 goto out;
339 start += PAGE_SIZE;
340 kaddr += PAGE_SIZE;
341 }
342 }
343 }
344
345 atomic_inc(&nlk->mapped);
346 vma->vm_ops = &netlink_mmap_ops;
347 err = 0;
348out:
349 mutex_unlock(&nlk->pg_vec_lock);
350 return 0;
351}
Patrick McHardy9652e932013-04-17 06:47:02 +0000352
353static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr)
354{
355#if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1
356 struct page *p_start, *p_end;
357
358 /* First page is flushed through netlink_{get,set}_status */
359 p_start = pgvec_to_page(hdr + PAGE_SIZE);
360 p_end = pgvec_to_page((void *)hdr + NL_MMAP_MSG_HDRLEN + hdr->nm_len - 1);
361 while (p_start <= p_end) {
362 flush_dcache_page(p_start);
363 p_start++;
364 }
365#endif
366}
367
368static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr)
369{
370 smp_rmb();
371 flush_dcache_page(pgvec_to_page(hdr));
372 return hdr->nm_status;
373}
374
375static void netlink_set_status(struct nl_mmap_hdr *hdr,
376 enum nl_mmap_status status)
377{
378 hdr->nm_status = status;
379 flush_dcache_page(pgvec_to_page(hdr));
380 smp_wmb();
381}
382
383static struct nl_mmap_hdr *
384__netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos)
385{
386 unsigned int pg_vec_pos, frame_off;
387
388 pg_vec_pos = pos / ring->frames_per_block;
389 frame_off = pos % ring->frames_per_block;
390
391 return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size);
392}
393
394static struct nl_mmap_hdr *
395netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos,
396 enum nl_mmap_status status)
397{
398 struct nl_mmap_hdr *hdr;
399
400 hdr = __netlink_lookup_frame(ring, pos);
401 if (netlink_get_status(hdr) != status)
402 return NULL;
403
404 return hdr;
405}
406
407static struct nl_mmap_hdr *
408netlink_current_frame(const struct netlink_ring *ring,
409 enum nl_mmap_status status)
410{
411 return netlink_lookup_frame(ring, ring->head, status);
412}
413
414static struct nl_mmap_hdr *
415netlink_previous_frame(const struct netlink_ring *ring,
416 enum nl_mmap_status status)
417{
418 unsigned int prev;
419
420 prev = ring->head ? ring->head - 1 : ring->frame_max;
421 return netlink_lookup_frame(ring, prev, status);
422}
423
424static void netlink_increment_head(struct netlink_ring *ring)
425{
426 ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0;
427}
428
429static void netlink_forward_ring(struct netlink_ring *ring)
430{
431 unsigned int head = ring->head, pos = head;
432 const struct nl_mmap_hdr *hdr;
433
434 do {
435 hdr = __netlink_lookup_frame(ring, pos);
436 if (hdr->nm_status == NL_MMAP_STATUS_UNUSED)
437 break;
438 if (hdr->nm_status != NL_MMAP_STATUS_SKIP)
439 break;
440 netlink_increment_head(ring);
441 } while (ring->head != head);
442}
443
444static unsigned int netlink_poll(struct file *file, struct socket *sock,
445 poll_table *wait)
446{
447 struct sock *sk = sock->sk;
448 struct netlink_sock *nlk = nlk_sk(sk);
449 unsigned int mask;
450
Patrick McHardy5fd96122013-04-17 06:47:03 +0000451 if (nlk->cb != NULL && nlk->rx_ring.pg_vec != NULL)
452 netlink_dump(sk);
453
Patrick McHardy9652e932013-04-17 06:47:02 +0000454 mask = datagram_poll(file, sock, wait);
455
456 spin_lock_bh(&sk->sk_receive_queue.lock);
457 if (nlk->rx_ring.pg_vec) {
458 netlink_forward_ring(&nlk->rx_ring);
459 if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED))
460 mask |= POLLIN | POLLRDNORM;
461 }
462 spin_unlock_bh(&sk->sk_receive_queue.lock);
463
464 spin_lock_bh(&sk->sk_write_queue.lock);
465 if (nlk->tx_ring.pg_vec) {
466 if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED))
467 mask |= POLLOUT | POLLWRNORM;
468 }
469 spin_unlock_bh(&sk->sk_write_queue.lock);
470
471 return mask;
472}
473
474static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb)
475{
476 return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN);
477}
478
479static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk,
480 struct netlink_ring *ring,
481 struct nl_mmap_hdr *hdr)
482{
483 unsigned int size;
484 void *data;
485
486 size = ring->frame_size - NL_MMAP_HDRLEN;
487 data = (void *)hdr + NL_MMAP_HDRLEN;
488
489 skb->head = data;
490 skb->data = data;
491 skb_reset_tail_pointer(skb);
492 skb->end = skb->tail + size;
493 skb->len = 0;
494
495 skb->destructor = netlink_skb_destructor;
496 NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED;
497 NETLINK_CB(skb).sk = sk;
498}
Patrick McHardy5fd96122013-04-17 06:47:03 +0000499
500static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg,
501 u32 dst_portid, u32 dst_group,
502 struct sock_iocb *siocb)
503{
504 struct netlink_sock *nlk = nlk_sk(sk);
505 struct netlink_ring *ring;
506 struct nl_mmap_hdr *hdr;
507 struct sk_buff *skb;
508 unsigned int maxlen;
509 bool excl = true;
510 int err = 0, len = 0;
511
512 /* Netlink messages are validated by the receiver before processing.
513 * In order to avoid userspace changing the contents of the message
514 * after validation, the socket and the ring may only be used by a
515 * single process, otherwise we fall back to copying.
516 */
517 if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 ||
518 atomic_read(&nlk->mapped) > 1)
519 excl = false;
520
521 mutex_lock(&nlk->pg_vec_lock);
522
523 ring = &nlk->tx_ring;
524 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
525
526 do {
527 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID);
528 if (hdr == NULL) {
529 if (!(msg->msg_flags & MSG_DONTWAIT) &&
530 atomic_read(&nlk->tx_ring.pending))
531 schedule();
532 continue;
533 }
534 if (hdr->nm_len > maxlen) {
535 err = -EINVAL;
536 goto out;
537 }
538
539 netlink_frame_flush_dcache(hdr);
540
541 if (likely(dst_portid == 0 && dst_group == 0 && excl)) {
542 skb = alloc_skb_head(GFP_KERNEL);
543 if (skb == NULL) {
544 err = -ENOBUFS;
545 goto out;
546 }
547 sock_hold(sk);
548 netlink_ring_setup_skb(skb, sk, ring, hdr);
549 NETLINK_CB(skb).flags |= NETLINK_SKB_TX;
550 __skb_put(skb, hdr->nm_len);
551 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
552 atomic_inc(&ring->pending);
553 } else {
554 skb = alloc_skb(hdr->nm_len, GFP_KERNEL);
555 if (skb == NULL) {
556 err = -ENOBUFS;
557 goto out;
558 }
559 __skb_put(skb, hdr->nm_len);
560 memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len);
561 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
562 }
563
564 netlink_increment_head(ring);
565
566 NETLINK_CB(skb).portid = nlk->portid;
567 NETLINK_CB(skb).dst_group = dst_group;
568 NETLINK_CB(skb).creds = siocb->scm->creds;
569
570 err = security_netlink_send(sk, skb);
571 if (err) {
572 kfree_skb(skb);
573 goto out;
574 }
575
576 if (unlikely(dst_group)) {
577 atomic_inc(&skb->users);
578 netlink_broadcast(sk, skb, dst_portid, dst_group,
579 GFP_KERNEL);
580 }
581 err = netlink_unicast(sk, skb, dst_portid,
582 msg->msg_flags & MSG_DONTWAIT);
583 if (err < 0)
584 goto out;
585 len += err;
586
587 } while (hdr != NULL ||
588 (!(msg->msg_flags & MSG_DONTWAIT) &&
589 atomic_read(&nlk->tx_ring.pending)));
590
591 if (len > 0)
592 err = len;
593out:
594 mutex_unlock(&nlk->pg_vec_lock);
595 return err;
596}
Patrick McHardyf9c22882013-04-17 06:47:04 +0000597
598static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb)
599{
600 struct nl_mmap_hdr *hdr;
601
602 hdr = netlink_mmap_hdr(skb);
603 hdr->nm_len = skb->len;
604 hdr->nm_group = NETLINK_CB(skb).dst_group;
605 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
606 hdr->nm_uid = NETLINK_CB(skb).creds.uid;
607 hdr->nm_gid = NETLINK_CB(skb).creds.gid;
608 netlink_frame_flush_dcache(hdr);
609 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
610
611 NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED;
612 kfree_skb(skb);
613}
614
615static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb)
616{
617 struct netlink_sock *nlk = nlk_sk(sk);
618 struct netlink_ring *ring = &nlk->rx_ring;
619 struct nl_mmap_hdr *hdr;
620
621 spin_lock_bh(&sk->sk_receive_queue.lock);
622 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
623 if (hdr == NULL) {
624 spin_unlock_bh(&sk->sk_receive_queue.lock);
625 kfree_skb(skb);
626 sk->sk_err = ENOBUFS;
627 sk->sk_error_report(sk);
628 return;
629 }
630 netlink_increment_head(ring);
631 __skb_queue_tail(&sk->sk_receive_queue, skb);
632 spin_unlock_bh(&sk->sk_receive_queue.lock);
633
634 hdr->nm_len = skb->len;
635 hdr->nm_group = NETLINK_CB(skb).dst_group;
636 hdr->nm_pid = NETLINK_CB(skb).creds.pid;
637 hdr->nm_uid = NETLINK_CB(skb).creds.uid;
638 hdr->nm_gid = NETLINK_CB(skb).creds.gid;
639 netlink_set_status(hdr, NL_MMAP_STATUS_COPY);
640}
641
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000642#else /* CONFIG_NETLINK_MMAP */
Patrick McHardy9652e932013-04-17 06:47:02 +0000643#define netlink_skb_is_mmaped(skb) false
Patrick McHardyf9c22882013-04-17 06:47:04 +0000644#define netlink_rx_is_mmaped(sk) false
Patrick McHardy5fd96122013-04-17 06:47:03 +0000645#define netlink_tx_is_mmaped(sk) false
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000646#define netlink_mmap sock_no_mmap
Patrick McHardy9652e932013-04-17 06:47:02 +0000647#define netlink_poll datagram_poll
Patrick McHardy5fd96122013-04-17 06:47:03 +0000648#define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000649#endif /* CONFIG_NETLINK_MMAP */
650
Eric Dumazet658cb352012-04-22 21:30:21 +0000651static void netlink_destroy_callback(struct netlink_callback *cb)
652{
653 kfree_skb(cb->skb);
654 kfree(cb);
655}
656
Eric Dumazetbfb253c2012-04-22 21:30:29 +0000657static void netlink_consume_callback(struct netlink_callback *cb)
658{
659 consume_skb(cb->skb);
660 kfree(cb);
661}
662
Patrick McHardycf0a0182013-04-17 06:47:00 +0000663static void netlink_skb_destructor(struct sk_buff *skb)
664{
Patrick McHardy9652e932013-04-17 06:47:02 +0000665#ifdef CONFIG_NETLINK_MMAP
666 struct nl_mmap_hdr *hdr;
667 struct netlink_ring *ring;
668 struct sock *sk;
669
670 /* If a packet from the kernel to userspace was freed because of an
671 * error without being delivered to userspace, the kernel must reset
672 * the status. In the direction userspace to kernel, the status is
673 * always reset here after the packet was processed and freed.
674 */
675 if (netlink_skb_is_mmaped(skb)) {
676 hdr = netlink_mmap_hdr(skb);
677 sk = NETLINK_CB(skb).sk;
678
Patrick McHardy5fd96122013-04-17 06:47:03 +0000679 if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) {
680 netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED);
681 ring = &nlk_sk(sk)->tx_ring;
682 } else {
683 if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) {
684 hdr->nm_len = 0;
685 netlink_set_status(hdr, NL_MMAP_STATUS_VALID);
686 }
687 ring = &nlk_sk(sk)->rx_ring;
Patrick McHardy9652e932013-04-17 06:47:02 +0000688 }
Patrick McHardy9652e932013-04-17 06:47:02 +0000689
690 WARN_ON(atomic_read(&ring->pending) == 0);
691 atomic_dec(&ring->pending);
692 sock_put(sk);
693
694 skb->data = NULL;
695 }
696#endif
697 if (skb->sk != NULL)
698 sock_rfree(skb);
Patrick McHardycf0a0182013-04-17 06:47:00 +0000699}
700
701static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
702{
703 WARN_ON(skb->sk != NULL);
704 skb->sk = sk;
705 skb->destructor = netlink_skb_destructor;
706 atomic_add(skb->truesize, &sk->sk_rmem_alloc);
707 sk_mem_charge(sk, skb->truesize);
708}
709
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710static void netlink_sock_destruct(struct sock *sk)
711{
Herbert Xu3f660d62007-05-03 03:17:14 -0700712 struct netlink_sock *nlk = nlk_sk(sk);
713
Herbert Xu3f660d62007-05-03 03:17:14 -0700714 if (nlk->cb) {
715 if (nlk->cb->done)
716 nlk->cb->done(nlk->cb);
Gao feng6dc878a2012-10-04 20:15:48 +0000717
718 module_put(nlk->cb->module);
Herbert Xu3f660d62007-05-03 03:17:14 -0700719 netlink_destroy_callback(nlk->cb);
720 }
721
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 skb_queue_purge(&sk->sk_receive_queue);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000723#ifdef CONFIG_NETLINK_MMAP
724 if (1) {
725 struct nl_mmap_req req;
726
727 memset(&req, 0, sizeof(req));
728 if (nlk->rx_ring.pg_vec)
729 netlink_set_ring(sk, &req, true, false);
730 memset(&req, 0, sizeof(req));
731 if (nlk->tx_ring.pg_vec)
732 netlink_set_ring(sk, &req, true, true);
733 }
734#endif /* CONFIG_NETLINK_MMAP */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 if (!sock_flag(sk, SOCK_DEAD)) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800737 printk(KERN_ERR "Freeing alive netlink socket %p\n", sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return;
739 }
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700740
741 WARN_ON(atomic_read(&sk->sk_rmem_alloc));
742 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
743 WARN_ON(nlk_sk(sk)->groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800746/* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on
747 * SMP. Look, when several writers sleep and reader wakes them up, all but one
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * immediately hit write lock and grab all the cpus. Exclusive sleep solves
749 * this, _but_ remember, it adds useless work on UP machines.
750 */
751
Johannes Bergd136f1b2009-09-12 03:03:15 +0000752void netlink_table_grab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800753 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Johannes Bergd136f1b2009-09-12 03:03:15 +0000755 might_sleep();
756
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700757 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 if (atomic_read(&nl_table_users)) {
760 DECLARE_WAITQUEUE(wait, current);
761
762 add_wait_queue_exclusive(&nl_table_wait, &wait);
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800763 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 set_current_state(TASK_UNINTERRUPTIBLE);
765 if (atomic_read(&nl_table_users) == 0)
766 break;
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700767 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 schedule();
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700769 write_lock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
772 __set_current_state(TASK_RUNNING);
773 remove_wait_queue(&nl_table_wait, &wait);
774 }
775}
776
Johannes Bergd136f1b2009-09-12 03:03:15 +0000777void netlink_table_ungrab(void)
Eric Dumazet9a429c42008-01-01 21:58:02 -0800778 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Arjan van de Ven6abd2192006-07-03 00:24:07 -0700780 write_unlock_irq(&nl_table_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 wake_up(&nl_table_wait);
782}
783
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800784static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785netlink_lock_table(void)
786{
787 /* read_lock() synchronizes us to netlink_table_grab */
788
789 read_lock(&nl_table_lock);
790 atomic_inc(&nl_table_users);
791 read_unlock(&nl_table_lock);
792}
793
Patrick McHardy6ac552f2007-12-04 00:19:38 -0800794static inline void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795netlink_unlock_table(void)
796{
797 if (atomic_dec_and_test(&nl_table_users))
798 wake_up(&nl_table_wait);
799}
800
Eric W. Biederman15e47302012-09-07 20:12:54 +0000801static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000803 struct nl_portid_hash *hash = &nl_table[protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 struct hlist_head *head;
805 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 read_lock(&nl_table_lock);
Eric W. Biederman15e47302012-09-07 20:12:54 +0000808 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800809 sk_for_each(sk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000810 if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 sock_hold(sk);
812 goto found;
813 }
814 }
815 sk = NULL;
816found:
817 read_unlock(&nl_table_lock);
818 return sk;
819}
820
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821static struct hlist_head *nl_portid_hash_zalloc(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 if (size <= PAGE_SIZE)
Eric Dumazetea729122007-12-11 02:09:47 -0800824 return kzalloc(size, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 else
826 return (struct hlist_head *)
Eric Dumazetea729122007-12-11 02:09:47 -0800827 __get_free_pages(GFP_ATOMIC | __GFP_ZERO,
828 get_order(size));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Eric W. Biederman15e47302012-09-07 20:12:54 +0000831static void nl_portid_hash_free(struct hlist_head *table, size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
833 if (size <= PAGE_SIZE)
834 kfree(table);
835 else
836 free_pages((unsigned long)table, get_order(size));
837}
838
Eric W. Biederman15e47302012-09-07 20:12:54 +0000839static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 unsigned int omask, mask, shift;
842 size_t osize, size;
843 struct hlist_head *otable, *table;
844 int i;
845
846 omask = mask = hash->mask;
847 osize = size = (mask + 1) * sizeof(*table);
848 shift = hash->shift;
849
850 if (grow) {
851 if (++shift > hash->max_shift)
852 return 0;
853 mask = mask * 2 + 1;
854 size *= 2;
855 }
856
Eric W. Biederman15e47302012-09-07 20:12:54 +0000857 table = nl_portid_hash_zalloc(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (!table)
859 return 0;
860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 otable = hash->table;
862 hash->table = table;
863 hash->mask = mask;
864 hash->shift = shift;
865 get_random_bytes(&hash->rnd, sizeof(hash->rnd));
866
867 for (i = 0; i <= omask; i++) {
868 struct sock *sk;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800869 struct hlist_node *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Sasha Levinb67bfe02013-02-27 17:06:00 -0800871 sk_for_each_safe(sk, tmp, &otable[i])
Eric W. Biederman15e47302012-09-07 20:12:54 +0000872 __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874
Eric W. Biederman15e47302012-09-07 20:12:54 +0000875 nl_portid_hash_free(otable, osize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 hash->rehash_time = jiffies + 10 * 60 * HZ;
877 return 1;
878}
879
Eric W. Biederman15e47302012-09-07 20:12:54 +0000880static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 int avg = hash->entries >> hash->shift;
883
Eric W. Biederman15e47302012-09-07 20:12:54 +0000884 if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 return 1;
886
887 if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000888 nl_portid_hash_rehash(hash, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 return 1;
890 }
891
892 return 0;
893}
894
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800895static const struct proto_ops netlink_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Patrick McHardy4277a082006-03-20 18:52:01 -0800897static void
898netlink_update_listeners(struct sock *sk)
899{
900 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Patrick McHardy4277a082006-03-20 18:52:01 -0800901 unsigned long mask;
902 unsigned int i;
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000903 struct listeners *listeners;
904
905 listeners = nl_deref_protected(tbl->listeners);
906 if (!listeners)
907 return;
Patrick McHardy4277a082006-03-20 18:52:01 -0800908
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700909 for (i = 0; i < NLGRPLONGS(tbl->groups); i++) {
Patrick McHardy4277a082006-03-20 18:52:01 -0800910 mask = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800911 sk_for_each_bound(sk, &tbl->mc_list) {
Johannes Bergb4ff4f02007-07-18 15:46:06 -0700912 if (i < NLGRPLONGS(nlk_sk(sk)->ngroups))
913 mask |= nlk_sk(sk)->groups[i];
914 }
Eric Dumazet6d772ac2012-10-18 03:21:55 +0000915 listeners->masks[i] = mask;
Patrick McHardy4277a082006-03-20 18:52:01 -0800916 }
917 /* this function is only called with the netlink table "grabbed", which
918 * makes sure updates are visible before bind or setsockopt return. */
919}
920
Eric W. Biederman15e47302012-09-07 20:12:54 +0000921static int netlink_insert(struct sock *sk, struct net *net, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Eric W. Biederman15e47302012-09-07 20:12:54 +0000923 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 struct hlist_head *head;
925 int err = -EADDRINUSE;
926 struct sock *osk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 int len;
928
929 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +0000930 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 len = 0;
Sasha Levinb67bfe02013-02-27 17:06:00 -0800932 sk_for_each(osk, head) {
Eric W. Biederman15e47302012-09-07 20:12:54 +0000933 if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 break;
935 len++;
936 }
Sasha Levinb67bfe02013-02-27 17:06:00 -0800937 if (osk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto err;
939
940 err = -EBUSY;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000941 if (nlk_sk(sk)->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 goto err;
943
944 err = -ENOMEM;
945 if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX))
946 goto err;
947
Eric W. Biederman15e47302012-09-07 20:12:54 +0000948 if (len && nl_portid_hash_dilute(hash, len))
949 head = nl_portid_hashfn(hash, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 hash->entries++;
Eric W. Biederman15e47302012-09-07 20:12:54 +0000951 nlk_sk(sk)->portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 sk_add_node(sk, head);
953 err = 0;
954
955err:
956 netlink_table_ungrab();
957 return err;
958}
959
960static void netlink_remove(struct sock *sk)
961{
962 netlink_table_grab();
David S. Millerd470e3b2005-06-26 15:31:51 -0700963 if (sk_del_node_init(sk))
964 nl_table[sk->sk_protocol].hash.entries--;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -0700965 if (nlk_sk(sk)->subscriptions)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 __sk_del_bind_node(sk);
967 netlink_table_ungrab();
968}
969
970static struct proto netlink_proto = {
971 .name = "NETLINK",
972 .owner = THIS_MODULE,
973 .obj_size = sizeof(struct netlink_sock),
974};
975
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700976static int __netlink_create(struct net *net, struct socket *sock,
977 struct mutex *cb_mutex, int protocol)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978{
979 struct sock *sk;
980 struct netlink_sock *nlk;
Patrick McHardyab33a172005-08-14 19:31:36 -0700981
982 sock->ops = &netlink_ops;
983
Pavel Emelyanov6257ff22007-11-01 00:39:31 -0700984 sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto);
Patrick McHardyab33a172005-08-14 19:31:36 -0700985 if (!sk)
986 return -ENOMEM;
987
988 sock_init_data(sock, sk);
989
990 nlk = nlk_sk(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +0000991 if (cb_mutex) {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700992 nlk->cb_mutex = cb_mutex;
Eric Dumazet658cb352012-04-22 21:30:21 +0000993 } else {
Patrick McHardyffa4d722007-04-25 14:01:17 -0700994 nlk->cb_mutex = &nlk->cb_def_mutex;
995 mutex_init(nlk->cb_mutex);
996 }
Patrick McHardyab33a172005-08-14 19:31:36 -0700997 init_waitqueue_head(&nlk->wait);
Patrick McHardyccdfcc32013-04-17 06:47:01 +0000998#ifdef CONFIG_NETLINK_MMAP
999 mutex_init(&nlk->pg_vec_lock);
1000#endif
Patrick McHardyab33a172005-08-14 19:31:36 -07001001
1002 sk->sk_destruct = netlink_sock_destruct;
1003 sk->sk_protocol = protocol;
1004 return 0;
1005}
1006
Eric Paris3f378b62009-11-05 22:18:14 -08001007static int netlink_create(struct net *net, struct socket *sock, int protocol,
1008 int kern)
Patrick McHardyab33a172005-08-14 19:31:36 -07001009{
1010 struct module *module = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001011 struct mutex *cb_mutex;
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001012 struct netlink_sock *nlk;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001013 void (*bind)(int group);
Patrick McHardyab33a172005-08-14 19:31:36 -07001014 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
1016 sock->state = SS_UNCONNECTED;
1017
1018 if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM)
1019 return -ESOCKTNOSUPPORT;
1020
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001021 if (protocol < 0 || protocol >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return -EPROTONOSUPPORT;
1023
Patrick McHardy77247bb2005-08-14 19:27:13 -07001024 netlink_lock_table();
Johannes Berg95a5afc2008-10-16 15:24:51 -07001025#ifdef CONFIG_MODULES
Patrick McHardyab33a172005-08-14 19:31:36 -07001026 if (!nl_table[protocol].registered) {
Patrick McHardy77247bb2005-08-14 19:27:13 -07001027 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001028 request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol);
Patrick McHardy77247bb2005-08-14 19:27:13 -07001029 netlink_lock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001030 }
Patrick McHardyab33a172005-08-14 19:31:36 -07001031#endif
1032 if (nl_table[protocol].registered &&
1033 try_module_get(nl_table[protocol].module))
1034 module = nl_table[protocol].module;
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001035 else
1036 err = -EPROTONOSUPPORT;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07001037 cb_mutex = nl_table[protocol].cb_mutex;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001038 bind = nl_table[protocol].bind;
Patrick McHardy77247bb2005-08-14 19:27:13 -07001039 netlink_unlock_table();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001040
Alexey Dobriyan974c37e2010-01-30 10:05:05 +00001041 if (err < 0)
1042 goto out;
1043
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001044 err = __netlink_create(net, sock, cb_mutex, protocol);
1045 if (err < 0)
Patrick McHardyab33a172005-08-14 19:31:36 -07001046 goto out_module;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
David S. Miller6f756a82008-11-23 17:34:03 -08001048 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001049 sock_prot_inuse_add(net, &netlink_proto, 1);
David S. Miller6f756a82008-11-23 17:34:03 -08001050 local_bh_enable();
1051
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001052 nlk = nlk_sk(sock->sk);
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001053 nlk->module = module;
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001054 nlk->netlink_bind = bind;
Patrick McHardyab33a172005-08-14 19:31:36 -07001055out:
1056 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Patrick McHardyab33a172005-08-14 19:31:36 -07001058out_module:
1059 module_put(module);
1060 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
1063static int netlink_release(struct socket *sock)
1064{
1065 struct sock *sk = sock->sk;
1066 struct netlink_sock *nlk;
1067
1068 if (!sk)
1069 return 0;
1070
1071 netlink_remove(sk);
Denis Lunevac57b3a2007-04-18 17:05:58 -07001072 sock_orphan(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 nlk = nlk_sk(sk);
1074
Herbert Xu3f660d62007-05-03 03:17:14 -07001075 /*
1076 * OK. Socket is unlinked, any packets that arrive now
1077 * will be purged.
1078 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 sock->sk = NULL;
1081 wake_up_interruptible_all(&nlk->wait);
1082
1083 skb_queue_purge(&sk->sk_write_queue);
1084
Eric W. Biederman15e47302012-09-07 20:12:54 +00001085 if (nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 struct netlink_notify n = {
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001087 .net = sock_net(sk),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 .protocol = sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001089 .portid = nlk->portid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 };
Alan Sterne041c682006-03-27 01:16:30 -08001091 atomic_notifier_call_chain(&netlink_chain,
1092 NETLINK_URELEASE, &n);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001093 }
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001094
Mariusz Kozlowski5e7c0012007-01-02 15:24:30 -08001095 module_put(nlk->module);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07001096
Patrick McHardy4277a082006-03-20 18:52:01 -08001097 netlink_table_grab();
Denis V. Lunevaed81562007-10-10 21:14:32 -07001098 if (netlink_is_kernel(sk)) {
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001099 BUG_ON(nl_table[sk->sk_protocol].registered == 0);
1100 if (--nl_table[sk->sk_protocol].registered == 0) {
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001101 struct listeners *old;
1102
1103 old = nl_deref_protected(nl_table[sk->sk_protocol].listeners);
1104 RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL);
1105 kfree_rcu(old, rcu);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001106 nl_table[sk->sk_protocol].module = NULL;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001107 nl_table[sk->sk_protocol].bind = NULL;
1108 nl_table[sk->sk_protocol].flags = 0;
Denis V. Lunev869e58f2008-01-18 23:53:31 -08001109 nl_table[sk->sk_protocol].registered = 0;
1110 }
Eric Dumazet658cb352012-04-22 21:30:21 +00001111 } else if (nlk->subscriptions) {
Patrick McHardy4277a082006-03-20 18:52:01 -08001112 netlink_update_listeners(sk);
Eric Dumazet658cb352012-04-22 21:30:21 +00001113 }
Patrick McHardy4277a082006-03-20 18:52:01 -08001114 netlink_table_ungrab();
Patrick McHardy77247bb2005-08-14 19:27:13 -07001115
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001116 kfree(nlk->groups);
1117 nlk->groups = NULL;
1118
Eric Dumazet37558102008-11-24 14:05:22 -08001119 local_bh_disable();
Eric Dumazetc1fd3b92008-11-23 15:48:22 -08001120 sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1);
Eric Dumazet37558102008-11-24 14:05:22 -08001121 local_bh_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 sock_put(sk);
1123 return 0;
1124}
1125
1126static int netlink_autobind(struct socket *sock)
1127{
1128 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001129 struct net *net = sock_net(sk);
Eric W. Biederman15e47302012-09-07 20:12:54 +00001130 struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 struct hlist_head *head;
1132 struct sock *osk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001133 s32 portid = task_tgid_vnr(current);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 int err;
1135 static s32 rover = -4097;
1136
1137retry:
1138 cond_resched();
1139 netlink_table_grab();
Eric W. Biederman15e47302012-09-07 20:12:54 +00001140 head = nl_portid_hashfn(hash, portid);
Sasha Levinb67bfe02013-02-27 17:06:00 -08001141 sk_for_each(osk, head) {
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001142 if (!net_eq(sock_net(osk), net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001143 continue;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001144 if (nlk_sk(osk)->portid == portid) {
1145 /* Bind collision, search negative portid values. */
1146 portid = rover--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (rover > -4097)
1148 rover = -4097;
1149 netlink_table_ungrab();
1150 goto retry;
1151 }
1152 }
1153 netlink_table_ungrab();
1154
Eric W. Biederman15e47302012-09-07 20:12:54 +00001155 err = netlink_insert(sk, net, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (err == -EADDRINUSE)
1157 goto retry;
David S. Millerd470e3b2005-06-26 15:31:51 -07001158
1159 /* If 2 threads race to autobind, that is fine. */
1160 if (err == -EBUSY)
1161 err = 0;
1162
1163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164}
1165
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001166static inline int netlink_capable(const struct socket *sock, unsigned int flag)
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001167{
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001168 return (nl_table[sock->sk->sk_protocol].flags & flag) ||
Eric W. Biedermandf008c92012-11-16 03:03:07 +00001169 ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001170}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001172static void
1173netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions)
1174{
1175 struct netlink_sock *nlk = nlk_sk(sk);
1176
1177 if (nlk->subscriptions && !subscriptions)
1178 __sk_del_bind_node(sk);
1179 else if (!nlk->subscriptions && subscriptions)
1180 sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list);
1181 nlk->subscriptions = subscriptions;
1182}
1183
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001184static int netlink_realloc_groups(struct sock *sk)
Patrick McHardy513c2502005-09-06 15:43:59 -07001185{
1186 struct netlink_sock *nlk = nlk_sk(sk);
1187 unsigned int groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001188 unsigned long *new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001189 int err = 0;
1190
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001191 netlink_table_grab();
1192
Patrick McHardy513c2502005-09-06 15:43:59 -07001193 groups = nl_table[sk->sk_protocol].groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001194 if (!nl_table[sk->sk_protocol].registered) {
Patrick McHardy513c2502005-09-06 15:43:59 -07001195 err = -ENOENT;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001196 goto out_unlock;
1197 }
Patrick McHardy513c2502005-09-06 15:43:59 -07001198
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001199 if (nlk->ngroups >= groups)
1200 goto out_unlock;
Patrick McHardy513c2502005-09-06 15:43:59 -07001201
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001202 new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC);
1203 if (new_groups == NULL) {
1204 err = -ENOMEM;
1205 goto out_unlock;
1206 }
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001207 memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0,
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001208 NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups));
1209
1210 nlk->groups = new_groups;
Patrick McHardy513c2502005-09-06 15:43:59 -07001211 nlk->ngroups = groups;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001212 out_unlock:
1213 netlink_table_ungrab();
1214 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001215}
1216
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001217static int netlink_bind(struct socket *sock, struct sockaddr *addr,
1218 int addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
1220 struct sock *sk = sock->sk;
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001221 struct net *net = sock_net(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 struct netlink_sock *nlk = nlk_sk(sk);
1223 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
1224 int err;
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001225
Hannes Frederic Sowa4e4b5372012-12-15 15:42:19 +00001226 if (addr_len < sizeof(struct sockaddr_nl))
1227 return -EINVAL;
1228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (nladdr->nl_family != AF_NETLINK)
1230 return -EINVAL;
1231
1232 /* Only superuser is allowed to listen multicasts */
Patrick McHardy513c2502005-09-06 15:43:59 -07001233 if (nladdr->nl_groups) {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001234 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy513c2502005-09-06 15:43:59 -07001235 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001236 err = netlink_realloc_groups(sk);
1237 if (err)
1238 return err;
Patrick McHardy513c2502005-09-06 15:43:59 -07001239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Eric W. Biederman15e47302012-09-07 20:12:54 +00001241 if (nlk->portid) {
1242 if (nladdr->nl_pid != nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return -EINVAL;
1244 } else {
1245 err = nladdr->nl_pid ?
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001246 netlink_insert(sk, net, nladdr->nl_pid) :
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 netlink_autobind(sock);
1248 if (err)
1249 return err;
1250 }
1251
Patrick McHardy513c2502005-09-06 15:43:59 -07001252 if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0]))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 return 0;
1254
1255 netlink_table_grab();
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001256 netlink_update_subscriptions(sk, nlk->subscriptions +
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001257 hweight32(nladdr->nl_groups) -
1258 hweight32(nlk->groups[0]));
1259 nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08001260 netlink_update_listeners(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 netlink_table_ungrab();
1262
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001263 if (nlk->netlink_bind && nlk->groups[0]) {
1264 int i;
1265
1266 for (i=0; i<nlk->ngroups; i++) {
1267 if (test_bit(i, nlk->groups))
1268 nlk->netlink_bind(i);
1269 }
1270 }
1271
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 return 0;
1273}
1274
1275static int netlink_connect(struct socket *sock, struct sockaddr *addr,
1276 int alen, int flags)
1277{
1278 int err = 0;
1279 struct sock *sk = sock->sk;
1280 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001281 struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Changli Gao6503d962010-03-31 22:58:26 +00001283 if (alen < sizeof(addr->sa_family))
1284 return -EINVAL;
1285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (addr->sa_family == AF_UNSPEC) {
1287 sk->sk_state = NETLINK_UNCONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001288 nlk->dst_portid = 0;
Patrick McHardyd629b832005-08-14 19:27:50 -07001289 nlk->dst_group = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291 }
1292 if (addr->sa_family != AF_NETLINK)
1293 return -EINVAL;
1294
1295 /* Only superuser is allowed to send multicasts */
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001296 if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return -EPERM;
1298
Eric W. Biederman15e47302012-09-07 20:12:54 +00001299 if (!nlk->portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 err = netlink_autobind(sock);
1301
1302 if (err == 0) {
1303 sk->sk_state = NETLINK_CONNECTED;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001304 nlk->dst_portid = nladdr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001305 nlk->dst_group = ffs(nladdr->nl_groups);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
1308 return err;
1309}
1310
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001311static int netlink_getname(struct socket *sock, struct sockaddr *addr,
1312 int *addr_len, int peer)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313{
1314 struct sock *sk = sock->sk;
1315 struct netlink_sock *nlk = nlk_sk(sk);
Cyrill Gorcunov13cfa972009-11-08 05:51:19 +00001316 DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 nladdr->nl_family = AF_NETLINK;
1319 nladdr->nl_pad = 0;
1320 *addr_len = sizeof(*nladdr);
1321
1322 if (peer) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001323 nladdr->nl_pid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07001324 nladdr->nl_groups = netlink_group_mask(nlk->dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00001326 nladdr->nl_pid = nlk->portid;
Patrick McHardy513c2502005-09-06 15:43:59 -07001327 nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329 return 0;
1330}
1331
1332static void netlink_overrun(struct sock *sk)
1333{
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001334 struct netlink_sock *nlk = nlk_sk(sk);
1335
1336 if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) {
Patrick McHardycd967e02013-04-17 06:46:56 +00001337 if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001338 sk->sk_err = ENOBUFS;
1339 sk->sk_error_report(sk);
1340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001342 atomic_inc(&sk->sk_drops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343}
1344
Eric W. Biederman15e47302012-09-07 20:12:54 +00001345static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 struct sock *sock;
1348 struct netlink_sock *nlk;
1349
Eric W. Biederman15e47302012-09-07 20:12:54 +00001350 sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 if (!sock)
1352 return ERR_PTR(-ECONNREFUSED);
1353
1354 /* Don't bother queuing skb if kernel socket has no input function */
1355 nlk = nlk_sk(sock);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001356 if (sock->sk_state == NETLINK_CONNECTED &&
Eric W. Biederman15e47302012-09-07 20:12:54 +00001357 nlk->dst_portid != nlk_sk(ssk)->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 sock_put(sock);
1359 return ERR_PTR(-ECONNREFUSED);
1360 }
1361 return sock;
1362}
1363
1364struct sock *netlink_getsockbyfilp(struct file *filp)
1365{
Al Viro496ad9a2013-01-23 17:07:38 -05001366 struct inode *inode = file_inode(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 struct sock *sock;
1368
1369 if (!S_ISSOCK(inode->i_mode))
1370 return ERR_PTR(-ENOTSOCK);
1371
1372 sock = SOCKET_I(inode)->sk;
1373 if (sock->sk_family != AF_NETLINK)
1374 return ERR_PTR(-EINVAL);
1375
1376 sock_hold(sock);
1377 return sock;
1378}
1379
1380/*
1381 * Attach a skb to a netlink socket.
1382 * The caller must hold a reference to the destination socket. On error, the
1383 * reference is dropped. The skb is not send to the destination, just all
1384 * all error checks are performed and memory in the queue is reserved.
1385 * Return values:
1386 * < 0: error. skb freed, reference to sock dropped.
1387 * 0: continue
1388 * 1: repeat lookup - reference dropped while waiting for socket memory.
1389 */
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001390int netlink_attachskb(struct sock *sk, struct sk_buff *skb,
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001391 long *timeo, struct sock *ssk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct netlink_sock *nlk;
1394
1395 nlk = nlk_sk(sk);
1396
Patrick McHardy5fd96122013-04-17 06:47:03 +00001397 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
1398 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
1399 !netlink_skb_is_mmaped(skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 DECLARE_WAITQUEUE(wait, current);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001401 if (!*timeo) {
Denis V. Lunevaed81562007-10-10 21:14:32 -07001402 if (!ssk || netlink_is_kernel(ssk))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 netlink_overrun(sk);
1404 sock_put(sk);
1405 kfree_skb(skb);
1406 return -EAGAIN;
1407 }
1408
1409 __set_current_state(TASK_INTERRUPTIBLE);
1410 add_wait_queue(&nlk->wait, &wait);
1411
1412 if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf ||
Patrick McHardycd967e02013-04-17 06:46:56 +00001413 test_bit(NETLINK_CONGESTED, &nlk->state)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 !sock_flag(sk, SOCK_DEAD))
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001415 *timeo = schedule_timeout(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416
1417 __set_current_state(TASK_RUNNING);
1418 remove_wait_queue(&nlk->wait, &wait);
1419 sock_put(sk);
1420
1421 if (signal_pending(current)) {
1422 kfree_skb(skb);
Patrick McHardyc3d8d1e2007-11-07 02:42:09 -08001423 return sock_intr_errno(*timeo);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 }
1425 return 1;
1426 }
Patrick McHardycf0a0182013-04-17 06:47:00 +00001427 netlink_skb_set_owner_r(skb, sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429}
1430
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001431static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 int len = skb->len;
1434
Patrick McHardyf9c22882013-04-17 06:47:04 +00001435#ifdef CONFIG_NETLINK_MMAP
1436 if (netlink_skb_is_mmaped(skb))
1437 netlink_queue_mmaped_skb(sk, skb);
1438 else if (netlink_rx_is_mmaped(sk))
1439 netlink_ring_set_copied(sk, skb);
1440 else
1441#endif /* CONFIG_NETLINK_MMAP */
1442 skb_queue_tail(&sk->sk_receive_queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 sk->sk_data_ready(sk, len);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001444 return len;
1445}
1446
1447int netlink_sendskb(struct sock *sk, struct sk_buff *skb)
1448{
1449 int len = __netlink_sendskb(sk, skb);
1450
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 sock_put(sk);
1452 return len;
1453}
1454
1455void netlink_detachskb(struct sock *sk, struct sk_buff *skb)
1456{
1457 kfree_skb(skb);
1458 sock_put(sk);
1459}
1460
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001461static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462{
1463 int delta;
1464
Patrick McHardy1298ca42013-04-17 06:46:59 +00001465 WARN_ON(skb->sk != NULL);
Patrick McHardy5fd96122013-04-17 06:47:03 +00001466 if (netlink_skb_is_mmaped(skb))
1467 return skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Arnaldo Carvalho de Melo4305b542007-04-19 20:43:29 -07001469 delta = skb->end - skb->tail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 if (delta * 2 < skb->truesize)
1471 return skb;
1472
1473 if (skb_shared(skb)) {
1474 struct sk_buff *nskb = skb_clone(skb, allocation);
1475 if (!nskb)
1476 return skb;
Eric Dumazet8460c002012-04-19 02:24:28 +00001477 consume_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 skb = nskb;
1479 }
1480
1481 if (!pskb_expand_head(skb, 0, -delta, allocation))
1482 skb->truesize -= delta;
1483
1484 return skb;
1485}
1486
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001487static void netlink_rcv_wake(struct sock *sk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001488{
1489 struct netlink_sock *nlk = nlk_sk(sk);
1490
1491 if (skb_queue_empty(&sk->sk_receive_queue))
Patrick McHardycd967e02013-04-17 06:46:56 +00001492 clear_bit(NETLINK_CONGESTED, &nlk->state);
1493 if (!test_bit(NETLINK_CONGESTED, &nlk->state))
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001494 wake_up_interruptible(&nlk->wait);
1495}
1496
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001497static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb,
1498 struct sock *ssk)
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001499{
1500 int ret;
1501 struct netlink_sock *nlk = nlk_sk(sk);
1502
1503 ret = -ECONNREFUSED;
1504 if (nlk->netlink_rcv != NULL) {
1505 ret = skb->len;
Patrick McHardycf0a0182013-04-17 06:47:00 +00001506 netlink_skb_set_owner_r(skb, sk);
Patrick McHardye32123e2013-04-17 06:46:57 +00001507 NETLINK_CB(skb).sk = ssk;
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001508 nlk->netlink_rcv(skb);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00001509 consume_skb(skb);
1510 } else {
1511 kfree_skb(skb);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001512 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001513 sock_put(sk);
1514 return ret;
1515}
1516
1517int netlink_unicast(struct sock *ssk, struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00001518 u32 portid, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519{
1520 struct sock *sk;
1521 int err;
1522 long timeo;
1523
1524 skb = netlink_trim(skb, gfp_any());
1525
1526 timeo = sock_sndtimeo(ssk, nonblock);
1527retry:
Eric W. Biederman15e47302012-09-07 20:12:54 +00001528 sk = netlink_getsockbyportid(ssk, portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (IS_ERR(sk)) {
1530 kfree_skb(skb);
1531 return PTR_ERR(sk);
1532 }
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001533 if (netlink_is_kernel(sk))
Eric W. Biederman3fbc2902012-05-24 17:21:27 -06001534 return netlink_unicast_kernel(sk, skb, ssk);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07001535
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001536 if (sk_filter(sk, skb)) {
Wang Chen84874602008-07-01 19:55:09 -07001537 err = skb->len;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001538 kfree_skb(skb);
1539 sock_put(sk);
1540 return err;
1541 }
1542
Denis V. Lunev9457afe2008-06-05 11:23:39 -07001543 err = netlink_attachskb(sk, skb, &timeo, ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 if (err == 1)
1545 goto retry;
1546 if (err)
1547 return err;
1548
Denis V. Lunev7ee015e2007-10-10 21:14:03 -07001549 return netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001551EXPORT_SYMBOL(netlink_unicast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552
Patrick McHardyf9c22882013-04-17 06:47:04 +00001553struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size,
1554 u32 dst_portid, gfp_t gfp_mask)
1555{
1556#ifdef CONFIG_NETLINK_MMAP
1557 struct sock *sk = NULL;
1558 struct sk_buff *skb;
1559 struct netlink_ring *ring;
1560 struct nl_mmap_hdr *hdr;
1561 unsigned int maxlen;
1562
1563 sk = netlink_getsockbyportid(ssk, dst_portid);
1564 if (IS_ERR(sk))
1565 goto out;
1566
1567 ring = &nlk_sk(sk)->rx_ring;
1568 /* fast-path without atomic ops for common case: non-mmaped receiver */
1569 if (ring->pg_vec == NULL)
1570 goto out_put;
1571
1572 skb = alloc_skb_head(gfp_mask);
1573 if (skb == NULL)
1574 goto err1;
1575
1576 spin_lock_bh(&sk->sk_receive_queue.lock);
1577 /* check again under lock */
1578 if (ring->pg_vec == NULL)
1579 goto out_free;
1580
1581 maxlen = ring->frame_size - NL_MMAP_HDRLEN;
1582 if (maxlen < size)
1583 goto out_free;
1584
1585 netlink_forward_ring(ring);
1586 hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED);
1587 if (hdr == NULL)
1588 goto err2;
1589 netlink_ring_setup_skb(skb, sk, ring, hdr);
1590 netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED);
1591 atomic_inc(&ring->pending);
1592 netlink_increment_head(ring);
1593
1594 spin_unlock_bh(&sk->sk_receive_queue.lock);
1595 return skb;
1596
1597err2:
1598 kfree_skb(skb);
1599 spin_unlock_bh(&sk->sk_receive_queue.lock);
1600err1:
1601 sock_put(sk);
1602 return NULL;
1603
1604out_free:
1605 kfree_skb(skb);
1606 spin_unlock_bh(&sk->sk_receive_queue.lock);
1607out_put:
1608 sock_put(sk);
1609out:
1610#endif
1611 return alloc_skb(size, gfp_mask);
1612}
1613EXPORT_SYMBOL_GPL(netlink_alloc_skb);
1614
Patrick McHardy4277a082006-03-20 18:52:01 -08001615int netlink_has_listeners(struct sock *sk, unsigned int group)
1616{
1617 int res = 0;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001618 struct listeners *listeners;
Patrick McHardy4277a082006-03-20 18:52:01 -08001619
Denis V. Lunevaed81562007-10-10 21:14:32 -07001620 BUG_ON(!netlink_is_kernel(sk));
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001621
1622 rcu_read_lock();
1623 listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners);
1624
Eric Dumazet6d772ac2012-10-18 03:21:55 +00001625 if (listeners && group - 1 < nl_table[sk->sk_protocol].groups)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00001626 res = test_bit(group - 1, listeners->masks);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001627
1628 rcu_read_unlock();
1629
Patrick McHardy4277a082006-03-20 18:52:01 -08001630 return res;
1631}
1632EXPORT_SYMBOL_GPL(netlink_has_listeners);
1633
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001634static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635{
1636 struct netlink_sock *nlk = nlk_sk(sk);
1637
1638 if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf &&
Patrick McHardycd967e02013-04-17 06:46:56 +00001639 !test_bit(NETLINK_CONGESTED, &nlk->state)) {
Patrick McHardycf0a0182013-04-17 06:47:00 +00001640 netlink_skb_set_owner_r(skb, sk);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00001641 __netlink_sendskb(sk, skb);
stephen hemminger2c6458002011-12-22 08:52:03 +00001642 return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 }
1644 return -1;
1645}
1646
1647struct netlink_broadcast_data {
1648 struct sock *exclude_sk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001649 struct net *net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001650 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 u32 group;
1652 int failure;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001653 int delivery_failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654 int congested;
1655 int delivered;
Al Viro7d877f32005-10-21 03:20:43 -04001656 gfp_t allocation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 struct sk_buff *skb, *skb2;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001658 int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data);
1659 void *tx_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660};
1661
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001662static int do_one_broadcast(struct sock *sk,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 struct netlink_broadcast_data *p)
1664{
1665 struct netlink_sock *nlk = nlk_sk(sk);
1666 int val;
1667
1668 if (p->exclude_sk == sk)
1669 goto out;
1670
Eric W. Biederman15e47302012-09-07 20:12:54 +00001671 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001672 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 goto out;
1674
YOSHIFUJI Hideaki878628f2008-03-26 03:57:35 +09001675 if (!net_eq(sock_net(sk), p->net))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001676 goto out;
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 if (p->failure) {
1679 netlink_overrun(sk);
1680 goto out;
1681 }
1682
1683 sock_hold(sk);
1684 if (p->skb2 == NULL) {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001685 if (skb_shared(p->skb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 p->skb2 = skb_clone(p->skb, p->allocation);
1687 } else {
Tommy S. Christensen68acc022005-05-19 13:06:35 -07001688 p->skb2 = skb_get(p->skb);
1689 /*
1690 * skb ownership may have been set when
1691 * delivered to a previous socket.
1692 */
1693 skb_orphan(p->skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 }
1695 }
1696 if (p->skb2 == NULL) {
1697 netlink_overrun(sk);
1698 /* Clone failed. Notify ALL listeners. */
1699 p->failure = 1;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001700 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1701 p->delivery_failure = 1;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001702 } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) {
1703 kfree_skb(p->skb2);
1704 p->skb2 = NULL;
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07001705 } else if (sk_filter(sk, p->skb2)) {
1706 kfree_skb(p->skb2);
1707 p->skb2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) {
1709 netlink_overrun(sk);
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001710 if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR)
1711 p->delivery_failure = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 } else {
1713 p->congested |= val;
1714 p->delivered = 1;
1715 p->skb2 = NULL;
1716 }
1717 sock_put(sk);
1718
1719out:
1720 return 0;
1721}
1722
Eric W. Biederman15e47302012-09-07 20:12:54 +00001723int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001724 u32 group, gfp_t allocation,
1725 int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data),
1726 void *filter_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09001728 struct net *net = sock_net(ssk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 struct netlink_broadcast_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 struct sock *sk;
1731
1732 skb = netlink_trim(skb, allocation);
1733
1734 info.exclude_sk = ssk;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001735 info.net = net;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001736 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 info.group = group;
1738 info.failure = 0;
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001739 info.delivery_failure = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 info.congested = 0;
1741 info.delivered = 0;
1742 info.allocation = allocation;
1743 info.skb = skb;
1744 info.skb2 = NULL;
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001745 info.tx_filter = filter;
1746 info.tx_data = filter_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747
1748 /* While we sleep in clone, do not allow to change socket list */
1749
1750 netlink_lock_table();
1751
Sasha Levinb67bfe02013-02-27 17:06:00 -08001752 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 do_one_broadcast(sk, &info);
1754
Neil Horman70d4bf62010-07-20 06:45:56 +00001755 consume_skb(skb);
Tommy S. Christensenaa1c6a62005-05-19 13:07:32 -07001756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 netlink_unlock_table();
1758
Neil Horman70d4bf62010-07-20 06:45:56 +00001759 if (info.delivery_failure) {
1760 kfree_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001761 return -ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001762 }
1763 consume_skb(info.skb2);
Pablo Neira Ayusoff491a72009-02-05 23:56:36 -08001764
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 if (info.delivered) {
1766 if (info.congested && (allocation & __GFP_WAIT))
1767 yield();
1768 return 0;
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 return -ESRCH;
1771}
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001772EXPORT_SYMBOL(netlink_broadcast_filtered);
1773
Eric W. Biederman15e47302012-09-07 20:12:54 +00001774int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001775 u32 group, gfp_t allocation)
1776{
Eric W. Biederman15e47302012-09-07 20:12:54 +00001777 return netlink_broadcast_filtered(ssk, skb, portid, group, allocation,
Eric W. Biederman910a7e92010-05-04 17:36:46 -07001778 NULL, NULL);
1779}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08001780EXPORT_SYMBOL(netlink_broadcast);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782struct netlink_set_err_data {
1783 struct sock *exclude_sk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001784 u32 portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 u32 group;
1786 int code;
1787};
1788
stephen hemmingerb57ef81f2011-12-22 08:52:02 +00001789static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
1791 struct netlink_sock *nlk = nlk_sk(sk);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001792 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 if (sk == p->exclude_sk)
1795 goto out;
1796
Octavian Purdila09ad9bc2009-11-25 15:14:13 -08001797 if (!net_eq(sock_net(sk), sock_net(p->exclude_sk)))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02001798 goto out;
1799
Eric W. Biederman15e47302012-09-07 20:12:54 +00001800 if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups ||
Patrick McHardyf7fa9b12005-08-15 12:29:13 -07001801 !test_bit(p->group - 1, nlk->groups))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 goto out;
1803
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001804 if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) {
1805 ret = 1;
1806 goto out;
1807 }
1808
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 sk->sk_err = p->code;
1810 sk->sk_error_report(sk);
1811out:
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001812 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813}
1814
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001815/**
1816 * netlink_set_err - report error to broadcast listeners
1817 * @ssk: the kernel netlink socket, as returned by netlink_kernel_create()
Eric W. Biederman15e47302012-09-07 20:12:54 +00001818 * @portid: the PORTID of a process that we want to skip (if any)
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001819 * @groups: the broadcast group that will notice the error
1820 * @code: error code, must be negative (as usual in kernelspace)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001821 *
1822 * This function returns the number of broadcast listeners that have set the
1823 * NETLINK_RECV_NO_ENOBUFS socket option.
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001824 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00001825int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826{
1827 struct netlink_set_err_data info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct sock *sk;
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001829 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 info.exclude_sk = ssk;
Eric W. Biederman15e47302012-09-07 20:12:54 +00001832 info.portid = portid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 info.group = group;
Pablo Neira Ayuso4843b932009-03-03 23:37:30 -08001834 /* sk->sk_err wants a positive error value */
1835 info.code = -code;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836
1837 read_lock(&nl_table_lock);
1838
Sasha Levinb67bfe02013-02-27 17:06:00 -08001839 sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list)
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001840 ret += do_one_set_err(sk, &info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 read_unlock(&nl_table_lock);
Pablo Neira Ayuso1a503072010-03-18 14:24:42 +00001843 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844}
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +01001845EXPORT_SYMBOL(netlink_set_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
Johannes Berg84659eb2007-07-18 15:47:05 -07001847/* must be called with netlink table grabbed */
1848static void netlink_update_socket_mc(struct netlink_sock *nlk,
1849 unsigned int group,
1850 int is_new)
1851{
1852 int old, new = !!is_new, subscriptions;
1853
1854 old = test_bit(group - 1, nlk->groups);
1855 subscriptions = nlk->subscriptions - old + new;
1856 if (new)
1857 __set_bit(group - 1, nlk->groups);
1858 else
1859 __clear_bit(group - 1, nlk->groups);
1860 netlink_update_subscriptions(&nlk->sk, subscriptions);
1861 netlink_update_listeners(&nlk->sk);
1862}
1863
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001864static int netlink_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -07001865 char __user *optval, unsigned int optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001866{
1867 struct sock *sk = sock->sk;
1868 struct netlink_sock *nlk = nlk_sk(sk);
Johannes Bergeb496532007-07-18 02:07:51 -07001869 unsigned int val = 0;
1870 int err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001871
1872 if (level != SOL_NETLINK)
1873 return -ENOPROTOOPT;
1874
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001875 if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING &&
1876 optlen >= sizeof(int) &&
Johannes Bergeb496532007-07-18 02:07:51 -07001877 get_user(val, (unsigned int __user *)optval))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001878 return -EFAULT;
1879
1880 switch (optname) {
1881 case NETLINK_PKTINFO:
1882 if (val)
1883 nlk->flags |= NETLINK_RECV_PKTINFO;
1884 else
1885 nlk->flags &= ~NETLINK_RECV_PKTINFO;
1886 err = 0;
1887 break;
1888 case NETLINK_ADD_MEMBERSHIP:
1889 case NETLINK_DROP_MEMBERSHIP: {
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00001890 if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV))
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001891 return -EPERM;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07001892 err = netlink_realloc_groups(sk);
1893 if (err)
1894 return err;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001895 if (!val || val - 1 >= nlk->ngroups)
1896 return -EINVAL;
1897 netlink_table_grab();
Johannes Berg84659eb2007-07-18 15:47:05 -07001898 netlink_update_socket_mc(nlk, val,
1899 optname == NETLINK_ADD_MEMBERSHIP);
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001900 netlink_table_ungrab();
Pablo Neira Ayuso03292742012-06-29 06:15:22 +00001901
1902 if (nlk->netlink_bind)
1903 nlk->netlink_bind(val);
1904
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001905 err = 0;
1906 break;
1907 }
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001908 case NETLINK_BROADCAST_ERROR:
1909 if (val)
1910 nlk->flags |= NETLINK_BROADCAST_SEND_ERROR;
1911 else
1912 nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR;
1913 err = 0;
1914 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001915 case NETLINK_NO_ENOBUFS:
1916 if (val) {
1917 nlk->flags |= NETLINK_RECV_NO_ENOBUFS;
Patrick McHardycd967e02013-04-17 06:46:56 +00001918 clear_bit(NETLINK_CONGESTED, &nlk->state);
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001919 wake_up_interruptible(&nlk->wait);
Eric Dumazet658cb352012-04-22 21:30:21 +00001920 } else {
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001921 nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS;
Eric Dumazet658cb352012-04-22 21:30:21 +00001922 }
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001923 err = 0;
1924 break;
Patrick McHardyccdfcc32013-04-17 06:47:01 +00001925#ifdef CONFIG_NETLINK_MMAP
1926 case NETLINK_RX_RING:
1927 case NETLINK_TX_RING: {
1928 struct nl_mmap_req req;
1929
1930 /* Rings might consume more memory than queue limits, require
1931 * CAP_NET_ADMIN.
1932 */
1933 if (!capable(CAP_NET_ADMIN))
1934 return -EPERM;
1935 if (optlen < sizeof(req))
1936 return -EINVAL;
1937 if (copy_from_user(&req, optval, sizeof(req)))
1938 return -EFAULT;
1939 err = netlink_set_ring(sk, &req, false,
1940 optname == NETLINK_TX_RING);
1941 break;
1942 }
1943#endif /* CONFIG_NETLINK_MMAP */
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001944 default:
1945 err = -ENOPROTOOPT;
1946 }
1947 return err;
1948}
1949
1950static int netlink_getsockopt(struct socket *sock, int level, int optname,
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09001951 char __user *optval, int __user *optlen)
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001952{
1953 struct sock *sk = sock->sk;
1954 struct netlink_sock *nlk = nlk_sk(sk);
1955 int len, val, err;
1956
1957 if (level != SOL_NETLINK)
1958 return -ENOPROTOOPT;
1959
1960 if (get_user(len, optlen))
1961 return -EFAULT;
1962 if (len < 0)
1963 return -EINVAL;
1964
1965 switch (optname) {
1966 case NETLINK_PKTINFO:
1967 if (len < sizeof(int))
1968 return -EINVAL;
1969 len = sizeof(int);
1970 val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0;
Heiko Carstensa27b58f2006-10-30 15:06:12 -08001971 if (put_user(len, optlen) ||
1972 put_user(val, optval))
1973 return -EFAULT;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001974 err = 0;
1975 break;
Pablo Neira Ayusobe0c22a2009-02-18 01:40:43 +00001976 case NETLINK_BROADCAST_ERROR:
1977 if (len < sizeof(int))
1978 return -EINVAL;
1979 len = sizeof(int);
1980 val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0;
1981 if (put_user(len, optlen) ||
1982 put_user(val, optval))
1983 return -EFAULT;
1984 err = 0;
1985 break;
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07001986 case NETLINK_NO_ENOBUFS:
1987 if (len < sizeof(int))
1988 return -EINVAL;
1989 len = sizeof(int);
1990 val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0;
1991 if (put_user(len, optlen) ||
1992 put_user(val, optval))
1993 return -EFAULT;
1994 err = 0;
1995 break;
Patrick McHardy9a4595b2005-08-15 12:32:15 -07001996 default:
1997 err = -ENOPROTOOPT;
1998 }
1999 return err;
2000}
2001
2002static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
2003{
2004 struct nl_pktinfo info;
2005
2006 info.group = NETLINK_CB(skb).dst_group;
2007 put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info);
2008}
2009
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock,
2011 struct msghdr *msg, size_t len)
2012{
2013 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2014 struct sock *sk = sock->sk;
2015 struct netlink_sock *nlk = nlk_sk(sk);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002016 struct sockaddr_nl *addr = msg->msg_name;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002017 u32 dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002018 u32 dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 struct sk_buff *skb;
2020 int err;
2021 struct scm_cookie scm;
2022
2023 if (msg->msg_flags&MSG_OOB)
2024 return -EOPNOTSUPP;
2025
Eric Dumazet16e57262011-09-19 05:52:27 +00002026 if (NULL == siocb->scm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 siocb->scm = &scm;
Eric Dumazet16e57262011-09-19 05:52:27 +00002028
Eric Dumazete0e3cea2012-08-21 06:21:17 +00002029 err = scm_send(sock, msg, siocb->scm, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 if (err < 0)
2031 return err;
2032
2033 if (msg->msg_namelen) {
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002034 err = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 if (addr->nl_family != AF_NETLINK)
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002036 goto out;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002037 dst_portid = addr->nl_pid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002038 dst_group = ffs(addr->nl_groups);
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002039 err = -EPERM;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002040 if ((dst_group || dst_portid) &&
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002041 !netlink_capable(sock, NL_CFG_F_NONROOT_SEND))
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002042 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 } else {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002044 dst_portid = nlk->dst_portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002045 dst_group = nlk->dst_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047
Eric W. Biederman15e47302012-09-07 20:12:54 +00002048 if (!nlk->portid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 err = netlink_autobind(sock);
2050 if (err)
2051 goto out;
2052 }
2053
Patrick McHardy5fd96122013-04-17 06:47:03 +00002054 if (netlink_tx_is_mmaped(sk) &&
2055 msg->msg_iov->iov_base == NULL) {
2056 err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group,
2057 siocb);
2058 goto out;
2059 }
2060
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 err = -EMSGSIZE;
2062 if (len > sk->sk_sndbuf - 32)
2063 goto out;
2064 err = -ENOBUFS;
Thomas Graf339bf982006-11-10 14:10:15 -08002065 skb = alloc_skb(len, GFP_KERNEL);
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002066 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 goto out;
2068
Eric W. Biederman15e47302012-09-07 20:12:54 +00002069 NETLINK_CB(skb).portid = nlk->portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002070 NETLINK_CB(skb).dst_group = dst_group;
Eric W. Biedermandbe9a412012-09-06 18:20:01 +00002071 NETLINK_CB(skb).creds = siocb->scm->creds;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 err = -EFAULT;
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002074 if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 kfree_skb(skb);
2076 goto out;
2077 }
2078
2079 err = security_netlink_send(sk, skb);
2080 if (err) {
2081 kfree_skb(skb);
2082 goto out;
2083 }
2084
Patrick McHardyd629b832005-08-14 19:27:50 -07002085 if (dst_group) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002087 netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 }
Eric W. Biederman15e47302012-09-07 20:12:54 +00002089 err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090
2091out:
Eric W. Biedermanb47030c2010-06-13 03:31:06 +00002092 scm_destroy(siocb->scm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 return err;
2094}
2095
2096static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock,
2097 struct msghdr *msg, size_t len,
2098 int flags)
2099{
2100 struct sock_iocb *siocb = kiocb_to_siocb(kiocb);
2101 struct scm_cookie scm;
2102 struct sock *sk = sock->sk;
2103 struct netlink_sock *nlk = nlk_sk(sk);
2104 int noblock = flags&MSG_DONTWAIT;
2105 size_t copied;
Johannes Berg68d6ac62010-08-15 21:20:44 +00002106 struct sk_buff *skb, *data_skb;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002107 int err, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109 if (flags&MSG_OOB)
2110 return -EOPNOTSUPP;
2111
2112 copied = 0;
2113
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002114 skb = skb_recv_datagram(sk, flags, noblock, &err);
2115 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto out;
2117
Johannes Berg68d6ac62010-08-15 21:20:44 +00002118 data_skb = skb;
2119
Johannes Berg1dacc762009-07-01 11:26:02 +00002120#ifdef CONFIG_COMPAT_NETLINK_MESSAGES
2121 if (unlikely(skb_shinfo(skb)->frag_list)) {
Johannes Berg1dacc762009-07-01 11:26:02 +00002122 /*
Johannes Berg68d6ac62010-08-15 21:20:44 +00002123 * If this skb has a frag_list, then here that means that we
2124 * will have to use the frag_list skb's data for compat tasks
2125 * and the regular skb's data for normal (non-compat) tasks.
Johannes Berg1dacc762009-07-01 11:26:02 +00002126 *
Johannes Berg68d6ac62010-08-15 21:20:44 +00002127 * If we need to send the compat skb, assign it to the
2128 * 'data_skb' variable so that it will be used below for data
2129 * copying. We keep 'skb' for everything else, including
2130 * freeing both later.
Johannes Berg1dacc762009-07-01 11:26:02 +00002131 */
Johannes Berg68d6ac62010-08-15 21:20:44 +00002132 if (flags & MSG_CMSG_COMPAT)
2133 data_skb = skb_shinfo(skb)->frag_list;
Johannes Berg1dacc762009-07-01 11:26:02 +00002134 }
2135#endif
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 msg->msg_namelen = 0;
2138
Johannes Berg68d6ac62010-08-15 21:20:44 +00002139 copied = data_skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 if (len < copied) {
2141 msg->msg_flags |= MSG_TRUNC;
2142 copied = len;
2143 }
2144
Johannes Berg68d6ac62010-08-15 21:20:44 +00002145 skb_reset_transport_header(data_skb);
2146 err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (msg->msg_name) {
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002149 struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 addr->nl_family = AF_NETLINK;
2151 addr->nl_pad = 0;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002152 addr->nl_pid = NETLINK_CB(skb).portid;
Patrick McHardyd629b832005-08-14 19:27:50 -07002153 addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 msg->msg_namelen = sizeof(*addr);
2155 }
2156
Patrick McHardycc9a06c2006-03-12 20:34:27 -08002157 if (nlk->flags & NETLINK_RECV_PKTINFO)
2158 netlink_cmsg_recv_pktinfo(msg, skb);
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (NULL == siocb->scm) {
2161 memset(&scm, 0, sizeof(scm));
2162 siocb->scm = &scm;
2163 }
2164 siocb->scm->creds = *NETLINK_CREDS(skb);
Patrick McHardy188ccb52007-05-03 03:27:01 -07002165 if (flags & MSG_TRUNC)
Johannes Berg68d6ac62010-08-15 21:20:44 +00002166 copied = data_skb->len;
David S. Millerdaa37662010-08-15 23:21:50 -07002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 skb_free_datagram(sk, skb);
2169
Andrey Vaginb44d2112011-02-21 02:40:47 +00002170 if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) {
2171 ret = netlink_dump(sk);
2172 if (ret) {
2173 sk->sk_err = ret;
2174 sk->sk_error_report(sk);
2175 }
2176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177
2178 scm_recv(sock, msg, siocb->scm, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179out:
2180 netlink_rcv_wake(sk);
2181 return err ? : copied;
2182}
2183
2184static void netlink_data_ready(struct sock *sk, int len)
2185{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002186 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187}
2188
2189/*
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002190 * We export these functions to other modules. They provide a
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 * complete set of kernel non-blocking support for message
2192 * queueing.
2193 */
2194
2195struct sock *
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002196__netlink_kernel_create(struct net *net, int unit, struct module *module,
2197 struct netlink_kernel_cfg *cfg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 struct socket *sock;
2200 struct sock *sk;
Patrick McHardy77247bb2005-08-14 19:27:13 -07002201 struct netlink_sock *nlk;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002202 struct listeners *listeners = NULL;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002203 struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL;
2204 unsigned int groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002206 BUG_ON(!nl_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002208 if (unit < 0 || unit >= MAX_LINKS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return NULL;
2210
2211 if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock))
2212 return NULL;
2213
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002214 /*
2215 * We have to just have a reference on the net from sk, but don't
2216 * get_net it. Besides, we cannot get and then put the net here.
2217 * So we create one inside init_net and the move it to net.
2218 */
2219
2220 if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0)
2221 goto out_sock_release_nosk;
2222
2223 sk = sock->sk;
Denis V. Lunevedf02082008-02-29 11:18:32 -08002224 sk_change_net(sk, net);
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002225
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002226 if (!cfg || cfg->groups < 32)
Patrick McHardy4277a082006-03-20 18:52:01 -08002227 groups = 32;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002228 else
2229 groups = cfg->groups;
Patrick McHardy4277a082006-03-20 18:52:01 -08002230
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002231 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
Patrick McHardy4277a082006-03-20 18:52:01 -08002232 if (!listeners)
2233 goto out_sock_release;
2234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 sk->sk_data_ready = netlink_data_ready;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +00002236 if (cfg && cfg->input)
2237 nlk_sk(sk)->netlink_rcv = cfg->input;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002239 if (netlink_insert(sk, net, 0))
Patrick McHardy77247bb2005-08-14 19:27:13 -07002240 goto out_sock_release;
2241
2242 nlk = nlk_sk(sk);
2243 nlk->flags |= NETLINK_KERNEL_SOCKET;
2244
2245 netlink_table_grab();
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002246 if (!nl_table[unit].registered) {
2247 nl_table[unit].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002248 rcu_assign_pointer(nl_table[unit].listeners, listeners);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002249 nl_table[unit].cb_mutex = cb_mutex;
2250 nl_table[unit].module = module;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002251 if (cfg) {
2252 nl_table[unit].bind = cfg->bind;
2253 nl_table[unit].flags = cfg->flags;
2254 }
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002255 nl_table[unit].registered = 1;
Jesper Juhlf937f1f462007-10-15 01:39:12 -07002256 } else {
2257 kfree(listeners);
Denis V. Lunev869e58f2008-01-18 23:53:31 -08002258 nl_table[unit].registered++;
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002259 }
Patrick McHardy77247bb2005-08-14 19:27:13 -07002260 netlink_table_ungrab();
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002261 return sk;
2262
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002263out_sock_release:
Patrick McHardy4277a082006-03-20 18:52:01 -08002264 kfree(listeners);
Denis V. Lunev9dfbec12008-02-29 11:17:56 -08002265 netlink_kernel_release(sk);
Pavel Emelyanov23fe1862008-01-30 19:31:06 -08002266 return NULL;
2267
2268out_sock_release_nosk:
Harald Welte4fdb3bb2005-08-09 19:40:55 -07002269 sock_release(sock);
Patrick McHardy77247bb2005-08-14 19:27:13 -07002270 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271}
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +00002272EXPORT_SYMBOL(__netlink_kernel_create);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002273
2274void
2275netlink_kernel_release(struct sock *sk)
2276{
Denis V. Lunevedf02082008-02-29 11:18:32 -08002277 sk_release_kernel(sk);
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -08002278}
2279EXPORT_SYMBOL(netlink_kernel_release);
2280
Johannes Bergd136f1b2009-09-12 03:03:15 +00002281int __netlink_change_ngroups(struct sock *sk, unsigned int groups)
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002282{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002283 struct listeners *new, *old;
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002284 struct netlink_table *tbl = &nl_table[sk->sk_protocol];
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002285
2286 if (groups < 32)
2287 groups = 32;
2288
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002289 if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) {
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002290 new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC);
2291 if (!new)
Johannes Bergd136f1b2009-09-12 03:03:15 +00002292 return -ENOMEM;
Eric Dumazet6d772ac2012-10-18 03:21:55 +00002293 old = nl_deref_protected(tbl->listeners);
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002294 memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups));
2295 rcu_assign_pointer(tbl->listeners, new);
2296
Lai Jiangshan37b6b932011-03-15 18:01:42 +08002297 kfree_rcu(old, rcu);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002298 }
2299 tbl->groups = groups;
2300
Johannes Bergd136f1b2009-09-12 03:03:15 +00002301 return 0;
2302}
2303
2304/**
2305 * netlink_change_ngroups - change number of multicast groups
2306 *
2307 * This changes the number of multicast groups that are available
2308 * on a certain netlink family. Note that it is not possible to
2309 * change the number of groups to below 32. Also note that it does
2310 * not implicitly call netlink_clear_multicast_users() when the
2311 * number of groups is reduced.
2312 *
2313 * @sk: The kernel netlink socket, as returned by netlink_kernel_create().
2314 * @groups: The new number of groups.
2315 */
2316int netlink_change_ngroups(struct sock *sk, unsigned int groups)
2317{
2318 int err;
2319
2320 netlink_table_grab();
2321 err = __netlink_change_ngroups(sk, groups);
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002322 netlink_table_ungrab();
Johannes Bergd136f1b2009-09-12 03:03:15 +00002323
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002324 return err;
2325}
Johannes Bergb4ff4f02007-07-18 15:46:06 -07002326
Johannes Bergb8273572009-09-24 15:44:05 -07002327void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2328{
2329 struct sock *sk;
Johannes Bergb8273572009-09-24 15:44:05 -07002330 struct netlink_table *tbl = &nl_table[ksk->sk_protocol];
2331
Sasha Levinb67bfe02013-02-27 17:06:00 -08002332 sk_for_each_bound(sk, &tbl->mc_list)
Johannes Bergb8273572009-09-24 15:44:05 -07002333 netlink_update_socket_mc(nlk_sk(sk), group, 0);
2334}
2335
Johannes Berg84659eb2007-07-18 15:47:05 -07002336/**
2337 * netlink_clear_multicast_users - kick off multicast listeners
2338 *
2339 * This function removes all listeners from the given group.
2340 * @ksk: The kernel netlink socket, as returned by
2341 * netlink_kernel_create().
2342 * @group: The multicast group to clear.
2343 */
2344void netlink_clear_multicast_users(struct sock *ksk, unsigned int group)
2345{
Johannes Berg84659eb2007-07-18 15:47:05 -07002346 netlink_table_grab();
Johannes Bergb8273572009-09-24 15:44:05 -07002347 __netlink_clear_multicast_users(ksk, group);
Johannes Berg84659eb2007-07-18 15:47:05 -07002348 netlink_table_ungrab();
2349}
Johannes Berg84659eb2007-07-18 15:47:05 -07002350
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002351struct nlmsghdr *
Eric W. Biederman15e47302012-09-07 20:12:54 +00002352__nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags)
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002353{
2354 struct nlmsghdr *nlh;
Hong zhi guo573ce262013-03-27 06:47:04 +00002355 int size = nlmsg_msg_size(len);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002356
2357 nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size));
2358 nlh->nlmsg_type = type;
2359 nlh->nlmsg_len = size;
2360 nlh->nlmsg_flags = flags;
Eric W. Biederman15e47302012-09-07 20:12:54 +00002361 nlh->nlmsg_pid = portid;
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002362 nlh->nlmsg_seq = seq;
2363 if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0)
Hong zhi guo573ce262013-03-27 06:47:04 +00002364 memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size);
Denys Vlasenkoa46621a2012-01-30 15:22:06 -05002365 return nlh;
2366}
2367EXPORT_SYMBOL(__nlmsg_put);
2368
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369/*
2370 * It looks a bit ugly.
2371 * It would be better to create kernel thread.
2372 */
2373
2374static int netlink_dump(struct sock *sk)
2375{
2376 struct netlink_sock *nlk = nlk_sk(sk);
2377 struct netlink_callback *cb;
Greg Rosec7ac8672011-06-10 01:27:09 +00002378 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct nlmsghdr *nlh;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002380 int len, err = -ENOBUFS;
Greg Rosec7ac8672011-06-10 01:27:09 +00002381 int alloc_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002383 mutex_lock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384
2385 cb = nlk->cb;
2386 if (cb == NULL) {
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002387 err = -EINVAL;
2388 goto errout_skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
2390
Greg Rosec7ac8672011-06-10 01:27:09 +00002391 alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE);
2392
Patrick McHardyf9c22882013-04-17 06:47:04 +00002393 if (!netlink_rx_is_mmaped(sk) &&
2394 atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf)
2395 goto errout_skb;
2396 skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, GFP_KERNEL);
Greg Rosec7ac8672011-06-10 01:27:09 +00002397 if (!skb)
Dan Carpenterc63d6ea2011-06-15 03:11:42 +00002398 goto errout_skb;
Patrick McHardyf9c22882013-04-17 06:47:04 +00002399 netlink_skb_set_owner_r(skb, sk);
Greg Rosec7ac8672011-06-10 01:27:09 +00002400
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 len = cb->dump(skb, cb);
2402
2403 if (len > 0) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002404 mutex_unlock(nlk->cb_mutex);
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002405
2406 if (sk_filter(sk, skb))
2407 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002408 else
2409 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return 0;
2411 }
2412
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002413 nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI);
2414 if (!nlh)
2415 goto errout_skb;
2416
Johannes Berg670dc282011-06-20 13:40:46 +02002417 nl_dump_check_consistent(cb, nlh);
2418
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002419 memcpy(nlmsg_data(nlh), &len, sizeof(len));
2420
Stephen Hemmingerb1153f22008-03-21 15:46:12 -07002421 if (sk_filter(sk, skb))
2422 kfree_skb(skb);
Eric Dumazet4a7e7c22012-04-05 22:17:46 +00002423 else
2424 __netlink_sendskb(sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
Thomas Grafa8f74b22005-11-10 02:25:52 +01002426 if (cb->done)
2427 cb->done(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 nlk->cb = NULL;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002429 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
Gao feng6dc878a2012-10-04 20:15:48 +00002431 module_put(cb->module);
Eric Dumazetbfb253c2012-04-22 21:30:29 +00002432 netlink_consume_callback(cb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 return 0;
Thomas Graf17977542005-06-18 22:53:48 -07002434
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002435errout_skb:
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002436 mutex_unlock(nlk->cb_mutex);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002437 kfree_skb(skb);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002438 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439}
2440
Gao feng6dc878a2012-10-04 20:15:48 +00002441int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb,
2442 const struct nlmsghdr *nlh,
2443 struct netlink_dump_control *control)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444{
2445 struct netlink_callback *cb;
2446 struct sock *sk;
2447 struct netlink_sock *nlk;
Andrey Vaginb44d2112011-02-21 02:40:47 +00002448 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002450 cb = kzalloc(sizeof(*cb), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (cb == NULL)
2452 return -ENOBUFS;
2453
Patrick McHardyf9c22882013-04-17 06:47:04 +00002454 /* Memory mapped dump requests need to be copied to avoid looping
2455 * on the pending state in netlink_mmap_sendmsg() while the CB hold
2456 * a reference to the skb.
2457 */
2458 if (netlink_skb_is_mmaped(skb)) {
2459 skb = skb_copy(skb, GFP_KERNEL);
2460 if (skb == NULL) {
2461 kfree(cb);
2462 return -ENOBUFS;
2463 }
2464 } else
2465 atomic_inc(&skb->users);
2466
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002467 cb->dump = control->dump;
2468 cb->done = control->done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 cb->nlh = nlh;
Pablo Neira Ayuso7175c882012-02-24 14:30:16 +00002470 cb->data = control->data;
Gao feng6dc878a2012-10-04 20:15:48 +00002471 cb->module = control->module;
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00002472 cb->min_dump_alloc = control->min_dump_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 atomic_inc(&skb->users);
2474 cb->skb = skb;
2475
Eric W. Biederman15e47302012-09-07 20:12:54 +00002476 sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 if (sk == NULL) {
2478 netlink_destroy_callback(cb);
2479 return -ECONNREFUSED;
2480 }
2481 nlk = nlk_sk(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002482
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002483 mutex_lock(nlk->cb_mutex);
Gao feng6dc878a2012-10-04 20:15:48 +00002484 /* A dump is in progress... */
Herbert Xu3f660d62007-05-03 03:17:14 -07002485 if (nlk->cb) {
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002486 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487 netlink_destroy_callback(cb);
Gao feng6dc878a2012-10-04 20:15:48 +00002488 ret = -EBUSY;
2489 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 }
Gao feng6dc878a2012-10-04 20:15:48 +00002491 /* add reference of module which cb->dump belongs to */
2492 if (!try_module_get(cb->module)) {
2493 mutex_unlock(nlk->cb_mutex);
2494 netlink_destroy_callback(cb);
2495 ret = -EPROTONOSUPPORT;
2496 goto out;
2497 }
2498
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 nlk->cb = cb;
Patrick McHardyaf65bdf2007-04-20 14:14:21 -07002500 mutex_unlock(nlk->cb_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501
Andrey Vaginb44d2112011-02-21 02:40:47 +00002502 ret = netlink_dump(sk);
Gao feng6dc878a2012-10-04 20:15:48 +00002503out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504 sock_put(sk);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002505
Andrey Vaginb44d2112011-02-21 02:40:47 +00002506 if (ret)
2507 return ret;
2508
Denis V. Lunev5c582982007-10-23 20:29:25 -07002509 /* We successfully started a dump, by returning -EINTR we
2510 * signal not to send ACK even if it was requested.
2511 */
2512 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
Gao feng6dc878a2012-10-04 20:15:48 +00002514EXPORT_SYMBOL(__netlink_dump_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
2516void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err)
2517{
2518 struct sk_buff *skb;
2519 struct nlmsghdr *rep;
2520 struct nlmsgerr *errmsg;
Thomas Graf339bf982006-11-10 14:10:15 -08002521 size_t payload = sizeof(*errmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Thomas Graf339bf982006-11-10 14:10:15 -08002523 /* error messages get the original request appened */
2524 if (err)
2525 payload += nlmsg_len(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
Patrick McHardyf9c22882013-04-17 06:47:04 +00002527 skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload),
2528 NETLINK_CB(in_skb).portid, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (!skb) {
2530 struct sock *sk;
2531
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +09002532 sk = netlink_lookup(sock_net(in_skb->sk),
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002533 in_skb->sk->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002534 NETLINK_CB(in_skb).portid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 if (sk) {
2536 sk->sk_err = ENOBUFS;
2537 sk->sk_error_report(sk);
2538 sock_put(sk);
2539 }
2540 return;
2541 }
2542
Eric W. Biederman15e47302012-09-07 20:12:54 +00002543 rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq,
John Fastabend5dba93a2009-09-25 13:11:44 +00002544 NLMSG_ERROR, payload, 0);
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002545 errmsg = nlmsg_data(rep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546 errmsg->error = err;
Thomas Grafbf8b79e2006-08-04 23:03:29 -07002547 memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh));
Eric W. Biederman15e47302012-09-07 20:12:54 +00002548 netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002550EXPORT_SYMBOL(netlink_ack);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002552int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002553 struct nlmsghdr *))
Thomas Graf82ace472005-11-10 02:25:53 +01002554{
Thomas Graf82ace472005-11-10 02:25:53 +01002555 struct nlmsghdr *nlh;
2556 int err;
2557
2558 while (skb->len >= nlmsg_total_size(0)) {
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002559 int msglen;
2560
Arnaldo Carvalho de Melob529ccf2007-04-25 19:08:35 -07002561 nlh = nlmsg_hdr(skb);
Thomas Grafd35b6852007-03-22 23:28:46 -07002562 err = 0;
Thomas Graf82ace472005-11-10 02:25:53 +01002563
Martin Murrayad8e4b72006-01-10 13:02:29 -08002564 if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len)
Thomas Graf82ace472005-11-10 02:25:53 +01002565 return 0;
2566
Thomas Grafd35b6852007-03-22 23:28:46 -07002567 /* Only requests are handled by the kernel */
2568 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
Denis V. Lunev5c582982007-10-23 20:29:25 -07002569 goto ack;
Thomas Grafd35b6852007-03-22 23:28:46 -07002570
Thomas Graf45e7ae72007-03-22 23:29:10 -07002571 /* Skip control messages */
2572 if (nlh->nlmsg_type < NLMSG_MIN_TYPE)
Denis V. Lunev5c582982007-10-23 20:29:25 -07002573 goto ack;
Thomas Graf45e7ae72007-03-22 23:29:10 -07002574
Thomas Graf1d00a4e2007-03-22 23:30:12 -07002575 err = cb(skb, nlh);
Denis V. Lunev5c582982007-10-23 20:29:25 -07002576 if (err == -EINTR)
2577 goto skip;
2578
2579ack:
Thomas Grafd35b6852007-03-22 23:28:46 -07002580 if (nlh->nlmsg_flags & NLM_F_ACK || err)
Thomas Graf82ace472005-11-10 02:25:53 +01002581 netlink_ack(skb, nlh, err);
Thomas Graf82ace472005-11-10 02:25:53 +01002582
Denis V. Lunev5c582982007-10-23 20:29:25 -07002583skip:
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002584 msglen = NLMSG_ALIGN(nlh->nlmsg_len);
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -07002585 if (msglen > skb->len)
2586 msglen = skb->len;
2587 skb_pull(skb, msglen);
Thomas Graf82ace472005-11-10 02:25:53 +01002588 }
2589
2590 return 0;
2591}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002592EXPORT_SYMBOL(netlink_rcv_skb);
Thomas Graf82ace472005-11-10 02:25:53 +01002593
2594/**
Thomas Grafd387f6a2006-08-15 00:31:06 -07002595 * nlmsg_notify - send a notification netlink message
2596 * @sk: netlink socket to use
2597 * @skb: notification message
Eric W. Biederman15e47302012-09-07 20:12:54 +00002598 * @portid: destination netlink portid for reports or 0
Thomas Grafd387f6a2006-08-15 00:31:06 -07002599 * @group: destination multicast group or 0
2600 * @report: 1 to report back, 0 to disable
2601 * @flags: allocation flags
2602 */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002603int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid,
Thomas Grafd387f6a2006-08-15 00:31:06 -07002604 unsigned int group, int report, gfp_t flags)
2605{
2606 int err = 0;
2607
2608 if (group) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002609 int exclude_portid = 0;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002610
2611 if (report) {
2612 atomic_inc(&skb->users);
Eric W. Biederman15e47302012-09-07 20:12:54 +00002613 exclude_portid = portid;
Thomas Grafd387f6a2006-08-15 00:31:06 -07002614 }
2615
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002616 /* errors reported via destination sk->sk_err, but propagate
2617 * delivery errors if NETLINK_BROADCAST_ERROR flag is set */
Eric W. Biederman15e47302012-09-07 20:12:54 +00002618 err = nlmsg_multicast(sk, skb, exclude_portid, group, flags);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002619 }
2620
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002621 if (report) {
2622 int err2;
2623
Eric W. Biederman15e47302012-09-07 20:12:54 +00002624 err2 = nlmsg_unicast(sk, skb, portid);
Pablo Neira Ayuso1ce85fe2009-02-24 23:18:28 -08002625 if (!err || err == -ESRCH)
2626 err = err2;
2627 }
Thomas Grafd387f6a2006-08-15 00:31:06 -07002628
2629 return err;
2630}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002631EXPORT_SYMBOL(nlmsg_notify);
Thomas Grafd387f6a2006-08-15 00:31:06 -07002632
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633#ifdef CONFIG_PROC_FS
2634struct nl_seq_iter {
Denis V. Luneve372c412007-11-19 22:31:54 -08002635 struct seq_net_private p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 int link;
2637 int hash_idx;
2638};
2639
2640static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos)
2641{
2642 struct nl_seq_iter *iter = seq->private;
2643 int i, j;
2644 struct sock *s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 loff_t off = 0;
2646
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002647 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002648 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
2650 for (j = 0; j <= hash->mask; j++) {
Sasha Levinb67bfe02013-02-27 17:06:00 -08002651 sk_for_each(s, &hash->table[j]) {
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002652 if (sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002653 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 if (off == pos) {
2655 iter->link = i;
2656 iter->hash_idx = j;
2657 return s;
2658 }
2659 ++off;
2660 }
2661 }
2662 }
2663 return NULL;
2664}
2665
2666static void *netlink_seq_start(struct seq_file *seq, loff_t *pos)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002667 __acquires(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668{
2669 read_lock(&nl_table_lock);
2670 return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2671}
2672
2673static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2674{
2675 struct sock *s;
2676 struct nl_seq_iter *iter;
2677 int i, j;
2678
2679 ++*pos;
2680
2681 if (v == SEQ_START_TOKEN)
2682 return netlink_seq_socket_idx(seq, 0);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002683
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002684 iter = seq->private;
2685 s = v;
2686 do {
2687 s = sk_next(s);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002688 } while (s && sock_net(s) != seq_file_net(seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 if (s)
2690 return s;
2691
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 i = iter->link;
2693 j = iter->hash_idx + 1;
2694
2695 do {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002696 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
2698 for (; j <= hash->mask; j++) {
2699 s = sk_head(&hash->table[j]);
YOSHIFUJI Hideaki12188542008-03-26 02:36:06 +09002700 while (s && sock_net(s) != seq_file_net(seq))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002701 s = sk_next(s);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (s) {
2703 iter->link = i;
2704 iter->hash_idx = j;
2705 return s;
2706 }
2707 }
2708
2709 j = 0;
2710 } while (++i < MAX_LINKS);
2711
2712 return NULL;
2713}
2714
2715static void netlink_seq_stop(struct seq_file *seq, void *v)
Eric Dumazet9a429c42008-01-01 21:58:02 -08002716 __releases(nl_table_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717{
2718 read_unlock(&nl_table_lock);
2719}
2720
2721
2722static int netlink_seq_show(struct seq_file *seq, void *v)
2723{
Eric Dumazet658cb352012-04-22 21:30:21 +00002724 if (v == SEQ_START_TOKEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 seq_puts(seq,
2726 "sk Eth Pid Groups "
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002727 "Rmem Wmem Dump Locks Drops Inode\n");
Eric Dumazet658cb352012-04-22 21:30:21 +00002728 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 struct sock *s = v;
2730 struct netlink_sock *nlk = nlk_sk(s);
2731
Hannes Frederic Sowa9f1e0ad2012-12-15 15:09:19 +00002732 seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 s,
2734 s->sk_protocol,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002735 nlk->portid,
Patrick McHardy513c2502005-09-06 15:43:59 -07002736 nlk->groups ? (u32)nlk->groups[0] : 0,
Eric Dumazet31e6d362009-06-17 19:05:41 -07002737 sk_rmem_alloc_get(s),
2738 sk_wmem_alloc_get(s),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 nlk->cb,
Pablo Neira Ayuso38938bf2009-03-24 16:37:55 -07002740 atomic_read(&s->sk_refcnt),
Masatake YAMATOcf0aa4e2010-02-27 19:45:37 +00002741 atomic_read(&s->sk_drops),
2742 sock_i_ino(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 );
2744
2745 }
2746 return 0;
2747}
2748
Philippe De Muyter56b3d972007-07-10 23:07:31 -07002749static const struct seq_operations netlink_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 .start = netlink_seq_start,
2751 .next = netlink_seq_next,
2752 .stop = netlink_seq_stop,
2753 .show = netlink_seq_show,
2754};
2755
2756
2757static int netlink_seq_open(struct inode *inode, struct file *file)
2758{
Denis V. Luneve372c412007-11-19 22:31:54 -08002759 return seq_open_net(inode, file, &netlink_seq_ops,
2760 sizeof(struct nl_seq_iter));
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002761}
2762
Arjan van de Venda7071d2007-02-12 00:55:36 -08002763static const struct file_operations netlink_seq_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 .owner = THIS_MODULE,
2765 .open = netlink_seq_open,
2766 .read = seq_read,
2767 .llseek = seq_lseek,
Denis V. Luneve372c412007-11-19 22:31:54 -08002768 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769};
2770
2771#endif
2772
2773int netlink_register_notifier(struct notifier_block *nb)
2774{
Alan Sterne041c682006-03-27 01:16:30 -08002775 return atomic_notifier_chain_register(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002777EXPORT_SYMBOL(netlink_register_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
2779int netlink_unregister_notifier(struct notifier_block *nb)
2780{
Alan Sterne041c682006-03-27 01:16:30 -08002781 return atomic_notifier_chain_unregister(&netlink_chain, nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782}
Patrick McHardy6ac552f2007-12-04 00:19:38 -08002783EXPORT_SYMBOL(netlink_unregister_notifier);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002784
Eric Dumazet90ddc4f2005-12-22 12:49:22 -08002785static const struct proto_ops netlink_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 .family = PF_NETLINK,
2787 .owner = THIS_MODULE,
2788 .release = netlink_release,
2789 .bind = netlink_bind,
2790 .connect = netlink_connect,
2791 .socketpair = sock_no_socketpair,
2792 .accept = sock_no_accept,
2793 .getname = netlink_getname,
Patrick McHardy9652e932013-04-17 06:47:02 +00002794 .poll = netlink_poll,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 .ioctl = sock_no_ioctl,
2796 .listen = sock_no_listen,
2797 .shutdown = sock_no_shutdown,
Patrick McHardy9a4595b2005-08-15 12:32:15 -07002798 .setsockopt = netlink_setsockopt,
2799 .getsockopt = netlink_getsockopt,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 .sendmsg = netlink_sendmsg,
2801 .recvmsg = netlink_recvmsg,
Patrick McHardyccdfcc32013-04-17 06:47:01 +00002802 .mmap = netlink_mmap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 .sendpage = sock_no_sendpage,
2804};
2805
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +00002806static const struct net_proto_family netlink_family_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 .family = PF_NETLINK,
2808 .create = netlink_create,
2809 .owner = THIS_MODULE, /* for consistency 8) */
2810};
2811
Pavel Emelyanov46650792007-10-08 20:38:39 -07002812static int __net_init netlink_net_init(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002813{
2814#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +00002815 if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops))
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002816 return -ENOMEM;
2817#endif
2818 return 0;
2819}
2820
Pavel Emelyanov46650792007-10-08 20:38:39 -07002821static void __net_exit netlink_net_exit(struct net *net)
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002822{
2823#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002824 remove_proc_entry("netlink", net->proc_net);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002825#endif
2826}
2827
David S. Millerb963ea82010-08-30 19:08:01 -07002828static void __init netlink_add_usersock_entry(void)
2829{
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002830 struct listeners *listeners;
David S. Millerb963ea82010-08-30 19:08:01 -07002831 int groups = 32;
2832
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002833 listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL);
David S. Millerb963ea82010-08-30 19:08:01 -07002834 if (!listeners)
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002835 panic("netlink_add_usersock_entry: Cannot allocate listeners\n");
David S. Millerb963ea82010-08-30 19:08:01 -07002836
2837 netlink_table_grab();
2838
2839 nl_table[NETLINK_USERSOCK].groups = groups;
Eric Dumazet5c398dc2010-10-24 04:27:10 +00002840 rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners);
David S. Millerb963ea82010-08-30 19:08:01 -07002841 nl_table[NETLINK_USERSOCK].module = THIS_MODULE;
2842 nl_table[NETLINK_USERSOCK].registered = 1;
Pablo Neira Ayuso9785e102012-09-08 02:53:53 +00002843 nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND;
David S. Millerb963ea82010-08-30 19:08:01 -07002844
2845 netlink_table_ungrab();
2846}
2847
Denis V. Lunev022cbae2007-11-13 03:23:50 -08002848static struct pernet_operations __net_initdata netlink_net_ops = {
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002849 .init = netlink_net_init,
2850 .exit = netlink_net_exit,
2851};
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853static int __init netlink_proto_init(void)
2854{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 int i;
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002856 unsigned long limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 unsigned int order;
2858 int err = proto_register(&netlink_proto, 0);
2859
2860 if (err != 0)
2861 goto out;
2862
YOSHIFUJI Hideaki / 吉藤英明fab25742013-01-09 07:19:48 +00002863 BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07002865 nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002866 if (!nl_table)
2867 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Jan Beulich44813742009-09-21 17:03:05 -07002869 if (totalram_pages >= (128 * 1024))
2870 limit = totalram_pages >> (21 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 else
Jan Beulich44813742009-09-21 17:03:05 -07002872 limit = totalram_pages >> (23 - PAGE_SHIFT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
Denis Cheng26ff5dd2007-09-16 16:36:02 -07002874 order = get_bitmask_order(limit) - 1 + PAGE_SHIFT;
2875 limit = (1UL << order) / sizeof(struct hlist_head);
2876 order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 for (i = 0; i < MAX_LINKS; i++) {
Eric W. Biederman15e47302012-09-07 20:12:54 +00002879 struct nl_portid_hash *hash = &nl_table[i].hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
Eric W. Biederman15e47302012-09-07 20:12:54 +00002881 hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 if (!hash->table) {
2883 while (i-- > 0)
Eric W. Biederman15e47302012-09-07 20:12:54 +00002884 nl_portid_hash_free(nl_table[i].hash.table,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 1 * sizeof(*hash->table));
2886 kfree(nl_table);
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002887 goto panic;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 hash->max_shift = order;
2890 hash->shift = 0;
2891 hash->mask = 0;
2892 hash->rehash_time = jiffies;
2893 }
2894
David S. Millerb963ea82010-08-30 19:08:01 -07002895 netlink_add_usersock_entry();
2896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 sock_register(&netlink_family_ops);
Eric W. Biedermanb4b51022007-09-12 13:05:38 +02002898 register_pernet_subsys(&netlink_net_ops);
YOSHIFUJI Hideaki746fac42007-02-09 23:25:07 +09002899 /* The netlink device handler may be needed early. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 rtnetlink_init();
2901out:
2902 return err;
Akinobu Mitafab2caf2006-08-29 02:15:24 -07002903panic:
2904 panic("netlink_init: Cannot allocate nl_table\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905}
2906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907core_initcall(netlink_proto_init);