]> git.proxmox.com Git - mirror_ubuntu-eoan-kernel.git/blame - net/rxrpc/conn_event.c
Merge tag 'mac80211-next-for-davem-2018-03-29' of git://git.kernel.org/pub/scm/linux...
[mirror_ubuntu-eoan-kernel.git] / net / rxrpc / conn_event.c
CommitLineData
17926a79
DH
1/* 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
9b6d5398
JP
12#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
13
17926a79
DH
14#include <linux/module.h>
15#include <linux/net.h>
16#include <linux/skbuff.h>
17#include <linux/errqueue.h>
17926a79
DH
18#include <net/sock.h>
19#include <net/af_rxrpc.h>
20#include <net/ip.h>
21#include "ar-internal.h"
22
18bfeba5
DH
23/*
24 * Retransmit terminal ACK or ABORT of the previous call.
25 */
f5c17aae 26static void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
3136ef49
DH
27 struct sk_buff *skb,
28 unsigned int channel)
18bfeba5 29{
3136ef49 30 struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
18bfeba5
DH
31 struct rxrpc_channel *chan;
32 struct msghdr msg;
5fc62f6a 33 struct kvec iov[3];
18bfeba5
DH
34 struct {
35 struct rxrpc_wire_header whdr;
36 union {
5fc62f6a
DH
37 __be32 abort_code;
38 struct rxrpc_ackpacket ack;
18bfeba5
DH
39 };
40 } __attribute__((packed)) pkt;
5fc62f6a 41 struct rxrpc_ackinfo ack_info;
18bfeba5 42 size_t len;
5fc62f6a
DH
43 int ioc;
44 u32 serial, mtu, call_id, padding;
18bfeba5
DH
45
46 _enter("%d", conn->debug_id);
47
3136ef49 48 chan = &conn->channels[channel];
18bfeba5
DH
49
50 /* If the last call got moved on whilst we were waiting to run, just
51 * ignore this packet.
52 */
53 call_id = READ_ONCE(chan->last_call);
54 /* Sync with __rxrpc_disconnect_call() */
55 smp_rmb();
3136ef49 56 if (skb && call_id != sp->hdr.callNumber)
18bfeba5
DH
57 return;
58
59 msg.msg_name = &conn->params.peer->srx.transport;
60 msg.msg_namelen = conn->params.peer->srx.transport_len;
61 msg.msg_control = NULL;
62 msg.msg_controllen = 0;
63 msg.msg_flags = 0;
64
5fc62f6a
DH
65 iov[0].iov_base = &pkt;
66 iov[0].iov_len = sizeof(pkt.whdr);
67 iov[1].iov_base = &padding;
68 iov[1].iov_len = 3;
69 iov[2].iov_base = &ack_info;
70 iov[2].iov_len = sizeof(ack_info);
71
3136ef49
DH
72 pkt.whdr.epoch = htonl(conn->proto.epoch);
73 pkt.whdr.cid = htonl(conn->proto.cid);
74 pkt.whdr.callNumber = htonl(call_id);
18bfeba5
DH
75 pkt.whdr.seq = 0;
76 pkt.whdr.type = chan->last_type;
77 pkt.whdr.flags = conn->out_clientflag;
78 pkt.whdr.userStatus = 0;
79 pkt.whdr.securityIndex = conn->security_ix;
80 pkt.whdr._rsvd = 0;
68d6d1ae 81 pkt.whdr.serviceId = htons(conn->service_id);
18bfeba5
DH
82
83 len = sizeof(pkt.whdr);
84 switch (chan->last_type) {
85 case RXRPC_PACKET_TYPE_ABORT:
5fc62f6a
DH
86 pkt.abort_code = htonl(chan->last_abort);
87 iov[0].iov_len += sizeof(pkt.abort_code);
88 len += sizeof(pkt.abort_code);
89 ioc = 1;
18bfeba5
DH
90 break;
91
92 case RXRPC_PACKET_TYPE_ACK:
93 mtu = conn->params.peer->if_mtu;
94 mtu -= conn->params.peer->hdrsize;
95 pkt.ack.bufferSpace = 0;
3136ef49
DH
96 pkt.ack.maxSkew = htons(skb ? skb->priority : 0);
97 pkt.ack.firstPacket = htonl(chan->last_seq + 1);
98 pkt.ack.previousPacket = htonl(chan->last_seq);
99 pkt.ack.serial = htonl(skb ? sp->hdr.serial : 0);
100 pkt.ack.reason = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
18bfeba5 101 pkt.ack.nAcks = 0;
5fc62f6a
DH
102 ack_info.rxMTU = htonl(rxrpc_rx_mtu);
103 ack_info.maxMTU = htonl(mtu);
104 ack_info.rwind = htonl(rxrpc_rx_window_size);
105 ack_info.jumbo_max = htonl(rxrpc_rx_jumbo_max);
57494343 106 pkt.whdr.flags |= RXRPC_SLOW_START_OK;
5fc62f6a
DH
107 padding = 0;
108 iov[0].iov_len += sizeof(pkt.ack);
109 len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
110 ioc = 3;
18bfeba5 111 break;
5fc62f6a
DH
112
113 default:
114 return;
18bfeba5
DH
115 }
116
117 /* Resync with __rxrpc_disconnect_call() and check that the last call
118 * didn't get advanced whilst we were filling out the packets.
119 */
120 smp_rmb();
121 if (READ_ONCE(chan->last_call) != call_id)
122 return;
123
18bfeba5
DH
124 serial = atomic_inc_return(&conn->serial);
125 pkt.whdr.serial = htonl(serial);
126
127 switch (chan->last_type) {
128 case RXRPC_PACKET_TYPE_ABORT:
129 _proto("Tx ABORT %%%u { %d } [re]", serial, conn->local_abort);
130 break;
131 case RXRPC_PACKET_TYPE_ACK:
be832aec
DH
132 trace_rxrpc_tx_ack(NULL, serial, chan->last_seq, 0,
133 RXRPC_ACK_DUPLICATE, 0);
18bfeba5
DH
134 _proto("Tx ACK %%%u [re]", serial);
135 break;
136 }
137
5fc62f6a 138 kernel_sendmsg(conn->params.local->socket, &msg, iov, ioc, len);
18bfeba5
DH
139 _leave("");
140 return;
141}
142
17926a79
DH
143/*
144 * pass a connection-level abort onto all calls on that connection
145 */
f5c17aae
DH
146static void rxrpc_abort_calls(struct rxrpc_connection *conn,
147 enum rxrpc_call_completion compl,
148 u32 abort_code, int error)
17926a79
DH
149{
150 struct rxrpc_call *call;
248f219c 151 int i;
17926a79
DH
152
153 _enter("{%d},%x", conn->debug_id, abort_code);
154
a1399f8b 155 spin_lock(&conn->channel_lock);
17926a79 156
a1399f8b
DH
157 for (i = 0; i < RXRPC_MAXCALLS; i++) {
158 call = rcu_dereference_protected(
159 conn->channels[i].call,
160 lockdep_is_held(&conn->channel_lock));
ccbd3dbe 161 if (call) {
5a42976d 162 if (compl == RXRPC_CALL_LOCALLY_ABORTED)
a25e21f0
DH
163 trace_rxrpc_abort(call->debug_id,
164 "CON", call->cid,
5a42976d
DH
165 call->call_id, 0,
166 abort_code, error);
248f219c
DH
167 if (rxrpc_set_call_completion(call, compl,
168 abort_code, error))
169 rxrpc_notify_socket(call);
17926a79 170 }
17926a79
DH
171 }
172
a1399f8b 173 spin_unlock(&conn->channel_lock);
17926a79
DH
174 _leave("");
175}
176
177/*
178 * generate a connection-level abort
179 */
180static int rxrpc_abort_connection(struct rxrpc_connection *conn,
3a92789a 181 int error, u32 abort_code)
17926a79 182{
0d12f8a4 183 struct rxrpc_wire_header whdr;
17926a79
DH
184 struct msghdr msg;
185 struct kvec iov[2];
186 __be32 word;
187 size_t len;
0d12f8a4 188 u32 serial;
17926a79
DH
189 int ret;
190
191 _enter("%d,,%u,%u", conn->debug_id, error, abort_code);
192
193 /* generate a connection-level abort */
194 spin_lock_bh(&conn->state_lock);
f5c17aae 195 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
17926a79
DH
196 spin_unlock_bh(&conn->state_lock);
197 _leave(" = 0 [already dead]");
198 return 0;
199 }
200
f5c17aae
DH
201 conn->state = RXRPC_CONN_LOCALLY_ABORTED;
202 spin_unlock_bh(&conn->state_lock);
203
204 rxrpc_abort_calls(conn, RXRPC_CALL_LOCALLY_ABORTED, abort_code, error);
17926a79 205
85f32278
DH
206 msg.msg_name = &conn->params.peer->srx.transport;
207 msg.msg_namelen = conn->params.peer->srx.transport_len;
17926a79
DH
208 msg.msg_control = NULL;
209 msg.msg_controllen = 0;
210 msg.msg_flags = 0;
211
19ffa01c
DH
212 whdr.epoch = htonl(conn->proto.epoch);
213 whdr.cid = htonl(conn->proto.cid);
0d12f8a4
DH
214 whdr.callNumber = 0;
215 whdr.seq = 0;
216 whdr.type = RXRPC_PACKET_TYPE_ABORT;
217 whdr.flags = conn->out_clientflag;
218 whdr.userStatus = 0;
219 whdr.securityIndex = conn->security_ix;
220 whdr._rsvd = 0;
68d6d1ae 221 whdr.serviceId = htons(conn->service_id);
17926a79 222
dc44b3a0 223 word = htonl(conn->local_abort);
17926a79 224
0d12f8a4
DH
225 iov[0].iov_base = &whdr;
226 iov[0].iov_len = sizeof(whdr);
17926a79
DH
227 iov[1].iov_base = &word;
228 iov[1].iov_len = sizeof(word);
229
230 len = iov[0].iov_len + iov[1].iov_len;
231
0d12f8a4
DH
232 serial = atomic_inc_return(&conn->serial);
233 whdr.serial = htonl(serial);
dc44b3a0 234 _proto("Tx CONN ABORT %%%u { %d }", serial, conn->local_abort);
17926a79 235
85f32278 236 ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
17926a79
DH
237 if (ret < 0) {
238 _debug("sendmsg failed: %d", ret);
239 return -EAGAIN;
240 }
241
242 _leave(" = 0");
243 return 0;
244}
245
246/*
247 * mark a call as being on a now-secured channel
248f219c 248 * - must be called with BH's disabled.
17926a79 249 */
5eaa65b2 250static void rxrpc_call_is_secure(struct rxrpc_call *call)
17926a79
DH
251{
252 _enter("%p", call);
253 if (call) {
248f219c
DH
254 write_lock_bh(&call->state_lock);
255 if (call->state == RXRPC_CALL_SERVER_SECURING) {
256 call->state = RXRPC_CALL_SERVER_ACCEPTING;
257 rxrpc_notify_socket(call);
258 }
259 write_unlock_bh(&call->state_lock);
17926a79
DH
260 }
261}
262
263/*
264 * connection-level Rx packet processor
265 */
266static int rxrpc_process_event(struct rxrpc_connection *conn,
267 struct sk_buff *skb,
268 u32 *_abort_code)
269{
270 struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
0d12f8a4
DH
271 __be32 wtmp;
272 u32 abort_code;
17926a79
DH
273 int loop, ret;
274
519d2567 275 if (conn->state >= RXRPC_CONN_REMOTELY_ABORTED) {
248f219c 276 _leave(" = -ECONNABORTED [%u]", conn->state);
17926a79 277 return -ECONNABORTED;
519d2567 278 }
17926a79 279
0d12f8a4 280 _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
519d2567 281
17926a79 282 switch (sp->hdr.type) {
18bfeba5
DH
283 case RXRPC_PACKET_TYPE_DATA:
284 case RXRPC_PACKET_TYPE_ACK:
3136ef49
DH
285 rxrpc_conn_retransmit_call(conn, skb,
286 sp->hdr.cid & RXRPC_CHANNELMASK);
18bfeba5
DH
287 return 0;
288
4d4a6ac7
DH
289 case RXRPC_PACKET_TYPE_BUSY:
290 /* Just ignore BUSY packets for now. */
291 return 0;
292
17926a79 293 case RXRPC_PACKET_TYPE_ABORT:
775e5b71 294 if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
fb46f6ee
DH
295 &wtmp, sizeof(wtmp)) < 0) {
296 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
297 tracepoint_string("bad_abort"));
17926a79 298 return -EPROTO;
fb46f6ee 299 }
0d12f8a4
DH
300 abort_code = ntohl(wtmp);
301 _proto("Rx ABORT %%%u { ac=%d }", sp->hdr.serial, abort_code);
17926a79
DH
302
303 conn->state = RXRPC_CONN_REMOTELY_ABORTED;
248f219c 304 rxrpc_abort_calls(conn, RXRPC_CALL_REMOTELY_ABORTED,
3a92789a 305 abort_code, -ECONNABORTED);
17926a79
DH
306 return -ECONNABORTED;
307
308 case RXRPC_PACKET_TYPE_CHALLENGE:
e0e4d82f
DH
309 return conn->security->respond_to_challenge(conn, skb,
310 _abort_code);
17926a79
DH
311
312 case RXRPC_PACKET_TYPE_RESPONSE:
17926a79
DH
313 ret = conn->security->verify_response(conn, skb, _abort_code);
314 if (ret < 0)
315 return ret;
316
317 ret = conn->security->init_connection_security(conn);
318 if (ret < 0)
319 return ret;
320
a263629d
HX
321 ret = conn->security->prime_packet_security(conn);
322 if (ret < 0)
323 return ret;
324
a1399f8b 325 spin_lock(&conn->channel_lock);
17926a79
DH
326 spin_lock(&conn->state_lock);
327
bba304db
DH
328 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING) {
329 conn->state = RXRPC_CONN_SERVICE;
248f219c 330 spin_unlock(&conn->state_lock);
17926a79 331 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
dee46364
DH
332 rxrpc_call_is_secure(
333 rcu_dereference_protected(
a1399f8b
DH
334 conn->channels[loop].call,
335 lockdep_is_held(&conn->channel_lock)));
248f219c
DH
336 } else {
337 spin_unlock(&conn->state_lock);
17926a79
DH
338 }
339
a1399f8b 340 spin_unlock(&conn->channel_lock);
17926a79
DH
341 return 0;
342
343 default:
fb46f6ee
DH
344 trace_rxrpc_rx_eproto(NULL, sp->hdr.serial,
345 tracepoint_string("bad_conn_pkt"));
17926a79
DH
346 return -EPROTO;
347 }
348}
349
350/*
351 * set up security and issue a challenge
352 */
353static void rxrpc_secure_connection(struct rxrpc_connection *conn)
354{
355 u32 abort_code;
356 int ret;
357
358 _enter("{%d}", conn->debug_id);
359
360 ASSERT(conn->security_ix != 0);
361
19ffa01c 362 if (!conn->params.key) {
17926a79
DH
363 _debug("set up security");
364 ret = rxrpc_init_server_conn_security(conn);
365 switch (ret) {
366 case 0:
367 break;
368 case -ENOENT:
369 abort_code = RX_CALL_DEAD;
370 goto abort;
371 default:
372 abort_code = RXKADNOAUTH;
373 goto abort;
374 }
375 }
376
17926a79
DH
377 if (conn->security->issue_challenge(conn) < 0) {
378 abort_code = RX_CALL_DEAD;
379 ret = -ENOMEM;
380 goto abort;
381 }
382
383 _leave("");
384 return;
385
386abort:
387 _debug("abort %d, %d", ret, abort_code);
3a92789a 388 rxrpc_abort_connection(conn, ret, abort_code);
17926a79
DH
389 _leave(" [aborted]");
390}
391
3136ef49
DH
392/*
393 * Process delayed final ACKs that we haven't subsumed into a subsequent call.
394 */
395static void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn)
396{
397 unsigned long j = jiffies, next_j;
398 unsigned int channel;
399 bool set;
400
401again:
402 next_j = j + LONG_MAX;
403 set = false;
404 for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
405 struct rxrpc_channel *chan = &conn->channels[channel];
406 unsigned long ack_at;
407
408 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
409 continue;
410
411 smp_rmb(); /* vs rxrpc_disconnect_client_call */
412 ack_at = READ_ONCE(chan->final_ack_at);
413
414 if (time_before(j, ack_at)) {
415 if (time_before(ack_at, next_j)) {
416 next_j = ack_at;
417 set = true;
418 }
419 continue;
420 }
421
422 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
423 &conn->flags))
424 rxrpc_conn_retransmit_call(conn, NULL, channel);
425 }
426
427 j = jiffies;
428 if (time_before_eq(next_j, j))
429 goto again;
430 if (set)
431 rxrpc_reduce_conn_timer(conn, next_j);
432}
433
17926a79
DH
434/*
435 * connection-level event processor
436 */
437void rxrpc_process_connection(struct work_struct *work)
438{
439 struct rxrpc_connection *conn =
440 container_of(work, struct rxrpc_connection, processor);
17926a79
DH
441 struct sk_buff *skb;
442 u32 abort_code = RX_PROTOCOL_ERROR;
443 int ret;
444
363deeab 445 rxrpc_see_connection(conn);
17926a79 446
2c4579e4 447 if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
17926a79 448 rxrpc_secure_connection(conn);
17926a79 449
3136ef49
DH
450 /* Process delayed ACKs whose time has come. */
451 if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
452 rxrpc_process_delayed_final_acks(conn);
453
17926a79
DH
454 /* go through the conn-level event packets, releasing the ref on this
455 * connection that each one has when we've finished with it */
456 while ((skb = skb_dequeue(&conn->rx_queue))) {
71f3ca40 457 rxrpc_see_skb(skb, rxrpc_skb_rx_seen);
17926a79
DH
458 ret = rxrpc_process_event(conn, skb, &abort_code);
459 switch (ret) {
460 case -EPROTO:
461 case -EKEYEXPIRED:
462 case -EKEYREJECTED:
463 goto protocol_error;
8c2f826d 464 case -ENOMEM:
17926a79
DH
465 case -EAGAIN:
466 goto requeue_and_leave;
467 case -ECONNABORTED:
468 default:
71f3ca40 469 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
17926a79
DH
470 break;
471 }
472 }
473
474out:
475 rxrpc_put_connection(conn);
476 _leave("");
477 return;
478
479requeue_and_leave:
480 skb_queue_head(&conn->rx_queue, skb);
481 goto out;
482
483protocol_error:
3a92789a 484 if (rxrpc_abort_connection(conn, ret, abort_code) < 0)
17926a79 485 goto requeue_and_leave;
71f3ca40 486 rxrpc_free_skb(skb, rxrpc_skb_rx_freed);
17926a79
DH
487 goto out;
488}