1*8601Sroot /* tcp_subr.c 4.31 82/10/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 515852Sroot m = m_get(M_WAIT); 525068Swnj if (m == 0) 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) { 1045585Sroot m = m_get(M_DONTWAIT); 1055164Swnj if (m == 0) 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; 1266212Swnj ti->ti_len = sizeof (struct tcphdr) + tlen; 1275245Sroot ti->ti_seq = seq; 1285245Sroot ti->ti_ack = ack; 129*8601Sroot #if vax || pdp11 || ns16032 1306163Ssam ti->ti_len = htons((u_short)ti->ti_len); 1315245Sroot ti->ti_seq = htonl(ti->ti_seq); 1325245Sroot ti->ti_ack = htonl(ti->ti_ack); 1335245Sroot #endif 1345089Swnj ti->ti_x2 = 0; 1355089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1365068Swnj ti->ti_flags = flags; 1375392Swnj ti->ti_win = win; 138*8601Sroot #if vax || pdp11 || ns16032 1395392Swnj ti->ti_win = htons(ti->ti_win); 1405392Swnj #endif 1415392Swnj ti->ti_urp = 0; 1426304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1436212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1445089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1456353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1465068Swnj } 1475075Swnj 1485089Swnj /* 1495089Swnj * Create a new TCP control block, making an 1505089Swnj * empty reassembly queue and hooking it to the argument 1515089Swnj * protocol control block. 1525089Swnj */ 1535075Swnj struct tcpcb * 1545075Swnj tcp_newtcpcb(inp) 1555075Swnj struct inpcb *inp; 1565075Swnj { 1575852Sroot struct mbuf *m = m_getclr(M_DONTWAIT); 1585075Swnj register struct tcpcb *tp; 1595075Swnj 1605075Swnj if (m == 0) 1615075Swnj return (0); 1625075Swnj tp = mtod(m, struct tcpcb *); 1635075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 1646601Ssam tp->t_maxseg = 576; /* satisfy the rest of the world */ 1656470Sroot tp->t_flags = 0; /* sends options! */ 1665075Swnj tp->t_inpcb = inp; 1675075Swnj inp->inp_ppcb = (caddr_t)tp; 1685075Swnj return (tp); 1695075Swnj } 1705075Swnj 1715089Swnj /* 1725089Swnj * Drop a TCP connection, reporting 1735089Swnj * the specified error. If connection is synchronized, 1745089Swnj * then send a RST to peer. 1755089Swnj */ 1765075Swnj tcp_drop(tp, errno) 1775075Swnj 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; 1845075Swnj tcp_output(tp); 1855075Swnj } 1865075Swnj so->so_error = errno; 1875075Swnj tcp_close(tp); 1885075Swnj } 1895075Swnj 1906584Ssam tcp_abort(inp) 1916584Ssam struct inpcb *inp; 1926584Ssam { 1936584Ssam tcp_close(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; 2546591Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 2556591Ssam } 2565075Swnj } 257