123190Smckusick /* 233745Skarels * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 332787Sbostic * All rights reserved. 423190Smckusick * 532787Sbostic * Redistribution and use in source and binary forms are permitted 634854Sbostic * provided that the above copyright notice and this paragraph are 734854Sbostic * duplicated in all such forms and that any documentation, 834854Sbostic * advertising materials, and other materials related to such 934854Sbostic * distribution and use acknowledge that the software was developed 1034854Sbostic * by the University of California, Berkeley. The name of the 1134854Sbostic * University may not be used to endorse or promote products derived 1234854Sbostic * from this software without specific prior written permission. 1334854Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 1434854Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 1534854Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1632787Sbostic * 17*37320Skarels * @(#)tcp_input.c 7.21 (Berkeley) 04/08/89 1823190Smckusick */ 194601Swnj 2017062Sbloom #include "param.h" 2117062Sbloom #include "systm.h" 22*37320Skarels #include "malloc.h" 2317062Sbloom #include "mbuf.h" 2417062Sbloom #include "protosw.h" 2517062Sbloom #include "socket.h" 2617062Sbloom #include "socketvar.h" 2717062Sbloom #include "errno.h" 2810894Ssam 2910894Ssam #include "../net/if.h" 3010894Ssam #include "../net/route.h" 3110894Ssam 3217062Sbloom #include "in.h" 3317062Sbloom #include "in_pcb.h" 3417062Sbloom #include "in_systm.h" 3517062Sbloom #include "ip.h" 3617062Sbloom #include "ip_var.h" 3717062Sbloom #include "tcp.h" 3817062Sbloom #include "tcp_fsm.h" 3917062Sbloom #include "tcp_seq.h" 4017062Sbloom #include "tcp_timer.h" 4117062Sbloom #include "tcp_var.h" 4217062Sbloom #include "tcpip.h" 4317062Sbloom #include "tcp_debug.h" 444601Swnj 455300Sroot int tcpprintfs = 0; 4632098Skarels int tcprexmtthresh = 3; 475267Sroot struct tcpiphdr tcp_saveti; 484601Swnj 495267Sroot struct tcpcb *tcp_newtcpcb(); 5024816Skarels 515065Swnj /* 5224816Skarels * Insert segment ti into reassembly queue of tcp with 5324816Skarels * control block tp. Return TH_FIN if reassembly now includes 5424816Skarels * a segment with FIN. The macro form does the common case inline 5524816Skarels * (segment is the next to be received on an established connection, 5624816Skarels * and the queue is empty), avoiding linkage into and removal 5724816Skarels * from the queue and repetition of various conversions. 5834278Skarels * Set DELACK for segments received in order, but ack immediately 5934278Skarels * when segments are out of order (so fast retransmit can work). 6024816Skarels */ 6124816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 6224816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 6324816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 6424816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 6534278Skarels tp->t_flags |= TF_DELACK; \ 6624816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 6724816Skarels flags = (ti)->ti_flags & TH_FIN; \ 6830525Skarels tcpstat.tcps_rcvpack++;\ 6930525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 7024816Skarels sbappend(&(so)->so_rcv, (m)); \ 7124816Skarels sorwakeup(so); \ 7234278Skarels } else { \ 7324816Skarels (flags) = tcp_reass((tp), (ti)); \ 7434278Skarels tp->t_flags |= TF_ACKNOW; \ 7534278Skarels } \ 7624816Skarels } 7724816Skarels 7824816Skarels tcp_reass(tp, ti) 7924816Skarels register struct tcpcb *tp; 8024816Skarels register struct tcpiphdr *ti; 8124816Skarels { 8224816Skarels register struct tcpiphdr *q; 8324816Skarels struct socket *so = tp->t_inpcb->inp_socket; 8424816Skarels struct mbuf *m; 8524816Skarels int flags; 8624816Skarels 8724816Skarels /* 8824816Skarels * Call with ti==0 after become established to 8924816Skarels * force pre-ESTABLISHED data up to user socket. 9024816Skarels */ 9124816Skarels if (ti == 0) 9224816Skarels goto present; 9324816Skarels 9424816Skarels /* 9524816Skarels * Find a segment which begins after this one does. 9624816Skarels */ 9724816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 9824816Skarels q = (struct tcpiphdr *)q->ti_next) 9924816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 10024816Skarels break; 10124816Skarels 10224816Skarels /* 10324816Skarels * If there is a preceding segment, it may provide some of 10424816Skarels * our data already. If so, drop the data from the incoming 10524816Skarels * segment. If it provides all of our data, drop us. 10624816Skarels */ 10724816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 10824816Skarels register int i; 10924816Skarels q = (struct tcpiphdr *)q->ti_prev; 11024816Skarels /* conversion to int (in i) handles seq wraparound */ 11124816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 11224816Skarels if (i > 0) { 11330525Skarels if (i >= ti->ti_len) { 11430525Skarels tcpstat.tcps_rcvduppack++; 11530525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 11624816Skarels goto drop; 11730525Skarels } 11824816Skarels m_adj(dtom(ti), i); 11924816Skarels ti->ti_len -= i; 12024816Skarels ti->ti_seq += i; 12124816Skarels } 12224816Skarels q = (struct tcpiphdr *)(q->ti_next); 12324816Skarels } 12430525Skarels tcpstat.tcps_rcvoopack++; 12530525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 12624816Skarels 12724816Skarels /* 12824816Skarels * While we overlap succeeding segments trim them or, 12924816Skarels * if they are completely covered, dequeue them. 13024816Skarels */ 13124816Skarels while (q != (struct tcpiphdr *)tp) { 13224816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 13324816Skarels if (i <= 0) 13424816Skarels break; 13524816Skarels if (i < q->ti_len) { 13624816Skarels q->ti_seq += i; 13724816Skarels q->ti_len -= i; 13824816Skarels m_adj(dtom(q), i); 13924816Skarels break; 14024816Skarels } 14124816Skarels q = (struct tcpiphdr *)q->ti_next; 14224816Skarels m = dtom(q->ti_prev); 14324816Skarels remque(q->ti_prev); 14424816Skarels m_freem(m); 14524816Skarels } 14624816Skarels 14724816Skarels /* 14824816Skarels * Stick new segment in its place. 14924816Skarels */ 15024816Skarels insque(ti, q->ti_prev); 15124816Skarels 15224816Skarels present: 15324816Skarels /* 15424816Skarels * Present data to user, advancing rcv_nxt through 15524816Skarels * completed sequence space. 15624816Skarels */ 15724816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 15824816Skarels return (0); 15924816Skarels ti = tp->seg_next; 16024816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 16124816Skarels return (0); 16224816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 16324816Skarels return (0); 16424816Skarels do { 16524816Skarels tp->rcv_nxt += ti->ti_len; 16624816Skarels flags = ti->ti_flags & TH_FIN; 16724816Skarels remque(ti); 16824816Skarels m = dtom(ti); 16924816Skarels ti = (struct tcpiphdr *)ti->ti_next; 17024816Skarels if (so->so_state & SS_CANTRCVMORE) 17124816Skarels m_freem(m); 17224816Skarels else 17324816Skarels sbappend(&so->so_rcv, m); 17424816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 17524816Skarels sorwakeup(so); 17624816Skarels return (flags); 17724816Skarels drop: 17824816Skarels m_freem(dtom(ti)); 17924816Skarels return (0); 18024816Skarels } 18124816Skarels 18224816Skarels /* 1835065Swnj * TCP input routine, follows pages 65-76 of the 1845065Swnj * protocol specification dated September, 1981 very closely. 1855065Swnj */ 186*37320Skarels tcp_input(m, iphlen) 187*37320Skarels register struct mbuf *m; 188*37320Skarels int iphlen; 1894601Swnj { 1904924Swnj register struct tcpiphdr *ti; 1914924Swnj struct inpcb *inp; 1925440Swnj struct mbuf *om = 0; 1934924Swnj int len, tlen, off; 1945391Swnj register struct tcpcb *tp = 0; 1954924Swnj register int tiflags; 1964803Swnj struct socket *so; 19731721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1985267Sroot short ostate; 1996028Sroot struct in_addr laddr; 20010769Ssam int dropsocket = 0; 20130525Skarels int iss = 0; 2024924Swnj 20330525Skarels tcpstat.tcps_rcvtotal++; 2044924Swnj /* 2055244Sroot * Get IP and TCP header together in first mbuf. 2065244Sroot * Note: IP leaves IP header in first mbuf. 2074924Swnj */ 2085020Sroot ti = mtod(m, struct tcpiphdr *); 209*37320Skarels if (iphlen > sizeof (struct ip)) 210*37320Skarels ip_stripoptions(m, (struct mbuf *)0); 211*37320Skarels if (m->m_flags & M_EXT || m->m_len < sizeof (struct tcpiphdr)) { 2125307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 21330525Skarels tcpstat.tcps_rcvshort++; 2145307Sroot return; 2155085Swnj } 2165085Swnj ti = mtod(m, struct tcpiphdr *); 2175085Swnj } 2184601Swnj 2194601Swnj /* 2205244Sroot * Checksum extended TCP header and data. 2214601Swnj */ 2224924Swnj tlen = ((struct ip *)ti)->ip_len; 2234924Swnj len = sizeof (struct ip) + tlen; 224*37320Skarels ti->ti_next = ti->ti_prev = 0; 225*37320Skarels ti->ti_x1 = 0; 226*37320Skarels ti->ti_len = (u_short)tlen; 227*37320Skarels ti->ti_len = htons((u_short)ti->ti_len); 228*37320Skarels if (ti->ti_sum = in_cksum(m, len)) { 229*37320Skarels if (tcpprintfs) 230*37320Skarels printf("tcp sum: src %x\n", ti->ti_src); 231*37320Skarels tcpstat.tcps_rcvbadsum++; 232*37320Skarels goto drop; 2334601Swnj } 2344601Swnj 2354601Swnj /* 2365244Sroot * Check that TCP offset makes sense, 2375440Swnj * pull out TCP options and adjust length. 2384601Swnj */ 2394924Swnj off = ti->ti_off << 2; 2405231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 24111830Ssam if (tcpprintfs) 24211830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 24330525Skarels tcpstat.tcps_rcvbadoff++; 2445085Swnj goto drop; 2454924Swnj } 2466211Swnj tlen -= off; 2476211Swnj ti->ti_len = tlen; 2485440Swnj if (off > sizeof (struct tcphdr)) { 24924816Skarels if (m->m_len < sizeof(struct ip) + off) { 25024816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 25130525Skarels tcpstat.tcps_rcvshort++; 25224816Skarels return; 25324816Skarels } 25424816Skarels ti = mtod(m, struct tcpiphdr *); 2555440Swnj } 2569642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2575440Swnj if (om == 0) 2585440Swnj goto drop; 2595440Swnj om->m_len = off - sizeof (struct tcphdr); 2605440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2616161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2625440Swnj m->m_len -= om->m_len; 263*37320Skarels m->m_pkthdr.len -= om->m_len; 2646161Ssam bcopy(op+om->m_len, op, 2656161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2665440Swnj } 2675440Swnj } 2685065Swnj tiflags = ti->ti_flags; 2694924Swnj 2706093Sroot /* 27125729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2726093Sroot */ 273*37320Skarels m->m_data += sizeof(struct tcpiphdr); 27425729Smckusick m->m_len -= sizeof(struct tcpiphdr); 275*37320Skarels m->m_pkthdr.len -= sizeof(struct tcpiphdr); 2766093Sroot 2774924Swnj /* 2785244Sroot * Convert TCP protocol specific fields to host format. 2795085Swnj */ 2805085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2815085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2825085Swnj ti->ti_win = ntohs(ti->ti_win); 2835085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2845085Swnj 2855085Swnj /* 2868271Sroot * Locate pcb for segment. 2874924Swnj */ 28830525Skarels findpcb: 2895065Swnj inp = in_pcblookup 2906028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2916028Sroot INPLOOKUP_WILDCARD); 2925065Swnj 2935065Swnj /* 2945065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2955244Sroot * all data in the incoming segment is discarded. 29632098Skarels * If the TCB exists but is in CLOSED state, it is embryonic, 29732098Skarels * but should either do a listen or a connect soon. 2985065Swnj */ 2995300Sroot if (inp == 0) 3005085Swnj goto dropwithreset; 3015065Swnj tp = intotcpcb(inp); 3025300Sroot if (tp == 0) 3035085Swnj goto dropwithreset; 30432098Skarels if (tp->t_state == TCPS_CLOSED) 30532098Skarels goto drop; 3065109Swnj so = inp->inp_socket; 3075267Sroot if (so->so_options & SO_DEBUG) { 3085267Sroot ostate = tp->t_state; 3095267Sroot tcp_saveti = *ti; 3105267Sroot } 3117510Sroot if (so->so_options & SO_ACCEPTCONN) { 3127510Sroot so = sonewconn(so); 3137510Sroot if (so == 0) 3147510Sroot goto drop; 31510769Ssam /* 31610769Ssam * This is ugly, but .... 31710769Ssam * 31810769Ssam * Mark socket as temporary until we're 31910769Ssam * committed to keeping it. The code at 32010769Ssam * ``drop'' and ``dropwithreset'' check the 32110769Ssam * flag dropsocket to see if the temporary 32210769Ssam * socket created here should be discarded. 32310769Ssam * We mark the socket as discardable until 32410769Ssam * we're committed to it below in TCPS_LISTEN. 32510769Ssam */ 32610769Ssam dropsocket++; 3277510Sroot inp = (struct inpcb *)so->so_pcb; 3287510Sroot inp->inp_laddr = ti->ti_dst; 3297510Sroot inp->inp_lport = ti->ti_dport; 33024816Skarels inp->inp_options = ip_srcroute(); 3317510Sroot tp = intotcpcb(inp); 3327510Sroot tp->t_state = TCPS_LISTEN; 3337510Sroot } 3344601Swnj 3354601Swnj /* 3365162Swnj * Segment received on connection. 3375162Swnj * Reset idle time and keep-alive timer. 3385162Swnj */ 3395162Swnj tp->t_idle = 0; 34033745Skarels tp->t_timer[TCPT_KEEP] = tcp_keepidle; 3415162Swnj 3425162Swnj /* 34317272Skarels * Process options if not in LISTEN state, 34417272Skarels * else do it below (after getting remote address). 3455440Swnj */ 34617272Skarels if (om && tp->t_state != TCPS_LISTEN) { 34717272Skarels tcp_dooptions(tp, om, ti); 3485440Swnj om = 0; 3495440Swnj } 3505440Swnj 3515440Swnj /* 3525085Swnj * Calculate amount of space in receive window, 3535085Swnj * and then do TCP input processing. 35424816Skarels * Receive window is amount of space in rcv queue, 35524816Skarels * but not less than advertised window. 3564601Swnj */ 35725939Skarels { int win; 3584601Swnj 35925939Skarels win = sbspace(&so->so_rcv); 36025939Skarels if (win < 0) 36125939Skarels win = 0; 362*37320Skarels tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 36325939Skarels } 36425939Skarels 3654601Swnj switch (tp->t_state) { 3664601Swnj 3675065Swnj /* 3685065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3695065Swnj * If the segment contains an ACK then it is bad and send a RST. 3705065Swnj * If it does not contain a SYN then it is not interesting; drop it. 37125197Skarels * Don't bother responding if the destination was a broadcast. 3725085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3735065Swnj * tp->iss, and send a segment: 3745085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3755065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3765065Swnj * Fill in remote peer address fields if not previously specified. 3775065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3785244Sroot * segment in this state. 3795065Swnj */ 3808271Sroot case TCPS_LISTEN: { 38110145Ssam struct mbuf *am; 3828271Sroot register struct sockaddr_in *sin; 3838271Sroot 3845065Swnj if (tiflags & TH_RST) 3855065Swnj goto drop; 3865300Sroot if (tiflags & TH_ACK) 3875085Swnj goto dropwithreset; 3885300Sroot if ((tiflags & TH_SYN) == 0) 3895065Swnj goto drop; 390*37320Skarels if (m->m_flags & M_BCAST) 39125197Skarels goto drop; 39210145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 39310145Ssam if (am == NULL) 39410145Ssam goto drop; 39510145Ssam am->m_len = sizeof (struct sockaddr_in); 3968599Sroot sin = mtod(am, struct sockaddr_in *); 3978271Sroot sin->sin_family = AF_INET; 398*37320Skarels sin->sin_len = sizeof(*sin); 3998271Sroot sin->sin_addr = ti->ti_src; 4008271Sroot sin->sin_port = ti->ti_sport; 4016028Sroot laddr = inp->inp_laddr; 40210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 4036028Sroot inp->inp_laddr = ti->ti_dst; 4048599Sroot if (in_pcbconnect(inp, am)) { 4056028Sroot inp->inp_laddr = laddr; 4068716Sroot (void) m_free(am); 4075244Sroot goto drop; 4086028Sroot } 4098716Sroot (void) m_free(am); 4105244Sroot tp->t_template = tcp_template(tp); 4115244Sroot if (tp->t_template == 0) { 41226386Skarels tp = tcp_drop(tp, ENOBUFS); 41317264Skarels dropsocket = 0; /* socket is already gone */ 4145244Sroot goto drop; 4155244Sroot } 41617272Skarels if (om) { 41717272Skarels tcp_dooptions(tp, om, ti); 41817272Skarels om = 0; 41917272Skarels } 42030525Skarels if (iss) 42130525Skarels tp->iss = iss; 42230525Skarels else 42330525Skarels tp->iss = tcp_iss; 42430525Skarels tcp_iss += TCP_ISSINCR/2; 4255065Swnj tp->irs = ti->ti_seq; 4265085Swnj tcp_sendseqinit(tp); 4275085Swnj tcp_rcvseqinit(tp); 42825939Skarels tp->t_flags |= TF_ACKNOW; 4295065Swnj tp->t_state = TCPS_SYN_RECEIVED; 43033745Skarels tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT; 43110769Ssam dropsocket = 0; /* committed to socket */ 43230525Skarels tcpstat.tcps_accepts++; 4335085Swnj goto trimthenstep6; 4348271Sroot } 4354601Swnj 4365065Swnj /* 4375065Swnj * If the state is SYN_SENT: 4385065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4395065Swnj * if seg contains a RST, then drop the connection. 4405065Swnj * if seg does not contain SYN, then drop it. 4415065Swnj * Otherwise this is an acceptable SYN segment 4425065Swnj * initialize tp->rcv_nxt and tp->irs 4435065Swnj * if seg contains ack then advance tp->snd_una 4445065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4455065Swnj * arrange for segment to be acked (eventually) 4465065Swnj * continue processing rest of data/controls, beginning with URG 4475065Swnj */ 4485065Swnj case TCPS_SYN_SENT: 4495065Swnj if ((tiflags & TH_ACK) && 45024816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4515231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4525085Swnj goto dropwithreset; 4535065Swnj if (tiflags & TH_RST) { 45410394Ssam if (tiflags & TH_ACK) 45510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4565065Swnj goto drop; 4574601Swnj } 4585065Swnj if ((tiflags & TH_SYN) == 0) 4595065Swnj goto drop; 46030974Skarels if (tiflags & TH_ACK) { 46130974Skarels tp->snd_una = ti->ti_ack; 46230974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 46330974Skarels tp->snd_nxt = tp->snd_una; 46430974Skarels } 4655244Sroot tp->t_timer[TCPT_REXMT] = 0; 4665065Swnj tp->irs = ti->ti_seq; 4675085Swnj tcp_rcvseqinit(tp); 4685085Swnj tp->t_flags |= TF_ACKNOW; 46930974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 47030525Skarels tcpstat.tcps_connects++; 4715244Sroot soisconnected(so); 4725065Swnj tp->t_state = TCPS_ESTABLISHED; 473*37320Skarels tp->t_maxseg = min(tp->t_maxseg, tcp_mss(tp)); 4745162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 47532098Skarels /* 47632098Skarels * if we didn't have to retransmit the SYN, 47732098Skarels * use its rtt as our initial srtt & rtt var. 47832098Skarels */ 47932098Skarels if (tp->t_rtt) { 48032098Skarels tp->t_srtt = tp->t_rtt << 3; 48132098Skarels tp->t_rttvar = tp->t_rtt << 1; 48232098Skarels TCPT_RANGESET(tp->t_rxtcur, 48332098Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 48432098Skarels TCPTV_MIN, TCPTV_REXMTMAX); 48532098Skarels tp->t_rtt = 0; 48632098Skarels } 4875162Swnj } else 4885085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4895085Swnj 4905085Swnj trimthenstep6: 4915085Swnj /* 4925231Swnj * Advance ti->ti_seq to correspond to first data byte. 4935085Swnj * If data, trim to stay within window, 4945085Swnj * dropping FIN if necessary. 4955085Swnj */ 4965231Swnj ti->ti_seq++; 4975085Swnj if (ti->ti_len > tp->rcv_wnd) { 4985085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4995085Swnj m_adj(m, -todrop); 5005085Swnj ti->ti_len = tp->rcv_wnd; 50125939Skarels tiflags &= ~TH_FIN; 50230525Skarels tcpstat.tcps_rcvpackafterwin++; 50330525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 5045065Swnj } 5055263Swnj tp->snd_wl1 = ti->ti_seq - 1; 50625939Skarels tp->rcv_up = ti->ti_seq; 5075085Swnj goto step6; 5085065Swnj } 5094601Swnj 5105065Swnj /* 51130525Skarels * States other than LISTEN or SYN_SENT. 51230525Skarels * First check that at least some bytes of segment are within 51330525Skarels * receive window. If segment begins before rcv_nxt, 51430525Skarels * drop leading data (and SYN); if nothing left, just ack. 51516222Skarels */ 51630525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 51730525Skarels if (todrop > 0) { 51830525Skarels if (tiflags & TH_SYN) { 51930525Skarels tiflags &= ~TH_SYN; 52030525Skarels ti->ti_seq++; 52130525Skarels if (ti->ti_urp > 1) 52230525Skarels ti->ti_urp--; 52330525Skarels else 52430525Skarels tiflags &= ~TH_URG; 52530525Skarels todrop--; 52630525Skarels } 52730525Skarels if (todrop > ti->ti_len || 52830525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 52933745Skarels tcpstat.tcps_rcvduppack++; 53033745Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 53131730Skarels /* 53233745Skarels * If segment is just one to the left of the window, 53333745Skarels * check two special cases: 53433745Skarels * 1. Don't toss RST in response to 4.2-style keepalive. 53533745Skarels * 2. If the only thing to drop is a FIN, we can drop 53633745Skarels * it, but check the ACK or we will get into FIN 53733745Skarels * wars if our FINs crossed (both CLOSING). 53833745Skarels * In either case, send ACK to resynchronize, 53933745Skarels * but keep on processing for RST or ACK. 54031730Skarels */ 54133745Skarels if ((tiflags & TH_FIN && todrop == ti->ti_len + 1) 54233745Skarels #ifdef TCP_COMPAT_42 54333745Skarels || (tiflags & TH_RST && ti->ti_seq == tp->rcv_nxt - 1) 54431730Skarels #endif 54533745Skarels ) { 54633745Skarels todrop = ti->ti_len; 54733745Skarels tiflags &= ~TH_FIN; 54833745Skarels tp->t_flags |= TF_ACKNOW; 54933745Skarels } else 55033745Skarels goto dropafterack; 55132034Skarels } else { 55232034Skarels tcpstat.tcps_rcvpartduppack++; 55332034Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 55430525Skarels } 55530525Skarels m_adj(m, todrop); 55630525Skarels ti->ti_seq += todrop; 55730525Skarels ti->ti_len -= todrop; 55830525Skarels if (ti->ti_urp > todrop) 55930525Skarels ti->ti_urp -= todrop; 56030525Skarels else { 56130525Skarels tiflags &= ~TH_URG; 56230525Skarels ti->ti_urp = 0; 56330525Skarels } 56416222Skarels } 56516222Skarels 56632612Skarels /* 56733745Skarels * If new data are received on a connection after the 56832612Skarels * user processes are gone, then RST the other end. 56932612Skarels */ 57032612Skarels if ((so->so_state & SS_NOFDREF) && 57132612Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 57232612Skarels tp = tcp_close(tp); 57332612Skarels tcpstat.tcps_rcvafterclose++; 57432612Skarels goto dropwithreset; 57532612Skarels } 57632612Skarels 57733445Skarels /* 57833445Skarels * If segment ends after window, drop trailing data 57933445Skarels * (and PUSH and FIN); if nothing left, just ACK. 58033445Skarels */ 58133445Skarels todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 58233445Skarels if (todrop > 0) { 58333445Skarels tcpstat.tcps_rcvpackafterwin++; 58433445Skarels if (todrop >= ti->ti_len) { 58530525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 58633445Skarels /* 58733445Skarels * If a new connection request is received 58833445Skarels * while in TIME_WAIT, drop the old connection 58933445Skarels * and start over if the sequence numbers 59033445Skarels * are above the previous ones. 59133445Skarels */ 59233445Skarels if (tiflags & TH_SYN && 59333445Skarels tp->t_state == TCPS_TIME_WAIT && 59433445Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 59533445Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 59633445Skarels (void) tcp_close(tp); 59733445Skarels goto findpcb; 59833445Skarels } 59933445Skarels /* 60033445Skarels * If window is closed can only take segments at 60133445Skarels * window edge, and have to drop data and PUSH from 60233445Skarels * incoming segments. Continue processing, but 60333445Skarels * remember to ack. Otherwise, drop segment 60433445Skarels * and ack. 60533445Skarels */ 60633445Skarels if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) { 60733445Skarels tp->t_flags |= TF_ACKNOW; 60830525Skarels tcpstat.tcps_rcvwinprobe++; 60933445Skarels } else 6105065Swnj goto dropafterack; 61133445Skarels } else 61230525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 61333445Skarels m_adj(m, -todrop); 61433445Skarels ti->ti_len -= todrop; 61533445Skarels tiflags &= ~(TH_PUSH|TH_FIN); 6165065Swnj } 6174601Swnj 6185065Swnj /* 6195065Swnj * If the RST bit is set examine the state: 6205065Swnj * SYN_RECEIVED STATE: 6215065Swnj * If passive open, return to LISTEN state. 6225065Swnj * If active open, inform user that connection was refused. 6235065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 6245065Swnj * Inform user that connection was reset, and close tcb. 6255065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6265065Swnj * Close the tcb. 6275065Swnj */ 6285065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6295267Sroot 6305065Swnj case TCPS_SYN_RECEIVED: 63133745Skarels so->so_error = ECONNREFUSED; 63233745Skarels goto close; 6334601Swnj 6345065Swnj case TCPS_ESTABLISHED: 6355065Swnj case TCPS_FIN_WAIT_1: 6365065Swnj case TCPS_FIN_WAIT_2: 6375065Swnj case TCPS_CLOSE_WAIT: 63833745Skarels so->so_error = ECONNRESET; 63933745Skarels close: 64033745Skarels tp->t_state = TCPS_CLOSED; 64133745Skarels tcpstat.tcps_drops++; 64233745Skarels tp = tcp_close(tp); 6435065Swnj goto drop; 6445065Swnj 6455065Swnj case TCPS_CLOSING: 6465065Swnj case TCPS_LAST_ACK: 6475065Swnj case TCPS_TIME_WAIT: 64810394Ssam tp = tcp_close(tp); 6495065Swnj goto drop; 6504601Swnj } 6514601Swnj 6524601Swnj /* 6535065Swnj * If a SYN is in the window, then this is an 6545065Swnj * error and we send an RST and drop the connection. 6554601Swnj */ 6565065Swnj if (tiflags & TH_SYN) { 65710394Ssam tp = tcp_drop(tp, ECONNRESET); 6585085Swnj goto dropwithreset; 6594601Swnj } 6604601Swnj 6614601Swnj /* 6625065Swnj * If the ACK bit is off we drop the segment and return. 6634601Swnj */ 6645085Swnj if ((tiflags & TH_ACK) == 0) 6655065Swnj goto drop; 6665065Swnj 6675065Swnj /* 6685065Swnj * Ack processing. 6695065Swnj */ 6704601Swnj switch (tp->t_state) { 6714601Swnj 6725065Swnj /* 6735065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 67431721Skarels * ESTABLISHED state and continue processing, otherwise 6755065Swnj * send an RST. 6765065Swnj */ 6775065Swnj case TCPS_SYN_RECEIVED: 6785085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6795231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6805085Swnj goto dropwithreset; 68130525Skarels tcpstat.tcps_connects++; 6825085Swnj soisconnected(so); 6835085Swnj tp->t_state = TCPS_ESTABLISHED; 684*37320Skarels tp->t_maxseg = min(tp->t_maxseg, tcp_mss(tp)); 6855162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6865244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6875085Swnj /* fall into ... */ 6884601Swnj 6895065Swnj /* 6905065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6915065Swnj * ACKs. If the ack is in the range 6925231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6935065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6945065Swnj * data from the retransmission queue. If this ACK reflects 6955065Swnj * more up to date window information we update our window information. 6965065Swnj */ 6975065Swnj case TCPS_ESTABLISHED: 6985065Swnj case TCPS_FIN_WAIT_1: 6995065Swnj case TCPS_FIN_WAIT_2: 7005065Swnj case TCPS_CLOSE_WAIT: 7015065Swnj case TCPS_CLOSING: 7025244Sroot case TCPS_LAST_ACK: 7035244Sroot case TCPS_TIME_WAIT: 7045085Swnj 70530525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 70632098Skarels if (ti->ti_len == 0 && ti->ti_win == tp->snd_wnd) { 70730525Skarels tcpstat.tcps_rcvdupack++; 70832098Skarels /* 70932098Skarels * If we have outstanding data (not a 71032098Skarels * window probe), this is a completely 71132098Skarels * duplicate ack (ie, window info didn't 71232098Skarels * change), the ack is the biggest we've 71332098Skarels * seen and we've seen exactly our rexmt 71432098Skarels * threshhold of them, assume a packet 71532098Skarels * has been dropped and retransmit it. 71632098Skarels * Kludge snd_nxt & the congestion 71732098Skarels * window so we send only this one 71832376Skarels * packet. If this packet fills the 71932376Skarels * only hole in the receiver's seq. 72032376Skarels * space, the next real ack will fully 72132376Skarels * open our window. This means we 72232376Skarels * have to do the usual slow-start to 72332376Skarels * not overwhelm an intermediate gateway 72432376Skarels * with a burst of packets. Leave 72532376Skarels * here with the congestion window set 72632376Skarels * to allow 2 packets on the next real 72732376Skarels * ack and the exp-to-linear thresh 72832376Skarels * set for half the current window 72932376Skarels * size (since we know we're losing at 73032376Skarels * the current window size). 73132098Skarels */ 73232098Skarels if (tp->t_timer[TCPT_REXMT] == 0 || 73332098Skarels ti->ti_ack != tp->snd_una) 73432098Skarels tp->t_dupacks = 0; 73532098Skarels else if (++tp->t_dupacks == tcprexmtthresh) { 73632098Skarels tcp_seq onxt = tp->snd_nxt; 73732376Skarels u_int win = 738*37320Skarels min(tp->snd_wnd, tp->snd_cwnd) / 2 / 73932376Skarels tp->t_maxseg; 74032098Skarels 74132376Skarels if (win < 2) 74232376Skarels win = 2; 74332376Skarels tp->snd_ssthresh = win * tp->t_maxseg; 74432376Skarels 74532098Skarels tp->t_timer[TCPT_REXMT] = 0; 74632098Skarels tp->t_rtt = 0; 74732098Skarels tp->snd_nxt = ti->ti_ack; 74832098Skarels tp->snd_cwnd = tp->t_maxseg; 74932098Skarels (void) tcp_output(tp); 75032098Skarels 75132098Skarels if (SEQ_GT(onxt, tp->snd_nxt)) 75232098Skarels tp->snd_nxt = onxt; 75332098Skarels goto drop; 75432098Skarels } 75532098Skarels } else 75632098Skarels tp->t_dupacks = 0; 7575065Swnj break; 75830525Skarels } 75932098Skarels tp->t_dupacks = 0; 76030525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 76130525Skarels tcpstat.tcps_rcvacktoomuch++; 7625065Swnj goto dropafterack; 76330525Skarels } 7645085Swnj acked = ti->ti_ack - tp->snd_una; 76530525Skarels tcpstat.tcps_rcvackpack++; 76630525Skarels tcpstat.tcps_rcvackbyte += acked; 7675951Swnj 7685951Swnj /* 7695951Swnj * If transmit timer is running and timed sequence 7705951Swnj * number was acked, update smoothed round trip time. 77132034Skarels * Since we now have an rtt measurement, cancel the 77232034Skarels * timer backoff (cf., Phil Karn's retransmit alg.). 77332034Skarels * Recompute the initial retransmit timer. 7745951Swnj */ 7755951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 77630525Skarels tcpstat.tcps_rttupdated++; 77731726Skarels if (tp->t_srtt != 0) { 77831726Skarels register short delta; 77931726Skarels 78031726Skarels /* 78131726Skarels * srtt is stored as fixed point with 3 bits 78231726Skarels * after the binary point (i.e., scaled by 8). 78331726Skarels * The following magic is equivalent 78431726Skarels * to the smoothing algorithm in rfc793 78531726Skarels * with an alpha of .875 78631726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 78732098Skarels * Adjust t_rtt to origin 0. 78831726Skarels */ 78933745Skarels delta = tp->t_rtt - 1 - (tp->t_srtt >> 3); 79031726Skarels if ((tp->t_srtt += delta) <= 0) 79131726Skarels tp->t_srtt = 1; 79231726Skarels /* 79332034Skarels * We accumulate a smoothed rtt variance 79432034Skarels * (actually, a smoothed mean difference), 79531726Skarels * then set the retransmit timer to smoothed 79631726Skarels * rtt + 2 times the smoothed variance. 79732098Skarels * rttvar is stored as fixed point 79831726Skarels * with 2 bits after the binary point 79931726Skarels * (scaled by 4). The following is equivalent 80031726Skarels * to rfc793 smoothing with an alpha of .75 80131726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 80231726Skarels * This replaces rfc793's wired-in beta. 80331726Skarels */ 80431726Skarels if (delta < 0) 80531726Skarels delta = -delta; 80631726Skarels delta -= (tp->t_rttvar >> 2); 80731726Skarels if ((tp->t_rttvar += delta) <= 0) 80831726Skarels tp->t_rttvar = 1; 80931726Skarels } else { 81031726Skarels /* 81131726Skarels * No rtt measurement yet - use the 81231726Skarels * unsmoothed rtt. Set the variance 81331726Skarels * to half the rtt (so our first 81431726Skarels * retransmit happens at 2*rtt) 81531726Skarels */ 81631726Skarels tp->t_srtt = tp->t_rtt << 3; 81731726Skarels tp->t_rttvar = tp->t_rtt << 1; 81831726Skarels } 8195951Swnj tp->t_rtt = 0; 82032034Skarels tp->t_rxtshift = 0; 82132034Skarels TCPT_RANGESET(tp->t_rxtcur, 82232034Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 82332034Skarels TCPTV_MIN, TCPTV_REXMTMAX); 8245951Swnj } 8255951Swnj 82626824Skarels /* 82726824Skarels * If all outstanding data is acked, stop retransmit 82826824Skarels * timer and remember to restart (more output or persist). 82926824Skarels * If there is more data to be acked, restart retransmit 83032034Skarels * timer, using current (possibly backed-off) value. 83126824Skarels */ 83226824Skarels if (ti->ti_ack == tp->snd_max) { 8335244Sroot tp->t_timer[TCPT_REXMT] = 0; 83426824Skarels needoutput = 1; 83532034Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) 83632034Skarels tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; 83717360Skarels /* 83832098Skarels * When new data is acked, open the congestion window. 83932098Skarels * If the window gives us less than ssthresh packets 84032098Skarels * in flight, open exponentially (maxseg per packet). 84132098Skarels * Otherwise open linearly (maxseg per window, 84232098Skarels * or maxseg^2 / cwnd per packet). 84317360Skarels */ 84432098Skarels { 84532098Skarels u_int incr = tp->t_maxseg; 84632098Skarels 84732098Skarels if (tp->snd_cwnd > tp->snd_ssthresh) 848*37320Skarels incr = max(incr * incr / tp->snd_cwnd, 1); 84932098Skarels 850*37320Skarels tp->snd_cwnd = min(tp->snd_cwnd + incr, USHRT_MAX); /* XXX */ 85132098Skarels } 8525307Sroot if (acked > so->so_snd.sb_cc) { 85315386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 85426386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 85531721Skarels ourfinisacked = 1; 8565307Sroot } else { 8576161Ssam sbdrop(&so->so_snd, acked); 8585307Sroot tp->snd_wnd -= acked; 85931721Skarels ourfinisacked = 0; 8605307Sroot } 861*37320Skarels sowwakeup(so); 8625231Swnj tp->snd_una = ti->ti_ack; 8635357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 8645357Sroot tp->snd_nxt = tp->snd_una; 8655162Swnj 8664601Swnj switch (tp->t_state) { 8674601Swnj 8685065Swnj /* 8695065Swnj * In FIN_WAIT_1 STATE in addition to the processing 8705065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 8715085Swnj * then enter FIN_WAIT_2. 8725065Swnj */ 8735065Swnj case TCPS_FIN_WAIT_1: 8745896Swnj if (ourfinisacked) { 8755896Swnj /* 8765896Swnj * If we can't receive any more 8775896Swnj * data, then closing user can proceed. 87824816Skarels * Starting the timer is contrary to the 87924816Skarels * specification, but if we don't get a FIN 88024816Skarels * we'll hang forever. 8815896Swnj */ 88224816Skarels if (so->so_state & SS_CANTRCVMORE) { 8835896Swnj soisdisconnected(so); 88433745Skarels tp->t_timer[TCPT_2MSL] = tcp_maxidle; 88524816Skarels } 8865085Swnj tp->t_state = TCPS_FIN_WAIT_2; 8875896Swnj } 8884601Swnj break; 8894601Swnj 8905065Swnj /* 8915065Swnj * In CLOSING STATE in addition to the processing for 8925065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 8935065Swnj * then enter the TIME-WAIT state, otherwise ignore 8945065Swnj * the segment. 8955065Swnj */ 8965065Swnj case TCPS_CLOSING: 8975244Sroot if (ourfinisacked) { 8985065Swnj tp->t_state = TCPS_TIME_WAIT; 8995244Sroot tcp_canceltimers(tp); 9005244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9015244Sroot soisdisconnected(so); 9025244Sroot } 9035244Sroot break; 9044601Swnj 9055065Swnj /* 90631743Skarels * In LAST_ACK, we may still be waiting for data to drain 90731743Skarels * and/or to be acked, as well as for the ack of our FIN. 90831743Skarels * If our FIN is now acknowledged, delete the TCB, 90931743Skarels * enter the closed state and return. 9105065Swnj */ 9115065Swnj case TCPS_LAST_ACK: 91231743Skarels if (ourfinisacked) { 91310394Ssam tp = tcp_close(tp); 91431743Skarels goto drop; 91531743Skarels } 91631743Skarels break; 9174601Swnj 9185065Swnj /* 9195065Swnj * In TIME_WAIT state the only thing that should arrive 9205065Swnj * is a retransmission of the remote FIN. Acknowledge 9215065Swnj * it and restart the finack timer. 9225065Swnj */ 9235065Swnj case TCPS_TIME_WAIT: 9245162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9255065Swnj goto dropafterack; 9264601Swnj } 9275085Swnj } 9284601Swnj 9295065Swnj step6: 9305065Swnj /* 9315244Sroot * Update window information. 93225939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 9335244Sroot */ 93425939Skarels if ((tiflags & TH_ACK) && 93525939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 9365391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 93725939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 93830525Skarels /* keep track of pure window updates */ 93930525Skarels if (ti->ti_len == 0 && 94032098Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) 94130525Skarels tcpstat.tcps_rcvwinupd++; 9425244Sroot tp->snd_wnd = ti->ti_win; 9435244Sroot tp->snd_wl1 = ti->ti_seq; 9445244Sroot tp->snd_wl2 = ti->ti_ack; 94525260Skarels if (tp->snd_wnd > tp->max_sndwnd) 94625260Skarels tp->max_sndwnd = tp->snd_wnd; 94726824Skarels needoutput = 1; 94826824Skarels } 9495244Sroot 9505244Sroot /* 9515547Swnj * Process segments with URG. 9525065Swnj */ 9537267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 9547267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9555547Swnj /* 95625939Skarels * This is a kludge, but if we receive and accept 95713121Ssam * random urgent pointers, we'll crash in 95813121Ssam * soreceive. It's hard to imagine someone 95913121Ssam * actually wanting to send this much urgent data. 96012441Ssam */ 96125652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 96212441Ssam ti->ti_urp = 0; /* XXX */ 96312441Ssam tiflags &= ~TH_URG; /* XXX */ 96425939Skarels goto dodata; /* XXX */ 96512441Ssam } 96612441Ssam /* 9675547Swnj * If this segment advances the known urgent pointer, 9685547Swnj * then mark the data stream. This should not happen 9695547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 9705547Swnj * a FIN has been received from the remote side. 9715547Swnj * In these states we ignore the URG. 97227190Skarels * 97327190Skarels * According to RFC961 (Assigned Protocols), 97427190Skarels * the urgent pointer points to the last octet 97527190Skarels * of urgent data. We continue, however, 97627190Skarels * to consider it to indicate the first octet 97727190Skarels * of data past the urgent section 97827190Skarels * as the original spec states. 9795547Swnj */ 9805547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 9815547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 9825547Swnj so->so_oobmark = so->so_rcv.sb_cc + 9835547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 9845547Swnj if (so->so_oobmark == 0) 9855547Swnj so->so_state |= SS_RCVATMARK; 9868313Sroot sohasoutofband(so); 98724816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 9885440Swnj } 9895547Swnj /* 9905547Swnj * Remove out of band data so doesn't get presented to user. 9915547Swnj * This can happen independent of advancing the URG pointer, 9925547Swnj * but if two URG's are pending at once, some out-of-band 9935547Swnj * data may creep in... ick. 9945547Swnj */ 995*37320Skarels if (ti->ti_urp <= ti->ti_len && 996*37320Skarels (so->so_options & SO_OOBINLINE) == 0) 9975547Swnj tcp_pulloutofband(so, ti); 99825939Skarels } else 99925939Skarels /* 100025939Skarels * If no out of band data is expected, 100125939Skarels * pull receive urgent pointer along 100225939Skarels * with the receive window. 100325939Skarels */ 100425939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 100525939Skarels tp->rcv_up = tp->rcv_nxt; 100625939Skarels dodata: /* XXX */ 10074601Swnj 10084601Swnj /* 10095065Swnj * Process the segment text, merging it into the TCP sequencing queue, 10105065Swnj * and arranging for acknowledgment of receipt if necessary. 10115065Swnj * This process logically involves adjusting tp->rcv_wnd as data 10125065Swnj * is presented to the user (this happens in tcp_usrreq.c, 10135065Swnj * case PRU_RCVD). If a FIN has already been received on this 10145065Swnj * connection then we just ignore the text. 10154601Swnj */ 101617946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 101717946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 101824816Skarels TCP_REASS(tp, ti, m, so, tiflags); 101925260Skarels /* 102025260Skarels * Note the amount of data that peer has sent into 102125260Skarels * our window, in order to estimate the sender's 102225260Skarels * buffer size. 102325260Skarels */ 102432098Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); 102525260Skarels if (len > tp->max_rcvd) 102625260Skarels tp->max_rcvd = len; 10275244Sroot } else { 10284924Swnj m_freem(m); 10295263Swnj tiflags &= ~TH_FIN; 10305244Sroot } 10314601Swnj 10324601Swnj /* 10335263Swnj * If FIN is received ACK the FIN and let the user know 10345263Swnj * that the connection is closing. 10354601Swnj */ 10365263Swnj if (tiflags & TH_FIN) { 10375244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 10385244Sroot socantrcvmore(so); 10395244Sroot tp->t_flags |= TF_ACKNOW; 10405244Sroot tp->rcv_nxt++; 10415244Sroot } 10425065Swnj switch (tp->t_state) { 10434601Swnj 10445065Swnj /* 10455065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 10465065Swnj * enter the CLOSE_WAIT state. 10474884Swnj */ 10485065Swnj case TCPS_SYN_RECEIVED: 10495065Swnj case TCPS_ESTABLISHED: 10505065Swnj tp->t_state = TCPS_CLOSE_WAIT; 10515065Swnj break; 10524884Swnj 10535065Swnj /* 10545085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 10555085Swnj * enter the CLOSING state. 10564884Swnj */ 10575065Swnj case TCPS_FIN_WAIT_1: 10585085Swnj tp->t_state = TCPS_CLOSING; 10595065Swnj break; 10604601Swnj 10615065Swnj /* 10625065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 10635065Swnj * starting the time-wait timer, turning off the other 10645065Swnj * standard timers. 10655065Swnj */ 10665065Swnj case TCPS_FIN_WAIT_2: 10675244Sroot tp->t_state = TCPS_TIME_WAIT; 10685074Swnj tcp_canceltimers(tp); 10695162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10705244Sroot soisdisconnected(so); 10715065Swnj break; 10725065Swnj 10734884Swnj /* 10745065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 10754884Swnj */ 10765065Swnj case TCPS_TIME_WAIT: 10775162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 10785065Swnj break; 10795085Swnj } 10804601Swnj } 10815267Sroot if (so->so_options & SO_DEBUG) 10825267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 10835085Swnj 10845085Swnj /* 10855085Swnj * Return any desired output. 10865085Swnj */ 108726824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 108825939Skarels (void) tcp_output(tp); 10895065Swnj return; 10905085Swnj 10915065Swnj dropafterack: 10925085Swnj /* 10936211Swnj * Generate an ACK dropping incoming segment if it occupies 10946211Swnj * sequence space, where the ACK reflects our state. 10955085Swnj */ 109626057Skarels if (tiflags & TH_RST) 10975085Swnj goto drop; 109831749Skarels m_freem(m); 109931721Skarels tp->t_flags |= TF_ACKNOW; 110031721Skarels (void) tcp_output(tp); 11015231Swnj return; 11025085Swnj 11035085Swnj dropwithreset: 110411731Ssam if (om) { 11056161Ssam (void) m_free(om); 110611731Ssam om = 0; 110711731Ssam } 11085085Swnj /* 11095244Sroot * Generate a RST, dropping incoming segment. 11105085Swnj * Make ACK acceptable to originator of segment. 111125197Skarels * Don't bother to respond if destination was broadcast. 11125085Swnj */ 1113*37320Skarels if ((tiflags & TH_RST) || m->m_flags & M_BCAST) 11145085Swnj goto drop; 11155085Swnj if (tiflags & TH_ACK) 1116*37320Skarels tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST); 11175085Swnj else { 11185085Swnj if (tiflags & TH_SYN) 11195085Swnj ti->ti_len++; 1120*37320Skarels tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0, 11216211Swnj TH_RST|TH_ACK); 11225085Swnj } 112310769Ssam /* destroy temporarily created socket */ 112410769Ssam if (dropsocket) 112510769Ssam (void) soabort(so); 11265231Swnj return; 11275085Swnj 11285065Swnj drop: 112911730Ssam if (om) 113011730Ssam (void) m_free(om); 11315085Swnj /* 11325085Swnj * Drop space held by incoming segment and return. 11335085Swnj */ 11346303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 11356303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 11365065Swnj m_freem(m); 113710769Ssam /* destroy temporarily created socket */ 113810769Ssam if (dropsocket) 113910769Ssam (void) soabort(so); 11405267Sroot return; 11415065Swnj } 11425065Swnj 114317272Skarels tcp_dooptions(tp, om, ti) 11445440Swnj struct tcpcb *tp; 11455440Swnj struct mbuf *om; 114617272Skarels struct tcpiphdr *ti; 11475419Swnj { 11485440Swnj register u_char *cp; 11495440Swnj int opt, optlen, cnt; 11505419Swnj 11515440Swnj cp = mtod(om, u_char *); 11525440Swnj cnt = om->m_len; 11535440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 11545440Swnj opt = cp[0]; 11555440Swnj if (opt == TCPOPT_EOL) 11565440Swnj break; 11575440Swnj if (opt == TCPOPT_NOP) 11585440Swnj optlen = 1; 115912169Ssam else { 11605440Swnj optlen = cp[1]; 116112169Ssam if (optlen <= 0) 116212169Ssam break; 116312169Ssam } 11645440Swnj switch (opt) { 11655440Swnj 11665440Swnj default: 11675440Swnj break; 11685440Swnj 11695440Swnj case TCPOPT_MAXSEG: 11705440Swnj if (optlen != 4) 11715440Swnj continue; 117217272Skarels if (!(ti->ti_flags & TH_SYN)) 117317272Skarels continue; 11745440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 11756161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 1176*37320Skarels tp->t_maxseg = min(tp->t_maxseg, tcp_mss(tp)); 11775440Swnj break; 11785419Swnj } 11795419Swnj } 11806161Ssam (void) m_free(om); 11815419Swnj } 11825419Swnj 11835419Swnj /* 11845547Swnj * Pull out of band byte out of a segment so 11855547Swnj * it doesn't appear in the user's data queue. 11865547Swnj * It is still reflected in the segment length for 11875547Swnj * sequencing purposes. 11885547Swnj */ 11895547Swnj tcp_pulloutofband(so, ti) 11905547Swnj struct socket *so; 11915547Swnj struct tcpiphdr *ti; 11925547Swnj { 11935547Swnj register struct mbuf *m; 11946116Swnj int cnt = ti->ti_urp - 1; 11955547Swnj 11965547Swnj m = dtom(ti); 11975547Swnj while (cnt >= 0) { 11985547Swnj if (m->m_len > cnt) { 11995547Swnj char *cp = mtod(m, caddr_t) + cnt; 12005547Swnj struct tcpcb *tp = sototcpcb(so); 12015547Swnj 12025547Swnj tp->t_iobc = *cp; 12035547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 12046161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 12055547Swnj m->m_len--; 12065547Swnj return; 12075547Swnj } 12085547Swnj cnt -= m->m_len; 12095547Swnj m = m->m_next; 12105547Swnj if (m == 0) 12115547Swnj break; 12125547Swnj } 12135547Swnj panic("tcp_pulloutofband"); 12145547Swnj } 12155547Swnj 12165547Swnj /* 121717272Skarels * Determine a reasonable value for maxseg size. 121817272Skarels * If the route is known, use one that can be handled 121917272Skarels * on the given interface without forcing IP to fragment. 122031726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 122131726Skarels * to utilize large mbufs. 122217272Skarels * If interface pointer is unavailable, or the destination isn't local, 122323975Skarels * use a conservative size (512 or the default IP max size, but no more 122423975Skarels * than the mtu of the interface through which we route), 122517272Skarels * as we can't discover anything about intervening gateways or networks. 122632034Skarels * We also initialize the congestion/slow start window to be a single 122732034Skarels * segment if the destination isn't local; this information should 122832034Skarels * probably all be saved with the routing entry at the transport level. 122917272Skarels * 123017272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 123117272Skarels */ 123217272Skarels tcp_mss(tp) 123323975Skarels register struct tcpcb *tp; 123417272Skarels { 123517272Skarels struct route *ro; 123617272Skarels struct ifnet *ifp; 123717272Skarels int mss; 123817272Skarels struct inpcb *inp; 123917272Skarels 124017272Skarels inp = tp->t_inpcb; 124117272Skarels ro = &inp->inp_route; 124217272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 124317272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 124417272Skarels /* No route yet, so try to acquire one */ 124517272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 124617272Skarels ro->ro_dst.sa_family = AF_INET; 1247*37320Skarels ro->ro_dst.sa_len = sizeof(ro->ro_dst); 124817272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 124917272Skarels inp->inp_faddr; 125017272Skarels rtalloc(ro); 125117272Skarels } 125217272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 125317316Skarels return (TCP_MSS); 125417272Skarels } 125517272Skarels 125617272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 125731726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 125831726Skarels if (mss > MCLBYTES) 125931726Skarels mss &= ~(MCLBYTES-1); 126017272Skarels #else 126131726Skarels if (mss > MCLBYTES) 126231726Skarels mss = mss / MCLBYTES * MCLBYTES; 126317272Skarels #endif 126423975Skarels if (in_localaddr(inp->inp_faddr)) 126523975Skarels return (mss); 126632098Skarels 1267*37320Skarels mss = min(mss, TCP_MSS); 126832034Skarels tp->snd_cwnd = mss; 126932034Skarels return (mss); 127017272Skarels } 1271