rxrpc: Split service connection code out into its own file
[deliverable/linux.git] / net / rxrpc / call_accept.c
CommitLineData
17926a79
DH
1/* incoming call handling
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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/errqueue.h>
18#include <linux/udp.h>
19#include <linux/in.h>
20#include <linux/in6.h>
21#include <linux/icmp.h>
5a0e3ad6 22#include <linux/gfp.h>
17926a79
DH
23#include <net/sock.h>
24#include <net/af_rxrpc.h>
25#include <net/ip.h>
26#include "ar-internal.h"
27
28/*
29 * generate a connection-level abort
30 */
31static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
0d12f8a4 32 struct rxrpc_wire_header *whdr)
17926a79
DH
33{
34 struct msghdr msg;
35 struct kvec iov[1];
36 size_t len;
37 int ret;
38
39 _enter("%d,,", local->debug_id);
40
0d12f8a4
DH
41 whdr->type = RXRPC_PACKET_TYPE_BUSY;
42 whdr->serial = htonl(1);
43
17926a79
DH
44 msg.msg_name = &srx->transport.sin;
45 msg.msg_namelen = sizeof(srx->transport.sin);
46 msg.msg_control = NULL;
47 msg.msg_controllen = 0;
48 msg.msg_flags = 0;
49
0d12f8a4
DH
50 iov[0].iov_base = whdr;
51 iov[0].iov_len = sizeof(*whdr);
17926a79
DH
52
53 len = iov[0].iov_len;
54
0d12f8a4 55 _proto("Tx BUSY %%1");
17926a79
DH
56
57 ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
58 if (ret < 0) {
59 _leave(" = -EAGAIN [sendmsg failed: %d]", ret);
60 return -EAGAIN;
61 }
62
63 _leave(" = 0");
64 return 0;
65}
66
67/*
68 * accept an incoming call that needs peer, transport and/or connection setting
69 * up
70 */
71static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
72 struct rxrpc_sock *rx,
73 struct sk_buff *skb,
74 struct sockaddr_rxrpc *srx)
75{
76 struct rxrpc_connection *conn;
17926a79
DH
77 struct rxrpc_skb_priv *sp, *nsp;
78 struct rxrpc_peer *peer;
79 struct rxrpc_call *call;
80 struct sk_buff *notification;
81 int ret;
82
83 _enter("");
84
85 sp = rxrpc_skb(skb);
86
87 /* get a notification message to send to the server app */
88 notification = alloc_skb(0, GFP_NOFS);
c3824d21
TH
89 if (!notification) {
90 _debug("no memory");
91 ret = -ENOMEM;
92 goto error_nofree;
93 }
17926a79
DH
94 rxrpc_new_skb(notification);
95 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
96
be6e6707 97 peer = rxrpc_lookup_peer(local, srx, GFP_NOIO);
0e4699e4 98 if (!peer) {
17926a79
DH
99 _debug("no peer");
100 ret = -EBUSY;
101 goto error;
102 }
103
aa390bbe 104 conn = rxrpc_incoming_connection(local, peer, skb);
17926a79 105 rxrpc_put_peer(peer);
17926a79
DH
106 if (IS_ERR(conn)) {
107 _debug("no conn");
108 ret = PTR_ERR(conn);
109 goto error;
110 }
111
42886ffe 112 call = rxrpc_incoming_call(rx, conn, skb);
17926a79
DH
113 rxrpc_put_connection(conn);
114 if (IS_ERR(call)) {
115 _debug("no call");
116 ret = PTR_ERR(call);
117 goto error;
118 }
119
120 /* attach the call to the socket */
121 read_lock_bh(&local->services_lock);
122 if (rx->sk.sk_state == RXRPC_CLOSE)
123 goto invalid_service;
124
125 write_lock(&rx->call_lock);
126 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
127 rxrpc_get_call(call);
128
129 spin_lock(&call->conn->state_lock);
130 if (sp->hdr.securityIndex > 0 &&
bba304db 131 call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
17926a79
DH
132 _debug("await conn sec");
133 list_add_tail(&call->accept_link, &rx->secureq);
bba304db 134 call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
bba304db 135 set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
651350d1 136 rxrpc_queue_conn(call->conn);
17926a79
DH
137 } else {
138 _debug("conn ready");
139 call->state = RXRPC_CALL_SERVER_ACCEPTING;
140 list_add_tail(&call->accept_link, &rx->acceptq);
141 rxrpc_get_call(call);
142 nsp = rxrpc_skb(notification);
143 nsp->call = call;
144
145 ASSERTCMP(atomic_read(&call->usage), >=, 3);
146
147 _debug("notify");
148 spin_lock(&call->lock);
149 ret = rxrpc_queue_rcv_skb(call, notification, true,
150 false);
151 spin_unlock(&call->lock);
152 notification = NULL;
163e3cb7 153 BUG_ON(ret < 0);
17926a79
DH
154 }
155 spin_unlock(&call->conn->state_lock);
156
157 _debug("queued");
158 }
159 write_unlock(&rx->call_lock);
160
161 _debug("process");
162 rxrpc_fast_process_packet(call, skb);
163
164 _debug("done");
165 read_unlock_bh(&local->services_lock);
166 rxrpc_free_skb(notification);
167 rxrpc_put_call(call);
168 _leave(" = 0");
169 return 0;
170
171invalid_service:
172 _debug("invalid");
173 read_unlock_bh(&local->services_lock);
174
175 read_lock_bh(&call->state_lock);
e721498a 176 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 177 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
17926a79 178 rxrpc_get_call(call);
651350d1 179 rxrpc_queue_call(call);
17926a79
DH
180 }
181 read_unlock_bh(&call->state_lock);
182 rxrpc_put_call(call);
183 ret = -ECONNREFUSED;
184error:
185 rxrpc_free_skb(notification);
c3824d21 186error_nofree:
17926a79
DH
187 _leave(" = %d", ret);
188 return ret;
189}
190
191/*
192 * accept incoming calls that need peer, transport and/or connection setting up
193 * - the packets we get are all incoming client DATA packets that have seq == 1
194 */
4f95dd78 195void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
17926a79 196{
17926a79
DH
197 struct rxrpc_skb_priv *sp;
198 struct sockaddr_rxrpc srx;
199 struct rxrpc_sock *rx;
0d12f8a4 200 struct rxrpc_wire_header whdr;
17926a79 201 struct sk_buff *skb;
17926a79
DH
202 int ret;
203
204 _enter("%d", local->debug_id);
205
17926a79
DH
206 skb = skb_dequeue(&local->accept_queue);
207 if (!skb) {
17926a79
DH
208 _leave("\n");
209 return;
210 }
211
212 _net("incoming call skb %p", skb);
213
214 sp = rxrpc_skb(skb);
215
0d12f8a4
DH
216 /* Set up a response packet header in case we need it */
217 whdr.epoch = htonl(sp->hdr.epoch);
218 whdr.cid = htonl(sp->hdr.cid);
219 whdr.callNumber = htonl(sp->hdr.callNumber);
220 whdr.seq = htonl(sp->hdr.seq);
221 whdr.serial = 0;
222 whdr.flags = 0;
223 whdr.type = 0;
224 whdr.userStatus = 0;
225 whdr.securityIndex = sp->hdr.securityIndex;
226 whdr._rsvd = 0;
227 whdr.serviceId = htons(sp->hdr.serviceId);
228
17926a79
DH
229 /* determine the remote address */
230 memset(&srx, 0, sizeof(srx));
231 srx.srx_family = AF_RXRPC;
232 srx.transport.family = local->srx.transport.family;
233 srx.transport_type = local->srx.transport_type;
234 switch (srx.transport.family) {
235 case AF_INET:
236 srx.transport_len = sizeof(struct sockaddr_in);
237 srx.transport.sin.sin_port = udp_hdr(skb)->source;
238 srx.transport.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
239 break;
240 default:
241 goto busy;
242 }
243
244 /* get the socket providing the service */
17926a79
DH
245 read_lock_bh(&local->services_lock);
246 list_for_each_entry(rx, &local->services, listen_link) {
0d12f8a4 247 if (rx->srx.srx_service == sp->hdr.serviceId &&
17926a79
DH
248 rx->sk.sk_state != RXRPC_CLOSE)
249 goto found_service;
250 }
251 read_unlock_bh(&local->services_lock);
252 goto invalid_service;
253
254found_service:
0d12f8a4 255 _debug("found service %hd", rx->srx.srx_service);
17926a79
DH
256 if (sk_acceptq_is_full(&rx->sk))
257 goto backlog_full;
258 sk_acceptq_added(&rx->sk);
259 sock_hold(&rx->sk);
260 read_unlock_bh(&local->services_lock);
261
262 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
263 if (ret < 0)
264 sk_acceptq_removed(&rx->sk);
265 sock_put(&rx->sk);
266 switch (ret) {
267 case -ECONNRESET: /* old calls are ignored */
268 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
269 case 0:
4f95dd78 270 return;
17926a79
DH
271 case -ECONNREFUSED:
272 goto invalid_service;
273 case -EBUSY:
274 goto busy;
275 case -EKEYREJECTED:
276 goto security_mismatch;
277 default:
278 BUG();
279 }
280
281backlog_full:
282 read_unlock_bh(&local->services_lock);
283busy:
0d12f8a4 284 rxrpc_busy(local, &srx, &whdr);
17926a79 285 rxrpc_free_skb(skb);
4f95dd78 286 return;
17926a79
DH
287
288invalid_service:
289 skb->priority = RX_INVALID_OPERATION;
290 rxrpc_reject_packet(local, skb);
4f95dd78 291 return;
17926a79
DH
292
293 /* can't change connection security type mid-flow */
294security_mismatch:
295 skb->priority = RX_PROTOCOL_ERROR;
296 rxrpc_reject_packet(local, skb);
4f95dd78 297 return;
17926a79
DH
298}
299
300/*
301 * handle acceptance of a call by userspace
302 * - assign the user call ID to the call at the front of the queue
303 */
651350d1
DH
304struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
305 unsigned long user_call_ID)
17926a79
DH
306{
307 struct rxrpc_call *call;
308 struct rb_node *parent, **pp;
309 int ret;
310
311 _enter(",%lx", user_call_ID);
312
313 ASSERT(!irqs_disabled());
314
315 write_lock(&rx->call_lock);
316
317 ret = -ENODATA;
318 if (list_empty(&rx->acceptq))
319 goto out;
320
321 /* check the user ID isn't already in use */
322 ret = -EBADSLT;
323 pp = &rx->calls.rb_node;
324 parent = NULL;
325 while (*pp) {
326 parent = *pp;
327 call = rb_entry(parent, struct rxrpc_call, sock_node);
328
329 if (user_call_ID < call->user_call_ID)
330 pp = &(*pp)->rb_left;
331 else if (user_call_ID > call->user_call_ID)
332 pp = &(*pp)->rb_right;
333 else
334 goto out;
335 }
336
337 /* dequeue the first call and check it's still valid */
338 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
339 list_del_init(&call->accept_link);
340 sk_acceptq_removed(&rx->sk);
341
342 write_lock_bh(&call->state_lock);
343 switch (call->state) {
344 case RXRPC_CALL_SERVER_ACCEPTING:
345 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
346 break;
347 case RXRPC_CALL_REMOTELY_ABORTED:
348 case RXRPC_CALL_LOCALLY_ABORTED:
349 ret = -ECONNABORTED;
350 goto out_release;
351 case RXRPC_CALL_NETWORK_ERROR:
352 ret = call->conn->error;
353 goto out_release;
354 case RXRPC_CALL_DEAD:
355 ret = -ETIME;
356 goto out_discard;
357 default:
358 BUG();
359 }
360
361 /* formalise the acceptance */
362 call->user_call_ID = user_call_ID;
363 rb_link_node(&call->sock_node, parent, pp);
364 rb_insert_color(&call->sock_node, &rx->calls);
365 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
366 BUG();
4c198ad1 367 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
17926a79 368 BUG();
651350d1 369 rxrpc_queue_call(call);
17926a79 370
651350d1 371 rxrpc_get_call(call);
17926a79
DH
372 write_unlock_bh(&call->state_lock);
373 write_unlock(&rx->call_lock);
651350d1
DH
374 _leave(" = %p{%d}", call, call->debug_id);
375 return call;
376
377 /* if the call is already dying or dead, then we leave the socket's ref
378 * on it to be released by rxrpc_dead_call_expired() as induced by
379 * rxrpc_release_call() */
380out_release:
381 _debug("release %p", call);
382 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 383 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
651350d1
DH
384 rxrpc_queue_call(call);
385out_discard:
386 write_unlock_bh(&call->state_lock);
387 _debug("discard %p", call);
388out:
389 write_unlock(&rx->call_lock);
390 _leave(" = %d", ret);
391 return ERR_PTR(ret);
392}
393
394/*
b4f1342f 395 * Handle rejection of a call by userspace
651350d1
DH
396 * - reject the call at the front of the queue
397 */
398int rxrpc_reject_call(struct rxrpc_sock *rx)
399{
400 struct rxrpc_call *call;
401 int ret;
402
403 _enter("");
404
405 ASSERT(!irqs_disabled());
406
407 write_lock(&rx->call_lock);
408
409 ret = -ENODATA;
410 if (list_empty(&rx->acceptq))
411 goto out;
412
413 /* dequeue the first call and check it's still valid */
414 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
415 list_del_init(&call->accept_link);
416 sk_acceptq_removed(&rx->sk);
417
418 write_lock_bh(&call->state_lock);
419 switch (call->state) {
420 case RXRPC_CALL_SERVER_ACCEPTING:
421 call->state = RXRPC_CALL_SERVER_BUSY;
4c198ad1 422 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
651350d1
DH
423 rxrpc_queue_call(call);
424 ret = 0;
425 goto out_release;
426 case RXRPC_CALL_REMOTELY_ABORTED:
427 case RXRPC_CALL_LOCALLY_ABORTED:
428 ret = -ECONNABORTED;
429 goto out_release;
430 case RXRPC_CALL_NETWORK_ERROR:
431 ret = call->conn->error;
432 goto out_release;
433 case RXRPC_CALL_DEAD:
434 ret = -ETIME;
435 goto out_discard;
436 default:
437 BUG();
438 }
17926a79
DH
439
440 /* if the call is already dying or dead, then we leave the socket's ref
441 * on it to be released by rxrpc_dead_call_expired() as induced by
442 * rxrpc_release_call() */
443out_release:
444 _debug("release %p", call);
445 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
4c198ad1 446 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
651350d1 447 rxrpc_queue_call(call);
17926a79
DH
448out_discard:
449 write_unlock_bh(&call->state_lock);
450 _debug("discard %p", call);
451out:
452 write_unlock(&rx->call_lock);
453 _leave(" = %d", ret);
454 return ret;
455}
651350d1
DH
456
457/**
458 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
459 * @sock: The socket on which the impending call is waiting
460 * @user_call_ID: The tag to attach to the call
461 *
462 * Allow a kernel service to accept an incoming call, assuming the incoming
463 * call is still valid.
464 */
465struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
466 unsigned long user_call_ID)
467{
468 struct rxrpc_call *call;
469
470 _enter(",%lx", user_call_ID);
471 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
472 _leave(" = %p", call);
473 return call;
474}
651350d1
DH
475EXPORT_SYMBOL(rxrpc_kernel_accept_call);
476
477/**
478 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
479 * @sock: The socket on which the impending call is waiting
480 *
481 * Allow a kernel service to reject an incoming call with a BUSY message,
482 * assuming the incoming call is still valid.
483 */
484int rxrpc_kernel_reject_call(struct socket *sock)
485{
486 int ret;
487
488 _enter("");
489 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
490 _leave(" = %d", ret);
491 return ret;
492}
651350d1 493EXPORT_SYMBOL(rxrpc_kernel_reject_call);
This page took 0.820845 seconds and 5 git commands to generate.