blob: 8dbf7bed2cc453f2b00cf058af081af3966d4bcb [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;
139 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
David Howells17926a72007-04-26 15:48:28 -0700140 int ret;
141
142 _enter("%p,%p,%d", rx, saddr, len);
143
144 ret = rxrpc_validate_address(rx, srx, len);
145 if (ret < 0)
146 goto error;
147
148 lock_sock(&rx->sk);
149
David Howells2341e072016-06-09 23:02:51 +0100150 if (rx->sk.sk_state != RXRPC_UNBOUND) {
David Howells17926a72007-04-26 15:48:28 -0700151 ret = -EINVAL;
152 goto error_unlock;
153 }
154
155 memcpy(&rx->srx, srx, sizeof(rx->srx));
156
David Howells17926a72007-04-26 15:48:28 -0700157 local = rxrpc_lookup_local(&rx->srx);
158 if (IS_ERR(local)) {
159 ret = PTR_ERR(local);
160 goto error_unlock;
161 }
162
David Howells2341e072016-06-09 23:02:51 +0100163 if (rx->srx.srx_service) {
David Howells248f2192016-09-08 11:10:12 +0100164 write_lock(&local->services_lock);
David Howellsde8d6c72016-09-08 11:10:11 +0100165 hlist_for_each_entry(prx, &local->services, listen_link) {
David Howells2341e072016-06-09 23:02:51 +0100166 if (prx->srx.srx_service == rx->srx.srx_service)
David Howells17926a72007-04-26 15:48:28 -0700167 goto service_in_use;
168 }
169
David Howells2341e072016-06-09 23:02:51 +0100170 rx->local = local;
David Howellsde8d6c72016-09-08 11:10:11 +0100171 hlist_add_head_rcu(&rx->listen_link, &local->services);
David Howells248f2192016-09-08 11:10:12 +0100172 write_unlock(&local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700173
174 rx->sk.sk_state = RXRPC_SERVER_BOUND;
175 } else {
David Howells2341e072016-06-09 23:02:51 +0100176 rx->local = local;
David Howells17926a72007-04-26 15:48:28 -0700177 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
178 }
179
180 release_sock(&rx->sk);
181 _leave(" = 0");
182 return 0;
183
184service_in_use:
David Howells248f2192016-09-08 11:10:12 +0100185 write_unlock(&local->services_lock);
David Howells2341e072016-06-09 23:02:51 +0100186 rxrpc_put_local(local);
187 ret = -EADDRINUSE;
David Howells17926a72007-04-26 15:48:28 -0700188error_unlock:
189 release_sock(&rx->sk);
190error:
191 _leave(" = %d", ret);
192 return ret;
193}
194
195/*
196 * set the number of pending calls permitted on a listening socket
197 */
198static int rxrpc_listen(struct socket *sock, int backlog)
199{
200 struct sock *sk = sock->sk;
201 struct rxrpc_sock *rx = rxrpc_sk(sk);
David Howells00e90712016-09-08 11:10:12 +0100202 unsigned int max, old;
David Howells17926a72007-04-26 15:48:28 -0700203 int ret;
204
205 _enter("%p,%d", rx, backlog);
206
207 lock_sock(&rx->sk);
208
209 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100210 case RXRPC_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700211 ret = -EADDRNOTAVAIL;
212 break;
David Howells17926a72007-04-26 15:48:28 -0700213 case RXRPC_SERVER_BOUND:
214 ASSERT(rx->local != NULL);
David Howells0e119b42016-06-10 22:30:37 +0100215 max = READ_ONCE(rxrpc_max_backlog);
216 ret = -EINVAL;
217 if (backlog == INT_MAX)
218 backlog = max;
219 else if (backlog < 0 || backlog > max)
220 break;
David Howells00e90712016-09-08 11:10:12 +0100221 old = sk->sk_max_ack_backlog;
David Howells17926a72007-04-26 15:48:28 -0700222 sk->sk_max_ack_backlog = backlog;
David Howells00e90712016-09-08 11:10:12 +0100223 ret = rxrpc_service_prealloc(rx, GFP_KERNEL);
224 if (ret == 0)
225 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
226 else
227 sk->sk_max_ack_backlog = old;
David Howells17926a72007-04-26 15:48:28 -0700228 break;
David Howells0e119b42016-06-10 22:30:37 +0100229 default:
230 ret = -EBUSY;
231 break;
David Howells17926a72007-04-26 15:48:28 -0700232 }
233
234 release_sock(&rx->sk);
235 _leave(" = %d", ret);
236 return ret;
237}
238
David Howells651350d2007-04-26 15:50:17 -0700239/**
240 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
241 * @sock: The socket on which to make the call
David Howells2341e072016-06-09 23:02:51 +0100242 * @srx: The address of the peer to contact
David Howells651350d2007-04-26 15:50:17 -0700243 * @key: The security context to use (defaults to socket setting)
244 * @user_call_ID: The ID to use
David Howellsd0016482016-08-30 20:42:14 +0100245 * @gfp: The allocation constraints
246 * @notify_rx: Where to send notifications instead of socket queue
David Howells651350d2007-04-26 15:50:17 -0700247 *
248 * Allow a kernel service to begin a call on the nominated socket. This just
249 * sets up all the internal tracking structures and allocates connection and
250 * call IDs as appropriate. The call to be used is returned.
251 *
252 * The default socket destination address and security may be overridden by
253 * supplying @srx and @key.
254 */
255struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
256 struct sockaddr_rxrpc *srx,
257 struct key *key,
258 unsigned long user_call_ID,
David Howellsd0016482016-08-30 20:42:14 +0100259 gfp_t gfp,
260 rxrpc_notify_rx_t notify_rx)
David Howells651350d2007-04-26 15:50:17 -0700261{
David Howells19ffa012016-04-04 14:00:36 +0100262 struct rxrpc_conn_parameters cp;
David Howells651350d2007-04-26 15:50:17 -0700263 struct rxrpc_call *call;
264 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howellsf4552c22016-06-17 11:00:48 +0100265 int ret;
David Howells651350d2007-04-26 15:50:17 -0700266
267 _enter(",,%x,%lx", key_serial(key), user_call_ID);
268
David Howellsf4552c22016-06-17 11:00:48 +0100269 ret = rxrpc_validate_address(rx, srx, sizeof(*srx));
270 if (ret < 0)
271 return ERR_PTR(ret);
272
David Howells651350d2007-04-26 15:50:17 -0700273 lock_sock(&rx->sk);
274
David Howells19ffa012016-04-04 14:00:36 +0100275 if (!key)
276 key = rx->key;
277 if (key && !key->payload.data[0])
278 key = NULL; /* a no-security key */
279
280 memset(&cp, 0, sizeof(cp));
281 cp.local = rx->local;
282 cp.key = key;
283 cp.security_level = 0;
284 cp.exclusive = false;
285 cp.service_id = srx->srx_service;
David Howellsaa390bb2016-06-17 10:06:56 +0100286 call = rxrpc_new_client_call(rx, &cp, srx, user_call_ID, gfp);
David Howellsd0016482016-08-30 20:42:14 +0100287 if (!IS_ERR(call))
288 call->notify_rx = notify_rx;
David Howells19ffa012016-04-04 14:00:36 +0100289
David Howells651350d2007-04-26 15:50:17 -0700290 release_sock(&rx->sk);
291 _leave(" = %p", call);
292 return call;
293}
David Howells651350d2007-04-26 15:50:17 -0700294EXPORT_SYMBOL(rxrpc_kernel_begin_call);
295
296/**
297 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
David Howells4de48af2016-08-30 12:00:48 +0100298 * @sock: The socket the call is on
David Howells651350d2007-04-26 15:50:17 -0700299 * @call: The call to end
300 *
301 * Allow a kernel service to end a call it was using. The call must be
302 * complete before this is called (the call should be aborted if necessary).
303 */
David Howells4de48af2016-08-30 12:00:48 +0100304void rxrpc_kernel_end_call(struct socket *sock, struct rxrpc_call *call)
David Howells651350d2007-04-26 15:50:17 -0700305{
306 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
David Howells8d94aa32016-09-07 09:19:31 +0100307 rxrpc_release_call(rxrpc_sk(sock->sk), call);
David Howellscbd00892016-09-13 09:12:34 +0100308 rxrpc_put_call(call, rxrpc_call_put_kernel);
David Howells651350d2007-04-26 15:50:17 -0700309}
David Howells651350d2007-04-26 15:50:17 -0700310EXPORT_SYMBOL(rxrpc_kernel_end_call);
311
312/**
David Howellsd0016482016-08-30 20:42:14 +0100313 * rxrpc_kernel_new_call_notification - Get notifications of new calls
David Howells651350d2007-04-26 15:50:17 -0700314 * @sock: The socket to intercept received messages on
David Howellsd0016482016-08-30 20:42:14 +0100315 * @notify_new_call: Function to be called when new calls appear
David Howells00e90712016-09-08 11:10:12 +0100316 * @discard_new_call: Function to discard preallocated calls
David Howells651350d2007-04-26 15:50:17 -0700317 *
David Howellsd0016482016-08-30 20:42:14 +0100318 * Allow a kernel service to be given notifications about new calls.
David Howells651350d2007-04-26 15:50:17 -0700319 */
David Howellsd0016482016-08-30 20:42:14 +0100320void rxrpc_kernel_new_call_notification(
321 struct socket *sock,
David Howells00e90712016-09-08 11:10:12 +0100322 rxrpc_notify_new_call_t notify_new_call,
323 rxrpc_discard_new_call_t discard_new_call)
David Howells651350d2007-04-26 15:50:17 -0700324{
325 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
326
David Howellsd0016482016-08-30 20:42:14 +0100327 rx->notify_new_call = notify_new_call;
David Howells00e90712016-09-08 11:10:12 +0100328 rx->discard_new_call = discard_new_call;
David Howells651350d2007-04-26 15:50:17 -0700329}
David Howellsd0016482016-08-30 20:42:14 +0100330EXPORT_SYMBOL(rxrpc_kernel_new_call_notification);
David Howells651350d2007-04-26 15:50:17 -0700331
David Howells17926a72007-04-26 15:48:28 -0700332/*
333 * connect an RxRPC socket
334 * - this just targets it at a specific destination; no actual connection
335 * negotiation takes place
336 */
337static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
338 int addr_len, int flags)
339{
David Howells2341e072016-06-09 23:02:51 +0100340 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)addr;
341 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells17926a72007-04-26 15:48:28 -0700342 int ret;
343
344 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
345
346 ret = rxrpc_validate_address(rx, srx, addr_len);
347 if (ret < 0) {
348 _leave(" = %d [bad addr]", ret);
349 return ret;
350 }
351
352 lock_sock(&rx->sk);
353
David Howells2341e072016-06-09 23:02:51 +0100354 ret = -EISCONN;
355 if (test_bit(RXRPC_SOCK_CONNECTED, &rx->flags))
356 goto error;
357
David Howells17926a72007-04-26 15:48:28 -0700358 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100359 case RXRPC_UNBOUND:
360 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
361 case RXRPC_CLIENT_UNBOUND:
David Howells17926a72007-04-26 15:48:28 -0700362 case RXRPC_CLIENT_BOUND:
363 break;
David Howells17926a72007-04-26 15:48:28 -0700364 default:
David Howells2341e072016-06-09 23:02:51 +0100365 ret = -EBUSY;
366 goto error;
David Howells17926a72007-04-26 15:48:28 -0700367 }
368
David Howells2341e072016-06-09 23:02:51 +0100369 rx->connect_srx = *srx;
370 set_bit(RXRPC_SOCK_CONNECTED, &rx->flags);
371 ret = 0;
David Howells17926a72007-04-26 15:48:28 -0700372
David Howells2341e072016-06-09 23:02:51 +0100373error:
David Howells17926a72007-04-26 15:48:28 -0700374 release_sock(&rx->sk);
David Howells2341e072016-06-09 23:02:51 +0100375 return ret;
David Howells17926a72007-04-26 15:48:28 -0700376}
377
378/*
379 * send a message through an RxRPC socket
380 * - in a client this does a number of things:
381 * - finds/sets up a connection for the security specified (if any)
382 * - initiates a call (ID in control data)
383 * - ends the request phase of a call (if MSG_MORE is not set)
384 * - sends a call data packet
385 * - may send an abort (abort code in control data)
386 */
Ying Xue1b784142015-03-02 15:37:48 +0800387static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700388{
David Howells2341e072016-06-09 23:02:51 +0100389 struct rxrpc_local *local;
David Howells17926a72007-04-26 15:48:28 -0700390 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
391 int ret;
392
393 _enter(",{%d},,%zu", rx->sk.sk_state, len);
394
395 if (m->msg_flags & MSG_OOB)
396 return -EOPNOTSUPP;
397
398 if (m->msg_name) {
399 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
400 if (ret < 0) {
401 _leave(" = %d [bad addr]", ret);
402 return ret;
403 }
404 }
405
David Howells17926a72007-04-26 15:48:28 -0700406 lock_sock(&rx->sk);
407
David Howells17926a72007-04-26 15:48:28 -0700408 switch (rx->sk.sk_state) {
David Howells2341e072016-06-09 23:02:51 +0100409 case RXRPC_UNBOUND:
David Howellscd5892c2016-09-13 08:49:05 +0100410 rx->srx.srx_family = AF_RXRPC;
411 rx->srx.srx_service = 0;
412 rx->srx.transport_type = SOCK_DGRAM;
413 rx->srx.transport.family = rx->family;
414 switch (rx->family) {
415 case AF_INET:
416 rx->srx.transport_len = sizeof(struct sockaddr_in);
417 break;
David Howellsd1912742016-09-17 07:26:01 +0100418#ifdef CONFIG_AF_RXRPC_IPV6
David Howells75b54cb2016-09-13 08:49:05 +0100419 case AF_INET6:
420 rx->srx.transport_len = sizeof(struct sockaddr_in6);
421 break;
David Howellsd1912742016-09-17 07:26:01 +0100422#endif
David Howellscd5892c2016-09-13 08:49:05 +0100423 default:
424 ret = -EAFNOSUPPORT;
425 goto error_unlock;
426 }
David Howells2341e072016-06-09 23:02:51 +0100427 local = rxrpc_lookup_local(&rx->srx);
428 if (IS_ERR(local)) {
429 ret = PTR_ERR(local);
430 goto error_unlock;
431 }
432
433 rx->local = local;
434 rx->sk.sk_state = RXRPC_CLIENT_UNBOUND;
435 /* Fall through */
436
437 case RXRPC_CLIENT_UNBOUND:
438 case RXRPC_CLIENT_BOUND:
439 if (!m->msg_name &&
440 test_bit(RXRPC_SOCK_CONNECTED, &rx->flags)) {
441 m->msg_name = &rx->connect_srx;
442 m->msg_namelen = sizeof(rx->connect_srx);
David Howells17926a72007-04-26 15:48:28 -0700443 }
444 case RXRPC_SERVER_BOUND:
David Howells2341e072016-06-09 23:02:51 +0100445 case RXRPC_SERVER_LISTENING:
446 ret = rxrpc_do_sendmsg(rx, m, len);
David Howells17926a72007-04-26 15:48:28 -0700447 break;
448 default:
David Howells2341e072016-06-09 23:02:51 +0100449 ret = -EINVAL;
David Howells17926a72007-04-26 15:48:28 -0700450 break;
451 }
452
David Howells2341e072016-06-09 23:02:51 +0100453error_unlock:
David Howells17926a72007-04-26 15:48:28 -0700454 release_sock(&rx->sk);
David Howells17926a72007-04-26 15:48:28 -0700455 _leave(" = %d", ret);
456 return ret;
457}
458
459/*
460 * set RxRPC socket options
461 */
462static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700463 char __user *optval, unsigned int optlen)
David Howells17926a72007-04-26 15:48:28 -0700464{
465 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000466 unsigned int min_sec_level;
David Howells17926a72007-04-26 15:48:28 -0700467 int ret;
468
469 _enter(",%d,%d,,%d", level, optname, optlen);
470
471 lock_sock(&rx->sk);
472 ret = -EOPNOTSUPP;
473
474 if (level == SOL_RXRPC) {
475 switch (optname) {
476 case RXRPC_EXCLUSIVE_CONNECTION:
477 ret = -EINVAL;
478 if (optlen != 0)
479 goto error;
480 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100481 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700482 goto error;
David Howellscc8feb82016-04-04 14:00:37 +0100483 rx->exclusive = true;
David Howells17926a72007-04-26 15:48:28 -0700484 goto success;
485
486 case RXRPC_SECURITY_KEY:
487 ret = -EINVAL;
488 if (rx->key)
489 goto error;
490 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100491 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700492 goto error;
493 ret = rxrpc_request_key(rx, optval, optlen);
494 goto error;
495
496 case RXRPC_SECURITY_KEYRING:
497 ret = -EINVAL;
498 if (rx->key)
499 goto error;
500 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100501 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700502 goto error;
503 ret = rxrpc_server_keyring(rx, optval, optlen);
504 goto error;
505
506 case RXRPC_MIN_SECURITY_LEVEL:
507 ret = -EINVAL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000508 if (optlen != sizeof(unsigned int))
David Howells17926a72007-04-26 15:48:28 -0700509 goto error;
510 ret = -EISCONN;
David Howells2341e072016-06-09 23:02:51 +0100511 if (rx->sk.sk_state != RXRPC_UNBOUND)
David Howells17926a72007-04-26 15:48:28 -0700512 goto error;
513 ret = get_user(min_sec_level,
Eric Dumazet95c96172012-04-15 05:58:06 +0000514 (unsigned int __user *) optval);
David Howells17926a72007-04-26 15:48:28 -0700515 if (ret < 0)
516 goto error;
517 ret = -EINVAL;
518 if (min_sec_level > RXRPC_SECURITY_MAX)
519 goto error;
520 rx->min_sec_level = min_sec_level;
521 goto success;
522
523 default:
524 break;
525 }
526 }
527
528success:
529 ret = 0;
530error:
531 release_sock(&rx->sk);
532 return ret;
533}
534
535/*
536 * permit an RxRPC socket to be polled
537 */
538static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
539 poll_table *wait)
540{
David Howells17926a72007-04-26 15:48:28 -0700541 struct sock *sk = sock->sk;
David Howells248f2192016-09-08 11:10:12 +0100542 struct rxrpc_sock *rx = rxrpc_sk(sk);
543 unsigned int mask;
David Howells17926a72007-04-26 15:48:28 -0700544
Eric Dumazetaa395142010-04-20 13:03:51 +0000545 sock_poll_wait(file, sk_sleep(sk), wait);
David Howells17926a72007-04-26 15:48:28 -0700546 mask = 0;
547
548 /* the socket is readable if there are any messages waiting on the Rx
549 * queue */
David Howells248f2192016-09-08 11:10:12 +0100550 if (!list_empty(&rx->recvmsg_q))
David Howells17926a72007-04-26 15:48:28 -0700551 mask |= POLLIN | POLLRDNORM;
552
553 /* the socket is writable if there is space to add new data to the
554 * socket; there is no guarantee that any particular call in progress
555 * on the socket may have space in the Tx ACK window */
556 if (rxrpc_writable(sk))
557 mask |= POLLOUT | POLLWRNORM;
558
559 return mask;
560}
561
562/*
563 * create an RxRPC socket
564 */
Eric Paris3f378b62009-11-05 22:18:14 -0800565static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
566 int kern)
David Howells17926a72007-04-26 15:48:28 -0700567{
568 struct rxrpc_sock *rx;
569 struct sock *sk;
570
571 _enter("%p,%d", sock, protocol);
572
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800573 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700574 return -EAFNOSUPPORT;
575
David Howellsb4f13422016-03-04 15:56:19 +0000576 /* we support transport protocol UDP/UDP6 only */
David Howellsd1912742016-09-17 07:26:01 +0100577 if (protocol != PF_INET &&
578 IS_ENABLED(CONFIG_AF_RXRPC_IPV6) && protocol != PF_INET6)
David Howells17926a72007-04-26 15:48:28 -0700579 return -EPROTONOSUPPORT;
580
581 if (sock->type != SOCK_DGRAM)
582 return -ESOCKTNOSUPPORT;
583
584 sock->ops = &rxrpc_rpc_ops;
585 sock->state = SS_UNCONNECTED;
586
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500587 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
David Howells17926a72007-04-26 15:48:28 -0700588 if (!sk)
589 return -ENOMEM;
590
591 sock_init_data(sock, sk);
David Howells8d94aa32016-09-07 09:19:31 +0100592 sock_set_flag(sk, SOCK_RCU_FREE);
David Howells2341e072016-06-09 23:02:51 +0100593 sk->sk_state = RXRPC_UNBOUND;
David Howells17926a72007-04-26 15:48:28 -0700594 sk->sk_write_space = rxrpc_write_space;
David Howells0e119b42016-06-10 22:30:37 +0100595 sk->sk_max_ack_backlog = 0;
David Howells17926a72007-04-26 15:48:28 -0700596 sk->sk_destruct = rxrpc_sock_destructor;
597
598 rx = rxrpc_sk(sk);
David Howells19ffa012016-04-04 14:00:36 +0100599 rx->family = protocol;
David Howells17926a72007-04-26 15:48:28 -0700600 rx->calls = RB_ROOT;
601
David Howellsde8d6c72016-09-08 11:10:11 +0100602 INIT_HLIST_NODE(&rx->listen_link);
David Howells248f2192016-09-08 11:10:12 +0100603 spin_lock_init(&rx->incoming_lock);
604 INIT_LIST_HEAD(&rx->sock_calls);
605 INIT_LIST_HEAD(&rx->to_be_accepted);
606 INIT_LIST_HEAD(&rx->recvmsg_q);
607 rwlock_init(&rx->recvmsg_lock);
David Howells17926a72007-04-26 15:48:28 -0700608 rwlock_init(&rx->call_lock);
609 memset(&rx->srx, 0, sizeof(rx->srx));
610
611 _leave(" = 0 [%p]", rx);
612 return 0;
613}
614
615/*
David Howells248f2192016-09-08 11:10:12 +0100616 * Kill all the calls on a socket and shut it down.
617 */
618static int rxrpc_shutdown(struct socket *sock, int flags)
619{
620 struct sock *sk = sock->sk;
621 struct rxrpc_sock *rx = rxrpc_sk(sk);
622 int ret = 0;
623
624 _enter("%p,%d", sk, flags);
625
626 if (flags != SHUT_RDWR)
627 return -EOPNOTSUPP;
628 if (sk->sk_state == RXRPC_CLOSE)
629 return -ESHUTDOWN;
630
631 lock_sock(sk);
632
633 spin_lock_bh(&sk->sk_receive_queue.lock);
634 if (sk->sk_state < RXRPC_CLOSE) {
635 sk->sk_state = RXRPC_CLOSE;
636 sk->sk_shutdown = SHUTDOWN_MASK;
637 } else {
638 ret = -ESHUTDOWN;
639 }
640 spin_unlock_bh(&sk->sk_receive_queue.lock);
641
642 rxrpc_discard_prealloc(rx);
643
644 release_sock(sk);
645 return ret;
646}
647
648/*
David Howells17926a72007-04-26 15:48:28 -0700649 * RxRPC socket destructor
650 */
651static void rxrpc_sock_destructor(struct sock *sk)
652{
653 _enter("%p", sk);
654
655 rxrpc_purge_queue(&sk->sk_receive_queue);
656
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700657 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
658 WARN_ON(!sk_unhashed(sk));
659 WARN_ON(sk->sk_socket);
David Howells17926a72007-04-26 15:48:28 -0700660
661 if (!sock_flag(sk, SOCK_DEAD)) {
662 printk("Attempt to release alive rxrpc socket: %p\n", sk);
663 return;
664 }
665}
666
667/*
668 * release an RxRPC socket
669 */
670static int rxrpc_release_sock(struct sock *sk)
671{
672 struct rxrpc_sock *rx = rxrpc_sk(sk);
673
674 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
675
676 /* declare the socket closed for business */
677 sock_orphan(sk);
678 sk->sk_shutdown = SHUTDOWN_MASK;
679
680 spin_lock_bh(&sk->sk_receive_queue.lock);
681 sk->sk_state = RXRPC_CLOSE;
682 spin_unlock_bh(&sk->sk_receive_queue.lock);
683
684 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
685
David Howellsde8d6c72016-09-08 11:10:11 +0100686 if (!hlist_unhashed(&rx->listen_link)) {
David Howells248f2192016-09-08 11:10:12 +0100687 write_lock(&rx->local->services_lock);
David Howellsde8d6c72016-09-08 11:10:11 +0100688 hlist_del_rcu(&rx->listen_link);
David Howells248f2192016-09-08 11:10:12 +0100689 write_unlock(&rx->local->services_lock);
David Howells17926a72007-04-26 15:48:28 -0700690 }
691
692 /* try to flush out this socket */
David Howells00e90712016-09-08 11:10:12 +0100693 rxrpc_discard_prealloc(rx);
David Howells17926a72007-04-26 15:48:28 -0700694 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700695 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700696 rxrpc_purge_queue(&sk->sk_receive_queue);
697
David Howells5627cc82016-04-04 14:00:38 +0100698 rxrpc_put_local(rx->local);
699 rx->local = NULL;
David Howells17926a72007-04-26 15:48:28 -0700700 key_put(rx->key);
701 rx->key = NULL;
702 key_put(rx->securities);
703 rx->securities = NULL;
704 sock_put(sk);
705
706 _leave(" = 0");
707 return 0;
708}
709
710/*
711 * release an RxRPC BSD socket on close() or equivalent
712 */
713static int rxrpc_release(struct socket *sock)
714{
715 struct sock *sk = sock->sk;
716
717 _enter("%p{%p}", sock, sk);
718
719 if (!sk)
720 return 0;
721
722 sock->sk = NULL;
723
724 return rxrpc_release_sock(sk);
725}
726
727/*
728 * RxRPC network protocol
729 */
730static const struct proto_ops rxrpc_rpc_ops = {
David Howellse33b3d92016-03-04 15:54:27 +0000731 .family = PF_RXRPC,
David Howells17926a72007-04-26 15:48:28 -0700732 .owner = THIS_MODULE,
733 .release = rxrpc_release,
734 .bind = rxrpc_bind,
735 .connect = rxrpc_connect,
736 .socketpair = sock_no_socketpair,
737 .accept = sock_no_accept,
738 .getname = sock_no_getname,
739 .poll = rxrpc_poll,
740 .ioctl = sock_no_ioctl,
741 .listen = rxrpc_listen,
David Howells248f2192016-09-08 11:10:12 +0100742 .shutdown = rxrpc_shutdown,
David Howells17926a72007-04-26 15:48:28 -0700743 .setsockopt = rxrpc_setsockopt,
744 .getsockopt = sock_no_getsockopt,
745 .sendmsg = rxrpc_sendmsg,
746 .recvmsg = rxrpc_recvmsg,
747 .mmap = sock_no_mmap,
748 .sendpage = sock_no_sendpage,
749};
750
751static struct proto rxrpc_proto = {
752 .name = "RXRPC",
753 .owner = THIS_MODULE,
754 .obj_size = sizeof(struct rxrpc_sock),
David Howells0d12f8a2016-03-04 15:53:46 +0000755 .max_header = sizeof(struct rxrpc_wire_header),
David Howells17926a72007-04-26 15:48:28 -0700756};
757
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000758static const struct net_proto_family rxrpc_family_ops = {
David Howells17926a72007-04-26 15:48:28 -0700759 .family = PF_RXRPC,
760 .create = rxrpc_create,
761 .owner = THIS_MODULE,
762};
763
764/*
765 * initialise and register the RxRPC protocol
766 */
767static int __init af_rxrpc_init(void)
768{
David Howells17926a72007-04-26 15:48:28 -0700769 int ret = -1;
770
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +0000771 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
David Howells17926a72007-04-26 15:48:28 -0700772
David Howells5f2d9c42016-09-02 22:39:45 +0100773 get_random_bytes(&rxrpc_epoch, sizeof(rxrpc_epoch));
774 rxrpc_epoch |= RXRPC_RANDOM_EPOCH;
775 get_random_bytes(&rxrpc_client_conn_ids.cur,
776 sizeof(rxrpc_client_conn_ids.cur));
777 rxrpc_client_conn_ids.cur &= 0x3fffffff;
778 if (rxrpc_client_conn_ids.cur == 0)
779 rxrpc_client_conn_ids.cur = 1;
David Howells17926a72007-04-26 15:48:28 -0700780
David Howells651350d2007-04-26 15:50:17 -0700781 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700782 rxrpc_call_jar = kmem_cache_create(
783 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900784 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700785 if (!rxrpc_call_jar) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700786 pr_notice("Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700787 goto error_call_jar;
788 }
789
Tejun Heoe1fcc7e2011-01-14 15:56:31 +0000790 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
David Howells651350d2007-04-26 15:50:17 -0700791 if (!rxrpc_workqueue) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700792 pr_notice("Failed to allocate work queue\n");
David Howells651350d2007-04-26 15:50:17 -0700793 goto error_work_queue;
794 }
795
David Howells648af7f2016-04-07 17:23:51 +0100796 ret = rxrpc_init_security();
797 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700798 pr_crit("Cannot initialise security\n");
David Howells648af7f2016-04-07 17:23:51 +0100799 goto error_security;
800 }
801
David Howells17926a72007-04-26 15:48:28 -0700802 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900803 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700804 pr_crit("Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700805 goto error_proto;
806 }
807
808 ret = sock_register(&rxrpc_family_ops);
809 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700810 pr_crit("Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700811 goto error_sock;
812 }
813
814 ret = register_key_type(&key_type_rxrpc);
815 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700816 pr_crit("Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700817 goto error_key_type;
818 }
819
820 ret = register_key_type(&key_type_rxrpc_s);
821 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700822 pr_crit("Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700823 goto error_key_type_s;
824 }
825
David Howells5873c082014-02-07 18:58:44 +0000826 ret = rxrpc_sysctl_init();
827 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700828 pr_crit("Cannot register sysctls\n");
David Howells5873c082014-02-07 18:58:44 +0000829 goto error_sysctls;
830 }
831
David Howells17926a72007-04-26 15:48:28 -0700832#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +0000833 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
834 proc_create("rxrpc_conns", 0, init_net.proc_net,
835 &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700836#endif
837 return 0;
838
David Howells5873c082014-02-07 18:58:44 +0000839error_sysctls:
840 unregister_key_type(&key_type_rxrpc_s);
David Howells17926a72007-04-26 15:48:28 -0700841error_key_type_s:
842 unregister_key_type(&key_type_rxrpc);
843error_key_type:
844 sock_unregister(PF_RXRPC);
845error_sock:
846 proto_unregister(&rxrpc_proto);
847error_proto:
David Howells648af7f2016-04-07 17:23:51 +0100848 rxrpc_exit_security();
Wei Yongjun8addc042016-07-12 11:21:17 +0000849error_security:
850 destroy_workqueue(rxrpc_workqueue);
David Howells651350d2007-04-26 15:50:17 -0700851error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700852 kmem_cache_destroy(rxrpc_call_jar);
853error_call_jar:
854 return ret;
855}
856
857/*
858 * unregister the RxRPC protocol
859 */
860static void __exit af_rxrpc_exit(void)
861{
862 _enter("");
David Howells5873c082014-02-07 18:58:44 +0000863 rxrpc_sysctl_exit();
David Howells17926a72007-04-26 15:48:28 -0700864 unregister_key_type(&key_type_rxrpc_s);
865 unregister_key_type(&key_type_rxrpc);
866 sock_unregister(PF_RXRPC);
867 proto_unregister(&rxrpc_proto);
868 rxrpc_destroy_all_calls();
869 rxrpc_destroy_all_connections();
David Howells71f3ca42016-09-17 10:49:14 +0100870 ASSERTCMP(atomic_read(&rxrpc_n_tx_skbs), ==, 0);
871 ASSERTCMP(atomic_read(&rxrpc_n_rx_skbs), ==, 0);
David Howells4f95dd72016-04-04 14:00:35 +0100872 rxrpc_destroy_all_locals();
873
Gao fengece31ff2013-02-18 01:34:56 +0000874 remove_proc_entry("rxrpc_conns", init_net.proc_net);
875 remove_proc_entry("rxrpc_calls", init_net.proc_net);
David Howells651350d2007-04-26 15:50:17 -0700876 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100877 rxrpc_exit_security();
David Howells17926a72007-04-26 15:48:28 -0700878 kmem_cache_destroy(rxrpc_call_jar);
879 _leave("");
880}
881
882module_init(af_rxrpc_init);
883module_exit(af_rxrpc_exit);