1*5164Swnj /* tcp_subr.c 4.5 81/12/02 */ 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" 105089Swnj #include "../net/in_pcb.h" 115089Swnj #include "../net/in_systm.h" 125068Swnj #include "../net/if.h" 135068Swnj #include "../net/ip.h" 145068Swnj #include "../net/ip_var.h" 155068Swnj #include "../net/tcp.h" 165068Swnj #include "../net/tcp_fsm.h" 175089Swnj #include "../net/tcp_seq.h" 185089Swnj #include "../net/tcp_timer.h" 195068Swnj #include "../net/tcp_var.h" 205089Swnj #include "../net/tcpip.h" 215111Swnj #include "../errno.h" 225068Swnj 235068Swnj /* 245068Swnj * Tcp initialization 255068Swnj */ 265068Swnj tcp_init() 275068Swnj { 285068Swnj 295089Swnj COUNT(TCP_INIT); 305068Swnj tcp_iss = 1; /* wrong */ 315068Swnj tcb.inp_next = tcb.inp_prev = &tcb; 32*5164Swnj tcp_alpha = TCP_ALPHA; 33*5164Swnj tcp_beta = TCP_BETA; 345068Swnj } 355068Swnj 365068Swnj /* 375068Swnj * Create template to be used to send tcp packets on a connection. 385068Swnj * Call after host entry created, allocates an mbuf and fills 395068Swnj * in a skeletal tcp/ip header, minimizing the amount of work 405068Swnj * necessary when the connection is used. 415068Swnj */ 425068Swnj struct tcpiphdr * 435068Swnj tcp_template(tp) 445068Swnj struct tcpcb *tp; 455068Swnj { 465068Swnj register struct inpcb *inp = tp->t_inpcb; 475068Swnj register struct mbuf *m; 485068Swnj register struct tcpiphdr *n; 495068Swnj 505068Swnj COUNT(TCP_TEMPLATE); 515068Swnj m = m_get(1); 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 /* 77*5164Swnj * Send a single message to the TCP at address specified by 78*5164Swnj * the given TCP/IP header. If flags==0, then we make a copy 79*5164Swnj * of the tcpiphdr at ti and send directly to the addressed host. 80*5164Swnj * This is used to force keep alive messages out using the TCP 81*5164Swnj * template for a connection tp->t_template. If flags are given 82*5164Swnj * then we send a message back to the TCP which originated the 83*5164Swnj * segment ti, and discard the mbuf containing it and any other 84*5164Swnj * attached mbufs. 85*5164Swnj * 86*5164Swnj * In any case the ack and sequence number of the transmitted 87*5164Swnj * segment are as specified by the parameters. 885068Swnj */ 895089Swnj tcp_respond(ti, ack, seq, flags) 905068Swnj register struct tcpiphdr *ti; 915089Swnj tcp_seq ack, seq; 925068Swnj int flags; 935068Swnj { 94*5164Swnj struct mbuf *m; 955068Swnj 965089Swnj COUNT(TCP_RESPOND); 97*5164Swnj if (flags == 0) { 98*5164Swnj m = m_get(0); 99*5164Swnj if (m == 0) 100*5164Swnj return; 101*5164Swnj m->m_off = MMINOFF; 102*5164Swnj m->m_len = sizeof (struct tcpiphdr); 103*5164Swnj *mtod(m, struct tcpiphdr *) = *ti; 104*5164Swnj ti = mtod(m, struct tcpiphdr *); 105*5164Swnj flags = TH_ACK; 106*5164Swnj } else { 107*5164Swnj m_freem(m->m_next); 108*5164Swnj m->m_next = 0; 109*5164Swnj m->m_len = sizeof (struct tcpiphdr); 1105089Swnj #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 111*5164Swnj xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 112*5164Swnj xchg(ti->ti_dport, ti->ti_sport, u_short); 1135068Swnj #undef xchg 114*5164Swnj } 1155089Swnj ti->ti_next = ti->ti_prev = 0; 1165089Swnj ti->ti_x1 = 0; 1175089Swnj ti->ti_len = htons(sizeof (struct tcphdr)); 1185089Swnj ti->ti_seq = htonl(seq); 1195068Swnj ti->ti_ack = htonl(ack); 1205089Swnj ti->ti_x2 = 0; 1215089Swnj ti->ti_off = sizeof (struct tcphdr) >> 2; 1225068Swnj ti->ti_flags = flags; 1235089Swnj ti->ti_win = ti->ti_urp = 0; 1245089Swnj ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr)); 1255068Swnj ((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr); 1265089Swnj ((struct ip *)ti)->ip_ttl = TCP_TTL; 1275111Swnj (void) ip_output(m, (struct mbuf *)0); 1285068Swnj } 1295075Swnj 1305089Swnj /* 1315089Swnj * Create a new TCP control block, making an 1325089Swnj * empty reassembly queue and hooking it to the argument 1335089Swnj * protocol control block. 1345089Swnj */ 1355075Swnj struct tcpcb * 1365075Swnj tcp_newtcpcb(inp) 1375075Swnj struct inpcb *inp; 1385075Swnj { 1395075Swnj struct mbuf *m = m_getclr(0); 1405075Swnj register struct tcpcb *tp; 1415075Swnj COUNT(TCP_NEWTCPCB); 1425075Swnj 1435075Swnj if (m == 0) 1445075Swnj return (0); 1455075Swnj tp = mtod(m, struct tcpcb *); 1465075Swnj tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 1475089Swnj tp->t_maxseg = 1024; 1485075Swnj tp->t_inpcb = inp; 1495075Swnj inp->inp_ppcb = (caddr_t)tp; 1505075Swnj return (tp); 1515075Swnj } 1525075Swnj 1535089Swnj /* 1545089Swnj * Drop a TCP connection, reporting 1555089Swnj * the specified error. If connection is synchronized, 1565089Swnj * then send a RST to peer. 1575089Swnj */ 1585075Swnj tcp_drop(tp, errno) 1595075Swnj struct tcpcb *tp; 1605075Swnj int errno; 1615075Swnj { 1625075Swnj struct socket *so = tp->t_inpcb->inp_socket; 1635075Swnj 1645075Swnj COUNT(TCP_DROP); 1655075Swnj if (TCPS_HAVERCVDSYN(tp->t_state) && 1665089Swnj TCPS_OURFINNOTACKED(tp->t_state)) { 1675075Swnj tp->t_state = TCPS_CLOSED; 1685075Swnj tcp_output(tp); 1695075Swnj } 1705075Swnj so->so_error = errno; 1715075Swnj tcp_close(tp); 172*5164Swnj in_pcbdetach(tp->t_inpcb); 1735075Swnj } 1745075Swnj 1755089Swnj /* 1765089Swnj * Close a TCP control block: 1775089Swnj * discard all space held by the tcp 1785089Swnj * discard internet protocol block 1795089Swnj * wake up any sleepers 1805089Swnj */ 1815075Swnj tcp_close(tp) 1825075Swnj register struct tcpcb *tp; 1835075Swnj { 1845075Swnj register struct tcpiphdr *t; 1855089Swnj struct socket *so = tp->t_inpcb->inp_socket; 1865075Swnj 1875075Swnj COUNT(TCP_CLOSE); 1885075Swnj t = tp->seg_next; 1895075Swnj for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 1905075Swnj m_freem(dtom(t)); 1915089Swnj if (tp->t_template) 1925075Swnj (void) m_free(dtom(tp->t_template)); 1935089Swnj if (tp->t_tcpopt) 1945089Swnj (void) m_free(dtom(tp->t_tcpopt)); 1955089Swnj if (tp->t_ipopt) 1965089Swnj (void) m_free(dtom(tp->t_ipopt)); 1975075Swnj (void) m_free(dtom(tp)); 1985089Swnj socantrcvmore(so); 1995089Swnj socantsendmore(so); 200*5164Swnj in_pcbdisconnect(tp->t_inpcb); 2015075Swnj } 2025075Swnj 2035075Swnj tcp_drain() 2045075Swnj { 2055075Swnj 2065075Swnj COUNT(TCP_DRAIN); 2075075Swnj } 2085075Swnj 2095075Swnj tcp_ctlinput(m) 2105075Swnj struct mbuf *m; 2115075Swnj { 2125075Swnj 2135075Swnj COUNT(TCP_CTLINPUT); 2145075Swnj m_freem(m); 2155075Swnj } 216