1*10144Ssam /* tcp_subr.c 4.38 83/01/04 */ 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); 52*10144Ssam 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); 105*10144Ssam 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 152*10144Ssam if (m == NULL) 153*10144Ssam 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 */ 1755075Swnj tcp_drop(tp, errno) 1765075Swnj struct tcpcb *tp; 1775075Swnj int errno; 1785075Swnj { 1795075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1805075Swnj 1815286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1825075Swnj tp->t_state = TCPS_CLOSED; 1838776Sroot (void) tcp_output(tp); 1845075Swnj } 1855075Swnj so->so_error = errno; 1865075Swnj tcp_close(tp); 1875075Swnj } 1885075Swnj 1896584Ssam tcp_abort(inp) 1906584Ssam struct inpcb *inp; 1916584Ssam { 1928640Sroot 1938640Sroot tcp_close((struct tcpcb *)inp->inp_ppcb); 1946584Ssam } 1956584Ssam 1965089Swnj /* 1975089Swnj * Close a TCP control block: 1985089Swnj * discard all space held by the tcp 1995089Swnj * discard internet protocol block 2005089Swnj * wake up any sleepers 2015089Swnj */ 2025075Swnj tcp_close(tp) 2035075Swnj register struct tcpcb *tp; 2045075Swnj { 2055075Swnj register struct tcpiphdr *t; 2065261Swnj struct inpcb *inp = tp->t_inpcb; 2075261Swnj struct socket *so = inp->inp_socket; 2085075Swnj 2095075Swnj t = tp->seg_next; 2105075Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 2115075Swnj m_freem(dtom(t)); 2125089Swnj if (tp->t_template) 2135075Swnj (void) m_free(dtom(tp->t_template)); 2145089Swnj if (tp->t_tcpopt) 2155089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2165089Swnj if (tp->t_ipopt) 2175089Swnj (void) m_free(dtom(tp->t_ipopt)); 2185075Swnj (void) m_free(dtom(tp)); 2195261Swnj inp->inp_ppcb = 0; 2206472Sroot soisdisconnected(so); 2215269Sroot in_pcbdetach(inp); 2225075Swnj } 2235075Swnj 2245075Swnj tcp_drain() 2255075Swnj { 2265075Swnj 2275075Swnj } 2285075Swnj 2296584Ssam tcp_ctlinput(cmd, arg) 2306584Ssam int cmd; 2316584Ssam caddr_t arg; 2325075Swnj { 2336591Ssam struct in_addr *sin; 2346591Ssam extern u_char inetctlerrmap[]; 2356591Ssam 2366591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2376591Ssam return; 2386591Ssam switch (cmd) { 2396591Ssam 2406591Ssam case PRC_ROUTEDEAD: 2416591Ssam break; 2426591Ssam 2436591Ssam case PRC_QUENCH: 2446591Ssam break; 2456591Ssam 2466591Ssam /* these are handled by ip */ 2476591Ssam case PRC_IFDOWN: 2486591Ssam case PRC_HOSTDEAD: 2496591Ssam case PRC_HOSTUNREACH: 2506591Ssam break; 2516591Ssam 2526591Ssam default: 2536591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 2548640Sroot in_pcbnotify(&tcb, sin, (int)inetctlerrmap[cmd], tcp_abort); 2556591Ssam } 2565075Swnj } 257