blob: 7840b8e7da80ba1caa61083f158c80bf0e234e86 [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>
19#include <linux/poll.h>
20#include <linux/proc_fs.h>
David Howells76181c12007-10-16 23:29:46 -070021#include <linux/key-type.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020022#include <net/net_namespace.h>
David Howells17926a72007-04-26 15:48:28 -070023#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include "ar-internal.h"
26
27MODULE_DESCRIPTION("RxRPC network protocol");
28MODULE_AUTHOR("Red Hat, Inc.");
29MODULE_LICENSE("GPL");
30MODULE_ALIAS_NETPROTO(PF_RXRPC);
31
Eric Dumazet95c96172012-04-15 05:58:06 +000032unsigned int rxrpc_debug; // = RXRPC_DEBUG_KPROTO;
David Howells17926a72007-04-26 15:48:28 -070033module_param_named(debug, rxrpc_debug, uint, S_IWUSR | S_IRUGO);
Paul Bolle424b00e2008-04-16 11:08:22 +010034MODULE_PARM_DESC(debug, "RxRPC debugging mask");
David Howells17926a72007-04-26 15:48:28 -070035
36static int sysctl_rxrpc_max_qlen __read_mostly = 10;
37
38static 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 */
48atomic_t rxrpc_n_skbs;
49
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
102 if (srx->transport.family != rx->proto)
103 return -EAFNOSUPPORT;
104
105 switch (srx->transport.family) {
106 case AF_INET:
Harvey Harrison21454aa2008-10-31 00:54:56 -0700107 _debug("INET: %x @ %pI4",
David Howells17926a72007-04-26 15:48:28 -0700108 ntohs(srx->transport.sin.sin_port),
Harvey Harrison21454aa2008-10-31 00:54:56 -0700109 &srx->transport.sin.sin_addr);
David Howellsab802ee2016-03-04 15:59:49 +0000110 tail = offsetof(struct sockaddr_rxrpc, transport.sin.__pad);
David Howells17926a72007-04-26 15:48:28 -0700111 break;
112
113 case AF_INET6:
114 default:
115 return -EAFNOSUPPORT;
116 }
117
David Howellsab802ee2016-03-04 15:59:49 +0000118 if (tail < len)
119 memset((void *)srx + tail, 0, len - tail);
David Howells17926a72007-04-26 15:48:28 -0700120 return 0;
121}
122
123/*
124 * bind a local address to an RxRPC socket
125 */
126static int rxrpc_bind(struct socket *sock, struct sockaddr *saddr, int len)
127{
David Howellsb4f13422016-03-04 15:56:19 +0000128 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *)saddr;
David Howells17926a72007-04-26 15:48:28 -0700129 struct sock *sk = sock->sk;
130 struct rxrpc_local *local;
131 struct rxrpc_sock *rx = rxrpc_sk(sk), *prx;
David Howells17926a72007-04-26 15:48:28 -0700132 int ret;
133
134 _enter("%p,%p,%d", rx, saddr, len);
135
136 ret = rxrpc_validate_address(rx, srx, len);
137 if (ret < 0)
138 goto error;
139
140 lock_sock(&rx->sk);
141
142 if (rx->sk.sk_state != RXRPC_UNCONNECTED) {
143 ret = -EINVAL;
144 goto error_unlock;
145 }
146
147 memcpy(&rx->srx, srx, sizeof(rx->srx));
148
David Howellsb4f13422016-03-04 15:56:19 +0000149 /* Find or create a local transport endpoint to use */
David Howells17926a72007-04-26 15:48:28 -0700150 local = rxrpc_lookup_local(&rx->srx);
151 if (IS_ERR(local)) {
152 ret = PTR_ERR(local);
153 goto error_unlock;
154 }
155
156 rx->local = local;
157 if (srx->srx_service) {
David Howells17926a72007-04-26 15:48:28 -0700158 write_lock_bh(&local->services_lock);
159 list_for_each_entry(prx, &local->services, listen_link) {
David Howells0d12f8a2016-03-04 15:53:46 +0000160 if (prx->srx.srx_service == srx->srx_service)
David Howells17926a72007-04-26 15:48:28 -0700161 goto service_in_use;
162 }
163
David Howells17926a72007-04-26 15:48:28 -0700164 list_add_tail(&rx->listen_link, &local->services);
165 write_unlock_bh(&local->services_lock);
166
167 rx->sk.sk_state = RXRPC_SERVER_BOUND;
168 } else {
169 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
170 }
171
172 release_sock(&rx->sk);
173 _leave(" = 0");
174 return 0;
175
176service_in_use:
177 ret = -EADDRINUSE;
178 write_unlock_bh(&local->services_lock);
179error_unlock:
180 release_sock(&rx->sk);
181error:
182 _leave(" = %d", ret);
183 return ret;
184}
185
186/*
187 * set the number of pending calls permitted on a listening socket
188 */
189static int rxrpc_listen(struct socket *sock, int backlog)
190{
191 struct sock *sk = sock->sk;
192 struct rxrpc_sock *rx = rxrpc_sk(sk);
193 int ret;
194
195 _enter("%p,%d", rx, backlog);
196
197 lock_sock(&rx->sk);
198
199 switch (rx->sk.sk_state) {
200 case RXRPC_UNCONNECTED:
201 ret = -EADDRNOTAVAIL;
202 break;
203 case RXRPC_CLIENT_BOUND:
204 case RXRPC_CLIENT_CONNECTED:
205 default:
206 ret = -EBUSY;
207 break;
208 case RXRPC_SERVER_BOUND:
209 ASSERT(rx->local != NULL);
210 sk->sk_max_ack_backlog = backlog;
211 rx->sk.sk_state = RXRPC_SERVER_LISTENING;
212 ret = 0;
213 break;
214 }
215
216 release_sock(&rx->sk);
217 _leave(" = %d", ret);
218 return ret;
219}
220
221/*
222 * find a transport by address
223 */
224static struct rxrpc_transport *rxrpc_name_to_transport(struct socket *sock,
225 struct sockaddr *addr,
David Howells651350d2007-04-26 15:50:17 -0700226 int addr_len, int flags,
227 gfp_t gfp)
David Howells17926a72007-04-26 15:48:28 -0700228{
229 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
230 struct rxrpc_transport *trans;
231 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
232 struct rxrpc_peer *peer;
233
234 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
235
236 ASSERT(rx->local != NULL);
237 ASSERT(rx->sk.sk_state > RXRPC_UNCONNECTED);
238
239 if (rx->srx.transport_type != srx->transport_type)
240 return ERR_PTR(-ESOCKTNOSUPPORT);
241 if (rx->srx.transport.family != srx->transport.family)
242 return ERR_PTR(-EAFNOSUPPORT);
243
244 /* find a remote transport endpoint from the local one */
David Howells651350d2007-04-26 15:50:17 -0700245 peer = rxrpc_get_peer(srx, gfp);
David Howells17926a72007-04-26 15:48:28 -0700246 if (IS_ERR(peer))
David Howellse231c2e2008-02-07 00:15:26 -0800247 return ERR_CAST(peer);
David Howells17926a72007-04-26 15:48:28 -0700248
249 /* find a transport */
David Howells651350d2007-04-26 15:50:17 -0700250 trans = rxrpc_get_transport(rx->local, peer, gfp);
David Howells17926a72007-04-26 15:48:28 -0700251 rxrpc_put_peer(peer);
252 _leave(" = %p", trans);
253 return trans;
254}
255
David Howells651350d2007-04-26 15:50:17 -0700256/**
257 * rxrpc_kernel_begin_call - Allow a kernel service to begin a call
258 * @sock: The socket on which to make the call
259 * @srx: The address of the peer to contact (defaults to socket setting)
260 * @key: The security context to use (defaults to socket setting)
261 * @user_call_ID: The ID to use
262 *
263 * Allow a kernel service to begin a call on the nominated socket. This just
264 * sets up all the internal tracking structures and allocates connection and
265 * call IDs as appropriate. The call to be used is returned.
266 *
267 * The default socket destination address and security may be overridden by
268 * supplying @srx and @key.
269 */
270struct rxrpc_call *rxrpc_kernel_begin_call(struct socket *sock,
271 struct sockaddr_rxrpc *srx,
272 struct key *key,
273 unsigned long user_call_ID,
274 gfp_t gfp)
275{
276 struct rxrpc_conn_bundle *bundle;
277 struct rxrpc_transport *trans;
278 struct rxrpc_call *call;
279 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
David Howells651350d2007-04-26 15:50:17 -0700280
281 _enter(",,%x,%lx", key_serial(key), user_call_ID);
282
283 lock_sock(&rx->sk);
284
285 if (srx) {
286 trans = rxrpc_name_to_transport(sock, (struct sockaddr *) srx,
287 sizeof(*srx), 0, gfp);
288 if (IS_ERR(trans)) {
David Howellse231c2e2008-02-07 00:15:26 -0800289 call = ERR_CAST(trans);
David Howells651350d2007-04-26 15:50:17 -0700290 trans = NULL;
David Howells15bde722009-02-06 21:50:52 -0800291 goto out_notrans;
David Howells651350d2007-04-26 15:50:17 -0700292 }
293 } else {
294 trans = rx->trans;
295 if (!trans) {
296 call = ERR_PTR(-ENOTCONN);
David Howells15bde722009-02-06 21:50:52 -0800297 goto out_notrans;
David Howells651350d2007-04-26 15:50:17 -0700298 }
299 atomic_inc(&trans->usage);
300 }
301
David Howells0d12f8a2016-03-04 15:53:46 +0000302 if (!srx)
303 srx = &rx->srx;
David Howells651350d2007-04-26 15:50:17 -0700304 if (!key)
305 key = rx->key;
David Howells146aa8b2015-10-21 14:04:48 +0100306 if (key && !key->payload.data[0])
David Howells651350d2007-04-26 15:50:17 -0700307 key = NULL; /* a no-security key */
308
David Howells0d12f8a2016-03-04 15:53:46 +0000309 bundle = rxrpc_get_bundle(rx, trans, key, srx->srx_service, gfp);
David Howells651350d2007-04-26 15:50:17 -0700310 if (IS_ERR(bundle)) {
David Howellse231c2e2008-02-07 00:15:26 -0800311 call = ERR_CAST(bundle);
David Howells651350d2007-04-26 15:50:17 -0700312 goto out;
313 }
314
315 call = rxrpc_get_client_call(rx, trans, bundle, user_call_ID, true,
316 gfp);
317 rxrpc_put_bundle(trans, bundle);
318out:
319 rxrpc_put_transport(trans);
David Howells15bde722009-02-06 21:50:52 -0800320out_notrans:
David Howells651350d2007-04-26 15:50:17 -0700321 release_sock(&rx->sk);
322 _leave(" = %p", call);
323 return call;
324}
David Howells651350d2007-04-26 15:50:17 -0700325EXPORT_SYMBOL(rxrpc_kernel_begin_call);
326
327/**
328 * rxrpc_kernel_end_call - Allow a kernel service to end a call it was using
329 * @call: The call to end
330 *
331 * Allow a kernel service to end a call it was using. The call must be
332 * complete before this is called (the call should be aborted if necessary).
333 */
334void rxrpc_kernel_end_call(struct rxrpc_call *call)
335{
336 _enter("%d{%d}", call->debug_id, atomic_read(&call->usage));
337 rxrpc_remove_user_ID(call->socket, call);
338 rxrpc_put_call(call);
339}
David Howells651350d2007-04-26 15:50:17 -0700340EXPORT_SYMBOL(rxrpc_kernel_end_call);
341
342/**
343 * rxrpc_kernel_intercept_rx_messages - Intercept received RxRPC messages
344 * @sock: The socket to intercept received messages on
345 * @interceptor: The function to pass the messages to
346 *
347 * Allow a kernel service to intercept messages heading for the Rx queue on an
348 * RxRPC socket. They get passed to the specified function instead.
349 * @interceptor should free the socket buffers it is given. @interceptor is
350 * called with the socket receive queue spinlock held and softirqs disabled -
351 * this ensures that the messages will be delivered in the right order.
352 */
353void rxrpc_kernel_intercept_rx_messages(struct socket *sock,
354 rxrpc_interceptor_t interceptor)
355{
356 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
357
358 _enter("");
359 rx->interceptor = interceptor;
360}
361
362EXPORT_SYMBOL(rxrpc_kernel_intercept_rx_messages);
363
David Howells17926a72007-04-26 15:48:28 -0700364/*
365 * connect an RxRPC socket
366 * - this just targets it at a specific destination; no actual connection
367 * negotiation takes place
368 */
369static int rxrpc_connect(struct socket *sock, struct sockaddr *addr,
370 int addr_len, int flags)
371{
372 struct sockaddr_rxrpc *srx = (struct sockaddr_rxrpc *) addr;
373 struct sock *sk = sock->sk;
374 struct rxrpc_transport *trans;
375 struct rxrpc_local *local;
376 struct rxrpc_sock *rx = rxrpc_sk(sk);
377 int ret;
378
379 _enter("%p,%p,%d,%d", rx, addr, addr_len, flags);
380
381 ret = rxrpc_validate_address(rx, srx, addr_len);
382 if (ret < 0) {
383 _leave(" = %d [bad addr]", ret);
384 return ret;
385 }
386
387 lock_sock(&rx->sk);
388
389 switch (rx->sk.sk_state) {
390 case RXRPC_UNCONNECTED:
391 /* find a local transport endpoint if we don't have one already */
392 ASSERTCMP(rx->local, ==, NULL);
393 rx->srx.srx_family = AF_RXRPC;
394 rx->srx.srx_service = 0;
395 rx->srx.transport_type = srx->transport_type;
396 rx->srx.transport_len = sizeof(sa_family_t);
397 rx->srx.transport.family = srx->transport.family;
398 local = rxrpc_lookup_local(&rx->srx);
399 if (IS_ERR(local)) {
400 release_sock(&rx->sk);
401 return PTR_ERR(local);
402 }
403 rx->local = local;
404 rx->sk.sk_state = RXRPC_CLIENT_BOUND;
405 case RXRPC_CLIENT_BOUND:
406 break;
407 case RXRPC_CLIENT_CONNECTED:
408 release_sock(&rx->sk);
409 return -EISCONN;
410 default:
411 release_sock(&rx->sk);
412 return -EBUSY; /* server sockets can't connect as well */
413 }
414
David Howells651350d2007-04-26 15:50:17 -0700415 trans = rxrpc_name_to_transport(sock, addr, addr_len, flags,
416 GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700417 if (IS_ERR(trans)) {
418 release_sock(&rx->sk);
419 _leave(" = %ld", PTR_ERR(trans));
420 return PTR_ERR(trans);
421 }
422
423 rx->trans = trans;
David Howells17926a72007-04-26 15:48:28 -0700424 rx->sk.sk_state = RXRPC_CLIENT_CONNECTED;
425
426 release_sock(&rx->sk);
427 return 0;
428}
429
430/*
431 * send a message through an RxRPC socket
432 * - in a client this does a number of things:
433 * - finds/sets up a connection for the security specified (if any)
434 * - initiates a call (ID in control data)
435 * - ends the request phase of a call (if MSG_MORE is not set)
436 * - sends a call data packet
437 * - may send an abort (abort code in control data)
438 */
Ying Xue1b784142015-03-02 15:37:48 +0800439static int rxrpc_sendmsg(struct socket *sock, struct msghdr *m, size_t len)
David Howells17926a72007-04-26 15:48:28 -0700440{
441 struct rxrpc_transport *trans;
442 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
443 int ret;
444
445 _enter(",{%d},,%zu", rx->sk.sk_state, len);
446
447 if (m->msg_flags & MSG_OOB)
448 return -EOPNOTSUPP;
449
450 if (m->msg_name) {
451 ret = rxrpc_validate_address(rx, m->msg_name, m->msg_namelen);
452 if (ret < 0) {
453 _leave(" = %d [bad addr]", ret);
454 return ret;
455 }
456 }
457
458 trans = NULL;
459 lock_sock(&rx->sk);
460
461 if (m->msg_name) {
462 ret = -EISCONN;
463 trans = rxrpc_name_to_transport(sock, m->msg_name,
David Howells651350d2007-04-26 15:50:17 -0700464 m->msg_namelen, 0, GFP_KERNEL);
David Howells17926a72007-04-26 15:48:28 -0700465 if (IS_ERR(trans)) {
466 ret = PTR_ERR(trans);
467 trans = NULL;
468 goto out;
469 }
470 } else {
471 trans = rx->trans;
472 if (trans)
473 atomic_inc(&trans->usage);
474 }
475
476 switch (rx->sk.sk_state) {
477 case RXRPC_SERVER_LISTENING:
478 if (!m->msg_name) {
Ying Xue1b784142015-03-02 15:37:48 +0800479 ret = rxrpc_server_sendmsg(rx, m, len);
David Howells17926a72007-04-26 15:48:28 -0700480 break;
481 }
482 case RXRPC_SERVER_BOUND:
483 case RXRPC_CLIENT_BOUND:
484 if (!m->msg_name) {
485 ret = -ENOTCONN;
486 break;
487 }
488 case RXRPC_CLIENT_CONNECTED:
Ying Xue1b784142015-03-02 15:37:48 +0800489 ret = rxrpc_client_sendmsg(rx, trans, m, len);
David Howells17926a72007-04-26 15:48:28 -0700490 break;
491 default:
492 ret = -ENOTCONN;
493 break;
494 }
495
496out:
497 release_sock(&rx->sk);
498 if (trans)
499 rxrpc_put_transport(trans);
500 _leave(" = %d", ret);
501 return ret;
502}
503
504/*
505 * set RxRPC socket options
506 */
507static int rxrpc_setsockopt(struct socket *sock, int level, int optname,
David S. Millerb7058842009-09-30 16:12:20 -0700508 char __user *optval, unsigned int optlen)
David Howells17926a72007-04-26 15:48:28 -0700509{
510 struct rxrpc_sock *rx = rxrpc_sk(sock->sk);
Eric Dumazet95c96172012-04-15 05:58:06 +0000511 unsigned int min_sec_level;
David Howells17926a72007-04-26 15:48:28 -0700512 int ret;
513
514 _enter(",%d,%d,,%d", level, optname, optlen);
515
516 lock_sock(&rx->sk);
517 ret = -EOPNOTSUPP;
518
519 if (level == SOL_RXRPC) {
520 switch (optname) {
521 case RXRPC_EXCLUSIVE_CONNECTION:
522 ret = -EINVAL;
523 if (optlen != 0)
524 goto error;
525 ret = -EISCONN;
526 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
527 goto error;
528 set_bit(RXRPC_SOCK_EXCLUSIVE_CONN, &rx->flags);
529 goto success;
530
531 case RXRPC_SECURITY_KEY:
532 ret = -EINVAL;
533 if (rx->key)
534 goto error;
535 ret = -EISCONN;
536 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
537 goto error;
538 ret = rxrpc_request_key(rx, optval, optlen);
539 goto error;
540
541 case RXRPC_SECURITY_KEYRING:
542 ret = -EINVAL;
543 if (rx->key)
544 goto error;
545 ret = -EISCONN;
546 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
547 goto error;
548 ret = rxrpc_server_keyring(rx, optval, optlen);
549 goto error;
550
551 case RXRPC_MIN_SECURITY_LEVEL:
552 ret = -EINVAL;
Eric Dumazet95c96172012-04-15 05:58:06 +0000553 if (optlen != sizeof(unsigned int))
David Howells17926a72007-04-26 15:48:28 -0700554 goto error;
555 ret = -EISCONN;
556 if (rx->sk.sk_state != RXRPC_UNCONNECTED)
557 goto error;
558 ret = get_user(min_sec_level,
Eric Dumazet95c96172012-04-15 05:58:06 +0000559 (unsigned int __user *) optval);
David Howells17926a72007-04-26 15:48:28 -0700560 if (ret < 0)
561 goto error;
562 ret = -EINVAL;
563 if (min_sec_level > RXRPC_SECURITY_MAX)
564 goto error;
565 rx->min_sec_level = min_sec_level;
566 goto success;
567
568 default:
569 break;
570 }
571 }
572
573success:
574 ret = 0;
575error:
576 release_sock(&rx->sk);
577 return ret;
578}
579
580/*
581 * permit an RxRPC socket to be polled
582 */
583static unsigned int rxrpc_poll(struct file *file, struct socket *sock,
584 poll_table *wait)
585{
586 unsigned int mask;
587 struct sock *sk = sock->sk;
588
Eric Dumazetaa395142010-04-20 13:03:51 +0000589 sock_poll_wait(file, sk_sleep(sk), wait);
David Howells17926a72007-04-26 15:48:28 -0700590 mask = 0;
591
592 /* the socket is readable if there are any messages waiting on the Rx
593 * queue */
594 if (!skb_queue_empty(&sk->sk_receive_queue))
595 mask |= POLLIN | POLLRDNORM;
596
597 /* the socket is writable if there is space to add new data to the
598 * socket; there is no guarantee that any particular call in progress
599 * on the socket may have space in the Tx ACK window */
600 if (rxrpc_writable(sk))
601 mask |= POLLOUT | POLLWRNORM;
602
603 return mask;
604}
605
606/*
607 * create an RxRPC socket
608 */
Eric Paris3f378b62009-11-05 22:18:14 -0800609static int rxrpc_create(struct net *net, struct socket *sock, int protocol,
610 int kern)
David Howells17926a72007-04-26 15:48:28 -0700611{
612 struct rxrpc_sock *rx;
613 struct sock *sk;
614
615 _enter("%p,%d", sock, protocol);
616
Octavian Purdila09ad9bc2009-11-25 15:14:13 -0800617 if (!net_eq(net, &init_net))
Eric W. Biederman1b8d7ae2007-10-08 23:24:22 -0700618 return -EAFNOSUPPORT;
619
David Howellsb4f13422016-03-04 15:56:19 +0000620 /* we support transport protocol UDP/UDP6 only */
David Howells17926a72007-04-26 15:48:28 -0700621 if (protocol != PF_INET)
622 return -EPROTONOSUPPORT;
623
624 if (sock->type != SOCK_DGRAM)
625 return -ESOCKTNOSUPPORT;
626
627 sock->ops = &rxrpc_rpc_ops;
628 sock->state = SS_UNCONNECTED;
629
Eric W. Biederman11aa9c22015-05-08 21:09:13 -0500630 sk = sk_alloc(net, PF_RXRPC, GFP_KERNEL, &rxrpc_proto, kern);
David Howells17926a72007-04-26 15:48:28 -0700631 if (!sk)
632 return -ENOMEM;
633
634 sock_init_data(sock, sk);
635 sk->sk_state = RXRPC_UNCONNECTED;
636 sk->sk_write_space = rxrpc_write_space;
637 sk->sk_max_ack_backlog = sysctl_rxrpc_max_qlen;
638 sk->sk_destruct = rxrpc_sock_destructor;
639
640 rx = rxrpc_sk(sk);
641 rx->proto = protocol;
642 rx->calls = RB_ROOT;
643
644 INIT_LIST_HEAD(&rx->listen_link);
645 INIT_LIST_HEAD(&rx->secureq);
646 INIT_LIST_HEAD(&rx->acceptq);
647 rwlock_init(&rx->call_lock);
648 memset(&rx->srx, 0, sizeof(rx->srx));
649
650 _leave(" = 0 [%p]", rx);
651 return 0;
652}
653
654/*
655 * RxRPC socket destructor
656 */
657static void rxrpc_sock_destructor(struct sock *sk)
658{
659 _enter("%p", sk);
660
661 rxrpc_purge_queue(&sk->sk_receive_queue);
662
Ilpo Järvinen547b7922008-07-25 21:43:18 -0700663 WARN_ON(atomic_read(&sk->sk_wmem_alloc));
664 WARN_ON(!sk_unhashed(sk));
665 WARN_ON(sk->sk_socket);
David Howells17926a72007-04-26 15:48:28 -0700666
667 if (!sock_flag(sk, SOCK_DEAD)) {
668 printk("Attempt to release alive rxrpc socket: %p\n", sk);
669 return;
670 }
671}
672
673/*
674 * release an RxRPC socket
675 */
676static int rxrpc_release_sock(struct sock *sk)
677{
678 struct rxrpc_sock *rx = rxrpc_sk(sk);
679
680 _enter("%p{%d,%d}", sk, sk->sk_state, atomic_read(&sk->sk_refcnt));
681
682 /* declare the socket closed for business */
683 sock_orphan(sk);
684 sk->sk_shutdown = SHUTDOWN_MASK;
685
686 spin_lock_bh(&sk->sk_receive_queue.lock);
687 sk->sk_state = RXRPC_CLOSE;
688 spin_unlock_bh(&sk->sk_receive_queue.lock);
689
690 ASSERTCMP(rx->listen_link.next, !=, LIST_POISON1);
691
692 if (!list_empty(&rx->listen_link)) {
693 write_lock_bh(&rx->local->services_lock);
694 list_del(&rx->listen_link);
695 write_unlock_bh(&rx->local->services_lock);
696 }
697
698 /* try to flush out this socket */
699 rxrpc_release_calls_on_socket(rx);
David Howells651350d2007-04-26 15:50:17 -0700700 flush_workqueue(rxrpc_workqueue);
David Howells17926a72007-04-26 15:48:28 -0700701 rxrpc_purge_queue(&sk->sk_receive_queue);
702
703 if (rx->conn) {
704 rxrpc_put_connection(rx->conn);
705 rx->conn = NULL;
706 }
707
708 if (rx->bundle) {
709 rxrpc_put_bundle(rx->trans, rx->bundle);
710 rx->bundle = NULL;
711 }
712 if (rx->trans) {
713 rxrpc_put_transport(rx->trans);
714 rx->trans = NULL;
715 }
716 if (rx->local) {
717 rxrpc_put_local(rx->local);
718 rx->local = NULL;
719 }
720
721 key_put(rx->key);
722 rx->key = NULL;
723 key_put(rx->securities);
724 rx->securities = NULL;
725 sock_put(sk);
726
727 _leave(" = 0");
728 return 0;
729}
730
731/*
732 * release an RxRPC BSD socket on close() or equivalent
733 */
734static int rxrpc_release(struct socket *sock)
735{
736 struct sock *sk = sock->sk;
737
738 _enter("%p{%p}", sock, sk);
739
740 if (!sk)
741 return 0;
742
743 sock->sk = NULL;
744
745 return rxrpc_release_sock(sk);
746}
747
748/*
749 * RxRPC network protocol
750 */
751static const struct proto_ops rxrpc_rpc_ops = {
David Howellse33b3d92016-03-04 15:54:27 +0000752 .family = PF_RXRPC,
David Howells17926a72007-04-26 15:48:28 -0700753 .owner = THIS_MODULE,
754 .release = rxrpc_release,
755 .bind = rxrpc_bind,
756 .connect = rxrpc_connect,
757 .socketpair = sock_no_socketpair,
758 .accept = sock_no_accept,
759 .getname = sock_no_getname,
760 .poll = rxrpc_poll,
761 .ioctl = sock_no_ioctl,
762 .listen = rxrpc_listen,
763 .shutdown = sock_no_shutdown,
764 .setsockopt = rxrpc_setsockopt,
765 .getsockopt = sock_no_getsockopt,
766 .sendmsg = rxrpc_sendmsg,
767 .recvmsg = rxrpc_recvmsg,
768 .mmap = sock_no_mmap,
769 .sendpage = sock_no_sendpage,
770};
771
772static struct proto rxrpc_proto = {
773 .name = "RXRPC",
774 .owner = THIS_MODULE,
775 .obj_size = sizeof(struct rxrpc_sock),
David Howells0d12f8a2016-03-04 15:53:46 +0000776 .max_header = sizeof(struct rxrpc_wire_header),
David Howells17926a72007-04-26 15:48:28 -0700777};
778
Stephen Hemmingerec1b4cf2009-10-05 05:58:39 +0000779static const struct net_proto_family rxrpc_family_ops = {
David Howells17926a72007-04-26 15:48:28 -0700780 .family = PF_RXRPC,
781 .create = rxrpc_create,
782 .owner = THIS_MODULE,
783};
784
785/*
786 * initialise and register the RxRPC protocol
787 */
788static int __init af_rxrpc_init(void)
789{
David Howells17926a72007-04-26 15:48:28 -0700790 int ret = -1;
791
YOSHIFUJI Hideaki / 吉藤英明ce6654c2013-01-09 07:20:01 +0000792 BUILD_BUG_ON(sizeof(struct rxrpc_skb_priv) > FIELD_SIZEOF(struct sk_buff, cb));
David Howells17926a72007-04-26 15:48:28 -0700793
David Howells0d12f8a2016-03-04 15:53:46 +0000794 rxrpc_epoch = get_seconds();
David Howells17926a72007-04-26 15:48:28 -0700795
David Howells651350d2007-04-26 15:50:17 -0700796 ret = -ENOMEM;
David Howells17926a72007-04-26 15:48:28 -0700797 rxrpc_call_jar = kmem_cache_create(
798 "rxrpc_call_jar", sizeof(struct rxrpc_call), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +0900799 SLAB_HWCACHE_ALIGN, NULL);
David Howells17926a72007-04-26 15:48:28 -0700800 if (!rxrpc_call_jar) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700801 pr_notice("Failed to allocate call jar\n");
David Howells17926a72007-04-26 15:48:28 -0700802 goto error_call_jar;
803 }
804
Tejun Heoe1fcc7e2011-01-14 15:56:31 +0000805 rxrpc_workqueue = alloc_workqueue("krxrpcd", 0, 1);
David Howells651350d2007-04-26 15:50:17 -0700806 if (!rxrpc_workqueue) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700807 pr_notice("Failed to allocate work queue\n");
David Howells651350d2007-04-26 15:50:17 -0700808 goto error_work_queue;
809 }
810
David Howells648af7f2016-04-07 17:23:51 +0100811 ret = rxrpc_init_security();
812 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700813 pr_crit("Cannot initialise security\n");
David Howells648af7f2016-04-07 17:23:51 +0100814 goto error_security;
815 }
816
David Howells17926a72007-04-26 15:48:28 -0700817 ret = proto_register(&rxrpc_proto, 1);
YOSHIFUJI Hideaki1c899642007-07-19 10:44:44 +0900818 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700819 pr_crit("Cannot register protocol\n");
David Howells17926a72007-04-26 15:48:28 -0700820 goto error_proto;
821 }
822
823 ret = sock_register(&rxrpc_family_ops);
824 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700825 pr_crit("Cannot register socket family\n");
David Howells17926a72007-04-26 15:48:28 -0700826 goto error_sock;
827 }
828
829 ret = register_key_type(&key_type_rxrpc);
830 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700831 pr_crit("Cannot register client key type\n");
David Howells17926a72007-04-26 15:48:28 -0700832 goto error_key_type;
833 }
834
835 ret = register_key_type(&key_type_rxrpc_s);
836 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700837 pr_crit("Cannot register server key type\n");
David Howells17926a72007-04-26 15:48:28 -0700838 goto error_key_type_s;
839 }
840
David Howells5873c082014-02-07 18:58:44 +0000841 ret = rxrpc_sysctl_init();
842 if (ret < 0) {
Joe Perches9b6d5392016-06-02 12:08:52 -0700843 pr_crit("Cannot register sysctls\n");
David Howells5873c082014-02-07 18:58:44 +0000844 goto error_sysctls;
845 }
846
David Howells17926a72007-04-26 15:48:28 -0700847#ifdef CONFIG_PROC_FS
Gao fengd4beaa62013-02-18 01:34:54 +0000848 proc_create("rxrpc_calls", 0, init_net.proc_net, &rxrpc_call_seq_fops);
849 proc_create("rxrpc_conns", 0, init_net.proc_net,
850 &rxrpc_connection_seq_fops);
David Howells17926a72007-04-26 15:48:28 -0700851#endif
852 return 0;
853
David Howells5873c082014-02-07 18:58:44 +0000854error_sysctls:
855 unregister_key_type(&key_type_rxrpc_s);
David Howells17926a72007-04-26 15:48:28 -0700856error_key_type_s:
857 unregister_key_type(&key_type_rxrpc);
858error_key_type:
859 sock_unregister(PF_RXRPC);
860error_sock:
861 proto_unregister(&rxrpc_proto);
862error_proto:
David Howells651350d2007-04-26 15:50:17 -0700863 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100864error_security:
865 rxrpc_exit_security();
David Howells651350d2007-04-26 15:50:17 -0700866error_work_queue:
David Howells17926a72007-04-26 15:48:28 -0700867 kmem_cache_destroy(rxrpc_call_jar);
868error_call_jar:
869 return ret;
870}
871
872/*
873 * unregister the RxRPC protocol
874 */
875static void __exit af_rxrpc_exit(void)
876{
877 _enter("");
David Howells5873c082014-02-07 18:58:44 +0000878 rxrpc_sysctl_exit();
David Howells17926a72007-04-26 15:48:28 -0700879 unregister_key_type(&key_type_rxrpc_s);
880 unregister_key_type(&key_type_rxrpc);
881 sock_unregister(PF_RXRPC);
882 proto_unregister(&rxrpc_proto);
883 rxrpc_destroy_all_calls();
884 rxrpc_destroy_all_connections();
885 rxrpc_destroy_all_transports();
886 rxrpc_destroy_all_peers();
887 rxrpc_destroy_all_locals();
888
889 ASSERTCMP(atomic_read(&rxrpc_n_skbs), ==, 0);
890
891 _debug("flush scheduled work");
David Howells651350d2007-04-26 15:50:17 -0700892 flush_workqueue(rxrpc_workqueue);
Gao fengece31ff2013-02-18 01:34:56 +0000893 remove_proc_entry("rxrpc_conns", init_net.proc_net);
894 remove_proc_entry("rxrpc_calls", init_net.proc_net);
David Howells651350d2007-04-26 15:50:17 -0700895 destroy_workqueue(rxrpc_workqueue);
David Howells648af7f2016-04-07 17:23:51 +0100896 rxrpc_exit_security();
David Howells17926a72007-04-26 15:48:28 -0700897 kmem_cache_destroy(rxrpc_call_jar);
898 _leave("");
899}
900
901module_init(af_rxrpc_init);
902module_exit(af_rxrpc_exit);