123190Smckusick /* 229149Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 3*32787Sbostic * All rights reserved. 423190Smckusick * 5*32787Sbostic * Redistribution and use in source and binary forms are permitted 6*32787Sbostic * provided that this notice is preserved and that due credit is given 7*32787Sbostic * to the University of California at Berkeley. The name of the University 8*32787Sbostic * may not be used to endorse or promote products derived from this 9*32787Sbostic * software without specific prior written permission. This software 10*32787Sbostic * is provided ``as is'' without express or implied warranty. 11*32787Sbostic * 12*32787Sbostic * @(#)tcp_input.c 7.14 (Berkeley) 12/07/87 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; 435440Swnj extern tcpnodelack; 444601Swnj 455267Sroot struct tcpcb *tcp_newtcpcb(); 4624816Skarels 475065Swnj /* 4824816Skarels * Insert segment ti into reassembly queue of tcp with 4924816Skarels * control block tp. Return TH_FIN if reassembly now includes 5024816Skarels * a segment with FIN. The macro form does the common case inline 5124816Skarels * (segment is the next to be received on an established connection, 5224816Skarels * and the queue is empty), avoiding linkage into and removal 5324816Skarels * from the queue and repetition of various conversions. 5424816Skarels */ 5524816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 5624816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5724816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5824816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 5924816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6024816Skarels flags = (ti)->ti_flags & TH_FIN; \ 6130525Skarels tcpstat.tcps_rcvpack++;\ 6230525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 6324816Skarels sbappend(&(so)->so_rcv, (m)); \ 6424816Skarels sorwakeup(so); \ 6524816Skarels } else \ 6624816Skarels (flags) = tcp_reass((tp), (ti)); \ 6724816Skarels } 6824816Skarels 6924816Skarels tcp_reass(tp, ti) 7024816Skarels register struct tcpcb *tp; 7124816Skarels register struct tcpiphdr *ti; 7224816Skarels { 7324816Skarels register struct tcpiphdr *q; 7424816Skarels struct socket *so = tp->t_inpcb->inp_socket; 7524816Skarels struct mbuf *m; 7624816Skarels int flags; 7724816Skarels 7824816Skarels /* 7924816Skarels * Call with ti==0 after become established to 8024816Skarels * force pre-ESTABLISHED data up to user socket. 8124816Skarels */ 8224816Skarels if (ti == 0) 8324816Skarels goto present; 8424816Skarels 8524816Skarels /* 8624816Skarels * Find a segment which begins after this one does. 8724816Skarels */ 8824816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8924816Skarels q = (struct tcpiphdr *)q->ti_next) 9024816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 9124816Skarels break; 9224816Skarels 9324816Skarels /* 9424816Skarels * If there is a preceding segment, it may provide some of 9524816Skarels * our data already. If so, drop the data from the incoming 9624816Skarels * segment. If it provides all of our data, drop us. 9724816Skarels */ 9824816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 9924816Skarels register int i; 10024816Skarels q = (struct tcpiphdr *)q->ti_prev; 10124816Skarels /* conversion to int (in i) handles seq wraparound */ 10224816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 10324816Skarels if (i > 0) { 10430525Skarels if (i >= ti->ti_len) { 10530525Skarels tcpstat.tcps_rcvduppack++; 10630525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10724816Skarels goto drop; 10830525Skarels } 10924816Skarels m_adj(dtom(ti), i); 11024816Skarels ti->ti_len -= i; 11124816Skarels ti->ti_seq += i; 11224816Skarels } 11324816Skarels q = (struct tcpiphdr *)(q->ti_next); 11424816Skarels } 11530525Skarels tcpstat.tcps_rcvoopack++; 11630525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 11724816Skarels 11824816Skarels /* 11924816Skarels * While we overlap succeeding segments trim them or, 12024816Skarels * if they are completely covered, dequeue them. 12124816Skarels */ 12224816Skarels while (q != (struct tcpiphdr *)tp) { 12324816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 12424816Skarels if (i <= 0) 12524816Skarels break; 12624816Skarels if (i < q->ti_len) { 12724816Skarels q->ti_seq += i; 12824816Skarels q->ti_len -= i; 12924816Skarels m_adj(dtom(q), i); 13024816Skarels break; 13124816Skarels } 13224816Skarels q = (struct tcpiphdr *)q->ti_next; 13324816Skarels m = dtom(q->ti_prev); 13424816Skarels remque(q->ti_prev); 13524816Skarels m_freem(m); 13624816Skarels } 13724816Skarels 13824816Skarels /* 13924816Skarels * Stick new segment in its place. 14024816Skarels */ 14124816Skarels insque(ti, q->ti_prev); 14224816Skarels 14324816Skarels present: 14424816Skarels /* 14524816Skarels * Present data to user, advancing rcv_nxt through 14624816Skarels * completed sequence space. 14724816Skarels */ 14824816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 14924816Skarels return (0); 15024816Skarels ti = tp->seg_next; 15124816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 15224816Skarels return (0); 15324816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 15424816Skarels return (0); 15524816Skarels do { 15624816Skarels tp->rcv_nxt += ti->ti_len; 15724816Skarels flags = ti->ti_flags & TH_FIN; 15824816Skarels remque(ti); 15924816Skarels m = dtom(ti); 16024816Skarels ti = (struct tcpiphdr *)ti->ti_next; 16124816Skarels if (so->so_state & SS_CANTRCVMORE) 16224816Skarels m_freem(m); 16324816Skarels else 16424816Skarels sbappend(&so->so_rcv, m); 16524816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 16624816Skarels sorwakeup(so); 16724816Skarels return (flags); 16824816Skarels drop: 16924816Skarels m_freem(dtom(ti)); 17024816Skarels return (0); 17124816Skarels } 17224816Skarels 17324816Skarels /* 1745065Swnj * TCP input routine, follows pages 65-76 of the 1755065Swnj * protocol specification dated September, 1981 very closely. 1765065Swnj */ 1774924Swnj tcp_input(m0) 1784924Swnj struct mbuf *m0; 1794601Swnj { 1804924Swnj register struct tcpiphdr *ti; 1814924Swnj struct inpcb *inp; 1824924Swnj register struct mbuf *m; 1835440Swnj struct mbuf *om = 0; 1844924Swnj int len, tlen, off; 1855391Swnj register struct tcpcb *tp = 0; 1864924Swnj register int tiflags; 1874803Swnj struct socket *so; 18831721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1895267Sroot short ostate; 1906028Sroot struct in_addr laddr; 19110769Ssam int dropsocket = 0; 19230525Skarels int iss = 0; 1934924Swnj 19430525Skarels tcpstat.tcps_rcvtotal++; 1954924Swnj /* 1965244Sroot * Get IP and TCP header together in first mbuf. 1975244Sroot * Note: IP leaves IP header in first mbuf. 1984924Swnj */ 1994924Swnj m = m0; 2005020Sroot ti = mtod(m, struct tcpiphdr *); 2015244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 2025208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 2035307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 2045307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 20530525Skarels tcpstat.tcps_rcvshort++; 2065307Sroot return; 2075085Swnj } 2085085Swnj ti = mtod(m, struct tcpiphdr *); 2095085Swnj } 2104601Swnj 2114601Swnj /* 2125244Sroot * Checksum extended TCP header and data. 2134601Swnj */ 2144924Swnj tlen = ((struct ip *)ti)->ip_len; 2154924Swnj len = sizeof (struct ip) + tlen; 2164679Swnj if (tcpcksum) { 2174924Swnj ti->ti_next = ti->ti_prev = 0; 2184924Swnj ti->ti_x1 = 0; 2195223Swnj ti->ti_len = (u_short)tlen; 2206161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2215231Swnj if (ti->ti_sum = in_cksum(m, len)) { 22211830Ssam if (tcpprintfs) 22311830Ssam printf("tcp sum: src %x\n", ti->ti_src); 22430525Skarels tcpstat.tcps_rcvbadsum++; 2255085Swnj goto drop; 2264601Swnj } 2274601Swnj } 2284601Swnj 2294601Swnj /* 2305244Sroot * Check that TCP offset makes sense, 2315440Swnj * pull out TCP options and adjust length. 2324601Swnj */ 2334924Swnj off = ti->ti_off << 2; 2345231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 23511830Ssam if (tcpprintfs) 23611830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 23730525Skarels tcpstat.tcps_rcvbadoff++; 2385085Swnj goto drop; 2394924Swnj } 2406211Swnj tlen -= off; 2416211Swnj ti->ti_len = tlen; 2425440Swnj if (off > sizeof (struct tcphdr)) { 24324816Skarels if (m->m_len < sizeof(struct ip) + off) { 24424816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 24530525Skarels tcpstat.tcps_rcvshort++; 24624816Skarels return; 24724816Skarels } 24824816Skarels ti = mtod(m, struct tcpiphdr *); 2495440Swnj } 2509642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2515440Swnj if (om == 0) 2525440Swnj goto drop; 2535440Swnj om->m_len = off - sizeof (struct tcphdr); 2545440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2556161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2565440Swnj m->m_len -= om->m_len; 2576161Ssam bcopy(op+om->m_len, op, 2586161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2595440Swnj } 2605440Swnj } 2615065Swnj tiflags = ti->ti_flags; 2624924Swnj 2636093Sroot /* 26425729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2656093Sroot */ 26625729Smckusick m->m_off += sizeof(struct tcpiphdr); 26725729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2686093Sroot 2694924Swnj /* 2705244Sroot * Convert TCP protocol specific fields to host format. 2715085Swnj */ 2725085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2735085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2745085Swnj ti->ti_win = ntohs(ti->ti_win); 2755085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2765085Swnj 2775085Swnj /* 2788271Sroot * Locate pcb for segment. 2794924Swnj */ 28030525Skarels findpcb: 2815065Swnj inp = in_pcblookup 2826028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2836028Sroot INPLOOKUP_WILDCARD); 2845065Swnj 2855065Swnj /* 2865065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2875244Sroot * all data in the incoming segment is discarded. 28832098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 28932098Skarels * but should either do a listen or a connect soon. 2905065Swnj */ 2915300Sroot if (inp == 0) 2925085Swnj goto dropwithreset; 2935065Swnj tp = intotcpcb(inp); 2945300Sroot if (tp == 0) 2955085Swnj goto dropwithreset; 29632098Skarels if (tp->t_state == TCPS_CLOSED) 29732098Skarels goto drop; 2985109Swnj so = inp->inp_socket; 2995267Sroot if (so->so_options & SO_DEBUG) { 3005267Sroot ostate = tp->t_state; 3015267Sroot tcp_saveti = *ti; 3025267Sroot } 3037510Sroot if (so->so_options & SO_ACCEPTCONN) { 3047510Sroot so = sonewconn(so); 3057510Sroot if (so == 0) 3067510Sroot goto drop; 30710769Ssam /* 30810769Ssam * This is ugly, but .... 30910769Ssam * 31010769Ssam * Mark socket as temporary until we're 31110769Ssam * committed to keeping it. The code at 31210769Ssam * ``drop'' and ``dropwithreset'' check the 31310769Ssam * flag dropsocket to see if the temporary 31410769Ssam * socket created here should be discarded. 31510769Ssam * We mark the socket as discardable until 31610769Ssam * we're committed to it below in TCPS_LISTEN. 31710769Ssam */ 31810769Ssam dropsocket++; 3197510Sroot inp = (struct inpcb *)so->so_pcb; 3207510Sroot inp->inp_laddr = ti->ti_dst; 3217510Sroot inp->inp_lport = ti->ti_dport; 32224816Skarels inp->inp_options = ip_srcroute(); 3237510Sroot tp = intotcpcb(inp); 3247510Sroot tp->t_state = TCPS_LISTEN; 3257510Sroot } 3264601Swnj 3274601Swnj /* 3285162Swnj * Segment received on connection. 3295162Swnj * Reset idle time and keep-alive timer. 3305162Swnj */ 3315162Swnj tp->t_idle = 0; 3325162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 3335162Swnj 3345162Swnj /* 33517272Skarels * Process options if not in LISTEN state, 33617272Skarels * else do it below (after getting remote address). 3375440Swnj */ 33817272Skarels if (om && tp->t_state != TCPS_LISTEN) { 33917272Skarels tcp_dooptions(tp, om, ti); 3405440Swnj om = 0; 3415440Swnj } 3425440Swnj 3435440Swnj /* 3445085Swnj * Calculate amount of space in receive window, 3455085Swnj * and then do TCP input processing. 34624816Skarels * Receive window is amount of space in rcv queue, 34724816Skarels * but not less than advertised window. 3484601Swnj */ 34925939Skarels { int win; 3504601Swnj 35125939Skarels win = sbspace(&so->so_rcv); 35225939Skarels if (win < 0) 35325939Skarels win = 0; 35425939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 35525939Skarels } 35625939Skarels 3574601Swnj switch (tp->t_state) { 3584601Swnj 3595065Swnj /* 3605065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3615065Swnj * If the segment contains an ACK then it is bad and send a RST. 3625065Swnj * If it does not contain a SYN then it is not interesting; drop it. 36325197Skarels * Don't bother responding if the destination was a broadcast. 3645085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3655065Swnj * tp->iss, and send a segment: 3665085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3675065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3685065Swnj * Fill in remote peer address fields if not previously specified. 3695065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3705244Sroot * segment in this state. 3715065Swnj */ 3728271Sroot case TCPS_LISTEN: { 37310145Ssam struct mbuf *am; 3748271Sroot register struct sockaddr_in *sin; 3758271Sroot 3765065Swnj if (tiflags & TH_RST) 3775065Swnj goto drop; 3785300Sroot if (tiflags & TH_ACK) 3795085Swnj goto dropwithreset; 3805300Sroot if ((tiflags & TH_SYN) == 0) 3815065Swnj goto drop; 38225197Skarels if (in_broadcast(ti->ti_dst)) 38325197Skarels goto drop; 38410145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 38510145Ssam if (am == NULL) 38610145Ssam goto drop; 38710145Ssam am->m_len = sizeof (struct sockaddr_in); 3888599Sroot sin = mtod(am, struct sockaddr_in *); 3898271Sroot sin->sin_family = AF_INET; 3908271Sroot sin->sin_addr = ti->ti_src; 3918271Sroot sin->sin_port = ti->ti_sport; 3926028Sroot laddr = inp->inp_laddr; 39310145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3946028Sroot inp->inp_laddr = ti->ti_dst; 3958599Sroot if (in_pcbconnect(inp, am)) { 3966028Sroot inp->inp_laddr = laddr; 3978716Sroot (void) m_free(am); 3985244Sroot goto drop; 3996028Sroot } 4008716Sroot (void) m_free(am); 4015244Sroot tp->t_template = tcp_template(tp); 4025244Sroot if (tp->t_template == 0) { 40326386Skarels tp = tcp_drop(tp, ENOBUFS); 40417264Skarels dropsocket = 0; /* socket is already gone */ 4055244Sroot goto drop; 4065244Sroot } 40717272Skarels if (om) { 40817272Skarels tcp_dooptions(tp, om, ti); 40917272Skarels om = 0; 41017272Skarels } 41130525Skarels if (iss) 41230525Skarels tp->iss = iss; 41330525Skarels else 41430525Skarels tp->iss = tcp_iss; 41530525Skarels tcp_iss += TCP_ISSINCR/2; 4165065Swnj tp->irs = ti->ti_seq; 4175085Swnj tcp_sendseqinit(tp); 4185085Swnj tcp_rcvseqinit(tp); 41925939Skarels tp->t_flags |= TF_ACKNOW; 4205065Swnj tp->t_state = TCPS_SYN_RECEIVED; 4215244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 42210769Ssam dropsocket = 0; /* committed to socket */ 42330525Skarels tcpstat.tcps_accepts++; 4245085Swnj goto trimthenstep6; 4258271Sroot } 4264601Swnj 4275065Swnj /* 4285065Swnj * If the state is SYN_SENT: 4295065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4305065Swnj * if seg contains a RST, then drop the connection. 4315065Swnj * if seg does not contain SYN, then drop it. 4325065Swnj * Otherwise this is an acceptable SYN segment 4335065Swnj * initialize tp->rcv_nxt and tp->irs 4345065Swnj * if seg contains ack then advance tp->snd_una 4355065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4365065Swnj * arrange for segment to be acked (eventually) 4375065Swnj * continue processing rest of data/controls, beginning with URG 4385065Swnj */ 4395065Swnj case TCPS_SYN_SENT: 4405065Swnj if ((tiflags & TH_ACK) && 44124816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4425231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4435085Swnj goto dropwithreset; 4445065Swnj if (tiflags & TH_RST) { 44510394Ssam if (tiflags & TH_ACK) 44610394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4475065Swnj goto drop; 4484601Swnj } 4495065Swnj if ((tiflags & TH_SYN) == 0) 4505065Swnj goto drop; 45130974Skarels if (tiflags & TH_ACK) { 45230974Skarels tp->snd_una = ti->ti_ack; 45330974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 45430974Skarels tp->snd_nxt = tp->snd_una; 45530974Skarels } 4565244Sroot tp->t_timer[TCPT_REXMT] = 0; 4575065Swnj tp->irs = ti->ti_seq; 4585085Swnj tcp_rcvseqinit(tp); 4595085Swnj tp->t_flags |= TF_ACKNOW; 46030974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 46130525Skarels tcpstat.tcps_connects++; 4625244Sroot soisconnected(so); 4635065Swnj tp->t_state = TCPS_ESTABLISHED; 46417272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4655162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 46632098Skarels /* 46732098Skarels * if we didn't have to retransmit the SYN, 46832098Skarels * use its rtt as our initial srtt & rtt var. 46932098Skarels */ 47032098Skarels if (tp->t_rtt) { 47132098Skarels tp->t_srtt = tp->t_rtt << 3; 47232098Skarels tp->t_rttvar = tp->t_rtt << 1; 47332098Skarels TCPT_RANGESET(tp->t_rxtcur, 47432098Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 47532098Skarels TCPTV_MIN, TCPTV_REXMTMAX); 47632098Skarels tp->t_rtt = 0; 47732098Skarels } 4785162Swnj } else 4795085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4805085Swnj 4815085Swnj trimthenstep6: 4825085Swnj /* 4835231Swnj * Advance ti->ti_seq to correspond to first data byte. 4845085Swnj * If data, trim to stay within window, 4855085Swnj * dropping FIN if necessary. 4865085Swnj */ 4875231Swnj ti->ti_seq++; 4885085Swnj if (ti->ti_len > tp->rcv_wnd) { 4895085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4905085Swnj m_adj(m, -todrop); 4915085Swnj ti->ti_len = tp->rcv_wnd; 49225939Skarels tiflags &= ~TH_FIN; 49330525Skarels tcpstat.tcps_rcvpackafterwin++; 49430525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4955065Swnj } 4965263Swnj tp->snd_wl1 = ti->ti_seq - 1; 49725939Skarels tp->rcv_up = ti->ti_seq; 4985085Swnj goto step6; 4995065Swnj } 5004601Swnj 5015065Swnj /* 50230525Skarels * States other than LISTEN or SYN_SENT. 50330525Skarels * First check that at least some bytes of segment are within 50430525Skarels * receive window. If segment begins before rcv_nxt, 50530525Skarels * drop leading data (and SYN); if nothing left, just ack. 50616222Skarels */ 50730525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 50830525Skarels if (todrop > 0) { 50930525Skarels if (tiflags & TH_SYN) { 51030525Skarels tiflags &= ~TH_SYN; 51130525Skarels ti->ti_seq++; 51230525Skarels if (ti->ti_urp > 1) 51330525Skarels ti->ti_urp--; 51430525Skarels else 51530525Skarels tiflags &= ~TH_URG; 51630525Skarels todrop--; 51730525Skarels } 51830525Skarels if (todrop > ti->ti_len || 51930525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 52031730Skarels #ifdef TCP_COMPAT_42 52131730Skarels /* 52231730Skarels * Don't toss RST in response to 4.2-style keepalive. 52331730Skarels */ 52431730Skarels if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 52531730Skarels goto do_rst; 52631730Skarels #endif 52730525Skarels tcpstat.tcps_rcvduppack++; 52830525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 52932034Skarels todrop = ti->ti_len; 53032098Skarels tiflags &= ~TH_FIN; 53132034Skarels tp->t_flags |= TF_ACKNOW; 53232034Skarels } else { 53332034Skarels tcpstat.tcps_rcvpartduppack++; 53432034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 53530525Skarels } 53630525Skarels m_adj(m, todrop); 53730525Skarels ti->ti_seq += todrop; 53830525Skarels ti->ti_len -= todrop; 53930525Skarels if (ti->ti_urp > todrop) 54030525Skarels ti->ti_urp -= todrop; 54130525Skarels else { 54230525Skarels tiflags &= ~TH_URG; 54330525Skarels ti->ti_urp = 0; 54430525Skarels } 54516222Skarels } 54616222Skarels 54732612Skarels /* 54832612Skarels * If new data is received on a connection after the 54932612Skarels * user processes are gone, then RST the other end. 55032612Skarels */ 55132612Skarels if ((so->so_state & SS_NOFDREF) && 55232612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 55332612Skarels tp = tcp_close(tp); 55432612Skarels tcpstat.tcps_rcvafterclose++; 55532612Skarels goto dropwithreset; 55632612Skarels } 55732612Skarels 5585065Swnj if (tp->rcv_wnd == 0) { 5595065Swnj /* 5605065Swnj * If window is closed can only take segments at 5615231Swnj * window edge, and have to drop data and PUSH from 5625065Swnj * incoming segments. 5635065Swnj */ 56430525Skarels if (tp->rcv_nxt != ti->ti_seq) { 56530525Skarels tcpstat.tcps_rcvpackafterwin++; 56630525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 5675065Swnj goto dropafterack; 56830525Skarels } 5695085Swnj if (ti->ti_len > 0) { 57030525Skarels if (ti->ti_len == 1) 57130525Skarels tcpstat.tcps_rcvwinprobe++; 57230525Skarels else { 57330525Skarels tcpstat.tcps_rcvpackafterwin++; 57430525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 57530525Skarels } 5765690Swnj m_adj(m, ti->ti_len); 5775085Swnj ti->ti_len = 0; 57825939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5795065Swnj } 5805065Swnj } else { 5815065Swnj /* 5825065Swnj * If segment ends after window, drop trailing data 5835085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5845065Swnj */ 5855690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5865690Swnj if (todrop > 0) { 58730525Skarels if (todrop >= ti->ti_len) { 58830525Skarels /* 58930525Skarels * If a new connection request is received 59030525Skarels * while in TIME_WAIT, drop the old connection 59130525Skarels * and start over if the sequence numbers 59230525Skarels * are above the previous ones. 59330525Skarels */ 59430525Skarels if (tiflags & TH_SYN && 59530525Skarels tp->t_state == TCPS_TIME_WAIT && 59630525Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 59730525Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 59830525Skarels (void) tcp_close(tp); 59930525Skarels goto findpcb; 60030525Skarels } 60130525Skarels if (todrop == 1) 60230525Skarels tcpstat.tcps_rcvwinprobe++; 60330525Skarels else { 60430525Skarels tcpstat.tcps_rcvpackafterwin++; 60531442Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 60630525Skarels } 6075065Swnj goto dropafterack; 60830525Skarels } 60930525Skarels tcpstat.tcps_rcvpackafterwin++; 61030525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 6115065Swnj m_adj(m, -todrop); 6125065Swnj ti->ti_len -= todrop; 61325939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6145065Swnj } 6155065Swnj } 6164601Swnj 61731730Skarels #ifdef TCP_COMPAT_42 61831730Skarels do_rst: 61931730Skarels #endif 6205065Swnj /* 6215065Swnj * If the RST bit is set examine the state: 6225065Swnj * SYN_RECEIVED STATE: 6235065Swnj * If passive open, return to LISTEN state. 6245065Swnj * If active open, inform user that connection was refused. 6255065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6265065Swnj * Inform user that connection was reset, and close tcb. 6275065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6285065Swnj * Close the tcb. 6295065Swnj */ 6305065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6315267Sroot 6325065Swnj case TCPS_SYN_RECEIVED: 63310394Ssam tp = tcp_drop(tp, ECONNREFUSED); 6345065Swnj goto drop; 6354601Swnj 6365065Swnj case TCPS_ESTABLISHED: 6375065Swnj case TCPS_FIN_WAIT_1: 6385065Swnj case TCPS_FIN_WAIT_2: 6395065Swnj case TCPS_CLOSE_WAIT: 64010394Ssam tp = tcp_drop(tp, ECONNRESET); 6415065Swnj goto drop; 6425065Swnj 6435065Swnj case TCPS_CLOSING: 6445065Swnj case TCPS_LAST_ACK: 6455065Swnj case TCPS_TIME_WAIT: 64610394Ssam tp = tcp_close(tp); 6475065Swnj goto drop; 6484601Swnj } 6494601Swnj 6504601Swnj /* 6515065Swnj * If a SYN is in the window, then this is an 6525065Swnj * error and we send an RST and drop the connection. 6534601Swnj */ 6545065Swnj if (tiflags & TH_SYN) { 65510394Ssam tp = tcp_drop(tp, ECONNRESET); 6565085Swnj goto dropwithreset; 6574601Swnj } 6584601Swnj 6594601Swnj /* 6605065Swnj * If the ACK bit is off we drop the segment and return. 6614601Swnj */ 6625085Swnj if ((tiflags & TH_ACK) == 0) 6635065Swnj goto drop; 6645065Swnj 6655065Swnj /* 6665065Swnj * Ack processing. 6675065Swnj */ 6684601Swnj switch (tp->t_state) { 6694601Swnj 6705065Swnj /* 6715065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 67231721Skarels * ESTABLISHED state and continue processing, otherwise 6735065Swnj * send an RST. 6745065Swnj */ 6755065Swnj case TCPS_SYN_RECEIVED: 6765085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6775231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6785085Swnj goto dropwithreset; 67930525Skarels tcpstat.tcps_connects++; 6805085Swnj soisconnected(so); 6815085Swnj tp->t_state = TCPS_ESTABLISHED; 68217272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6835162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6845244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6855085Swnj /* fall into ... */ 6864601Swnj 6875065Swnj /* 6885065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6895065Swnj * ACKs. If the ack is in the range 6905231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6915065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6925065Swnj * data from the retransmission queue. If this ACK reflects 6935065Swnj * more up to date window information we update our window information. 6945065Swnj */ 6955065Swnj case TCPS_ESTABLISHED: 6965065Swnj case TCPS_FIN_WAIT_1: 6975065Swnj case TCPS_FIN_WAIT_2: 6985065Swnj case TCPS_CLOSE_WAIT: 6995065Swnj case TCPS_CLOSING: 7005244Sroot case TCPS_LAST_ACK: 7015244Sroot case TCPS_TIME_WAIT: 7025085Swnj 70330525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 70432098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 70530525Skarels tcpstat.tcps_rcvdupack++; 70632098Skarels /* 70732098Skarels * If we have outstanding data (not a 70832098Skarels * window probe), this is a completely 70932098Skarels * duplicate ack (ie, window info didn't 71032098Skarels * change), the ack is the biggest we've 71132098Skarels * seen and we've seen exactly our rexmt 71232098Skarels * threshhold of them, assume a packet 71332098Skarels * has been dropped and retransmit it. 71432098Skarels * Kludge snd_nxt & the congestion 71532098Skarels * window so we send only this one 71632376Skarels * packet. If this packet fills the 71732376Skarels * only hole in the receiver's seq. 71832376Skarels * space, the next real ack will fully 71932376Skarels * open our window. This means we 72032376Skarels * have to do the usual slow-start to 72132376Skarels * not overwhelm an intermediate gateway 72232376Skarels * with a burst of packets. Leave 72332376Skarels * here with the congestion window set 72432376Skarels * to allow 2 packets on the next real 72532376Skarels * ack and the exp-to-linear thresh 72632376Skarels * set for half the current window 72732376Skarels * size (since we know we're losing at 72832376Skarels * the current window size). 72932098Skarels */ 73032098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 73132098Skarels ti->ti_ack != tp->snd_una) 73232098Skarels tp->t_dupacks = 0; 73332098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 73432098Skarels tcp_seq onxt = tp->snd_nxt; 73532376Skarels u_int win = 73632376Skarels MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 73732376Skarels tp->t_maxseg; 73832098Skarels 73932376Skarels if (win < 2) 74032376Skarels win = 2; 74132376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 74232376Skarels 74332098Skarels tp->t_timer[TCPT_REXMT] = 0; 74432098Skarels tp->t_rtt = 0; 74532098Skarels tp->snd_nxt = ti->ti_ack; 74632098Skarels tp->snd_cwnd = tp->t_maxseg; 74732098Skarels (void) tcp_output(tp); 74832098Skarels 74932098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 75032098Skarels tp->snd_nxt = onxt; 75132098Skarels goto drop; 75232098Skarels } 75332098Skarels } else 75432098Skarels tp->t_dupacks = 0; 7555065Swnj break; 75630525Skarels } 75732098Skarels tp->t_dupacks = 0; 75830525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 75930525Skarels tcpstat.tcps_rcvacktoomuch++; 7605065Swnj goto dropafterack; 76130525Skarels } 7625085Swnj acked = ti->ti_ack - tp->snd_una; 76330525Skarels tcpstat.tcps_rcvackpack++; 76430525Skarels tcpstat.tcps_rcvackbyte += acked; 7655951Swnj 7665951Swnj /* 7675951Swnj * If transmit timer is running and timed sequence 7685951Swnj * number was acked, update smoothed round trip time. 76932034Skarels * Since we now have an rtt measurement, cancel the 77032034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 77132034Skarels * Recompute the initial retransmit timer. 7725951Swnj */ 7735951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 77430525Skarels tcpstat.tcps_rttupdated++; 77531726Skarels if (tp->t_srtt != 0) { 77631726Skarels register short delta; 77731726Skarels 77831726Skarels /* 77931726Skarels * srtt is stored as fixed point with 3 bits 78031726Skarels * after the binary point (i.e., scaled by 8). 78131726Skarels * The following magic is equivalent 78231726Skarels * to the smoothing algorithm in rfc793 78331726Skarels * with an alpha of .875 78431726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 78532098Skarels * Adjust t_rtt to origin 0. 78631726Skarels */ 78732098Skarels tp->t_rtt--; 78831726Skarels delta = tp->t_rtt - (tp->t_srtt >> 3); 78931726Skarels if ((tp->t_srtt += delta) <= 0) 79031726Skarels tp->t_srtt = 1; 79131726Skarels /* 79232034Skarels * We accumulate a smoothed rtt variance 79332034Skarels * (actually, a smoothed mean difference), 79431726Skarels * then set the retransmit timer to smoothed 79531726Skarels * rtt + 2 times the smoothed variance. 79632098Skarels * rttvar is stored as fixed point 79731726Skarels * with 2 bits after the binary point 79831726Skarels * (scaled by 4). The following is equivalent 79931726Skarels * to rfc793 smoothing with an alpha of .75 80031726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 80131726Skarels * This replaces rfc793's wired-in beta. 80231726Skarels */ 80331726Skarels if (delta < 0) 80431726Skarels delta = -delta; 80531726Skarels delta -= (tp->t_rttvar >> 2); 80631726Skarels if ((tp->t_rttvar += delta) <= 0) 80731726Skarels tp->t_rttvar = 1; 80831726Skarels } else { 80931726Skarels /* 81031726Skarels * No rtt measurement yet - use the 81131726Skarels * unsmoothed rtt. Set the variance 81231726Skarels * to half the rtt (so our first 81331726Skarels * retransmit happens at 2*rtt) 81431726Skarels */ 81531726Skarels tp->t_srtt = tp->t_rtt << 3; 81631726Skarels tp->t_rttvar = tp->t_rtt << 1; 81731726Skarels } 8185951Swnj tp->t_rtt = 0; 81932034Skarels tp->t_rxtshift = 0; 82032034Skarels TCPT_RANGESET(tp->t_rxtcur, 82132034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 82232034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 8235951Swnj } 8245951Swnj 82526824Skarels /* 82626824Skarels * If all outstanding data is acked, stop retransmit 82726824Skarels * timer and remember to restart (more output or persist). 82826824Skarels * If there is more data to be acked, restart retransmit 82932034Skarels * timer, using current (possibly backed-off) value. 83026824Skarels */ 83126824Skarels if (ti->ti_ack == tp->snd_max) { 8325244Sroot tp->t_timer[TCPT_REXMT] = 0; 83326824Skarels needoutput = 1; 83432034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 83532034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 83617360Skarels /* 83732098Skarels * When new data is acked, open the congestion window. 83832098Skarels * If the window gives us less than ssthresh packets 83932098Skarels * in flight, open exponentially (maxseg per packet). 84032098Skarels * Otherwise open linearly (maxseg per window, 84132098Skarels * or maxseg^2 / cwnd per packet). 84217360Skarels */ 84332098Skarels { 84432098Skarels u_int incr = tp->t_maxseg; 84532098Skarels 84632098Skarels if (tp->snd_cwnd > tp->snd_ssthresh) 84732098Skarels incr = MAX(incr * incr / tp->snd_cwnd, 1); 84832098Skarels 84932098Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */ 85032098Skarels } 8515307Sroot if (acked > so->so_snd.sb_cc) { 85215386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 85326386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 85431721Skarels ourfinisacked = 1; 8555307Sroot } else { 8566161Ssam sbdrop(&so->so_snd, acked); 8575307Sroot tp->snd_wnd -= acked; 85831721Skarels ourfinisacked = 0; 8595307Sroot } 8606434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 8615300Sroot sowwakeup(so); 8625231Swnj tp->snd_una = ti->ti_ack; 8635357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 8645357Sroot tp->snd_nxt = tp->snd_una; 8655162Swnj 8664601Swnj switch (tp->t_state) { 8674601Swnj 8685065Swnj /* 8695065Swnj * In FIN_WAIT_1 STATE in addition to the processing 8705065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 8715085Swnj * then enter FIN_WAIT_2. 8725065Swnj */ 8735065Swnj case TCPS_FIN_WAIT_1: 8745896Swnj if (ourfinisacked) { 8755896Swnj /* 8765896Swnj * If we can't receive any more 8775896Swnj * data, then closing user can proceed. 87824816Skarels * Starting the timer is contrary to the 87924816Skarels * specification, but if we don't get a FIN 88024816Skarels * we'll hang forever. 8815896Swnj */ 88224816Skarels if (so->so_state & SS_CANTRCVMORE) { 8835896Swnj soisdisconnected(so); 88424816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 88524816Skarels } 8865085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8875896Swnj } 8884601Swnj break; 8894601Swnj 8905065Swnj /* 8915065Swnj * In CLOSING STATE in addition to the processing for 8925065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8935065Swnj * then enter the TIME-WAIT state, otherwise ignore 8945065Swnj * the segment. 8955065Swnj */ 8965065Swnj case TCPS_CLOSING: 8975244Sroot if (ourfinisacked) { 8985065Swnj tp->t_state = TCPS_TIME_WAIT; 8995244Sroot tcp_canceltimers(tp); 9005244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9015244Sroot soisdisconnected(so); 9025244Sroot } 9035244Sroot break; 9044601Swnj 9055065Swnj /* 90631743Skarels * In LAST_ACK, we may still be waiting for data to drain 90731743Skarels * and/or to be acked, as well as for the ack of our FIN. 90831743Skarels * If our FIN is now acknowledged, delete the TCB, 90931743Skarels * enter the closed state and return. 9105065Swnj */ 9115065Swnj case TCPS_LAST_ACK: 91231743Skarels if (ourfinisacked) { 91310394Ssam tp = tcp_close(tp); 91431743Skarels goto drop; 91531743Skarels } 91631743Skarels break; 9174601Swnj 9185065Swnj /* 9195065Swnj * In TIME_WAIT state the only thing that should arrive 9205065Swnj * is a retransmission of the remote FIN. Acknowledge 9215065Swnj * it and restart the finack timer. 9225065Swnj */ 9235065Swnj case TCPS_TIME_WAIT: 9245162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9255065Swnj goto dropafterack; 9264601Swnj } 9275085Swnj } 9284601Swnj 9295065Swnj step6: 9305065Swnj /* 9315244Sroot * Update window information. 93225939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9335244Sroot */ 93425939Skarels if ((tiflags & TH_ACK) && 93525939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9365391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 93725939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 93830525Skarels /* keep track of pure window updates */ 93930525Skarels if (ti->ti_len == 0 && 94032098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 94130525Skarels tcpstat.tcps_rcvwinupd++; 9425244Sroot tp->snd_wnd = ti->ti_win; 9435244Sroot tp->snd_wl1 = ti->ti_seq; 9445244Sroot tp->snd_wl2 = ti->ti_ack; 94525260Skarels if (tp->snd_wnd > tp->max_sndwnd) 94625260Skarels tp->max_sndwnd = tp->snd_wnd; 94726824Skarels needoutput = 1; 94826824Skarels } 9495244Sroot 9505244Sroot /* 9515547Swnj * Process segments with URG. 9525065Swnj */ 9537267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9547267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9555547Swnj /* 95625939Skarels * This is a kludge, but if we receive and accept 95713121Ssam * random urgent pointers, we'll crash in 95813121Ssam * soreceive. It's hard to imagine someone 95913121Ssam * actually wanting to send this much urgent data. 96012441Ssam */ 96125652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 96212441Ssam ti->ti_urp = 0; /* XXX */ 96312441Ssam tiflags &= ~TH_URG; /* XXX */ 96425939Skarels goto dodata; /* XXX */ 96512441Ssam } 96612441Ssam /* 9675547Swnj * If this segment advances the known urgent pointer, 9685547Swnj * then mark the data stream. This should not happen 9695547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 9705547Swnj * a FIN has been received from the remote side. 9715547Swnj * In these states we ignore the URG. 97227190Skarels * 97327190Skarels * According to RFC961 (Assigned Protocols), 97427190Skarels * the urgent pointer points to the last octet 97527190Skarels * of urgent data. We continue, however, 97627190Skarels * to consider it to indicate the first octet 97727190Skarels * of data past the urgent section 97827190Skarels * as the original spec states. 9795547Swnj */ 9805547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9815547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9825547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9835547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9845547Swnj if (so->so_oobmark == 0) 9855547Swnj so->so_state |= SS_RCVATMARK; 9868313Sroot sohasoutofband(so); 98724816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9885440Swnj } 9895547Swnj /* 9905547Swnj * Remove out of band data so doesn't get presented to user. 9915547Swnj * This can happen independent of advancing the URG pointer, 9925547Swnj * but if two URG's are pending at once, some out-of-band 9935547Swnj * data may creep in... ick. 9945547Swnj */ 99527190Skarels if (ti->ti_urp <= ti->ti_len && 99627190Skarels (so->so_options & SO_OOBINLINE) == 0) 9975547Swnj tcp_pulloutofband(so, ti); 99825939Skarels } else 99925939Skarels /* 100025939Skarels * If no out of band data is expected, 100125939Skarels * pull receive urgent pointer along 100225939Skarels * with the receive window. 100325939Skarels */ 100425939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 100525939Skarels tp->rcv_up = tp->rcv_nxt; 100625939Skarels dodata: /* XXX */ 10074601Swnj 10084601Swnj /* 10095065Swnj * Process the segment text, merging it into the TCP sequencing queue, 10105065Swnj * and arranging for acknowledgment of receipt if necessary. 10115065Swnj * This process logically involves adjusting tp->rcv_wnd as data 10125065Swnj * is presented to the user (this happens in tcp_usrreq.c, 10135065Swnj * case PRU_RCVD). If a FIN has already been received on this 10145065Swnj * connection then we just ignore the text. 10154601Swnj */ 101617946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 101717946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 101824816Skarels TCP_REASS(tp, ti, m, so, tiflags); 10195440Swnj if (tcpnodelack == 0) 10205440Swnj tp->t_flags |= TF_DELACK; 10215440Swnj else 10225440Swnj tp->t_flags |= TF_ACKNOW; 102325260Skarels /* 102425260Skarels * Note the amount of data that peer has sent into 102525260Skarels * our window, in order to estimate the sender's 102625260Skarels * buffer size. 102725260Skarels */ 102832098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 102925260Skarels if (len > tp->max_rcvd) 103025260Skarels tp->max_rcvd = len; 10315244Sroot } else { 10324924Swnj m_freem(m); 10335263Swnj tiflags &= ~TH_FIN; 10345244Sroot } 10354601Swnj 10364601Swnj /* 10375263Swnj * If FIN is received ACK the FIN and let the user know 10385263Swnj * that the connection is closing. 10394601Swnj */ 10405263Swnj if (tiflags & TH_FIN) { 10415244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10425244Sroot socantrcvmore(so); 10435244Sroot tp->t_flags |= TF_ACKNOW; 10445244Sroot tp->rcv_nxt++; 10455244Sroot } 10465065Swnj switch (tp->t_state) { 10474601Swnj 10485065Swnj /* 10495065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10505065Swnj * enter the CLOSE_WAIT state. 10514884Swnj */ 10525065Swnj case TCPS_SYN_RECEIVED: 10535065Swnj case TCPS_ESTABLISHED: 10545065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10555065Swnj break; 10564884Swnj 10575065Swnj /* 10585085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10595085Swnj * enter the CLOSING state. 10604884Swnj */ 10615065Swnj case TCPS_FIN_WAIT_1: 10625085Swnj tp->t_state = TCPS_CLOSING; 10635065Swnj break; 10644601Swnj 10655065Swnj /* 10665065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 10675065Swnj * starting the time-wait timer, turning off the other 10685065Swnj * standard timers. 10695065Swnj */ 10705065Swnj case TCPS_FIN_WAIT_2: 10715244Sroot tp->t_state = TCPS_TIME_WAIT; 10725074Swnj tcp_canceltimers(tp); 10735162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10745244Sroot soisdisconnected(so); 10755065Swnj break; 10765065Swnj 10774884Swnj /* 10785065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10794884Swnj */ 10805065Swnj case TCPS_TIME_WAIT: 10815162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10825065Swnj break; 10835085Swnj } 10844601Swnj } 10855267Sroot if (so->so_options & SO_DEBUG) 10865267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10875085Swnj 10885085Swnj /* 10895085Swnj * Return any desired output. 10905085Swnj */ 109126824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 109225939Skarels (void) tcp_output(tp); 10935065Swnj return; 10945085Swnj 10955065Swnj dropafterack: 10965085Swnj /* 10976211Swnj * Generate an ACK dropping incoming segment if it occupies 10986211Swnj * sequence space, where the ACK reflects our state. 10995085Swnj */ 110026057Skarels if (tiflags & TH_RST) 11015085Swnj goto drop; 110231749Skarels m_freem(m); 110331721Skarels tp->t_flags |= TF_ACKNOW; 110431721Skarels (void) tcp_output(tp); 11055231Swnj return; 11065085Swnj 11075085Swnj dropwithreset: 110811731Ssam if (om) { 11096161Ssam (void) m_free(om); 111011731Ssam om = 0; 111111731Ssam } 11125085Swnj /* 11135244Sroot * Generate a RST, dropping incoming segment. 11145085Swnj * Make ACK acceptable to originator of segment. 111525197Skarels * Don't bother to respond if destination was broadcast. 11165085Swnj */ 111725197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 11185085Swnj goto drop; 11195085Swnj if (tiflags & TH_ACK) 11205391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 11215085Swnj else { 11225085Swnj if (tiflags & TH_SYN) 11235085Swnj ti->ti_len++; 11246211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11256211Swnj TH_RST|TH_ACK); 11265085Swnj } 112710769Ssam /* destroy temporarily created socket */ 112810769Ssam if (dropsocket) 112910769Ssam (void) soabort(so); 11305231Swnj return; 11315085Swnj 11325065Swnj drop: 113311730Ssam if (om) 113411730Ssam (void) m_free(om); 11355085Swnj /* 11365085Swnj * Drop space held by incoming segment and return. 11375085Swnj */ 11386303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11396303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11405065Swnj m_freem(m); 114110769Ssam /* destroy temporarily created socket */ 114210769Ssam if (dropsocket) 114310769Ssam (void) soabort(so); 11445267Sroot return; 11455065Swnj } 11465065Swnj 114717272Skarels tcp_dooptions(tp, om, ti) 11485440Swnj struct tcpcb *tp; 11495440Swnj struct mbuf *om; 115017272Skarels struct tcpiphdr *ti; 11515419Swnj { 11525440Swnj register u_char *cp; 11535440Swnj int opt, optlen, cnt; 11545419Swnj 11555440Swnj cp = mtod(om, u_char *); 11565440Swnj cnt = om->m_len; 11575440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11585440Swnj opt = cp[0]; 11595440Swnj if (opt == TCPOPT_EOL) 11605440Swnj break; 11615440Swnj if (opt == TCPOPT_NOP) 11625440Swnj optlen = 1; 116312169Ssam else { 11645440Swnj optlen = cp[1]; 116512169Ssam if (optlen <= 0) 116612169Ssam break; 116712169Ssam } 11685440Swnj switch (opt) { 11695440Swnj 11705440Swnj default: 11715440Swnj break; 11725440Swnj 11735440Swnj case TCPOPT_MAXSEG: 11745440Swnj if (optlen != 4) 11755440Swnj continue; 117617272Skarels if (!(ti->ti_flags & TH_SYN)) 117717272Skarels continue; 11785440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11796161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 118017272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 11815440Swnj break; 11825419Swnj } 11835419Swnj } 11846161Ssam (void) m_free(om); 11855419Swnj } 11865419Swnj 11875419Swnj /* 11885547Swnj * Pull out of band byte out of a segment so 11895547Swnj * it doesn't appear in the user's data queue. 11905547Swnj * It is still reflected in the segment length for 11915547Swnj * sequencing purposes. 11925547Swnj */ 11935547Swnj tcp_pulloutofband(so, ti) 11945547Swnj struct socket *so; 11955547Swnj struct tcpiphdr *ti; 11965547Swnj { 11975547Swnj register struct mbuf *m; 11986116Swnj int cnt = ti->ti_urp - 1; 11995547Swnj 12005547Swnj m = dtom(ti); 12015547Swnj while (cnt >= 0) { 12025547Swnj if (m->m_len > cnt) { 12035547Swnj char *cp = mtod(m, caddr_t) + cnt; 12045547Swnj struct tcpcb *tp = sototcpcb(so); 12055547Swnj 12065547Swnj tp->t_iobc = *cp; 12075547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 12086161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 12095547Swnj m->m_len--; 12105547Swnj return; 12115547Swnj } 12125547Swnj cnt -= m->m_len; 12135547Swnj m = m->m_next; 12145547Swnj if (m == 0) 12155547Swnj break; 12165547Swnj } 12175547Swnj panic("tcp_pulloutofband"); 12185547Swnj } 12195547Swnj 12205547Swnj /* 122117272Skarels * Determine a reasonable value for maxseg size. 122217272Skarels * If the route is known, use one that can be handled 122317272Skarels * on the given interface without forcing IP to fragment. 122431726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 122531726Skarels * to utilize large mbufs. 122617272Skarels * If interface pointer is unavailable, or the destination isn't local, 122723975Skarels * use a conservative size (512 or the default IP max size, but no more 122823975Skarels * than the mtu of the interface through which we route), 122917272Skarels * as we can't discover anything about intervening gateways or networks. 123032034Skarels * We also initialize the congestion/slow start window to be a single 123132034Skarels * segment if the destination isn't local; this information should 123232034Skarels * probably all be saved with the routing entry at the transport level. 123317272Skarels * 123417272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 123517272Skarels */ 123617272Skarels tcp_mss(tp) 123723975Skarels register struct tcpcb *tp; 123817272Skarels { 123917272Skarels struct route *ro; 124017272Skarels struct ifnet *ifp; 124117272Skarels int mss; 124217272Skarels struct inpcb *inp; 124317272Skarels 124417272Skarels inp = tp->t_inpcb; 124517272Skarels ro = &inp->inp_route; 124617272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 124717272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 124817272Skarels /* No route yet, so try to acquire one */ 124917272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 125017272Skarels ro->ro_dst.sa_family = AF_INET; 125117272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 125217272Skarels inp->inp_faddr; 125317272Skarels rtalloc(ro); 125417272Skarels } 125517272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 125617316Skarels return (TCP_MSS); 125717272Skarels } 125817272Skarels 125917272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 126031726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 126131726Skarels if (mss > MCLBYTES) 126231726Skarels mss &= ~(MCLBYTES-1); 126317272Skarels #else 126431726Skarels if (mss > MCLBYTES) 126531726Skarels mss = mss / MCLBYTES * MCLBYTES; 126617272Skarels #endif 126723975Skarels if (in_localaddr(inp->inp_faddr)) 126823975Skarels return (mss); 126932098Skarels 127032034Skarels mss = MIN(mss, TCP_MSS); 127132034Skarels tp->snd_cwnd = mss; 127232034Skarels return (mss); 127317272Skarels } 1274