blob: 199b46e93e64ee7786e8a8d441ba5eb5b02bf31f [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* 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 Perches9b6d5392016-06-02 12:08:52 -070012#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
David Howells17926a72007-04-26 15:48:28 -070014#include <linux/module.h>
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +000015#include <linux/kernel.h>
David Howells17926a72007-04-26 15:48:28 -070016#include <linux/net.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
David Howells17926a72007-04-26 15:48:28 -070018#include <linux/skbuff.h>
David Howells5f2d9c42016-09-02 22:39:45 +010019#include <linux/random.h>
David Howells17926a72007-04-26 15:48:28 -070020#include <linux/poll.h>
21#include <linux/proc_fs.h>
David Howells76181c12007-10-16 23:29:46 -070022#include <linux/key-type.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020023#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070024#include <net/sock.h>
25#include <net/af_rxrpc.h>
David Howellsdf844fd2016-08-23 15:27:24 +010026#define CREATE_TRACE_POINTS
David Howells17926a72007-04-26 15:48:28 -070027#include "ar-internal.h"
28
29MODULE_DESCRIPTION("RxRPC network protocol");
30MODULE_AUTHOR("Red Hat, Inc.");
31MODULE_LICENSE("GPL");
32MODULE_ALIAS_NETPROTO(PF_RXRPC);
33
Eric Dumazet95c96172012-04-15 05:58:06 +000034unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
David Howells17926a72007-04-26 15:48:28 -070035module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010036MODULE_PARM_DESC(debug, "RxRPC debugging mask");
David Howells17926a72007-04-26 15:48:28 -070037
David Howells17926a72007-04-26 15:48:28 -070038static struct proto rxrpc_proto;
39static const struct proto_ops rxrpc_rpc_ops;
40
41/* local epoch for detecting local-end reset */
David Howells0d12f8a2016-03-04 15:53:46 +000042u32 rxrpc_epoch;
David Howells17926a72007-04-26 15:48:28 -070043
44/* current debugging ID */
45atomic_t rxrpc_debug_id;
46
47/* count of skbs currently in use */
David Howells71f3ca42016-09-17 10:49:14 +010048atomic_t rxrpc_n_tx_skbs, rxrpc_n_rx_skbs;
David Howells17926a72007-04-26 15:48:28 -070049
David Howells651350d2007-04-26 15:50:17 -070050struct workqueue_struct *rxrpc_workqueue;
51
David Howells17926a72007-04-26 15:48:28 -070052static void rxrpc_sock_destructor(struct sock *);
53
54/*
55 * see if an RxRPC socket is currently writable
56 */
57static 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 */
65static void rxrpc_write_space(struct sock *sk)
66{
67 _enter("%p", sk);
Eric Dumazet43815482010-04-29 11:01:49 +000068 rcu_read_lock();
David Howells17926a72007-04-26 15:48:28 -070069 if (rxrpc_writable(sk)) {
Eric Dumazet43815482010-04-29 11:01:49 +000070 struct socket_wq *wq = rcu_dereference(sk->sk_wq);
71
Herbert Xu1ce0bf52015-11-26 13:55:39 +080072 if (skwq_has_sleeper(wq))
Eric Dumazet43815482010-04-29 11:01:49 +000073 wake_up_interruptible(&wq->wait);
Pavel Emelyanov8d8ad9d2007-11-26 20:10:50 +080074 sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT);
David Howells17926a72007-04-26 15:48:28 -070075 }
Eric Dumazet43815482010-04-29 11:01:49 +000076 rcu_read_unlock();
David Howells17926a72007-04-26 15:48:28 -070077}
78
79/*
80 * validate an RxRPC address
81 */
82static int rxrpc_validate_address(struct rxrpc_sock *rx,
83 struct sockaddr_rxrpc *srx,
84 int len)
85{
David Howellsdad8aff2016-03-09 23:22:56 +000086 unsigned int tail;
David Howellsab802ee2016-03-04 15:59:49 +000087
David Howells17926a72007-04-26 15:48:28 -070088 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 Howells19ffa012016-04-04 14:00:36 +0100102 if (srx->transport.family != rx->family)
David Howells17926a72007-04-26 15:48:28 -0700103 return -EAFNOSUPPORT;
104
105 switch (srx->transport.family) {
106 case AF_INET:
David Howells4f95dd72016-04-04 14:00:35 +0100107 if (srx->transport_len < sizeof(struct sockaddr_in))
108 return -EINVAL;
David Howellsab802ee2016-03-04 15:59:49 +0000109 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
David Howells17926a72007-04-26 15:48:28 -0700110 break;
111
David Howellsd1912742016-09-17 07:26:01 +0100112#ifdef CONFIG_AF_RXRPC_IPV6
David Howells17926a72007-04-26 15:48:28 -0700113 case AF_INET6:
David Howells75b54cb2016-09-13 08:49:05 +0100114 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 Howellsd1912742016-09-17 07:26:01 +0100119#endif
David Howells75b54cb2016-09-13 08:49:05 +0100120
David Howells17926a72007-04-26 15:48:28 -0700121 default:
122 return -EAFNOSUPPORT;
123 }
124
David Howellsab802ee2016-03-04 15:59:49 +0000125 if (tail < len)
126 memset((void *)srx + tail, 0, len - tail);
David Howells75b54cb2016-09-13 08:49:05 +0100127 _debug("INET: %pISp", &srx->transport);
David Howells17926a72007-04-26 15:48:28 -0700128 return 0;
129}
130
131/*
132 * bind a local address to an RxRPC socket
133 */
134static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
135{
David Howellsb4f13422016-03-04 15:56:19 +0000136 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
David Howells17926a72007-04-26 15:48:28 -0700137 struct sock *sk = sock->sk;
138 struct rxrpc_local *local;
David Howells1e9e5c92016-09-29 22:37:15 +0100139 struct rxrpc_sock *rx = rxrpc_sk(sk);
140 u16 service_id = srx->srx_service;
David Howells17926a72007-04-26 15:48:28 -0700141 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 Howells2341e072016-06-09 23:02:51 +0100151 if (rx->sk.sk_state != RXRPC_UNBOUND) {
David Howells17926a72007-04-26 15:48:28 -0700152 ret = -EINVAL;
153 goto error_unlock;
154 }
155
156 memcpy(&rx->srx, srx, sizeof(rx->srx));
157
David Howells17926a72007-04-26 15:48:28 -0700158 local = rxrpc_lookup_local(&rx->srx);
159 if (IS_ERR(local)) {
160 ret = PTR_ERR(local);
161 goto error_unlock;
162 }
163
David Howells1e9e5c92016-09-29 22:37:15 +0100164 if (service_id) {
David Howells248f2192016-09-08 11:10:12 +0100165 write_lock(&local->services_lock);
David Howells1e9e5c92016-09-29 22:37:15 +0100166 if (rcu_access_pointer(local->service))
167 goto service_in_use;
David Howells2341e072016-06-09 23:02:51 +0100168 rx->local = local;
David Howells1e9e5c92016-09-29 22:37:15 +0100169 rcu_assign_pointer(local->service, rx);
David Howells248f2192016-09-08 11:10:12 +0100170 write_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700171
172 rx->sk.sk_state = RXRPC_SERVER_BOUND;
173 } else {
David Howells2341e072016-06-09 23:02:51 +0100174 rx->local = local;
David Howells17926a72007-04-26 15:48:28 -0700175 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
176 }
177
178 release_sock(&rx->sk);
179 _leave(" = 0");
180 return 0;
181
182service_in_use:
David Howells248f2192016-09-08 11:10:12 +0100183 write_unlock(&local->services_lock);
David Howells2341e072016-06-09 23:02:51 +0100184 rxrpc_put_local(local);
185 ret = -EADDRINUSE;
David Howells17926a72007-04-26 15:48:28 -0700186error_unlock:
187 release_sock(&rx->sk);
188error:
189 _leave(" = %d", ret);
190 return ret;
191}
192
193/*
194 * set the number of pending calls permitted on a listening socket
195 */
196static int rxrpc_listen(struct socket *sock, int backlog)
197{
198 struct sock *sk = sock->sk;
199 struct rxrpc_sock *rx = rxrpc_sk(sk);
David Howells00e90712016-09-08 11:10:12 +0100200 unsigned int max, old;
David Howells17926a72007-04-26 15:48:28 -0700201 int ret;
202
203 _enter("%p,%d", rx, backlog);
204
205 lock_sock(&rx->sk);
206
207 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100208 case RXRPC_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700209 ret = -EADDRNOTAVAIL;
210 break;
David Howells17926a72007-04-26 15:48:28 -0700211 case RXRPC_SERVER_BOUND:
212 ASSERT(rx->local != NULL);
David Howells0e119b42016-06-10 22:30:37 +0100213 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 Howells00e90712016-09-08 11:10:12 +0100219 old = sk->sk_max_ack_backlog;
David Howells17926a72007-04-26 15:48:28 -0700220 sk->sk_max_ack_backlog = backlog;
David Howells00e90712016-09-08 11:10:12 +0100221 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 Howells17926a72007-04-26 15:48:28 -0700226 break;
David Howells210f0352017-01-05 10:38:36 +0000227 case RXRPC_SERVER_LISTENING:
228 if (backlog == 0) {
229 rx->sk.sk_state = RXRPC_SERVER_LISTEN_DISABLED;
230 sk->sk_max_ack_backlog = 0;
231 rxrpc_discard_prealloc(rx);
232 ret = 0;
233 break;
234 }
David Howells0e119b42016-06-10 22:30:37 +0100235 default:
236 ret = -EBUSY;
237 break;
David Howells17926a72007-04-26 15:48:28 -0700238 }
239
240 release_sock(&rx->sk);
241 _leave(" = %d", ret);
242 return ret;
243}
244
David Howells651350d2007-04-26 15:50:17 -0700245/**
246 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
247 * @sock: The socket on which to make the call
David Howells2341e072016-06-09 23:02:51 +0100248 * @srx: The address of the peer to contact
David Howells651350d2007-04-26 15:50:17 -0700249 * @key: The security context to use (defaults to socket setting)
250 * @user_call_ID: The ID to use
David Howellsd0016482016-08-30 20:42:14 +0100251 * @gfp: The allocation constraints
252 * @notify_rx: Where to send notifications instead of socket queue
David Howells651350d2007-04-26 15:50:17 -0700253 *
254 * Allow a kernel service to begin a call on the nominated socket. This just
255 * sets up all the internal tracking structures and allocates connection and
256 * call IDs as appropriate. The call to be used is returned.
257 *
258 * The default socket destination address and security may be overridden by
259 * supplying @srx and @key.
260 */
261struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
262 struct sockaddr_rxrpc *srx,
263 struct key *key,
264 unsigned long user_call_ID,
David Howellsd0016482016-08-30 20:42:14 +0100265 gfp_t gfp,
266 rxrpc_notify_rx_t notify_rx)
David Howells651350d2007-04-26 15:50:17 -0700267{
David Howells19ffa012016-04-04 14:00:36 +0100268 struct rxrpc_conn_parameters cp;
David Howells651350d2007-04-26 15:50:17 -0700269 struct rxrpc_call *call;
270 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howellsf4552c22016-06-17 11:00:48 +0100271 int ret;
David Howells651350d2007-04-26 15:50:17 -0700272
273 _enter(",,%x,%lx", key_serial(key), user_call_ID);
274
David Howellsf4552c22016-06-17 11:00:48 +0100275 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
276 if (ret < 0)
277 return ERR_PTR(ret);
278
David Howells651350d2007-04-26 15:50:17 -0700279 lock_sock(&rx->sk);
280
David Howells19ffa012016-04-04 14:00:36 +0100281 if (!key)
282 key = rx->key;
283 if (key && !key->payload.data[0])
284 key = NULL; /* a no-security key */
285
286 memset(&cp, 0, sizeof(cp));
287 cp.local = rx->local;
288 cp.key = key;
289 cp.security_level = 0;
290 cp.exclusive = false;
291 cp.service_id = srx->srx_service;
David Howellsaa390bb2016-06-17 10:06:56 +0100292 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
David Howellsd0016482016-08-30 20:42:14 +0100293 if (!IS_ERR(call))
294 call->notify_rx = notify_rx;
David Howells19ffa012016-04-04 14:00:36 +0100295
David Howells651350d2007-04-26 15:50:17 -0700296 release_sock(&rx->sk);
297 _leave(" = %p", call);
298 return call;
299}
David Howells651350d2007-04-26 15:50:17 -0700300EXPORT_SYMBOL(rxrpc_kernel_begin_call);
301
302/**
303 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
David Howells4de48af2016-08-30 12:00:48 +0100304 * @sock: The socket the call is on
David Howells651350d2007-04-26 15:50:17 -0700305 * @call: The call to end
306 *
307 * Allow a kernel service to end a call it was using. The call must be
308 * complete before this is called (the call should be aborted if necessary).
309 */
David Howells4de48af2016-08-30 12:00:48 +0100310void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
David Howells651350d2007-04-26 15:50:17 -0700311{
312 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
David Howells8d94aa32016-09-07 09:19:31 +0100313 rxrpc_release_call(rxrpc_sk(sock->sk), call);
David Howellscbd00892016-09-13 09:12:34 +0100314 rxrpc_put_call(call, rxrpc_call_put_kernel);
David Howells651350d2007-04-26 15:50:17 -0700315}
David Howells651350d2007-04-26 15:50:17 -0700316EXPORT_SYMBOL(rxrpc_kernel_end_call);
317
318/**
David Howellsd0016482016-08-30 20:42:14 +0100319 * rxrpc_kernel_new_call_notification - Get notifications of new calls
David Howells651350d2007-04-26 15:50:17 -0700320 * @sock: The socket to intercept received messages on
David Howellsd0016482016-08-30 20:42:14 +0100321 * @notify_new_call: Function to be called when new calls appear
David Howells00e90712016-09-08 11:10:12 +0100322 * @discard_new_call: Function to discard preallocated calls
David Howells651350d2007-04-26 15:50:17 -0700323 *
David Howellsd0016482016-08-30 20:42:14 +0100324 * Allow a kernel service to be given notifications about new calls.
David Howells651350d2007-04-26 15:50:17 -0700325 */
David Howellsd0016482016-08-30 20:42:14 +0100326void rxrpc_kernel_new_call_notification(
327 struct socket *sock,
David Howells00e90712016-09-08 11:10:12 +0100328 rxrpc_notify_new_call_t notify_new_call,
329 rxrpc_discard_new_call_t discard_new_call)
David Howells651350d2007-04-26 15:50:17 -0700330{
331 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
332
David Howellsd0016482016-08-30 20:42:14 +0100333 rx->notify_new_call = notify_new_call;
David Howells00e90712016-09-08 11:10:12 +0100334 rx->discard_new_call = discard_new_call;
David Howells651350d2007-04-26 15:50:17 -0700335}
David Howellsd0016482016-08-30 20:42:14 +0100336EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
David Howells651350d2007-04-26 15:50:17 -0700337
David Howells17926a72007-04-26 15:48:28 -0700338/*
339 * connect an RxRPC socket
340 * - this just targets it at a specific destination; no actual connection
341 * negotiation takes place
342 */
343static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
344 int addr_len, int flags)
345{
David Howells2341e072016-06-09 23:02:51 +0100346 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
347 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells17926a72007-04-26 15:48:28 -0700348 int ret;
349
350 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
351
352 ret = rxrpc_validate_address(rx, srx, addr_len);
353 if (ret < 0) {
354 _leave(" = %d [bad addr]", ret);
355 return ret;
356 }
357
358 lock_sock(&rx->sk);
359
David Howells2341e072016-06-09 23:02:51 +0100360 ret = -EISCONN;
361 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
362 goto error;
363
David Howells17926a72007-04-26 15:48:28 -0700364 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100365 case RXRPC_UNBOUND:
366 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
367 case RXRPC_CLIENT_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700368 case RXRPC_CLIENT_BOUND:
369 break;
David Howells17926a72007-04-26 15:48:28 -0700370 default:
David Howells2341e072016-06-09 23:02:51 +0100371 ret = -EBUSY;
372 goto error;
David Howells17926a72007-04-26 15:48:28 -0700373 }
374
David Howells2341e072016-06-09 23:02:51 +0100375 rx->connect_srx = *srx;
376 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
377 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700378
David Howells2341e072016-06-09 23:02:51 +0100379error:
David Howells17926a72007-04-26 15:48:28 -0700380 release_sock(&rx->sk);
David Howells2341e072016-06-09 23:02:51 +0100381 return ret;
David Howells17926a72007-04-26 15:48:28 -0700382}
383
384/*
385 * send a message through an RxRPC socket
386 * - in a client this does a number of things:
387 * - finds/sets up a connection for the security specified (if any)
388 * - initiates a call (ID in control data)
389 * - ends the request phase of a call (if MSG_MORE is not set)
390 * - sends a call data packet
391 * - may send an abort (abort code in control data)
392 */
Ying Xue1b784142015-03-02 15:37:48 +0800393static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700394{
David Howells2341e072016-06-09 23:02:51 +0100395 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700396 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
397 int ret;
398
399 _enter(",{%d},,%zu", rx->sk.sk_state, len);
400
401 if (m->msg_flags & MSG_OOB)
402 return -EOPNOTSUPP;
403
404 if (m->msg_name) {
405 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
406 if (ret < 0) {
407 _leave(" = %d [bad addr]", ret);
408 return ret;
409 }
410 }
411
David Howells17926a72007-04-26 15:48:28 -0700412 lock_sock(&rx->sk);
413
David Howells17926a72007-04-26 15:48:28 -0700414 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100415 case RXRPC_UNBOUND:
David Howellscd5892c2016-09-13 08:49:05 +0100416 rx->srx.srx_family = AF_RXRPC;
417 rx->srx.srx_service = 0;
418 rx->srx.transport_type = SOCK_DGRAM;
419 rx->srx.transport.family = rx->family;
420 switch (rx->family) {
421 case AF_INET:
422 rx->srx.transport_len = sizeof(struct sockaddr_in);
423 break;
David Howellsd1912742016-09-17 07:26:01 +0100424#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100425 case AF_INET6:
426 rx->srx.transport_len = sizeof(struct sockaddr_in6);
427 break;
David Howellsd1912742016-09-17 07:26:01 +0100428#endif
David Howellscd5892c2016-09-13 08:49:05 +0100429 default:
430 ret = -EAFNOSUPPORT;
431 goto error_unlock;
432 }
David Howells2341e072016-06-09 23:02:51 +0100433 local = rxrpc_lookup_local(&rx->srx);
434 if (IS_ERR(local)) {
435 ret = PTR_ERR(local);
436 goto error_unlock;
437 }
438
439 rx->local = local;
440 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
441 /* Fall through */
442
443 case RXRPC_CLIENT_UNBOUND:
444 case RXRPC_CLIENT_BOUND:
445 if (!m->msg_name &&
446 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
447 m->msg_name = &rx->connect_srx;
448 m->msg_namelen = sizeof(rx->connect_srx);
David Howells17926a72007-04-26 15:48:28 -0700449 }
450 case RXRPC_SERVER_BOUND:
David Howells2341e072016-06-09 23:02:51 +0100451 case RXRPC_SERVER_LISTENING:
452 ret = rxrpc_do_sendmsg(rx, m, len);
David Howells17926a72007-04-26 15:48:28 -0700453 break;
454 default:
David Howells2341e072016-06-09 23:02:51 +0100455 ret = -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700456 break;
457 }
458
David Howells2341e072016-06-09 23:02:51 +0100459error_unlock:
David Howells17926a72007-04-26 15:48:28 -0700460 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700461 _leave(" = %d", ret);
462 return ret;
463}
464
465/*
466 * set RxRPC socket options
467 */
468static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700469 char __user *optval, unsigned int optlen)
David Howells17926a72007-04-26 15:48:28 -0700470{
471 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000472 unsigned int min_sec_level;
David Howells17926a72007-04-26 15:48:28 -0700473 int ret;
474
475 _enter(",%d,%d,,%d", level, optname, optlen);
476
477 lock_sock(&rx->sk);
478 ret = -EOPNOTSUPP;
479
480 if (level == SOL_RXRPC) {
481 switch (optname) {
482 case RXRPC_EXCLUSIVE_CONNECTION:
483 ret = -EINVAL;
484 if (optlen != 0)
485 goto error;
486 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100487 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700488 goto error;
David Howellscc8feb82016-04-04 14:00:37 +0100489 rx->exclusive = true;
David Howells17926a72007-04-26 15:48:28 -0700490 goto success;
491
492 case RXRPC_SECURITY_KEY:
493 ret = -EINVAL;
494 if (rx->key)
495 goto error;
496 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100497 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700498 goto error;
499 ret = rxrpc_request_key(rx, optval, optlen);
500 goto error;
501
502 case RXRPC_SECURITY_KEYRING:
503 ret = -EINVAL;
504 if (rx->key)
505 goto error;
506 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100507 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700508 goto error;
509 ret = rxrpc_server_keyring(rx, optval, optlen);
510 goto error;
511
512 case RXRPC_MIN_SECURITY_LEVEL:
513 ret = -EINVAL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000514 if (optlen != sizeof(unsigned int))
David Howells17926a72007-04-26 15:48:28 -0700515 goto error;
516 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100517 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700518 goto error;
519 ret = get_user(min_sec_level,
Eric Dumazet95c96172012-04-15 05:58:06 +0000520 (unsigned int __user *) optval);
David Howells17926a72007-04-26 15:48:28 -0700521 if (ret < 0)
522 goto error;
523 ret = -EINVAL;
524 if (min_sec_level > RXRPC_SECURITY_MAX)
525 goto error;
526 rx->min_sec_level = min_sec_level;
527 goto success;
528
529 default:
530 break;
531 }
532 }
533
534success:
535 ret = 0;
536error:
537 release_sock(&rx->sk);
538 return ret;
539}
540
541/*
542 * permit an RxRPC socket to be polled
543 */
544static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
545 poll_table *wait)
546{
David Howells17926a72007-04-26 15:48:28 -0700547 struct sock *sk = sock->sk;
David Howells248f2192016-09-08 11:10:12 +0100548 struct rxrpc_sock *rx = rxrpc_sk(sk);
549 unsigned int mask;
David Howells17926a72007-04-26 15:48:28 -0700550
Eric Dumazetaa395142010-04-20 13:03:51 +0000551 sock_poll_wait(file, sk_sleep(sk), wait);
David Howells17926a72007-04-26 15:48:28 -0700552 mask = 0;
553
554 /* the socket is readable if there are any messages waiting on the Rx
555 * queue */
David Howells248f2192016-09-08 11:10:12 +0100556 if (!list_empty(&rx->recvmsg_q))
David Howells17926a72007-04-26 15:48:28 -0700557 mask |= POLLIN | POLLRDNORM;
558
559 /* the socket is writable if there is space to add new data to the
560 * socket; there is no guarantee that any particular call in progress
561 * on the socket may have space in the Tx ACK window */
562 if (rxrpc_writable(sk))
563 mask |= POLLOUT | POLLWRNORM;
564
565 return mask;
566}
567
568/*
569 * create an RxRPC socket
570 */
Eric Paris3f378b62009-11-05 22:18:14 -0800571static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
572 int kern)
David Howells17926a72007-04-26 15:48:28 -0700573{
574 struct rxrpc_sock *rx;
575 struct sock *sk;
576
577 _enter("%p,%d", sock, protocol);
578
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800579 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700580 return -EAFNOSUPPORT;
581
David Howellsb4f13422016-03-04 15:56:19 +0000582 /* we support transport protocol UDP/UDP6 only */
David Howellsd1912742016-09-17 07:26:01 +0100583 if (protocol != PF_INET &&
584 IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
David Howells17926a72007-04-26 15:48:28 -0700585 return -EPROTONOSUPPORT;
586
587 if (sock->type != SOCK_DGRAM)
588 return -ESOCKTNOSUPPORT;
589
590 sock->ops = &rxrpc_rpc_ops;
591 sock->state = SS_UNCONNECTED;
592
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500593 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
David Howells17926a72007-04-26 15:48:28 -0700594 if (!sk)
595 return -ENOMEM;
596
597 sock_init_data(sock, sk);
David Howells8d94aa32016-09-07 09:19:31 +0100598 sock_set_flag(sk, SOCK_RCU_FREE);
David Howells2341e072016-06-09 23:02:51 +0100599 sk->sk_state = RXRPC_UNBOUND;
David Howells17926a72007-04-26 15:48:28 -0700600 sk->sk_write_space = rxrpc_write_space;
David Howells0e119b42016-06-10 22:30:37 +0100601 sk->sk_max_ack_backlog = 0;
David Howells17926a72007-04-26 15:48:28 -0700602 sk->sk_destruct = rxrpc_sock_destructor;
603
604 rx = rxrpc_sk(sk);
David Howells19ffa012016-04-04 14:00:36 +0100605 rx->family = protocol;
David Howells17926a72007-04-26 15:48:28 -0700606 rx->calls = RB_ROOT;
607
David Howells248f2192016-09-08 11:10:12 +0100608 spin_lock_init(&rx->incoming_lock);
609 INIT_LIST_HEAD(&rx->sock_calls);
610 INIT_LIST_HEAD(&rx->to_be_accepted);
611 INIT_LIST_HEAD(&rx->recvmsg_q);
612 rwlock_init(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700613 rwlock_init(&rx->call_lock);
614 memset(&rx->srx, 0, sizeof(rx->srx));
615
616 _leave(" = 0 [%p]", rx);
617 return 0;
618}
619
620/*
David Howells248f2192016-09-08 11:10:12 +0100621 * Kill all the calls on a socket and shut it down.
622 */
623static int rxrpc_shutdown(struct socket *sock, int flags)
624{
625 struct sock *sk = sock->sk;
626 struct rxrpc_sock *rx = rxrpc_sk(sk);
627 int ret = 0;
628
629 _enter("%p,%d", sk, flags);
630
631 if (flags != SHUT_RDWR)
632 return -EOPNOTSUPP;
633 if (sk->sk_state == RXRPC_CLOSE)
634 return -ESHUTDOWN;
635
636 lock_sock(sk);
637
638 spin_lock_bh(&sk->sk_receive_queue.lock);
639 if (sk->sk_state < RXRPC_CLOSE) {
640 sk->sk_state = RXRPC_CLOSE;
641 sk->sk_shutdown = SHUTDOWN_MASK;
642 } else {
643 ret = -ESHUTDOWN;
644 }
645 spin_unlock_bh(&sk->sk_receive_queue.lock);
646
647 rxrpc_discard_prealloc(rx);
648
649 release_sock(sk);
650 return ret;
651}
652
653/*
David Howells17926a72007-04-26 15:48:28 -0700654 * RxRPC socket destructor
655 */
656static void rxrpc_sock_destructor(struct sock *sk)
657{
658 _enter("%p", sk);
659
660 rxrpc_purge_queue(&sk->sk_receive_queue);
661
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700662 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
663 WARN_ON(!sk_unhashed(sk));
664 WARN_ON(sk->sk_socket);
David Howells17926a72007-04-26 15:48:28 -0700665
666 if (!sock_flag(sk, SOCK_DEAD)) {
667 printk("Attempt to release alive rxrpc socket: %p\n", sk);
668 return;
669 }
670}
671
672/*
673 * release an RxRPC socket
674 */
675static int rxrpc_release_sock(struct sock *sk)
676{
677 struct rxrpc_sock *rx = rxrpc_sk(sk);
678
679 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
680
681 /* declare the socket closed for business */
682 sock_orphan(sk);
683 sk->sk_shutdown = SHUTDOWN_MASK;
684
685 spin_lock_bh(&sk->sk_receive_queue.lock);
686 sk->sk_state = RXRPC_CLOSE;
687 spin_unlock_bh(&sk->sk_receive_queue.lock);
688
David Howellsb63452c2016-10-06 08:11:48 +0100689 if (rx->local && rcu_access_pointer(rx->local->service) == rx) {
David Howells248f2192016-09-08 11:10:12 +0100690 write_lock(&rx->local->services_lock);
David Howellsb63452c2016-10-06 08:11:48 +0100691 rcu_assign_pointer(rx->local->service, NULL);
David Howells248f2192016-09-08 11:10:12 +0100692 write_unlock(&rx->local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700693 }
694
695 /* try to flush out this socket */
David Howells00e90712016-09-08 11:10:12 +0100696 rxrpc_discard_prealloc(rx);
David Howells17926a72007-04-26 15:48:28 -0700697 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700698 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700699 rxrpc_purge_queue(&sk->sk_receive_queue);
700
David Howells5627cc82016-04-04 14:00:38 +0100701 rxrpc_put_local(rx->local);
702 rx->local = NULL;
David Howells17926a72007-04-26 15:48:28 -0700703 key_put(rx->key);
704 rx->key = NULL;
705 key_put(rx->securities);
706 rx->securities = NULL;
707 sock_put(sk);
708
709 _leave(" = 0");
710 return 0;
711}
712
713/*
714 * release an RxRPC BSD socket on close() or equivalent
715 */
716static int rxrpc_release(struct socket *sock)
717{
718 struct sock *sk = sock->sk;
719
720 _enter("%p{%p}", sock, sk);
721
722 if (!sk)
723 return 0;
724
725 sock->sk = NULL;
726
727 return rxrpc_release_sock(sk);
728}
729
730/*
731 * RxRPC network protocol
732 */
733static const struct proto_ops rxrpc_rpc_ops = {
David Howellse33b3d92016-03-04 15:54:27 +0000734 .family = PF_RXRPC,
David Howells17926a72007-04-26 15:48:28 -0700735 .owner = THIS_MODULE,
736 .release = rxrpc_release,
737 .bind = rxrpc_bind,
738 .connect = rxrpc_connect,
739 .socketpair = sock_no_socketpair,
740 .accept = sock_no_accept,
741 .getname = sock_no_getname,
742 .poll = rxrpc_poll,
743 .ioctl = sock_no_ioctl,
744 .listen = rxrpc_listen,
David Howells248f2192016-09-08 11:10:12 +0100745 .shutdown = rxrpc_shutdown,
David Howells17926a72007-04-26 15:48:28 -0700746 .setsockopt = rxrpc_setsockopt,
747 .getsockopt = sock_no_getsockopt,
748 .sendmsg = rxrpc_sendmsg,
749 .recvmsg = rxrpc_recvmsg,
750 .mmap = sock_no_mmap,
751 .sendpage = sock_no_sendpage,
752};
753
754static struct proto rxrpc_proto = {
755 .name = "RXRPC",
756 .owner = THIS_MODULE,
757 .obj_size = sizeof(struct rxrpc_sock),
David Howells0d12f8a2016-03-04 15:53:46 +0000758 .max_header = sizeof(struct rxrpc_wire_header),
David Howells17926a72007-04-26 15:48:28 -0700759};
760
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000761static const struct net_proto_family rxrpc_family_ops = {
David Howells17926a72007-04-26 15:48:28 -0700762 .family = PF_RXRPC,
763 .create = rxrpc_create,
764 .owner = THIS_MODULE,
765};
766
767/*
768 * initialise and register the RxRPC protocol
769 */
770static int __init af_rxrpc_init(void)
771{
David Howells17926a72007-04-26 15:48:28 -0700772 int ret = -1;
Matthew Wilcox44430612016-12-14 15:09:19 -0800773 unsigned int tmp;
David Howells17926a72007-04-26 15:48:28 -0700774
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +0000775 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
David Howells17926a72007-04-26 15:48:28 -0700776
David Howells5f2d9c42016-09-02 22:39:45 +0100777 get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
778 rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
Matthew Wilcox44430612016-12-14 15:09:19 -0800779 get_random_bytes(&tmp, sizeof(tmp));
780 tmp &= 0x3fffffff;
781 if (tmp == 0)
782 tmp = 1;
783 idr_set_cursor(&rxrpc_client_conn_ids, tmp);
David Howells17926a72007-04-26 15:48:28 -0700784
David Howells651350d2007-04-26 15:50:17 -0700785 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700786 rxrpc_call_jar = kmem_cache_create(
787 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900788 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700789 if (!rxrpc_call_jar) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700790 pr_notice("Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700791 goto error_call_jar;
792 }
793
Tejun Heoe1fcc7e2011-01-14 15:56:31 +0000794 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
David Howells651350d2007-04-26 15:50:17 -0700795 if (!rxrpc_workqueue) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700796 pr_notice("Failed to allocate work queue\n");
David Howells651350d2007-04-26 15:50:17 -0700797 goto error_work_queue;
798 }
799
David Howells648af7f2016-04-07 17:23:51 +0100800 ret = rxrpc_init_security();
801 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700802 pr_crit("Cannot initialise security\n");
David Howells648af7f2016-04-07 17:23:51 +0100803 goto error_security;
804 }
805
David Howells17926a72007-04-26 15:48:28 -0700806 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900807 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700808 pr_crit("Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700809 goto error_proto;
810 }
811
812 ret = sock_register(&rxrpc_family_ops);
813 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700814 pr_crit("Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700815 goto error_sock;
816 }
817
818 ret = register_key_type(&key_type_rxrpc);
819 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700820 pr_crit("Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700821 goto error_key_type;
822 }
823
824 ret = register_key_type(&key_type_rxrpc_s);
825 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700826 pr_crit("Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700827 goto error_key_type_s;
828 }
829
David Howells5873c082014-02-07 18:58:44 +0000830 ret = rxrpc_sysctl_init();
831 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700832 pr_crit("Cannot register sysctls\n");
David Howells5873c082014-02-07 18:58:44 +0000833 goto error_sysctls;
834 }
835
David Howells17926a72007-04-26 15:48:28 -0700836#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +0000837 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
838 proc_create("rxrpc_conns", 0, init_net.proc_net,
839 &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700840#endif
841 return 0;
842
David Howells5873c082014-02-07 18:58:44 +0000843error_sysctls:
844 unregister_key_type(&key_type_rxrpc_s);
David Howells17926a72007-04-26 15:48:28 -0700845error_key_type_s:
846 unregister_key_type(&key_type_rxrpc);
847error_key_type:
848 sock_unregister(PF_RXRPC);
849error_sock:
850 proto_unregister(&rxrpc_proto);
851error_proto:
David Howells648af7f2016-04-07 17:23:51 +0100852 rxrpc_exit_security();
Wei Yongjun8addc042016-07-12 11:21:17 +0000853error_security:
854 destroy_workqueue(rxrpc_workqueue);
David Howells651350d2007-04-26 15:50:17 -0700855error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700856 kmem_cache_destroy(rxrpc_call_jar);
857error_call_jar:
858 return ret;
859}
860
861/*
862 * unregister the RxRPC protocol
863 */
864static void __exit af_rxrpc_exit(void)
865{
866 _enter("");
David Howells5873c082014-02-07 18:58:44 +0000867 rxrpc_sysctl_exit();
David Howells17926a72007-04-26 15:48:28 -0700868 unregister_key_type(&key_type_rxrpc_s);
869 unregister_key_type(&key_type_rxrpc);
870 sock_unregister(PF_RXRPC);
871 proto_unregister(&rxrpc_proto);
872 rxrpc_destroy_all_calls();
873 rxrpc_destroy_all_connections();
David Howells71f3ca42016-09-17 10:49:14 +0100874 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
875 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
David Howells4f95dd72016-04-04 14:00:35 +0100876 rxrpc_destroy_all_locals();
877
Gao fengece31ff2013-02-18 01:34:56 +0000878 remove_proc_entry("rxrpc_conns", init_net.proc_net);
879 remove_proc_entry("rxrpc_calls", init_net.proc_net);
David Howells651350d2007-04-26 15:50:17 -0700880 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100881 rxrpc_exit_security();
David Howells17926a72007-04-26 15:48:28 -0700882 kmem_cache_destroy(rxrpc_call_jar);
883 _leave("");
884}
885
886module_init(af_rxrpc_init);
887module_exit(af_rxrpc_exit);