123190Smckusick /* 229149Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323190Smckusick * All rights reserved. The Berkeley software License Agreement 423190Smckusick * specifies the terms and conditions for redistribution. 523190Smckusick * 6*31730Skarels * @(#)tcp_input.c 7.7 (Berkeley) 06/30/87 723190Smckusick */ 84601Swnj 917062Sbloom #include "param.h" 1017062Sbloom #include "systm.h" 1117062Sbloom #include "mbuf.h" 1217062Sbloom #include "protosw.h" 1317062Sbloom #include "socket.h" 1417062Sbloom #include "socketvar.h" 1517062Sbloom #include "errno.h" 1610894Ssam 1710894Ssam #include "../net/if.h" 1810894Ssam #include "../net/route.h" 1910894Ssam 2017062Sbloom #include "in.h" 2117062Sbloom #include "in_pcb.h" 2217062Sbloom #include "in_systm.h" 2317062Sbloom #include "ip.h" 2417062Sbloom #include "ip_var.h" 2517062Sbloom #include "tcp.h" 2617062Sbloom #include "tcp_fsm.h" 2717062Sbloom #include "tcp_seq.h" 2817062Sbloom #include "tcp_timer.h" 2917062Sbloom #include "tcp_var.h" 3017062Sbloom #include "tcpip.h" 3117062Sbloom #include "tcp_debug.h" 324601Swnj 335300Sroot int tcpprintfs = 0; 344679Swnj int tcpcksum = 1; 355267Sroot struct tcpiphdr tcp_saveti; 365440Swnj extern tcpnodelack; 374601Swnj 385267Sroot struct tcpcb *tcp_newtcpcb(); 3924816Skarels 405065Swnj /* 4124816Skarels * Insert segment ti into reassembly queue of tcp with 4224816Skarels * control block tp. Return TH_FIN if reassembly now includes 4324816Skarels * a segment with FIN. The macro form does the common case inline 4424816Skarels * (segment is the next to be received on an established connection, 4524816Skarels * and the queue is empty), avoiding linkage into and removal 4624816Skarels * from the queue and repetition of various conversions. 4724816Skarels */ 4824816Skarels #define TCP_REASS(tp, ti, m, so, flags) { \ 4924816Skarels if ((ti)->ti_seq == (tp)->rcv_nxt && \ 5024816Skarels (tp)->seg_next == (struct tcpiphdr *)(tp) && \ 5124816Skarels (tp)->t_state == TCPS_ESTABLISHED) { \ 5224816Skarels (tp)->rcv_nxt += (ti)->ti_len; \ 5324816Skarels flags = (ti)->ti_flags & TH_FIN; \ 5430525Skarels tcpstat.tcps_rcvpack++;\ 5530525Skarels tcpstat.tcps_rcvbyte += (ti)->ti_len;\ 5624816Skarels sbappend(&(so)->so_rcv, (m)); \ 5724816Skarels sorwakeup(so); \ 5824816Skarels } else \ 5924816Skarels (flags) = tcp_reass((tp), (ti)); \ 6024816Skarels } 6124816Skarels 6224816Skarels tcp_reass(tp, ti) 6324816Skarels register struct tcpcb *tp; 6424816Skarels register struct tcpiphdr *ti; 6524816Skarels { 6624816Skarels register struct tcpiphdr *q; 6724816Skarels struct socket *so = tp->t_inpcb->inp_socket; 6824816Skarels struct mbuf *m; 6924816Skarels int flags; 7024816Skarels 7124816Skarels /* 7224816Skarels * Call with ti==0 after become established to 7324816Skarels * force pre-ESTABLISHED data up to user socket. 7424816Skarels */ 7524816Skarels if (ti == 0) 7624816Skarels goto present; 7724816Skarels 7824816Skarels /* 7924816Skarels * Find a segment which begins after this one does. 8024816Skarels */ 8124816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8224816Skarels q = (struct tcpiphdr *)q->ti_next) 8324816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8424816Skarels break; 8524816Skarels 8624816Skarels /* 8724816Skarels * If there is a preceding segment, it may provide some of 8824816Skarels * our data already. If so, drop the data from the incoming 8924816Skarels * segment. If it provides all of our data, drop us. 9024816Skarels */ 9124816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 9224816Skarels register int i; 9324816Skarels q = (struct tcpiphdr *)q->ti_prev; 9424816Skarels /* conversion to int (in i) handles seq wraparound */ 9524816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 9624816Skarels if (i > 0) { 9730525Skarels if (i >= ti->ti_len) { 9830525Skarels tcpstat.tcps_rcvduppack++; 9930525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10024816Skarels goto drop; 10130525Skarels } 10224816Skarels m_adj(dtom(ti), i); 10324816Skarels ti->ti_len -= i; 10424816Skarels ti->ti_seq += i; 10524816Skarels } 10624816Skarels q = (struct tcpiphdr *)(q->ti_next); 10724816Skarels } 10830525Skarels tcpstat.tcps_rcvoopack++; 10930525Skarels tcpstat.tcps_rcvoobyte += ti->ti_len; 11024816Skarels 11124816Skarels /* 11224816Skarels * While we overlap succeeding segments trim them or, 11324816Skarels * if they are completely covered, dequeue them. 11424816Skarels */ 11524816Skarels while (q != (struct tcpiphdr *)tp) { 11624816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 11724816Skarels if (i <= 0) 11824816Skarels break; 11924816Skarels if (i < q->ti_len) { 12024816Skarels q->ti_seq += i; 12124816Skarels q->ti_len -= i; 12224816Skarels m_adj(dtom(q), i); 12324816Skarels break; 12424816Skarels } 12524816Skarels q = (struct tcpiphdr *)q->ti_next; 12624816Skarels m = dtom(q->ti_prev); 12724816Skarels remque(q->ti_prev); 12824816Skarels m_freem(m); 12924816Skarels } 13024816Skarels 13124816Skarels /* 13224816Skarels * Stick new segment in its place. 13324816Skarels */ 13424816Skarels insque(ti, q->ti_prev); 13524816Skarels 13624816Skarels present: 13724816Skarels /* 13824816Skarels * Present data to user, advancing rcv_nxt through 13924816Skarels * completed sequence space. 14024816Skarels */ 14124816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 14224816Skarels return (0); 14324816Skarels ti = tp->seg_next; 14424816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 14524816Skarels return (0); 14624816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 14724816Skarels return (0); 14824816Skarels do { 14924816Skarels tp->rcv_nxt += ti->ti_len; 15024816Skarels flags = ti->ti_flags & TH_FIN; 15124816Skarels remque(ti); 15224816Skarels m = dtom(ti); 15324816Skarels ti = (struct tcpiphdr *)ti->ti_next; 15424816Skarels if (so->so_state & SS_CANTRCVMORE) 15524816Skarels m_freem(m); 15624816Skarels else 15724816Skarels sbappend(&so->so_rcv, m); 15824816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 15924816Skarels sorwakeup(so); 16024816Skarels return (flags); 16124816Skarels drop: 16224816Skarels m_freem(dtom(ti)); 16324816Skarels return (0); 16424816Skarels } 16524816Skarels 16624816Skarels /* 1675065Swnj * TCP input routine, follows pages 65-76 of the 1685065Swnj * protocol specification dated September, 1981 very closely. 1695065Swnj */ 1704924Swnj tcp_input(m0) 1714924Swnj struct mbuf *m0; 1724601Swnj { 1734924Swnj register struct tcpiphdr *ti; 1744924Swnj struct inpcb *inp; 1754924Swnj register struct mbuf *m; 1765440Swnj struct mbuf *om = 0; 1774924Swnj int len, tlen, off; 1785391Swnj register struct tcpcb *tp = 0; 1794924Swnj register int tiflags; 1804803Swnj struct socket *so; 18131721Skarels int todrop, acked, ourfinisacked, needoutput = 0; 1825267Sroot short ostate; 1836028Sroot struct in_addr laddr; 18410769Ssam int dropsocket = 0; 18530525Skarels int iss = 0; 1864924Swnj 18730525Skarels tcpstat.tcps_rcvtotal++; 1884924Swnj /* 1895244Sroot * Get IP and TCP header together in first mbuf. 1905244Sroot * Note: IP leaves IP header in first mbuf. 1914924Swnj */ 1924924Swnj m = m0; 1935020Sroot ti = mtod(m, struct tcpiphdr *); 1945244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 1955208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 1965307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 1975307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 19830525Skarels tcpstat.tcps_rcvshort++; 1995307Sroot return; 2005085Swnj } 2015085Swnj ti = mtod(m, struct tcpiphdr *); 2025085Swnj } 2034601Swnj 2044601Swnj /* 2055244Sroot * Checksum extended TCP header and data. 2064601Swnj */ 2074924Swnj tlen = ((struct ip *)ti)->ip_len; 2084924Swnj len = sizeof (struct ip) + tlen; 2094679Swnj if (tcpcksum) { 2104924Swnj ti->ti_next = ti->ti_prev = 0; 2114924Swnj ti->ti_x1 = 0; 2125223Swnj ti->ti_len = (u_short)tlen; 2136161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2145231Swnj if (ti->ti_sum = in_cksum(m, len)) { 21511830Ssam if (tcpprintfs) 21611830Ssam printf("tcp sum: src %x\n", ti->ti_src); 21730525Skarels tcpstat.tcps_rcvbadsum++; 2185085Swnj goto drop; 2194601Swnj } 2204601Swnj } 2214601Swnj 2224601Swnj /* 2235244Sroot * Check that TCP offset makes sense, 2245440Swnj * pull out TCP options and adjust length. 2254601Swnj */ 2264924Swnj off = ti->ti_off << 2; 2275231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 22811830Ssam if (tcpprintfs) 22911830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 23030525Skarels tcpstat.tcps_rcvbadoff++; 2315085Swnj goto drop; 2324924Swnj } 2336211Swnj tlen -= off; 2346211Swnj ti->ti_len = tlen; 2355440Swnj if (off > sizeof (struct tcphdr)) { 23624816Skarels if (m->m_len < sizeof(struct ip) + off) { 23724816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 23830525Skarels tcpstat.tcps_rcvshort++; 23924816Skarels return; 24024816Skarels } 24124816Skarels ti = mtod(m, struct tcpiphdr *); 2425440Swnj } 2439642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2445440Swnj if (om == 0) 2455440Swnj goto drop; 2465440Swnj om->m_len = off - sizeof (struct tcphdr); 2475440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2486161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2495440Swnj m->m_len -= om->m_len; 2506161Ssam bcopy(op+om->m_len, op, 2516161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2525440Swnj } 2535440Swnj } 2545065Swnj tiflags = ti->ti_flags; 2554924Swnj 2566093Sroot /* 25725729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2586093Sroot */ 25925729Smckusick m->m_off += sizeof(struct tcpiphdr); 26025729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2616093Sroot 2624924Swnj /* 2635244Sroot * Convert TCP protocol specific fields to host format. 2645085Swnj */ 2655085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2665085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2675085Swnj ti->ti_win = ntohs(ti->ti_win); 2685085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2695085Swnj 2705085Swnj /* 2718271Sroot * Locate pcb for segment. 2724924Swnj */ 27330525Skarels findpcb: 2745065Swnj inp = in_pcblookup 2756028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2766028Sroot INPLOOKUP_WILDCARD); 2775065Swnj 2785065Swnj /* 2795065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2805244Sroot * all data in the incoming segment is discarded. 2815065Swnj */ 2825300Sroot if (inp == 0) 2835085Swnj goto dropwithreset; 2845065Swnj tp = intotcpcb(inp); 2855300Sroot if (tp == 0) 2865085Swnj goto dropwithreset; 2875109Swnj so = inp->inp_socket; 2885267Sroot if (so->so_options & SO_DEBUG) { 2895267Sroot ostate = tp->t_state; 2905267Sroot tcp_saveti = *ti; 2915267Sroot } 2927510Sroot if (so->so_options & SO_ACCEPTCONN) { 2937510Sroot so = sonewconn(so); 2947510Sroot if (so == 0) 2957510Sroot goto drop; 29610769Ssam /* 29710769Ssam * This is ugly, but .... 29810769Ssam * 29910769Ssam * Mark socket as temporary until we're 30010769Ssam * committed to keeping it. The code at 30110769Ssam * ``drop'' and ``dropwithreset'' check the 30210769Ssam * flag dropsocket to see if the temporary 30310769Ssam * socket created here should be discarded. 30410769Ssam * We mark the socket as discardable until 30510769Ssam * we're committed to it below in TCPS_LISTEN. 30610769Ssam */ 30710769Ssam dropsocket++; 3087510Sroot inp = (struct inpcb *)so->so_pcb; 3097510Sroot inp->inp_laddr = ti->ti_dst; 3107510Sroot inp->inp_lport = ti->ti_dport; 31124816Skarels inp->inp_options = ip_srcroute(); 3127510Sroot tp = intotcpcb(inp); 3137510Sroot tp->t_state = TCPS_LISTEN; 3147510Sroot } 3154601Swnj 3164601Swnj /* 3175162Swnj * Segment received on connection. 3185162Swnj * Reset idle time and keep-alive timer. 3195162Swnj */ 3205162Swnj tp->t_idle = 0; 3215162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 3225162Swnj 3235162Swnj /* 32417272Skarels * Process options if not in LISTEN state, 32517272Skarels * else do it below (after getting remote address). 3265440Swnj */ 32717272Skarels if (om && tp->t_state != TCPS_LISTEN) { 32817272Skarels tcp_dooptions(tp, om, ti); 3295440Swnj om = 0; 3305440Swnj } 3315440Swnj 3325440Swnj /* 3335085Swnj * Calculate amount of space in receive window, 3345085Swnj * and then do TCP input processing. 33524816Skarels * Receive window is amount of space in rcv queue, 33624816Skarels * but not less than advertised window. 3374601Swnj */ 33825939Skarels { int win; 3394601Swnj 34025939Skarels win = sbspace(&so->so_rcv); 34125939Skarels if (win < 0) 34225939Skarels win = 0; 34325939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 34425939Skarels } 34525939Skarels 3464601Swnj switch (tp->t_state) { 3474601Swnj 3485065Swnj /* 3495065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3505065Swnj * If the segment contains an ACK then it is bad and send a RST. 3515065Swnj * If it does not contain a SYN then it is not interesting; drop it. 35225197Skarels * Don't bother responding if the destination was a broadcast. 3535085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3545065Swnj * tp->iss, and send a segment: 3555085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3565065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3575065Swnj * Fill in remote peer address fields if not previously specified. 3585065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3595244Sroot * segment in this state. 3605065Swnj */ 3618271Sroot case TCPS_LISTEN: { 36210145Ssam struct mbuf *am; 3638271Sroot register struct sockaddr_in *sin; 3648271Sroot 3655065Swnj if (tiflags & TH_RST) 3665065Swnj goto drop; 3675300Sroot if (tiflags & TH_ACK) 3685085Swnj goto dropwithreset; 3695300Sroot if ((tiflags & TH_SYN) == 0) 3705065Swnj goto drop; 37125197Skarels if (in_broadcast(ti->ti_dst)) 37225197Skarels goto drop; 37310145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 37410145Ssam if (am == NULL) 37510145Ssam goto drop; 37610145Ssam am->m_len = sizeof (struct sockaddr_in); 3778599Sroot sin = mtod(am, struct sockaddr_in *); 3788271Sroot sin->sin_family = AF_INET; 3798271Sroot sin->sin_addr = ti->ti_src; 3808271Sroot sin->sin_port = ti->ti_sport; 3816028Sroot laddr = inp->inp_laddr; 38210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3836028Sroot inp->inp_laddr = ti->ti_dst; 3848599Sroot if (in_pcbconnect(inp, am)) { 3856028Sroot inp->inp_laddr = laddr; 3868716Sroot (void) m_free(am); 3875244Sroot goto drop; 3886028Sroot } 3898716Sroot (void) m_free(am); 3905244Sroot tp->t_template = tcp_template(tp); 3915244Sroot if (tp->t_template == 0) { 39226386Skarels tp = tcp_drop(tp, ENOBUFS); 39317264Skarels dropsocket = 0; /* socket is already gone */ 3945244Sroot goto drop; 3955244Sroot } 39617272Skarels if (om) { 39717272Skarels tcp_dooptions(tp, om, ti); 39817272Skarels om = 0; 39917272Skarels } 40030525Skarels if (iss) 40130525Skarels tp->iss = iss; 40230525Skarels else 40330525Skarels tp->iss = tcp_iss; 40430525Skarels tcp_iss += TCP_ISSINCR/2; 4055065Swnj tp->irs = ti->ti_seq; 4065085Swnj tcp_sendseqinit(tp); 4075085Swnj tcp_rcvseqinit(tp); 40825939Skarels tp->t_flags |= TF_ACKNOW; 4095065Swnj tp->t_state = TCPS_SYN_RECEIVED; 4105244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 41110769Ssam dropsocket = 0; /* committed to socket */ 41230525Skarels tcpstat.tcps_accepts++; 4135085Swnj goto trimthenstep6; 4148271Sroot } 4154601Swnj 4165065Swnj /* 4175065Swnj * If the state is SYN_SENT: 4185065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4195065Swnj * if seg contains a RST, then drop the connection. 4205065Swnj * if seg does not contain SYN, then drop it. 4215065Swnj * Otherwise this is an acceptable SYN segment 4225065Swnj * initialize tp->rcv_nxt and tp->irs 4235065Swnj * if seg contains ack then advance tp->snd_una 4245065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4255065Swnj * arrange for segment to be acked (eventually) 4265065Swnj * continue processing rest of data/controls, beginning with URG 4275065Swnj */ 4285065Swnj case TCPS_SYN_SENT: 4295065Swnj if ((tiflags & TH_ACK) && 43024816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4315231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4325085Swnj goto dropwithreset; 4335065Swnj if (tiflags & TH_RST) { 43410394Ssam if (tiflags & TH_ACK) 43510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4365065Swnj goto drop; 4374601Swnj } 4385065Swnj if ((tiflags & TH_SYN) == 0) 4395065Swnj goto drop; 44030974Skarels if (tiflags & TH_ACK) { 44130974Skarels tp->snd_una = ti->ti_ack; 44230974Skarels if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 44330974Skarels tp->snd_nxt = tp->snd_una; 44430974Skarels } 4455244Sroot tp->t_timer[TCPT_REXMT] = 0; 4465065Swnj tp->irs = ti->ti_seq; 4475085Swnj tcp_rcvseqinit(tp); 4485085Swnj tp->t_flags |= TF_ACKNOW; 44930974Skarels if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) { 45030525Skarels tcpstat.tcps_connects++; 4515244Sroot soisconnected(so); 4525065Swnj tp->t_state = TCPS_ESTABLISHED; 45317272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4545162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4555162Swnj } else 4565085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4575085Swnj 4585085Swnj trimthenstep6: 4595085Swnj /* 4605231Swnj * Advance ti->ti_seq to correspond to first data byte. 4615085Swnj * If data, trim to stay within window, 4625085Swnj * dropping FIN if necessary. 4635085Swnj */ 4645231Swnj ti->ti_seq++; 4655085Swnj if (ti->ti_len > tp->rcv_wnd) { 4665085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4675085Swnj m_adj(m, -todrop); 4685085Swnj ti->ti_len = tp->rcv_wnd; 46925939Skarels tiflags &= ~TH_FIN; 47030525Skarels tcpstat.tcps_rcvpackafterwin++; 47130525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4725065Swnj } 4735263Swnj tp->snd_wl1 = ti->ti_seq - 1; 47425939Skarels tp->rcv_up = ti->ti_seq; 4755085Swnj goto step6; 4765065Swnj } 4774601Swnj 4785065Swnj /* 47930525Skarels * States other than LISTEN or SYN_SENT. 48030525Skarels * First check that at least some bytes of segment are within 48130525Skarels * receive window. If segment begins before rcv_nxt, 48230525Skarels * drop leading data (and SYN); if nothing left, just ack. 48316222Skarels */ 48430525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 48530525Skarels if (todrop > 0) { 48630525Skarels if (tiflags & TH_SYN) { 48730525Skarels tiflags &= ~TH_SYN; 48830525Skarels ti->ti_seq++; 48930525Skarels if (ti->ti_urp > 1) 49030525Skarels ti->ti_urp--; 49130525Skarels else 49230525Skarels tiflags &= ~TH_URG; 49330525Skarels todrop--; 49430525Skarels } 49530525Skarels if (todrop > ti->ti_len || 49630525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 497*31730Skarels #ifdef TCP_COMPAT_42 498*31730Skarels /* 499*31730Skarels * Don't toss RST in response to 4.2-style keepalive. 500*31730Skarels */ 501*31730Skarels if (ti->ti_seq == tp->rcv_nxt - 1 && tiflags & TH_RST) 502*31730Skarels goto do_rst; 503*31730Skarels #endif 50430525Skarels tcpstat.tcps_rcvduppack++; 50530525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 50630525Skarels goto dropafterack; 50730525Skarels } 50830525Skarels tcpstat.tcps_rcvpartduppack++; 50930525Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 51030525Skarels m_adj(m, todrop); 51130525Skarels ti->ti_seq += todrop; 51230525Skarels ti->ti_len -= todrop; 51330525Skarels if (ti->ti_urp > todrop) 51430525Skarels ti->ti_urp -= todrop; 51530525Skarels else { 51630525Skarels tiflags &= ~TH_URG; 51730525Skarels ti->ti_urp = 0; 51830525Skarels } 51916222Skarels } 52016222Skarels 5215065Swnj if (tp->rcv_wnd == 0) { 5225065Swnj /* 5235065Swnj * If window is closed can only take segments at 5245231Swnj * window edge, and have to drop data and PUSH from 5255065Swnj * incoming segments. 52630525Skarels * 52730525Skarels * If new data is received on a connection after the 52830525Skarels * user processes are gone, then RST the other end. 5295065Swnj */ 53030525Skarels if ((so->so_state & SS_NOFDREF) && 53130525Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 53230525Skarels tp = tcp_close(tp); 53330525Skarels tcpstat.tcps_rcvafterclose++; 53430525Skarels goto dropwithreset; 53530525Skarels } 53630525Skarels if (tp->rcv_nxt != ti->ti_seq) { 53730525Skarels tcpstat.tcps_rcvpackafterwin++; 53830525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 5395065Swnj goto dropafterack; 54030525Skarels } 5415085Swnj if (ti->ti_len > 0) { 54230525Skarels if (ti->ti_len == 1) 54330525Skarels tcpstat.tcps_rcvwinprobe++; 54430525Skarels else { 54530525Skarels tcpstat.tcps_rcvpackafterwin++; 54630525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 54730525Skarels } 5485690Swnj m_adj(m, ti->ti_len); 5495085Swnj ti->ti_len = 0; 55025939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5515065Swnj } 5525065Swnj } else { 5535065Swnj /* 5545065Swnj * If segment ends after window, drop trailing data 5555085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5565065Swnj */ 5575690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5585690Swnj if (todrop > 0) { 55930525Skarels if (todrop >= ti->ti_len) { 56030525Skarels /* 56130525Skarels * If a new connection request is received 56230525Skarels * while in TIME_WAIT, drop the old connection 56330525Skarels * and start over if the sequence numbers 56430525Skarels * are above the previous ones. 56530525Skarels */ 56630525Skarels if (tiflags & TH_SYN && 56730525Skarels tp->t_state == TCPS_TIME_WAIT && 56830525Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 56930525Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 57030525Skarels (void) tcp_close(tp); 57130525Skarels goto findpcb; 57230525Skarels } 57330525Skarels if (todrop == 1) 57430525Skarels tcpstat.tcps_rcvwinprobe++; 57530525Skarels else { 57630525Skarels tcpstat.tcps_rcvpackafterwin++; 57731442Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 57830525Skarels } 5795065Swnj goto dropafterack; 58030525Skarels } 58130525Skarels tcpstat.tcps_rcvpackafterwin++; 58230525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 5835065Swnj m_adj(m, -todrop); 5845065Swnj ti->ti_len -= todrop; 58525939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5865065Swnj } 5875065Swnj } 5884601Swnj 589*31730Skarels #ifdef TCP_COMPAT_42 590*31730Skarels do_rst: 591*31730Skarels #endif 5925065Swnj /* 5935065Swnj * If the RST bit is set examine the state: 5945065Swnj * SYN_RECEIVED STATE: 5955065Swnj * If passive open, return to LISTEN state. 5965065Swnj * If active open, inform user that connection was refused. 5975065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 5985065Swnj * Inform user that connection was reset, and close tcb. 5995065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 6005065Swnj * Close the tcb. 6015065Swnj */ 6025065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 6035267Sroot 6045065Swnj case TCPS_SYN_RECEIVED: 60510394Ssam tp = tcp_drop(tp, ECONNREFUSED); 6065065Swnj goto drop; 6074601Swnj 6085065Swnj case TCPS_ESTABLISHED: 6095065Swnj case TCPS_FIN_WAIT_1: 6105065Swnj case TCPS_FIN_WAIT_2: 6115065Swnj case TCPS_CLOSE_WAIT: 61210394Ssam tp = tcp_drop(tp, ECONNRESET); 6135065Swnj goto drop; 6145065Swnj 6155065Swnj case TCPS_CLOSING: 6165065Swnj case TCPS_LAST_ACK: 6175065Swnj case TCPS_TIME_WAIT: 61810394Ssam tp = tcp_close(tp); 6195065Swnj goto drop; 6204601Swnj } 6214601Swnj 6224601Swnj /* 6235065Swnj * If a SYN is in the window, then this is an 6245065Swnj * error and we send an RST and drop the connection. 6254601Swnj */ 6265065Swnj if (tiflags & TH_SYN) { 62710394Ssam tp = tcp_drop(tp, ECONNRESET); 6285085Swnj goto dropwithreset; 6294601Swnj } 6304601Swnj 6314601Swnj /* 6325065Swnj * If the ACK bit is off we drop the segment and return. 6334601Swnj */ 6345085Swnj if ((tiflags & TH_ACK) == 0) 6355065Swnj goto drop; 6365065Swnj 6375065Swnj /* 6385065Swnj * Ack processing. 6395065Swnj */ 6404601Swnj switch (tp->t_state) { 6414601Swnj 6425065Swnj /* 6435065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 64431721Skarels * ESTABLISHED state and continue processing, otherwise 6455065Swnj * send an RST. 6465065Swnj */ 6475065Swnj case TCPS_SYN_RECEIVED: 6485085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6495231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6505085Swnj goto dropwithreset; 65130525Skarels tcpstat.tcps_connects++; 6525085Swnj soisconnected(so); 6535085Swnj tp->t_state = TCPS_ESTABLISHED; 65417272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6555162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6565244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6575085Swnj /* fall into ... */ 6584601Swnj 6595065Swnj /* 6605065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6615065Swnj * ACKs. If the ack is in the range 6625231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6635065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6645065Swnj * data from the retransmission queue. If this ACK reflects 6655065Swnj * more up to date window information we update our window information. 6665065Swnj */ 6675065Swnj case TCPS_ESTABLISHED: 6685065Swnj case TCPS_FIN_WAIT_1: 6695065Swnj case TCPS_FIN_WAIT_2: 6705065Swnj case TCPS_CLOSE_WAIT: 6715065Swnj case TCPS_CLOSING: 6725244Sroot case TCPS_LAST_ACK: 6735244Sroot case TCPS_TIME_WAIT: 6745085Swnj 67530525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 67630525Skarels if (ti->ti_len == 0) 67730525Skarels tcpstat.tcps_rcvdupack++; 6785065Swnj break; 67930525Skarels } 68030525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 68130525Skarels tcpstat.tcps_rcvacktoomuch++; 6825065Swnj goto dropafterack; 68330525Skarels } 6845085Swnj acked = ti->ti_ack - tp->snd_una; 68530525Skarels tcpstat.tcps_rcvackpack++; 68630525Skarels tcpstat.tcps_rcvackbyte += acked; 6875951Swnj 6885951Swnj /* 6895951Swnj * If transmit timer is running and timed sequence 6905951Swnj * number was acked, update smoothed round trip time. 6915951Swnj */ 6925951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 69330525Skarels tcpstat.tcps_rttupdated++; 69431726Skarels if (tp->t_srtt != 0) { 69531726Skarels register short delta; 69631726Skarels 69731726Skarels /* 69831726Skarels * srtt is stored as fixed point with 3 bits 69931726Skarels * after the binary point (i.e., scaled by 8). 70031726Skarels * The following magic is equivalent 70131726Skarels * to the smoothing algorithm in rfc793 70231726Skarels * with an alpha of .875 70331726Skarels * (srtt = rtt/8 + srtt*7/8 in fixed point). 70431726Skarels */ 70531726Skarels delta = tp->t_rtt - (tp->t_srtt >> 3); 70631726Skarels if ((tp->t_srtt += delta) <= 0) 70731726Skarels tp->t_srtt = 1; 70831726Skarels /* 70931726Skarels * We accumulate a smoothed rtt variance, 71031726Skarels * then set the retransmit timer to smoothed 71131726Skarels * rtt + 2 times the smoothed variance. 71231726Skarels * rttvar is strored as fixed point 71331726Skarels * with 2 bits after the binary point 71431726Skarels * (scaled by 4). The following is equivalent 71531726Skarels * to rfc793 smoothing with an alpha of .75 71631726Skarels * (rttvar = rttvar*3/4 + |delta| / 4). 71731726Skarels * This replaces rfc793's wired-in beta. 71831726Skarels */ 71931726Skarels if (delta < 0) 72031726Skarels delta = -delta; 72131726Skarels delta -= (tp->t_rttvar >> 2); 72231726Skarels if ((tp->t_rttvar += delta) <= 0) 72331726Skarels tp->t_rttvar = 1; 72431726Skarels } else { 72531726Skarels /* 72631726Skarels * No rtt measurement yet - use the 72731726Skarels * unsmoothed rtt. Set the variance 72831726Skarels * to half the rtt (so our first 72931726Skarels * retransmit happens at 2*rtt) 73031726Skarels */ 73131726Skarels tp->t_srtt = tp->t_rtt << 3; 73231726Skarels tp->t_rttvar = tp->t_rtt << 1; 73331726Skarels } 7345951Swnj tp->t_rtt = 0; 7355951Swnj } 7365951Swnj 73726824Skarels /* 73826824Skarels * If all outstanding data is acked, stop retransmit 73926824Skarels * timer and remember to restart (more output or persist). 74026824Skarels * If there is more data to be acked, restart retransmit 74131726Skarels * timer; set to smoothed rtt + 2*rttvar. 74226824Skarels */ 74326824Skarels if (ti->ti_ack == tp->snd_max) { 7445244Sroot tp->t_timer[TCPT_REXMT] = 0; 74526824Skarels needoutput = 1; 74626824Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) { 7475244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 74831726Skarels ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, 74931726Skarels TCPTV_MIN, TCPTV_REXMTMAX); 7505300Sroot tp->t_rxtshift = 0; 7515085Swnj } 75217360Skarels /* 75331442Skarels * When new data is acked, open the congestion window 75431442Skarels * by one max sized segment. 75517360Skarels */ 75631442Skarels tp->snd_cwnd = MIN(tp->snd_cwnd + tp->t_maxseg, 65535); 7575307Sroot if (acked > so->so_snd.sb_cc) { 75815386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 75926386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 76031721Skarels ourfinisacked = 1; 7615307Sroot } else { 7626161Ssam sbdrop(&so->so_snd, acked); 7635307Sroot tp->snd_wnd -= acked; 76431721Skarels ourfinisacked = 0; 7655307Sroot } 7666434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 7675300Sroot sowwakeup(so); 7685231Swnj tp->snd_una = ti->ti_ack; 7695357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 7705357Sroot tp->snd_nxt = tp->snd_una; 7715162Swnj 7724601Swnj switch (tp->t_state) { 7734601Swnj 7745065Swnj /* 7755065Swnj * In FIN_WAIT_1 STATE in addition to the processing 7765065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 7775085Swnj * then enter FIN_WAIT_2. 7785065Swnj */ 7795065Swnj case TCPS_FIN_WAIT_1: 7805896Swnj if (ourfinisacked) { 7815896Swnj /* 7825896Swnj * If we can't receive any more 7835896Swnj * data, then closing user can proceed. 78424816Skarels * Starting the timer is contrary to the 78524816Skarels * specification, but if we don't get a FIN 78624816Skarels * we'll hang forever. 7875896Swnj */ 78824816Skarels if (so->so_state & SS_CANTRCVMORE) { 7895896Swnj soisdisconnected(so); 79024816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 79124816Skarels } 7925085Swnj tp->t_state = TCPS_FIN_WAIT_2; 7935896Swnj } 7944601Swnj break; 7954601Swnj 7965065Swnj /* 7975065Swnj * In CLOSING STATE in addition to the processing for 7985065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 7995065Swnj * then enter the TIME-WAIT state, otherwise ignore 8005065Swnj * the segment. 8015065Swnj */ 8025065Swnj case TCPS_CLOSING: 8035244Sroot if (ourfinisacked) { 8045065Swnj tp->t_state = TCPS_TIME_WAIT; 8055244Sroot tcp_canceltimers(tp); 8065244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8075244Sroot soisdisconnected(so); 8085244Sroot } 8095244Sroot break; 8104601Swnj 8115065Swnj /* 8125085Swnj * The only thing that can arrive in LAST_ACK state 8135085Swnj * is an acknowledgment of our FIN. If our FIN is now 8145085Swnj * acknowledged, delete the TCB, enter the closed state 8155085Swnj * and return. 8165065Swnj */ 8175065Swnj case TCPS_LAST_ACK: 81810394Ssam if (ourfinisacked) 81910394Ssam tp = tcp_close(tp); 8205065Swnj goto drop; 8214601Swnj 8225065Swnj /* 8235065Swnj * In TIME_WAIT state the only thing that should arrive 8245065Swnj * is a retransmission of the remote FIN. Acknowledge 8255065Swnj * it and restart the finack timer. 8265065Swnj */ 8275065Swnj case TCPS_TIME_WAIT: 8285162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8295065Swnj goto dropafterack; 8304601Swnj } 8315085Swnj } 8324601Swnj 8335065Swnj step6: 8345065Swnj /* 8355244Sroot * Update window information. 83625939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 8375244Sroot */ 83825939Skarels if ((tiflags & TH_ACK) && 83925939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 8405391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 84125939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 84230525Skarels /* keep track of pure window updates */ 84330525Skarels if (ti->ti_len == 0 && 84430525Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) { 84530525Skarels tcpstat.tcps_rcvwinupd++; 84630525Skarels tcpstat.tcps_rcvdupack--; 84730525Skarels } 8485244Sroot tp->snd_wnd = ti->ti_win; 8495244Sroot tp->snd_wl1 = ti->ti_seq; 8505244Sroot tp->snd_wl2 = ti->ti_ack; 85125260Skarels if (tp->snd_wnd > tp->max_sndwnd) 85225260Skarels tp->max_sndwnd = tp->snd_wnd; 85326824Skarels needoutput = 1; 85426824Skarels } 8555244Sroot 8565244Sroot /* 8575547Swnj * Process segments with URG. 8585065Swnj */ 8597267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 8607267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8615547Swnj /* 86225939Skarels * This is a kludge, but if we receive and accept 86313121Ssam * random urgent pointers, we'll crash in 86413121Ssam * soreceive. It's hard to imagine someone 86513121Ssam * actually wanting to send this much urgent data. 86612441Ssam */ 86725652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 86812441Ssam ti->ti_urp = 0; /* XXX */ 86912441Ssam tiflags &= ~TH_URG; /* XXX */ 87025939Skarels goto dodata; /* XXX */ 87112441Ssam } 87212441Ssam /* 8735547Swnj * If this segment advances the known urgent pointer, 8745547Swnj * then mark the data stream. This should not happen 8755547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 8765547Swnj * a FIN has been received from the remote side. 8775547Swnj * In these states we ignore the URG. 87827190Skarels * 87927190Skarels * According to RFC961 (Assigned Protocols), 88027190Skarels * the urgent pointer points to the last octet 88127190Skarels * of urgent data. We continue, however, 88227190Skarels * to consider it to indicate the first octet 88327190Skarels * of data past the urgent section 88427190Skarels * as the original spec states. 8855547Swnj */ 8865547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 8875547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 8885547Swnj so->so_oobmark = so->so_rcv.sb_cc + 8895547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 8905547Swnj if (so->so_oobmark == 0) 8915547Swnj so->so_state |= SS_RCVATMARK; 8928313Sroot sohasoutofband(so); 89324816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 8945440Swnj } 8955547Swnj /* 8965547Swnj * Remove out of band data so doesn't get presented to user. 8975547Swnj * This can happen independent of advancing the URG pointer, 8985547Swnj * but if two URG's are pending at once, some out-of-band 8995547Swnj * data may creep in... ick. 9005547Swnj */ 90127190Skarels if (ti->ti_urp <= ti->ti_len && 90227190Skarels (so->so_options & SO_OOBINLINE) == 0) 9035547Swnj tcp_pulloutofband(so, ti); 90425939Skarels } else 90525939Skarels /* 90625939Skarels * If no out of band data is expected, 90725939Skarels * pull receive urgent pointer along 90825939Skarels * with the receive window. 90925939Skarels */ 91025939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 91125939Skarels tp->rcv_up = tp->rcv_nxt; 91225939Skarels dodata: /* XXX */ 9134601Swnj 9144601Swnj /* 9155065Swnj * Process the segment text, merging it into the TCP sequencing queue, 9165065Swnj * and arranging for acknowledgment of receipt if necessary. 9175065Swnj * This process logically involves adjusting tp->rcv_wnd as data 9185065Swnj * is presented to the user (this happens in tcp_usrreq.c, 9195065Swnj * case PRU_RCVD). If a FIN has already been received on this 9205065Swnj * connection then we just ignore the text. 9214601Swnj */ 92217946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 92317946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 92424816Skarels TCP_REASS(tp, ti, m, so, tiflags); 9255440Swnj if (tcpnodelack == 0) 9265440Swnj tp->t_flags |= TF_DELACK; 9275440Swnj else 9285440Swnj tp->t_flags |= TF_ACKNOW; 92925260Skarels /* 93025260Skarels * Note the amount of data that peer has sent into 93125260Skarels * our window, in order to estimate the sender's 93225260Skarels * buffer size. 93325260Skarels */ 93425260Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv); 93525260Skarels if (len > tp->max_rcvd) 93625260Skarels tp->max_rcvd = len; 9375244Sroot } else { 9384924Swnj m_freem(m); 9395263Swnj tiflags &= ~TH_FIN; 9405244Sroot } 9414601Swnj 9424601Swnj /* 9435263Swnj * If FIN is received ACK the FIN and let the user know 9445263Swnj * that the connection is closing. 9454601Swnj */ 9465263Swnj if (tiflags & TH_FIN) { 9475244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9485244Sroot socantrcvmore(so); 9495244Sroot tp->t_flags |= TF_ACKNOW; 9505244Sroot tp->rcv_nxt++; 9515244Sroot } 9525065Swnj switch (tp->t_state) { 9534601Swnj 9545065Swnj /* 9555065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 9565065Swnj * enter the CLOSE_WAIT state. 9574884Swnj */ 9585065Swnj case TCPS_SYN_RECEIVED: 9595065Swnj case TCPS_ESTABLISHED: 9605065Swnj tp->t_state = TCPS_CLOSE_WAIT; 9615065Swnj break; 9624884Swnj 9635065Swnj /* 9645085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 9655085Swnj * enter the CLOSING state. 9664884Swnj */ 9675065Swnj case TCPS_FIN_WAIT_1: 9685085Swnj tp->t_state = TCPS_CLOSING; 9695065Swnj break; 9704601Swnj 9715065Swnj /* 9725065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 9735065Swnj * starting the time-wait timer, turning off the other 9745065Swnj * standard timers. 9755065Swnj */ 9765065Swnj case TCPS_FIN_WAIT_2: 9775244Sroot tp->t_state = TCPS_TIME_WAIT; 9785074Swnj tcp_canceltimers(tp); 9795162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9805244Sroot soisdisconnected(so); 9815065Swnj break; 9825065Swnj 9834884Swnj /* 9845065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 9854884Swnj */ 9865065Swnj case TCPS_TIME_WAIT: 9875162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9885065Swnj break; 9895085Swnj } 9904601Swnj } 9915267Sroot if (so->so_options & SO_DEBUG) 9925267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 9935085Swnj 9945085Swnj /* 9955085Swnj * Return any desired output. 9965085Swnj */ 99726824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 99825939Skarels (void) tcp_output(tp); 9995065Swnj return; 10005085Swnj 10015065Swnj dropafterack: 10025085Swnj /* 10036211Swnj * Generate an ACK dropping incoming segment if it occupies 10046211Swnj * sequence space, where the ACK reflects our state. 10055085Swnj */ 100626057Skarels if (tiflags & TH_RST) 10075085Swnj goto drop; 10086303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 10096303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 101031721Skarels tp->t_flags |= TF_ACKNOW; 101131721Skarels (void) tcp_output(tp); 10125231Swnj return; 10135085Swnj 10145085Swnj dropwithreset: 101511731Ssam if (om) { 10166161Ssam (void) m_free(om); 101711731Ssam om = 0; 101811731Ssam } 10195085Swnj /* 10205244Sroot * Generate a RST, dropping incoming segment. 10215085Swnj * Make ACK acceptable to originator of segment. 102225197Skarels * Don't bother to respond if destination was broadcast. 10235085Swnj */ 102425197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 10255085Swnj goto drop; 10265085Swnj if (tiflags & TH_ACK) 10275391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 10285085Swnj else { 10295085Swnj if (tiflags & TH_SYN) 10305085Swnj ti->ti_len++; 10316211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 10326211Swnj TH_RST|TH_ACK); 10335085Swnj } 103410769Ssam /* destroy temporarily created socket */ 103510769Ssam if (dropsocket) 103610769Ssam (void) soabort(so); 10375231Swnj return; 10385085Swnj 10395065Swnj drop: 104011730Ssam if (om) 104111730Ssam (void) m_free(om); 10425085Swnj /* 10435085Swnj * Drop space held by incoming segment and return. 10445085Swnj */ 10456303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 10466303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 10475065Swnj m_freem(m); 104810769Ssam /* destroy temporarily created socket */ 104910769Ssam if (dropsocket) 105010769Ssam (void) soabort(so); 10515267Sroot return; 10525065Swnj } 10535065Swnj 105417272Skarels tcp_dooptions(tp, om, ti) 10555440Swnj struct tcpcb *tp; 10565440Swnj struct mbuf *om; 105717272Skarels struct tcpiphdr *ti; 10585419Swnj { 10595440Swnj register u_char *cp; 10605440Swnj int opt, optlen, cnt; 10615419Swnj 10625440Swnj cp = mtod(om, u_char *); 10635440Swnj cnt = om->m_len; 10645440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 10655440Swnj opt = cp[0]; 10665440Swnj if (opt == TCPOPT_EOL) 10675440Swnj break; 10685440Swnj if (opt == TCPOPT_NOP) 10695440Swnj optlen = 1; 107012169Ssam else { 10715440Swnj optlen = cp[1]; 107212169Ssam if (optlen <= 0) 107312169Ssam break; 107412169Ssam } 10755440Swnj switch (opt) { 10765440Swnj 10775440Swnj default: 10785440Swnj break; 10795440Swnj 10805440Swnj case TCPOPT_MAXSEG: 10815440Swnj if (optlen != 4) 10825440Swnj continue; 108317272Skarels if (!(ti->ti_flags & TH_SYN)) 108417272Skarels continue; 10855440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 10866161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 108717272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 10885440Swnj break; 10895419Swnj } 10905419Swnj } 10916161Ssam (void) m_free(om); 10925419Swnj } 10935419Swnj 10945419Swnj /* 10955547Swnj * Pull out of band byte out of a segment so 10965547Swnj * it doesn't appear in the user's data queue. 10975547Swnj * It is still reflected in the segment length for 10985547Swnj * sequencing purposes. 10995547Swnj */ 11005547Swnj tcp_pulloutofband(so, ti) 11015547Swnj struct socket *so; 11025547Swnj struct tcpiphdr *ti; 11035547Swnj { 11045547Swnj register struct mbuf *m; 11056116Swnj int cnt = ti->ti_urp - 1; 11065547Swnj 11075547Swnj m = dtom(ti); 11085547Swnj while (cnt >= 0) { 11095547Swnj if (m->m_len > cnt) { 11105547Swnj char *cp = mtod(m, caddr_t) + cnt; 11115547Swnj struct tcpcb *tp = sototcpcb(so); 11125547Swnj 11135547Swnj tp->t_iobc = *cp; 11145547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 11156161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 11165547Swnj m->m_len--; 11175547Swnj return; 11185547Swnj } 11195547Swnj cnt -= m->m_len; 11205547Swnj m = m->m_next; 11215547Swnj if (m == 0) 11225547Swnj break; 11235547Swnj } 11245547Swnj panic("tcp_pulloutofband"); 11255547Swnj } 11265547Swnj 11275547Swnj /* 112817272Skarels * Determine a reasonable value for maxseg size. 112917272Skarels * If the route is known, use one that can be handled 113017272Skarels * on the given interface without forcing IP to fragment. 113131726Skarels * If bigger than an mbuf cluster (MCLBYTES), round down to nearest size 113231726Skarels * to utilize large mbufs. 113317272Skarels * If interface pointer is unavailable, or the destination isn't local, 113423975Skarels * use a conservative size (512 or the default IP max size, but no more 113523975Skarels * than the mtu of the interface through which we route), 113617272Skarels * as we can't discover anything about intervening gateways or networks. 113717272Skarels * 113817272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 113917272Skarels */ 114017272Skarels tcp_mss(tp) 114123975Skarels register struct tcpcb *tp; 114217272Skarels { 114317272Skarels struct route *ro; 114417272Skarels struct ifnet *ifp; 114517272Skarels int mss; 114617272Skarels struct inpcb *inp; 114717272Skarels 114817272Skarels inp = tp->t_inpcb; 114917272Skarels ro = &inp->inp_route; 115017272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 115117272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 115217272Skarels /* No route yet, so try to acquire one */ 115317272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 115417272Skarels ro->ro_dst.sa_family = AF_INET; 115517272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 115617272Skarels inp->inp_faddr; 115717272Skarels rtalloc(ro); 115817272Skarels } 115917272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 116017316Skarels return (TCP_MSS); 116117272Skarels } 116217272Skarels 116317272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 116431726Skarels #if (MCLBYTES & (MCLBYTES - 1)) == 0 116531726Skarels if (mss > MCLBYTES) 116631726Skarels mss &= ~(MCLBYTES-1); 116717272Skarels #else 116831726Skarels if (mss > MCLBYTES) 116931726Skarels mss = mss / MCLBYTES * MCLBYTES; 117017272Skarels #endif 117123975Skarels if (in_localaddr(inp->inp_faddr)) 117223975Skarels return (mss); 117317316Skarels return (MIN(mss, TCP_MSS)); 117417272Skarels } 1175