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*32101Skarels * @(#)tcp_subr.c 7.11 (Berkeley) 09/04/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) { 60*32101Skarels 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; 18017359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 181*32101Skarels tp->snd_ssthresh = 65535; /* XXX */ 1825075Swnj inp->inp_ppcb = (caddr_t)tp; 1835075Swnj return (tp); 1845075Swnj } 1855075Swnj 1865089Swnj /* 1875089Swnj * Drop a TCP connection, reporting 1885089Swnj * the specified error. If connection is synchronized, 1895089Swnj * then send a RST to peer. 1905089Swnj */ 19110395Ssam struct tcpcb * 1925075Swnj tcp_drop(tp, errno) 19310395Ssam register struct tcpcb *tp; 1945075Swnj int errno; 1955075Swnj { 1965075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1975075Swnj 1985286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1995075Swnj tp->t_state = TCPS_CLOSED; 2008776Sroot (void) tcp_output(tp); 20130524Skarels tcpstat.tcps_drops++; 20230524Skarels } else 20330524Skarels tcpstat.tcps_conndrops++; 2045075Swnj so->so_error = errno; 20510395Ssam return (tcp_close(tp)); 2065075Swnj } 2075075Swnj 2085089Swnj /* 2095089Swnj * Close a TCP control block: 2105089Swnj * discard all space held by the tcp 2115089Swnj * discard internet protocol block 2125089Swnj * wake up any sleepers 2135089Swnj */ 21410395Ssam struct tcpcb * 2155075Swnj tcp_close(tp) 2165075Swnj register struct tcpcb *tp; 2175075Swnj { 2185075Swnj register struct tcpiphdr *t; 2195261Swnj struct inpcb *inp = tp->t_inpcb; 2205261Swnj struct socket *so = inp->inp_socket; 22112422Ssam register struct mbuf *m; 2225075Swnj 2235075Swnj t = tp->seg_next; 22412422Ssam while (t != (struct tcpiphdr *)tp) { 22512422Ssam t = (struct tcpiphdr *)t->ti_next; 22612422Ssam m = dtom(t->ti_prev); 22712422Ssam remque(t->ti_prev); 22812422Ssam m_freem(m); 22912422Ssam } 2305089Swnj if (tp->t_template) 2315075Swnj (void) m_free(dtom(tp->t_template)); 2325075Swnj (void) m_free(dtom(tp)); 2335261Swnj inp->inp_ppcb = 0; 2346472Sroot soisdisconnected(so); 2355269Sroot in_pcbdetach(inp); 23630524Skarels tcpstat.tcps_closed++; 23710395Ssam return ((struct tcpcb *)0); 2385075Swnj } 2395075Swnj 2405075Swnj tcp_drain() 2415075Swnj { 2425075Swnj 2435075Swnj } 2445075Swnj 24530233Skarels /* 24630233Skarels * Notify a tcp user of an asynchronous error; 24730233Skarels * just wake up so that he can collect error status. 24830233Skarels */ 24930233Skarels tcp_notify(inp) 25030233Skarels register struct inpcb *inp; 25130233Skarels { 25230233Skarels 25330233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 25430233Skarels sorwakeup(inp->inp_socket); 25530233Skarels sowwakeup(inp->inp_socket); 25630233Skarels } 25724818Skarels tcp_ctlinput(cmd, sa) 2586584Ssam int cmd; 25924818Skarels struct sockaddr *sa; 2605075Swnj { 2616591Ssam extern u_char inetctlerrmap[]; 26224818Skarels struct sockaddr_in *sin; 26321119Skarels int tcp_quench(), in_rtchange(); 2646591Ssam 26524818Skarels if ((unsigned)cmd > PRC_NCMDS) 2666591Ssam return; 26724818Skarels if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 26824818Skarels return; 26924818Skarels sin = (struct sockaddr_in *)sa; 27024818Skarels if (sin->sin_addr.s_addr == INADDR_ANY) 27124818Skarels return; 27224818Skarels 2736591Ssam switch (cmd) { 2746591Ssam 2756591Ssam case PRC_QUENCH: 27624818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 2776591Ssam break; 2786591Ssam 27924818Skarels case PRC_ROUTEDEAD: 28021119Skarels case PRC_REDIRECT_NET: 28121119Skarels case PRC_REDIRECT_HOST: 28224818Skarels case PRC_REDIRECT_TOSNET: 28324818Skarels case PRC_REDIRECT_TOSHOST: 28424818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 28521119Skarels break; 28621119Skarels 2876591Ssam default: 28821119Skarels if (inetctlerrmap[cmd] == 0) 28921119Skarels return; /* XXX */ 29024818Skarels in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 29130233Skarels tcp_notify); 2926591Ssam } 2935075Swnj } 29417359Skarels 29517359Skarels /* 29617359Skarels * When a source quench is received, close congestion window 29731442Skarels * to one segment. We will gradually open it again as we proceed. 29817359Skarels */ 29917359Skarels tcp_quench(inp) 30017359Skarels struct inpcb *inp; 30117359Skarels { 30217359Skarels struct tcpcb *tp = intotcpcb(inp); 30317359Skarels 30424818Skarels if (tp) 30531442Skarels tp->snd_cwnd = tp->t_maxseg; 30617359Skarels } 307