123190Smckusick /* 223190Smckusick * Copyright (c) 1982 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*26057Skarels * @(#)tcp_input.c 6.20 (Berkeley) 02/03/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; 17425939Skarels int todrop, acked, newwin; 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) { 38225939Skarels tp = tcp_drop(tp); 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 6375307Sroot if (ti->ti_ack == tp->snd_max) 6385244Sroot tp->t_timer[TCPT_REXMT] = 0; 6395307Sroot else { 6405244Sroot TCPT_RANGESET(tp->t_timer[TCPT_REXMT], 6415244Sroot tcp_beta * tp->t_srtt, TCPTV_MIN, TCPTV_MAX); 6425300Sroot tp->t_rxtshift = 0; 6435085Swnj } 64417360Skarels /* 64517360Skarels * When new data is acked, open the congestion window a bit. 64617360Skarels */ 64717360Skarels if (acked > 0) 64817360Skarels tp->snd_cwnd = MIN(11 * tp->snd_cwnd / 10, 65535); 6495307Sroot if (acked > so->so_snd.sb_cc) { 65015386Ssam tp->snd_wnd -= so->so_snd.sb_cc; 6515307Sroot sbdrop(&so->so_snd, so->so_snd.sb_cc); 6525307Sroot } else { 6536161Ssam sbdrop(&so->so_snd, acked); 6545307Sroot tp->snd_wnd -= acked; 6555307Sroot acked = 0; 6565307Sroot } 6576434Swnj if ((so->so_snd.sb_flags & SB_WAIT) || so->so_snd.sb_sel) 6585300Sroot sowwakeup(so); 6595231Swnj tp->snd_una = ti->ti_ack; 6605357Sroot if (SEQ_LT(tp->snd_nxt, tp->snd_una)) 6615357Sroot tp->snd_nxt = tp->snd_una; 6625162Swnj 6634601Swnj switch (tp->t_state) { 6644601Swnj 6655065Swnj /* 6665065Swnj * In FIN_WAIT_1 STATE in addition to the processing 6675065Swnj * for the ESTABLISHED state if our FIN is now acknowledged 6685085Swnj * then enter FIN_WAIT_2. 6695065Swnj */ 6705065Swnj case TCPS_FIN_WAIT_1: 6715896Swnj if (ourfinisacked) { 6725896Swnj /* 6735896Swnj * If we can't receive any more 6745896Swnj * data, then closing user can proceed. 67524816Skarels * Starting the timer is contrary to the 67624816Skarels * specification, but if we don't get a FIN 67724816Skarels * we'll hang forever. 6785896Swnj */ 67924816Skarels if (so->so_state & SS_CANTRCVMORE) { 6805896Swnj soisdisconnected(so); 68124816Skarels tp->t_timer[TCPT_2MSL] = TCPTV_MAXIDLE; 68224816Skarels } 6835085Swnj tp->t_state = TCPS_FIN_WAIT_2; 6845896Swnj } 6854601Swnj break; 6864601Swnj 6875065Swnj /* 6885065Swnj * In CLOSING STATE in addition to the processing for 6895065Swnj * the ESTABLISHED state if the ACK acknowledges our FIN 6905065Swnj * then enter the TIME-WAIT state, otherwise ignore 6915065Swnj * the segment. 6925065Swnj */ 6935065Swnj case TCPS_CLOSING: 6945244Sroot if (ourfinisacked) { 6955065Swnj tp->t_state = TCPS_TIME_WAIT; 6965244Sroot tcp_canceltimers(tp); 6975244Sroot tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 6985244Sroot soisdisconnected(so); 6995244Sroot } 7005244Sroot break; 7014601Swnj 7025065Swnj /* 7035085Swnj * The only thing that can arrive in LAST_ACK state 7045085Swnj * is an acknowledgment of our FIN. If our FIN is now 7055085Swnj * acknowledged, delete the TCB, enter the closed state 7065085Swnj * and return. 7075065Swnj */ 7085065Swnj case TCPS_LAST_ACK: 70910394Ssam if (ourfinisacked) 71010394Ssam tp = tcp_close(tp); 7115065Swnj goto drop; 7124601Swnj 7135065Swnj /* 7145065Swnj * In TIME_WAIT state the only thing that should arrive 7155065Swnj * is a retransmission of the remote FIN. Acknowledge 7165065Swnj * it and restart the finack timer. 7175065Swnj */ 7185065Swnj case TCPS_TIME_WAIT: 7195162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 7205065Swnj goto dropafterack; 7214601Swnj } 7225085Swnj #undef ourfinisacked 7235085Swnj } 7244601Swnj 7255065Swnj step6: 7265065Swnj /* 7275244Sroot * Update window information. 72825939Skarels * Don't look at window if no ACK: TAC's send garbage on first SYN. 7295244Sroot */ 73025939Skarels if ((tiflags & TH_ACK) && 73125939Skarels (SEQ_LT(tp->snd_wl1, ti->ti_seq) || tp->snd_wl1 == ti->ti_seq && 7325391Swnj (SEQ_LT(tp->snd_wl2, ti->ti_ack) || 73325939Skarels tp->snd_wl2 == ti->ti_ack && ti->ti_win > tp->snd_wnd))) { 7345244Sroot tp->snd_wnd = ti->ti_win; 7355244Sroot tp->snd_wl1 = ti->ti_seq; 7365244Sroot tp->snd_wl2 = ti->ti_ack; 73725260Skarels if (tp->snd_wnd > tp->max_sndwnd) 73825260Skarels tp->max_sndwnd = tp->snd_wnd; 73925939Skarels newwin = 1; 74025939Skarels } else 74125939Skarels newwin = 0; 7425244Sroot 7435244Sroot /* 7445547Swnj * Process segments with URG. 7455065Swnj */ 7467267Swnj if ((tiflags & TH_URG) && ti->ti_urp && 7477267Swnj TCPS_HAVERCVDFIN(tp->t_state) == 0) { 7485547Swnj /* 74925939Skarels * This is a kludge, but if we receive and accept 75013121Ssam * random urgent pointers, we'll crash in 75113121Ssam * soreceive. It's hard to imagine someone 75213121Ssam * actually wanting to send this much urgent data. 75312441Ssam */ 75425652Skarels if (ti->ti_urp + so->so_rcv.sb_cc > SB_MAX) { 75512441Ssam ti->ti_urp = 0; /* XXX */ 75612441Ssam tiflags &= ~TH_URG; /* XXX */ 75725939Skarels goto dodata; /* XXX */ 75812441Ssam } 75912441Ssam /* 7605547Swnj * If this segment advances the known urgent pointer, 7615547Swnj * then mark the data stream. This should not happen 7625547Swnj * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since 7635547Swnj * a FIN has been received from the remote side. 7645547Swnj * In these states we ignore the URG. 7655547Swnj */ 7665547Swnj if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) { 7675547Swnj tp->rcv_up = ti->ti_seq + ti->ti_urp; 7685547Swnj so->so_oobmark = so->so_rcv.sb_cc + 7695547Swnj (tp->rcv_up - tp->rcv_nxt) - 1; 7705547Swnj if (so->so_oobmark == 0) 7715547Swnj so->so_state |= SS_RCVATMARK; 7728313Sroot sohasoutofband(so); 77324816Skarels tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); 7745440Swnj } 7755547Swnj /* 7765547Swnj * Remove out of band data so doesn't get presented to user. 7775547Swnj * This can happen independent of advancing the URG pointer, 7785547Swnj * but if two URG's are pending at once, some out-of-band 7795547Swnj * data may creep in... ick. 7805547Swnj */ 7817510Sroot if (ti->ti_urp <= ti->ti_len) 7825547Swnj tcp_pulloutofband(so, ti); 78325939Skarels } else 78425939Skarels /* 78525939Skarels * If no out of band data is expected, 78625939Skarels * pull receive urgent pointer along 78725939Skarels * with the receive window. 78825939Skarels */ 78925939Skarels if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) 79025939Skarels tp->rcv_up = tp->rcv_nxt; 79125939Skarels dodata: /* XXX */ 7924601Swnj 7934601Swnj /* 7945065Swnj * Process the segment text, merging it into the TCP sequencing queue, 7955065Swnj * and arranging for acknowledgment of receipt if necessary. 7965065Swnj * This process logically involves adjusting tp->rcv_wnd as data 7975065Swnj * is presented to the user (this happens in tcp_usrreq.c, 7985065Swnj * case PRU_RCVD). If a FIN has already been received on this 7995065Swnj * connection then we just ignore the text. 8004601Swnj */ 80117946Skarels if ((ti->ti_len || (tiflags&TH_FIN)) && 80217946Skarels TCPS_HAVERCVDFIN(tp->t_state) == 0) { 80324816Skarels TCP_REASS(tp, ti, m, so, tiflags); 8045440Swnj if (tcpnodelack == 0) 8055440Swnj tp->t_flags |= TF_DELACK; 8065440Swnj else 8075440Swnj tp->t_flags |= TF_ACKNOW; 80825260Skarels /* 80925260Skarels * Note the amount of data that peer has sent into 81025260Skarels * our window, in order to estimate the sender's 81125260Skarels * buffer size. 81225260Skarels */ 81325260Skarels len = so->so_rcv.sb_hiwat - (tp->rcv_nxt - tp->rcv_adv); 81425260Skarels if (len > tp->max_rcvd) 81525260Skarels tp->max_rcvd = len; 8165244Sroot } else { 8174924Swnj m_freem(m); 8185263Swnj tiflags &= ~TH_FIN; 8195244Sroot } 8204601Swnj 8214601Swnj /* 8225263Swnj * If FIN is received ACK the FIN and let the user know 8235263Swnj * that the connection is closing. 8244601Swnj */ 8255263Swnj if (tiflags & TH_FIN) { 8265244Sroot if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { 8275244Sroot socantrcvmore(so); 8285244Sroot tp->t_flags |= TF_ACKNOW; 8295244Sroot tp->rcv_nxt++; 8305244Sroot } 8315065Swnj switch (tp->t_state) { 8324601Swnj 8335065Swnj /* 8345065Swnj * In SYN_RECEIVED and ESTABLISHED STATES 8355065Swnj * enter the CLOSE_WAIT state. 8364884Swnj */ 8375065Swnj case TCPS_SYN_RECEIVED: 8385065Swnj case TCPS_ESTABLISHED: 8395065Swnj tp->t_state = TCPS_CLOSE_WAIT; 8405065Swnj break; 8414884Swnj 8425065Swnj /* 8435085Swnj * If still in FIN_WAIT_1 STATE FIN has not been acked so 8445085Swnj * enter the CLOSING state. 8454884Swnj */ 8465065Swnj case TCPS_FIN_WAIT_1: 8475085Swnj tp->t_state = TCPS_CLOSING; 8485065Swnj break; 8494601Swnj 8505065Swnj /* 8515065Swnj * In FIN_WAIT_2 state enter the TIME_WAIT state, 8525065Swnj * starting the time-wait timer, turning off the other 8535065Swnj * standard timers. 8545065Swnj */ 8555065Swnj case TCPS_FIN_WAIT_2: 8565244Sroot tp->t_state = TCPS_TIME_WAIT; 8575074Swnj tcp_canceltimers(tp); 8585162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8595244Sroot soisdisconnected(so); 8605065Swnj break; 8615065Swnj 8624884Swnj /* 8635065Swnj * In TIME_WAIT state restart the 2 MSL time_wait timer. 8644884Swnj */ 8655065Swnj case TCPS_TIME_WAIT: 8665162Swnj tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; 8675065Swnj break; 8685085Swnj } 8694601Swnj } 8705267Sroot if (so->so_options & SO_DEBUG) 8715267Sroot tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0); 8725085Swnj 8735085Swnj /* 8745085Swnj * Return any desired output. 8755085Swnj */ 876*26057Skarels if (newwin || (tp->t_flags & TF_ACKNOW)) 87725939Skarels (void) tcp_output(tp); 8785065Swnj return; 8795085Swnj 8805065Swnj dropafterack: 8815085Swnj /* 8826211Swnj * Generate an ACK dropping incoming segment if it occupies 8836211Swnj * sequence space, where the ACK reflects our state. 8845085Swnj */ 885*26057Skarels if (tiflags & TH_RST) 8865085Swnj goto drop; 8876303Sroot if (tp->t_inpcb->inp_socket->so_options & SO_DEBUG) 8886303Sroot tcp_trace(TA_RESPOND, ostate, tp, &tcp_saveti, 0); 8895391Swnj tcp_respond(tp, ti, tp->rcv_nxt, tp->snd_nxt, TH_ACK); 8905231Swnj return; 8915085Swnj 8925085Swnj dropwithreset: 89311731Ssam if (om) { 8946161Ssam (void) m_free(om); 89511731Ssam om = 0; 89611731Ssam } 8975085Swnj /* 8985244Sroot * Generate a RST, dropping incoming segment. 8995085Swnj * Make ACK acceptable to originator of segment. 90025197Skarels * Don't bother to respond if destination was broadcast. 9015085Swnj */ 90225197Skarels if ((tiflags & TH_RST) || in_broadcast(ti->ti_dst)) 9035085Swnj goto drop; 9045085Swnj if (tiflags & TH_ACK) 9055391Swnj tcp_respond(tp, ti, (tcp_seq)0, ti->ti_ack, TH_RST); 9065085Swnj else { 9075085Swnj if (tiflags & TH_SYN) 9085085Swnj ti->ti_len++; 9096211Swnj tcp_respond(tp, ti, ti->ti_seq+ti->ti_len, (tcp_seq)0, 9106211Swnj TH_RST|TH_ACK); 9115085Swnj } 91210769Ssam /* destroy temporarily created socket */ 91310769Ssam if (dropsocket) 91410769Ssam (void) soabort(so); 9155231Swnj return; 9165085Swnj 9175065Swnj drop: 91811730Ssam if (om) 91911730Ssam (void) m_free(om); 9205085Swnj /* 9215085Swnj * Drop space held by incoming segment and return. 9225085Swnj */ 9236303Sroot if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) 9246303Sroot tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0); 9255065Swnj m_freem(m); 92610769Ssam /* destroy temporarily created socket */ 92710769Ssam if (dropsocket) 92810769Ssam (void) soabort(so); 9295267Sroot return; 9305065Swnj } 9315065Swnj 93217272Skarels tcp_dooptions(tp, om, ti) 9335440Swnj struct tcpcb *tp; 9345440Swnj struct mbuf *om; 93517272Skarels struct tcpiphdr *ti; 9365419Swnj { 9375440Swnj register u_char *cp; 9385440Swnj int opt, optlen, cnt; 9395419Swnj 9405440Swnj cp = mtod(om, u_char *); 9415440Swnj cnt = om->m_len; 9425440Swnj for (; cnt > 0; cnt -= optlen, cp += optlen) { 9435440Swnj opt = cp[0]; 9445440Swnj if (opt == TCPOPT_EOL) 9455440Swnj break; 9465440Swnj if (opt == TCPOPT_NOP) 9475440Swnj optlen = 1; 94812169Ssam else { 9495440Swnj optlen = cp[1]; 95012169Ssam if (optlen <= 0) 95112169Ssam break; 95212169Ssam } 9535440Swnj switch (opt) { 9545440Swnj 9555440Swnj default: 9565440Swnj break; 9575440Swnj 9585440Swnj case TCPOPT_MAXSEG: 9595440Swnj if (optlen != 4) 9605440Swnj continue; 96117272Skarels if (!(ti->ti_flags & TH_SYN)) 96217272Skarels continue; 9635440Swnj tp->t_maxseg = *(u_short *)(cp + 2); 9646161Ssam tp->t_maxseg = ntohs((u_short)tp->t_maxseg); 96517272Skarels tp->t_maxseg = MIN(tp->t_maxseg, tcp_mss(tp)); 9665440Swnj break; 9675419Swnj } 9685419Swnj } 9696161Ssam (void) m_free(om); 9705419Swnj } 9715419Swnj 9725419Swnj /* 9735547Swnj * Pull out of band byte out of a segment so 9745547Swnj * it doesn't appear in the user's data queue. 9755547Swnj * It is still reflected in the segment length for 9765547Swnj * sequencing purposes. 9775547Swnj */ 9785547Swnj tcp_pulloutofband(so, ti) 9795547Swnj struct socket *so; 9805547Swnj struct tcpiphdr *ti; 9815547Swnj { 9825547Swnj register struct mbuf *m; 9836116Swnj int cnt = ti->ti_urp - 1; 9845547Swnj 9855547Swnj m = dtom(ti); 9865547Swnj while (cnt >= 0) { 9875547Swnj if (m->m_len > cnt) { 9885547Swnj char *cp = mtod(m, caddr_t) + cnt; 9895547Swnj struct tcpcb *tp = sototcpcb(so); 9905547Swnj 9915547Swnj tp->t_iobc = *cp; 9925547Swnj tp->t_oobflags |= TCPOOB_HAVEDATA; 9936161Ssam bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); 9945547Swnj m->m_len--; 9955547Swnj return; 9965547Swnj } 9975547Swnj cnt -= m->m_len; 9985547Swnj m = m->m_next; 9995547Swnj if (m == 0) 10005547Swnj break; 10015547Swnj } 10025547Swnj panic("tcp_pulloutofband"); 10035547Swnj } 10045547Swnj 10055547Swnj /* 100617272Skarels * Determine a reasonable value for maxseg size. 100717272Skarels * If the route is known, use one that can be handled 100817272Skarels * on the given interface without forcing IP to fragment. 100923975Skarels * If bigger than a page (CLBYTES), round down to nearest pagesize 101017272Skarels * to utilize pagesize mbufs. 101117272Skarels * If interface pointer is unavailable, or the destination isn't local, 101223975Skarels * use a conservative size (512 or the default IP max size, but no more 101323975Skarels * than the mtu of the interface through which we route), 101417272Skarels * as we can't discover anything about intervening gateways or networks. 101517272Skarels * 101617272Skarels * This is ugly, and doesn't belong at this level, but has to happen somehow. 101717272Skarels */ 101817272Skarels tcp_mss(tp) 101923975Skarels register struct tcpcb *tp; 102017272Skarels { 102117272Skarels struct route *ro; 102217272Skarels struct ifnet *ifp; 102317272Skarels int mss; 102417272Skarels struct inpcb *inp; 102517272Skarels 102617272Skarels inp = tp->t_inpcb; 102717272Skarels ro = &inp->inp_route; 102817272Skarels if ((ro->ro_rt == (struct rtentry *)0) || 102917272Skarels (ifp = ro->ro_rt->rt_ifp) == (struct ifnet *)0) { 103017272Skarels /* No route yet, so try to acquire one */ 103117272Skarels if (inp->inp_faddr.s_addr != INADDR_ANY) { 103217272Skarels ro->ro_dst.sa_family = AF_INET; 103317272Skarels ((struct sockaddr_in *) &ro->ro_dst)->sin_addr = 103417272Skarels inp->inp_faddr; 103517272Skarels rtalloc(ro); 103617272Skarels } 103717272Skarels if ((ro->ro_rt == 0) || (ifp = ro->ro_rt->rt_ifp) == 0) 103817316Skarels return (TCP_MSS); 103917272Skarels } 104017272Skarels 104117272Skarels mss = ifp->if_mtu - sizeof(struct tcpiphdr); 104217272Skarels #if (CLBYTES & (CLBYTES - 1)) == 0 104317272Skarels if (mss > CLBYTES) 104417272Skarels mss &= ~(CLBYTES-1); 104517272Skarels #else 104617272Skarels if (mss > CLBYTES) 104717272Skarels mss = mss / CLBYTES * CLBYTES; 104817272Skarels #endif 104923975Skarels if (in_localaddr(inp->inp_faddr)) 105023975Skarels return (mss); 105117316Skarels return (MIN(mss, TCP_MSS)); 105217272Skarels } 1053