123190Smckusick /* 2*29149Smckusick * 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*29149Smckusick * @(#)tcp_input.c 7.1 (Berkeley) 06/05/86 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; \ 5424816Skarels sbappend(&(so)->so_rcv, (m)); \ 5524816Skarels sorwakeup(so); \ 5624816Skarels } else \ 5724816Skarels (flags) = tcp_reass((tp), (ti)); \ 5824816Skarels } 5924816Skarels 6024816Skarels tcp_reass(tp, ti) 6124816Skarels register struct tcpcb *tp; 6224816Skarels register struct tcpiphdr *ti; 6324816Skarels { 6424816Skarels register struct tcpiphdr *q; 6524816Skarels struct socket *so = tp->t_inpcb->inp_socket; 6624816Skarels struct mbuf *m; 6724816Skarels int flags; 6824816Skarels 6924816Skarels /* 7024816Skarels * Call with ti==0 after become established to 7124816Skarels * force pre-ESTABLISHED data up to user socket. 7224816Skarels */ 7324816Skarels if (ti == 0) 7424816Skarels goto present; 7524816Skarels 7624816Skarels /* 7724816Skarels * Find a segment which begins after this one does. 7824816Skarels */ 7924816Skarels for (q = tp->seg_next; q != (struct tcpiphdr *)tp; 8024816Skarels q = (struct tcpiphdr *)q->ti_next) 8124816Skarels if (SEQ_GT(q->ti_seq, ti->ti_seq)) 8224816Skarels break; 8324816Skarels 8424816Skarels /* 8524816Skarels * If there is a preceding segment, it may provide some of 8624816Skarels * our data already. If so, drop the data from the incoming 8724816Skarels * segment. If it provides all of our data, drop us. 8824816Skarels */ 8924816Skarels if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) { 9024816Skarels register int i; 9124816Skarels q = (struct tcpiphdr *)q->ti_prev; 9224816Skarels /* conversion to int (in i) handles seq wraparound */ 9324816Skarels i = q->ti_seq + q->ti_len - ti->ti_seq; 9424816Skarels if (i > 0) { 9524816Skarels if (i >= ti->ti_len) 9624816Skarels goto drop; 9724816Skarels m_adj(dtom(ti), i); 9824816Skarels ti->ti_len -= i; 9924816Skarels ti->ti_seq += i; 10024816Skarels } 10124816Skarels q = (struct tcpiphdr *)(q->ti_next); 10224816Skarels } 10324816Skarels 10424816Skarels /* 10524816Skarels * While we overlap succeeding segments trim them or, 10624816Skarels * if they are completely covered, dequeue them. 10724816Skarels */ 10824816Skarels while (q != (struct tcpiphdr *)tp) { 10924816Skarels register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq; 11024816Skarels if (i <= 0) 11124816Skarels break; 11224816Skarels if (i < q->ti_len) { 11324816Skarels q->ti_seq += i; 11424816Skarels q->ti_len -= i; 11524816Skarels m_adj(dtom(q), i); 11624816Skarels break; 11724816Skarels } 11824816Skarels q = (struct tcpiphdr *)q->ti_next; 11924816Skarels m = dtom(q->ti_prev); 12024816Skarels remque(q->ti_prev); 12124816Skarels m_freem(m); 12224816Skarels } 12324816Skarels 12424816Skarels /* 12524816Skarels * Stick new segment in its place. 12624816Skarels */ 12724816Skarels insque(ti, q->ti_prev); 12824816Skarels 12924816Skarels present: 13024816Skarels /* 13124816Skarels * Present data to user, advancing rcv_nxt through 13224816Skarels * completed sequence space. 13324816Skarels */ 13424816Skarels if (TCPS_HAVERCVDSYN(tp->t_state) == 0) 13524816Skarels return (0); 13624816Skarels ti = tp->seg_next; 13724816Skarels if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt) 13824816Skarels return (0); 13924816Skarels if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len) 14024816Skarels return (0); 14124816Skarels do { 14224816Skarels tp->rcv_nxt += ti->ti_len; 14324816Skarels flags = ti->ti_flags & TH_FIN; 14424816Skarels remque(ti); 14524816Skarels m = dtom(ti); 14624816Skarels ti = (struct tcpiphdr *)ti->ti_next; 14724816Skarels if (so->so_state & SS_CANTRCVMORE) 14824816Skarels m_freem(m); 14924816Skarels else 15024816Skarels sbappend(&so->so_rcv, m); 15124816Skarels } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt); 15224816Skarels sorwakeup(so); 15324816Skarels return (flags); 15424816Skarels drop: 15524816Skarels m_freem(dtom(ti)); 15624816Skarels return (0); 15724816Skarels } 15824816Skarels 15924816Skarels /* 1605065Swnj * TCP input routine, follows pages 65-76 of the 1615065Swnj * protocol specification dated September, 1981 very closely. 1625065Swnj */ 1634924Swnj tcp_input(m0) 1644924Swnj struct mbuf *m0; 1654601Swnj { 1664924Swnj register struct tcpiphdr *ti; 1674924Swnj struct inpcb *inp; 1684924Swnj register struct mbuf *m; 1695440Swnj struct mbuf *om = 0; 1704924Swnj int len, tlen, off; 1715391Swnj register struct tcpcb *tp = 0; 1724924Swnj register int tiflags; 1734803Swnj struct socket *so; 17426824Skarels int todrop, acked, needoutput = 0; 1755267Sroot short ostate; 1766028Sroot struct in_addr laddr; 17710769Ssam int dropsocket = 0; 1784924Swnj 1794924Swnj /* 1805244Sroot * Get IP and TCP header together in first mbuf. 1815244Sroot * Note: IP leaves IP header in first mbuf. 1824924Swnj */ 1834924Swnj m = m0; 1845020Sroot ti = mtod(m, struct tcpiphdr *); 1855244Sroot if (((struct ip *)ti)->ip_hl > (sizeof (struct ip) >> 2)) 1865208Swnj ip_stripoptions((struct ip *)ti, (struct mbuf *)0); 1875307Sroot if (m->m_off > MMAXOFF || m->m_len < sizeof (struct tcpiphdr)) { 1885307Sroot if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) { 1895085Swnj tcpstat.tcps_hdrops++; 1905307Sroot return; 1915085Swnj } 1925085Swnj ti = mtod(m, struct tcpiphdr *); 1935085Swnj } 1944601Swnj 1954601Swnj /* 1965244Sroot * Checksum extended TCP header and data. 1974601Swnj */ 1984924Swnj tlen = ((struct ip *)ti)->ip_len; 1994924Swnj len = sizeof (struct ip) + tlen; 2004679Swnj if (tcpcksum) { 2014924Swnj ti->ti_next = ti->ti_prev = 0; 2024924Swnj ti->ti_x1 = 0; 2035223Swnj ti->ti_len = (u_short)tlen; 2046161Ssam ti->ti_len = htons((u_short)ti->ti_len); 2055231Swnj if (ti->ti_sum = in_cksum(m, len)) { 20611830Ssam if (tcpprintfs) 20711830Ssam printf("tcp sum: src %x\n", ti->ti_src); 2084924Swnj tcpstat.tcps_badsum++; 2095085Swnj goto drop; 2104601Swnj } 2114601Swnj } 2124601Swnj 2134601Swnj /* 2145244Sroot * Check that TCP offset makes sense, 2155440Swnj * pull out TCP options and adjust length. 2164601Swnj */ 2174924Swnj off = ti->ti_off << 2; 2185231Swnj if (off < sizeof (struct tcphdr) || off > tlen) { 21911830Ssam if (tcpprintfs) 22011830Ssam printf("tcp off: src %x off %d\n", ti->ti_src, off); 2214924Swnj tcpstat.tcps_badoff++; 2225085Swnj goto drop; 2234924Swnj } 2246211Swnj tlen -= off; 2256211Swnj ti->ti_len = tlen; 2265440Swnj if (off > sizeof (struct tcphdr)) { 22724816Skarels if (m->m_len < sizeof(struct ip) + off) { 22824816Skarels if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) { 22924816Skarels tcpstat.tcps_hdrops++; 23024816Skarels return; 23124816Skarels } 23224816Skarels ti = mtod(m, struct tcpiphdr *); 2335440Swnj } 2349642Ssam om = m_get(M_DONTWAIT, MT_DATA); 2355440Swnj if (om == 0) 2365440Swnj goto drop; 2375440Swnj om->m_len = off - sizeof (struct tcphdr); 2385440Swnj { caddr_t op = mtod(m, caddr_t) + sizeof (struct tcpiphdr); 2396161Ssam bcopy(op, mtod(om, caddr_t), (unsigned)om->m_len); 2405440Swnj m->m_len -= om->m_len; 2416161Ssam bcopy(op+om->m_len, op, 2426161Ssam (unsigned)(m->m_len-sizeof (struct tcpiphdr))); 2435440Swnj } 2445440Swnj } 2455065Swnj tiflags = ti->ti_flags; 2464924Swnj 2476093Sroot /* 24825729Smckusick * Drop TCP and IP headers; TCP options were dropped above. 2496093Sroot */ 25025729Smckusick m->m_off += sizeof(struct tcpiphdr); 25125729Smckusick m->m_len -= sizeof(struct tcpiphdr); 2526093Sroot 2534924Swnj /* 2545244Sroot * Convert TCP protocol specific fields to host format. 2555085Swnj */ 2565085Swnj ti->ti_seq = ntohl(ti->ti_seq); 2575085Swnj ti->ti_ack = ntohl(ti->ti_ack); 2585085Swnj ti->ti_win = ntohs(ti->ti_win); 2595085Swnj ti->ti_urp = ntohs(ti->ti_urp); 2605085Swnj 2615085Swnj /* 2628271Sroot * Locate pcb for segment. 2634924Swnj */ 2645065Swnj inp = in_pcblookup 2656028Sroot (&tcb, ti->ti_src, ti->ti_sport, ti->ti_dst, ti->ti_dport, 2666028Sroot INPLOOKUP_WILDCARD); 2675065Swnj 2685065Swnj /* 2695065Swnj * If the state is CLOSED (i.e., TCB does not exist) then 2705244Sroot * all data in the incoming segment is discarded. 2715065Swnj */ 2725300Sroot if (inp == 0) 2735085Swnj goto dropwithreset; 2745065Swnj tp = intotcpcb(inp); 2755300Sroot if (tp == 0) 2765085Swnj goto dropwithreset; 2775109Swnj so = inp->inp_socket; 2785267Sroot if (so->so_options & SO_DEBUG) { 2795267Sroot ostate = tp->t_state; 2805267Sroot tcp_saveti = *ti; 2815267Sroot } 2827510Sroot if (so->so_options & SO_ACCEPTCONN) { 2837510Sroot so = sonewconn(so); 2847510Sroot if (so == 0) 2857510Sroot goto drop; 28610769Ssam /* 28710769Ssam * This is ugly, but .... 28810769Ssam * 28910769Ssam * Mark socket as temporary until we're 29010769Ssam * committed to keeping it. The code at 29110769Ssam * ``drop'' and ``dropwithreset'' check the 29210769Ssam * flag dropsocket to see if the temporary 29310769Ssam * socket created here should be discarded. 29410769Ssam * We mark the socket as discardable until 29510769Ssam * we're committed to it below in TCPS_LISTEN. 29610769Ssam */ 29710769Ssam dropsocket++; 2987510Sroot inp = (struct inpcb *)so->so_pcb; 2997510Sroot inp->inp_laddr = ti->ti_dst; 3007510Sroot inp->inp_lport = ti->ti_dport; 30124816Skarels inp->inp_options = ip_srcroute(); 3027510Sroot tp = intotcpcb(inp); 3037510Sroot tp->t_state = TCPS_LISTEN; 3047510Sroot } 3054601Swnj 3064601Swnj /* 3075162Swnj * Segment received on connection. 3085162Swnj * Reset idle time and keep-alive timer. 3095162Swnj */ 3105162Swnj tp->t_idle = 0; 3115162Swnj tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 3125162Swnj 3135162Swnj /* 31417272Skarels * Process options if not in LISTEN state, 31517272Skarels * else do it below (after getting remote address). 3165440Swnj */ 31717272Skarels if (om && tp->t_state != TCPS_LISTEN) { 31817272Skarels tcp_dooptions(tp, om, ti); 3195440Swnj om = 0; 3205440Swnj } 3215440Swnj 3225440Swnj /* 3235085Swnj * Calculate amount of space in receive window, 3245085Swnj * and then do TCP input processing. 32524816Skarels * Receive window is amount of space in rcv queue, 32624816Skarels * but not less than advertised window. 3274601Swnj */ 32825939Skarels { int win; 3294601Swnj 33025939Skarels win = sbspace(&so->so_rcv); 33125939Skarels if (win < 0) 33225939Skarels win = 0; 33325939Skarels tp->rcv_wnd = MAX(win, (int)(tp->rcv_adv - tp->rcv_nxt)); 33425939Skarels } 33525939Skarels 3364601Swnj switch (tp->t_state) { 3374601Swnj 3385065Swnj /* 3395065Swnj * If the state is LISTEN then ignore segment if it contains an RST. 3405065Swnj * If the segment contains an ACK then it is bad and send a RST. 3415065Swnj * If it does not contain a SYN then it is not interesting; drop it. 34225197Skarels * Don't bother responding if the destination was a broadcast. 3435085Swnj * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial 3445065Swnj * tp->iss, and send a segment: 3455085Swnj * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> 3465065Swnj * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. 3475065Swnj * Fill in remote peer address fields if not previously specified. 3485065Swnj * Enter SYN_RECEIVED state, and process any other fields of this 3495244Sroot * segment in this state. 3505065Swnj */ 3518271Sroot case TCPS_LISTEN: { 35210145Ssam struct mbuf *am; 3538271Sroot register struct sockaddr_in *sin; 3548271Sroot 3555065Swnj if (tiflags & TH_RST) 3565065Swnj goto drop; 3575300Sroot if (tiflags & TH_ACK) 3585085Swnj goto dropwithreset; 3595300Sroot if ((tiflags & TH_SYN) == 0) 3605065Swnj goto drop; 36125197Skarels if (in_broadcast(ti->ti_dst)) 36225197Skarels goto drop; 36310145Ssam am = m_get(M_DONTWAIT, MT_SONAME); 36410145Ssam if (am == NULL) 36510145Ssam goto drop; 36610145Ssam am->m_len = sizeof (struct sockaddr_in); 3678599Sroot sin = mtod(am, struct sockaddr_in *); 3688271Sroot sin->sin_family = AF_INET; 3698271Sroot sin->sin_addr = ti->ti_src; 3708271Sroot sin->sin_port = ti->ti_sport; 3716028Sroot laddr = inp->inp_laddr; 37210145Ssam if (inp->inp_laddr.s_addr == INADDR_ANY) 3736028Sroot inp->inp_laddr = ti->ti_dst; 3748599Sroot if (in_pcbconnect(inp, am)) { 3756028Sroot inp->inp_laddr = laddr; 3768716Sroot (void) m_free(am); 3775244Sroot goto drop; 3786028Sroot } 3798716Sroot (void) m_free(am); 3805244Sroot tp->t_template = tcp_template(tp); 3815244Sroot if (tp->t_template == 0) { 38226386Skarels tp = tcp_drop(tp, ENOBUFS); 38317264Skarels dropsocket = 0; /* socket is already gone */ 3845244Sroot goto drop; 3855244Sroot } 38617272Skarels if (om) { 38717272Skarels tcp_dooptions(tp, om, ti); 38817272Skarels om = 0; 38917272Skarels } 3905085Swnj tp->iss = tcp_iss; tcp_iss += TCP_ISSINCR/2; 3915065Swnj tp->irs = ti->ti_seq; 3925085Swnj tcp_sendseqinit(tp); 3935085Swnj tcp_rcvseqinit(tp); 39425939Skarels tp->t_flags |= TF_ACKNOW; 3955065Swnj tp->t_state = TCPS_SYN_RECEIVED; 3965244Sroot tp->t_timer[TCPT_KEEP] = TCPTV_KEEP; 39710769Ssam dropsocket = 0; /* committed to socket */ 3985085Swnj goto trimthenstep6; 3998271Sroot } 4004601Swnj 4015065Swnj /* 4025065Swnj * If the state is SYN_SENT: 4035065Swnj * if seg contains an ACK, but not for our SYN, drop the input. 4045065Swnj * if seg contains a RST, then drop the connection. 4055065Swnj * if seg does not contain SYN, then drop it. 4065065Swnj * Otherwise this is an acceptable SYN segment 4075065Swnj * initialize tp->rcv_nxt and tp->irs 4085065Swnj * if seg contains ack then advance tp->snd_una 4095065Swnj * if SYN has been acked change to ESTABLISHED else SYN_RCVD state 4105065Swnj * arrange for segment to be acked (eventually) 4115065Swnj * continue processing rest of data/controls, beginning with URG 4125065Swnj */ 4135065Swnj case TCPS_SYN_SENT: 4145065Swnj if ((tiflags & TH_ACK) && 41524816Skarels (SEQ_LEQ(ti->ti_ack, tp->iss) || 4165231Swnj SEQ_GT(ti->ti_ack, tp->snd_max))) 4175085Swnj goto dropwithreset; 4185065Swnj if (tiflags & TH_RST) { 41910394Ssam if (tiflags & TH_ACK) 42010394Ssam tp = tcp_drop(tp, ECONNREFUSED); 4215065Swnj goto drop; 4224601Swnj } 4235065Swnj if ((tiflags & TH_SYN) == 0) 4245065Swnj goto drop; 4255231Swnj tp->snd_una = ti->ti_ack; 4265357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 4275357Sroot tp->snd_nxt = tp->snd_una; 4285244Sroot tp->t_timer[TCPT_REXMT] = 0; 4295065Swnj tp->irs = ti->ti_seq; 4305085Swnj tcp_rcvseqinit(tp); 4315085Swnj tp->t_flags |= TF_ACKNOW; 4325162Swnj if (SEQ_GT(tp->snd_una, tp->iss)) { 4335244Sroot soisconnected(so); 4345065Swnj tp->t_state = TCPS_ESTABLISHED; 43517272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 4365162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 4375162Swnj } else 4385085Swnj tp->t_state = TCPS_SYN_RECEIVED; 4395085Swnj goto trimthenstep6; 4405085Swnj 4415085Swnj trimthenstep6: 4425085Swnj /* 4435231Swnj * Advance ti->ti_seq to correspond to first data byte. 4445085Swnj * If data, trim to stay within window, 4455085Swnj * dropping FIN if necessary. 4465085Swnj */ 4475231Swnj ti->ti_seq++; 4485085Swnj if (ti->ti_len > tp->rcv_wnd) { 4495085Swnj todrop = ti->ti_len - tp->rcv_wnd; 4505085Swnj m_adj(m, -todrop); 4515085Swnj ti->ti_len = tp->rcv_wnd; 45225939Skarels tiflags &= ~TH_FIN; 4535065Swnj } 4545263Swnj tp->snd_wl1 = ti->ti_seq - 1; 45525939Skarels tp->rcv_up = ti->ti_seq; 4565085Swnj goto step6; 4575065Swnj } 4584601Swnj 4595065Swnj /* 46016222Skarels * If data is received on a connection after the 46116222Skarels * user processes are gone, then RST the other end. 46216222Skarels */ 46316222Skarels if ((so->so_state & SS_NOFDREF) && tp->t_state > TCPS_CLOSE_WAIT && 46416222Skarels ti->ti_len) { 46516222Skarels tp = tcp_close(tp); 46616222Skarels goto dropwithreset; 46716222Skarels } 46816222Skarels 46916222Skarels /* 4705065Swnj * States other than LISTEN or SYN_SENT. 4715065Swnj * First check that at least some bytes of segment are within 4725065Swnj * receive window. 4735065Swnj */ 4745065Swnj if (tp->rcv_wnd == 0) { 4755065Swnj /* 4765065Swnj * If window is closed can only take segments at 4775231Swnj * window edge, and have to drop data and PUSH from 4785065Swnj * incoming segments. 4795065Swnj */ 4805300Sroot if (tp->rcv_nxt != ti->ti_seq) 4815065Swnj goto dropafterack; 4825085Swnj if (ti->ti_len > 0) { 4835690Swnj m_adj(m, ti->ti_len); 4845085Swnj ti->ti_len = 0; 48525939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 4865065Swnj } 4875065Swnj } else { 4885065Swnj /* 4895231Swnj * If segment begins before rcv_nxt, drop leading 4905065Swnj * data (and SYN); if nothing left, just ack. 4915065Swnj */ 4925690Swnj todrop = tp->rcv_nxt - ti->ti_seq; 4935690Swnj if (todrop > 0) { 4945085Swnj if (tiflags & TH_SYN) { 4955300Sroot tiflags &= ~TH_SYN; 4965085Swnj ti->ti_seq++; 4975085Swnj if (ti->ti_urp > 1) 4985085Swnj ti->ti_urp--; 4995085Swnj else 5005085Swnj tiflags &= ~TH_URG; 5015085Swnj todrop--; 5025085Swnj } 5036211Swnj if (todrop > ti->ti_len || 5046211Swnj todrop == ti->ti_len && (tiflags&TH_FIN) == 0) 5055065Swnj goto dropafterack; 5065065Swnj m_adj(m, todrop); 5075065Swnj ti->ti_seq += todrop; 5085065Swnj ti->ti_len -= todrop; 5095085Swnj if (ti->ti_urp > todrop) 5105085Swnj ti->ti_urp -= todrop; 5115085Swnj else { 5125085Swnj tiflags &= ~TH_URG; 5135690Swnj ti->ti_urp = 0; 5145085Swnj } 5155065Swnj } 5165065Swnj /* 5175065Swnj * If segment ends after window, drop trailing data 5185085Swnj * (and PUSH and FIN); if nothing left, just ACK. 5195065Swnj */ 5205690Swnj todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd); 5215690Swnj if (todrop > 0) { 5226211Swnj if (todrop >= ti->ti_len) 5235065Swnj goto dropafterack; 5245065Swnj m_adj(m, -todrop); 5255065Swnj ti->ti_len -= todrop; 52625939Skarels tiflags &= ~(TH_PUSH|TH_FIN); 5275065Swnj } 5285065Swnj } 5294601Swnj 5305065Swnj /* 5315065Swnj * If the RST bit is set examine the state: 5325065Swnj * SYN_RECEIVED STATE: 5335065Swnj * If passive open, return to LISTEN state. 5345065Swnj * If active open, inform user that connection was refused. 5355065Swnj * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: 5365065Swnj * Inform user that connection was reset, and close tcb. 5375065Swnj * CLOSING, LAST_ACK, TIME_WAIT STATES 5385065Swnj * Close the tcb. 5395065Swnj */ 5405065Swnj if (tiflags&TH_RST) switch (tp->t_state) { 5415267Sroot 5425065Swnj case TCPS_SYN_RECEIVED: 54310394Ssam tp = tcp_drop(tp, ECONNREFUSED); 5445065Swnj goto drop; 5454601Swnj 5465065Swnj case TCPS_ESTABLISHED: 5475065Swnj case TCPS_FIN_WAIT_1: 5485065Swnj case TCPS_FIN_WAIT_2: 5495065Swnj case TCPS_CLOSE_WAIT: 55010394Ssam tp = tcp_drop(tp, ECONNRESET); 5515065Swnj goto drop; 5525065Swnj 5535065Swnj case TCPS_CLOSING: 5545065Swnj case TCPS_LAST_ACK: 5555065Swnj case TCPS_TIME_WAIT: 55610394Ssam tp = tcp_close(tp); 5575065Swnj goto drop; 5584601Swnj } 5594601Swnj 5604601Swnj /* 5615065Swnj * If a SYN is in the window, then this is an 5625065Swnj * error and we send an RST and drop the connection. 5634601Swnj */ 5645065Swnj if (tiflags & TH_SYN) { 56510394Ssam tp = tcp_drop(tp, ECONNRESET); 5665085Swnj goto dropwithreset; 5674601Swnj } 5684601Swnj 5694601Swnj /* 5705065Swnj * If the ACK bit is off we drop the segment and return. 5714601Swnj */ 5725085Swnj if ((tiflags & TH_ACK) == 0) 5735065Swnj goto drop; 5745065Swnj 5755065Swnj /* 5765065Swnj * Ack processing. 5775065Swnj */ 5784601Swnj switch (tp->t_state) { 5794601Swnj 5805065Swnj /* 5815065Swnj * In SYN_RECEIVED state if the ack ACKs our SYN then enter 5825065Swnj * ESTABLISHED state and continue processing, othewise 5835065Swnj * send an RST. 5845065Swnj */ 5855065Swnj case TCPS_SYN_RECEIVED: 5865085Swnj if (SEQ_GT(tp->snd_una, ti->ti_ack) || 5875231Swnj SEQ_GT(ti->ti_ack, tp->snd_max)) 5885085Swnj goto dropwithreset; 5895244Sroot tp->snd_una++; /* SYN acked */ 5905357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 5915357Sroot tp->snd_nxt = tp->snd_una; 5925244Sroot tp->t_timer[TCPT_REXMT] = 0; 5935085Swnj soisconnected(so); 5945085Swnj tp->t_state = TCPS_ESTABLISHED; 59517272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 5965162Swnj (void) tcp_reass(tp, (struct tcpiphdr *)0); 5975244Sroot tp->snd_wl1 = ti->ti_seq - 1; 5985085Swnj /* fall into ... */ 5994601Swnj 6005065Swnj /* 6015065Swnj * In ESTABLISHED state: drop duplicate ACKs; ACK out of range 6025065Swnj * ACKs. If the ack is in the range 6035231Swnj * tp->snd_una < ti->ti_ack <= tp->snd_max 6045065Swnj * then advance tp->snd_una to ti->ti_ack and drop 6055065Swnj * data from the retransmission queue. If this ACK reflects 6065065Swnj * more up to date window information we update our window information. 6075065Swnj */ 6085065Swnj case TCPS_ESTABLISHED: 6095065Swnj case TCPS_FIN_WAIT_1: 6105065Swnj case TCPS_FIN_WAIT_2: 6115065Swnj case TCPS_CLOSE_WAIT: 6125065Swnj case TCPS_CLOSING: 6135244Sroot case TCPS_LAST_ACK: 6145244Sroot case TCPS_TIME_WAIT: 6155085Swnj #define ourfinisacked (acked > 0) 6165085Swnj 6175244Sroot if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) 6185065Swnj break; 6195300Sroot if (SEQ_GT(ti->ti_ack, tp->snd_max)) 6205065Swnj goto dropafterack; 6215085Swnj acked = ti->ti_ack - tp->snd_una; 6225951Swnj 6235951Swnj /* 6245951Swnj * If transmit timer is running and timed sequence 6255951Swnj * number was acked, update smoothed round trip time. 6265951Swnj */ 6275951Swnj if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq)) { 6285951Swnj if (tp->t_srtt == 0) 6295951Swnj tp->t_srtt = tp->t_rtt; 6305951Swnj else 6315951Swnj tp->t_srtt = 6325951Swnj tcp_alpha * tp->t_srtt + 6335951Swnj (1 - tcp_alpha) * tp->t_rtt; 6345951Swnj tp->t_rtt = 0; 6355951Swnj } 6365951Swnj 63726824Skarels /* 63826824Skarels * If all outstanding data is acked, stop retransmit 63926824Skarels * timer and remember to restart (more output or persist). 64026824Skarels * If there is more data to be acked, restart retransmit 64126824Skarels * timer. 64226824Skarels */ 64326824Skarels if (ti->ti_ack == tp->snd_max) { 6445244Sroot tp->t_timer[TCPT_REXMT] = 0; 64526824Skarels needoutput = 1; 64626824Skarels } else if (tp->t_timer[TCPT_PERSIST] == 0) { 6475244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 6485244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 6495300Sroot tp->t_rxtshift = 0; 6505085Swnj } 65117360Skarels /* 65217360Skarels * When new data is acked, open the congestion window a bit. 65317360Skarels */ 65417360Skarels if (acked > 0) 65517360Skarels tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535); 6565307Sroot if (acked > so->so_snd.sb_cc) { 65715386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 65826386Skarels sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); 6595307Sroot } else { 6606161Ssam sbdrop(&so->so_snd, acked); 6615307Sroot tp->snd_wnd -= acked; 6625307Sroot acked = 0; 6635307Sroot } 6646434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 6655300Sroot sowwakeup(so); 6665231Swnj tp->snd_una = ti->ti_ack; 6675357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 6685357Sroot tp->snd_nxt = tp->snd_una; 6695162Swnj 6704601Swnj switch (tp->t_state) { 6714601Swnj 6725065Swnj /* 6735065Swnj * In FIN_WAIT_1 STATE in addition to the processing 6745065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 6755085Swnj * then enter FIN_WAIT_2. 6765065Swnj */ 6775065Swnj case TCPS_FIN_WAIT_1: 6785896Swnj if (ourfinisacked) { 6795896Swnj /* 6805896Swnj * If we can't receive any more 6815896Swnj * data, then closing user can proceed. 68224816Skarels * Starting the timer is contrary to the 68324816Skarels * specification, but if we don't get a FIN 68424816Skarels * we'll hang forever. 6855896Swnj */ 68624816Skarels if (so->so_state & SS_CANTRCVMORE) { 6875896Swnj soisdisconnected(so); 68824816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 68924816Skarels } 6905085Swnj tp->t_state = TCPS_FIN_WAIT_2; 6915896Swnj } 6924601Swnj break; 6934601Swnj 6945065Swnj /* 6955065Swnj * In CLOSING STATE in addition to the processing for 6965065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 6975065Swnj * then enter the TIME-WAIT state, otherwise ignore 6985065Swnj * the segment. 6995065Swnj */ 7005065Swnj case TCPS_CLOSING: 7015244Sroot if (ourfinisacked) { 7025065Swnj tp->t_state = TCPS_TIME_WAIT; 7035244Sroot tcp_canceltimers(tp); 7045244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7055244Sroot soisdisconnected(so); 7065244Sroot } 7075244Sroot break; 7084601Swnj 7095065Swnj /* 7105085Swnj * The only thing that can arrive in LAST_ACK state 7115085Swnj * is an acknowledgment of our FIN. If our FIN is now 7125085Swnj * acknowledged, delete the TCB, enter the closed state 7135085Swnj * and return. 7145065Swnj */ 7155065Swnj case TCPS_LAST_ACK: 71610394Ssam if (ourfinisacked) 71710394Ssam tp = tcp_close(tp); 7185065Swnj goto drop; 7194601Swnj 7205065Swnj /* 7215065Swnj * In TIME_WAIT state the only thing that should arrive 7225065Swnj * is a retransmission of the remote FIN. Acknowledge 7235065Swnj * it and restart the finack timer. 7245065Swnj */ 7255065Swnj case TCPS_TIME_WAIT: 7265162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7275065Swnj goto dropafterack; 7284601Swnj } 7295085Swnj #undef ourfinisacked 7305085Swnj } 7314601Swnj 7325065Swnj step6: 7335065Swnj /* 7345244Sroot * Update window information. 73525939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 7365244Sroot */ 73725939Skarels if ((tiflags & TH_ACK) && 73825939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 7395391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 74025939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 7415244Sroot tp->snd_wnd = ti->ti_win; 7425244Sroot tp->snd_wl1 = ti->ti_seq; 7435244Sroot tp->snd_wl2 = ti->ti_ack; 74425260Skarels if (tp->snd_wnd > tp->max_sndwnd) 74525260Skarels tp->max_sndwnd = tp->snd_wnd; 74626824Skarels needoutput = 1; 74726824Skarels } 7485244Sroot 7495244Sroot /* 7505547Swnj * Process segments with URG. 7515065Swnj */ 7527267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 7537267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 7545547Swnj /* 75525939Skarels * This is a kludge, but if we receive and accept 75613121Ssam * random urgent pointers, we'll crash in 75713121Ssam * soreceive. It's hard to imagine someone 75813121Ssam * actually wanting to send this much urgent data. 75912441Ssam */ 76025652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 76112441Ssam ti->ti_urp = 0; /* XXX */ 76212441Ssam tiflags &= ~TH_URG; /* XXX */ 76325939Skarels goto dodata; /* XXX */ 76412441Ssam } 76512441Ssam /* 7665547Swnj * If this segment advances the known urgent pointer, 7675547Swnj * then mark the data stream. This should not happen 7685547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 7695547Swnj * a FIN has been received from the remote side. 7705547Swnj * In these states we ignore the URG. 77127190Skarels * 77227190Skarels * According to RFC961 (Assigned Protocols), 77327190Skarels * the urgent pointer points to the last octet 77427190Skarels * of urgent data. We continue, however, 77527190Skarels * to consider it to indicate the first octet 77627190Skarels * of data past the urgent section 77727190Skarels * as the original spec states. 7785547Swnj */ 7795547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 7805547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 7815547Swnj so->so_oobmark = so->so_rcv.sb_cc + 7825547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 7835547Swnj if (so->so_oobmark == 0) 7845547Swnj so->so_state |= SS_RCVATMARK; 7858313Sroot sohasoutofband(so); 78624816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 7875440Swnj } 7885547Swnj /* 7895547Swnj * Remove out of band data so doesn't get presented to user. 7905547Swnj * This can happen independent of advancing the URG pointer, 7915547Swnj * but if two URG's are pending at once, some out-of-band 7925547Swnj * data may creep in... ick. 7935547Swnj */ 79427190Skarels if (ti->ti_urp <= ti->ti_len && 79527190Skarels (so->so_options & SO_OOBINLINE) == 0) 7965547Swnj tcp_pulloutofband(so, ti); 79725939Skarels } else 79825939Skarels /* 79925939Skarels * If no out of band data is expected, 80025939Skarels * pull receive urgent pointer along 80125939Skarels * with the receive window. 80225939Skarels */ 80325939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 80425939Skarels tp->rcv_up = tp->rcv_nxt; 80525939Skarels dodata: /* XXX */ 8064601Swnj 8074601Swnj /* 8085065Swnj * Process the segment text, merging it into the TCP sequencing queue, 8095065Swnj * and arranging for acknowledgment of receipt if necessary. 8105065Swnj * This process logically involves adjusting tp->rcv_wnd as data 8115065Swnj * is presented to the user (this happens in tcp_usrreq.c, 8125065Swnj * case PRU_RCVD). If a FIN has already been received on this 8135065Swnj * connection then we just ignore the text. 8144601Swnj */ 81517946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 81617946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 81724816Skarels TCP_REASS(tp, ti, m, so, tiflags); 8185440Swnj if (tcpnodelack == 0) 8195440Swnj tp->t_flags |= TF_DELACK; 8205440Swnj else 8215440Swnj tp->t_flags |= TF_ACKNOW; 82225260Skarels /* 82325260Skarels * Note the amount of data that peer has sent into 82425260Skarels * our window, in order to estimate the sender's 82525260Skarels * buffer size. 82625260Skarels */ 82725260Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv); 82825260Skarels if (len > tp->max_rcvd) 82925260Skarels tp->max_rcvd = len; 8305244Sroot } else { 8314924Swnj m_freem(m); 8325263Swnj tiflags &= ~TH_FIN; 8335244Sroot } 8344601Swnj 8354601Swnj /* 8365263Swnj * If FIN is received ACK the FIN and let the user know 8375263Swnj * that the connection is closing. 8384601Swnj */ 8395263Swnj if (tiflags & TH_FIN) { 8405244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8415244Sroot socantrcvmore(so); 8425244Sroot tp->t_flags |= TF_ACKNOW; 8435244Sroot tp->rcv_nxt++; 8445244Sroot } 8455065Swnj switch (tp->t_state) { 8464601Swnj 8475065Swnj /* 8485065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 8495065Swnj * enter the CLOSE_WAIT state. 8504884Swnj */ 8515065Swnj case TCPS_SYN_RECEIVED: 8525065Swnj case TCPS_ESTABLISHED: 8535065Swnj tp->t_state = TCPS_CLOSE_WAIT; 8545065Swnj break; 8554884Swnj 8565065Swnj /* 8575085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 8585085Swnj * enter the CLOSING state. 8594884Swnj */ 8605065Swnj case TCPS_FIN_WAIT_1: 8615085Swnj tp->t_state = TCPS_CLOSING; 8625065Swnj break; 8634601Swnj 8645065Swnj /* 8655065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 8665065Swnj * starting the time-wait timer, turning off the other 8675065Swnj * standard timers. 8685065Swnj */ 8695065Swnj case TCPS_FIN_WAIT_2: 8705244Sroot tp->t_state = TCPS_TIME_WAIT; 8715074Swnj tcp_canceltimers(tp); 8725162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8735244Sroot soisdisconnected(so); 8745065Swnj break; 8755065Swnj 8764884Swnj /* 8775065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 8784884Swnj */ 8795065Swnj case TCPS_TIME_WAIT: 8805162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8815065Swnj break; 8825085Swnj } 8834601Swnj } 8845267Sroot if (so->so_options & SO_DEBUG) 8855267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 8865085Swnj 8875085Swnj /* 8885085Swnj * Return any desired output. 8895085Swnj */ 89026824Skarels if (needoutput || (tp->t_flags & TF_ACKNOW)) 89125939Skarels (void) tcp_output(tp); 8925065Swnj return; 8935085Swnj 8945065Swnj dropafterack: 8955085Swnj /* 8966211Swnj * Generate an ACK dropping incoming segment if it occupies 8976211Swnj * sequence space, where the ACK reflects our state. 8985085Swnj */ 89926057Skarels if (tiflags & TH_RST) 9005085Swnj goto drop; 9016303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 9026303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 9035391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 9045231Swnj return; 9055085Swnj 9065085Swnj dropwithreset: 90711731Ssam if (om) { 9086161Ssam (void) m_free(om); 90911731Ssam om = 0; 91011731Ssam } 9115085Swnj /* 9125244Sroot * Generate a RST, dropping incoming segment. 9135085Swnj * Make ACK acceptable to originator of segment. 91425197Skarels * Don't bother to respond if destination was broadcast. 9155085Swnj */ 91625197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 9175085Swnj goto drop; 9185085Swnj if (tiflags & TH_ACK) 9195391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 9205085Swnj else { 9215085Swnj if (tiflags & TH_SYN) 9225085Swnj ti->ti_len++; 9236211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 9246211Swnj TH_RST|TH_ACK); 9255085Swnj } 92610769Ssam /* destroy temporarily created socket */ 92710769Ssam if (dropsocket) 92810769Ssam (void) soabort(so); 9295231Swnj return; 9305085Swnj 9315065Swnj drop: 93211730Ssam if (om) 93311730Ssam (void) m_free(om); 9345085Swnj /* 9355085Swnj * Drop space held by incoming segment and return. 9365085Swnj */ 9376303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 9386303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 9395065Swnj m_freem(m); 94010769Ssam /* destroy temporarily created socket */ 94110769Ssam if (dropsocket) 94210769Ssam (void) soabort(so); 9435267Sroot return; 9445065Swnj } 9455065Swnj 94617272Skarels tcp_dooptions(tp, om, ti) 9475440Swnj struct tcpcb *tp; 9485440Swnj struct mbuf *om; 94917272Skarels struct tcpiphdr *ti; 9505419Swnj { 9515440Swnj register u_char *cp; 9525440Swnj int opt, optlen, cnt; 9535419Swnj 9545440Swnj cp = mtod(om, u_char *); 9555440Swnj cnt = om->m_len; 9565440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 9575440Swnj opt = cp[0]; 9585440Swnj if (opt == TCPOPT_EOL) 9595440Swnj break; 9605440Swnj if (opt == TCPOPT_NOP) 9615440Swnj optlen = 1; 96212169Ssam else { 9635440Swnj optlen = cp[1]; 96412169Ssam if (optlen <= 0) 96512169Ssam break; 96612169Ssam } 9675440Swnj switch (opt) { 9685440Swnj 9695440Swnj default: 9705440Swnj break; 9715440Swnj 9725440Swnj case TCPOPT_MAXSEG: 9735440Swnj if (optlen != 4) 9745440Swnj continue; 97517272Skarels if (!(ti->ti_flags & TH_SYN)) 97617272Skarels continue; 9775440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 9786161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 97917272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 9805440Swnj break; 9815419Swnj } 9825419Swnj } 9836161Ssam (void) m_free(om); 9845419Swnj } 9855419Swnj 9865419Swnj /* 9875547Swnj * Pull out of band byte out of a segment so 9885547Swnj * it doesn't appear in the user's data queue. 9895547Swnj * It is still reflected in the segment length for 9905547Swnj * sequencing purposes. 9915547Swnj */ 9925547Swnj tcp_pulloutofband(so, ti) 9935547Swnj struct socket *so; 9945547Swnj struct tcpiphdr *ti; 9955547Swnj { 9965547Swnj register struct mbuf *m; 9976116Swnj int cnt = ti->ti_urp - 1; 9985547Swnj 9995547Swnj m = dtom(ti); 10005547Swnj while (cnt >= 0) { 10015547Swnj if (m->m_len > cnt) { 10025547Swnj char *cp = mtod(m, caddr_t) + cnt; 10035547Swnj struct tcpcb *tp = sototcpcb(so); 10045547Swnj 10055547Swnj tp->t_iobc = *cp; 10065547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 10076161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 10085547Swnj m->m_len--; 10095547Swnj return; 10105547Swnj } 10115547Swnj cnt -= m->m_len; 10125547Swnj m = m->m_next; 10135547Swnj if (m == 0) 10145547Swnj break; 10155547Swnj } 10165547Swnj panic("tcp_pulloutofband"); 10175547Swnj } 10185547Swnj 10195547Swnj /* 102017272Skarels * Determine a reasonable value for maxseg size. 102117272Skarels * If the route is known, use one that can be handled 102217272Skarels * on the given interface without forcing IP to fragment. 102323975Skarels * If bigger than a page (CLBYTES), round down to nearest pagesize 102417272Skarels * to utilize pagesize mbufs. 102517272Skarels * If interface pointer is unavailable, or the destination isn't local, 102623975Skarels * use a conservative size (512 or the default IP max size, but no more 102723975Skarels * than the mtu of the interface through which we route), 102817272Skarels * as we can't discover anything about intervening gateways or networks. 102917272Skarels * 103017272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 103117272Skarels */ 103217272Skarels tcp_mss(tp) 103323975Skarels register struct tcpcb *tp; 103417272Skarels { 103517272Skarels struct route *ro; 103617272Skarels struct ifnet *ifp; 103717272Skarels int mss; 103817272Skarels struct inpcb *inp; 103917272Skarels 104017272Skarels inp = tp->t_inpcb; 104117272Skarels ro = &inp->inp_route; 104217272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 104317272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 104417272Skarels /* No route yet, so try to acquire one */ 104517272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 104617272Skarels ro->ro_dst.sa_family = AF_INET; 104717272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 104817272Skarels inp->inp_faddr; 104917272Skarels rtalloc(ro); 105017272Skarels } 105117272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 105217316Skarels return (TCP_MSS); 105317272Skarels } 105417272Skarels 105517272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 105617272Skarels #if (CLBYTES & (CLBYTES - 1)) == 0 105717272Skarels if (mss > CLBYTES) 105817272Skarels mss &= ~(CLBYTES-1); 105917272Skarels #else 106017272Skarels if (mss > CLBYTES) 106117272Skarels mss = mss / CLBYTES * CLBYTES; 106217272Skarels #endif 106323975Skarels if (in_localaddr(inp->inp_faddr)) 106423975Skarels return (mss); 106517316Skarels return (MIN(mss, TCP_MSS)); 106617272Skarels } 1067