call_accept.c 10.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* incoming call handling
 *
 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
 * Written by David Howells (dhowells@redhat.com)
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version
 * 2 of the License, or (at your option) any later version.
 */

12 13
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt

14 15 16 17 18 19 20 21
#include <linux/module.h>
#include <linux/net.h>
#include <linux/skbuff.h>
#include <linux/errqueue.h>
#include <linux/udp.h>
#include <linux/in.h>
#include <linux/in6.h>
#include <linux/icmp.h>
22
#include <linux/gfp.h>
23 24 25 26 27 28 29 30 31
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <net/ip.h>
#include "ar-internal.h"

/*
 * generate a connection-level abort
 */
static int rxrpc_busy(struct rxrpc_local *local, struct sockaddr_rxrpc *srx,
32
		      struct rxrpc_wire_header *whdr)
33 34 35 36 37 38 39 40
{
	struct msghdr msg;
	struct kvec iov[1];
	size_t len;
	int ret;

	_enter("%d,,", local->debug_id);

41 42 43
	whdr->type	= RXRPC_PACKET_TYPE_BUSY;
	whdr->serial	= htonl(1);

44 45 46 47 48 49
	msg.msg_name	= &srx->transport.sin;
	msg.msg_namelen	= sizeof(srx->transport.sin);
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

50 51
	iov[0].iov_base	= whdr;
	iov[0].iov_len	= sizeof(*whdr);
52 53 54

	len = iov[0].iov_len;

55
	_proto("Tx BUSY %%1");
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

	ret = kernel_sendmsg(local->socket, &msg, iov, 1, len);
	if (ret < 0) {
		_leave(" = -EAGAIN [sendmsg failed: %d]", ret);
		return -EAGAIN;
	}

	_leave(" = 0");
	return 0;
}

/*
 * accept an incoming call that needs peer, transport and/or connection setting
 * up
 */
static int rxrpc_accept_incoming_call(struct rxrpc_local *local,
				      struct rxrpc_sock *rx,
				      struct sk_buff *skb,
				      struct sockaddr_rxrpc *srx)
{
	struct rxrpc_connection *conn;
	struct rxrpc_skb_priv *sp, *nsp;
	struct rxrpc_call *call;
	struct sk_buff *notification;
	int ret;

	_enter("");

	sp = rxrpc_skb(skb);

	/* get a notification message to send to the server app */
	notification = alloc_skb(0, GFP_NOFS);
T
Tetsuo Handa 已提交
88 89 90 91 92
	if (!notification) {
		_debug("no memory");
		ret = -ENOMEM;
		goto error_nofree;
	}
93 94 95
	rxrpc_new_skb(notification);
	notification->mark = RXRPC_SKB_MARK_NEW_CALL;

96
	conn = rxrpc_incoming_connection(local, srx, skb);
97 98 99 100 101 102
	if (IS_ERR(conn)) {
		_debug("no conn");
		ret = PTR_ERR(conn);
		goto error;
	}

103
	call = rxrpc_incoming_call(rx, conn, skb);
104 105 106 107 108 109 110 111 112 113 114 115 116 117
	rxrpc_put_connection(conn);
	if (IS_ERR(call)) {
		_debug("no call");
		ret = PTR_ERR(call);
		goto error;
	}

	/* attach the call to the socket */
	read_lock_bh(&local->services_lock);
	if (rx->sk.sk_state == RXRPC_CLOSE)
		goto invalid_service;

	write_lock(&rx->call_lock);
	if (!test_and_set_bit(RXRPC_CALL_INIT_ACCEPT, &call->flags)) {
118
		rxrpc_get_call(call, rxrpc_call_got);
119 120 121

		spin_lock(&call->conn->state_lock);
		if (sp->hdr.securityIndex > 0 &&
122
		    call->conn->state == RXRPC_CONN_SERVICE_UNSECURED) {
123 124
			_debug("await conn sec");
			list_add_tail(&call->accept_link, &rx->secureq);
125 126
			call->conn->state = RXRPC_CONN_SERVICE_CHALLENGING;
			set_bit(RXRPC_CONN_EV_CHALLENGE, &call->conn->events);
127
			rxrpc_queue_conn(call->conn);
128 129 130 131
		} else {
			_debug("conn ready");
			call->state = RXRPC_CALL_SERVER_ACCEPTING;
			list_add_tail(&call->accept_link, &rx->acceptq);
D
David Howells 已提交
132
			rxrpc_get_call_for_skb(call, notification);
133 134 135 136 137 138 139 140 141 142 143
			nsp = rxrpc_skb(notification);
			nsp->call = call;

			ASSERTCMP(atomic_read(&call->usage), >=, 3);

			_debug("notify");
			spin_lock(&call->lock);
			ret = rxrpc_queue_rcv_skb(call, notification, true,
						  false);
			spin_unlock(&call->lock);
			notification = NULL;
J
Julia Lawall 已提交
144
			BUG_ON(ret < 0);
145 146 147 148 149 150 151 152 153 154 155 156 157
		}
		spin_unlock(&call->conn->state_lock);

		_debug("queued");
	}
	write_unlock(&rx->call_lock);

	_debug("process");
	rxrpc_fast_process_packet(call, skb);

	_debug("done");
	read_unlock_bh(&local->services_lock);
	rxrpc_free_skb(notification);
158
	rxrpc_put_call(call, rxrpc_call_put);
159 160 161 162 163 164 165
	_leave(" = 0");
	return 0;

invalid_service:
	_debug("invalid");
	read_unlock_bh(&local->services_lock);

166
	rxrpc_release_call(rx, call);
167
	rxrpc_put_call(call, rxrpc_call_put);
168 169 170
	ret = -ECONNREFUSED;
error:
	rxrpc_free_skb(notification);
T
Tetsuo Handa 已提交
171
error_nofree:
172 173 174 175 176 177 178 179
	_leave(" = %d", ret);
	return ret;
}

/*
 * accept incoming calls that need peer, transport and/or connection setting up
 * - the packets we get are all incoming client DATA packets that have seq == 1
 */
180
void rxrpc_accept_incoming_calls(struct rxrpc_local *local)
181 182 183 184
{
	struct rxrpc_skb_priv *sp;
	struct sockaddr_rxrpc srx;
	struct rxrpc_sock *rx;
185
	struct rxrpc_wire_header whdr;
186 187 188 189 190 191 192 193 194 195 196 197 198
	struct sk_buff *skb;
	int ret;

	_enter("%d", local->debug_id);

	skb = skb_dequeue(&local->accept_queue);
	if (!skb) {
		_leave("\n");
		return;
	}

	_net("incoming call skb %p", skb);

199
	rxrpc_see_skb(skb);
200 201
	sp = rxrpc_skb(skb);

202 203 204 205 206 207 208 209 210 211 212 213 214
	/* Set up a response packet header in case we need it */
	whdr.epoch	= htonl(sp->hdr.epoch);
	whdr.cid	= htonl(sp->hdr.cid);
	whdr.callNumber	= htonl(sp->hdr.callNumber);
	whdr.seq	= htonl(sp->hdr.seq);
	whdr.serial	= 0;
	whdr.flags	= 0;
	whdr.type	= 0;
	whdr.userStatus	= 0;
	whdr.securityIndex = sp->hdr.securityIndex;
	whdr._rsvd	= 0;
	whdr.serviceId	= htons(sp->hdr.serviceId);

215 216
	if (rxrpc_extract_addr_from_skb(&srx, skb) < 0)
		goto drop;
217 218 219

	/* get the socket providing the service */
	read_lock_bh(&local->services_lock);
220
	hlist_for_each_entry(rx, &local->services, listen_link) {
221
		if (rx->srx.srx_service == sp->hdr.serviceId &&
222 223 224 225 226 227 228
		    rx->sk.sk_state != RXRPC_CLOSE)
			goto found_service;
	}
	read_unlock_bh(&local->services_lock);
	goto invalid_service;

found_service:
229
	_debug("found service %hd", rx->srx.srx_service);
230 231 232 233 234 235 236 237 238 239 240 241
	if (sk_acceptq_is_full(&rx->sk))
		goto backlog_full;
	sk_acceptq_added(&rx->sk);
	read_unlock_bh(&local->services_lock);

	ret = rxrpc_accept_incoming_call(local, rx, skb, &srx);
	if (ret < 0)
		sk_acceptq_removed(&rx->sk);
	switch (ret) {
	case -ECONNRESET: /* old calls are ignored */
	case -ECONNABORTED: /* aborted calls are reaborted or ignored */
	case 0:
242
		return;
243 244 245 246 247 248 249 250 251 252 253 254 255
	case -ECONNREFUSED:
		goto invalid_service;
	case -EBUSY:
		goto busy;
	case -EKEYREJECTED:
		goto security_mismatch;
	default:
		BUG();
	}

backlog_full:
	read_unlock_bh(&local->services_lock);
busy:
256
	rxrpc_busy(local, &srx, &whdr);
257
	rxrpc_free_skb(skb);
258
	return;
259

260 261 262 263
drop:
	rxrpc_free_skb(skb);
	return;

264 265 266
invalid_service:
	skb->priority = RX_INVALID_OPERATION;
	rxrpc_reject_packet(local, skb);
267
	return;
268 269 270 271 272

	/* can't change connection security type mid-flow */
security_mismatch:
	skb->priority = RX_PROTOCOL_ERROR;
	rxrpc_reject_packet(local, skb);
273
	return;
274 275 276 277 278 279
}

/*
 * handle acceptance of a call by userspace
 * - assign the user call ID to the call at the front of the queue
 */
280
struct rxrpc_call *rxrpc_accept_call(struct rxrpc_sock *rx,
281 282
				     unsigned long user_call_ID,
				     rxrpc_notify_rx_t notify_rx)
283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
{
	struct rxrpc_call *call;
	struct rb_node *parent, **pp;
	int ret;

	_enter(",%lx", user_call_ID);

	ASSERT(!irqs_disabled());

	write_lock(&rx->call_lock);

	ret = -ENODATA;
	if (list_empty(&rx->acceptq))
		goto out;

	/* check the user ID isn't already in use */
	ret = -EBADSLT;
	pp = &rx->calls.rb_node;
	parent = NULL;
	while (*pp) {
		parent = *pp;
		call = rb_entry(parent, struct rxrpc_call, sock_node);

		if (user_call_ID < call->user_call_ID)
			pp = &(*pp)->rb_left;
		else if (user_call_ID > call->user_call_ID)
			pp = &(*pp)->rb_right;
		else
			goto out;
	}

	/* dequeue the first call and check it's still valid */
	call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
	list_del_init(&call->accept_link);
	sk_acceptq_removed(&rx->sk);
D
David Howells 已提交
318
	rxrpc_see_call(call);
319 320 321 322 323 324

	write_lock_bh(&call->state_lock);
	switch (call->state) {
	case RXRPC_CALL_SERVER_ACCEPTING:
		call->state = RXRPC_CALL_SERVER_RECV_REQUEST;
		break;
325 326
	case RXRPC_CALL_COMPLETE:
		ret = call->error;
327 328 329 330 331 332
		goto out_release;
	default:
		BUG();
	}

	/* formalise the acceptance */
333
	rxrpc_get_call(call, rxrpc_call_got_userid);
334
	call->notify_rx = notify_rx;
335 336 337 338 339
	call->user_call_ID = user_call_ID;
	rb_link_node(&call->sock_node, parent, pp);
	rb_insert_color(&call->sock_node, &rx->calls);
	if (test_and_set_bit(RXRPC_CALL_HAS_USERID, &call->flags))
		BUG();
340
	if (test_and_set_bit(RXRPC_CALL_EV_ACCEPTED, &call->events))
341 342 343 344
		BUG();

	write_unlock_bh(&call->state_lock);
	write_unlock(&rx->call_lock);
345
	rxrpc_queue_call(call);
346 347 348 349 350
	_leave(" = %p{%d}", call, call->debug_id);
	return call;

out_release:
	write_unlock_bh(&call->state_lock);
351 352 353 354 355
	write_unlock(&rx->call_lock);
	_debug("release %p", call);
	rxrpc_release_call(rx, call);
	_leave(" = %d", ret);
	return ERR_PTR(ret);
356 357 358 359 360 361 362
out:
	write_unlock(&rx->call_lock);
	_leave(" = %d", ret);
	return ERR_PTR(ret);
}

/*
363
 * Handle rejection of a call by userspace
364 365 366 367 368 369 370 371 372 373 374 375 376 377
 * - reject the call at the front of the queue
 */
int rxrpc_reject_call(struct rxrpc_sock *rx)
{
	struct rxrpc_call *call;
	int ret;

	_enter("");

	ASSERT(!irqs_disabled());

	write_lock(&rx->call_lock);

	ret = -ENODATA;
378 379 380 381 382
	if (list_empty(&rx->acceptq)) {
		write_unlock(&rx->call_lock);
		_leave(" = -ENODATA");
		return -ENODATA;
	}
383 384 385 386 387

	/* dequeue the first call and check it's still valid */
	call = list_entry(rx->acceptq.next, struct rxrpc_call, accept_link);
	list_del_init(&call->accept_link);
	sk_acceptq_removed(&rx->sk);
D
David Howells 已提交
388
	rxrpc_see_call(call);
389 390 391 392

	write_lock_bh(&call->state_lock);
	switch (call->state) {
	case RXRPC_CALL_SERVER_ACCEPTING:
393 394
		__rxrpc_set_call_completion(call, RXRPC_CALL_SERVER_BUSY,
					    0, ECONNABORTED);
395
		if (test_and_set_bit(RXRPC_CALL_EV_REJECT_BUSY, &call->events))
396 397
			rxrpc_queue_call(call);
		ret = 0;
398
		break;
399 400
	case RXRPC_CALL_COMPLETE:
		ret = call->error;
401
		break;
402 403 404
	default:
		BUG();
	}
405 406 407

	write_unlock_bh(&call->state_lock);
	write_unlock(&rx->call_lock);
408
	rxrpc_release_call(rx, call);
409 410 411
	_leave(" = %d", ret);
	return ret;
}
412 413 414 415 416

/**
 * rxrpc_kernel_accept_call - Allow a kernel service to accept an incoming call
 * @sock: The socket on which the impending call is waiting
 * @user_call_ID: The tag to attach to the call
417
 * @notify_rx: Where to send notifications instead of socket queue
418 419
 *
 * Allow a kernel service to accept an incoming call, assuming the incoming
420 421
 * call is still valid.  The caller should immediately trigger their own
 * notification as there must be data waiting.
422 423
 */
struct rxrpc_call *rxrpc_kernel_accept_call(struct socket *sock,
424 425
					    unsigned long user_call_ID,
					    rxrpc_notify_rx_t notify_rx)
426 427 428 429
{
	struct rxrpc_call *call;

	_enter(",%lx", user_call_ID);
430
	call = rxrpc_accept_call(rxrpc_sk(sock->sk), user_call_ID, notify_rx);
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
	_leave(" = %p", call);
	return call;
}
EXPORT_SYMBOL(rxrpc_kernel_accept_call);

/**
 * rxrpc_kernel_reject_call - Allow a kernel service to reject an incoming call
 * @sock: The socket on which the impending call is waiting
 *
 * Allow a kernel service to reject an incoming call with a BUSY message,
 * assuming the incoming call is still valid.
 */
int rxrpc_kernel_reject_call(struct socket *sock)
{
	int ret;

	_enter("");
	ret = rxrpc_reject_call(rxrpc_sk(sock->sk));
	_leave(" = %d", ret);
	return ret;
}
EXPORT_SYMBOL(rxrpc_kernel_reject_call);