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