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*56531Sbostic * @(#)tcp_subr.c 7.23 (Berkeley) 10/11/92 823193Smckusick */ 95068Swnj 10*56531Sbostic #include <param.h> 11*56531Sbostic #include <proc.h> 12*56531Sbostic #include <systm.h> 13*56531Sbostic #include <malloc.h> 14*56531Sbostic #include <mbuf.h> 15*56531Sbostic #include <socket.h> 16*56531Sbostic #include <socketvar.h> 17*56531Sbostic #include <protosw.h> 18*56531Sbostic #include <errno.h> 1910896Ssam 20*56531Sbostic #include <net/route.h> 21*56531Sbostic #include <net/if.h> 2210896Ssam 23*56531Sbostic #include <netinet/in.h> 24*56531Sbostic #include <netinet/in_systm.h> 25*56531Sbostic #include <netinet/ip.h> 26*56531Sbostic #include <netinet/in_pcb.h> 27*56531Sbostic #include <netinet/ip_var.h> 28*56531Sbostic #include <netinet/ip_icmp.h> 29*56531Sbostic #include <netinet/tcp.h> 30*56531Sbostic #include <netinet/tcp_fsm.h> 31*56531Sbostic #include <netinet/tcp_seq.h> 32*56531Sbostic #include <netinet/tcp_timer.h> 33*56531Sbostic #include <netinet/tcp_var.h> 34*56531Sbostic #include <netinet/tcpip.h> 355068Swnj 3644378Skarels /* patchable/settable parameters for tcp */ 3731395Skarels int tcp_ttl = TCP_TTL; 3844378Skarels int tcp_mssdflt = TCP_MSS; 3944378Skarels int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 4031395Skarels 4144378Skarels extern struct inpcb *tcp_last_inpcb; 4244378Skarels 435068Swnj /* 445068Swnj * Tcp initialization 455068Swnj */ 465068Swnj tcp_init() 475068Swnj { 485068Swnj 495068Swnj tcp_iss = 1; /* wrong */ 505068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 5140691Skarels if (max_protohdr < sizeof(struct tcpiphdr)) 5240691Skarels max_protohdr = sizeof(struct tcpiphdr); 5340691Skarels if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) 5440691Skarels panic("tcp_init"); 555068Swnj } 565068Swnj 575068Swnj /* 585068Swnj * Create template to be used to send tcp packets on a connection. 595068Swnj * Call after host entry created, allocates an mbuf and fills 605068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 615068Swnj * necessary when the connection is used. 625068Swnj */ 635068Swnj struct tcpiphdr * 645068Swnj tcp_template(tp) 655068Swnj struct tcpcb *tp; 665068Swnj { 675068Swnj register struct inpcb *inp = tp->t_inpcb; 685068Swnj register struct mbuf *m; 695068Swnj register struct tcpiphdr *n; 705068Swnj 7126815Skarels if ((n = tp->t_template) == 0) { 7232101Skarels m = m_get(M_DONTWAIT, MT_HEADER); 7326815Skarels if (m == NULL) 7426815Skarels return (0); 7526815Skarels m->m_len = sizeof (struct tcpiphdr); 7626815Skarels n = mtod(m, struct tcpiphdr *); 7726815Skarels } 785068Swnj n->ti_next = n->ti_prev = 0; 795068Swnj n->ti_x1 = 0; 805068Swnj n->ti_pr = IPPROTO_TCP; 815068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 825068Swnj n->ti_src = inp->inp_laddr; 835068Swnj n->ti_dst = inp->inp_faddr; 845068Swnj n->ti_sport = inp->inp_lport; 855068Swnj n->ti_dport = inp->inp_fport; 865068Swnj n->ti_seq = 0; 875089Swnj n->ti_ack = 0; 885068Swnj n->ti_x2 = 0; 895068Swnj n->ti_off = 5; 905068Swnj n->ti_flags = 0; 915068Swnj n->ti_win = 0; 925068Swnj n->ti_sum = 0; 935068Swnj n->ti_urp = 0; 945068Swnj return (n); 955068Swnj } 965068Swnj 975068Swnj /* 985164Swnj * Send a single message to the TCP at address specified by 9944378Skarels * the given TCP/IP header. If m == 0, then we make a copy 1005164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 1015164Swnj * This is used to force keep alive messages out using the TCP 1025164Swnj * template for a connection tp->t_template. If flags are given 1035164Swnj * then we send a message back to the TCP which originated the 1045164Swnj * segment ti, and discard the mbuf containing it and any other 1055164Swnj * attached mbufs. 1065164Swnj * 1075164Swnj * In any case the ack and sequence number of the transmitted 1085164Swnj * segment are as specified by the parameters. 1095068Swnj */ 11040691Skarels tcp_respond(tp, ti, m, ack, seq, flags) 1115392Swnj struct tcpcb *tp; 1125068Swnj register struct tcpiphdr *ti; 11340691Skarels register struct mbuf *m; 1145089Swnj tcp_seq ack, seq; 1155068Swnj int flags; 1165068Swnj { 11744966Skarels register int tlen; 11844966Skarels int win = 0; 1196353Ssam struct route *ro = 0; 1205068Swnj 1216353Ssam if (tp) { 1225392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1236353Ssam ro = &tp->t_inpcb->inp_route; 1246353Ssam } 12540691Skarels if (m == 0) { 12640691Skarels m = m_gethdr(M_DONTWAIT, MT_HEADER); 12710144Ssam if (m == NULL) 1285164Swnj return; 12931727Skarels #ifdef TCP_COMPAT_42 13031727Skarels tlen = 1; 13131727Skarels #else 13231727Skarels tlen = 0; 13331727Skarels #endif 13440691Skarels m->m_data += max_linkhdr; 1355164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1365164Swnj ti = mtod(m, struct tcpiphdr *); 1375164Swnj flags = TH_ACK; 1385164Swnj } else { 1395164Swnj m_freem(m->m_next); 1405164Swnj m->m_next = 0; 14140691Skarels m->m_data = (caddr_t)ti; 14244378Skarels m->m_len = sizeof (struct tcpiphdr); 14330762Skarels tlen = 0; 1445089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1455164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1465164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1475068Swnj #undef xchg 1485164Swnj } 14944966Skarels ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 15044966Skarels tlen += sizeof (struct tcpiphdr); 15144966Skarels m->m_len = tlen; 15244966Skarels m->m_pkthdr.len = tlen; 15344966Skarels m->m_pkthdr.rcvif = (struct ifnet *) 0; 1545089Swnj ti->ti_next = ti->ti_prev = 0; 1555089Swnj ti->ti_x1 = 0; 1568942Sroot ti->ti_seq = htonl(seq); 1578942Sroot ti->ti_ack = htonl(ack); 1585089Swnj ti->ti_x2 = 0; 1595089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1605068Swnj ti->ti_flags = flags; 1619185Ssam ti->ti_win = htons((u_short)win); 1625392Swnj ti->ti_urp = 0; 16344966Skarels ti->ti_sum = in_cksum(m, tlen); 16444966Skarels ((struct ip *)ti)->ip_len = tlen; 16531395Skarels ((struct ip *)ti)->ip_ttl = tcp_ttl; 1666353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1675068Swnj } 1685075Swnj 1695089Swnj /* 1705089Swnj * Create a new TCP control block, making an 1715089Swnj * empty reassembly queue and hooking it to the argument 1725089Swnj * protocol control block. 1735089Swnj */ 1745075Swnj struct tcpcb * 1755075Swnj tcp_newtcpcb(inp) 1765075Swnj struct inpcb *inp; 1775075Swnj { 1789644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1795075Swnj register struct tcpcb *tp; 1805075Swnj 18110144Ssam if (m == NULL) 18210144Ssam return ((struct tcpcb *)0); 1835075Swnj tp = mtod(m, struct tcpcb *); 1845075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 18544378Skarels tp->t_maxseg = tcp_mssdflt; 18644378Skarels 1876470Sroot tp->t_flags = 0; /* sends options! */ 1885075Swnj tp->t_inpcb = inp; 18931726Skarels /* 19031757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 19131757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 19231757Skarels * reasonable initial retransmit time. 19331726Skarels */ 19431757Skarels tp->t_srtt = TCPTV_SRTTBASE; 19544378Skarels tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 19644378Skarels tp->t_rttmin = TCPTV_MIN; 19732374Skarels TCPT_RANGESET(tp->t_rxtcur, 19832374Skarels ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 19932374Skarels TCPTV_MIN, TCPTV_REXMTMAX); 20044378Skarels tp->snd_cwnd = TCP_MAXWIN; 20144378Skarels tp->snd_ssthresh = TCP_MAXWIN; 20244378Skarels inp->inp_ip.ip_ttl = tcp_ttl; 2035075Swnj inp->inp_ppcb = (caddr_t)tp; 2045075Swnj return (tp); 2055075Swnj } 2065075Swnj 2075089Swnj /* 2085089Swnj * Drop a TCP connection, reporting 2095089Swnj * the specified error. If connection is synchronized, 2105089Swnj * then send a RST to peer. 2115089Swnj */ 21210395Ssam struct tcpcb * 2135075Swnj tcp_drop(tp, errno) 21410395Ssam register struct tcpcb *tp; 2155075Swnj int errno; 2165075Swnj { 2175075Swnj struct socket *so = tp->t_inpcb->inp_socket; 2185075Swnj 2195286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 2205075Swnj tp->t_state = TCPS_CLOSED; 2218776Sroot (void) tcp_output(tp); 22230524Skarels tcpstat.tcps_drops++; 22330524Skarels } else 22430524Skarels tcpstat.tcps_conndrops++; 22544378Skarels if (errno == ETIMEDOUT && tp->t_softerror) 22644378Skarels errno = tp->t_softerror; 2275075Swnj so->so_error = errno; 22810395Ssam return (tcp_close(tp)); 2295075Swnj } 2305075Swnj 2315089Swnj /* 2325089Swnj * Close a TCP control block: 2335089Swnj * discard all space held by the tcp 2345089Swnj * discard internet protocol block 2355089Swnj * wake up any sleepers 2365089Swnj */ 23710395Ssam struct tcpcb * 2385075Swnj tcp_close(tp) 2395075Swnj register struct tcpcb *tp; 2405075Swnj { 2415075Swnj register struct tcpiphdr *t; 2425261Swnj struct inpcb *inp = tp->t_inpcb; 2435261Swnj struct socket *so = inp->inp_socket; 24412422Ssam register struct mbuf *m; 24544378Skarels #ifdef RTV_RTT 24644378Skarels register struct rtentry *rt; 2475075Swnj 24844378Skarels /* 24944378Skarels * If we sent enough data to get some meaningful characteristics, 25044378Skarels * save them in the routing entry. 'Enough' is arbitrarily 25145676Skarels * defined as the sendpipesize (default 4K) * 16. This would 25244378Skarels * give us 16 rtt samples assuming we only get one sample per 25344378Skarels * window (the usual case on a long haul net). 16 samples is 25444378Skarels * enough for the srtt filter to converge to within 5% of the correct 25544378Skarels * value; fewer samples and we could save a very bogus rtt. 25644378Skarels * 25744378Skarels * Don't update the default route's characteristics and don't 25844378Skarels * update anything that the user "locked". 25944378Skarels */ 26045676Skarels if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 26144378Skarels (rt = inp->inp_route.ro_rt) && 26245676Skarels ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != 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) 34754810Skarels struct inpcb *inp; 34844378Skarels int error; 34930233Skarels { 35054810Skarels register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 35154810Skarels register struct socket *so = inp->inp_socket; 35230233Skarels 35354810Skarels /* 35454810Skarels * If connection hasn't completed, has retransmitted several times, 35554810Skarels * and receives a second error, give up now. This is better 35654810Skarels * than waiting a long time to establish a connection that 35754810Skarels * can never complete. 35854810Skarels */ 35954810Skarels if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 36054810Skarels tp->t_softerror) 36154810Skarels so->so_error = error; 36254810Skarels else 36354810Skarels tp->t_softerror = error; 36454810Skarels wakeup((caddr_t) &so->so_timeo); 36554810Skarels sorwakeup(so); 36654810Skarels sowwakeup(so); 36730233Skarels } 36840691Skarels 36940691Skarels tcp_ctlinput(cmd, sa, ip) 3706584Ssam int cmd; 37124818Skarels struct sockaddr *sa; 37240691Skarels register struct ip *ip; 3735075Swnj { 37440691Skarels register struct tcphdr *th; 37540691Skarels extern struct in_addr zeroin_addr; 3766591Ssam extern u_char inetctlerrmap[]; 37740691Skarels int (*notify)() = tcp_notify, tcp_quench(); 3786591Ssam 37940691Skarels if (cmd == PRC_QUENCH) 38040691Skarels notify = tcp_quench; 38140691Skarels else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) 3826591Ssam return; 38340691Skarels if (ip) { 38440691Skarels th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 38540691Skarels in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 38640691Skarels cmd, notify); 38740691Skarels } else 38840691Skarels in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 3895075Swnj } 39017359Skarels 39117359Skarels /* 39217359Skarels * When a source quench is received, close congestion window 39331442Skarels * to one segment. We will gradually open it again as we proceed. 39417359Skarels */ 39517359Skarels tcp_quench(inp) 39617359Skarels struct inpcb *inp; 39717359Skarels { 39817359Skarels struct tcpcb *tp = intotcpcb(inp); 39917359Skarels 40024818Skarels if (tp) 40131442Skarels tp->snd_cwnd = tp->t_maxseg; 40217359Skarels } 403