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