1*17317Skarels /* tcp_subr.c 6.3 84/11/01 */ 25068Swnj 317064Sbloom #include "param.h" 417064Sbloom #include "systm.h" 517064Sbloom #include "mbuf.h" 617064Sbloom #include "socket.h" 717064Sbloom #include "socketvar.h" 817064Sbloom #include "protosw.h" 917064Sbloom #include "errno.h" 1010896Ssam 1110896Ssam #include "../net/route.h" 1210896Ssam #include "../net/if.h" 1310896Ssam 1417064Sbloom #include "in.h" 1517064Sbloom #include "in_pcb.h" 1617064Sbloom #include "in_systm.h" 1717064Sbloom #include "ip.h" 1817064Sbloom #include "ip_var.h" 1917064Sbloom #include "ip_icmp.h" 2017064Sbloom #include "tcp.h" 2117064Sbloom #include "tcp_fsm.h" 2217064Sbloom #include "tcp_seq.h" 2317064Sbloom #include "tcp_timer.h" 2417064Sbloom #include "tcp_var.h" 2517064Sbloom #include "tcpip.h" 265068Swnj 275068Swnj /* 285068Swnj * Tcp initialization 295068Swnj */ 305068Swnj tcp_init() 315068Swnj { 325068Swnj 335068Swnj tcp_iss = 1; /* wrong */ 345068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 355164Swnj tcp_alpha = TCP_ALPHA; 365164Swnj tcp_beta = TCP_BETA; 375068Swnj } 385068Swnj 395068Swnj /* 405068Swnj * Create template to be used to send tcp packets on a connection. 415068Swnj * Call after host entry created, allocates an mbuf and fills 425068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 435068Swnj * necessary when the connection is used. 445068Swnj */ 455068Swnj struct tcpiphdr * 465068Swnj tcp_template(tp) 475068Swnj struct tcpcb *tp; 485068Swnj { 495068Swnj register struct inpcb *inp = tp->t_inpcb; 505068Swnj register struct mbuf *m; 515068Swnj register struct tcpiphdr *n; 525068Swnj 539644Ssam m = m_get(M_WAIT, MT_HEADER); 5410144Ssam if (m == NULL) 555068Swnj return (0); 565068Swnj m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 575068Swnj m->m_len = sizeof (struct tcpiphdr); 585068Swnj n = mtod(m, struct tcpiphdr *); 595068Swnj n->ti_next = n->ti_prev = 0; 605068Swnj n->ti_x1 = 0; 615068Swnj n->ti_pr = IPPROTO_TCP; 625068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 635068Swnj n->ti_src = inp->inp_laddr; 645068Swnj n->ti_dst = inp->inp_faddr; 655068Swnj n->ti_sport = inp->inp_lport; 665068Swnj n->ti_dport = inp->inp_fport; 675068Swnj n->ti_seq = 0; 685089Swnj n->ti_ack = 0; 695068Swnj n->ti_x2 = 0; 705068Swnj n->ti_off = 5; 715068Swnj n->ti_flags = 0; 725068Swnj n->ti_win = 0; 735068Swnj n->ti_sum = 0; 745068Swnj n->ti_urp = 0; 755068Swnj return (n); 765068Swnj } 775068Swnj 785068Swnj /* 795164Swnj * Send a single message to the TCP at address specified by 805164Swnj * the given TCP/IP header. If flags==0, then we make a copy 815164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 825164Swnj * This is used to force keep alive messages out using the TCP 835164Swnj * template for a connection tp->t_template. If flags are given 845164Swnj * then we send a message back to the TCP which originated the 855164Swnj * segment ti, and discard the mbuf containing it and any other 865164Swnj * attached mbufs. 875164Swnj * 885164Swnj * In any case the ack and sequence number of the transmitted 895164Swnj * segment are as specified by the parameters. 905068Swnj */ 915392Swnj tcp_respond(tp, ti, ack, seq, flags) 925392Swnj struct tcpcb *tp; 935068Swnj register struct tcpiphdr *ti; 945089Swnj tcp_seq ack, seq; 955068Swnj int flags; 965068Swnj { 975164Swnj struct mbuf *m; 986212Swnj int win = 0, tlen; 996353Ssam struct route *ro = 0; 1005068Swnj 1016353Ssam if (tp) { 1025392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1036353Ssam ro = &tp->t_inpcb->inp_route; 1046353Ssam } 1055164Swnj if (flags == 0) { 1069644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 10710144Ssam if (m == NULL) 1085164Swnj return; 1096212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1105164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1115164Swnj ti = mtod(m, struct tcpiphdr *); 1125164Swnj flags = TH_ACK; 1136212Swnj tlen = 1; 1145164Swnj } else { 1155245Sroot m = dtom(ti); 1165164Swnj m_freem(m->m_next); 1175164Swnj m->m_next = 0; 1186117Swnj m->m_off = (int)ti - (int)m; 1195164Swnj m->m_len = sizeof (struct tcpiphdr); 1205089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1215164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1225164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1235068Swnj #undef xchg 1246212Swnj tlen = 0; 1255164Swnj } 1265089Swnj ti->ti_next = ti->ti_prev = 0; 1275089Swnj ti->ti_x1 = 0; 1289185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1298942Sroot ti->ti_seq = htonl(seq); 1308942Sroot ti->ti_ack = htonl(ack); 1315089Swnj ti->ti_x2 = 0; 1325089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1335068Swnj ti->ti_flags = flags; 1349185Ssam ti->ti_win = htons((u_short)win); 1355392Swnj ti->ti_urp = 0; 1366304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1376212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1385089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1396353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1405068Swnj } 1415075Swnj 1425089Swnj /* 1435089Swnj * Create a new TCP control block, making an 1445089Swnj * empty reassembly queue and hooking it to the argument 1455089Swnj * protocol control block. 1465089Swnj */ 1475075Swnj struct tcpcb * 1485075Swnj tcp_newtcpcb(inp) 1495075Swnj struct inpcb *inp; 1505075Swnj { 1519644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1525075Swnj register struct tcpcb *tp; 1535075Swnj 15410144Ssam if (m == NULL) 15510144Ssam return ((struct tcpcb *)0); 1565075Swnj tp = mtod(m, struct tcpcb *); 1575075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 158*17317Skarels tp->t_maxseg = TCP_MSS; 1596470Sroot tp->t_flags = 0; /* sends options! */ 1605075Swnj tp->t_inpcb = inp; 161*17317Skarels tp->t_srtt = TCPTV_SRTTBASE; 1625075Swnj inp->inp_ppcb = (caddr_t)tp; 1635075Swnj return (tp); 1645075Swnj } 1655075Swnj 1665089Swnj /* 1675089Swnj * Drop a TCP connection, reporting 1685089Swnj * the specified error. If connection is synchronized, 1695089Swnj * then send a RST to peer. 1705089Swnj */ 17110395Ssam struct tcpcb * 1725075Swnj tcp_drop(tp, errno) 17310395Ssam register struct tcpcb *tp; 1745075Swnj int errno; 1755075Swnj { 1765075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1775075Swnj 1785286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1795075Swnj tp->t_state = TCPS_CLOSED; 1808776Sroot (void) tcp_output(tp); 1815075Swnj } 1825075Swnj so->so_error = errno; 18310395Ssam return (tcp_close(tp)); 1845075Swnj } 1855075Swnj 1866584Ssam tcp_abort(inp) 1876584Ssam struct inpcb *inp; 1886584Ssam { 1898640Sroot 19010395Ssam (void) tcp_close((struct tcpcb *)inp->inp_ppcb); 1916584Ssam } 1926584Ssam 1935089Swnj /* 1945089Swnj * Close a TCP control block: 1955089Swnj * discard all space held by the tcp 1965089Swnj * discard internet protocol block 1975089Swnj * wake up any sleepers 1985089Swnj */ 19910395Ssam struct tcpcb * 2005075Swnj tcp_close(tp) 2015075Swnj register struct tcpcb *tp; 2025075Swnj { 2035075Swnj register struct tcpiphdr *t; 2045261Swnj struct inpcb *inp = tp->t_inpcb; 2055261Swnj struct socket *so = inp->inp_socket; 20612422Ssam register struct mbuf *m; 2075075Swnj 2085075Swnj t = tp->seg_next; 20912422Ssam while (t != (struct tcpiphdr *)tp) { 21012422Ssam t = (struct tcpiphdr *)t->ti_next; 21112422Ssam m = dtom(t->ti_prev); 21212422Ssam remque(t->ti_prev); 21312422Ssam m_freem(m); 21412422Ssam } 2155089Swnj if (tp->t_template) 2165075Swnj (void) m_free(dtom(tp->t_template)); 2175089Swnj if (tp->t_tcpopt) 2185089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2195089Swnj if (tp->t_ipopt) 2205089Swnj (void) m_free(dtom(tp->t_ipopt)); 2215075Swnj (void) m_free(dtom(tp)); 2225261Swnj inp->inp_ppcb = 0; 2236472Sroot soisdisconnected(so); 2245269Sroot in_pcbdetach(inp); 22510395Ssam return ((struct tcpcb *)0); 2265075Swnj } 2275075Swnj 2285075Swnj tcp_drain() 2295075Swnj { 2305075Swnj 2315075Swnj } 2325075Swnj 2336584Ssam tcp_ctlinput(cmd, arg) 2346584Ssam int cmd; 2356584Ssam caddr_t arg; 2365075Swnj { 2376591Ssam struct in_addr *sin; 2386591Ssam extern u_char inetctlerrmap[]; 2396591Ssam 2406591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2416591Ssam return; 2426591Ssam switch (cmd) { 2436591Ssam 2446591Ssam case PRC_ROUTEDEAD: 2456591Ssam break; 2466591Ssam 2476591Ssam case PRC_QUENCH: 2486591Ssam break; 2496591Ssam 2506591Ssam /* these are handled by ip */ 2516591Ssam case PRC_IFDOWN: 2526591Ssam case PRC_HOSTDEAD: 2536591Ssam case PRC_HOSTUNREACH: 2546591Ssam break; 2556591Ssam 2566591Ssam default: 2576591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 2588640Sroot in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort); 2596591Ssam } 2605075Swnj } 261