Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NETLINK Kernel-user communication protocol. |
| 3 | * |
Alan Cox | 113aa83 | 2008-10-13 19:01:08 -0700 | [diff] [blame] | 4 | * Authors: Alan Cox <alan@lxorguk.ukuu.org.uk> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 6 | * Patrick McHardy <kaber@trash.net> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License |
| 10 | * as published by the Free Software Foundation; either version |
| 11 | * 2 of the License, or (at your option) any later version. |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 12 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | * Tue Jun 26 14:36:48 MEST 2001 Herbert "herp" Rosmanith |
| 14 | * added netlink_proto_exit |
| 15 | * Tue Jan 22 18:32:44 BRST 2002 Arnaldo C. de Melo <acme@conectiva.com.br> |
| 16 | * use nlk_sk, as sk->protinfo is on a diet 8) |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 17 | * Fri Jul 22 19:51:12 MEST 2005 Harald Welte <laforge@gnumonks.org> |
| 18 | * - inc module use count of module that owns |
| 19 | * the kernel socket in case userspace opens |
| 20 | * socket of same protocol |
| 21 | * - remove all module support, since netlink is |
| 22 | * mandatory if CONFIG_NET=y these days |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | */ |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <linux/module.h> |
| 26 | |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 27 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/kernel.h> |
| 29 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/signal.h> |
| 31 | #include <linux/sched.h> |
| 32 | #include <linux/errno.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/stat.h> |
| 35 | #include <linux/socket.h> |
| 36 | #include <linux/un.h> |
| 37 | #include <linux/fcntl.h> |
| 38 | #include <linux/termios.h> |
| 39 | #include <linux/sockios.h> |
| 40 | #include <linux/net.h> |
| 41 | #include <linux/fs.h> |
| 42 | #include <linux/slab.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | #include <linux/skbuff.h> |
| 45 | #include <linux/netdevice.h> |
| 46 | #include <linux/rtnetlink.h> |
| 47 | #include <linux/proc_fs.h> |
| 48 | #include <linux/seq_file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | #include <linux/notifier.h> |
| 50 | #include <linux/security.h> |
| 51 | #include <linux/jhash.h> |
| 52 | #include <linux/jiffies.h> |
| 53 | #include <linux/random.h> |
| 54 | #include <linux/bitops.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/types.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 57 | #include <linux/audit.h> |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 58 | #include <linux/mutex.h> |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 59 | #include <linux/vmalloc.h> |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 60 | #include <asm/cacheflush.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 61 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 62 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #include <net/sock.h> |
| 64 | #include <net/scm.h> |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 65 | #include <net/netlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 67 | #include "af_netlink.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 69 | struct listeners { |
| 70 | struct rcu_head rcu; |
| 71 | unsigned long masks[0]; |
Johannes Berg | 6c04bb1 | 2009-07-10 09:51:32 +0000 | [diff] [blame] | 72 | }; |
| 73 | |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 74 | /* state bits */ |
| 75 | #define NETLINK_CONGESTED 0x0 |
| 76 | |
| 77 | /* flags */ |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 78 | #define NETLINK_KERNEL_SOCKET 0x1 |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 79 | #define NETLINK_RECV_PKTINFO 0x2 |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 80 | #define NETLINK_BROADCAST_SEND_ERROR 0x4 |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 81 | #define NETLINK_RECV_NO_ENOBUFS 0x8 |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 82 | |
David S. Miller | 035c4c1 | 2011-12-23 17:33:03 -0500 | [diff] [blame] | 83 | static inline int netlink_is_kernel(struct sock *sk) |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 84 | { |
| 85 | return nlk_sk(sk)->flags & NETLINK_KERNEL_SOCKET; |
| 86 | } |
| 87 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 88 | struct netlink_table *nl_table; |
| 89 | EXPORT_SYMBOL_GPL(nl_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); |
| 92 | |
| 93 | static int netlink_dump(struct sock *sk); |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 94 | static void netlink_skb_destructor(struct sk_buff *skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | |
Andrey Vagin | 0f29c76 | 2013-03-21 20:33:47 +0400 | [diff] [blame] | 96 | DEFINE_RWLOCK(nl_table_lock); |
| 97 | EXPORT_SYMBOL_GPL(nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
| 99 | |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 100 | #define nl_deref_protected(X) rcu_dereference_protected(X, lockdep_is_held(&nl_table_lock)); |
| 101 | |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 102 | static ATOMIC_NOTIFIER_HEAD(netlink_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 104 | static inline u32 netlink_group_mask(u32 group) |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 105 | { |
| 106 | return group ? 1 << (group - 1) : 0; |
| 107 | } |
| 108 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 109 | static inline struct hlist_head *nl_portid_hashfn(struct nl_portid_hash *hash, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 111 | return &hash->table[jhash_1word(portid, hash->rnd) & hash->mask]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 114 | static void netlink_overrun(struct sock *sk) |
| 115 | { |
| 116 | struct netlink_sock *nlk = nlk_sk(sk); |
| 117 | |
| 118 | if (!(nlk->flags & NETLINK_RECV_NO_ENOBUFS)) { |
| 119 | if (!test_and_set_bit(NETLINK_CONGESTED, &nlk_sk(sk)->state)) { |
| 120 | sk->sk_err = ENOBUFS; |
| 121 | sk->sk_error_report(sk); |
| 122 | } |
| 123 | } |
| 124 | atomic_inc(&sk->sk_drops); |
| 125 | } |
| 126 | |
| 127 | static void netlink_rcv_wake(struct sock *sk) |
| 128 | { |
| 129 | struct netlink_sock *nlk = nlk_sk(sk); |
| 130 | |
| 131 | if (skb_queue_empty(&sk->sk_receive_queue)) |
| 132 | clear_bit(NETLINK_CONGESTED, &nlk->state); |
| 133 | if (!test_bit(NETLINK_CONGESTED, &nlk->state)) |
| 134 | wake_up_interruptible(&nlk->wait); |
| 135 | } |
| 136 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 137 | #ifdef CONFIG_NETLINK_MMAP |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 138 | static bool netlink_skb_is_mmaped(const struct sk_buff *skb) |
| 139 | { |
| 140 | return NETLINK_CB(skb).flags & NETLINK_SKB_MMAPED; |
| 141 | } |
| 142 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 143 | static bool netlink_rx_is_mmaped(struct sock *sk) |
| 144 | { |
| 145 | return nlk_sk(sk)->rx_ring.pg_vec != NULL; |
| 146 | } |
| 147 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 148 | static bool netlink_tx_is_mmaped(struct sock *sk) |
| 149 | { |
| 150 | return nlk_sk(sk)->tx_ring.pg_vec != NULL; |
| 151 | } |
| 152 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 153 | static __pure struct page *pgvec_to_page(const void *addr) |
| 154 | { |
| 155 | if (is_vmalloc_addr(addr)) |
| 156 | return vmalloc_to_page(addr); |
| 157 | else |
| 158 | return virt_to_page(addr); |
| 159 | } |
| 160 | |
| 161 | static void free_pg_vec(void **pg_vec, unsigned int order, unsigned int len) |
| 162 | { |
| 163 | unsigned int i; |
| 164 | |
| 165 | for (i = 0; i < len; i++) { |
| 166 | if (pg_vec[i] != NULL) { |
| 167 | if (is_vmalloc_addr(pg_vec[i])) |
| 168 | vfree(pg_vec[i]); |
| 169 | else |
| 170 | free_pages((unsigned long)pg_vec[i], order); |
| 171 | } |
| 172 | } |
| 173 | kfree(pg_vec); |
| 174 | } |
| 175 | |
| 176 | static void *alloc_one_pg_vec_page(unsigned long order) |
| 177 | { |
| 178 | void *buffer; |
| 179 | gfp_t gfp_flags = GFP_KERNEL | __GFP_COMP | __GFP_ZERO | |
| 180 | __GFP_NOWARN | __GFP_NORETRY; |
| 181 | |
| 182 | buffer = (void *)__get_free_pages(gfp_flags, order); |
| 183 | if (buffer != NULL) |
| 184 | return buffer; |
| 185 | |
| 186 | buffer = vzalloc((1 << order) * PAGE_SIZE); |
| 187 | if (buffer != NULL) |
| 188 | return buffer; |
| 189 | |
| 190 | gfp_flags &= ~__GFP_NORETRY; |
| 191 | return (void *)__get_free_pages(gfp_flags, order); |
| 192 | } |
| 193 | |
| 194 | static void **alloc_pg_vec(struct netlink_sock *nlk, |
| 195 | struct nl_mmap_req *req, unsigned int order) |
| 196 | { |
| 197 | unsigned int block_nr = req->nm_block_nr; |
| 198 | unsigned int i; |
| 199 | void **pg_vec, *ptr; |
| 200 | |
| 201 | pg_vec = kcalloc(block_nr, sizeof(void *), GFP_KERNEL); |
| 202 | if (pg_vec == NULL) |
| 203 | return NULL; |
| 204 | |
| 205 | for (i = 0; i < block_nr; i++) { |
| 206 | pg_vec[i] = ptr = alloc_one_pg_vec_page(order); |
| 207 | if (pg_vec[i] == NULL) |
| 208 | goto err1; |
| 209 | } |
| 210 | |
| 211 | return pg_vec; |
| 212 | err1: |
| 213 | free_pg_vec(pg_vec, order, block_nr); |
| 214 | return NULL; |
| 215 | } |
| 216 | |
| 217 | static int netlink_set_ring(struct sock *sk, struct nl_mmap_req *req, |
| 218 | bool closing, bool tx_ring) |
| 219 | { |
| 220 | struct netlink_sock *nlk = nlk_sk(sk); |
| 221 | struct netlink_ring *ring; |
| 222 | struct sk_buff_head *queue; |
| 223 | void **pg_vec = NULL; |
| 224 | unsigned int order = 0; |
| 225 | int err; |
| 226 | |
| 227 | ring = tx_ring ? &nlk->tx_ring : &nlk->rx_ring; |
| 228 | queue = tx_ring ? &sk->sk_write_queue : &sk->sk_receive_queue; |
| 229 | |
| 230 | if (!closing) { |
| 231 | if (atomic_read(&nlk->mapped)) |
| 232 | return -EBUSY; |
| 233 | if (atomic_read(&ring->pending)) |
| 234 | return -EBUSY; |
| 235 | } |
| 236 | |
| 237 | if (req->nm_block_nr) { |
| 238 | if (ring->pg_vec != NULL) |
| 239 | return -EBUSY; |
| 240 | |
| 241 | if ((int)req->nm_block_size <= 0) |
| 242 | return -EINVAL; |
| 243 | if (!IS_ALIGNED(req->nm_block_size, PAGE_SIZE)) |
| 244 | return -EINVAL; |
| 245 | if (req->nm_frame_size < NL_MMAP_HDRLEN) |
| 246 | return -EINVAL; |
| 247 | if (!IS_ALIGNED(req->nm_frame_size, NL_MMAP_MSG_ALIGNMENT)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | ring->frames_per_block = req->nm_block_size / |
| 251 | req->nm_frame_size; |
| 252 | if (ring->frames_per_block == 0) |
| 253 | return -EINVAL; |
| 254 | if (ring->frames_per_block * req->nm_block_nr != |
| 255 | req->nm_frame_nr) |
| 256 | return -EINVAL; |
| 257 | |
| 258 | order = get_order(req->nm_block_size); |
| 259 | pg_vec = alloc_pg_vec(nlk, req, order); |
| 260 | if (pg_vec == NULL) |
| 261 | return -ENOMEM; |
| 262 | } else { |
| 263 | if (req->nm_frame_nr) |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
| 267 | err = -EBUSY; |
| 268 | mutex_lock(&nlk->pg_vec_lock); |
| 269 | if (closing || atomic_read(&nlk->mapped) == 0) { |
| 270 | err = 0; |
| 271 | spin_lock_bh(&queue->lock); |
| 272 | |
| 273 | ring->frame_max = req->nm_frame_nr - 1; |
| 274 | ring->head = 0; |
| 275 | ring->frame_size = req->nm_frame_size; |
| 276 | ring->pg_vec_pages = req->nm_block_size / PAGE_SIZE; |
| 277 | |
| 278 | swap(ring->pg_vec_len, req->nm_block_nr); |
| 279 | swap(ring->pg_vec_order, order); |
| 280 | swap(ring->pg_vec, pg_vec); |
| 281 | |
| 282 | __skb_queue_purge(queue); |
| 283 | spin_unlock_bh(&queue->lock); |
| 284 | |
| 285 | WARN_ON(atomic_read(&nlk->mapped)); |
| 286 | } |
| 287 | mutex_unlock(&nlk->pg_vec_lock); |
| 288 | |
| 289 | if (pg_vec) |
| 290 | free_pg_vec(pg_vec, order, req->nm_block_nr); |
| 291 | return err; |
| 292 | } |
| 293 | |
| 294 | static void netlink_mm_open(struct vm_area_struct *vma) |
| 295 | { |
| 296 | struct file *file = vma->vm_file; |
| 297 | struct socket *sock = file->private_data; |
| 298 | struct sock *sk = sock->sk; |
| 299 | |
| 300 | if (sk) |
| 301 | atomic_inc(&nlk_sk(sk)->mapped); |
| 302 | } |
| 303 | |
| 304 | static void netlink_mm_close(struct vm_area_struct *vma) |
| 305 | { |
| 306 | struct file *file = vma->vm_file; |
| 307 | struct socket *sock = file->private_data; |
| 308 | struct sock *sk = sock->sk; |
| 309 | |
| 310 | if (sk) |
| 311 | atomic_dec(&nlk_sk(sk)->mapped); |
| 312 | } |
| 313 | |
| 314 | static const struct vm_operations_struct netlink_mmap_ops = { |
| 315 | .open = netlink_mm_open, |
| 316 | .close = netlink_mm_close, |
| 317 | }; |
| 318 | |
| 319 | static int netlink_mmap(struct file *file, struct socket *sock, |
| 320 | struct vm_area_struct *vma) |
| 321 | { |
| 322 | struct sock *sk = sock->sk; |
| 323 | struct netlink_sock *nlk = nlk_sk(sk); |
| 324 | struct netlink_ring *ring; |
| 325 | unsigned long start, size, expected; |
| 326 | unsigned int i; |
| 327 | int err = -EINVAL; |
| 328 | |
| 329 | if (vma->vm_pgoff) |
| 330 | return -EINVAL; |
| 331 | |
| 332 | mutex_lock(&nlk->pg_vec_lock); |
| 333 | |
| 334 | expected = 0; |
| 335 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { |
| 336 | if (ring->pg_vec == NULL) |
| 337 | continue; |
| 338 | expected += ring->pg_vec_len * ring->pg_vec_pages * PAGE_SIZE; |
| 339 | } |
| 340 | |
| 341 | if (expected == 0) |
| 342 | goto out; |
| 343 | |
| 344 | size = vma->vm_end - vma->vm_start; |
| 345 | if (size != expected) |
| 346 | goto out; |
| 347 | |
| 348 | start = vma->vm_start; |
| 349 | for (ring = &nlk->rx_ring; ring <= &nlk->tx_ring; ring++) { |
| 350 | if (ring->pg_vec == NULL) |
| 351 | continue; |
| 352 | |
| 353 | for (i = 0; i < ring->pg_vec_len; i++) { |
| 354 | struct page *page; |
| 355 | void *kaddr = ring->pg_vec[i]; |
| 356 | unsigned int pg_num; |
| 357 | |
| 358 | for (pg_num = 0; pg_num < ring->pg_vec_pages; pg_num++) { |
| 359 | page = pgvec_to_page(kaddr); |
| 360 | err = vm_insert_page(vma, start, page); |
| 361 | if (err < 0) |
| 362 | goto out; |
| 363 | start += PAGE_SIZE; |
| 364 | kaddr += PAGE_SIZE; |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | atomic_inc(&nlk->mapped); |
| 370 | vma->vm_ops = &netlink_mmap_ops; |
| 371 | err = 0; |
| 372 | out: |
| 373 | mutex_unlock(&nlk->pg_vec_lock); |
| 374 | return 0; |
| 375 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 376 | |
| 377 | static void netlink_frame_flush_dcache(const struct nl_mmap_hdr *hdr) |
| 378 | { |
| 379 | #if ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE == 1 |
| 380 | struct page *p_start, *p_end; |
| 381 | |
| 382 | /* First page is flushed through netlink_{get,set}_status */ |
| 383 | p_start = pgvec_to_page(hdr + PAGE_SIZE); |
Stephen Rothwell | 1d5085c | 2013-04-23 17:40:35 +1000 | [diff] [blame] | 384 | p_end = pgvec_to_page((void *)hdr + NL_MMAP_HDRLEN + hdr->nm_len - 1); |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 385 | while (p_start <= p_end) { |
| 386 | flush_dcache_page(p_start); |
| 387 | p_start++; |
| 388 | } |
| 389 | #endif |
| 390 | } |
| 391 | |
| 392 | static enum nl_mmap_status netlink_get_status(const struct nl_mmap_hdr *hdr) |
| 393 | { |
| 394 | smp_rmb(); |
| 395 | flush_dcache_page(pgvec_to_page(hdr)); |
| 396 | return hdr->nm_status; |
| 397 | } |
| 398 | |
| 399 | static void netlink_set_status(struct nl_mmap_hdr *hdr, |
| 400 | enum nl_mmap_status status) |
| 401 | { |
| 402 | hdr->nm_status = status; |
| 403 | flush_dcache_page(pgvec_to_page(hdr)); |
| 404 | smp_wmb(); |
| 405 | } |
| 406 | |
| 407 | static struct nl_mmap_hdr * |
| 408 | __netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos) |
| 409 | { |
| 410 | unsigned int pg_vec_pos, frame_off; |
| 411 | |
| 412 | pg_vec_pos = pos / ring->frames_per_block; |
| 413 | frame_off = pos % ring->frames_per_block; |
| 414 | |
| 415 | return ring->pg_vec[pg_vec_pos] + (frame_off * ring->frame_size); |
| 416 | } |
| 417 | |
| 418 | static struct nl_mmap_hdr * |
| 419 | netlink_lookup_frame(const struct netlink_ring *ring, unsigned int pos, |
| 420 | enum nl_mmap_status status) |
| 421 | { |
| 422 | struct nl_mmap_hdr *hdr; |
| 423 | |
| 424 | hdr = __netlink_lookup_frame(ring, pos); |
| 425 | if (netlink_get_status(hdr) != status) |
| 426 | return NULL; |
| 427 | |
| 428 | return hdr; |
| 429 | } |
| 430 | |
| 431 | static struct nl_mmap_hdr * |
| 432 | netlink_current_frame(const struct netlink_ring *ring, |
| 433 | enum nl_mmap_status status) |
| 434 | { |
| 435 | return netlink_lookup_frame(ring, ring->head, status); |
| 436 | } |
| 437 | |
| 438 | static struct nl_mmap_hdr * |
| 439 | netlink_previous_frame(const struct netlink_ring *ring, |
| 440 | enum nl_mmap_status status) |
| 441 | { |
| 442 | unsigned int prev; |
| 443 | |
| 444 | prev = ring->head ? ring->head - 1 : ring->frame_max; |
| 445 | return netlink_lookup_frame(ring, prev, status); |
| 446 | } |
| 447 | |
| 448 | static void netlink_increment_head(struct netlink_ring *ring) |
| 449 | { |
| 450 | ring->head = ring->head != ring->frame_max ? ring->head + 1 : 0; |
| 451 | } |
| 452 | |
| 453 | static void netlink_forward_ring(struct netlink_ring *ring) |
| 454 | { |
| 455 | unsigned int head = ring->head, pos = head; |
| 456 | const struct nl_mmap_hdr *hdr; |
| 457 | |
| 458 | do { |
| 459 | hdr = __netlink_lookup_frame(ring, pos); |
| 460 | if (hdr->nm_status == NL_MMAP_STATUS_UNUSED) |
| 461 | break; |
| 462 | if (hdr->nm_status != NL_MMAP_STATUS_SKIP) |
| 463 | break; |
| 464 | netlink_increment_head(ring); |
| 465 | } while (ring->head != head); |
| 466 | } |
| 467 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 468 | static bool netlink_dump_space(struct netlink_sock *nlk) |
| 469 | { |
| 470 | struct netlink_ring *ring = &nlk->rx_ring; |
| 471 | struct nl_mmap_hdr *hdr; |
| 472 | unsigned int n; |
| 473 | |
| 474 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 475 | if (hdr == NULL) |
| 476 | return false; |
| 477 | |
| 478 | n = ring->head + ring->frame_max / 2; |
| 479 | if (n > ring->frame_max) |
| 480 | n -= ring->frame_max; |
| 481 | |
| 482 | hdr = __netlink_lookup_frame(ring, n); |
| 483 | |
| 484 | return hdr->nm_status == NL_MMAP_STATUS_UNUSED; |
| 485 | } |
| 486 | |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 487 | static unsigned int netlink_poll(struct file *file, struct socket *sock, |
| 488 | poll_table *wait) |
| 489 | { |
| 490 | struct sock *sk = sock->sk; |
| 491 | struct netlink_sock *nlk = nlk_sk(sk); |
| 492 | unsigned int mask; |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 493 | int err; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 494 | |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 495 | if (nlk->rx_ring.pg_vec != NULL) { |
| 496 | /* Memory mapped sockets don't call recvmsg(), so flow control |
| 497 | * for dumps is performed here. A dump is allowed to continue |
| 498 | * if at least half the ring is unused. |
| 499 | */ |
| 500 | while (nlk->cb != NULL && netlink_dump_space(nlk)) { |
| 501 | err = netlink_dump(sk); |
| 502 | if (err < 0) { |
| 503 | sk->sk_err = err; |
| 504 | sk->sk_error_report(sk); |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | netlink_rcv_wake(sk); |
| 509 | } |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 510 | |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 511 | mask = datagram_poll(file, sock, wait); |
| 512 | |
| 513 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 514 | if (nlk->rx_ring.pg_vec) { |
| 515 | netlink_forward_ring(&nlk->rx_ring); |
| 516 | if (!netlink_previous_frame(&nlk->rx_ring, NL_MMAP_STATUS_UNUSED)) |
| 517 | mask |= POLLIN | POLLRDNORM; |
| 518 | } |
| 519 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 520 | |
| 521 | spin_lock_bh(&sk->sk_write_queue.lock); |
| 522 | if (nlk->tx_ring.pg_vec) { |
| 523 | if (netlink_current_frame(&nlk->tx_ring, NL_MMAP_STATUS_UNUSED)) |
| 524 | mask |= POLLOUT | POLLWRNORM; |
| 525 | } |
| 526 | spin_unlock_bh(&sk->sk_write_queue.lock); |
| 527 | |
| 528 | return mask; |
| 529 | } |
| 530 | |
| 531 | static struct nl_mmap_hdr *netlink_mmap_hdr(struct sk_buff *skb) |
| 532 | { |
| 533 | return (struct nl_mmap_hdr *)(skb->head - NL_MMAP_HDRLEN); |
| 534 | } |
| 535 | |
| 536 | static void netlink_ring_setup_skb(struct sk_buff *skb, struct sock *sk, |
| 537 | struct netlink_ring *ring, |
| 538 | struct nl_mmap_hdr *hdr) |
| 539 | { |
| 540 | unsigned int size; |
| 541 | void *data; |
| 542 | |
| 543 | size = ring->frame_size - NL_MMAP_HDRLEN; |
| 544 | data = (void *)hdr + NL_MMAP_HDRLEN; |
| 545 | |
| 546 | skb->head = data; |
| 547 | skb->data = data; |
| 548 | skb_reset_tail_pointer(skb); |
| 549 | skb->end = skb->tail + size; |
| 550 | skb->len = 0; |
| 551 | |
| 552 | skb->destructor = netlink_skb_destructor; |
| 553 | NETLINK_CB(skb).flags |= NETLINK_SKB_MMAPED; |
| 554 | NETLINK_CB(skb).sk = sk; |
| 555 | } |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 556 | |
| 557 | static int netlink_mmap_sendmsg(struct sock *sk, struct msghdr *msg, |
| 558 | u32 dst_portid, u32 dst_group, |
| 559 | struct sock_iocb *siocb) |
| 560 | { |
| 561 | struct netlink_sock *nlk = nlk_sk(sk); |
| 562 | struct netlink_ring *ring; |
| 563 | struct nl_mmap_hdr *hdr; |
| 564 | struct sk_buff *skb; |
| 565 | unsigned int maxlen; |
| 566 | bool excl = true; |
| 567 | int err = 0, len = 0; |
| 568 | |
| 569 | /* Netlink messages are validated by the receiver before processing. |
| 570 | * In order to avoid userspace changing the contents of the message |
| 571 | * after validation, the socket and the ring may only be used by a |
| 572 | * single process, otherwise we fall back to copying. |
| 573 | */ |
| 574 | if (atomic_long_read(&sk->sk_socket->file->f_count) > 2 || |
| 575 | atomic_read(&nlk->mapped) > 1) |
| 576 | excl = false; |
| 577 | |
| 578 | mutex_lock(&nlk->pg_vec_lock); |
| 579 | |
| 580 | ring = &nlk->tx_ring; |
| 581 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; |
| 582 | |
| 583 | do { |
| 584 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_VALID); |
| 585 | if (hdr == NULL) { |
| 586 | if (!(msg->msg_flags & MSG_DONTWAIT) && |
| 587 | atomic_read(&nlk->tx_ring.pending)) |
| 588 | schedule(); |
| 589 | continue; |
| 590 | } |
| 591 | if (hdr->nm_len > maxlen) { |
| 592 | err = -EINVAL; |
| 593 | goto out; |
| 594 | } |
| 595 | |
| 596 | netlink_frame_flush_dcache(hdr); |
| 597 | |
| 598 | if (likely(dst_portid == 0 && dst_group == 0 && excl)) { |
| 599 | skb = alloc_skb_head(GFP_KERNEL); |
| 600 | if (skb == NULL) { |
| 601 | err = -ENOBUFS; |
| 602 | goto out; |
| 603 | } |
| 604 | sock_hold(sk); |
| 605 | netlink_ring_setup_skb(skb, sk, ring, hdr); |
| 606 | NETLINK_CB(skb).flags |= NETLINK_SKB_TX; |
| 607 | __skb_put(skb, hdr->nm_len); |
| 608 | netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED); |
| 609 | atomic_inc(&ring->pending); |
| 610 | } else { |
| 611 | skb = alloc_skb(hdr->nm_len, GFP_KERNEL); |
| 612 | if (skb == NULL) { |
| 613 | err = -ENOBUFS; |
| 614 | goto out; |
| 615 | } |
| 616 | __skb_put(skb, hdr->nm_len); |
| 617 | memcpy(skb->data, (void *)hdr + NL_MMAP_HDRLEN, hdr->nm_len); |
| 618 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); |
| 619 | } |
| 620 | |
| 621 | netlink_increment_head(ring); |
| 622 | |
| 623 | NETLINK_CB(skb).portid = nlk->portid; |
| 624 | NETLINK_CB(skb).dst_group = dst_group; |
| 625 | NETLINK_CB(skb).creds = siocb->scm->creds; |
| 626 | |
| 627 | err = security_netlink_send(sk, skb); |
| 628 | if (err) { |
| 629 | kfree_skb(skb); |
| 630 | goto out; |
| 631 | } |
| 632 | |
| 633 | if (unlikely(dst_group)) { |
| 634 | atomic_inc(&skb->users); |
| 635 | netlink_broadcast(sk, skb, dst_portid, dst_group, |
| 636 | GFP_KERNEL); |
| 637 | } |
| 638 | err = netlink_unicast(sk, skb, dst_portid, |
| 639 | msg->msg_flags & MSG_DONTWAIT); |
| 640 | if (err < 0) |
| 641 | goto out; |
| 642 | len += err; |
| 643 | |
| 644 | } while (hdr != NULL || |
| 645 | (!(msg->msg_flags & MSG_DONTWAIT) && |
| 646 | atomic_read(&nlk->tx_ring.pending))); |
| 647 | |
| 648 | if (len > 0) |
| 649 | err = len; |
| 650 | out: |
| 651 | mutex_unlock(&nlk->pg_vec_lock); |
| 652 | return err; |
| 653 | } |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 654 | |
| 655 | static void netlink_queue_mmaped_skb(struct sock *sk, struct sk_buff *skb) |
| 656 | { |
| 657 | struct nl_mmap_hdr *hdr; |
| 658 | |
| 659 | hdr = netlink_mmap_hdr(skb); |
| 660 | hdr->nm_len = skb->len; |
| 661 | hdr->nm_group = NETLINK_CB(skb).dst_group; |
| 662 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; |
Nicolas Dichtel | 1bf9310 | 2013-04-24 10:36:23 +0200 | [diff] [blame] | 663 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
| 664 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 665 | netlink_frame_flush_dcache(hdr); |
| 666 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); |
| 667 | |
| 668 | NETLINK_CB(skb).flags |= NETLINK_SKB_DELIVERED; |
| 669 | kfree_skb(skb); |
| 670 | } |
| 671 | |
| 672 | static void netlink_ring_set_copied(struct sock *sk, struct sk_buff *skb) |
| 673 | { |
| 674 | struct netlink_sock *nlk = nlk_sk(sk); |
| 675 | struct netlink_ring *ring = &nlk->rx_ring; |
| 676 | struct nl_mmap_hdr *hdr; |
| 677 | |
| 678 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 679 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 680 | if (hdr == NULL) { |
| 681 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 682 | kfree_skb(skb); |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 683 | netlink_overrun(sk); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 684 | return; |
| 685 | } |
| 686 | netlink_increment_head(ring); |
| 687 | __skb_queue_tail(&sk->sk_receive_queue, skb); |
| 688 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 689 | |
| 690 | hdr->nm_len = skb->len; |
| 691 | hdr->nm_group = NETLINK_CB(skb).dst_group; |
| 692 | hdr->nm_pid = NETLINK_CB(skb).creds.pid; |
Nicolas Dichtel | 1bf9310 | 2013-04-24 10:36:23 +0200 | [diff] [blame] | 693 | hdr->nm_uid = from_kuid(sk_user_ns(sk), NETLINK_CB(skb).creds.uid); |
| 694 | hdr->nm_gid = from_kgid(sk_user_ns(sk), NETLINK_CB(skb).creds.gid); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 695 | netlink_set_status(hdr, NL_MMAP_STATUS_COPY); |
| 696 | } |
| 697 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 698 | #else /* CONFIG_NETLINK_MMAP */ |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 699 | #define netlink_skb_is_mmaped(skb) false |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 700 | #define netlink_rx_is_mmaped(sk) false |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 701 | #define netlink_tx_is_mmaped(sk) false |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 702 | #define netlink_mmap sock_no_mmap |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 703 | #define netlink_poll datagram_poll |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 704 | #define netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, siocb) 0 |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 705 | #endif /* CONFIG_NETLINK_MMAP */ |
| 706 | |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 707 | static void netlink_destroy_callback(struct netlink_callback *cb) |
| 708 | { |
| 709 | kfree_skb(cb->skb); |
| 710 | kfree(cb); |
| 711 | } |
| 712 | |
Eric Dumazet | bfb253c | 2012-04-22 21:30:29 +0000 | [diff] [blame] | 713 | static void netlink_consume_callback(struct netlink_callback *cb) |
| 714 | { |
| 715 | consume_skb(cb->skb); |
| 716 | kfree(cb); |
| 717 | } |
| 718 | |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 719 | static void netlink_skb_destructor(struct sk_buff *skb) |
| 720 | { |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 721 | #ifdef CONFIG_NETLINK_MMAP |
| 722 | struct nl_mmap_hdr *hdr; |
| 723 | struct netlink_ring *ring; |
| 724 | struct sock *sk; |
| 725 | |
| 726 | /* If a packet from the kernel to userspace was freed because of an |
| 727 | * error without being delivered to userspace, the kernel must reset |
| 728 | * the status. In the direction userspace to kernel, the status is |
| 729 | * always reset here after the packet was processed and freed. |
| 730 | */ |
| 731 | if (netlink_skb_is_mmaped(skb)) { |
| 732 | hdr = netlink_mmap_hdr(skb); |
| 733 | sk = NETLINK_CB(skb).sk; |
| 734 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 735 | if (NETLINK_CB(skb).flags & NETLINK_SKB_TX) { |
| 736 | netlink_set_status(hdr, NL_MMAP_STATUS_UNUSED); |
| 737 | ring = &nlk_sk(sk)->tx_ring; |
| 738 | } else { |
| 739 | if (!(NETLINK_CB(skb).flags & NETLINK_SKB_DELIVERED)) { |
| 740 | hdr->nm_len = 0; |
| 741 | netlink_set_status(hdr, NL_MMAP_STATUS_VALID); |
| 742 | } |
| 743 | ring = &nlk_sk(sk)->rx_ring; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 744 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 745 | |
| 746 | WARN_ON(atomic_read(&ring->pending) == 0); |
| 747 | atomic_dec(&ring->pending); |
| 748 | sock_put(sk); |
| 749 | |
Pablo Neira | 5e71d9d | 2013-06-03 09:28:43 +0000 | [diff] [blame] | 750 | skb->head = NULL; |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 751 | } |
| 752 | #endif |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame^] | 753 | if (is_vmalloc_addr(skb->head)) { |
| 754 | vfree(skb->head); |
| 755 | skb->head = NULL; |
| 756 | } |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 757 | if (skb->sk != NULL) |
| 758 | sock_rfree(skb); |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | static void netlink_skb_set_owner_r(struct sk_buff *skb, struct sock *sk) |
| 762 | { |
| 763 | WARN_ON(skb->sk != NULL); |
| 764 | skb->sk = sk; |
| 765 | skb->destructor = netlink_skb_destructor; |
| 766 | atomic_add(skb->truesize, &sk->sk_rmem_alloc); |
| 767 | sk_mem_charge(sk, skb->truesize); |
| 768 | } |
| 769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | static void netlink_sock_destruct(struct sock *sk) |
| 771 | { |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 772 | struct netlink_sock *nlk = nlk_sk(sk); |
| 773 | |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 774 | if (nlk->cb) { |
| 775 | if (nlk->cb->done) |
| 776 | nlk->cb->done(nlk->cb); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 777 | |
| 778 | module_put(nlk->cb->module); |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 779 | netlink_destroy_callback(nlk->cb); |
| 780 | } |
| 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | skb_queue_purge(&sk->sk_receive_queue); |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 783 | #ifdef CONFIG_NETLINK_MMAP |
| 784 | if (1) { |
| 785 | struct nl_mmap_req req; |
| 786 | |
| 787 | memset(&req, 0, sizeof(req)); |
| 788 | if (nlk->rx_ring.pg_vec) |
| 789 | netlink_set_ring(sk, &req, true, false); |
| 790 | memset(&req, 0, sizeof(req)); |
| 791 | if (nlk->tx_ring.pg_vec) |
| 792 | netlink_set_ring(sk, &req, true, true); |
| 793 | } |
| 794 | #endif /* CONFIG_NETLINK_MMAP */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
| 796 | if (!sock_flag(sk, SOCK_DEAD)) { |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 797 | printk(KERN_ERR "Freeing alive netlink socket %p\n", sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return; |
| 799 | } |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 800 | |
| 801 | WARN_ON(atomic_read(&sk->sk_rmem_alloc)); |
| 802 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
| 803 | WARN_ON(nlk_sk(sk)->groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 806 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on |
| 807 | * SMP. Look, when several writers sleep and reader wakes them up, all but one |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
| 809 | * this, _but_ remember, it adds useless work on UP machines. |
| 810 | */ |
| 811 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 812 | void netlink_table_grab(void) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 813 | __acquires(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | { |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 815 | might_sleep(); |
| 816 | |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 817 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | |
| 819 | if (atomic_read(&nl_table_users)) { |
| 820 | DECLARE_WAITQUEUE(wait, current); |
| 821 | |
| 822 | add_wait_queue_exclusive(&nl_table_wait, &wait); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 823 | for (;;) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 824 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 825 | if (atomic_read(&nl_table_users) == 0) |
| 826 | break; |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 827 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | schedule(); |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 829 | write_lock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | __set_current_state(TASK_RUNNING); |
| 833 | remove_wait_queue(&nl_table_wait, &wait); |
| 834 | } |
| 835 | } |
| 836 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 837 | void netlink_table_ungrab(void) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 838 | __releases(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | { |
Arjan van de Ven | 6abd219 | 2006-07-03 00:24:07 -0700 | [diff] [blame] | 840 | write_unlock_irq(&nl_table_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 841 | wake_up(&nl_table_wait); |
| 842 | } |
| 843 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 844 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | netlink_lock_table(void) |
| 846 | { |
| 847 | /* read_lock() synchronizes us to netlink_table_grab */ |
| 848 | |
| 849 | read_lock(&nl_table_lock); |
| 850 | atomic_inc(&nl_table_users); |
| 851 | read_unlock(&nl_table_lock); |
| 852 | } |
| 853 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 854 | static inline void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | netlink_unlock_table(void) |
| 856 | { |
| 857 | if (atomic_dec_and_test(&nl_table_users)) |
| 858 | wake_up(&nl_table_wait); |
| 859 | } |
| 860 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 861 | static struct sock *netlink_lookup(struct net *net, int protocol, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 863 | struct nl_portid_hash *hash = &nl_table[protocol].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 864 | struct hlist_head *head; |
| 865 | struct sock *sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 866 | |
| 867 | read_lock(&nl_table_lock); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 868 | head = nl_portid_hashfn(hash, portid); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 869 | sk_for_each(sk, head) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 870 | if (net_eq(sock_net(sk), net) && (nlk_sk(sk)->portid == portid)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | sock_hold(sk); |
| 872 | goto found; |
| 873 | } |
| 874 | } |
| 875 | sk = NULL; |
| 876 | found: |
| 877 | read_unlock(&nl_table_lock); |
| 878 | return sk; |
| 879 | } |
| 880 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 881 | static struct hlist_head *nl_portid_hash_zalloc(size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | { |
| 883 | if (size <= PAGE_SIZE) |
Eric Dumazet | ea72912 | 2007-12-11 02:09:47 -0800 | [diff] [blame] | 884 | return kzalloc(size, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | else |
| 886 | return (struct hlist_head *) |
Eric Dumazet | ea72912 | 2007-12-11 02:09:47 -0800 | [diff] [blame] | 887 | __get_free_pages(GFP_ATOMIC | __GFP_ZERO, |
| 888 | get_order(size)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 891 | static void nl_portid_hash_free(struct hlist_head *table, size_t size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 892 | { |
| 893 | if (size <= PAGE_SIZE) |
| 894 | kfree(table); |
| 895 | else |
| 896 | free_pages((unsigned long)table, get_order(size)); |
| 897 | } |
| 898 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 899 | static int nl_portid_hash_rehash(struct nl_portid_hash *hash, int grow) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 900 | { |
| 901 | unsigned int omask, mask, shift; |
| 902 | size_t osize, size; |
| 903 | struct hlist_head *otable, *table; |
| 904 | int i; |
| 905 | |
| 906 | omask = mask = hash->mask; |
| 907 | osize = size = (mask + 1) * sizeof(*table); |
| 908 | shift = hash->shift; |
| 909 | |
| 910 | if (grow) { |
| 911 | if (++shift > hash->max_shift) |
| 912 | return 0; |
| 913 | mask = mask * 2 + 1; |
| 914 | size *= 2; |
| 915 | } |
| 916 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 917 | table = nl_portid_hash_zalloc(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | if (!table) |
| 919 | return 0; |
| 920 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | otable = hash->table; |
| 922 | hash->table = table; |
| 923 | hash->mask = mask; |
| 924 | hash->shift = shift; |
| 925 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); |
| 926 | |
| 927 | for (i = 0; i <= omask; i++) { |
| 928 | struct sock *sk; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 929 | struct hlist_node *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 931 | sk_for_each_safe(sk, tmp, &otable[i]) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 932 | __sk_add_node(sk, nl_portid_hashfn(hash, nlk_sk(sk)->portid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | } |
| 934 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 935 | nl_portid_hash_free(otable, osize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | hash->rehash_time = jiffies + 10 * 60 * HZ; |
| 937 | return 1; |
| 938 | } |
| 939 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 940 | static inline int nl_portid_hash_dilute(struct nl_portid_hash *hash, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 | { |
| 942 | int avg = hash->entries >> hash->shift; |
| 943 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 944 | if (unlikely(avg > 1) && nl_portid_hash_rehash(hash, 1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | return 1; |
| 946 | |
| 947 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 948 | nl_portid_hash_rehash(hash, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | return 1; |
| 950 | } |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 955 | static const struct proto_ops netlink_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 956 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 957 | static void |
| 958 | netlink_update_listeners(struct sock *sk) |
| 959 | { |
| 960 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 961 | unsigned long mask; |
| 962 | unsigned int i; |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 963 | struct listeners *listeners; |
| 964 | |
| 965 | listeners = nl_deref_protected(tbl->listeners); |
| 966 | if (!listeners) |
| 967 | return; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 968 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 969 | for (i = 0; i < NLGRPLONGS(tbl->groups); i++) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 970 | mask = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 971 | sk_for_each_bound(sk, &tbl->mc_list) { |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 972 | if (i < NLGRPLONGS(nlk_sk(sk)->ngroups)) |
| 973 | mask |= nlk_sk(sk)->groups[i]; |
| 974 | } |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 975 | listeners->masks[i] = mask; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 976 | } |
| 977 | /* this function is only called with the netlink table "grabbed", which |
| 978 | * makes sure updates are visible before bind or setsockopt return. */ |
| 979 | } |
| 980 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 981 | static int netlink_insert(struct sock *sk, struct net *net, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 983 | struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | struct hlist_head *head; |
| 985 | int err = -EADDRINUSE; |
| 986 | struct sock *osk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | int len; |
| 988 | |
| 989 | netlink_table_grab(); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 990 | head = nl_portid_hashfn(hash, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | len = 0; |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 992 | sk_for_each(osk, head) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 993 | if (net_eq(sock_net(osk), net) && (nlk_sk(osk)->portid == portid)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | break; |
| 995 | len++; |
| 996 | } |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 997 | if (osk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | goto err; |
| 999 | |
| 1000 | err = -EBUSY; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1001 | if (nlk_sk(sk)->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1002 | goto err; |
| 1003 | |
| 1004 | err = -ENOMEM; |
| 1005 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) |
| 1006 | goto err; |
| 1007 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1008 | if (len && nl_portid_hash_dilute(hash, len)) |
| 1009 | head = nl_portid_hashfn(hash, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | hash->entries++; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1011 | nlk_sk(sk)->portid = portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | sk_add_node(sk, head); |
| 1013 | err = 0; |
| 1014 | |
| 1015 | err: |
| 1016 | netlink_table_ungrab(); |
| 1017 | return err; |
| 1018 | } |
| 1019 | |
| 1020 | static void netlink_remove(struct sock *sk) |
| 1021 | { |
| 1022 | netlink_table_grab(); |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 1023 | if (sk_del_node_init(sk)) |
| 1024 | nl_table[sk->sk_protocol].hash.entries--; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1025 | if (nlk_sk(sk)->subscriptions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | __sk_del_bind_node(sk); |
| 1027 | netlink_table_ungrab(); |
| 1028 | } |
| 1029 | |
| 1030 | static struct proto netlink_proto = { |
| 1031 | .name = "NETLINK", |
| 1032 | .owner = THIS_MODULE, |
| 1033 | .obj_size = sizeof(struct netlink_sock), |
| 1034 | }; |
| 1035 | |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 1036 | static int __netlink_create(struct net *net, struct socket *sock, |
| 1037 | struct mutex *cb_mutex, int protocol) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | { |
| 1039 | struct sock *sk; |
| 1040 | struct netlink_sock *nlk; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1041 | |
| 1042 | sock->ops = &netlink_ops; |
| 1043 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 1044 | sk = sk_alloc(net, PF_NETLINK, GFP_KERNEL, &netlink_proto); |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1045 | if (!sk) |
| 1046 | return -ENOMEM; |
| 1047 | |
| 1048 | sock_init_data(sock, sk); |
| 1049 | |
| 1050 | nlk = nlk_sk(sk); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1051 | if (cb_mutex) { |
Patrick McHardy | ffa4d72 | 2007-04-25 14:01:17 -0700 | [diff] [blame] | 1052 | nlk->cb_mutex = cb_mutex; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1053 | } else { |
Patrick McHardy | ffa4d72 | 2007-04-25 14:01:17 -0700 | [diff] [blame] | 1054 | nlk->cb_mutex = &nlk->cb_def_mutex; |
| 1055 | mutex_init(nlk->cb_mutex); |
| 1056 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1057 | init_waitqueue_head(&nlk->wait); |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 1058 | #ifdef CONFIG_NETLINK_MMAP |
| 1059 | mutex_init(&nlk->pg_vec_lock); |
| 1060 | #endif |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1061 | |
| 1062 | sk->sk_destruct = netlink_sock_destruct; |
| 1063 | sk->sk_protocol = protocol; |
| 1064 | return 0; |
| 1065 | } |
| 1066 | |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 1067 | static int netlink_create(struct net *net, struct socket *sock, int protocol, |
| 1068 | int kern) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1069 | { |
| 1070 | struct module *module = NULL; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1071 | struct mutex *cb_mutex; |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1072 | struct netlink_sock *nlk; |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1073 | void (*bind)(int group); |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1074 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
| 1076 | sock->state = SS_UNCONNECTED; |
| 1077 | |
| 1078 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) |
| 1079 | return -ESOCKTNOSUPPORT; |
| 1080 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1081 | if (protocol < 0 || protocol >= MAX_LINKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | return -EPROTONOSUPPORT; |
| 1083 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1084 | netlink_lock_table(); |
Johannes Berg | 95a5afc | 2008-10-16 15:24:51 -0700 | [diff] [blame] | 1085 | #ifdef CONFIG_MODULES |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1086 | if (!nl_table[protocol].registered) { |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1087 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1088 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1089 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1090 | } |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1091 | #endif |
| 1092 | if (nl_table[protocol].registered && |
| 1093 | try_module_get(nl_table[protocol].module)) |
| 1094 | module = nl_table[protocol].module; |
Alexey Dobriyan | 974c37e | 2010-01-30 10:05:05 +0000 | [diff] [blame] | 1095 | else |
| 1096 | err = -EPROTONOSUPPORT; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 1097 | cb_mutex = nl_table[protocol].cb_mutex; |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1098 | bind = nl_table[protocol].bind; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1099 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1100 | |
Alexey Dobriyan | 974c37e | 2010-01-30 10:05:05 +0000 | [diff] [blame] | 1101 | if (err < 0) |
| 1102 | goto out; |
| 1103 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1104 | err = __netlink_create(net, sock, cb_mutex, protocol); |
| 1105 | if (err < 0) |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1106 | goto out_module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 1108 | local_bh_disable(); |
Eric Dumazet | c1fd3b9 | 2008-11-23 15:48:22 -0800 | [diff] [blame] | 1109 | sock_prot_inuse_add(net, &netlink_proto, 1); |
David S. Miller | 6f756a8 | 2008-11-23 17:34:03 -0800 | [diff] [blame] | 1110 | local_bh_enable(); |
| 1111 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1112 | nlk = nlk_sk(sock->sk); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1113 | nlk->module = module; |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1114 | nlk->netlink_bind = bind; |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1115 | out: |
| 1116 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1117 | |
Patrick McHardy | ab33a17 | 2005-08-14 19:31:36 -0700 | [diff] [blame] | 1118 | out_module: |
| 1119 | module_put(module); |
| 1120 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static int netlink_release(struct socket *sock) |
| 1124 | { |
| 1125 | struct sock *sk = sock->sk; |
| 1126 | struct netlink_sock *nlk; |
| 1127 | |
| 1128 | if (!sk) |
| 1129 | return 0; |
| 1130 | |
| 1131 | netlink_remove(sk); |
Denis Lunev | ac57b3a | 2007-04-18 17:05:58 -0700 | [diff] [blame] | 1132 | sock_orphan(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | nlk = nlk_sk(sk); |
| 1134 | |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 1135 | /* |
| 1136 | * OK. Socket is unlinked, any packets that arrive now |
| 1137 | * will be purged. |
| 1138 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | sock->sk = NULL; |
| 1141 | wake_up_interruptible_all(&nlk->wait); |
| 1142 | |
| 1143 | skb_queue_purge(&sk->sk_write_queue); |
| 1144 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1145 | if (nlk->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 | struct netlink_notify n = { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1147 | .net = sock_net(sk), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | .protocol = sk->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1149 | .portid = nlk->portid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1150 | }; |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 1151 | atomic_notifier_call_chain(&netlink_chain, |
| 1152 | NETLINK_URELEASE, &n); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1153 | } |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1154 | |
Mariusz Kozlowski | 5e7c001 | 2007-01-02 15:24:30 -0800 | [diff] [blame] | 1155 | module_put(nlk->module); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1156 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1157 | netlink_table_grab(); |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1158 | if (netlink_is_kernel(sk)) { |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1159 | BUG_ON(nl_table[sk->sk_protocol].registered == 0); |
| 1160 | if (--nl_table[sk->sk_protocol].registered == 0) { |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1161 | struct listeners *old; |
| 1162 | |
| 1163 | old = nl_deref_protected(nl_table[sk->sk_protocol].listeners); |
| 1164 | RCU_INIT_POINTER(nl_table[sk->sk_protocol].listeners, NULL); |
| 1165 | kfree_rcu(old, rcu); |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1166 | nl_table[sk->sk_protocol].module = NULL; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1167 | nl_table[sk->sk_protocol].bind = NULL; |
| 1168 | nl_table[sk->sk_protocol].flags = 0; |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 1169 | nl_table[sk->sk_protocol].registered = 0; |
| 1170 | } |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1171 | } else if (nlk->subscriptions) { |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1172 | netlink_update_listeners(sk); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1173 | } |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1174 | netlink_table_ungrab(); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 1175 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1176 | kfree(nlk->groups); |
| 1177 | nlk->groups = NULL; |
| 1178 | |
Eric Dumazet | 3755810 | 2008-11-24 14:05:22 -0800 | [diff] [blame] | 1179 | local_bh_disable(); |
Eric Dumazet | c1fd3b9 | 2008-11-23 15:48:22 -0800 | [diff] [blame] | 1180 | sock_prot_inuse_add(sock_net(sk), &netlink_proto, -1); |
Eric Dumazet | 3755810 | 2008-11-24 14:05:22 -0800 | [diff] [blame] | 1181 | local_bh_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1182 | sock_put(sk); |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | static int netlink_autobind(struct socket *sock) |
| 1187 | { |
| 1188 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1189 | struct net *net = sock_net(sk); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1190 | struct nl_portid_hash *hash = &nl_table[sk->sk_protocol].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1191 | struct hlist_head *head; |
| 1192 | struct sock *osk; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1193 | s32 portid = task_tgid_vnr(current); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1194 | int err; |
| 1195 | static s32 rover = -4097; |
| 1196 | |
| 1197 | retry: |
| 1198 | cond_resched(); |
| 1199 | netlink_table_grab(); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1200 | head = nl_portid_hashfn(hash, portid); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1201 | sk_for_each(osk, head) { |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1202 | if (!net_eq(sock_net(osk), net)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1203 | continue; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1204 | if (nlk_sk(osk)->portid == portid) { |
| 1205 | /* Bind collision, search negative portid values. */ |
| 1206 | portid = rover--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | if (rover > -4097) |
| 1208 | rover = -4097; |
| 1209 | netlink_table_ungrab(); |
| 1210 | goto retry; |
| 1211 | } |
| 1212 | } |
| 1213 | netlink_table_ungrab(); |
| 1214 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1215 | err = netlink_insert(sk, net, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | if (err == -EADDRINUSE) |
| 1217 | goto retry; |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 1218 | |
| 1219 | /* If 2 threads race to autobind, that is fine. */ |
| 1220 | if (err == -EBUSY) |
| 1221 | err = 0; |
| 1222 | |
| 1223 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1224 | } |
| 1225 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1226 | static inline int netlink_capable(const struct socket *sock, unsigned int flag) |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1227 | { |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1228 | return (nl_table[sock->sk->sk_protocol].flags & flag) || |
Eric W. Biederman | df008c9 | 2012-11-16 03:03:07 +0000 | [diff] [blame] | 1229 | ns_capable(sock_net(sock->sk)->user_ns, CAP_NET_ADMIN); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1232 | static void |
| 1233 | netlink_update_subscriptions(struct sock *sk, unsigned int subscriptions) |
| 1234 | { |
| 1235 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1236 | |
| 1237 | if (nlk->subscriptions && !subscriptions) |
| 1238 | __sk_del_bind_node(sk); |
| 1239 | else if (!nlk->subscriptions && subscriptions) |
| 1240 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); |
| 1241 | nlk->subscriptions = subscriptions; |
| 1242 | } |
| 1243 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1244 | static int netlink_realloc_groups(struct sock *sk) |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1245 | { |
| 1246 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1247 | unsigned int groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1248 | unsigned long *new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1249 | int err = 0; |
| 1250 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1251 | netlink_table_grab(); |
| 1252 | |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1253 | groups = nl_table[sk->sk_protocol].groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1254 | if (!nl_table[sk->sk_protocol].registered) { |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1255 | err = -ENOENT; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1256 | goto out_unlock; |
| 1257 | } |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1258 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1259 | if (nlk->ngroups >= groups) |
| 1260 | goto out_unlock; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1261 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1262 | new_groups = krealloc(nlk->groups, NLGRPSZ(groups), GFP_ATOMIC); |
| 1263 | if (new_groups == NULL) { |
| 1264 | err = -ENOMEM; |
| 1265 | goto out_unlock; |
| 1266 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1267 | memset((char *)new_groups + NLGRPSZ(nlk->ngroups), 0, |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1268 | NLGRPSZ(groups) - NLGRPSZ(nlk->ngroups)); |
| 1269 | |
| 1270 | nlk->groups = new_groups; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1271 | nlk->ngroups = groups; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1272 | out_unlock: |
| 1273 | netlink_table_ungrab(); |
| 1274 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1275 | } |
| 1276 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1277 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, |
| 1278 | int addr_len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1279 | { |
| 1280 | struct sock *sk = sock->sk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1281 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1283 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
| 1284 | int err; |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1285 | |
Hannes Frederic Sowa | 4e4b537 | 2012-12-15 15:42:19 +0000 | [diff] [blame] | 1286 | if (addr_len < sizeof(struct sockaddr_nl)) |
| 1287 | return -EINVAL; |
| 1288 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | if (nladdr->nl_family != AF_NETLINK) |
| 1290 | return -EINVAL; |
| 1291 | |
| 1292 | /* Only superuser is allowed to listen multicasts */ |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1293 | if (nladdr->nl_groups) { |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1294 | if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV)) |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1295 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1296 | err = netlink_realloc_groups(sk); |
| 1297 | if (err) |
| 1298 | return err; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1300 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1301 | if (nlk->portid) { |
| 1302 | if (nladdr->nl_pid != nlk->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1303 | return -EINVAL; |
| 1304 | } else { |
| 1305 | err = nladdr->nl_pid ? |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1306 | netlink_insert(sk, net, nladdr->nl_pid) : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | netlink_autobind(sock); |
| 1308 | if (err) |
| 1309 | return err; |
| 1310 | } |
| 1311 | |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1312 | if (!nladdr->nl_groups && (nlk->groups == NULL || !(u32)nlk->groups[0])) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1313 | return 0; |
| 1314 | |
| 1315 | netlink_table_grab(); |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1316 | netlink_update_subscriptions(sk, nlk->subscriptions + |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1317 | hweight32(nladdr->nl_groups) - |
| 1318 | hweight32(nlk->groups[0])); |
| 1319 | nlk->groups[0] = (nlk->groups[0] & ~0xffffffffUL) | nladdr->nl_groups; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1320 | netlink_update_listeners(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | netlink_table_ungrab(); |
| 1322 | |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1323 | if (nlk->netlink_bind && nlk->groups[0]) { |
| 1324 | int i; |
| 1325 | |
| 1326 | for (i=0; i<nlk->ngroups; i++) { |
| 1327 | if (test_bit(i, nlk->groups)) |
| 1328 | nlk->netlink_bind(i); |
| 1329 | } |
| 1330 | } |
| 1331 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 | return 0; |
| 1333 | } |
| 1334 | |
| 1335 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, |
| 1336 | int alen, int flags) |
| 1337 | { |
| 1338 | int err = 0; |
| 1339 | struct sock *sk = sock->sk; |
| 1340 | struct netlink_sock *nlk = nlk_sk(sk); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1341 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
Changli Gao | 6503d96 | 2010-03-31 22:58:26 +0000 | [diff] [blame] | 1343 | if (alen < sizeof(addr->sa_family)) |
| 1344 | return -EINVAL; |
| 1345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | if (addr->sa_family == AF_UNSPEC) { |
| 1347 | sk->sk_state = NETLINK_UNCONNECTED; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1348 | nlk->dst_portid = 0; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1349 | nlk->dst_group = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | return 0; |
| 1351 | } |
| 1352 | if (addr->sa_family != AF_NETLINK) |
| 1353 | return -EINVAL; |
| 1354 | |
| 1355 | /* Only superuser is allowed to send multicasts */ |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1356 | if (nladdr->nl_groups && !netlink_capable(sock, NL_CFG_F_NONROOT_SEND)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | return -EPERM; |
| 1358 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1359 | if (!nlk->portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | err = netlink_autobind(sock); |
| 1361 | |
| 1362 | if (err == 0) { |
| 1363 | sk->sk_state = NETLINK_CONNECTED; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1364 | nlk->dst_portid = nladdr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1365 | nlk->dst_group = ffs(nladdr->nl_groups); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | return err; |
| 1369 | } |
| 1370 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1371 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, |
| 1372 | int *addr_len, int peer) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | { |
| 1374 | struct sock *sk = sock->sk; |
| 1375 | struct netlink_sock *nlk = nlk_sk(sk); |
Cyrill Gorcunov | 13cfa97 | 2009-11-08 05:51:19 +0000 | [diff] [blame] | 1376 | DECLARE_SOCKADDR(struct sockaddr_nl *, nladdr, addr); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 1377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | nladdr->nl_family = AF_NETLINK; |
| 1379 | nladdr->nl_pad = 0; |
| 1380 | *addr_len = sizeof(*nladdr); |
| 1381 | |
| 1382 | if (peer) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1383 | nladdr->nl_pid = nlk->dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 1384 | nladdr->nl_groups = netlink_group_mask(nlk->dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1385 | } else { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1386 | nladdr->nl_pid = nlk->portid; |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 1387 | nladdr->nl_groups = nlk->groups ? nlk->groups[0] : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | } |
| 1389 | return 0; |
| 1390 | } |
| 1391 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1392 | static struct sock *netlink_getsockbyportid(struct sock *ssk, u32 portid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | struct sock *sock; |
| 1395 | struct netlink_sock *nlk; |
| 1396 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1397 | sock = netlink_lookup(sock_net(ssk), ssk->sk_protocol, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1398 | if (!sock) |
| 1399 | return ERR_PTR(-ECONNREFUSED); |
| 1400 | |
| 1401 | /* Don't bother queuing skb if kernel socket has no input function */ |
| 1402 | nlk = nlk_sk(sock); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1403 | if (sock->sk_state == NETLINK_CONNECTED && |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1404 | nlk->dst_portid != nlk_sk(ssk)->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1405 | sock_put(sock); |
| 1406 | return ERR_PTR(-ECONNREFUSED); |
| 1407 | } |
| 1408 | return sock; |
| 1409 | } |
| 1410 | |
| 1411 | struct sock *netlink_getsockbyfilp(struct file *filp) |
| 1412 | { |
Al Viro | 496ad9a | 2013-01-23 17:07:38 -0500 | [diff] [blame] | 1413 | struct inode *inode = file_inode(filp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | struct sock *sock; |
| 1415 | |
| 1416 | if (!S_ISSOCK(inode->i_mode)) |
| 1417 | return ERR_PTR(-ENOTSOCK); |
| 1418 | |
| 1419 | sock = SOCKET_I(inode)->sk; |
| 1420 | if (sock->sk_family != AF_NETLINK) |
| 1421 | return ERR_PTR(-EINVAL); |
| 1422 | |
| 1423 | sock_hold(sock); |
| 1424 | return sock; |
| 1425 | } |
| 1426 | |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame^] | 1427 | static struct sk_buff *netlink_alloc_large_skb(unsigned int size) |
| 1428 | { |
| 1429 | struct sk_buff *skb; |
| 1430 | void *data; |
| 1431 | |
| 1432 | if (size <= NLMSG_GOODSIZE) |
| 1433 | return alloc_skb(size, GFP_KERNEL); |
| 1434 | |
| 1435 | skb = alloc_skb_head(GFP_KERNEL); |
| 1436 | if (skb == NULL) |
| 1437 | return NULL; |
| 1438 | |
| 1439 | data = vmalloc(size); |
| 1440 | if (data == NULL) |
| 1441 | goto err; |
| 1442 | |
| 1443 | skb->head = data; |
| 1444 | skb->data = data; |
| 1445 | skb_reset_tail_pointer(skb); |
| 1446 | skb->end = skb->tail + size; |
| 1447 | skb->len = 0; |
| 1448 | skb->destructor = netlink_skb_destructor; |
| 1449 | |
| 1450 | return skb; |
| 1451 | err: |
| 1452 | kfree_skb(skb); |
| 1453 | return NULL; |
| 1454 | } |
| 1455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | /* |
| 1457 | * Attach a skb to a netlink socket. |
| 1458 | * The caller must hold a reference to the destination socket. On error, the |
| 1459 | * reference is dropped. The skb is not send to the destination, just all |
| 1460 | * all error checks are performed and memory in the queue is reserved. |
| 1461 | * Return values: |
| 1462 | * < 0: error. skb freed, reference to sock dropped. |
| 1463 | * 0: continue |
| 1464 | * 1: repeat lookup - reference dropped while waiting for socket memory. |
| 1465 | */ |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1466 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1467 | long *timeo, struct sock *ssk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1468 | { |
| 1469 | struct netlink_sock *nlk; |
| 1470 | |
| 1471 | nlk = nlk_sk(sk); |
| 1472 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 1473 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 1474 | test_bit(NETLINK_CONGESTED, &nlk->state)) && |
| 1475 | !netlink_skb_is_mmaped(skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | DECLARE_WAITQUEUE(wait, current); |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1477 | if (!*timeo) { |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1478 | if (!ssk || netlink_is_kernel(ssk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | netlink_overrun(sk); |
| 1480 | sock_put(sk); |
| 1481 | kfree_skb(skb); |
| 1482 | return -EAGAIN; |
| 1483 | } |
| 1484 | |
| 1485 | __set_current_state(TASK_INTERRUPTIBLE); |
| 1486 | add_wait_queue(&nlk->wait, &wait); |
| 1487 | |
| 1488 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 1489 | test_bit(NETLINK_CONGESTED, &nlk->state)) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | !sock_flag(sk, SOCK_DEAD)) |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1491 | *timeo = schedule_timeout(*timeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | |
| 1493 | __set_current_state(TASK_RUNNING); |
| 1494 | remove_wait_queue(&nlk->wait, &wait); |
| 1495 | sock_put(sk); |
| 1496 | |
| 1497 | if (signal_pending(current)) { |
| 1498 | kfree_skb(skb); |
Patrick McHardy | c3d8d1e | 2007-11-07 02:42:09 -0800 | [diff] [blame] | 1499 | return sock_intr_errno(*timeo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 | } |
| 1501 | return 1; |
| 1502 | } |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1503 | netlink_skb_set_owner_r(skb, sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | return 0; |
| 1505 | } |
| 1506 | |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1507 | static int __netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | int len = skb->len; |
| 1510 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1511 | #ifdef CONFIG_NETLINK_MMAP |
| 1512 | if (netlink_skb_is_mmaped(skb)) |
| 1513 | netlink_queue_mmaped_skb(sk, skb); |
| 1514 | else if (netlink_rx_is_mmaped(sk)) |
| 1515 | netlink_ring_set_copied(sk, skb); |
| 1516 | else |
| 1517 | #endif /* CONFIG_NETLINK_MMAP */ |
| 1518 | skb_queue_tail(&sk->sk_receive_queue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1519 | sk->sk_data_ready(sk, len); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1520 | return len; |
| 1521 | } |
| 1522 | |
| 1523 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb) |
| 1524 | { |
| 1525 | int len = __netlink_sendskb(sk, skb); |
| 1526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | sock_put(sk); |
| 1528 | return len; |
| 1529 | } |
| 1530 | |
| 1531 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) |
| 1532 | { |
| 1533 | kfree_skb(skb); |
| 1534 | sock_put(sk); |
| 1535 | } |
| 1536 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1537 | static struct sk_buff *netlink_trim(struct sk_buff *skb, gfp_t allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | { |
| 1539 | int delta; |
| 1540 | |
Patrick McHardy | 1298ca4 | 2013-04-17 06:46:59 +0000 | [diff] [blame] | 1541 | WARN_ON(skb->sk != NULL); |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 1542 | if (netlink_skb_is_mmaped(skb)) |
| 1543 | return skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
Arnaldo Carvalho de Melo | 4305b54 | 2007-04-19 20:43:29 -0700 | [diff] [blame] | 1545 | delta = skb->end - skb->tail; |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame^] | 1546 | if (is_vmalloc_addr(skb->head) || delta * 2 < skb->truesize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | return skb; |
| 1548 | |
| 1549 | if (skb_shared(skb)) { |
| 1550 | struct sk_buff *nskb = skb_clone(skb, allocation); |
| 1551 | if (!nskb) |
| 1552 | return skb; |
Eric Dumazet | 8460c00 | 2012-04-19 02:24:28 +0000 | [diff] [blame] | 1553 | consume_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | skb = nskb; |
| 1555 | } |
| 1556 | |
| 1557 | if (!pskb_expand_head(skb, 0, -delta, allocation)) |
| 1558 | skb->truesize -= delta; |
| 1559 | |
| 1560 | return skb; |
| 1561 | } |
| 1562 | |
Eric W. Biederman | 3fbc290 | 2012-05-24 17:21:27 -0600 | [diff] [blame] | 1563 | static int netlink_unicast_kernel(struct sock *sk, struct sk_buff *skb, |
| 1564 | struct sock *ssk) |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1565 | { |
| 1566 | int ret; |
| 1567 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1568 | |
| 1569 | ret = -ECONNREFUSED; |
| 1570 | if (nlk->netlink_rcv != NULL) { |
| 1571 | ret = skb->len; |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1572 | netlink_skb_set_owner_r(skb, sk); |
Patrick McHardy | e32123e | 2013-04-17 06:46:57 +0000 | [diff] [blame] | 1573 | NETLINK_CB(skb).sk = ssk; |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1574 | nlk->netlink_rcv(skb); |
Eric Dumazet | bfb253c | 2012-04-22 21:30:29 +0000 | [diff] [blame] | 1575 | consume_skb(skb); |
| 1576 | } else { |
| 1577 | kfree_skb(skb); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1578 | } |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1579 | sock_put(sk); |
| 1580 | return ret; |
| 1581 | } |
| 1582 | |
| 1583 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1584 | u32 portid, int nonblock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1585 | { |
| 1586 | struct sock *sk; |
| 1587 | int err; |
| 1588 | long timeo; |
| 1589 | |
| 1590 | skb = netlink_trim(skb, gfp_any()); |
| 1591 | |
| 1592 | timeo = sock_sndtimeo(ssk, nonblock); |
| 1593 | retry: |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1594 | sk = netlink_getsockbyportid(ssk, portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1595 | if (IS_ERR(sk)) { |
| 1596 | kfree_skb(skb); |
| 1597 | return PTR_ERR(sk); |
| 1598 | } |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1599 | if (netlink_is_kernel(sk)) |
Eric W. Biederman | 3fbc290 | 2012-05-24 17:21:27 -0600 | [diff] [blame] | 1600 | return netlink_unicast_kernel(sk, skb, ssk); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 1601 | |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1602 | if (sk_filter(sk, skb)) { |
Wang Chen | 8487460 | 2008-07-01 19:55:09 -0700 | [diff] [blame] | 1603 | err = skb->len; |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1604 | kfree_skb(skb); |
| 1605 | sock_put(sk); |
| 1606 | return err; |
| 1607 | } |
| 1608 | |
Denis V. Lunev | 9457afe | 2008-06-05 11:23:39 -0700 | [diff] [blame] | 1609 | err = netlink_attachskb(sk, skb, &timeo, ssk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | if (err == 1) |
| 1611 | goto retry; |
| 1612 | if (err) |
| 1613 | return err; |
| 1614 | |
Denis V. Lunev | 7ee015e | 2007-10-10 21:14:03 -0700 | [diff] [blame] | 1615 | return netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1617 | EXPORT_SYMBOL(netlink_unicast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1619 | struct sk_buff *netlink_alloc_skb(struct sock *ssk, unsigned int size, |
| 1620 | u32 dst_portid, gfp_t gfp_mask) |
| 1621 | { |
| 1622 | #ifdef CONFIG_NETLINK_MMAP |
| 1623 | struct sock *sk = NULL; |
| 1624 | struct sk_buff *skb; |
| 1625 | struct netlink_ring *ring; |
| 1626 | struct nl_mmap_hdr *hdr; |
| 1627 | unsigned int maxlen; |
| 1628 | |
| 1629 | sk = netlink_getsockbyportid(ssk, dst_portid); |
| 1630 | if (IS_ERR(sk)) |
| 1631 | goto out; |
| 1632 | |
| 1633 | ring = &nlk_sk(sk)->rx_ring; |
| 1634 | /* fast-path without atomic ops for common case: non-mmaped receiver */ |
| 1635 | if (ring->pg_vec == NULL) |
| 1636 | goto out_put; |
| 1637 | |
| 1638 | skb = alloc_skb_head(gfp_mask); |
| 1639 | if (skb == NULL) |
| 1640 | goto err1; |
| 1641 | |
| 1642 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 1643 | /* check again under lock */ |
| 1644 | if (ring->pg_vec == NULL) |
| 1645 | goto out_free; |
| 1646 | |
| 1647 | maxlen = ring->frame_size - NL_MMAP_HDRLEN; |
| 1648 | if (maxlen < size) |
| 1649 | goto out_free; |
| 1650 | |
| 1651 | netlink_forward_ring(ring); |
| 1652 | hdr = netlink_current_frame(ring, NL_MMAP_STATUS_UNUSED); |
| 1653 | if (hdr == NULL) |
| 1654 | goto err2; |
| 1655 | netlink_ring_setup_skb(skb, sk, ring, hdr); |
| 1656 | netlink_set_status(hdr, NL_MMAP_STATUS_RESERVED); |
| 1657 | atomic_inc(&ring->pending); |
| 1658 | netlink_increment_head(ring); |
| 1659 | |
| 1660 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1661 | return skb; |
| 1662 | |
| 1663 | err2: |
| 1664 | kfree_skb(skb); |
| 1665 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
Patrick McHardy | cd1df52 | 2013-04-17 06:47:05 +0000 | [diff] [blame] | 1666 | netlink_overrun(sk); |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 1667 | err1: |
| 1668 | sock_put(sk); |
| 1669 | return NULL; |
| 1670 | |
| 1671 | out_free: |
| 1672 | kfree_skb(skb); |
| 1673 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 1674 | out_put: |
| 1675 | sock_put(sk); |
| 1676 | out: |
| 1677 | #endif |
| 1678 | return alloc_skb(size, gfp_mask); |
| 1679 | } |
| 1680 | EXPORT_SYMBOL_GPL(netlink_alloc_skb); |
| 1681 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1682 | int netlink_has_listeners(struct sock *sk, unsigned int group) |
| 1683 | { |
| 1684 | int res = 0; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 1685 | struct listeners *listeners; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1686 | |
Denis V. Lunev | aed8156 | 2007-10-10 21:14:32 -0700 | [diff] [blame] | 1687 | BUG_ON(!netlink_is_kernel(sk)); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1688 | |
| 1689 | rcu_read_lock(); |
| 1690 | listeners = rcu_dereference(nl_table[sk->sk_protocol].listeners); |
| 1691 | |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 1692 | if (listeners && group - 1 < nl_table[sk->sk_protocol].groups) |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 1693 | res = test_bit(group - 1, listeners->masks); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1694 | |
| 1695 | rcu_read_unlock(); |
| 1696 | |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 1697 | return res; |
| 1698 | } |
| 1699 | EXPORT_SYMBOL_GPL(netlink_has_listeners); |
| 1700 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1701 | static int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | { |
| 1703 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1704 | |
| 1705 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 1706 | !test_bit(NETLINK_CONGESTED, &nlk->state)) { |
Patrick McHardy | cf0a018 | 2013-04-17 06:47:00 +0000 | [diff] [blame] | 1707 | netlink_skb_set_owner_r(skb, sk); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 1708 | __netlink_sendskb(sk, skb); |
stephen hemminger | 2c645800 | 2011-12-22 08:52:03 +0000 | [diff] [blame] | 1709 | return atomic_read(&sk->sk_rmem_alloc) > (sk->sk_rcvbuf >> 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | } |
| 1711 | return -1; |
| 1712 | } |
| 1713 | |
| 1714 | struct netlink_broadcast_data { |
| 1715 | struct sock *exclude_sk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1716 | struct net *net; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1717 | u32 portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | u32 group; |
| 1719 | int failure; |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1720 | int delivery_failure; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | int congested; |
| 1722 | int delivered; |
Al Viro | 7d877f3 | 2005-10-21 03:20:43 -0400 | [diff] [blame] | 1723 | gfp_t allocation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | struct sk_buff *skb, *skb2; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1725 | int (*tx_filter)(struct sock *dsk, struct sk_buff *skb, void *data); |
| 1726 | void *tx_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | }; |
| 1728 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1729 | static int do_one_broadcast(struct sock *sk, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | struct netlink_broadcast_data *p) |
| 1731 | { |
| 1732 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1733 | int val; |
| 1734 | |
| 1735 | if (p->exclude_sk == sk) |
| 1736 | goto out; |
| 1737 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1738 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1739 | !test_bit(p->group - 1, nlk->groups)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | goto out; |
| 1741 | |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 1742 | if (!net_eq(sock_net(sk), p->net)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1743 | goto out; |
| 1744 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | if (p->failure) { |
| 1746 | netlink_overrun(sk); |
| 1747 | goto out; |
| 1748 | } |
| 1749 | |
| 1750 | sock_hold(sk); |
| 1751 | if (p->skb2 == NULL) { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 1752 | if (skb_shared(p->skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | p->skb2 = skb_clone(p->skb, p->allocation); |
| 1754 | } else { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 1755 | p->skb2 = skb_get(p->skb); |
| 1756 | /* |
| 1757 | * skb ownership may have been set when |
| 1758 | * delivered to a previous socket. |
| 1759 | */ |
| 1760 | skb_orphan(p->skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } |
| 1763 | if (p->skb2 == NULL) { |
| 1764 | netlink_overrun(sk); |
| 1765 | /* Clone failed. Notify ALL listeners. */ |
| 1766 | p->failure = 1; |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 1767 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
| 1768 | p->delivery_failure = 1; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1769 | } else if (p->tx_filter && p->tx_filter(sk, p->skb2, p->tx_data)) { |
| 1770 | kfree_skb(p->skb2); |
| 1771 | p->skb2 = NULL; |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 1772 | } else if (sk_filter(sk, p->skb2)) { |
| 1773 | kfree_skb(p->skb2); |
| 1774 | p->skb2 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
| 1776 | netlink_overrun(sk); |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 1777 | if (nlk->flags & NETLINK_BROADCAST_SEND_ERROR) |
| 1778 | p->delivery_failure = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1779 | } else { |
| 1780 | p->congested |= val; |
| 1781 | p->delivered = 1; |
| 1782 | p->skb2 = NULL; |
| 1783 | } |
| 1784 | sock_put(sk); |
| 1785 | |
| 1786 | out: |
| 1787 | return 0; |
| 1788 | } |
| 1789 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1790 | int netlink_broadcast_filtered(struct sock *ssk, struct sk_buff *skb, u32 portid, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1791 | u32 group, gfp_t allocation, |
| 1792 | int (*filter)(struct sock *dsk, struct sk_buff *skb, void *data), |
| 1793 | void *filter_data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | { |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 1795 | struct net *net = sock_net(ssk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1796 | struct netlink_broadcast_data info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | struct sock *sk; |
| 1798 | |
| 1799 | skb = netlink_trim(skb, allocation); |
| 1800 | |
| 1801 | info.exclude_sk = ssk; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1802 | info.net = net; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1803 | info.portid = portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | info.group = group; |
| 1805 | info.failure = 0; |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1806 | info.delivery_failure = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1807 | info.congested = 0; |
| 1808 | info.delivered = 0; |
| 1809 | info.allocation = allocation; |
| 1810 | info.skb = skb; |
| 1811 | info.skb2 = NULL; |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1812 | info.tx_filter = filter; |
| 1813 | info.tx_data = filter_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | |
| 1815 | /* While we sleep in clone, do not allow to change socket list */ |
| 1816 | |
| 1817 | netlink_lock_table(); |
| 1818 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1819 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | do_one_broadcast(sk, &info); |
| 1821 | |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 1822 | consume_skb(skb); |
Tommy S. Christensen | aa1c6a6 | 2005-05-19 13:07:32 -0700 | [diff] [blame] | 1823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | netlink_unlock_table(); |
| 1825 | |
Neil Horman | 70d4bf6 | 2010-07-20 06:45:56 +0000 | [diff] [blame] | 1826 | if (info.delivery_failure) { |
| 1827 | kfree_skb(info.skb2); |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1828 | return -ENOBUFS; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1829 | } |
| 1830 | consume_skb(info.skb2); |
Pablo Neira Ayuso | ff491a7 | 2009-02-05 23:56:36 -0800 | [diff] [blame] | 1831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | if (info.delivered) { |
| 1833 | if (info.congested && (allocation & __GFP_WAIT)) |
| 1834 | yield(); |
| 1835 | return 0; |
| 1836 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | return -ESRCH; |
| 1838 | } |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1839 | EXPORT_SYMBOL(netlink_broadcast_filtered); |
| 1840 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1841 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 portid, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1842 | u32 group, gfp_t allocation) |
| 1843 | { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1844 | return netlink_broadcast_filtered(ssk, skb, portid, group, allocation, |
Eric W. Biederman | 910a7e9 | 2010-05-04 17:36:46 -0700 | [diff] [blame] | 1845 | NULL, NULL); |
| 1846 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 1847 | EXPORT_SYMBOL(netlink_broadcast); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
| 1849 | struct netlink_set_err_data { |
| 1850 | struct sock *exclude_sk; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1851 | u32 portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | u32 group; |
| 1853 | int code; |
| 1854 | }; |
| 1855 | |
stephen hemminger | b57ef81f | 2011-12-22 08:52:02 +0000 | [diff] [blame] | 1856 | static int do_one_set_err(struct sock *sk, struct netlink_set_err_data *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | { |
| 1858 | struct netlink_sock *nlk = nlk_sk(sk); |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1859 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1860 | |
| 1861 | if (sk == p->exclude_sk) |
| 1862 | goto out; |
| 1863 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 1864 | if (!net_eq(sock_net(sk), sock_net(p->exclude_sk))) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 1865 | goto out; |
| 1866 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1867 | if (nlk->portid == p->portid || p->group - 1 >= nlk->ngroups || |
Patrick McHardy | f7fa9b1 | 2005-08-15 12:29:13 -0700 | [diff] [blame] | 1868 | !test_bit(p->group - 1, nlk->groups)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1869 | goto out; |
| 1870 | |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1871 | if (p->code == ENOBUFS && nlk->flags & NETLINK_RECV_NO_ENOBUFS) { |
| 1872 | ret = 1; |
| 1873 | goto out; |
| 1874 | } |
| 1875 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | sk->sk_err = p->code; |
| 1877 | sk->sk_error_report(sk); |
| 1878 | out: |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1879 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1880 | } |
| 1881 | |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 1882 | /** |
| 1883 | * netlink_set_err - report error to broadcast listeners |
| 1884 | * @ssk: the kernel netlink socket, as returned by netlink_kernel_create() |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1885 | * @portid: the PORTID of a process that we want to skip (if any) |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 1886 | * @groups: the broadcast group that will notice the error |
| 1887 | * @code: error code, must be negative (as usual in kernelspace) |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1888 | * |
| 1889 | * This function returns the number of broadcast listeners that have set the |
| 1890 | * NETLINK_RECV_NO_ENOBUFS socket option. |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 1891 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1892 | int netlink_set_err(struct sock *ssk, u32 portid, u32 group, int code) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | { |
| 1894 | struct netlink_set_err_data info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | struct sock *sk; |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1896 | int ret = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | info.exclude_sk = ssk; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 1899 | info.portid = portid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | info.group = group; |
Pablo Neira Ayuso | 4843b93 | 2009-03-03 23:37:30 -0800 | [diff] [blame] | 1901 | /* sk->sk_err wants a positive error value */ |
| 1902 | info.code = -code; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
| 1904 | read_lock(&nl_table_lock); |
| 1905 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 1906 | sk_for_each_bound(sk, &nl_table[ssk->sk_protocol].mc_list) |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1907 | ret += do_one_set_err(sk, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | |
| 1909 | read_unlock(&nl_table_lock); |
Pablo Neira Ayuso | 1a50307 | 2010-03-18 14:24:42 +0000 | [diff] [blame] | 1910 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | } |
Pablo Neira Ayuso | dd5b6ce | 2009-03-23 13:21:06 +0100 | [diff] [blame] | 1912 | EXPORT_SYMBOL(netlink_set_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1914 | /* must be called with netlink table grabbed */ |
| 1915 | static void netlink_update_socket_mc(struct netlink_sock *nlk, |
| 1916 | unsigned int group, |
| 1917 | int is_new) |
| 1918 | { |
| 1919 | int old, new = !!is_new, subscriptions; |
| 1920 | |
| 1921 | old = test_bit(group - 1, nlk->groups); |
| 1922 | subscriptions = nlk->subscriptions - old + new; |
| 1923 | if (new) |
| 1924 | __set_bit(group - 1, nlk->groups); |
| 1925 | else |
| 1926 | __clear_bit(group - 1, nlk->groups); |
| 1927 | netlink_update_subscriptions(&nlk->sk, subscriptions); |
| 1928 | netlink_update_listeners(&nlk->sk); |
| 1929 | } |
| 1930 | |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1931 | static int netlink_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 1932 | char __user *optval, unsigned int optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1933 | { |
| 1934 | struct sock *sk = sock->sk; |
| 1935 | struct netlink_sock *nlk = nlk_sk(sk); |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 1936 | unsigned int val = 0; |
| 1937 | int err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1938 | |
| 1939 | if (level != SOL_NETLINK) |
| 1940 | return -ENOPROTOOPT; |
| 1941 | |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 1942 | if (optname != NETLINK_RX_RING && optname != NETLINK_TX_RING && |
| 1943 | optlen >= sizeof(int) && |
Johannes Berg | eb49653 | 2007-07-18 02:07:51 -0700 | [diff] [blame] | 1944 | get_user(val, (unsigned int __user *)optval)) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1945 | return -EFAULT; |
| 1946 | |
| 1947 | switch (optname) { |
| 1948 | case NETLINK_PKTINFO: |
| 1949 | if (val) |
| 1950 | nlk->flags |= NETLINK_RECV_PKTINFO; |
| 1951 | else |
| 1952 | nlk->flags &= ~NETLINK_RECV_PKTINFO; |
| 1953 | err = 0; |
| 1954 | break; |
| 1955 | case NETLINK_ADD_MEMBERSHIP: |
| 1956 | case NETLINK_DROP_MEMBERSHIP: { |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 1957 | if (!netlink_capable(sock, NL_CFG_F_NONROOT_RECV)) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1958 | return -EPERM; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 1959 | err = netlink_realloc_groups(sk); |
| 1960 | if (err) |
| 1961 | return err; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1962 | if (!val || val - 1 >= nlk->ngroups) |
| 1963 | return -EINVAL; |
| 1964 | netlink_table_grab(); |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 1965 | netlink_update_socket_mc(nlk, val, |
| 1966 | optname == NETLINK_ADD_MEMBERSHIP); |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1967 | netlink_table_ungrab(); |
Pablo Neira Ayuso | 0329274 | 2012-06-29 06:15:22 +0000 | [diff] [blame] | 1968 | |
| 1969 | if (nlk->netlink_bind) |
| 1970 | nlk->netlink_bind(val); |
| 1971 | |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 1972 | err = 0; |
| 1973 | break; |
| 1974 | } |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 1975 | case NETLINK_BROADCAST_ERROR: |
| 1976 | if (val) |
| 1977 | nlk->flags |= NETLINK_BROADCAST_SEND_ERROR; |
| 1978 | else |
| 1979 | nlk->flags &= ~NETLINK_BROADCAST_SEND_ERROR; |
| 1980 | err = 0; |
| 1981 | break; |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 1982 | case NETLINK_NO_ENOBUFS: |
| 1983 | if (val) { |
| 1984 | nlk->flags |= NETLINK_RECV_NO_ENOBUFS; |
Patrick McHardy | cd967e0 | 2013-04-17 06:46:56 +0000 | [diff] [blame] | 1985 | clear_bit(NETLINK_CONGESTED, &nlk->state); |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 1986 | wake_up_interruptible(&nlk->wait); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1987 | } else { |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 1988 | nlk->flags &= ~NETLINK_RECV_NO_ENOBUFS; |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 1989 | } |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 1990 | err = 0; |
| 1991 | break; |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 1992 | #ifdef CONFIG_NETLINK_MMAP |
| 1993 | case NETLINK_RX_RING: |
| 1994 | case NETLINK_TX_RING: { |
| 1995 | struct nl_mmap_req req; |
| 1996 | |
| 1997 | /* Rings might consume more memory than queue limits, require |
| 1998 | * CAP_NET_ADMIN. |
| 1999 | */ |
| 2000 | if (!capable(CAP_NET_ADMIN)) |
| 2001 | return -EPERM; |
| 2002 | if (optlen < sizeof(req)) |
| 2003 | return -EINVAL; |
| 2004 | if (copy_from_user(&req, optval, sizeof(req))) |
| 2005 | return -EFAULT; |
| 2006 | err = netlink_set_ring(sk, &req, false, |
| 2007 | optname == NETLINK_TX_RING); |
| 2008 | break; |
| 2009 | } |
| 2010 | #endif /* CONFIG_NETLINK_MMAP */ |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2011 | default: |
| 2012 | err = -ENOPROTOOPT; |
| 2013 | } |
| 2014 | return err; |
| 2015 | } |
| 2016 | |
| 2017 | static int netlink_getsockopt(struct socket *sock, int level, int optname, |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2018 | char __user *optval, int __user *optlen) |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2019 | { |
| 2020 | struct sock *sk = sock->sk; |
| 2021 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2022 | int len, val, err; |
| 2023 | |
| 2024 | if (level != SOL_NETLINK) |
| 2025 | return -ENOPROTOOPT; |
| 2026 | |
| 2027 | if (get_user(len, optlen)) |
| 2028 | return -EFAULT; |
| 2029 | if (len < 0) |
| 2030 | return -EINVAL; |
| 2031 | |
| 2032 | switch (optname) { |
| 2033 | case NETLINK_PKTINFO: |
| 2034 | if (len < sizeof(int)) |
| 2035 | return -EINVAL; |
| 2036 | len = sizeof(int); |
| 2037 | val = nlk->flags & NETLINK_RECV_PKTINFO ? 1 : 0; |
Heiko Carstens | a27b58f | 2006-10-30 15:06:12 -0800 | [diff] [blame] | 2038 | if (put_user(len, optlen) || |
| 2039 | put_user(val, optval)) |
| 2040 | return -EFAULT; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2041 | err = 0; |
| 2042 | break; |
Pablo Neira Ayuso | be0c22a | 2009-02-18 01:40:43 +0000 | [diff] [blame] | 2043 | case NETLINK_BROADCAST_ERROR: |
| 2044 | if (len < sizeof(int)) |
| 2045 | return -EINVAL; |
| 2046 | len = sizeof(int); |
| 2047 | val = nlk->flags & NETLINK_BROADCAST_SEND_ERROR ? 1 : 0; |
| 2048 | if (put_user(len, optlen) || |
| 2049 | put_user(val, optval)) |
| 2050 | return -EFAULT; |
| 2051 | err = 0; |
| 2052 | break; |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2053 | case NETLINK_NO_ENOBUFS: |
| 2054 | if (len < sizeof(int)) |
| 2055 | return -EINVAL; |
| 2056 | len = sizeof(int); |
| 2057 | val = nlk->flags & NETLINK_RECV_NO_ENOBUFS ? 1 : 0; |
| 2058 | if (put_user(len, optlen) || |
| 2059 | put_user(val, optval)) |
| 2060 | return -EFAULT; |
| 2061 | err = 0; |
| 2062 | break; |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2063 | default: |
| 2064 | err = -ENOPROTOOPT; |
| 2065 | } |
| 2066 | return err; |
| 2067 | } |
| 2068 | |
| 2069 | static void netlink_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) |
| 2070 | { |
| 2071 | struct nl_pktinfo info; |
| 2072 | |
| 2073 | info.group = NETLINK_CB(skb).dst_group; |
| 2074 | put_cmsg(msg, SOL_NETLINK, NETLINK_PKTINFO, sizeof(info), &info); |
| 2075 | } |
| 2076 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
| 2078 | struct msghdr *msg, size_t len) |
| 2079 | { |
| 2080 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 2081 | struct sock *sk = sock->sk; |
| 2082 | struct netlink_sock *nlk = nlk_sk(sk); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2083 | struct sockaddr_nl *addr = msg->msg_name; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2084 | u32 dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2085 | u32 dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2086 | struct sk_buff *skb; |
| 2087 | int err; |
| 2088 | struct scm_cookie scm; |
| 2089 | |
| 2090 | if (msg->msg_flags&MSG_OOB) |
| 2091 | return -EOPNOTSUPP; |
| 2092 | |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2093 | if (NULL == siocb->scm) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | siocb->scm = &scm; |
Eric Dumazet | 16e5726 | 2011-09-19 05:52:27 +0000 | [diff] [blame] | 2095 | |
Eric Dumazet | e0e3cea | 2012-08-21 06:21:17 +0000 | [diff] [blame] | 2096 | err = scm_send(sock, msg, siocb->scm, true); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | if (err < 0) |
| 2098 | return err; |
| 2099 | |
| 2100 | if (msg->msg_namelen) { |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2101 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2102 | if (addr->nl_family != AF_NETLINK) |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2103 | goto out; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2104 | dst_portid = addr->nl_pid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2105 | dst_group = ffs(addr->nl_groups); |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2106 | err = -EPERM; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2107 | if ((dst_group || dst_portid) && |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 2108 | !netlink_capable(sock, NL_CFG_F_NONROOT_SEND)) |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2109 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } else { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2111 | dst_portid = nlk->dst_portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2112 | dst_group = nlk->dst_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | } |
| 2114 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2115 | if (!nlk->portid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | err = netlink_autobind(sock); |
| 2117 | if (err) |
| 2118 | goto out; |
| 2119 | } |
| 2120 | |
Patrick McHardy | 5fd9612 | 2013-04-17 06:47:03 +0000 | [diff] [blame] | 2121 | if (netlink_tx_is_mmaped(sk) && |
| 2122 | msg->msg_iov->iov_base == NULL) { |
| 2123 | err = netlink_mmap_sendmsg(sk, msg, dst_portid, dst_group, |
| 2124 | siocb); |
| 2125 | goto out; |
| 2126 | } |
| 2127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | err = -EMSGSIZE; |
| 2129 | if (len > sk->sk_sndbuf - 32) |
| 2130 | goto out; |
| 2131 | err = -ENOBUFS; |
Pablo Neira Ayuso | c05cdb1 | 2013-06-03 09:46:28 +0000 | [diff] [blame^] | 2132 | skb = netlink_alloc_large_skb(len); |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2133 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | goto out; |
| 2135 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2136 | NETLINK_CB(skb).portid = nlk->portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2137 | NETLINK_CB(skb).dst_group = dst_group; |
Eric W. Biederman | dbe9a41 | 2012-09-06 18:20:01 +0000 | [diff] [blame] | 2138 | NETLINK_CB(skb).creds = siocb->scm->creds; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | err = -EFAULT; |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2141 | if (memcpy_fromiovec(skb_put(skb, len), msg->msg_iov, len)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | kfree_skb(skb); |
| 2143 | goto out; |
| 2144 | } |
| 2145 | |
| 2146 | err = security_netlink_send(sk, skb); |
| 2147 | if (err) { |
| 2148 | kfree_skb(skb); |
| 2149 | goto out; |
| 2150 | } |
| 2151 | |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2152 | if (dst_group) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | atomic_inc(&skb->users); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2154 | netlink_broadcast(sk, skb, dst_portid, dst_group, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | } |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2156 | err = netlink_unicast(sk, skb, dst_portid, msg->msg_flags&MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | |
| 2158 | out: |
Eric W. Biederman | b47030c | 2010-06-13 03:31:06 +0000 | [diff] [blame] | 2159 | scm_destroy(siocb->scm); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | return err; |
| 2161 | } |
| 2162 | |
| 2163 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, |
| 2164 | struct msghdr *msg, size_t len, |
| 2165 | int flags) |
| 2166 | { |
| 2167 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 2168 | struct scm_cookie scm; |
| 2169 | struct sock *sk = sock->sk; |
| 2170 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2171 | int noblock = flags&MSG_DONTWAIT; |
| 2172 | size_t copied; |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2173 | struct sk_buff *skb, *data_skb; |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2174 | int err, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
| 2176 | if (flags&MSG_OOB) |
| 2177 | return -EOPNOTSUPP; |
| 2178 | |
| 2179 | copied = 0; |
| 2180 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2181 | skb = skb_recv_datagram(sk, flags, noblock, &err); |
| 2182 | if (skb == NULL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2183 | goto out; |
| 2184 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2185 | data_skb = skb; |
| 2186 | |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2187 | #ifdef CONFIG_COMPAT_NETLINK_MESSAGES |
| 2188 | if (unlikely(skb_shinfo(skb)->frag_list)) { |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2189 | /* |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2190 | * If this skb has a frag_list, then here that means that we |
| 2191 | * will have to use the frag_list skb's data for compat tasks |
| 2192 | * and the regular skb's data for normal (non-compat) tasks. |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2193 | * |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2194 | * If we need to send the compat skb, assign it to the |
| 2195 | * 'data_skb' variable so that it will be used below for data |
| 2196 | * copying. We keep 'skb' for everything else, including |
| 2197 | * freeing both later. |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2198 | */ |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2199 | if (flags & MSG_CMSG_COMPAT) |
| 2200 | data_skb = skb_shinfo(skb)->frag_list; |
Johannes Berg | 1dacc76 | 2009-07-01 11:26:02 +0000 | [diff] [blame] | 2201 | } |
| 2202 | #endif |
| 2203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2204 | msg->msg_namelen = 0; |
| 2205 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2206 | copied = data_skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | if (len < copied) { |
| 2208 | msg->msg_flags |= MSG_TRUNC; |
| 2209 | copied = len; |
| 2210 | } |
| 2211 | |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2212 | skb_reset_transport_header(data_skb); |
| 2213 | err = skb_copy_datagram_iovec(data_skb, 0, msg->msg_iov, copied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | |
| 2215 | if (msg->msg_name) { |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2216 | struct sockaddr_nl *addr = (struct sockaddr_nl *)msg->msg_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | addr->nl_family = AF_NETLINK; |
| 2218 | addr->nl_pad = 0; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2219 | addr->nl_pid = NETLINK_CB(skb).portid; |
Patrick McHardy | d629b83 | 2005-08-14 19:27:50 -0700 | [diff] [blame] | 2220 | addr->nl_groups = netlink_group_mask(NETLINK_CB(skb).dst_group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | msg->msg_namelen = sizeof(*addr); |
| 2222 | } |
| 2223 | |
Patrick McHardy | cc9a06c | 2006-03-12 20:34:27 -0800 | [diff] [blame] | 2224 | if (nlk->flags & NETLINK_RECV_PKTINFO) |
| 2225 | netlink_cmsg_recv_pktinfo(msg, skb); |
| 2226 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | if (NULL == siocb->scm) { |
| 2228 | memset(&scm, 0, sizeof(scm)); |
| 2229 | siocb->scm = &scm; |
| 2230 | } |
| 2231 | siocb->scm->creds = *NETLINK_CREDS(skb); |
Patrick McHardy | 188ccb5 | 2007-05-03 03:27:01 -0700 | [diff] [blame] | 2232 | if (flags & MSG_TRUNC) |
Johannes Berg | 68d6ac6 | 2010-08-15 21:20:44 +0000 | [diff] [blame] | 2233 | copied = data_skb->len; |
David S. Miller | daa3766 | 2010-08-15 23:21:50 -0700 | [diff] [blame] | 2234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | skb_free_datagram(sk, skb); |
| 2236 | |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2237 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) { |
| 2238 | ret = netlink_dump(sk); |
| 2239 | if (ret) { |
| 2240 | sk->sk_err = ret; |
| 2241 | sk->sk_error_report(sk); |
| 2242 | } |
| 2243 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | |
| 2245 | scm_recv(sock, msg, siocb->scm, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | out: |
| 2247 | netlink_rcv_wake(sk); |
| 2248 | return err ? : copied; |
| 2249 | } |
| 2250 | |
| 2251 | static void netlink_data_ready(struct sock *sk, int len) |
| 2252 | { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2253 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /* |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2257 | * We export these functions to other modules. They provide a |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | * complete set of kernel non-blocking support for message |
| 2259 | * queueing. |
| 2260 | */ |
| 2261 | |
| 2262 | struct sock * |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 2263 | __netlink_kernel_create(struct net *net, int unit, struct module *module, |
| 2264 | struct netlink_kernel_cfg *cfg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | { |
| 2266 | struct socket *sock; |
| 2267 | struct sock *sk; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2268 | struct netlink_sock *nlk; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2269 | struct listeners *listeners = NULL; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2270 | struct mutex *cb_mutex = cfg ? cfg->cb_mutex : NULL; |
| 2271 | unsigned int groups; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 2273 | BUG_ON(!nl_table); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2275 | if (unit < 0 || unit >= MAX_LINKS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | return NULL; |
| 2277 | |
| 2278 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) |
| 2279 | return NULL; |
| 2280 | |
Pavel Emelyanov | 23fe186 | 2008-01-30 19:31:06 -0800 | [diff] [blame] | 2281 | /* |
| 2282 | * We have to just have a reference on the net from sk, but don't |
| 2283 | * get_net it. Besides, we cannot get and then put the net here. |
| 2284 | * So we create one inside init_net and the move it to net. |
| 2285 | */ |
| 2286 | |
| 2287 | if (__netlink_create(&init_net, sock, cb_mutex, unit) < 0) |
| 2288 | goto out_sock_release_nosk; |
| 2289 | |
| 2290 | sk = sock->sk; |
Denis V. Lunev | edf0208 | 2008-02-29 11:18:32 -0800 | [diff] [blame] | 2291 | sk_change_net(sk, net); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2292 | |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2293 | if (!cfg || cfg->groups < 32) |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2294 | groups = 32; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2295 | else |
| 2296 | groups = cfg->groups; |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2297 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2298 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2299 | if (!listeners) |
| 2300 | goto out_sock_release; |
| 2301 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | sk->sk_data_ready = netlink_data_ready; |
Pablo Neira Ayuso | a31f2d1 | 2012-06-29 06:15:21 +0000 | [diff] [blame] | 2303 | if (cfg && cfg->input) |
| 2304 | nlk_sk(sk)->netlink_rcv = cfg->input; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2306 | if (netlink_insert(sk, net, 0)) |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2307 | goto out_sock_release; |
| 2308 | |
| 2309 | nlk = nlk_sk(sk); |
| 2310 | nlk->flags |= NETLINK_KERNEL_SOCKET; |
| 2311 | |
| 2312 | netlink_table_grab(); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2313 | if (!nl_table[unit].registered) { |
| 2314 | nl_table[unit].groups = groups; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2315 | rcu_assign_pointer(nl_table[unit].listeners, listeners); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2316 | nl_table[unit].cb_mutex = cb_mutex; |
| 2317 | nl_table[unit].module = module; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 2318 | if (cfg) { |
| 2319 | nl_table[unit].bind = cfg->bind; |
| 2320 | nl_table[unit].flags = cfg->flags; |
| 2321 | } |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2322 | nl_table[unit].registered = 1; |
Jesper Juhl | f937f1f46 | 2007-10-15 01:39:12 -0700 | [diff] [blame] | 2323 | } else { |
| 2324 | kfree(listeners); |
Denis V. Lunev | 869e58f | 2008-01-18 23:53:31 -0800 | [diff] [blame] | 2325 | nl_table[unit].registered++; |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2326 | } |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2327 | netlink_table_ungrab(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2328 | return sk; |
| 2329 | |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2330 | out_sock_release: |
Patrick McHardy | 4277a08 | 2006-03-20 18:52:01 -0800 | [diff] [blame] | 2331 | kfree(listeners); |
Denis V. Lunev | 9dfbec1 | 2008-02-29 11:17:56 -0800 | [diff] [blame] | 2332 | netlink_kernel_release(sk); |
Pavel Emelyanov | 23fe186 | 2008-01-30 19:31:06 -0800 | [diff] [blame] | 2333 | return NULL; |
| 2334 | |
| 2335 | out_sock_release_nosk: |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 2336 | sock_release(sock); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame] | 2337 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2338 | } |
Pablo Neira Ayuso | 9f00d97 | 2012-09-08 02:53:54 +0000 | [diff] [blame] | 2339 | EXPORT_SYMBOL(__netlink_kernel_create); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 2340 | |
| 2341 | void |
| 2342 | netlink_kernel_release(struct sock *sk) |
| 2343 | { |
Denis V. Lunev | edf0208 | 2008-02-29 11:18:32 -0800 | [diff] [blame] | 2344 | sk_release_kernel(sk); |
Denis V. Lunev | b7c6ba6 | 2008-01-28 14:41:19 -0800 | [diff] [blame] | 2345 | } |
| 2346 | EXPORT_SYMBOL(netlink_kernel_release); |
| 2347 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2348 | int __netlink_change_ngroups(struct sock *sk, unsigned int groups) |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2349 | { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2350 | struct listeners *new, *old; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2351 | struct netlink_table *tbl = &nl_table[sk->sk_protocol]; |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2352 | |
| 2353 | if (groups < 32) |
| 2354 | groups = 32; |
| 2355 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2356 | if (NLGRPSZ(tbl->groups) < NLGRPSZ(groups)) { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2357 | new = kzalloc(sizeof(*new) + NLGRPSZ(groups), GFP_ATOMIC); |
| 2358 | if (!new) |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2359 | return -ENOMEM; |
Eric Dumazet | 6d772ac | 2012-10-18 03:21:55 +0000 | [diff] [blame] | 2360 | old = nl_deref_protected(tbl->listeners); |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2361 | memcpy(new->masks, old->masks, NLGRPSZ(tbl->groups)); |
| 2362 | rcu_assign_pointer(tbl->listeners, new); |
| 2363 | |
Lai Jiangshan | 37b6b93 | 2011-03-15 18:01:42 +0800 | [diff] [blame] | 2364 | kfree_rcu(old, rcu); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2365 | } |
| 2366 | tbl->groups = groups; |
| 2367 | |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | /** |
| 2372 | * netlink_change_ngroups - change number of multicast groups |
| 2373 | * |
| 2374 | * This changes the number of multicast groups that are available |
| 2375 | * on a certain netlink family. Note that it is not possible to |
| 2376 | * change the number of groups to below 32. Also note that it does |
| 2377 | * not implicitly call netlink_clear_multicast_users() when the |
| 2378 | * number of groups is reduced. |
| 2379 | * |
| 2380 | * @sk: The kernel netlink socket, as returned by netlink_kernel_create(). |
| 2381 | * @groups: The new number of groups. |
| 2382 | */ |
| 2383 | int netlink_change_ngroups(struct sock *sk, unsigned int groups) |
| 2384 | { |
| 2385 | int err; |
| 2386 | |
| 2387 | netlink_table_grab(); |
| 2388 | err = __netlink_change_ngroups(sk, groups); |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2389 | netlink_table_ungrab(); |
Johannes Berg | d136f1b | 2009-09-12 03:03:15 +0000 | [diff] [blame] | 2390 | |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2391 | return err; |
| 2392 | } |
Johannes Berg | b4ff4f0 | 2007-07-18 15:46:06 -0700 | [diff] [blame] | 2393 | |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2394 | void __netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
| 2395 | { |
| 2396 | struct sock *sk; |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2397 | struct netlink_table *tbl = &nl_table[ksk->sk_protocol]; |
| 2398 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2399 | sk_for_each_bound(sk, &tbl->mc_list) |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2400 | netlink_update_socket_mc(nlk_sk(sk), group, 0); |
| 2401 | } |
| 2402 | |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2403 | /** |
| 2404 | * netlink_clear_multicast_users - kick off multicast listeners |
| 2405 | * |
| 2406 | * This function removes all listeners from the given group. |
| 2407 | * @ksk: The kernel netlink socket, as returned by |
| 2408 | * netlink_kernel_create(). |
| 2409 | * @group: The multicast group to clear. |
| 2410 | */ |
| 2411 | void netlink_clear_multicast_users(struct sock *ksk, unsigned int group) |
| 2412 | { |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2413 | netlink_table_grab(); |
Johannes Berg | b827357 | 2009-09-24 15:44:05 -0700 | [diff] [blame] | 2414 | __netlink_clear_multicast_users(ksk, group); |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2415 | netlink_table_ungrab(); |
| 2416 | } |
Johannes Berg | 84659eb | 2007-07-18 15:47:05 -0700 | [diff] [blame] | 2417 | |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2418 | struct nlmsghdr * |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2419 | __nlmsg_put(struct sk_buff *skb, u32 portid, u32 seq, int type, int len, int flags) |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2420 | { |
| 2421 | struct nlmsghdr *nlh; |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 2422 | int size = nlmsg_msg_size(len); |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2423 | |
| 2424 | nlh = (struct nlmsghdr*)skb_put(skb, NLMSG_ALIGN(size)); |
| 2425 | nlh->nlmsg_type = type; |
| 2426 | nlh->nlmsg_len = size; |
| 2427 | nlh->nlmsg_flags = flags; |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2428 | nlh->nlmsg_pid = portid; |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2429 | nlh->nlmsg_seq = seq; |
| 2430 | if (!__builtin_constant_p(size) || NLMSG_ALIGN(size) - size != 0) |
Hong zhi guo | 573ce26 | 2013-03-27 06:47:04 +0000 | [diff] [blame] | 2431 | memset(nlmsg_data(nlh) + len, 0, NLMSG_ALIGN(size) - size); |
Denys Vlasenko | a46621a | 2012-01-30 15:22:06 -0500 | [diff] [blame] | 2432 | return nlh; |
| 2433 | } |
| 2434 | EXPORT_SYMBOL(__nlmsg_put); |
| 2435 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | /* |
| 2437 | * It looks a bit ugly. |
| 2438 | * It would be better to create kernel thread. |
| 2439 | */ |
| 2440 | |
| 2441 | static int netlink_dump(struct sock *sk) |
| 2442 | { |
| 2443 | struct netlink_sock *nlk = nlk_sk(sk); |
| 2444 | struct netlink_callback *cb; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2445 | struct sk_buff *skb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2446 | struct nlmsghdr *nlh; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2447 | int len, err = -ENOBUFS; |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2448 | int alloc_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2450 | mutex_lock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | |
| 2452 | cb = nlk->cb; |
| 2453 | if (cb == NULL) { |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2454 | err = -EINVAL; |
| 2455 | goto errout_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2458 | alloc_size = max_t(int, cb->min_dump_alloc, NLMSG_GOODSIZE); |
| 2459 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2460 | if (!netlink_rx_is_mmaped(sk) && |
| 2461 | atomic_read(&sk->sk_rmem_alloc) >= sk->sk_rcvbuf) |
| 2462 | goto errout_skb; |
| 2463 | skb = netlink_alloc_skb(sk, alloc_size, nlk->portid, GFP_KERNEL); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2464 | if (!skb) |
Dan Carpenter | c63d6ea | 2011-06-15 03:11:42 +0000 | [diff] [blame] | 2465 | goto errout_skb; |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2466 | netlink_skb_set_owner_r(skb, sk); |
Greg Rose | c7ac867 | 2011-06-10 01:27:09 +0000 | [diff] [blame] | 2467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | len = cb->dump(skb, cb); |
| 2469 | |
| 2470 | if (len > 0) { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2471 | mutex_unlock(nlk->cb_mutex); |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 2472 | |
| 2473 | if (sk_filter(sk, skb)) |
| 2474 | kfree_skb(skb); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 2475 | else |
| 2476 | __netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | return 0; |
| 2478 | } |
| 2479 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2480 | nlh = nlmsg_put_answer(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
| 2481 | if (!nlh) |
| 2482 | goto errout_skb; |
| 2483 | |
Johannes Berg | 670dc28 | 2011-06-20 13:40:46 +0200 | [diff] [blame] | 2484 | nl_dump_check_consistent(cb, nlh); |
| 2485 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2486 | memcpy(nlmsg_data(nlh), &len, sizeof(len)); |
| 2487 | |
Stephen Hemminger | b1153f2 | 2008-03-21 15:46:12 -0700 | [diff] [blame] | 2488 | if (sk_filter(sk, skb)) |
| 2489 | kfree_skb(skb); |
Eric Dumazet | 4a7e7c2 | 2012-04-05 22:17:46 +0000 | [diff] [blame] | 2490 | else |
| 2491 | __netlink_sendskb(sk, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | |
Thomas Graf | a8f74b2 | 2005-11-10 02:25:52 +0100 | [diff] [blame] | 2493 | if (cb->done) |
| 2494 | cb->done(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | nlk->cb = NULL; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2496 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2498 | module_put(cb->module); |
Eric Dumazet | bfb253c | 2012-04-22 21:30:29 +0000 | [diff] [blame] | 2499 | netlink_consume_callback(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2500 | return 0; |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 2501 | |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2502 | errout_skb: |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2503 | mutex_unlock(nlk->cb_mutex); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2504 | kfree_skb(skb); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2505 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | } |
| 2507 | |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2508 | int __netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
| 2509 | const struct nlmsghdr *nlh, |
| 2510 | struct netlink_dump_control *control) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | { |
| 2512 | struct netlink_callback *cb; |
| 2513 | struct sock *sk; |
| 2514 | struct netlink_sock *nlk; |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2515 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 2517 | cb = kzalloc(sizeof(*cb), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | if (cb == NULL) |
| 2519 | return -ENOBUFS; |
| 2520 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2521 | /* Memory mapped dump requests need to be copied to avoid looping |
| 2522 | * on the pending state in netlink_mmap_sendmsg() while the CB hold |
| 2523 | * a reference to the skb. |
| 2524 | */ |
| 2525 | if (netlink_skb_is_mmaped(skb)) { |
| 2526 | skb = skb_copy(skb, GFP_KERNEL); |
| 2527 | if (skb == NULL) { |
| 2528 | kfree(cb); |
| 2529 | return -ENOBUFS; |
| 2530 | } |
| 2531 | } else |
| 2532 | atomic_inc(&skb->users); |
| 2533 | |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 2534 | cb->dump = control->dump; |
| 2535 | cb->done = control->done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2536 | cb->nlh = nlh; |
Pablo Neira Ayuso | 7175c88 | 2012-02-24 14:30:16 +0000 | [diff] [blame] | 2537 | cb->data = control->data; |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2538 | cb->module = control->module; |
Pablo Neira Ayuso | 80d326f | 2012-02-24 14:30:15 +0000 | [diff] [blame] | 2539 | cb->min_dump_alloc = control->min_dump_alloc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2540 | cb->skb = skb; |
| 2541 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2542 | sk = netlink_lookup(sock_net(ssk), ssk->sk_protocol, NETLINK_CB(skb).portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2543 | if (sk == NULL) { |
| 2544 | netlink_destroy_callback(cb); |
| 2545 | return -ECONNREFUSED; |
| 2546 | } |
| 2547 | nlk = nlk_sk(sk); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2548 | |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2549 | mutex_lock(nlk->cb_mutex); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2550 | /* A dump is in progress... */ |
Herbert Xu | 3f660d6 | 2007-05-03 03:17:14 -0700 | [diff] [blame] | 2551 | if (nlk->cb) { |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2552 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | netlink_destroy_callback(cb); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2554 | ret = -EBUSY; |
| 2555 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | } |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2557 | /* add reference of module which cb->dump belongs to */ |
| 2558 | if (!try_module_get(cb->module)) { |
| 2559 | mutex_unlock(nlk->cb_mutex); |
| 2560 | netlink_destroy_callback(cb); |
| 2561 | ret = -EPROTONOSUPPORT; |
| 2562 | goto out; |
| 2563 | } |
| 2564 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | nlk->cb = cb; |
Patrick McHardy | af65bdf | 2007-04-20 14:14:21 -0700 | [diff] [blame] | 2566 | mutex_unlock(nlk->cb_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2568 | ret = netlink_dump(sk); |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2569 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2570 | sock_put(sk); |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2571 | |
Andrey Vagin | b44d211 | 2011-02-21 02:40:47 +0000 | [diff] [blame] | 2572 | if (ret) |
| 2573 | return ret; |
| 2574 | |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2575 | /* We successfully started a dump, by returning -EINTR we |
| 2576 | * signal not to send ACK even if it was requested. |
| 2577 | */ |
| 2578 | return -EINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | } |
Gao feng | 6dc878a | 2012-10-04 20:15:48 +0000 | [diff] [blame] | 2580 | EXPORT_SYMBOL(__netlink_dump_start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | |
| 2582 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) |
| 2583 | { |
| 2584 | struct sk_buff *skb; |
| 2585 | struct nlmsghdr *rep; |
| 2586 | struct nlmsgerr *errmsg; |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2587 | size_t payload = sizeof(*errmsg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | |
Thomas Graf | 339bf98 | 2006-11-10 14:10:15 -0800 | [diff] [blame] | 2589 | /* error messages get the original request appened */ |
| 2590 | if (err) |
| 2591 | payload += nlmsg_len(nlh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | |
Patrick McHardy | f9c2288 | 2013-04-17 06:47:04 +0000 | [diff] [blame] | 2593 | skb = netlink_alloc_skb(in_skb->sk, nlmsg_total_size(payload), |
| 2594 | NETLINK_CB(in_skb).portid, GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | if (!skb) { |
| 2596 | struct sock *sk; |
| 2597 | |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 2598 | sk = netlink_lookup(sock_net(in_skb->sk), |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2599 | in_skb->sk->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2600 | NETLINK_CB(in_skb).portid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2601 | if (sk) { |
| 2602 | sk->sk_err = ENOBUFS; |
| 2603 | sk->sk_error_report(sk); |
| 2604 | sock_put(sk); |
| 2605 | } |
| 2606 | return; |
| 2607 | } |
| 2608 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2609 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).portid, nlh->nlmsg_seq, |
John Fastabend | 5dba93a | 2009-09-25 13:11:44 +0000 | [diff] [blame] | 2610 | NLMSG_ERROR, payload, 0); |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2611 | errmsg = nlmsg_data(rep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2612 | errmsg->error = err; |
Thomas Graf | bf8b79e | 2006-08-04 23:03:29 -0700 | [diff] [blame] | 2613 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(*nlh)); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2614 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).portid, MSG_DONTWAIT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2616 | EXPORT_SYMBOL(netlink_ack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2618 | int netlink_rcv_skb(struct sk_buff *skb, int (*cb)(struct sk_buff *, |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2619 | struct nlmsghdr *)) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2620 | { |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2621 | struct nlmsghdr *nlh; |
| 2622 | int err; |
| 2623 | |
| 2624 | while (skb->len >= nlmsg_total_size(0)) { |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2625 | int msglen; |
| 2626 | |
Arnaldo Carvalho de Melo | b529ccf | 2007-04-25 19:08:35 -0700 | [diff] [blame] | 2627 | nlh = nlmsg_hdr(skb); |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2628 | err = 0; |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2629 | |
Martin Murray | ad8e4b7 | 2006-01-10 13:02:29 -0800 | [diff] [blame] | 2630 | if (nlh->nlmsg_len < NLMSG_HDRLEN || skb->len < nlh->nlmsg_len) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2631 | return 0; |
| 2632 | |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2633 | /* Only requests are handled by the kernel */ |
| 2634 | if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2635 | goto ack; |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2636 | |
Thomas Graf | 45e7ae7 | 2007-03-22 23:29:10 -0700 | [diff] [blame] | 2637 | /* Skip control messages */ |
| 2638 | if (nlh->nlmsg_type < NLMSG_MIN_TYPE) |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2639 | goto ack; |
Thomas Graf | 45e7ae7 | 2007-03-22 23:29:10 -0700 | [diff] [blame] | 2640 | |
Thomas Graf | 1d00a4e | 2007-03-22 23:30:12 -0700 | [diff] [blame] | 2641 | err = cb(skb, nlh); |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2642 | if (err == -EINTR) |
| 2643 | goto skip; |
| 2644 | |
| 2645 | ack: |
Thomas Graf | d35b685 | 2007-03-22 23:28:46 -0700 | [diff] [blame] | 2646 | if (nlh->nlmsg_flags & NLM_F_ACK || err) |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2647 | netlink_ack(skb, nlh, err); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2648 | |
Denis V. Lunev | 5c58298 | 2007-10-23 20:29:25 -0700 | [diff] [blame] | 2649 | skip: |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2650 | msglen = NLMSG_ALIGN(nlh->nlmsg_len); |
Denis V. Lunev | cd40b7d | 2007-10-10 21:15:29 -0700 | [diff] [blame] | 2651 | if (msglen > skb->len) |
| 2652 | msglen = skb->len; |
| 2653 | skb_pull(skb, msglen); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | return 0; |
| 2657 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2658 | EXPORT_SYMBOL(netlink_rcv_skb); |
Thomas Graf | 82ace47 | 2005-11-10 02:25:53 +0100 | [diff] [blame] | 2659 | |
| 2660 | /** |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2661 | * nlmsg_notify - send a notification netlink message |
| 2662 | * @sk: netlink socket to use |
| 2663 | * @skb: notification message |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2664 | * @portid: destination netlink portid for reports or 0 |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2665 | * @group: destination multicast group or 0 |
| 2666 | * @report: 1 to report back, 0 to disable |
| 2667 | * @flags: allocation flags |
| 2668 | */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2669 | int nlmsg_notify(struct sock *sk, struct sk_buff *skb, u32 portid, |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2670 | unsigned int group, int report, gfp_t flags) |
| 2671 | { |
| 2672 | int err = 0; |
| 2673 | |
| 2674 | if (group) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2675 | int exclude_portid = 0; |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2676 | |
| 2677 | if (report) { |
| 2678 | atomic_inc(&skb->users); |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2679 | exclude_portid = portid; |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2680 | } |
| 2681 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2682 | /* errors reported via destination sk->sk_err, but propagate |
| 2683 | * delivery errors if NETLINK_BROADCAST_ERROR flag is set */ |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2684 | err = nlmsg_multicast(sk, skb, exclude_portid, group, flags); |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2685 | } |
| 2686 | |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2687 | if (report) { |
| 2688 | int err2; |
| 2689 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2690 | err2 = nlmsg_unicast(sk, skb, portid); |
Pablo Neira Ayuso | 1ce85fe | 2009-02-24 23:18:28 -0800 | [diff] [blame] | 2691 | if (!err || err == -ESRCH) |
| 2692 | err = err2; |
| 2693 | } |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2694 | |
| 2695 | return err; |
| 2696 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2697 | EXPORT_SYMBOL(nlmsg_notify); |
Thomas Graf | d387f6a | 2006-08-15 00:31:06 -0700 | [diff] [blame] | 2698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | #ifdef CONFIG_PROC_FS |
| 2700 | struct nl_seq_iter { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2701 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | int link; |
| 2703 | int hash_idx; |
| 2704 | }; |
| 2705 | |
| 2706 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) |
| 2707 | { |
| 2708 | struct nl_seq_iter *iter = seq->private; |
| 2709 | int i, j; |
| 2710 | struct sock *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | loff_t off = 0; |
| 2712 | |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2713 | for (i = 0; i < MAX_LINKS; i++) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2714 | struct nl_portid_hash *hash = &nl_table[i].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2715 | |
| 2716 | for (j = 0; j <= hash->mask; j++) { |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 2717 | sk_for_each(s, &hash->table[j]) { |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2718 | if (sock_net(s) != seq_file_net(seq)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2719 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2720 | if (off == pos) { |
| 2721 | iter->link = i; |
| 2722 | iter->hash_idx = j; |
| 2723 | return s; |
| 2724 | } |
| 2725 | ++off; |
| 2726 | } |
| 2727 | } |
| 2728 | } |
| 2729 | return NULL; |
| 2730 | } |
| 2731 | |
| 2732 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 2733 | __acquires(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | { |
| 2735 | read_lock(&nl_table_lock); |
| 2736 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2737 | } |
| 2738 | |
| 2739 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2740 | { |
| 2741 | struct sock *s; |
| 2742 | struct nl_seq_iter *iter; |
| 2743 | int i, j; |
| 2744 | |
| 2745 | ++*pos; |
| 2746 | |
| 2747 | if (v == SEQ_START_TOKEN) |
| 2748 | return netlink_seq_socket_idx(seq, 0); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2749 | |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2750 | iter = seq->private; |
| 2751 | s = v; |
| 2752 | do { |
| 2753 | s = sk_next(s); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2754 | } while (s && sock_net(s) != seq_file_net(seq)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2755 | if (s) |
| 2756 | return s; |
| 2757 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | i = iter->link; |
| 2759 | j = iter->hash_idx + 1; |
| 2760 | |
| 2761 | do { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2762 | struct nl_portid_hash *hash = &nl_table[i].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | |
| 2764 | for (; j <= hash->mask; j++) { |
| 2765 | s = sk_head(&hash->table[j]); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2766 | while (s && sock_net(s) != seq_file_net(seq)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2767 | s = sk_next(s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | if (s) { |
| 2769 | iter->link = i; |
| 2770 | iter->hash_idx = j; |
| 2771 | return s; |
| 2772 | } |
| 2773 | } |
| 2774 | |
| 2775 | j = 0; |
| 2776 | } while (++i < MAX_LINKS); |
| 2777 | |
| 2778 | return NULL; |
| 2779 | } |
| 2780 | |
| 2781 | static void netlink_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | 9a429c4 | 2008-01-01 21:58:02 -0800 | [diff] [blame] | 2782 | __releases(nl_table_lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | { |
| 2784 | read_unlock(&nl_table_lock); |
| 2785 | } |
| 2786 | |
| 2787 | |
| 2788 | static int netlink_seq_show(struct seq_file *seq, void *v) |
| 2789 | { |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2790 | if (v == SEQ_START_TOKEN) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | seq_puts(seq, |
| 2792 | "sk Eth Pid Groups " |
Masatake YAMATO | cf0aa4e | 2010-02-27 19:45:37 +0000 | [diff] [blame] | 2793 | "Rmem Wmem Dump Locks Drops Inode\n"); |
Eric Dumazet | 658cb35 | 2012-04-22 21:30:21 +0000 | [diff] [blame] | 2794 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | struct sock *s = v; |
| 2796 | struct netlink_sock *nlk = nlk_sk(s); |
| 2797 | |
Hannes Frederic Sowa | 9f1e0ad | 2012-12-15 15:09:19 +0000 | [diff] [blame] | 2798 | seq_printf(seq, "%pK %-3d %-6u %08x %-8d %-8d %pK %-8d %-8d %-8lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2799 | s, |
| 2800 | s->sk_protocol, |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2801 | nlk->portid, |
Patrick McHardy | 513c250 | 2005-09-06 15:43:59 -0700 | [diff] [blame] | 2802 | nlk->groups ? (u32)nlk->groups[0] : 0, |
Eric Dumazet | 31e6d36 | 2009-06-17 19:05:41 -0700 | [diff] [blame] | 2803 | sk_rmem_alloc_get(s), |
| 2804 | sk_wmem_alloc_get(s), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | nlk->cb, |
Pablo Neira Ayuso | 38938bf | 2009-03-24 16:37:55 -0700 | [diff] [blame] | 2806 | atomic_read(&s->sk_refcnt), |
Masatake YAMATO | cf0aa4e | 2010-02-27 19:45:37 +0000 | [diff] [blame] | 2807 | atomic_read(&s->sk_drops), |
| 2808 | sock_i_ino(s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | ); |
| 2810 | |
| 2811 | } |
| 2812 | return 0; |
| 2813 | } |
| 2814 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2815 | static const struct seq_operations netlink_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | .start = netlink_seq_start, |
| 2817 | .next = netlink_seq_next, |
| 2818 | .stop = netlink_seq_stop, |
| 2819 | .show = netlink_seq_show, |
| 2820 | }; |
| 2821 | |
| 2822 | |
| 2823 | static int netlink_seq_open(struct inode *inode, struct file *file) |
| 2824 | { |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2825 | return seq_open_net(inode, file, &netlink_seq_ops, |
| 2826 | sizeof(struct nl_seq_iter)); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2827 | } |
| 2828 | |
Arjan van de Ven | da7071d | 2007-02-12 00:55:36 -0800 | [diff] [blame] | 2829 | static const struct file_operations netlink_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | .owner = THIS_MODULE, |
| 2831 | .open = netlink_seq_open, |
| 2832 | .read = seq_read, |
| 2833 | .llseek = seq_lseek, |
Denis V. Lunev | e372c41 | 2007-11-19 22:31:54 -0800 | [diff] [blame] | 2834 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | }; |
| 2836 | |
| 2837 | #endif |
| 2838 | |
| 2839 | int netlink_register_notifier(struct notifier_block *nb) |
| 2840 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2841 | return atomic_notifier_chain_register(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2843 | EXPORT_SYMBOL(netlink_register_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | |
| 2845 | int netlink_unregister_notifier(struct notifier_block *nb) |
| 2846 | { |
Alan Stern | e041c68 | 2006-03-27 01:16:30 -0800 | [diff] [blame] | 2847 | return atomic_notifier_chain_unregister(&netlink_chain, nb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | } |
Patrick McHardy | 6ac552f | 2007-12-04 00:19:38 -0800 | [diff] [blame] | 2849 | EXPORT_SYMBOL(netlink_unregister_notifier); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2850 | |
Eric Dumazet | 90ddc4f | 2005-12-22 12:49:22 -0800 | [diff] [blame] | 2851 | static const struct proto_ops netlink_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | .family = PF_NETLINK, |
| 2853 | .owner = THIS_MODULE, |
| 2854 | .release = netlink_release, |
| 2855 | .bind = netlink_bind, |
| 2856 | .connect = netlink_connect, |
| 2857 | .socketpair = sock_no_socketpair, |
| 2858 | .accept = sock_no_accept, |
| 2859 | .getname = netlink_getname, |
Patrick McHardy | 9652e93 | 2013-04-17 06:47:02 +0000 | [diff] [blame] | 2860 | .poll = netlink_poll, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | .ioctl = sock_no_ioctl, |
| 2862 | .listen = sock_no_listen, |
| 2863 | .shutdown = sock_no_shutdown, |
Patrick McHardy | 9a4595b | 2005-08-15 12:32:15 -0700 | [diff] [blame] | 2864 | .setsockopt = netlink_setsockopt, |
| 2865 | .getsockopt = netlink_getsockopt, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | .sendmsg = netlink_sendmsg, |
| 2867 | .recvmsg = netlink_recvmsg, |
Patrick McHardy | ccdfcc3 | 2013-04-17 06:47:01 +0000 | [diff] [blame] | 2868 | .mmap = netlink_mmap, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | .sendpage = sock_no_sendpage, |
| 2870 | }; |
| 2871 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 2872 | static const struct net_proto_family netlink_family_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | .family = PF_NETLINK, |
| 2874 | .create = netlink_create, |
| 2875 | .owner = THIS_MODULE, /* for consistency 8) */ |
| 2876 | }; |
| 2877 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 2878 | static int __net_init netlink_net_init(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2879 | { |
| 2880 | #ifdef CONFIG_PROC_FS |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2881 | if (!proc_create("netlink", 0, net->proc_net, &netlink_seq_fops)) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2882 | return -ENOMEM; |
| 2883 | #endif |
| 2884 | return 0; |
| 2885 | } |
| 2886 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 2887 | static void __net_exit netlink_net_exit(struct net *net) |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2888 | { |
| 2889 | #ifdef CONFIG_PROC_FS |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2890 | remove_proc_entry("netlink", net->proc_net); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2891 | #endif |
| 2892 | } |
| 2893 | |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2894 | static void __init netlink_add_usersock_entry(void) |
| 2895 | { |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2896 | struct listeners *listeners; |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2897 | int groups = 32; |
| 2898 | |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2899 | listeners = kzalloc(sizeof(*listeners) + NLGRPSZ(groups), GFP_KERNEL); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2900 | if (!listeners) |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2901 | panic("netlink_add_usersock_entry: Cannot allocate listeners\n"); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2902 | |
| 2903 | netlink_table_grab(); |
| 2904 | |
| 2905 | nl_table[NETLINK_USERSOCK].groups = groups; |
Eric Dumazet | 5c398dc | 2010-10-24 04:27:10 +0000 | [diff] [blame] | 2906 | rcu_assign_pointer(nl_table[NETLINK_USERSOCK].listeners, listeners); |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2907 | nl_table[NETLINK_USERSOCK].module = THIS_MODULE; |
| 2908 | nl_table[NETLINK_USERSOCK].registered = 1; |
Pablo Neira Ayuso | 9785e10 | 2012-09-08 02:53:53 +0000 | [diff] [blame] | 2909 | nl_table[NETLINK_USERSOCK].flags = NL_CFG_F_NONROOT_SEND; |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2910 | |
| 2911 | netlink_table_ungrab(); |
| 2912 | } |
| 2913 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 2914 | static struct pernet_operations __net_initdata netlink_net_ops = { |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2915 | .init = netlink_net_init, |
| 2916 | .exit = netlink_net_exit, |
| 2917 | }; |
| 2918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2919 | static int __init netlink_proto_init(void) |
| 2920 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | int i; |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 2922 | unsigned long limit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | unsigned int order; |
| 2924 | int err = proto_register(&netlink_proto, 0); |
| 2925 | |
| 2926 | if (err != 0) |
| 2927 | goto out; |
| 2928 | |
YOSHIFUJI Hideaki / 吉藤英明 | fab2574 | 2013-01-09 07:19:48 +0000 | [diff] [blame] | 2929 | BUILD_BUG_ON(sizeof(struct netlink_skb_parms) > FIELD_SIZEOF(struct sk_buff, cb)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2930 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 2931 | nl_table = kcalloc(MAX_LINKS, sizeof(*nl_table), GFP_KERNEL); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 2932 | if (!nl_table) |
| 2933 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 2935 | if (totalram_pages >= (128 * 1024)) |
| 2936 | limit = totalram_pages >> (21 - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | else |
Jan Beulich | 4481374 | 2009-09-21 17:03:05 -0700 | [diff] [blame] | 2938 | limit = totalram_pages >> (23 - PAGE_SHIFT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2939 | |
Denis Cheng | 26ff5dd | 2007-09-16 16:36:02 -0700 | [diff] [blame] | 2940 | order = get_bitmask_order(limit) - 1 + PAGE_SHIFT; |
| 2941 | limit = (1UL << order) / sizeof(struct hlist_head); |
| 2942 | order = get_bitmask_order(min(limit, (unsigned long)UINT_MAX)) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | |
| 2944 | for (i = 0; i < MAX_LINKS; i++) { |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2945 | struct nl_portid_hash *hash = &nl_table[i].hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2947 | hash->table = nl_portid_hash_zalloc(1 * sizeof(*hash->table)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | if (!hash->table) { |
| 2949 | while (i-- > 0) |
Eric W. Biederman | 15e4730 | 2012-09-07 20:12:54 +0000 | [diff] [blame] | 2950 | nl_portid_hash_free(nl_table[i].hash.table, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | 1 * sizeof(*hash->table)); |
| 2952 | kfree(nl_table); |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 2953 | goto panic; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2954 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | hash->max_shift = order; |
| 2956 | hash->shift = 0; |
| 2957 | hash->mask = 0; |
| 2958 | hash->rehash_time = jiffies; |
| 2959 | } |
| 2960 | |
David S. Miller | b963ea8 | 2010-08-30 19:08:01 -0700 | [diff] [blame] | 2961 | netlink_add_usersock_entry(); |
| 2962 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2963 | sock_register(&netlink_family_ops); |
Eric W. Biederman | b4b5102 | 2007-09-12 13:05:38 +0200 | [diff] [blame] | 2964 | register_pernet_subsys(&netlink_net_ops); |
YOSHIFUJI Hideaki | 746fac4 | 2007-02-09 23:25:07 +0900 | [diff] [blame] | 2965 | /* The netlink device handler may be needed early. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | rtnetlink_init(); |
| 2967 | out: |
| 2968 | return err; |
Akinobu Mita | fab2caf | 2006-08-29 02:15:24 -0700 | [diff] [blame] | 2969 | panic: |
| 2970 | panic("netlink_init: Cannot allocate nl_table\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | } |
| 2972 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2973 | core_initcall(netlink_proto_init); |