123193Smckusick /* 229152Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 323193Smckusick * All rights reserved. The Berkeley software License Agreement 423193Smckusick * specifies the terms and conditions for redistribution. 523193Smckusick * 6*31757Skarels * @(#)tcp_subr.c 7.9 (Berkeley) 07/03/87 723193Smckusick */ 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 3331395Skarels int tcp_ttl = TCP_TTL; 3431395Skarels 355068Swnj /* 365068Swnj * Tcp initialization 375068Swnj */ 385068Swnj tcp_init() 395068Swnj { 405068Swnj 415068Swnj tcp_iss = 1; /* wrong */ 425068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 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 5926815Skarels if ((n = tp->t_template) == 0) { 6026815Skarels m = m_get(M_WAIT, MT_HEADER); 6126815Skarels if (m == NULL) 6226815Skarels return (0); 6326815Skarels m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 6426815Skarels m->m_len = sizeof (struct tcpiphdr); 6526815Skarels n = mtod(m, struct tcpiphdr *); 6626815Skarels } 675068Swnj n->ti_next = n->ti_prev = 0; 685068Swnj n->ti_x1 = 0; 695068Swnj n->ti_pr = IPPROTO_TCP; 705068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 715068Swnj n->ti_src = inp->inp_laddr; 725068Swnj n->ti_dst = inp->inp_faddr; 735068Swnj n->ti_sport = inp->inp_lport; 745068Swnj n->ti_dport = inp->inp_fport; 755068Swnj n->ti_seq = 0; 765089Swnj n->ti_ack = 0; 775068Swnj n->ti_x2 = 0; 785068Swnj n->ti_off = 5; 795068Swnj n->ti_flags = 0; 805068Swnj n->ti_win = 0; 815068Swnj n->ti_sum = 0; 825068Swnj n->ti_urp = 0; 835068Swnj return (n); 845068Swnj } 855068Swnj 865068Swnj /* 875164Swnj * Send a single message to the TCP at address specified by 885164Swnj * the given TCP/IP header. If flags==0, then we make a copy 895164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 905164Swnj * This is used to force keep alive messages out using the TCP 915164Swnj * template for a connection tp->t_template. If flags are given 925164Swnj * then we send a message back to the TCP which originated the 935164Swnj * segment ti, and discard the mbuf containing it and any other 945164Swnj * attached mbufs. 955164Swnj * 965164Swnj * In any case the ack and sequence number of the transmitted 975164Swnj * segment are as specified by the parameters. 985068Swnj */ 995392Swnj tcp_respond(tp, ti, ack, seq, flags) 1005392Swnj struct tcpcb *tp; 1015068Swnj register struct tcpiphdr *ti; 1025089Swnj tcp_seq ack, seq; 1035068Swnj int flags; 1045068Swnj { 10530762Skarels register struct mbuf *m; 1066212Swnj int win = 0, tlen; 1076353Ssam struct route *ro = 0; 1085068Swnj 1096353Ssam if (tp) { 1105392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1116353Ssam ro = &tp->t_inpcb->inp_route; 1126353Ssam } 1135164Swnj if (flags == 0) { 1149644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 11510144Ssam if (m == NULL) 1165164Swnj return; 11731727Skarels #ifdef TCP_COMPAT_42 11831727Skarels tlen = 1; 11931727Skarels #else 12031727Skarels tlen = 0; 12131727Skarels #endif 12230762Skarels m->m_len = sizeof (struct tcpiphdr) + tlen; 1235164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1245164Swnj ti = mtod(m, struct tcpiphdr *); 1255164Swnj flags = TH_ACK; 1265164Swnj } else { 1275245Sroot m = dtom(ti); 1285164Swnj m_freem(m->m_next); 1295164Swnj m->m_next = 0; 1306117Swnj m->m_off = (int)ti - (int)m; 13130762Skarels tlen = 0; 1325164Swnj m->m_len = sizeof (struct tcpiphdr); 1335089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1345164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1355164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1365068Swnj #undef xchg 1375164Swnj } 1385089Swnj ti->ti_next = ti->ti_prev = 0; 1395089Swnj ti->ti_x1 = 0; 1409185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1418942Sroot ti->ti_seq = htonl(seq); 1428942Sroot ti->ti_ack = htonl(ack); 1435089Swnj ti->ti_x2 = 0; 1445089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1455068Swnj ti->ti_flags = flags; 1469185Ssam ti->ti_win = htons((u_short)win); 1475392Swnj ti->ti_urp = 0; 1486304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1496212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 15031395Skarels ((struct ip *)ti)->ip_ttl = tcp_ttl; 1516353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1525068Swnj } 1535075Swnj 1545089Swnj /* 1555089Swnj * Create a new TCP control block, making an 1565089Swnj * empty reassembly queue and hooking it to the argument 1575089Swnj * protocol control block. 1585089Swnj */ 1595075Swnj struct tcpcb * 1605075Swnj tcp_newtcpcb(inp) 1615075Swnj struct inpcb *inp; 1625075Swnj { 1639644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1645075Swnj register struct tcpcb *tp; 1655075Swnj 16610144Ssam if (m == NULL) 16710144Ssam return ((struct tcpcb *)0); 1685075Swnj tp = mtod(m, struct tcpcb *); 1695075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 17017317Skarels tp->t_maxseg = TCP_MSS; 1716470Sroot tp->t_flags = 0; /* sends options! */ 1725075Swnj tp->t_inpcb = inp; 17331726Skarels /* 174*31757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 175*31757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 176*31757Skarels * reasonable initial retransmit time. 17731726Skarels */ 178*31757Skarels tp->t_srtt = TCPTV_SRTTBASE; 179*31757Skarels tp->t_rttvar = TCPTV_SRTTDFLT << 2; 18017359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 1815075Swnj inp->inp_ppcb = (caddr_t)tp; 1825075Swnj return (tp); 1835075Swnj } 1845075Swnj 1855089Swnj /* 1865089Swnj * Drop a TCP connection, reporting 1875089Swnj * the specified error. If connection is synchronized, 1885089Swnj * then send a RST to peer. 1895089Swnj */ 19010395Ssam struct tcpcb * 1915075Swnj tcp_drop(tp, errno) 19210395Ssam register struct tcpcb *tp; 1935075Swnj int errno; 1945075Swnj { 1955075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1965075Swnj 1975286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1985075Swnj tp->t_state = TCPS_CLOSED; 1998776Sroot (void) tcp_output(tp); 20030524Skarels tcpstat.tcps_drops++; 20130524Skarels } else 20230524Skarels tcpstat.tcps_conndrops++; 2035075Swnj so->so_error = errno; 20410395Ssam return (tcp_close(tp)); 2055075Swnj } 2065075Swnj 2075089Swnj /* 2085089Swnj * Close a TCP control block: 2095089Swnj * discard all space held by the tcp 2105089Swnj * discard internet protocol block 2115089Swnj * wake up any sleepers 2125089Swnj */ 21310395Ssam struct tcpcb * 2145075Swnj tcp_close(tp) 2155075Swnj register struct tcpcb *tp; 2165075Swnj { 2175075Swnj register struct tcpiphdr *t; 2185261Swnj struct inpcb *inp = tp->t_inpcb; 2195261Swnj struct socket *so = inp->inp_socket; 22012422Ssam register struct mbuf *m; 2215075Swnj 2225075Swnj t = tp->seg_next; 22312422Ssam while (t != (struct tcpiphdr *)tp) { 22412422Ssam t = (struct tcpiphdr *)t->ti_next; 22512422Ssam m = dtom(t->ti_prev); 22612422Ssam remque(t->ti_prev); 22712422Ssam m_freem(m); 22812422Ssam } 2295089Swnj if (tp->t_template) 2305075Swnj (void) m_free(dtom(tp->t_template)); 2315089Swnj if (tp->t_tcpopt) 2325089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2335075Swnj (void) m_free(dtom(tp)); 2345261Swnj inp->inp_ppcb = 0; 2356472Sroot soisdisconnected(so); 2365269Sroot in_pcbdetach(inp); 23730524Skarels tcpstat.tcps_closed++; 23810395Ssam return ((struct tcpcb *)0); 2395075Swnj } 2405075Swnj 2415075Swnj tcp_drain() 2425075Swnj { 2435075Swnj 2445075Swnj } 2455075Swnj 24630233Skarels /* 24730233Skarels * Notify a tcp user of an asynchronous error; 24830233Skarels * just wake up so that he can collect error status. 24930233Skarels */ 25030233Skarels tcp_notify(inp) 25130233Skarels register struct inpcb *inp; 25230233Skarels { 25330233Skarels 25430233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 25530233Skarels sorwakeup(inp->inp_socket); 25630233Skarels sowwakeup(inp->inp_socket); 25730233Skarels } 25824818Skarels tcp_ctlinput(cmd, sa) 2596584Ssam int cmd; 26024818Skarels struct sockaddr *sa; 2615075Swnj { 2626591Ssam extern u_char inetctlerrmap[]; 26324818Skarels struct sockaddr_in *sin; 26421119Skarels int tcp_quench(), in_rtchange(); 2656591Ssam 26624818Skarels if ((unsigned)cmd > PRC_NCMDS) 2676591Ssam return; 26824818Skarels if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 26924818Skarels return; 27024818Skarels sin = (struct sockaddr_in *)sa; 27124818Skarels if (sin->sin_addr.s_addr == INADDR_ANY) 27224818Skarels return; 27324818Skarels 2746591Ssam switch (cmd) { 2756591Ssam 2766591Ssam case PRC_QUENCH: 27724818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 2786591Ssam break; 2796591Ssam 28024818Skarels case PRC_ROUTEDEAD: 28121119Skarels case PRC_REDIRECT_NET: 28221119Skarels case PRC_REDIRECT_HOST: 28324818Skarels case PRC_REDIRECT_TOSNET: 28424818Skarels case PRC_REDIRECT_TOSHOST: 28524818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 28621119Skarels break; 28721119Skarels 2886591Ssam default: 28921119Skarels if (inetctlerrmap[cmd] == 0) 29021119Skarels return; /* XXX */ 29124818Skarels in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 29230233Skarels tcp_notify); 2936591Ssam } 2945075Swnj } 29517359Skarels 29617359Skarels /* 29717359Skarels * When a source quench is received, close congestion window 29831442Skarels * to one segment. We will gradually open it again as we proceed. 29917359Skarels */ 30017359Skarels tcp_quench(inp) 30117359Skarels struct inpcb *inp; 30217359Skarels { 30317359Skarels struct tcpcb *tp = intotcpcb(inp); 30417359Skarels 30524818Skarels if (tp) 30631442Skarels tp->snd_cwnd = tp->t_maxseg; 30717359Skarels } 308