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