1 /* tcp_subr.c 4.18 82/03/15 */ 2 3 #include "../h/param.h" 4 #include "../h/systm.h" 5 #include "../h/mbuf.h" 6 #include "../h/socket.h" 7 #include "../h/socketvar.h" 8 #include "../h/protosw.h" 9 #include "../net/in.h" 10 #include "../net/in_pcb.h" 11 #include "../net/in_systm.h" 12 #include "../net/if.h" 13 #include "../net/ip.h" 14 #include "../net/ip_var.h" 15 #include "../net/tcp.h" 16 #include "../net/tcp_fsm.h" 17 #include "../net/tcp_seq.h" 18 #include "../net/tcp_timer.h" 19 #include "../net/tcp_var.h" 20 #include "../net/tcpip.h" 21 #include "../errno.h" 22 23 /* 24 * Tcp initialization 25 */ 26 tcp_init() 27 { 28 29 COUNT(TCP_INIT); 30 tcp_iss = 1; /* wrong */ 31 tcb.inp_next = tcb.inp_prev = &tcb; 32 tcp_alpha = TCP_ALPHA; 33 tcp_beta = TCP_BETA; 34 } 35 36 /* 37 * Create template to be used to send tcp packets on a connection. 38 * Call after host entry created, allocates an mbuf and fills 39 * in a skeletal tcp/ip header, minimizing the amount of work 40 * necessary when the connection is used. 41 */ 42 struct tcpiphdr * 43 tcp_template(tp) 44 struct tcpcb *tp; 45 { 46 register struct inpcb *inp = tp->t_inpcb; 47 register struct mbuf *m; 48 register struct tcpiphdr *n; 49 50 COUNT(TCP_TEMPLATE); 51 m = m_get(M_WAIT); 52 if (m == 0) 53 return (0); 54 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 55 m->m_len = sizeof (struct tcpiphdr); 56 n = mtod(m, struct tcpiphdr *); 57 n->ti_next = n->ti_prev = 0; 58 n->ti_x1 = 0; 59 n->ti_pr = IPPROTO_TCP; 60 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 61 n->ti_src = inp->inp_laddr; 62 n->ti_dst = inp->inp_faddr; 63 n->ti_sport = inp->inp_lport; 64 n->ti_dport = inp->inp_fport; 65 n->ti_seq = 0; 66 n->ti_ack = 0; 67 n->ti_x2 = 0; 68 n->ti_off = 5; 69 n->ti_flags = 0; 70 n->ti_win = 0; 71 n->ti_sum = 0; 72 n->ti_urp = 0; 73 return (n); 74 } 75 76 /* 77 * Send a single message to the TCP at address specified by 78 * the given TCP/IP header. If flags==0, then we make a copy 79 * of the tcpiphdr at ti and send directly to the addressed host. 80 * This is used to force keep alive messages out using the TCP 81 * template for a connection tp->t_template. If flags are given 82 * then we send a message back to the TCP which originated the 83 * segment ti, and discard the mbuf containing it and any other 84 * attached mbufs. 85 * 86 * In any case the ack and sequence number of the transmitted 87 * segment are as specified by the parameters. 88 */ 89 tcp_respond(tp, ti, ack, seq, flags) 90 struct tcpcb *tp; 91 register struct tcpiphdr *ti; 92 tcp_seq ack, seq; 93 int flags; 94 { 95 struct mbuf *m; 96 int win = 0, tlen; 97 98 COUNT(TCP_RESPOND); 99 if (tp) 100 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 101 if (flags == 0) { 102 m = m_get(M_DONTWAIT); 103 if (m == 0) 104 return; 105 m->m_off = MMINOFF; 106 m->m_len = sizeof (struct tcpiphdr) + 1; 107 *mtod(m, struct tcpiphdr *) = *ti; 108 ti = mtod(m, struct tcpiphdr *); 109 flags = TH_ACK; 110 tlen = 1; 111 } else { 112 m = dtom(ti); 113 m_freem(m->m_next); 114 m->m_next = 0; 115 m->m_off = (int)ti - (int)m; 116 m->m_len = sizeof (struct tcpiphdr); 117 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 118 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 119 xchg(ti->ti_dport, ti->ti_sport, u_short); 120 #undef xchg 121 tlen = 0; 122 } 123 ti->ti_next = ti->ti_prev = 0; 124 ti->ti_x1 = 0; 125 ti->ti_len = sizeof (struct tcphdr) + tlen; 126 ti->ti_seq = seq; 127 ti->ti_ack = ack; 128 #if vax 129 ti->ti_len = htons((u_short)ti->ti_len); 130 ti->ti_seq = htonl(ti->ti_seq); 131 ti->ti_ack = htonl(ti->ti_ack); 132 #endif 133 ti->ti_x2 = 0; 134 ti->ti_off = sizeof (struct tcphdr) >> 2; 135 ti->ti_flags = flags; 136 ti->ti_win = win; 137 #if vax 138 ti->ti_win = htons(ti->ti_win); 139 #endif 140 ti->ti_urp = 0; 141 ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr)); 142 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 143 ((struct ip *)ti)->ip_ttl = TCP_TTL; 144 (void) ip_output(m, (struct mbuf *)0, 0); 145 } 146 147 /* 148 * Create a new TCP control block, making an 149 * empty reassembly queue and hooking it to the argument 150 * protocol control block. 151 */ 152 struct tcpcb * 153 tcp_newtcpcb(inp) 154 struct inpcb *inp; 155 { 156 struct mbuf *m = m_getclr(M_DONTWAIT); 157 register struct tcpcb *tp; 158 COUNT(TCP_NEWTCPCB); 159 160 if (m == 0) 161 return (0); 162 tp = mtod(m, struct tcpcb *); 163 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 164 tp->t_maxseg = 1024; 165 tp->t_flags = TF_NOOPT; /* until all TCP's take options */ 166 tp->t_inpcb = inp; 167 inp->inp_ppcb = (caddr_t)tp; 168 return (tp); 169 } 170 171 /* 172 * Drop a TCP connection, reporting 173 * the specified error. If connection is synchronized, 174 * then send a RST to peer. 175 */ 176 tcp_drop(tp, errno) 177 struct tcpcb *tp; 178 int errno; 179 { 180 struct socket *so = tp->t_inpcb->inp_socket; 181 182 COUNT(TCP_DROP); 183 if (TCPS_HAVERCVDSYN(tp->t_state)) { 184 tp->t_state = TCPS_CLOSED; 185 tcp_output(tp); 186 } 187 so->so_error = errno; 188 tcp_close(tp); 189 } 190 191 /* 192 * Close a TCP control block: 193 * discard all space held by the tcp 194 * discard internet protocol block 195 * wake up any sleepers 196 */ 197 tcp_close(tp) 198 register struct tcpcb *tp; 199 { 200 register struct tcpiphdr *t; 201 struct inpcb *inp = tp->t_inpcb; 202 struct socket *so = inp->inp_socket; 203 204 COUNT(TCP_CLOSE); 205 t = tp->seg_next; 206 for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 207 m_freem(dtom(t)); 208 if (tp->t_template) 209 (void) m_free(dtom(tp->t_template)); 210 if (tp->t_tcpopt) 211 (void) m_free(dtom(tp->t_tcpopt)); 212 if (tp->t_ipopt) 213 (void) m_free(dtom(tp->t_ipopt)); 214 (void) m_free(dtom(tp)); 215 inp->inp_ppcb = 0; 216 in_pcbdetach(inp); 217 soisdisconnected(so); 218 } 219 220 tcp_drain() 221 { 222 223 COUNT(TCP_DRAIN); 224 } 225 226 tcp_ctlinput(m) 227 struct mbuf *m; 228 { 229 230 COUNT(TCP_CTLINPUT); 231 m_freem(m); 232 } 233