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*32034Skarels * @(#)tcp_input.c 7.10 (Berkeley) 08/10/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; 355267Sroot struct tcpiphdr tcp_saveti; 365440Swnj extern tcpnodelack; 374601Swnj 385267Sroot struct tcpcb *tcp_newtcpcb(); 3924816Skarels 405065Swnj /* 4124816Skarels * Insert segment ti into reassembly queue of tcp with 4224816Skarels * control block tp. Return TH_FIN if reassembly now includes 4324816Skarels * a segment with FIN. The macro form does the common case inline 4424816Skarels * (segment is the next to be received on an established connection, 4524816Skarels * and the queue is empty), avoiding linkage into and removal 4624816Skarels * from the queue and repetition of various conversions. 4724816Skarels */ 4824816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 4924816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5024816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5124816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 5224816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 5324816Skarels flags = (ti)->ti_flags & TH_FIN; \ 5430525Skarels tcpstat.tcps_rcvpack++;\ 5530525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 5624816Skarels sbappend(&(so)->so_rcv, (m)); \ 5724816Skarels sorwakeup(so); \ 5824816Skarels } else \ 5924816Skarels (flags) = tcp_reass((tp), (ti)); \ 6024816Skarels } 6124816Skarels 6224816Skarels tcp_reass(tp, ti) 6324816Skarels register struct tcpcb *tp; 6424816Skarels register struct tcpiphdr *ti; 6524816Skarels { 6624816Skarels register struct tcpiphdr *q; 6724816Skarels struct socket *so = tp->t_inpcb->inp_socket; 6824816Skarels struct mbuf *m; 6924816Skarels int flags; 7024816Skarels 7124816Skarels /* 7224816Skarels * Call with ti==0 after become established to 7324816Skarels * force pre-ESTABLISHED data up to user socket. 7424816Skarels */ 7524816Skarels if (ti == 0) 7624816Skarels goto present; 7724816Skarels 7824816Skarels /* 7924816Skarels * Find a segment which begins after this one does. 8024816Skarels */ 8124816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8224816Skarels q = (struct tcpiphdr *)q->ti_next) 8324816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8424816Skarels break; 8524816Skarels 8624816Skarels /* 8724816Skarels * If there is a preceding segment, it may provide some of 8824816Skarels * our data already. If so, drop the data from the incoming 8924816Skarels * segment. If it provides all of our data, drop us. 9024816Skarels */ 9124816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 9224816Skarels register int i; 9324816Skarels q = (struct tcpiphdr *)q->ti_prev; 9424816Skarels /* conversion to int (in i) handles seq wraparound */ 9524816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 9624816Skarels if (i > 0) { 9730525Skarels if (i >= ti->ti_len) { 9830525Skarels tcpstat.tcps_rcvduppack++; 9930525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10024816Skarels goto drop; 10130525Skarels } 10224816Skarels m_adj(dtom(ti), i); 10324816Skarels ti->ti_len -= i; 10424816Skarels ti->ti_seq += i; 10524816Skarels } 10624816Skarels q = (struct tcpiphdr *)(q->ti_next); 10724816Skarels } 10830525Skarels tcpstat.tcps_rcvoopack++; 10930525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 11024816Skarels 11124816Skarels /* 11224816Skarels * While we overlap succeeding segments trim them or, 11324816Skarels * if they are completely covered, dequeue them. 11424816Skarels */ 11524816Skarels while (q != (struct tcpiphdr *)tp) { 11624816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 11724816Skarels if (i <= 0) 11824816Skarels break; 11924816Skarels if (i < q->ti_len) { 12024816Skarels q->ti_seq += i; 12124816Skarels q->ti_len -= i; 12224816Skarels m_adj(dtom(q), i); 12324816Skarels break; 12424816Skarels } 12524816Skarels q = (struct tcpiphdr *)q->ti_next; 12624816Skarels m = dtom(q->ti_prev); 12724816Skarels remque(q->ti_prev); 12824816Skarels m_freem(m); 12924816Skarels } 13024816Skarels 13124816Skarels /* 13224816Skarels * Stick new segment in its place. 13324816Skarels */ 13424816Skarels insque(ti, q->ti_prev); 13524816Skarels 13624816Skarels present: 13724816Skarels /* 13824816Skarels * Present data to user, advancing rcv_nxt through 13924816Skarels * completed sequence space. 14024816Skarels */ 14124816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 14224816Skarels return (0); 14324816Skarels ti = tp->seg_next; 14424816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 14524816Skarels return (0); 14624816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 14724816Skarels return (0); 14824816Skarels do { 14924816Skarels tp->rcv_nxt += ti->ti_len; 15024816Skarels flags = ti->ti_flags & TH_FIN; 15124816Skarels remque(ti); 15224816Skarels m = dtom(ti); 15324816Skarels ti = (struct tcpiphdr *)ti->ti_next; 15424816Skarels if (so->so_state & SS_CANTRCVMORE) 15524816Skarels m_freem(m); 15624816Skarels else 15724816Skarels sbappend(&so->so_rcv, m); 15824816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 15924816Skarels sorwakeup(so); 16024816Skarels return (flags); 16124816Skarels drop: 16224816Skarels m_freem(dtom(ti)); 16324816Skarels return (0); 16424816Skarels } 16524816Skarels 16624816Skarels /* 1675065Swnj * TCP input routine, follows pages 65-76 of the 1685065Swnj * protocol specification dated September, 1981 very closely. 1695065Swnj */ 1704924Swnj tcp_input(m0) 1714924Swnj struct mbuf *m0; 1724601Swnj { 1734924Swnj register struct tcpiphdr *ti; 1744924Swnj struct inpcb *inp; 1754924Swnj register struct mbuf *m; 1765440Swnj struct mbuf *om = 0; 1774924Swnj int len, tlen, off; 1785391Swnj register struct tcpcb *tp = 0; 1794924Swnj register int tiflags; 1804803Swnj struct socket *so; 18131721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1825267Sroot short ostate; 1836028Sroot struct in_addr laddr; 18410769Ssam int dropsocket = 0; 18530525Skarels int iss = 0; 1864924Swnj 18730525Skarels tcpstat.tcps_rcvtotal++; 1884924Swnj /* 1895244Sroot * Get IP and TCP header together in first mbuf. 1905244Sroot * Note: IP leaves IP header in first mbuf. 1914924Swnj */ 1924924Swnj m = m0; 1935020Sroot ti = mtod(m, struct tcpiphdr *); 1945244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 1955208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 1965307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 1975307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 19830525Skarels tcpstat.tcps_rcvshort++; 1995307Sroot return; 2005085Swnj } 2015085Swnj ti = mtod(m, struct tcpiphdr *); 2025085Swnj } 2034601Swnj 2044601Swnj /* 2055244Sroot * Checksum extended TCP header and data. 2064601Swnj */ 2074924Swnj tlen = ((struct ip *)ti)->ip_len; 2084924Swnj len = sizeof (struct ip) + tlen; 2094679Swnj if (tcpcksum) { 2104924Swnj ti->ti_next = ti->ti_prev = 0; 2114924Swnj ti->ti_x1 = 0; 2125223Swnj ti->ti_len = (u_short)tlen; 2136161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2145231Swnj if (ti->ti_sum = in_cksum(m, len)) { 21511830Ssam if (tcpprintfs) 21611830Ssam printf("tcp sum: src %x\n", ti->ti_src); 21730525Skarels tcpstat.tcps_rcvbadsum++; 2185085Swnj goto drop; 2194601Swnj } 2204601Swnj } 2214601Swnj 2224601Swnj /* 2235244Sroot * Check that TCP offset makes sense, 2245440Swnj * pull out TCP options and adjust length. 2254601Swnj */ 2264924Swnj off = ti->ti_off << 2; 2275231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 22811830Ssam if (tcpprintfs) 22911830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 23030525Skarels tcpstat.tcps_rcvbadoff++; 2315085Swnj goto drop; 2324924Swnj } 2336211Swnj tlen -= off; 2346211Swnj ti->ti_len = tlen; 2355440Swnj if (off > sizeof (struct tcphdr)) { 23624816Skarels if (m->m_len < sizeof(struct ip) + off) { 23724816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 23830525Skarels tcpstat.tcps_rcvshort++; 23924816Skarels return; 24024816Skarels } 24124816Skarels ti = mtod(m, struct tcpiphdr *); 2425440Swnj } 2439642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2445440Swnj if (om == 0) 2455440Swnj goto drop; 2465440Swnj om->m_len = off - sizeof (struct tcphdr); 2475440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2486161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2495440Swnj m->m_len -= om->m_len; 2506161Ssam bcopy(op+om->m_len, op, 2516161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2525440Swnj } 2535440Swnj } 2545065Swnj tiflags = ti->ti_flags; 2554924Swnj 2566093Sroot /* 25725729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2586093Sroot */ 25925729Smckusick m->m_off += sizeof(struct tcpiphdr); 26025729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2616093Sroot 2624924Swnj /* 2635244Sroot * Convert TCP protocol specific fields to host format. 2645085Swnj */ 2655085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2665085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2675085Swnj ti->ti_win = ntohs(ti->ti_win); 2685085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2695085Swnj 2705085Swnj /* 2718271Sroot * Locate pcb for segment. 2724924Swnj */ 27330525Skarels findpcb: 2745065Swnj inp = in_pcblookup 2756028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2766028Sroot INPLOOKUP_WILDCARD); 2775065Swnj 2785065Swnj /* 2795065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2805244Sroot * all data in the incoming segment is discarded. 2815065Swnj */ 2825300Sroot if (inp == 0) 2835085Swnj goto dropwithreset; 2845065Swnj tp = intotcpcb(inp); 2855300Sroot if (tp == 0) 2865085Swnj goto dropwithreset; 2875109Swnj so = inp->inp_socket; 2885267Sroot if (so->so_options & SO_DEBUG) { 2895267Sroot ostate = tp->t_state; 2905267Sroot tcp_saveti = *ti; 2915267Sroot } 2927510Sroot if (so->so_options & SO_ACCEPTCONN) { 2937510Sroot so = sonewconn(so); 2947510Sroot if (so == 0) 2957510Sroot goto drop; 29610769Ssam /* 29710769Ssam * This is ugly, but .... 29810769Ssam * 29910769Ssam * Mark socket as temporary until we're 30010769Ssam * committed to keeping it. The code at 30110769Ssam * ``drop'' and ``dropwithreset'' check the 30210769Ssam * flag dropsocket to see if the temporary 30310769Ssam * socket created here should be discarded. 30410769Ssam * We mark the socket as discardable until 30510769Ssam * we're committed to it below in TCPS_LISTEN. 30610769Ssam */ 30710769Ssam dropsocket++; 3087510Sroot inp = (struct inpcb *)so->so_pcb; 3097510Sroot inp->inp_laddr = ti->ti_dst; 3107510Sroot inp->inp_lport = ti->ti_dport; 31124816Skarels inp->inp_options = ip_srcroute(); 3127510Sroot tp = intotcpcb(inp); 3137510Sroot tp->t_state = TCPS_LISTEN; 3147510Sroot } 3154601Swnj 3164601Swnj /* 3175162Swnj * Segment received on connection. 3185162Swnj * Reset idle time and keep-alive timer. 3195162Swnj */ 3205162Swnj tp->t_idle = 0; 3215162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 3225162Swnj 3235162Swnj /* 32417272Skarels * Process options if not in LISTEN state, 32517272Skarels * else do it below (after getting remote address). 3265440Swnj */ 32717272Skarels if (om && tp->t_state != TCPS_LISTEN) { 32817272Skarels tcp_dooptions(tp, om, ti); 3295440Swnj om = 0; 3305440Swnj } 3315440Swnj 3325440Swnj /* 3335085Swnj * Calculate amount of space in receive window, 3345085Swnj * and then do TCP input processing. 33524816Skarels * Receive window is amount of space in rcv queue, 33624816Skarels * but not less than advertised window. 3374601Swnj */ 33825939Skarels { int win; 3394601Swnj 34025939Skarels win = sbspace(&so->so_rcv); 34125939Skarels if (win < 0) 34225939Skarels win = 0; 34325939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 34425939Skarels } 34525939Skarels 3464601Swnj switch (tp->t_state) { 3474601Swnj 3485065Swnj /* 3495065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3505065Swnj * If the segment contains an ACK then it is bad and send a RST. 3515065Swnj * If it does not contain a SYN then it is not interesting; drop it. 35225197Skarels * Don't bother responding if the destination was a broadcast. 3535085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3545065Swnj * tp->iss, and send a segment: 3555085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3565065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3575065Swnj * Fill in remote peer address fields if not previously specified. 3585065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3595244Sroot * segment in this state. 3605065Swnj */ 3618271Sroot case TCPS_LISTEN: { 36210145Ssam struct mbuf *am; 3638271Sroot register struct sockaddr_in *sin; 3648271Sroot 3655065Swnj if (tiflags & TH_RST) 3665065Swnj goto drop; 3675300Sroot if (tiflags & TH_ACK) 3685085Swnj goto dropwithreset; 3695300Sroot if ((tiflags & TH_SYN) == 0) 3705065Swnj goto drop; 37125197Skarels if (in_broadcast(ti->ti_dst)) 37225197Skarels goto drop; 37310145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 37410145Ssam if (am == NULL) 37510145Ssam goto drop; 37610145Ssam am->m_len = sizeof (struct sockaddr_in); 3778599Sroot sin = mtod(am, struct sockaddr_in *); 3788271Sroot sin->sin_family = AF_INET; 3798271Sroot sin->sin_addr = ti->ti_src; 3808271Sroot sin->sin_port = ti->ti_sport; 3816028Sroot laddr = inp->inp_laddr; 38210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3836028Sroot inp->inp_laddr = ti->ti_dst; 3848599Sroot if (in_pcbconnect(inp, am)) { 3856028Sroot inp->inp_laddr = laddr; 3868716Sroot (void) m_free(am); 3875244Sroot goto drop; 3886028Sroot } 3898716Sroot (void) m_free(am); 3905244Sroot tp->t_template = tcp_template(tp); 3915244Sroot if (tp->t_template == 0) { 39226386Skarels tp = tcp_drop(tp, ENOBUFS); 39317264Skarels dropsocket = 0; /* socket is already gone */ 3945244Sroot goto drop; 3955244Sroot } 39617272Skarels if (om) { 39717272Skarels tcp_dooptions(tp, om, ti); 39817272Skarels om = 0; 39917272Skarels } 40030525Skarels if (iss) 40130525Skarels tp->iss = iss; 40230525Skarels else 40330525Skarels tp->iss = tcp_iss; 40430525Skarels tcp_iss += TCP_ISSINCR/2; 4055065Swnj tp->irs = ti->ti_seq; 4065085Swnj tcp_sendseqinit(tp); 4075085Swnj tcp_rcvseqinit(tp); 40825939Skarels tp->t_flags |= TF_ACKNOW; 4095065Swnj tp->t_state = TCPS_SYN_RECEIVED; 4105244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 41110769Ssam dropsocket = 0; /* committed to socket */ 41230525Skarels tcpstat.tcps_accepts++; 4135085Swnj goto trimthenstep6; 4148271Sroot } 4154601Swnj 4165065Swnj /* 4175065Swnj * If the state is SYN_SENT: 4185065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4195065Swnj * if seg contains a RST, then drop the connection. 4205065Swnj * if seg does not contain SYN, then drop it. 4215065Swnj * Otherwise this is an acceptable SYN segment 4225065Swnj * initialize tp->rcv_nxt and tp->irs 4235065Swnj * if seg contains ack then advance tp->snd_una 4245065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4255065Swnj * arrange for segment to be acked (eventually) 4265065Swnj * continue processing rest of data/controls, beginning with URG 4275065Swnj */ 4285065Swnj case TCPS_SYN_SENT: 4295065Swnj if ((tiflags & TH_ACK) && 43024816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4315231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4325085Swnj goto dropwithreset; 4335065Swnj if (tiflags & TH_RST) { 43410394Ssam if (tiflags & TH_ACK) 43510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4365065Swnj goto drop; 4374601Swnj } 4385065Swnj if ((tiflags & TH_SYN) == 0) 4395065Swnj goto drop; 44030974Skarels if (tiflags & TH_ACK) { 44130974Skarels tp->snd_una = ti->ti_ack; 44230974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 44330974Skarels tp->snd_nxt = tp->snd_una; 44430974Skarels } 4455244Sroot tp->t_timer[TCPT_REXMT] = 0; 446*32034Skarels /* 447*32034Skarels * If we didn't have to retransmit, 448*32034Skarels * set the initial estimate of srtt. 449*32034Skarels * Set the variance to half the rtt 450*32034Skarels * (so our first retransmit happens at 2*rtt). 451*32034Skarels */ 452*32034Skarels if (tp->t_rtt) { 453*32034Skarels tp->t_srtt = tp->t_rtt << 3; 454*32034Skarels tp->t_rttvar = tp->t_rtt << 1; 455*32034Skarels tp->t_rtt = 0; 456*32034Skarels tp->t_rxtshift = 0; 457*32034Skarels TCPT_RANGESET(tp->t_rxtcur, 458*32034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 459*32034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 460*32034Skarels } 4615065Swnj tp->irs = ti->ti_seq; 4625085Swnj tcp_rcvseqinit(tp); 4635085Swnj tp->t_flags |= TF_ACKNOW; 46430974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 46530525Skarels tcpstat.tcps_connects++; 4665244Sroot soisconnected(so); 4675065Swnj tp->t_state = TCPS_ESTABLISHED; 46817272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4695162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4705162Swnj } else 4715085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4725085Swnj 4735085Swnj trimthenstep6: 4745085Swnj /* 4755231Swnj * Advance ti->ti_seq to correspond to first data byte. 4765085Swnj * If data, trim to stay within window, 4775085Swnj * dropping FIN if necessary. 4785085Swnj */ 4795231Swnj ti->ti_seq++; 4805085Swnj if (ti->ti_len > tp->rcv_wnd) { 4815085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4825085Swnj m_adj(m, -todrop); 4835085Swnj ti->ti_len = tp->rcv_wnd; 48425939Skarels tiflags &= ~TH_FIN; 48530525Skarels tcpstat.tcps_rcvpackafterwin++; 48630525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4875065Swnj } 4885263Swnj tp->snd_wl1 = ti->ti_seq - 1; 48925939Skarels tp->rcv_up = ti->ti_seq; 4905085Swnj goto step6; 4915065Swnj } 4924601Swnj 4935065Swnj /* 49430525Skarels * States other than LISTEN or SYN_SENT. 49530525Skarels * First check that at least some bytes of segment are within 49630525Skarels * receive window. If segment begins before rcv_nxt, 49730525Skarels * drop leading data (and SYN); if nothing left, just ack. 49816222Skarels */ 49930525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 50030525Skarels if (todrop > 0) { 50130525Skarels if (tiflags & TH_SYN) { 50230525Skarels tiflags &= ~TH_SYN; 50330525Skarels ti->ti_seq++; 50430525Skarels if (ti->ti_urp > 1) 50530525Skarels ti->ti_urp--; 50630525Skarels else 50730525Skarels tiflags &= ~TH_URG; 50830525Skarels todrop--; 50930525Skarels } 51030525Skarels if (todrop > ti->ti_len || 51130525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 51231730Skarels #ifdef TCP_COMPAT_42 51331730Skarels /* 51431730Skarels * Don't toss RST in response to 4.2-style keepalive. 51531730Skarels */ 51631730Skarels if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 51731730Skarels goto do_rst; 51831730Skarels #endif 51930525Skarels tcpstat.tcps_rcvduppack++; 52030525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 521*32034Skarels todrop = ti->ti_len; 522*32034Skarels tp->t_flags |= TF_ACKNOW; 523*32034Skarels } else { 524*32034Skarels tcpstat.tcps_rcvpartduppack++; 525*32034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 52630525Skarels } 52730525Skarels m_adj(m, todrop); 52830525Skarels ti->ti_seq += todrop; 52930525Skarels ti->ti_len -= todrop; 53030525Skarels if (ti->ti_urp > todrop) 53130525Skarels ti->ti_urp -= todrop; 53230525Skarels else { 53330525Skarels tiflags &= ~TH_URG; 53430525Skarels ti->ti_urp = 0; 53530525Skarels } 53616222Skarels } 53716222Skarels 5385065Swnj if (tp->rcv_wnd == 0) { 5395065Swnj /* 5405065Swnj * If window is closed can only take segments at 5415231Swnj * window edge, and have to drop data and PUSH from 5425065Swnj * incoming segments. 54330525Skarels * 54430525Skarels * If new data is received on a connection after the 54530525Skarels * user processes are gone, then RST the other end. 5465065Swnj */ 54730525Skarels if ((so->so_state & SS_NOFDREF) && 54830525Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 54930525Skarels tp = tcp_close(tp); 55030525Skarels tcpstat.tcps_rcvafterclose++; 55130525Skarels goto dropwithreset; 55230525Skarels } 55330525Skarels if (tp->rcv_nxt != ti->ti_seq) { 55430525Skarels tcpstat.tcps_rcvpackafterwin++; 55530525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 5565065Swnj goto dropafterack; 55730525Skarels } 5585085Swnj if (ti->ti_len > 0) { 55930525Skarels if (ti->ti_len == 1) 56030525Skarels tcpstat.tcps_rcvwinprobe++; 56130525Skarels else { 56230525Skarels tcpstat.tcps_rcvpackafterwin++; 56330525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 56430525Skarels } 5655690Swnj m_adj(m, ti->ti_len); 5665085Swnj ti->ti_len = 0; 56725939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5685065Swnj } 5695065Swnj } else { 5705065Swnj /* 5715065Swnj * If segment ends after window, drop trailing data 5725085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5735065Swnj */ 5745690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5755690Swnj if (todrop > 0) { 57630525Skarels if (todrop >= ti->ti_len) { 57730525Skarels /* 57830525Skarels * If a new connection request is received 57930525Skarels * while in TIME_WAIT, drop the old connection 58030525Skarels * and start over if the sequence numbers 58130525Skarels * are above the previous ones. 58230525Skarels */ 58330525Skarels if (tiflags & TH_SYN && 58430525Skarels tp->t_state == TCPS_TIME_WAIT && 58530525Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 58630525Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 58730525Skarels (void) tcp_close(tp); 58830525Skarels goto findpcb; 58930525Skarels } 59030525Skarels if (todrop == 1) 59130525Skarels tcpstat.tcps_rcvwinprobe++; 59230525Skarels else { 59330525Skarels tcpstat.tcps_rcvpackafterwin++; 59431442Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 59530525Skarels } 5965065Swnj goto dropafterack; 59730525Skarels } 59830525Skarels tcpstat.tcps_rcvpackafterwin++; 59930525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 6005065Swnj m_adj(m, -todrop); 6015065Swnj ti->ti_len -= todrop; 60225939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6035065Swnj } 6045065Swnj } 6054601Swnj 60631730Skarels #ifdef TCP_COMPAT_42 60731730Skarels do_rst: 60831730Skarels #endif 6095065Swnj /* 6105065Swnj * If the RST bit is set examine the state: 6115065Swnj * SYN_RECEIVED STATE: 6125065Swnj * If passive open, return to LISTEN state. 6135065Swnj * If active open, inform user that connection was refused. 6145065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6155065Swnj * Inform user that connection was reset, and close tcb. 6165065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6175065Swnj * Close the tcb. 6185065Swnj */ 6195065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6205267Sroot 6215065Swnj case TCPS_SYN_RECEIVED: 62210394Ssam tp = tcp_drop(tp, ECONNREFUSED); 6235065Swnj goto drop; 6244601Swnj 6255065Swnj case TCPS_ESTABLISHED: 6265065Swnj case TCPS_FIN_WAIT_1: 6275065Swnj case TCPS_FIN_WAIT_2: 6285065Swnj case TCPS_CLOSE_WAIT: 62910394Ssam tp = tcp_drop(tp, ECONNRESET); 6305065Swnj goto drop; 6315065Swnj 6325065Swnj case TCPS_CLOSING: 6335065Swnj case TCPS_LAST_ACK: 6345065Swnj case TCPS_TIME_WAIT: 63510394Ssam tp = tcp_close(tp); 6365065Swnj goto drop; 6374601Swnj } 6384601Swnj 6394601Swnj /* 6405065Swnj * If a SYN is in the window, then this is an 6415065Swnj * error and we send an RST and drop the connection. 6424601Swnj */ 6435065Swnj if (tiflags & TH_SYN) { 64410394Ssam tp = tcp_drop(tp, ECONNRESET); 6455085Swnj goto dropwithreset; 6464601Swnj } 6474601Swnj 6484601Swnj /* 6495065Swnj * If the ACK bit is off we drop the segment and return. 6504601Swnj */ 6515085Swnj if ((tiflags & TH_ACK) == 0) 6525065Swnj goto drop; 6535065Swnj 6545065Swnj /* 6555065Swnj * Ack processing. 6565065Swnj */ 6574601Swnj switch (tp->t_state) { 6584601Swnj 6595065Swnj /* 6605065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 66131721Skarels * ESTABLISHED state and continue processing, otherwise 6625065Swnj * send an RST. 6635065Swnj */ 6645065Swnj case TCPS_SYN_RECEIVED: 6655085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6665231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6675085Swnj goto dropwithreset; 66830525Skarels tcpstat.tcps_connects++; 6695085Swnj soisconnected(so); 6705085Swnj tp->t_state = TCPS_ESTABLISHED; 67117272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6725162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6735244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6745085Swnj /* fall into ... */ 6754601Swnj 6765065Swnj /* 6775065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6785065Swnj * ACKs. If the ack is in the range 6795231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6805065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6815065Swnj * data from the retransmission queue. If this ACK reflects 6825065Swnj * more up to date window information we update our window information. 6835065Swnj */ 6845065Swnj case TCPS_ESTABLISHED: 6855065Swnj case TCPS_FIN_WAIT_1: 6865065Swnj case TCPS_FIN_WAIT_2: 6875065Swnj case TCPS_CLOSE_WAIT: 6885065Swnj case TCPS_CLOSING: 6895244Sroot case TCPS_LAST_ACK: 6905244Sroot case TCPS_TIME_WAIT: 6915085Swnj 69230525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 69330525Skarels if (ti->ti_len == 0) 69430525Skarels tcpstat.tcps_rcvdupack++; 6955065Swnj break; 69630525Skarels } 69730525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 69830525Skarels tcpstat.tcps_rcvacktoomuch++; 6995065Swnj goto dropafterack; 70030525Skarels } 7015085Swnj acked = ti->ti_ack - tp->snd_una; 70230525Skarels tcpstat.tcps_rcvackpack++; 70330525Skarels tcpstat.tcps_rcvackbyte += acked; 7045951Swnj 7055951Swnj /* 7065951Swnj * If transmit timer is running and timed sequence 7075951Swnj * number was acked, update smoothed round trip time. 708*32034Skarels * Since we now have an rtt measurement, cancel the 709*32034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 710*32034Skarels * Recompute the initial retransmit timer. 7115951Swnj */ 7125951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 71330525Skarels tcpstat.tcps_rttupdated++; 71431726Skarels if (tp->t_srtt != 0) { 71531726Skarels register short delta; 71631726Skarels 71731726Skarels /* 71831726Skarels * srtt is stored as fixed point with 3 bits 71931726Skarels * after the binary point (i.e., scaled by 8). 72031726Skarels * The following magic is equivalent 72131726Skarels * to the smoothing algorithm in rfc793 72231726Skarels * with an alpha of .875 72331726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 72431726Skarels */ 72531726Skarels delta = tp->t_rtt - (tp->t_srtt >> 3); 72631726Skarels if ((tp->t_srtt += delta) <= 0) 72731726Skarels tp->t_srtt = 1; 72831726Skarels /* 729*32034Skarels * We accumulate a smoothed rtt variance 730*32034Skarels * (actually, a smoothed mean difference), 73131726Skarels * then set the retransmit timer to smoothed 73231726Skarels * rtt + 2 times the smoothed variance. 73331726Skarels * rttvar is strored as fixed point 73431726Skarels * with 2 bits after the binary point 73531726Skarels * (scaled by 4). The following is equivalent 73631726Skarels * to rfc793 smoothing with an alpha of .75 73731726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 73831726Skarels * This replaces rfc793's wired-in beta. 73931726Skarels */ 74031726Skarels if (delta < 0) 74131726Skarels delta = -delta; 74231726Skarels delta -= (tp->t_rttvar >> 2); 74331726Skarels if ((tp->t_rttvar += delta) <= 0) 74431726Skarels tp->t_rttvar = 1; 74531726Skarels } else { 74631726Skarels /* 74731726Skarels * No rtt measurement yet - use the 74831726Skarels * unsmoothed rtt. Set the variance 74931726Skarels * to half the rtt (so our first 75031726Skarels * retransmit happens at 2*rtt) 75131726Skarels */ 75231726Skarels tp->t_srtt = tp->t_rtt << 3; 75331726Skarels tp->t_rttvar = tp->t_rtt << 1; 75431726Skarels } 7555951Swnj tp->t_rtt = 0; 756*32034Skarels tp->t_rxtshift = 0; 757*32034Skarels TCPT_RANGESET(tp->t_rxtcur, 758*32034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 759*32034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 7605951Swnj } 7615951Swnj 76226824Skarels /* 76326824Skarels * If all outstanding data is acked, stop retransmit 76426824Skarels * timer and remember to restart (more output or persist). 76526824Skarels * If there is more data to be acked, restart retransmit 766*32034Skarels * timer, using current (possibly backed-off) value. 76726824Skarels */ 76826824Skarels if (ti->ti_ack == tp->snd_max) { 7695244Sroot tp->t_timer[TCPT_REXMT] = 0; 77026824Skarels needoutput = 1; 771*32034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 772*32034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 77317360Skarels /* 77431442Skarels * When new data is acked, open the congestion window 775*32034Skarels * by one max-sized segment. 77617360Skarels */ 77731442Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + tp->t_maxseg, 65535); 7785307Sroot if (acked > so->so_snd.sb_cc) { 77915386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 78026386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 78131721Skarels ourfinisacked = 1; 7825307Sroot } else { 7836161Ssam sbdrop(&so->so_snd, acked); 7845307Sroot tp->snd_wnd -= acked; 78531721Skarels ourfinisacked = 0; 7865307Sroot } 7876434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 7885300Sroot sowwakeup(so); 7895231Swnj tp->snd_una = ti->ti_ack; 7905357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 7915357Sroot tp->snd_nxt = tp->snd_una; 7925162Swnj 7934601Swnj switch (tp->t_state) { 7944601Swnj 7955065Swnj /* 7965065Swnj * In FIN_WAIT_1 STATE in addition to the processing 7975065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 7985085Swnj * then enter FIN_WAIT_2. 7995065Swnj */ 8005065Swnj case TCPS_FIN_WAIT_1: 8015896Swnj if (ourfinisacked) { 8025896Swnj /* 8035896Swnj * If we can't receive any more 8045896Swnj * data, then closing user can proceed. 80524816Skarels * Starting the timer is contrary to the 80624816Skarels * specification, but if we don't get a FIN 80724816Skarels * we'll hang forever. 8085896Swnj */ 80924816Skarels if (so->so_state & SS_CANTRCVMORE) { 8105896Swnj soisdisconnected(so); 81124816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 81224816Skarels } 8135085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8145896Swnj } 8154601Swnj break; 8164601Swnj 8175065Swnj /* 8185065Swnj * In CLOSING STATE in addition to the processing for 8195065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8205065Swnj * then enter the TIME-WAIT state, otherwise ignore 8215065Swnj * the segment. 8225065Swnj */ 8235065Swnj case TCPS_CLOSING: 8245244Sroot if (ourfinisacked) { 8255065Swnj tp->t_state = TCPS_TIME_WAIT; 8265244Sroot tcp_canceltimers(tp); 8275244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8285244Sroot soisdisconnected(so); 8295244Sroot } 8305244Sroot break; 8314601Swnj 8325065Swnj /* 83331743Skarels * In LAST_ACK, we may still be waiting for data to drain 83431743Skarels * and/or to be acked, as well as for the ack of our FIN. 83531743Skarels * If our FIN is now acknowledged, delete the TCB, 83631743Skarels * enter the closed state and return. 8375065Swnj */ 8385065Swnj case TCPS_LAST_ACK: 83931743Skarels if (ourfinisacked) { 84010394Ssam tp = tcp_close(tp); 84131743Skarels goto drop; 84231743Skarels } 84331743Skarels break; 8444601Swnj 8455065Swnj /* 8465065Swnj * In TIME_WAIT state the only thing that should arrive 8475065Swnj * is a retransmission of the remote FIN. Acknowledge 8485065Swnj * it and restart the finack timer. 8495065Swnj */ 8505065Swnj case TCPS_TIME_WAIT: 8515162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8525065Swnj goto dropafterack; 8534601Swnj } 8545085Swnj } 8554601Swnj 8565065Swnj step6: 8575065Swnj /* 8585244Sroot * Update window information. 85925939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 8605244Sroot */ 86125939Skarels if ((tiflags & TH_ACK) && 86225939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 8635391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 86425939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 86530525Skarels /* keep track of pure window updates */ 86630525Skarels if (ti->ti_len == 0 && 86730525Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) { 86830525Skarels tcpstat.tcps_rcvwinupd++; 86930525Skarels tcpstat.tcps_rcvdupack--; 87030525Skarels } 8715244Sroot tp->snd_wnd = ti->ti_win; 8725244Sroot tp->snd_wl1 = ti->ti_seq; 8735244Sroot tp->snd_wl2 = ti->ti_ack; 87425260Skarels if (tp->snd_wnd > tp->max_sndwnd) 87525260Skarels tp->max_sndwnd = tp->snd_wnd; 87626824Skarels needoutput = 1; 87726824Skarels } 8785244Sroot 8795244Sroot /* 8805547Swnj * Process segments with URG. 8815065Swnj */ 8827267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 8837267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8845547Swnj /* 88525939Skarels * This is a kludge, but if we receive and accept 88613121Ssam * random urgent pointers, we'll crash in 88713121Ssam * soreceive. It's hard to imagine someone 88813121Ssam * actually wanting to send this much urgent data. 88912441Ssam */ 89025652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 89112441Ssam ti->ti_urp = 0; /* XXX */ 89212441Ssam tiflags &= ~TH_URG; /* XXX */ 89325939Skarels goto dodata; /* XXX */ 89412441Ssam } 89512441Ssam /* 8965547Swnj * If this segment advances the known urgent pointer, 8975547Swnj * then mark the data stream. This should not happen 8985547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 8995547Swnj * a FIN has been received from the remote side. 9005547Swnj * In these states we ignore the URG. 90127190Skarels * 90227190Skarels * According to RFC961 (Assigned Protocols), 90327190Skarels * the urgent pointer points to the last octet 90427190Skarels * of urgent data. We continue, however, 90527190Skarels * to consider it to indicate the first octet 90627190Skarels * of data past the urgent section 90727190Skarels * as the original spec states. 9085547Swnj */ 9095547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9105547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9115547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9125547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9135547Swnj if (so->so_oobmark == 0) 9145547Swnj so->so_state |= SS_RCVATMARK; 9158313Sroot sohasoutofband(so); 91624816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9175440Swnj } 9185547Swnj /* 9195547Swnj * Remove out of band data so doesn't get presented to user. 9205547Swnj * This can happen independent of advancing the URG pointer, 9215547Swnj * but if two URG's are pending at once, some out-of-band 9225547Swnj * data may creep in... ick. 9235547Swnj */ 92427190Skarels if (ti->ti_urp <= ti->ti_len && 92527190Skarels (so->so_options & SO_OOBINLINE) == 0) 9265547Swnj tcp_pulloutofband(so, ti); 92725939Skarels } else 92825939Skarels /* 92925939Skarels * If no out of band data is expected, 93025939Skarels * pull receive urgent pointer along 93125939Skarels * with the receive window. 93225939Skarels */ 93325939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 93425939Skarels tp->rcv_up = tp->rcv_nxt; 93525939Skarels dodata: /* XXX */ 9364601Swnj 9374601Swnj /* 9385065Swnj * Process the segment text, merging it into the TCP sequencing queue, 9395065Swnj * and arranging for acknowledgment of receipt if necessary. 9405065Swnj * This process logically involves adjusting tp->rcv_wnd as data 9415065Swnj * is presented to the user (this happens in tcp_usrreq.c, 9425065Swnj * case PRU_RCVD). If a FIN has already been received on this 9435065Swnj * connection then we just ignore the text. 9444601Swnj */ 94517946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 94617946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 94724816Skarels TCP_REASS(tp, ti, m, so, tiflags); 9485440Swnj if (tcpnodelack == 0) 9495440Swnj tp->t_flags |= TF_DELACK; 9505440Swnj else 9515440Swnj tp->t_flags |= TF_ACKNOW; 95225260Skarels /* 95325260Skarels * Note the amount of data that peer has sent into 95425260Skarels * our window, in order to estimate the sender's 95525260Skarels * buffer size. 95625260Skarels */ 957*32034Skarels len = tp->rcv_nxt - tp->rcv_adv; 95825260Skarels if (len > tp->max_rcvd) 95925260Skarels tp->max_rcvd = len; 9605244Sroot } else { 9614924Swnj m_freem(m); 9625263Swnj tiflags &= ~TH_FIN; 9635244Sroot } 9644601Swnj 9654601Swnj /* 9665263Swnj * If FIN is received ACK the FIN and let the user know 9675263Swnj * that the connection is closing. 9684601Swnj */ 9695263Swnj if (tiflags & TH_FIN) { 9705244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9715244Sroot socantrcvmore(so); 9725244Sroot tp->t_flags |= TF_ACKNOW; 9735244Sroot tp->rcv_nxt++; 9745244Sroot } 9755065Swnj switch (tp->t_state) { 9764601Swnj 9775065Swnj /* 9785065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 9795065Swnj * enter the CLOSE_WAIT state. 9804884Swnj */ 9815065Swnj case TCPS_SYN_RECEIVED: 9825065Swnj case TCPS_ESTABLISHED: 9835065Swnj tp->t_state = TCPS_CLOSE_WAIT; 9845065Swnj break; 9854884Swnj 9865065Swnj /* 9875085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 9885085Swnj * enter the CLOSING state. 9894884Swnj */ 9905065Swnj case TCPS_FIN_WAIT_1: 9915085Swnj tp->t_state = TCPS_CLOSING; 9925065Swnj break; 9934601Swnj 9945065Swnj /* 9955065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 9965065Swnj * starting the time-wait timer, turning off the other 9975065Swnj * standard timers. 9985065Swnj */ 9995065Swnj case TCPS_FIN_WAIT_2: 10005244Sroot tp->t_state = TCPS_TIME_WAIT; 10015074Swnj tcp_canceltimers(tp); 10025162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10035244Sroot soisdisconnected(so); 10045065Swnj break; 10055065Swnj 10064884Swnj /* 10075065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10084884Swnj */ 10095065Swnj case TCPS_TIME_WAIT: 10105162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10115065Swnj break; 10125085Swnj } 10134601Swnj } 10145267Sroot if (so->so_options & SO_DEBUG) 10155267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10165085Swnj 10175085Swnj /* 10185085Swnj * Return any desired output. 10195085Swnj */ 102026824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 102125939Skarels (void) tcp_output(tp); 10225065Swnj return; 10235085Swnj 10245065Swnj dropafterack: 10255085Swnj /* 10266211Swnj * Generate an ACK dropping incoming segment if it occupies 10276211Swnj * sequence space, where the ACK reflects our state. 10285085Swnj */ 102926057Skarels if (tiflags & TH_RST) 10305085Swnj goto drop; 103131749Skarels m_freem(m); 103231721Skarels tp->t_flags |= TF_ACKNOW; 103331721Skarels (void) tcp_output(tp); 10345231Swnj return; 10355085Swnj 10365085Swnj dropwithreset: 103711731Ssam if (om) { 10386161Ssam (void) m_free(om); 103911731Ssam om = 0; 104011731Ssam } 10415085Swnj /* 10425244Sroot * Generate a RST, dropping incoming segment. 10435085Swnj * Make ACK acceptable to originator of segment. 104425197Skarels * Don't bother to respond if destination was broadcast. 10455085Swnj */ 104625197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 10475085Swnj goto drop; 10485085Swnj if (tiflags & TH_ACK) 10495391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 10505085Swnj else { 10515085Swnj if (tiflags & TH_SYN) 10525085Swnj ti->ti_len++; 10536211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 10546211Swnj TH_RST|TH_ACK); 10555085Swnj } 105610769Ssam /* destroy temporarily created socket */ 105710769Ssam if (dropsocket) 105810769Ssam (void) soabort(so); 10595231Swnj return; 10605085Swnj 10615065Swnj drop: 106211730Ssam if (om) 106311730Ssam (void) m_free(om); 10645085Swnj /* 10655085Swnj * Drop space held by incoming segment and return. 10665085Swnj */ 10676303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 10686303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 10695065Swnj m_freem(m); 107010769Ssam /* destroy temporarily created socket */ 107110769Ssam if (dropsocket) 107210769Ssam (void) soabort(so); 10735267Sroot return; 10745065Swnj } 10755065Swnj 107617272Skarels tcp_dooptions(tp, om, ti) 10775440Swnj struct tcpcb *tp; 10785440Swnj struct mbuf *om; 107917272Skarels struct tcpiphdr *ti; 10805419Swnj { 10815440Swnj register u_char *cp; 10825440Swnj int opt, optlen, cnt; 10835419Swnj 10845440Swnj cp = mtod(om, u_char *); 10855440Swnj cnt = om->m_len; 10865440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 10875440Swnj opt = cp[0]; 10885440Swnj if (opt == TCPOPT_EOL) 10895440Swnj break; 10905440Swnj if (opt == TCPOPT_NOP) 10915440Swnj optlen = 1; 109212169Ssam else { 10935440Swnj optlen = cp[1]; 109412169Ssam if (optlen <= 0) 109512169Ssam break; 109612169Ssam } 10975440Swnj switch (opt) { 10985440Swnj 10995440Swnj default: 11005440Swnj break; 11015440Swnj 11025440Swnj case TCPOPT_MAXSEG: 11035440Swnj if (optlen != 4) 11045440Swnj continue; 110517272Skarels if (!(ti->ti_flags & TH_SYN)) 110617272Skarels continue; 11075440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11086161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 110917272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 11105440Swnj break; 11115419Swnj } 11125419Swnj } 11136161Ssam (void) m_free(om); 11145419Swnj } 11155419Swnj 11165419Swnj /* 11175547Swnj * Pull out of band byte out of a segment so 11185547Swnj * it doesn't appear in the user's data queue. 11195547Swnj * It is still reflected in the segment length for 11205547Swnj * sequencing purposes. 11215547Swnj */ 11225547Swnj tcp_pulloutofband(so, ti) 11235547Swnj struct socket *so; 11245547Swnj struct tcpiphdr *ti; 11255547Swnj { 11265547Swnj register struct mbuf *m; 11276116Swnj int cnt = ti->ti_urp - 1; 11285547Swnj 11295547Swnj m = dtom(ti); 11305547Swnj while (cnt >= 0) { 11315547Swnj if (m->m_len > cnt) { 11325547Swnj char *cp = mtod(m, caddr_t) + cnt; 11335547Swnj struct tcpcb *tp = sototcpcb(so); 11345547Swnj 11355547Swnj tp->t_iobc = *cp; 11365547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 11376161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 11385547Swnj m->m_len--; 11395547Swnj return; 11405547Swnj } 11415547Swnj cnt -= m->m_len; 11425547Swnj m = m->m_next; 11435547Swnj if (m == 0) 11445547Swnj break; 11455547Swnj } 11465547Swnj panic("tcp_pulloutofband"); 11475547Swnj } 11485547Swnj 11495547Swnj /* 115017272Skarels * Determine a reasonable value for maxseg size. 115117272Skarels * If the route is known, use one that can be handled 115217272Skarels * on the given interface without forcing IP to fragment. 115331726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 115431726Skarels * to utilize large mbufs. 115517272Skarels * If interface pointer is unavailable, or the destination isn't local, 115623975Skarels * use a conservative size (512 or the default IP max size, but no more 115723975Skarels * than the mtu of the interface through which we route), 115817272Skarels * as we can't discover anything about intervening gateways or networks. 1159*32034Skarels * We also initialize the congestion/slow start window to be a single 1160*32034Skarels * segment if the destination isn't local; this information should 1161*32034Skarels * probably all be saved with the routing entry at the transport level. 116217272Skarels * 116317272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 116417272Skarels */ 116517272Skarels tcp_mss(tp) 116623975Skarels register struct tcpcb *tp; 116717272Skarels { 116817272Skarels struct route *ro; 116917272Skarels struct ifnet *ifp; 117017272Skarels int mss; 117117272Skarels struct inpcb *inp; 117217272Skarels 117317272Skarels inp = tp->t_inpcb; 117417272Skarels ro = &inp->inp_route; 117517272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 117617272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 117717272Skarels /* No route yet, so try to acquire one */ 117817272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 117917272Skarels ro->ro_dst.sa_family = AF_INET; 118017272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 118117272Skarels inp->inp_faddr; 118217272Skarels rtalloc(ro); 118317272Skarels } 118417272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 118517316Skarels return (TCP_MSS); 118617272Skarels } 118717272Skarels 118817272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 118931726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 119031726Skarels if (mss > MCLBYTES) 119131726Skarels mss &= ~(MCLBYTES-1); 119217272Skarels #else 119331726Skarels if (mss > MCLBYTES) 119431726Skarels mss = mss / MCLBYTES * MCLBYTES; 119517272Skarels #endif 119623975Skarels if (in_localaddr(inp->inp_faddr)) 119723975Skarels return (mss); 1198*32034Skarels mss = MIN(mss, TCP_MSS); 1199*32034Skarels tp->snd_cwnd = mss; 1200*32034Skarels return (mss); 120117272Skarels } 1202