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