conn_event.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* connection-level event 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 22 23 24 25 26 27 28 29 30 31 32 33
#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>
#include <net/sock.h>
#include <net/af_rxrpc.h>
#include <net/ip.h>
#include "ar-internal.h"

/*
 * pass a connection-level abort onto all calls on that connection
 */
static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
			      u32 abort_code)
{
	struct rxrpc_call *call;
34
	int i;
35 36 37

	_enter("{%d},%x", conn->debug_id, abort_code);

38
	spin_lock(&conn->channel_lock);
39

40 41 42 43 44
	for (i = 0; i < RXRPC_MAXCALLS; i++) {
		call = rcu_dereference_protected(
			conn->channels[i].call,
			lockdep_is_held(&conn->channel_lock));
		write_lock_bh(&call->state_lock);
45 46
		if (call->state <= RXRPC_CALL_COMPLETE) {
			call->state = state;
47 48
			if (state == RXRPC_CALL_LOCALLY_ABORTED) {
				call->local_abort = conn->local_abort;
49
				set_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
50 51
			} else {
				call->remote_abort = conn->remote_abort;
52
				set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
53
			}
54
			rxrpc_queue_call(call);
55
		}
56
		write_unlock_bh(&call->state_lock);
57 58
	}

59
	spin_unlock(&conn->channel_lock);
60 61 62 63 64 65 66 67 68
	_leave("");
}

/*
 * generate a connection-level abort
 */
static int rxrpc_abort_connection(struct rxrpc_connection *conn,
				  u32 error, u32 abort_code)
{
69
	struct rxrpc_wire_header whdr;
70 71 72 73
	struct msghdr msg;
	struct kvec iov[2];
	__be32 word;
	size_t len;
74
	u32 serial;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	int ret;

	_enter("%d,,%u,%u", conn->debug_id, error, abort_code);

	/* generate a connection-level abort */
	spin_lock_bh(&conn->state_lock);
	if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
		conn->state = RXRPC_CONN_LOCALLY_ABORTED;
		conn->error = error;
		spin_unlock_bh(&conn->state_lock);
	} else {
		spin_unlock_bh(&conn->state_lock);
		_leave(" = 0 [already dead]");
		return 0;
	}

	rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);

93 94
	msg.msg_name	= &conn->params.peer->srx.transport;
	msg.msg_namelen	= conn->params.peer->srx.transport_len;
95 96 97 98
	msg.msg_control	= NULL;
	msg.msg_controllen = 0;
	msg.msg_flags	= 0;

99 100
	whdr.epoch	= htonl(conn->proto.epoch);
	whdr.cid	= htonl(conn->proto.cid);
101 102 103 104 105 106 107
	whdr.callNumber	= 0;
	whdr.seq	= 0;
	whdr.type	= RXRPC_PACKET_TYPE_ABORT;
	whdr.flags	= conn->out_clientflag;
	whdr.userStatus	= 0;
	whdr.securityIndex = conn->security_ix;
	whdr._rsvd	= 0;
108
	whdr.serviceId	= htons(conn->params.service_id);
109

110
	word		= htonl(conn->local_abort);
111

112 113
	iov[0].iov_base	= &whdr;
	iov[0].iov_len	= sizeof(whdr);
114 115 116 117 118
	iov[1].iov_base	= &word;
	iov[1].iov_len	= sizeof(word);

	len = iov[0].iov_len + iov[1].iov_len;

119 120
	serial = atomic_inc_return(&conn->serial);
	whdr.serial = htonl(serial);
121
	_proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
122

123
	ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
124 125 126 127 128 129 130 131 132 133 134 135 136
	if (ret < 0) {
		_debug("sendmsg failed: %d", ret);
		return -EAGAIN;
	}

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

/*
 * mark a call as being on a now-secured channel
 * - must be called with softirqs disabled
 */
R
Roel Kluin 已提交
137
static void rxrpc_call_is_secure(struct rxrpc_call *call)
138 139 140 141 142
{
	_enter("%p", call);
	if (call) {
		read_lock(&call->state_lock);
		if (call->state < RXRPC_CALL_COMPLETE &&
143
		    !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events))
144
			rxrpc_queue_call(call);
145 146 147 148 149 150 151 152 153 154 155 156
		read_unlock(&call->state_lock);
	}
}

/*
 * connection-level Rx packet processor
 */
static int rxrpc_process_event(struct rxrpc_connection *conn,
			       struct sk_buff *skb,
			       u32 *_abort_code)
{
	struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
157 158
	__be32 wtmp;
	u32 abort_code;
159 160
	int loop, ret;

161 162
	if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
		kleave(" = -ECONNABORTED [%u]", conn->state);
163
		return -ECONNABORTED;
164
	}
165

166
	_enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
167

168 169
	switch (sp->hdr.type) {
	case RXRPC_PACKET_TYPE_ABORT:
170
		if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
171
			return -EPROTO;
172 173
		abort_code = ntohl(wtmp);
		_proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
174 175 176

		conn->state = RXRPC_CONN_REMOTELY_ABORTED;
		rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
177
				  abort_code);
178 179 180
		return -ECONNABORTED;

	case RXRPC_PACKET_TYPE_CHALLENGE:
181 182
		return conn->security->respond_to_challenge(conn, skb,
							    _abort_code);
183 184 185 186 187 188 189 190 191 192

	case RXRPC_PACKET_TYPE_RESPONSE:
		ret = conn->security->verify_response(conn, skb, _abort_code);
		if (ret < 0)
			return ret;

		ret = conn->security->init_connection_security(conn);
		if (ret < 0)
			return ret;

193 194 195 196
		ret = conn->security->prime_packet_security(conn);
		if (ret < 0)
			return ret;

197
		spin_lock(&conn->channel_lock);
198 199
		spin_lock(&conn->state_lock);

200 201
		if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
			conn->state = RXRPC_CONN_SERVICE;
202
			for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
203 204
				rxrpc_call_is_secure(
					rcu_dereference_protected(
205 206
						conn->channels[loop].call,
						lockdep_is_held(&conn->channel_lock)));
207 208 209
		}

		spin_unlock(&conn->state_lock);
210
		spin_unlock(&conn->channel_lock);
211 212 213
		return 0;

	default:
214
		_leave(" = -EPROTO [%u]", sp->hdr.type);
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
		return -EPROTO;
	}
}

/*
 * set up security and issue a challenge
 */
static void rxrpc_secure_connection(struct rxrpc_connection *conn)
{
	u32 abort_code;
	int ret;

	_enter("{%d}", conn->debug_id);

	ASSERT(conn->security_ix != 0);

231
	if (!conn->params.key) {
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
		_debug("set up security");
		ret = rxrpc_init_server_conn_security(conn);
		switch (ret) {
		case 0:
			break;
		case -ENOENT:
			abort_code = RX_CALL_DEAD;
			goto abort;
		default:
			abort_code = RXKADNOAUTH;
			goto abort;
		}
	}

	if (conn->security->issue_challenge(conn) < 0) {
		abort_code = RX_CALL_DEAD;
		ret = -ENOMEM;
		goto abort;
	}

	_leave("");
	return;

abort:
	_debug("abort %d, %d", ret, abort_code);
	rxrpc_abort_connection(conn, -ret, abort_code);
	_leave(" [aborted]");
}

/*
 * connection-level event processor
 */
void rxrpc_process_connection(struct work_struct *work)
{
	struct rxrpc_connection *conn =
		container_of(work, struct rxrpc_connection, processor);
	struct sk_buff *skb;
	u32 abort_code = RX_PROTOCOL_ERROR;
	int ret;

	_enter("{%d}", conn->debug_id);

274
	if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
275 276 277 278 279
		rxrpc_secure_connection(conn);

	/* go through the conn-level event packets, releasing the ref on this
	 * connection that each one has when we've finished with it */
	while ((skb = skb_dequeue(&conn->rx_queue))) {
280
		rxrpc_see_skb(skb);
281 282 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
		ret = rxrpc_process_event(conn, skb, &abort_code);
		switch (ret) {
		case -EPROTO:
		case -EKEYEXPIRED:
		case -EKEYREJECTED:
			goto protocol_error;
		case -EAGAIN:
			goto requeue_and_leave;
		case -ECONNABORTED:
		default:
			rxrpc_free_skb(skb);
			break;
		}
	}

out:
	rxrpc_put_connection(conn);
	_leave("");
	return;

requeue_and_leave:
	skb_queue_head(&conn->rx_queue, skb);
	goto out;

protocol_error:
	if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
		goto requeue_and_leave;
	rxrpc_free_skb(skb);
	_leave(" [EPROTO]");
	goto out;
}

313 314 315 316 317 318 319 320
/*
 * put a packet up for transport-level abort
 */
void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
{
	CHECK_SLAB_OKAY(&local->usage);

	skb_queue_tail(&local->reject_queue, skb);
321
	rxrpc_queue_local(local);
322 323
}

324 325 326
/*
 * reject packets through the local endpoint
 */
327
void rxrpc_reject_packets(struct rxrpc_local *local)
328 329 330 331 332 333
{
	union {
		struct sockaddr sa;
		struct sockaddr_in sin;
	} sa;
	struct rxrpc_skb_priv *sp;
334
	struct rxrpc_wire_header whdr;
335 336 337 338 339 340 341 342
	struct sk_buff *skb;
	struct msghdr msg;
	struct kvec iov[2];
	size_t size;
	__be32 code;

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

343 344
	iov[0].iov_base = &whdr;
	iov[0].iov_len = sizeof(whdr);
345 346
	iov[1].iov_base = &code;
	iov[1].iov_len = sizeof(code);
347
	size = sizeof(whdr) + sizeof(code);
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364

	msg.msg_name = &sa;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_flags = 0;

	memset(&sa, 0, sizeof(sa));
	sa.sa.sa_family = local->srx.transport.family;
	switch (sa.sa.sa_family) {
	case AF_INET:
		msg.msg_namelen = sizeof(sa.sin);
		break;
	default:
		msg.msg_namelen = 0;
		break;
	}

365 366
	memset(&whdr, 0, sizeof(whdr));
	whdr.type = RXRPC_PACKET_TYPE_ABORT;
367 368

	while ((skb = skb_dequeue(&local->reject_queue))) {
369
		rxrpc_see_skb(skb);
370 371 372 373 374 375 376
		sp = rxrpc_skb(skb);
		switch (sa.sa.sa_family) {
		case AF_INET:
			sa.sin.sin_port = udp_hdr(skb)->source;
			sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
			code = htonl(skb->priority);

377 378 379 380 381 382 383
			whdr.epoch	= htonl(sp->hdr.epoch);
			whdr.cid	= htonl(sp->hdr.cid);
			whdr.callNumber	= htonl(sp->hdr.callNumber);
			whdr.serviceId	= htons(sp->hdr.serviceId);
			whdr.flags	= sp->hdr.flags;
			whdr.flags	^= RXRPC_CLIENT_INITIATED;
			whdr.flags	&= RXRPC_CLIENT_INITIATED;
384 385 386 387 388 389 390 391 392 393 394 395 396

			kernel_sendmsg(local->socket, &msg, iov, 2, size);
			break;

		default:
			break;
		}

		rxrpc_free_skb(skb);
	}

	_leave("");
}