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*31395Skarels * @(#)tcp_subr.c 7.5 (Berkeley) 06/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 33*31395Skarels int tcp_ttl = TCP_TTL; 34*31395Skarels float tcp_alpha = TCP_ALPHA; 35*31395Skarels float tcp_beta = TCP_BETA; 36*31395Skarels 375068Swnj /* 385068Swnj * Tcp initialization 395068Swnj */ 405068Swnj tcp_init() 415068Swnj { 425068Swnj 435068Swnj tcp_iss = 1; /* wrong */ 445068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 455068Swnj } 465068Swnj 475068Swnj /* 485068Swnj * Create template to be used to send tcp packets on a connection. 495068Swnj * Call after host entry created, allocates an mbuf and fills 505068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 515068Swnj * necessary when the connection is used. 525068Swnj */ 535068Swnj struct tcpiphdr * 545068Swnj tcp_template(tp) 555068Swnj struct tcpcb *tp; 565068Swnj { 575068Swnj register struct inpcb *inp = tp->t_inpcb; 585068Swnj register struct mbuf *m; 595068Swnj register struct tcpiphdr *n; 605068Swnj 6126815Skarels if ((n = tp->t_template) == 0) { 6226815Skarels m = m_get(M_WAIT, MT_HEADER); 6326815Skarels if (m == NULL) 6426815Skarels return (0); 6526815Skarels m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 6626815Skarels m->m_len = sizeof (struct tcpiphdr); 6726815Skarels n = mtod(m, struct tcpiphdr *); 6826815Skarels } 695068Swnj n->ti_next = n->ti_prev = 0; 705068Swnj n->ti_x1 = 0; 715068Swnj n->ti_pr = IPPROTO_TCP; 725068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 735068Swnj n->ti_src = inp->inp_laddr; 745068Swnj n->ti_dst = inp->inp_faddr; 755068Swnj n->ti_sport = inp->inp_lport; 765068Swnj n->ti_dport = inp->inp_fport; 775068Swnj n->ti_seq = 0; 785089Swnj n->ti_ack = 0; 795068Swnj n->ti_x2 = 0; 805068Swnj n->ti_off = 5; 815068Swnj n->ti_flags = 0; 825068Swnj n->ti_win = 0; 835068Swnj n->ti_sum = 0; 845068Swnj n->ti_urp = 0; 855068Swnj return (n); 865068Swnj } 875068Swnj 885068Swnj /* 895164Swnj * Send a single message to the TCP at address specified by 905164Swnj * the given TCP/IP header. If flags==0, then we make a copy 915164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 925164Swnj * This is used to force keep alive messages out using the TCP 935164Swnj * template for a connection tp->t_template. If flags are given 945164Swnj * then we send a message back to the TCP which originated the 955164Swnj * segment ti, and discard the mbuf containing it and any other 965164Swnj * attached mbufs. 975164Swnj * 985164Swnj * In any case the ack and sequence number of the transmitted 995164Swnj * segment are as specified by the parameters. 1005068Swnj */ 1015392Swnj tcp_respond(tp, ti, ack, seq, flags) 1025392Swnj struct tcpcb *tp; 1035068Swnj register struct tcpiphdr *ti; 1045089Swnj tcp_seq ack, seq; 1055068Swnj int flags; 1065068Swnj { 10730762Skarels register struct mbuf *m; 1086212Swnj int win = 0, tlen; 1096353Ssam struct route *ro = 0; 11030762Skarels extern int tcp_keeplen; 1115068Swnj 1126353Ssam if (tp) { 1135392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1146353Ssam ro = &tp->t_inpcb->inp_route; 1156353Ssam } 1165164Swnj if (flags == 0) { 1179644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 11810144Ssam if (m == NULL) 1195164Swnj return; 12030762Skarels tlen = tcp_keeplen; 12130762Skarels m->m_len = sizeof (struct tcpiphdr) + tlen; 1225164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1235164Swnj ti = mtod(m, struct tcpiphdr *); 1245164Swnj flags = TH_ACK; 1255164Swnj } else { 1265245Sroot m = dtom(ti); 1275164Swnj m_freem(m->m_next); 1285164Swnj m->m_next = 0; 1296117Swnj m->m_off = (int)ti - (int)m; 13030762Skarels tlen = 0; 1315164Swnj m->m_len = sizeof (struct tcpiphdr); 1325089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1335164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1345164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1355068Swnj #undef xchg 1365164Swnj } 1375089Swnj ti->ti_next = ti->ti_prev = 0; 1385089Swnj ti->ti_x1 = 0; 1399185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1408942Sroot ti->ti_seq = htonl(seq); 1418942Sroot ti->ti_ack = htonl(ack); 1425089Swnj ti->ti_x2 = 0; 1435089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1445068Swnj ti->ti_flags = flags; 1459185Ssam ti->ti_win = htons((u_short)win); 1465392Swnj ti->ti_urp = 0; 1476304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1486212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 149*31395Skarels ((struct ip *)ti)->ip_ttl = tcp_ttl; 1506353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1515068Swnj } 1525075Swnj 1535089Swnj /* 1545089Swnj * Create a new TCP control block, making an 1555089Swnj * empty reassembly queue and hooking it to the argument 1565089Swnj * protocol control block. 1575089Swnj */ 1585075Swnj struct tcpcb * 1595075Swnj tcp_newtcpcb(inp) 1605075Swnj struct inpcb *inp; 1615075Swnj { 1629644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1635075Swnj register struct tcpcb *tp; 1645075Swnj 16510144Ssam if (m == NULL) 16610144Ssam return ((struct tcpcb *)0); 1675075Swnj tp = mtod(m, struct tcpcb *); 1685075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 16917317Skarels tp->t_maxseg = TCP_MSS; 1706470Sroot tp->t_flags = 0; /* sends options! */ 1715075Swnj tp->t_inpcb = inp; 17217317Skarels tp->t_srtt = TCPTV_SRTTBASE; 17317359Skarels tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 1745075Swnj inp->inp_ppcb = (caddr_t)tp; 1755075Swnj return (tp); 1765075Swnj } 1775075Swnj 1785089Swnj /* 1795089Swnj * Drop a TCP connection, reporting 1805089Swnj * the specified error. If connection is synchronized, 1815089Swnj * then send a RST to peer. 1825089Swnj */ 18310395Ssam struct tcpcb * 1845075Swnj tcp_drop(tp, errno) 18510395Ssam register struct tcpcb *tp; 1865075Swnj int errno; 1875075Swnj { 1885075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1895075Swnj 1905286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1915075Swnj tp->t_state = TCPS_CLOSED; 1928776Sroot (void) tcp_output(tp); 19330524Skarels tcpstat.tcps_drops++; 19430524Skarels } else 19530524Skarels tcpstat.tcps_conndrops++; 1965075Swnj so->so_error = errno; 19710395Ssam return (tcp_close(tp)); 1985075Swnj } 1995075Swnj 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)); 2265075Swnj (void) m_free(dtom(tp)); 2275261Swnj inp->inp_ppcb = 0; 2286472Sroot soisdisconnected(so); 2295269Sroot in_pcbdetach(inp); 23030524Skarels tcpstat.tcps_closed++; 23110395Ssam return ((struct tcpcb *)0); 2325075Swnj } 2335075Swnj 2345075Swnj tcp_drain() 2355075Swnj { 2365075Swnj 2375075Swnj } 2385075Swnj 23930233Skarels /* 24030233Skarels * Notify a tcp user of an asynchronous error; 24130233Skarels * just wake up so that he can collect error status. 24230233Skarels */ 24330233Skarels tcp_notify(inp) 24430233Skarels register struct inpcb *inp; 24530233Skarels { 24630233Skarels 24730233Skarels wakeup((caddr_t) &inp->inp_socket->so_timeo); 24830233Skarels sorwakeup(inp->inp_socket); 24930233Skarels sowwakeup(inp->inp_socket); 25030233Skarels } 25124818Skarels tcp_ctlinput(cmd, sa) 2526584Ssam int cmd; 25324818Skarels struct sockaddr *sa; 2545075Swnj { 2556591Ssam extern u_char inetctlerrmap[]; 25624818Skarels struct sockaddr_in *sin; 25721119Skarels int tcp_quench(), in_rtchange(); 2586591Ssam 25924818Skarels if ((unsigned)cmd > PRC_NCMDS) 2606591Ssam return; 26124818Skarels if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 26224818Skarels return; 26324818Skarels sin = (struct sockaddr_in *)sa; 26424818Skarels if (sin->sin_addr.s_addr == INADDR_ANY) 26524818Skarels return; 26624818Skarels 2676591Ssam switch (cmd) { 2686591Ssam 2696591Ssam case PRC_QUENCH: 27024818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 2716591Ssam break; 2726591Ssam 27324818Skarels case PRC_ROUTEDEAD: 27421119Skarels case PRC_REDIRECT_NET: 27521119Skarels case PRC_REDIRECT_HOST: 27624818Skarels case PRC_REDIRECT_TOSNET: 27724818Skarels case PRC_REDIRECT_TOSHOST: 27824818Skarels in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 27921119Skarels break; 28021119Skarels 2816591Ssam default: 28221119Skarels if (inetctlerrmap[cmd] == 0) 28321119Skarels return; /* XXX */ 28424818Skarels in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 28530233Skarels tcp_notify); 2866591Ssam } 2875075Swnj } 28817359Skarels 28917359Skarels /* 29017359Skarels * When a source quench is received, close congestion window 29117359Skarels * to 80% of the outstanding data (but not less than one segment). 29217359Skarels */ 29317359Skarels tcp_quench(inp) 29417359Skarels struct inpcb *inp; 29517359Skarels { 29617359Skarels struct tcpcb *tp = intotcpcb(inp); 29717359Skarels 29824818Skarels if (tp) 29924818Skarels tp->snd_cwnd = MAX(8 * (tp->snd_nxt - tp->snd_una) / 10, 30024818Skarels tp->t_maxseg); 30117359Skarels } 302