Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NETLINK Kernel-user communication protocol. |
| 3 | * |
| 4 | * Authors: Alan Cox <alan@redhat.com> |
| 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. |
| 11 | * |
| 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 | |
| 24 | #include <linux/config.h> |
| 25 | #include <linux/module.h> |
| 26 | |
| 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> |
| 48 | #include <linux/smp_lock.h> |
| 49 | #include <linux/notifier.h> |
| 50 | #include <linux/security.h> |
| 51 | #include <linux/jhash.h> |
| 52 | #include <linux/jiffies.h> |
| 53 | #include <linux/random.h> |
| 54 | #include <linux/bitops.h> |
| 55 | #include <linux/mm.h> |
| 56 | #include <linux/types.h> |
Andrew Morton | 54e0f52 | 2005-04-30 07:07:04 +0100 | [diff] [blame] | 57 | #include <linux/audit.h> |
| 58 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | #include <net/sock.h> |
| 60 | #include <net/scm.h> |
| 61 | |
| 62 | #define Nprintk(a...) |
| 63 | |
| 64 | struct netlink_sock { |
| 65 | /* struct sock has to be the first member of netlink_sock */ |
| 66 | struct sock sk; |
| 67 | u32 pid; |
| 68 | unsigned int groups; |
| 69 | u32 dst_pid; |
| 70 | unsigned int dst_groups; |
| 71 | unsigned long state; |
| 72 | wait_queue_head_t wait; |
| 73 | struct netlink_callback *cb; |
| 74 | spinlock_t cb_lock; |
| 75 | void (*data_ready)(struct sock *sk, int bytes); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 76 | struct module *module; |
| 77 | u32 flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | }; |
| 79 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 80 | #define NETLINK_KERNEL_SOCKET 0x1 |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | static inline struct netlink_sock *nlk_sk(struct sock *sk) |
| 83 | { |
| 84 | return (struct netlink_sock *)sk; |
| 85 | } |
| 86 | |
| 87 | struct nl_pid_hash { |
| 88 | struct hlist_head *table; |
| 89 | unsigned long rehash_time; |
| 90 | |
| 91 | unsigned int mask; |
| 92 | unsigned int shift; |
| 93 | |
| 94 | unsigned int entries; |
| 95 | unsigned int max_shift; |
| 96 | |
| 97 | u32 rnd; |
| 98 | }; |
| 99 | |
| 100 | struct netlink_table { |
| 101 | struct nl_pid_hash hash; |
| 102 | struct hlist_head mc_list; |
| 103 | unsigned int nl_nonroot; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 104 | struct module *module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | static struct netlink_table *nl_table; |
| 108 | |
| 109 | static DECLARE_WAIT_QUEUE_HEAD(nl_table_wait); |
| 110 | |
| 111 | static int netlink_dump(struct sock *sk); |
| 112 | static void netlink_destroy_callback(struct netlink_callback *cb); |
| 113 | |
| 114 | static DEFINE_RWLOCK(nl_table_lock); |
| 115 | static atomic_t nl_table_users = ATOMIC_INIT(0); |
| 116 | |
| 117 | static struct notifier_block *netlink_chain; |
| 118 | |
| 119 | static struct hlist_head *nl_pid_hashfn(struct nl_pid_hash *hash, u32 pid) |
| 120 | { |
| 121 | return &hash->table[jhash_1word(pid, hash->rnd) & hash->mask]; |
| 122 | } |
| 123 | |
| 124 | static void netlink_sock_destruct(struct sock *sk) |
| 125 | { |
| 126 | skb_queue_purge(&sk->sk_receive_queue); |
| 127 | |
| 128 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 129 | printk("Freeing alive netlink socket %p\n", sk); |
| 130 | return; |
| 131 | } |
| 132 | BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc)); |
| 133 | BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc)); |
| 134 | BUG_TRAP(!nlk_sk(sk)->cb); |
| 135 | } |
| 136 | |
| 137 | /* This lock without WQ_FLAG_EXCLUSIVE is good on UP and it is _very_ bad on SMP. |
| 138 | * Look, when several writers sleep and reader wakes them up, all but one |
| 139 | * immediately hit write lock and grab all the cpus. Exclusive sleep solves |
| 140 | * this, _but_ remember, it adds useless work on UP machines. |
| 141 | */ |
| 142 | |
| 143 | static void netlink_table_grab(void) |
| 144 | { |
| 145 | write_lock_bh(&nl_table_lock); |
| 146 | |
| 147 | if (atomic_read(&nl_table_users)) { |
| 148 | DECLARE_WAITQUEUE(wait, current); |
| 149 | |
| 150 | add_wait_queue_exclusive(&nl_table_wait, &wait); |
| 151 | for(;;) { |
| 152 | set_current_state(TASK_UNINTERRUPTIBLE); |
| 153 | if (atomic_read(&nl_table_users) == 0) |
| 154 | break; |
| 155 | write_unlock_bh(&nl_table_lock); |
| 156 | schedule(); |
| 157 | write_lock_bh(&nl_table_lock); |
| 158 | } |
| 159 | |
| 160 | __set_current_state(TASK_RUNNING); |
| 161 | remove_wait_queue(&nl_table_wait, &wait); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | static __inline__ void netlink_table_ungrab(void) |
| 166 | { |
| 167 | write_unlock_bh(&nl_table_lock); |
| 168 | wake_up(&nl_table_wait); |
| 169 | } |
| 170 | |
| 171 | static __inline__ void |
| 172 | netlink_lock_table(void) |
| 173 | { |
| 174 | /* read_lock() synchronizes us to netlink_table_grab */ |
| 175 | |
| 176 | read_lock(&nl_table_lock); |
| 177 | atomic_inc(&nl_table_users); |
| 178 | read_unlock(&nl_table_lock); |
| 179 | } |
| 180 | |
| 181 | static __inline__ void |
| 182 | netlink_unlock_table(void) |
| 183 | { |
| 184 | if (atomic_dec_and_test(&nl_table_users)) |
| 185 | wake_up(&nl_table_wait); |
| 186 | } |
| 187 | |
| 188 | static __inline__ struct sock *netlink_lookup(int protocol, u32 pid) |
| 189 | { |
| 190 | struct nl_pid_hash *hash = &nl_table[protocol].hash; |
| 191 | struct hlist_head *head; |
| 192 | struct sock *sk; |
| 193 | struct hlist_node *node; |
| 194 | |
| 195 | read_lock(&nl_table_lock); |
| 196 | head = nl_pid_hashfn(hash, pid); |
| 197 | sk_for_each(sk, node, head) { |
| 198 | if (nlk_sk(sk)->pid == pid) { |
| 199 | sock_hold(sk); |
| 200 | goto found; |
| 201 | } |
| 202 | } |
| 203 | sk = NULL; |
| 204 | found: |
| 205 | read_unlock(&nl_table_lock); |
| 206 | return sk; |
| 207 | } |
| 208 | |
| 209 | static inline struct hlist_head *nl_pid_hash_alloc(size_t size) |
| 210 | { |
| 211 | if (size <= PAGE_SIZE) |
| 212 | return kmalloc(size, GFP_ATOMIC); |
| 213 | else |
| 214 | return (struct hlist_head *) |
| 215 | __get_free_pages(GFP_ATOMIC, get_order(size)); |
| 216 | } |
| 217 | |
| 218 | static inline void nl_pid_hash_free(struct hlist_head *table, size_t size) |
| 219 | { |
| 220 | if (size <= PAGE_SIZE) |
| 221 | kfree(table); |
| 222 | else |
| 223 | free_pages((unsigned long)table, get_order(size)); |
| 224 | } |
| 225 | |
| 226 | static int nl_pid_hash_rehash(struct nl_pid_hash *hash, int grow) |
| 227 | { |
| 228 | unsigned int omask, mask, shift; |
| 229 | size_t osize, size; |
| 230 | struct hlist_head *otable, *table; |
| 231 | int i; |
| 232 | |
| 233 | omask = mask = hash->mask; |
| 234 | osize = size = (mask + 1) * sizeof(*table); |
| 235 | shift = hash->shift; |
| 236 | |
| 237 | if (grow) { |
| 238 | if (++shift > hash->max_shift) |
| 239 | return 0; |
| 240 | mask = mask * 2 + 1; |
| 241 | size *= 2; |
| 242 | } |
| 243 | |
| 244 | table = nl_pid_hash_alloc(size); |
| 245 | if (!table) |
| 246 | return 0; |
| 247 | |
| 248 | memset(table, 0, size); |
| 249 | otable = hash->table; |
| 250 | hash->table = table; |
| 251 | hash->mask = mask; |
| 252 | hash->shift = shift; |
| 253 | get_random_bytes(&hash->rnd, sizeof(hash->rnd)); |
| 254 | |
| 255 | for (i = 0; i <= omask; i++) { |
| 256 | struct sock *sk; |
| 257 | struct hlist_node *node, *tmp; |
| 258 | |
| 259 | sk_for_each_safe(sk, node, tmp, &otable[i]) |
| 260 | __sk_add_node(sk, nl_pid_hashfn(hash, nlk_sk(sk)->pid)); |
| 261 | } |
| 262 | |
| 263 | nl_pid_hash_free(otable, osize); |
| 264 | hash->rehash_time = jiffies + 10 * 60 * HZ; |
| 265 | return 1; |
| 266 | } |
| 267 | |
| 268 | static inline int nl_pid_hash_dilute(struct nl_pid_hash *hash, int len) |
| 269 | { |
| 270 | int avg = hash->entries >> hash->shift; |
| 271 | |
| 272 | if (unlikely(avg > 1) && nl_pid_hash_rehash(hash, 1)) |
| 273 | return 1; |
| 274 | |
| 275 | if (unlikely(len > avg) && time_after(jiffies, hash->rehash_time)) { |
| 276 | nl_pid_hash_rehash(hash, 0); |
| 277 | return 1; |
| 278 | } |
| 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static struct proto_ops netlink_ops; |
| 284 | |
| 285 | static int netlink_insert(struct sock *sk, u32 pid) |
| 286 | { |
| 287 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; |
| 288 | struct hlist_head *head; |
| 289 | int err = -EADDRINUSE; |
| 290 | struct sock *osk; |
| 291 | struct hlist_node *node; |
| 292 | int len; |
| 293 | |
| 294 | netlink_table_grab(); |
| 295 | head = nl_pid_hashfn(hash, pid); |
| 296 | len = 0; |
| 297 | sk_for_each(osk, node, head) { |
| 298 | if (nlk_sk(osk)->pid == pid) |
| 299 | break; |
| 300 | len++; |
| 301 | } |
| 302 | if (node) |
| 303 | goto err; |
| 304 | |
| 305 | err = -EBUSY; |
| 306 | if (nlk_sk(sk)->pid) |
| 307 | goto err; |
| 308 | |
| 309 | err = -ENOMEM; |
| 310 | if (BITS_PER_LONG > 32 && unlikely(hash->entries >= UINT_MAX)) |
| 311 | goto err; |
| 312 | |
| 313 | if (len && nl_pid_hash_dilute(hash, len)) |
| 314 | head = nl_pid_hashfn(hash, pid); |
| 315 | hash->entries++; |
| 316 | nlk_sk(sk)->pid = pid; |
| 317 | sk_add_node(sk, head); |
| 318 | err = 0; |
| 319 | |
| 320 | err: |
| 321 | netlink_table_ungrab(); |
| 322 | return err; |
| 323 | } |
| 324 | |
| 325 | static void netlink_remove(struct sock *sk) |
| 326 | { |
| 327 | netlink_table_grab(); |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 328 | if (sk_del_node_init(sk)) |
| 329 | nl_table[sk->sk_protocol].hash.entries--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (nlk_sk(sk)->groups) |
| 331 | __sk_del_bind_node(sk); |
| 332 | netlink_table_ungrab(); |
| 333 | } |
| 334 | |
| 335 | static struct proto netlink_proto = { |
| 336 | .name = "NETLINK", |
| 337 | .owner = THIS_MODULE, |
| 338 | .obj_size = sizeof(struct netlink_sock), |
| 339 | }; |
| 340 | |
| 341 | static int netlink_create(struct socket *sock, int protocol) |
| 342 | { |
| 343 | struct sock *sk; |
| 344 | struct netlink_sock *nlk; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 345 | struct module *module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | |
| 347 | sock->state = SS_UNCONNECTED; |
| 348 | |
| 349 | if (sock->type != SOCK_RAW && sock->type != SOCK_DGRAM) |
| 350 | return -ESOCKTNOSUPPORT; |
| 351 | |
| 352 | if (protocol<0 || protocol >= MAX_LINKS) |
| 353 | return -EPROTONOSUPPORT; |
| 354 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 355 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 356 | if (!nl_table[protocol].hash.entries) { |
| 357 | #ifdef CONFIG_KMOD |
| 358 | /* We do 'best effort'. If we find a matching module, |
| 359 | * it is loaded. If not, we don't return an error to |
| 360 | * allow pure userspace<->userspace communication. -HW |
| 361 | */ |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 362 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 363 | request_module("net-pf-%d-proto-%d", PF_NETLINK, protocol); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 364 | netlink_lock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 365 | #endif |
| 366 | } |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 367 | module = nl_table[protocol].module; |
| 368 | if (!try_module_get(module)) |
| 369 | module = NULL; |
| 370 | netlink_unlock_table(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 371 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 372 | sock->ops = &netlink_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | sk = sk_alloc(PF_NETLINK, GFP_KERNEL, &netlink_proto, 1); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 375 | if (!sk) { |
| 376 | module_put(module); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return -ENOMEM; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | |
| 380 | sock_init_data(sock, sk); |
| 381 | |
| 382 | nlk = nlk_sk(sk); |
| 383 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 384 | nlk->module = module; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | spin_lock_init(&nlk->cb_lock); |
| 386 | init_waitqueue_head(&nlk->wait); |
| 387 | sk->sk_destruct = netlink_sock_destruct; |
| 388 | |
| 389 | sk->sk_protocol = protocol; |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | static int netlink_release(struct socket *sock) |
| 394 | { |
| 395 | struct sock *sk = sock->sk; |
| 396 | struct netlink_sock *nlk; |
| 397 | |
| 398 | if (!sk) |
| 399 | return 0; |
| 400 | |
| 401 | netlink_remove(sk); |
| 402 | nlk = nlk_sk(sk); |
| 403 | |
| 404 | spin_lock(&nlk->cb_lock); |
| 405 | if (nlk->cb) { |
| 406 | nlk->cb->done(nlk->cb); |
| 407 | netlink_destroy_callback(nlk->cb); |
| 408 | nlk->cb = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | } |
| 410 | spin_unlock(&nlk->cb_lock); |
| 411 | |
| 412 | /* OK. Socket is unlinked, and, therefore, |
| 413 | no new packets will arrive */ |
| 414 | |
| 415 | sock_orphan(sk); |
| 416 | sock->sk = NULL; |
| 417 | wake_up_interruptible_all(&nlk->wait); |
| 418 | |
| 419 | skb_queue_purge(&sk->sk_write_queue); |
| 420 | |
| 421 | if (nlk->pid && !nlk->groups) { |
| 422 | struct netlink_notify n = { |
| 423 | .protocol = sk->sk_protocol, |
| 424 | .pid = nlk->pid, |
| 425 | }; |
| 426 | notifier_call_chain(&netlink_chain, NETLINK_URELEASE, &n); |
| 427 | } |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 428 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 429 | if (nlk->module) |
| 430 | module_put(nlk->module); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 431 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 432 | if (nlk->flags & NETLINK_KERNEL_SOCKET) { |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 433 | netlink_table_grab(); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 434 | nl_table[sk->sk_protocol].module = NULL; |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 435 | netlink_table_ungrab(); |
| 436 | } |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | sock_put(sk); |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int netlink_autobind(struct socket *sock) |
| 443 | { |
| 444 | struct sock *sk = sock->sk; |
| 445 | struct nl_pid_hash *hash = &nl_table[sk->sk_protocol].hash; |
| 446 | struct hlist_head *head; |
| 447 | struct sock *osk; |
| 448 | struct hlist_node *node; |
| 449 | s32 pid = current->pid; |
| 450 | int err; |
| 451 | static s32 rover = -4097; |
| 452 | |
| 453 | retry: |
| 454 | cond_resched(); |
| 455 | netlink_table_grab(); |
| 456 | head = nl_pid_hashfn(hash, pid); |
| 457 | sk_for_each(osk, node, head) { |
| 458 | if (nlk_sk(osk)->pid == pid) { |
| 459 | /* Bind collision, search negative pid values. */ |
| 460 | pid = rover--; |
| 461 | if (rover > -4097) |
| 462 | rover = -4097; |
| 463 | netlink_table_ungrab(); |
| 464 | goto retry; |
| 465 | } |
| 466 | } |
| 467 | netlink_table_ungrab(); |
| 468 | |
| 469 | err = netlink_insert(sk, pid); |
| 470 | if (err == -EADDRINUSE) |
| 471 | goto retry; |
David S. Miller | d470e3b | 2005-06-26 15:31:51 -0700 | [diff] [blame] | 472 | |
| 473 | /* If 2 threads race to autobind, that is fine. */ |
| 474 | if (err == -EBUSY) |
| 475 | err = 0; |
| 476 | |
| 477 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | static inline int netlink_capable(struct socket *sock, unsigned int flag) |
| 481 | { |
| 482 | return (nl_table[sock->sk->sk_protocol].nl_nonroot & flag) || |
| 483 | capable(CAP_NET_ADMIN); |
| 484 | } |
| 485 | |
| 486 | static int netlink_bind(struct socket *sock, struct sockaddr *addr, int addr_len) |
| 487 | { |
| 488 | struct sock *sk = sock->sk; |
| 489 | struct netlink_sock *nlk = nlk_sk(sk); |
| 490 | struct sockaddr_nl *nladdr = (struct sockaddr_nl *)addr; |
| 491 | int err; |
| 492 | |
| 493 | if (nladdr->nl_family != AF_NETLINK) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | /* Only superuser is allowed to listen multicasts */ |
| 497 | if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_RECV)) |
| 498 | return -EPERM; |
| 499 | |
| 500 | if (nlk->pid) { |
| 501 | if (nladdr->nl_pid != nlk->pid) |
| 502 | return -EINVAL; |
| 503 | } else { |
| 504 | err = nladdr->nl_pid ? |
| 505 | netlink_insert(sk, nladdr->nl_pid) : |
| 506 | netlink_autobind(sock); |
| 507 | if (err) |
| 508 | return err; |
| 509 | } |
| 510 | |
| 511 | if (!nladdr->nl_groups && !nlk->groups) |
| 512 | return 0; |
| 513 | |
| 514 | netlink_table_grab(); |
| 515 | if (nlk->groups && !nladdr->nl_groups) |
| 516 | __sk_del_bind_node(sk); |
| 517 | else if (!nlk->groups && nladdr->nl_groups) |
| 518 | sk_add_bind_node(sk, &nl_table[sk->sk_protocol].mc_list); |
| 519 | nlk->groups = nladdr->nl_groups; |
| 520 | netlink_table_ungrab(); |
| 521 | |
| 522 | return 0; |
| 523 | } |
| 524 | |
| 525 | static int netlink_connect(struct socket *sock, struct sockaddr *addr, |
| 526 | int alen, int flags) |
| 527 | { |
| 528 | int err = 0; |
| 529 | struct sock *sk = sock->sk; |
| 530 | struct netlink_sock *nlk = nlk_sk(sk); |
| 531 | struct sockaddr_nl *nladdr=(struct sockaddr_nl*)addr; |
| 532 | |
| 533 | if (addr->sa_family == AF_UNSPEC) { |
| 534 | sk->sk_state = NETLINK_UNCONNECTED; |
| 535 | nlk->dst_pid = 0; |
| 536 | nlk->dst_groups = 0; |
| 537 | return 0; |
| 538 | } |
| 539 | if (addr->sa_family != AF_NETLINK) |
| 540 | return -EINVAL; |
| 541 | |
| 542 | /* Only superuser is allowed to send multicasts */ |
| 543 | if (nladdr->nl_groups && !netlink_capable(sock, NL_NONROOT_SEND)) |
| 544 | return -EPERM; |
| 545 | |
| 546 | if (!nlk->pid) |
| 547 | err = netlink_autobind(sock); |
| 548 | |
| 549 | if (err == 0) { |
| 550 | sk->sk_state = NETLINK_CONNECTED; |
| 551 | nlk->dst_pid = nladdr->nl_pid; |
| 552 | nlk->dst_groups = nladdr->nl_groups; |
| 553 | } |
| 554 | |
| 555 | return err; |
| 556 | } |
| 557 | |
| 558 | static int netlink_getname(struct socket *sock, struct sockaddr *addr, int *addr_len, int peer) |
| 559 | { |
| 560 | struct sock *sk = sock->sk; |
| 561 | struct netlink_sock *nlk = nlk_sk(sk); |
| 562 | struct sockaddr_nl *nladdr=(struct sockaddr_nl *)addr; |
| 563 | |
| 564 | nladdr->nl_family = AF_NETLINK; |
| 565 | nladdr->nl_pad = 0; |
| 566 | *addr_len = sizeof(*nladdr); |
| 567 | |
| 568 | if (peer) { |
| 569 | nladdr->nl_pid = nlk->dst_pid; |
| 570 | nladdr->nl_groups = nlk->dst_groups; |
| 571 | } else { |
| 572 | nladdr->nl_pid = nlk->pid; |
| 573 | nladdr->nl_groups = nlk->groups; |
| 574 | } |
| 575 | return 0; |
| 576 | } |
| 577 | |
| 578 | static void netlink_overrun(struct sock *sk) |
| 579 | { |
| 580 | if (!test_and_set_bit(0, &nlk_sk(sk)->state)) { |
| 581 | sk->sk_err = ENOBUFS; |
| 582 | sk->sk_error_report(sk); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static struct sock *netlink_getsockbypid(struct sock *ssk, u32 pid) |
| 587 | { |
| 588 | int protocol = ssk->sk_protocol; |
| 589 | struct sock *sock; |
| 590 | struct netlink_sock *nlk; |
| 591 | |
| 592 | sock = netlink_lookup(protocol, pid); |
| 593 | if (!sock) |
| 594 | return ERR_PTR(-ECONNREFUSED); |
| 595 | |
| 596 | /* Don't bother queuing skb if kernel socket has no input function */ |
| 597 | nlk = nlk_sk(sock); |
| 598 | if ((nlk->pid == 0 && !nlk->data_ready) || |
| 599 | (sock->sk_state == NETLINK_CONNECTED && |
| 600 | nlk->dst_pid != nlk_sk(ssk)->pid)) { |
| 601 | sock_put(sock); |
| 602 | return ERR_PTR(-ECONNREFUSED); |
| 603 | } |
| 604 | return sock; |
| 605 | } |
| 606 | |
| 607 | struct sock *netlink_getsockbyfilp(struct file *filp) |
| 608 | { |
| 609 | struct inode *inode = filp->f_dentry->d_inode; |
| 610 | struct sock *sock; |
| 611 | |
| 612 | if (!S_ISSOCK(inode->i_mode)) |
| 613 | return ERR_PTR(-ENOTSOCK); |
| 614 | |
| 615 | sock = SOCKET_I(inode)->sk; |
| 616 | if (sock->sk_family != AF_NETLINK) |
| 617 | return ERR_PTR(-EINVAL); |
| 618 | |
| 619 | sock_hold(sock); |
| 620 | return sock; |
| 621 | } |
| 622 | |
| 623 | /* |
| 624 | * Attach a skb to a netlink socket. |
| 625 | * The caller must hold a reference to the destination socket. On error, the |
| 626 | * reference is dropped. The skb is not send to the destination, just all |
| 627 | * all error checks are performed and memory in the queue is reserved. |
| 628 | * Return values: |
| 629 | * < 0: error. skb freed, reference to sock dropped. |
| 630 | * 0: continue |
| 631 | * 1: repeat lookup - reference dropped while waiting for socket memory. |
| 632 | */ |
| 633 | int netlink_attachskb(struct sock *sk, struct sk_buff *skb, int nonblock, long timeo) |
| 634 | { |
| 635 | struct netlink_sock *nlk; |
| 636 | |
| 637 | nlk = nlk_sk(sk); |
| 638 | |
| 639 | if (atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 640 | test_bit(0, &nlk->state)) { |
| 641 | DECLARE_WAITQUEUE(wait, current); |
| 642 | if (!timeo) { |
| 643 | if (!nlk->pid) |
| 644 | netlink_overrun(sk); |
| 645 | sock_put(sk); |
| 646 | kfree_skb(skb); |
| 647 | return -EAGAIN; |
| 648 | } |
| 649 | |
| 650 | __set_current_state(TASK_INTERRUPTIBLE); |
| 651 | add_wait_queue(&nlk->wait, &wait); |
| 652 | |
| 653 | if ((atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf || |
| 654 | test_bit(0, &nlk->state)) && |
| 655 | !sock_flag(sk, SOCK_DEAD)) |
| 656 | timeo = schedule_timeout(timeo); |
| 657 | |
| 658 | __set_current_state(TASK_RUNNING); |
| 659 | remove_wait_queue(&nlk->wait, &wait); |
| 660 | sock_put(sk); |
| 661 | |
| 662 | if (signal_pending(current)) { |
| 663 | kfree_skb(skb); |
| 664 | return sock_intr_errno(timeo); |
| 665 | } |
| 666 | return 1; |
| 667 | } |
| 668 | skb_set_owner_r(skb, sk); |
| 669 | return 0; |
| 670 | } |
| 671 | |
| 672 | int netlink_sendskb(struct sock *sk, struct sk_buff *skb, int protocol) |
| 673 | { |
| 674 | struct netlink_sock *nlk; |
| 675 | int len = skb->len; |
| 676 | |
| 677 | nlk = nlk_sk(sk); |
| 678 | |
| 679 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 680 | sk->sk_data_ready(sk, len); |
| 681 | sock_put(sk); |
| 682 | return len; |
| 683 | } |
| 684 | |
| 685 | void netlink_detachskb(struct sock *sk, struct sk_buff *skb) |
| 686 | { |
| 687 | kfree_skb(skb); |
| 688 | sock_put(sk); |
| 689 | } |
| 690 | |
Victor Fusco | 37da647 | 2005-07-18 13:35:43 -0700 | [diff] [blame] | 691 | static inline struct sk_buff *netlink_trim(struct sk_buff *skb, |
| 692 | unsigned int __nocast allocation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | { |
| 694 | int delta; |
| 695 | |
| 696 | skb_orphan(skb); |
| 697 | |
| 698 | delta = skb->end - skb->tail; |
| 699 | if (delta * 2 < skb->truesize) |
| 700 | return skb; |
| 701 | |
| 702 | if (skb_shared(skb)) { |
| 703 | struct sk_buff *nskb = skb_clone(skb, allocation); |
| 704 | if (!nskb) |
| 705 | return skb; |
| 706 | kfree_skb(skb); |
| 707 | skb = nskb; |
| 708 | } |
| 709 | |
| 710 | if (!pskb_expand_head(skb, 0, -delta, allocation)) |
| 711 | skb->truesize -= delta; |
| 712 | |
| 713 | return skb; |
| 714 | } |
| 715 | |
| 716 | int netlink_unicast(struct sock *ssk, struct sk_buff *skb, u32 pid, int nonblock) |
| 717 | { |
| 718 | struct sock *sk; |
| 719 | int err; |
| 720 | long timeo; |
| 721 | |
| 722 | skb = netlink_trim(skb, gfp_any()); |
| 723 | |
| 724 | timeo = sock_sndtimeo(ssk, nonblock); |
| 725 | retry: |
| 726 | sk = netlink_getsockbypid(ssk, pid); |
| 727 | if (IS_ERR(sk)) { |
| 728 | kfree_skb(skb); |
| 729 | return PTR_ERR(sk); |
| 730 | } |
| 731 | err = netlink_attachskb(sk, skb, nonblock, timeo); |
| 732 | if (err == 1) |
| 733 | goto retry; |
| 734 | if (err) |
| 735 | return err; |
| 736 | |
| 737 | return netlink_sendskb(sk, skb, ssk->sk_protocol); |
| 738 | } |
| 739 | |
| 740 | static __inline__ int netlink_broadcast_deliver(struct sock *sk, struct sk_buff *skb) |
| 741 | { |
| 742 | struct netlink_sock *nlk = nlk_sk(sk); |
| 743 | |
| 744 | if (atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf && |
| 745 | !test_bit(0, &nlk->state)) { |
| 746 | skb_set_owner_r(skb, sk); |
| 747 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 748 | sk->sk_data_ready(sk, skb->len); |
| 749 | return atomic_read(&sk->sk_rmem_alloc) > sk->sk_rcvbuf; |
| 750 | } |
| 751 | return -1; |
| 752 | } |
| 753 | |
| 754 | struct netlink_broadcast_data { |
| 755 | struct sock *exclude_sk; |
| 756 | u32 pid; |
| 757 | u32 group; |
| 758 | int failure; |
| 759 | int congested; |
| 760 | int delivered; |
Victor Fusco | 37da647 | 2005-07-18 13:35:43 -0700 | [diff] [blame] | 761 | unsigned int allocation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | struct sk_buff *skb, *skb2; |
| 763 | }; |
| 764 | |
| 765 | static inline int do_one_broadcast(struct sock *sk, |
| 766 | struct netlink_broadcast_data *p) |
| 767 | { |
| 768 | struct netlink_sock *nlk = nlk_sk(sk); |
| 769 | int val; |
| 770 | |
| 771 | if (p->exclude_sk == sk) |
| 772 | goto out; |
| 773 | |
| 774 | if (nlk->pid == p->pid || !(nlk->groups & p->group)) |
| 775 | goto out; |
| 776 | |
| 777 | if (p->failure) { |
| 778 | netlink_overrun(sk); |
| 779 | goto out; |
| 780 | } |
| 781 | |
| 782 | sock_hold(sk); |
| 783 | if (p->skb2 == NULL) { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 784 | if (skb_shared(p->skb)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | p->skb2 = skb_clone(p->skb, p->allocation); |
| 786 | } else { |
Tommy S. Christensen | 68acc02 | 2005-05-19 13:06:35 -0700 | [diff] [blame] | 787 | p->skb2 = skb_get(p->skb); |
| 788 | /* |
| 789 | * skb ownership may have been set when |
| 790 | * delivered to a previous socket. |
| 791 | */ |
| 792 | skb_orphan(p->skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | } |
| 794 | } |
| 795 | if (p->skb2 == NULL) { |
| 796 | netlink_overrun(sk); |
| 797 | /* Clone failed. Notify ALL listeners. */ |
| 798 | p->failure = 1; |
| 799 | } else if ((val = netlink_broadcast_deliver(sk, p->skb2)) < 0) { |
| 800 | netlink_overrun(sk); |
| 801 | } else { |
| 802 | p->congested |= val; |
| 803 | p->delivered = 1; |
| 804 | p->skb2 = NULL; |
| 805 | } |
| 806 | sock_put(sk); |
| 807 | |
| 808 | out: |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | int netlink_broadcast(struct sock *ssk, struct sk_buff *skb, u32 pid, |
| 813 | u32 group, int allocation) |
| 814 | { |
| 815 | struct netlink_broadcast_data info; |
| 816 | struct hlist_node *node; |
| 817 | struct sock *sk; |
| 818 | |
| 819 | skb = netlink_trim(skb, allocation); |
| 820 | |
| 821 | info.exclude_sk = ssk; |
| 822 | info.pid = pid; |
| 823 | info.group = group; |
| 824 | info.failure = 0; |
| 825 | info.congested = 0; |
| 826 | info.delivered = 0; |
| 827 | info.allocation = allocation; |
| 828 | info.skb = skb; |
| 829 | info.skb2 = NULL; |
| 830 | |
| 831 | /* While we sleep in clone, do not allow to change socket list */ |
| 832 | |
| 833 | netlink_lock_table(); |
| 834 | |
| 835 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) |
| 836 | do_one_broadcast(sk, &info); |
| 837 | |
Tommy S. Christensen | aa1c6a6 | 2005-05-19 13:07:32 -0700 | [diff] [blame] | 838 | kfree_skb(skb); |
| 839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | netlink_unlock_table(); |
| 841 | |
| 842 | if (info.skb2) |
| 843 | kfree_skb(info.skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | |
| 845 | if (info.delivered) { |
| 846 | if (info.congested && (allocation & __GFP_WAIT)) |
| 847 | yield(); |
| 848 | return 0; |
| 849 | } |
| 850 | if (info.failure) |
| 851 | return -ENOBUFS; |
| 852 | return -ESRCH; |
| 853 | } |
| 854 | |
| 855 | struct netlink_set_err_data { |
| 856 | struct sock *exclude_sk; |
| 857 | u32 pid; |
| 858 | u32 group; |
| 859 | int code; |
| 860 | }; |
| 861 | |
| 862 | static inline int do_one_set_err(struct sock *sk, |
| 863 | struct netlink_set_err_data *p) |
| 864 | { |
| 865 | struct netlink_sock *nlk = nlk_sk(sk); |
| 866 | |
| 867 | if (sk == p->exclude_sk) |
| 868 | goto out; |
| 869 | |
| 870 | if (nlk->pid == p->pid || !(nlk->groups & p->group)) |
| 871 | goto out; |
| 872 | |
| 873 | sk->sk_err = p->code; |
| 874 | sk->sk_error_report(sk); |
| 875 | out: |
| 876 | return 0; |
| 877 | } |
| 878 | |
| 879 | void netlink_set_err(struct sock *ssk, u32 pid, u32 group, int code) |
| 880 | { |
| 881 | struct netlink_set_err_data info; |
| 882 | struct hlist_node *node; |
| 883 | struct sock *sk; |
| 884 | |
| 885 | info.exclude_sk = ssk; |
| 886 | info.pid = pid; |
| 887 | info.group = group; |
| 888 | info.code = code; |
| 889 | |
| 890 | read_lock(&nl_table_lock); |
| 891 | |
| 892 | sk_for_each_bound(sk, node, &nl_table[ssk->sk_protocol].mc_list) |
| 893 | do_one_set_err(sk, &info); |
| 894 | |
| 895 | read_unlock(&nl_table_lock); |
| 896 | } |
| 897 | |
| 898 | static inline void netlink_rcv_wake(struct sock *sk) |
| 899 | { |
| 900 | struct netlink_sock *nlk = nlk_sk(sk); |
| 901 | |
David S. Miller | b03efcf | 2005-07-08 14:57:23 -0700 | [diff] [blame] | 902 | if (skb_queue_empty(&sk->sk_receive_queue)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | clear_bit(0, &nlk->state); |
| 904 | if (!test_bit(0, &nlk->state)) |
| 905 | wake_up_interruptible(&nlk->wait); |
| 906 | } |
| 907 | |
| 908 | static int netlink_sendmsg(struct kiocb *kiocb, struct socket *sock, |
| 909 | struct msghdr *msg, size_t len) |
| 910 | { |
| 911 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 912 | struct sock *sk = sock->sk; |
| 913 | struct netlink_sock *nlk = nlk_sk(sk); |
| 914 | struct sockaddr_nl *addr=msg->msg_name; |
| 915 | u32 dst_pid; |
| 916 | u32 dst_groups; |
| 917 | struct sk_buff *skb; |
| 918 | int err; |
| 919 | struct scm_cookie scm; |
| 920 | |
| 921 | if (msg->msg_flags&MSG_OOB) |
| 922 | return -EOPNOTSUPP; |
| 923 | |
| 924 | if (NULL == siocb->scm) |
| 925 | siocb->scm = &scm; |
| 926 | err = scm_send(sock, msg, siocb->scm); |
| 927 | if (err < 0) |
| 928 | return err; |
| 929 | |
| 930 | if (msg->msg_namelen) { |
| 931 | if (addr->nl_family != AF_NETLINK) |
| 932 | return -EINVAL; |
| 933 | dst_pid = addr->nl_pid; |
| 934 | dst_groups = addr->nl_groups; |
| 935 | if (dst_groups && !netlink_capable(sock, NL_NONROOT_SEND)) |
| 936 | return -EPERM; |
| 937 | } else { |
| 938 | dst_pid = nlk->dst_pid; |
| 939 | dst_groups = nlk->dst_groups; |
| 940 | } |
| 941 | |
| 942 | if (!nlk->pid) { |
| 943 | err = netlink_autobind(sock); |
| 944 | if (err) |
| 945 | goto out; |
| 946 | } |
| 947 | |
| 948 | err = -EMSGSIZE; |
| 949 | if (len > sk->sk_sndbuf - 32) |
| 950 | goto out; |
| 951 | err = -ENOBUFS; |
| 952 | skb = alloc_skb(len, GFP_KERNEL); |
| 953 | if (skb==NULL) |
| 954 | goto out; |
| 955 | |
| 956 | NETLINK_CB(skb).pid = nlk->pid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | NETLINK_CB(skb).dst_pid = dst_pid; |
| 958 | NETLINK_CB(skb).dst_groups = dst_groups; |
Serge Hallyn | c94c257 | 2005-04-29 16:27:17 +0100 | [diff] [blame] | 959 | NETLINK_CB(skb).loginuid = audit_get_loginuid(current->audit_context); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | memcpy(NETLINK_CREDS(skb), &siocb->scm->creds, sizeof(struct ucred)); |
| 961 | |
| 962 | /* What can I do? Netlink is asynchronous, so that |
| 963 | we will have to save current capabilities to |
| 964 | check them, when this message will be delivered |
| 965 | to corresponding kernel module. --ANK (980802) |
| 966 | */ |
| 967 | |
| 968 | err = -EFAULT; |
| 969 | if (memcpy_fromiovec(skb_put(skb,len), msg->msg_iov, len)) { |
| 970 | kfree_skb(skb); |
| 971 | goto out; |
| 972 | } |
| 973 | |
| 974 | err = security_netlink_send(sk, skb); |
| 975 | if (err) { |
| 976 | kfree_skb(skb); |
| 977 | goto out; |
| 978 | } |
| 979 | |
| 980 | if (dst_groups) { |
| 981 | atomic_inc(&skb->users); |
| 982 | netlink_broadcast(sk, skb, dst_pid, dst_groups, GFP_KERNEL); |
| 983 | } |
| 984 | err = netlink_unicast(sk, skb, dst_pid, msg->msg_flags&MSG_DONTWAIT); |
| 985 | |
| 986 | out: |
| 987 | return err; |
| 988 | } |
| 989 | |
| 990 | static int netlink_recvmsg(struct kiocb *kiocb, struct socket *sock, |
| 991 | struct msghdr *msg, size_t len, |
| 992 | int flags) |
| 993 | { |
| 994 | struct sock_iocb *siocb = kiocb_to_siocb(kiocb); |
| 995 | struct scm_cookie scm; |
| 996 | struct sock *sk = sock->sk; |
| 997 | struct netlink_sock *nlk = nlk_sk(sk); |
| 998 | int noblock = flags&MSG_DONTWAIT; |
| 999 | size_t copied; |
| 1000 | struct sk_buff *skb; |
| 1001 | int err; |
| 1002 | |
| 1003 | if (flags&MSG_OOB) |
| 1004 | return -EOPNOTSUPP; |
| 1005 | |
| 1006 | copied = 0; |
| 1007 | |
| 1008 | skb = skb_recv_datagram(sk,flags,noblock,&err); |
| 1009 | if (skb==NULL) |
| 1010 | goto out; |
| 1011 | |
| 1012 | msg->msg_namelen = 0; |
| 1013 | |
| 1014 | copied = skb->len; |
| 1015 | if (len < copied) { |
| 1016 | msg->msg_flags |= MSG_TRUNC; |
| 1017 | copied = len; |
| 1018 | } |
| 1019 | |
| 1020 | skb->h.raw = skb->data; |
| 1021 | err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); |
| 1022 | |
| 1023 | if (msg->msg_name) { |
| 1024 | struct sockaddr_nl *addr = (struct sockaddr_nl*)msg->msg_name; |
| 1025 | addr->nl_family = AF_NETLINK; |
| 1026 | addr->nl_pad = 0; |
| 1027 | addr->nl_pid = NETLINK_CB(skb).pid; |
| 1028 | addr->nl_groups = NETLINK_CB(skb).dst_groups; |
| 1029 | msg->msg_namelen = sizeof(*addr); |
| 1030 | } |
| 1031 | |
| 1032 | if (NULL == siocb->scm) { |
| 1033 | memset(&scm, 0, sizeof(scm)); |
| 1034 | siocb->scm = &scm; |
| 1035 | } |
| 1036 | siocb->scm->creds = *NETLINK_CREDS(skb); |
| 1037 | skb_free_datagram(sk, skb); |
| 1038 | |
| 1039 | if (nlk->cb && atomic_read(&sk->sk_rmem_alloc) <= sk->sk_rcvbuf / 2) |
| 1040 | netlink_dump(sk); |
| 1041 | |
| 1042 | scm_recv(sock, msg, siocb->scm, flags); |
| 1043 | |
| 1044 | out: |
| 1045 | netlink_rcv_wake(sk); |
| 1046 | return err ? : copied; |
| 1047 | } |
| 1048 | |
| 1049 | static void netlink_data_ready(struct sock *sk, int len) |
| 1050 | { |
| 1051 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1052 | |
| 1053 | if (nlk->data_ready) |
| 1054 | nlk->data_ready(sk, len); |
| 1055 | netlink_rcv_wake(sk); |
| 1056 | } |
| 1057 | |
| 1058 | /* |
| 1059 | * We export these functions to other modules. They provide a |
| 1060 | * complete set of kernel non-blocking support for message |
| 1061 | * queueing. |
| 1062 | */ |
| 1063 | |
| 1064 | struct sock * |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1065 | netlink_kernel_create(int unit, void (*input)(struct sock *sk, int len), struct module *module) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
| 1067 | struct socket *sock; |
| 1068 | struct sock *sk; |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 1069 | struct netlink_sock *nlk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | |
| 1071 | if (!nl_table) |
| 1072 | return NULL; |
| 1073 | |
| 1074 | if (unit<0 || unit>=MAX_LINKS) |
| 1075 | return NULL; |
| 1076 | |
| 1077 | if (sock_create_lite(PF_NETLINK, SOCK_DGRAM, unit, &sock)) |
| 1078 | return NULL; |
| 1079 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 1080 | if (netlink_create(sock, unit) < 0) |
| 1081 | goto out_sock_release; |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1082 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | sk = sock->sk; |
| 1084 | sk->sk_data_ready = netlink_data_ready; |
| 1085 | if (input) |
| 1086 | nlk_sk(sk)->data_ready = input; |
| 1087 | |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 1088 | if (netlink_insert(sk, 0)) |
| 1089 | goto out_sock_release; |
| 1090 | |
| 1091 | nlk = nlk_sk(sk); |
| 1092 | nlk->flags |= NETLINK_KERNEL_SOCKET; |
| 1093 | |
| 1094 | netlink_table_grab(); |
| 1095 | nl_table[unit].module = module; |
| 1096 | netlink_table_ungrab(); |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1097 | |
| 1098 | return sk; |
| 1099 | |
Harald Welte | 4fdb3bb | 2005-08-09 19:40:55 -0700 | [diff] [blame] | 1100 | out_sock_release: |
| 1101 | sock_release(sock); |
Patrick McHardy | 77247bb | 2005-08-14 19:27:13 -0700 | [diff] [blame^] | 1102 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | void netlink_set_nonroot(int protocol, unsigned int flags) |
| 1106 | { |
| 1107 | if ((unsigned int)protocol < MAX_LINKS) |
| 1108 | nl_table[protocol].nl_nonroot = flags; |
| 1109 | } |
| 1110 | |
| 1111 | static void netlink_destroy_callback(struct netlink_callback *cb) |
| 1112 | { |
| 1113 | if (cb->skb) |
| 1114 | kfree_skb(cb->skb); |
| 1115 | kfree(cb); |
| 1116 | } |
| 1117 | |
| 1118 | /* |
| 1119 | * It looks a bit ugly. |
| 1120 | * It would be better to create kernel thread. |
| 1121 | */ |
| 1122 | |
| 1123 | static int netlink_dump(struct sock *sk) |
| 1124 | { |
| 1125 | struct netlink_sock *nlk = nlk_sk(sk); |
| 1126 | struct netlink_callback *cb; |
| 1127 | struct sk_buff *skb; |
| 1128 | struct nlmsghdr *nlh; |
| 1129 | int len; |
| 1130 | |
| 1131 | skb = sock_rmalloc(sk, NLMSG_GOODSIZE, 0, GFP_KERNEL); |
| 1132 | if (!skb) |
| 1133 | return -ENOBUFS; |
| 1134 | |
| 1135 | spin_lock(&nlk->cb_lock); |
| 1136 | |
| 1137 | cb = nlk->cb; |
| 1138 | if (cb == NULL) { |
| 1139 | spin_unlock(&nlk->cb_lock); |
| 1140 | kfree_skb(skb); |
| 1141 | return -EINVAL; |
| 1142 | } |
| 1143 | |
| 1144 | len = cb->dump(skb, cb); |
| 1145 | |
| 1146 | if (len > 0) { |
| 1147 | spin_unlock(&nlk->cb_lock); |
| 1148 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 1149 | sk->sk_data_ready(sk, len); |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 1153 | nlh = NLMSG_NEW_ANSWER(skb, cb, NLMSG_DONE, sizeof(len), NLM_F_MULTI); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | memcpy(NLMSG_DATA(nlh), &len, sizeof(len)); |
| 1155 | skb_queue_tail(&sk->sk_receive_queue, skb); |
| 1156 | sk->sk_data_ready(sk, skb->len); |
| 1157 | |
| 1158 | cb->done(cb); |
| 1159 | nlk->cb = NULL; |
| 1160 | spin_unlock(&nlk->cb_lock); |
| 1161 | |
| 1162 | netlink_destroy_callback(cb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | return 0; |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 1164 | |
| 1165 | nlmsg_failure: |
| 1166 | return -ENOBUFS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | int netlink_dump_start(struct sock *ssk, struct sk_buff *skb, |
| 1170 | struct nlmsghdr *nlh, |
| 1171 | int (*dump)(struct sk_buff *skb, struct netlink_callback*), |
| 1172 | int (*done)(struct netlink_callback*)) |
| 1173 | { |
| 1174 | struct netlink_callback *cb; |
| 1175 | struct sock *sk; |
| 1176 | struct netlink_sock *nlk; |
| 1177 | |
| 1178 | cb = kmalloc(sizeof(*cb), GFP_KERNEL); |
| 1179 | if (cb == NULL) |
| 1180 | return -ENOBUFS; |
| 1181 | |
| 1182 | memset(cb, 0, sizeof(*cb)); |
| 1183 | cb->dump = dump; |
| 1184 | cb->done = done; |
| 1185 | cb->nlh = nlh; |
| 1186 | atomic_inc(&skb->users); |
| 1187 | cb->skb = skb; |
| 1188 | |
| 1189 | sk = netlink_lookup(ssk->sk_protocol, NETLINK_CB(skb).pid); |
| 1190 | if (sk == NULL) { |
| 1191 | netlink_destroy_callback(cb); |
| 1192 | return -ECONNREFUSED; |
| 1193 | } |
| 1194 | nlk = nlk_sk(sk); |
| 1195 | /* A dump is in progress... */ |
| 1196 | spin_lock(&nlk->cb_lock); |
| 1197 | if (nlk->cb) { |
| 1198 | spin_unlock(&nlk->cb_lock); |
| 1199 | netlink_destroy_callback(cb); |
| 1200 | sock_put(sk); |
| 1201 | return -EBUSY; |
| 1202 | } |
| 1203 | nlk->cb = cb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | spin_unlock(&nlk->cb_lock); |
| 1205 | |
| 1206 | netlink_dump(sk); |
| 1207 | sock_put(sk); |
| 1208 | return 0; |
| 1209 | } |
| 1210 | |
| 1211 | void netlink_ack(struct sk_buff *in_skb, struct nlmsghdr *nlh, int err) |
| 1212 | { |
| 1213 | struct sk_buff *skb; |
| 1214 | struct nlmsghdr *rep; |
| 1215 | struct nlmsgerr *errmsg; |
| 1216 | int size; |
| 1217 | |
| 1218 | if (err == 0) |
| 1219 | size = NLMSG_SPACE(sizeof(struct nlmsgerr)); |
| 1220 | else |
| 1221 | size = NLMSG_SPACE(4 + NLMSG_ALIGN(nlh->nlmsg_len)); |
| 1222 | |
| 1223 | skb = alloc_skb(size, GFP_KERNEL); |
| 1224 | if (!skb) { |
| 1225 | struct sock *sk; |
| 1226 | |
| 1227 | sk = netlink_lookup(in_skb->sk->sk_protocol, |
| 1228 | NETLINK_CB(in_skb).pid); |
| 1229 | if (sk) { |
| 1230 | sk->sk_err = ENOBUFS; |
| 1231 | sk->sk_error_report(sk); |
| 1232 | sock_put(sk); |
| 1233 | } |
| 1234 | return; |
| 1235 | } |
| 1236 | |
| 1237 | rep = __nlmsg_put(skb, NETLINK_CB(in_skb).pid, nlh->nlmsg_seq, |
Thomas Graf | 1797754 | 2005-06-18 22:53:48 -0700 | [diff] [blame] | 1238 | NLMSG_ERROR, sizeof(struct nlmsgerr), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | errmsg = NLMSG_DATA(rep); |
| 1240 | errmsg->error = err; |
| 1241 | memcpy(&errmsg->msg, nlh, err ? nlh->nlmsg_len : sizeof(struct nlmsghdr)); |
| 1242 | netlink_unicast(in_skb->sk, skb, NETLINK_CB(in_skb).pid, MSG_DONTWAIT); |
| 1243 | } |
| 1244 | |
| 1245 | |
| 1246 | #ifdef CONFIG_PROC_FS |
| 1247 | struct nl_seq_iter { |
| 1248 | int link; |
| 1249 | int hash_idx; |
| 1250 | }; |
| 1251 | |
| 1252 | static struct sock *netlink_seq_socket_idx(struct seq_file *seq, loff_t pos) |
| 1253 | { |
| 1254 | struct nl_seq_iter *iter = seq->private; |
| 1255 | int i, j; |
| 1256 | struct sock *s; |
| 1257 | struct hlist_node *node; |
| 1258 | loff_t off = 0; |
| 1259 | |
| 1260 | for (i=0; i<MAX_LINKS; i++) { |
| 1261 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1262 | |
| 1263 | for (j = 0; j <= hash->mask; j++) { |
| 1264 | sk_for_each(s, node, &hash->table[j]) { |
| 1265 | if (off == pos) { |
| 1266 | iter->link = i; |
| 1267 | iter->hash_idx = j; |
| 1268 | return s; |
| 1269 | } |
| 1270 | ++off; |
| 1271 | } |
| 1272 | } |
| 1273 | } |
| 1274 | return NULL; |
| 1275 | } |
| 1276 | |
| 1277 | static void *netlink_seq_start(struct seq_file *seq, loff_t *pos) |
| 1278 | { |
| 1279 | read_lock(&nl_table_lock); |
| 1280 | return *pos ? netlink_seq_socket_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 1281 | } |
| 1282 | |
| 1283 | static void *netlink_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 1284 | { |
| 1285 | struct sock *s; |
| 1286 | struct nl_seq_iter *iter; |
| 1287 | int i, j; |
| 1288 | |
| 1289 | ++*pos; |
| 1290 | |
| 1291 | if (v == SEQ_START_TOKEN) |
| 1292 | return netlink_seq_socket_idx(seq, 0); |
| 1293 | |
| 1294 | s = sk_next(v); |
| 1295 | if (s) |
| 1296 | return s; |
| 1297 | |
| 1298 | iter = seq->private; |
| 1299 | i = iter->link; |
| 1300 | j = iter->hash_idx + 1; |
| 1301 | |
| 1302 | do { |
| 1303 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1304 | |
| 1305 | for (; j <= hash->mask; j++) { |
| 1306 | s = sk_head(&hash->table[j]); |
| 1307 | if (s) { |
| 1308 | iter->link = i; |
| 1309 | iter->hash_idx = j; |
| 1310 | return s; |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | j = 0; |
| 1315 | } while (++i < MAX_LINKS); |
| 1316 | |
| 1317 | return NULL; |
| 1318 | } |
| 1319 | |
| 1320 | static void netlink_seq_stop(struct seq_file *seq, void *v) |
| 1321 | { |
| 1322 | read_unlock(&nl_table_lock); |
| 1323 | } |
| 1324 | |
| 1325 | |
| 1326 | static int netlink_seq_show(struct seq_file *seq, void *v) |
| 1327 | { |
| 1328 | if (v == SEQ_START_TOKEN) |
| 1329 | seq_puts(seq, |
| 1330 | "sk Eth Pid Groups " |
| 1331 | "Rmem Wmem Dump Locks\n"); |
| 1332 | else { |
| 1333 | struct sock *s = v; |
| 1334 | struct netlink_sock *nlk = nlk_sk(s); |
| 1335 | |
| 1336 | seq_printf(seq, "%p %-3d %-6d %08x %-8d %-8d %p %d\n", |
| 1337 | s, |
| 1338 | s->sk_protocol, |
| 1339 | nlk->pid, |
| 1340 | nlk->groups, |
| 1341 | atomic_read(&s->sk_rmem_alloc), |
| 1342 | atomic_read(&s->sk_wmem_alloc), |
| 1343 | nlk->cb, |
| 1344 | atomic_read(&s->sk_refcnt) |
| 1345 | ); |
| 1346 | |
| 1347 | } |
| 1348 | return 0; |
| 1349 | } |
| 1350 | |
| 1351 | static struct seq_operations netlink_seq_ops = { |
| 1352 | .start = netlink_seq_start, |
| 1353 | .next = netlink_seq_next, |
| 1354 | .stop = netlink_seq_stop, |
| 1355 | .show = netlink_seq_show, |
| 1356 | }; |
| 1357 | |
| 1358 | |
| 1359 | static int netlink_seq_open(struct inode *inode, struct file *file) |
| 1360 | { |
| 1361 | struct seq_file *seq; |
| 1362 | struct nl_seq_iter *iter; |
| 1363 | int err; |
| 1364 | |
| 1365 | iter = kmalloc(sizeof(*iter), GFP_KERNEL); |
| 1366 | if (!iter) |
| 1367 | return -ENOMEM; |
| 1368 | |
| 1369 | err = seq_open(file, &netlink_seq_ops); |
| 1370 | if (err) { |
| 1371 | kfree(iter); |
| 1372 | return err; |
| 1373 | } |
| 1374 | |
| 1375 | memset(iter, 0, sizeof(*iter)); |
| 1376 | seq = file->private_data; |
| 1377 | seq->private = iter; |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
| 1381 | static struct file_operations netlink_seq_fops = { |
| 1382 | .owner = THIS_MODULE, |
| 1383 | .open = netlink_seq_open, |
| 1384 | .read = seq_read, |
| 1385 | .llseek = seq_lseek, |
| 1386 | .release = seq_release_private, |
| 1387 | }; |
| 1388 | |
| 1389 | #endif |
| 1390 | |
| 1391 | int netlink_register_notifier(struct notifier_block *nb) |
| 1392 | { |
| 1393 | return notifier_chain_register(&netlink_chain, nb); |
| 1394 | } |
| 1395 | |
| 1396 | int netlink_unregister_notifier(struct notifier_block *nb) |
| 1397 | { |
| 1398 | return notifier_chain_unregister(&netlink_chain, nb); |
| 1399 | } |
| 1400 | |
| 1401 | static struct proto_ops netlink_ops = { |
| 1402 | .family = PF_NETLINK, |
| 1403 | .owner = THIS_MODULE, |
| 1404 | .release = netlink_release, |
| 1405 | .bind = netlink_bind, |
| 1406 | .connect = netlink_connect, |
| 1407 | .socketpair = sock_no_socketpair, |
| 1408 | .accept = sock_no_accept, |
| 1409 | .getname = netlink_getname, |
| 1410 | .poll = datagram_poll, |
| 1411 | .ioctl = sock_no_ioctl, |
| 1412 | .listen = sock_no_listen, |
| 1413 | .shutdown = sock_no_shutdown, |
| 1414 | .setsockopt = sock_no_setsockopt, |
| 1415 | .getsockopt = sock_no_getsockopt, |
| 1416 | .sendmsg = netlink_sendmsg, |
| 1417 | .recvmsg = netlink_recvmsg, |
| 1418 | .mmap = sock_no_mmap, |
| 1419 | .sendpage = sock_no_sendpage, |
| 1420 | }; |
| 1421 | |
| 1422 | static struct net_proto_family netlink_family_ops = { |
| 1423 | .family = PF_NETLINK, |
| 1424 | .create = netlink_create, |
| 1425 | .owner = THIS_MODULE, /* for consistency 8) */ |
| 1426 | }; |
| 1427 | |
| 1428 | extern void netlink_skb_parms_too_large(void); |
| 1429 | |
| 1430 | static int __init netlink_proto_init(void) |
| 1431 | { |
| 1432 | struct sk_buff *dummy_skb; |
| 1433 | int i; |
| 1434 | unsigned long max; |
| 1435 | unsigned int order; |
| 1436 | int err = proto_register(&netlink_proto, 0); |
| 1437 | |
| 1438 | if (err != 0) |
| 1439 | goto out; |
| 1440 | |
| 1441 | if (sizeof(struct netlink_skb_parms) > sizeof(dummy_skb->cb)) |
| 1442 | netlink_skb_parms_too_large(); |
| 1443 | |
| 1444 | nl_table = kmalloc(sizeof(*nl_table) * MAX_LINKS, GFP_KERNEL); |
| 1445 | if (!nl_table) { |
| 1446 | enomem: |
| 1447 | printk(KERN_CRIT "netlink_init: Cannot allocate nl_table\n"); |
| 1448 | return -ENOMEM; |
| 1449 | } |
| 1450 | |
| 1451 | memset(nl_table, 0, sizeof(*nl_table) * MAX_LINKS); |
| 1452 | |
| 1453 | if (num_physpages >= (128 * 1024)) |
| 1454 | max = num_physpages >> (21 - PAGE_SHIFT); |
| 1455 | else |
| 1456 | max = num_physpages >> (23 - PAGE_SHIFT); |
| 1457 | |
| 1458 | order = get_bitmask_order(max) - 1 + PAGE_SHIFT; |
| 1459 | max = (1UL << order) / sizeof(struct hlist_head); |
| 1460 | order = get_bitmask_order(max > UINT_MAX ? UINT_MAX : max) - 1; |
| 1461 | |
| 1462 | for (i = 0; i < MAX_LINKS; i++) { |
| 1463 | struct nl_pid_hash *hash = &nl_table[i].hash; |
| 1464 | |
| 1465 | hash->table = nl_pid_hash_alloc(1 * sizeof(*hash->table)); |
| 1466 | if (!hash->table) { |
| 1467 | while (i-- > 0) |
| 1468 | nl_pid_hash_free(nl_table[i].hash.table, |
| 1469 | 1 * sizeof(*hash->table)); |
| 1470 | kfree(nl_table); |
| 1471 | goto enomem; |
| 1472 | } |
| 1473 | memset(hash->table, 0, 1 * sizeof(*hash->table)); |
| 1474 | hash->max_shift = order; |
| 1475 | hash->shift = 0; |
| 1476 | hash->mask = 0; |
| 1477 | hash->rehash_time = jiffies; |
| 1478 | } |
| 1479 | |
| 1480 | sock_register(&netlink_family_ops); |
| 1481 | #ifdef CONFIG_PROC_FS |
| 1482 | proc_net_fops_create("netlink", 0, &netlink_seq_fops); |
| 1483 | #endif |
| 1484 | /* The netlink device handler may be needed early. */ |
| 1485 | rtnetlink_init(); |
| 1486 | out: |
| 1487 | return err; |
| 1488 | } |
| 1489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | core_initcall(netlink_proto_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | EXPORT_SYMBOL(netlink_ack); |
| 1493 | EXPORT_SYMBOL(netlink_broadcast); |
| 1494 | EXPORT_SYMBOL(netlink_dump_start); |
| 1495 | EXPORT_SYMBOL(netlink_kernel_create); |
| 1496 | EXPORT_SYMBOL(netlink_register_notifier); |
| 1497 | EXPORT_SYMBOL(netlink_set_err); |
| 1498 | EXPORT_SYMBOL(netlink_set_nonroot); |
| 1499 | EXPORT_SYMBOL(netlink_unicast); |
| 1500 | EXPORT_SYMBOL(netlink_unregister_notifier); |
| 1501 | |