The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994 |
| 3 | * The Regents of the University of California. All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions |
| 7 | * are met: |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer in the |
| 12 | * documentation and/or other materials provided with the distribution. |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 13 | * 3. Neither the name of the University nor the names of its contributors |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 14 | * may be used to endorse or promote products derived from this software |
| 15 | * without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 | * SUCH DAMAGE. |
| 28 | * |
| 29 | * @(#)tcp_input.c 8.5 (Berkeley) 4/10/94 |
| 30 | * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp |
| 31 | */ |
| 32 | |
| 33 | /* |
| 34 | * Changes and additions relating to SLiRP |
| 35 | * Copyright (c) 1995 Danny Gasparovski. |
| 36 | * |
| 37 | * Please read the file COPYRIGHT for the |
| 38 | * terms and conditions of the copyright. |
| 39 | */ |
| 40 | |
| 41 | #include <slirp.h> |
| 42 | #include "ip_icmp.h" |
| 43 | |
| 44 | struct socket tcb; |
| 45 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 46 | #define TCPREXMTTHRESH 3 |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 47 | struct socket *tcp_last_so = &tcb; |
| 48 | |
| 49 | tcp_seq tcp_iss; /* tcp initial send seq # */ |
| 50 | |
| 51 | #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ) |
| 52 | |
| 53 | /* for modulo comparisons of timestamps */ |
| 54 | #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0) |
| 55 | #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0) |
| 56 | |
| 57 | /* |
| 58 | * Insert segment ti into reassembly queue of tcp with |
| 59 | * control block tp. Return TH_FIN if reassembly now includes |
| 60 | * a segment with FIN. The macro form does the common case inline |
| 61 | * (segment is the next to be received on an established connection, |
| 62 | * and the queue is empty), avoiding linkage into and removal |
| 63 | * from the queue and repetition of various conversions. |
| 64 | * Set DELACK for segments received in order, but ack immediately |
| 65 | * when segments are out of order (so fast retransmit can work). |
| 66 | */ |
| 67 | #ifdef TCP_ACK_HACK |
| 68 | #define TCP_REASS(tp, ti, m, so, flags) {\ |
| 69 | if ((ti)->ti_seq == (tp)->rcv_nxt && \ |
| 70 | tcpfrag_list_empty(tp) && \ |
| 71 | (tp)->t_state == TCPS_ESTABLISHED) {\ |
| 72 | if (ti->ti_flags & TH_PUSH) \ |
| 73 | tp->t_flags |= TF_ACKNOW; \ |
| 74 | else \ |
| 75 | tp->t_flags |= TF_DELACK; \ |
| 76 | (tp)->rcv_nxt += (ti)->ti_len; \ |
| 77 | flags = (ti)->ti_flags & TH_FIN; \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 78 | STAT(tcpstat.tcps_rcvpack++); \ |
| 79 | STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 80 | if (so->so_emu) { \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 81 | if (tcp_emu((so),(m))) sbappend((so), (m)); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 82 | } else \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 83 | sbappend((so), (m)); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 84 | /* sorwakeup(so); */ \ |
| 85 | } else {\ |
| 86 | (flags) = tcp_reass((tp), (ti), (m)); \ |
| 87 | tp->t_flags |= TF_ACKNOW; \ |
| 88 | } \ |
| 89 | } |
| 90 | #else |
| 91 | #define TCP_REASS(tp, ti, m, so, flags) { \ |
| 92 | if ((ti)->ti_seq == (tp)->rcv_nxt && \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 93 | tcpfrag_list_empty(tp) && \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 94 | (tp)->t_state == TCPS_ESTABLISHED) { \ |
| 95 | tp->t_flags |= TF_DELACK; \ |
| 96 | (tp)->rcv_nxt += (ti)->ti_len; \ |
| 97 | flags = (ti)->ti_flags & TH_FIN; \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 98 | STAT(tcpstat.tcps_rcvpack++); \ |
| 99 | STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 100 | if (so->so_emu) { \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 101 | if (tcp_emu((so),(m))) sbappend(so, (m)); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 102 | } else \ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 103 | sbappend((so), (m)); \ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 104 | /* sorwakeup(so); */ \ |
| 105 | } else { \ |
| 106 | (flags) = tcp_reass((tp), (ti), (m)); \ |
| 107 | tp->t_flags |= TF_ACKNOW; \ |
| 108 | } \ |
| 109 | } |
| 110 | #endif |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 111 | static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, |
| 112 | struct tcpiphdr *ti); |
| 113 | static void tcp_xmit_timer(register struct tcpcb *tp, int rtt); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 114 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 115 | static int |
| 116 | tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti, |
| 117 | struct mbuf *m) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 118 | { |
| 119 | register struct tcpiphdr *q; |
| 120 | struct socket *so = tp->t_socket; |
| 121 | int flags; |
| 122 | |
| 123 | /* |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 124 | * Call with ti==NULL after become established to |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 125 | * force pre-ESTABLISHED data up to user socket. |
| 126 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 127 | if (ti == NULL) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 128 | goto present; |
| 129 | |
| 130 | /* |
| 131 | * Find a segment which begins after this one does. |
| 132 | */ |
| 133 | for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 134 | q = tcpiphdr_next(q)) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 135 | if (SEQ_GT(q->ti_seq, ti->ti_seq)) |
| 136 | break; |
| 137 | |
| 138 | /* |
| 139 | * If there is a preceding segment, it may provide some of |
| 140 | * our data already. If so, drop the data from the incoming |
| 141 | * segment. If it provides all of our data, drop us. |
| 142 | */ |
| 143 | if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) { |
| 144 | register int i; |
| 145 | q = tcpiphdr_prev(q); |
| 146 | /* conversion to int (in i) handles seq wraparound */ |
| 147 | i = q->ti_seq + q->ti_len - ti->ti_seq; |
| 148 | if (i > 0) { |
| 149 | if (i >= ti->ti_len) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 150 | STAT(tcpstat.tcps_rcvduppack++); |
| 151 | STAT(tcpstat.tcps_rcvdupbyte += ti->ti_len); |
| 152 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 153 | /* |
| 154 | * Try to present any queued data |
| 155 | * at the left window edge to the user. |
| 156 | * This is needed after the 3-WHS |
| 157 | * completes. |
| 158 | */ |
| 159 | goto present; /* ??? */ |
| 160 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 161 | m_adj(m, i); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 162 | ti->ti_len -= i; |
| 163 | ti->ti_seq += i; |
| 164 | } |
| 165 | q = tcpiphdr_next(q); |
| 166 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 167 | STAT(tcpstat.tcps_rcvoopack++); |
| 168 | STAT(tcpstat.tcps_rcvoobyte += ti->ti_len); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 169 | ti->ti_mbuf = m; |
| 170 | |
| 171 | /* |
| 172 | * While we overlap succeeding segments trim them or, |
| 173 | * if they are completely covered, dequeue them. |
| 174 | */ |
| 175 | while (!tcpfrag_list_end(q, tp)) { |
| 176 | register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; |
| 177 | if (i <= 0) |
| 178 | break; |
| 179 | if (i < q->ti_len) { |
| 180 | q->ti_seq += i; |
| 181 | q->ti_len -= i; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 182 | m_adj(q->ti_mbuf, i); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 183 | break; |
| 184 | } |
| 185 | q = tcpiphdr_next(q); |
| 186 | m = tcpiphdr_prev(q)->ti_mbuf; |
| 187 | remque(tcpiphdr2qlink(tcpiphdr_prev(q))); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 188 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * Stick new segment in its place. |
| 193 | */ |
| 194 | insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q))); |
| 195 | |
| 196 | present: |
| 197 | /* |
| 198 | * Present data to user, advancing rcv_nxt through |
| 199 | * completed sequence space. |
| 200 | */ |
| 201 | if (!TCPS_HAVEESTABLISHED(tp->t_state)) |
| 202 | return (0); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 203 | ti = tcpfrag_list_first(tp); |
| 204 | if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 205 | return (0); |
| 206 | if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) |
| 207 | return (0); |
| 208 | do { |
| 209 | tp->rcv_nxt += ti->ti_len; |
| 210 | flags = ti->ti_flags & TH_FIN; |
| 211 | remque(tcpiphdr2qlink(ti)); |
| 212 | m = ti->ti_mbuf; |
| 213 | ti = tcpiphdr_next(ti); |
| 214 | /* if (so->so_state & SS_FCANTRCVMORE) */ |
| 215 | if (so->so_state & SS_FCANTSENDMORE) |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 216 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 217 | else { |
| 218 | if (so->so_emu) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 219 | if (tcp_emu(so,m)) sbappend(so, m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 220 | } else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 221 | sbappend(so, m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 222 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 223 | } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 224 | /* sorwakeup(so); */ |
| 225 | return (flags); |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * TCP input routine, follows pages 65-76 of the |
| 230 | * protocol specification dated September, 1981 very closely. |
| 231 | */ |
| 232 | void |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 233 | tcp_input(struct mbuf *m, int iphlen, struct socket *inso) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 234 | { |
| 235 | struct ip save_ip, *ip; |
| 236 | register struct tcpiphdr *ti; |
| 237 | caddr_t optp = NULL; |
| 238 | int optlen = 0; |
| 239 | int len, tlen, off; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 240 | register struct tcpcb *tp = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 241 | register int tiflags; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 242 | struct socket *so = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 243 | int todrop, acked, ourfinisacked, needoutput = 0; |
| 244 | /* int dropsocket = 0; */ |
| 245 | int iss = 0; |
| 246 | u_long tiwin; |
| 247 | int ret; |
| 248 | /* int ts_present = 0; */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 249 | struct ex_list *ex_ptr; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 250 | |
| 251 | DEBUG_CALL("tcp_input"); |
| 252 | DEBUG_ARGS((dfd," m = %8lx iphlen = %2d inso = %lx\n", |
| 253 | (long )m, iphlen, (long )inso )); |
| 254 | |
| 255 | /* |
| 256 | * If called with m == 0, then we're continuing the connect |
| 257 | */ |
| 258 | if (m == NULL) { |
| 259 | so = inso; |
| 260 | |
| 261 | /* Re-set a few variables */ |
| 262 | tp = sototcpcb(so); |
| 263 | m = so->so_m; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 264 | so->so_m = NULL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 265 | ti = so->so_ti; |
| 266 | tiwin = ti->ti_win; |
| 267 | tiflags = ti->ti_flags; |
| 268 | |
| 269 | goto cont_conn; |
| 270 | } |
| 271 | |
| 272 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 273 | STAT(tcpstat.tcps_rcvtotal++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 274 | /* |
| 275 | * Get IP and TCP header together in first mbuf. |
| 276 | * Note: IP leaves IP header in first mbuf. |
| 277 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 278 | ti = mtod(m, struct tcpiphdr *); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 279 | if (iphlen > sizeof(struct ip )) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 280 | ip_stripoptions(m, (struct mbuf *)0); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 281 | iphlen=sizeof(struct ip ); |
| 282 | } |
| 283 | /* XXX Check if too short */ |
| 284 | |
| 285 | |
| 286 | /* |
| 287 | * Save a copy of the IP header in case we want restore it |
| 288 | * for sending an ICMP error message in response. |
| 289 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 290 | ip=mtod(m, struct ip *); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 291 | save_ip = *ip; |
| 292 | save_ip.ip_len+= iphlen; |
| 293 | |
| 294 | /* |
| 295 | * Checksum extended TCP header and data. |
| 296 | */ |
| 297 | tlen = ((struct ip *)ti)->ip_len; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 298 | tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL; |
| 299 | memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr)); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 300 | ti->ti_x1 = 0; |
| 301 | ti->ti_len = htons((u_int16_t)tlen); |
| 302 | len = sizeof(struct ip ) + tlen; |
| 303 | /* keep checksum for ICMP reply |
| 304 | * ti->ti_sum = cksum(m, len); |
| 305 | * if (ti->ti_sum) { */ |
| 306 | if(cksum(m, len)) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 307 | STAT(tcpstat.tcps_rcvbadsum++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 308 | goto drop; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Check that TCP offset makes sense, |
| 313 | * pull out TCP options and adjust length. XXX |
| 314 | */ |
| 315 | off = ti->ti_off << 2; |
| 316 | if (off < sizeof (struct tcphdr) || off > tlen) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 317 | STAT(tcpstat.tcps_rcvbadoff++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 318 | goto drop; |
| 319 | } |
| 320 | tlen -= off; |
| 321 | ti->ti_len = tlen; |
| 322 | if (off > sizeof (struct tcphdr)) { |
| 323 | optlen = off - sizeof (struct tcphdr); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 324 | optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 325 | |
| 326 | /* |
| 327 | * Do quick retrieval of timestamp options ("options |
| 328 | * prediction?"). If timestamp is the only option and it's |
| 329 | * formatted as recommended in RFC 1323 appendix A, we |
| 330 | * quickly get the values now and not bother calling |
| 331 | * tcp_dooptions(), etc. |
| 332 | */ |
| 333 | /* if ((optlen == TCPOLEN_TSTAMP_APPA || |
| 334 | * (optlen > TCPOLEN_TSTAMP_APPA && |
| 335 | * optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && |
| 336 | * *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) && |
| 337 | * (ti->ti_flags & TH_SYN) == 0) { |
| 338 | * ts_present = 1; |
| 339 | * ts_val = ntohl(*(u_int32_t *)(optp + 4)); |
| 340 | * ts_ecr = ntohl(*(u_int32_t *)(optp + 8)); |
| 341 | * optp = NULL; / * we've parsed the options * / |
| 342 | * } |
| 343 | */ |
| 344 | } |
| 345 | tiflags = ti->ti_flags; |
| 346 | |
| 347 | /* |
| 348 | * Convert TCP protocol specific fields to host format. |
| 349 | */ |
| 350 | NTOHL(ti->ti_seq); |
| 351 | NTOHL(ti->ti_ack); |
| 352 | NTOHS(ti->ti_win); |
| 353 | NTOHS(ti->ti_urp); |
| 354 | |
| 355 | /* |
| 356 | * Drop TCP, IP headers and TCP options. |
| 357 | */ |
| 358 | m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
| 359 | m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
| 360 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 361 | if (slirp_restrict) { |
| 362 | for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) |
| 363 | if (ex_ptr->ex_fport == port_geth(ti->ti_dport) && |
| 364 | (ip_geth(ti->ti_dst) & 0xff) == ex_ptr->ex_addr) |
| 365 | break; |
| 366 | |
| 367 | if (!ex_ptr) |
| 368 | goto drop; |
| 369 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 370 | /* |
| 371 | * Locate pcb for segment. |
| 372 | */ |
| 373 | findso: |
| 374 | so = tcp_last_so; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 375 | { |
| 376 | uint32_t srcip = ip_geth(ti->ti_src); |
| 377 | uint32_t dstip = ip_geth(ti->ti_dst); |
| 378 | uint16_t dstport = port_geth(ti->ti_dport); |
| 379 | uint16_t srcport = port_geth(ti->ti_sport); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 380 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 381 | if (so->so_faddr_port != dstport || |
| 382 | so->so_laddr_port != srcport || |
| 383 | so->so_laddr_ip != srcip || |
| 384 | so->so_faddr_ip != dstip) { |
| 385 | so = solookup(&tcb, srcip, srcport, dstip, dstport); |
| 386 | if (so) |
| 387 | tcp_last_so = so; |
| 388 | STAT(tcpstat.tcps_socachemiss++); |
| 389 | } |
| 390 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 391 | /* |
| 392 | * If the state is CLOSED (i.e., TCB does not exist) then |
| 393 | * all data in the incoming segment is discarded. |
| 394 | * If the TCB exists but is in CLOSED state, it is embryonic, |
| 395 | * but should either do a listen or a connect soon. |
| 396 | * |
| 397 | * state == CLOSED means we've done socreate() but haven't |
| 398 | * attached it to a protocol yet... |
| 399 | * |
| 400 | * XXX If a TCB does not exist, and the TH_SYN flag is |
| 401 | * the only flag set, then create a session, mark it |
| 402 | * as if it was LISTENING, and continue... |
| 403 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 404 | if (so == NULL) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 405 | if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN) |
| 406 | goto dropwithreset; |
| 407 | |
| 408 | if ((so = socreate()) == NULL) |
| 409 | goto dropwithreset; |
| 410 | if (tcp_attach(so) < 0) { |
| 411 | free(so); /* Not sofree (if it failed, it's not insqued) */ |
| 412 | goto dropwithreset; |
| 413 | } |
| 414 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 415 | sbreserve(&so->so_snd, TCP_SNDSPACE); |
| 416 | sbreserve(&so->so_rcv, TCP_RCVSPACE); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 417 | |
| 418 | /* tcp_last_so = so; */ /* XXX ? */ |
| 419 | /* tp = sototcpcb(so); */ |
| 420 | |
| 421 | so->so_laddr_ip = ip_geth(ti->ti_src); |
| 422 | so->so_laddr_port = port_geth(ti->ti_sport); |
| 423 | so->so_faddr_ip = ip_geth(ti->ti_dst); |
| 424 | so->so_faddr_port = port_geth(ti->ti_dport); |
| 425 | |
| 426 | if ((so->so_iptos = tcp_tos(so)) == 0) |
| 427 | so->so_iptos = ((struct ip *)ti)->ip_tos; |
| 428 | |
| 429 | tp = sototcpcb(so); |
| 430 | tp->t_state = TCPS_LISTEN; |
| 431 | } |
| 432 | |
| 433 | /* |
| 434 | * If this is a still-connecting socket, this probably |
| 435 | * a retransmit of the SYN. Whether it's a retransmit SYN |
| 436 | * or something else, we nuke it. |
| 437 | */ |
| 438 | if (so->so_state & SS_ISFCONNECTING) |
| 439 | goto drop; |
| 440 | |
| 441 | tp = sototcpcb(so); |
| 442 | |
| 443 | /* XXX Should never fail */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 444 | if (tp == NULL) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 445 | goto dropwithreset; |
| 446 | if (tp->t_state == TCPS_CLOSED) |
| 447 | goto drop; |
| 448 | |
| 449 | /* Unscale the window into a 32-bit value. */ |
| 450 | /* if ((tiflags & TH_SYN) == 0) |
| 451 | * tiwin = ti->ti_win << tp->snd_scale; |
| 452 | * else |
| 453 | */ |
| 454 | tiwin = ti->ti_win; |
| 455 | |
| 456 | /* |
| 457 | * Segment received on connection. |
| 458 | * Reset idle time and keep-alive timer. |
| 459 | */ |
| 460 | tp->t_idle = 0; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 461 | if (SO_OPTIONS) |
| 462 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 463 | else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 464 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * Process options if not in LISTEN state, |
| 468 | * else do it below (after getting remote address). |
| 469 | */ |
| 470 | if (optp && tp->t_state != TCPS_LISTEN) |
| 471 | tcp_dooptions(tp, (u_char *)optp, optlen, ti); |
| 472 | /* , */ |
| 473 | /* &ts_present, &ts_val, &ts_ecr); */ |
| 474 | |
| 475 | /* |
| 476 | * Header prediction: check for the two common cases |
| 477 | * of a uni-directional data xfer. If the packet has |
| 478 | * no control flags, is in-sequence, the window didn't |
| 479 | * change and we're not retransmitting, it's a |
| 480 | * candidate. If the length is zero and the ack moved |
| 481 | * forward, we're the sender side of the xfer. Just |
| 482 | * free the data acked & wake any higher level process |
| 483 | * that was blocked waiting for space. If the length |
| 484 | * is non-zero and the ack didn't move, we're the |
| 485 | * receiver side. If we're getting packets in-order |
| 486 | * (the reassembly queue is empty), add the data to |
| 487 | * the socket buffer and note that we need a delayed ack. |
| 488 | * |
| 489 | * XXX Some of these tests are not needed |
| 490 | * eg: the tiwin == tp->snd_wnd prevents many more |
| 491 | * predictions.. with no *real* advantage.. |
| 492 | */ |
| 493 | if (tp->t_state == TCPS_ESTABLISHED && |
| 494 | (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && |
| 495 | /* (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */ |
| 496 | ti->ti_seq == tp->rcv_nxt && |
| 497 | tiwin && tiwin == tp->snd_wnd && |
| 498 | tp->snd_nxt == tp->snd_max) { |
| 499 | /* |
| 500 | * If last ACK falls within this segment's sequence numbers, |
| 501 | * record the timestamp. |
| 502 | */ |
| 503 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && |
| 504 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) { |
| 505 | * tp->ts_recent_age = tcp_now; |
| 506 | * tp->ts_recent = ts_val; |
| 507 | * } |
| 508 | */ |
| 509 | if (ti->ti_len == 0) { |
| 510 | if (SEQ_GT(ti->ti_ack, tp->snd_una) && |
| 511 | SEQ_LEQ(ti->ti_ack, tp->snd_max) && |
| 512 | tp->snd_cwnd >= tp->snd_wnd) { |
| 513 | /* |
| 514 | * this is a pure ack for outstanding data. |
| 515 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 516 | STAT(tcpstat.tcps_predack++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 517 | /* if (ts_present) |
| 518 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1); |
| 519 | * else |
| 520 | */ if (tp->t_rtt && |
| 521 | SEQ_GT(ti->ti_ack, tp->t_rtseq)) |
| 522 | tcp_xmit_timer(tp, tp->t_rtt); |
| 523 | acked = ti->ti_ack - tp->snd_una; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 524 | STAT(tcpstat.tcps_rcvackpack++); |
| 525 | STAT(tcpstat.tcps_rcvackbyte += acked); |
| 526 | sbdrop(&so->so_snd, acked); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 527 | tp->snd_una = ti->ti_ack; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 528 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * If all outstanding data are acked, stop |
| 532 | * retransmit timer, otherwise restart timer |
| 533 | * using current (possibly backed-off) value. |
| 534 | * If process is waiting for space, |
| 535 | * wakeup/selwakeup/signal. If data |
| 536 | * are ready to send, let tcp_output |
| 537 | * decide between more output or persist. |
| 538 | */ |
| 539 | if (tp->snd_una == tp->snd_max) |
| 540 | tp->t_timer[TCPT_REXMT] = 0; |
| 541 | else if (tp->t_timer[TCPT_PERSIST] == 0) |
| 542 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; |
| 543 | |
| 544 | /* |
| 545 | * There's room in so_snd, sowwakup will read() |
| 546 | * from the socket if we can |
| 547 | */ |
| 548 | /* if (so->so_snd.sb_flags & SB_NOTIFY) |
| 549 | * sowwakeup(so); |
| 550 | */ |
| 551 | /* |
| 552 | * This is called because sowwakeup might have |
| 553 | * put data into so_snd. Since we don't so sowwakeup, |
| 554 | * we don't need this.. XXX??? |
| 555 | */ |
| 556 | if (so->so_snd.sb_cc) |
| 557 | (void) tcp_output(tp); |
| 558 | |
| 559 | return; |
| 560 | } |
| 561 | } else if (ti->ti_ack == tp->snd_una && |
| 562 | tcpfrag_list_empty(tp) && |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 563 | ti->ti_len <= sbspace(&so->so_rcv)) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 564 | /* |
| 565 | * this is a pure, in-sequence data packet |
| 566 | * with nothing on the reassembly queue and |
| 567 | * we have enough buffer space to take it. |
| 568 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 569 | STAT(tcpstat.tcps_preddat++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 570 | tp->rcv_nxt += ti->ti_len; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 571 | STAT(tcpstat.tcps_rcvpack++); |
| 572 | STAT(tcpstat.tcps_rcvbyte += ti->ti_len); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 573 | /* |
| 574 | * Add data to socket buffer. |
| 575 | */ |
| 576 | if (so->so_emu) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 577 | if (tcp_emu(so,m)) sbappend(so, m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 578 | } else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 579 | sbappend(so, m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * XXX This is called when data arrives. Later, check |
| 583 | * if we can actually write() to the socket |
| 584 | * XXX Need to check? It's be NON_BLOCKING |
| 585 | */ |
| 586 | /* sorwakeup(so); */ |
| 587 | |
| 588 | /* |
| 589 | * If this is a short packet, then ACK now - with Nagel |
| 590 | * congestion avoidance sender won't send more until |
| 591 | * he gets an ACK. |
| 592 | * |
| 593 | * It is better to not delay acks at all to maximize |
| 594 | * TCP throughput. See RFC 2581. |
| 595 | */ |
| 596 | tp->t_flags |= TF_ACKNOW; |
| 597 | tcp_output(tp); |
| 598 | return; |
| 599 | } |
| 600 | } /* header prediction */ |
| 601 | /* |
| 602 | * Calculate amount of space in receive window, |
| 603 | * and then do TCP input processing. |
| 604 | * Receive window is amount of space in rcv queue, |
| 605 | * but not less than advertised window. |
| 606 | */ |
| 607 | { int win; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 608 | win = sbspace(&so->so_rcv); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 609 | if (win < 0) |
| 610 | win = 0; |
| 611 | tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); |
| 612 | } |
| 613 | |
| 614 | switch (tp->t_state) { |
| 615 | |
| 616 | /* |
| 617 | * If the state is LISTEN then ignore segment if it contains an RST. |
| 618 | * If the segment contains an ACK then it is bad and send a RST. |
| 619 | * If it does not contain a SYN then it is not interesting; drop it. |
| 620 | * Don't bother responding if the destination was a broadcast. |
| 621 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial |
| 622 | * tp->iss, and send a segment: |
| 623 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> |
| 624 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. |
| 625 | * Fill in remote peer address fields if not previously specified. |
| 626 | * Enter SYN_RECEIVED state, and process any other fields of this |
| 627 | * segment in this state. |
| 628 | */ |
| 629 | case TCPS_LISTEN: { |
| 630 | |
| 631 | if (tiflags & TH_RST) |
| 632 | goto drop; |
| 633 | if (tiflags & TH_ACK) |
| 634 | goto dropwithreset; |
| 635 | if ((tiflags & TH_SYN) == 0) |
| 636 | goto drop; |
| 637 | |
| 638 | /* |
| 639 | * This has way too many gotos... |
| 640 | * But a bit of spaghetti code never hurt anybody :) |
| 641 | */ |
| 642 | |
| 643 | /* |
| 644 | * If this is destined for the control address, then flag to |
| 645 | * tcp_ctl once connected, otherwise connect |
| 646 | */ |
| 647 | if ((so->so_faddr_ip & 0xffffff00) == special_addr_ip) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 648 | int lastbyte=so->so_faddr_ip & 0xff; |
| 649 | if (lastbyte!=CTL_ALIAS && lastbyte!=CTL_DNS) { |
| 650 | #if 0 |
| 651 | if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) { |
| 652 | /* Command or exec adress */ |
| 653 | so->so_state |= SS_CTL; |
| 654 | } else |
| 655 | #endif |
| 656 | { |
| 657 | /* May be an add exec */ |
| 658 | for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) { |
| 659 | if(ex_ptr->ex_fport == so->so_faddr_port && |
| 660 | lastbyte == ex_ptr->ex_addr) { |
| 661 | so->so_state |= SS_CTL; |
| 662 | break; |
| 663 | } |
| 664 | } |
| 665 | } |
| 666 | if(so->so_state & SS_CTL) goto cont_input; |
| 667 | } |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 668 | /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */ |
| 669 | } |
| 670 | |
| 671 | if (so->so_emu & EMU_NOCONNECT) { |
| 672 | so->so_emu &= ~EMU_NOCONNECT; |
| 673 | goto cont_input; |
| 674 | } |
| 675 | |
| 676 | if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) { |
| 677 | u_char code=ICMP_UNREACH_NET; |
| 678 | DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n", |
| 679 | errno,errno_str)); |
| 680 | if(errno == ECONNREFUSED) { |
| 681 | /* ACK the SYN, send RST to refuse the connection */ |
| 682 | tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0, |
| 683 | TH_RST|TH_ACK); |
| 684 | } else { |
| 685 | if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST; |
| 686 | HTONL(ti->ti_seq); /* restore tcp header */ |
| 687 | HTONL(ti->ti_ack); |
| 688 | HTONS(ti->ti_win); |
| 689 | HTONS(ti->ti_urp); |
| 690 | m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
| 691 | m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr); |
| 692 | *ip=save_ip; |
| 693 | icmp_error(m, ICMP_UNREACH,code, 0,errno_str); |
| 694 | } |
| 695 | tp = tcp_close(tp); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 696 | m_free(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 697 | } else { |
| 698 | /* |
| 699 | * Haven't connected yet, save the current mbuf |
| 700 | * and ti, and return |
| 701 | * XXX Some OS's don't tell us whether the connect() |
| 702 | * succeeded or not. So we must time it out. |
| 703 | */ |
| 704 | so->so_m = m; |
| 705 | so->so_ti = ti; |
| 706 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; |
| 707 | tp->t_state = TCPS_SYN_RECEIVED; |
| 708 | } |
| 709 | return; |
| 710 | |
| 711 | cont_conn: |
| 712 | /* m==NULL |
| 713 | * Check if the connect succeeded |
| 714 | */ |
| 715 | if (so->so_state & SS_NOFDREF) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 716 | tp = tcp_close(tp); |
| 717 | goto dropwithreset; |
| 718 | } |
| 719 | cont_input: |
| 720 | tcp_template(tp); |
| 721 | |
| 722 | if (optp) |
| 723 | tcp_dooptions(tp, (u_char *)optp, optlen, ti); |
| 724 | /* , */ |
| 725 | /* &ts_present, &ts_val, &ts_ecr); */ |
| 726 | |
| 727 | if (iss) |
| 728 | tp->iss = iss; |
| 729 | else |
| 730 | tp->iss = tcp_iss; |
| 731 | tcp_iss += TCP_ISSINCR/2; |
| 732 | tp->irs = ti->ti_seq; |
| 733 | tcp_sendseqinit(tp); |
| 734 | tcp_rcvseqinit(tp); |
| 735 | tp->t_flags |= TF_ACKNOW; |
| 736 | tp->t_state = TCPS_SYN_RECEIVED; |
| 737 | tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 738 | STAT(tcpstat.tcps_accepts++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 739 | goto trimthenstep6; |
| 740 | } /* case TCPS_LISTEN */ |
| 741 | |
| 742 | /* |
| 743 | * If the state is SYN_SENT: |
| 744 | * if seg contains an ACK, but not for our SYN, drop the input. |
| 745 | * if seg contains a RST, then drop the connection. |
| 746 | * if seg does not contain SYN, then drop it. |
| 747 | * Otherwise this is an acceptable SYN segment |
| 748 | * initialize tp->rcv_nxt and tp->irs |
| 749 | * if seg contains ack then advance tp->snd_una |
| 750 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state |
| 751 | * arrange for segment to be acked (eventually) |
| 752 | * continue processing rest of data/controls, beginning with URG |
| 753 | */ |
| 754 | case TCPS_SYN_SENT: |
| 755 | if ((tiflags & TH_ACK) && |
| 756 | (SEQ_LEQ(ti->ti_ack, tp->iss) || |
| 757 | SEQ_GT(ti->ti_ack, tp->snd_max))) |
| 758 | goto dropwithreset; |
| 759 | |
| 760 | if (tiflags & TH_RST) { |
| 761 | if (tiflags & TH_ACK) |
| 762 | tp = tcp_drop(tp,0); /* XXX Check t_softerror! */ |
| 763 | goto drop; |
| 764 | } |
| 765 | |
| 766 | if ((tiflags & TH_SYN) == 0) |
| 767 | goto drop; |
| 768 | if (tiflags & TH_ACK) { |
| 769 | tp->snd_una = ti->ti_ack; |
| 770 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 771 | tp->snd_nxt = tp->snd_una; |
| 772 | } |
| 773 | |
| 774 | tp->t_timer[TCPT_REXMT] = 0; |
| 775 | tp->irs = ti->ti_seq; |
| 776 | tcp_rcvseqinit(tp); |
| 777 | tp->t_flags |= TF_ACKNOW; |
| 778 | if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 779 | STAT(tcpstat.tcps_connects++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 780 | soisfconnected(so); |
| 781 | tp->t_state = TCPS_ESTABLISHED; |
| 782 | |
| 783 | /* Do window scaling on this connection? */ |
| 784 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == |
| 785 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) { |
| 786 | * tp->snd_scale = tp->requested_s_scale; |
| 787 | * tp->rcv_scale = tp->request_r_scale; |
| 788 | * } |
| 789 | */ |
| 790 | (void) tcp_reass(tp, (struct tcpiphdr *)0, |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 791 | (struct mbuf *)0); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 792 | /* |
| 793 | * if we didn't have to retransmit the SYN, |
| 794 | * use its rtt as our initial srtt & rtt var. |
| 795 | */ |
| 796 | if (tp->t_rtt) |
| 797 | tcp_xmit_timer(tp, tp->t_rtt); |
| 798 | } else |
| 799 | tp->t_state = TCPS_SYN_RECEIVED; |
| 800 | |
| 801 | trimthenstep6: |
| 802 | /* |
| 803 | * Advance ti->ti_seq to correspond to first data byte. |
| 804 | * If data, trim to stay within window, |
| 805 | * dropping FIN if necessary. |
| 806 | */ |
| 807 | ti->ti_seq++; |
| 808 | if (ti->ti_len > tp->rcv_wnd) { |
| 809 | todrop = ti->ti_len - tp->rcv_wnd; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 810 | m_adj(m, -todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 811 | ti->ti_len = tp->rcv_wnd; |
| 812 | tiflags &= ~TH_FIN; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 813 | STAT(tcpstat.tcps_rcvpackafterwin++); |
| 814 | STAT(tcpstat.tcps_rcvbyteafterwin += todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 815 | } |
| 816 | tp->snd_wl1 = ti->ti_seq - 1; |
| 817 | tp->rcv_up = ti->ti_seq; |
| 818 | goto step6; |
| 819 | } /* switch tp->t_state */ |
| 820 | /* |
| 821 | * States other than LISTEN or SYN_SENT. |
| 822 | * First check timestamp, if present. |
| 823 | * Then check that at least some bytes of segment are within |
| 824 | * receive window. If segment begins before rcv_nxt, |
| 825 | * drop leading data (and SYN); if nothing left, just ack. |
| 826 | * |
| 827 | * RFC 1323 PAWS: If we have a timestamp reply on this segment |
| 828 | * and it's less than ts_recent, drop it. |
| 829 | */ |
| 830 | /* if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent && |
| 831 | * TSTMP_LT(ts_val, tp->ts_recent)) { |
| 832 | * |
| 833 | */ /* Check to see if ts_recent is over 24 days old. */ |
| 834 | /* if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { |
| 835 | */ /* |
| 836 | * * Invalidate ts_recent. If this segment updates |
| 837 | * * ts_recent, the age will be reset later and ts_recent |
| 838 | * * will get a valid value. If it does not, setting |
| 839 | * * ts_recent to zero will at least satisfy the |
| 840 | * * requirement that zero be placed in the timestamp |
| 841 | * * echo reply when ts_recent isn't valid. The |
| 842 | * * age isn't reset until we get a valid ts_recent |
| 843 | * * because we don't want out-of-order segments to be |
| 844 | * * dropped when ts_recent is old. |
| 845 | * */ |
| 846 | /* tp->ts_recent = 0; |
| 847 | * } else { |
| 848 | * tcpstat.tcps_rcvduppack++; |
| 849 | * tcpstat.tcps_rcvdupbyte += ti->ti_len; |
| 850 | * tcpstat.tcps_pawsdrop++; |
| 851 | * goto dropafterack; |
| 852 | * } |
| 853 | * } |
| 854 | */ |
| 855 | |
| 856 | todrop = tp->rcv_nxt - ti->ti_seq; |
| 857 | if (todrop > 0) { |
| 858 | if (tiflags & TH_SYN) { |
| 859 | tiflags &= ~TH_SYN; |
| 860 | ti->ti_seq++; |
| 861 | if (ti->ti_urp > 1) |
| 862 | ti->ti_urp--; |
| 863 | else |
| 864 | tiflags &= ~TH_URG; |
| 865 | todrop--; |
| 866 | } |
| 867 | /* |
| 868 | * Following if statement from Stevens, vol. 2, p. 960. |
| 869 | */ |
| 870 | if (todrop > ti->ti_len |
| 871 | || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) { |
| 872 | /* |
| 873 | * Any valid FIN must be to the left of the window. |
| 874 | * At this point the FIN must be a duplicate or out |
| 875 | * of sequence; drop it. |
| 876 | */ |
| 877 | tiflags &= ~TH_FIN; |
| 878 | |
| 879 | /* |
| 880 | * Send an ACK to resynchronize and drop any data. |
| 881 | * But keep on processing for RST or ACK. |
| 882 | */ |
| 883 | tp->t_flags |= TF_ACKNOW; |
| 884 | todrop = ti->ti_len; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 885 | STAT(tcpstat.tcps_rcvduppack++); |
| 886 | STAT(tcpstat.tcps_rcvdupbyte += todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 887 | } else { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 888 | STAT(tcpstat.tcps_rcvpartduppack++); |
| 889 | STAT(tcpstat.tcps_rcvpartdupbyte += todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 890 | } |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 891 | m_adj(m, todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 892 | ti->ti_seq += todrop; |
| 893 | ti->ti_len -= todrop; |
| 894 | if (ti->ti_urp > todrop) |
| 895 | ti->ti_urp -= todrop; |
| 896 | else { |
| 897 | tiflags &= ~TH_URG; |
| 898 | ti->ti_urp = 0; |
| 899 | } |
| 900 | } |
| 901 | /* |
| 902 | * If new data are received on a connection after the |
| 903 | * user processes are gone, then RST the other end. |
| 904 | */ |
| 905 | if ((so->so_state & SS_NOFDREF) && |
| 906 | tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { |
| 907 | tp = tcp_close(tp); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 908 | STAT(tcpstat.tcps_rcvafterclose++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 909 | goto dropwithreset; |
| 910 | } |
| 911 | |
| 912 | /* |
| 913 | * If segment ends after window, drop trailing data |
| 914 | * (and PUSH and FIN); if nothing left, just ACK. |
| 915 | */ |
| 916 | todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); |
| 917 | if (todrop > 0) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 918 | STAT(tcpstat.tcps_rcvpackafterwin++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 919 | if (todrop >= ti->ti_len) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 920 | STAT(tcpstat.tcps_rcvbyteafterwin += ti->ti_len); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 921 | /* |
| 922 | * If a new connection request is received |
| 923 | * while in TIME_WAIT, drop the old connection |
| 924 | * and start over if the sequence numbers |
| 925 | * are above the previous ones. |
| 926 | */ |
| 927 | if (tiflags & TH_SYN && |
| 928 | tp->t_state == TCPS_TIME_WAIT && |
| 929 | SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { |
| 930 | iss = tp->rcv_nxt + TCP_ISSINCR; |
| 931 | tp = tcp_close(tp); |
| 932 | goto findso; |
| 933 | } |
| 934 | /* |
| 935 | * If window is closed can only take segments at |
| 936 | * window edge, and have to drop data and PUSH from |
| 937 | * incoming segments. Continue processing, but |
| 938 | * remember to ack. Otherwise, drop segment |
| 939 | * and ack. |
| 940 | */ |
| 941 | if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { |
| 942 | tp->t_flags |= TF_ACKNOW; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 943 | STAT(tcpstat.tcps_rcvwinprobe++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 944 | } else |
| 945 | goto dropafterack; |
| 946 | } else |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 947 | STAT(tcpstat.tcps_rcvbyteafterwin += todrop); |
| 948 | m_adj(m, -todrop); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 949 | ti->ti_len -= todrop; |
| 950 | tiflags &= ~(TH_PUSH|TH_FIN); |
| 951 | } |
| 952 | |
| 953 | /* |
| 954 | * If last ACK falls within this segment's sequence numbers, |
| 955 | * record its timestamp. |
| 956 | */ |
| 957 | /* if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) && |
| 958 | * SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len + |
| 959 | * ((tiflags & (TH_SYN|TH_FIN)) != 0))) { |
| 960 | * tp->ts_recent_age = tcp_now; |
| 961 | * tp->ts_recent = ts_val; |
| 962 | * } |
| 963 | */ |
| 964 | |
| 965 | /* |
| 966 | * If the RST bit is set examine the state: |
| 967 | * SYN_RECEIVED STATE: |
| 968 | * If passive open, return to LISTEN state. |
| 969 | * If active open, inform user that connection was refused. |
| 970 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: |
| 971 | * Inform user that connection was reset, and close tcb. |
| 972 | * CLOSING, LAST_ACK, TIME_WAIT STATES |
| 973 | * Close the tcb. |
| 974 | */ |
| 975 | if (tiflags&TH_RST) switch (tp->t_state) { |
| 976 | |
| 977 | case TCPS_SYN_RECEIVED: |
| 978 | /* so->so_error = ECONNREFUSED; */ |
| 979 | goto close; |
| 980 | |
| 981 | case TCPS_ESTABLISHED: |
| 982 | case TCPS_FIN_WAIT_1: |
| 983 | case TCPS_FIN_WAIT_2: |
| 984 | case TCPS_CLOSE_WAIT: |
| 985 | /* so->so_error = ECONNRESET; */ |
| 986 | close: |
| 987 | tp->t_state = TCPS_CLOSED; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 988 | STAT(tcpstat.tcps_drops++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 989 | tp = tcp_close(tp); |
| 990 | goto drop; |
| 991 | |
| 992 | case TCPS_CLOSING: |
| 993 | case TCPS_LAST_ACK: |
| 994 | case TCPS_TIME_WAIT: |
| 995 | tp = tcp_close(tp); |
| 996 | goto drop; |
| 997 | } |
| 998 | |
| 999 | /* |
| 1000 | * If a SYN is in the window, then this is an |
| 1001 | * error and we send an RST and drop the connection. |
| 1002 | */ |
| 1003 | if (tiflags & TH_SYN) { |
| 1004 | tp = tcp_drop(tp,0); |
| 1005 | goto dropwithreset; |
| 1006 | } |
| 1007 | |
| 1008 | /* |
| 1009 | * If the ACK bit is off we drop the segment and return. |
| 1010 | */ |
| 1011 | if ((tiflags & TH_ACK) == 0) goto drop; |
| 1012 | |
| 1013 | /* |
| 1014 | * Ack processing. |
| 1015 | */ |
| 1016 | switch (tp->t_state) { |
| 1017 | /* |
| 1018 | * In SYN_RECEIVED state if the ack ACKs our SYN then enter |
| 1019 | * ESTABLISHED state and continue processing, otherwise |
| 1020 | * send an RST. una<=ack<=max |
| 1021 | */ |
| 1022 | case TCPS_SYN_RECEIVED: |
| 1023 | |
| 1024 | if (SEQ_GT(tp->snd_una, ti->ti_ack) || |
| 1025 | SEQ_GT(ti->ti_ack, tp->snd_max)) |
| 1026 | goto dropwithreset; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1027 | STAT(tcpstat.tcps_connects++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1028 | tp->t_state = TCPS_ESTABLISHED; |
| 1029 | /* |
| 1030 | * The sent SYN is ack'ed with our sequence number +1 |
| 1031 | * The first data byte already in the buffer will get |
| 1032 | * lost if no correction is made. This is only needed for |
| 1033 | * SS_CTL since the buffer is empty otherwise. |
| 1034 | * tp->snd_una++; or: |
| 1035 | */ |
| 1036 | tp->snd_una=ti->ti_ack; |
| 1037 | if (so->so_state & SS_CTL) { |
| 1038 | /* So tcp_ctl reports the right state */ |
| 1039 | ret = tcp_ctl(so); |
| 1040 | if (ret == 1) { |
| 1041 | soisfconnected(so); |
| 1042 | so->so_state &= ~SS_CTL; /* success XXX */ |
| 1043 | } else if (ret == 2) { |
| 1044 | so->so_state = SS_NOFDREF; /* CTL_CMD */ |
| 1045 | } else { |
| 1046 | needoutput = 1; |
| 1047 | tp->t_state = TCPS_FIN_WAIT_1; |
| 1048 | } |
| 1049 | } else { |
| 1050 | soisfconnected(so); |
| 1051 | } |
| 1052 | |
| 1053 | /* Do window scaling? */ |
| 1054 | /* if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == |
| 1055 | * (TF_RCVD_SCALE|TF_REQ_SCALE)) { |
| 1056 | * tp->snd_scale = tp->requested_s_scale; |
| 1057 | * tp->rcv_scale = tp->request_r_scale; |
| 1058 | * } |
| 1059 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1060 | (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1061 | tp->snd_wl1 = ti->ti_seq - 1; |
| 1062 | /* Avoid ack processing; snd_una==ti_ack => dup ack */ |
| 1063 | goto synrx_to_est; |
| 1064 | /* fall into ... */ |
| 1065 | |
| 1066 | /* |
| 1067 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range |
| 1068 | * ACKs. If the ack is in the range |
| 1069 | * tp->snd_una < ti->ti_ack <= tp->snd_max |
| 1070 | * then advance tp->snd_una to ti->ti_ack and drop |
| 1071 | * data from the retransmission queue. If this ACK reflects |
| 1072 | * more up to date window information we update our window information. |
| 1073 | */ |
| 1074 | case TCPS_ESTABLISHED: |
| 1075 | case TCPS_FIN_WAIT_1: |
| 1076 | case TCPS_FIN_WAIT_2: |
| 1077 | case TCPS_CLOSE_WAIT: |
| 1078 | case TCPS_CLOSING: |
| 1079 | case TCPS_LAST_ACK: |
| 1080 | case TCPS_TIME_WAIT: |
| 1081 | |
| 1082 | if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { |
| 1083 | if (ti->ti_len == 0 && tiwin == tp->snd_wnd) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1084 | STAT(tcpstat.tcps_rcvdupack++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1085 | DEBUG_MISC((dfd," dup ack m = %lx so = %lx \n", |
| 1086 | (long )m, (long )so)); |
| 1087 | /* |
| 1088 | * If we have outstanding data (other than |
| 1089 | * a window probe), this is a completely |
| 1090 | * duplicate ack (ie, window info didn't |
| 1091 | * change), the ack is the biggest we've |
| 1092 | * seen and we've seen exactly our rexmt |
| 1093 | * threshold of them, assume a packet |
| 1094 | * has been dropped and retransmit it. |
| 1095 | * Kludge snd_nxt & the congestion |
| 1096 | * window so we send only this one |
| 1097 | * packet. |
| 1098 | * |
| 1099 | * We know we're losing at the current |
| 1100 | * window size so do congestion avoidance |
| 1101 | * (set ssthresh to half the current window |
| 1102 | * and pull our congestion window back to |
| 1103 | * the new ssthresh). |
| 1104 | * |
| 1105 | * Dup acks mean that packets have left the |
| 1106 | * network (they're now cached at the receiver) |
| 1107 | * so bump cwnd by the amount in the receiver |
| 1108 | * to keep a constant cwnd packets in the |
| 1109 | * network. |
| 1110 | */ |
| 1111 | if (tp->t_timer[TCPT_REXMT] == 0 || |
| 1112 | ti->ti_ack != tp->snd_una) |
| 1113 | tp->t_dupacks = 0; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1114 | else if (++tp->t_dupacks == TCPREXMTTHRESH) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1115 | tcp_seq onxt = tp->snd_nxt; |
| 1116 | u_int win = |
| 1117 | min(tp->snd_wnd, tp->snd_cwnd) / 2 / |
| 1118 | tp->t_maxseg; |
| 1119 | |
| 1120 | if (win < 2) |
| 1121 | win = 2; |
| 1122 | tp->snd_ssthresh = win * tp->t_maxseg; |
| 1123 | tp->t_timer[TCPT_REXMT] = 0; |
| 1124 | tp->t_rtt = 0; |
| 1125 | tp->snd_nxt = ti->ti_ack; |
| 1126 | tp->snd_cwnd = tp->t_maxseg; |
| 1127 | (void) tcp_output(tp); |
| 1128 | tp->snd_cwnd = tp->snd_ssthresh + |
| 1129 | tp->t_maxseg * tp->t_dupacks; |
| 1130 | if (SEQ_GT(onxt, tp->snd_nxt)) |
| 1131 | tp->snd_nxt = onxt; |
| 1132 | goto drop; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1133 | } else if (tp->t_dupacks > TCPREXMTTHRESH) { |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1134 | tp->snd_cwnd += tp->t_maxseg; |
| 1135 | (void) tcp_output(tp); |
| 1136 | goto drop; |
| 1137 | } |
| 1138 | } else |
| 1139 | tp->t_dupacks = 0; |
| 1140 | break; |
| 1141 | } |
| 1142 | synrx_to_est: |
| 1143 | /* |
| 1144 | * If the congestion window was inflated to account |
| 1145 | * for the other side's cached packets, retract it. |
| 1146 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1147 | if (tp->t_dupacks > TCPREXMTTHRESH && |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1148 | tp->snd_cwnd > tp->snd_ssthresh) |
| 1149 | tp->snd_cwnd = tp->snd_ssthresh; |
| 1150 | tp->t_dupacks = 0; |
| 1151 | if (SEQ_GT(ti->ti_ack, tp->snd_max)) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1152 | STAT(tcpstat.tcps_rcvacktoomuch++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1153 | goto dropafterack; |
| 1154 | } |
| 1155 | acked = ti->ti_ack - tp->snd_una; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1156 | STAT(tcpstat.tcps_rcvackpack++); |
| 1157 | STAT(tcpstat.tcps_rcvackbyte += acked); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1158 | |
| 1159 | /* |
| 1160 | * If we have a timestamp reply, update smoothed |
| 1161 | * round trip time. If no timestamp is present but |
| 1162 | * transmit timer is running and timed sequence |
| 1163 | * number was acked, update smoothed round trip time. |
| 1164 | * Since we now have an rtt measurement, cancel the |
| 1165 | * timer backoff (cf., Phil Karn's retransmit alg.). |
| 1166 | * Recompute the initial retransmit timer. |
| 1167 | */ |
| 1168 | /* if (ts_present) |
| 1169 | * tcp_xmit_timer(tp, tcp_now-ts_ecr+1); |
| 1170 | * else |
| 1171 | */ |
| 1172 | if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) |
| 1173 | tcp_xmit_timer(tp,tp->t_rtt); |
| 1174 | |
| 1175 | /* |
| 1176 | * If all outstanding data is acked, stop retransmit |
| 1177 | * timer and remember to restart (more output or persist). |
| 1178 | * If there is more data to be acked, restart retransmit |
| 1179 | * timer, using current (possibly backed-off) value. |
| 1180 | */ |
| 1181 | if (ti->ti_ack == tp->snd_max) { |
| 1182 | tp->t_timer[TCPT_REXMT] = 0; |
| 1183 | needoutput = 1; |
| 1184 | } else if (tp->t_timer[TCPT_PERSIST] == 0) |
| 1185 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; |
| 1186 | /* |
| 1187 | * When new data is acked, open the congestion window. |
| 1188 | * If the window gives us less than ssthresh packets |
| 1189 | * in flight, open exponentially (maxseg per packet). |
| 1190 | * Otherwise open linearly: maxseg per window |
| 1191 | * (maxseg^2 / cwnd per packet). |
| 1192 | */ |
| 1193 | { |
| 1194 | register u_int cw = tp->snd_cwnd; |
| 1195 | register u_int incr = tp->t_maxseg; |
| 1196 | |
| 1197 | if (cw > tp->snd_ssthresh) |
| 1198 | incr = incr * incr / cw; |
| 1199 | tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); |
| 1200 | } |
| 1201 | if (acked > so->so_snd.sb_cc) { |
| 1202 | tp->snd_wnd -= so->so_snd.sb_cc; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1203 | sbdrop(&so->so_snd, (int )so->so_snd.sb_cc); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1204 | ourfinisacked = 1; |
| 1205 | } else { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1206 | sbdrop(&so->so_snd, acked); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1207 | tp->snd_wnd -= acked; |
| 1208 | ourfinisacked = 0; |
| 1209 | } |
| 1210 | /* |
| 1211 | * XXX sowwakup is called when data is acked and there's room for |
| 1212 | * for more data... it should read() the socket |
| 1213 | */ |
| 1214 | /* if (so->so_snd.sb_flags & SB_NOTIFY) |
| 1215 | * sowwakeup(so); |
| 1216 | */ |
| 1217 | tp->snd_una = ti->ti_ack; |
| 1218 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) |
| 1219 | tp->snd_nxt = tp->snd_una; |
| 1220 | |
| 1221 | switch (tp->t_state) { |
| 1222 | |
| 1223 | /* |
| 1224 | * In FIN_WAIT_1 STATE in addition to the processing |
| 1225 | * for the ESTABLISHED state if our FIN is now acknowledged |
| 1226 | * then enter FIN_WAIT_2. |
| 1227 | */ |
| 1228 | case TCPS_FIN_WAIT_1: |
| 1229 | if (ourfinisacked) { |
| 1230 | /* |
| 1231 | * If we can't receive any more |
| 1232 | * data, then closing user can proceed. |
| 1233 | * Starting the timer is contrary to the |
| 1234 | * specification, but if we don't get a FIN |
| 1235 | * we'll hang forever. |
| 1236 | */ |
| 1237 | if (so->so_state & SS_FCANTRCVMORE) { |
| 1238 | soisfdisconnected(so); |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1239 | tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1240 | } |
| 1241 | tp->t_state = TCPS_FIN_WAIT_2; |
| 1242 | } |
| 1243 | break; |
| 1244 | |
| 1245 | /* |
| 1246 | * In CLOSING STATE in addition to the processing for |
| 1247 | * the ESTABLISHED state if the ACK acknowledges our FIN |
| 1248 | * then enter the TIME-WAIT state, otherwise ignore |
| 1249 | * the segment. |
| 1250 | */ |
| 1251 | case TCPS_CLOSING: |
| 1252 | if (ourfinisacked) { |
| 1253 | tp->t_state = TCPS_TIME_WAIT; |
| 1254 | tcp_canceltimers(tp); |
| 1255 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 1256 | soisfdisconnected(so); |
| 1257 | } |
| 1258 | break; |
| 1259 | |
| 1260 | /* |
| 1261 | * In LAST_ACK, we may still be waiting for data to drain |
| 1262 | * and/or to be acked, as well as for the ack of our FIN. |
| 1263 | * If our FIN is now acknowledged, delete the TCB, |
| 1264 | * enter the closed state and return. |
| 1265 | */ |
| 1266 | case TCPS_LAST_ACK: |
| 1267 | if (ourfinisacked) { |
| 1268 | tp = tcp_close(tp); |
| 1269 | goto drop; |
| 1270 | } |
| 1271 | break; |
| 1272 | |
| 1273 | /* |
| 1274 | * In TIME_WAIT state the only thing that should arrive |
| 1275 | * is a retransmission of the remote FIN. Acknowledge |
| 1276 | * it and restart the finack timer. |
| 1277 | */ |
| 1278 | case TCPS_TIME_WAIT: |
| 1279 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 1280 | goto dropafterack; |
| 1281 | } |
| 1282 | } /* switch(tp->t_state) */ |
| 1283 | |
| 1284 | step6: |
| 1285 | /* |
| 1286 | * Update window information. |
| 1287 | * Don't look at window if no ACK: TAC's send garbage on first SYN. |
| 1288 | */ |
| 1289 | if ((tiflags & TH_ACK) && |
| 1290 | (SEQ_LT(tp->snd_wl1, ti->ti_seq) || |
| 1291 | (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) || |
| 1292 | (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) { |
| 1293 | /* keep track of pure window updates */ |
| 1294 | if (ti->ti_len == 0 && |
| 1295 | tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd) |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1296 | STAT(tcpstat.tcps_rcvwinupd++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1297 | tp->snd_wnd = tiwin; |
| 1298 | tp->snd_wl1 = ti->ti_seq; |
| 1299 | tp->snd_wl2 = ti->ti_ack; |
| 1300 | if (tp->snd_wnd > tp->max_sndwnd) |
| 1301 | tp->max_sndwnd = tp->snd_wnd; |
| 1302 | needoutput = 1; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * Process segments with URG. |
| 1307 | */ |
| 1308 | if ((tiflags & TH_URG) && ti->ti_urp && |
| 1309 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 1310 | /* |
| 1311 | * This is a kludge, but if we receive and accept |
| 1312 | * random urgent pointers, we'll crash in |
| 1313 | * soreceive. It's hard to imagine someone |
| 1314 | * actually wanting to send this much urgent data. |
| 1315 | */ |
| 1316 | if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) { |
| 1317 | ti->ti_urp = 0; |
| 1318 | tiflags &= ~TH_URG; |
| 1319 | goto dodata; |
| 1320 | } |
| 1321 | /* |
| 1322 | * If this segment advances the known urgent pointer, |
| 1323 | * then mark the data stream. This should not happen |
| 1324 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since |
| 1325 | * a FIN has been received from the remote side. |
| 1326 | * In these states we ignore the URG. |
| 1327 | * |
| 1328 | * According to RFC961 (Assigned Protocols), |
| 1329 | * the urgent pointer points to the last octet |
| 1330 | * of urgent data. We continue, however, |
| 1331 | * to consider it to indicate the first octet |
| 1332 | * of data past the urgent section as the original |
| 1333 | * spec states (in one of two places). |
| 1334 | */ |
| 1335 | if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { |
| 1336 | tp->rcv_up = ti->ti_seq + ti->ti_urp; |
| 1337 | so->so_urgc = so->so_rcv.sb_cc + |
| 1338 | (tp->rcv_up - tp->rcv_nxt); /* -1; */ |
| 1339 | tp->rcv_up = ti->ti_seq + ti->ti_urp; |
| 1340 | |
| 1341 | } |
| 1342 | } else |
| 1343 | /* |
| 1344 | * If no out of band data is expected, |
| 1345 | * pull receive urgent pointer along |
| 1346 | * with the receive window. |
| 1347 | */ |
| 1348 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) |
| 1349 | tp->rcv_up = tp->rcv_nxt; |
| 1350 | dodata: |
| 1351 | |
| 1352 | /* |
| 1353 | * Process the segment text, merging it into the TCP sequencing queue, |
| 1354 | * and arranging for acknowledgment of receipt if necessary. |
| 1355 | * This process logically involves adjusting tp->rcv_wnd as data |
| 1356 | * is presented to the user (this happens in tcp_usrreq.c, |
| 1357 | * case PRU_RCVD). If a FIN has already been received on this |
| 1358 | * connection then we just ignore the text. |
| 1359 | */ |
| 1360 | if ((ti->ti_len || (tiflags&TH_FIN)) && |
| 1361 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 1362 | TCP_REASS(tp, ti, m, so, tiflags); |
| 1363 | /* |
| 1364 | * Note the amount of data that peer has sent into |
| 1365 | * our window, in order to estimate the sender's |
| 1366 | * buffer size. |
| 1367 | */ |
| 1368 | len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt); |
| 1369 | } else { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1370 | m_free(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1371 | tiflags &= ~TH_FIN; |
| 1372 | } |
| 1373 | |
| 1374 | /* |
| 1375 | * If FIN is received ACK the FIN and let the user know |
| 1376 | * that the connection is closing. |
| 1377 | */ |
| 1378 | if (tiflags & TH_FIN) { |
| 1379 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { |
| 1380 | /* |
| 1381 | * If we receive a FIN we can't send more data, |
| 1382 | * set it SS_FDRAIN |
| 1383 | * Shutdown the socket if there is no rx data in the |
| 1384 | * buffer. |
| 1385 | * soread() is called on completion of shutdown() and |
| 1386 | * will got to TCPS_LAST_ACK, and use tcp_output() |
| 1387 | * to send the FIN. |
| 1388 | */ |
| 1389 | /* sofcantrcvmore(so); */ |
| 1390 | sofwdrain(so); |
| 1391 | |
| 1392 | tp->t_flags |= TF_ACKNOW; |
| 1393 | tp->rcv_nxt++; |
| 1394 | } |
| 1395 | switch (tp->t_state) { |
| 1396 | |
| 1397 | /* |
| 1398 | * In SYN_RECEIVED and ESTABLISHED STATES |
| 1399 | * enter the CLOSE_WAIT state. |
| 1400 | */ |
| 1401 | case TCPS_SYN_RECEIVED: |
| 1402 | case TCPS_ESTABLISHED: |
| 1403 | if(so->so_emu == EMU_CTL) /* no shutdown on socket */ |
| 1404 | tp->t_state = TCPS_LAST_ACK; |
| 1405 | else |
| 1406 | tp->t_state = TCPS_CLOSE_WAIT; |
| 1407 | break; |
| 1408 | |
| 1409 | /* |
| 1410 | * If still in FIN_WAIT_1 STATE FIN has not been acked so |
| 1411 | * enter the CLOSING state. |
| 1412 | */ |
| 1413 | case TCPS_FIN_WAIT_1: |
| 1414 | tp->t_state = TCPS_CLOSING; |
| 1415 | break; |
| 1416 | |
| 1417 | /* |
| 1418 | * In FIN_WAIT_2 state enter the TIME_WAIT state, |
| 1419 | * starting the time-wait timer, turning off the other |
| 1420 | * standard timers. |
| 1421 | */ |
| 1422 | case TCPS_FIN_WAIT_2: |
| 1423 | tp->t_state = TCPS_TIME_WAIT; |
| 1424 | tcp_canceltimers(tp); |
| 1425 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 1426 | soisfdisconnected(so); |
| 1427 | break; |
| 1428 | |
| 1429 | /* |
| 1430 | * In TIME_WAIT state restart the 2 MSL time_wait timer. |
| 1431 | */ |
| 1432 | case TCPS_TIME_WAIT: |
| 1433 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; |
| 1434 | break; |
| 1435 | } |
| 1436 | } |
| 1437 | |
| 1438 | /* |
| 1439 | * If this is a small packet, then ACK now - with Nagel |
| 1440 | * congestion avoidance sender won't send more until |
| 1441 | * he gets an ACK. |
| 1442 | * |
| 1443 | * See above. |
| 1444 | */ |
| 1445 | /* if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) { |
| 1446 | */ |
| 1447 | /* if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg && |
| 1448 | * (so->so_iptos & IPTOS_LOWDELAY) == 0) || |
| 1449 | * ((so->so_iptos & IPTOS_LOWDELAY) && |
| 1450 | * ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) { |
| 1451 | */ |
| 1452 | if (ti->ti_len && (unsigned)ti->ti_len <= 5 && |
| 1453 | ((struct tcpiphdr_2 *)ti)->first_char == (char)27) { |
| 1454 | tp->t_flags |= TF_ACKNOW; |
| 1455 | } |
| 1456 | |
| 1457 | /* |
| 1458 | * Return any desired output. |
| 1459 | */ |
| 1460 | if (needoutput || (tp->t_flags & TF_ACKNOW)) { |
| 1461 | (void) tcp_output(tp); |
| 1462 | } |
| 1463 | return; |
| 1464 | |
| 1465 | dropafterack: |
| 1466 | /* |
| 1467 | * Generate an ACK dropping incoming segment if it occupies |
| 1468 | * sequence space, where the ACK reflects our state. |
| 1469 | */ |
| 1470 | if (tiflags & TH_RST) |
| 1471 | goto drop; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1472 | m_freem(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1473 | tp->t_flags |= TF_ACKNOW; |
| 1474 | (void) tcp_output(tp); |
| 1475 | return; |
| 1476 | |
| 1477 | dropwithreset: |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1478 | /* reuses m if m!=NULL, m_free() unnecessary */ |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1479 | if (tiflags & TH_ACK) |
| 1480 | tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); |
| 1481 | else { |
| 1482 | if (tiflags & TH_SYN) ti->ti_len++; |
| 1483 | tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, |
| 1484 | TH_RST|TH_ACK); |
| 1485 | } |
| 1486 | |
| 1487 | return; |
| 1488 | |
| 1489 | drop: |
| 1490 | /* |
| 1491 | * Drop space held by incoming segment and return. |
| 1492 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1493 | m_free(m); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1494 | |
| 1495 | return; |
| 1496 | } |
| 1497 | |
| 1498 | /* , ts_present, ts_val, ts_ecr) */ |
| 1499 | /* int *ts_present; |
| 1500 | * u_int32_t *ts_val, *ts_ecr; |
| 1501 | */ |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1502 | static void |
| 1503 | tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1504 | { |
| 1505 | u_int16_t mss; |
| 1506 | int opt, optlen; |
| 1507 | |
| 1508 | DEBUG_CALL("tcp_dooptions"); |
| 1509 | DEBUG_ARGS((dfd," tp = %lx cnt=%i \n", (long )tp, cnt)); |
| 1510 | |
| 1511 | for (; cnt > 0; cnt -= optlen, cp += optlen) { |
| 1512 | opt = cp[0]; |
| 1513 | if (opt == TCPOPT_EOL) |
| 1514 | break; |
| 1515 | if (opt == TCPOPT_NOP) |
| 1516 | optlen = 1; |
| 1517 | else { |
| 1518 | optlen = cp[1]; |
| 1519 | if (optlen <= 0) |
| 1520 | break; |
| 1521 | } |
| 1522 | switch (opt) { |
| 1523 | |
| 1524 | default: |
| 1525 | continue; |
| 1526 | |
| 1527 | case TCPOPT_MAXSEG: |
| 1528 | if (optlen != TCPOLEN_MAXSEG) |
| 1529 | continue; |
| 1530 | if (!(ti->ti_flags & TH_SYN)) |
| 1531 | continue; |
| 1532 | memcpy((char *) &mss, (char *) cp + 2, sizeof(mss)); |
| 1533 | NTOHS(mss); |
| 1534 | (void) tcp_mss(tp, mss); /* sets t_maxseg */ |
| 1535 | break; |
| 1536 | |
| 1537 | /* case TCPOPT_WINDOW: |
| 1538 | * if (optlen != TCPOLEN_WINDOW) |
| 1539 | * continue; |
| 1540 | * if (!(ti->ti_flags & TH_SYN)) |
| 1541 | * continue; |
| 1542 | * tp->t_flags |= TF_RCVD_SCALE; |
| 1543 | * tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); |
| 1544 | * break; |
| 1545 | */ |
| 1546 | /* case TCPOPT_TIMESTAMP: |
| 1547 | * if (optlen != TCPOLEN_TIMESTAMP) |
| 1548 | * continue; |
| 1549 | * *ts_present = 1; |
| 1550 | * memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val)); |
| 1551 | * NTOHL(*ts_val); |
| 1552 | * memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr)); |
| 1553 | * NTOHL(*ts_ecr); |
| 1554 | * |
| 1555 | */ /* |
| 1556 | * * A timestamp received in a SYN makes |
| 1557 | * * it ok to send timestamp requests and replies. |
| 1558 | * */ |
| 1559 | /* if (ti->ti_flags & TH_SYN) { |
| 1560 | * tp->t_flags |= TF_RCVD_TSTMP; |
| 1561 | * tp->ts_recent = *ts_val; |
| 1562 | * tp->ts_recent_age = tcp_now; |
| 1563 | * } |
| 1564 | */ break; |
| 1565 | } |
| 1566 | } |
| 1567 | } |
| 1568 | |
| 1569 | |
| 1570 | /* |
| 1571 | * Pull out of band byte out of a segment so |
| 1572 | * it doesn't appear in the user's data queue. |
| 1573 | * It is still reflected in the segment length for |
| 1574 | * sequencing purposes. |
| 1575 | */ |
| 1576 | |
| 1577 | #ifdef notdef |
| 1578 | |
| 1579 | void |
| 1580 | tcp_pulloutofband(so, ti, m) |
| 1581 | struct socket *so; |
| 1582 | struct tcpiphdr *ti; |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1583 | register struct mbuf *m; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1584 | { |
| 1585 | int cnt = ti->ti_urp - 1; |
| 1586 | |
| 1587 | while (cnt >= 0) { |
| 1588 | if (m->m_len > cnt) { |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1589 | char *cp = mtod(m, caddr_t) + cnt; |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1590 | struct tcpcb *tp = sototcpcb(so); |
| 1591 | |
| 1592 | tp->t_iobc = *cp; |
| 1593 | tp->t_oobflags |= TCPOOB_HAVEDATA; |
| 1594 | memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1)); |
| 1595 | m->m_len--; |
| 1596 | return; |
| 1597 | } |
| 1598 | cnt -= m->m_len; |
| 1599 | m = m->m_next; /* XXX WRONG! Fix it! */ |
| 1600 | if (m == 0) |
| 1601 | break; |
| 1602 | } |
| 1603 | panic("tcp_pulloutofband"); |
| 1604 | } |
| 1605 | |
| 1606 | #endif /* notdef */ |
| 1607 | |
| 1608 | /* |
| 1609 | * Collect new round-trip time estimate |
| 1610 | * and update averages and current timeout. |
| 1611 | */ |
| 1612 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1613 | static void |
| 1614 | tcp_xmit_timer(register struct tcpcb *tp, int rtt) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1615 | { |
| 1616 | register short delta; |
| 1617 | |
| 1618 | DEBUG_CALL("tcp_xmit_timer"); |
| 1619 | DEBUG_ARG("tp = %lx", (long)tp); |
| 1620 | DEBUG_ARG("rtt = %d", rtt); |
| 1621 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1622 | STAT(tcpstat.tcps_rttupdated++); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1623 | if (tp->t_srtt != 0) { |
| 1624 | /* |
| 1625 | * srtt is stored as fixed point with 3 bits after the |
| 1626 | * binary point (i.e., scaled by 8). The following magic |
| 1627 | * is equivalent to the smoothing algorithm in rfc793 with |
| 1628 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed |
| 1629 | * point). Adjust rtt to origin 0. |
| 1630 | */ |
| 1631 | delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT); |
| 1632 | if ((tp->t_srtt += delta) <= 0) |
| 1633 | tp->t_srtt = 1; |
| 1634 | /* |
| 1635 | * We accumulate a smoothed rtt variance (actually, a |
| 1636 | * smoothed mean difference), then set the retransmit |
| 1637 | * timer to smoothed rtt + 4 times the smoothed variance. |
| 1638 | * rttvar is stored as fixed point with 2 bits after the |
| 1639 | * binary point (scaled by 4). The following is |
| 1640 | * equivalent to rfc793 smoothing with an alpha of .75 |
| 1641 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces |
| 1642 | * rfc793's wired-in beta. |
| 1643 | */ |
| 1644 | if (delta < 0) |
| 1645 | delta = -delta; |
| 1646 | delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT); |
| 1647 | if ((tp->t_rttvar += delta) <= 0) |
| 1648 | tp->t_rttvar = 1; |
| 1649 | } else { |
| 1650 | /* |
| 1651 | * No rtt measurement yet - use the unsmoothed rtt. |
| 1652 | * Set the variance to half the rtt (so our first |
| 1653 | * retransmit happens at 3*rtt). |
| 1654 | */ |
| 1655 | tp->t_srtt = rtt << TCP_RTT_SHIFT; |
| 1656 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); |
| 1657 | } |
| 1658 | tp->t_rtt = 0; |
| 1659 | tp->t_rxtshift = 0; |
| 1660 | |
| 1661 | /* |
| 1662 | * the retransmit should happen at rtt + 4 * rttvar. |
| 1663 | * Because of the way we do the smoothing, srtt and rttvar |
| 1664 | * will each average +1/2 tick of bias. When we compute |
| 1665 | * the retransmit timer, we want 1/2 tick of rounding and |
| 1666 | * 1 extra tick because of +-1/2 tick uncertainty in the |
| 1667 | * firing of the timer. The bias will give us exactly the |
| 1668 | * 1.5 tick we need. But, because the bias is |
| 1669 | * statistical, we have to test that we don't drop below |
| 1670 | * the minimum feasible timer (which is 2 ticks). |
| 1671 | */ |
| 1672 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), |
| 1673 | (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */ |
| 1674 | |
| 1675 | /* |
| 1676 | * We received an ack for a packet that wasn't retransmitted; |
| 1677 | * it is probably safe to discard any error indications we've |
| 1678 | * received recently. This isn't quite right, but close enough |
| 1679 | * for now (a route might have failed after we sent a segment, |
| 1680 | * and the return path might not be symmetrical). |
| 1681 | */ |
| 1682 | tp->t_softerror = 0; |
| 1683 | } |
| 1684 | |
| 1685 | /* |
| 1686 | * Determine a reasonable value for maxseg size. |
| 1687 | * If the route is known, check route for mtu. |
| 1688 | * If none, use an mss that can be handled on the outgoing |
| 1689 | * interface without forcing IP to fragment; if bigger than |
| 1690 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES |
| 1691 | * to utilize large mbufs. If no route is found, route has no mtu, |
| 1692 | * or the destination isn't local, use a default, hopefully conservative |
| 1693 | * size (usually 512 or the default IP max size, but no more than the mtu |
| 1694 | * of the interface), as we can't discover anything about intervening |
| 1695 | * gateways or networks. We also initialize the congestion/slow start |
| 1696 | * window to be a single segment if the destination isn't local. |
| 1697 | * While looking at the routing entry, we also initialize other path-dependent |
| 1698 | * parameters from pre-set or cached values in the routing entry. |
| 1699 | */ |
| 1700 | |
| 1701 | int |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1702 | tcp_mss(struct tcpcb *tp, u_int offer) |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1703 | { |
| 1704 | struct socket *so = tp->t_socket; |
| 1705 | int mss; |
| 1706 | |
| 1707 | DEBUG_CALL("tcp_mss"); |
| 1708 | DEBUG_ARG("tp = %lx", (long)tp); |
| 1709 | DEBUG_ARG("offer = %d", offer); |
| 1710 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1711 | mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1712 | if (offer) |
| 1713 | mss = min(mss, offer); |
| 1714 | mss = max(mss, 32); |
| 1715 | if (mss < tp->t_maxseg || offer != 0) |
| 1716 | tp->t_maxseg = mss; |
| 1717 | |
| 1718 | tp->snd_cwnd = mss; |
| 1719 | |
David 'Digit' Turner | 5d8f37a | 2009-09-14 14:32:27 -0700 | [diff] [blame^] | 1720 | sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ? |
| 1721 | (mss - (TCP_SNDSPACE % mss)) : |
| 1722 | 0)); |
| 1723 | sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ? |
| 1724 | (mss - (TCP_RCVSPACE % mss)) : |
| 1725 | 0)); |
The Android Open Source Project | 8b23a6c | 2009-03-03 19:30:32 -0800 | [diff] [blame] | 1726 | |
| 1727 | DEBUG_MISC((dfd, " returning mss = %d\n", mss)); |
| 1728 | |
| 1729 | return mss; |
| 1730 | } |