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