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