blob: 03af88fe798bac99d88cba32b2baa9b185a5726c [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* 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
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>
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
David Howells17926a72007-04-26 15:48:28 -070023#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,
David Howells0d12f8a2016-03-04 15:53:46 +000032 struct rxrpc_wire_header *whdr)
David Howells17926a72007-04-26 15:48:28 -070033{
34 struct msghdr msg;
35 struct kvec iov[1];
36 size_t len;
37 int ret;
38
39 _enter("%d,,", local->debug_id);
40
David Howells0d12f8a2016-03-04 15:53:46 +000041 whdr->type = RXRPC_PACKET_TYPE_BUSY;
42 whdr->serial = htonl(1);
43
David Howells17926a72007-04-26 15:48:28 -070044 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
David Howells0d12f8a2016-03-04 15:53:46 +000050 iov[0].iov_base = whdr;
51 iov[0].iov_len = sizeof(*whdr);
David Howells17926a72007-04-26 15:48:28 -070052
53 len = iov[0].iov_len;
54
David Howells0d12f8a2016-03-04 15:53:46 +000055 _proto("Tx BUSY %%1");
David Howells17926a72007-04-26 15:48:28 -070056
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;
David Howells17926a72007-04-26 15:48:28 -070077 struct rxrpc_skb_priv *sp, *nsp;
David Howells17926a72007-04-26 15:48:28 -070078 struct rxrpc_call *call;
79 struct sk_buff *notification;
80 int ret;
81
82 _enter("");
83
84 sp = rxrpc_skb(skb);
85
86 /* get a notification message to send to the server app */
87 notification = alloc_skb(0, GFP_NOFS);
Tetsuo Handac3824d22010-03-22 13:50:19 +000088 if (!notification) {
89 _debug("no memory");
90 ret = -ENOMEM;
91 goto error_nofree;
92 }
David Howells17926a72007-04-26 15:48:28 -070093 rxrpc_new_skb(notification);
94 notification->mark = RXRPC_SKB_MARK_NEW_CALL;
95
David Howellsd991b4a2016-06-29 14:40:39 +010096 conn = rxrpc_incoming_connection(local, srx, skb);
David Howells17926a72007-04-26 15:48:28 -070097 if (IS_ERR(conn)) {
98 _debug("no conn");
99 ret = PTR_ERR(conn);
100 goto error;
101 }
102
David Howells42886ff2016-06-16 13:31:07 +0100103 call = rxrpc_incoming_call(rx, conn, skb);
David Howells17926a72007-04-26 15:48:28 -0700104 rxrpc_put_connection(conn);
105 if (IS_ERR(call)) {
106 _debug("no call");
107 ret = PTR_ERR(call);
108 goto error;
109 }
110
111 /* attach the call to the socket */
112 read_lock_bh(&local->services_lock);
113 if (rx->sk.sk_state == RXRPC_CLOSE)
114 goto invalid_service;
115
116 write_lock(&rx->call_lock);
117 if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
118 rxrpc_get_call(call);
119
120 spin_lock(&call->conn->state_lock);
121 if (sp->hdr.securityIndex > 0 &&
David Howellsbba304d2016-06-27 10:32:02 +0100122 call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
David Howells17926a72007-04-26 15:48:28 -0700123 _debug("await conn sec");
124 list_add_tail(&call->accept_link, &rx->secureq);
David Howellsbba304d2016-06-27 10:32:02 +0100125 call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
David Howellsbba304d2016-06-27 10:32:02 +0100126 set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
David Howells651350d2007-04-26 15:50:17 -0700127 rxrpc_queue_conn(call->conn);
David Howells17926a72007-04-26 15:48:28 -0700128 } else {
129 _debug("conn ready");
130 call->state = RXRPC_CALL_SERVER_ACCEPTING;
131 list_add_tail(&call->accept_link, &rx->acceptq);
David Howellse34d4232016-08-30 09:49:29 +0100132 rxrpc_get_call_for_skb(call, notification);
David Howells17926a72007-04-26 15:48:28 -0700133 nsp = rxrpc_skb(notification);
134 nsp->call = call;
135
136 ASSERTCMP(atomic_read(&call->usage), >=, 3);
137
138 _debug("notify");
139 spin_lock(&call->lock);
140 ret = rxrpc_queue_rcv_skb(call, notification, true,
141 false);
142 spin_unlock(&call->lock);
143 notification = NULL;
Julia Lawall163e3cb2008-02-17 18:42:03 -0800144 BUG_ON(ret < 0);
David Howells17926a72007-04-26 15:48:28 -0700145 }
146 spin_unlock(&call->conn->state_lock);
147
148 _debug("queued");
149 }
150 write_unlock(&rx->call_lock);
151
152 _debug("process");
153 rxrpc_fast_process_packet(call, skb);
154
155 _debug("done");
156 read_unlock_bh(&local->services_lock);
157 rxrpc_free_skb(notification);
158 rxrpc_put_call(call);
159 _leave(" = 0");
160 return 0;
161
162invalid_service:
163 _debug("invalid");
164 read_unlock_bh(&local->services_lock);
165
166 read_lock_bh(&call->state_lock);
David Howellse7214982016-03-04 15:53:46 +0000167 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +0000168 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events)) {
David Howells17926a72007-04-26 15:48:28 -0700169 rxrpc_get_call(call);
David Howells651350d2007-04-26 15:50:17 -0700170 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700171 }
172 read_unlock_bh(&call->state_lock);
173 rxrpc_put_call(call);
174 ret = -ECONNREFUSED;
175error:
176 rxrpc_free_skb(notification);
Tetsuo Handac3824d22010-03-22 13:50:19 +0000177error_nofree:
David Howells17926a72007-04-26 15:48:28 -0700178 _leave(" = %d", ret);
179 return ret;
180}
181
182/*
183 * accept incoming calls that need peer, transport and/or connection setting up
184 * - the packets we get are all incoming client DATA packets that have seq == 1
185 */
David Howells4f95dd72016-04-04 14:00:35 +0100186void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
David Howells17926a72007-04-26 15:48:28 -0700187{
David Howells17926a72007-04-26 15:48:28 -0700188 struct rxrpc_skb_priv *sp;
189 struct sockaddr_rxrpc srx;
190 struct rxrpc_sock *rx;
David Howells0d12f8a2016-03-04 15:53:46 +0000191 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700192 struct sk_buff *skb;
David Howells17926a72007-04-26 15:48:28 -0700193 int ret;
194
195 _enter("%d", local->debug_id);
196
David Howells17926a72007-04-26 15:48:28 -0700197 skb = skb_dequeue(&local->accept_queue);
198 if (!skb) {
David Howells17926a72007-04-26 15:48:28 -0700199 _leave("\n");
200 return;
201 }
202
203 _net("incoming call skb %p", skb);
204
David Howellsdf844fd2016-08-23 15:27:24 +0100205 rxrpc_see_skb(skb);
David Howells17926a72007-04-26 15:48:28 -0700206 sp = rxrpc_skb(skb);
207
David Howells0d12f8a2016-03-04 15:53:46 +0000208 /* Set up a response packet header in case we need it */
209 whdr.epoch = htonl(sp->hdr.epoch);
210 whdr.cid = htonl(sp->hdr.cid);
211 whdr.callNumber = htonl(sp->hdr.callNumber);
212 whdr.seq = htonl(sp->hdr.seq);
213 whdr.serial = 0;
214 whdr.flags = 0;
215 whdr.type = 0;
216 whdr.userStatus = 0;
217 whdr.securityIndex = sp->hdr.securityIndex;
218 whdr._rsvd = 0;
219 whdr.serviceId = htons(sp->hdr.serviceId);
220
David Howellsd991b4a2016-06-29 14:40:39 +0100221 if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
222 goto drop;
David Howells17926a72007-04-26 15:48:28 -0700223
224 /* get the socket providing the service */
David Howells17926a72007-04-26 15:48:28 -0700225 read_lock_bh(&local->services_lock);
226 list_for_each_entry(rx, &local->services, listen_link) {
David Howells0d12f8a2016-03-04 15:53:46 +0000227 if (rx->srx.srx_service == sp->hdr.serviceId &&
David Howells17926a72007-04-26 15:48:28 -0700228 rx->sk.sk_state != RXRPC_CLOSE)
229 goto found_service;
230 }
231 read_unlock_bh(&local->services_lock);
232 goto invalid_service;
233
234found_service:
David Howells0d12f8a2016-03-04 15:53:46 +0000235 _debug("found service %hd", rx->srx.srx_service);
David Howells17926a72007-04-26 15:48:28 -0700236 if (sk_acceptq_is_full(&rx->sk))
237 goto backlog_full;
238 sk_acceptq_added(&rx->sk);
239 sock_hold(&rx->sk);
240 read_unlock_bh(&local->services_lock);
241
242 ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
243 if (ret < 0)
244 sk_acceptq_removed(&rx->sk);
245 sock_put(&rx->sk);
246 switch (ret) {
247 case -ECONNRESET: /* old calls are ignored */
248 case -ECONNABORTED: /* aborted calls are reaborted or ignored */
249 case 0:
David Howells4f95dd72016-04-04 14:00:35 +0100250 return;
David Howells17926a72007-04-26 15:48:28 -0700251 case -ECONNREFUSED:
252 goto invalid_service;
253 case -EBUSY:
254 goto busy;
255 case -EKEYREJECTED:
256 goto security_mismatch;
257 default:
258 BUG();
259 }
260
261backlog_full:
262 read_unlock_bh(&local->services_lock);
263busy:
David Howells0d12f8a2016-03-04 15:53:46 +0000264 rxrpc_busy(local, &srx, &whdr);
David Howells17926a72007-04-26 15:48:28 -0700265 rxrpc_free_skb(skb);
David Howells4f95dd72016-04-04 14:00:35 +0100266 return;
David Howells17926a72007-04-26 15:48:28 -0700267
David Howellsd991b4a2016-06-29 14:40:39 +0100268drop:
269 rxrpc_free_skb(skb);
270 return;
271
David Howells17926a72007-04-26 15:48:28 -0700272invalid_service:
273 skb->priority = RX_INVALID_OPERATION;
274 rxrpc_reject_packet(local, skb);
David Howells4f95dd72016-04-04 14:00:35 +0100275 return;
David Howells17926a72007-04-26 15:48:28 -0700276
277 /* can't change connection security type mid-flow */
278security_mismatch:
279 skb->priority = RX_PROTOCOL_ERROR;
280 rxrpc_reject_packet(local, skb);
David Howells4f95dd72016-04-04 14:00:35 +0100281 return;
David Howells17926a72007-04-26 15:48:28 -0700282}
283
284/*
285 * handle acceptance of a call by userspace
286 * - assign the user call ID to the call at the front of the queue
287 */
David Howells651350d2007-04-26 15:50:17 -0700288struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
289 unsigned long user_call_ID)
David Howells17926a72007-04-26 15:48:28 -0700290{
291 struct rxrpc_call *call;
292 struct rb_node *parent, **pp;
293 int ret;
294
295 _enter(",%lx", user_call_ID);
296
297 ASSERT(!irqs_disabled());
298
299 write_lock(&rx->call_lock);
300
301 ret = -ENODATA;
302 if (list_empty(&rx->acceptq))
303 goto out;
304
305 /* check the user ID isn't already in use */
306 ret = -EBADSLT;
307 pp = &rx->calls.rb_node;
308 parent = NULL;
309 while (*pp) {
310 parent = *pp;
311 call = rb_entry(parent, struct rxrpc_call, sock_node);
312
313 if (user_call_ID < call->user_call_ID)
314 pp = &(*pp)->rb_left;
315 else if (user_call_ID > call->user_call_ID)
316 pp = &(*pp)->rb_right;
317 else
318 goto out;
319 }
320
321 /* dequeue the first call and check it's still valid */
322 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
323 list_del_init(&call->accept_link);
324 sk_acceptq_removed(&rx->sk);
David Howellse34d4232016-08-30 09:49:29 +0100325 rxrpc_see_call(call);
David Howells17926a72007-04-26 15:48:28 -0700326
327 write_lock_bh(&call->state_lock);
328 switch (call->state) {
329 case RXRPC_CALL_SERVER_ACCEPTING:
330 call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
331 break;
David Howellsf5c17aa2016-08-30 09:49:28 +0100332 case RXRPC_CALL_COMPLETE:
333 ret = call->error;
David Howells17926a72007-04-26 15:48:28 -0700334 goto out_release;
335 case RXRPC_CALL_DEAD:
336 ret = -ETIME;
337 goto out_discard;
338 default:
339 BUG();
340 }
341
342 /* formalise the acceptance */
343 call->user_call_ID = user_call_ID;
344 rb_link_node(&call->sock_node, parent, pp);
345 rb_insert_color(&call->sock_node, &rx->calls);
346 if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
347 BUG();
David Howells4c198ad2016-03-04 15:53:46 +0000348 if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
David Howells17926a72007-04-26 15:48:28 -0700349 BUG();
David Howells651350d2007-04-26 15:50:17 -0700350 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700351
David Howells651350d2007-04-26 15:50:17 -0700352 rxrpc_get_call(call);
David Howells17926a72007-04-26 15:48:28 -0700353 write_unlock_bh(&call->state_lock);
354 write_unlock(&rx->call_lock);
David Howells651350d2007-04-26 15:50:17 -0700355 _leave(" = %p{%d}", call, call->debug_id);
356 return call;
David Howells17926a72007-04-26 15:48:28 -0700357
358 /* if the call is already dying or dead, then we leave the socket's ref
359 * on it to be released by rxrpc_dead_call_expired() as induced by
360 * rxrpc_release_call() */
361out_release:
362 _debug("release %p", call);
363 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +0000364 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700365 rxrpc_queue_call(call);
366out_discard:
367 write_unlock_bh(&call->state_lock);
368 _debug("discard %p", call);
369out:
370 write_unlock(&rx->call_lock);
371 _leave(" = %d", ret);
372 return ERR_PTR(ret);
373}
374
375/*
David Howellsb4f13422016-03-04 15:56:19 +0000376 * Handle rejection of a call by userspace
David Howells651350d2007-04-26 15:50:17 -0700377 * - reject the call at the front of the queue
378 */
379int rxrpc_reject_call(struct rxrpc_sock *rx)
380{
381 struct rxrpc_call *call;
382 int ret;
383
384 _enter("");
385
386 ASSERT(!irqs_disabled());
387
388 write_lock(&rx->call_lock);
389
390 ret = -ENODATA;
391 if (list_empty(&rx->acceptq))
392 goto out;
393
394 /* dequeue the first call and check it's still valid */
395 call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
396 list_del_init(&call->accept_link);
397 sk_acceptq_removed(&rx->sk);
David Howellse34d4232016-08-30 09:49:29 +0100398 rxrpc_see_call(call);
David Howells651350d2007-04-26 15:50:17 -0700399
400 write_lock_bh(&call->state_lock);
401 switch (call->state) {
402 case RXRPC_CALL_SERVER_ACCEPTING:
David Howellsf5c17aa2016-08-30 09:49:28 +0100403 __rxrpc_set_call_completion(call, RXRPC_CALL_SERVER_BUSY,
404 0, ECONNABORTED);
David Howells4c198ad2016-03-04 15:53:46 +0000405 if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700406 rxrpc_queue_call(call);
407 ret = 0;
408 goto out_release;
David Howellsf5c17aa2016-08-30 09:49:28 +0100409 case RXRPC_CALL_COMPLETE:
410 ret = call->error;
David Howells651350d2007-04-26 15:50:17 -0700411 goto out_release;
412 case RXRPC_CALL_DEAD:
413 ret = -ETIME;
414 goto out_discard;
415 default:
416 BUG();
417 }
418
419 /* if the call is already dying or dead, then we leave the socket's ref
420 * on it to be released by rxrpc_dead_call_expired() as induced by
421 * rxrpc_release_call() */
422out_release:
423 _debug("release %p", call);
424 if (!test_bit(RXRPC_CALL_RELEASED, &call->flags) &&
David Howells4c198ad2016-03-04 15:53:46 +0000425 !test_and_set_bit(RXRPC_CALL_EV_RELEASE, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700426 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700427out_discard:
428 write_unlock_bh(&call->state_lock);
429 _debug("discard %p", call);
430out:
431 write_unlock(&rx->call_lock);
432 _leave(" = %d", ret);
433 return ret;
434}
David Howells651350d2007-04-26 15:50:17 -0700435
436/**
437 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
438 * @sock: The socket on which the impending call is waiting
439 * @user_call_ID: The tag to attach to the call
440 *
441 * Allow a kernel service to accept an incoming call, assuming the incoming
442 * call is still valid.
443 */
444struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
445 unsigned long user_call_ID)
446{
447 struct rxrpc_call *call;
448
449 _enter(",%lx", user_call_ID);
450 call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID);
451 _leave(" = %p", call);
452 return call;
453}
David Howells651350d2007-04-26 15:50:17 -0700454EXPORT_SYMBOL(rxrpc_kernel_accept_call);
455
456/**
457 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
458 * @sock: The socket on which the impending call is waiting
459 *
460 * Allow a kernel service to reject an incoming call with a BUSY message,
461 * assuming the incoming call is still valid.
462 */
463int rxrpc_kernel_reject_call(struct socket *sock)
464{
465 int ret;
466
467 _enter("");
468 ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
469 _leave(" = %d", ret);
470 return ret;
471}
David Howells651350d2007-04-26 15:50:17 -0700472EXPORT_SYMBOL(rxrpc_kernel_reject_call);