123190Smckusick /* 229149Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 332787Sbostic * All rights reserved. 423190Smckusick * 532787Sbostic * Redistribution and use in source and binary forms are permitted 632787Sbostic * provided that this notice is preserved and that due credit is given 732787Sbostic * to the University of California at Berkeley. The name of the University 832787Sbostic * may not be used to endorse or promote products derived from this 932787Sbostic * software without specific prior written permission. This software 1032787Sbostic * is provided ``as is'' without express or implied warranty. 1132787Sbostic * 12*33445Skarels * @(#)tcp_input.c 7.15 (Berkeley) 02/07/88 1323190Smckusick */ 144601Swnj 1517062Sbloom #include "param.h" 1617062Sbloom #include "systm.h" 1717062Sbloom #include "mbuf.h" 1817062Sbloom #include "protosw.h" 1917062Sbloom #include "socket.h" 2017062Sbloom #include "socketvar.h" 2117062Sbloom #include "errno.h" 2210894Ssam 2310894Ssam #include "../net/if.h" 2410894Ssam #include "../net/route.h" 2510894Ssam 2617062Sbloom #include "in.h" 2717062Sbloom #include "in_pcb.h" 2817062Sbloom #include "in_systm.h" 2917062Sbloom #include "ip.h" 3017062Sbloom #include "ip_var.h" 3117062Sbloom #include "tcp.h" 3217062Sbloom #include "tcp_fsm.h" 3317062Sbloom #include "tcp_seq.h" 3417062Sbloom #include "tcp_timer.h" 3517062Sbloom #include "tcp_var.h" 3617062Sbloom #include "tcpip.h" 3717062Sbloom #include "tcp_debug.h" 384601Swnj 395300Sroot int tcpprintfs = 0; 404679Swnj int tcpcksum = 1; 4132098Skarels int tcprexmtthresh = 3; 425267Sroot struct tcpiphdr tcp_saveti; 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 558*33445Skarels /* 559*33445Skarels * If segment ends after window, drop trailing data 560*33445Skarels * (and PUSH and FIN); if nothing left, just ACK. 561*33445Skarels */ 562*33445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 563*33445Skarels if (todrop > 0) { 564*33445Skarels tcpstat.tcps_rcvpackafterwin++; 565*33445Skarels if (todrop >= ti->ti_len) { 56630525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 567*33445Skarels /* 568*33445Skarels * If a new connection request is received 569*33445Skarels * while in TIME_WAIT, drop the old connection 570*33445Skarels * and start over if the sequence numbers 571*33445Skarels * are above the previous ones. 572*33445Skarels */ 573*33445Skarels if (tiflags & TH_SYN && 574*33445Skarels tp->t_state == TCPS_TIME_WAIT && 575*33445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 576*33445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 577*33445Skarels (void) tcp_close(tp); 578*33445Skarels goto findpcb; 579*33445Skarels } 580*33445Skarels /* 581*33445Skarels * If window is closed can only take segments at 582*33445Skarels * window edge, and have to drop data and PUSH from 583*33445Skarels * incoming segments. Continue processing, but 584*33445Skarels * remember to ack. Otherwise, drop segment 585*33445Skarels * and ack. 586*33445Skarels */ 587*33445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 588*33445Skarels tp->t_flags |= TF_ACKNOW; 58930525Skarels tcpstat.tcps_rcvwinprobe++; 590*33445Skarels } else 5915065Swnj goto dropafterack; 592*33445Skarels } else 59330525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 594*33445Skarels m_adj(m, -todrop); 595*33445Skarels ti->ti_len -= todrop; 596*33445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5975065Swnj } 5984601Swnj 59931730Skarels #ifdef TCP_COMPAT_42 60031730Skarels do_rst: 60131730Skarels #endif 6025065Swnj /* 6035065Swnj * If the RST bit is set examine the state: 6045065Swnj * SYN_RECEIVED STATE: 6055065Swnj * If passive open, return to LISTEN state. 6065065Swnj * If active open, inform user that connection was refused. 6075065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6085065Swnj * Inform user that connection was reset, and close tcb. 6095065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6105065Swnj * Close the tcb. 6115065Swnj */ 6125065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6135267Sroot 6145065Swnj case TCPS_SYN_RECEIVED: 61510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 6165065Swnj goto drop; 6174601Swnj 6185065Swnj case TCPS_ESTABLISHED: 6195065Swnj case TCPS_FIN_WAIT_1: 6205065Swnj case TCPS_FIN_WAIT_2: 6215065Swnj case TCPS_CLOSE_WAIT: 62210394Ssam tp = tcp_drop(tp, ECONNRESET); 6235065Swnj goto drop; 6245065Swnj 6255065Swnj case TCPS_CLOSING: 6265065Swnj case TCPS_LAST_ACK: 6275065Swnj case TCPS_TIME_WAIT: 62810394Ssam tp = tcp_close(tp); 6295065Swnj goto drop; 6304601Swnj } 6314601Swnj 6324601Swnj /* 6335065Swnj * If a SYN is in the window, then this is an 6345065Swnj * error and we send an RST and drop the connection. 6354601Swnj */ 6365065Swnj if (tiflags & TH_SYN) { 63710394Ssam tp = tcp_drop(tp, ECONNRESET); 6385085Swnj goto dropwithreset; 6394601Swnj } 6404601Swnj 6414601Swnj /* 6425065Swnj * If the ACK bit is off we drop the segment and return. 6434601Swnj */ 6445085Swnj if ((tiflags & TH_ACK) == 0) 6455065Swnj goto drop; 6465065Swnj 6475065Swnj /* 6485065Swnj * Ack processing. 6495065Swnj */ 6504601Swnj switch (tp->t_state) { 6514601Swnj 6525065Swnj /* 6535065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 65431721Skarels * ESTABLISHED state and continue processing, otherwise 6555065Swnj * send an RST. 6565065Swnj */ 6575065Swnj case TCPS_SYN_RECEIVED: 6585085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6595231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6605085Swnj goto dropwithreset; 66130525Skarels tcpstat.tcps_connects++; 6625085Swnj soisconnected(so); 6635085Swnj tp->t_state = TCPS_ESTABLISHED; 66417272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6655162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6665244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6675085Swnj /* fall into ... */ 6684601Swnj 6695065Swnj /* 6705065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6715065Swnj * ACKs. If the ack is in the range 6725231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6735065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6745065Swnj * data from the retransmission queue. If this ACK reflects 6755065Swnj * more up to date window information we update our window information. 6765065Swnj */ 6775065Swnj case TCPS_ESTABLISHED: 6785065Swnj case TCPS_FIN_WAIT_1: 6795065Swnj case TCPS_FIN_WAIT_2: 6805065Swnj case TCPS_CLOSE_WAIT: 6815065Swnj case TCPS_CLOSING: 6825244Sroot case TCPS_LAST_ACK: 6835244Sroot case TCPS_TIME_WAIT: 6845085Swnj 68530525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 68632098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 68730525Skarels tcpstat.tcps_rcvdupack++; 68832098Skarels /* 68932098Skarels * If we have outstanding data (not a 69032098Skarels * window probe), this is a completely 69132098Skarels * duplicate ack (ie, window info didn't 69232098Skarels * change), the ack is the biggest we've 69332098Skarels * seen and we've seen exactly our rexmt 69432098Skarels * threshhold of them, assume a packet 69532098Skarels * has been dropped and retransmit it. 69632098Skarels * Kludge snd_nxt & the congestion 69732098Skarels * window so we send only this one 69832376Skarels * packet. If this packet fills the 69932376Skarels * only hole in the receiver's seq. 70032376Skarels * space, the next real ack will fully 70132376Skarels * open our window. This means we 70232376Skarels * have to do the usual slow-start to 70332376Skarels * not overwhelm an intermediate gateway 70432376Skarels * with a burst of packets. Leave 70532376Skarels * here with the congestion window set 70632376Skarels * to allow 2 packets on the next real 70732376Skarels * ack and the exp-to-linear thresh 70832376Skarels * set for half the current window 70932376Skarels * size (since we know we're losing at 71032376Skarels * the current window size). 71132098Skarels */ 71232098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 71332098Skarels ti->ti_ack != tp->snd_una) 71432098Skarels tp->t_dupacks = 0; 71532098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 71632098Skarels tcp_seq onxt = tp->snd_nxt; 71732376Skarels u_int win = 71832376Skarels MIN(tp->snd_wnd, tp->snd_cwnd) / 2 / 71932376Skarels tp->t_maxseg; 72032098Skarels 72132376Skarels if (win < 2) 72232376Skarels win = 2; 72332376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 72432376Skarels 72532098Skarels tp->t_timer[TCPT_REXMT] = 0; 72632098Skarels tp->t_rtt = 0; 72732098Skarels tp->snd_nxt = ti->ti_ack; 72832098Skarels tp->snd_cwnd = tp->t_maxseg; 72932098Skarels (void) tcp_output(tp); 73032098Skarels 73132098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 73232098Skarels tp->snd_nxt = onxt; 73332098Skarels goto drop; 73432098Skarels } 73532098Skarels } else 73632098Skarels tp->t_dupacks = 0; 7375065Swnj break; 73830525Skarels } 73932098Skarels tp->t_dupacks = 0; 74030525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 74130525Skarels tcpstat.tcps_rcvacktoomuch++; 7425065Swnj goto dropafterack; 74330525Skarels } 7445085Swnj acked = ti->ti_ack - tp->snd_una; 74530525Skarels tcpstat.tcps_rcvackpack++; 74630525Skarels tcpstat.tcps_rcvackbyte += acked; 7475951Swnj 7485951Swnj /* 7495951Swnj * If transmit timer is running and timed sequence 7505951Swnj * number was acked, update smoothed round trip time. 75132034Skarels * Since we now have an rtt measurement, cancel the 75232034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 75332034Skarels * Recompute the initial retransmit timer. 7545951Swnj */ 7555951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 75630525Skarels tcpstat.tcps_rttupdated++; 75731726Skarels if (tp->t_srtt != 0) { 75831726Skarels register short delta; 75931726Skarels 76031726Skarels /* 76131726Skarels * srtt is stored as fixed point with 3 bits 76231726Skarels * after the binary point (i.e., scaled by 8). 76331726Skarels * The following magic is equivalent 76431726Skarels * to the smoothing algorithm in rfc793 76531726Skarels * with an alpha of .875 76631726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 76732098Skarels * Adjust t_rtt to origin 0. 76831726Skarels */ 76932098Skarels tp->t_rtt--; 77031726Skarels delta = tp->t_rtt - (tp->t_srtt >> 3); 77131726Skarels if ((tp->t_srtt += delta) <= 0) 77231726Skarels tp->t_srtt = 1; 77331726Skarels /* 77432034Skarels * We accumulate a smoothed rtt variance 77532034Skarels * (actually, a smoothed mean difference), 77631726Skarels * then set the retransmit timer to smoothed 77731726Skarels * rtt + 2 times the smoothed variance. 77832098Skarels * rttvar is stored as fixed point 77931726Skarels * with 2 bits after the binary point 78031726Skarels * (scaled by 4). The following is equivalent 78131726Skarels * to rfc793 smoothing with an alpha of .75 78231726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 78331726Skarels * This replaces rfc793's wired-in beta. 78431726Skarels */ 78531726Skarels if (delta < 0) 78631726Skarels delta = -delta; 78731726Skarels delta -= (tp->t_rttvar >> 2); 78831726Skarels if ((tp->t_rttvar += delta) <= 0) 78931726Skarels tp->t_rttvar = 1; 79031726Skarels } else { 79131726Skarels /* 79231726Skarels * No rtt measurement yet - use the 79331726Skarels * unsmoothed rtt. Set the variance 79431726Skarels * to half the rtt (so our first 79531726Skarels * retransmit happens at 2*rtt) 79631726Skarels */ 79731726Skarels tp->t_srtt = tp->t_rtt << 3; 79831726Skarels tp->t_rttvar = tp->t_rtt << 1; 79931726Skarels } 8005951Swnj tp->t_rtt = 0; 80132034Skarels tp->t_rxtshift = 0; 80232034Skarels TCPT_RANGESET(tp->t_rxtcur, 80332034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 80432034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 8055951Swnj } 8065951Swnj 80726824Skarels /* 80826824Skarels * If all outstanding data is acked, stop retransmit 80926824Skarels * timer and remember to restart (more output or persist). 81026824Skarels * If there is more data to be acked, restart retransmit 81132034Skarels * timer, using current (possibly backed-off) value. 81226824Skarels */ 81326824Skarels if (ti->ti_ack == tp->snd_max) { 8145244Sroot tp->t_timer[TCPT_REXMT] = 0; 81526824Skarels needoutput = 1; 81632034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 81732034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 81817360Skarels /* 81932098Skarels * When new data is acked, open the congestion window. 82032098Skarels * If the window gives us less than ssthresh packets 82132098Skarels * in flight, open exponentially (maxseg per packet). 82232098Skarels * Otherwise open linearly (maxseg per window, 82332098Skarels * or maxseg^2 / cwnd per packet). 82417360Skarels */ 82532098Skarels { 82632098Skarels u_int incr = tp->t_maxseg; 82732098Skarels 82832098Skarels if (tp->snd_cwnd > tp->snd_ssthresh) 82932098Skarels incr = MAX(incr * incr / tp->snd_cwnd, 1); 83032098Skarels 83132098Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + incr, 65535); /* XXX */ 83232098Skarels } 8335307Sroot if (acked > so->so_snd.sb_cc) { 83415386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 83526386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 83631721Skarels ourfinisacked = 1; 8375307Sroot } else { 8386161Ssam sbdrop(&so->so_snd, acked); 8395307Sroot tp->snd_wnd -= acked; 84031721Skarels ourfinisacked = 0; 8415307Sroot } 8426434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 8435300Sroot sowwakeup(so); 8445231Swnj tp->snd_una = ti->ti_ack; 8455357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 8465357Sroot tp->snd_nxt = tp->snd_una; 8475162Swnj 8484601Swnj switch (tp->t_state) { 8494601Swnj 8505065Swnj /* 8515065Swnj * In FIN_WAIT_1 STATE in addition to the processing 8525065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 8535085Swnj * then enter FIN_WAIT_2. 8545065Swnj */ 8555065Swnj case TCPS_FIN_WAIT_1: 8565896Swnj if (ourfinisacked) { 8575896Swnj /* 8585896Swnj * If we can't receive any more 8595896Swnj * data, then closing user can proceed. 86024816Skarels * Starting the timer is contrary to the 86124816Skarels * specification, but if we don't get a FIN 86224816Skarels * we'll hang forever. 8635896Swnj */ 86424816Skarels if (so->so_state & SS_CANTRCVMORE) { 8655896Swnj soisdisconnected(so); 86624816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 86724816Skarels } 8685085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8695896Swnj } 8704601Swnj break; 8714601Swnj 8725065Swnj /* 8735065Swnj * In CLOSING STATE in addition to the processing for 8745065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8755065Swnj * then enter the TIME-WAIT state, otherwise ignore 8765065Swnj * the segment. 8775065Swnj */ 8785065Swnj case TCPS_CLOSING: 8795244Sroot if (ourfinisacked) { 8805065Swnj tp->t_state = TCPS_TIME_WAIT; 8815244Sroot tcp_canceltimers(tp); 8825244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8835244Sroot soisdisconnected(so); 8845244Sroot } 8855244Sroot break; 8864601Swnj 8875065Swnj /* 88831743Skarels * In LAST_ACK, we may still be waiting for data to drain 88931743Skarels * and/or to be acked, as well as for the ack of our FIN. 89031743Skarels * If our FIN is now acknowledged, delete the TCB, 89131743Skarels * enter the closed state and return. 8925065Swnj */ 8935065Swnj case TCPS_LAST_ACK: 89431743Skarels if (ourfinisacked) { 89510394Ssam tp = tcp_close(tp); 89631743Skarels goto drop; 89731743Skarels } 89831743Skarels break; 8994601Swnj 9005065Swnj /* 9015065Swnj * In TIME_WAIT state the only thing that should arrive 9025065Swnj * is a retransmission of the remote FIN. Acknowledge 9035065Swnj * it and restart the finack timer. 9045065Swnj */ 9055065Swnj case TCPS_TIME_WAIT: 9065162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9075065Swnj goto dropafterack; 9084601Swnj } 9095085Swnj } 9104601Swnj 9115065Swnj step6: 9125065Swnj /* 9135244Sroot * Update window information. 91425939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9155244Sroot */ 91625939Skarels if ((tiflags & TH_ACK) && 91725939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9185391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 91925939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 92030525Skarels /* keep track of pure window updates */ 92130525Skarels if (ti->ti_len == 0 && 92232098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 92330525Skarels tcpstat.tcps_rcvwinupd++; 9245244Sroot tp->snd_wnd = ti->ti_win; 9255244Sroot tp->snd_wl1 = ti->ti_seq; 9265244Sroot tp->snd_wl2 = ti->ti_ack; 92725260Skarels if (tp->snd_wnd > tp->max_sndwnd) 92825260Skarels tp->max_sndwnd = tp->snd_wnd; 92926824Skarels needoutput = 1; 93026824Skarels } 9315244Sroot 9325244Sroot /* 9335547Swnj * Process segments with URG. 9345065Swnj */ 9357267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9367267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9375547Swnj /* 93825939Skarels * This is a kludge, but if we receive and accept 93913121Ssam * random urgent pointers, we'll crash in 94013121Ssam * soreceive. It's hard to imagine someone 94113121Ssam * actually wanting to send this much urgent data. 94212441Ssam */ 94325652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 94412441Ssam ti->ti_urp = 0; /* XXX */ 94512441Ssam tiflags &= ~TH_URG; /* XXX */ 94625939Skarels goto dodata; /* XXX */ 94712441Ssam } 94812441Ssam /* 9495547Swnj * If this segment advances the known urgent pointer, 9505547Swnj * then mark the data stream. This should not happen 9515547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 9525547Swnj * a FIN has been received from the remote side. 9535547Swnj * In these states we ignore the URG. 95427190Skarels * 95527190Skarels * According to RFC961 (Assigned Protocols), 95627190Skarels * the urgent pointer points to the last octet 95727190Skarels * of urgent data. We continue, however, 95827190Skarels * to consider it to indicate the first octet 95927190Skarels * of data past the urgent section 96027190Skarels * as the original spec states. 9615547Swnj */ 9625547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9635547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9645547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9655547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9665547Swnj if (so->so_oobmark == 0) 9675547Swnj so->so_state |= SS_RCVATMARK; 9688313Sroot sohasoutofband(so); 96924816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9705440Swnj } 9715547Swnj /* 9725547Swnj * Remove out of band data so doesn't get presented to user. 9735547Swnj * This can happen independent of advancing the URG pointer, 9745547Swnj * but if two URG's are pending at once, some out-of-band 9755547Swnj * data may creep in... ick. 9765547Swnj */ 97727190Skarels if (ti->ti_urp <= ti->ti_len && 97827190Skarels (so->so_options & SO_OOBINLINE) == 0) 9795547Swnj tcp_pulloutofband(so, ti); 98025939Skarels } else 98125939Skarels /* 98225939Skarels * If no out of band data is expected, 98325939Skarels * pull receive urgent pointer along 98425939Skarels * with the receive window. 98525939Skarels */ 98625939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 98725939Skarels tp->rcv_up = tp->rcv_nxt; 98825939Skarels dodata: /* XXX */ 9894601Swnj 9904601Swnj /* 9915065Swnj * Process the segment text, merging it into the TCP sequencing queue, 9925065Swnj * and arranging for acknowledgment of receipt if necessary. 9935065Swnj * This process logically involves adjusting tp->rcv_wnd as data 9945065Swnj * is presented to the user (this happens in tcp_usrreq.c, 9955065Swnj * case PRU_RCVD). If a FIN has already been received on this 9965065Swnj * connection then we just ignore the text. 9974601Swnj */ 99817946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 99917946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 100024816Skarels TCP_REASS(tp, ti, m, so, tiflags); 10015440Swnj if (tcpnodelack == 0) 10025440Swnj tp->t_flags |= TF_DELACK; 10035440Swnj else 10045440Swnj tp->t_flags |= TF_ACKNOW; 100525260Skarels /* 100625260Skarels * Note the amount of data that peer has sent into 100725260Skarels * our window, in order to estimate the sender's 100825260Skarels * buffer size. 100925260Skarels */ 101032098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 101125260Skarels if (len > tp->max_rcvd) 101225260Skarels tp->max_rcvd = len; 10135244Sroot } else { 10144924Swnj m_freem(m); 10155263Swnj tiflags &= ~TH_FIN; 10165244Sroot } 10174601Swnj 10184601Swnj /* 10195263Swnj * If FIN is received ACK the FIN and let the user know 10205263Swnj * that the connection is closing. 10214601Swnj */ 10225263Swnj if (tiflags & TH_FIN) { 10235244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10245244Sroot socantrcvmore(so); 10255244Sroot tp->t_flags |= TF_ACKNOW; 10265244Sroot tp->rcv_nxt++; 10275244Sroot } 10285065Swnj switch (tp->t_state) { 10294601Swnj 10305065Swnj /* 10315065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10325065Swnj * enter the CLOSE_WAIT state. 10334884Swnj */ 10345065Swnj case TCPS_SYN_RECEIVED: 10355065Swnj case TCPS_ESTABLISHED: 10365065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10375065Swnj break; 10384884Swnj 10395065Swnj /* 10405085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10415085Swnj * enter the CLOSING state. 10424884Swnj */ 10435065Swnj case TCPS_FIN_WAIT_1: 10445085Swnj tp->t_state = TCPS_CLOSING; 10455065Swnj break; 10464601Swnj 10475065Swnj /* 10485065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 10495065Swnj * starting the time-wait timer, turning off the other 10505065Swnj * standard timers. 10515065Swnj */ 10525065Swnj case TCPS_FIN_WAIT_2: 10535244Sroot tp->t_state = TCPS_TIME_WAIT; 10545074Swnj tcp_canceltimers(tp); 10555162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10565244Sroot soisdisconnected(so); 10575065Swnj break; 10585065Swnj 10594884Swnj /* 10605065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10614884Swnj */ 10625065Swnj case TCPS_TIME_WAIT: 10635162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10645065Swnj break; 10655085Swnj } 10664601Swnj } 10675267Sroot if (so->so_options & SO_DEBUG) 10685267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10695085Swnj 10705085Swnj /* 10715085Swnj * Return any desired output. 10725085Swnj */ 107326824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 107425939Skarels (void) tcp_output(tp); 10755065Swnj return; 10765085Swnj 10775065Swnj dropafterack: 10785085Swnj /* 10796211Swnj * Generate an ACK dropping incoming segment if it occupies 10806211Swnj * sequence space, where the ACK reflects our state. 10815085Swnj */ 108226057Skarels if (tiflags & TH_RST) 10835085Swnj goto drop; 108431749Skarels m_freem(m); 108531721Skarels tp->t_flags |= TF_ACKNOW; 108631721Skarels (void) tcp_output(tp); 10875231Swnj return; 10885085Swnj 10895085Swnj dropwithreset: 109011731Ssam if (om) { 10916161Ssam (void) m_free(om); 109211731Ssam om = 0; 109311731Ssam } 10945085Swnj /* 10955244Sroot * Generate a RST, dropping incoming segment. 10965085Swnj * Make ACK acceptable to originator of segment. 109725197Skarels * Don't bother to respond if destination was broadcast. 10985085Swnj */ 109925197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 11005085Swnj goto drop; 11015085Swnj if (tiflags & TH_ACK) 11025391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 11035085Swnj else { 11045085Swnj if (tiflags & TH_SYN) 11055085Swnj ti->ti_len++; 11066211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11076211Swnj TH_RST|TH_ACK); 11085085Swnj } 110910769Ssam /* destroy temporarily created socket */ 111010769Ssam if (dropsocket) 111110769Ssam (void) soabort(so); 11125231Swnj return; 11135085Swnj 11145065Swnj drop: 111511730Ssam if (om) 111611730Ssam (void) m_free(om); 11175085Swnj /* 11185085Swnj * Drop space held by incoming segment and return. 11195085Swnj */ 11206303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11216303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11225065Swnj m_freem(m); 112310769Ssam /* destroy temporarily created socket */ 112410769Ssam if (dropsocket) 112510769Ssam (void) soabort(so); 11265267Sroot return; 11275065Swnj } 11285065Swnj 112917272Skarels tcp_dooptions(tp, om, ti) 11305440Swnj struct tcpcb *tp; 11315440Swnj struct mbuf *om; 113217272Skarels struct tcpiphdr *ti; 11335419Swnj { 11345440Swnj register u_char *cp; 11355440Swnj int opt, optlen, cnt; 11365419Swnj 11375440Swnj cp = mtod(om, u_char *); 11385440Swnj cnt = om->m_len; 11395440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11405440Swnj opt = cp[0]; 11415440Swnj if (opt == TCPOPT_EOL) 11425440Swnj break; 11435440Swnj if (opt == TCPOPT_NOP) 11445440Swnj optlen = 1; 114512169Ssam else { 11465440Swnj optlen = cp[1]; 114712169Ssam if (optlen <= 0) 114812169Ssam break; 114912169Ssam } 11505440Swnj switch (opt) { 11515440Swnj 11525440Swnj default: 11535440Swnj break; 11545440Swnj 11555440Swnj case TCPOPT_MAXSEG: 11565440Swnj if (optlen != 4) 11575440Swnj continue; 115817272Skarels if (!(ti->ti_flags & TH_SYN)) 115917272Skarels continue; 11605440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11616161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 116217272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 11635440Swnj break; 11645419Swnj } 11655419Swnj } 11666161Ssam (void) m_free(om); 11675419Swnj } 11685419Swnj 11695419Swnj /* 11705547Swnj * Pull out of band byte out of a segment so 11715547Swnj * it doesn't appear in the user's data queue. 11725547Swnj * It is still reflected in the segment length for 11735547Swnj * sequencing purposes. 11745547Swnj */ 11755547Swnj tcp_pulloutofband(so, ti) 11765547Swnj struct socket *so; 11775547Swnj struct tcpiphdr *ti; 11785547Swnj { 11795547Swnj register struct mbuf *m; 11806116Swnj int cnt = ti->ti_urp - 1; 11815547Swnj 11825547Swnj m = dtom(ti); 11835547Swnj while (cnt >= 0) { 11845547Swnj if (m->m_len > cnt) { 11855547Swnj char *cp = mtod(m, caddr_t) + cnt; 11865547Swnj struct tcpcb *tp = sototcpcb(so); 11875547Swnj 11885547Swnj tp->t_iobc = *cp; 11895547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 11906161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 11915547Swnj m->m_len--; 11925547Swnj return; 11935547Swnj } 11945547Swnj cnt -= m->m_len; 11955547Swnj m = m->m_next; 11965547Swnj if (m == 0) 11975547Swnj break; 11985547Swnj } 11995547Swnj panic("tcp_pulloutofband"); 12005547Swnj } 12015547Swnj 12025547Swnj /* 120317272Skarels * Determine a reasonable value for maxseg size. 120417272Skarels * If the route is known, use one that can be handled 120517272Skarels * on the given interface without forcing IP to fragment. 120631726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 120731726Skarels * to utilize large mbufs. 120817272Skarels * If interface pointer is unavailable, or the destination isn't local, 120923975Skarels * use a conservative size (512 or the default IP max size, but no more 121023975Skarels * than the mtu of the interface through which we route), 121117272Skarels * as we can't discover anything about intervening gateways or networks. 121232034Skarels * We also initialize the congestion/slow start window to be a single 121332034Skarels * segment if the destination isn't local; this information should 121432034Skarels * probably all be saved with the routing entry at the transport level. 121517272Skarels * 121617272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 121717272Skarels */ 121817272Skarels tcp_mss(tp) 121923975Skarels register struct tcpcb *tp; 122017272Skarels { 122117272Skarels struct route *ro; 122217272Skarels struct ifnet *ifp; 122317272Skarels int mss; 122417272Skarels struct inpcb *inp; 122517272Skarels 122617272Skarels inp = tp->t_inpcb; 122717272Skarels ro = &inp->inp_route; 122817272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 122917272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 123017272Skarels /* No route yet, so try to acquire one */ 123117272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 123217272Skarels ro->ro_dst.sa_family = AF_INET; 123317272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 123417272Skarels inp->inp_faddr; 123517272Skarels rtalloc(ro); 123617272Skarels } 123717272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 123817316Skarels return (TCP_MSS); 123917272Skarels } 124017272Skarels 124117272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 124231726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 124331726Skarels if (mss > MCLBYTES) 124431726Skarels mss &= ~(MCLBYTES-1); 124517272Skarels #else 124631726Skarels if (mss > MCLBYTES) 124731726Skarels mss = mss / MCLBYTES * MCLBYTES; 124817272Skarels #endif 124923975Skarels if (in_localaddr(inp->inp_faddr)) 125023975Skarels return (mss); 125132098Skarels 125232034Skarels mss = MIN(mss, TCP_MSS); 125332034Skarels tp->snd_cwnd = mss; 125432034Skarels return (mss); 125517272Skarels } 1256