David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1 | /* RxRPC packet reception |
| 2 | * |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 3 | * Copyright (C) 2007, 2016 Red Hat, Inc. All Rights Reserved. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 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 Perches | 9b6d539 | 2016-06-02 12:08:52 -0700 | [diff] [blame] | 12 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 13 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 14 | #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> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/gfp.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 23 | #include <net/sock.h> |
| 24 | #include <net/af_rxrpc.h> |
| 25 | #include <net/ip.h> |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 26 | #include <net/udp.h> |
Pavel Emelyanov | 0283328 | 2008-07-05 21:18:48 -0700 | [diff] [blame] | 27 | #include <net/net_namespace.h> |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 28 | #include "ar-internal.h" |
| 29 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 30 | static void rxrpc_proto_abort(const char *why, |
| 31 | struct rxrpc_call *call, rxrpc_seq_t seq) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 32 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 33 | if (rxrpc_abort_call(why, call, seq, RX_PROTOCOL_ERROR, EBADMSG)) { |
| 34 | set_bit(RXRPC_CALL_EV_ABORT, &call->events); |
| 35 | rxrpc_queue_call(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 36 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | /* |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 40 | * Ping the other end to fill our RTT cache and to retrieve the rwind |
| 41 | * and MTU parameters. |
| 42 | */ |
| 43 | static void rxrpc_send_ping(struct rxrpc_call *call, struct sk_buff *skb, |
| 44 | int skew) |
| 45 | { |
| 46 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | fc943f6 | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 47 | ktime_t now = skb->tstamp; |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 48 | |
David Howells | fc943f6 | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 49 | if (call->peer->rtt_usage < 3 || |
| 50 | ktime_before(ktime_add_ms(call->peer->rtt_last_req, 1000), now)) |
| 51 | rxrpc_propose_ACK(call, RXRPC_ACK_PING, skew, sp->hdr.serial, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 52 | true, true, |
| 53 | rxrpc_propose_ack_ping_for_params); |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 57 | * Apply a hard ACK by advancing the Tx window. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 58 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 59 | static void rxrpc_rotate_tx_window(struct rxrpc_call *call, rxrpc_seq_t to) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 60 | { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 61 | struct sk_buff *skb, *list = NULL; |
| 62 | int ix; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 63 | u8 annotation; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 64 | |
| 65 | spin_lock(&call->lock); |
| 66 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 67 | while (before(call->tx_hard_ack, to)) { |
| 68 | call->tx_hard_ack++; |
| 69 | ix = call->tx_hard_ack & RXRPC_RXTX_BUFF_MASK; |
| 70 | skb = call->rxtx_buffer[ix]; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 71 | annotation = call->rxtx_annotations[ix]; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 72 | rxrpc_see_skb(skb, rxrpc_skb_tx_rotated); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 73 | call->rxtx_buffer[ix] = NULL; |
| 74 | call->rxtx_annotations[ix] = 0; |
| 75 | skb->next = list; |
| 76 | list = skb; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 77 | |
| 78 | if (annotation & RXRPC_TX_ANNO_LAST) |
| 79 | set_bit(RXRPC_CALL_TX_LAST, &call->flags); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | spin_unlock(&call->lock); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 83 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 84 | trace_rxrpc_transmit(call, (test_bit(RXRPC_CALL_TX_LAST, &call->flags) ? |
| 85 | rxrpc_transmit_rotate_last : |
| 86 | rxrpc_transmit_rotate)); |
David Howells | bc4abfc | 2016-09-13 22:36:21 +0100 | [diff] [blame] | 87 | wake_up(&call->waitq); |
| 88 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 89 | while (list) { |
| 90 | skb = list; |
| 91 | list = skb->next; |
| 92 | skb->next = NULL; |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 93 | rxrpc_free_skb(skb, rxrpc_skb_tx_freed); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 94 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 98 | * End the transmission phase of a call. |
| 99 | * |
| 100 | * This occurs when we get an ACKALL packet, the first DATA packet of a reply, |
| 101 | * or a final ACK packet. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 102 | */ |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 103 | static bool rxrpc_end_tx_phase(struct rxrpc_call *call, bool reply_begun, |
| 104 | const char *abort_why) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 105 | { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 106 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 107 | ASSERT(test_bit(RXRPC_CALL_TX_LAST, &call->flags)); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 108 | |
| 109 | write_lock(&call->state_lock); |
| 110 | |
| 111 | switch (call->state) { |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 112 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 113 | case RXRPC_CALL_CLIENT_AWAIT_REPLY: |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 114 | if (reply_begun) |
| 115 | call->state = RXRPC_CALL_CLIENT_RECV_REPLY; |
| 116 | else |
| 117 | call->state = RXRPC_CALL_CLIENT_AWAIT_REPLY; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 118 | break; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 119 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 120 | case RXRPC_CALL_SERVER_AWAIT_ACK: |
| 121 | __rxrpc_call_completed(call); |
| 122 | rxrpc_notify_socket(call); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 123 | break; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 124 | |
| 125 | default: |
| 126 | goto bad_state; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 127 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 128 | |
| 129 | write_unlock(&call->state_lock); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 130 | if (call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) { |
| 131 | trace_rxrpc_transmit(call, rxrpc_transmit_await_reply); |
| 132 | } else { |
| 133 | trace_rxrpc_transmit(call, rxrpc_transmit_end); |
| 134 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 135 | _leave(" = ok"); |
| 136 | return true; |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 137 | |
| 138 | bad_state: |
| 139 | write_unlock(&call->state_lock); |
| 140 | kdebug("end_tx %s", rxrpc_call_states[call->state]); |
| 141 | rxrpc_proto_abort(abort_why, call, call->tx_top); |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /* |
| 146 | * Begin the reply reception phase of a call. |
| 147 | */ |
| 148 | static bool rxrpc_receiving_reply(struct rxrpc_call *call) |
| 149 | { |
| 150 | rxrpc_seq_t top = READ_ONCE(call->tx_top); |
| 151 | |
| 152 | if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) |
| 153 | rxrpc_rotate_tx_window(call, top); |
| 154 | if (!test_bit(RXRPC_CALL_TX_LAST, &call->flags)) { |
| 155 | rxrpc_proto_abort("TXL", call, top); |
| 156 | return false; |
| 157 | } |
| 158 | if (!rxrpc_end_tx_phase(call, true, "ETD")) |
| 159 | return false; |
| 160 | call->tx_phase = false; |
| 161 | return true; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | /* |
| 165 | * Scan a jumbo packet to validate its structure and to work out how many |
| 166 | * subpackets it contains. |
| 167 | * |
| 168 | * A jumbo packet is a collection of consecutive packets glued together with |
| 169 | * little headers between that indicate how to change the initial header for |
| 170 | * each subpacket. |
| 171 | * |
| 172 | * RXRPC_JUMBO_PACKET must be set on all but the last subpacket - and all but |
| 173 | * the last are RXRPC_JUMBO_DATALEN in size. The last subpacket may be of any |
| 174 | * size. |
| 175 | */ |
| 176 | static bool rxrpc_validate_jumbo(struct sk_buff *skb) |
| 177 | { |
| 178 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 179 | unsigned int offset = sp->offset; |
David Howells | 89a80ed | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 180 | unsigned int len = skb->len; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 181 | int nr_jumbo = 1; |
| 182 | u8 flags = sp->hdr.flags; |
| 183 | |
| 184 | do { |
| 185 | nr_jumbo++; |
| 186 | if (len - offset < RXRPC_JUMBO_SUBPKTLEN) |
| 187 | goto protocol_error; |
| 188 | if (flags & RXRPC_LAST_PACKET) |
| 189 | goto protocol_error; |
| 190 | offset += RXRPC_JUMBO_DATALEN; |
| 191 | if (skb_copy_bits(skb, offset, &flags, 1) < 0) |
| 192 | goto protocol_error; |
| 193 | offset += sizeof(struct rxrpc_jumbo_header); |
| 194 | } while (flags & RXRPC_JUMBO_PACKET); |
| 195 | |
| 196 | sp->nr_jumbo = nr_jumbo; |
| 197 | return true; |
| 198 | |
| 199 | protocol_error: |
| 200 | return false; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * Handle reception of a duplicate packet. |
| 205 | * |
| 206 | * We have to take care to avoid an attack here whereby we're given a series of |
| 207 | * jumbograms, each with a sequence number one before the preceding one and |
| 208 | * filled up to maximum UDP size. If they never send us the first packet in |
| 209 | * the sequence, they can cause us to have to hold on to around 2MiB of kernel |
| 210 | * space until the call times out. |
| 211 | * |
| 212 | * We limit the space usage by only accepting three duplicate jumbo packets per |
| 213 | * call. After that, we tell the other side we're no longer accepting jumbos |
| 214 | * (that information is encoded in the ACK packet). |
| 215 | */ |
| 216 | static void rxrpc_input_dup_data(struct rxrpc_call *call, rxrpc_seq_t seq, |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 217 | u8 annotation, bool *_jumbo_bad) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 218 | { |
| 219 | /* Discard normal packets that are duplicates. */ |
| 220 | if (annotation == 0) |
| 221 | return; |
| 222 | |
| 223 | /* Skip jumbo subpackets that are duplicates. When we've had three or |
| 224 | * more partially duplicate jumbo packets, we refuse to take any more |
| 225 | * jumbos for this call. |
| 226 | */ |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 227 | if (!*_jumbo_bad) { |
| 228 | call->nr_jumbo_bad++; |
| 229 | *_jumbo_bad = true; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 230 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 234 | * Process a DATA packet, adding the packet to the Rx ring. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 235 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 236 | static void rxrpc_input_data(struct rxrpc_call *call, struct sk_buff *skb, |
| 237 | u16 skew) |
| 238 | { |
| 239 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 240 | unsigned int offset = sp->offset; |
| 241 | unsigned int ix; |
| 242 | rxrpc_serial_t serial = sp->hdr.serial, ack_serial = 0; |
| 243 | rxrpc_seq_t seq = sp->hdr.seq, hard_ack; |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 244 | bool immediate_ack = false, jumbo_bad = false, queued; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 245 | u16 len; |
| 246 | u8 ack = 0, flags, annotation = 0; |
| 247 | |
| 248 | _enter("{%u,%u},{%u,%u}", |
David Howells | 89a80ed | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 249 | call->rx_hard_ack, call->rx_top, skb->len, seq); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 250 | |
| 251 | _proto("Rx DATA %%%u { #%u f=%02x }", |
| 252 | sp->hdr.serial, seq, sp->hdr.flags); |
| 253 | |
| 254 | if (call->state >= RXRPC_CALL_COMPLETE) |
| 255 | return; |
| 256 | |
| 257 | /* Received data implicitly ACKs all of the request packets we sent |
| 258 | * when we're acting as a client. |
| 259 | */ |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 260 | if ((call->state == RXRPC_CALL_CLIENT_SEND_REQUEST || |
| 261 | call->state == RXRPC_CALL_CLIENT_AWAIT_REPLY) && |
| 262 | !rxrpc_receiving_reply(call)) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 263 | return; |
| 264 | |
| 265 | call->ackr_prev_seq = seq; |
| 266 | |
| 267 | hard_ack = READ_ONCE(call->rx_hard_ack); |
| 268 | if (after(seq, hard_ack + call->rx_winsize)) { |
| 269 | ack = RXRPC_ACK_EXCEEDS_WINDOW; |
| 270 | ack_serial = serial; |
| 271 | goto ack; |
| 272 | } |
| 273 | |
| 274 | flags = sp->hdr.flags; |
| 275 | if (flags & RXRPC_JUMBO_PACKET) { |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 276 | if (call->nr_jumbo_bad > 3) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 277 | ack = RXRPC_ACK_NOSPACE; |
| 278 | ack_serial = serial; |
| 279 | goto ack; |
| 280 | } |
| 281 | annotation = 1; |
| 282 | } |
| 283 | |
| 284 | next_subpacket: |
| 285 | queued = false; |
| 286 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
David Howells | 89a80ed | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 287 | len = skb->len; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 288 | if (flags & RXRPC_JUMBO_PACKET) |
| 289 | len = RXRPC_JUMBO_DATALEN; |
| 290 | |
| 291 | if (flags & RXRPC_LAST_PACKET) { |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 292 | if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) && |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 293 | seq != call->rx_top) |
| 294 | return rxrpc_proto_abort("LSN", call, seq); |
| 295 | } else { |
| 296 | if (test_bit(RXRPC_CALL_RX_LAST, &call->flags) && |
| 297 | after_eq(seq, call->rx_top)) |
| 298 | return rxrpc_proto_abort("LSA", call, seq); |
| 299 | } |
| 300 | |
| 301 | if (before_eq(seq, hard_ack)) { |
| 302 | ack = RXRPC_ACK_DUPLICATE; |
| 303 | ack_serial = serial; |
| 304 | goto skip; |
| 305 | } |
| 306 | |
| 307 | if (flags & RXRPC_REQUEST_ACK && !ack) { |
| 308 | ack = RXRPC_ACK_REQUESTED; |
| 309 | ack_serial = serial; |
| 310 | } |
| 311 | |
| 312 | if (call->rxtx_buffer[ix]) { |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 313 | rxrpc_input_dup_data(call, seq, annotation, &jumbo_bad); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 314 | if (ack != RXRPC_ACK_DUPLICATE) { |
| 315 | ack = RXRPC_ACK_DUPLICATE; |
| 316 | ack_serial = serial; |
| 317 | } |
| 318 | immediate_ack = true; |
| 319 | goto skip; |
| 320 | } |
| 321 | |
| 322 | /* Queue the packet. We use a couple of memory barriers here as need |
| 323 | * to make sure that rx_top is perceived to be set after the buffer |
| 324 | * pointer and that the buffer pointer is set after the annotation and |
| 325 | * the skb data. |
| 326 | * |
| 327 | * Barriers against rxrpc_recvmsg_data() and rxrpc_rotate_rx_window() |
| 328 | * and also rxrpc_fill_out_ack(). |
| 329 | */ |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 330 | rxrpc_get_skb(skb, rxrpc_skb_rx_got); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 331 | call->rxtx_annotations[ix] = annotation; |
| 332 | smp_wmb(); |
| 333 | call->rxtx_buffer[ix] = skb; |
David Howells | a7056c5 | 2016-09-24 18:05:27 +0100 | [diff] [blame^] | 334 | if (after(seq, call->rx_top)) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 335 | smp_store_release(&call->rx_top, seq); |
David Howells | a7056c5 | 2016-09-24 18:05:27 +0100 | [diff] [blame^] | 336 | } else if (before(seq, call->rx_top)) { |
| 337 | /* Send an immediate ACK if we fill in a hole */ |
| 338 | if (!ack) { |
| 339 | ack = RXRPC_ACK_DELAY; |
| 340 | ack_serial = serial; |
| 341 | } |
| 342 | immediate_ack = true; |
| 343 | } |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 344 | if (flags & RXRPC_LAST_PACKET) { |
David Howells | 816c9fc | 2016-09-17 10:49:11 +0100 | [diff] [blame] | 345 | set_bit(RXRPC_CALL_RX_LAST, &call->flags); |
David Howells | 58dc63c | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 346 | trace_rxrpc_receive(call, rxrpc_receive_queue_last, serial, seq); |
| 347 | } else { |
| 348 | trace_rxrpc_receive(call, rxrpc_receive_queue, serial, seq); |
| 349 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 350 | queued = true; |
| 351 | |
| 352 | if (after_eq(seq, call->rx_expect_next)) { |
| 353 | if (after(seq, call->rx_expect_next)) { |
| 354 | _net("OOS %u > %u", seq, call->rx_expect_next); |
| 355 | ack = RXRPC_ACK_OUT_OF_SEQUENCE; |
| 356 | ack_serial = serial; |
| 357 | } |
| 358 | call->rx_expect_next = seq + 1; |
| 359 | } |
| 360 | |
| 361 | skip: |
| 362 | offset += len; |
| 363 | if (flags & RXRPC_JUMBO_PACKET) { |
| 364 | if (skb_copy_bits(skb, offset, &flags, 1) < 0) |
| 365 | return rxrpc_proto_abort("XJF", call, seq); |
| 366 | offset += sizeof(struct rxrpc_jumbo_header); |
| 367 | seq++; |
| 368 | serial++; |
| 369 | annotation++; |
| 370 | if (flags & RXRPC_JUMBO_PACKET) |
| 371 | annotation |= RXRPC_RX_ANNO_JLAST; |
David Howells | 75e4212 | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 372 | if (after(seq, hard_ack + call->rx_winsize)) { |
| 373 | ack = RXRPC_ACK_EXCEEDS_WINDOW; |
| 374 | ack_serial = serial; |
| 375 | if (!jumbo_bad) { |
| 376 | call->nr_jumbo_bad++; |
| 377 | jumbo_bad = true; |
| 378 | } |
| 379 | goto ack; |
| 380 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 381 | |
| 382 | _proto("Rx DATA Jumbo %%%u", serial); |
| 383 | goto next_subpacket; |
| 384 | } |
| 385 | |
| 386 | if (queued && flags & RXRPC_LAST_PACKET && !ack) { |
| 387 | ack = RXRPC_ACK_DELAY; |
| 388 | ack_serial = serial; |
| 389 | } |
| 390 | |
| 391 | ack: |
| 392 | if (ack) |
| 393 | rxrpc_propose_ACK(call, ack, skew, ack_serial, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 394 | immediate_ack, true, |
| 395 | rxrpc_propose_ack_input_data); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 396 | |
| 397 | if (sp->hdr.seq == READ_ONCE(call->rx_hard_ack) + 1) |
| 398 | rxrpc_notify_socket(call); |
| 399 | _leave(" [queued]"); |
| 400 | } |
| 401 | |
| 402 | /* |
David Howells | 50235c4 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 403 | * Process a requested ACK. |
| 404 | */ |
| 405 | static void rxrpc_input_requested_ack(struct rxrpc_call *call, |
| 406 | ktime_t resp_time, |
| 407 | rxrpc_serial_t orig_serial, |
| 408 | rxrpc_serial_t ack_serial) |
| 409 | { |
| 410 | struct rxrpc_skb_priv *sp; |
| 411 | struct sk_buff *skb; |
| 412 | ktime_t sent_at; |
| 413 | int ix; |
| 414 | |
| 415 | for (ix = 0; ix < RXRPC_RXTX_BUFF_SIZE; ix++) { |
| 416 | skb = call->rxtx_buffer[ix]; |
| 417 | if (!skb) |
| 418 | continue; |
| 419 | |
| 420 | sp = rxrpc_skb(skb); |
| 421 | if (sp->hdr.serial != orig_serial) |
| 422 | continue; |
| 423 | smp_rmb(); |
| 424 | sent_at = skb->tstamp; |
| 425 | goto found; |
| 426 | } |
| 427 | return; |
| 428 | |
| 429 | found: |
| 430 | rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_requested_ack, |
| 431 | orig_serial, ack_serial, sent_at, resp_time); |
| 432 | } |
| 433 | |
| 434 | /* |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 435 | * Process a ping response. |
| 436 | */ |
| 437 | static void rxrpc_input_ping_response(struct rxrpc_call *call, |
| 438 | ktime_t resp_time, |
| 439 | rxrpc_serial_t orig_serial, |
| 440 | rxrpc_serial_t ack_serial) |
| 441 | { |
| 442 | rxrpc_serial_t ping_serial; |
| 443 | ktime_t ping_time; |
| 444 | |
| 445 | ping_time = call->ackr_ping_time; |
| 446 | smp_rmb(); |
| 447 | ping_serial = call->ackr_ping; |
| 448 | |
| 449 | if (!test_bit(RXRPC_CALL_PINGING, &call->flags) || |
| 450 | before(orig_serial, ping_serial)) |
| 451 | return; |
| 452 | clear_bit(RXRPC_CALL_PINGING, &call->flags); |
| 453 | if (after(orig_serial, ping_serial)) |
| 454 | return; |
| 455 | |
| 456 | rxrpc_peer_add_rtt(call, rxrpc_rtt_rx_ping_response, |
| 457 | orig_serial, ack_serial, ping_time, resp_time); |
| 458 | } |
| 459 | |
| 460 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 461 | * Process the extra information that may be appended to an ACK packet |
| 462 | */ |
| 463 | static void rxrpc_input_ackinfo(struct rxrpc_call *call, struct sk_buff *skb, |
| 464 | struct rxrpc_ackinfo *ackinfo) |
| 465 | { |
| 466 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 467 | struct rxrpc_peer *peer; |
| 468 | unsigned int mtu; |
David Howells | 01fd074 | 2016-09-13 10:23:01 +0100 | [diff] [blame] | 469 | u32 rwind = ntohl(ackinfo->rwind); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 470 | |
| 471 | _proto("Rx ACK %%%u Info { rx=%u max=%u rwin=%u jm=%u }", |
| 472 | sp->hdr.serial, |
| 473 | ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU), |
David Howells | 01fd074 | 2016-09-13 10:23:01 +0100 | [diff] [blame] | 474 | rwind, ntohl(ackinfo->jumbo_max)); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 475 | |
David Howells | 01fd074 | 2016-09-13 10:23:01 +0100 | [diff] [blame] | 476 | if (rwind > RXRPC_RXTX_BUFF_SIZE - 1) |
| 477 | rwind = RXRPC_RXTX_BUFF_SIZE - 1; |
| 478 | call->tx_winsize = rwind; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 479 | |
| 480 | mtu = min(ntohl(ackinfo->rxMTU), ntohl(ackinfo->maxMTU)); |
| 481 | |
| 482 | peer = call->peer; |
| 483 | if (mtu < peer->maxdata) { |
| 484 | spin_lock_bh(&peer->lock); |
| 485 | peer->maxdata = mtu; |
| 486 | peer->mtu = mtu + peer->hdrsize; |
| 487 | spin_unlock_bh(&peer->lock); |
| 488 | _net("Net MTU %u (maxdata %u)", peer->mtu, peer->maxdata); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | /* |
| 493 | * Process individual soft ACKs. |
| 494 | * |
| 495 | * Each ACK in the array corresponds to one packet and can be either an ACK or |
| 496 | * a NAK. If we get find an explicitly NAK'd packet we resend immediately; |
| 497 | * packets that lie beyond the end of the ACK list are scheduled for resend by |
| 498 | * the timer on the basis that the peer might just not have processed them at |
| 499 | * the time the ACK was sent. |
| 500 | */ |
| 501 | static void rxrpc_input_soft_acks(struct rxrpc_call *call, u8 *acks, |
| 502 | rxrpc_seq_t seq, int nr_acks) |
| 503 | { |
| 504 | bool resend = false; |
| 505 | int ix; |
David Howells | f07373e | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 506 | u8 annotation, anno_type; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 507 | |
| 508 | for (; nr_acks > 0; nr_acks--, seq++) { |
| 509 | ix = seq & RXRPC_RXTX_BUFF_MASK; |
David Howells | f07373e | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 510 | annotation = call->rxtx_annotations[ix]; |
| 511 | anno_type = annotation & RXRPC_TX_ANNO_MASK; |
| 512 | annotation &= ~RXRPC_TX_ANNO_MASK; |
David Howells | d01dc4c | 2016-09-17 10:49:12 +0100 | [diff] [blame] | 513 | switch (*acks++) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 514 | case RXRPC_ACK_TYPE_ACK: |
David Howells | f07373e | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 515 | if (anno_type == RXRPC_TX_ANNO_ACK) |
| 516 | continue; |
| 517 | call->rxtx_annotations[ix] = |
| 518 | RXRPC_TX_ANNO_ACK | annotation; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 519 | break; |
| 520 | case RXRPC_ACK_TYPE_NACK: |
David Howells | f07373e | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 521 | if (anno_type == RXRPC_TX_ANNO_NAK) |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 522 | continue; |
David Howells | be8aa33 | 2016-09-23 12:39:23 +0100 | [diff] [blame] | 523 | if (anno_type == RXRPC_TX_ANNO_RETRANS) |
| 524 | continue; |
David Howells | f07373e | 2016-09-22 00:29:32 +0100 | [diff] [blame] | 525 | call->rxtx_annotations[ix] = |
| 526 | RXRPC_TX_ANNO_NAK | annotation; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 527 | resend = true; |
| 528 | break; |
| 529 | default: |
| 530 | return rxrpc_proto_abort("SFT", call, 0); |
| 531 | } |
| 532 | } |
| 533 | |
| 534 | if (resend && |
| 535 | !test_and_set_bit(RXRPC_CALL_EV_RESEND, &call->events)) |
| 536 | rxrpc_queue_call(call); |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Process an ACK packet. |
| 541 | * |
| 542 | * ack.firstPacket is the sequence number of the first soft-ACK'd/NAK'd packet |
| 543 | * in the ACK array. Anything before that is hard-ACK'd and may be discarded. |
| 544 | * |
| 545 | * A hard-ACK means that a packet has been processed and may be discarded; a |
| 546 | * soft-ACK means that the packet may be discarded and retransmission |
| 547 | * requested. A phase is complete when all packets are hard-ACK'd. |
| 548 | */ |
| 549 | static void rxrpc_input_ack(struct rxrpc_call *call, struct sk_buff *skb, |
| 550 | u16 skew) |
| 551 | { |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 552 | u8 ack_reason; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 553 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 554 | union { |
| 555 | struct rxrpc_ackpacket ack; |
| 556 | struct rxrpc_ackinfo info; |
| 557 | u8 acks[RXRPC_MAXACKS]; |
| 558 | } buf; |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 559 | rxrpc_serial_t acked_serial; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 560 | rxrpc_seq_t first_soft_ack, hard_ack; |
| 561 | int nr_acks, offset; |
| 562 | |
| 563 | _enter(""); |
| 564 | |
| 565 | if (skb_copy_bits(skb, sp->offset, &buf.ack, sizeof(buf.ack)) < 0) { |
| 566 | _debug("extraction failure"); |
| 567 | return rxrpc_proto_abort("XAK", call, 0); |
| 568 | } |
| 569 | sp->offset += sizeof(buf.ack); |
| 570 | |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 571 | acked_serial = ntohl(buf.ack.serial); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 572 | first_soft_ack = ntohl(buf.ack.firstPacket); |
| 573 | hard_ack = first_soft_ack - 1; |
| 574 | nr_acks = buf.ack.nAcks; |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 575 | ack_reason = (buf.ack.reason < RXRPC_ACK__INVALID ? |
| 576 | buf.ack.reason : RXRPC_ACK__INVALID); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 577 | |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 578 | trace_rxrpc_rx_ack(call, first_soft_ack, ack_reason, nr_acks); |
David Howells | ec71eb9 | 2016-09-17 10:49:13 +0100 | [diff] [blame] | 579 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 580 | _proto("Rx ACK %%%u { m=%hu f=#%u p=#%u s=%%%u r=%s n=%u }", |
| 581 | sp->hdr.serial, |
| 582 | ntohs(buf.ack.maxSkew), |
| 583 | first_soft_ack, |
| 584 | ntohl(buf.ack.previousPacket), |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 585 | acked_serial, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 586 | rxrpc_ack_names[ack_reason], |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 587 | buf.ack.nAcks); |
| 588 | |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 589 | if (buf.ack.reason == RXRPC_ACK_PING_RESPONSE) |
| 590 | rxrpc_input_ping_response(call, skb->tstamp, acked_serial, |
| 591 | sp->hdr.serial); |
David Howells | 50235c4 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 592 | if (buf.ack.reason == RXRPC_ACK_REQUESTED) |
| 593 | rxrpc_input_requested_ack(call, skb->tstamp, acked_serial, |
| 594 | sp->hdr.serial); |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 595 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 596 | if (buf.ack.reason == RXRPC_ACK_PING) { |
| 597 | _proto("Rx ACK %%%u PING Request", sp->hdr.serial); |
| 598 | rxrpc_propose_ACK(call, RXRPC_ACK_PING_RESPONSE, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 599 | skew, sp->hdr.serial, true, true, |
| 600 | rxrpc_propose_ack_respond_to_ping); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 601 | } else if (sp->hdr.flags & RXRPC_REQUEST_ACK) { |
| 602 | rxrpc_propose_ACK(call, RXRPC_ACK_REQUESTED, |
David Howells | 9c7ad43 | 2016-09-23 13:50:40 +0100 | [diff] [blame] | 603 | skew, sp->hdr.serial, true, true, |
| 604 | rxrpc_propose_ack_respond_to_ack); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 605 | } |
| 606 | |
| 607 | offset = sp->offset + nr_acks + 3; |
David Howells | 89a80ed | 2016-09-13 22:36:22 +0100 | [diff] [blame] | 608 | if (skb->len >= offset + sizeof(buf.info)) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 609 | if (skb_copy_bits(skb, offset, &buf.info, sizeof(buf.info)) < 0) |
| 610 | return rxrpc_proto_abort("XAI", call, 0); |
| 611 | rxrpc_input_ackinfo(call, skb, &buf.info); |
| 612 | } |
| 613 | |
| 614 | if (first_soft_ack == 0) |
| 615 | return rxrpc_proto_abort("AK0", call, 0); |
| 616 | |
| 617 | /* Ignore ACKs unless we are or have just been transmitting. */ |
| 618 | switch (call->state) { |
| 619 | case RXRPC_CALL_CLIENT_SEND_REQUEST: |
| 620 | case RXRPC_CALL_CLIENT_AWAIT_REPLY: |
| 621 | case RXRPC_CALL_SERVER_SEND_REPLY: |
| 622 | case RXRPC_CALL_SERVER_AWAIT_ACK: |
| 623 | break; |
| 624 | default: |
| 625 | return; |
| 626 | } |
| 627 | |
| 628 | /* Discard any out-of-order or duplicate ACKs. */ |
David Howells | 98dafac | 2016-09-23 14:04:38 +0100 | [diff] [blame] | 629 | if (before_eq(sp->hdr.serial, call->acks_latest)) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 630 | _debug("discard ACK %d <= %d", |
| 631 | sp->hdr.serial, call->acks_latest); |
| 632 | return; |
| 633 | } |
| 634 | call->acks_latest = sp->hdr.serial; |
| 635 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 636 | if (before(hard_ack, call->tx_hard_ack) || |
| 637 | after(hard_ack, call->tx_top)) |
| 638 | return rxrpc_proto_abort("AKW", call, 0); |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 639 | if (nr_acks > call->tx_top - hard_ack) |
| 640 | return rxrpc_proto_abort("AKN", call, 0); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 641 | |
| 642 | if (after(hard_ack, call->tx_hard_ack)) |
| 643 | rxrpc_rotate_tx_window(call, hard_ack); |
| 644 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 645 | if (nr_acks > 0) { |
| 646 | if (skb_copy_bits(skb, sp->offset, buf.acks, nr_acks) < 0) |
| 647 | return rxrpc_proto_abort("XSA", call, 0); |
| 648 | rxrpc_input_soft_acks(call, buf.acks, first_soft_ack, nr_acks); |
| 649 | } |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 650 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 651 | if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) { |
| 652 | rxrpc_end_tx_phase(call, false, "ETA"); |
| 653 | return; |
| 654 | } |
| 655 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | /* |
| 659 | * Process an ACKALL packet. |
| 660 | */ |
| 661 | static void rxrpc_input_ackall(struct rxrpc_call *call, struct sk_buff *skb) |
| 662 | { |
| 663 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
| 664 | |
| 665 | _proto("Rx ACKALL %%%u", sp->hdr.serial); |
| 666 | |
David Howells | 70790db | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 667 | rxrpc_rotate_tx_window(call, call->tx_top); |
| 668 | if (test_bit(RXRPC_CALL_TX_LAST, &call->flags)) |
| 669 | rxrpc_end_tx_phase(call, false, "ETL"); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | /* |
| 673 | * Process an ABORT packet. |
| 674 | */ |
| 675 | static void rxrpc_input_abort(struct rxrpc_call *call, struct sk_buff *skb) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 676 | { |
| 677 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 678 | __be32 wtmp; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 679 | u32 abort_code = RX_CALL_DEAD; |
| 680 | |
| 681 | _enter(""); |
| 682 | |
| 683 | if (skb->len >= 4 && |
| 684 | skb_copy_bits(skb, sp->offset, &wtmp, sizeof(wtmp)) >= 0) |
| 685 | abort_code = ntohl(wtmp); |
| 686 | |
| 687 | _proto("Rx ABORT %%%u { %x }", sp->hdr.serial, abort_code); |
| 688 | |
| 689 | if (rxrpc_set_call_completion(call, RXRPC_CALL_REMOTELY_ABORTED, |
| 690 | abort_code, ECONNABORTED)) |
| 691 | rxrpc_notify_socket(call); |
| 692 | } |
| 693 | |
| 694 | /* |
| 695 | * Process an incoming call packet. |
| 696 | */ |
| 697 | static void rxrpc_input_call_packet(struct rxrpc_call *call, |
| 698 | struct sk_buff *skb, u16 skew) |
| 699 | { |
| 700 | struct rxrpc_skb_priv *sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 701 | |
| 702 | _enter("%p,%p", call, skb); |
| 703 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 704 | switch (sp->hdr.type) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 705 | case RXRPC_PACKET_TYPE_DATA: |
| 706 | rxrpc_input_data(call, skb, skew); |
| 707 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 708 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 709 | case RXRPC_PACKET_TYPE_ACK: |
| 710 | rxrpc_input_ack(call, skb, skew); |
| 711 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 712 | |
| 713 | case RXRPC_PACKET_TYPE_BUSY: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 714 | _proto("Rx BUSY %%%u", sp->hdr.serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 715 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 716 | /* Just ignore BUSY packets from the server; the retry and |
| 717 | * lifespan timers will take care of business. BUSY packets |
| 718 | * from the client don't make sense. |
| 719 | */ |
| 720 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 721 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 722 | case RXRPC_PACKET_TYPE_ABORT: |
| 723 | rxrpc_input_abort(call, skb); |
| 724 | break; |
| 725 | |
| 726 | case RXRPC_PACKET_TYPE_ACKALL: |
| 727 | rxrpc_input_ackall(call, skb); |
| 728 | break; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 729 | |
| 730 | default: |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 731 | _proto("Rx %s %%%u", rxrpc_pkts[sp->hdr.type], sp->hdr.serial); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 732 | break; |
| 733 | } |
| 734 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 735 | _leave(""); |
| 736 | } |
| 737 | |
| 738 | /* |
| 739 | * post connection-level events to the connection |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 740 | * - this includes challenges, responses, some aborts and call terminal packet |
| 741 | * retransmission. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 742 | */ |
David Howells | 2e7e975 | 2016-08-09 10:11:48 +0100 | [diff] [blame] | 743 | static void rxrpc_post_packet_to_conn(struct rxrpc_connection *conn, |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 744 | struct sk_buff *skb) |
| 745 | { |
| 746 | _enter("%p,%p", conn, skb); |
| 747 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 748 | skb_queue_tail(&conn->rx_queue, skb); |
David Howells | 2e7e975 | 2016-08-09 10:11:48 +0100 | [diff] [blame] | 749 | rxrpc_queue_conn(conn); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 750 | } |
| 751 | |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 752 | /* |
| 753 | * post endpoint-level events to the local endpoint |
| 754 | * - this includes debug and version messages |
| 755 | */ |
| 756 | static void rxrpc_post_packet_to_local(struct rxrpc_local *local, |
| 757 | struct sk_buff *skb) |
| 758 | { |
| 759 | _enter("%p,%p", local, skb); |
| 760 | |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 761 | skb_queue_tail(&local->event_queue, skb); |
David Howells | 5acbee4 | 2016-06-27 10:32:02 +0100 | [diff] [blame] | 762 | rxrpc_queue_local(local); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 763 | } |
| 764 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 765 | /* |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 766 | * put a packet up for transport-level abort |
| 767 | */ |
| 768 | static void rxrpc_reject_packet(struct rxrpc_local *local, struct sk_buff *skb) |
| 769 | { |
| 770 | CHECK_SLAB_OKAY(&local->usage); |
| 771 | |
| 772 | skb_queue_tail(&local->reject_queue, skb); |
| 773 | rxrpc_queue_local(local); |
| 774 | } |
| 775 | |
| 776 | /* |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 777 | * Extract the wire header from a packet and translate the byte order. |
| 778 | */ |
| 779 | static noinline |
| 780 | int rxrpc_extract_header(struct rxrpc_skb_priv *sp, struct sk_buff *skb) |
| 781 | { |
| 782 | struct rxrpc_wire_header whdr; |
| 783 | |
| 784 | /* dig out the RxRPC connection details */ |
Willem de Bruijn | 4d0fc73 | 2016-04-07 11:44:59 -0400 | [diff] [blame] | 785 | if (skb_copy_bits(skb, 0, &whdr, sizeof(whdr)) < 0) |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 786 | return -EBADMSG; |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 787 | |
| 788 | memset(sp, 0, sizeof(*sp)); |
| 789 | sp->hdr.epoch = ntohl(whdr.epoch); |
| 790 | sp->hdr.cid = ntohl(whdr.cid); |
| 791 | sp->hdr.callNumber = ntohl(whdr.callNumber); |
| 792 | sp->hdr.seq = ntohl(whdr.seq); |
| 793 | sp->hdr.serial = ntohl(whdr.serial); |
| 794 | sp->hdr.flags = whdr.flags; |
| 795 | sp->hdr.type = whdr.type; |
| 796 | sp->hdr.userStatus = whdr.userStatus; |
| 797 | sp->hdr.securityIndex = whdr.securityIndex; |
| 798 | sp->hdr._rsvd = ntohs(whdr._rsvd); |
| 799 | sp->hdr.serviceId = ntohs(whdr.serviceId); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 800 | sp->offset = sizeof(whdr); |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 801 | return 0; |
| 802 | } |
| 803 | |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 804 | /* |
| 805 | * handle data received on the local endpoint |
| 806 | * - may be called in interrupt context |
David Howells | 4f95dd7 | 2016-04-04 14:00:35 +0100 | [diff] [blame] | 807 | * |
| 808 | * The socket is locked by the caller and this prevents the socket from being |
| 809 | * shut down and the local endpoint from going away, thus sk_user_data will not |
| 810 | * be cleared until this function returns. |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 811 | */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 812 | void rxrpc_data_ready(struct sock *udp_sk) |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 813 | { |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 814 | struct rxrpc_connection *conn; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 815 | struct rxrpc_channel *chan; |
| 816 | struct rxrpc_call *call; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 817 | struct rxrpc_skb_priv *sp; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 818 | struct rxrpc_local *local = udp_sk->sk_user_data; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 819 | struct sk_buff *skb; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 820 | unsigned int channel; |
David Howells | 563ea7d | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 821 | int ret, skew; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 822 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 823 | _enter("%p", udp_sk); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 824 | |
| 825 | ASSERT(!irqs_disabled()); |
| 826 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 827 | skb = skb_recv_datagram(udp_sk, 0, 1, &ret); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 828 | if (!skb) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 829 | if (ret == -EAGAIN) |
| 830 | return; |
| 831 | _debug("UDP socket error %d", ret); |
| 832 | return; |
| 833 | } |
| 834 | |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 835 | rxrpc_new_skb(skb, rxrpc_skb_rx_received); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 836 | |
| 837 | _net("recv skb %p", skb); |
| 838 | |
| 839 | /* we'll probably need to checksum it (didn't call sock_recvmsg) */ |
| 840 | if (skb_checksum_complete(skb)) { |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 841 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 842 | __UDP_INC_STATS(&init_net, UDP_MIB_INERRORS, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 843 | _leave(" [CSUM failed]"); |
| 844 | return; |
| 845 | } |
| 846 | |
Eric Dumazet | 02c2234 | 2016-04-27 16:44:30 -0700 | [diff] [blame] | 847 | __UDP_INC_STATS(&init_net, UDP_MIB_INDATAGRAMS, 0); |
Herbert Xu | 1781f7f | 2007-12-11 11:30:32 -0800 | [diff] [blame] | 848 | |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 849 | /* The socket buffer we have is owned by UDP, with UDP's data all over |
| 850 | * it, but we really want our own data there. |
| 851 | */ |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 852 | skb_orphan(skb); |
| 853 | sp = rxrpc_skb(skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 854 | |
David Howells | 89b475a | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 855 | /* dig out the RxRPC connection details */ |
| 856 | if (rxrpc_extract_header(sp, skb) < 0) |
| 857 | goto bad_message; |
| 858 | |
David Howells | 8a681c36 | 2016-09-17 10:49:15 +0100 | [diff] [blame] | 859 | if (IS_ENABLED(CONFIG_AF_RXRPC_INJECT_LOSS)) { |
| 860 | static int lose; |
| 861 | if ((lose++ & 7) == 7) { |
David Howells | 89b475a | 2016-09-23 12:39:22 +0100 | [diff] [blame] | 862 | trace_rxrpc_rx_lose(sp); |
David Howells | 8a681c36 | 2016-09-17 10:49:15 +0100 | [diff] [blame] | 863 | rxrpc_lose_skb(skb, rxrpc_skb_rx_lost); |
| 864 | return; |
| 865 | } |
| 866 | } |
| 867 | |
David Howells | 49e19ec | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 868 | trace_rxrpc_rx_packet(sp); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 869 | |
| 870 | _net("Rx RxRPC %s ep=%x call=%x:%x", |
| 871 | sp->hdr.flags & RXRPC_CLIENT_INITIATED ? "ToServer" : "ToClient", |
David Howells | 0d12f8a | 2016-03-04 15:53:46 +0000 | [diff] [blame] | 872 | sp->hdr.epoch, sp->hdr.cid, sp->hdr.callNumber); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 873 | |
David Howells | 351c1e6 | 2016-03-04 15:56:06 +0000 | [diff] [blame] | 874 | if (sp->hdr.type >= RXRPC_N_PACKET_TYPES || |
| 875 | !((RXRPC_SUPPORTED_PACKET_TYPES >> sp->hdr.type) & 1)) { |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 876 | _proto("Rx Bad Packet Type %u", sp->hdr.type); |
| 877 | goto bad_message; |
| 878 | } |
| 879 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 880 | switch (sp->hdr.type) { |
| 881 | case RXRPC_PACKET_TYPE_VERSION: |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 882 | rxrpc_post_packet_to_local(local, skb); |
| 883 | goto out; |
David Howells | bc6e1ea | 2016-06-10 22:30:27 +0100 | [diff] [blame] | 884 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 885 | case RXRPC_PACKET_TYPE_BUSY: |
| 886 | if (sp->hdr.flags & RXRPC_CLIENT_INITIATED) |
| 887 | goto discard; |
| 888 | |
| 889 | case RXRPC_PACKET_TYPE_DATA: |
| 890 | if (sp->hdr.callNumber == 0) |
| 891 | goto bad_message; |
| 892 | if (sp->hdr.flags & RXRPC_JUMBO_PACKET && |
| 893 | !rxrpc_validate_jumbo(skb)) |
| 894 | goto bad_message; |
| 895 | break; |
| 896 | } |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 897 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 898 | rcu_read_lock(); |
| 899 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 900 | conn = rxrpc_find_connection_rcu(local, skb); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 901 | if (conn) { |
| 902 | if (sp->hdr.securityIndex != conn->security_ix) |
| 903 | goto wrong_security; |
David Howells | 563ea7d | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 904 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 905 | if (sp->hdr.callNumber == 0) { |
| 906 | /* Connection-level packet */ |
| 907 | _debug("CONN %p {%d}", conn, conn->debug_id); |
| 908 | rxrpc_post_packet_to_conn(conn, skb); |
| 909 | goto out_unlock; |
| 910 | } |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 911 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 912 | /* Note the serial number skew here */ |
| 913 | skew = (int)sp->hdr.serial - (int)conn->hi_serial; |
| 914 | if (skew >= 0) { |
| 915 | if (skew > 0) |
| 916 | conn->hi_serial = sp->hdr.serial; |
| 917 | } else { |
| 918 | skew = -skew; |
| 919 | skew = min(skew, 65535); |
| 920 | } |
| 921 | |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 922 | /* Call-bound packets are routed by connection channel. */ |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 923 | channel = sp->hdr.cid & RXRPC_CHANNELMASK; |
| 924 | chan = &conn->channels[channel]; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 925 | |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 926 | /* Ignore really old calls */ |
| 927 | if (sp->hdr.callNumber < chan->last_call) |
| 928 | goto discard_unlock; |
| 929 | |
| 930 | if (sp->hdr.callNumber == chan->last_call) { |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 931 | /* For the previous service call, if completed successfully, we |
| 932 | * discard all further packets. |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 933 | */ |
David Howells | 2266ffd | 2016-08-24 13:06:14 +0100 | [diff] [blame] | 934 | if (rxrpc_conn_is_service(conn) && |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 935 | (chan->last_type == RXRPC_PACKET_TYPE_ACK || |
| 936 | sp->hdr.type == RXRPC_PACKET_TYPE_ABORT)) |
| 937 | goto discard_unlock; |
| 938 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 939 | /* But otherwise we need to retransmit the final packet from |
| 940 | * data cached in the connection record. |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 941 | */ |
| 942 | rxrpc_post_packet_to_conn(conn, skb); |
| 943 | goto out_unlock; |
| 944 | } |
| 945 | |
| 946 | call = rcu_dereference(chan->call); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 947 | } else { |
| 948 | skew = 0; |
| 949 | call = NULL; |
Tim Smith | 7727640 | 2014-03-03 23:04:45 +0000 | [diff] [blame] | 950 | } |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 951 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 952 | if (!call || atomic_read(&call->usage) == 0) { |
| 953 | if (!(sp->hdr.type & RXRPC_CLIENT_INITIATED) || |
| 954 | sp->hdr.callNumber == 0 || |
| 955 | sp->hdr.type != RXRPC_PACKET_TYPE_DATA) |
| 956 | goto bad_message_unlock; |
| 957 | if (sp->hdr.seq != 1) |
| 958 | goto discard_unlock; |
| 959 | call = rxrpc_new_incoming_call(local, conn, skb); |
| 960 | if (!call) { |
| 961 | rcu_read_unlock(); |
| 962 | goto reject_packet; |
| 963 | } |
David Howells | 8e83134 | 2016-09-22 00:29:31 +0100 | [diff] [blame] | 964 | rxrpc_send_ping(call, skb, skew); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | rxrpc_input_call_packet(call, skb, skew); |
| 968 | goto discard_unlock; |
| 969 | |
David Howells | 18bfeba | 2016-08-23 15:27:25 +0100 | [diff] [blame] | 970 | discard_unlock: |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 971 | rcu_read_unlock(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 972 | discard: |
David Howells | 71f3ca4 | 2016-09-17 10:49:14 +0100 | [diff] [blame] | 973 | rxrpc_free_skb(skb, rxrpc_skb_rx_freed); |
David Howells | 44ba069 | 2015-04-01 16:31:26 +0100 | [diff] [blame] | 974 | out: |
David Howells | 49e19ec | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 975 | trace_rxrpc_rx_done(0, 0); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 976 | return; |
| 977 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 978 | out_unlock: |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 979 | rcu_read_unlock(); |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 980 | goto out; |
David Howells | 8496af5 | 2016-07-01 07:51:50 +0100 | [diff] [blame] | 981 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 982 | wrong_security: |
| 983 | rcu_read_unlock(); |
| 984 | trace_rxrpc_abort("SEC", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 985 | RXKADINCONSISTENCY, EBADMSG); |
| 986 | skb->priority = RXKADINCONSISTENCY; |
| 987 | goto post_abort; |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 988 | |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 989 | bad_message_unlock: |
| 990 | rcu_read_unlock(); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 991 | bad_message: |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 992 | trace_rxrpc_abort("BAD", sp->hdr.cid, sp->hdr.callNumber, sp->hdr.seq, |
| 993 | RX_PROTOCOL_ERROR, EBADMSG); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 994 | skb->priority = RX_PROTOCOL_ERROR; |
David Howells | 248f219 | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 995 | post_abort: |
| 996 | skb->mark = RXRPC_SKB_MARK_LOCAL_ABORT; |
David Howells | 49e19ec | 2016-09-08 11:10:12 +0100 | [diff] [blame] | 997 | reject_packet: |
| 998 | trace_rxrpc_rx_done(skb->mark, skb->priority); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 999 | rxrpc_reject_packet(local, skb); |
David Howells | 17926a7 | 2007-04-26 15:48:28 -0700 | [diff] [blame] | 1000 | _leave(" [badmsg]"); |
| 1001 | } |