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*30525Skarels * @(#)tcp_input.c 7.2 (Berkeley) 02/19/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; \ 54*30525Skarels tcpstat.tcps_rcvpack++;\ 55*30525Skarels 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) { 97*30525Skarels if (i >= ti->ti_len) { 98*30525Skarels tcpstat.tcps_rcvduppack++; 99*30525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 10024816Skarels goto drop; 101*30525Skarels } 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 } 108*30525Skarels tcpstat.tcps_rcvoopack++; 109*30525Skarels 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; 18126824Skarels int todrop, acked, needoutput = 0; 1825267Sroot short ostate; 1836028Sroot struct in_addr laddr; 18410769Ssam int dropsocket = 0; 185*30525Skarels int iss = 0; 1864924Swnj 187*30525Skarels 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) { 198*30525Skarels 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); 217*30525Skarels 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); 230*30525Skarels 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) { 238*30525Skarels 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 */ 273*30525Skarels 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 } 400*30525Skarels if (iss) 401*30525Skarels tp->iss = iss; 402*30525Skarels else 403*30525Skarels tp->iss = tcp_iss; 404*30525Skarels 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 */ 412*30525Skarels 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; 4405231Swnj tp->snd_una = ti->ti_ack; 4415357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4425357Sroot tp->snd_nxt = tp->snd_una; 4435244Sroot tp->t_timer[TCPT_REXMT] = 0; 4445065Swnj tp->irs = ti->ti_seq; 4455085Swnj tcp_rcvseqinit(tp); 4465085Swnj tp->t_flags |= TF_ACKNOW; 4475162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 448*30525Skarels tcpstat.tcps_connects++; 4495244Sroot soisconnected(so); 4505065Swnj tp->t_state = TCPS_ESTABLISHED; 45117272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4525162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4535162Swnj } else 4545085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4555085Swnj 4565085Swnj trimthenstep6: 4575085Swnj /* 4585231Swnj * Advance ti->ti_seq to correspond to first data byte. 4595085Swnj * If data, trim to stay within window, 4605085Swnj * dropping FIN if necessary. 4615085Swnj */ 4625231Swnj ti->ti_seq++; 4635085Swnj if (ti->ti_len > tp->rcv_wnd) { 4645085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4655085Swnj m_adj(m, -todrop); 4665085Swnj ti->ti_len = tp->rcv_wnd; 46725939Skarels tiflags &= ~TH_FIN; 468*30525Skarels tcpstat.tcps_rcvpackafterwin++; 469*30525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 4705065Swnj } 4715263Swnj tp->snd_wl1 = ti->ti_seq - 1; 47225939Skarels tp->rcv_up = ti->ti_seq; 4735085Swnj goto step6; 4745065Swnj } 4754601Swnj 4765065Swnj /* 477*30525Skarels * States other than LISTEN or SYN_SENT. 478*30525Skarels * First check that at least some bytes of segment are within 479*30525Skarels * receive window. If segment begins before rcv_nxt, 480*30525Skarels * drop leading data (and SYN); if nothing left, just ack. 48116222Skarels */ 482*30525Skarels todrop = tp->rcv_nxt - ti->ti_seq; 483*30525Skarels if (todrop > 0) { 484*30525Skarels if (tiflags & TH_SYN) { 485*30525Skarels tiflags &= ~TH_SYN; 486*30525Skarels ti->ti_seq++; 487*30525Skarels if (ti->ti_urp > 1) 488*30525Skarels ti->ti_urp--; 489*30525Skarels else 490*30525Skarels tiflags &= ~TH_URG; 491*30525Skarels todrop--; 492*30525Skarels } 493*30525Skarels if (todrop > ti->ti_len || 494*30525Skarels todrop == ti->ti_len && (tiflags&TH_FIN) == 0) { 495*30525Skarels tcpstat.tcps_rcvduppack++; 496*30525Skarels tcpstat.tcps_rcvdupbyte += ti->ti_len; 497*30525Skarels goto dropafterack; 498*30525Skarels } 499*30525Skarels tcpstat.tcps_rcvpartduppack++; 500*30525Skarels tcpstat.tcps_rcvpartdupbyte += todrop; 501*30525Skarels m_adj(m, todrop); 502*30525Skarels ti->ti_seq += todrop; 503*30525Skarels ti->ti_len -= todrop; 504*30525Skarels if (ti->ti_urp > todrop) 505*30525Skarels ti->ti_urp -= todrop; 506*30525Skarels else { 507*30525Skarels tiflags &= ~TH_URG; 508*30525Skarels ti->ti_urp = 0; 509*30525Skarels } 51016222Skarels } 51116222Skarels 5125065Swnj if (tp->rcv_wnd == 0) { 5135065Swnj /* 5145065Swnj * If window is closed can only take segments at 5155231Swnj * window edge, and have to drop data and PUSH from 5165065Swnj * incoming segments. 517*30525Skarels * 518*30525Skarels * If new data is received on a connection after the 519*30525Skarels * user processes are gone, then RST the other end. 5205065Swnj */ 521*30525Skarels if ((so->so_state & SS_NOFDREF) && 522*30525Skarels tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) { 523*30525Skarels tp = tcp_close(tp); 524*30525Skarels tcpstat.tcps_rcvafterclose++; 525*30525Skarels goto dropwithreset; 526*30525Skarels } 527*30525Skarels if (tp->rcv_nxt != ti->ti_seq) { 528*30525Skarels tcpstat.tcps_rcvpackafterwin++; 529*30525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 5305065Swnj goto dropafterack; 531*30525Skarels } 5325085Swnj if (ti->ti_len > 0) { 533*30525Skarels if (ti->ti_len == 1) 534*30525Skarels tcpstat.tcps_rcvwinprobe++; 535*30525Skarels else { 536*30525Skarels tcpstat.tcps_rcvpackafterwin++; 537*30525Skarels tcpstat.tcps_rcvbyteafterwin += ti->ti_len; 538*30525Skarels } 5395690Swnj m_adj(m, ti->ti_len); 5405085Swnj ti->ti_len = 0; 54125939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5425065Swnj } 5435065Swnj } else { 5445065Swnj /* 5455065Swnj * If segment ends after window, drop trailing data 5465085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5475065Swnj */ 5485690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5495690Swnj if (todrop > 0) { 550*30525Skarels if (todrop >= ti->ti_len) { 551*30525Skarels /* 552*30525Skarels * If a new connection request is received 553*30525Skarels * while in TIME_WAIT, drop the old connection 554*30525Skarels * and start over if the sequence numbers 555*30525Skarels * are above the previous ones. 556*30525Skarels */ 557*30525Skarels if (tiflags & TH_SYN && 558*30525Skarels tp->t_state == TCPS_TIME_WAIT && 559*30525Skarels SEQ_GT(ti->ti_seq, tp->rcv_nxt)) { 560*30525Skarels iss = tp->rcv_nxt + TCP_ISSINCR; 561*30525Skarels (void) tcp_close(tp); 562*30525Skarels goto findpcb; 563*30525Skarels } 564*30525Skarels if (todrop == 1) 565*30525Skarels tcpstat.tcps_rcvwinprobe++; 566*30525Skarels else { 567*30525Skarels tcpstat.tcps_rcvpackafterwin++; 568*30525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 569*30525Skarels } 5705065Swnj goto dropafterack; 571*30525Skarels } 572*30525Skarels tcpstat.tcps_rcvpackafterwin++; 573*30525Skarels tcpstat.tcps_rcvbyteafterwin += todrop; 5745065Swnj m_adj(m, -todrop); 5755065Swnj ti->ti_len -= todrop; 57625939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5775065Swnj } 5785065Swnj } 5794601Swnj 5805065Swnj /* 5815065Swnj * If the RST bit is set examine the state: 5825065Swnj * SYN_RECEIVED STATE: 5835065Swnj * If passive open, return to LISTEN state. 5845065Swnj * If active open, inform user that connection was refused. 5855065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 5865065Swnj * Inform user that connection was reset, and close tcb. 5875065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 5885065Swnj * Close the tcb. 5895065Swnj */ 5905065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 5915267Sroot 5925065Swnj case TCPS_SYN_RECEIVED: 59310394Ssam tp = tcp_drop(tp, ECONNREFUSED); 5945065Swnj goto drop; 5954601Swnj 5965065Swnj case TCPS_ESTABLISHED: 5975065Swnj case TCPS_FIN_WAIT_1: 5985065Swnj case TCPS_FIN_WAIT_2: 5995065Swnj case TCPS_CLOSE_WAIT: 60010394Ssam tp = tcp_drop(tp, ECONNRESET); 6015065Swnj goto drop; 6025065Swnj 6035065Swnj case TCPS_CLOSING: 6045065Swnj case TCPS_LAST_ACK: 6055065Swnj case TCPS_TIME_WAIT: 60610394Ssam tp = tcp_close(tp); 6075065Swnj goto drop; 6084601Swnj } 6094601Swnj 6104601Swnj /* 6115065Swnj * If a SYN is in the window, then this is an 6125065Swnj * error and we send an RST and drop the connection. 6134601Swnj */ 6145065Swnj if (tiflags & TH_SYN) { 61510394Ssam tp = tcp_drop(tp, ECONNRESET); 6165085Swnj goto dropwithreset; 6174601Swnj } 6184601Swnj 6194601Swnj /* 6205065Swnj * If the ACK bit is off we drop the segment and return. 6214601Swnj */ 6225085Swnj if ((tiflags & TH_ACK) == 0) 6235065Swnj goto drop; 6245065Swnj 6255065Swnj /* 6265065Swnj * Ack processing. 6275065Swnj */ 6284601Swnj switch (tp->t_state) { 6294601Swnj 6305065Swnj /* 6315065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 6325065Swnj * ESTABLISHED state and continue processing, othewise 6335065Swnj * send an RST. 6345065Swnj */ 6355065Swnj case TCPS_SYN_RECEIVED: 6365085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 6375231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 6385085Swnj goto dropwithreset; 6395244Sroot tp->snd_una++; /* SYN acked */ 6405357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 6415357Sroot tp->snd_nxt = tp->snd_una; 6425244Sroot tp->t_timer[TCPT_REXMT] = 0; 643*30525Skarels tcpstat.tcps_connects++; 6445085Swnj soisconnected(so); 6455085Swnj tp->t_state = TCPS_ESTABLISHED; 64617272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 6475162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 6485244Sroot tp->snd_wl1 = ti->ti_seq - 1; 6495085Swnj /* fall into ... */ 6504601Swnj 6515065Swnj /* 6525065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6535065Swnj * ACKs. If the ack is in the range 6545231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6555065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6565065Swnj * data from the retransmission queue. If this ACK reflects 6575065Swnj * more up to date window information we update our window information. 6585065Swnj */ 6595065Swnj case TCPS_ESTABLISHED: 6605065Swnj case TCPS_FIN_WAIT_1: 6615065Swnj case TCPS_FIN_WAIT_2: 6625065Swnj case TCPS_CLOSE_WAIT: 6635065Swnj case TCPS_CLOSING: 6645244Sroot case TCPS_LAST_ACK: 6655244Sroot case TCPS_TIME_WAIT: 6665085Swnj 667*30525Skarels if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) { 668*30525Skarels if (ti->ti_len == 0) 669*30525Skarels tcpstat.tcps_rcvdupack++; 6705065Swnj break; 671*30525Skarels } 672*30525Skarels if (SEQ_GT(ti->ti_ack, tp->snd_max)) { 673*30525Skarels tcpstat.tcps_rcvacktoomuch++; 6745065Swnj goto dropafterack; 675*30525Skarels } 6765085Swnj acked = ti->ti_ack - tp->snd_una; 677*30525Skarels tcpstat.tcps_rcvackpack++; 678*30525Skarels tcpstat.tcps_rcvackbyte += acked; 6795951Swnj 6805951Swnj /* 6815951Swnj * If transmit timer is running and timed sequence 6825951Swnj * number was acked, update smoothed round trip time. 6835951Swnj */ 6845951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 685*30525Skarels tcpstat.tcps_rttupdated++; 6865951Swnj if (tp->t_srtt == 0) 6875951Swnj tp->t_srtt = tp->t_rtt; 6885951Swnj else 6895951Swnj tp->t_srtt = 6905951Swnj tcp_alpha * tp->t_srtt + 6915951Swnj (1 - tcp_alpha) * tp->t_rtt; 6925951Swnj tp->t_rtt = 0; 6935951Swnj } 6945951Swnj 69526824Skarels /* 69626824Skarels * If all outstanding data is acked, stop retransmit 69726824Skarels * timer and remember to restart (more output or persist). 69826824Skarels * If there is more data to be acked, restart retransmit 69926824Skarels * timer. 70026824Skarels */ 70126824Skarels if (ti->ti_ack == tp->snd_max) { 7025244Sroot tp->t_timer[TCPT_REXMT] = 0; 70326824Skarels needoutput = 1; 70426824Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) { 7055244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 7065244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 7075300Sroot tp->t_rxtshift = 0; 7085085Swnj } 70917360Skarels /* 71017360Skarels * When new data is acked, open the congestion window a bit. 71117360Skarels */ 712*30525Skarels tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535); 7135307Sroot if (acked > so->so_snd.sb_cc) { 71415386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 71526386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 716*30525Skarels #define ourfinisacked (acked > 0) 7175307Sroot } else { 7186161Ssam sbdrop(&so->so_snd, acked); 7195307Sroot tp->snd_wnd -= acked; 7205307Sroot acked = 0; 7215307Sroot } 7226434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 7235300Sroot sowwakeup(so); 7245231Swnj tp->snd_una = ti->ti_ack; 7255357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 7265357Sroot tp->snd_nxt = tp->snd_una; 7275162Swnj 7284601Swnj switch (tp->t_state) { 7294601Swnj 7305065Swnj /* 7315065Swnj * In FIN_WAIT_1 STATE in addition to the processing 7325065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 7335085Swnj * then enter FIN_WAIT_2. 7345065Swnj */ 7355065Swnj case TCPS_FIN_WAIT_1: 7365896Swnj if (ourfinisacked) { 7375896Swnj /* 7385896Swnj * If we can't receive any more 7395896Swnj * data, then closing user can proceed. 74024816Skarels * Starting the timer is contrary to the 74124816Skarels * specification, but if we don't get a FIN 74224816Skarels * we'll hang forever. 7435896Swnj */ 74424816Skarels if (so->so_state & SS_CANTRCVMORE) { 7455896Swnj soisdisconnected(so); 74624816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 74724816Skarels } 7485085Swnj tp->t_state = TCPS_FIN_WAIT_2; 7495896Swnj } 7504601Swnj break; 7514601Swnj 7525065Swnj /* 7535065Swnj * In CLOSING STATE in addition to the processing for 7545065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 7555065Swnj * then enter the TIME-WAIT state, otherwise ignore 7565065Swnj * the segment. 7575065Swnj */ 7585065Swnj case TCPS_CLOSING: 7595244Sroot if (ourfinisacked) { 7605065Swnj tp->t_state = TCPS_TIME_WAIT; 7615244Sroot tcp_canceltimers(tp); 7625244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7635244Sroot soisdisconnected(so); 7645244Sroot } 7655244Sroot break; 7664601Swnj 7675065Swnj /* 7685085Swnj * The only thing that can arrive in LAST_ACK state 7695085Swnj * is an acknowledgment of our FIN. If our FIN is now 7705085Swnj * acknowledged, delete the TCB, enter the closed state 7715085Swnj * and return. 7725065Swnj */ 7735065Swnj case TCPS_LAST_ACK: 77410394Ssam if (ourfinisacked) 77510394Ssam tp = tcp_close(tp); 7765065Swnj goto drop; 7774601Swnj 7785065Swnj /* 7795065Swnj * In TIME_WAIT state the only thing that should arrive 7805065Swnj * is a retransmission of the remote FIN. Acknowledge 7815065Swnj * it and restart the finack timer. 7825065Swnj */ 7835065Swnj case TCPS_TIME_WAIT: 7845162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7855065Swnj goto dropafterack; 7864601Swnj } 7875085Swnj #undef ourfinisacked 7885085Swnj } 7894601Swnj 7905065Swnj step6: 7915065Swnj /* 7925244Sroot * Update window information. 79325939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 7945244Sroot */ 79525939Skarels if ((tiflags & TH_ACK) && 79625939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 7975391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 79825939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 799*30525Skarels /* keep track of pure window updates */ 800*30525Skarels if (ti->ti_len == 0 && 801*30525Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd) { 802*30525Skarels tcpstat.tcps_rcvwinupd++; 803*30525Skarels tcpstat.tcps_rcvdupack--; 804*30525Skarels } 8055244Sroot tp->snd_wnd = ti->ti_win; 8065244Sroot tp->snd_wl1 = ti->ti_seq; 8075244Sroot tp->snd_wl2 = ti->ti_ack; 80825260Skarels if (tp->snd_wnd > tp->max_sndwnd) 80925260Skarels tp->max_sndwnd = tp->snd_wnd; 81026824Skarels needoutput = 1; 81126824Skarels } 8125244Sroot 8135244Sroot /* 8145547Swnj * Process segments with URG. 8155065Swnj */ 8167267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 8177267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8185547Swnj /* 81925939Skarels * This is a kludge, but if we receive and accept 82013121Ssam * random urgent pointers, we'll crash in 82113121Ssam * soreceive. It's hard to imagine someone 82213121Ssam * actually wanting to send this much urgent data. 82312441Ssam */ 82425652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 82512441Ssam ti->ti_urp = 0; /* XXX */ 82612441Ssam tiflags &= ~TH_URG; /* XXX */ 82725939Skarels goto dodata; /* XXX */ 82812441Ssam } 82912441Ssam /* 8305547Swnj * If this segment advances the known urgent pointer, 8315547Swnj * then mark the data stream. This should not happen 8325547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 8335547Swnj * a FIN has been received from the remote side. 8345547Swnj * In these states we ignore the URG. 83527190Skarels * 83627190Skarels * According to RFC961 (Assigned Protocols), 83727190Skarels * the urgent pointer points to the last octet 83827190Skarels * of urgent data. We continue, however, 83927190Skarels * to consider it to indicate the first octet 84027190Skarels * of data past the urgent section 84127190Skarels * as the original spec states. 8425547Swnj */ 8435547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 8445547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 8455547Swnj so->so_oobmark = so->so_rcv.sb_cc + 8465547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 8475547Swnj if (so->so_oobmark == 0) 8485547Swnj so->so_state |= SS_RCVATMARK; 8498313Sroot sohasoutofband(so); 85024816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 8515440Swnj } 8525547Swnj /* 8535547Swnj * Remove out of band data so doesn't get presented to user. 8545547Swnj * This can happen independent of advancing the URG pointer, 8555547Swnj * but if two URG's are pending at once, some out-of-band 8565547Swnj * data may creep in... ick. 8575547Swnj */ 85827190Skarels if (ti->ti_urp <= ti->ti_len && 85927190Skarels (so->so_options & SO_OOBINLINE) == 0) 8605547Swnj tcp_pulloutofband(so, ti); 86125939Skarels } else 86225939Skarels /* 86325939Skarels * If no out of band data is expected, 86425939Skarels * pull receive urgent pointer along 86525939Skarels * with the receive window. 86625939Skarels */ 86725939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 86825939Skarels tp->rcv_up = tp->rcv_nxt; 86925939Skarels dodata: /* XXX */ 8704601Swnj 8714601Swnj /* 8725065Swnj * Process the segment text, merging it into the TCP sequencing queue, 8735065Swnj * and arranging for acknowledgment of receipt if necessary. 8745065Swnj * This process logically involves adjusting tp->rcv_wnd as data 8755065Swnj * is presented to the user (this happens in tcp_usrreq.c, 8765065Swnj * case PRU_RCVD). If a FIN has already been received on this 8775065Swnj * connection then we just ignore the text. 8784601Swnj */ 87917946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 88017946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 88124816Skarels TCP_REASS(tp, ti, m, so, tiflags); 8825440Swnj if (tcpnodelack == 0) 8835440Swnj tp->t_flags |= TF_DELACK; 8845440Swnj else 8855440Swnj tp->t_flags |= TF_ACKNOW; 88625260Skarels /* 88725260Skarels * Note the amount of data that peer has sent into 88825260Skarels * our window, in order to estimate the sender's 88925260Skarels * buffer size. 89025260Skarels */ 89125260Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv); 89225260Skarels if (len > tp->max_rcvd) 89325260Skarels tp->max_rcvd = len; 8945244Sroot } else { 8954924Swnj m_freem(m); 8965263Swnj tiflags &= ~TH_FIN; 8975244Sroot } 8984601Swnj 8994601Swnj /* 9005263Swnj * If FIN is received ACK the FIN and let the user know 9015263Swnj * that the connection is closing. 9024601Swnj */ 9035263Swnj if (tiflags & TH_FIN) { 9045244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 9055244Sroot socantrcvmore(so); 9065244Sroot tp->t_flags |= TF_ACKNOW; 9075244Sroot tp->rcv_nxt++; 9085244Sroot } 9095065Swnj switch (tp->t_state) { 9104601Swnj 9115065Swnj /* 9125065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 9135065Swnj * enter the CLOSE_WAIT state. 9144884Swnj */ 9155065Swnj case TCPS_SYN_RECEIVED: 9165065Swnj case TCPS_ESTABLISHED: 9175065Swnj tp->t_state = TCPS_CLOSE_WAIT; 9185065Swnj break; 9194884Swnj 9205065Swnj /* 9215085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 9225085Swnj * enter the CLOSING state. 9234884Swnj */ 9245065Swnj case TCPS_FIN_WAIT_1: 9255085Swnj tp->t_state = TCPS_CLOSING; 9265065Swnj break; 9274601Swnj 9285065Swnj /* 9295065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 9305065Swnj * starting the time-wait timer, turning off the other 9315065Swnj * standard timers. 9325065Swnj */ 9335065Swnj case TCPS_FIN_WAIT_2: 9345244Sroot tp->t_state = TCPS_TIME_WAIT; 9355074Swnj tcp_canceltimers(tp); 9365162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9375244Sroot soisdisconnected(so); 9385065Swnj break; 9395065Swnj 9404884Swnj /* 9415065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 9424884Swnj */ 9435065Swnj case TCPS_TIME_WAIT: 9445162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 9455065Swnj break; 9465085Swnj } 9474601Swnj } 9485267Sroot if (so->so_options & SO_DEBUG) 9495267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 9505085Swnj 9515085Swnj /* 9525085Swnj * Return any desired output. 9535085Swnj */ 95426824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 95525939Skarels (void) tcp_output(tp); 9565065Swnj return; 9575085Swnj 9585065Swnj dropafterack: 9595085Swnj /* 9606211Swnj * Generate an ACK dropping incoming segment if it occupies 9616211Swnj * sequence space, where the ACK reflects our state. 9625085Swnj */ 96326057Skarels if (tiflags & TH_RST) 9645085Swnj goto drop; 9656303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 9666303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 9675391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 9685231Swnj return; 9695085Swnj 9705085Swnj dropwithreset: 97111731Ssam if (om) { 9726161Ssam (void) m_free(om); 97311731Ssam om = 0; 97411731Ssam } 9755085Swnj /* 9765244Sroot * Generate a RST, dropping incoming segment. 9775085Swnj * Make ACK acceptable to originator of segment. 97825197Skarels * Don't bother to respond if destination was broadcast. 9795085Swnj */ 98025197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 9815085Swnj goto drop; 9825085Swnj if (tiflags & TH_ACK) 9835391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 9845085Swnj else { 9855085Swnj if (tiflags & TH_SYN) 9865085Swnj ti->ti_len++; 9876211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 9886211Swnj TH_RST|TH_ACK); 9895085Swnj } 99010769Ssam /* destroy temporarily created socket */ 99110769Ssam if (dropsocket) 99210769Ssam (void) soabort(so); 9935231Swnj return; 9945085Swnj 9955065Swnj drop: 99611730Ssam if (om) 99711730Ssam (void) m_free(om); 9985085Swnj /* 9995085Swnj * Drop space held by incoming segment and return. 10005085Swnj */ 10016303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 10026303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 10035065Swnj m_freem(m); 100410769Ssam /* destroy temporarily created socket */ 100510769Ssam if (dropsocket) 100610769Ssam (void) soabort(so); 10075267Sroot return; 10085065Swnj } 10095065Swnj 101017272Skarels tcp_dooptions(tp, om, ti) 10115440Swnj struct tcpcb *tp; 10125440Swnj struct mbuf *om; 101317272Skarels struct tcpiphdr *ti; 10145419Swnj { 10155440Swnj register u_char *cp; 10165440Swnj int opt, optlen, cnt; 10175419Swnj 10185440Swnj cp = mtod(om, u_char *); 10195440Swnj cnt = om->m_len; 10205440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 10215440Swnj opt = cp[0]; 10225440Swnj if (opt == TCPOPT_EOL) 10235440Swnj break; 10245440Swnj if (opt == TCPOPT_NOP) 10255440Swnj optlen = 1; 102612169Ssam else { 10275440Swnj optlen = cp[1]; 102812169Ssam if (optlen <= 0) 102912169Ssam break; 103012169Ssam } 10315440Swnj switch (opt) { 10325440Swnj 10335440Swnj default: 10345440Swnj break; 10355440Swnj 10365440Swnj case TCPOPT_MAXSEG: 10375440Swnj if (optlen != 4) 10385440Swnj continue; 103917272Skarels if (!(ti->ti_flags & TH_SYN)) 104017272Skarels continue; 10415440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 10426161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 104317272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 10445440Swnj break; 10455419Swnj } 10465419Swnj } 10476161Ssam (void) m_free(om); 10485419Swnj } 10495419Swnj 10505419Swnj /* 10515547Swnj * Pull out of band byte out of a segment so 10525547Swnj * it doesn't appear in the user's data queue. 10535547Swnj * It is still reflected in the segment length for 10545547Swnj * sequencing purposes. 10555547Swnj */ 10565547Swnj tcp_pulloutofband(so, ti) 10575547Swnj struct socket *so; 10585547Swnj struct tcpiphdr *ti; 10595547Swnj { 10605547Swnj register struct mbuf *m; 10616116Swnj int cnt = ti->ti_urp - 1; 10625547Swnj 10635547Swnj m = dtom(ti); 10645547Swnj while (cnt >= 0) { 10655547Swnj if (m->m_len > cnt) { 10665547Swnj char *cp = mtod(m, caddr_t) + cnt; 10675547Swnj struct tcpcb *tp = sototcpcb(so); 10685547Swnj 10695547Swnj tp->t_iobc = *cp; 10705547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 10716161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 10725547Swnj m->m_len--; 10735547Swnj return; 10745547Swnj } 10755547Swnj cnt -= m->m_len; 10765547Swnj m = m->m_next; 10775547Swnj if (m == 0) 10785547Swnj break; 10795547Swnj } 10805547Swnj panic("tcp_pulloutofband"); 10815547Swnj } 10825547Swnj 10835547Swnj /* 108417272Skarels * Determine a reasonable value for maxseg size. 108517272Skarels * If the route is known, use one that can be handled 108617272Skarels * on the given interface without forcing IP to fragment. 108723975Skarels * If bigger than a page (CLBYTES), round down to nearest pagesize 108817272Skarels * to utilize pagesize mbufs. 108917272Skarels * If interface pointer is unavailable, or the destination isn't local, 109023975Skarels * use a conservative size (512 or the default IP max size, but no more 109123975Skarels * than the mtu of the interface through which we route), 109217272Skarels * as we can't discover anything about intervening gateways or networks. 109317272Skarels * 109417272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 109517272Skarels */ 109617272Skarels tcp_mss(tp) 109723975Skarels register struct tcpcb *tp; 109817272Skarels { 109917272Skarels struct route *ro; 110017272Skarels struct ifnet *ifp; 110117272Skarels int mss; 110217272Skarels struct inpcb *inp; 110317272Skarels 110417272Skarels inp = tp->t_inpcb; 110517272Skarels ro = &inp->inp_route; 110617272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 110717272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 110817272Skarels /* No route yet, so try to acquire one */ 110917272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 111017272Skarels ro->ro_dst.sa_family = AF_INET; 111117272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 111217272Skarels inp->inp_faddr; 111317272Skarels rtalloc(ro); 111417272Skarels } 111517272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 111617316Skarels return (TCP_MSS); 111717272Skarels } 111817272Skarels 111917272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 112017272Skarels #if (CLBYTES & (CLBYTES - 1)) == 0 112117272Skarels if (mss > CLBYTES) 112217272Skarels mss &= ~(CLBYTES-1); 112317272Skarels #else 112417272Skarels if (mss > CLBYTES) 112517272Skarels mss = mss / CLBYTES * CLBYTES; 112617272Skarels #endif 112723975Skarels if (in_localaddr(inp->inp_faddr)) 112823975Skarels return (mss); 112917316Skarels return (MIN(mss, TCP_MSS)); 113017272Skarels } 1131