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