123190Smckusick /* 233745Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332787Sbostic * All rights reserved. 423190Smckusick * 532787Sbostic * Redistribution and use in source and binary forms are permitted 632787Sbostic * provided that this notice is preserved and that due credit is given 732787Sbostic * to the University of California at Berkeley. The name of the University 832787Sbostic * may not be used to endorse or promote products derived from this 932787Sbostic * software without specific prior written permission. This software 1032787Sbostic * is provided ``as is'' without express or implied warranty. 1132787Sbostic * 12*34278Skarels * @(#)tcp_input.c 7.18 (Berkeley) 05/14/88 1323190Smckusick */ 144601Swnj 1517062Sbloom #include "param.h" 1617062Sbloom #include "systm.h" 1717062Sbloom #include "mbuf.h" 1817062Sbloom #include "protosw.h" 1917062Sbloom #include "socket.h" 2017062Sbloom #include "socketvar.h" 2117062Sbloom #include "errno.h" 2210894Ssam 2310894Ssam #include "../net/if.h" 2410894Ssam #include "../net/route.h" 2510894Ssam 2617062Sbloom #include "in.h" 2717062Sbloom #include "in_pcb.h" 2817062Sbloom #include "in_systm.h" 2917062Sbloom #include "ip.h" 3017062Sbloom #include "ip_var.h" 3117062Sbloom #include "tcp.h" 3217062Sbloom #include "tcp_fsm.h" 3317062Sbloom #include "tcp_seq.h" 3417062Sbloom #include "tcp_timer.h" 3517062Sbloom #include "tcp_var.h" 3617062Sbloom #include "tcpip.h" 3717062Sbloom #include "tcp_debug.h" 384601Swnj 395300Sroot int tcpprintfs = 0; 404679Swnj int tcpcksum = 1; 4132098Skarels int tcprexmtthresh = 3; 425267Sroot struct tcpiphdr tcp_saveti; 434601Swnj 445267Sroot struct tcpcb *tcp_newtcpcb(); 4524816Skarels 465065Swnj /* 4724816Skarels * Insert segment ti into reassembly queue of tcp with 4824816Skarels * control block tp. Return TH_FIN if reassembly now includes 4924816Skarels * a segment with FIN. The macro form does the common case inline 5024816Skarels * (segment is the next to be received on an established connection, 5124816Skarels * and the queue is empty), avoiding linkage into and removal 5224816Skarels * from the queue and repetition of various conversions. 53*34278Skarels * Set DELACK for segments received in order, but ack immediately 54*34278Skarels * when segments are out of order (so fast retransmit can work). 5524816Skarels */ 5624816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 5724816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5824816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5924816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 60*34278Skarels tp->t_flags |= TF_DELACK; \ 6124816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6224816Skarels flags = (ti)->ti_flags & TH_FIN; \ 6330525Skarels tcpstat.tcps_rcvpack++;\ 6430525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 6524816Skarels sbappend(&(so)->so_rcv, (m)); \ 6624816Skarels sorwakeup(so); \ 67*34278Skarels } else { \ 6824816Skarels (flags) = tcp_reass((tp), (ti)); \ 69*34278Skarels tp->t_flags |= TF_ACKNOW; \ 70*34278Skarels } \ 7124816Skarels } 7224816Skarels 7324816Skarels tcp_reass(tp, ti) 7424816Skarels register struct tcpcb *tp; 7524816Skarels register struct tcpiphdr *ti; 7624816Skarels { 7724816Skarels register struct tcpiphdr *q; 7824816Skarels struct socket *so = tp->t_inpcb->inp_socket; 7924816Skarels struct mbuf *m; 8024816Skarels int flags; 8124816Skarels 8224816Skarels /* 8324816Skarels * Call with ti==0 after become established to 8424816Skarels * force pre-ESTABLISHED data up to user socket. 8524816Skarels */ 8624816Skarels if (ti == 0) 8724816Skarels goto present; 8824816Skarels 8924816Skarels /* 9024816Skarels * Find a segment which begins after this one does. 9124816Skarels */ 9224816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 9324816Skarels q = (struct tcpiphdr *)q->ti_next) 9424816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 9524816Skarels break; 9624816Skarels 9724816Skarels /* 9824816Skarels * If there is a preceding segment, it may provide some of 9924816Skarels * our data already. If so, drop the data from the incoming 10024816Skarels * segment. If it provides all of our data, drop us. 10124816Skarels */ 10224816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 10324816Skarels register int i; 10424816Skarels q = (struct tcpiphdr *)q->ti_prev; 10524816Skarels /* conversion to int (in i) handles seq wraparound */ 10624816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 10724816Skarels if (i > 0) { 10830525Skarels if (i >= ti->ti_len) { 10930525Skarels tcpstat.tcps_rcvduppack++; 11030525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 11124816Skarels goto drop; 11230525Skarels } 11324816Skarels m_adj(dtom(ti), i); 11424816Skarels ti->ti_len -= i; 11524816Skarels ti->ti_seq += i; 11624816Skarels } 11724816Skarels q = (struct tcpiphdr *)(q->ti_next); 11824816Skarels } 11930525Skarels tcpstat.tcps_rcvoopack++; 12030525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 12124816Skarels 12224816Skarels /* 12324816Skarels * While we overlap succeeding segments trim them or, 12424816Skarels * if they are completely covered, dequeue them. 12524816Skarels */ 12624816Skarels while (q != (struct tcpiphdr *)tp) { 12724816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 12824816Skarels if (i <= 0) 12924816Skarels break; 13024816Skarels if (i < q->ti_len) { 13124816Skarels q->ti_seq += i; 13224816Skarels q->ti_len -= i; 13324816Skarels m_adj(dtom(q), i); 13424816Skarels break; 13524816Skarels } 13624816Skarels q = (struct tcpiphdr *)q->ti_next; 13724816Skarels m = dtom(q->ti_prev); 13824816Skarels remque(q->ti_prev); 13924816Skarels m_freem(m); 14024816Skarels } 14124816Skarels 14224816Skarels /* 14324816Skarels * Stick new segment in its place. 14424816Skarels */ 14524816Skarels insque(ti, q->ti_prev); 14624816Skarels 14724816Skarels present: 14824816Skarels /* 14924816Skarels * Present data to user, advancing rcv_nxt through 15024816Skarels * completed sequence space. 15124816Skarels */ 15224816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 15324816Skarels return (0); 15424816Skarels ti = tp->seg_next; 15524816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 15624816Skarels return (0); 15724816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 15824816Skarels return (0); 15924816Skarels do { 16024816Skarels tp->rcv_nxt += ti->ti_len; 16124816Skarels flags = ti->ti_flags & TH_FIN; 16224816Skarels remque(ti); 16324816Skarels m = dtom(ti); 16424816Skarels ti = (struct tcpiphdr *)ti->ti_next; 16524816Skarels if (so->so_state & SS_CANTRCVMORE) 16624816Skarels m_freem(m); 16724816Skarels else 16824816Skarels sbappend(&so->so_rcv, m); 16924816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 17024816Skarels sorwakeup(so); 17124816Skarels return (flags); 17224816Skarels drop: 17324816Skarels m_freem(dtom(ti)); 17424816Skarels return (0); 17524816Skarels } 17624816Skarels 17724816Skarels /* 1785065Swnj * TCP input routine, follows pages 65-76 of the 1795065Swnj * protocol specification dated September, 1981 very closely. 1805065Swnj */ 1814924Swnj tcp_input(m0) 1824924Swnj struct mbuf *m0; 1834601Swnj { 1844924Swnj register struct tcpiphdr *ti; 1854924Swnj struct inpcb *inp; 1864924Swnj register struct mbuf *m; 1875440Swnj struct mbuf *om = 0; 1884924Swnj int len, tlen, off; 1895391Swnj register struct tcpcb *tp = 0; 1904924Swnj register int tiflags; 1914803Swnj struct socket *so; 19231721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1935267Sroot short ostate; 1946028Sroot struct in_addr laddr; 19510769Ssam int dropsocket = 0; 19630525Skarels int iss = 0; 1974924Swnj 19830525Skarels tcpstat.tcps_rcvtotal++; 1994924Swnj /* 2005244Sroot * Get IP and TCP header together in first mbuf. 2015244Sroot * Note: IP leaves IP header in first mbuf. 2024924Swnj */ 2034924Swnj m = m0; 2045020Sroot ti = mtod(m, struct tcpiphdr *); 2055244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 2065208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 2075307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 2085307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 20930525Skarels tcpstat.tcps_rcvshort++; 2105307Sroot return; 2115085Swnj } 2125085Swnj ti = mtod(m, struct tcpiphdr *); 2135085Swnj } 2144601Swnj 2154601Swnj /* 2165244Sroot * Checksum extended TCP header and data. 2174601Swnj */ 2184924Swnj tlen = ((struct ip *)ti)->ip_len; 2194924Swnj len = sizeof (struct ip) + tlen; 2204679Swnj if (tcpcksum) { 2214924Swnj ti->ti_next = ti->ti_prev = 0; 2224924Swnj ti->ti_x1 = 0; 2235223Swnj ti->ti_len = (u_short)tlen; 2246161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2255231Swnj if (ti->ti_sum = in_cksum(m, len)) { 22611830Ssam if (tcpprintfs) 22711830Ssam printf("tcp sum: src %x\n", ti->ti_src); 22830525Skarels tcpstat.tcps_rcvbadsum++; 2295085Swnj goto drop; 2304601Swnj } 2314601Swnj } 2324601Swnj 2334601Swnj /* 2345244Sroot * Check that TCP offset makes sense, 2355440Swnj * pull out TCP options and adjust length. 2364601Swnj */ 2374924Swnj off = ti->ti_off << 2; 2385231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 23911830Ssam if (tcpprintfs) 24011830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 24130525Skarels tcpstat.tcps_rcvbadoff++; 2425085Swnj goto drop; 2434924Swnj } 2446211Swnj tlen -= off; 2456211Swnj ti->ti_len = tlen; 2465440Swnj if (off > sizeof (struct tcphdr)) { 24724816Skarels if (m->m_len < sizeof(struct ip) + off) { 24824816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 24930525Skarels tcpstat.tcps_rcvshort++; 25024816Skarels return; 25124816Skarels } 25224816Skarels ti = mtod(m, struct tcpiphdr *); 2535440Swnj } 2549642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2555440Swnj if (om == 0) 2565440Swnj goto drop; 2575440Swnj om->m_len = off - sizeof (struct tcphdr); 2585440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2596161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2605440Swnj m->m_len -= om->m_len; 2616161Ssam bcopy(op+om->m_len, op, 2626161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2635440Swnj } 2645440Swnj } 2655065Swnj tiflags = ti->ti_flags; 2664924Swnj 2676093Sroot /* 26825729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2696093Sroot */ 27025729Smckusick m->m_off += sizeof(struct tcpiphdr); 27125729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2726093Sroot 2734924Swnj /* 2745244Sroot * Convert TCP protocol specific fields to host format. 2755085Swnj */ 2765085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2775085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2785085Swnj ti->ti_win = ntohs(ti->ti_win); 2795085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2805085Swnj 2815085Swnj /* 2828271Sroot * Locate pcb for segment. 2834924Swnj */ 28430525Skarels findpcb: 2855065Swnj inp = in_pcblookup 2866028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2876028Sroot INPLOOKUP_WILDCARD); 2885065Swnj 2895065Swnj /* 2905065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2915244Sroot * all data in the incoming segment is discarded. 29232098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 29332098Skarels * but should either do a listen or a connect soon. 2945065Swnj */ 2955300Sroot if (inp == 0) 2965085Swnj goto dropwithreset; 2975065Swnj tp = intotcpcb(inp); 2985300Sroot if (tp == 0) 2995085Swnj goto dropwithreset; 30032098Skarels if (tp->t_state == TCPS_CLOSED) 30132098Skarels goto drop; 3025109Swnj so = inp->inp_socket; 3035267Sroot if (so->so_options & SO_DEBUG) { 3045267Sroot ostate = tp->t_state; 3055267Sroot tcp_saveti = *ti; 3065267Sroot } 3077510Sroot if (so->so_options & SO_ACCEPTCONN) { 3087510Sroot so = sonewconn(so); 3097510Sroot if (so == 0) 3107510Sroot goto drop; 31110769Ssam /* 31210769Ssam * This is ugly, but .... 31310769Ssam * 31410769Ssam * Mark socket as temporary until we're 31510769Ssam * committed to keeping it. The code at 31610769Ssam * ``drop'' and ``dropwithreset'' check the 31710769Ssam * flag dropsocket to see if the temporary 31810769Ssam * socket created here should be discarded. 31910769Ssam * We mark the socket as discardable until 32010769Ssam * we're committed to it below in TCPS_LISTEN. 32110769Ssam */ 32210769Ssam dropsocket++; 3237510Sroot inp = (struct inpcb *)so->so_pcb; 3247510Sroot inp->inp_laddr = ti->ti_dst; 3257510Sroot inp->inp_lport = ti->ti_dport; 32624816Skarels inp->inp_options = ip_srcroute(); 3277510Sroot tp = intotcpcb(inp); 3287510Sroot tp->t_state = TCPS_LISTEN; 3297510Sroot } 3304601Swnj 3314601Swnj /* 3325162Swnj * Segment received on connection. 3335162Swnj * Reset idle time and keep-alive timer. 3345162Swnj */ 3355162Swnj tp->t_idle = 0; 33633745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle; 3375162Swnj 3385162Swnj /* 33917272Skarels * Process options if not in LISTEN state, 34017272Skarels * else do it below (after getting remote address). 3415440Swnj */ 34217272Skarels if (om && tp->t_state != TCPS_LISTEN) { 34317272Skarels tcp_dooptions(tp, om, ti); 3445440Swnj om = 0; 3455440Swnj } 3465440Swnj 3475440Swnj /* 3485085Swnj * Calculate amount of space in receive window, 3495085Swnj * and then do TCP input processing. 35024816Skarels * Receive window is amount of space in rcv queue, 35124816Skarels * but not less than advertised window. 3524601Swnj */ 35325939Skarels { int win; 3544601Swnj 35525939Skarels win = sbspace(&so->so_rcv); 35625939Skarels if (win < 0) 35725939Skarels win = 0; 35825939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 35925939Skarels } 36025939Skarels 3614601Swnj switch (tp->t_state) { 3624601Swnj 3635065Swnj /* 3645065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3655065Swnj * If the segment contains an ACK then it is bad and send a RST. 3665065Swnj * If it does not contain a SYN then it is not interesting; drop it. 36725197Skarels * Don't bother responding if the destination was a broadcast. 3685085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3695065Swnj * tp->iss, and send a segment: 3705085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3715065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3725065Swnj * Fill in remote peer address fields if not previously specified. 3735065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3745244Sroot * segment in this state. 3755065Swnj */ 3768271Sroot case TCPS_LISTEN: { 37710145Ssam struct mbuf *am; 3788271Sroot register struct sockaddr_in *sin; 3798271Sroot 3805065Swnj if (tiflags & TH_RST) 3815065Swnj goto drop; 3825300Sroot if (tiflags & TH_ACK) 3835085Swnj goto dropwithreset; 3845300Sroot if ((tiflags & TH_SYN) == 0) 3855065Swnj goto drop; 38625197Skarels if (in_broadcast(ti->ti_dst)) 38725197Skarels goto drop; 38810145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 38910145Ssam if (am == NULL) 39010145Ssam goto drop; 39110145Ssam am->m_len = sizeof (struct sockaddr_in); 3928599Sroot sin = mtod(am, struct sockaddr_in *); 3938271Sroot sin->sin_family = AF_INET; 3948271Sroot sin->sin_addr = ti->ti_src; 3958271Sroot sin->sin_port = ti->ti_sport; 3966028Sroot laddr = inp->inp_laddr; 39710145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3986028Sroot inp->inp_laddr = ti->ti_dst; 3998599Sroot if (in_pcbconnect(inp, am)) { 4006028Sroot inp->inp_laddr = laddr; 4018716Sroot (void) m_free(am); 4025244Sroot goto drop; 4036028Sroot } 4048716Sroot (void) m_free(am); 4055244Sroot tp->t_template = tcp_template(tp); 4065244Sroot if (tp->t_template == 0) { 40726386Skarels tp = tcp_drop(tp, ENOBUFS); 40817264Skarels dropsocket = 0; /* socket is already gone */ 4095244Sroot goto drop; 4105244Sroot } 41117272Skarels if (om) { 41217272Skarels tcp_dooptions(tp, om, ti); 41317272Skarels om = 0; 41417272Skarels } 41530525Skarels if (iss) 41630525Skarels tp->iss = iss; 41730525Skarels else 41830525Skarels tp->iss = tcp_iss; 41930525Skarels tcp_iss += TCP_ISSINCR/2; 4205065Swnj tp->irs = ti->ti_seq; 4215085Swnj tcp_sendseqinit(tp); 4225085Swnj tcp_rcvseqinit(tp); 42325939Skarels tp->t_flags |= TF_ACKNOW; 4245065Swnj tp->t_state = TCPS_SYN_RECEIVED; 42533745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 42610769Ssam dropsocket = 0; /* committed to socket */ 42730525Skarels tcpstat.tcps_accepts++; 4285085Swnj goto trimthenstep6; 4298271Sroot } 4304601Swnj 4315065Swnj /* 4325065Swnj * If the state is SYN_SENT: 4335065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4345065Swnj * if seg contains a RST, then drop the connection. 4355065Swnj * if seg does not contain SYN, then drop it. 4365065Swnj * Otherwise this is an acceptable SYN segment 4375065Swnj * initialize tp->rcv_nxt and tp->irs 4385065Swnj * if seg contains ack then advance tp->snd_una 4395065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4405065Swnj * arrange for segment to be acked (eventually) 4415065Swnj * continue processing rest of data/controls, beginning with URG 4425065Swnj */ 4435065Swnj case TCPS_SYN_SENT: 4445065Swnj if ((tiflags & TH_ACK) && 44524816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4465231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4475085Swnj goto dropwithreset; 4485065Swnj if (tiflags & TH_RST) { 44910394Ssam if (tiflags & TH_ACK) 45010394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4515065Swnj goto drop; 4524601Swnj } 4535065Swnj if ((tiflags & TH_SYN) == 0) 4545065Swnj goto drop; 45530974Skarels if (tiflags & TH_ACK) { 45630974Skarels tp->snd_una = ti->ti_ack; 45730974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 45830974Skarels tp->snd_nxt = tp->snd_una; 45930974Skarels } 4605244Sroot tp->t_timer[TCPT_REXMT] = 0; 4615065Swnj tp->irs = ti->ti_seq; 4625085Swnj tcp_rcvseqinit(tp); 4635085Swnj tp->t_flags |= TF_ACKNOW; 46430974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 46530525Skarels tcpstat.tcps_connects++; 4665244Sroot soisconnected(so); 4675065Swnj tp->t_state = TCPS_ESTABLISHED; 46817272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4695162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 47032098Skarels /* 47132098Skarels * if we didn't have to retransmit the SYN, 47232098Skarels * use its rtt as our initial srtt & rtt var. 47332098Skarels */ 47432098Skarels if (tp->t_rtt) { 47532098Skarels tp->t_srtt = tp->t_rtt << 3; 47632098Skarels tp->t_rttvar = tp->t_rtt << 1; 47732098Skarels TCPT_RANGESET(tp->t_rxtcur, 47832098Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 47932098Skarels TCPTV_MIN, TCPTV_REXMTMAX); 48032098Skarels tp->t_rtt = 0; 48132098Skarels } 4825162Swnj } else 4835085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4845085Swnj 4855085Swnj trimthenstep6: 4865085Swnj /* 4875231Swnj * Advance ti->ti_seq to correspond to first data byte. 4885085Swnj * If data, trim to stay within window, 4895085Swnj * dropping FIN if necessary. 4905085Swnj */ 4915231Swnj ti->ti_seq++; 4925085Swnj if (ti->ti_len > tp->rcv_wnd) { 4935085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4945085Swnj m_adj(m, -todrop); 4955085Swnj ti->ti_len = tp->rcv_wnd; 49625939Skarels tiflags &= ~TH_FIN; 49730525Skarels tcpstat.tcps_rcvpackafterwin++; 49830525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4995065Swnj } 5005263Swnj tp->snd_wl1 = ti->ti_seq - 1; 50125939Skarels tp->rcv_up = ti->ti_seq; 5025085Swnj goto step6; 5035065Swnj } 5044601Swnj 5055065Swnj /* 50630525Skarels * States other than LISTEN or SYN_SENT. 50730525Skarels * First check that at least some bytes of segment are within 50830525Skarels * receive window. If segment begins before rcv_nxt, 50930525Skarels * drop leading data (and SYN); if nothing left, just ack. 51016222Skarels */ 51130525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 51230525Skarels if (todrop > 0) { 51330525Skarels if (tiflags & TH_SYN) { 51430525Skarels tiflags &= ~TH_SYN; 51530525Skarels ti->ti_seq++; 51630525Skarels if (ti->ti_urp > 1) 51730525Skarels ti->ti_urp--; 51830525Skarels else 51930525Skarels tiflags &= ~TH_URG; 52030525Skarels todrop--; 52130525Skarels } 52230525Skarels if (todrop > ti->ti_len || 52330525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 52433745Skarels tcpstat.tcps_rcvduppack++; 52533745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 52631730Skarels /* 52733745Skarels * If segment is just one to the left of the window, 52833745Skarels * check two special cases: 52933745Skarels * 1. Don't toss RST in response to 4.2-style keepalive. 53033745Skarels * 2. If the only thing to drop is a FIN, we can drop 53133745Skarels * it, but check the ACK or we will get into FIN 53233745Skarels * wars if our FINs crossed (both CLOSING). 53333745Skarels * In either case, send ACK to resynchronize, 53433745Skarels * but keep on processing for RST or ACK. 53531730Skarels */ 53633745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 53733745Skarels #ifdef TCP_COMPAT_42 53833745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 53931730Skarels #endif 54033745Skarels ) { 54133745Skarels todrop = ti->ti_len; 54233745Skarels tiflags &= ~TH_FIN; 54333745Skarels tp->t_flags |= TF_ACKNOW; 54433745Skarels } else 54533745Skarels goto dropafterack; 54632034Skarels } else { 54732034Skarels tcpstat.tcps_rcvpartduppack++; 54832034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 54930525Skarels } 55030525Skarels m_adj(m, todrop); 55130525Skarels ti->ti_seq += todrop; 55230525Skarels ti->ti_len -= todrop; 55330525Skarels if (ti->ti_urp > todrop) 55430525Skarels ti->ti_urp -= todrop; 55530525Skarels else { 55630525Skarels tiflags &= ~TH_URG; 55730525Skarels ti->ti_urp = 0; 55830525Skarels } 55916222Skarels } 56016222Skarels 56132612Skarels /* 56233745Skarels * If new data are received on a connection after the 56332612Skarels * user processes are gone, then RST the other end. 56432612Skarels */ 56532612Skarels if ((so->so_state & SS_NOFDREF) && 56632612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 56732612Skarels tp = tcp_close(tp); 56832612Skarels tcpstat.tcps_rcvafterclose++; 56932612Skarels goto dropwithreset; 57032612Skarels } 57132612Skarels 57233445Skarels /* 57333445Skarels * If segment ends after window, drop trailing data 57433445Skarels * (and PUSH and FIN); if nothing left, just ACK. 57533445Skarels */ 57633445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 57733445Skarels if (todrop > 0) { 57833445Skarels tcpstat.tcps_rcvpackafterwin++; 57933445Skarels if (todrop >= ti->ti_len) { 58030525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 58133445Skarels /* 58233445Skarels * If a new connection request is received 58333445Skarels * while in TIME_WAIT, drop the old connection 58433445Skarels * and start over if the sequence numbers 58533445Skarels * are above the previous ones. 58633445Skarels */ 58733445Skarels if (tiflags & TH_SYN && 58833445Skarels tp->t_state == TCPS_TIME_WAIT && 58933445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 59033445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 59133445Skarels (void) tcp_close(tp); 59233445Skarels goto findpcb; 59333445Skarels } 59433445Skarels /* 59533445Skarels * If window is closed can only take segments at 59633445Skarels * window edge, and have to drop data and PUSH from 59733445Skarels * incoming segments. Continue processing, but 59833445Skarels * remember to ack. Otherwise, drop segment 59933445Skarels * and ack. 60033445Skarels */ 60133445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 60233445Skarels tp->t_flags |= TF_ACKNOW; 60330525Skarels tcpstat.tcps_rcvwinprobe++; 60433445Skarels } else 6055065Swnj goto dropafterack; 60633445Skarels } else 60730525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 60833445Skarels m_adj(m, -todrop); 60933445Skarels ti->ti_len -= todrop; 61033445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6115065Swnj } 6124601Swnj 6135065Swnj /* 6145065Swnj * If the RST bit is set examine the state: 6155065Swnj * SYN_RECEIVED STATE: 6165065Swnj * If passive open, return to LISTEN state. 6175065Swnj * If active open, inform user that connection was refused. 6185065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6195065Swnj * Inform user that connection was reset, and close tcb. 6205065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6215065Swnj * Close the tcb. 6225065Swnj */ 6235065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6245267Sroot 6255065Swnj case TCPS_SYN_RECEIVED: 62633745Skarels so->so_error = ECONNREFUSED; 62733745Skarels goto close; 6284601Swnj 6295065Swnj case TCPS_ESTABLISHED: 6305065Swnj case TCPS_FIN_WAIT_1: 6315065Swnj case TCPS_FIN_WAIT_2: 6325065Swnj case TCPS_CLOSE_WAIT: 63333745Skarels so->so_error = ECONNRESET; 63433745Skarels close: 63533745Skarels tp->t_state = TCPS_CLOSED; 63633745Skarels tcpstat.tcps_drops++; 63733745Skarels tp = tcp_close(tp); 6385065Swnj goto drop; 6395065Swnj 6405065Swnj case TCPS_CLOSING: 6415065Swnj case TCPS_LAST_ACK: 6425065Swnj case TCPS_TIME_WAIT: 64310394Ssam tp = tcp_close(tp); 6445065Swnj goto drop; 6454601Swnj } 6464601Swnj 6474601Swnj /* 6485065Swnj * If a SYN is in the window, then this is an 6495065Swnj * error and we send an RST and drop the connection. 6504601Swnj */ 6515065Swnj if (tiflags & TH_SYN) { 65210394Ssam tp = tcp_drop(tp, ECONNRESET); 6535085Swnj goto dropwithreset; 6544601Swnj } 6554601Swnj 6564601Swnj /* 6575065Swnj * If the ACK bit is off we drop the segment and return. 6584601Swnj */ 6595085Swnj if ((tiflags & TH_ACK) == 0) 6605065Swnj goto drop; 6615065Swnj 6625065Swnj /* 6635065Swnj * Ack processing. 6645065Swnj */ 6654601Swnj switch (tp->t_state) { 6664601Swnj 6675065Swnj /* 6685065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 66931721Skarels * ESTABLISHED state and continue processing, otherwise 6705065Swnj * send an RST. 6715065Swnj */ 6725065Swnj case TCPS_SYN_RECEIVED: 6735085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6745231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6755085Swnj goto dropwithreset; 67630525Skarels tcpstat.tcps_connects++; 6775085Swnj soisconnected(so); 6785085Swnj tp->t_state = TCPS_ESTABLISHED; 67917272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6805162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6815244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6825085Swnj /* fall into ... */ 6834601Swnj 6845065Swnj /* 6855065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6865065Swnj * ACKs. If the ack is in the range 6875231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6885065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6895065Swnj * data from the retransmission queue. If this ACK reflects 6905065Swnj * more up to date window information we update our window information. 6915065Swnj */ 6925065Swnj case TCPS_ESTABLISHED: 6935065Swnj case TCPS_FIN_WAIT_1: 6945065Swnj case TCPS_FIN_WAIT_2: 6955065Swnj case TCPS_CLOSE_WAIT: 6965065Swnj case TCPS_CLOSING: 6975244Sroot case TCPS_LAST_ACK: 6985244Sroot case TCPS_TIME_WAIT: 6995085Swnj 70030525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 70132098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 70230525Skarels tcpstat.tcps_rcvdupack++; 70332098Skarels /* 70432098Skarels * If we have outstanding data (not a 70532098Skarels * window probe), this is a completely 70632098Skarels * duplicate ack (ie, window info didn't 70732098Skarels * change), the ack is the biggest we've 70832098Skarels * seen and we've seen exactly our rexmt 70932098Skarels * threshhold of them, assume a packet 71032098Skarels * has been dropped and retransmit it. 71132098Skarels * Kludge snd_nxt & the congestion 71232098Skarels * window so we send only this one 71332376Skarels * packet. If this packet fills the 71432376Skarels * only hole in the receiver's seq. 71532376Skarels * space, the next real ack will fully 71632376Skarels * open our window. This means we 71732376Skarels * have to do the usual slow-start to 71832376Skarels * not overwhelm an intermediate gateway 71932376Skarels * with a burst of packets. Leave 72032376Skarels * here with the congestion window set 72132376Skarels * to allow 2 packets on the next real 72232376Skarels * ack and the exp-to-linear thresh 72332376Skarels * set for half the current window 72432376Skarels * size (since we know we're losing at 72532376Skarels * the current window size). 72632098Skarels */ 72732098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 72832098Skarels ti->ti_ack != tp->snd_una) 72932098Skarels tp->t_dupacks = 0; 73032098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 73132098Skarels tcp_seq onxt = tp->snd_nxt; 73232376Skarels u_int win = 73332376Skarels MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 73432376Skarels tp->t_maxseg; 73532098Skarels 73632376Skarels if (win < 2) 73732376Skarels win = 2; 73832376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 73932376Skarels 74032098Skarels tp->t_timer[TCPT_REXMT] = 0; 74132098Skarels tp->t_rtt = 0; 74232098Skarels tp->snd_nxt = ti->ti_ack; 74332098Skarels tp->snd_cwnd = tp->t_maxseg; 74432098Skarels (void) tcp_output(tp); 74532098Skarels 74632098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 74732098Skarels tp->snd_nxt = onxt; 74832098Skarels goto drop; 74932098Skarels } 75032098Skarels } else 75132098Skarels tp->t_dupacks = 0; 7525065Swnj break; 75330525Skarels } 75432098Skarels tp->t_dupacks = 0; 75530525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 75630525Skarels tcpstat.tcps_rcvacktoomuch++; 7575065Swnj goto dropafterack; 75830525Skarels } 7595085Swnj acked = ti->ti_ack - tp->snd_una; 76030525Skarels tcpstat.tcps_rcvackpack++; 76130525Skarels tcpstat.tcps_rcvackbyte += acked; 7625951Swnj 7635951Swnj /* 7645951Swnj * If transmit timer is running and timed sequence 7655951Swnj * number was acked, update smoothed round trip time. 76632034Skarels * Since we now have an rtt measurement, cancel the 76732034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 76832034Skarels * Recompute the initial retransmit timer. 7695951Swnj */ 7705951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 77130525Skarels tcpstat.tcps_rttupdated++; 77231726Skarels if (tp->t_srtt != 0) { 77331726Skarels register short delta; 77431726Skarels 77531726Skarels /* 77631726Skarels * srtt is stored as fixed point with 3 bits 77731726Skarels * after the binary point (i.e., scaled by 8). 77831726Skarels * The following magic is equivalent 77931726Skarels * to the smoothing algorithm in rfc793 78031726Skarels * with an alpha of .875 78131726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 78232098Skarels * Adjust t_rtt to origin 0. 78331726Skarels */ 78433745Skarels delta = tp->t_rtt - 1 - (tp->t_srtt >> 3); 78531726Skarels if ((tp->t_srtt += delta) <= 0) 78631726Skarels tp->t_srtt = 1; 78731726Skarels /* 78832034Skarels * We accumulate a smoothed rtt variance 78932034Skarels * (actually, a smoothed mean difference), 79031726Skarels * then set the retransmit timer to smoothed 79131726Skarels * rtt + 2 times the smoothed variance. 79232098Skarels * rttvar is stored as fixed point 79331726Skarels * with 2 bits after the binary point 79431726Skarels * (scaled by 4). The following is equivalent 79531726Skarels * to rfc793 smoothing with an alpha of .75 79631726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 79731726Skarels * This replaces rfc793's wired-in beta. 79831726Skarels */ 79931726Skarels if (delta < 0) 80031726Skarels delta = -delta; 80131726Skarels delta -= (tp->t_rttvar >> 2); 80231726Skarels if ((tp->t_rttvar += delta) <= 0) 80331726Skarels tp->t_rttvar = 1; 80431726Skarels } else { 80531726Skarels /* 80631726Skarels * No rtt measurement yet - use the 80731726Skarels * unsmoothed rtt. Set the variance 80831726Skarels * to half the rtt (so our first 80931726Skarels * retransmit happens at 2*rtt) 81031726Skarels */ 81131726Skarels tp->t_srtt = tp->t_rtt << 3; 81231726Skarels tp->t_rttvar = tp->t_rtt << 1; 81331726Skarels } 8145951Swnj tp->t_rtt = 0; 81532034Skarels tp->t_rxtshift = 0; 81632034Skarels TCPT_RANGESET(tp->t_rxtcur, 81732034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 81832034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 8195951Swnj } 8205951Swnj 82126824Skarels /* 82226824Skarels * If all outstanding data is acked, stop retransmit 82326824Skarels * timer and remember to restart (more output or persist). 82426824Skarels * If there is more data to be acked, restart retransmit 82532034Skarels * timer, using current (possibly backed-off) value. 82626824Skarels */ 82726824Skarels if (ti->ti_ack == tp->snd_max) { 8285244Sroot tp->t_timer[TCPT_REXMT] = 0; 82926824Skarels needoutput = 1; 83032034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 83132034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 83217360Skarels /* 83332098Skarels * When new data is acked, open the congestion window. 83432098Skarels * If the window gives us less than ssthresh packets 83532098Skarels * in flight, open exponentially (maxseg per packet). 83632098Skarels * Otherwise open linearly (maxseg per window, 83732098Skarels * or maxseg^2 / cwnd per packet). 83817360Skarels */ 83932098Skarels { 84032098Skarels u_int incr = tp->t_maxseg; 84132098Skarels 84232098Skarels if (tp->snd_cwnd > tp->snd_ssthresh) 84332098Skarels incr = MAX(incr * incr / tp->snd_cwnd, 1); 84432098Skarels 84533603Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + incr, IP_MAXPACKET); /* XXX */ 84632098Skarels } 8475307Sroot if (acked > so->so_snd.sb_cc) { 84815386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 84926386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 85031721Skarels ourfinisacked = 1; 8515307Sroot } else { 8526161Ssam sbdrop(&so->so_snd, acked); 8535307Sroot tp->snd_wnd -= acked; 85431721Skarels ourfinisacked = 0; 8555307Sroot } 8566434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 8575300Sroot sowwakeup(so); 8585231Swnj tp->snd_una = ti->ti_ack; 8595357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 8605357Sroot tp->snd_nxt = tp->snd_una; 8615162Swnj 8624601Swnj switch (tp->t_state) { 8634601Swnj 8645065Swnj /* 8655065Swnj * In FIN_WAIT_1 STATE in addition to the processing 8665065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 8675085Swnj * then enter FIN_WAIT_2. 8685065Swnj */ 8695065Swnj case TCPS_FIN_WAIT_1: 8705896Swnj if (ourfinisacked) { 8715896Swnj /* 8725896Swnj * If we can't receive any more 8735896Swnj * data, then closing user can proceed. 87424816Skarels * Starting the timer is contrary to the 87524816Skarels * specification, but if we don't get a FIN 87624816Skarels * we'll hang forever. 8775896Swnj */ 87824816Skarels if (so->so_state & SS_CANTRCVMORE) { 8795896Swnj soisdisconnected(so); 88033745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 88124816Skarels } 8825085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8835896Swnj } 8844601Swnj break; 8854601Swnj 8865065Swnj /* 8875065Swnj * In CLOSING STATE in addition to the processing for 8885065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8895065Swnj * then enter the TIME-WAIT state, otherwise ignore 8905065Swnj * the segment. 8915065Swnj */ 8925065Swnj case TCPS_CLOSING: 8935244Sroot if (ourfinisacked) { 8945065Swnj tp->t_state = TCPS_TIME_WAIT; 8955244Sroot tcp_canceltimers(tp); 8965244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8975244Sroot soisdisconnected(so); 8985244Sroot } 8995244Sroot break; 9004601Swnj 9015065Swnj /* 90231743Skarels * In LAST_ACK, we may still be waiting for data to drain 90331743Skarels * and/or to be acked, as well as for the ack of our FIN. 90431743Skarels * If our FIN is now acknowledged, delete the TCB, 90531743Skarels * enter the closed state and return. 9065065Swnj */ 9075065Swnj case TCPS_LAST_ACK: 90831743Skarels if (ourfinisacked) { 90910394Ssam tp = tcp_close(tp); 91031743Skarels goto drop; 91131743Skarels } 91231743Skarels break; 9134601Swnj 9145065Swnj /* 9155065Swnj * In TIME_WAIT state the only thing that should arrive 9165065Swnj * is a retransmission of the remote FIN. Acknowledge 9175065Swnj * it and restart the finack timer. 9185065Swnj */ 9195065Swnj case TCPS_TIME_WAIT: 9205162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9215065Swnj goto dropafterack; 9224601Swnj } 9235085Swnj } 9244601Swnj 9255065Swnj step6: 9265065Swnj /* 9275244Sroot * Update window information. 92825939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9295244Sroot */ 93025939Skarels if ((tiflags & TH_ACK) && 93125939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9325391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 93325939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 93430525Skarels /* keep track of pure window updates */ 93530525Skarels if (ti->ti_len == 0 && 93632098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 93730525Skarels tcpstat.tcps_rcvwinupd++; 9385244Sroot tp->snd_wnd = ti->ti_win; 9395244Sroot tp->snd_wl1 = ti->ti_seq; 9405244Sroot tp->snd_wl2 = ti->ti_ack; 94125260Skarels if (tp->snd_wnd > tp->max_sndwnd) 94225260Skarels tp->max_sndwnd = tp->snd_wnd; 94326824Skarels needoutput = 1; 94426824Skarels } 9455244Sroot 9465244Sroot /* 9475547Swnj * Process segments with URG. 9485065Swnj */ 9497267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9507267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9515547Swnj /* 95225939Skarels * This is a kludge, but if we receive and accept 95313121Ssam * random urgent pointers, we'll crash in 95413121Ssam * soreceive. It's hard to imagine someone 95513121Ssam * actually wanting to send this much urgent data. 95612441Ssam */ 95725652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 95812441Ssam ti->ti_urp = 0; /* XXX */ 95912441Ssam tiflags &= ~TH_URG; /* XXX */ 96025939Skarels goto dodata; /* XXX */ 96112441Ssam } 96212441Ssam /* 9635547Swnj * If this segment advances the known urgent pointer, 9645547Swnj * then mark the data stream. This should not happen 9655547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 9665547Swnj * a FIN has been received from the remote side. 9675547Swnj * In these states we ignore the URG. 96827190Skarels * 96927190Skarels * According to RFC961 (Assigned Protocols), 97027190Skarels * the urgent pointer points to the last octet 97127190Skarels * of urgent data. We continue, however, 97227190Skarels * to consider it to indicate the first octet 97327190Skarels * of data past the urgent section 97427190Skarels * as the original spec states. 9755547Swnj */ 9765547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9775547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9785547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9795547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9805547Swnj if (so->so_oobmark == 0) 9815547Swnj so->so_state |= SS_RCVATMARK; 9828313Sroot sohasoutofband(so); 98324816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9845440Swnj } 9855547Swnj /* 9865547Swnj * Remove out of band data so doesn't get presented to user. 9875547Swnj * This can happen independent of advancing the URG pointer, 9885547Swnj * but if two URG's are pending at once, some out-of-band 9895547Swnj * data may creep in... ick. 9905547Swnj */ 991*34278Skarels if (ti->ti_urp <= ti->ti_len && 992*34278Skarels (so->so_options & SO_OOBINLINE) == 0) 9935547Swnj tcp_pulloutofband(so, ti); 99425939Skarels } else 99525939Skarels /* 99625939Skarels * If no out of band data is expected, 99725939Skarels * pull receive urgent pointer along 99825939Skarels * with the receive window. 99925939Skarels */ 100025939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 100125939Skarels tp->rcv_up = tp->rcv_nxt; 100225939Skarels dodata: /* XXX */ 10034601Swnj 10044601Swnj /* 10055065Swnj * Process the segment text, merging it into the TCP sequencing queue, 10065065Swnj * and arranging for acknowledgment of receipt if necessary. 10075065Swnj * This process logically involves adjusting tp->rcv_wnd as data 10085065Swnj * is presented to the user (this happens in tcp_usrreq.c, 10095065Swnj * case PRU_RCVD). If a FIN has already been received on this 10105065Swnj * connection then we just ignore the text. 10114601Swnj */ 101217946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 101317946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 101424816Skarels TCP_REASS(tp, ti, m, so, tiflags); 101525260Skarels /* 101625260Skarels * Note the amount of data that peer has sent into 101725260Skarels * our window, in order to estimate the sender's 101825260Skarels * buffer size. 101925260Skarels */ 102032098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 102125260Skarels if (len > tp->max_rcvd) 102225260Skarels tp->max_rcvd = len; 10235244Sroot } else { 10244924Swnj m_freem(m); 10255263Swnj tiflags &= ~TH_FIN; 10265244Sroot } 10274601Swnj 10284601Swnj /* 10295263Swnj * If FIN is received ACK the FIN and let the user know 10305263Swnj * that the connection is closing. 10314601Swnj */ 10325263Swnj if (tiflags & TH_FIN) { 10335244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10345244Sroot socantrcvmore(so); 10355244Sroot tp->t_flags |= TF_ACKNOW; 10365244Sroot tp->rcv_nxt++; 10375244Sroot } 10385065Swnj switch (tp->t_state) { 10394601Swnj 10405065Swnj /* 10415065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10425065Swnj * enter the CLOSE_WAIT state. 10434884Swnj */ 10445065Swnj case TCPS_SYN_RECEIVED: 10455065Swnj case TCPS_ESTABLISHED: 10465065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10475065Swnj break; 10484884Swnj 10495065Swnj /* 10505085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10515085Swnj * enter the CLOSING state. 10524884Swnj */ 10535065Swnj case TCPS_FIN_WAIT_1: 10545085Swnj tp->t_state = TCPS_CLOSING; 10555065Swnj break; 10564601Swnj 10575065Swnj /* 10585065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 10595065Swnj * starting the time-wait timer, turning off the other 10605065Swnj * standard timers. 10615065Swnj */ 10625065Swnj case TCPS_FIN_WAIT_2: 10635244Sroot tp->t_state = TCPS_TIME_WAIT; 10645074Swnj tcp_canceltimers(tp); 10655162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10665244Sroot soisdisconnected(so); 10675065Swnj break; 10685065Swnj 10694884Swnj /* 10705065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10714884Swnj */ 10725065Swnj case TCPS_TIME_WAIT: 10735162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10745065Swnj break; 10755085Swnj } 10764601Swnj } 10775267Sroot if (so->so_options & SO_DEBUG) 10785267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10795085Swnj 10805085Swnj /* 10815085Swnj * Return any desired output. 10825085Swnj */ 108326824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 108425939Skarels (void) tcp_output(tp); 10855065Swnj return; 10865085Swnj 10875065Swnj dropafterack: 10885085Swnj /* 10896211Swnj * Generate an ACK dropping incoming segment if it occupies 10906211Swnj * sequence space, where the ACK reflects our state. 10915085Swnj */ 109226057Skarels if (tiflags & TH_RST) 10935085Swnj goto drop; 109431749Skarels m_freem(m); 109531721Skarels tp->t_flags |= TF_ACKNOW; 109631721Skarels (void) tcp_output(tp); 10975231Swnj return; 10985085Swnj 10995085Swnj dropwithreset: 110011731Ssam if (om) { 11016161Ssam (void) m_free(om); 110211731Ssam om = 0; 110311731Ssam } 11045085Swnj /* 11055244Sroot * Generate a RST, dropping incoming segment. 11065085Swnj * Make ACK acceptable to originator of segment. 110725197Skarels * Don't bother to respond if destination was broadcast. 11085085Swnj */ 110925197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 11105085Swnj goto drop; 11115085Swnj if (tiflags & TH_ACK) 11125391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 11135085Swnj else { 11145085Swnj if (tiflags & TH_SYN) 11155085Swnj ti->ti_len++; 11166211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11176211Swnj TH_RST|TH_ACK); 11185085Swnj } 111910769Ssam /* destroy temporarily created socket */ 112010769Ssam if (dropsocket) 112110769Ssam (void) soabort(so); 11225231Swnj return; 11235085Swnj 11245065Swnj drop: 112511730Ssam if (om) 112611730Ssam (void) m_free(om); 11275085Swnj /* 11285085Swnj * Drop space held by incoming segment and return. 11295085Swnj */ 11306303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11316303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11325065Swnj m_freem(m); 113310769Ssam /* destroy temporarily created socket */ 113410769Ssam if (dropsocket) 113510769Ssam (void) soabort(so); 11365267Sroot return; 11375065Swnj } 11385065Swnj 113917272Skarels tcp_dooptions(tp, om, ti) 11405440Swnj struct tcpcb *tp; 11415440Swnj struct mbuf *om; 114217272Skarels struct tcpiphdr *ti; 11435419Swnj { 11445440Swnj register u_char *cp; 11455440Swnj int opt, optlen, cnt; 11465419Swnj 11475440Swnj cp = mtod(om, u_char *); 11485440Swnj cnt = om->m_len; 11495440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11505440Swnj opt = cp[0]; 11515440Swnj if (opt == TCPOPT_EOL) 11525440Swnj break; 11535440Swnj if (opt == TCPOPT_NOP) 11545440Swnj optlen = 1; 115512169Ssam else { 11565440Swnj optlen = cp[1]; 115712169Ssam if (optlen <= 0) 115812169Ssam break; 115912169Ssam } 11605440Swnj switch (opt) { 11615440Swnj 11625440Swnj default: 11635440Swnj break; 11645440Swnj 11655440Swnj case TCPOPT_MAXSEG: 11665440Swnj if (optlen != 4) 11675440Swnj continue; 116817272Skarels if (!(ti->ti_flags & TH_SYN)) 116917272Skarels continue; 11705440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11716161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 117217272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 11735440Swnj break; 11745419Swnj } 11755419Swnj } 11766161Ssam (void) m_free(om); 11775419Swnj } 11785419Swnj 11795419Swnj /* 11805547Swnj * Pull out of band byte out of a segment so 11815547Swnj * it doesn't appear in the user's data queue. 11825547Swnj * It is still reflected in the segment length for 11835547Swnj * sequencing purposes. 11845547Swnj */ 11855547Swnj tcp_pulloutofband(so, ti) 11865547Swnj struct socket *so; 11875547Swnj struct tcpiphdr *ti; 11885547Swnj { 11895547Swnj register struct mbuf *m; 11906116Swnj int cnt = ti->ti_urp - 1; 11915547Swnj 11925547Swnj m = dtom(ti); 11935547Swnj while (cnt >= 0) { 11945547Swnj if (m->m_len > cnt) { 11955547Swnj char *cp = mtod(m, caddr_t) + cnt; 11965547Swnj struct tcpcb *tp = sototcpcb(so); 11975547Swnj 11985547Swnj tp->t_iobc = *cp; 11995547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 12006161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 12015547Swnj m->m_len--; 12025547Swnj return; 12035547Swnj } 12045547Swnj cnt -= m->m_len; 12055547Swnj m = m->m_next; 12065547Swnj if (m == 0) 12075547Swnj break; 12085547Swnj } 12095547Swnj panic("tcp_pulloutofband"); 12105547Swnj } 12115547Swnj 12125547Swnj /* 121317272Skarels * Determine a reasonable value for maxseg size. 121417272Skarels * If the route is known, use one that can be handled 121517272Skarels * on the given interface without forcing IP to fragment. 121631726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 121731726Skarels * to utilize large mbufs. 121817272Skarels * If interface pointer is unavailable, or the destination isn't local, 121923975Skarels * use a conservative size (512 or the default IP max size, but no more 122023975Skarels * than the mtu of the interface through which we route), 122117272Skarels * as we can't discover anything about intervening gateways or networks. 122232034Skarels * We also initialize the congestion/slow start window to be a single 122332034Skarels * segment if the destination isn't local; this information should 122432034Skarels * probably all be saved with the routing entry at the transport level. 122517272Skarels * 122617272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 122717272Skarels */ 122817272Skarels tcp_mss(tp) 122923975Skarels register struct tcpcb *tp; 123017272Skarels { 123117272Skarels struct route *ro; 123217272Skarels struct ifnet *ifp; 123317272Skarels int mss; 123417272Skarels struct inpcb *inp; 123517272Skarels 123617272Skarels inp = tp->t_inpcb; 123717272Skarels ro = &inp->inp_route; 123817272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 123917272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 124017272Skarels /* No route yet, so try to acquire one */ 124117272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 124217272Skarels ro->ro_dst.sa_family = AF_INET; 124317272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 124417272Skarels inp->inp_faddr; 124517272Skarels rtalloc(ro); 124617272Skarels } 124717272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 124817316Skarels return (TCP_MSS); 124917272Skarels } 125017272Skarels 125117272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 125231726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 125331726Skarels if (mss > MCLBYTES) 125431726Skarels mss &= ~(MCLBYTES-1); 125517272Skarels #else 125631726Skarels if (mss > MCLBYTES) 125731726Skarels mss = mss / MCLBYTES * MCLBYTES; 125817272Skarels #endif 125923975Skarels if (in_localaddr(inp->inp_faddr)) 126023975Skarels return (mss); 126132098Skarels 126232034Skarels mss = MIN(mss, TCP_MSS); 126332034Skarels tp->snd_cwnd = mss; 126432034Skarels return (mss); 126517272Skarels } 1266