Commit | Line | Data |
---|---|---|
8ae0e4b4 | 1 | /* |
33042259 | 2 | * Copyright (c) 1982, 1986, 1988, 1990 Regents of the University of California. |
2b6b6284 | 3 | * All rights reserved. |
8ae0e4b4 | 4 | * |
dbf0c423 | 5 | * %sccs.include.redist.c% |
2b6b6284 | 6 | * |
4cc16258 | 7 | * @(#)tcp_subr.c 7.21 (Berkeley) %G% |
8ae0e4b4 | 8 | */ |
ecaa4e6f | 9 | |
20666ad3 | 10 | #include "param.h" |
4cc16258 | 11 | #include "proc.h" |
20666ad3 | 12 | #include "systm.h" |
9d91b170 | 13 | #include "malloc.h" |
20666ad3 JB |
14 | #include "mbuf.h" |
15 | #include "socket.h" | |
16 | #include "socketvar.h" | |
17 | #include "protosw.h" | |
18 | #include "errno.h" | |
f4d55810 | 19 | |
c124e997 | 20 | #include "../net/route.h" |
f4d55810 SL |
21 | #include "../net/if.h" |
22 | ||
20666ad3 | 23 | #include "in.h" |
20666ad3 JB |
24 | #include "in_systm.h" |
25 | #include "ip.h" | |
b1dd4cca | 26 | #include "in_pcb.h" |
20666ad3 JB |
27 | #include "ip_var.h" |
28 | #include "ip_icmp.h" | |
29 | #include "tcp.h" | |
30 | #include "tcp_fsm.h" | |
31 | #include "tcp_seq.h" | |
32 | #include "tcp_timer.h" | |
33 | #include "tcp_var.h" | |
34 | #include "tcpip.h" | |
ecaa4e6f | 35 | |
33042259 | 36 | /* patchable/settable parameters for tcp */ |
10604dba | 37 | int tcp_ttl = TCP_TTL; |
33042259 MK |
38 | int tcp_mssdflt = TCP_MSS; |
39 | int tcp_rttdflt = TCPTV_SRTTDFLT / PR_SLOWHZ; | |
40 | ||
41 | extern struct inpcb *tcp_last_inpcb; | |
10604dba | 42 | |
ecaa4e6f BJ |
43 | /* |
44 | * Tcp initialization | |
45 | */ | |
46 | tcp_init() | |
47 | { | |
48 | ||
49 | tcp_iss = 1; /* wrong */ | |
50 | tcb.inp_next = tcb.inp_prev = &tcb; | |
9d91b170 MK |
51 | if (max_protohdr < sizeof(struct tcpiphdr)) |
52 | max_protohdr = sizeof(struct tcpiphdr); | |
53 | if (max_linkhdr + sizeof(struct tcpiphdr) > MHLEN) | |
54 | panic("tcp_init"); | |
ecaa4e6f BJ |
55 | } |
56 | ||
57 | /* | |
58 | * Create template to be used to send tcp packets on a connection. | |
59 | * Call after host entry created, allocates an mbuf and fills | |
60 | * in a skeletal tcp/ip header, minimizing the amount of work | |
61 | * necessary when the connection is used. | |
62 | */ | |
63 | struct tcpiphdr * | |
64 | tcp_template(tp) | |
65 | struct tcpcb *tp; | |
66 | { | |
67 | register struct inpcb *inp = tp->t_inpcb; | |
68 | register struct mbuf *m; | |
69 | register struct tcpiphdr *n; | |
70 | ||
ece01391 | 71 | if ((n = tp->t_template) == 0) { |
9f5105e3 | 72 | m = m_get(M_DONTWAIT, MT_HEADER); |
ece01391 MK |
73 | if (m == NULL) |
74 | return (0); | |
ece01391 MK |
75 | m->m_len = sizeof (struct tcpiphdr); |
76 | n = mtod(m, struct tcpiphdr *); | |
77 | } | |
ecaa4e6f BJ |
78 | n->ti_next = n->ti_prev = 0; |
79 | n->ti_x1 = 0; | |
80 | n->ti_pr = IPPROTO_TCP; | |
81 | n->ti_len = htons(sizeof (struct tcpiphdr) - sizeof (struct ip)); | |
82 | n->ti_src = inp->inp_laddr; | |
83 | n->ti_dst = inp->inp_faddr; | |
84 | n->ti_sport = inp->inp_lport; | |
85 | n->ti_dport = inp->inp_fport; | |
86 | n->ti_seq = 0; | |
0974b45c | 87 | n->ti_ack = 0; |
ecaa4e6f BJ |
88 | n->ti_x2 = 0; |
89 | n->ti_off = 5; | |
90 | n->ti_flags = 0; | |
91 | n->ti_win = 0; | |
92 | n->ti_sum = 0; | |
93 | n->ti_urp = 0; | |
94 | return (n); | |
95 | } | |
96 | ||
97 | /* | |
405c9168 | 98 | * Send a single message to the TCP at address specified by |
33042259 | 99 | * the given TCP/IP header. If m == 0, then we make a copy |
405c9168 BJ |
100 | * of the tcpiphdr at ti and send directly to the addressed host. |
101 | * This is used to force keep alive messages out using the TCP | |
102 | * template for a connection tp->t_template. If flags are given | |
103 | * then we send a message back to the TCP which originated the | |
104 | * segment ti, and discard the mbuf containing it and any other | |
105 | * attached mbufs. | |
106 | * | |
107 | * In any case the ack and sequence number of the transmitted | |
108 | * segment are as specified by the parameters. | |
ecaa4e6f | 109 | */ |
9d91b170 | 110 | tcp_respond(tp, ti, m, ack, seq, flags) |
8e65fd66 | 111 | struct tcpcb *tp; |
ecaa4e6f | 112 | register struct tcpiphdr *ti; |
9d91b170 | 113 | register struct mbuf *m; |
0974b45c | 114 | tcp_seq ack, seq; |
ecaa4e6f BJ |
115 | int flags; |
116 | { | |
37a28d38 MK |
117 | register int tlen; |
118 | int win = 0; | |
c124e997 | 119 | struct route *ro = 0; |
ecaa4e6f | 120 | |
c124e997 | 121 | if (tp) { |
8e65fd66 | 122 | win = sbspace(&tp->t_inpcb->inp_socket->so_rcv); |
c124e997 SL |
123 | ro = &tp->t_inpcb->inp_route; |
124 | } | |
9d91b170 MK |
125 | if (m == 0) { |
126 | m = m_gethdr(M_DONTWAIT, MT_HEADER); | |
5cdc4d65 | 127 | if (m == NULL) |
405c9168 | 128 | return; |
eeef4ac3 MK |
129 | #ifdef TCP_COMPAT_42 |
130 | tlen = 1; | |
131 | #else | |
132 | tlen = 0; | |
133 | #endif | |
9d91b170 | 134 | m->m_data += max_linkhdr; |
405c9168 BJ |
135 | *mtod(m, struct tcpiphdr *) = *ti; |
136 | ti = mtod(m, struct tcpiphdr *); | |
137 | flags = TH_ACK; | |
138 | } else { | |
139 | m_freem(m->m_next); | |
140 | m->m_next = 0; | |
9d91b170 | 141 | m->m_data = (caddr_t)ti; |
405c9168 | 142 | m->m_len = sizeof (struct tcpiphdr); |
33042259 | 143 | tlen = 0; |
0974b45c | 144 | #define xchg(a,b,type) { type t; t=a; a=b; b=t; } |
405c9168 BJ |
145 | xchg(ti->ti_dst.s_addr, ti->ti_src.s_addr, u_long); |
146 | xchg(ti->ti_dport, ti->ti_sport, u_short); | |
ecaa4e6f | 147 | #undef xchg |
405c9168 | 148 | } |
37a28d38 MK |
149 | ti->ti_len = htons((u_short)(sizeof (struct tcphdr) + tlen)); |
150 | tlen += sizeof (struct tcpiphdr); | |
151 | m->m_len = tlen; | |
152 | m->m_pkthdr.len = tlen; | |
153 | m->m_pkthdr.rcvif = (struct ifnet *) 0; | |
0974b45c BJ |
154 | ti->ti_next = ti->ti_prev = 0; |
155 | ti->ti_x1 = 0; | |
2c48b3f8 BJ |
156 | ti->ti_seq = htonl(seq); |
157 | ti->ti_ack = htonl(ack); | |
0974b45c BJ |
158 | ti->ti_x2 = 0; |
159 | ti->ti_off = sizeof (struct tcphdr) >> 2; | |
ecaa4e6f | 160 | ti->ti_flags = flags; |
af8f6a21 | 161 | ti->ti_win = htons((u_short)win); |
8e65fd66 | 162 | ti->ti_urp = 0; |
37a28d38 MK |
163 | ti->ti_sum = in_cksum(m, tlen); |
164 | ((struct ip *)ti)->ip_len = tlen; | |
10604dba | 165 | ((struct ip *)ti)->ip_ttl = tcp_ttl; |
c124e997 | 166 | (void) ip_output(m, (struct mbuf *)0, ro, 0); |
ecaa4e6f | 167 | } |
a6503abf | 168 | |
0974b45c BJ |
169 | /* |
170 | * Create a new TCP control block, making an | |
171 | * empty reassembly queue and hooking it to the argument | |
172 | * protocol control block. | |
173 | */ | |
a6503abf BJ |
174 | struct tcpcb * |
175 | tcp_newtcpcb(inp) | |
176 | struct inpcb *inp; | |
177 | { | |
cce93e4b | 178 | struct mbuf *m = m_getclr(M_DONTWAIT, MT_PCB); |
a6503abf | 179 | register struct tcpcb *tp; |
a6503abf | 180 | |
5cdc4d65 SL |
181 | if (m == NULL) |
182 | return ((struct tcpcb *)0); | |
a6503abf | 183 | tp = mtod(m, struct tcpcb *); |
a6503abf | 184 | tp->seg_next = tp->seg_prev = (struct tcpiphdr *)tp; |
33042259 MK |
185 | tp->t_maxseg = tcp_mssdflt; |
186 | ||
3e60e1e6 | 187 | tp->t_flags = 0; /* sends options! */ |
a6503abf | 188 | tp->t_inpcb = inp; |
7cc62c26 | 189 | /* |
5ca0b868 MK |
190 | * Init srtt to TCPTV_SRTTBASE (0), so we can tell that we have no |
191 | * rtt estimate. Set rttvar so that srtt + 2 * rttvar gives | |
192 | * reasonable initial retransmit time. | |
7cc62c26 | 193 | */ |
5ca0b868 | 194 | tp->t_srtt = TCPTV_SRTTBASE; |
33042259 MK |
195 | tp->t_rttvar = tcp_rttdflt * PR_SLOWHZ << 2; |
196 | tp->t_rttmin = TCPTV_MIN; | |
dabb0e53 MK |
197 | TCPT_RANGESET(tp->t_rxtcur, |
198 | ((TCPTV_SRTTBASE >> 2) + (TCPTV_SRTTDFLT << 2)) >> 1, | |
199 | TCPTV_MIN, TCPTV_REXMTMAX); | |
33042259 MK |
200 | tp->snd_cwnd = TCP_MAXWIN; |
201 | tp->snd_ssthresh = TCP_MAXWIN; | |
202 | inp->inp_ip.ip_ttl = tcp_ttl; | |
a6503abf BJ |
203 | inp->inp_ppcb = (caddr_t)tp; |
204 | return (tp); | |
205 | } | |
206 | ||
0974b45c BJ |
207 | /* |
208 | * Drop a TCP connection, reporting | |
209 | * the specified error. If connection is synchronized, | |
210 | * then send a RST to peer. | |
211 | */ | |
0e3936fa | 212 | struct tcpcb * |
a6503abf | 213 | tcp_drop(tp, errno) |
0e3936fa | 214 | register struct tcpcb *tp; |
a6503abf BJ |
215 | int errno; |
216 | { | |
217 | struct socket *so = tp->t_inpcb->inp_socket; | |
218 | ||
d3504cc0 | 219 | if (TCPS_HAVERCVDSYN(tp->t_state)) { |
a6503abf | 220 | tp->t_state = TCPS_CLOSED; |
39d536e6 | 221 | (void) tcp_output(tp); |
35f3fc10 MK |
222 | tcpstat.tcps_drops++; |
223 | } else | |
224 | tcpstat.tcps_conndrops++; | |
33042259 MK |
225 | if (errno == ETIMEDOUT && tp->t_softerror) |
226 | errno = tp->t_softerror; | |
a6503abf | 227 | so->so_error = errno; |
0e3936fa | 228 | return (tcp_close(tp)); |
a6503abf BJ |
229 | } |
230 | ||
0974b45c BJ |
231 | /* |
232 | * Close a TCP control block: | |
233 | * discard all space held by the tcp | |
234 | * discard internet protocol block | |
235 | * wake up any sleepers | |
236 | */ | |
0e3936fa | 237 | struct tcpcb * |
a6503abf BJ |
238 | tcp_close(tp) |
239 | register struct tcpcb *tp; | |
240 | { | |
241 | register struct tcpiphdr *t; | |
364801f5 BJ |
242 | struct inpcb *inp = tp->t_inpcb; |
243 | struct socket *so = inp->inp_socket; | |
13e2480b | 244 | register struct mbuf *m; |
33042259 MK |
245 | #ifdef RTV_RTT |
246 | register struct rtentry *rt; | |
a6503abf | 247 | |
33042259 MK |
248 | /* |
249 | * If we sent enough data to get some meaningful characteristics, | |
250 | * save them in the routing entry. 'Enough' is arbitrarily | |
1ac2096c | 251 | * defined as the sendpipesize (default 4K) * 16. This would |
33042259 MK |
252 | * give us 16 rtt samples assuming we only get one sample per |
253 | * window (the usual case on a long haul net). 16 samples is | |
254 | * enough for the srtt filter to converge to within 5% of the correct | |
255 | * value; fewer samples and we could save a very bogus rtt. | |
256 | * | |
257 | * Don't update the default route's characteristics and don't | |
258 | * update anything that the user "locked". | |
259 | */ | |
1ac2096c | 260 | if (SEQ_LT(tp->iss + so->so_snd.sb_hiwat * 16, tp->snd_max) && |
33042259 | 261 | (rt = inp->inp_route.ro_rt) && |
1ac2096c | 262 | ((struct sockaddr_in *)rt_key(rt))->sin_addr.s_addr != INADDR_ANY) { |
33042259 MK |
263 | register u_long i; |
264 | ||
265 | if ((rt->rt_rmx.rmx_locks & RTV_RTT) == 0) { | |
266 | i = tp->t_srtt * | |
267 | (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); | |
268 | if (rt->rt_rmx.rmx_rtt && i) | |
269 | /* | |
270 | * filter this update to half the old & half | |
271 | * the new values, converting scale. | |
272 | * See route.h and tcp_var.h for a | |
273 | * description of the scaling constants. | |
274 | */ | |
275 | rt->rt_rmx.rmx_rtt = | |
276 | (rt->rt_rmx.rmx_rtt + i) / 2; | |
277 | else | |
278 | rt->rt_rmx.rmx_rtt = i; | |
279 | } | |
280 | if ((rt->rt_rmx.rmx_locks & RTV_RTTVAR) == 0) { | |
281 | i = tp->t_rttvar * | |
282 | (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); | |
283 | if (rt->rt_rmx.rmx_rttvar && i) | |
284 | rt->rt_rmx.rmx_rttvar = | |
285 | (rt->rt_rmx.rmx_rttvar + i) / 2; | |
286 | else | |
287 | rt->rt_rmx.rmx_rttvar = i; | |
288 | } | |
289 | /* | |
290 | * update the pipelimit (ssthresh) if it has been updated | |
291 | * already or if a pipesize was specified & the threshhold | |
292 | * got below half the pipesize. I.e., wait for bad news | |
293 | * before we start updating, then update on both good | |
294 | * and bad news. | |
295 | */ | |
296 | if ((rt->rt_rmx.rmx_locks & RTV_SSTHRESH) == 0 && | |
297 | (i = tp->snd_ssthresh) && rt->rt_rmx.rmx_ssthresh || | |
298 | i < (rt->rt_rmx.rmx_sendpipe / 2)) { | |
299 | /* | |
300 | * convert the limit from user data bytes to | |
301 | * packets then to packet data bytes. | |
302 | */ | |
303 | i = (i + tp->t_maxseg / 2) / tp->t_maxseg; | |
304 | if (i < 2) | |
305 | i = 2; | |
306 | i *= (u_long)(tp->t_maxseg + sizeof (struct tcpiphdr)); | |
307 | if (rt->rt_rmx.rmx_ssthresh) | |
308 | rt->rt_rmx.rmx_ssthresh = | |
309 | (rt->rt_rmx.rmx_ssthresh + i) / 2; | |
310 | else | |
311 | rt->rt_rmx.rmx_ssthresh = i; | |
312 | } | |
313 | } | |
314 | #endif RTV_RTT | |
315 | /* free the reassembly queue, if any */ | |
a6503abf | 316 | t = tp->seg_next; |
13e2480b SL |
317 | while (t != (struct tcpiphdr *)tp) { |
318 | t = (struct tcpiphdr *)t->ti_next; | |
33042259 | 319 | m = REASS_MBUF((struct tcpiphdr *)t->ti_prev); |
13e2480b SL |
320 | remque(t->ti_prev); |
321 | m_freem(m); | |
322 | } | |
0974b45c | 323 | if (tp->t_template) |
a6503abf | 324 | (void) m_free(dtom(tp->t_template)); |
a6503abf | 325 | (void) m_free(dtom(tp)); |
364801f5 | 326 | inp->inp_ppcb = 0; |
4aed14e3 | 327 | soisdisconnected(so); |
33042259 MK |
328 | /* clobber input pcb cache if we're closing the cached connection */ |
329 | if (inp == tcp_last_inpcb) | |
330 | tcp_last_inpcb = &tcb; | |
86676257 | 331 | in_pcbdetach(inp); |
35f3fc10 | 332 | tcpstat.tcps_closed++; |
0e3936fa | 333 | return ((struct tcpcb *)0); |
a6503abf BJ |
334 | } |
335 | ||
a6503abf BJ |
336 | tcp_drain() |
337 | { | |
a6503abf | 338 | |
a6503abf BJ |
339 | } |
340 | ||
be841dc3 MK |
341 | /* |
342 | * Notify a tcp user of an asynchronous error; | |
33042259 MK |
343 | * store error as soft error, but wake up user |
344 | * (for now, won't do anything until can select for soft error). | |
be841dc3 | 345 | */ |
33042259 | 346 | tcp_notify(inp, error) |
be841dc3 | 347 | register struct inpcb *inp; |
33042259 | 348 | int error; |
be841dc3 MK |
349 | { |
350 | ||
33042259 | 351 | ((struct tcpcb *)inp->inp_ppcb)->t_softerror = error; |
be841dc3 MK |
352 | wakeup((caddr_t) &inp->inp_socket->so_timeo); |
353 | sorwakeup(inp->inp_socket); | |
354 | sowwakeup(inp->inp_socket); | |
355 | } | |
b1dd4cca MK |
356 | |
357 | tcp_ctlinput(cmd, sa, ip) | |
72e4f44e | 358 | int cmd; |
7c626d4d | 359 | struct sockaddr *sa; |
b1dd4cca | 360 | register struct ip *ip; |
a6503abf | 361 | { |
b1dd4cca MK |
362 | register struct tcphdr *th; |
363 | extern struct in_addr zeroin_addr; | |
39674d5f | 364 | extern u_char inetctlerrmap[]; |
b1dd4cca | 365 | int (*notify)() = tcp_notify, tcp_quench(); |
39674d5f | 366 | |
b1dd4cca MK |
367 | if (cmd == PRC_QUENCH) |
368 | notify = tcp_quench; | |
369 | else if ((unsigned)cmd > PRC_NCMDS || inetctlerrmap[cmd] == 0) | |
7c626d4d | 370 | return; |
b1dd4cca MK |
371 | if (ip) { |
372 | th = (struct tcphdr *)((caddr_t)ip + (ip->ip_hl << 2)); | |
373 | in_pcbnotify(&tcb, sa, th->th_dport, ip->ip_src, th->th_sport, | |
374 | cmd, notify); | |
375 | } else | |
376 | in_pcbnotify(&tcb, sa, 0, zeroin_addr, 0, cmd, notify); | |
a6503abf | 377 | } |
05586739 | 378 | |
9d866d2f MK |
379 | #if BSD<43 |
380 | /* XXX fake routine */ | |
381 | tcp_abort(inp) | |
382 | struct inpcb *inp; | |
383 | { | |
384 | return; | |
385 | } | |
386 | #endif | |
387 | ||
05586739 MK |
388 | /* |
389 | * When a source quench is received, close congestion window | |
2e5a76f2 | 390 | * to one segment. We will gradually open it again as we proceed. |
05586739 MK |
391 | */ |
392 | tcp_quench(inp) | |
393 | struct inpcb *inp; | |
394 | { | |
395 | struct tcpcb *tp = intotcpcb(inp); | |
396 | ||
7c626d4d | 397 | if (tp) |
2e5a76f2 | 398 | tp->snd_cwnd = tp->t_maxseg; |
05586739 | 399 | } |