123190Smckusick /* 229149Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323190Smckusick * All rights reserved. The Berkeley software License Agreement 423190Smckusick * specifies the terms and conditions for redistribution. 523190Smckusick * 6*32612Skarels * @(#)tcp_input.c 7.13 (Berkeley) 11/13/87 723190Smckusick */ 84601Swnj 917062Sbloom #include "param.h" 1017062Sbloom #include "systm.h" 1117062Sbloom #include "mbuf.h" 1217062Sbloom #include "protosw.h" 1317062Sbloom #include "socket.h" 1417062Sbloom #include "socketvar.h" 1517062Sbloom #include "errno.h" 1610894Ssam 1710894Ssam #include "../net/if.h" 1810894Ssam #include "../net/route.h" 1910894Ssam 2017062Sbloom #include "in.h" 2117062Sbloom #include "in_pcb.h" 2217062Sbloom #include "in_systm.h" 2317062Sbloom #include "ip.h" 2417062Sbloom #include "ip_var.h" 2517062Sbloom #include "tcp.h" 2617062Sbloom #include "tcp_fsm.h" 2717062Sbloom #include "tcp_seq.h" 2817062Sbloom #include "tcp_timer.h" 2917062Sbloom #include "tcp_var.h" 3017062Sbloom #include "tcpip.h" 3117062Sbloom #include "tcp_debug.h" 324601Swnj 335300Sroot int tcpprintfs = 0; 344679Swnj int tcpcksum = 1; 3532098Skarels int tcprexmtthresh = 3; 365267Sroot struct tcpiphdr tcp_saveti; 375440Swnj extern tcpnodelack; 384601Swnj 395267Sroot struct tcpcb *tcp_newtcpcb(); 4024816Skarels 415065Swnj /* 4224816Skarels * Insert segment ti into reassembly queue of tcp with 4324816Skarels * control block tp. Return TH_FIN if reassembly now includes 4424816Skarels * a segment with FIN. The macro form does the common case inline 4524816Skarels * (segment is the next to be received on an established connection, 4624816Skarels * and the queue is empty), avoiding linkage into and removal 4724816Skarels * from the queue and repetition of various conversions. 4824816Skarels */ 4924816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 5024816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5124816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5224816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 5324816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 5424816Skarels flags = (ti)->ti_flags & TH_FIN; \ 5530525Skarels tcpstat.tcps_rcvpack++;\ 5630525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 5724816Skarels sbappend(&(so)->so_rcv, (m)); \ 5824816Skarels sorwakeup(so); \ 5924816Skarels } else \ 6024816Skarels (flags) = tcp_reass((tp), (ti)); \ 6124816Skarels } 6224816Skarels 6324816Skarels tcp_reass(tp, ti) 6424816Skarels register struct tcpcb *tp; 6524816Skarels register struct tcpiphdr *ti; 6624816Skarels { 6724816Skarels register struct tcpiphdr *q; 6824816Skarels struct socket *so = tp->t_inpcb->inp_socket; 6924816Skarels struct mbuf *m; 7024816Skarels int flags; 7124816Skarels 7224816Skarels /* 7324816Skarels * Call with ti==0 after become established to 7424816Skarels * force pre-ESTABLISHED data up to user socket. 7524816Skarels */ 7624816Skarels if (ti == 0) 7724816Skarels goto present; 7824816Skarels 7924816Skarels /* 8024816Skarels * Find a segment which begins after this one does. 8124816Skarels */ 8224816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8324816Skarels q = (struct tcpiphdr *)q->ti_next) 8424816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8524816Skarels break; 8624816Skarels 8724816Skarels /* 8824816Skarels * If there is a preceding segment, it may provide some of 8924816Skarels * our data already. If so, drop the data from the incoming 9024816Skarels * segment. If it provides all of our data, drop us. 9124816Skarels */ 9224816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 9324816Skarels register int i; 9424816Skarels q = (struct tcpiphdr *)q->ti_prev; 9524816Skarels /* conversion to int (in i) handles seq wraparound */ 9624816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 9724816Skarels if (i > 0) { 9830525Skarels if (i >= ti->ti_len) { 9930525Skarels tcpstat.tcps_rcvduppack++; 10030525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10124816Skarels goto drop; 10230525Skarels } 10324816Skarels m_adj(dtom(ti), i); 10424816Skarels ti->ti_len -= i; 10524816Skarels ti->ti_seq += i; 10624816Skarels } 10724816Skarels q = (struct tcpiphdr *)(q->ti_next); 10824816Skarels } 10930525Skarels tcpstat.tcps_rcvoopack++; 11030525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 11124816Skarels 11224816Skarels /* 11324816Skarels * While we overlap succeeding segments trim them or, 11424816Skarels * if they are completely covered, dequeue them. 11524816Skarels */ 11624816Skarels while (q != (struct tcpiphdr *)tp) { 11724816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 11824816Skarels if (i <= 0) 11924816Skarels break; 12024816Skarels if (i < q->ti_len) { 12124816Skarels q->ti_seq += i; 12224816Skarels q->ti_len -= i; 12324816Skarels m_adj(dtom(q), i); 12424816Skarels break; 12524816Skarels } 12624816Skarels q = (struct tcpiphdr *)q->ti_next; 12724816Skarels m = dtom(q->ti_prev); 12824816Skarels remque(q->ti_prev); 12924816Skarels m_freem(m); 13024816Skarels } 13124816Skarels 13224816Skarels /* 13324816Skarels * Stick new segment in its place. 13424816Skarels */ 13524816Skarels insque(ti, q->ti_prev); 13624816Skarels 13724816Skarels present: 13824816Skarels /* 13924816Skarels * Present data to user, advancing rcv_nxt through 14024816Skarels * completed sequence space. 14124816Skarels */ 14224816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 14324816Skarels return (0); 14424816Skarels ti = tp->seg_next; 14524816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 14624816Skarels return (0); 14724816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 14824816Skarels return (0); 14924816Skarels do { 15024816Skarels tp->rcv_nxt += ti->ti_len; 15124816Skarels flags = ti->ti_flags & TH_FIN; 15224816Skarels remque(ti); 15324816Skarels m = dtom(ti); 15424816Skarels ti = (struct tcpiphdr *)ti->ti_next; 15524816Skarels if (so->so_state & SS_CANTRCVMORE) 15624816Skarels m_freem(m); 15724816Skarels else 15824816Skarels sbappend(&so->so_rcv, m); 15924816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 16024816Skarels sorwakeup(so); 16124816Skarels return (flags); 16224816Skarels drop: 16324816Skarels m_freem(dtom(ti)); 16424816Skarels return (0); 16524816Skarels } 16624816Skarels 16724816Skarels /* 1685065Swnj * TCP input routine, follows pages 65-76 of the 1695065Swnj * protocol specification dated September, 1981 very closely. 1705065Swnj */ 1714924Swnj tcp_input(m0) 1724924Swnj struct mbuf *m0; 1734601Swnj { 1744924Swnj register struct tcpiphdr *ti; 1754924Swnj struct inpcb *inp; 1764924Swnj register struct mbuf *m; 1775440Swnj struct mbuf *om = 0; 1784924Swnj int len, tlen, off; 1795391Swnj register struct tcpcb *tp = 0; 1804924Swnj register int tiflags; 1814803Swnj struct socket *so; 18231721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1835267Sroot short ostate; 1846028Sroot struct in_addr laddr; 18510769Ssam int dropsocket = 0; 18630525Skarels int iss = 0; 1874924Swnj 18830525Skarels tcpstat.tcps_rcvtotal++; 1894924Swnj /* 1905244Sroot * Get IP and TCP header together in first mbuf. 1915244Sroot * Note: IP leaves IP header in first mbuf. 1924924Swnj */ 1934924Swnj m = m0; 1945020Sroot ti = mtod(m, struct tcpiphdr *); 1955244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 1965208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 1975307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 1985307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 19930525Skarels tcpstat.tcps_rcvshort++; 2005307Sroot return; 2015085Swnj } 2025085Swnj ti = mtod(m, struct tcpiphdr *); 2035085Swnj } 2044601Swnj 2054601Swnj /* 2065244Sroot * Checksum extended TCP header and data. 2074601Swnj */ 2084924Swnj tlen = ((struct ip *)ti)->ip_len; 2094924Swnj len = sizeof (struct ip) + tlen; 2104679Swnj if (tcpcksum) { 2114924Swnj ti->ti_next = ti->ti_prev = 0; 2124924Swnj ti->ti_x1 = 0; 2135223Swnj ti->ti_len = (u_short)tlen; 2146161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2155231Swnj if (ti->ti_sum = in_cksum(m, len)) { 21611830Ssam if (tcpprintfs) 21711830Ssam printf("tcp sum: src %x\n", ti->ti_src); 21830525Skarels tcpstat.tcps_rcvbadsum++; 2195085Swnj goto drop; 2204601Swnj } 2214601Swnj } 2224601Swnj 2234601Swnj /* 2245244Sroot * Check that TCP offset makes sense, 2255440Swnj * pull out TCP options and adjust length. 2264601Swnj */ 2274924Swnj off = ti->ti_off << 2; 2285231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 22911830Ssam if (tcpprintfs) 23011830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 23130525Skarels tcpstat.tcps_rcvbadoff++; 2325085Swnj goto drop; 2334924Swnj } 2346211Swnj tlen -= off; 2356211Swnj ti->ti_len = tlen; 2365440Swnj if (off > sizeof (struct tcphdr)) { 23724816Skarels if (m->m_len < sizeof(struct ip) + off) { 23824816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 23930525Skarels tcpstat.tcps_rcvshort++; 24024816Skarels return; 24124816Skarels } 24224816Skarels ti = mtod(m, struct tcpiphdr *); 2435440Swnj } 2449642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2455440Swnj if (om == 0) 2465440Swnj goto drop; 2475440Swnj om->m_len = off - sizeof (struct tcphdr); 2485440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2496161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2505440Swnj m->m_len -= om->m_len; 2516161Ssam bcopy(op+om->m_len, op, 2526161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2535440Swnj } 2545440Swnj } 2555065Swnj tiflags = ti->ti_flags; 2564924Swnj 2576093Sroot /* 25825729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2596093Sroot */ 26025729Smckusick m->m_off += sizeof(struct tcpiphdr); 26125729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2626093Sroot 2634924Swnj /* 2645244Sroot * Convert TCP protocol specific fields to host format. 2655085Swnj */ 2665085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2675085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2685085Swnj ti->ti_win = ntohs(ti->ti_win); 2695085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2705085Swnj 2715085Swnj /* 2728271Sroot * Locate pcb for segment. 2734924Swnj */ 27430525Skarels findpcb: 2755065Swnj inp = in_pcblookup 2766028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2776028Sroot INPLOOKUP_WILDCARD); 2785065Swnj 2795065Swnj /* 2805065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2815244Sroot * all data in the incoming segment is discarded. 28232098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 28332098Skarels * but should either do a listen or a connect soon. 2845065Swnj */ 2855300Sroot if (inp == 0) 2865085Swnj goto dropwithreset; 2875065Swnj tp = intotcpcb(inp); 2885300Sroot if (tp == 0) 2895085Swnj goto dropwithreset; 29032098Skarels if (tp->t_state == TCPS_CLOSED) 29132098Skarels goto drop; 2925109Swnj so = inp->inp_socket; 2935267Sroot if (so->so_options & SO_DEBUG) { 2945267Sroot ostate = tp->t_state; 2955267Sroot tcp_saveti = *ti; 2965267Sroot } 2977510Sroot if (so->so_options & SO_ACCEPTCONN) { 2987510Sroot so = sonewconn(so); 2997510Sroot if (so == 0) 3007510Sroot goto drop; 30110769Ssam /* 30210769Ssam * This is ugly, but .... 30310769Ssam * 30410769Ssam * Mark socket as temporary until we're 30510769Ssam * committed to keeping it. The code at 30610769Ssam * ``drop'' and ``dropwithreset'' check the 30710769Ssam * flag dropsocket to see if the temporary 30810769Ssam * socket created here should be discarded. 30910769Ssam * We mark the socket as discardable until 31010769Ssam * we're committed to it below in TCPS_LISTEN. 31110769Ssam */ 31210769Ssam dropsocket++; 3137510Sroot inp = (struct inpcb *)so->so_pcb; 3147510Sroot inp->inp_laddr = ti->ti_dst; 3157510Sroot inp->inp_lport = ti->ti_dport; 31624816Skarels inp->inp_options = ip_srcroute(); 3177510Sroot tp = intotcpcb(inp); 3187510Sroot tp->t_state = TCPS_LISTEN; 3197510Sroot } 3204601Swnj 3214601Swnj /* 3225162Swnj * Segment received on connection. 3235162Swnj * Reset idle time and keep-alive timer. 3245162Swnj */ 3255162Swnj tp->t_idle = 0; 3265162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 3275162Swnj 3285162Swnj /* 32917272Skarels * Process options if not in LISTEN state, 33017272Skarels * else do it below (after getting remote address). 3315440Swnj */ 33217272Skarels if (om && tp->t_state != TCPS_LISTEN) { 33317272Skarels tcp_dooptions(tp, om, ti); 3345440Swnj om = 0; 3355440Swnj } 3365440Swnj 3375440Swnj /* 3385085Swnj * Calculate amount of space in receive window, 3395085Swnj * and then do TCP input processing. 34024816Skarels * Receive window is amount of space in rcv queue, 34124816Skarels * but not less than advertised window. 3424601Swnj */ 34325939Skarels { int win; 3444601Swnj 34525939Skarels win = sbspace(&so->so_rcv); 34625939Skarels if (win < 0) 34725939Skarels win = 0; 34825939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 34925939Skarels } 35025939Skarels 3514601Swnj switch (tp->t_state) { 3524601Swnj 3535065Swnj /* 3545065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3555065Swnj * If the segment contains an ACK then it is bad and send a RST. 3565065Swnj * If it does not contain a SYN then it is not interesting; drop it. 35725197Skarels * Don't bother responding if the destination was a broadcast. 3585085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3595065Swnj * tp->iss, and send a segment: 3605085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3615065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3625065Swnj * Fill in remote peer address fields if not previously specified. 3635065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3645244Sroot * segment in this state. 3655065Swnj */ 3668271Sroot case TCPS_LISTEN: { 36710145Ssam struct mbuf *am; 3688271Sroot register struct sockaddr_in *sin; 3698271Sroot 3705065Swnj if (tiflags & TH_RST) 3715065Swnj goto drop; 3725300Sroot if (tiflags & TH_ACK) 3735085Swnj goto dropwithreset; 3745300Sroot if ((tiflags & TH_SYN) == 0) 3755065Swnj goto drop; 37625197Skarels if (in_broadcast(ti->ti_dst)) 37725197Skarels goto drop; 37810145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 37910145Ssam if (am == NULL) 38010145Ssam goto drop; 38110145Ssam am->m_len = sizeof (struct sockaddr_in); 3828599Sroot sin = mtod(am, struct sockaddr_in *); 3838271Sroot sin->sin_family = AF_INET; 3848271Sroot sin->sin_addr = ti->ti_src; 3858271Sroot sin->sin_port = ti->ti_sport; 3866028Sroot laddr = inp->inp_laddr; 38710145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3886028Sroot inp->inp_laddr = ti->ti_dst; 3898599Sroot if (in_pcbconnect(inp, am)) { 3906028Sroot inp->inp_laddr = laddr; 3918716Sroot (void) m_free(am); 3925244Sroot goto drop; 3936028Sroot } 3948716Sroot (void) m_free(am); 3955244Sroot tp->t_template = tcp_template(tp); 3965244Sroot if (tp->t_template == 0) { 39726386Skarels tp = tcp_drop(tp, ENOBUFS); 39817264Skarels dropsocket = 0; /* socket is already gone */ 3995244Sroot goto drop; 4005244Sroot } 40117272Skarels if (om) { 40217272Skarels tcp_dooptions(tp, om, ti); 40317272Skarels om = 0; 40417272Skarels } 40530525Skarels if (iss) 40630525Skarels tp->iss = iss; 40730525Skarels else 40830525Skarels tp->iss = tcp_iss; 40930525Skarels tcp_iss += TCP_ISSINCR/2; 4105065Swnj tp->irs = ti->ti_seq; 4115085Swnj tcp_sendseqinit(tp); 4125085Swnj tcp_rcvseqinit(tp); 41325939Skarels tp->t_flags |= TF_ACKNOW; 4145065Swnj tp->t_state = TCPS_SYN_RECEIVED; 4155244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 41610769Ssam dropsocket = 0; /* committed to socket */ 41730525Skarels tcpstat.tcps_accepts++; 4185085Swnj goto trimthenstep6; 4198271Sroot } 4204601Swnj 4215065Swnj /* 4225065Swnj * If the state is SYN_SENT: 4235065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4245065Swnj * if seg contains a RST, then drop the connection. 4255065Swnj * if seg does not contain SYN, then drop it. 4265065Swnj * Otherwise this is an acceptable SYN segment 4275065Swnj * initialize tp->rcv_nxt and tp->irs 4285065Swnj * if seg contains ack then advance tp->snd_una 4295065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4305065Swnj * arrange for segment to be acked (eventually) 4315065Swnj * continue processing rest of data/controls, beginning with URG 4325065Swnj */ 4335065Swnj case TCPS_SYN_SENT: 4345065Swnj if ((tiflags & TH_ACK) && 43524816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4365231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4375085Swnj goto dropwithreset; 4385065Swnj if (tiflags & TH_RST) { 43910394Ssam if (tiflags & TH_ACK) 44010394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4415065Swnj goto drop; 4424601Swnj } 4435065Swnj if ((tiflags & TH_SYN) == 0) 4445065Swnj goto drop; 44530974Skarels if (tiflags & TH_ACK) { 44630974Skarels tp->snd_una = ti->ti_ack; 44730974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 44830974Skarels tp->snd_nxt = tp->snd_una; 44930974Skarels } 4505244Sroot tp->t_timer[TCPT_REXMT] = 0; 4515065Swnj tp->irs = ti->ti_seq; 4525085Swnj tcp_rcvseqinit(tp); 4535085Swnj tp->t_flags |= TF_ACKNOW; 45430974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 45530525Skarels tcpstat.tcps_connects++; 4565244Sroot soisconnected(so); 4575065Swnj tp->t_state = TCPS_ESTABLISHED; 45817272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4595162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 46032098Skarels /* 46132098Skarels * if we didn't have to retransmit the SYN, 46232098Skarels * use its rtt as our initial srtt & rtt var. 46332098Skarels */ 46432098Skarels if (tp->t_rtt) { 46532098Skarels tp->t_srtt = tp->t_rtt << 3; 46632098Skarels tp->t_rttvar = tp->t_rtt << 1; 46732098Skarels TCPT_RANGESET(tp->t_rxtcur, 46832098Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 46932098Skarels TCPTV_MIN, TCPTV_REXMTMAX); 47032098Skarels tp->t_rtt = 0; 47132098Skarels } 4725162Swnj } else 4735085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4745085Swnj 4755085Swnj trimthenstep6: 4765085Swnj /* 4775231Swnj * Advance ti->ti_seq to correspond to first data byte. 4785085Swnj * If data, trim to stay within window, 4795085Swnj * dropping FIN if necessary. 4805085Swnj */ 4815231Swnj ti->ti_seq++; 4825085Swnj if (ti->ti_len > tp->rcv_wnd) { 4835085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4845085Swnj m_adj(m, -todrop); 4855085Swnj ti->ti_len = tp->rcv_wnd; 48625939Skarels tiflags &= ~TH_FIN; 48730525Skarels tcpstat.tcps_rcvpackafterwin++; 48830525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4895065Swnj } 4905263Swnj tp->snd_wl1 = ti->ti_seq - 1; 49125939Skarels tp->rcv_up = ti->ti_seq; 4925085Swnj goto step6; 4935065Swnj } 4944601Swnj 4955065Swnj /* 49630525Skarels * States other than LISTEN or SYN_SENT. 49730525Skarels * First check that at least some bytes of segment are within 49830525Skarels * receive window. If segment begins before rcv_nxt, 49930525Skarels * drop leading data (and SYN); if nothing left, just ack. 50016222Skarels */ 50130525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 50230525Skarels if (todrop > 0) { 50330525Skarels if (tiflags & TH_SYN) { 50430525Skarels tiflags &= ~TH_SYN; 50530525Skarels ti->ti_seq++; 50630525Skarels if (ti->ti_urp > 1) 50730525Skarels ti->ti_urp--; 50830525Skarels else 50930525Skarels tiflags &= ~TH_URG; 51030525Skarels todrop--; 51130525Skarels } 51230525Skarels if (todrop > ti->ti_len || 51330525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 51431730Skarels #ifdef TCP_COMPAT_42 51531730Skarels /* 51631730Skarels * Don't toss RST in response to 4.2-style keepalive. 51731730Skarels */ 51831730Skarels if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 51931730Skarels goto do_rst; 52031730Skarels #endif 52130525Skarels tcpstat.tcps_rcvduppack++; 52230525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 52332034Skarels todrop = ti->ti_len; 52432098Skarels tiflags &= ~TH_FIN; 52532034Skarels tp->t_flags |= TF_ACKNOW; 52632034Skarels } else { 52732034Skarels tcpstat.tcps_rcvpartduppack++; 52832034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 52930525Skarels } 53030525Skarels m_adj(m, todrop); 53130525Skarels ti->ti_seq += todrop; 53230525Skarels ti->ti_len -= todrop; 53330525Skarels if (ti->ti_urp > todrop) 53430525Skarels ti->ti_urp -= todrop; 53530525Skarels else { 53630525Skarels tiflags &= ~TH_URG; 53730525Skarels ti->ti_urp = 0; 53830525Skarels } 53916222Skarels } 54016222Skarels 541*32612Skarels /* 542*32612Skarels * If new data is received on a connection after the 543*32612Skarels * user processes are gone, then RST the other end. 544*32612Skarels */ 545*32612Skarels if ((so->so_state & SS_NOFDREF) && 546*32612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 547*32612Skarels tp = tcp_close(tp); 548*32612Skarels tcpstat.tcps_rcvafterclose++; 549*32612Skarels goto dropwithreset; 550*32612Skarels } 551*32612Skarels 5525065Swnj if (tp->rcv_wnd == 0) { 5535065Swnj /* 5545065Swnj * If window is closed can only take segments at 5555231Swnj * window edge, and have to drop data and PUSH from 5565065Swnj * incoming segments. 5575065Swnj */ 55830525Skarels if (tp->rcv_nxt != ti->ti_seq) { 55930525Skarels tcpstat.tcps_rcvpackafterwin++; 56030525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 5615065Swnj goto dropafterack; 56230525Skarels } 5635085Swnj if (ti->ti_len > 0) { 56430525Skarels if (ti->ti_len == 1) 56530525Skarels tcpstat.tcps_rcvwinprobe++; 56630525Skarels else { 56730525Skarels tcpstat.tcps_rcvpackafterwin++; 56830525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 56930525Skarels } 5705690Swnj m_adj(m, ti->ti_len); 5715085Swnj ti->ti_len = 0; 57225939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5735065Swnj } 5745065Swnj } else { 5755065Swnj /* 5765065Swnj * If segment ends after window, drop trailing data 5775085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5785065Swnj */ 5795690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5805690Swnj if (todrop > 0) { 58130525Skarels if (todrop >= ti->ti_len) { 58230525Skarels /* 58330525Skarels * If a new connection request is received 58430525Skarels * while in TIME_WAIT, drop the old connection 58530525Skarels * and start over if the sequence numbers 58630525Skarels * are above the previous ones. 58730525Skarels */ 58830525Skarels if (tiflags & TH_SYN && 58930525Skarels tp->t_state == TCPS_TIME_WAIT && 59030525Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 59130525Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 59230525Skarels (void) tcp_close(tp); 59330525Skarels goto findpcb; 59430525Skarels } 59530525Skarels if (todrop == 1) 59630525Skarels tcpstat.tcps_rcvwinprobe++; 59730525Skarels else { 59830525Skarels tcpstat.tcps_rcvpackafterwin++; 59931442Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 60030525Skarels } 6015065Swnj goto dropafterack; 60230525Skarels } 60330525Skarels tcpstat.tcps_rcvpackafterwin++; 60430525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 6055065Swnj m_adj(m, -todrop); 6065065Swnj ti->ti_len -= todrop; 60725939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6085065Swnj } 6095065Swnj } 6104601Swnj 61131730Skarels #ifdef TCP_COMPAT_42 61231730Skarels do_rst: 61331730Skarels #endif 6145065Swnj /* 6155065Swnj * If the RST bit is set examine the state: 6165065Swnj * SYN_RECEIVED STATE: 6175065Swnj * If passive open, return to LISTEN state. 6185065Swnj * If active open, inform user that connection was refused. 6195065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6205065Swnj * Inform user that connection was reset, and close tcb. 6215065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6225065Swnj * Close the tcb. 6235065Swnj */ 6245065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6255267Sroot 6265065Swnj case TCPS_SYN_RECEIVED: 62710394Ssam tp = tcp_drop(tp, ECONNREFUSED); 6285065Swnj goto drop; 6294601Swnj 6305065Swnj case TCPS_ESTABLISHED: 6315065Swnj case TCPS_FIN_WAIT_1: 6325065Swnj case TCPS_FIN_WAIT_2: 6335065Swnj case TCPS_CLOSE_WAIT: 63410394Ssam tp = tcp_drop(tp, ECONNRESET); 6355065Swnj goto drop; 6365065Swnj 6375065Swnj case TCPS_CLOSING: 6385065Swnj case TCPS_LAST_ACK: 6395065Swnj case TCPS_TIME_WAIT: 64010394Ssam tp = tcp_close(tp); 6415065Swnj goto drop; 6424601Swnj } 6434601Swnj 6444601Swnj /* 6455065Swnj * If a SYN is in the window, then this is an 6465065Swnj * error and we send an RST and drop the connection. 6474601Swnj */ 6485065Swnj if (tiflags & TH_SYN) { 64910394Ssam tp = tcp_drop(tp, ECONNRESET); 6505085Swnj goto dropwithreset; 6514601Swnj } 6524601Swnj 6534601Swnj /* 6545065Swnj * If the ACK bit is off we drop the segment and return. 6554601Swnj */ 6565085Swnj if ((tiflags & TH_ACK) == 0) 6575065Swnj goto drop; 6585065Swnj 6595065Swnj /* 6605065Swnj * Ack processing. 6615065Swnj */ 6624601Swnj switch (tp->t_state) { 6634601Swnj 6645065Swnj /* 6655065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 66631721Skarels * ESTABLISHED state and continue processing, otherwise 6675065Swnj * send an RST. 6685065Swnj */ 6695065Swnj case TCPS_SYN_RECEIVED: 6705085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6715231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6725085Swnj goto dropwithreset; 67330525Skarels tcpstat.tcps_connects++; 6745085Swnj soisconnected(so); 6755085Swnj tp->t_state = TCPS_ESTABLISHED; 67617272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6775162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6785244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6795085Swnj /* fall into ... */ 6804601Swnj 6815065Swnj /* 6825065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6835065Swnj * ACKs. If the ack is in the range 6845231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6855065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6865065Swnj * data from the retransmission queue. If this ACK reflects 6875065Swnj * more up to date window information we update our window information. 6885065Swnj */ 6895065Swnj case TCPS_ESTABLISHED: 6905065Swnj case TCPS_FIN_WAIT_1: 6915065Swnj case TCPS_FIN_WAIT_2: 6925065Swnj case TCPS_CLOSE_WAIT: 6935065Swnj case TCPS_CLOSING: 6945244Sroot case TCPS_LAST_ACK: 6955244Sroot case TCPS_TIME_WAIT: 6965085Swnj 69730525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 69832098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 69930525Skarels tcpstat.tcps_rcvdupack++; 70032098Skarels /* 70132098Skarels * If we have outstanding data (not a 70232098Skarels * window probe), this is a completely 70332098Skarels * duplicate ack (ie, window info didn't 70432098Skarels * change), the ack is the biggest we've 70532098Skarels * seen and we've seen exactly our rexmt 70632098Skarels * threshhold of them, assume a packet 70732098Skarels * has been dropped and retransmit it. 70832098Skarels * Kludge snd_nxt & the congestion 70932098Skarels * window so we send only this one 71032376Skarels * packet. If this packet fills the 71132376Skarels * only hole in the receiver's seq. 71232376Skarels * space, the next real ack will fully 71332376Skarels * open our window. This means we 71432376Skarels * have to do the usual slow-start to 71532376Skarels * not overwhelm an intermediate gateway 71632376Skarels * with a burst of packets. Leave 71732376Skarels * here with the congestion window set 71832376Skarels * to allow 2 packets on the next real 71932376Skarels * ack and the exp-to-linear thresh 72032376Skarels * set for half the current window 72132376Skarels * size (since we know we're losing at 72232376Skarels * the current window size). 72332098Skarels */ 72432098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 72532098Skarels ti->ti_ack != tp->snd_una) 72632098Skarels tp->t_dupacks = 0; 72732098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 72832098Skarels tcp_seq onxt = tp->snd_nxt; 72932376Skarels u_int win = 73032376Skarels MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 73132376Skarels tp->t_maxseg; 73232098Skarels 73332376Skarels if (win < 2) 73432376Skarels win = 2; 73532376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 73632376Skarels 73732098Skarels tp->t_timer[TCPT_REXMT] = 0; 73832098Skarels tp->t_rtt = 0; 73932098Skarels tp->snd_nxt = ti->ti_ack; 74032098Skarels tp->snd_cwnd = tp->t_maxseg; 74132098Skarels (void) tcp_output(tp); 74232098Skarels 74332098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 74432098Skarels tp->snd_nxt = onxt; 74532098Skarels goto drop; 74632098Skarels } 74732098Skarels } else 74832098Skarels tp->t_dupacks = 0; 7495065Swnj break; 75030525Skarels } 75132098Skarels tp->t_dupacks = 0; 75230525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 75330525Skarels tcpstat.tcps_rcvacktoomuch++; 7545065Swnj goto dropafterack; 75530525Skarels } 7565085Swnj acked = ti->ti_ack - tp->snd_una; 75730525Skarels tcpstat.tcps_rcvackpack++; 75830525Skarels tcpstat.tcps_rcvackbyte += acked; 7595951Swnj 7605951Swnj /* 7615951Swnj * If transmit timer is running and timed sequence 7625951Swnj * number was acked, update smoothed round trip time. 76332034Skarels * Since we now have an rtt measurement, cancel the 76432034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 76532034Skarels * Recompute the initial retransmit timer. 7665951Swnj */ 7675951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 76830525Skarels tcpstat.tcps_rttupdated++; 76931726Skarels if (tp->t_srtt != 0) { 77031726Skarels register short delta; 77131726Skarels 77231726Skarels /* 77331726Skarels * srtt is stored as fixed point with 3 bits 77431726Skarels * after the binary point (i.e., scaled by 8). 77531726Skarels * The following magic is equivalent 77631726Skarels * to the smoothing algorithm in rfc793 77731726Skarels * with an alpha of .875 77831726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 77932098Skarels * Adjust t_rtt to origin 0. 78031726Skarels */ 78132098Skarels tp->t_rtt--; 78231726Skarels delta = tp->t_rtt - (tp->t_srtt >> 3); 78331726Skarels if ((tp->t_srtt += delta) <= 0) 78431726Skarels tp->t_srtt = 1; 78531726Skarels /* 78632034Skarels * We accumulate a smoothed rtt variance 78732034Skarels * (actually, a smoothed mean difference), 78831726Skarels * then set the retransmit timer to smoothed 78931726Skarels * rtt + 2 times the smoothed variance. 79032098Skarels * rttvar is stored as fixed point 79131726Skarels * with 2 bits after the binary point 79231726Skarels * (scaled by 4). The following is equivalent 79331726Skarels * to rfc793 smoothing with an alpha of .75 79431726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 79531726Skarels * This replaces rfc793's wired-in beta. 79631726Skarels */ 79731726Skarels if (delta < 0) 79831726Skarels delta = -delta; 79931726Skarels delta -= (tp->t_rttvar >> 2); 80031726Skarels if ((tp->t_rttvar += delta) <= 0) 80131726Skarels tp->t_rttvar = 1; 80231726Skarels } else { 80331726Skarels /* 80431726Skarels * No rtt measurement yet - use the 80531726Skarels * unsmoothed rtt. Set the variance 80631726Skarels * to half the rtt (so our first 80731726Skarels * retransmit happens at 2*rtt) 80831726Skarels */ 80931726Skarels tp->t_srtt = tp->t_rtt << 3; 81031726Skarels tp->t_rttvar = tp->t_rtt << 1; 81131726Skarels } 8125951Swnj tp->t_rtt = 0; 81332034Skarels tp->t_rxtshift = 0; 81432034Skarels TCPT_RANGESET(tp->t_rxtcur, 81532034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 81632034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 8175951Swnj } 8185951Swnj 81926824Skarels /* 82026824Skarels * If all outstanding data is acked, stop retransmit 82126824Skarels * timer and remember to restart (more output or persist). 82226824Skarels * If there is more data to be acked, restart retransmit 82332034Skarels * timer, using current (possibly backed-off) value. 82426824Skarels */ 82526824Skarels if (ti->ti_ack == tp->snd_max) { 8265244Sroot tp->t_timer[TCPT_REXMT] = 0; 82726824Skarels needoutput = 1; 82832034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 82932034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 83017360Skarels /* 83132098Skarels * When new data is acked, open the congestion window. 83232098Skarels * If the window gives us less than ssthresh packets 83332098Skarels * in flight, open exponentially (maxseg per packet). 83432098Skarels * Otherwise open linearly (maxseg per window, 83532098Skarels * or maxseg^2 / cwnd per packet). 83617360Skarels */ 83732098Skarels { 83832098Skarels u_int incr = tp->t_maxseg; 83932098Skarels 84032098Skarels if (tp->snd_cwnd > tp->snd_ssthresh) 84132098Skarels incr = MAX(incr * incr / tp->snd_cwnd, 1); 84232098Skarels 84332098Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */ 84432098Skarels } 8455307Sroot if (acked > so->so_snd.sb_cc) { 84615386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 84726386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 84831721Skarels ourfinisacked = 1; 8495307Sroot } else { 8506161Ssam sbdrop(&so->so_snd, acked); 8515307Sroot tp->snd_wnd -= acked; 85231721Skarels ourfinisacked = 0; 8535307Sroot } 8546434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 8555300Sroot sowwakeup(so); 8565231Swnj tp->snd_una = ti->ti_ack; 8575357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 8585357Sroot tp->snd_nxt = tp->snd_una; 8595162Swnj 8604601Swnj switch (tp->t_state) { 8614601Swnj 8625065Swnj /* 8635065Swnj * In FIN_WAIT_1 STATE in addition to the processing 8645065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 8655085Swnj * then enter FIN_WAIT_2. 8665065Swnj */ 8675065Swnj case TCPS_FIN_WAIT_1: 8685896Swnj if (ourfinisacked) { 8695896Swnj /* 8705896Swnj * If we can't receive any more 8715896Swnj * data, then closing user can proceed. 87224816Skarels * Starting the timer is contrary to the 87324816Skarels * specification, but if we don't get a FIN 87424816Skarels * we'll hang forever. 8755896Swnj */ 87624816Skarels if (so->so_state & SS_CANTRCVMORE) { 8775896Swnj soisdisconnected(so); 87824816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 87924816Skarels } 8805085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8815896Swnj } 8824601Swnj break; 8834601Swnj 8845065Swnj /* 8855065Swnj * In CLOSING STATE in addition to the processing for 8865065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8875065Swnj * then enter the TIME-WAIT state, otherwise ignore 8885065Swnj * the segment. 8895065Swnj */ 8905065Swnj case TCPS_CLOSING: 8915244Sroot if (ourfinisacked) { 8925065Swnj tp->t_state = TCPS_TIME_WAIT; 8935244Sroot tcp_canceltimers(tp); 8945244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8955244Sroot soisdisconnected(so); 8965244Sroot } 8975244Sroot break; 8984601Swnj 8995065Swnj /* 90031743Skarels * In LAST_ACK, we may still be waiting for data to drain 90131743Skarels * and/or to be acked, as well as for the ack of our FIN. 90231743Skarels * If our FIN is now acknowledged, delete the TCB, 90331743Skarels * enter the closed state and return. 9045065Swnj */ 9055065Swnj case TCPS_LAST_ACK: 90631743Skarels if (ourfinisacked) { 90710394Ssam tp = tcp_close(tp); 90831743Skarels goto drop; 90931743Skarels } 91031743Skarels break; 9114601Swnj 9125065Swnj /* 9135065Swnj * In TIME_WAIT state the only thing that should arrive 9145065Swnj * is a retransmission of the remote FIN. Acknowledge 9155065Swnj * it and restart the finack timer. 9165065Swnj */ 9175065Swnj case TCPS_TIME_WAIT: 9185162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9195065Swnj goto dropafterack; 9204601Swnj } 9215085Swnj } 9224601Swnj 9235065Swnj step6: 9245065Swnj /* 9255244Sroot * Update window information. 92625939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9275244Sroot */ 92825939Skarels if ((tiflags & TH_ACK) && 92925939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9305391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 93125939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 93230525Skarels /* keep track of pure window updates */ 93330525Skarels if (ti->ti_len == 0 && 93432098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 93530525Skarels tcpstat.tcps_rcvwinupd++; 9365244Sroot tp->snd_wnd = ti->ti_win; 9375244Sroot tp->snd_wl1 = ti->ti_seq; 9385244Sroot tp->snd_wl2 = ti->ti_ack; 93925260Skarels if (tp->snd_wnd > tp->max_sndwnd) 94025260Skarels tp->max_sndwnd = tp->snd_wnd; 94126824Skarels needoutput = 1; 94226824Skarels } 9435244Sroot 9445244Sroot /* 9455547Swnj * Process segments with URG. 9465065Swnj */ 9477267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9487267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9495547Swnj /* 95025939Skarels * This is a kludge, but if we receive and accept 95113121Ssam * random urgent pointers, we'll crash in 95213121Ssam * soreceive. It's hard to imagine someone 95313121Ssam * actually wanting to send this much urgent data. 95412441Ssam */ 95525652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 95612441Ssam ti->ti_urp = 0; /* XXX */ 95712441Ssam tiflags &= ~TH_URG; /* XXX */ 95825939Skarels goto dodata; /* XXX */ 95912441Ssam } 96012441Ssam /* 9615547Swnj * If this segment advances the known urgent pointer, 9625547Swnj * then mark the data stream. This should not happen 9635547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 9645547Swnj * a FIN has been received from the remote side. 9655547Swnj * In these states we ignore the URG. 96627190Skarels * 96727190Skarels * According to RFC961 (Assigned Protocols), 96827190Skarels * the urgent pointer points to the last octet 96927190Skarels * of urgent data. We continue, however, 97027190Skarels * to consider it to indicate the first octet 97127190Skarels * of data past the urgent section 97227190Skarels * as the original spec states. 9735547Swnj */ 9745547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9755547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9765547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9775547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9785547Swnj if (so->so_oobmark == 0) 9795547Swnj so->so_state |= SS_RCVATMARK; 9808313Sroot sohasoutofband(so); 98124816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9825440Swnj } 9835547Swnj /* 9845547Swnj * Remove out of band data so doesn't get presented to user. 9855547Swnj * This can happen independent of advancing the URG pointer, 9865547Swnj * but if two URG's are pending at once, some out-of-band 9875547Swnj * data may creep in... ick. 9885547Swnj */ 98927190Skarels if (ti->ti_urp <= ti->ti_len && 99027190Skarels (so->so_options & SO_OOBINLINE) == 0) 9915547Swnj tcp_pulloutofband(so, ti); 99225939Skarels } else 99325939Skarels /* 99425939Skarels * If no out of band data is expected, 99525939Skarels * pull receive urgent pointer along 99625939Skarels * with the receive window. 99725939Skarels */ 99825939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 99925939Skarels tp->rcv_up = tp->rcv_nxt; 100025939Skarels dodata: /* XXX */ 10014601Swnj 10024601Swnj /* 10035065Swnj * Process the segment text, merging it into the TCP sequencing queue, 10045065Swnj * and arranging for acknowledgment of receipt if necessary. 10055065Swnj * This process logically involves adjusting tp->rcv_wnd as data 10065065Swnj * is presented to the user (this happens in tcp_usrreq.c, 10075065Swnj * case PRU_RCVD). If a FIN has already been received on this 10085065Swnj * connection then we just ignore the text. 10094601Swnj */ 101017946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 101117946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 101224816Skarels TCP_REASS(tp, ti, m, so, tiflags); 10135440Swnj if (tcpnodelack == 0) 10145440Swnj tp->t_flags |= TF_DELACK; 10155440Swnj else 10165440Swnj tp->t_flags |= TF_ACKNOW; 101725260Skarels /* 101825260Skarels * Note the amount of data that peer has sent into 101925260Skarels * our window, in order to estimate the sender's 102025260Skarels * buffer size. 102125260Skarels */ 102232098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 102325260Skarels if (len > tp->max_rcvd) 102425260Skarels tp->max_rcvd = len; 10255244Sroot } else { 10264924Swnj m_freem(m); 10275263Swnj tiflags &= ~TH_FIN; 10285244Sroot } 10294601Swnj 10304601Swnj /* 10315263Swnj * If FIN is received ACK the FIN and let the user know 10325263Swnj * that the connection is closing. 10334601Swnj */ 10345263Swnj if (tiflags & TH_FIN) { 10355244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10365244Sroot socantrcvmore(so); 10375244Sroot tp->t_flags |= TF_ACKNOW; 10385244Sroot tp->rcv_nxt++; 10395244Sroot } 10405065Swnj switch (tp->t_state) { 10414601Swnj 10425065Swnj /* 10435065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10445065Swnj * enter the CLOSE_WAIT state. 10454884Swnj */ 10465065Swnj case TCPS_SYN_RECEIVED: 10475065Swnj case TCPS_ESTABLISHED: 10485065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10495065Swnj break; 10504884Swnj 10515065Swnj /* 10525085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10535085Swnj * enter the CLOSING state. 10544884Swnj */ 10555065Swnj case TCPS_FIN_WAIT_1: 10565085Swnj tp->t_state = TCPS_CLOSING; 10575065Swnj break; 10584601Swnj 10595065Swnj /* 10605065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 10615065Swnj * starting the time-wait timer, turning off the other 10625065Swnj * standard timers. 10635065Swnj */ 10645065Swnj case TCPS_FIN_WAIT_2: 10655244Sroot tp->t_state = TCPS_TIME_WAIT; 10665074Swnj tcp_canceltimers(tp); 10675162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10685244Sroot soisdisconnected(so); 10695065Swnj break; 10705065Swnj 10714884Swnj /* 10725065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10734884Swnj */ 10745065Swnj case TCPS_TIME_WAIT: 10755162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10765065Swnj break; 10775085Swnj } 10784601Swnj } 10795267Sroot if (so->so_options & SO_DEBUG) 10805267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10815085Swnj 10825085Swnj /* 10835085Swnj * Return any desired output. 10845085Swnj */ 108526824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 108625939Skarels (void) tcp_output(tp); 10875065Swnj return; 10885085Swnj 10895065Swnj dropafterack: 10905085Swnj /* 10916211Swnj * Generate an ACK dropping incoming segment if it occupies 10926211Swnj * sequence space, where the ACK reflects our state. 10935085Swnj */ 109426057Skarels if (tiflags & TH_RST) 10955085Swnj goto drop; 109631749Skarels m_freem(m); 109731721Skarels tp->t_flags |= TF_ACKNOW; 109831721Skarels (void) tcp_output(tp); 10995231Swnj return; 11005085Swnj 11015085Swnj dropwithreset: 110211731Ssam if (om) { 11036161Ssam (void) m_free(om); 110411731Ssam om = 0; 110511731Ssam } 11065085Swnj /* 11075244Sroot * Generate a RST, dropping incoming segment. 11085085Swnj * Make ACK acceptable to originator of segment. 110925197Skarels * Don't bother to respond if destination was broadcast. 11105085Swnj */ 111125197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 11125085Swnj goto drop; 11135085Swnj if (tiflags & TH_ACK) 11145391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 11155085Swnj else { 11165085Swnj if (tiflags & TH_SYN) 11175085Swnj ti->ti_len++; 11186211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11196211Swnj TH_RST|TH_ACK); 11205085Swnj } 112110769Ssam /* destroy temporarily created socket */ 112210769Ssam if (dropsocket) 112310769Ssam (void) soabort(so); 11245231Swnj return; 11255085Swnj 11265065Swnj drop: 112711730Ssam if (om) 112811730Ssam (void) m_free(om); 11295085Swnj /* 11305085Swnj * Drop space held by incoming segment and return. 11315085Swnj */ 11326303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11336303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11345065Swnj m_freem(m); 113510769Ssam /* destroy temporarily created socket */ 113610769Ssam if (dropsocket) 113710769Ssam (void) soabort(so); 11385267Sroot return; 11395065Swnj } 11405065Swnj 114117272Skarels tcp_dooptions(tp, om, ti) 11425440Swnj struct tcpcb *tp; 11435440Swnj struct mbuf *om; 114417272Skarels struct tcpiphdr *ti; 11455419Swnj { 11465440Swnj register u_char *cp; 11475440Swnj int opt, optlen, cnt; 11485419Swnj 11495440Swnj cp = mtod(om, u_char *); 11505440Swnj cnt = om->m_len; 11515440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11525440Swnj opt = cp[0]; 11535440Swnj if (opt == TCPOPT_EOL) 11545440Swnj break; 11555440Swnj if (opt == TCPOPT_NOP) 11565440Swnj optlen = 1; 115712169Ssam else { 11585440Swnj optlen = cp[1]; 115912169Ssam if (optlen <= 0) 116012169Ssam break; 116112169Ssam } 11625440Swnj switch (opt) { 11635440Swnj 11645440Swnj default: 11655440Swnj break; 11665440Swnj 11675440Swnj case TCPOPT_MAXSEG: 11685440Swnj if (optlen != 4) 11695440Swnj continue; 117017272Skarels if (!(ti->ti_flags & TH_SYN)) 117117272Skarels continue; 11725440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11736161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 117417272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 11755440Swnj break; 11765419Swnj } 11775419Swnj } 11786161Ssam (void) m_free(om); 11795419Swnj } 11805419Swnj 11815419Swnj /* 11825547Swnj * Pull out of band byte out of a segment so 11835547Swnj * it doesn't appear in the user's data queue. 11845547Swnj * It is still reflected in the segment length for 11855547Swnj * sequencing purposes. 11865547Swnj */ 11875547Swnj tcp_pulloutofband(so, ti) 11885547Swnj struct socket *so; 11895547Swnj struct tcpiphdr *ti; 11905547Swnj { 11915547Swnj register struct mbuf *m; 11926116Swnj int cnt = ti->ti_urp - 1; 11935547Swnj 11945547Swnj m = dtom(ti); 11955547Swnj while (cnt >= 0) { 11965547Swnj if (m->m_len > cnt) { 11975547Swnj char *cp = mtod(m, caddr_t) + cnt; 11985547Swnj struct tcpcb *tp = sototcpcb(so); 11995547Swnj 12005547Swnj tp->t_iobc = *cp; 12015547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 12026161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 12035547Swnj m->m_len--; 12045547Swnj return; 12055547Swnj } 12065547Swnj cnt -= m->m_len; 12075547Swnj m = m->m_next; 12085547Swnj if (m == 0) 12095547Swnj break; 12105547Swnj } 12115547Swnj panic("tcp_pulloutofband"); 12125547Swnj } 12135547Swnj 12145547Swnj /* 121517272Skarels * Determine a reasonable value for maxseg size. 121617272Skarels * If the route is known, use one that can be handled 121717272Skarels * on the given interface without forcing IP to fragment. 121831726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 121931726Skarels * to utilize large mbufs. 122017272Skarels * If interface pointer is unavailable, or the destination isn't local, 122123975Skarels * use a conservative size (512 or the default IP max size, but no more 122223975Skarels * than the mtu of the interface through which we route), 122317272Skarels * as we can't discover anything about intervening gateways or networks. 122432034Skarels * We also initialize the congestion/slow start window to be a single 122532034Skarels * segment if the destination isn't local; this information should 122632034Skarels * probably all be saved with the routing entry at the transport level. 122717272Skarels * 122817272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 122917272Skarels */ 123017272Skarels tcp_mss(tp) 123123975Skarels register struct tcpcb *tp; 123217272Skarels { 123317272Skarels struct route *ro; 123417272Skarels struct ifnet *ifp; 123517272Skarels int mss; 123617272Skarels struct inpcb *inp; 123717272Skarels 123817272Skarels inp = tp->t_inpcb; 123917272Skarels ro = &inp->inp_route; 124017272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 124117272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 124217272Skarels /* No route yet, so try to acquire one */ 124317272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 124417272Skarels ro->ro_dst.sa_family = AF_INET; 124517272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 124617272Skarels inp->inp_faddr; 124717272Skarels rtalloc(ro); 124817272Skarels } 124917272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 125017316Skarels return (TCP_MSS); 125117272Skarels } 125217272Skarels 125317272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 125431726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 125531726Skarels if (mss > MCLBYTES) 125631726Skarels mss &= ~(MCLBYTES-1); 125717272Skarels #else 125831726Skarels if (mss > MCLBYTES) 125931726Skarels mss = mss / MCLBYTES * MCLBYTES; 126017272Skarels #endif 126123975Skarels if (in_localaddr(inp->inp_faddr)) 126223975Skarels return (mss); 126332098Skarels 126432034Skarels mss = MIN(mss, TCP_MSS); 126532034Skarels tp->snd_cwnd = mss; 126632034Skarels return (mss); 126717272Skarels } 1268