1*7301Ssam /* tcp_subr.c 4.28 82/06/26 */ 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" 95089Swnj #include "../net/in.h" 106353Ssam #include "../net/route.h" 115089Swnj #include "../net/in_pcb.h" 125089Swnj #include "../net/in_systm.h" 135068Swnj #include "../net/if.h" 145068Swnj #include "../net/ip.h" 155068Swnj #include "../net/ip_var.h" 166591Ssam #include "../net/ip_icmp.h" 175068Swnj #include "../net/tcp.h" 185068Swnj #include "../net/tcp_fsm.h" 195089Swnj #include "../net/tcp_seq.h" 205089Swnj #include "../net/tcp_timer.h" 215068Swnj #include "../net/tcp_var.h" 225089Swnj #include "../net/tcpip.h" 23*7301Ssam #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; 1075164Swnj m->m_off = MMINOFF; 1086212Swnj m->m_len = sizeof (struct tcpiphdr) + 1; 1095164Swnj *mtod(m, struct tcpiphdr *) = *ti; 1105164Swnj ti = mtod(m, struct tcpiphdr *); 1115164Swnj flags = TH_ACK; 1126212Swnj tlen = 1; 1135164Swnj } else { 1145245Sroot m = dtom(ti); 1155164Swnj m_freem(m->m_next); 1165164Swnj m->m_next = 0; 1176117Swnj m->m_off = (int)ti - (int)m; 1185164Swnj m->m_len = sizeof (struct tcpiphdr); 1195089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 1205164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 1215164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1225068Swnj #undef xchg 1236212Swnj tlen = 0; 1245164Swnj } 1255089Swnj ti->ti_next = ti->ti_prev = 0; 1265089Swnj ti->ti_x1 = 0; 1276212Swnj ti->ti_len = sizeof (struct tcphdr) + tlen; 1285245Sroot ti->ti_seq = seq; 1295245Sroot ti->ti_ack = ack; 1305245Sroot #if vax 1316163Ssam ti->ti_len = htons((u_short)ti->ti_len); 1325245Sroot ti->ti_seq = htonl(ti->ti_seq); 1335245Sroot ti->ti_ack = htonl(ti->ti_ack); 1345245Sroot #endif 1355089Swnj ti->ti_x2 = 0; 1365089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1375068Swnj ti->ti_flags = flags; 1385392Swnj ti->ti_win = win; 1395392Swnj #if vax 1405392Swnj ti->ti_win = htons(ti->ti_win); 1415392Swnj #endif 1425392Swnj ti->ti_urp = 0; 1436304Sroot ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 1446212Swnj ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 1455089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1466353Ssam (void) ip_output(m, (struct mbuf *)0, ro, 0); 1475068Swnj } 1485075Swnj 1495089Swnj /* 1505089Swnj * Create a new TCP control block, making an 1515089Swnj * empty reassembly queue and hooking it to the argument 1525089Swnj * protocol control block. 1535089Swnj */ 1545075Swnj struct tcpcb * 1555075Swnj tcp_newtcpcb(inp) 1565075Swnj struct inpcb *inp; 1575075Swnj { 1585852Sroot struct mbuf *m = m_getclr(M_DONTWAIT); 1595075Swnj register struct tcpcb *tp; 1605075Swnj 1615075Swnj if (m == 0) 1625075Swnj return (0); 1635075Swnj tp = mtod(m, struct tcpcb *); 1645075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 1656601Ssam tp->t_maxseg = 576; /* satisfy the rest of the world */ 1666470Sroot tp->t_flags = 0; /* sends options! */ 1675075Swnj tp->t_inpcb = inp; 1685075Swnj inp->inp_ppcb = (caddr_t)tp; 1695075Swnj return (tp); 1705075Swnj } 1715075Swnj 1725089Swnj /* 1735089Swnj * Drop a TCP connection, reporting 1745089Swnj * the specified error. If connection is synchronized, 1755089Swnj * then send a RST to peer. 1765089Swnj */ 1775075Swnj tcp_drop(tp, errno) 1785075Swnj struct tcpcb *tp; 1795075Swnj int errno; 1805075Swnj { 1815075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1825075Swnj 1835286Sroot if (TCPS_HAVERCVDSYN(tp->t_state)) { 1845075Swnj tp->t_state = TCPS_CLOSED; 1855075Swnj tcp_output(tp); 1865075Swnj } 1875075Swnj so->so_error = errno; 1885075Swnj tcp_close(tp); 1895075Swnj } 1905075Swnj 1916584Ssam tcp_abort(inp) 1926584Ssam struct inpcb *inp; 1936584Ssam { 1946584Ssam tcp_close(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 */ 2035075Swnj tcp_close(tp) 2045075Swnj register struct tcpcb *tp; 2055075Swnj { 2065075Swnj register struct tcpiphdr *t; 2075261Swnj struct inpcb *inp = tp->t_inpcb; 2085261Swnj struct socket *so = inp->inp_socket; 2095075Swnj 2105075Swnj t = tp->seg_next; 2115075Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 2125075Swnj m_freem(dtom(t)); 2135089Swnj if (tp->t_template) 2145075Swnj (void) m_free(dtom(tp->t_template)); 2155089Swnj if (tp->t_tcpopt) 2165089Swnj (void) m_free(dtom(tp->t_tcpopt)); 2175089Swnj if (tp->t_ipopt) 2185089Swnj (void) m_free(dtom(tp->t_ipopt)); 2195075Swnj (void) m_free(dtom(tp)); 2205261Swnj inp->inp_ppcb = 0; 2216472Sroot soisdisconnected(so); 2225269Sroot in_pcbdetach(inp); 2235075Swnj } 2245075Swnj 2255075Swnj tcp_drain() 2265075Swnj { 2275075Swnj 2285075Swnj } 2295075Swnj 2306584Ssam tcp_ctlinput(cmd, arg) 2316584Ssam int cmd; 2326584Ssam caddr_t arg; 2335075Swnj { 2346591Ssam struct in_addr *sin; 2356591Ssam extern u_char inetctlerrmap[]; 2366591Ssam 2376591Ssam if (cmd < 0 || cmd > PRC_NCMDS) 2386591Ssam return; 2396591Ssam switch (cmd) { 2406591Ssam 2416591Ssam case PRC_ROUTEDEAD: 2426591Ssam break; 2436591Ssam 2446591Ssam case PRC_QUENCH: 2456591Ssam break; 2466591Ssam 2476591Ssam /* these are handled by ip */ 2486591Ssam case PRC_IFDOWN: 2496591Ssam case PRC_HOSTDEAD: 2506591Ssam case PRC_HOSTUNREACH: 2516591Ssam break; 2526591Ssam 2536591Ssam default: 2546591Ssam sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 2556591Ssam in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 2566591Ssam } 2575075Swnj } 258