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