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*30762Skarels * @(#)tcp_subr.c 7.4 (Berkeley) 04/02/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 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 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 { 105*30762Skarels register struct mbuf *m; 1066212Swnj int win = 0, tlen; 1076353Ssam struct route *ro = 0; 108*30762Skarels extern int tcp_keeplen; 1095068Swnj 1106353Ssam if (tp) { 1115392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1126353Ssam ro = &tp->t_inpcb->inp_route; 1136353Ssam } 1145164Swnj if (flags == 0) { 1159644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 11610144Ssam if (m == NULL) 1175164Swnj return; 118*30762Skarels tlen = tcp_keeplen; 119*30762Skarels m->m_len = sizeof (struct tcpiphdr) + tlen; 1205164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1215164Swnj ti = mtod(m, struct tcpiphdr *); 1225164Swnj flags = TH_ACK; 1235164Swnj } else { 1245245Sroot m = dtom(ti); 1255164Swnj m_freem(m->m_next); 1265164Swnj m->m_next = 0; 1276117Swnj m->m_off = (int)ti - (int)m; 128*30762Skarels tlen = 0; 1295164Swnj m->m_len = sizeof (struct tcpiphdr); 1305089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1315164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1325164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1335068Swnj #undef xchg 1345164Swnj } 1355089Swnj ti->ti_next = ti->ti_prev = 0; 1365089Swnj ti->ti_x1 = 0; 1379185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1388942Sroot ti->ti_seq = htonl(seq); 1398942Sroot ti->ti_ack = htonl(ack); 1405089Swnj ti->ti_x2 = 0; 1415089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1425068Swnj ti->ti_flags = flags; 1439185Ssam ti->ti_win = htons((u_short)win); 1445392Swnj ti->ti_urp = 0; 1456304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1466212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1475089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1486353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1495068Swnj } 1505075Swnj 1515089Swnj /* 1525089Swnj * Create a new TCP control block, making an 1535089Swnj * empty reassembly queue and hooking it to the argument 1545089Swnj * protocol control block. 1555089Swnj */ 1565075Swnj struct tcpcb * 1575075Swnj tcp_newtcpcb(inp) 1585075Swnj struct inpcb *inp; 1595075Swnj { 1609644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1615075Swnj register struct tcpcb *tp; 1625075Swnj 16310144Ssam if (m == NULL) 16410144Ssam return ((struct tcpcb *)0); 1655075Swnj tp = mtod(m, struct tcpcb *); 1665075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 16717317Skarels tp->t_maxseg = TCP_MSS; 1686470Sroot tp->t_flags = 0; /* sends options! */ 1695075Swnj tp->t_inpcb = inp; 17017317Skarels tp->t_srtt = TCPTV_SRTTBASE; 17117359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 1725075Swnj inp->inp_ppcb = (caddr_t)tp; 1735075Swnj return (tp); 1745075Swnj } 1755075Swnj 1765089Swnj /* 1775089Swnj * Drop a TCP connection, reporting 1785089Swnj * the specified error. If connection is synchronized, 1795089Swnj * then send a RST to peer. 1805089Swnj */ 18110395Ssam struct tcpcb * 1825075Swnj tcp_drop(tp, errno) 18310395Ssam register struct tcpcb *tp; 1845075Swnj int errno; 1855075Swnj { 1865075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1875075Swnj 1885286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1895075Swnj tp->t_state = TCPS_CLOSED; 1908776Sroot (void) tcp_output(tp); 19130524Skarels tcpstat.tcps_drops++; 19230524Skarels } else 19330524Skarels tcpstat.tcps_conndrops++; 1945075Swnj so->so_error = errno; 19510395Ssam return (tcp_close(tp)); 1965075Swnj } 1975075Swnj 1985089Swnj /* 1995089Swnj * Close a TCP control block: 2005089Swnj * discard all space held by the tcp 2015089Swnj * discard internet protocol block 2025089Swnj * wake up any sleepers 2035089Swnj */ 20410395Ssam struct tcpcb * 2055075Swnj tcp_close(tp) 2065075Swnj register struct tcpcb *tp; 2075075Swnj { 2085075Swnj register struct tcpiphdr *t; 2095261Swnj struct inpcb *inp = tp->t_inpcb; 2105261Swnj struct socket *so = inp->inp_socket; 21112422Ssam register struct mbuf *m; 2125075Swnj 2135075Swnj t = tp->seg_next; 21412422Ssam while (t != (struct tcpiphdr *)tp) { 21512422Ssam t = (struct tcpiphdr *)t->ti_next; 21612422Ssam m = dtom(t->ti_prev); 21712422Ssam remque(t->ti_prev); 21812422Ssam m_freem(m); 21912422Ssam } 2205089Swnj if (tp->t_template) 2215075Swnj (void) m_free(dtom(tp->t_template)); 2225089Swnj if (tp->t_tcpopt) 2235089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2245075Swnj (void) m_free(dtom(tp)); 2255261Swnj inp->inp_ppcb = 0; 2266472Sroot soisdisconnected(so); 2275269Sroot in_pcbdetach(inp); 22830524Skarels tcpstat.tcps_closed++; 22910395Ssam return ((struct tcpcb *)0); 2305075Swnj } 2315075Swnj 2325075Swnj tcp_drain() 2335075Swnj { 2345075Swnj 2355075Swnj } 2365075Swnj 23730233Skarels /* 23830233Skarels * Notify a tcp user of an asynchronous error; 23930233Skarels * just wake up so that he can collect error status. 24030233Skarels */ 24130233Skarels tcp_notify(inp) 24230233Skarels register struct inpcb *inp; 24330233Skarels { 24430233Skarels 24530233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 24630233Skarels sorwakeup(inp->inp_socket); 24730233Skarels sowwakeup(inp->inp_socket); 24830233Skarels } 24924818Skarels tcp_ctlinput(cmd, sa) 2506584Ssam int cmd; 25124818Skarels struct sockaddr *sa; 2525075Swnj { 2536591Ssam extern u_char inetctlerrmap[]; 25424818Skarels struct sockaddr_in *sin; 25521119Skarels int tcp_quench(), in_rtchange(); 2566591Ssam 25724818Skarels if ((unsigned)cmd > PRC_NCMDS) 2586591Ssam return; 25924818Skarels if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 26024818Skarels return; 26124818Skarels sin = (struct sockaddr_in *)sa; 26224818Skarels if (sin->sin_addr.s_addr == INADDR_ANY) 26324818Skarels return; 26424818Skarels 2656591Ssam switch (cmd) { 2666591Ssam 2676591Ssam case PRC_QUENCH: 26824818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 2696591Ssam break; 2706591Ssam 27124818Skarels case PRC_ROUTEDEAD: 27221119Skarels case PRC_REDIRECT_NET: 27321119Skarels case PRC_REDIRECT_HOST: 27424818Skarels case PRC_REDIRECT_TOSNET: 27524818Skarels case PRC_REDIRECT_TOSHOST: 27624818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 27721119Skarels break; 27821119Skarels 2796591Ssam default: 28021119Skarels if (inetctlerrmap[cmd] == 0) 28121119Skarels return; /* XXX */ 28224818Skarels in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 28330233Skarels tcp_notify); 2846591Ssam } 2855075Swnj } 28617359Skarels 28717359Skarels /* 28817359Skarels * When a source quench is received, close congestion window 28917359Skarels * to 80% of the outstanding data (but not less than one segment). 29017359Skarels */ 29117359Skarels tcp_quench(inp) 29217359Skarels struct inpcb *inp; 29317359Skarels { 29417359Skarels struct tcpcb *tp = intotcpcb(inp); 29517359Skarels 29624818Skarels if (tp) 29724818Skarels tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10, 29824818Skarels tp->t_maxseg); 29917359Skarels } 300