123193Smckusick /* 244378Skarels * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 332789Sbostic * All rights reserved. 423193Smckusick * 5*44488Sbostic * %sccs.include.redist.c% 632789Sbostic * 7*44488Sbostic * @(#)tcp_subr.c 7.18 (Berkeley) 06/28/90 823193Smckusick */ 95068Swnj 1017064Sbloom #include "param.h" 1117064Sbloom #include "systm.h" 1240691Skarels #include "malloc.h" 1317064Sbloom #include "mbuf.h" 1417064Sbloom #include "socket.h" 1517064Sbloom #include "socketvar.h" 1617064Sbloom #include "protosw.h" 1717064Sbloom #include "errno.h" 1810896Ssam 1910896Ssam #include "../net/route.h" 2010896Ssam #include "../net/if.h" 2110896Ssam 2217064Sbloom #include "in.h" 2317064Sbloom #include "in_systm.h" 2417064Sbloom #include "ip.h" 2540691Skarels #include "in_pcb.h" 2617064Sbloom #include "ip_var.h" 2717064Sbloom #include "ip_icmp.h" 2817064Sbloom #include "tcp.h" 2917064Sbloom #include "tcp_fsm.h" 3017064Sbloom #include "tcp_seq.h" 3117064Sbloom #include "tcp_timer.h" 3217064Sbloom #include "tcp_var.h" 3317064Sbloom #include "tcpip.h" 345068Swnj 3544378Skarels /* patchable/settable parameters for tcp */ 3631395Skarels int tcp_ttl = TCP_TTL; 3744378Skarels int tcp_mssdflt = TCP_MSS; 3844378Skarels int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 3931395Skarels 4044378Skarels extern struct inpcb *tcp_last_inpcb; 4144378Skarels 425068Swnj /* 435068Swnj * Tcp initialization 445068Swnj */ 455068Swnj tcp_init() 465068Swnj { 475068Swnj 485068Swnj tcp_iss = 1; /* wrong */ 495068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 5040691Skarels if (max_protohdr < sizeof(struct tcpiphdr)) 5140691Skarels max_protohdr = sizeof(struct tcpiphdr); 5240691Skarels if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 5340691Skarels panic("tcp_init"); 545068Swnj } 555068Swnj 565068Swnj /* 575068Swnj * Create template to be used to send tcp packets on a connection. 585068Swnj * Call after host entry created, allocates an mbuf and fills 595068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 605068Swnj * necessary when the connection is used. 615068Swnj */ 625068Swnj struct tcpiphdr * 635068Swnj tcp_template(tp) 645068Swnj struct tcpcb *tp; 655068Swnj { 665068Swnj register struct inpcb *inp = tp->t_inpcb; 675068Swnj register struct mbuf *m; 685068Swnj register struct tcpiphdr *n; 695068Swnj 7026815Skarels if ((n = tp->t_template) == 0) { 7132101Skarels m = m_get(M_DONTWAIT, MT_HEADER); 7226815Skarels if (m == NULL) 7326815Skarels return (0); 7426815Skarels m->m_len = sizeof (struct tcpiphdr); 7526815Skarels n = mtod(m, struct tcpiphdr *); 7626815Skarels } 775068Swnj n->ti_next = n->ti_prev = 0; 785068Swnj n->ti_x1 = 0; 795068Swnj n->ti_pr = IPPROTO_TCP; 805068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 815068Swnj n->ti_src = inp->inp_laddr; 825068Swnj n->ti_dst = inp->inp_faddr; 835068Swnj n->ti_sport = inp->inp_lport; 845068Swnj n->ti_dport = inp->inp_fport; 855068Swnj n->ti_seq = 0; 865089Swnj n->ti_ack = 0; 875068Swnj n->ti_x2 = 0; 885068Swnj n->ti_off = 5; 895068Swnj n->ti_flags = 0; 905068Swnj n->ti_win = 0; 915068Swnj n->ti_sum = 0; 925068Swnj n->ti_urp = 0; 935068Swnj return (n); 945068Swnj } 955068Swnj 965068Swnj /* 975164Swnj * Send a single message to the TCP at address specified by 9844378Skarels * the given TCP/IP header. If m == 0, then we make a copy 995164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 1005164Swnj * This is used to force keep alive messages out using the TCP 1015164Swnj * template for a connection tp->t_template. If flags are given 1025164Swnj * then we send a message back to the TCP which originated the 1035164Swnj * segment ti, and discard the mbuf containing it and any other 1045164Swnj * attached mbufs. 1055164Swnj * 1065164Swnj * In any case the ack and sequence number of the transmitted 1075164Swnj * segment are as specified by the parameters. 1085068Swnj */ 10940691Skarels tcp_respond(tp, ti, m, ack, seq, flags) 1105392Swnj struct tcpcb *tp; 1115068Swnj register struct tcpiphdr *ti; 11240691Skarels register struct mbuf *m; 1135089Swnj tcp_seq ack, seq; 1145068Swnj int flags; 1155068Swnj { 1166212Swnj int win = 0, tlen; 1176353Ssam struct route *ro = 0; 1185068Swnj 1196353Ssam if (tp) { 1205392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1216353Ssam ro = &tp->t_inpcb->inp_route; 1226353Ssam } 12340691Skarels if (m == 0) { 12440691Skarels m = m_gethdr(M_DONTWAIT, MT_HEADER); 12510144Ssam if (m == NULL) 1265164Swnj return; 12731727Skarels #ifdef TCP_COMPAT_42 12831727Skarels tlen = 1; 12931727Skarels #else 13031727Skarels tlen = 0; 13131727Skarels #endif 13240691Skarels m->m_data += max_linkhdr; 1335164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1345164Swnj ti = mtod(m, struct tcpiphdr *); 1355164Swnj flags = TH_ACK; 1365164Swnj } else { 1375164Swnj m_freem(m->m_next); 1385164Swnj m->m_next = 0; 13940691Skarels m->m_data = (caddr_t)ti; 14044378Skarels m->m_len = sizeof (struct tcpiphdr); 14130762Skarels tlen = 0; 1425089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1435164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1445164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1455068Swnj #undef xchg 1465164Swnj } 14744378Skarels m->m_len = sizeof (struct tcpiphdr) + tlen; 1485089Swnj ti->ti_next = ti->ti_prev = 0; 1495089Swnj ti->ti_x1 = 0; 1509185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1518942Sroot ti->ti_seq = htonl(seq); 1528942Sroot ti->ti_ack = htonl(ack); 1535089Swnj ti->ti_x2 = 0; 1545089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1555068Swnj ti->ti_flags = flags; 1569185Ssam ti->ti_win = htons((u_short)win); 1575392Swnj ti->ti_urp = 0; 1586304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1596212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 16031395Skarels ((struct ip *)ti)->ip_ttl = tcp_ttl; 1616353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1625068Swnj } 1635075Swnj 1645089Swnj /* 1655089Swnj * Create a new TCP control block, making an 1665089Swnj * empty reassembly queue and hooking it to the argument 1675089Swnj * protocol control block. 1685089Swnj */ 1695075Swnj struct tcpcb * 1705075Swnj tcp_newtcpcb(inp) 1715075Swnj struct inpcb *inp; 1725075Swnj { 1739644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1745075Swnj register struct tcpcb *tp; 1755075Swnj 17610144Ssam if (m == NULL) 17710144Ssam return ((struct tcpcb *)0); 1785075Swnj tp = mtod(m, struct tcpcb *); 1795075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 18044378Skarels tp->t_maxseg = tcp_mssdflt; 18144378Skarels 1826470Sroot tp->t_flags = 0; /* sends options! */ 1835075Swnj tp->t_inpcb = inp; 18431726Skarels /* 18531757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 18631757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 18731757Skarels * reasonable initial retransmit time. 18831726Skarels */ 18931757Skarels tp->t_srtt = TCPTV_SRTTBASE; 19044378Skarels tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 19144378Skarels tp->t_rttmin = TCPTV_MIN; 19232374Skarels TCPT_RANGESET(tp->t_rxtcur, 19332374Skarels ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 19432374Skarels TCPTV_MIN, TCPTV_REXMTMAX); 19544378Skarels tp->snd_cwnd = TCP_MAXWIN; 19644378Skarels tp->snd_ssthresh = TCP_MAXWIN; 19744378Skarels inp->inp_ip.ip_ttl = tcp_ttl; 1985075Swnj inp->inp_ppcb = (caddr_t)tp; 1995075Swnj return (tp); 2005075Swnj } 2015075Swnj 2025089Swnj /* 2035089Swnj * Drop a TCP connection, reporting 2045089Swnj * the specified error. If connection is synchronized, 2055089Swnj * then send a RST to peer. 2065089Swnj */ 20710395Ssam struct tcpcb * 2085075Swnj tcp_drop(tp, errno) 20910395Ssam register struct tcpcb *tp; 2105075Swnj int errno; 2115075Swnj { 2125075Swnj struct socket *so = tp->t_inpcb->inp_socket; 2135075Swnj 2145286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 2155075Swnj tp->t_state = TCPS_CLOSED; 2168776Sroot (void) tcp_output(tp); 21730524Skarels tcpstat.tcps_drops++; 21830524Skarels } else 21930524Skarels tcpstat.tcps_conndrops++; 22044378Skarels if (errno == ETIMEDOUT && tp->t_softerror) 22144378Skarels errno = tp->t_softerror; 2225075Swnj so->so_error = errno; 22310395Ssam return (tcp_close(tp)); 2245075Swnj } 2255075Swnj 2265089Swnj /* 2275089Swnj * Close a TCP control block: 2285089Swnj * discard all space held by the tcp 2295089Swnj * discard internet protocol block 2305089Swnj * wake up any sleepers 2315089Swnj */ 23210395Ssam struct tcpcb * 2335075Swnj tcp_close(tp) 2345075Swnj register struct tcpcb *tp; 2355075Swnj { 2365075Swnj register struct tcpiphdr *t; 2375261Swnj struct inpcb *inp = tp->t_inpcb; 2385261Swnj struct socket *so = inp->inp_socket; 23912422Ssam register struct mbuf *m; 24044378Skarels #ifdef RTV_RTT 24144378Skarels register struct rtentry *rt; 2425075Swnj 24344378Skarels /* 24444378Skarels * If we sent enough data to get some meaningful characteristics, 24544378Skarels * save them in the routing entry. 'Enough' is arbitrarily 24644378Skarels * defined as 4K (default tcp_sendspace) * 16. This would 24744378Skarels * give us 16 rtt samples assuming we only get one sample per 24844378Skarels * window (the usual case on a long haul net). 16 samples is 24944378Skarels * enough for the srtt filter to converge to within 5% of the correct 25044378Skarels * value; fewer samples and we could save a very bogus rtt. 25144378Skarels * 25244378Skarels * Don't update the default route's characteristics and don't 25344378Skarels * update anything that the user "locked". 25444378Skarels */ 25544378Skarels if (SEQ_LT(tp->iss+(4096*16), tp->snd_max) && 25644378Skarels (rt = inp->inp_route.ro_rt) && 25744378Skarels ((struct sockaddr_in *) rt_key(rt))->sin_addr.s_addr != 25844378Skarels INADDR_ANY) { 25944378Skarels register u_long i; 26044378Skarels 26144378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 26244378Skarels i = tp->t_srtt * 26344378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 26444378Skarels if (rt->rt_rmx.rmx_rtt && i) 26544378Skarels /* 26644378Skarels * filter this update to half the old & half 26744378Skarels * the new values, converting scale. 26844378Skarels * See route.h and tcp_var.h for a 26944378Skarels * description of the scaling constants. 27044378Skarels */ 27144378Skarels rt->rt_rmx.rmx_rtt = 27244378Skarels (rt->rt_rmx.rmx_rtt + i) / 2; 27344378Skarels else 27444378Skarels rt->rt_rmx.rmx_rtt = i; 27544378Skarels } 27644378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 27744378Skarels i = tp->t_rttvar * 27844378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 27944378Skarels if (rt->rt_rmx.rmx_rttvar && i) 28044378Skarels rt->rt_rmx.rmx_rttvar = 28144378Skarels (rt->rt_rmx.rmx_rttvar + i) / 2; 28244378Skarels else 28344378Skarels rt->rt_rmx.rmx_rttvar = i; 28444378Skarels } 28544378Skarels /* 28644378Skarels * update the pipelimit (ssthresh) if it has been updated 28744378Skarels * already or if a pipesize was specified & the threshhold 28844378Skarels * got below half the pipesize. I.e., wait for bad news 28944378Skarels * before we start updating, then update on both good 29044378Skarels * and bad news. 29144378Skarels */ 29244378Skarels if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 29344378Skarels (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || 29444378Skarels i < (rt->rt_rmx.rmx_sendpipe / 2)) { 29544378Skarels /* 29644378Skarels * convert the limit from user data bytes to 29744378Skarels * packets then to packet data bytes. 29844378Skarels */ 29944378Skarels i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 30044378Skarels if (i < 2) 30144378Skarels i = 2; 30244378Skarels i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 30344378Skarels if (rt->rt_rmx.rmx_ssthresh) 30444378Skarels rt->rt_rmx.rmx_ssthresh = 30544378Skarels (rt->rt_rmx.rmx_ssthresh + i) / 2; 30644378Skarels else 30744378Skarels rt->rt_rmx.rmx_ssthresh = i; 30844378Skarels } 30944378Skarels } 31044378Skarels #endif RTV_RTT 31144378Skarels /* free the reassembly queue, if any */ 3125075Swnj t = tp->seg_next; 31312422Ssam while (t != (struct tcpiphdr *)tp) { 31412422Ssam t = (struct tcpiphdr *)t->ti_next; 31544378Skarels m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 31612422Ssam remque(t->ti_prev); 31712422Ssam m_freem(m); 31812422Ssam } 3195089Swnj if (tp->t_template) 3205075Swnj (void) m_free(dtom(tp->t_template)); 3215075Swnj (void) m_free(dtom(tp)); 3225261Swnj inp->inp_ppcb = 0; 3236472Sroot soisdisconnected(so); 32444378Skarels /* clobber input pcb cache if we're closing the cached connection */ 32544378Skarels if (inp == tcp_last_inpcb) 32644378Skarels tcp_last_inpcb = &tcb; 3275269Sroot in_pcbdetach(inp); 32830524Skarels tcpstat.tcps_closed++; 32910395Ssam return ((struct tcpcb *)0); 3305075Swnj } 3315075Swnj 3325075Swnj tcp_drain() 3335075Swnj { 3345075Swnj 3355075Swnj } 3365075Swnj 33730233Skarels /* 33830233Skarels * Notify a tcp user of an asynchronous error; 33944378Skarels * store error as soft error, but wake up user 34044378Skarels * (for now, won't do anything until can select for soft error). 34130233Skarels */ 34244378Skarels tcp_notify(inp, error) 34330233Skarels register struct inpcb *inp; 34444378Skarels int error; 34530233Skarels { 34630233Skarels 34744378Skarels ((struct tcpcb *)inp->inp_ppcb)->t_softerror = error; 34830233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 34930233Skarels sorwakeup(inp->inp_socket); 35030233Skarels sowwakeup(inp->inp_socket); 35130233Skarels } 35240691Skarels 35340691Skarels tcp_ctlinput(cmd, sa, ip) 3546584Ssam int cmd; 35524818Skarels struct sockaddr *sa; 35640691Skarels register struct ip *ip; 3575075Swnj { 35840691Skarels register struct tcphdr *th; 35940691Skarels extern struct in_addr zeroin_addr; 3606591Ssam extern u_char inetctlerrmap[]; 36140691Skarels int (*notify)() = tcp_notify, tcp_quench(); 3626591Ssam 36340691Skarels if (cmd == PRC_QUENCH) 36440691Skarels notify = tcp_quench; 36540691Skarels else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) 3666591Ssam return; 36740691Skarels if (ip) { 36840691Skarels th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 36940691Skarels in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 37040691Skarels cmd, notify); 37140691Skarels } else 37240691Skarels in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 3735075Swnj } 37417359Skarels 37517359Skarels /* 37617359Skarels * When a source quench is received, close congestion window 37731442Skarels * to one segment. We will gradually open it again as we proceed. 37817359Skarels */ 37917359Skarels tcp_quench(inp) 38017359Skarels struct inpcb *inp; 38117359Skarels { 38217359Skarels struct tcpcb *tp = intotcpcb(inp); 38317359Skarels 38424818Skarels if (tp) 38531442Skarels tp->snd_cwnd = tp->t_maxseg; 38617359Skarels } 387