123193Smckusick /* 244378Skarels * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. 332789Sbostic * All rights reserved. 423193Smckusick * 544488Sbostic * %sccs.include.redist.c% 632789Sbostic * 7*44966Skarels * @(#)tcp_subr.c 7.19 (Berkeley) 07/25/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 { 116*44966Skarels register int tlen; 117*44966Skarels int win = 0; 1186353Ssam struct route *ro = 0; 1195068Swnj 1206353Ssam if (tp) { 1215392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1226353Ssam ro = &tp->t_inpcb->inp_route; 1236353Ssam } 12440691Skarels if (m == 0) { 12540691Skarels m = m_gethdr(M_DONTWAIT, MT_HEADER); 12610144Ssam if (m == NULL) 1275164Swnj return; 12831727Skarels #ifdef TCP_COMPAT_42 12931727Skarels tlen = 1; 13031727Skarels #else 13131727Skarels tlen = 0; 13231727Skarels #endif 13340691Skarels m->m_data += max_linkhdr; 1345164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1355164Swnj ti = mtod(m, struct tcpiphdr *); 1365164Swnj flags = TH_ACK; 1375164Swnj } else { 1385164Swnj m_freem(m->m_next); 1395164Swnj m->m_next = 0; 14040691Skarels m->m_data = (caddr_t)ti; 14144378Skarels m->m_len = sizeof (struct tcpiphdr); 14230762Skarels tlen = 0; 1435089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1445164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1455164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1465068Swnj #undef xchg 1475164Swnj } 148*44966Skarels ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 149*44966Skarels tlen += sizeof (struct tcpiphdr); 150*44966Skarels m->m_len = tlen; 151*44966Skarels m->m_pkthdr.len = tlen; 152*44966Skarels m->m_pkthdr.rcvif = (struct ifnet *) 0; 1535089Swnj ti->ti_next = ti->ti_prev = 0; 1545089Swnj ti->ti_x1 = 0; 1558942Sroot ti->ti_seq = htonl(seq); 1568942Sroot ti->ti_ack = htonl(ack); 1575089Swnj ti->ti_x2 = 0; 1585089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1595068Swnj ti->ti_flags = flags; 1609185Ssam ti->ti_win = htons((u_short)win); 1615392Swnj ti->ti_urp = 0; 162*44966Skarels ti->ti_sum = in_cksum(m, tlen); 163*44966Skarels ((struct ip *)ti)->ip_len = tlen; 16431395Skarels ((struct ip *)ti)->ip_ttl = tcp_ttl; 1656353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1665068Swnj } 1675075Swnj 1685089Swnj /* 1695089Swnj * Create a new TCP control block, making an 1705089Swnj * empty reassembly queue and hooking it to the argument 1715089Swnj * protocol control block. 1725089Swnj */ 1735075Swnj struct tcpcb * 1745075Swnj tcp_newtcpcb(inp) 1755075Swnj struct inpcb *inp; 1765075Swnj { 1779644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1785075Swnj register struct tcpcb *tp; 1795075Swnj 18010144Ssam if (m == NULL) 18110144Ssam return ((struct tcpcb *)0); 1825075Swnj tp = mtod(m, struct tcpcb *); 1835075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 18444378Skarels tp->t_maxseg = tcp_mssdflt; 18544378Skarels 1866470Sroot tp->t_flags = 0; /* sends options! */ 1875075Swnj tp->t_inpcb = inp; 18831726Skarels /* 18931757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 19031757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 19131757Skarels * reasonable initial retransmit time. 19231726Skarels */ 19331757Skarels tp->t_srtt = TCPTV_SRTTBASE; 19444378Skarels tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 19544378Skarels tp->t_rttmin = TCPTV_MIN; 19632374Skarels TCPT_RANGESET(tp->t_rxtcur, 19732374Skarels ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 19832374Skarels TCPTV_MIN, TCPTV_REXMTMAX); 19944378Skarels tp->snd_cwnd = TCP_MAXWIN; 20044378Skarels tp->snd_ssthresh = TCP_MAXWIN; 20144378Skarels inp->inp_ip.ip_ttl = tcp_ttl; 2025075Swnj inp->inp_ppcb = (caddr_t)tp; 2035075Swnj return (tp); 2045075Swnj } 2055075Swnj 2065089Swnj /* 2075089Swnj * Drop a TCP connection, reporting 2085089Swnj * the specified error. If connection is synchronized, 2095089Swnj * then send a RST to peer. 2105089Swnj */ 21110395Ssam struct tcpcb * 2125075Swnj tcp_drop(tp, errno) 21310395Ssam register struct tcpcb *tp; 2145075Swnj int errno; 2155075Swnj { 2165075Swnj struct socket *so = tp->t_inpcb->inp_socket; 2175075Swnj 2185286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 2195075Swnj tp->t_state = TCPS_CLOSED; 2208776Sroot (void) tcp_output(tp); 22130524Skarels tcpstat.tcps_drops++; 22230524Skarels } else 22330524Skarels tcpstat.tcps_conndrops++; 22444378Skarels if (errno == ETIMEDOUT && tp->t_softerror) 22544378Skarels errno = tp->t_softerror; 2265075Swnj so->so_error = errno; 22710395Ssam return (tcp_close(tp)); 2285075Swnj } 2295075Swnj 2305089Swnj /* 2315089Swnj * Close a TCP control block: 2325089Swnj * discard all space held by the tcp 2335089Swnj * discard internet protocol block 2345089Swnj * wake up any sleepers 2355089Swnj */ 23610395Ssam struct tcpcb * 2375075Swnj tcp_close(tp) 2385075Swnj register struct tcpcb *tp; 2395075Swnj { 2405075Swnj register struct tcpiphdr *t; 2415261Swnj struct inpcb *inp = tp->t_inpcb; 2425261Swnj struct socket *so = inp->inp_socket; 24312422Ssam register struct mbuf *m; 24444378Skarels #ifdef RTV_RTT 24544378Skarels register struct rtentry *rt; 2465075Swnj 24744378Skarels /* 24844378Skarels * If we sent enough data to get some meaningful characteristics, 24944378Skarels * save them in the routing entry. 'Enough' is arbitrarily 25044378Skarels * defined as 4K (default tcp_sendspace) * 16. This would 25144378Skarels * give us 16 rtt samples assuming we only get one sample per 25244378Skarels * window (the usual case on a long haul net). 16 samples is 25344378Skarels * enough for the srtt filter to converge to within 5% of the correct 25444378Skarels * value; fewer samples and we could save a very bogus rtt. 25544378Skarels * 25644378Skarels * Don't update the default route's characteristics and don't 25744378Skarels * update anything that the user "locked". 25844378Skarels */ 25944378Skarels if (SEQ_LT(tp->iss+(4096*16), tp->snd_max) && 26044378Skarels (rt = inp->inp_route.ro_rt) && 26144378Skarels ((struct sockaddr_in *) rt_key(rt))->sin_addr.s_addr != 26244378Skarels INADDR_ANY) { 26344378Skarels register u_long i; 26444378Skarels 26544378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 26644378Skarels i = tp->t_srtt * 26744378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 26844378Skarels if (rt->rt_rmx.rmx_rtt && i) 26944378Skarels /* 27044378Skarels * filter this update to half the old & half 27144378Skarels * the new values, converting scale. 27244378Skarels * See route.h and tcp_var.h for a 27344378Skarels * description of the scaling constants. 27444378Skarels */ 27544378Skarels rt->rt_rmx.rmx_rtt = 27644378Skarels (rt->rt_rmx.rmx_rtt + i) / 2; 27744378Skarels else 27844378Skarels rt->rt_rmx.rmx_rtt = i; 27944378Skarels } 28044378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 28144378Skarels i = tp->t_rttvar * 28244378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 28344378Skarels if (rt->rt_rmx.rmx_rttvar && i) 28444378Skarels rt->rt_rmx.rmx_rttvar = 28544378Skarels (rt->rt_rmx.rmx_rttvar + i) / 2; 28644378Skarels else 28744378Skarels rt->rt_rmx.rmx_rttvar = i; 28844378Skarels } 28944378Skarels /* 29044378Skarels * update the pipelimit (ssthresh) if it has been updated 29144378Skarels * already or if a pipesize was specified & the threshhold 29244378Skarels * got below half the pipesize. I.e., wait for bad news 29344378Skarels * before we start updating, then update on both good 29444378Skarels * and bad news. 29544378Skarels */ 29644378Skarels if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 29744378Skarels (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || 29844378Skarels i < (rt->rt_rmx.rmx_sendpipe / 2)) { 29944378Skarels /* 30044378Skarels * convert the limit from user data bytes to 30144378Skarels * packets then to packet data bytes. 30244378Skarels */ 30344378Skarels i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 30444378Skarels if (i < 2) 30544378Skarels i = 2; 30644378Skarels i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 30744378Skarels if (rt->rt_rmx.rmx_ssthresh) 30844378Skarels rt->rt_rmx.rmx_ssthresh = 30944378Skarels (rt->rt_rmx.rmx_ssthresh + i) / 2; 31044378Skarels else 31144378Skarels rt->rt_rmx.rmx_ssthresh = i; 31244378Skarels } 31344378Skarels } 31444378Skarels #endif RTV_RTT 31544378Skarels /* free the reassembly queue, if any */ 3165075Swnj t = tp->seg_next; 31712422Ssam while (t != (struct tcpiphdr *)tp) { 31812422Ssam t = (struct tcpiphdr *)t->ti_next; 31944378Skarels m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 32012422Ssam remque(t->ti_prev); 32112422Ssam m_freem(m); 32212422Ssam } 3235089Swnj if (tp->t_template) 3245075Swnj (void) m_free(dtom(tp->t_template)); 3255075Swnj (void) m_free(dtom(tp)); 3265261Swnj inp->inp_ppcb = 0; 3276472Sroot soisdisconnected(so); 32844378Skarels /* clobber input pcb cache if we're closing the cached connection */ 32944378Skarels if (inp == tcp_last_inpcb) 33044378Skarels tcp_last_inpcb = &tcb; 3315269Sroot in_pcbdetach(inp); 33230524Skarels tcpstat.tcps_closed++; 33310395Ssam return ((struct tcpcb *)0); 3345075Swnj } 3355075Swnj 3365075Swnj tcp_drain() 3375075Swnj { 3385075Swnj 3395075Swnj } 3405075Swnj 34130233Skarels /* 34230233Skarels * Notify a tcp user of an asynchronous error; 34344378Skarels * store error as soft error, but wake up user 34444378Skarels * (for now, won't do anything until can select for soft error). 34530233Skarels */ 34644378Skarels tcp_notify(inp, error) 34730233Skarels register struct inpcb *inp; 34844378Skarels int error; 34930233Skarels { 35030233Skarels 35144378Skarels ((struct tcpcb *)inp->inp_ppcb)->t_softerror = error; 35230233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 35330233Skarels sorwakeup(inp->inp_socket); 35430233Skarels sowwakeup(inp->inp_socket); 35530233Skarels } 35640691Skarels 35740691Skarels tcp_ctlinput(cmd, sa, ip) 3586584Ssam int cmd; 35924818Skarels struct sockaddr *sa; 36040691Skarels register struct ip *ip; 3615075Swnj { 36240691Skarels register struct tcphdr *th; 36340691Skarels extern struct in_addr zeroin_addr; 3646591Ssam extern u_char inetctlerrmap[]; 36540691Skarels int (*notify)() = tcp_notify, tcp_quench(); 3666591Ssam 36740691Skarels if (cmd == PRC_QUENCH) 36840691Skarels notify = tcp_quench; 36940691Skarels else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) 3706591Ssam return; 37140691Skarels if (ip) { 37240691Skarels th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 37340691Skarels in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 37440691Skarels cmd, notify); 37540691Skarels } else 37640691Skarels in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 3775075Swnj } 37817359Skarels 37917359Skarels /* 38017359Skarels * When a source quench is received, close congestion window 38131442Skarels * to one segment. We will gradually open it again as we proceed. 38217359Skarels */ 38317359Skarels tcp_quench(inp) 38417359Skarels struct inpcb *inp; 38517359Skarels { 38617359Skarels struct tcpcb *tp = intotcpcb(inp); 38717359Skarels 38824818Skarels if (tp) 38931442Skarels tp->snd_cwnd = tp->t_maxseg; 39017359Skarels } 391