1 /* 2 * Copyright (c) 1982, 1986 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)tcp_subr.c 7.12 (Berkeley) 10/09/87 7 */ 8 9 #include "param.h" 10 #include "systm.h" 11 #include "mbuf.h" 12 #include "socket.h" 13 #include "socketvar.h" 14 #include "protosw.h" 15 #include "errno.h" 16 17 #include "../net/route.h" 18 #include "../net/if.h" 19 20 #include "in.h" 21 #include "in_pcb.h" 22 #include "in_systm.h" 23 #include "ip.h" 24 #include "ip_var.h" 25 #include "ip_icmp.h" 26 #include "tcp.h" 27 #include "tcp_fsm.h" 28 #include "tcp_seq.h" 29 #include "tcp_timer.h" 30 #include "tcp_var.h" 31 #include "tcpip.h" 32 33 int tcp_ttl = TCP_TTL; 34 35 /* 36 * Tcp initialization 37 */ 38 tcp_init() 39 { 40 41 tcp_iss = 1; /* wrong */ 42 tcb.inp_next = tcb.inp_prev = &tcb; 43 } 44 45 /* 46 * Create template to be used to send tcp packets on a connection. 47 * Call after host entry created, allocates an mbuf and fills 48 * in a skeletal tcp/ip header, minimizing the amount of work 49 * necessary when the connection is used. 50 */ 51 struct tcpiphdr * 52 tcp_template(tp) 53 struct tcpcb *tp; 54 { 55 register struct inpcb *inp = tp->t_inpcb; 56 register struct mbuf *m; 57 register struct tcpiphdr *n; 58 59 if ((n = tp->t_template) == 0) { 60 m = m_get(M_DONTWAIT, MT_HEADER); 61 if (m == NULL) 62 return (0); 63 m->m_off = MMAXOFF - sizeof (struct tcpiphdr); 64 m->m_len = sizeof (struct tcpiphdr); 65 n = mtod(m, struct tcpiphdr *); 66 } 67 n->ti_next = n->ti_prev = 0; 68 n->ti_x1 = 0; 69 n->ti_pr = IPPROTO_TCP; 70 n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); 71 n->ti_src = inp->inp_laddr; 72 n->ti_dst = inp->inp_faddr; 73 n->ti_sport = inp->inp_lport; 74 n->ti_dport = inp->inp_fport; 75 n->ti_seq = 0; 76 n->ti_ack = 0; 77 n->ti_x2 = 0; 78 n->ti_off = 5; 79 n->ti_flags = 0; 80 n->ti_win = 0; 81 n->ti_sum = 0; 82 n->ti_urp = 0; 83 return (n); 84 } 85 86 /* 87 * Send a single message to the TCP at address specified by 88 * the given TCP/IP header. If flags==0, then we make a copy 89 * of the tcpiphdr at ti and send directly to the addressed host. 90 * This is used to force keep alive messages out using the TCP 91 * template for a connection tp->t_template. If flags are given 92 * then we send a message back to the TCP which originated the 93 * segment ti, and discard the mbuf containing it and any other 94 * attached mbufs. 95 * 96 * In any case the ack and sequence number of the transmitted 97 * segment are as specified by the parameters. 98 */ 99 tcp_respond(tp, ti, ack, seq, flags) 100 struct tcpcb *tp; 101 register struct tcpiphdr *ti; 102 tcp_seq ack, seq; 103 int flags; 104 { 105 register struct mbuf *m; 106 int win = 0, tlen; 107 struct route *ro = 0; 108 109 if (tp) { 110 win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); 111 ro = &tp->t_inpcb->inp_route; 112 } 113 if (flags == 0) { 114 m = m_get(M_DONTWAIT, MT_HEADER); 115 if (m == NULL) 116 return; 117 #ifdef TCP_COMPAT_42 118 tlen = 1; 119 #else 120 tlen = 0; 121 #endif 122 m->m_len = sizeof (struct tcpiphdr) + tlen; 123 *mtod(m, struct tcpiphdr *) = *ti; 124 ti = mtod(m, struct tcpiphdr *); 125 flags = TH_ACK; 126 } else { 127 m = dtom(ti); 128 m_freem(m->m_next); 129 m->m_next = 0; 130 m->m_off = (int)ti - (int)m; 131 tlen = 0; 132 m->m_len = sizeof (struct tcpiphdr); 133 #define xchg(a,b,type) { type t; t=a; a=b; b=t; } 134 xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); 135 xchg(ti->ti_dport, ti->ti_sport, u_short); 136 #undef xchg 137 } 138 ti->ti_next = ti->ti_prev = 0; 139 ti->ti_x1 = 0; 140 ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); 141 ti->ti_seq = htonl(seq); 142 ti->ti_ack = htonl(ack); 143 ti->ti_x2 = 0; 144 ti->ti_off = sizeof (struct tcphdr) >> 2; 145 ti->ti_flags = flags; 146 ti->ti_win = htons((u_short)win); 147 ti->ti_urp = 0; 148 ti->ti_sum = in_cksum(m, sizeof (struct tcpiphdr) + tlen); 149 ((struct ip *)ti)->ip_len = sizeof (struct tcpiphdr) + tlen; 150 ((struct ip *)ti)->ip_ttl = tcp_ttl; 151 (void) ip_output(m, (struct mbuf *)0, ro, 0); 152 } 153 154 /* 155 * Create a new TCP control block, making an 156 * empty reassembly queue and hooking it to the argument 157 * protocol control block. 158 */ 159 struct tcpcb * 160 tcp_newtcpcb(inp) 161 struct inpcb *inp; 162 { 163 struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); 164 register struct tcpcb *tp; 165 166 if (m == NULL) 167 return ((struct tcpcb *)0); 168 tp = mtod(m, struct tcpcb *); 169 tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; 170 tp->t_maxseg = TCP_MSS; 171 tp->t_flags = 0; /* sends options! */ 172 tp->t_inpcb = inp; 173 /* 174 * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no 175 * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives 176 * reasonable initial retransmit time. 177 */ 178 tp->t_srtt = TCPTV_SRTTBASE; 179 tp->t_rttvar = TCPTV_SRTTDFLT << 2; 180 TCPT_RANGESET(tp->t_rxtcur, 181 ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, 182 TCPTV_MIN, TCPTV_REXMTMAX); 183 tp->snd_cwnd = sbspace(&inp->inp_socket->so_snd); 184 tp->snd_ssthresh = 65535; /* XXX */ 185 inp->inp_ppcb = (caddr_t)tp; 186 return (tp); 187 } 188 189 /* 190 * Drop a TCP connection, reporting 191 * the specified error. If connection is synchronized, 192 * then send a RST to peer. 193 */ 194 struct tcpcb * 195 tcp_drop(tp, errno) 196 register struct tcpcb *tp; 197 int errno; 198 { 199 struct socket *so = tp->t_inpcb->inp_socket; 200 201 if (TCPS_HAVERCVDSYN(tp->t_state)) { 202 tp->t_state = TCPS_CLOSED; 203 (void) tcp_output(tp); 204 tcpstat.tcps_drops++; 205 } else 206 tcpstat.tcps_conndrops++; 207 so->so_error = errno; 208 return (tcp_close(tp)); 209 } 210 211 /* 212 * Close a TCP control block: 213 * discard all space held by the tcp 214 * discard internet protocol block 215 * wake up any sleepers 216 */ 217 struct tcpcb * 218 tcp_close(tp) 219 register struct tcpcb *tp; 220 { 221 register struct tcpiphdr *t; 222 struct inpcb *inp = tp->t_inpcb; 223 struct socket *so = inp->inp_socket; 224 register struct mbuf *m; 225 226 t = tp->seg_next; 227 while (t != (struct tcpiphdr *)tp) { 228 t = (struct tcpiphdr *)t->ti_next; 229 m = dtom(t->ti_prev); 230 remque(t->ti_prev); 231 m_freem(m); 232 } 233 if (tp->t_template) 234 (void) m_free(dtom(tp->t_template)); 235 (void) m_free(dtom(tp)); 236 inp->inp_ppcb = 0; 237 soisdisconnected(so); 238 in_pcbdetach(inp); 239 tcpstat.tcps_closed++; 240 return ((struct tcpcb *)0); 241 } 242 243 tcp_drain() 244 { 245 246 } 247 248 /* 249 * Notify a tcp user of an asynchronous error; 250 * just wake up so that he can collect error status. 251 */ 252 tcp_notify(inp) 253 register struct inpcb *inp; 254 { 255 256 wakeup((caddr_t) &inp->inp_socket->so_timeo); 257 sorwakeup(inp->inp_socket); 258 sowwakeup(inp->inp_socket); 259 } 260 tcp_ctlinput(cmd, sa) 261 int cmd; 262 struct sockaddr *sa; 263 { 264 extern u_char inetctlerrmap[]; 265 struct sockaddr_in *sin; 266 int tcp_quench(), in_rtchange(); 267 268 if ((unsigned)cmd > PRC_NCMDS) 269 return; 270 if (sa->sa_family != AF_INET && sa->sa_family != AF_IMPLINK) 271 return; 272 sin = (struct sockaddr_in *)sa; 273 if (sin->sin_addr.s_addr == INADDR_ANY) 274 return; 275 276 switch (cmd) { 277 278 case PRC_QUENCH: 279 in_pcbnotify(&tcb, &sin->sin_addr, 0, tcp_quench); 280 break; 281 282 case PRC_ROUTEDEAD: 283 case PRC_REDIRECT_NET: 284 case PRC_REDIRECT_HOST: 285 case PRC_REDIRECT_TOSNET: 286 case PRC_REDIRECT_TOSHOST: 287 in_pcbnotify(&tcb, &sin->sin_addr, 0, in_rtchange); 288 break; 289 290 default: 291 if (inetctlerrmap[cmd] == 0) 292 return; /* XXX */ 293 in_pcbnotify(&tcb, &sin->sin_addr, (int)inetctlerrmap[cmd], 294 tcp_notify); 295 } 296 } 297 298 /* 299 * When a source quench is received, close congestion window 300 * to one segment. We will gradually open it again as we proceed. 301 */ 302 tcp_quench(inp) 303 struct inpcb *inp; 304 { 305 struct tcpcb *tp = intotcpcb(inp); 306 307 if (tp) 308 tp->snd_cwnd = tp->t_maxseg; 309 } 310