blob: 8bdd692d486294003631caf0d5d143875e031c78 [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
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>
22#include <net/sock.h>
23#include <net/af_rxrpc.h>
24#include <net/ip.h>
25#include "ar-internal.h"
26
27/*
28 * pass a connection-level abort onto all calls on that connection
29 */
30static void rxrpc_abort_calls(struct rxrpc_connection *conn, int state,
31 u32 abort_code)
32{
33 struct rxrpc_call *call;
34 struct rb_node *p;
35
36 _enter("{%d},%x", conn->debug_id, abort_code);
37
38 read_lock_bh(&conn->lock);
39
40 for (p = rb_first(&conn->calls); p; p = rb_next(p)) {
41 call = rb_entry(p, struct rxrpc_call, conn_node);
42 write_lock(&call->state_lock);
43 if (call->state <= RXRPC_CALL_COMPLETE) {
44 call->state = state;
David Howellsdc44b3a2016-04-07 17:23:30 +010045 if (state == RXRPC_CALL_LOCALLY_ABORTED) {
46 call->local_abort = conn->local_abort;
David Howells4c198ad2016-03-04 15:53:46 +000047 set_bit(RXRPC_CALL_EV_CONN_ABORT, &call->events);
David Howellsdc44b3a2016-04-07 17:23:30 +010048 } else {
49 call->remote_abort = conn->remote_abort;
David Howells4c198ad2016-03-04 15:53:46 +000050 set_bit(RXRPC_CALL_EV_RCVD_ABORT, &call->events);
David Howellsdc44b3a2016-04-07 17:23:30 +010051 }
David Howells651350d2007-04-26 15:50:17 -070052 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -070053 }
54 write_unlock(&call->state_lock);
55 }
56
57 read_unlock_bh(&conn->lock);
58 _leave("");
59}
60
61/*
62 * generate a connection-level abort
63 */
64static int rxrpc_abort_connection(struct rxrpc_connection *conn,
65 u32 error, u32 abort_code)
66{
David Howells0d12f8a2016-03-04 15:53:46 +000067 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -070068 struct msghdr msg;
69 struct kvec iov[2];
70 __be32 word;
71 size_t len;
David Howells0d12f8a2016-03-04 15:53:46 +000072 u32 serial;
David Howells17926a72007-04-26 15:48:28 -070073 int ret;
74
75 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
76
77 /* generate a connection-level abort */
78 spin_lock_bh(&conn->state_lock);
79 if (conn->state < RXRPC_CONN_REMOTELY_ABORTED) {
80 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
81 conn->error = error;
82 spin_unlock_bh(&conn->state_lock);
83 } else {
84 spin_unlock_bh(&conn->state_lock);
85 _leave(" = 0 [already dead]");
86 return 0;
87 }
88
89 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code);
90
David Howells6dd050f2016-04-07 17:23:44 +010091 msg.msg_name = &conn->trans->peer->srx.transport;
92 msg.msg_namelen = conn->trans->peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -070093 msg.msg_control = NULL;
94 msg.msg_controllen = 0;
95 msg.msg_flags = 0;
96
David Howells0d12f8a2016-03-04 15:53:46 +000097 whdr.epoch = htonl(conn->epoch);
98 whdr.cid = htonl(conn->cid);
99 whdr.callNumber = 0;
100 whdr.seq = 0;
101 whdr.type = RXRPC_PACKET_TYPE_ABORT;
102 whdr.flags = conn->out_clientflag;
103 whdr.userStatus = 0;
104 whdr.securityIndex = conn->security_ix;
105 whdr._rsvd = 0;
106 whdr.serviceId = htons(conn->service_id);
David Howells17926a72007-04-26 15:48:28 -0700107
David Howellsdc44b3a2016-04-07 17:23:30 +0100108 word = htonl(conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700109
David Howells0d12f8a2016-03-04 15:53:46 +0000110 iov[0].iov_base = &whdr;
111 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700112 iov[1].iov_base = &word;
113 iov[1].iov_len = sizeof(word);
114
115 len = iov[0].iov_len + iov[1].iov_len;
116
David Howells0d12f8a2016-03-04 15:53:46 +0000117 serial = atomic_inc_return(&conn->serial);
118 whdr.serial = htonl(serial);
David Howellsdc44b3a2016-04-07 17:23:30 +0100119 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700120
121 ret = kernel_sendmsg(conn->trans->local->socket, &msg, iov, 2, len);
122 if (ret < 0) {
123 _debug("sendmsg failed: %d", ret);
124 return -EAGAIN;
125 }
126
127 _leave(" = 0");
128 return 0;
129}
130
131/*
132 * mark a call as being on a now-secured channel
133 * - must be called with softirqs disabled
134 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800135static void rxrpc_call_is_secure(struct rxrpc_call *call)
David Howells17926a72007-04-26 15:48:28 -0700136{
137 _enter("%p", call);
138 if (call) {
139 read_lock(&call->state_lock);
140 if (call->state < RXRPC_CALL_COMPLETE &&
David Howells4c198ad2016-03-04 15:53:46 +0000141 !test_and_set_bit(RXRPC_CALL_EV_SECURED, &call->events))
David Howells651350d2007-04-26 15:50:17 -0700142 rxrpc_queue_call(call);
David Howells17926a72007-04-26 15:48:28 -0700143 read_unlock(&call->state_lock);
144 }
145}
146
147/*
148 * connection-level Rx packet processor
149 */
150static int rxrpc_process_event(struct rxrpc_connection *conn,
151 struct sk_buff *skb,
152 u32 *_abort_code)
153{
154 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000155 __be32 wtmp;
156 u32 abort_code;
David Howells17926a72007-04-26 15:48:28 -0700157 int loop, ret;
158
David Howells519d2562009-06-16 21:36:44 +0100159 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
160 kleave(" = -ECONNABORTED [%u]", conn->state);
David Howells17926a72007-04-26 15:48:28 -0700161 return -ECONNABORTED;
David Howells519d2562009-06-16 21:36:44 +0100162 }
David Howells17926a72007-04-26 15:48:28 -0700163
David Howells0d12f8a2016-03-04 15:53:46 +0000164 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
David Howells519d2562009-06-16 21:36:44 +0100165
David Howells17926a72007-04-26 15:48:28 -0700166 switch (sp->hdr.type) {
167 case RXRPC_PACKET_TYPE_ABORT:
David Howells0d12f8a2016-03-04 15:53:46 +0000168 if (skb_copy_bits(skb, 0, &wtmp, sizeof(wtmp)) < 0)
David Howells17926a72007-04-26 15:48:28 -0700169 return -EPROTO;
David Howells0d12f8a2016-03-04 15:53:46 +0000170 abort_code = ntohl(wtmp);
171 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
David Howells17926a72007-04-26 15:48:28 -0700172
173 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
174 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
David Howells0d12f8a2016-03-04 15:53:46 +0000175 abort_code);
David Howells17926a72007-04-26 15:48:28 -0700176 return -ECONNABORTED;
177
178 case RXRPC_PACKET_TYPE_CHALLENGE:
David Howellse0e4d822016-04-07 17:23:58 +0100179 return conn->security->respond_to_challenge(conn, skb,
180 _abort_code);
David Howells17926a72007-04-26 15:48:28 -0700181
182 case RXRPC_PACKET_TYPE_RESPONSE:
David Howells17926a72007-04-26 15:48:28 -0700183 ret = conn->security->verify_response(conn, skb, _abort_code);
184 if (ret < 0)
185 return ret;
186
187 ret = conn->security->init_connection_security(conn);
188 if (ret < 0)
189 return ret;
190
191 conn->security->prime_packet_security(conn);
192 read_lock_bh(&conn->lock);
193 spin_lock(&conn->state_lock);
194
195 if (conn->state == RXRPC_CONN_SERVER_CHALLENGING) {
196 conn->state = RXRPC_CONN_SERVER;
197 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
198 rxrpc_call_is_secure(conn->channels[loop]);
199 }
200
201 spin_unlock(&conn->state_lock);
202 read_unlock_bh(&conn->lock);
203 return 0;
204
205 default:
David Howells519d2562009-06-16 21:36:44 +0100206 _leave(" = -EPROTO [%u]", sp->hdr.type);
David Howells17926a72007-04-26 15:48:28 -0700207 return -EPROTO;
208 }
209}
210
211/*
212 * set up security and issue a challenge
213 */
214static void rxrpc_secure_connection(struct rxrpc_connection *conn)
215{
216 u32 abort_code;
217 int ret;
218
219 _enter("{%d}", conn->debug_id);
220
221 ASSERT(conn->security_ix != 0);
222
223 if (!conn->key) {
224 _debug("set up security");
225 ret = rxrpc_init_server_conn_security(conn);
226 switch (ret) {
227 case 0:
228 break;
229 case -ENOENT:
230 abort_code = RX_CALL_DEAD;
231 goto abort;
232 default:
233 abort_code = RXKADNOAUTH;
234 goto abort;
235 }
236 }
237
David Howells17926a72007-04-26 15:48:28 -0700238 if (conn->security->issue_challenge(conn) < 0) {
239 abort_code = RX_CALL_DEAD;
240 ret = -ENOMEM;
241 goto abort;
242 }
243
244 _leave("");
245 return;
246
247abort:
248 _debug("abort %d, %d", ret, abort_code);
249 rxrpc_abort_connection(conn, -ret, abort_code);
250 _leave(" [aborted]");
251}
252
253/*
254 * connection-level event processor
255 */
256void rxrpc_process_connection(struct work_struct *work)
257{
258 struct rxrpc_connection *conn =
259 container_of(work, struct rxrpc_connection, processor);
David Howells17926a72007-04-26 15:48:28 -0700260 struct sk_buff *skb;
261 u32 abort_code = RX_PROTOCOL_ERROR;
262 int ret;
263
264 _enter("{%d}", conn->debug_id);
265
266 atomic_inc(&conn->usage);
267
268 if (test_and_clear_bit(RXRPC_CONN_CHALLENGE, &conn->events)) {
269 rxrpc_secure_connection(conn);
270 rxrpc_put_connection(conn);
271 }
272
273 /* go through the conn-level event packets, releasing the ref on this
274 * connection that each one has when we've finished with it */
275 while ((skb = skb_dequeue(&conn->rx_queue))) {
David Howells17926a72007-04-26 15:48:28 -0700276 ret = rxrpc_process_event(conn, skb, &abort_code);
277 switch (ret) {
278 case -EPROTO:
279 case -EKEYEXPIRED:
280 case -EKEYREJECTED:
281 goto protocol_error;
282 case -EAGAIN:
283 goto requeue_and_leave;
284 case -ECONNABORTED:
285 default:
286 rxrpc_put_connection(conn);
287 rxrpc_free_skb(skb);
288 break;
289 }
290 }
291
292out:
293 rxrpc_put_connection(conn);
294 _leave("");
295 return;
296
297requeue_and_leave:
298 skb_queue_head(&conn->rx_queue, skb);
299 goto out;
300
301protocol_error:
302 if (rxrpc_abort_connection(conn, -ret, abort_code) < 0)
303 goto requeue_and_leave;
304 rxrpc_put_connection(conn);
305 rxrpc_free_skb(skb);
306 _leave(" [EPROTO]");
307 goto out;
308}
309
310/*
David Howells651350d2007-04-26 15:50:17 -0700311 * put a packet up for transport-level abort
312 */
313void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb)
314{
315 CHECK_SLAB_OKAY(&local->usage);
316
317 if (!atomic_inc_not_zero(&local->usage)) {
318 printk("resurrected on reject\n");
319 BUG();
320 }
321
322 skb_queue_tail(&local->reject_queue, skb);
323 rxrpc_queue_work(&local->rejecter);
324}
325
326/*
David Howells17926a72007-04-26 15:48:28 -0700327 * reject packets through the local endpoint
328 */
329void rxrpc_reject_packets(struct work_struct *work)
330{
331 union {
332 struct sockaddr sa;
333 struct sockaddr_in sin;
334 } sa;
335 struct rxrpc_skb_priv *sp;
David Howells0d12f8a2016-03-04 15:53:46 +0000336 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700337 struct rxrpc_local *local;
338 struct sk_buff *skb;
339 struct msghdr msg;
340 struct kvec iov[2];
341 size_t size;
342 __be32 code;
343
344 local = container_of(work, struct rxrpc_local, rejecter);
345 rxrpc_get_local(local);
346
347 _enter("%d", local->debug_id);
348
David Howells0d12f8a2016-03-04 15:53:46 +0000349 iov[0].iov_base = &whdr;
350 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700351 iov[1].iov_base = &code;
352 iov[1].iov_len = sizeof(code);
David Howells0d12f8a2016-03-04 15:53:46 +0000353 size = sizeof(whdr) + sizeof(code);
David Howells17926a72007-04-26 15:48:28 -0700354
355 msg.msg_name = &sa;
356 msg.msg_control = NULL;
357 msg.msg_controllen = 0;
358 msg.msg_flags = 0;
359
360 memset(&sa, 0, sizeof(sa));
361 sa.sa.sa_family = local->srx.transport.family;
362 switch (sa.sa.sa_family) {
363 case AF_INET:
364 msg.msg_namelen = sizeof(sa.sin);
365 break;
366 default:
367 msg.msg_namelen = 0;
368 break;
369 }
370
David Howells0d12f8a2016-03-04 15:53:46 +0000371 memset(&whdr, 0, sizeof(whdr));
372 whdr.type = RXRPC_PACKET_TYPE_ABORT;
David Howells17926a72007-04-26 15:48:28 -0700373
374 while ((skb = skb_dequeue(&local->reject_queue))) {
375 sp = rxrpc_skb(skb);
376 switch (sa.sa.sa_family) {
377 case AF_INET:
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);
381
David Howells0d12f8a2016-03-04 15:53:46 +0000382 whdr.epoch = htonl(sp->hdr.epoch);
383 whdr.cid = htonl(sp->hdr.cid);
384 whdr.callNumber = htonl(sp->hdr.callNumber);
385 whdr.serviceId = htons(sp->hdr.serviceId);
386 whdr.flags = sp->hdr.flags;
387 whdr.flags ^= RXRPC_CLIENT_INITIATED;
388 whdr.flags &= RXRPC_CLIENT_INITIATED;
David Howells17926a72007-04-26 15:48:28 -0700389
390 kernel_sendmsg(local->socket, &msg, iov, 2, size);
391 break;
392
393 default:
394 break;
395 }
396
397 rxrpc_free_skb(skb);
398 rxrpc_put_local(local);
399 }
400
401 rxrpc_put_local(local);
402 _leave("");
403}