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*60347Storek * @(#)tcp_subr.c 7.27 (Berkeley) 05/24/93 823193Smckusick */ 95068Swnj 1056564Ssklower #include <sys/param.h> 1156564Ssklower #include <sys/proc.h> 1256564Ssklower #include <sys/systm.h> 1356564Ssklower #include <sys/malloc.h> 1456564Ssklower #include <sys/mbuf.h> 1556564Ssklower #include <sys/socket.h> 1656564Ssklower #include <sys/socketvar.h> 1756564Ssklower #include <sys/protosw.h> 1856564Ssklower #include <sys/errno.h> 1910896Ssam 2056531Sbostic #include <net/route.h> 2156531Sbostic #include <net/if.h> 2210896Ssam 2356531Sbostic #include <netinet/in.h> 2456531Sbostic #include <netinet/in_systm.h> 2556531Sbostic #include <netinet/ip.h> 2656531Sbostic #include <netinet/in_pcb.h> 2756531Sbostic #include <netinet/ip_var.h> 2856531Sbostic #include <netinet/ip_icmp.h> 2956531Sbostic #include <netinet/tcp.h> 3056531Sbostic #include <netinet/tcp_fsm.h> 3156531Sbostic #include <netinet/tcp_seq.h> 3256531Sbostic #include <netinet/tcp_timer.h> 3356531Sbostic #include <netinet/tcp_var.h> 3456531Sbostic #include <netinet/tcpip.h> 355068Swnj 3644378Skarels /* patchable/settable parameters for tcp */ 3744378Skarels int tcp_mssdflt = TCP_MSS; 3844378Skarels int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; 3957433Sandrew int tcp_do_rfc1323 = 1; 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; 16157433Sandrew if (tp) 16257433Sandrew ti->ti_win = htons((u_short) (win >> tp->rcv_scale)); 16357433Sandrew else 16457433Sandrew ti->ti_win = htons((u_short)win); 1655392Swnj ti->ti_urp = 0; 16657433Sandrew ti->ti_sum = 0; 16744966Skarels ti->ti_sum = in_cksum(m, tlen); 16844966Skarels ((struct ip *)ti)->ip_len = tlen; 16959132Smckusick ((struct ip *)ti)->ip_ttl = ip_defttl; 1706353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1715068Swnj } 1725075Swnj 1735089Swnj /* 1745089Swnj * Create a new TCP control block, making an 1755089Swnj * empty reassembly queue and hooking it to the argument 1765089Swnj * protocol control block. 1775089Swnj */ 1785075Swnj struct tcpcb * 1795075Swnj tcp_newtcpcb(inp) 1805075Swnj struct inpcb *inp; 1815075Swnj { 1825075Swnj register struct tcpcb *tp; 1835075Swnj 18457433Sandrew tp = malloc(sizeof(*tp), M_PCB, M_NOWAIT); 18557433Sandrew if (tp == NULL) 18610144Ssam return ((struct tcpcb *)0); 18757433Sandrew bzero((char *) tp, sizeof(struct tcpcb)); 1885075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 18944378Skarels tp->t_maxseg = tcp_mssdflt; 19044378Skarels 19157433Sandrew tp->t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0; 1925075Swnj tp->t_inpcb = inp; 19331726Skarels /* 19431757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 19531757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 19631757Skarels * reasonable initial retransmit time. 19731726Skarels */ 19831757Skarels tp->t_srtt = TCPTV_SRTTBASE; 19944378Skarels tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; 20044378Skarels tp->t_rttmin = TCPTV_MIN; 20132374Skarels TCPT_RANGESET(tp->t_rxtcur, 20232374Skarels ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 20332374Skarels TCPTV_MIN, TCPTV_REXMTMAX); 20457433Sandrew tp->snd_cwnd = TCP_MAXWIN << TCP_MAX_WINSHIFT; 20557433Sandrew tp->snd_ssthresh = TCP_MAXWIN << TCP_MAX_WINSHIFT; 20659132Smckusick inp->inp_ip.ip_ttl = ip_defttl; 2075075Swnj inp->inp_ppcb = (caddr_t)tp; 2085075Swnj return (tp); 2095075Swnj } 2105075Swnj 2115089Swnj /* 2125089Swnj * Drop a TCP connection, reporting 2135089Swnj * the specified error. If connection is synchronized, 2145089Swnj * then send a RST to peer. 2155089Swnj */ 21610395Ssam struct tcpcb * 2175075Swnj tcp_drop(tp, errno) 21810395Ssam register struct tcpcb *tp; 2195075Swnj int errno; 2205075Swnj { 2215075Swnj struct socket *so = tp->t_inpcb->inp_socket; 2225075Swnj 2235286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 2245075Swnj tp->t_state = TCPS_CLOSED; 2258776Sroot (void) tcp_output(tp); 22630524Skarels tcpstat.tcps_drops++; 22730524Skarels } else 22830524Skarels tcpstat.tcps_conndrops++; 22944378Skarels if (errno == ETIMEDOUT && tp->t_softerror) 23044378Skarels errno = tp->t_softerror; 2315075Swnj so->so_error = errno; 23210395Ssam return (tcp_close(tp)); 2335075Swnj } 2345075Swnj 2355089Swnj /* 2365089Swnj * Close a TCP control block: 2375089Swnj * discard all space held by the tcp 2385089Swnj * discard internet protocol block 2395089Swnj * wake up any sleepers 2405089Swnj */ 24110395Ssam struct tcpcb * 2425075Swnj tcp_close(tp) 2435075Swnj register struct tcpcb *tp; 2445075Swnj { 2455075Swnj register struct tcpiphdr *t; 2465261Swnj struct inpcb *inp = tp->t_inpcb; 2475261Swnj struct socket *so = inp->inp_socket; 24812422Ssam register struct mbuf *m; 24944378Skarels #ifdef RTV_RTT 25044378Skarels register struct rtentry *rt; 2515075Swnj 25244378Skarels /* 25344378Skarels * If we sent enough data to get some meaningful characteristics, 25444378Skarels * save them in the routing entry. 'Enough' is arbitrarily 25545676Skarels * defined as the sendpipesize (default 4K) * 16. This would 25644378Skarels * give us 16 rtt samples assuming we only get one sample per 25744378Skarels * window (the usual case on a long haul net). 16 samples is 25844378Skarels * enough for the srtt filter to converge to within 5% of the correct 25944378Skarels * value; fewer samples and we could save a very bogus rtt. 26044378Skarels * 26144378Skarels * Don't update the default route's characteristics and don't 26244378Skarels * update anything that the user "locked". 26344378Skarels */ 26445676Skarels if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && 26544378Skarels (rt = inp->inp_route.ro_rt) && 26645676Skarels ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { 26744378Skarels register u_long i; 26844378Skarels 26944378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { 27044378Skarels i = tp->t_srtt * 27144378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); 27244378Skarels if (rt->rt_rmx.rmx_rtt && i) 27344378Skarels /* 27444378Skarels * filter this update to half the old & half 27544378Skarels * the new values, converting scale. 27644378Skarels * See route.h and tcp_var.h for a 27744378Skarels * description of the scaling constants. 27844378Skarels */ 27944378Skarels rt->rt_rmx.rmx_rtt = 28044378Skarels (rt->rt_rmx.rmx_rtt + i) / 2; 28144378Skarels else 28244378Skarels rt->rt_rmx.rmx_rtt = i; 28344378Skarels } 28444378Skarels if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { 28544378Skarels i = tp->t_rttvar * 28644378Skarels (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); 28744378Skarels if (rt->rt_rmx.rmx_rttvar && i) 28844378Skarels rt->rt_rmx.rmx_rttvar = 28944378Skarels (rt->rt_rmx.rmx_rttvar + i) / 2; 29044378Skarels else 29144378Skarels rt->rt_rmx.rmx_rttvar = i; 29244378Skarels } 29344378Skarels /* 29444378Skarels * update the pipelimit (ssthresh) if it has been updated 29544378Skarels * already or if a pipesize was specified & the threshhold 29644378Skarels * got below half the pipesize. I.e., wait for bad news 29744378Skarels * before we start updating, then update on both good 29844378Skarels * and bad news. 29944378Skarels */ 30044378Skarels if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && 30144378Skarels (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || 30244378Skarels i < (rt->rt_rmx.rmx_sendpipe / 2)) { 30344378Skarels /* 30444378Skarels * convert the limit from user data bytes to 30544378Skarels * packets then to packet data bytes. 30644378Skarels */ 30744378Skarels i = (i + tp->t_maxseg / 2) / tp->t_maxseg; 30844378Skarels if (i < 2) 30944378Skarels i = 2; 31044378Skarels i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); 31144378Skarels if (rt->rt_rmx.rmx_ssthresh) 31244378Skarels rt->rt_rmx.rmx_ssthresh = 31344378Skarels (rt->rt_rmx.rmx_ssthresh + i) / 2; 31444378Skarels else 31544378Skarels rt->rt_rmx.rmx_ssthresh = i; 31644378Skarels } 31744378Skarels } 318*60347Storek #endif /* RTV_RTT */ 31944378Skarels /* free the reassembly queue, if any */ 3205075Swnj t = tp->seg_next; 32112422Ssam while (t != (struct tcpiphdr *)tp) { 32212422Ssam t = (struct tcpiphdr *)t->ti_next; 32344378Skarels m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); 32412422Ssam remque(t->ti_prev); 32512422Ssam m_freem(m); 32612422Ssam } 3275089Swnj if (tp->t_template) 3285075Swnj (void) m_free(dtom(tp->t_template)); 32957433Sandrew free(tp, M_PCB); 3305261Swnj inp->inp_ppcb = 0; 3316472Sroot soisdisconnected(so); 33244378Skarels /* clobber input pcb cache if we're closing the cached connection */ 33344378Skarels if (inp == tcp_last_inpcb) 33444378Skarels tcp_last_inpcb = &tcb; 3355269Sroot in_pcbdetach(inp); 33630524Skarels tcpstat.tcps_closed++; 33710395Ssam return ((struct tcpcb *)0); 3385075Swnj } 3395075Swnj 3405075Swnj tcp_drain() 3415075Swnj { 3425075Swnj 3435075Swnj } 3445075Swnj 34530233Skarels /* 34630233Skarels * Notify a tcp user of an asynchronous error; 34744378Skarels * store error as soft error, but wake up user 34844378Skarels * (for now, won't do anything until can select for soft error). 34930233Skarels */ 35044378Skarels tcp_notify(inp, error) 35154810Skarels struct inpcb *inp; 35244378Skarels int error; 35330233Skarels { 35454810Skarels register struct tcpcb *tp = (struct tcpcb *)inp->inp_ppcb; 35554810Skarels register struct socket *so = inp->inp_socket; 35630233Skarels 35754810Skarels /* 35857433Sandrew * Ignore some errors if we are hooked up. 35954810Skarels * If connection hasn't completed, has retransmitted several times, 36054810Skarels * and receives a second error, give up now. This is better 36154810Skarels * than waiting a long time to establish a connection that 36254810Skarels * can never complete. 36354810Skarels */ 36457433Sandrew if (tp->t_state == TCPS_ESTABLISHED && 36557433Sandrew (error == EHOSTUNREACH || error == ENETUNREACH || 36657433Sandrew error == EHOSTDOWN)) { 36757433Sandrew return; 36857433Sandrew } else if (tp->t_state < TCPS_ESTABLISHED && tp->t_rxtshift > 3 && 36954810Skarels tp->t_softerror) 37054810Skarels so->so_error = error; 37157433Sandrew else 37254810Skarels tp->t_softerror = error; 37354810Skarels wakeup((caddr_t) &so->so_timeo); 37454810Skarels sorwakeup(so); 37554810Skarels sowwakeup(so); 37630233Skarels } 37740691Skarels 37840691Skarels tcp_ctlinput(cmd, sa, ip) 3796584Ssam int cmd; 38024818Skarels struct sockaddr *sa; 38140691Skarels register struct ip *ip; 3825075Swnj { 38340691Skarels register struct tcphdr *th; 38440691Skarels extern struct in_addr zeroin_addr; 3856591Ssam extern u_char inetctlerrmap[]; 38640691Skarels int (*notify)() = tcp_notify, tcp_quench(); 3876591Ssam 38840691Skarels if (cmd == PRC_QUENCH) 38940691Skarels notify = tcp_quench; 39057433Sandrew else if (!PRC_IS_REDIRECT(cmd) && 39157433Sandrew ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0)) 3926591Ssam return; 39340691Skarels if (ip) { 39440691Skarels th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); 39540691Skarels in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, 39640691Skarels cmd, notify); 39740691Skarels } else 39840691Skarels in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); 3995075Swnj } 40017359Skarels 40117359Skarels /* 40217359Skarels * When a source quench is received, close congestion window 40331442Skarels * to one segment. We will gradually open it again as we proceed. 40417359Skarels */ 40517359Skarels tcp_quench(inp) 40617359Skarels struct inpcb *inp; 40717359Skarels { 40817359Skarels struct tcpcb *tp = intotcpcb(inp); 40917359Skarels 41024818Skarels if (tp) 41131442Skarels tp->snd_cwnd = tp->t_maxseg; 41217359Skarels } 413