1 /* tcp_subr.c 4.27 82/06/20 */ 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/route.h" 11 #include "../net/in_pcb.h" 12 #include "../net/in_systm.h" 13 #include "../net/if.h" 14 #include "../net/ip.h" 15 #include "../net/ip_var.h" 16 #include "../net/ip_icmp.h" 17 #include "../net/tcp.h" 18 #include "../net/tcp_fsm.h" 19 #include "../net/tcp_seq.h" 20 #include "../net/tcp_timer.h" 21 #include "../net/tcp_var.h" 22 #include "../net/tcpip.h" 23 #include "../errno.h" 24 25 /* 26 * Tcp initialization 27 */ 28 tcp_init() 29 { 30 31 tcp_iss = 1; /* wrong */ 32 tcb.inp_next = tcb.inp_prev = &tcb; 33 tcp_alpha = TCP_ALPHA; 34 tcp_beta = TCP_BETA; 35 } 36 37 /* 38 * Create template to be used to send tcp packets on a connection. 39 * Call after host entry created, allocates an mbuf and fills 40 * in a skeletal tcp/ip header, minimizing the amount of work 41 * necessary when the connection is used. 42 */ 43 struct tcpiphdr * 44 tcp_template(tp) 45 struct tcpcb *tp; 46 { 47 register struct inpcb *inp = tp->t_inpcb; 48 register struct mbuf *m; 49 register struct tcpiphdr *n; 50 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 struct route *ro = 0; 98 99 if (tp) { 100 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 101 ro = &tp->t_inpcb->inp_route; 102 } 103 if (flags == 0) { 104 m = m_get(M_DONTWAIT); 105 if (m == 0) 106 return; 107 m->m_off = MMINOFF; 108 m->m_len = sizeof (struct tcpiphdr) + 1; 109 *mtod(m, struct tcpiphdr *) = *ti; 110 ti = mtod(m, struct tcpiphdr *); 111 flags = TH_ACK; 112 tlen = 1; 113 } else { 114 m = dtom(ti); 115 m_freem(m->m_next); 116 m->m_next = 0; 117 m->m_off = (int)ti - (int)m; 118 m->m_len = sizeof (struct tcpiphdr); 119 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 120 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 121 xchg(ti->ti_dport, ti->ti_sport, u_short); 122 #undef xchg 123 tlen = 0; 124 } 125 ti->ti_next = ti->ti_prev = 0; 126 ti->ti_x1 = 0; 127 ti->ti_len = sizeof (struct tcphdr) + tlen; 128 ti->ti_seq = seq; 129 ti->ti_ack = ack; 130 #if vax 131 ti->ti_len = htons((u_short)ti->ti_len); 132 ti->ti_seq = htonl(ti->ti_seq); 133 ti->ti_ack = htonl(ti->ti_ack); 134 #endif 135 ti->ti_x2 = 0; 136 ti->ti_off = sizeof (struct tcphdr) >> 2; 137 ti->ti_flags = flags; 138 ti->ti_win = win; 139 #if vax 140 ti->ti_win = htons(ti->ti_win); 141 #endif 142 ti->ti_urp = 0; 143 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 144 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 145 ((struct ip *)ti)->ip_ttl = TCP_TTL; 146 (void) ip_output(m, (struct mbuf *)0, ro, 0); 147 } 148 149 /* 150 * Create a new TCP control block, making an 151 * empty reassembly queue and hooking it to the argument 152 * protocol control block. 153 */ 154 struct tcpcb * 155 tcp_newtcpcb(inp) 156 struct inpcb *inp; 157 { 158 struct mbuf *m = m_getclr(M_DONTWAIT); 159 register struct tcpcb *tp; 160 161 if (m == 0) 162 return (0); 163 tp = mtod(m, struct tcpcb *); 164 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 165 tp->t_maxseg = 576; /* satisfy the rest of the world */ 166 tp->t_flags = 0; /* sends options! */ 167 tp->t_inpcb = inp; 168 inp->inp_ppcb = (caddr_t)tp; 169 return (tp); 170 } 171 172 /* 173 * Drop a TCP connection, reporting 174 * the specified error. If connection is synchronized, 175 * then send a RST to peer. 176 */ 177 tcp_drop(tp, errno) 178 struct tcpcb *tp; 179 int errno; 180 { 181 struct socket *so = tp->t_inpcb->inp_socket; 182 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 tcp_abort(inp) 192 struct inpcb *inp; 193 { 194 tcp_close(inp->inp_ppcb); 195 } 196 197 /* 198 * Close a TCP control block: 199 * discard all space held by the tcp 200 * discard internet protocol block 201 * wake up any sleepers 202 */ 203 tcp_close(tp) 204 register struct tcpcb *tp; 205 { 206 register struct tcpiphdr *t; 207 struct inpcb *inp = tp->t_inpcb; 208 struct socket *so = inp->inp_socket; 209 210 t = tp->seg_next; 211 for (; t != (struct tcpiphdr *)tp; t = (struct tcpiphdr *)t->ti_next) 212 m_freem(dtom(t)); 213 if (tp->t_template) 214 (void) m_free(dtom(tp->t_template)); 215 if (tp->t_tcpopt) 216 (void) m_free(dtom(tp->t_tcpopt)); 217 if (tp->t_ipopt) 218 (void) m_free(dtom(tp->t_ipopt)); 219 (void) m_free(dtom(tp)); 220 inp->inp_ppcb = 0; 221 soisdisconnected(so); 222 in_pcbdetach(inp); 223 } 224 225 tcp_drain() 226 { 227 228 } 229 230 tcp_ctlinput(cmd, arg) 231 int cmd; 232 caddr_t arg; 233 { 234 struct in_addr *sin; 235 extern u_char inetctlerrmap[]; 236 237 if (cmd < 0 || cmd > PRC_NCMDS) 238 return; 239 switch (cmd) { 240 241 case PRC_ROUTEDEAD: 242 break; 243 244 case PRC_QUENCH: 245 break; 246 247 /* these are handled by ip */ 248 case PRC_IFDOWN: 249 case PRC_HOSTDEAD: 250 case PRC_HOSTUNREACH: 251 break; 252 253 default: 254 sin = &((struct icmp *)arg)->icmp_ip.ip_dst; 255 in_pcbnotify(&tcb, sin, inetctlerrmap[cmd], tcp_abort); 256 } 257 } 258