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