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