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*32374Skarels * @(#)tcp_subr.c 7.12 (Berkeley) 10/09/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) { 6032101Skarels m = m_get(M_DONTWAIT, 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 /* 17431757Skarels * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 17531757Skarels * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 17631757Skarels * reasonable initial retransmit time. 17731726Skarels */ 17831757Skarels tp->t_srtt = TCPTV_SRTTBASE; 17931757Skarels tp->t_rttvar = TCPTV_SRTTDFLT << 2; 180*32374Skarels TCPT_RANGESET(tp->t_rxtcur, 181*32374Skarels ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 182*32374Skarels TCPTV_MIN, TCPTV_REXMTMAX); 18317359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 18432101Skarels tp->snd_ssthresh = 65535; /* XXX */ 1855075Swnj inp->inp_ppcb = (caddr_t)tp; 1865075Swnj return (tp); 1875075Swnj } 1885075Swnj 1895089Swnj /* 1905089Swnj * Drop a TCP connection, reporting 1915089Swnj * the specified error. If connection is synchronized, 1925089Swnj * then send a RST to peer. 1935089Swnj */ 19410395Ssam struct tcpcb * 1955075Swnj tcp_drop(tp, errno) 19610395Ssam register struct tcpcb *tp; 1975075Swnj int errno; 1985075Swnj { 1995075Swnj struct socket *so = tp->t_inpcb->inp_socket; 2005075Swnj 2015286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 2025075Swnj tp->t_state = TCPS_CLOSED; 2038776Sroot (void) tcp_output(tp); 20430524Skarels tcpstat.tcps_drops++; 20530524Skarels } else 20630524Skarels tcpstat.tcps_conndrops++; 2075075Swnj so->so_error = errno; 20810395Ssam return (tcp_close(tp)); 2095075Swnj } 2105075Swnj 2115089Swnj /* 2125089Swnj * Close a TCP control block: 2135089Swnj * discard all space held by the tcp 2145089Swnj * discard internet protocol block 2155089Swnj * wake up any sleepers 2165089Swnj */ 21710395Ssam struct tcpcb * 2185075Swnj tcp_close(tp) 2195075Swnj register struct tcpcb *tp; 2205075Swnj { 2215075Swnj register struct tcpiphdr *t; 2225261Swnj struct inpcb *inp = tp->t_inpcb; 2235261Swnj struct socket *so = inp->inp_socket; 22412422Ssam register struct mbuf *m; 2255075Swnj 2265075Swnj t = tp->seg_next; 22712422Ssam while (t != (struct tcpiphdr *)tp) { 22812422Ssam t = (struct tcpiphdr *)t->ti_next; 22912422Ssam m = dtom(t->ti_prev); 23012422Ssam remque(t->ti_prev); 23112422Ssam m_freem(m); 23212422Ssam } 2335089Swnj if (tp->t_template) 2345075Swnj (void) m_free(dtom(tp->t_template)); 2355075Swnj (void) m_free(dtom(tp)); 2365261Swnj inp->inp_ppcb = 0; 2376472Sroot soisdisconnected(so); 2385269Sroot in_pcbdetach(inp); 23930524Skarels tcpstat.tcps_closed++; 24010395Ssam return ((struct tcpcb *)0); 2415075Swnj } 2425075Swnj 2435075Swnj tcp_drain() 2445075Swnj { 2455075Swnj 2465075Swnj } 2475075Swnj 24830233Skarels /* 24930233Skarels * Notify a tcp user of an asynchronous error; 25030233Skarels * just wake up so that he can collect error status. 25130233Skarels */ 25230233Skarels tcp_notify(inp) 25330233Skarels register struct inpcb *inp; 25430233Skarels { 25530233Skarels 25630233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 25730233Skarels sorwakeup(inp->inp_socket); 25830233Skarels sowwakeup(inp->inp_socket); 25930233Skarels } 26024818Skarels tcp_ctlinput(cmd, sa) 2616584Ssam int cmd; 26224818Skarels struct sockaddr *sa; 2635075Swnj { 2646591Ssam extern u_char inetctlerrmap[]; 26524818Skarels struct sockaddr_in *sin; 26621119Skarels int tcp_quench(), in_rtchange(); 2676591Ssam 26824818Skarels if ((unsigned)cmd > PRC_NCMDS) 2696591Ssam return; 27024818Skarels if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 27124818Skarels return; 27224818Skarels sin = (struct sockaddr_in *)sa; 27324818Skarels if (sin->sin_addr.s_addr == INADDR_ANY) 27424818Skarels return; 27524818Skarels 2766591Ssam switch (cmd) { 2776591Ssam 2786591Ssam case PRC_QUENCH: 27924818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 2806591Ssam break; 2816591Ssam 28224818Skarels case PRC_ROUTEDEAD: 28321119Skarels case PRC_REDIRECT_NET: 28421119Skarels case PRC_REDIRECT_HOST: 28524818Skarels case PRC_REDIRECT_TOSNET: 28624818Skarels case PRC_REDIRECT_TOSHOST: 28724818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 28821119Skarels break; 28921119Skarels 2906591Ssam default: 29121119Skarels if (inetctlerrmap[cmd] == 0) 29221119Skarels return; /* XXX */ 29324818Skarels in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 29430233Skarels tcp_notify); 2956591Ssam } 2965075Swnj } 29717359Skarels 29817359Skarels /* 29917359Skarels * When a source quench is received, close congestion window 30031442Skarels * to one segment. We will gradually open it again as we proceed. 30117359Skarels */ 30217359Skarels tcp_quench(inp) 30317359Skarels struct inpcb *inp; 30417359Skarels { 30517359Skarels struct tcpcb *tp = intotcpcb(inp); 30617359Skarels 30724818Skarels if (tp) 30831442Skarels tp->snd_cwnd = tp->t_maxseg; 30917359Skarels } 310