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