1*23193Smckusick /* 2*23193Smckusick * Copyright (c) 1982 Regents of the University of California. 3*23193Smckusick * All rights reserved. The Berkeley software License Agreement 4*23193Smckusick * specifies the terms and conditions for redistribution. 5*23193Smckusick * 6*23193Smckusick * @(#)tcp_subr.c 6.6 (Berkeley) 06/08/85 7*23193Smckusick */ 85068Swnj 917064Sbloom #include "param.h" 1017064Sbloom #include "systm.h" 1117064Sbloom #include "mbuf.h" 1217064Sbloom #include "socket.h" 1317064Sbloom #include "socketvar.h" 1417064Sbloom #include "protosw.h" 1517064Sbloom #include "errno.h" 1610896Ssam 1710896Ssam #include "../net/route.h" 1810896Ssam #include "../net/if.h" 1910896Ssam 2017064Sbloom #include "in.h" 2117064Sbloom #include "in_pcb.h" 2217064Sbloom #include "in_systm.h" 2317064Sbloom #include "ip.h" 2417064Sbloom #include "ip_var.h" 2517064Sbloom #include "ip_icmp.h" 2617064Sbloom #include "tcp.h" 2717064Sbloom #include "tcp_fsm.h" 2817064Sbloom #include "tcp_seq.h" 2917064Sbloom #include "tcp_timer.h" 3017064Sbloom #include "tcp_var.h" 3117064Sbloom #include "tcpip.h" 325068Swnj 335068Swnj /* 345068Swnj * Tcp initialization 355068Swnj */ 365068Swnj tcp_init() 375068Swnj { 385068Swnj 395068Swnj tcp_iss = 1; /* wrong */ 405068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 415164Swnj tcp_alpha = TCP_ALPHA; 425164Swnj tcp_beta = TCP_BETA; 435068Swnj } 445068Swnj 455068Swnj /* 465068Swnj * Create template to be used to send tcp packets on a connection. 475068Swnj * Call after host entry created, allocates an mbuf and fills 485068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 495068Swnj * necessary when the connection is used. 505068Swnj */ 515068Swnj struct tcpiphdr * 525068Swnj tcp_template(tp) 535068Swnj struct tcpcb *tp; 545068Swnj { 555068Swnj register struct inpcb *inp = tp->t_inpcb; 565068Swnj register struct mbuf *m; 575068Swnj register struct tcpiphdr *n; 585068Swnj 599644Ssam m = m_get(M_WAIT, MT_HEADER); 6010144Ssam if (m == NULL) 615068Swnj return (0); 625068Swnj m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 635068Swnj m->m_len = sizeof (struct tcpiphdr); 645068Swnj n = mtod(m, struct tcpiphdr *); 655068Swnj n->ti_next = n->ti_prev = 0; 665068Swnj n->ti_x1 = 0; 675068Swnj n->ti_pr = IPPROTO_TCP; 685068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 695068Swnj n->ti_src = inp->inp_laddr; 705068Swnj n->ti_dst = inp->inp_faddr; 715068Swnj n->ti_sport = inp->inp_lport; 725068Swnj n->ti_dport = inp->inp_fport; 735068Swnj n->ti_seq = 0; 745089Swnj n->ti_ack = 0; 755068Swnj n->ti_x2 = 0; 765068Swnj n->ti_off = 5; 775068Swnj n->ti_flags = 0; 785068Swnj n->ti_win = 0; 795068Swnj n->ti_sum = 0; 805068Swnj n->ti_urp = 0; 815068Swnj return (n); 825068Swnj } 835068Swnj 845068Swnj /* 855164Swnj * Send a single message to the TCP at address specified by 865164Swnj * the given TCP/IP header. If flags==0, then we make a copy 875164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 885164Swnj * This is used to force keep alive messages out using the TCP 895164Swnj * template for a connection tp->t_template. If flags are given 905164Swnj * then we send a message back to the TCP which originated the 915164Swnj * segment ti, and discard the mbuf containing it and any other 925164Swnj * attached mbufs. 935164Swnj * 945164Swnj * In any case the ack and sequence number of the transmitted 955164Swnj * segment are as specified by the parameters. 965068Swnj */ 975392Swnj tcp_respond(tp, ti, ack, seq, flags) 985392Swnj struct tcpcb *tp; 995068Swnj register struct tcpiphdr *ti; 1005089Swnj tcp_seq ack, seq; 1015068Swnj int flags; 1025068Swnj { 1035164Swnj struct mbuf *m; 1046212Swnj int win = 0, tlen; 1056353Ssam struct route *ro = 0; 1065068Swnj 1076353Ssam if (tp) { 1085392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1096353Ssam ro = &tp->t_inpcb->inp_route; 1106353Ssam } 1115164Swnj if (flags == 0) { 1129644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 11310144Ssam if (m == NULL) 1145164Swnj return; 1156212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1165164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1175164Swnj ti = mtod(m, struct tcpiphdr *); 1185164Swnj flags = TH_ACK; 1196212Swnj tlen = 1; 1205164Swnj } else { 1215245Sroot m = dtom(ti); 1225164Swnj m_freem(m->m_next); 1235164Swnj m->m_next = 0; 1246117Swnj m->m_off = (int)ti - (int)m; 1255164Swnj m->m_len = sizeof (struct tcpiphdr); 1265089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1275164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1285164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1295068Swnj #undef xchg 1306212Swnj tlen = 0; 1315164Swnj } 1325089Swnj ti->ti_next = ti->ti_prev = 0; 1335089Swnj ti->ti_x1 = 0; 1349185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1358942Sroot ti->ti_seq = htonl(seq); 1368942Sroot ti->ti_ack = htonl(ack); 1375089Swnj ti->ti_x2 = 0; 1385089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1395068Swnj ti->ti_flags = flags; 1409185Ssam ti->ti_win = htons((u_short)win); 1415392Swnj ti->ti_urp = 0; 1426304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1436212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1445089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1456353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1465068Swnj } 1475075Swnj 1485089Swnj /* 1495089Swnj * Create a new TCP control block, making an 1505089Swnj * empty reassembly queue and hooking it to the argument 1515089Swnj * protocol control block. 1525089Swnj */ 1535075Swnj struct tcpcb * 1545075Swnj tcp_newtcpcb(inp) 1555075Swnj struct inpcb *inp; 1565075Swnj { 1579644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1585075Swnj register struct tcpcb *tp; 1595075Swnj 16010144Ssam if (m == NULL) 16110144Ssam return ((struct tcpcb *)0); 1625075Swnj tp = mtod(m, struct tcpcb *); 1635075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 16417317Skarels tp->t_maxseg = TCP_MSS; 1656470Sroot tp->t_flags = 0; /* sends options! */ 1665075Swnj tp->t_inpcb = inp; 16717317Skarels tp->t_srtt = TCPTV_SRTTBASE; 16817359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 1695075Swnj inp->inp_ppcb = (caddr_t)tp; 1705075Swnj return (tp); 1715075Swnj } 1725075Swnj 1735089Swnj /* 1745089Swnj * Drop a TCP connection, reporting 1755089Swnj * the specified error. If connection is synchronized, 1765089Swnj * then send a RST to peer. 1775089Swnj */ 17810395Ssam struct tcpcb * 1795075Swnj tcp_drop(tp, errno) 18010395Ssam register struct tcpcb *tp; 1815075Swnj int errno; 1825075Swnj { 1835075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1845075Swnj 1855286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1865075Swnj tp->t_state = TCPS_CLOSED; 1878776Sroot (void) tcp_output(tp); 1885075Swnj } 1895075Swnj so->so_error = errno; 19010395Ssam return (tcp_close(tp)); 1915075Swnj } 1925075Swnj 1936584Ssam tcp_abort(inp) 1946584Ssam struct inpcb *inp; 1956584Ssam { 1968640Sroot 19710395Ssam (void) tcp_close((struct tcpcb *)inp->inp_ppcb); 1986584Ssam } 1996584Ssam 2005089Swnj /* 2015089Swnj * Close a TCP control block: 2025089Swnj * discard all space held by the tcp 2035089Swnj * discard internet protocol block 2045089Swnj * wake up any sleepers 2055089Swnj */ 20610395Ssam struct tcpcb * 2075075Swnj tcp_close(tp) 2085075Swnj register struct tcpcb *tp; 2095075Swnj { 2105075Swnj register struct tcpiphdr *t; 2115261Swnj struct inpcb *inp = tp->t_inpcb; 2125261Swnj struct socket *so = inp->inp_socket; 21312422Ssam register struct mbuf *m; 2145075Swnj 2155075Swnj t = tp->seg_next; 21612422Ssam while (t != (struct tcpiphdr *)tp) { 21712422Ssam t = (struct tcpiphdr *)t->ti_next; 21812422Ssam m = dtom(t->ti_prev); 21912422Ssam remque(t->ti_prev); 22012422Ssam m_freem(m); 22112422Ssam } 2225089Swnj if (tp->t_template) 2235075Swnj (void) m_free(dtom(tp->t_template)); 2245089Swnj if (tp->t_tcpopt) 2255089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2265089Swnj if (tp->t_ipopt) 2275089Swnj (void) m_free(dtom(tp->t_ipopt)); 2285075Swnj (void) m_free(dtom(tp)); 2295261Swnj inp->inp_ppcb = 0; 2306472Sroot soisdisconnected(so); 2315269Sroot in_pcbdetach(inp); 23210395Ssam return ((struct tcpcb *)0); 2335075Swnj } 2345075Swnj 2355075Swnj tcp_drain() 2365075Swnj { 2375075Swnj 2385075Swnj } 2395075Swnj 2406584Ssam tcp_ctlinput(cmd, arg) 2416584Ssam int cmd; 2426584Ssam caddr_t arg; 2435075Swnj { 24421119Skarels struct in_addr *in; 2456591Ssam extern u_char inetctlerrmap[]; 24621119Skarels int tcp_quench(), in_rtchange(); 2476591Ssam 2486591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2496591Ssam return; 2506591Ssam switch (cmd) { 2516591Ssam 2526591Ssam case PRC_ROUTEDEAD: 2536591Ssam break; 2546591Ssam 2556591Ssam case PRC_QUENCH: 25621119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 25721119Skarels in_pcbnotify(&tcb, in, 0, tcp_quench); 2586591Ssam break; 2596591Ssam 26021119Skarels case PRC_REDIRECT_NET: 26121119Skarels case PRC_REDIRECT_HOST: 26221119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 26321119Skarels in_pcbnotify(&tcb, in, 0, in_rtchange); 26421119Skarels break; 26521119Skarels 2666591Ssam case PRC_IFDOWN: 26721119Skarels in = &((struct sockaddr_in *)arg)->sin_addr; 26821119Skarels goto notify; 26921119Skarels 2706591Ssam case PRC_HOSTDEAD: 2716591Ssam case PRC_HOSTUNREACH: 27221119Skarels in = (struct in_addr *)arg; 27321119Skarels goto notify; 2746591Ssam 2756591Ssam default: 27621119Skarels if (inetctlerrmap[cmd] == 0) 27721119Skarels return; /* XXX */ 27821119Skarels in = &((struct icmp *)arg)->icmp_ip.ip_dst; 27921119Skarels notify: 28021119Skarels in_pcbnotify(&tcb, in, (int)inetctlerrmap[cmd], tcp_abort); 2816591Ssam } 2825075Swnj } 28317359Skarels 28417359Skarels /* 28517359Skarels * When a source quench is received, close congestion window 28617359Skarels * to 80% of the outstanding data (but not less than one segment). 28717359Skarels */ 28817359Skarels tcp_quench(inp) 28917359Skarels struct inpcb *inp; 29017359Skarels { 29117359Skarels struct tcpcb *tp = intotcpcb(inp); 29217359Skarels 29317359Skarels tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10, tp->t_maxseg); 29417359Skarels } 295