sdl: add support for high resolution window icon
[qemu/ar7.git] / slirp / tcp_input.c
blobde5b74a52b17aab24be272d9a7d4830dcdb49b9e
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 "qemu/osdep.h"
42 #include "slirp.h"
43 #include "ip_icmp.h"
45 #define TCPREXMTTHRESH 3
47 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
49 /* for modulo comparisons of timestamps */
50 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
51 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
54 * Insert segment ti into reassembly queue of tcp with
55 * control block tp. Return TH_FIN if reassembly now includes
56 * a segment with FIN. The macro form does the common case inline
57 * (segment is the next to be received on an established connection,
58 * and the queue is empty), avoiding linkage into and removal
59 * from the queue and repetition of various conversions.
60 * Set DELACK for segments received in order, but ack immediately
61 * when segments are out of order (so fast retransmit can work).
63 #define TCP_REASS(tp, ti, m, so, flags) { \
64 if ((ti)->ti_seq == (tp)->rcv_nxt && \
65 tcpfrag_list_empty(tp) && \
66 (tp)->t_state == TCPS_ESTABLISHED) { \
67 tp->t_flags |= TF_DELACK; \
68 (tp)->rcv_nxt += (ti)->ti_len; \
69 flags = (ti)->ti_flags & TH_FIN; \
70 if (so->so_emu) { \
71 if (tcp_emu((so),(m))) sbappend(so, (m)); \
72 } else \
73 sbappend((so), (m)); \
74 } else { \
75 (flags) = tcp_reass((tp), (ti), (m)); \
76 tp->t_flags |= TF_ACKNOW; \
77 } \
80 static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt,
81 struct tcpiphdr *ti);
82 static void tcp_xmit_timer(register struct tcpcb *tp, int rtt);
84 static int
85 tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti,
86 struct mbuf *m)
88 register struct tcpiphdr *q;
89 struct socket *so = tp->t_socket;
90 int flags;
93 * Call with ti==NULL after become established to
94 * force pre-ESTABLISHED data up to user socket.
96 if (ti == NULL)
97 goto present;
100 * Find a segment which begins after this one does.
102 for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
103 q = tcpiphdr_next(q))
104 if (SEQ_GT(q->ti_seq, ti->ti_seq))
105 break;
108 * If there is a preceding segment, it may provide some of
109 * our data already. If so, drop the data from the incoming
110 * segment. If it provides all of our data, drop us.
112 if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
113 register int i;
114 q = tcpiphdr_prev(q);
115 /* conversion to int (in i) handles seq wraparound */
116 i = q->ti_seq + q->ti_len - ti->ti_seq;
117 if (i > 0) {
118 if (i >= ti->ti_len) {
119 m_free(m);
121 * Try to present any queued data
122 * at the left window edge to the user.
123 * This is needed after the 3-WHS
124 * completes.
126 goto present; /* ??? */
128 m_adj(m, i);
129 ti->ti_len -= i;
130 ti->ti_seq += i;
132 q = tcpiphdr_next(q);
134 ti->ti_mbuf = m;
137 * While we overlap succeeding segments trim them or,
138 * if they are completely covered, dequeue them.
140 while (!tcpfrag_list_end(q, tp)) {
141 register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
142 if (i <= 0)
143 break;
144 if (i < q->ti_len) {
145 q->ti_seq += i;
146 q->ti_len -= i;
147 m_adj(q->ti_mbuf, i);
148 break;
150 q = tcpiphdr_next(q);
151 m = tcpiphdr_prev(q)->ti_mbuf;
152 remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
153 m_free(m);
157 * Stick new segment in its place.
159 insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
161 present:
163 * Present data to user, advancing rcv_nxt through
164 * completed sequence space.
166 if (!TCPS_HAVEESTABLISHED(tp->t_state))
167 return (0);
168 ti = tcpfrag_list_first(tp);
169 if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt)
170 return (0);
171 if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
172 return (0);
173 do {
174 tp->rcv_nxt += ti->ti_len;
175 flags = ti->ti_flags & TH_FIN;
176 remque(tcpiphdr2qlink(ti));
177 m = ti->ti_mbuf;
178 ti = tcpiphdr_next(ti);
179 if (so->so_state & SS_FCANTSENDMORE)
180 m_free(m);
181 else {
182 if (so->so_emu) {
183 if (tcp_emu(so,m)) sbappend(so, m);
184 } else
185 sbappend(so, m);
187 } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
188 return (flags);
192 * TCP input routine, follows pages 65-76 of the
193 * protocol specification dated September, 1981 very closely.
195 void
196 tcp_input(struct mbuf *m, int iphlen, struct socket *inso, unsigned short af)
198 struct ip save_ip, *ip;
199 struct ip6 save_ip6, *ip6;
200 register struct tcpiphdr *ti;
201 caddr_t optp = NULL;
202 int optlen = 0;
203 int len, tlen, off;
204 register struct tcpcb *tp = NULL;
205 register int tiflags;
206 struct socket *so = NULL;
207 int todrop, acked, ourfinisacked, needoutput = 0;
208 int iss = 0;
209 u_long tiwin;
210 int ret;
211 struct sockaddr_storage lhost, fhost;
212 struct sockaddr_in *lhost4, *fhost4;
213 struct sockaddr_in6 *lhost6, *fhost6;
214 struct gfwd_list *ex_ptr;
215 Slirp *slirp;
217 DEBUG_CALL("tcp_input");
218 DEBUG_ARG("m = %p iphlen = %2d inso = %p",
219 m, iphlen, inso);
222 * If called with m == 0, then we're continuing the connect
224 if (m == NULL) {
225 so = inso;
226 slirp = so->slirp;
228 /* Re-set a few variables */
229 tp = sototcpcb(so);
230 m = so->so_m;
231 so->so_m = NULL;
232 ti = so->so_ti;
233 tiwin = ti->ti_win;
234 tiflags = ti->ti_flags;
236 goto cont_conn;
238 slirp = m->slirp;
240 ip = mtod(m, struct ip *);
241 ip6 = mtod(m, struct ip6 *);
243 switch (af) {
244 case AF_INET:
245 if (iphlen > sizeof(struct ip)) {
246 ip_stripoptions(m, (struct mbuf *)0);
247 iphlen = sizeof(struct ip);
249 /* XXX Check if too short */
253 * Save a copy of the IP header in case we want restore it
254 * for sending an ICMP error message in response.
256 save_ip = *ip;
257 save_ip.ip_len += iphlen;
260 * Get IP and TCP header together in first mbuf.
261 * Note: IP leaves IP header in first mbuf.
263 m->m_data -= sizeof(struct tcpiphdr) - sizeof(struct ip)
264 - sizeof(struct tcphdr);
265 m->m_len += sizeof(struct tcpiphdr) - sizeof(struct ip)
266 - sizeof(struct tcphdr);
267 ti = mtod(m, struct tcpiphdr *);
270 * Checksum extended TCP header and data.
272 tlen = ip->ip_len;
273 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
274 memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
275 memset(&ti->ti, 0, sizeof(ti->ti));
276 ti->ti_x0 = 0;
277 ti->ti_src = save_ip.ip_src;
278 ti->ti_dst = save_ip.ip_dst;
279 ti->ti_pr = save_ip.ip_p;
280 ti->ti_len = htons((uint16_t)tlen);
281 break;
283 case AF_INET6:
285 * Save a copy of the IP header in case we want restore it
286 * for sending an ICMP error message in response.
288 save_ip6 = *ip6;
290 * Get IP and TCP header together in first mbuf.
291 * Note: IP leaves IP header in first mbuf.
293 m->m_data -= sizeof(struct tcpiphdr) - (sizeof(struct ip6)
294 + sizeof(struct tcphdr));
295 m->m_len += sizeof(struct tcpiphdr) - (sizeof(struct ip6)
296 + sizeof(struct tcphdr));
297 ti = mtod(m, struct tcpiphdr *);
299 tlen = ip6->ip_pl;
300 tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
301 memset(&ti->ih_mbuf, 0 , sizeof(struct mbuf_ptr));
302 memset(&ti->ti, 0, sizeof(ti->ti));
303 ti->ti_x0 = 0;
304 ti->ti_src6 = save_ip6.ip_src;
305 ti->ti_dst6 = save_ip6.ip_dst;
306 ti->ti_nh6 = save_ip6.ip_nh;
307 ti->ti_len = htons((uint16_t)tlen);
308 break;
310 default:
311 g_assert_not_reached();
314 len = ((sizeof(struct tcpiphdr) - sizeof(struct tcphdr)) + tlen);
315 if (cksum(m, len)) {
316 goto drop;
320 * Check that TCP offset makes sense,
321 * pull out TCP options and adjust length. XXX
323 off = ti->ti_off << 2;
324 if (off < sizeof (struct tcphdr) || off > tlen) {
325 goto drop;
327 tlen -= off;
328 ti->ti_len = tlen;
329 if (off > sizeof (struct tcphdr)) {
330 optlen = off - sizeof (struct tcphdr);
331 optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
333 tiflags = ti->ti_flags;
336 * Convert TCP protocol specific fields to host format.
338 NTOHL(ti->ti_seq);
339 NTOHL(ti->ti_ack);
340 NTOHS(ti->ti_win);
341 NTOHS(ti->ti_urp);
344 * Drop TCP, IP headers and TCP options.
346 m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
347 m->m_len -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
350 * Locate pcb for segment.
352 findso:
353 lhost.ss_family = af;
354 fhost.ss_family = af;
355 switch (af) {
356 case AF_INET:
357 lhost4 = (struct sockaddr_in *) &lhost;
358 lhost4->sin_addr = ti->ti_src;
359 lhost4->sin_port = ti->ti_sport;
360 fhost4 = (struct sockaddr_in *) &fhost;
361 fhost4->sin_addr = ti->ti_dst;
362 fhost4->sin_port = ti->ti_dport;
363 break;
364 case AF_INET6:
365 lhost6 = (struct sockaddr_in6 *) &lhost;
366 lhost6->sin6_addr = ti->ti_src6;
367 lhost6->sin6_port = ti->ti_sport;
368 fhost6 = (struct sockaddr_in6 *) &fhost;
369 fhost6->sin6_addr = ti->ti_dst6;
370 fhost6->sin6_port = ti->ti_dport;
371 break;
372 default:
373 g_assert_not_reached();
376 so = solookup(&slirp->tcp_last_so, &slirp->tcb, &lhost, &fhost);
379 * If the state is CLOSED (i.e., TCB does not exist) then
380 * all data in the incoming segment is discarded.
381 * If the TCB exists but is in CLOSED state, it is embryonic,
382 * but should either do a listen or a connect soon.
384 * state == CLOSED means we've done socreate() but haven't
385 * attached it to a protocol yet...
387 * XXX If a TCB does not exist, and the TH_SYN flag is
388 * the only flag set, then create a session, mark it
389 * as if it was LISTENING, and continue...
391 if (so == NULL) {
392 if (slirp->restricted) {
393 /* Any hostfwds will have an existing socket, so we only get here
394 * for non-hostfwd connections. These should be dropped, unless it
395 * happens to be a guestfwd.
397 for (ex_ptr = slirp->guestfwd_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
398 if (ex_ptr->ex_fport == ti->ti_dport &&
399 ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
400 break;
403 if (!ex_ptr) {
404 goto dropwithreset;
408 if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
409 goto dropwithreset;
411 so = socreate(slirp);
412 if (tcp_attach(so) < 0) {
413 g_free(so); /* Not sofree (if it failed, it's not insqued) */
414 goto dropwithreset;
417 sbreserve(&so->so_snd, TCP_SNDSPACE);
418 sbreserve(&so->so_rcv, TCP_RCVSPACE);
420 so->lhost.ss = lhost;
421 so->fhost.ss = fhost;
423 so->so_iptos = tcp_tos(so);
424 if (so->so_iptos == 0) {
425 switch (af) {
426 case AF_INET:
427 so->so_iptos = ((struct ip *)ti)->ip_tos;
428 break;
429 case AF_INET6:
430 break;
431 default:
432 g_assert_not_reached();
436 tp = sototcpcb(so);
437 tp->t_state = TCPS_LISTEN;
441 * If this is a still-connecting socket, this probably
442 * a retransmit of the SYN. Whether it's a retransmit SYN
443 * or something else, we nuke it.
445 if (so->so_state & SS_ISFCONNECTING)
446 goto drop;
448 tp = sototcpcb(so);
450 /* XXX Should never fail */
451 if (tp == NULL)
452 goto dropwithreset;
453 if (tp->t_state == TCPS_CLOSED)
454 goto drop;
456 tiwin = ti->ti_win;
459 * Segment received on connection.
460 * Reset idle time and keep-alive timer.
462 tp->t_idle = 0;
463 if (slirp_do_keepalive)
464 tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
465 else
466 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
469 * Process options if not in LISTEN state,
470 * else do it below (after getting remote address).
472 if (optp && tp->t_state != TCPS_LISTEN)
473 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
476 * Header prediction: check for the two common cases
477 * of a uni-directional data xfer. If the packet has
478 * no control flags, is in-sequence, the window didn't
479 * change and we're not retransmitting, it's a
480 * candidate. If the length is zero and the ack moved
481 * forward, we're the sender side of the xfer. Just
482 * free the data acked & wake any higher level process
483 * that was blocked waiting for space. If the length
484 * is non-zero and the ack didn't move, we're the
485 * receiver side. If we're getting packets in-order
486 * (the reassembly queue is empty), add the data to
487 * the socket buffer and note that we need a delayed ack.
489 * XXX Some of these tests are not needed
490 * eg: the tiwin == tp->snd_wnd prevents many more
491 * predictions.. with no *real* advantage..
493 if (tp->t_state == TCPS_ESTABLISHED &&
494 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
495 ti->ti_seq == tp->rcv_nxt &&
496 tiwin && tiwin == tp->snd_wnd &&
497 tp->snd_nxt == tp->snd_max) {
498 if (ti->ti_len == 0) {
499 if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
500 SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
501 tp->snd_cwnd >= tp->snd_wnd) {
503 * this is a pure ack for outstanding data.
505 if (tp->t_rtt &&
506 SEQ_GT(ti->ti_ack, tp->t_rtseq))
507 tcp_xmit_timer(tp, tp->t_rtt);
508 acked = ti->ti_ack - tp->snd_una;
509 sbdrop(&so->so_snd, acked);
510 tp->snd_una = ti->ti_ack;
511 m_free(m);
514 * If all outstanding data are acked, stop
515 * retransmit timer, otherwise restart timer
516 * using current (possibly backed-off) value.
517 * If process is waiting for space,
518 * wakeup/selwakeup/signal. If data
519 * are ready to send, let tcp_output
520 * decide between more output or persist.
522 if (tp->snd_una == tp->snd_max)
523 tp->t_timer[TCPT_REXMT] = 0;
524 else if (tp->t_timer[TCPT_PERSIST] == 0)
525 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
528 * This is called because sowwakeup might have
529 * put data into so_snd. Since we don't so sowwakeup,
530 * we don't need this.. XXX???
532 if (so->so_snd.sb_cc)
533 (void) tcp_output(tp);
535 return;
537 } else if (ti->ti_ack == tp->snd_una &&
538 tcpfrag_list_empty(tp) &&
539 ti->ti_len <= sbspace(&so->so_rcv)) {
541 * this is a pure, in-sequence data packet
542 * with nothing on the reassembly queue and
543 * we have enough buffer space to take it.
545 tp->rcv_nxt += ti->ti_len;
547 * Add data to socket buffer.
549 if (so->so_emu) {
550 if (tcp_emu(so,m)) sbappend(so, m);
551 } else
552 sbappend(so, m);
555 * If this is a short packet, then ACK now - with Nagel
556 * congestion avoidance sender won't send more until
557 * he gets an ACK.
559 * It is better to not delay acks at all to maximize
560 * TCP throughput. See RFC 2581.
562 tp->t_flags |= TF_ACKNOW;
563 tcp_output(tp);
564 return;
566 } /* header prediction */
568 * Calculate amount of space in receive window,
569 * and then do TCP input processing.
570 * Receive window is amount of space in rcv queue,
571 * but not less than advertised window.
573 { int win;
574 win = sbspace(&so->so_rcv);
575 if (win < 0)
576 win = 0;
577 tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt));
580 switch (tp->t_state) {
583 * If the state is LISTEN then ignore segment if it contains an RST.
584 * If the segment contains an ACK then it is bad and send a RST.
585 * If it does not contain a SYN then it is not interesting; drop it.
586 * Don't bother responding if the destination was a broadcast.
587 * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
588 * tp->iss, and send a segment:
589 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
590 * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
591 * Fill in remote peer address fields if not previously specified.
592 * Enter SYN_RECEIVED state, and process any other fields of this
593 * segment in this state.
595 case TCPS_LISTEN: {
597 if (tiflags & TH_RST)
598 goto drop;
599 if (tiflags & TH_ACK)
600 goto dropwithreset;
601 if ((tiflags & TH_SYN) == 0)
602 goto drop;
605 * This has way too many gotos...
606 * But a bit of spaghetti code never hurt anybody :)
610 * If this is destined for the control address, then flag to
611 * tcp_ctl once connected, otherwise connect
613 if (af == AF_INET &&
614 (so->so_faddr.s_addr & slirp->vnetwork_mask.s_addr) ==
615 slirp->vnetwork_addr.s_addr) {
616 if (so->so_faddr.s_addr != slirp->vhost_addr.s_addr &&
617 so->so_faddr.s_addr != slirp->vnameserver_addr.s_addr) {
618 /* May be an add exec */
619 for (ex_ptr = slirp->guestfwd_list; ex_ptr;
620 ex_ptr = ex_ptr->ex_next) {
621 if(ex_ptr->ex_fport == so->so_fport &&
622 so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
623 so->so_state |= SS_CTL;
624 break;
627 if (so->so_state & SS_CTL) {
628 goto cont_input;
631 /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
634 if (so->so_emu & EMU_NOCONNECT) {
635 so->so_emu &= ~EMU_NOCONNECT;
636 goto cont_input;
639 if ((tcp_fconnect(so, so->so_ffamily) == -1) &&
640 (errno != EAGAIN) &&
641 (errno != EINPROGRESS) && (errno != EWOULDBLOCK)
643 uint8_t code;
644 DEBUG_MISC(" tcp fconnect errno = %d-%s", errno, strerror(errno));
645 if(errno == ECONNREFUSED) {
646 /* ACK the SYN, send RST to refuse the connection */
647 tcp_respond(tp, ti, m, ti->ti_seq + 1, (tcp_seq) 0,
648 TH_RST | TH_ACK, af);
649 } else {
650 switch (af) {
651 case AF_INET:
652 code = ICMP_UNREACH_NET;
653 if (errno == EHOSTUNREACH) {
654 code = ICMP_UNREACH_HOST;
656 break;
657 case AF_INET6:
658 code = ICMP6_UNREACH_NO_ROUTE;
659 if (errno == EHOSTUNREACH) {
660 code = ICMP6_UNREACH_ADDRESS;
662 break;
663 default:
664 g_assert_not_reached();
666 HTONL(ti->ti_seq); /* restore tcp header */
667 HTONL(ti->ti_ack);
668 HTONS(ti->ti_win);
669 HTONS(ti->ti_urp);
670 m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
671 m->m_len += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
672 switch (af) {
673 case AF_INET:
674 m->m_data += sizeof(struct tcpiphdr) - sizeof(struct ip)
675 - sizeof(struct tcphdr);
676 m->m_len -= sizeof(struct tcpiphdr) - sizeof(struct ip)
677 - sizeof(struct tcphdr);
678 *ip = save_ip;
679 icmp_send_error(m, ICMP_UNREACH, code, 0, strerror(errno));
680 break;
681 case AF_INET6:
682 m->m_data += sizeof(struct tcpiphdr) - (sizeof(struct ip6)
683 + sizeof(struct tcphdr));
684 m->m_len -= sizeof(struct tcpiphdr) - (sizeof(struct ip6)
685 + sizeof(struct tcphdr));
686 *ip6 = save_ip6;
687 icmp6_send_error(m, ICMP6_UNREACH, code);
688 break;
689 default:
690 g_assert_not_reached();
693 tcp_close(tp);
694 m_free(m);
695 } else {
697 * Haven't connected yet, save the current mbuf
698 * and ti, and return
699 * XXX Some OS's don't tell us whether the connect()
700 * succeeded or not. So we must time it out.
702 so->so_m = m;
703 so->so_ti = ti;
704 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
705 tp->t_state = TCPS_SYN_RECEIVED;
707 * Initialize receive sequence numbers now so that we can send a
708 * valid RST if the remote end rejects our connection.
710 tp->irs = ti->ti_seq;
711 tcp_rcvseqinit(tp);
712 tcp_template(tp);
714 return;
716 cont_conn:
717 /* m==NULL
718 * Check if the connect succeeded
720 if (so->so_state & SS_NOFDREF) {
721 tp = tcp_close(tp);
722 goto dropwithreset;
724 cont_input:
725 tcp_template(tp);
727 if (optp)
728 tcp_dooptions(tp, (u_char *)optp, optlen, ti);
730 if (iss)
731 tp->iss = iss;
732 else
733 tp->iss = slirp->tcp_iss;
734 slirp->tcp_iss += TCP_ISSINCR/2;
735 tp->irs = ti->ti_seq;
736 tcp_sendseqinit(tp);
737 tcp_rcvseqinit(tp);
738 tp->t_flags |= TF_ACKNOW;
739 tp->t_state = TCPS_SYN_RECEIVED;
740 tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
741 goto trimthenstep6;
742 } /* case TCPS_LISTEN */
745 * If the state is SYN_SENT:
746 * if seg contains an ACK, but not for our SYN, drop the input.
747 * if seg contains a RST, then drop the connection.
748 * if seg does not contain SYN, then drop it.
749 * Otherwise this is an acceptable SYN segment
750 * initialize tp->rcv_nxt and tp->irs
751 * if seg contains ack then advance tp->snd_una
752 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
753 * arrange for segment to be acked (eventually)
754 * continue processing rest of data/controls, beginning with URG
756 case TCPS_SYN_SENT:
757 if ((tiflags & TH_ACK) &&
758 (SEQ_LEQ(ti->ti_ack, tp->iss) ||
759 SEQ_GT(ti->ti_ack, tp->snd_max)))
760 goto dropwithreset;
762 if (tiflags & TH_RST) {
763 if (tiflags & TH_ACK) {
764 tcp_drop(tp, 0); /* XXX Check t_softerror! */
766 goto drop;
769 if ((tiflags & TH_SYN) == 0)
770 goto drop;
771 if (tiflags & TH_ACK) {
772 tp->snd_una = ti->ti_ack;
773 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
774 tp->snd_nxt = tp->snd_una;
777 tp->t_timer[TCPT_REXMT] = 0;
778 tp->irs = ti->ti_seq;
779 tcp_rcvseqinit(tp);
780 tp->t_flags |= TF_ACKNOW;
781 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
782 soisfconnected(so);
783 tp->t_state = TCPS_ESTABLISHED;
785 (void) tcp_reass(tp, (struct tcpiphdr *)0,
786 (struct mbuf *)0);
788 * if we didn't have to retransmit the SYN,
789 * use its rtt as our initial srtt & rtt var.
791 if (tp->t_rtt)
792 tcp_xmit_timer(tp, tp->t_rtt);
793 } else
794 tp->t_state = TCPS_SYN_RECEIVED;
796 trimthenstep6:
798 * Advance ti->ti_seq to correspond to first data byte.
799 * If data, trim to stay within window,
800 * dropping FIN if necessary.
802 ti->ti_seq++;
803 if (ti->ti_len > tp->rcv_wnd) {
804 todrop = ti->ti_len - tp->rcv_wnd;
805 m_adj(m, -todrop);
806 ti->ti_len = tp->rcv_wnd;
807 tiflags &= ~TH_FIN;
809 tp->snd_wl1 = ti->ti_seq - 1;
810 tp->rcv_up = ti->ti_seq;
811 goto step6;
812 } /* switch tp->t_state */
814 * States other than LISTEN or SYN_SENT.
815 * Check that at least some bytes of segment are within
816 * receive window. If segment begins before rcv_nxt,
817 * drop leading data (and SYN); if nothing left, just ack.
819 todrop = tp->rcv_nxt - ti->ti_seq;
820 if (todrop > 0) {
821 if (tiflags & TH_SYN) {
822 tiflags &= ~TH_SYN;
823 ti->ti_seq++;
824 if (ti->ti_urp > 1)
825 ti->ti_urp--;
826 else
827 tiflags &= ~TH_URG;
828 todrop--;
831 * Following if statement from Stevens, vol. 2, p. 960.
833 if (todrop > ti->ti_len
834 || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
836 * Any valid FIN must be to the left of the window.
837 * At this point the FIN must be a duplicate or out
838 * of sequence; drop it.
840 tiflags &= ~TH_FIN;
843 * Send an ACK to resynchronize and drop any data.
844 * But keep on processing for RST or ACK.
846 tp->t_flags |= TF_ACKNOW;
847 todrop = ti->ti_len;
849 m_adj(m, todrop);
850 ti->ti_seq += todrop;
851 ti->ti_len -= todrop;
852 if (ti->ti_urp > todrop)
853 ti->ti_urp -= todrop;
854 else {
855 tiflags &= ~TH_URG;
856 ti->ti_urp = 0;
860 * If new data are received on a connection after the
861 * user processes are gone, then RST the other end.
863 if ((so->so_state & SS_NOFDREF) &&
864 tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
865 tp = tcp_close(tp);
866 goto dropwithreset;
870 * If segment ends after window, drop trailing data
871 * (and PUSH and FIN); if nothing left, just ACK.
873 todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
874 if (todrop > 0) {
875 if (todrop >= ti->ti_len) {
877 * If a new connection request is received
878 * while in TIME_WAIT, drop the old connection
879 * and start over if the sequence numbers
880 * are above the previous ones.
882 if (tiflags & TH_SYN &&
883 tp->t_state == TCPS_TIME_WAIT &&
884 SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
885 iss = tp->rcv_nxt + TCP_ISSINCR;
886 tp = tcp_close(tp);
887 goto findso;
890 * If window is closed can only take segments at
891 * window edge, and have to drop data and PUSH from
892 * incoming segments. Continue processing, but
893 * remember to ack. Otherwise, drop segment
894 * and ack.
896 if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
897 tp->t_flags |= TF_ACKNOW;
898 } else {
899 goto dropafterack;
902 m_adj(m, -todrop);
903 ti->ti_len -= todrop;
904 tiflags &= ~(TH_PUSH|TH_FIN);
908 * If the RST bit is set examine the state:
909 * SYN_RECEIVED STATE:
910 * If passive open, return to LISTEN state.
911 * If active open, inform user that connection was refused.
912 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
913 * Inform user that connection was reset, and close tcb.
914 * CLOSING, LAST_ACK, TIME_WAIT STATES
915 * Close the tcb.
917 if (tiflags&TH_RST) switch (tp->t_state) {
919 case TCPS_SYN_RECEIVED:
920 case TCPS_ESTABLISHED:
921 case TCPS_FIN_WAIT_1:
922 case TCPS_FIN_WAIT_2:
923 case TCPS_CLOSE_WAIT:
924 tp->t_state = TCPS_CLOSED;
925 tcp_close(tp);
926 goto drop;
928 case TCPS_CLOSING:
929 case TCPS_LAST_ACK:
930 case TCPS_TIME_WAIT:
931 tcp_close(tp);
932 goto drop;
936 * If a SYN is in the window, then this is an
937 * error and we send an RST and drop the connection.
939 if (tiflags & TH_SYN) {
940 tp = tcp_drop(tp,0);
941 goto dropwithreset;
945 * If the ACK bit is off we drop the segment and return.
947 if ((tiflags & TH_ACK) == 0) goto drop;
950 * Ack processing.
952 switch (tp->t_state) {
954 * In SYN_RECEIVED state if the ack ACKs our SYN then enter
955 * ESTABLISHED state and continue processing, otherwise
956 * send an RST. una<=ack<=max
958 case TCPS_SYN_RECEIVED:
960 if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
961 SEQ_GT(ti->ti_ack, tp->snd_max))
962 goto dropwithreset;
963 tp->t_state = TCPS_ESTABLISHED;
965 * The sent SYN is ack'ed with our sequence number +1
966 * The first data byte already in the buffer will get
967 * lost if no correction is made. This is only needed for
968 * SS_CTL since the buffer is empty otherwise.
969 * tp->snd_una++; or:
971 tp->snd_una=ti->ti_ack;
972 if (so->so_state & SS_CTL) {
973 /* So tcp_ctl reports the right state */
974 ret = tcp_ctl(so);
975 if (ret == 1) {
976 soisfconnected(so);
977 so->so_state &= ~SS_CTL; /* success XXX */
978 } else if (ret == 2) {
979 so->so_state &= SS_PERSISTENT_MASK;
980 so->so_state |= SS_NOFDREF; /* CTL_CMD */
981 } else {
982 needoutput = 1;
983 tp->t_state = TCPS_FIN_WAIT_1;
985 } else {
986 soisfconnected(so);
989 (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
990 tp->snd_wl1 = ti->ti_seq - 1;
991 /* Avoid ack processing; snd_una==ti_ack => dup ack */
992 goto synrx_to_est;
993 /* fall into ... */
996 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
997 * ACKs. If the ack is in the range
998 * tp->snd_una < ti->ti_ack <= tp->snd_max
999 * then advance tp->snd_una to ti->ti_ack and drop
1000 * data from the retransmission queue. If this ACK reflects
1001 * more up to date window information we update our window information.
1003 case TCPS_ESTABLISHED:
1004 case TCPS_FIN_WAIT_1:
1005 case TCPS_FIN_WAIT_2:
1006 case TCPS_CLOSE_WAIT:
1007 case TCPS_CLOSING:
1008 case TCPS_LAST_ACK:
1009 case TCPS_TIME_WAIT:
1011 if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1012 if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1013 DEBUG_MISC(" dup ack m = %p so = %p", m, so);
1015 * If we have outstanding data (other than
1016 * a window probe), this is a completely
1017 * duplicate ack (ie, window info didn't
1018 * change), the ack is the biggest we've
1019 * seen and we've seen exactly our rexmt
1020 * threshold of them, assume a packet
1021 * has been dropped and retransmit it.
1022 * Kludge snd_nxt & the congestion
1023 * window so we send only this one
1024 * packet.
1026 * We know we're losing at the current
1027 * window size so do congestion avoidance
1028 * (set ssthresh to half the current window
1029 * and pull our congestion window back to
1030 * the new ssthresh).
1032 * Dup acks mean that packets have left the
1033 * network (they're now cached at the receiver)
1034 * so bump cwnd by the amount in the receiver
1035 * to keep a constant cwnd packets in the
1036 * network.
1038 if (tp->t_timer[TCPT_REXMT] == 0 ||
1039 ti->ti_ack != tp->snd_una)
1040 tp->t_dupacks = 0;
1041 else if (++tp->t_dupacks == TCPREXMTTHRESH) {
1042 tcp_seq onxt = tp->snd_nxt;
1043 u_int win =
1044 MIN(tp->snd_wnd, tp->snd_cwnd) /
1045 2 / tp->t_maxseg;
1047 if (win < 2)
1048 win = 2;
1049 tp->snd_ssthresh = win * tp->t_maxseg;
1050 tp->t_timer[TCPT_REXMT] = 0;
1051 tp->t_rtt = 0;
1052 tp->snd_nxt = ti->ti_ack;
1053 tp->snd_cwnd = tp->t_maxseg;
1054 (void) tcp_output(tp);
1055 tp->snd_cwnd = tp->snd_ssthresh +
1056 tp->t_maxseg * tp->t_dupacks;
1057 if (SEQ_GT(onxt, tp->snd_nxt))
1058 tp->snd_nxt = onxt;
1059 goto drop;
1060 } else if (tp->t_dupacks > TCPREXMTTHRESH) {
1061 tp->snd_cwnd += tp->t_maxseg;
1062 (void) tcp_output(tp);
1063 goto drop;
1065 } else
1066 tp->t_dupacks = 0;
1067 break;
1069 synrx_to_est:
1071 * If the congestion window was inflated to account
1072 * for the other side's cached packets, retract it.
1074 if (tp->t_dupacks > TCPREXMTTHRESH &&
1075 tp->snd_cwnd > tp->snd_ssthresh)
1076 tp->snd_cwnd = tp->snd_ssthresh;
1077 tp->t_dupacks = 0;
1078 if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1079 goto dropafterack;
1081 acked = ti->ti_ack - tp->snd_una;
1084 * If transmit timer is running and timed sequence
1085 * number was acked, update smoothed round trip time.
1086 * Since we now have an rtt measurement, cancel the
1087 * timer backoff (cf., Phil Karn's retransmit alg.).
1088 * Recompute the initial retransmit timer.
1090 if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1091 tcp_xmit_timer(tp,tp->t_rtt);
1094 * If all outstanding data is acked, stop retransmit
1095 * timer and remember to restart (more output or persist).
1096 * If there is more data to be acked, restart retransmit
1097 * timer, using current (possibly backed-off) value.
1099 if (ti->ti_ack == tp->snd_max) {
1100 tp->t_timer[TCPT_REXMT] = 0;
1101 needoutput = 1;
1102 } else if (tp->t_timer[TCPT_PERSIST] == 0)
1103 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1105 * When new data is acked, open the congestion window.
1106 * If the window gives us less than ssthresh packets
1107 * in flight, open exponentially (maxseg per packet).
1108 * Otherwise open linearly: maxseg per window
1109 * (maxseg^2 / cwnd per packet).
1112 register u_int cw = tp->snd_cwnd;
1113 register u_int incr = tp->t_maxseg;
1115 if (cw > tp->snd_ssthresh)
1116 incr = incr * incr / cw;
1117 tp->snd_cwnd = MIN(cw + incr, TCP_MAXWIN << tp->snd_scale);
1119 if (acked > so->so_snd.sb_cc) {
1120 tp->snd_wnd -= so->so_snd.sb_cc;
1121 sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
1122 ourfinisacked = 1;
1123 } else {
1124 sbdrop(&so->so_snd, acked);
1125 tp->snd_wnd -= acked;
1126 ourfinisacked = 0;
1128 tp->snd_una = ti->ti_ack;
1129 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1130 tp->snd_nxt = tp->snd_una;
1132 switch (tp->t_state) {
1135 * In FIN_WAIT_1 STATE in addition to the processing
1136 * for the ESTABLISHED state if our FIN is now acknowledged
1137 * then enter FIN_WAIT_2.
1139 case TCPS_FIN_WAIT_1:
1140 if (ourfinisacked) {
1142 * If we can't receive any more
1143 * data, then closing user can proceed.
1144 * Starting the timer is contrary to the
1145 * specification, but if we don't get a FIN
1146 * we'll hang forever.
1148 if (so->so_state & SS_FCANTRCVMORE) {
1149 tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
1151 tp->t_state = TCPS_FIN_WAIT_2;
1153 break;
1156 * In CLOSING STATE in addition to the processing for
1157 * the ESTABLISHED state if the ACK acknowledges our FIN
1158 * then enter the TIME-WAIT state, otherwise ignore
1159 * the segment.
1161 case TCPS_CLOSING:
1162 if (ourfinisacked) {
1163 tp->t_state = TCPS_TIME_WAIT;
1164 tcp_canceltimers(tp);
1165 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1167 break;
1170 * In LAST_ACK, we may still be waiting for data to drain
1171 * and/or to be acked, as well as for the ack of our FIN.
1172 * If our FIN is now acknowledged, delete the TCB,
1173 * enter the closed state and return.
1175 case TCPS_LAST_ACK:
1176 if (ourfinisacked) {
1177 tcp_close(tp);
1178 goto drop;
1180 break;
1183 * In TIME_WAIT state the only thing that should arrive
1184 * is a retransmission of the remote FIN. Acknowledge
1185 * it and restart the finack timer.
1187 case TCPS_TIME_WAIT:
1188 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1189 goto dropafterack;
1191 } /* switch(tp->t_state) */
1193 step6:
1195 * Update window information.
1196 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1198 if ((tiflags & TH_ACK) &&
1199 (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1200 (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1201 (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1202 tp->snd_wnd = tiwin;
1203 tp->snd_wl1 = ti->ti_seq;
1204 tp->snd_wl2 = ti->ti_ack;
1205 if (tp->snd_wnd > tp->max_sndwnd)
1206 tp->max_sndwnd = tp->snd_wnd;
1207 needoutput = 1;
1211 * Process segments with URG.
1213 if ((tiflags & TH_URG) && ti->ti_urp &&
1214 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1216 * This is a kludge, but if we receive and accept
1217 * random urgent pointers, we'll crash in
1218 * soreceive. It's hard to imagine someone
1219 * actually wanting to send this much urgent data.
1221 if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1222 ti->ti_urp = 0;
1223 tiflags &= ~TH_URG;
1224 goto dodata;
1227 * If this segment advances the known urgent pointer,
1228 * then mark the data stream. This should not happen
1229 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1230 * a FIN has been received from the remote side.
1231 * In these states we ignore the URG.
1233 * According to RFC961 (Assigned Protocols),
1234 * the urgent pointer points to the last octet
1235 * of urgent data. We continue, however,
1236 * to consider it to indicate the first octet
1237 * of data past the urgent section as the original
1238 * spec states (in one of two places).
1240 if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1241 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1242 so->so_urgc = so->so_rcv.sb_cc +
1243 (tp->rcv_up - tp->rcv_nxt); /* -1; */
1244 tp->rcv_up = ti->ti_seq + ti->ti_urp;
1247 } else
1249 * If no out of band data is expected,
1250 * pull receive urgent pointer along
1251 * with the receive window.
1253 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1254 tp->rcv_up = tp->rcv_nxt;
1255 dodata:
1258 * If this is a small packet, then ACK now - with Nagel
1259 * congestion avoidance sender won't send more until
1260 * he gets an ACK.
1262 if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1263 ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1264 tp->t_flags |= TF_ACKNOW;
1268 * Process the segment text, merging it into the TCP sequencing queue,
1269 * and arranging for acknowledgment of receipt if necessary.
1270 * This process logically involves adjusting tp->rcv_wnd as data
1271 * is presented to the user (this happens in tcp_usrreq.c,
1272 * case PRU_RCVD). If a FIN has already been received on this
1273 * connection then we just ignore the text.
1275 if ((ti->ti_len || (tiflags&TH_FIN)) &&
1276 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1277 TCP_REASS(tp, ti, m, so, tiflags);
1278 } else {
1279 m_free(m);
1280 tiflags &= ~TH_FIN;
1284 * If FIN is received ACK the FIN and let the user know
1285 * that the connection is closing.
1287 if (tiflags & TH_FIN) {
1288 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1290 * If we receive a FIN we can't send more data,
1291 * set it SS_FDRAIN
1292 * Shutdown the socket if there is no rx data in the
1293 * buffer.
1294 * soread() is called on completion of shutdown() and
1295 * will got to TCPS_LAST_ACK, and use tcp_output()
1296 * to send the FIN.
1298 sofwdrain(so);
1300 tp->t_flags |= TF_ACKNOW;
1301 tp->rcv_nxt++;
1303 switch (tp->t_state) {
1306 * In SYN_RECEIVED and ESTABLISHED STATES
1307 * enter the CLOSE_WAIT state.
1309 case TCPS_SYN_RECEIVED:
1310 case TCPS_ESTABLISHED:
1311 if(so->so_emu == EMU_CTL) /* no shutdown on socket */
1312 tp->t_state = TCPS_LAST_ACK;
1313 else
1314 tp->t_state = TCPS_CLOSE_WAIT;
1315 break;
1318 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1319 * enter the CLOSING state.
1321 case TCPS_FIN_WAIT_1:
1322 tp->t_state = TCPS_CLOSING;
1323 break;
1326 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1327 * starting the time-wait timer, turning off the other
1328 * standard timers.
1330 case TCPS_FIN_WAIT_2:
1331 tp->t_state = TCPS_TIME_WAIT;
1332 tcp_canceltimers(tp);
1333 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1334 break;
1337 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1339 case TCPS_TIME_WAIT:
1340 tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1341 break;
1346 * Return any desired output.
1348 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1349 (void) tcp_output(tp);
1351 return;
1353 dropafterack:
1355 * Generate an ACK dropping incoming segment if it occupies
1356 * sequence space, where the ACK reflects our state.
1358 if (tiflags & TH_RST)
1359 goto drop;
1360 m_free(m);
1361 tp->t_flags |= TF_ACKNOW;
1362 (void) tcp_output(tp);
1363 return;
1365 dropwithreset:
1366 /* reuses m if m!=NULL, m_free() unnecessary */
1367 if (tiflags & TH_ACK)
1368 tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST, af);
1369 else {
1370 if (tiflags & TH_SYN) ti->ti_len++;
1371 tcp_respond(tp, ti, m, ti->ti_seq + ti->ti_len, (tcp_seq) 0,
1372 TH_RST | TH_ACK, af);
1375 return;
1377 drop:
1379 * Drop space held by incoming segment and return.
1381 m_free(m);
1384 static void
1385 tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1387 uint16_t mss;
1388 int opt, optlen;
1390 DEBUG_CALL("tcp_dooptions");
1391 DEBUG_ARG("tp = %p cnt=%i", tp, cnt);
1393 for (; cnt > 0; cnt -= optlen, cp += optlen) {
1394 opt = cp[0];
1395 if (opt == TCPOPT_EOL)
1396 break;
1397 if (opt == TCPOPT_NOP)
1398 optlen = 1;
1399 else {
1400 optlen = cp[1];
1401 if (optlen <= 0)
1402 break;
1404 switch (opt) {
1406 default:
1407 continue;
1409 case TCPOPT_MAXSEG:
1410 if (optlen != TCPOLEN_MAXSEG)
1411 continue;
1412 if (!(ti->ti_flags & TH_SYN))
1413 continue;
1414 memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1415 NTOHS(mss);
1416 (void) tcp_mss(tp, mss); /* sets t_maxseg */
1417 break;
1423 * Collect new round-trip time estimate
1424 * and update averages and current timeout.
1427 static void
1428 tcp_xmit_timer(register struct tcpcb *tp, int rtt)
1430 register short delta;
1432 DEBUG_CALL("tcp_xmit_timer");
1433 DEBUG_ARG("tp = %p", tp);
1434 DEBUG_ARG("rtt = %d", rtt);
1436 if (tp->t_srtt != 0) {
1438 * srtt is stored as fixed point with 3 bits after the
1439 * binary point (i.e., scaled by 8). The following magic
1440 * is equivalent to the smoothing algorithm in rfc793 with
1441 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1442 * point). Adjust rtt to origin 0.
1444 delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1445 if ((tp->t_srtt += delta) <= 0)
1446 tp->t_srtt = 1;
1448 * We accumulate a smoothed rtt variance (actually, a
1449 * smoothed mean difference), then set the retransmit
1450 * timer to smoothed rtt + 4 times the smoothed variance.
1451 * rttvar is stored as fixed point with 2 bits after the
1452 * binary point (scaled by 4). The following is
1453 * equivalent to rfc793 smoothing with an alpha of .75
1454 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
1455 * rfc793's wired-in beta.
1457 if (delta < 0)
1458 delta = -delta;
1459 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1460 if ((tp->t_rttvar += delta) <= 0)
1461 tp->t_rttvar = 1;
1462 } else {
1464 * No rtt measurement yet - use the unsmoothed rtt.
1465 * Set the variance to half the rtt (so our first
1466 * retransmit happens at 3*rtt).
1468 tp->t_srtt = rtt << TCP_RTT_SHIFT;
1469 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1471 tp->t_rtt = 0;
1472 tp->t_rxtshift = 0;
1475 * the retransmit should happen at rtt + 4 * rttvar.
1476 * Because of the way we do the smoothing, srtt and rttvar
1477 * will each average +1/2 tick of bias. When we compute
1478 * the retransmit timer, we want 1/2 tick of rounding and
1479 * 1 extra tick because of +-1/2 tick uncertainty in the
1480 * firing of the timer. The bias will give us exactly the
1481 * 1.5 tick we need. But, because the bias is
1482 * statistical, we have to test that we don't drop below
1483 * the minimum feasible timer (which is 2 ticks).
1485 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1486 (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1489 * We received an ack for a packet that wasn't retransmitted;
1490 * it is probably safe to discard any error indications we've
1491 * received recently. This isn't quite right, but close enough
1492 * for now (a route might have failed after we sent a segment,
1493 * and the return path might not be symmetrical).
1495 tp->t_softerror = 0;
1499 * Determine a reasonable value for maxseg size.
1500 * If the route is known, check route for mtu.
1501 * If none, use an mss that can be handled on the outgoing
1502 * interface without forcing IP to fragment; if bigger than
1503 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1504 * to utilize large mbufs. If no route is found, route has no mtu,
1505 * or the destination isn't local, use a default, hopefully conservative
1506 * size (usually 512 or the default IP max size, but no more than the mtu
1507 * of the interface), as we can't discover anything about intervening
1508 * gateways or networks. We also initialize the congestion/slow start
1509 * window to be a single segment if the destination isn't local.
1510 * While looking at the routing entry, we also initialize other path-dependent
1511 * parameters from pre-set or cached values in the routing entry.
1515 tcp_mss(struct tcpcb *tp, u_int offer)
1517 struct socket *so = tp->t_socket;
1518 int mss;
1520 DEBUG_CALL("tcp_mss");
1521 DEBUG_ARG("tp = %p", tp);
1522 DEBUG_ARG("offer = %d", offer);
1524 switch (so->so_ffamily) {
1525 case AF_INET:
1526 mss = MIN(IF_MTU, IF_MRU) - sizeof(struct tcphdr)
1527 - sizeof(struct ip);
1528 break;
1529 case AF_INET6:
1530 mss = MIN(IF_MTU, IF_MRU) - sizeof(struct tcphdr)
1531 - sizeof(struct ip6);
1532 break;
1533 default:
1534 g_assert_not_reached();
1537 if (offer)
1538 mss = MIN(mss, offer);
1539 mss = MAX(mss, 32);
1540 if (mss < tp->t_maxseg || offer != 0)
1541 tp->t_maxseg = mss;
1543 tp->snd_cwnd = mss;
1545 sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1546 (mss - (TCP_SNDSPACE % mss)) :
1547 0));
1548 sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1549 (mss - (TCP_RCVSPACE % mss)) :
1550 0));
1552 DEBUG_MISC(" returning mss = %d", mss);
1554 return mss;