1*21119Skarels /* tcp_subr.c 6.5 85/05/27 */ 25068Swnj 317064Sbloom #include "param.h" 417064Sbloom #include "systm.h" 517064Sbloom #include "mbuf.h" 617064Sbloom #include "socket.h" 717064Sbloom #include "socketvar.h" 817064Sbloom #include "protosw.h" 917064Sbloom #include "errno.h" 1010896Ssam 1110896Ssam #include "../net/route.h" 1210896Ssam #include "../net/if.h" 1310896Ssam 1417064Sbloom #include "in.h" 1517064Sbloom #include "in_pcb.h" 1617064Sbloom #include "in_systm.h" 1717064Sbloom #include "ip.h" 1817064Sbloom #include "ip_var.h" 1917064Sbloom #include "ip_icmp.h" 2017064Sbloom #include "tcp.h" 2117064Sbloom #include "tcp_fsm.h" 2217064Sbloom #include "tcp_seq.h" 2317064Sbloom #include "tcp_timer.h" 2417064Sbloom #include "tcp_var.h" 2517064Sbloom #include "tcpip.h" 265068Swnj 275068Swnj /* 285068Swnj * Tcp initialization 295068Swnj */ 305068Swnj tcp_init() 315068Swnj { 325068Swnj 335068Swnj tcp_iss = 1; /* wrong */ 345068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 355164Swnj tcp_alpha = TCP_ALPHA; 365164Swnj tcp_beta = TCP_BETA; 375068Swnj } 385068Swnj 395068Swnj /* 405068Swnj * Create template to be used to send tcp packets on a connection. 415068Swnj * Call after host entry created, allocates an mbuf and fills 425068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 435068Swnj * necessary when the connection is used. 445068Swnj */ 455068Swnj struct tcpiphdr * 465068Swnj tcp_template(tp) 475068Swnj struct tcpcb *tp; 485068Swnj { 495068Swnj register struct inpcb *inp = tp->t_inpcb; 505068Swnj register struct mbuf *m; 515068Swnj register struct tcpiphdr *n; 525068Swnj 539644Ssam m = m_get(M_WAIT, MT_HEADER); 5410144Ssam if (m == NULL) 555068Swnj return (0); 565068Swnj m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 575068Swnj m->m_len = sizeof (struct tcpiphdr); 585068Swnj n = mtod(m, struct tcpiphdr *); 595068Swnj n->ti_next = n->ti_prev = 0; 605068Swnj n->ti_x1 = 0; 615068Swnj n->ti_pr = IPPROTO_TCP; 625068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 635068Swnj n->ti_src = inp->inp_laddr; 645068Swnj n->ti_dst = inp->inp_faddr; 655068Swnj n->ti_sport = inp->inp_lport; 665068Swnj n->ti_dport = inp->inp_fport; 675068Swnj n->ti_seq = 0; 685089Swnj n->ti_ack = 0; 695068Swnj n->ti_x2 = 0; 705068Swnj n->ti_off = 5; 715068Swnj n->ti_flags = 0; 725068Swnj n->ti_win = 0; 735068Swnj n->ti_sum = 0; 745068Swnj n->ti_urp = 0; 755068Swnj return (n); 765068Swnj } 775068Swnj 785068Swnj /* 795164Swnj * Send a single message to the TCP at address specified by 805164Swnj * the given TCP/IP header. If flags==0, then we make a copy 815164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 825164Swnj * This is used to force keep alive messages out using the TCP 835164Swnj * template for a connection tp->t_template. If flags are given 845164Swnj * then we send a message back to the TCP which originated the 855164Swnj * segment ti, and discard the mbuf containing it and any other 865164Swnj * attached mbufs. 875164Swnj * 885164Swnj * In any case the ack and sequence number of the transmitted 895164Swnj * segment are as specified by the parameters. 905068Swnj */ 915392Swnj tcp_respond(tp, ti, ack, seq, flags) 925392Swnj struct tcpcb *tp; 935068Swnj register struct tcpiphdr *ti; 945089Swnj tcp_seq ack, seq; 955068Swnj int flags; 965068Swnj { 975164Swnj struct mbuf *m; 986212Swnj int win = 0, tlen; 996353Ssam struct route *ro = 0; 1005068Swnj 1016353Ssam if (tp) { 1025392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1036353Ssam ro = &tp->t_inpcb->inp_route; 1046353Ssam } 1055164Swnj if (flags == 0) { 1069644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 10710144Ssam if (m == NULL) 1085164Swnj return; 1096212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1105164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1115164Swnj ti = mtod(m, struct tcpiphdr *); 1125164Swnj flags = TH_ACK; 1136212Swnj tlen = 1; 1145164Swnj } else { 1155245Sroot m = dtom(ti); 1165164Swnj m_freem(m->m_next); 1175164Swnj m->m_next = 0; 1186117Swnj m->m_off = (int)ti - (int)m; 1195164Swnj m->m_len = sizeof (struct tcpiphdr); 1205089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1215164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1225164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1235068Swnj #undef xchg 1246212Swnj tlen = 0; 1255164Swnj } 1265089Swnj ti->ti_next = ti->ti_prev = 0; 1275089Swnj ti->ti_x1 = 0; 1289185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1298942Sroot ti->ti_seq = htonl(seq); 1308942Sroot ti->ti_ack = htonl(ack); 1315089Swnj ti->ti_x2 = 0; 1325089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1335068Swnj ti->ti_flags = flags; 1349185Ssam ti->ti_win = htons((u_short)win); 1355392Swnj ti->ti_urp = 0; 1366304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1376212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1385089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1396353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1405068Swnj } 1415075Swnj 1425089Swnj /* 1435089Swnj * Create a new TCP control block, making an 1445089Swnj * empty reassembly queue and hooking it to the argument 1455089Swnj * protocol control block. 1465089Swnj */ 1475075Swnj struct tcpcb * 1485075Swnj tcp_newtcpcb(inp) 1495075Swnj struct inpcb *inp; 1505075Swnj { 1519644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1525075Swnj register struct tcpcb *tp; 1535075Swnj 15410144Ssam if (m == NULL) 15510144Ssam return ((struct tcpcb *)0); 1565075Swnj tp = mtod(m, struct tcpcb *); 1575075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 15817317Skarels tp->t_maxseg = TCP_MSS; 1596470Sroot tp->t_flags = 0; /* sends options! */ 1605075Swnj tp->t_inpcb = inp; 16117317Skarels tp->t_srtt = TCPTV_SRTTBASE; 16217359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 1635075Swnj inp->inp_ppcb = (caddr_t)tp; 1645075Swnj return (tp); 1655075Swnj } 1665075Swnj 1675089Swnj /* 1685089Swnj * Drop a TCP connection, reporting 1695089Swnj * the specified error. If connection is synchronized, 1705089Swnj * then send a RST to peer. 1715089Swnj */ 17210395Ssam struct tcpcb * 1735075Swnj tcp_drop(tp, errno) 17410395Ssam register struct tcpcb *tp; 1755075Swnj int errno; 1765075Swnj { 1775075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1785075Swnj 1795286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1805075Swnj tp->t_state = TCPS_CLOSED; 1818776Sroot (void) tcp_output(tp); 1825075Swnj } 1835075Swnj so->so_error = errno; 18410395Ssam return (tcp_close(tp)); 1855075Swnj } 1865075Swnj 1876584Ssam tcp_abort(inp) 1886584Ssam struct inpcb *inp; 1896584Ssam { 1908640Sroot 19110395Ssam (void) tcp_close((struct tcpcb *)inp->inp_ppcb); 1926584Ssam } 1936584Ssam 1945089Swnj /* 1955089Swnj * Close a TCP control block: 1965089Swnj * discard all space held by the tcp 1975089Swnj * discard internet protocol block 1985089Swnj * wake up any sleepers 1995089Swnj */ 20010395Ssam struct tcpcb * 2015075Swnj tcp_close(tp) 2025075Swnj register struct tcpcb *tp; 2035075Swnj { 2045075Swnj register struct tcpiphdr *t; 2055261Swnj struct inpcb *inp = tp->t_inpcb; 2065261Swnj struct socket *so = inp->inp_socket; 20712422Ssam register struct mbuf *m; 2085075Swnj 2095075Swnj t = tp->seg_next; 21012422Ssam while (t != (struct tcpiphdr *)tp) { 21112422Ssam t = (struct tcpiphdr *)t->ti_next; 21212422Ssam m = dtom(t->ti_prev); 21312422Ssam remque(t->ti_prev); 21412422Ssam m_freem(m); 21512422Ssam } 2165089Swnj if (tp->t_template) 2175075Swnj (void) m_free(dtom(tp->t_template)); 2185089Swnj if (tp->t_tcpopt) 2195089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2205089Swnj if (tp->t_ipopt) 2215089Swnj (void) m_free(dtom(tp->t_ipopt)); 2225075Swnj (void) m_free(dtom(tp)); 2235261Swnj inp->inp_ppcb = 0; 2246472Sroot soisdisconnected(so); 2255269Sroot in_pcbdetach(inp); 22610395Ssam return ((struct tcpcb *)0); 2275075Swnj } 2285075Swnj 2295075Swnj tcp_drain() 2305075Swnj { 2315075Swnj 2325075Swnj } 2335075Swnj 2346584Ssam tcp_ctlinput(cmd, arg) 2356584Ssam int cmd; 2366584Ssam caddr_t arg; 2375075Swnj { 238*21119Skarels struct in_addr *in; 2396591Ssam extern u_char inetctlerrmap[]; 240*21119Skarels int tcp_quench(), in_rtchange(); 2416591Ssam 2426591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2436591Ssam return; 2446591Ssam switch (cmd) { 2456591Ssam 2466591Ssam case PRC_ROUTEDEAD: 2476591Ssam break; 2486591Ssam 2496591Ssam case PRC_QUENCH: 250*21119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 251*21119Skarels in_pcbnotify(&tcb, in, 0, tcp_quench); 2526591Ssam break; 2536591Ssam 254*21119Skarels case PRC_REDIRECT_NET: 255*21119Skarels case PRC_REDIRECT_HOST: 256*21119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 257*21119Skarels in_pcbnotify(&tcb, in, 0, in_rtchange); 258*21119Skarels break; 259*21119Skarels 2606591Ssam case PRC_IFDOWN: 261*21119Skarels in = &((struct sockaddr_in *)arg)->sin_addr; 262*21119Skarels goto notify; 263*21119Skarels 2646591Ssam case PRC_HOSTDEAD: 2656591Ssam case PRC_HOSTUNREACH: 266*21119Skarels in = (struct in_addr *)arg; 267*21119Skarels goto notify; 2686591Ssam 2696591Ssam default: 270*21119Skarels if (inetctlerrmap[cmd] == 0) 271*21119Skarels return; /* XXX */ 272*21119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 273*21119Skarels notify: 274*21119Skarels in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 2756591Ssam } 2765075Swnj } 27717359Skarels 27817359Skarels /* 27917359Skarels * When a source quench is received, close congestion window 28017359Skarels * to 80% of the outstanding data (but not less than one segment). 28117359Skarels */ 28217359Skarels tcp_quench(inp) 28317359Skarels struct inpcb *inp; 28417359Skarels { 28517359Skarels struct tcpcb *tp = intotcpcb(inp); 28617359Skarels 28717359Skarels tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10, tp->t_maxseg); 28817359Skarels } 289