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