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