8d0b9ff0a5e154e50ddf4825c9ce3007ac1a20ac
[platform/kernel/linux-rpi.git] / net / rxrpc / conn_event.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* connection-level event handling
3  *
4  * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9
10 #include <linux/module.h>
11 #include <linux/net.h>
12 #include <linux/skbuff.h>
13 #include <linux/errqueue.h>
14 #include <net/sock.h>
15 #include <net/af_rxrpc.h>
16 #include <net/ip.h>
17 #include "ar-internal.h"
18
19 /*
20  * Set the completion state on an aborted connection.
21  */
22 static bool rxrpc_set_conn_aborted(struct rxrpc_connection *conn, struct sk_buff *skb,
23                                    s32 abort_code, int err,
24                                    enum rxrpc_call_completion compl)
25 {
26         bool aborted = false;
27
28         if (conn->state != RXRPC_CONN_ABORTED) {
29                 spin_lock(&conn->state_lock);
30                 if (conn->state != RXRPC_CONN_ABORTED) {
31                         conn->abort_code = abort_code;
32                         conn->error      = err;
33                         conn->completion = compl;
34                         /* Order the abort info before the state change. */
35                         smp_store_release(&conn->state, RXRPC_CONN_ABORTED);
36                         set_bit(RXRPC_CONN_DONT_REUSE, &conn->flags);
37                         set_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events);
38                         aborted = true;
39                 }
40                 spin_unlock(&conn->state_lock);
41         }
42
43         return aborted;
44 }
45
46 /*
47  * Mark a socket buffer to indicate that the connection it's on should be aborted.
48  */
49 int rxrpc_abort_conn(struct rxrpc_connection *conn, struct sk_buff *skb,
50                      s32 abort_code, int err, enum rxrpc_abort_reason why)
51 {
52         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
53
54         if (rxrpc_set_conn_aborted(conn, skb, abort_code, err,
55                                    RXRPC_CALL_LOCALLY_ABORTED)) {
56                 trace_rxrpc_abort(0, why, sp->hdr.cid, sp->hdr.callNumber,
57                                   sp->hdr.seq, abort_code, err);
58                 rxrpc_poke_conn(conn, rxrpc_conn_get_poke_abort);
59         }
60         return -EPROTO;
61 }
62
63 /*
64  * Mark a connection as being remotely aborted.
65  */
66 static bool rxrpc_input_conn_abort(struct rxrpc_connection *conn,
67                                    struct sk_buff *skb)
68 {
69         return rxrpc_set_conn_aborted(conn, skb, skb->priority, -ECONNABORTED,
70                                       RXRPC_CALL_REMOTELY_ABORTED);
71 }
72
73 /*
74  * Retransmit terminal ACK or ABORT of the previous call.
75  */
76 void rxrpc_conn_retransmit_call(struct rxrpc_connection *conn,
77                                 struct sk_buff *skb,
78                                 unsigned int channel)
79 {
80         struct rxrpc_skb_priv *sp = skb ? rxrpc_skb(skb) : NULL;
81         struct rxrpc_channel *chan;
82         struct msghdr msg;
83         struct kvec iov[3];
84         struct {
85                 struct rxrpc_wire_header whdr;
86                 union {
87                         __be32 abort_code;
88                         struct rxrpc_ackpacket ack;
89                 };
90         } __attribute__((packed)) pkt;
91         struct rxrpc_ackinfo ack_info;
92         size_t len;
93         int ret, ioc;
94         u32 serial, mtu, call_id, padding;
95
96         _enter("%d", conn->debug_id);
97
98         chan = &conn->channels[channel];
99
100         /* If the last call got moved on whilst we were waiting to run, just
101          * ignore this packet.
102          */
103         call_id = READ_ONCE(chan->last_call);
104         /* Sync with __rxrpc_disconnect_call() */
105         smp_rmb();
106         if (skb && call_id != sp->hdr.callNumber)
107                 return;
108
109         msg.msg_name    = &conn->peer->srx.transport;
110         msg.msg_namelen = conn->peer->srx.transport_len;
111         msg.msg_control = NULL;
112         msg.msg_controllen = 0;
113         msg.msg_flags   = 0;
114
115         iov[0].iov_base = &pkt;
116         iov[0].iov_len  = sizeof(pkt.whdr);
117         iov[1].iov_base = &padding;
118         iov[1].iov_len  = 3;
119         iov[2].iov_base = &ack_info;
120         iov[2].iov_len  = sizeof(ack_info);
121
122         pkt.whdr.epoch          = htonl(conn->proto.epoch);
123         pkt.whdr.cid            = htonl(conn->proto.cid | channel);
124         pkt.whdr.callNumber     = htonl(call_id);
125         pkt.whdr.seq            = 0;
126         pkt.whdr.type           = chan->last_type;
127         pkt.whdr.flags          = conn->out_clientflag;
128         pkt.whdr.userStatus     = 0;
129         pkt.whdr.securityIndex  = conn->security_ix;
130         pkt.whdr._rsvd          = 0;
131         pkt.whdr.serviceId      = htons(conn->service_id);
132
133         len = sizeof(pkt.whdr);
134         switch (chan->last_type) {
135         case RXRPC_PACKET_TYPE_ABORT:
136                 pkt.abort_code  = htonl(chan->last_abort);
137                 iov[0].iov_len += sizeof(pkt.abort_code);
138                 len += sizeof(pkt.abort_code);
139                 ioc = 1;
140                 break;
141
142         case RXRPC_PACKET_TYPE_ACK:
143                 mtu = conn->peer->if_mtu;
144                 mtu -= conn->peer->hdrsize;
145                 pkt.ack.bufferSpace     = 0;
146                 pkt.ack.maxSkew         = htons(skb ? skb->priority : 0);
147                 pkt.ack.firstPacket     = htonl(chan->last_seq + 1);
148                 pkt.ack.previousPacket  = htonl(chan->last_seq);
149                 pkt.ack.serial          = htonl(skb ? sp->hdr.serial : 0);
150                 pkt.ack.reason          = skb ? RXRPC_ACK_DUPLICATE : RXRPC_ACK_IDLE;
151                 pkt.ack.nAcks           = 0;
152                 ack_info.rxMTU          = htonl(rxrpc_rx_mtu);
153                 ack_info.maxMTU         = htonl(mtu);
154                 ack_info.rwind          = htonl(rxrpc_rx_window_size);
155                 ack_info.jumbo_max      = htonl(rxrpc_rx_jumbo_max);
156                 pkt.whdr.flags          |= RXRPC_SLOW_START_OK;
157                 padding                 = 0;
158                 iov[0].iov_len += sizeof(pkt.ack);
159                 len += sizeof(pkt.ack) + 3 + sizeof(ack_info);
160                 ioc = 3;
161                 break;
162
163         default:
164                 return;
165         }
166
167         /* Resync with __rxrpc_disconnect_call() and check that the last call
168          * didn't get advanced whilst we were filling out the packets.
169          */
170         smp_rmb();
171         if (READ_ONCE(chan->last_call) != call_id)
172                 return;
173
174         serial = atomic_inc_return(&conn->serial);
175         pkt.whdr.serial = htonl(serial);
176
177         switch (chan->last_type) {
178         case RXRPC_PACKET_TYPE_ABORT:
179                 break;
180         case RXRPC_PACKET_TYPE_ACK:
181                 trace_rxrpc_tx_ack(chan->call_debug_id, serial,
182                                    ntohl(pkt.ack.firstPacket),
183                                    ntohl(pkt.ack.serial),
184                                    pkt.ack.reason, 0);
185                 break;
186         }
187
188         ret = kernel_sendmsg(conn->local->socket, &msg, iov, ioc, len);
189         conn->peer->last_tx_at = ktime_get_seconds();
190         if (ret < 0)
191                 trace_rxrpc_tx_fail(chan->call_debug_id, serial, ret,
192                                     rxrpc_tx_point_call_final_resend);
193         else
194                 trace_rxrpc_tx_packet(chan->call_debug_id, &pkt.whdr,
195                                       rxrpc_tx_point_call_final_resend);
196
197         _leave("");
198 }
199
200 /*
201  * pass a connection-level abort onto all calls on that connection
202  */
203 static void rxrpc_abort_calls(struct rxrpc_connection *conn)
204 {
205         struct rxrpc_call *call;
206         int i;
207
208         _enter("{%d},%x", conn->debug_id, conn->abort_code);
209
210         spin_lock(&conn->bundle->channel_lock);
211
212         for (i = 0; i < RXRPC_MAXCALLS; i++) {
213                 call = rcu_dereference_protected(
214                         conn->channels[i].call,
215                         lockdep_is_held(&conn->bundle->channel_lock));
216                 if (call)
217                         rxrpc_set_call_completion(call,
218                                                   conn->completion,
219                                                   conn->abort_code,
220                                                   conn->error);
221         }
222
223         spin_unlock(&conn->bundle->channel_lock);
224         _leave("");
225 }
226
227 /*
228  * mark a call as being on a now-secured channel
229  * - must be called with BH's disabled.
230  */
231 static void rxrpc_call_is_secure(struct rxrpc_call *call)
232 {
233         if (call && __rxrpc_call_state(call) == RXRPC_CALL_SERVER_SECURING) {
234                 rxrpc_set_call_state(call, RXRPC_CALL_SERVER_RECV_REQUEST);
235                 rxrpc_notify_socket(call);
236         }
237 }
238
239 /*
240  * connection-level Rx packet processor
241  */
242 static int rxrpc_process_event(struct rxrpc_connection *conn,
243                                struct sk_buff *skb)
244 {
245         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
246         int ret;
247
248         if (conn->state == RXRPC_CONN_ABORTED)
249                 return -ECONNABORTED;
250
251         _enter("{%d},{%u,%%%u},", conn->debug_id, sp->hdr.type, sp->hdr.serial);
252
253         switch (sp->hdr.type) {
254         case RXRPC_PACKET_TYPE_CHALLENGE:
255                 return conn->security->respond_to_challenge(conn, skb);
256
257         case RXRPC_PACKET_TYPE_RESPONSE:
258                 ret = conn->security->verify_response(conn, skb);
259                 if (ret < 0)
260                         return ret;
261
262                 ret = conn->security->init_connection_security(
263                         conn, conn->key->payload.data[0]);
264                 if (ret < 0)
265                         return ret;
266
267                 spin_lock(&conn->state_lock);
268                 if (conn->state == RXRPC_CONN_SERVICE_CHALLENGING)
269                         conn->state = RXRPC_CONN_SERVICE;
270                 spin_unlock(&conn->state_lock);
271
272                 if (conn->state == RXRPC_CONN_SERVICE) {
273                         /* Offload call state flipping to the I/O thread.  As
274                          * we've already received the packet, put it on the
275                          * front of the queue.
276                          */
277                         skb->mark = RXRPC_SKB_MARK_SERVICE_CONN_SECURED;
278                         rxrpc_get_skb(skb, rxrpc_skb_get_conn_secured);
279                         skb_queue_head(&conn->local->rx_queue, skb);
280                         rxrpc_wake_up_io_thread(conn->local);
281                 }
282                 return 0;
283
284         default:
285                 WARN_ON_ONCE(1);
286                 return -EPROTO;
287         }
288 }
289
290 /*
291  * set up security and issue a challenge
292  */
293 static void rxrpc_secure_connection(struct rxrpc_connection *conn)
294 {
295         if (conn->security->issue_challenge(conn) < 0)
296                 rxrpc_abort_conn(conn, NULL, RX_CALL_DEAD, -ENOMEM,
297                                  rxrpc_abort_nomem);
298 }
299
300 /*
301  * Process delayed final ACKs that we haven't subsumed into a subsequent call.
302  */
303 void rxrpc_process_delayed_final_acks(struct rxrpc_connection *conn, bool force)
304 {
305         unsigned long j = jiffies, next_j;
306         unsigned int channel;
307         bool set;
308
309 again:
310         next_j = j + LONG_MAX;
311         set = false;
312         for (channel = 0; channel < RXRPC_MAXCALLS; channel++) {
313                 struct rxrpc_channel *chan = &conn->channels[channel];
314                 unsigned long ack_at;
315
316                 if (!test_bit(RXRPC_CONN_FINAL_ACK_0 + channel, &conn->flags))
317                         continue;
318
319                 smp_rmb(); /* vs rxrpc_disconnect_client_call */
320                 ack_at = READ_ONCE(chan->final_ack_at);
321
322                 if (time_before(j, ack_at) && !force) {
323                         if (time_before(ack_at, next_j)) {
324                                 next_j = ack_at;
325                                 set = true;
326                         }
327                         continue;
328                 }
329
330                 if (test_and_clear_bit(RXRPC_CONN_FINAL_ACK_0 + channel,
331                                        &conn->flags))
332                         rxrpc_conn_retransmit_call(conn, NULL, channel);
333         }
334
335         j = jiffies;
336         if (time_before_eq(next_j, j))
337                 goto again;
338         if (set)
339                 rxrpc_reduce_conn_timer(conn, next_j);
340 }
341
342 /*
343  * connection-level event processor
344  */
345 static void rxrpc_do_process_connection(struct rxrpc_connection *conn)
346 {
347         struct sk_buff *skb;
348         int ret;
349
350         if (test_and_clear_bit(RXRPC_CONN_EV_CHALLENGE, &conn->events))
351                 rxrpc_secure_connection(conn);
352
353         /* go through the conn-level event packets, releasing the ref on this
354          * connection that each one has when we've finished with it */
355         while ((skb = skb_dequeue(&conn->rx_queue))) {
356                 rxrpc_see_skb(skb, rxrpc_skb_see_conn_work);
357                 ret = rxrpc_process_event(conn, skb);
358                 switch (ret) {
359                 case -ENOMEM:
360                 case -EAGAIN:
361                         skb_queue_head(&conn->rx_queue, skb);
362                         rxrpc_queue_conn(conn, rxrpc_conn_queue_retry_work);
363                         break;
364                 default:
365                         rxrpc_free_skb(skb, rxrpc_skb_put_conn_work);
366                         break;
367                 }
368         }
369 }
370
371 void rxrpc_process_connection(struct work_struct *work)
372 {
373         struct rxrpc_connection *conn =
374                 container_of(work, struct rxrpc_connection, processor);
375
376         rxrpc_see_connection(conn, rxrpc_conn_see_work);
377
378         if (__rxrpc_use_local(conn->local, rxrpc_local_use_conn_work)) {
379                 rxrpc_do_process_connection(conn);
380                 rxrpc_unuse_local(conn->local, rxrpc_local_unuse_conn_work);
381         }
382 }
383
384 /*
385  * post connection-level events to the connection
386  * - this includes challenges, responses, some aborts and call terminal packet
387  *   retransmission.
388  */
389 static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn,
390                                       struct sk_buff *skb)
391 {
392         _enter("%p,%p", conn, skb);
393
394         rxrpc_get_skb(skb, rxrpc_skb_get_conn_work);
395         skb_queue_tail(&conn->rx_queue, skb);
396         rxrpc_queue_conn(conn, rxrpc_conn_queue_rx_work);
397 }
398
399 /*
400  * Input a connection-level packet.
401  */
402 bool rxrpc_input_conn_packet(struct rxrpc_connection *conn, struct sk_buff *skb)
403 {
404         struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
405
406         switch (sp->hdr.type) {
407         case RXRPC_PACKET_TYPE_BUSY:
408                 /* Just ignore BUSY packets for now. */
409                 return true;
410
411         case RXRPC_PACKET_TYPE_ABORT:
412                 if (rxrpc_is_conn_aborted(conn))
413                         return true;
414                 rxrpc_input_conn_abort(conn, skb);
415                 rxrpc_abort_calls(conn);
416                 return true;
417
418         case RXRPC_PACKET_TYPE_CHALLENGE:
419         case RXRPC_PACKET_TYPE_RESPONSE:
420                 if (rxrpc_is_conn_aborted(conn)) {
421                         if (conn->completion == RXRPC_CALL_LOCALLY_ABORTED)
422                                 rxrpc_send_conn_abort(conn);
423                         return true;
424                 }
425                 rxrpc_post_packet_to_conn(conn, skb);
426                 return true;
427
428         default:
429                 WARN_ON_ONCE(1);
430                 return true;
431         }
432 }
433
434 /*
435  * Input a connection event.
436  */
437 void rxrpc_input_conn_event(struct rxrpc_connection *conn, struct sk_buff *skb)
438 {
439         unsigned int loop;
440
441         if (test_and_clear_bit(RXRPC_CONN_EV_ABORT_CALLS, &conn->events))
442                 rxrpc_abort_calls(conn);
443
444         switch (skb->mark) {
445         case RXRPC_SKB_MARK_SERVICE_CONN_SECURED:
446                 if (conn->state != RXRPC_CONN_SERVICE)
447                         break;
448
449                 spin_lock(&conn->bundle->channel_lock);
450
451                 for (loop = 0; loop < RXRPC_MAXCALLS; loop++)
452                         rxrpc_call_is_secure(
453                                 rcu_dereference_protected(
454                                         conn->channels[loop].call,
455                                         lockdep_is_held(&conn->bundle->channel_lock)));
456
457                 spin_unlock(&conn->bundle->channel_lock);
458                 break;
459         }
460
461         /* Process delayed ACKs whose time has come. */
462         if (conn->flags & RXRPC_CONN_FINAL_ACK_MASK)
463                 rxrpc_process_delayed_final_acks(conn, false);
464 }