blob: 9e9a8db1bc9cd0f1afd3efd7e9e26c4d2890a7d3 [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>
David Howells17926a72007-04-26 15:48:28 -070018#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include <net/ip.h>
21#include "ar-internal.h"
22
23/*
David Howells18bfeba2016-08-23 15:27:25 +010024 * Retransmit terminal ACK or ABORT of the previous call.
25 */
David Howellsf5c17aa2016-08-30 09:49:28 +010026static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
David Howells3136ef42017-11-24 10:18:41 +000027 struct sk_buff *skb,
28 unsigned int channel)
David Howells18bfeba2016-08-23 15:27:25 +010029{
David Howells3136ef42017-11-24 10:18:41 +000030 struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
David Howells18bfeba2016-08-23 15:27:25 +010031 struct rxrpc_channel *chan;
32 struct msghdr msg;
33 struct kvec iov;
34 struct {
35 struct rxrpc_wire_header whdr;
36 union {
37 struct {
38 __be32 code;
39 } abort;
40 struct {
41 struct rxrpc_ackpacket ack;
David Howells2266ffd2016-08-24 13:06:14 +010042 u8 padding[3];
David Howells18bfeba2016-08-23 15:27:25 +010043 struct rxrpc_ackinfo info;
44 };
45 };
46 } __attribute__((packed)) pkt;
47 size_t len;
48 u32 serial, mtu, call_id;
49
50 _enter("%d", conn->debug_id);
51
David Howells3136ef42017-11-24 10:18:41 +000052 chan = &conn->channels[channel];
David Howells18bfeba2016-08-23 15:27:25 +010053
54 /* If the last call got moved on whilst we were waiting to run, just
55 * ignore this packet.
56 */
57 call_id = READ_ONCE(chan->last_call);
58 /* Sync with __rxrpc_disconnect_call() */
59 smp_rmb();
David Howells3136ef42017-11-24 10:18:41 +000060 if (skb && call_id != sp->hdr.callNumber)
David Howells18bfeba2016-08-23 15:27:25 +010061 return;
62
63 msg.msg_name = &conn->params.peer->srx.transport;
64 msg.msg_namelen = conn->params.peer->srx.transport_len;
65 msg.msg_control = NULL;
66 msg.msg_controllen = 0;
67 msg.msg_flags = 0;
68
David Howells3136ef42017-11-24 10:18:41 +000069 pkt.whdr.epoch = htonl(conn->proto.epoch);
70 pkt.whdr.cid = htonl(conn->proto.cid);
71 pkt.whdr.callNumber = htonl(call_id);
David Howells18bfeba2016-08-23 15:27:25 +010072 pkt.whdr.seq = 0;
73 pkt.whdr.type = chan->last_type;
74 pkt.whdr.flags = conn->out_clientflag;
75 pkt.whdr.userStatus = 0;
76 pkt.whdr.securityIndex = conn->security_ix;
77 pkt.whdr._rsvd = 0;
David Howells68d6d1a2017-06-05 14:30:49 +010078 pkt.whdr.serviceId = htons(conn->service_id);
David Howells18bfeba2016-08-23 15:27:25 +010079
80 len = sizeof(pkt.whdr);
81 switch (chan->last_type) {
82 case RXRPC_PACKET_TYPE_ABORT:
83 pkt.abort.code = htonl(chan->last_abort);
84 len += sizeof(pkt.abort);
85 break;
86
87 case RXRPC_PACKET_TYPE_ACK:
88 mtu = conn->params.peer->if_mtu;
89 mtu -= conn->params.peer->hdrsize;
90 pkt.ack.bufferSpace = 0;
David Howells3136ef42017-11-24 10:18:41 +000091 pkt.ack.maxSkew = htons(skb ? skb->priority : 0);
92 pkt.ack.firstPacket = htonl(chan->last_seq + 1);
93 pkt.ack.previousPacket = htonl(chan->last_seq);
94 pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0);
95 pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
David Howells18bfeba2016-08-23 15:27:25 +010096 pkt.ack.nAcks = 0;
97 pkt.info.rxMTU = htonl(rxrpc_rx_mtu);
98 pkt.info.maxMTU = htonl(mtu);
99 pkt.info.rwind = htonl(rxrpc_rx_window_size);
100 pkt.info.jumbo_max = htonl(rxrpc_rx_jumbo_max);
David Howells57494342016-09-24 18:05:27 +0100101 pkt.whdr.flags |= RXRPC_SLOW_START_OK;
David Howells18bfeba2016-08-23 15:27:25 +0100102 len += sizeof(pkt.ack) + sizeof(pkt.info);
103 break;
104 }
105
106 /* Resync with __rxrpc_disconnect_call() and check that the last call
107 * didn't get advanced whilst we were filling out the packets.
108 */
109 smp_rmb();
110 if (READ_ONCE(chan->last_call) != call_id)
111 return;
112
113 iov.iov_base = &pkt;
114 iov.iov_len = len;
115
116 serial = atomic_inc_return(&conn->serial);
117 pkt.whdr.serial = htonl(serial);
118
119 switch (chan->last_type) {
120 case RXRPC_PACKET_TYPE_ABORT:
121 _proto("Tx ABORT %%%u { %d } [re]", serial, conn->local_abort);
122 break;
123 case RXRPC_PACKET_TYPE_ACK:
David Howellsbe832ae2016-09-23 12:39:22 +0100124 trace_rxrpc_tx_ack(NULL, serial, chan->last_seq, 0,
125 RXRPC_ACK_DUPLICATE, 0);
David Howells18bfeba2016-08-23 15:27:25 +0100126 _proto("Tx ACK %%%u [re]", serial);
127 break;
128 }
129
130 kernel_sendmsg(conn->params.local->socket, &msg, &iov, 1, len);
131 _leave("");
132 return;
133}
134
135/*
David Howells17926a72007-04-26 15:48:28 -0700136 * pass a connection-level abort onto all calls on that connection
137 */
David Howellsf5c17aa2016-08-30 09:49:28 +0100138static void rxrpc_abort_calls(struct rxrpc_connection *conn,
139 enum rxrpc_call_completion compl,
140 u32 abort_code, int error)
David Howells17926a72007-04-26 15:48:28 -0700141{
142 struct rxrpc_call *call;
David Howells248f2192016-09-08 11:10:12 +0100143 int i;
David Howells17926a72007-04-26 15:48:28 -0700144
145 _enter("{%d},%x", conn->debug_id, abort_code);
146
David Howellsa1399f82016-06-27 14:39:44 +0100147 spin_lock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700148
David Howellsa1399f82016-06-27 14:39:44 +0100149 for (i = 0; i < RXRPC_MAXCALLS; i++) {
150 call = rcu_dereference_protected(
151 conn->channels[i].call,
152 lockdep_is_held(&conn->channel_lock));
David Howellsccbd3db2016-08-30 09:49:28 +0100153 if (call) {
David Howells5a429762016-09-06 22:19:51 +0100154 if (compl == RXRPC_CALL_LOCALLY_ABORTED)
155 trace_rxrpc_abort("CON", call->cid,
156 call->call_id, 0,
157 abort_code, error);
David Howells248f2192016-09-08 11:10:12 +0100158 if (rxrpc_set_call_completion(call, compl,
159 abort_code, error))
160 rxrpc_notify_socket(call);
David Howells17926a72007-04-26 15:48:28 -0700161 }
David Howells17926a72007-04-26 15:48:28 -0700162 }
163
David Howellsa1399f82016-06-27 14:39:44 +0100164 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700165 _leave("");
166}
167
168/*
169 * generate a connection-level abort
170 */
171static int rxrpc_abort_connection(struct rxrpc_connection *conn,
David Howells3a927892017-04-06 10:11:56 +0100172 int error, u32 abort_code)
David Howells17926a72007-04-26 15:48:28 -0700173{
David Howells0d12f8a2016-03-04 15:53:46 +0000174 struct rxrpc_wire_header whdr;
David Howells17926a72007-04-26 15:48:28 -0700175 struct msghdr msg;
176 struct kvec iov[2];
177 __be32 word;
178 size_t len;
David Howells0d12f8a2016-03-04 15:53:46 +0000179 u32 serial;
David Howells17926a72007-04-26 15:48:28 -0700180 int ret;
181
182 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
183
184 /* generate a connection-level abort */
185 spin_lock_bh(&conn->state_lock);
David Howellsf5c17aa2016-08-30 09:49:28 +0100186 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
David Howells17926a72007-04-26 15:48:28 -0700187 spin_unlock_bh(&conn->state_lock);
188 _leave(" = 0 [already dead]");
189 return 0;
190 }
191
David Howellsf5c17aa2016-08-30 09:49:28 +0100192 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
193 spin_unlock_bh(&conn->state_lock);
194
195 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code, error);
David Howells17926a72007-04-26 15:48:28 -0700196
David Howells85f32272016-04-04 14:00:36 +0100197 msg.msg_name = &conn->params.peer->srx.transport;
198 msg.msg_namelen = conn->params.peer->srx.transport_len;
David Howells17926a72007-04-26 15:48:28 -0700199 msg.msg_control = NULL;
200 msg.msg_controllen = 0;
201 msg.msg_flags = 0;
202
David Howells19ffa012016-04-04 14:00:36 +0100203 whdr.epoch = htonl(conn->proto.epoch);
204 whdr.cid = htonl(conn->proto.cid);
David Howells0d12f8a2016-03-04 15:53:46 +0000205 whdr.callNumber = 0;
206 whdr.seq = 0;
207 whdr.type = RXRPC_PACKET_TYPE_ABORT;
208 whdr.flags = conn->out_clientflag;
209 whdr.userStatus = 0;
210 whdr.securityIndex = conn->security_ix;
211 whdr._rsvd = 0;
David Howells68d6d1a2017-06-05 14:30:49 +0100212 whdr.serviceId = htons(conn->service_id);
David Howells17926a72007-04-26 15:48:28 -0700213
David Howellsdc44b3a2016-04-07 17:23:30 +0100214 word = htonl(conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700215
David Howells0d12f8a2016-03-04 15:53:46 +0000216 iov[0].iov_base = &whdr;
217 iov[0].iov_len = sizeof(whdr);
David Howells17926a72007-04-26 15:48:28 -0700218 iov[1].iov_base = &word;
219 iov[1].iov_len = sizeof(word);
220
221 len = iov[0].iov_len + iov[1].iov_len;
222
David Howells0d12f8a2016-03-04 15:53:46 +0000223 serial = atomic_inc_return(&conn->serial);
224 whdr.serial = htonl(serial);
David Howellsdc44b3a2016-04-07 17:23:30 +0100225 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
David Howells17926a72007-04-26 15:48:28 -0700226
David Howells85f32272016-04-04 14:00:36 +0100227 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
David Howells17926a72007-04-26 15:48:28 -0700228 if (ret < 0) {
229 _debug("sendmsg failed: %d", ret);
230 return -EAGAIN;
231 }
232
233 _leave(" = 0");
234 return 0;
235}
236
237/*
238 * mark a call as being on a now-secured channel
David Howells248f2192016-09-08 11:10:12 +0100239 * - must be called with BH's disabled.
David Howells17926a72007-04-26 15:48:28 -0700240 */
Roel Kluin5eaa65b2008-12-10 15:18:31 -0800241static void rxrpc_call_is_secure(struct rxrpc_call *call)
David Howells17926a72007-04-26 15:48:28 -0700242{
243 _enter("%p", call);
244 if (call) {
David Howells248f2192016-09-08 11:10:12 +0100245 write_lock_bh(&call->state_lock);
246 if (call->state == RXRPC_CALL_SERVER_SECURING) {
247 call->state = RXRPC_CALL_SERVER_ACCEPTING;
248 rxrpc_notify_socket(call);
249 }
250 write_unlock_bh(&call->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700251 }
252}
253
254/*
255 * connection-level Rx packet processor
256 */
257static int rxrpc_process_event(struct rxrpc_connection *conn,
258 struct sk_buff *skb,
259 u32 *_abort_code)
260{
261 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
David Howells0d12f8a2016-03-04 15:53:46 +0000262 __be32 wtmp;
263 u32 abort_code;
David Howells17926a72007-04-26 15:48:28 -0700264 int loop, ret;
265
David Howells519d2562009-06-16 21:36:44 +0100266 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
David Howells248f2192016-09-08 11:10:12 +0100267 _leave(" = -ECONNABORTED [%u]", conn->state);
David Howells17926a72007-04-26 15:48:28 -0700268 return -ECONNABORTED;
David Howells519d2562009-06-16 21:36:44 +0100269 }
David Howells17926a72007-04-26 15:48:28 -0700270
David Howells0d12f8a2016-03-04 15:53:46 +0000271 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
David Howells519d2562009-06-16 21:36:44 +0100272
David Howells17926a72007-04-26 15:48:28 -0700273 switch (sp->hdr.type) {
David Howells18bfeba2016-08-23 15:27:25 +0100274 case RXRPC_PACKET_TYPE_DATA:
275 case RXRPC_PACKET_TYPE_ACK:
David Howells3136ef42017-11-24 10:18:41 +0000276 rxrpc_conn_retransmit_call(conn, skb,
277 sp->hdr.cid & RXRPC_CHANNELMASK);
David Howells18bfeba2016-08-23 15:27:25 +0100278 return 0;
279
David Howells4d4a6ac2017-03-16 16:27:10 +0000280 case RXRPC_PACKET_TYPE_BUSY:
281 /* Just ignore BUSY packets for now. */
282 return 0;
283
David Howells17926a72007-04-26 15:48:28 -0700284 case RXRPC_PACKET_TYPE_ABORT:
David Howells775e5b72016-09-30 13:26:03 +0100285 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
David Howellsfb46f6e2017-04-06 10:12:00 +0100286 &wtmp, sizeof(wtmp)) < 0) {
287 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
288 tracepoint_string("bad_abort"));
David Howells17926a72007-04-26 15:48:28 -0700289 return -EPROTO;
David Howellsfb46f6e2017-04-06 10:12:00 +0100290 }
David Howells0d12f8a2016-03-04 15:53:46 +0000291 abort_code = ntohl(wtmp);
292 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
David Howells17926a72007-04-26 15:48:28 -0700293
294 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
David Howells248f2192016-09-08 11:10:12 +0100295 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
David Howells3a927892017-04-06 10:11:56 +0100296 abort_code, -ECONNABORTED);
David Howells17926a72007-04-26 15:48:28 -0700297 return -ECONNABORTED;
298
299 case RXRPC_PACKET_TYPE_CHALLENGE:
David Howellse0e4d822016-04-07 17:23:58 +0100300 return conn->security->respond_to_challenge(conn, skb,
301 _abort_code);
David Howells17926a72007-04-26 15:48:28 -0700302
303 case RXRPC_PACKET_TYPE_RESPONSE:
David Howells17926a72007-04-26 15:48:28 -0700304 ret = conn->security->verify_response(conn, skb, _abort_code);
305 if (ret < 0)
306 return ret;
307
308 ret = conn->security->init_connection_security(conn);
309 if (ret < 0)
310 return ret;
311
Herbert Xua2636292016-06-26 14:55:24 -0700312 ret = conn->security->prime_packet_security(conn);
313 if (ret < 0)
314 return ret;
315
David Howellsa1399f82016-06-27 14:39:44 +0100316 spin_lock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700317 spin_lock(&conn->state_lock);
318
David Howellsbba304d2016-06-27 10:32:02 +0100319 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
320 conn->state = RXRPC_CONN_SERVICE;
David Howells248f2192016-09-08 11:10:12 +0100321 spin_unlock(&conn->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700322 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
David Howellsdee46362016-06-27 17:11:19 +0100323 rxrpc_call_is_secure(
324 rcu_dereference_protected(
David Howellsa1399f82016-06-27 14:39:44 +0100325 conn->channels[loop].call,
326 lockdep_is_held(&conn->channel_lock)));
David Howells248f2192016-09-08 11:10:12 +0100327 } else {
328 spin_unlock(&conn->state_lock);
David Howells17926a72007-04-26 15:48:28 -0700329 }
330
David Howellsa1399f82016-06-27 14:39:44 +0100331 spin_unlock(&conn->channel_lock);
David Howells17926a72007-04-26 15:48:28 -0700332 return 0;
333
334 default:
David Howellsfb46f6e2017-04-06 10:12:00 +0100335 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
336 tracepoint_string("bad_conn_pkt"));
David Howells17926a72007-04-26 15:48:28 -0700337 return -EPROTO;
338 }
339}
340
341/*
342 * set up security and issue a challenge
343 */
344static void rxrpc_secure_connection(struct rxrpc_connection *conn)
345{
346 u32 abort_code;
347 int ret;
348
349 _enter("{%d}", conn->debug_id);
350
351 ASSERT(conn->security_ix != 0);
352
David Howells19ffa012016-04-04 14:00:36 +0100353 if (!conn->params.key) {
David Howells17926a72007-04-26 15:48:28 -0700354 _debug("set up security");
355 ret = rxrpc_init_server_conn_security(conn);
356 switch (ret) {
357 case 0:
358 break;
359 case -ENOENT:
360 abort_code = RX_CALL_DEAD;
361 goto abort;
362 default:
363 abort_code = RXKADNOAUTH;
364 goto abort;
365 }
366 }
367
David Howells17926a72007-04-26 15:48:28 -0700368 if (conn->security->issue_challenge(conn) < 0) {
369 abort_code = RX_CALL_DEAD;
370 ret = -ENOMEM;
371 goto abort;
372 }
373
374 _leave("");
375 return;
376
377abort:
378 _debug("abort %d, %d", ret, abort_code);
David Howells3a927892017-04-06 10:11:56 +0100379 rxrpc_abort_connection(conn, ret, abort_code);
David Howells17926a72007-04-26 15:48:28 -0700380 _leave(" [aborted]");
381}
382
383/*
David Howells3136ef42017-11-24 10:18:41 +0000384 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
385 */
386static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn)
387{
388 unsigned long j = jiffies, next_j;
389 unsigned int channel;
390 bool set;
391
392again:
393 next_j = j + LONG_MAX;
394 set = false;
395 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
396 struct rxrpc_channel *chan = &conn->channels[channel];
397 unsigned long ack_at;
398
399 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
400 continue;
401
402 smp_rmb(); /* vs rxrpc_disconnect_client_call */
403 ack_at = READ_ONCE(chan->final_ack_at);
404
405 if (time_before(j, ack_at)) {
406 if (time_before(ack_at, next_j)) {
407 next_j = ack_at;
408 set = true;
409 }
410 continue;
411 }
412
413 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
414 &conn->flags))
415 rxrpc_conn_retransmit_call(conn, NULL, channel);
416 }
417
418 j = jiffies;
419 if (time_before_eq(next_j, j))
420 goto again;
421 if (set)
422 rxrpc_reduce_conn_timer(conn, next_j);
423}
424
425/*
David Howells17926a72007-04-26 15:48:28 -0700426 * connection-level event processor
427 */
428void rxrpc_process_connection(struct work_struct *work)
429{
430 struct rxrpc_connection *conn =
431 container_of(work, struct rxrpc_connection, processor);
David Howells17926a72007-04-26 15:48:28 -0700432 struct sk_buff *skb;
433 u32 abort_code = RX_PROTOCOL_ERROR;
434 int ret;
435
David Howells363deea2016-09-17 10:49:14 +0100436 rxrpc_see_connection(conn);
David Howells17926a72007-04-26 15:48:28 -0700437
David Howells2c4579e2016-06-27 10:32:03 +0100438 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
David Howells17926a72007-04-26 15:48:28 -0700439 rxrpc_secure_connection(conn);
David Howells17926a72007-04-26 15:48:28 -0700440
David Howells3136ef42017-11-24 10:18:41 +0000441 /* Process delayed ACKs whose time has come. */
442 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
443 rxrpc_process_delayed_final_acks(conn);
444
David Howells17926a72007-04-26 15:48:28 -0700445 /* go through the conn-level event packets, releasing the ref on this
446 * connection that each one has when we've finished with it */
447 while ((skb = skb_dequeue(&conn->rx_queue))) {
David Howells71f3ca42016-09-17 10:49:14 +0100448 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
David Howells17926a72007-04-26 15:48:28 -0700449 ret = rxrpc_process_event(conn, skb, &abort_code);
450 switch (ret) {
451 case -EPROTO:
452 case -EKEYEXPIRED:
453 case -EKEYREJECTED:
454 goto protocol_error;
455 case -EAGAIN:
456 goto requeue_and_leave;
457 case -ECONNABORTED:
458 default:
David Howells71f3ca42016-09-17 10:49:14 +0100459 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells17926a72007-04-26 15:48:28 -0700460 break;
461 }
462 }
463
464out:
465 rxrpc_put_connection(conn);
466 _leave("");
467 return;
468
469requeue_and_leave:
470 skb_queue_head(&conn->rx_queue, skb);
471 goto out;
472
473protocol_error:
David Howells3a927892017-04-06 10:11:56 +0100474 if (rxrpc_abort_connection(conn, ret, abort_code) < 0)
David Howells17926a72007-04-26 15:48:28 -0700475 goto requeue_and_leave;
David Howells71f3ca42016-09-17 10:49:14 +0100476 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
David Howells17926a72007-04-26 15:48:28 -0700477 goto out;
478}