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