1*10395Ssam /* tcp_subr.c 4.39 83/01/17 */ 25068Swnj 35068Swnj #include "../h/param.h" 45068Swnj #include "../h/systm.h" 55068Swnj #include "../h/mbuf.h" 65068Swnj #include "../h/socket.h" 75068Swnj #include "../h/socketvar.h" 85068Swnj #include "../h/protosw.h" 98403Swnj #include "../netinet/in.h" 106353Ssam #include "../net/route.h" 118403Swnj #include "../netinet/in_pcb.h" 128403Swnj #include "../netinet/in_systm.h" 135068Swnj #include "../net/if.h" 148403Swnj #include "../netinet/ip.h" 158403Swnj #include "../netinet/ip_var.h" 168403Swnj #include "../netinet/ip_icmp.h" 178403Swnj #include "../netinet/tcp.h" 188403Swnj #include "../netinet/tcp_fsm.h" 198403Swnj #include "../netinet/tcp_seq.h" 208403Swnj #include "../netinet/tcp_timer.h" 218403Swnj #include "../netinet/tcp_var.h" 228403Swnj #include "../netinet/tcpip.h" 237301Ssam #include <errno.h> 245068Swnj 255068Swnj /* 265068Swnj * Tcp initialization 275068Swnj */ 285068Swnj tcp_init() 295068Swnj { 305068Swnj 315068Swnj tcp_iss = 1; /* wrong */ 325068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 335164Swnj tcp_alpha = TCP_ALPHA; 345164Swnj tcp_beta = TCP_BETA; 355068Swnj } 365068Swnj 375068Swnj /* 385068Swnj * Create template to be used to send tcp packets on a connection. 395068Swnj * Call after host entry created, allocates an mbuf and fills 405068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 415068Swnj * necessary when the connection is used. 425068Swnj */ 435068Swnj struct tcpiphdr * 445068Swnj tcp_template(tp) 455068Swnj struct tcpcb *tp; 465068Swnj { 475068Swnj register struct inpcb *inp = tp->t_inpcb; 485068Swnj register struct mbuf *m; 495068Swnj register struct tcpiphdr *n; 505068Swnj 519644Ssam m = m_get(M_WAIT, MT_HEADER); 5210144Ssam if (m == NULL) 535068Swnj return (0); 545068Swnj m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 555068Swnj m->m_len = sizeof (struct tcpiphdr); 565068Swnj n = mtod(m, struct tcpiphdr *); 575068Swnj n->ti_next = n->ti_prev = 0; 585068Swnj n->ti_x1 = 0; 595068Swnj n->ti_pr = IPPROTO_TCP; 605068Swnj n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 615068Swnj n->ti_src = inp->inp_laddr; 625068Swnj n->ti_dst = inp->inp_faddr; 635068Swnj n->ti_sport = inp->inp_lport; 645068Swnj n->ti_dport = inp->inp_fport; 655068Swnj n->ti_seq = 0; 665089Swnj n->ti_ack = 0; 675068Swnj n->ti_x2 = 0; 685068Swnj n->ti_off = 5; 695068Swnj n->ti_flags = 0; 705068Swnj n->ti_win = 0; 715068Swnj n->ti_sum = 0; 725068Swnj n->ti_urp = 0; 735068Swnj return (n); 745068Swnj } 755068Swnj 765068Swnj /* 775164Swnj * Send a single message to the TCP at address specified by 785164Swnj * the given TCP/IP header. If flags==0, then we make a copy 795164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 805164Swnj * This is used to force keep alive messages out using the TCP 815164Swnj * template for a connection tp->t_template. If flags are given 825164Swnj * then we send a message back to the TCP which originated the 835164Swnj * segment ti, and discard the mbuf containing it and any other 845164Swnj * attached mbufs. 855164Swnj * 865164Swnj * In any case the ack and sequence number of the transmitted 875164Swnj * segment are as specified by the parameters. 885068Swnj */ 895392Swnj tcp_respond(tp, ti, ack, seq, flags) 905392Swnj struct tcpcb *tp; 915068Swnj register struct tcpiphdr *ti; 925089Swnj tcp_seq ack, seq; 935068Swnj int flags; 945068Swnj { 955164Swnj struct mbuf *m; 966212Swnj int win = 0, tlen; 976353Ssam struct route *ro = 0; 985068Swnj 996353Ssam if (tp) { 1005392Swnj win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 1016353Ssam ro = &tp->t_inpcb->inp_route; 1026353Ssam } 1035164Swnj if (flags == 0) { 1049644Ssam m = m_get(M_DONTWAIT, MT_HEADER); 10510144Ssam if (m == NULL) 1065164Swnj return; 1076212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1085164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1095164Swnj ti = mtod(m, struct tcpiphdr *); 1105164Swnj flags = TH_ACK; 1116212Swnj tlen = 1; 1125164Swnj } else { 1135245Sroot m = dtom(ti); 1145164Swnj m_freem(m->m_next); 1155164Swnj m->m_next = 0; 1166117Swnj m->m_off = (int)ti - (int)m; 1175164Swnj m->m_len = sizeof (struct tcpiphdr); 1185089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1195164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1205164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1215068Swnj #undef xchg 1226212Swnj tlen = 0; 1235164Swnj } 1245089Swnj ti->ti_next = ti->ti_prev = 0; 1255089Swnj ti->ti_x1 = 0; 1269185Ssam ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 1278942Sroot ti->ti_seq = htonl(seq); 1288942Sroot ti->ti_ack = htonl(ack); 1295089Swnj ti->ti_x2 = 0; 1305089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1315068Swnj ti->ti_flags = flags; 1329185Ssam ti->ti_win = htons((u_short)win); 1335392Swnj ti->ti_urp = 0; 1346304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1356212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1365089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1376353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1385068Swnj } 1395075Swnj 1405089Swnj /* 1415089Swnj * Create a new TCP control block, making an 1425089Swnj * empty reassembly queue and hooking it to the argument 1435089Swnj * protocol control block. 1445089Swnj */ 1455075Swnj struct tcpcb * 1465075Swnj tcp_newtcpcb(inp) 1475075Swnj struct inpcb *inp; 1485075Swnj { 1499644Ssam struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 1505075Swnj register struct tcpcb *tp; 1515075Swnj 15210144Ssam if (m == NULL) 15310144Ssam return ((struct tcpcb *)0); 1545075Swnj tp = mtod(m, struct tcpcb *); 1555075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 1569741Ssam /* 1579741Ssam * If the default maximum IP packet size is 576 bytes 1589741Ssam * and a standard IP header is 20 bytes, with a TCP 1599741Ssam * header of 20 bytes plus the options necessary to 1609741Ssam * upgrade it to something higher, then initialize the 1619741Ssam * maximum segment size to 576 - (20 + 20 + 8 + slop). 1629741Ssam */ 1639741Ssam tp->t_maxseg = 512; /* satisfy the rest of the world */ 1646470Sroot tp->t_flags = 0; /* sends options! */ 1655075Swnj tp->t_inpcb = inp; 1665075Swnj inp->inp_ppcb = (caddr_t)tp; 1675075Swnj return (tp); 1685075Swnj } 1695075Swnj 1705089Swnj /* 1715089Swnj * Drop a TCP connection, reporting 1725089Swnj * the specified error. If connection is synchronized, 1735089Swnj * then send a RST to peer. 1745089Swnj */ 175*10395Ssam struct tcpcb * 1765075Swnj tcp_drop(tp, errno) 177*10395Ssam register struct tcpcb *tp; 1785075Swnj int errno; 1795075Swnj { 1805075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1815075Swnj 1825286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1835075Swnj tp->t_state = TCPS_CLOSED; 1848776Sroot (void) tcp_output(tp); 1855075Swnj } 1865075Swnj so->so_error = errno; 187*10395Ssam return (tcp_close(tp)); 1885075Swnj } 1895075Swnj 1906584Ssam tcp_abort(inp) 1916584Ssam struct inpcb *inp; 1926584Ssam { 1938640Sroot 194*10395Ssam (void) tcp_close((struct tcpcb *)inp->inp_ppcb); 1956584Ssam } 1966584Ssam 1975089Swnj /* 1985089Swnj * Close a TCP control block: 1995089Swnj * discard all space held by the tcp 2005089Swnj * discard internet protocol block 2015089Swnj * wake up any sleepers 2025089Swnj */ 203*10395Ssam struct tcpcb * 2045075Swnj tcp_close(tp) 2055075Swnj register struct tcpcb *tp; 2065075Swnj { 2075075Swnj register struct tcpiphdr *t; 2085261Swnj struct inpcb *inp = tp->t_inpcb; 2095261Swnj struct socket *so = inp->inp_socket; 2105075Swnj 2115075Swnj t = tp->seg_next; 2125075Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 2135075Swnj m_freem(dtom(t)); 2145089Swnj if (tp->t_template) 2155075Swnj (void) m_free(dtom(tp->t_template)); 2165089Swnj if (tp->t_tcpopt) 2175089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2185089Swnj if (tp->t_ipopt) 2195089Swnj (void) m_free(dtom(tp->t_ipopt)); 2205075Swnj (void) m_free(dtom(tp)); 2215261Swnj inp->inp_ppcb = 0; 2226472Sroot soisdisconnected(so); 2235269Sroot in_pcbdetach(inp); 224*10395Ssam return ((struct tcpcb *)0); 2255075Swnj } 2265075Swnj 2275075Swnj tcp_drain() 2285075Swnj { 2295075Swnj 2305075Swnj } 2315075Swnj 2326584Ssam tcp_ctlinput(cmd, arg) 2336584Ssam int cmd; 2346584Ssam caddr_t arg; 2355075Swnj { 2366591Ssam struct in_addr *sin; 2376591Ssam extern u_char inetctlerrmap[]; 2386591Ssam 2396591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2406591Ssam return; 2416591Ssam switch (cmd) { 2426591Ssam 2436591Ssam case PRC_ROUTEDEAD: 2446591Ssam break; 2456591Ssam 2466591Ssam case PRC_QUENCH: 2476591Ssam break; 2486591Ssam 2496591Ssam /* these are handled by ip */ 2506591Ssam case PRC_IFDOWN: 2516591Ssam case PRC_HOSTDEAD: 2526591Ssam case PRC_HOSTUNREACH: 2536591Ssam break; 2546591Ssam 2556591Ssam default: 2566591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 2578640Sroot in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort); 2586591Ssam } 2595075Swnj } 260