1 /* connection-level event handling
3 * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/skbuff.h>
15 #include <linux/errqueue.h>
16 #include <linux/udp.h>
18 #include <linux/in6.h>
19 #include <linux/icmp.h>
21 #include <net/af_rxrpc.h>
23 #include "ar-internal.h"
26 * pass a connection-level abort onto all calls on that connection
28 static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
31 struct rxrpc_call *call;
34 _enter("{%d},%x", conn->debug_id, abort_code);
36 read_lock_bh(&conn->lock);
38 for (p = rb_first(&conn->calls); p; p = rb_next(p)) {
39 call = rb_entry(p, struct rxrpc_call, conn_node);
40 write_lock(&call->state_lock);
41 if (call->state <= RXRPC_CALL_COMPLETE) {
43 call->abort_code = abort_code;
44 if (state == RXRPC_CALL_LOCALLY_ABORTED)
45 set_bit(RXRPC_CALL_CONN_ABORT, &call->events);
47 set_bit(RXRPC_CALL_RCVD_ABORT, &call->events);
48 rxrpc_queue_call(call);
50 write_unlock(&call->state_lock);
53 read_unlock_bh(&conn->lock);
58 * generate a connection-level abort
60 static int rxrpc_abort_connection(struct rxrpc_connection *conn,
61 u32 error, u32 abort_code)
63 struct rxrpc_header hdr;
70 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
72 /* generate a connection-level abort */
73 spin_lock_bh(&conn->state_lock);
74 if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
75 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
77 spin_unlock_bh(&conn->state_lock);
79 spin_unlock_bh(&conn->state_lock);
80 _leave(" = 0 [already dead]");
84 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);
86 msg.msg_name = &conn->trans->peer->srx.transport.sin;
87 msg.msg_namelen = sizeof(conn->trans->peer->srx.transport.sin);
88 msg.msg_control = NULL;
89 msg.msg_controllen = 0;
92 hdr.epoch = conn->epoch;
96 hdr.type = RXRPC_PACKET_TYPE_ABORT;
97 hdr.flags = conn->out_clientflag;
99 hdr.securityIndex = conn->security_ix;
101 hdr.serviceId = conn->service_id;
103 word = htonl(abort_code);
105 iov[0].iov_base = &hdr;
106 iov[0].iov_len = sizeof(hdr);
107 iov[1].iov_base = &word;
108 iov[1].iov_len = sizeof(word);
110 len = iov[0].iov_len + iov[1].iov_len;
112 hdr.serial = htonl(atomic_inc_return(&conn->serial));
113 _proto("Tx CONN ABORT %%%u { %d }", ntohl(hdr.serial), abort_code);
115 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
117 _debug("sendmsg failed: %d", ret);
126 * mark a call as being on a now-secured channel
127 * - must be called with softirqs disabled
129 void rxrpc_call_is_secure(struct rxrpc_call *call)
133 read_lock(&call->state_lock);
134 if (call->state < RXRPC_CALL_COMPLETE &&
135 !test_and_set_bit(RXRPC_CALL_SECURED, &call->events))
136 rxrpc_queue_call(call);
137 read_unlock(&call->state_lock);
142 * connection-level Rx packet processor
144 static int rxrpc_process_event(struct rxrpc_connection *conn,
148 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
153 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED)
154 return -ECONNABORTED;
156 serial = ntohl(sp->hdr.serial);
158 switch (sp->hdr.type) {
159 case RXRPC_PACKET_TYPE_ABORT:
160 if (skb_copy_bits(skb, 0, &tmp, sizeof(tmp)) < 0)
162 _proto("Rx ABORT %%%u { ac=%d }", serial, ntohl(tmp));
164 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
165 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
167 return -ECONNABORTED;
169 case RXRPC_PACKET_TYPE_CHALLENGE:
171 return conn->security->respond_to_challenge(
172 conn, skb, _abort_code);
175 case RXRPC_PACKET_TYPE_RESPONSE:
179 ret = conn->security->verify_response(conn, skb, _abort_code);
183 ret = conn->security->init_connection_security(conn);
187 conn->security->prime_packet_security(conn);
188 read_lock_bh(&conn->lock);
189 spin_lock(&conn->state_lock);
191 if (conn->state == RXRPC_CONN_SERVER_CHALLENGING) {
192 conn->state = RXRPC_CONN_SERVER;
193 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
194 rxrpc_call_is_secure(conn->channels[loop]);
197 spin_unlock(&conn->state_lock);
198 read_unlock_bh(&conn->lock);
207 * set up security and issue a challenge
209 static void rxrpc_secure_connection(struct rxrpc_connection *conn)
214 _enter("{%d}", conn->debug_id);
216 ASSERT(conn->security_ix != 0);
219 _debug("set up security");
220 ret = rxrpc_init_server_conn_security(conn);
225 abort_code = RX_CALL_DEAD;
228 abort_code = RXKADNOAUTH;
233 ASSERT(conn->security != NULL);
235 if (conn->security->issue_challenge(conn) < 0) {
236 abort_code = RX_CALL_DEAD;
245 _debug("abort %d, %d", ret, abort_code);
246 rxrpc_abort_connection(conn, -ret, abort_code);
247 _leave(" [aborted]");
251 * connection-level event processor
253 void rxrpc_process_connection(struct work_struct *work)
255 struct rxrpc_connection *conn =
256 container_of(work, struct rxrpc_connection, processor);
257 struct rxrpc_skb_priv *sp;
259 u32 abort_code = RX_PROTOCOL_ERROR;
262 _enter("{%d}", conn->debug_id);
264 atomic_inc(&conn->usage);
266 if (test_and_clear_bit(RXRPC_CONN_CHALLENGE, &conn->events)) {
267 rxrpc_secure_connection(conn);
268 rxrpc_put_connection(conn);
271 /* go through the conn-level event packets, releasing the ref on this
272 * connection that each one has when we've finished with it */
273 while ((skb = skb_dequeue(&conn->rx_queue))) {
276 ret = rxrpc_process_event(conn, skb, &abort_code);
283 goto requeue_and_leave;
286 rxrpc_put_connection(conn);
293 rxrpc_put_connection(conn);
298 skb_queue_head(&conn->rx_queue, skb);
302 if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
303 goto requeue_and_leave;
304 rxrpc_put_connection(conn);
311 * put a packet up for transport-level abort
313 void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
315 CHECK_SLAB_OKAY(&local->usage);
317 if (!atomic_inc_not_zero(&local->usage)) {
318 printk("resurrected on reject\n");
322 skb_queue_tail(&local->reject_queue, skb);
323 rxrpc_queue_work(&local->rejecter);
327 * reject packets through the local endpoint
329 void rxrpc_reject_packets(struct work_struct *work)
333 struct sockaddr_in sin;
335 struct rxrpc_skb_priv *sp;
336 struct rxrpc_header hdr;
337 struct rxrpc_local *local;
344 local = container_of(work, struct rxrpc_local, rejecter);
345 rxrpc_get_local(local);
347 _enter("%d", local->debug_id);
349 iov[0].iov_base = &hdr;
350 iov[0].iov_len = sizeof(hdr);
351 iov[1].iov_base = &code;
352 iov[1].iov_len = sizeof(code);
353 size = sizeof(hdr) + sizeof(code);
356 msg.msg_control = NULL;
357 msg.msg_controllen = 0;
360 memset(&sa, 0, sizeof(sa));
361 sa.sa.sa_family = local->srx.transport.family;
362 switch (sa.sa.sa_family) {
364 msg.msg_namelen = sizeof(sa.sin);
371 memset(&hdr, 0, sizeof(hdr));
372 hdr.type = RXRPC_PACKET_TYPE_ABORT;
374 while ((skb = skb_dequeue(&local->reject_queue))) {
376 switch (sa.sa.sa_family) {
378 sa.sin.sin_port = udp_hdr(skb)->source;
379 sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
380 code = htonl(skb->priority);
382 hdr.epoch = sp->hdr.epoch;
383 hdr.cid = sp->hdr.cid;
384 hdr.callNumber = sp->hdr.callNumber;
385 hdr.serviceId = sp->hdr.serviceId;
386 hdr.flags = sp->hdr.flags;
387 hdr.flags ^= RXRPC_CLIENT_INITIATED;
388 hdr.flags &= RXRPC_CLIENT_INITIATED;
390 kernel_sendmsg(local->socket, &msg, iov, 2, size);
398 rxrpc_put_local(local);
401 rxrpc_put_local(local);