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