David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* AF_RXRPC implementation |
| 2 | * |
| 3 | * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
YOSHIFUJI Hideaki / 吉藤英明 | ce6654c | 2013-01-09 07:20:01 +0000 | [diff] [blame] | 13 | #include <linux/kernel.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #include <linux/net.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 16 | #include <linux/skbuff.h> |
| 17 | #include <linux/poll.h> |
| 18 | #include <linux/proc_fs.h> |
David Howells | 76181c1 | 2007-10-16 23:29:46 -0700 | [diff] [blame] | 19 | #include <linux/key-type.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 20 | #include <net/net_namespace.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 21 | #include <net/sock.h> |
| 22 | #include <net/af_rxrpc.h> |
| 23 | #include "ar-internal.h" |
| 24 | |
| 25 | MODULE_DESCRIPTION("RxRPC network protocol"); |
| 26 | MODULE_AUTHOR("Red Hat, Inc."); |
| 27 | MODULE_LICENSE("GPL"); |
| 28 | MODULE_ALIAS_NETPROTO(PF_RXRPC); |
| 29 | |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 30 | unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 31 | module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO); |
Paul Bolle | 424b00e | 2008-04-16 11:08:22 +0100 | [diff] [blame] | 32 | MODULE_PARM_DESC(debug, "RxRPC debugging mask"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 33 | |
| 34 | static int sysctl_rxrpc_max_qlen __read_mostly = 10; |
| 35 | |
| 36 | static struct proto rxrpc_proto; |
| 37 | static const struct proto_ops rxrpc_rpc_ops; |
| 38 | |
| 39 | /* local epoch for detecting local-end reset */ |
| 40 | __be32 rxrpc_epoch; |
| 41 | |
| 42 | /* current debugging ID */ |
| 43 | atomic_t rxrpc_debug_id; |
| 44 | |
| 45 | /* count of skbs currently in use */ |
| 46 | atomic_t rxrpc_n_skbs; |
| 47 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 48 | struct workqueue_struct *rxrpc_workqueue; |
| 49 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 50 | static void rxrpc_sock_destructor(struct sock *); |
| 51 | |
| 52 | /* |
| 53 | * see if an RxRPC socket is currently writable |
| 54 | */ |
| 55 | static inline int rxrpc_writable(struct sock *sk) |
| 56 | { |
| 57 | return atomic_read(&sk->sk_wmem_alloc) < (size_t) sk->sk_sndbuf; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * wait for write bufferage to become available |
| 62 | */ |
| 63 | static void rxrpc_write_space(struct sock *sk) |
| 64 | { |
| 65 | _enter("%p", sk); |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 66 | rcu_read_lock(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 67 | if (rxrpc_writable(sk)) { |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 68 | struct socket_wq *wq = rcu_dereference(sk->sk_wq); |
| 69 | |
| 70 | if (wq_has_sleeper(wq)) |
| 71 | wake_up_interruptible(&wq->wait); |
Pavel Emelyanov | 8d8ad9d | 2007-11-26 20:10:50 +0800 | [diff] [blame] | 72 | sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 73 | } |
Eric Dumazet | 4381548 | 2010-04-29 11:01:49 +0000 | [diff] [blame] | 74 | rcu_read_unlock(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /* |
| 78 | * validate an RxRPC address |
| 79 | */ |
| 80 | static int rxrpc_validate_address(struct rxrpc_sock *rx, |
| 81 | struct sockaddr_rxrpc *srx, |
| 82 | int len) |
| 83 | { |
| 84 | if (len < sizeof(struct sockaddr_rxrpc)) |
| 85 | return -EINVAL; |
| 86 | |
| 87 | if (srx->srx_family != AF_RXRPC) |
| 88 | return -EAFNOSUPPORT; |
| 89 | |
| 90 | if (srx->transport_type != SOCK_DGRAM) |
| 91 | return -ESOCKTNOSUPPORT; |
| 92 | |
| 93 | len -= offsetof(struct sockaddr_rxrpc, transport); |
| 94 | if (srx->transport_len < sizeof(sa_family_t) || |
| 95 | srx->transport_len > len) |
| 96 | return -EINVAL; |
| 97 | |
| 98 | if (srx->transport.family != rx->proto) |
| 99 | return -EAFNOSUPPORT; |
| 100 | |
| 101 | switch (srx->transport.family) { |
| 102 | case AF_INET: |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 103 | _debug("INET: %x @ %pI4", |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 104 | ntohs(srx->transport.sin.sin_port), |
Harvey Harrison | 21454aa | 2008-10-31 00:54:56 -0700 | [diff] [blame] | 105 | &srx->transport.sin.sin_addr); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 106 | if (srx->transport_len > 8) |
| 107 | memset((void *)&srx->transport + 8, 0, |
| 108 | srx->transport_len - 8); |
| 109 | break; |
| 110 | |
| 111 | case AF_INET6: |
| 112 | default: |
| 113 | return -EAFNOSUPPORT; |
| 114 | } |
| 115 | |
| 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * bind a local address to an RxRPC socket |
| 121 | */ |
| 122 | static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len) |
| 123 | { |
| 124 | struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) saddr; |
| 125 | struct sock *sk = sock->sk; |
| 126 | struct rxrpc_local *local; |
| 127 | struct rxrpc_sock *rx = rxrpc_sk(sk), *prx; |
| 128 | __be16 service_id; |
| 129 | int ret; |
| 130 | |
| 131 | _enter("%p,%p,%d", rx, saddr, len); |
| 132 | |
| 133 | ret = rxrpc_validate_address(rx, srx, len); |
| 134 | if (ret < 0) |
| 135 | goto error; |
| 136 | |
| 137 | lock_sock(&rx->sk); |
| 138 | |
| 139 | if (rx->sk.sk_state != RXRPC_UNCONNECTED) { |
| 140 | ret = -EINVAL; |
| 141 | goto error_unlock; |
| 142 | } |
| 143 | |
| 144 | memcpy(&rx->srx, srx, sizeof(rx->srx)); |
| 145 | |
| 146 | /* find a local transport endpoint if we don't have one already */ |
| 147 | local = rxrpc_lookup_local(&rx->srx); |
| 148 | if (IS_ERR(local)) { |
| 149 | ret = PTR_ERR(local); |
| 150 | goto error_unlock; |
| 151 | } |
| 152 | |
| 153 | rx->local = local; |
| 154 | if (srx->srx_service) { |
| 155 | service_id = htons(srx->srx_service); |
| 156 | write_lock_bh(&local->services_lock); |
| 157 | list_for_each_entry(prx, &local->services, listen_link) { |
| 158 | if (prx->service_id == service_id) |
| 159 | goto service_in_use; |
| 160 | } |
| 161 | |
| 162 | rx->service_id = service_id; |
| 163 | list_add_tail(&rx->listen_link, &local->services); |
| 164 | write_unlock_bh(&local->services_lock); |
| 165 | |
| 166 | rx->sk.sk_state = RXRPC_SERVER_BOUND; |
| 167 | } else { |
| 168 | rx->sk.sk_state = RXRPC_CLIENT_BOUND; |
| 169 | } |
| 170 | |
| 171 | release_sock(&rx->sk); |
| 172 | _leave(" = 0"); |
| 173 | return 0; |
| 174 | |
| 175 | service_in_use: |
| 176 | ret = -EADDRINUSE; |
| 177 | write_unlock_bh(&local->services_lock); |
| 178 | error_unlock: |
| 179 | release_sock(&rx->sk); |
| 180 | error: |
| 181 | _leave(" = %d", ret); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * set the number of pending calls permitted on a listening socket |
| 187 | */ |
| 188 | static int rxrpc_listen(struct socket *sock, int backlog) |
| 189 | { |
| 190 | struct sock *sk = sock->sk; |
| 191 | struct rxrpc_sock *rx = rxrpc_sk(sk); |
| 192 | int ret; |
| 193 | |
| 194 | _enter("%p,%d", rx, backlog); |
| 195 | |
| 196 | lock_sock(&rx->sk); |
| 197 | |
| 198 | switch (rx->sk.sk_state) { |
| 199 | case RXRPC_UNCONNECTED: |
| 200 | ret = -EADDRNOTAVAIL; |
| 201 | break; |
| 202 | case RXRPC_CLIENT_BOUND: |
| 203 | case RXRPC_CLIENT_CONNECTED: |
| 204 | default: |
| 205 | ret = -EBUSY; |
| 206 | break; |
| 207 | case RXRPC_SERVER_BOUND: |
| 208 | ASSERT(rx->local != NULL); |
| 209 | sk->sk_max_ack_backlog = backlog; |
| 210 | rx->sk.sk_state = RXRPC_SERVER_LISTENING; |
| 211 | ret = 0; |
| 212 | break; |
| 213 | } |
| 214 | |
| 215 | release_sock(&rx->sk); |
| 216 | _leave(" = %d", ret); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * find a transport by address |
| 222 | */ |
| 223 | static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock, |
| 224 | struct sockaddr *addr, |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 225 | int addr_len, int flags, |
| 226 | gfp_t gfp) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 227 | { |
| 228 | struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr; |
| 229 | struct rxrpc_transport *trans; |
| 230 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
| 231 | struct rxrpc_peer *peer; |
| 232 | |
| 233 | _enter("%p,%p,%d,%d", rx, addr, addr_len, flags); |
| 234 | |
| 235 | ASSERT(rx->local != NULL); |
| 236 | ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED); |
| 237 | |
| 238 | if (rx->srx.transport_type != srx->transport_type) |
| 239 | return ERR_PTR(-ESOCKTNOSUPPORT); |
| 240 | if (rx->srx.transport.family != srx->transport.family) |
| 241 | return ERR_PTR(-EAFNOSUPPORT); |
| 242 | |
| 243 | /* find a remote transport endpoint from the local one */ |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 244 | peer = rxrpc_get_peer(srx, gfp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 245 | if (IS_ERR(peer)) |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 246 | return ERR_CAST(peer); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 247 | |
| 248 | /* find a transport */ |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 249 | trans = rxrpc_get_transport(rx->local, peer, gfp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 250 | rxrpc_put_peer(peer); |
| 251 | _leave(" = %p", trans); |
| 252 | return trans; |
| 253 | } |
| 254 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 255 | /** |
| 256 | * rxrpc_kernel_begin_call - Allow a kernel service to begin a call |
| 257 | * @sock: The socket on which to make the call |
| 258 | * @srx: The address of the peer to contact (defaults to socket setting) |
| 259 | * @key: The security context to use (defaults to socket setting) |
| 260 | * @user_call_ID: The ID to use |
| 261 | * |
| 262 | * Allow a kernel service to begin a call on the nominated socket. This just |
| 263 | * sets up all the internal tracking structures and allocates connection and |
| 264 | * call IDs as appropriate. The call to be used is returned. |
| 265 | * |
| 266 | * The default socket destination address and security may be overridden by |
| 267 | * supplying @srx and @key. |
| 268 | */ |
| 269 | struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock, |
| 270 | struct sockaddr_rxrpc *srx, |
| 271 | struct key *key, |
| 272 | unsigned long user_call_ID, |
| 273 | gfp_t gfp) |
| 274 | { |
| 275 | struct rxrpc_conn_bundle *bundle; |
| 276 | struct rxrpc_transport *trans; |
| 277 | struct rxrpc_call *call; |
| 278 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
| 279 | __be16 service_id; |
| 280 | |
| 281 | _enter(",,%x,%lx", key_serial(key), user_call_ID); |
| 282 | |
| 283 | lock_sock(&rx->sk); |
| 284 | |
| 285 | if (srx) { |
| 286 | trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx, |
| 287 | sizeof(*srx), 0, gfp); |
| 288 | if (IS_ERR(trans)) { |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 289 | call = ERR_CAST(trans); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 290 | trans = NULL; |
David Howells | 15bde72 | 2009-02-06 21:50:52 -0800 | [diff] [blame] | 291 | goto out_notrans; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 292 | } |
| 293 | } else { |
| 294 | trans = rx->trans; |
| 295 | if (!trans) { |
| 296 | call = ERR_PTR(-ENOTCONN); |
David Howells | 15bde72 | 2009-02-06 21:50:52 -0800 | [diff] [blame] | 297 | goto out_notrans; |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 298 | } |
| 299 | atomic_inc(&trans->usage); |
| 300 | } |
| 301 | |
| 302 | service_id = rx->service_id; |
| 303 | if (srx) |
| 304 | service_id = htons(srx->srx_service); |
| 305 | |
| 306 | if (!key) |
| 307 | key = rx->key; |
| 308 | if (key && !key->payload.data) |
| 309 | key = NULL; /* a no-security key */ |
| 310 | |
| 311 | bundle = rxrpc_get_bundle(rx, trans, key, service_id, gfp); |
| 312 | if (IS_ERR(bundle)) { |
David Howells | e231c2e | 2008-02-07 00:15:26 -0800 | [diff] [blame] | 313 | call = ERR_CAST(bundle); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 314 | goto out; |
| 315 | } |
| 316 | |
| 317 | call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true, |
| 318 | gfp); |
| 319 | rxrpc_put_bundle(trans, bundle); |
| 320 | out: |
| 321 | rxrpc_put_transport(trans); |
David Howells | 15bde72 | 2009-02-06 21:50:52 -0800 | [diff] [blame] | 322 | out_notrans: |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 323 | release_sock(&rx->sk); |
| 324 | _leave(" = %p", call); |
| 325 | return call; |
| 326 | } |
| 327 | |
| 328 | EXPORT_SYMBOL(rxrpc_kernel_begin_call); |
| 329 | |
| 330 | /** |
| 331 | * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using |
| 332 | * @call: The call to end |
| 333 | * |
| 334 | * Allow a kernel service to end a call it was using. The call must be |
| 335 | * complete before this is called (the call should be aborted if necessary). |
| 336 | */ |
| 337 | void rxrpc_kernel_end_call(struct rxrpc_call *call) |
| 338 | { |
| 339 | _enter("%d{%d}", call->debug_id, atomic_read(&call->usage)); |
| 340 | rxrpc_remove_user_ID(call->socket, call); |
| 341 | rxrpc_put_call(call); |
| 342 | } |
| 343 | |
| 344 | EXPORT_SYMBOL(rxrpc_kernel_end_call); |
| 345 | |
| 346 | /** |
| 347 | * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages |
| 348 | * @sock: The socket to intercept received messages on |
| 349 | * @interceptor: The function to pass the messages to |
| 350 | * |
| 351 | * Allow a kernel service to intercept messages heading for the Rx queue on an |
| 352 | * RxRPC socket. They get passed to the specified function instead. |
| 353 | * @interceptor should free the socket buffers it is given. @interceptor is |
| 354 | * called with the socket receive queue spinlock held and softirqs disabled - |
| 355 | * this ensures that the messages will be delivered in the right order. |
| 356 | */ |
| 357 | void rxrpc_kernel_intercept_rx_messages(struct socket *sock, |
| 358 | rxrpc_interceptor_t interceptor) |
| 359 | { |
| 360 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
| 361 | |
| 362 | _enter(""); |
| 363 | rx->interceptor = interceptor; |
| 364 | } |
| 365 | |
| 366 | EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages); |
| 367 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 368 | /* |
| 369 | * connect an RxRPC socket |
| 370 | * - this just targets it at a specific destination; no actual connection |
| 371 | * negotiation takes place |
| 372 | */ |
| 373 | static int rxrpc_connect(struct socket *sock, struct sockaddr *addr, |
| 374 | int addr_len, int flags) |
| 375 | { |
| 376 | struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr; |
| 377 | struct sock *sk = sock->sk; |
| 378 | struct rxrpc_transport *trans; |
| 379 | struct rxrpc_local *local; |
| 380 | struct rxrpc_sock *rx = rxrpc_sk(sk); |
| 381 | int ret; |
| 382 | |
| 383 | _enter("%p,%p,%d,%d", rx, addr, addr_len, flags); |
| 384 | |
| 385 | ret = rxrpc_validate_address(rx, srx, addr_len); |
| 386 | if (ret < 0) { |
| 387 | _leave(" = %d [bad addr]", ret); |
| 388 | return ret; |
| 389 | } |
| 390 | |
| 391 | lock_sock(&rx->sk); |
| 392 | |
| 393 | switch (rx->sk.sk_state) { |
| 394 | case RXRPC_UNCONNECTED: |
| 395 | /* find a local transport endpoint if we don't have one already */ |
| 396 | ASSERTCMP(rx->local, ==, NULL); |
| 397 | rx->srx.srx_family = AF_RXRPC; |
| 398 | rx->srx.srx_service = 0; |
| 399 | rx->srx.transport_type = srx->transport_type; |
| 400 | rx->srx.transport_len = sizeof(sa_family_t); |
| 401 | rx->srx.transport.family = srx->transport.family; |
| 402 | local = rxrpc_lookup_local(&rx->srx); |
| 403 | if (IS_ERR(local)) { |
| 404 | release_sock(&rx->sk); |
| 405 | return PTR_ERR(local); |
| 406 | } |
| 407 | rx->local = local; |
| 408 | rx->sk.sk_state = RXRPC_CLIENT_BOUND; |
| 409 | case RXRPC_CLIENT_BOUND: |
| 410 | break; |
| 411 | case RXRPC_CLIENT_CONNECTED: |
| 412 | release_sock(&rx->sk); |
| 413 | return -EISCONN; |
| 414 | default: |
| 415 | release_sock(&rx->sk); |
| 416 | return -EBUSY; /* server sockets can't connect as well */ |
| 417 | } |
| 418 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 419 | trans = rxrpc_name_to_transport(sock, addr, addr_len, flags, |
| 420 | GFP_KERNEL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 421 | if (IS_ERR(trans)) { |
| 422 | release_sock(&rx->sk); |
| 423 | _leave(" = %ld", PTR_ERR(trans)); |
| 424 | return PTR_ERR(trans); |
| 425 | } |
| 426 | |
| 427 | rx->trans = trans; |
| 428 | rx->service_id = htons(srx->srx_service); |
| 429 | rx->sk.sk_state = RXRPC_CLIENT_CONNECTED; |
| 430 | |
| 431 | release_sock(&rx->sk); |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * send a message through an RxRPC socket |
| 437 | * - in a client this does a number of things: |
| 438 | * - finds/sets up a connection for the security specified (if any) |
| 439 | * - initiates a call (ID in control data) |
| 440 | * - ends the request phase of a call (if MSG_MORE is not set) |
| 441 | * - sends a call data packet |
| 442 | * - may send an abort (abort code in control data) |
| 443 | */ |
| 444 | static int rxrpc_sendmsg(struct kiocb *iocb, struct socket *sock, |
| 445 | struct msghdr *m, size_t len) |
| 446 | { |
| 447 | struct rxrpc_transport *trans; |
| 448 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
| 449 | int ret; |
| 450 | |
| 451 | _enter(",{%d},,%zu", rx->sk.sk_state, len); |
| 452 | |
| 453 | if (m->msg_flags & MSG_OOB) |
| 454 | return -EOPNOTSUPP; |
| 455 | |
| 456 | if (m->msg_name) { |
| 457 | ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen); |
| 458 | if (ret < 0) { |
| 459 | _leave(" = %d [bad addr]", ret); |
| 460 | return ret; |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | trans = NULL; |
| 465 | lock_sock(&rx->sk); |
| 466 | |
| 467 | if (m->msg_name) { |
| 468 | ret = -EISCONN; |
| 469 | trans = rxrpc_name_to_transport(sock, m->msg_name, |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 470 | m->msg_namelen, 0, GFP_KERNEL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 471 | if (IS_ERR(trans)) { |
| 472 | ret = PTR_ERR(trans); |
| 473 | trans = NULL; |
| 474 | goto out; |
| 475 | } |
| 476 | } else { |
| 477 | trans = rx->trans; |
| 478 | if (trans) |
| 479 | atomic_inc(&trans->usage); |
| 480 | } |
| 481 | |
| 482 | switch (rx->sk.sk_state) { |
| 483 | case RXRPC_SERVER_LISTENING: |
| 484 | if (!m->msg_name) { |
| 485 | ret = rxrpc_server_sendmsg(iocb, rx, m, len); |
| 486 | break; |
| 487 | } |
| 488 | case RXRPC_SERVER_BOUND: |
| 489 | case RXRPC_CLIENT_BOUND: |
| 490 | if (!m->msg_name) { |
| 491 | ret = -ENOTCONN; |
| 492 | break; |
| 493 | } |
| 494 | case RXRPC_CLIENT_CONNECTED: |
| 495 | ret = rxrpc_client_sendmsg(iocb, rx, trans, m, len); |
| 496 | break; |
| 497 | default: |
| 498 | ret = -ENOTCONN; |
| 499 | break; |
| 500 | } |
| 501 | |
| 502 | out: |
| 503 | release_sock(&rx->sk); |
| 504 | if (trans) |
| 505 | rxrpc_put_transport(trans); |
| 506 | _leave(" = %d", ret); |
| 507 | return ret; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * set RxRPC socket options |
| 512 | */ |
| 513 | static int rxrpc_setsockopt(struct socket *sock, int level, int optname, |
David S. Miller | b705884 | 2009-09-30 16:12:20 -0700 | [diff] [blame] | 514 | char __user *optval, unsigned int optlen) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 515 | { |
| 516 | struct rxrpc_sock *rx = rxrpc_sk(sock->sk); |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 517 | unsigned int min_sec_level; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 518 | int ret; |
| 519 | |
| 520 | _enter(",%d,%d,,%d", level, optname, optlen); |
| 521 | |
| 522 | lock_sock(&rx->sk); |
| 523 | ret = -EOPNOTSUPP; |
| 524 | |
| 525 | if (level == SOL_RXRPC) { |
| 526 | switch (optname) { |
| 527 | case RXRPC_EXCLUSIVE_CONNECTION: |
| 528 | ret = -EINVAL; |
| 529 | if (optlen != 0) |
| 530 | goto error; |
| 531 | ret = -EISCONN; |
| 532 | if (rx->sk.sk_state != RXRPC_UNCONNECTED) |
| 533 | goto error; |
| 534 | set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags); |
| 535 | goto success; |
| 536 | |
| 537 | case RXRPC_SECURITY_KEY: |
| 538 | ret = -EINVAL; |
| 539 | if (rx->key) |
| 540 | goto error; |
| 541 | ret = -EISCONN; |
| 542 | if (rx->sk.sk_state != RXRPC_UNCONNECTED) |
| 543 | goto error; |
| 544 | ret = rxrpc_request_key(rx, optval, optlen); |
| 545 | goto error; |
| 546 | |
| 547 | case RXRPC_SECURITY_KEYRING: |
| 548 | ret = -EINVAL; |
| 549 | if (rx->key) |
| 550 | goto error; |
| 551 | ret = -EISCONN; |
| 552 | if (rx->sk.sk_state != RXRPC_UNCONNECTED) |
| 553 | goto error; |
| 554 | ret = rxrpc_server_keyring(rx, optval, optlen); |
| 555 | goto error; |
| 556 | |
| 557 | case RXRPC_MIN_SECURITY_LEVEL: |
| 558 | ret = -EINVAL; |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 559 | if (optlen != sizeof(unsigned int)) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 560 | goto error; |
| 561 | ret = -EISCONN; |
| 562 | if (rx->sk.sk_state != RXRPC_UNCONNECTED) |
| 563 | goto error; |
| 564 | ret = get_user(min_sec_level, |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 565 | (unsigned int __user *) optval); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 566 | if (ret < 0) |
| 567 | goto error; |
| 568 | ret = -EINVAL; |
| 569 | if (min_sec_level > RXRPC_SECURITY_MAX) |
| 570 | goto error; |
| 571 | rx->min_sec_level = min_sec_level; |
| 572 | goto success; |
| 573 | |
| 574 | default: |
| 575 | break; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | success: |
| 580 | ret = 0; |
| 581 | error: |
| 582 | release_sock(&rx->sk); |
| 583 | return ret; |
| 584 | } |
| 585 | |
| 586 | /* |
| 587 | * permit an RxRPC socket to be polled |
| 588 | */ |
| 589 | static unsigned int rxrpc_poll(struct file *file, struct socket *sock, |
| 590 | poll_table *wait) |
| 591 | { |
| 592 | unsigned int mask; |
| 593 | struct sock *sk = sock->sk; |
| 594 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 595 | sock_poll_wait(file, sk_sleep(sk), wait); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 596 | mask = 0; |
| 597 | |
| 598 | /* the socket is readable if there are any messages waiting on the Rx |
| 599 | * queue */ |
| 600 | if (!skb_queue_empty(&sk->sk_receive_queue)) |
| 601 | mask |= POLLIN | POLLRDNORM; |
| 602 | |
| 603 | /* the socket is writable if there is space to add new data to the |
| 604 | * socket; there is no guarantee that any particular call in progress |
| 605 | * on the socket may have space in the Tx ACK window */ |
| 606 | if (rxrpc_writable(sk)) |
| 607 | mask |= POLLOUT | POLLWRNORM; |
| 608 | |
| 609 | return mask; |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * create an RxRPC socket |
| 614 | */ |
Eric Paris | 3f378b6 | 2009-11-05 22:18:14 -0800 | [diff] [blame] | 615 | static int rxrpc_create(struct net *net, struct socket *sock, int protocol, |
| 616 | int kern) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 617 | { |
| 618 | struct rxrpc_sock *rx; |
| 619 | struct sock *sk; |
| 620 | |
| 621 | _enter("%p,%d", sock, protocol); |
| 622 | |
Octavian Purdila | 09ad9bc | 2009-11-25 15:14:13 -0800 | [diff] [blame] | 623 | if (!net_eq(net, &init_net)) |
Eric W. Biederman | 1b8d7ae | 2007-10-08 23:24:22 -0700 | [diff] [blame] | 624 | return -EAFNOSUPPORT; |
| 625 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 626 | /* we support transport protocol UDP only */ |
| 627 | if (protocol != PF_INET) |
| 628 | return -EPROTONOSUPPORT; |
| 629 | |
| 630 | if (sock->type != SOCK_DGRAM) |
| 631 | return -ESOCKTNOSUPPORT; |
| 632 | |
| 633 | sock->ops = &rxrpc_rpc_ops; |
| 634 | sock->state = SS_UNCONNECTED; |
| 635 | |
Pavel Emelyanov | 6257ff2 | 2007-11-01 00:39:31 -0700 | [diff] [blame] | 636 | sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 637 | if (!sk) |
| 638 | return -ENOMEM; |
| 639 | |
| 640 | sock_init_data(sock, sk); |
| 641 | sk->sk_state = RXRPC_UNCONNECTED; |
| 642 | sk->sk_write_space = rxrpc_write_space; |
| 643 | sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen; |
| 644 | sk->sk_destruct = rxrpc_sock_destructor; |
| 645 | |
| 646 | rx = rxrpc_sk(sk); |
| 647 | rx->proto = protocol; |
| 648 | rx->calls = RB_ROOT; |
| 649 | |
| 650 | INIT_LIST_HEAD(&rx->listen_link); |
| 651 | INIT_LIST_HEAD(&rx->secureq); |
| 652 | INIT_LIST_HEAD(&rx->acceptq); |
| 653 | rwlock_init(&rx->call_lock); |
| 654 | memset(&rx->srx, 0, sizeof(rx->srx)); |
| 655 | |
| 656 | _leave(" = 0 [%p]", rx); |
| 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | /* |
| 661 | * RxRPC socket destructor |
| 662 | */ |
| 663 | static void rxrpc_sock_destructor(struct sock *sk) |
| 664 | { |
| 665 | _enter("%p", sk); |
| 666 | |
| 667 | rxrpc_purge_queue(&sk->sk_receive_queue); |
| 668 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 669 | WARN_ON(atomic_read(&sk->sk_wmem_alloc)); |
| 670 | WARN_ON(!sk_unhashed(sk)); |
| 671 | WARN_ON(sk->sk_socket); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 672 | |
| 673 | if (!sock_flag(sk, SOCK_DEAD)) { |
| 674 | printk("Attempt to release alive rxrpc socket: %p\n", sk); |
| 675 | return; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * release an RxRPC socket |
| 681 | */ |
| 682 | static int rxrpc_release_sock(struct sock *sk) |
| 683 | { |
| 684 | struct rxrpc_sock *rx = rxrpc_sk(sk); |
| 685 | |
| 686 | _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt)); |
| 687 | |
| 688 | /* declare the socket closed for business */ |
| 689 | sock_orphan(sk); |
| 690 | sk->sk_shutdown = SHUTDOWN_MASK; |
| 691 | |
| 692 | spin_lock_bh(&sk->sk_receive_queue.lock); |
| 693 | sk->sk_state = RXRPC_CLOSE; |
| 694 | spin_unlock_bh(&sk->sk_receive_queue.lock); |
| 695 | |
| 696 | ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1); |
| 697 | |
| 698 | if (!list_empty(&rx->listen_link)) { |
| 699 | write_lock_bh(&rx->local->services_lock); |
| 700 | list_del(&rx->listen_link); |
| 701 | write_unlock_bh(&rx->local->services_lock); |
| 702 | } |
| 703 | |
| 704 | /* try to flush out this socket */ |
| 705 | rxrpc_release_calls_on_socket(rx); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 706 | flush_workqueue(rxrpc_workqueue); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 707 | rxrpc_purge_queue(&sk->sk_receive_queue); |
| 708 | |
| 709 | if (rx->conn) { |
| 710 | rxrpc_put_connection(rx->conn); |
| 711 | rx->conn = NULL; |
| 712 | } |
| 713 | |
| 714 | if (rx->bundle) { |
| 715 | rxrpc_put_bundle(rx->trans, rx->bundle); |
| 716 | rx->bundle = NULL; |
| 717 | } |
| 718 | if (rx->trans) { |
| 719 | rxrpc_put_transport(rx->trans); |
| 720 | rx->trans = NULL; |
| 721 | } |
| 722 | if (rx->local) { |
| 723 | rxrpc_put_local(rx->local); |
| 724 | rx->local = NULL; |
| 725 | } |
| 726 | |
| 727 | key_put(rx->key); |
| 728 | rx->key = NULL; |
| 729 | key_put(rx->securities); |
| 730 | rx->securities = NULL; |
| 731 | sock_put(sk); |
| 732 | |
| 733 | _leave(" = 0"); |
| 734 | return 0; |
| 735 | } |
| 736 | |
| 737 | /* |
| 738 | * release an RxRPC BSD socket on close() or equivalent |
| 739 | */ |
| 740 | static int rxrpc_release(struct socket *sock) |
| 741 | { |
| 742 | struct sock *sk = sock->sk; |
| 743 | |
| 744 | _enter("%p{%p}", sock, sk); |
| 745 | |
| 746 | if (!sk) |
| 747 | return 0; |
| 748 | |
| 749 | sock->sk = NULL; |
| 750 | |
| 751 | return rxrpc_release_sock(sk); |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * RxRPC network protocol |
| 756 | */ |
| 757 | static const struct proto_ops rxrpc_rpc_ops = { |
| 758 | .family = PF_UNIX, |
| 759 | .owner = THIS_MODULE, |
| 760 | .release = rxrpc_release, |
| 761 | .bind = rxrpc_bind, |
| 762 | .connect = rxrpc_connect, |
| 763 | .socketpair = sock_no_socketpair, |
| 764 | .accept = sock_no_accept, |
| 765 | .getname = sock_no_getname, |
| 766 | .poll = rxrpc_poll, |
| 767 | .ioctl = sock_no_ioctl, |
| 768 | .listen = rxrpc_listen, |
| 769 | .shutdown = sock_no_shutdown, |
| 770 | .setsockopt = rxrpc_setsockopt, |
| 771 | .getsockopt = sock_no_getsockopt, |
| 772 | .sendmsg = rxrpc_sendmsg, |
| 773 | .recvmsg = rxrpc_recvmsg, |
| 774 | .mmap = sock_no_mmap, |
| 775 | .sendpage = sock_no_sendpage, |
| 776 | }; |
| 777 | |
| 778 | static struct proto rxrpc_proto = { |
| 779 | .name = "RXRPC", |
| 780 | .owner = THIS_MODULE, |
| 781 | .obj_size = sizeof(struct rxrpc_sock), |
| 782 | .max_header = sizeof(struct rxrpc_header), |
| 783 | }; |
| 784 | |
Stephen Hemminger | ec1b4cf | 2009-10-05 05:58:39 +0000 | [diff] [blame] | 785 | static const struct net_proto_family rxrpc_family_ops = { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 786 | .family = PF_RXRPC, |
| 787 | .create = rxrpc_create, |
| 788 | .owner = THIS_MODULE, |
| 789 | }; |
| 790 | |
| 791 | /* |
| 792 | * initialise and register the RxRPC protocol |
| 793 | */ |
| 794 | static int __init af_rxrpc_init(void) |
| 795 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 796 | int ret = -1; |
| 797 | |
YOSHIFUJI Hideaki / 吉藤英明 | ce6654c | 2013-01-09 07:20:01 +0000 | [diff] [blame] | 798 | BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb)); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 799 | |
john stultz | 2c6b47d | 2007-07-24 17:47:43 -0700 | [diff] [blame] | 800 | rxrpc_epoch = htonl(get_seconds()); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 801 | |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 802 | ret = -ENOMEM; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 803 | rxrpc_call_jar = kmem_cache_create( |
| 804 | "rxrpc_call_jar", sizeof(struct rxrpc_call), 0, |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 805 | SLAB_HWCACHE_ALIGN, NULL); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 806 | if (!rxrpc_call_jar) { |
| 807 | printk(KERN_NOTICE "RxRPC: Failed to allocate call jar\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 808 | goto error_call_jar; |
| 809 | } |
| 810 | |
Tejun Heo | e1fcc7e | 2011-01-14 15:56:31 +0000 | [diff] [blame] | 811 | rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 812 | if (!rxrpc_workqueue) { |
| 813 | printk(KERN_NOTICE "RxRPC: Failed to allocate work queue\n"); |
| 814 | goto error_work_queue; |
| 815 | } |
| 816 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 817 | ret = proto_register(&rxrpc_proto, 1); |
YOSHIFUJI Hideaki | 1c89964 | 2007-07-19 10:44:44 +0900 | [diff] [blame] | 818 | if (ret < 0) { |
| 819 | printk(KERN_CRIT "RxRPC: Cannot register protocol\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 820 | goto error_proto; |
| 821 | } |
| 822 | |
| 823 | ret = sock_register(&rxrpc_family_ops); |
| 824 | if (ret < 0) { |
YOSHIFUJI Hideaki | 1c89964 | 2007-07-19 10:44:44 +0900 | [diff] [blame] | 825 | printk(KERN_CRIT "RxRPC: Cannot register socket family\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 826 | goto error_sock; |
| 827 | } |
| 828 | |
| 829 | ret = register_key_type(&key_type_rxrpc); |
| 830 | if (ret < 0) { |
YOSHIFUJI Hideaki | 1c89964 | 2007-07-19 10:44:44 +0900 | [diff] [blame] | 831 | printk(KERN_CRIT "RxRPC: Cannot register client key type\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 832 | goto error_key_type; |
| 833 | } |
| 834 | |
| 835 | ret = register_key_type(&key_type_rxrpc_s); |
| 836 | if (ret < 0) { |
YOSHIFUJI Hideaki | 1c89964 | 2007-07-19 10:44:44 +0900 | [diff] [blame] | 837 | printk(KERN_CRIT "RxRPC: Cannot register server key type\n"); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 838 | goto error_key_type_s; |
| 839 | } |
| 840 | |
| 841 | #ifdef CONFIG_PROC_FS |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 842 | proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops); |
| 843 | proc_create("rxrpc_conns", 0, init_net.proc_net, |
| 844 | &rxrpc_connection_seq_fops); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 845 | #endif |
| 846 | return 0; |
| 847 | |
| 848 | error_key_type_s: |
| 849 | unregister_key_type(&key_type_rxrpc); |
| 850 | error_key_type: |
| 851 | sock_unregister(PF_RXRPC); |
| 852 | error_sock: |
| 853 | proto_unregister(&rxrpc_proto); |
| 854 | error_proto: |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 855 | destroy_workqueue(rxrpc_workqueue); |
| 856 | error_work_queue: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 857 | kmem_cache_destroy(rxrpc_call_jar); |
| 858 | error_call_jar: |
| 859 | return ret; |
| 860 | } |
| 861 | |
| 862 | /* |
| 863 | * unregister the RxRPC protocol |
| 864 | */ |
| 865 | static void __exit af_rxrpc_exit(void) |
| 866 | { |
| 867 | _enter(""); |
| 868 | unregister_key_type(&key_type_rxrpc_s); |
| 869 | unregister_key_type(&key_type_rxrpc); |
| 870 | sock_unregister(PF_RXRPC); |
| 871 | proto_unregister(&rxrpc_proto); |
| 872 | rxrpc_destroy_all_calls(); |
| 873 | rxrpc_destroy_all_connections(); |
| 874 | rxrpc_destroy_all_transports(); |
| 875 | rxrpc_destroy_all_peers(); |
| 876 | rxrpc_destroy_all_locals(); |
| 877 | |
| 878 | ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0); |
| 879 | |
| 880 | _debug("flush scheduled work"); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 881 | flush_workqueue(rxrpc_workqueue); |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 882 | remove_proc_entry("rxrpc_conns", init_net.proc_net); |
| 883 | remove_proc_entry("rxrpc_calls", init_net.proc_net); |
David Howells | 651350d | 2007-04-26 15:50:17 -0700 | [diff] [blame] | 884 | destroy_workqueue(rxrpc_workqueue); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 885 | kmem_cache_destroy(rxrpc_call_jar); |
| 886 | _leave(""); |
| 887 | } |
| 888 | |
| 889 | module_init(af_rxrpc_init); |
| 890 | module_exit(af_rxrpc_exit); |