1 /* tcp_subr.c 4.7 81/12/12 */ 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 printf("tcp_template %x\n", tp); 52 m = m_get(1); 53 if (m == 0) 54 return (0); 55 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 56 m->m_len = sizeof (struct tcpiphdr); 57 n = mtod(m, struct tcpiphdr *); 58 n->ti_next = n->ti_prev = 0; 59 n->ti_x1 = 0; 60 n->ti_pr = IPPROTO_TCP; 61 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 62 n->ti_src = inp->inp_laddr; 63 n->ti_dst = inp->inp_faddr; 64 n->ti_sport = inp->inp_lport; 65 n->ti_dport = inp->inp_fport; 66 n->ti_seq = 0; 67 n->ti_ack = 0; 68 n->ti_x2 = 0; 69 n->ti_off = 5; 70 n->ti_flags = 0; 71 n->ti_win = 0; 72 n->ti_sum = 0; 73 n->ti_urp = 0; 74 return (n); 75 } 76 77 /* 78 * Send a single message to the TCP at address specified by 79 * the given TCP/IP header. If flags==0, then we make a copy 80 * of the tcpiphdr at ti and send directly to the addressed host. 81 * This is used to force keep alive messages out using the TCP 82 * template for a connection tp->t_template. If flags are given 83 * then we send a message back to the TCP which originated the 84 * segment ti, and discard the mbuf containing it and any other 85 * attached mbufs. 86 * 87 * In any case the ack and sequence number of the transmitted 88 * segment are as specified by the parameters. 89 */ 90 tcp_respond(ti, ack, seq, flags) 91 register struct tcpiphdr *ti; 92 tcp_seq ack, seq; 93 int flags; 94 { 95 struct mbuf *m; 96 97 COUNT(TCP_RESPOND); 98 printf("tcp_respond ack %x seq %x flags %x\n", ack, seq, flags); 99 if (flags == 0) { 100 m = m_get(0); 101 if (m == 0) 102 return; 103 m->m_off = MMINOFF; 104 m->m_len = sizeof (struct tcpiphdr); 105 *mtod(m, struct tcpiphdr *) = *ti; 106 ti = mtod(m, struct tcpiphdr *); 107 flags = TH_ACK; 108 } else { 109 m = dtom(ti); 110 m_freem(m->m_next); 111 m->m_next = 0; 112 m->m_len = sizeof (struct tcpiphdr); 113 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 114 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 115 xchg(ti->ti_dport, ti->ti_sport, u_short); 116 #undef xchg 117 } 118 ti->ti_next = ti->ti_prev = 0; 119 ti->ti_x1 = 0; 120 ti->ti_len = sizeof (struct tcphdr); 121 ti->ti_seq = seq; 122 ti->ti_ack = ack; 123 #if vax 124 ti->ti_len = htons(ti->ti_len); 125 ti->ti_seq = htonl(ti->ti_seq); 126 ti->ti_ack = htonl(ti->ti_ack); 127 #endif 128 ti->ti_x2 = 0; 129 ti->ti_off = sizeof (struct tcphdr) >> 2; 130 ti->ti_flags = flags; 131 ti->ti_win = ti->ti_urp = 0; 132 printf("before cksum m->m_len %d\n", m->m_len); 133 ti->ti_sum = in_cksum(m, sizeof(struct tcpiphdr)); 134 ((struct ip *)ti)->ip_len = sizeof(struct tcpiphdr); 135 ((struct ip *)ti)->ip_ttl = TCP_TTL; 136 printf("to ip_output ip_len %d, m %x\n", ((struct ip *)ti)->ip_len, m); 137 (void) ip_output(m, (struct mbuf *)0); 138 } 139 140 /* 141 * Create a new TCP control block, making an 142 * empty reassembly queue and hooking it to the argument 143 * protocol control block. 144 */ 145 struct tcpcb * 146 tcp_newtcpcb(inp) 147 struct inpcb *inp; 148 { 149 struct mbuf *m = m_getclr(0); 150 register struct tcpcb *tp; 151 COUNT(TCP_NEWTCPCB); 152 153 printf("tcp_newtcpcb %x\n", inp); 154 if (m == 0) 155 return (0); 156 tp = mtod(m, struct tcpcb *); 157 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 158 tp->t_maxseg = 1024; 159 tp->t_inpcb = inp; 160 inp->inp_ppcb = (caddr_t)tp; 161 return (tp); 162 } 163 164 /* 165 * Drop a TCP connection, reporting 166 * the specified error. If connection is synchronized, 167 * then send a RST to peer. 168 */ 169 tcp_drop(tp, errno) 170 struct tcpcb *tp; 171 int errno; 172 { 173 struct socket *so = tp->t_inpcb->inp_socket; 174 175 COUNT(TCP_DROP); 176 printf("tcp_drop %x %d\n", tp, errno); 177 if (TCPS_HAVERCVDSYN(tp->t_state) && 178 TCPS_OURFINNOTACKED(tp->t_state)) { 179 tp->t_state = TCPS_CLOSED; 180 tcp_output(tp); 181 } 182 so->so_error = errno; 183 tcp_close(tp); 184 in_pcbdetach(tp->t_inpcb); 185 } 186 187 /* 188 * Close a TCP control block: 189 * discard all space held by the tcp 190 * discard internet protocol block 191 * wake up any sleepers 192 */ 193 tcp_close(tp) 194 register struct tcpcb *tp; 195 { 196 register struct tcpiphdr *t; 197 struct socket *so = tp->t_inpcb->inp_socket; 198 199 COUNT(TCP_CLOSE); 200 printf("tcp_close %x\n", tp); 201 t = tp->seg_next; 202 for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 203 m_freem(dtom(t)); 204 if (tp->t_template) 205 (void) m_free(dtom(tp->t_template)); 206 if (tp->t_tcpopt) 207 (void) m_free(dtom(tp->t_tcpopt)); 208 if (tp->t_ipopt) 209 (void) m_free(dtom(tp->t_ipopt)); 210 (void) m_free(dtom(tp)); 211 soisdisconnected(so); 212 in_pcbdisconnect(tp->t_inpcb); 213 } 214 215 tcp_drain() 216 { 217 218 COUNT(TCP_DRAIN); 219 } 220 221 tcp_ctlinput(m) 222 struct mbuf *m; 223 { 224 225 COUNT(TCP_CTLINPUT); 226 m_freem(m); 227 } 228