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