blob: 5f9563968a5b498e837bef9baf2921867a8073da [file] [log] [blame]
David Howells17926a72007-04-26 15:48:28 -07001/* connection-level event 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
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>
17#include <linux/in.h>
18#include <linux/in6.h>
19#include <linux/icmp.h>
20#include <net/sock.h>
21#include <net/af_rxrpc.h>
22#include <net/ip.h>
23#include "ar-internal.h"
24
25/*
26 * pass a connection-level abort onto all calls on that connection
27 */
28static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
29 u32 abort_code)
30{
31 struct rxrpc_call *call;
32 struct rb_node *p;
33
34 _enter("{%d},%x", conn->debug_id, abort_code);
35
36 read_lock_bh(&conn->lock);
37
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) {
42 call->state = state;
David Howellsdc44b3a2016-04-07 17:23:30 +010043 if (state == RXRPC_CALL_LOCALLY_ABORTED) {
44 call->local_abort = conn->local_abort;
David Howells4c198ad2016-03-04 15:53:46 +000045 set_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
David Howellsdc44b3a2016-04-07 17:23:30 +010046 } else {
47 call->remote_abort = conn->remote_abort;
David Howells4c198ad2016-03-04 15:53:46 +000048 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howellsdc44b3a2016-04-07 17:23:30 +010049 }
David Howells651350d2007-04-26 15:50:17 -070050 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -070051 }
52 write_unlock(&call->state_lock);
53 }
54
55 read_unlock_bh(&conn->lock);
56 _leave("");
57}
58
59/*
60 * generate a connection-level abort
61 */
62static int rxrpc_abort_connection(struct rxrpc_connection *conn,
63 u32 error, u32 abort_code)
64{
David Howells0d12f8a2016-03-04 15:53:46 +000065 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -070066 struct msghdr msg;
67 struct kvec iov[2];
68 __be32 word;
69 size_t len;
David Howells0d12f8a2016-03-04 15:53:46 +000070 u32 serial;
David Howells17926a72007-04-26 15:48:28 -070071 int ret;
72
73 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
74
75 /* generate a connection-level abort */
76 spin_lock_bh(&conn->state_lock);
77 if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
78 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
79 conn->error = error;
80 spin_unlock_bh(&conn->state_lock);
81 } else {
82 spin_unlock_bh(&conn->state_lock);
83 _leave(" = 0 [already dead]");
84 return 0;
85 }
86
87 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);
88
David Howells6dd050f2016-04-07 17:23:44 +010089 msg.msg_name = &conn->trans->peer->srx.transport;
90 msg.msg_namelen = conn->trans->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -070091 msg.msg_control = NULL;
92 msg.msg_controllen = 0;
93 msg.msg_flags = 0;
94
David Howells0d12f8a2016-03-04 15:53:46 +000095 whdr.epoch = htonl(conn->epoch);
96 whdr.cid = htonl(conn->cid);
97 whdr.callNumber = 0;
98 whdr.seq = 0;
99 whdr.type = RXRPC_PACKET_TYPE_ABORT;
100 whdr.flags = conn->out_clientflag;
101 whdr.userStatus = 0;
102 whdr.securityIndex = conn->security_ix;
103 whdr._rsvd = 0;
104 whdr.serviceId = htons(conn->service_id);
David Howells17926a72007-04-26 15:48:28 -0700105
David Howellsdc44b3a2016-04-07 17:23:30 +0100106 word = htonl(conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700107
David Howells0d12f8a2016-03-04 15:53:46 +0000108 iov[0].iov_base = &whdr;
109 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700110 iov[1].iov_base = &word;
111 iov[1].iov_len = sizeof(word);
112
113 len = iov[0].iov_len + iov[1].iov_len;
114
David Howells0d12f8a2016-03-04 15:53:46 +0000115 serial = atomic_inc_return(&conn->serial);
116 whdr.serial = htonl(serial);
David Howellsdc44b3a2016-04-07 17:23:30 +0100117 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700118
119 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
120 if (ret < 0) {
121 _debug("sendmsg failed: %d", ret);
122 return -EAGAIN;
123 }
124
125 _leave(" = 0");
126 return 0;
127}
128
129/*
130 * mark a call as being on a now-secured channel
131 * - must be called with softirqs disabled
132 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800133static void rxrpc_call_is_secure(struct rxrpc_call *call)
David Howells17926a72007-04-26 15:48:28 -0700134{
135 _enter("%p", call);
136 if (call) {
137 read_lock(&call->state_lock);
138 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000139 !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700140 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700141 read_unlock(&call->state_lock);
142 }
143}
144
145/*
146 * connection-level Rx packet processor
147 */
148static int rxrpc_process_event(struct rxrpc_connection *conn,
149 struct sk_buff *skb,
150 u32 *_abort_code)
151{
152 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000153 __be32 wtmp;
154 u32 abort_code;
David Howells17926a72007-04-26 15:48:28 -0700155 int loop, ret;
156
David Howells519d2562009-06-16 21:36:44 +0100157 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
158 kleave(" = -ECONNABORTED [%u]", conn->state);
David Howells17926a72007-04-26 15:48:28 -0700159 return -ECONNABORTED;
David Howells519d2562009-06-16 21:36:44 +0100160 }
David Howells17926a72007-04-26 15:48:28 -0700161
David Howells0d12f8a2016-03-04 15:53:46 +0000162 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
David Howells519d2562009-06-16 21:36:44 +0100163
David Howells17926a72007-04-26 15:48:28 -0700164 switch (sp->hdr.type) {
165 case RXRPC_PACKET_TYPE_ABORT:
David Howells0d12f8a2016-03-04 15:53:46 +0000166 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
David Howells17926a72007-04-26 15:48:28 -0700167 return -EPROTO;
David Howells0d12f8a2016-03-04 15:53:46 +0000168 abort_code = ntohl(wtmp);
169 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
David Howells17926a72007-04-26 15:48:28 -0700170
171 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
172 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
David Howells0d12f8a2016-03-04 15:53:46 +0000173 abort_code);
David Howells17926a72007-04-26 15:48:28 -0700174 return -ECONNABORTED;
175
176 case RXRPC_PACKET_TYPE_CHALLENGE:
David Howellse0e4d822016-04-07 17:23:58 +0100177 return conn->security->respond_to_challenge(conn, skb,
178 _abort_code);
David Howells17926a72007-04-26 15:48:28 -0700179
180 case RXRPC_PACKET_TYPE_RESPONSE:
David Howells17926a72007-04-26 15:48:28 -0700181 ret = conn->security->verify_response(conn, skb, _abort_code);
182 if (ret < 0)
183 return ret;
184
185 ret = conn->security->init_connection_security(conn);
186 if (ret < 0)
187 return ret;
188
189 conn->security->prime_packet_security(conn);
190 read_lock_bh(&conn->lock);
191 spin_lock(&conn->state_lock);
192
193 if (conn->state == RXRPC_CONN_SERVER_CHALLENGING) {
194 conn->state = RXRPC_CONN_SERVER;
195 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
196 rxrpc_call_is_secure(conn->channels[loop]);
197 }
198
199 spin_unlock(&conn->state_lock);
200 read_unlock_bh(&conn->lock);
201 return 0;
202
203 default:
David Howells519d2562009-06-16 21:36:44 +0100204 _leave(" = -EPROTO [%u]", sp->hdr.type);
David Howells17926a72007-04-26 15:48:28 -0700205 return -EPROTO;
206 }
207}
208
209/*
210 * set up security and issue a challenge
211 */
212static void rxrpc_secure_connection(struct rxrpc_connection *conn)
213{
214 u32 abort_code;
215 int ret;
216
217 _enter("{%d}", conn->debug_id);
218
219 ASSERT(conn->security_ix != 0);
220
221 if (!conn->key) {
222 _debug("set up security");
223 ret = rxrpc_init_server_conn_security(conn);
224 switch (ret) {
225 case 0:
226 break;
227 case -ENOENT:
228 abort_code = RX_CALL_DEAD;
229 goto abort;
230 default:
231 abort_code = RXKADNOAUTH;
232 goto abort;
233 }
234 }
235
David Howells17926a72007-04-26 15:48:28 -0700236 if (conn->security->issue_challenge(conn) < 0) {
237 abort_code = RX_CALL_DEAD;
238 ret = -ENOMEM;
239 goto abort;
240 }
241
242 _leave("");
243 return;
244
245abort:
246 _debug("abort %d, %d", ret, abort_code);
247 rxrpc_abort_connection(conn, -ret, abort_code);
248 _leave(" [aborted]");
249}
250
251/*
252 * connection-level event processor
253 */
254void rxrpc_process_connection(struct work_struct *work)
255{
256 struct rxrpc_connection *conn =
257 container_of(work, struct rxrpc_connection, processor);
David Howells17926a72007-04-26 15:48:28 -0700258 struct sk_buff *skb;
259 u32 abort_code = RX_PROTOCOL_ERROR;
260 int ret;
261
262 _enter("{%d}", conn->debug_id);
263
264 atomic_inc(&conn->usage);
265
266 if (test_and_clear_bit(RXRPC_CONN_CHALLENGE, &conn->events)) {
267 rxrpc_secure_connection(conn);
268 rxrpc_put_connection(conn);
269 }
270
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))) {
David Howells17926a72007-04-26 15:48:28 -0700274 ret = rxrpc_process_event(conn, skb, &abort_code);
275 switch (ret) {
276 case -EPROTO:
277 case -EKEYEXPIRED:
278 case -EKEYREJECTED:
279 goto protocol_error;
280 case -EAGAIN:
281 goto requeue_and_leave;
282 case -ECONNABORTED:
283 default:
284 rxrpc_put_connection(conn);
285 rxrpc_free_skb(skb);
286 break;
287 }
288 }
289
290out:
291 rxrpc_put_connection(conn);
292 _leave("");
293 return;
294
295requeue_and_leave:
296 skb_queue_head(&conn->rx_queue, skb);
297 goto out;
298
299protocol_error:
300 if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
301 goto requeue_and_leave;
302 rxrpc_put_connection(conn);
303 rxrpc_free_skb(skb);
304 _leave(" [EPROTO]");
305 goto out;
306}
307
308/*
David Howells651350d2007-04-26 15:50:17 -0700309 * put a packet up for transport-level abort
310 */
311void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
312{
313 CHECK_SLAB_OKAY(&local->usage);
314
315 if (!atomic_inc_not_zero(&local->usage)) {
316 printk("resurrected on reject\n");
317 BUG();
318 }
319
320 skb_queue_tail(&local->reject_queue, skb);
321 rxrpc_queue_work(&local->rejecter);
322}
323
324/*
David Howells17926a72007-04-26 15:48:28 -0700325 * reject packets through the local endpoint
326 */
327void rxrpc_reject_packets(struct work_struct *work)
328{
329 union {
330 struct sockaddr sa;
331 struct sockaddr_in sin;
332 } sa;
333 struct rxrpc_skb_priv *sp;
David Howells0d12f8a2016-03-04 15:53:46 +0000334 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700335 struct rxrpc_local *local;
336 struct sk_buff *skb;
337 struct msghdr msg;
338 struct kvec iov[2];
339 size_t size;
340 __be32 code;
341
342 local = container_of(work, struct rxrpc_local, rejecter);
343 rxrpc_get_local(local);
344
345 _enter("%d", local->debug_id);
346
David Howells0d12f8a2016-03-04 15:53:46 +0000347 iov[0].iov_base = &whdr;
348 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700349 iov[1].iov_base = &code;
350 iov[1].iov_len = sizeof(code);
David Howells0d12f8a2016-03-04 15:53:46 +0000351 size = sizeof(whdr) + sizeof(code);
David Howells17926a72007-04-26 15:48:28 -0700352
353 msg.msg_name = &sa;
354 msg.msg_control = NULL;
355 msg.msg_controllen = 0;
356 msg.msg_flags = 0;
357
358 memset(&sa, 0, sizeof(sa));
359 sa.sa.sa_family = local->srx.transport.family;
360 switch (sa.sa.sa_family) {
361 case AF_INET:
362 msg.msg_namelen = sizeof(sa.sin);
363 break;
364 default:
365 msg.msg_namelen = 0;
366 break;
367 }
368
David Howells0d12f8a2016-03-04 15:53:46 +0000369 memset(&whdr, 0, sizeof(whdr));
370 whdr.type = RXRPC_PACKET_TYPE_ABORT;
David Howells17926a72007-04-26 15:48:28 -0700371
372 while ((skb = skb_dequeue(&local->reject_queue))) {
373 sp = rxrpc_skb(skb);
374 switch (sa.sa.sa_family) {
375 case AF_INET:
376 sa.sin.sin_port = udp_hdr(skb)->source;
377 sa.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
378 code = htonl(skb->priority);
379
David Howells0d12f8a2016-03-04 15:53:46 +0000380 whdr.epoch = htonl(sp->hdr.epoch);
381 whdr.cid = htonl(sp->hdr.cid);
382 whdr.callNumber = htonl(sp->hdr.callNumber);
383 whdr.serviceId = htons(sp->hdr.serviceId);
384 whdr.flags = sp->hdr.flags;
385 whdr.flags ^= RXRPC_CLIENT_INITIATED;
386 whdr.flags &= RXRPC_CLIENT_INITIATED;
David Howells17926a72007-04-26 15:48:28 -0700387
388 kernel_sendmsg(local->socket, &msg, iov, 2, size);
389 break;
390
391 default:
392 break;
393 }
394
395 rxrpc_free_skb(skb);
396 rxrpc_put_local(local);
397 }
398
399 rxrpc_put_local(local);
400 _leave("");
401}