]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
6 | * The contents of this file constitute Original Code as defined in and | |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
11 | * | |
12 | * This Original Code and all software distributed under the License are | |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, | |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the | |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
19 | * | |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * @(#)tcp_input.c 8.12 (Berkeley) 5/24/95 | |
55 | */ | |
56 | ||
57 | #if ISFB31 | |
58 | #include "opt_ipfw.h" /* for ipfw_fwd */ | |
59 | #include "opt_tcpdebug.h" | |
60 | #endif | |
61 | ||
62 | #include <sys/param.h> | |
63 | #include <sys/systm.h> | |
64 | #include <sys/kernel.h> | |
65 | #include <sys/sysctl.h> | |
66 | #include <sys/malloc.h> | |
67 | #include <sys/mbuf.h> | |
68 | #include <sys/proc.h> /* for proc0 declaration */ | |
69 | #include <sys/protosw.h> | |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
72 | #include <sys/syslog.h> | |
73 | ||
74 | #include <kern/cpu_number.h> /* before tcp_seq.h, for tcp_random18() */ | |
75 | ||
76 | #include <net/if.h> | |
77 | #include <net/route.h> | |
78 | ||
79 | #include <netinet/in.h> | |
80 | #include <netinet/in_systm.h> | |
81 | #include <netinet/ip.h> | |
82 | #include <netinet/ip_icmp.h> /* for ICMP_BANDLIM */ | |
83 | #include <netinet/in_var.h> | |
84 | #include <netinet/ip_var.h> | |
85 | #include <netinet/icmp_var.h> /* for ICMP_BANDLIM */ | |
86 | #include <netinet/in_pcb.h> | |
87 | #if INET6 | |
88 | #include <netinet/ip6.h> | |
89 | #include <netinet/icmp6.h> | |
90 | #include <netinet6/nd6.h> | |
91 | #include <netinet6/ip6_var.h> | |
92 | #include <netinet6/in6_pcb.h> | |
93 | #endif | |
94 | #include <netinet/tcp.h> | |
95 | #include <netinet/tcp_fsm.h> | |
96 | #include <netinet/tcp_seq.h> | |
97 | #include <netinet/tcp_timer.h> | |
98 | #include <netinet/tcp_var.h> | |
99 | #include <netinet/tcpip.h> | |
100 | #if TCPDEBUG | |
101 | #include <netinet/tcp_debug.h> | |
102 | #if INET6 | |
103 | union { | |
104 | struct ip _tcp_si4; | |
105 | struct ip6_hdr _tcp_si6; | |
106 | } tcp_saveip; | |
107 | #else | |
108 | struct ip tcp_saveip; | |
109 | #endif /* INET6 */ | |
110 | struct tcphdr tcp_savetcp; | |
111 | #endif /* TCPDEBUG */ | |
112 | ||
113 | #if IPSEC | |
114 | #include <netinet6/ipsec.h> | |
115 | #include <netkey/key.h> | |
116 | #endif /*IPSEC*/ | |
117 | ||
118 | #include <sys/kdebug.h> | |
119 | ||
120 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 0) | |
121 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 2) | |
122 | #define DBG_FNC_TCP_INPUT NETDBG_CODE(DBG_NETTCP, (3 << 8)) | |
123 | #define DBG_FNC_TCP_NEWCONN NETDBG_CODE(DBG_NETTCP, (7 << 8)) | |
124 | ||
125 | static int tcprexmtthresh = 3; | |
126 | tcp_seq tcp_iss; | |
127 | tcp_cc tcp_ccgen; | |
128 | ||
129 | struct tcpstat tcpstat; | |
130 | SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats, | |
131 | CTLFLAG_RD, &tcpstat , tcpstat, ""); | |
132 | ||
133 | int log_in_vain = 0; | |
134 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW, | |
135 | &log_in_vain, 0, ""); | |
136 | ||
137 | int tcp_delack_enabled = 1; | |
138 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, delayed_ack, CTLFLAG_RW, | |
139 | &tcp_delack_enabled, 0, ""); | |
140 | ||
141 | u_long tcp_now; | |
142 | struct inpcbhead tcb; | |
143 | #define tcb6 tcb /* for KAME src sync over BSD*'s */ | |
144 | struct inpcbinfo tcbinfo; | |
145 | ||
146 | static void tcp_dooptions __P((struct tcpcb *, | |
147 | u_char *, int, struct tcphdr *, struct tcpopt *)); | |
148 | static void tcp_pulloutofband __P((struct socket *, | |
149 | struct tcphdr *, struct mbuf *)); | |
150 | static void tcp_xmit_timer __P((struct tcpcb *, int)); | |
151 | ||
152 | /* | |
153 | * Neighbor Discovery, Neighbor Unreachability Detection | |
154 | * Upper layer hint. | |
155 | */ | |
156 | #define ND6_HINT(tp) { \ | |
157 | if ((tp) && (tp)->t_inpcb && (tp)->t_inpcb->in6p_route.ro_rt) \ | |
158 | nd6_nud_hint((tp)->t_inpcb->in6p_route.ro_rt, NULL); \ | |
159 | } | |
160 | ||
161 | ||
162 | extern u_long current_active_connections; | |
163 | extern u_long last_active_conn_count; | |
164 | ||
165 | extern u_long *delack_bitmask; | |
166 | ||
167 | ||
168 | ||
169 | ||
170 | /* | |
171 | * Insert segment ti into reassembly queue of tcp with | |
172 | * control block tp. Return TH_FIN if reassembly now includes | |
173 | * a segment with FIN. The macro form does the common case inline | |
174 | * (segment is the next to be received on an established connection, | |
175 | * and the queue is empty), avoiding linkage into and removal | |
176 | * from the queue and repetition of various conversions. | |
177 | * Set DELACK for segments received in order, but ack immediately | |
178 | * when segments are out of order (so fast retransmit can work). | |
179 | */ | |
180 | #if INET6 | |
181 | #define _ONLY_IF_INET6_(x) x | |
182 | #else | |
183 | #define _ONLY_IF_INET6_(x) | |
184 | #endif | |
185 | #define TCP_REASS(tp, th, tilen, m, so, flags, isipv6, needwakeup) { \ | |
186 | if ((th)->th_seq == (tp)->rcv_nxt && \ | |
187 | (tp)->segq.lh_first == NULL && \ | |
188 | (tp)->t_state == TCPS_ESTABLISHED) { \ | |
189 | if (tcp_delack_enabled) {\ | |
190 | if (last_active_conn_count > DELACK_BITMASK_THRESH) \ | |
191 | TCP_DELACK_BITSET(tp->t_inpcb->hash_element); \ | |
192 | tp->t_flags |= TF_DELACK; \ | |
193 | } \ | |
194 | else \ | |
195 | tp->t_flags |= TF_ACKNOW; \ | |
196 | (tp)->rcv_nxt += (tilen); \ | |
197 | flags = (th)->th_flags & TH_FIN; \ | |
198 | tcpstat.tcps_rcvpack++;\ | |
199 | tcpstat.tcps_rcvbyte += (tilen);\ | |
200 | _ONLY_IF_INET6_(ND6_HINT(tp);) \ | |
201 | sbappend(&(so)->so_rcv, (m)); \ | |
202 | needwakeup++; \ | |
203 | } else { \ | |
204 | (flags) = tcp_reass((tp), (th), (tilen), (m), (isipv6)); \ | |
205 | tp->t_flags |= TF_ACKNOW; \ | |
206 | } \ | |
207 | } | |
208 | ||
209 | /* | |
210 | * Note: | |
211 | * in the ip header part of the ipqe_tcp structure only the length is used. | |
212 | */ | |
213 | int | |
214 | tcp_reass(tp, th, tilen, m, isipv6) | |
215 | register struct tcpcb *tp; | |
216 | register struct tcphdr *th; | |
217 | u_int16_t tilen; | |
218 | struct mbuf *m; | |
219 | #if INET6 | |
220 | int isipv6; | |
221 | #endif | |
222 | { | |
223 | register struct ipqent *p, *q, *nq, *tiqe; | |
224 | struct socket *so = tp->t_inpcb->inp_socket; | |
225 | int flags; | |
226 | ||
227 | /* | |
228 | * Call with th==0 after become established to | |
229 | * force pre-ESTABLISHED data up to user socket. | |
230 | */ | |
231 | if (th == 0) | |
232 | goto present; | |
233 | ||
234 | #if 0 /* Not using GETTCP(m) macro */ | |
235 | m->m_pkthdr.header = ti; | |
236 | #endif | |
237 | ||
238 | /* | |
239 | * Allocate a new queue entry, before we throw away any data. | |
240 | * If we can't, just drop the packet. XXX | |
241 | */ | |
242 | MALLOC(tiqe, struct ipqent *, sizeof (struct ipqent), M_SONAME, M_NOWAIT); | |
243 | if (tiqe == NULL) { | |
244 | tcpstat.tcps_rcvmemdrop++; | |
245 | m_freem(m); | |
246 | return (0); | |
247 | } | |
248 | ||
249 | /* | |
250 | * Find a segment which begins after this one does. | |
251 | */ | |
252 | for (p = NULL, q = tp->segq.lh_first; q != NULL; | |
253 | p = q, q = q->ipqe_q.le_next) | |
254 | if (SEQ_GT(q->ipqe_tcp->ti_seq, th->th_seq)) | |
255 | break; | |
256 | ||
257 | /* | |
258 | * If there is a preceding segment, it may provide some of | |
259 | * our data already. If so, drop the data from the incoming | |
260 | * segment. If it provides all of our data, drop us. | |
261 | */ | |
262 | if (p != NULL) { | |
263 | register struct tcpiphdr *phdr = p->ipqe_tcp; | |
264 | register int i; | |
265 | ||
266 | /* conversion to int (in i) handles seq wraparound */ | |
267 | i = phdr->ti_seq + phdr->ti_len - th->th_seq; | |
268 | if (i > 0) { | |
269 | if (i >= tilen) { | |
270 | tcpstat.tcps_rcvduppack++; | |
271 | tcpstat.tcps_rcvdupbyte += tilen; | |
272 | m_freem(m); | |
273 | FREE(tiqe, M_SONAME); | |
274 | ||
275 | #if 1 /* XXX: NetBSD just return 0 here */ | |
276 | /* | |
277 | * Try to present any queued data | |
278 | * at the left window edge to the user. | |
279 | * This is needed after the 3-WHS | |
280 | * completes. | |
281 | */ | |
282 | goto present; /* ??? */ | |
283 | #endif | |
284 | } | |
285 | m_adj(m, i); | |
286 | tilen -= i; | |
287 | th->th_seq += i; | |
288 | } | |
289 | } | |
290 | tcpstat.tcps_rcvoopack++; | |
291 | tcpstat.tcps_rcvoobyte += tilen; | |
292 | ||
293 | /* | |
294 | * While we overlap succeeding segments trim them or, | |
295 | * if they are completely covered, dequeue them. | |
296 | */ | |
297 | while (q) { | |
298 | register struct tcpiphdr *qhdr = q->ipqe_tcp; | |
299 | register int i = (th->th_seq + tilen) - qhdr->ti_seq; | |
300 | ||
301 | if (i <= 0) | |
302 | break; | |
303 | if (i < qhdr->ti_len) { | |
304 | qhdr->ti_seq += i; | |
305 | qhdr->ti_len -= i; | |
306 | m_adj(q->ipqe_m, i); | |
307 | break; | |
308 | } | |
309 | nq = q->ipqe_q.le_next; | |
310 | m_freem(q->ipqe_m); | |
311 | LIST_REMOVE(q, ipqe_q); | |
312 | FREE(q, M_SONAME); | |
313 | q = nq; | |
314 | } | |
315 | ||
316 | /* Insert the new fragment queue entry into place. */ | |
317 | tiqe->ipqe_m = m; | |
318 | /* | |
319 | * There is a IP or IPv6 header in the mbuf before th | |
320 | * so there is space for an ip header (for the length field) | |
321 | */ | |
322 | #define thtoti(x) \ | |
323 | ((struct tcpiphdr *)(((char *)(x)) - (sizeof (struct ip)))) | |
324 | ||
325 | tiqe->ipqe_tcp = thtoti(th); | |
326 | tiqe->ipqe_tcp->ti_len = tilen; | |
327 | if (p == NULL) { | |
328 | LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q); | |
329 | } else { | |
330 | LIST_INSERT_AFTER(p, tiqe, ipqe_q); | |
331 | } | |
332 | ||
333 | present: | |
334 | /* | |
335 | * Present data to user, advancing rcv_nxt through | |
336 | * completed sequence space. | |
337 | */ | |
338 | if (!TCPS_HAVEESTABLISHED(tp->t_state)) | |
339 | return (0); | |
340 | q = tp->segq.lh_first; | |
341 | if (!q || q->ipqe_tcp->ti_seq != tp->rcv_nxt) | |
342 | return (0); | |
343 | #if 0 | |
344 | /* | |
345 | * XXX from INRIA for NetBSD, but should not happen because | |
346 | * TCPS_HAVEESTABLISHED(tp->t_state) should be true here. | |
347 | */ | |
348 | if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->ti_len) | |
349 | return (0); | |
350 | #endif | |
351 | do { | |
352 | tp->rcv_nxt += q->ipqe_tcp->ti_len; | |
353 | flags = q->ipqe_tcp->ti_flags & TH_FIN; | |
354 | nq = q->ipqe_q.le_next; | |
355 | LIST_REMOVE(q, ipqe_q); | |
356 | if (so->so_state & SS_CANTRCVMORE) | |
357 | m_freem(q->ipqe_m); | |
358 | else | |
359 | sbappend(&so->so_rcv, q->ipqe_m); | |
360 | FREE(q, M_SONAME); | |
361 | q = nq; | |
362 | } while (q && q->ipqe_tcp->ti_seq == tp->rcv_nxt); | |
363 | #if INET6 | |
364 | if (isipv6) | |
365 | ND6_HINT(tp); | |
366 | #endif | |
367 | ||
368 | KERNEL_DEBUG(DBG_LAYER_END, ((ti->ti_dport << 16) | ti->ti_sport), | |
369 | (((ti->ti_src.s_addr & 0xffff) << 16) | (ti->ti_dst.s_addr & 0xffff)), | |
370 | ti->ti_seq, ti->ti_ack, ti->ti_win); | |
371 | ||
372 | sorwakeup(so); | |
373 | return (flags); | |
374 | } | |
375 | ||
376 | /* | |
377 | * TCP input routine, follows pages 65-76 of the | |
378 | * protocol specification dated September, 1981 very closely. | |
379 | */ | |
380 | #if INET6 | |
381 | int | |
382 | tcp6_input(mp, offp, proto) | |
383 | struct mbuf **mp; | |
384 | int *offp, proto; | |
385 | { | |
386 | tcp_input(*mp, *offp); | |
387 | return IPPROTO_DONE; | |
388 | } | |
389 | #endif | |
390 | ||
391 | void | |
392 | tcp_input(m, off) | |
393 | struct mbuf *m; | |
394 | int off; | |
395 | { | |
396 | register struct tcphdr *th; | |
397 | register struct ip *ip = NULL; | |
398 | register struct ipovly *ipov; | |
399 | register struct inpcb *inp; | |
400 | u_char *optp = NULL; | |
401 | int optlen = 0; | |
402 | int len, toff; | |
403 | int hdroptlen; | |
404 | u_int16_t tilen; | |
405 | register struct tcpcb *tp = 0; | |
406 | register int thflags; | |
407 | struct socket *so = 0; | |
408 | int todrop, acked, ourfinisacked, needoutput = 0; | |
409 | struct in_addr laddr; | |
410 | #if 0 | |
411 | struct in6_addr laddr6; | |
412 | #endif | |
413 | int dropsocket = 0; | |
414 | int iss = 0; | |
415 | u_long tiwin; | |
416 | struct tcpopt to; /* options in this segment */ | |
417 | struct rmxp_tao *taop; /* pointer to our TAO cache entry */ | |
418 | struct rmxp_tao tao_noncached; /* in case there's no cached entry */ | |
419 | int need_sowwakeup = 0; | |
420 | int need_sorwakeup = 0; | |
421 | #if TCPDEBUG | |
422 | short ostate = 0; | |
423 | #endif | |
424 | #if INET6 | |
425 | struct ip6_hdr *ip6 = NULL; | |
426 | int lgminh; | |
427 | #else /* INET6 */ | |
428 | #define lgminh (sizeof (struct tcpiphdr)) | |
429 | #endif /* INET6 */ | |
430 | int isipv6 = (mtod(m, struct ip *)->ip_v == 6) ? 1 : 0; | |
431 | ||
432 | struct proc *proc0=current_proc(); | |
433 | ||
434 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_START,0,0,0,0,0); | |
435 | ||
436 | bzero((char *)&to, sizeof(to)); | |
437 | ||
438 | tcpstat.tcps_rcvtotal++; | |
439 | /* | |
440 | * Get IP and TCP header together in first mbuf. | |
441 | * Note: IP leaves IP header in first mbuf. | |
442 | */ | |
443 | th = mtod(m, struct tcpiphdr *); | |
444 | ||
445 | KERNEL_DEBUG(DBG_LAYER_BEG, ((ti->ti_dport << 16) | ti->ti_sport), | |
446 | (((ti->ti_src.s_addr & 0xffff) << 16) | (ti->ti_dst.s_addr & 0xffff)), | |
447 | ti->ti_seq, ti->ti_ack, ti->ti_win); | |
448 | ||
449 | #if INET6 | |
450 | if (isipv6) { | |
451 | ip6 = mtod(m, struct ip6_hdr *); | |
452 | lgminh = sizeof(struct tcpip6hdr); | |
453 | } else { | |
454 | lgminh = sizeof(struct tcpiphdr); | |
455 | #endif /* INET6 */ | |
456 | ip = mtod(m, struct ip *); | |
457 | ipov = (struct ipovly *)ip; | |
458 | #if INET6 | |
459 | } | |
460 | #endif /* INET6 */ | |
461 | ||
462 | #if INET6 | |
463 | /* XXX not a good place to put this into... */ | |
464 | if (isipv6 && | |
465 | m && (m->m_flags & M_ANYCAST6)) { | |
466 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_ADDR, | |
467 | (caddr_t)&ip6->ip6_dst - (caddr_t)ip6); | |
468 | return; | |
469 | } | |
470 | #endif /* INET6 */ | |
471 | ||
472 | #if INET6 | |
473 | if (isipv6) { | |
474 | IP6_EXTHDR_CHECK(m, off, sizeof(struct tcphdr), ); | |
475 | ip6 = mtod(m, struct ip6_hdr *); | |
476 | tilen = ntohs(ip6->ip6_plen) - off + sizeof(*ip6); | |
477 | ||
478 | if (in6_cksum(m, IPPROTO_TCP, off, tilen)) { | |
479 | tcpstat.tcps_rcvbadsum++; | |
480 | goto drop; | |
481 | } | |
482 | th = (struct tcphdr *)((caddr_t)ip6 + off); | |
483 | } else | |
484 | #endif /* INET6 */ | |
485 | { | |
486 | /* | |
487 | * Get IP and TCP header together in first mbuf. | |
488 | * Note: IP leaves IP header in first mbuf. | |
489 | */ | |
490 | /* XXX: should we still require this for IPv4? */ | |
491 | if (off > sizeof (struct ip)) { | |
492 | ip_stripoptions(m, (struct mbuf *)0); | |
493 | off = sizeof(struct ip); | |
494 | } | |
495 | if (m->m_len < lgminh) { | |
496 | if ((m = m_pullup(m, lgminh)) == 0) { | |
497 | tcpstat.tcps_rcvshort++; | |
498 | return; | |
499 | } | |
500 | } | |
501 | ip = mtod(m, struct ip *); | |
502 | ipov = (struct ipovly *)ip; | |
503 | ||
504 | /* | |
505 | * Checksum extended TCP header and data. | |
506 | */ | |
507 | tilen = ip->ip_len; | |
508 | len = sizeof (struct ip) + tilen; | |
509 | bzero(ipov->ih_x1, sizeof(ipov->ih_x1)); | |
510 | ipov->ih_len = (u_short)tilen; | |
511 | HTONS(ipov->ih_len); | |
512 | th = (struct tcphdr *)((caddr_t)ip + off); | |
513 | th->th_sum = in_cksum(m, len); | |
514 | if (th->th_sum) { | |
515 | tcpstat.tcps_rcvbadsum++; | |
516 | goto drop; | |
517 | } | |
518 | } | |
519 | ||
520 | /* | |
521 | * Check that TCP offset makes sense, | |
522 | * pull out TCP options and adjust length. XXX | |
523 | */ | |
524 | toff = th->th_off << 2; | |
525 | if (toff < sizeof (struct tcphdr) || toff > tilen) { | |
526 | tcpstat.tcps_rcvbadoff++; | |
527 | goto drop; | |
528 | } | |
529 | tilen -= toff; | |
530 | if (toff > sizeof (struct tcphdr)) { | |
531 | #if INET6 | |
532 | if (isipv6) { | |
533 | IP6_EXTHDR_CHECK(m, off, toff, ); | |
534 | ip6 = mtod(m, struct ip6_hdr *); | |
535 | th = (struct tcphdr *)((caddr_t)ip6 + off); | |
536 | } else | |
537 | #endif /* INET6 */ | |
538 | { | |
539 | if (m->m_len < sizeof(struct ip) + toff) { | |
540 | if ((m = m_pullup(m, sizeof (struct ip) + toff)) == 0) { | |
541 | tcpstat.tcps_rcvshort++; | |
542 | return; | |
543 | } | |
544 | ip = mtod(m, struct ip *); | |
545 | ipov = (struct ipovly *)ip; | |
546 | th = (struct tcphdr *)((caddr_t)ip + off); | |
547 | } | |
548 | } | |
549 | optlen = toff - sizeof (struct tcphdr); | |
550 | optp = (u_char *)(th + 1); | |
551 | /* | |
552 | * Do quick retrieval of timestamp options ("options | |
553 | * prediction?"). If timestamp is the only option and it's | |
554 | * formatted as recommended in RFC 1323 appendix A, we | |
555 | * quickly get the values now and not bother calling | |
556 | * tcp_dooptions(), etc. | |
557 | */ | |
558 | if ((optlen == TCPOLEN_TSTAMP_APPA || | |
559 | (optlen > TCPOLEN_TSTAMP_APPA && | |
560 | optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) && | |
561 | *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) && | |
562 | (th->th_flags & TH_SYN) == 0) { | |
563 | to.to_flag |= TOF_TS; | |
564 | to.to_tsval = ntohl(*(u_int32_t *)(optp + 4)); | |
565 | to.to_tsecr = ntohl(*(u_int32_t *)(optp + 8)); | |
566 | optp = NULL; /* we've parsed the options */ | |
567 | } | |
568 | } | |
569 | thflags = th->th_flags; | |
570 | ||
571 | /* | |
572 | * Convert TCP protocol specific fields to host format. | |
573 | */ | |
574 | NTOHL(th->th_seq); | |
575 | NTOHL(th->th_ack); | |
576 | NTOHS(th->th_win); | |
577 | NTOHS(th->th_urp); | |
578 | ||
579 | /* | |
580 | * Drop TCP, IP headers and TCP options. | |
581 | */ | |
582 | hdroptlen = off+toff; | |
583 | m->m_data += hdroptlen; | |
584 | m->m_len -= hdroptlen; | |
585 | ||
586 | /* | |
587 | * Locate pcb for segment. | |
588 | */ | |
589 | findpcb: | |
590 | #if IPFIREWALL_FORWARD | |
591 | if (ip_fw_fwd_addr != NULL | |
592 | #if INET6 | |
593 | && isipv6 == NULL | |
594 | #endif /* INET6 */ | |
595 | ) { | |
596 | /* | |
597 | * Diverted. Pretend to be the destination. | |
598 | * already got one like this? | |
599 | */ | |
600 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, | |
601 | ip->ip_dst, th->th_dport, 0, m->m_pkthdr.rcvif); | |
602 | if (!inp) { | |
603 | /* | |
604 | * No, then it's new. Try find the ambushing socket | |
605 | */ | |
606 | if (!ip_fw_fwd_addr->sin_port) { | |
607 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, | |
608 | th->th_sport, ip_fw_fwd_addr->sin_addr, | |
609 | th->th_dport, 1, m->m_pkthdr.rcvif); | |
610 | } else { | |
611 | inp = in_pcblookup_hash(&tcbinfo, | |
612 | ip->ip_src, th->th_sport, | |
613 | ip_fw_fwd_addr->sin_addr, | |
614 | ntohs(ip_fw_fwd_addr->sin_port), 1, | |
615 | m->m_pkthdr.rcvif); | |
616 | } | |
617 | } | |
618 | ip_fw_fwd_addr = NULL; | |
619 | } else | |
620 | #endif /* IPFIREWALL_FORWARD */ | |
621 | ||
622 | #if INET6 | |
623 | if (isipv6) | |
624 | inp = in6_pcblookup_hash(&tcbinfo, &ip6->ip6_src, th->th_sport, | |
625 | &ip6->ip6_dst, th->th_dport, 1, | |
626 | m->m_pkthdr.rcvif); | |
627 | else | |
628 | #endif /* INET6 */ | |
629 | inp = in_pcblookup_hash(&tcbinfo, ip->ip_src, th->th_sport, | |
630 | ip->ip_dst, th->th_dport, 1, m->m_pkthdr.rcvif); | |
631 | ||
632 | #if IPSEC | |
633 | /* due to difference from other BSD stacks */ | |
634 | m->m_data -= hdroptlen; | |
635 | m->m_len += hdroptlen; | |
636 | #if INET6 | |
637 | if (isipv6) { | |
638 | if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) { | |
639 | ipsec6stat.in_polvio++; | |
640 | goto drop; | |
641 | } | |
642 | } else | |
643 | #endif /* INET6 */ | |
644 | if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) { | |
645 | ipsecstat.in_polvio++; | |
646 | goto drop; | |
647 | } | |
648 | m->m_data += hdroptlen; | |
649 | m->m_len -= hdroptlen; | |
650 | #endif /*IPSEC*/ | |
651 | ||
652 | /* | |
653 | * If the state is CLOSED (i.e., TCB does not exist) then | |
654 | * all data in the incoming segment is discarded. | |
655 | * If the TCB exists but is in CLOSED state, it is embryonic, | |
656 | * but should either do a listen or a connect soon. | |
657 | */ | |
658 | if (inp == NULL) { | |
659 | if (log_in_vain && thflags & TH_SYN) { | |
660 | #if INET6 | |
661 | char buf[INET6_ADDRSTRLEN]; | |
662 | #else /* INET6 */ | |
663 | char buf[4*sizeof "123"]; | |
664 | #endif /* INET6 */ | |
665 | ||
666 | #if INET6 | |
667 | if (isipv6) { | |
668 | strcpy(buf, ip6_sprintf(&ip6->ip6_dst)); | |
669 | log(LOG_INFO, | |
670 | "Connection attempt to TCP %s:%d from %s:%d\n", | |
671 | buf, ntohs(th->th_dport), | |
672 | ip6_sprintf(&ip6->ip6_src), | |
673 | ntohs(th->th_sport)); | |
674 | } else { | |
675 | #endif | |
676 | strcpy(buf, inet_ntoa(ip->ip_dst)); | |
677 | log(LOG_INFO, | |
678 | "Connection attempt to TCP %s:%d from %s:%d\n", | |
679 | buf, ntohs(th->th_dport), inet_ntoa(ip->ip_src), | |
680 | ntohs(th->th_sport)); | |
681 | #if INET6 | |
682 | } | |
683 | #endif /* INET6 */ | |
684 | } | |
685 | #if ICMP_BANDLIM | |
686 | if (badport_bandlim(1) < 0) | |
687 | goto drop; | |
688 | #endif | |
689 | goto dropwithreset; | |
690 | } | |
691 | tp = intotcpcb(inp); | |
692 | if (tp == 0) | |
693 | goto dropwithreset; | |
694 | if (tp->t_state == TCPS_CLOSED) | |
695 | goto drop; | |
696 | ||
697 | /* Unscale the window into a 32-bit value. */ | |
698 | if ((thflags & TH_SYN) == 0) | |
699 | tiwin = th->th_win << tp->snd_scale; | |
700 | else | |
701 | tiwin = th->th_win; | |
702 | ||
703 | so = inp->inp_socket; | |
704 | if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) { | |
705 | #if TCPDEBUG | |
706 | if (so->so_options & SO_DEBUG) { | |
707 | ostate = tp->t_state; | |
708 | #if INET6 | |
709 | if (isipv6) | |
710 | tcp_saveip._tcp_si6 = *ip6; | |
711 | else | |
712 | tcp_saveip._tcp_si4 = *ip; | |
713 | #else /* INET6 */ | |
714 | tcp_saveip = *ip; | |
715 | #endif /* INET6 */ | |
716 | ||
717 | tcp_savetcp = *th; | |
718 | } | |
719 | #endif | |
720 | if (so->so_options & SO_ACCEPTCONN) { | |
721 | register struct tcpcb *tp0 = tp; | |
722 | struct socket *so2; | |
723 | #if IPSEC | |
724 | struct socket *oso; | |
725 | #endif | |
726 | #if INET6 | |
727 | struct inpcb *oinp = sotoinpcb(so); | |
728 | #endif /* INET6 */ | |
729 | ||
730 | #if !IPSEC | |
731 | if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { | |
732 | /* | |
733 | * Note: dropwithreset makes sure we don't | |
734 | * send a RST in response to a RST. | |
735 | */ | |
736 | if (thflags & TH_ACK) { | |
737 | tcpstat.tcps_badsyn++; | |
738 | goto dropwithreset; | |
739 | } | |
740 | goto drop; | |
741 | } | |
742 | #endif | |
743 | KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_START,0,0,0,0,0); | |
744 | so2 = sonewconn(so, 0); | |
745 | ||
746 | ||
747 | if (so2 == 0) { | |
748 | tcpstat.tcps_listendrop++; | |
749 | so2 = sodropablereq(so); | |
750 | if (so2) { | |
751 | tcp_drop(sototcpcb(so2), ETIMEDOUT); | |
752 | so2 = sonewconn(so, 0); | |
753 | } | |
754 | if (!so2) | |
755 | goto drop; | |
756 | } | |
757 | #if IPSEC | |
758 | oso = so; | |
759 | #endif | |
760 | so = so2; | |
761 | /* | |
762 | * This is ugly, but .... | |
763 | * | |
764 | * Mark socket as temporary until we're | |
765 | * committed to keeping it. The code at | |
766 | * ``drop'' and ``dropwithreset'' check the | |
767 | * flag dropsocket to see if the temporary | |
768 | * socket created here should be discarded. | |
769 | * We mark the socket as discardable until | |
770 | * we're committed to it below in TCPS_LISTEN. | |
771 | */ | |
772 | dropsocket++; | |
773 | inp = (struct inpcb *)so->so_pcb; | |
774 | #if INET6 | |
775 | if (isipv6) | |
776 | inp->in6p_laddr = ip6->ip6_dst; | |
777 | else { | |
778 | if (ip6_mapped_addr_on) { | |
779 | inp->inp_vflag &= ~INP_IPV6; | |
780 | inp->inp_vflag |= INP_IPV4; | |
781 | } | |
782 | #endif /* INET6 */ | |
783 | inp->inp_laddr = ip->ip_dst; | |
784 | #if INET6 | |
785 | } | |
786 | #endif /* INET6 */ | |
787 | ||
788 | inp->inp_lport = th->th_dport; | |
789 | if (in_pcbinshash(inp) != 0) { | |
790 | /* | |
791 | * Undo the assignments above if we failed to put | |
792 | * the PCB on the hash lists. | |
793 | */ | |
794 | #if INET6 | |
795 | if (isipv6) | |
796 | inp->in6p_laddr = in6addr_any; | |
797 | else | |
798 | #endif /* INET6 */ | |
799 | inp->inp_laddr.s_addr = INADDR_ANY; | |
800 | inp->inp_lport = 0; | |
801 | goto drop; | |
802 | } | |
803 | #if IPSEC | |
804 | /* | |
805 | * from IPsec perspective, it is important to do it | |
806 | * after making actual listening socket. | |
807 | * otherwise, cached security association will bark. | |
808 | * | |
809 | * Subject: (KAME-snap 748) | |
810 | * From: Wayne Knowles <w.knowles@niwa.cri.nz> | |
811 | */ | |
812 | if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) { | |
813 | /* | |
814 | * Note: dropwithreset makes sure we don't | |
815 | * send a RST in response to a RST. | |
816 | */ | |
817 | if (thflags & TH_ACK) { | |
818 | tcpstat.tcps_badsyn++; | |
819 | goto dropwithreset; | |
820 | } | |
821 | goto drop; | |
822 | } | |
823 | #endif | |
824 | #if INET6 | |
825 | if (isipv6) { | |
826 | struct ip6_recvpktopts newopts; | |
827 | ||
828 | /* | |
829 | * Inherit socket options from the listening | |
830 | * socket. | |
831 | * Note that in6p_inputopts are not (even | |
832 | * should not be) copied, since it stores | |
833 | * previously received options and is used to | |
834 | * detect if each new option is different than | |
835 | * the previous one and hence should be passed | |
836 | * to a user. | |
837 | * If we copied in6p_inputopts, a user would | |
838 | * not be able to receive options just after | |
839 | * calling the accept system call. | |
840 | */ | |
841 | inp->inp_flags |= | |
842 | oinp->inp_flags & INP_CONTROLOPTS; | |
843 | if (oinp->in6p_outputopts) | |
844 | inp->in6p_outputopts = | |
845 | ip6_copypktopts(oinp->in6p_outputopts, | |
846 | M_NOWAIT); | |
847 | } else | |
848 | #endif /* INET6 */ | |
849 | inp->inp_options = ip_srcroute(); | |
850 | #if IPSEC | |
851 | /* copy old policy into new socket's */ | |
852 | if (ipsec_copy_policy(sotoinpcb(oso)->inp_sp, | |
853 | inp->inp_sp)) | |
854 | printf("tcp_input: could not copy policy\n"); | |
855 | #endif | |
856 | ||
857 | tp = intotcpcb(inp); | |
858 | tp->t_state = TCPS_LISTEN; | |
859 | tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT); | |
860 | ||
861 | /* Compute proper scaling value from buffer space */ | |
862 | while (tp->request_r_scale < TCP_MAX_WINSHIFT && | |
863 | TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat) | |
864 | tp->request_r_scale++; | |
865 | ||
866 | KERNEL_DEBUG(DBG_FNC_TCP_NEWCONN | DBG_FUNC_END,0,0,0,0,0); | |
867 | } | |
868 | } | |
869 | ||
870 | #if INET6 | |
871 | /* save packet options if user wanted */ | |
872 | if (isipv6 && (inp->in6p_flags & INP_CONTROLOPTS) != 0) { | |
873 | struct ip6_recvpktopts opts6; | |
874 | ||
875 | /* | |
876 | * Temporarily re-adjusting the mbuf before ip6_savecontrol(), | |
877 | * which is necessary for FreeBSD only due to difference from | |
878 | * other BSD stacks. | |
879 | * XXX: we'll soon make a more natural fix after getting a | |
880 | * consensus. | |
881 | */ | |
882 | #ifndef DEFER_MADJ | |
883 | m->m_data -= hdroptlen; | |
884 | m->m_len += hdroptlen; | |
885 | #endif | |
886 | ip6_savecontrol(inp, ip6, m, &opts6, &inp->in6p_inputopts); | |
887 | if (inp->in6p_inputopts) | |
888 | ip6_update_recvpcbopt(inp->in6p_inputopts, &opts6); | |
889 | if (opts6.head) { | |
890 | if (sbappendcontrol(&inp->in6p_socket->so_rcv, | |
891 | NULL, opts6.head) | |
892 | == 0) | |
893 | m_freem(opts6.head); | |
894 | } | |
895 | #ifndef DEFER_MADJ | |
896 | m->m_data += hdroptlen; /* XXX */ | |
897 | m->m_len -= hdroptlen; /* XXX */ | |
898 | #endif | |
899 | } | |
900 | #endif /* INET6 */ | |
901 | ||
902 | /* | |
903 | * Segment received on connection. | |
904 | * Reset idle time and keep-alive timer. | |
905 | */ | |
906 | tp->t_idle = 0; | |
907 | if (TCPS_HAVEESTABLISHED(tp->t_state)) | |
908 | tp->t_timer[TCPT_KEEP] = tcp_keepidle; | |
909 | ||
910 | /* | |
911 | * Process options if not in LISTEN state, | |
912 | * else do it below (after getting remote address). | |
913 | */ | |
914 | if (tp->t_state != TCPS_LISTEN && optp) | |
915 | tcp_dooptions(tp, optp, optlen, th, &to); | |
916 | if (th->th_flags & TH_SYN) | |
917 | tcp_mss(tp, to.to_maxseg, isipv6); /* sets t_maxseg */ | |
918 | ||
919 | /* | |
920 | * Header prediction: check for the two common cases | |
921 | * of a uni-directional data xfer. If the packet has | |
922 | * no control flags, is in-sequence, the window didn't | |
923 | * change and we're not retransmitting, it's a | |
924 | * candidate. If the length is zero and the ack moved | |
925 | * forward, we're the sender side of the xfer. Just | |
926 | * free the data acked & wake any higher level process | |
927 | * that was blocked waiting for space. If the length | |
928 | * is non-zero and the ack didn't move, we're the | |
929 | * receiver side. If we're getting packets in-order | |
930 | * (the reassembly queue is empty), add the data to | |
931 | * the socket buffer and note that we need a delayed ack. | |
932 | * Make sure that the hidden state-flags are also off. | |
933 | * Since we check for TCPS_ESTABLISHED above, it can only | |
934 | * be TH_NEEDSYN. | |
935 | */ | |
936 | if (tp->t_state == TCPS_ESTABLISHED && | |
937 | (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK && | |
938 | ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) && | |
939 | ((to.to_flag & TOF_TS) == 0 || | |
940 | TSTMP_GEQ(to.to_tsval, tp->ts_recent)) && | |
941 | /* | |
942 | * Using the CC option is compulsory if once started: | |
943 | * the segment is OK if no T/TCP was negotiated or | |
944 | * if the segment has a CC option equal to CCrecv | |
945 | */ | |
946 | ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) || | |
947 | ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) && | |
948 | th->th_seq == tp->rcv_nxt && | |
949 | tiwin && tiwin == tp->snd_wnd && | |
950 | tp->snd_nxt == tp->snd_max) { | |
951 | ||
952 | /* | |
953 | * If last ACK falls within this segment's sequence numbers, | |
954 | * record the timestamp. | |
955 | * NOTE that the test is modified according to the latest | |
956 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
957 | */ | |
958 | if ((to.to_flag & TOF_TS) != 0 && | |
959 | SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { | |
960 | tp->ts_recent_age = tcp_now; | |
961 | tp->ts_recent = to.to_tsval; | |
962 | } | |
963 | ||
964 | if (tilen == 0) { | |
965 | if (SEQ_GT(th->th_ack, tp->snd_una) && | |
966 | SEQ_LEQ(th->th_ack, tp->snd_max) && | |
967 | tp->snd_cwnd >= tp->snd_wnd && | |
968 | tp->t_dupacks < tcprexmtthresh) { | |
969 | /* | |
970 | * this is a pure ack for outstanding data. | |
971 | */ | |
972 | ++tcpstat.tcps_predack; | |
973 | if ((to.to_flag & TOF_TS) != 0) | |
974 | tcp_xmit_timer(tp, | |
975 | tcp_now - to.to_tsecr + 1); | |
976 | else if (tp->t_rtt && | |
977 | SEQ_GT(th->th_ack, tp->t_rtseq)) | |
978 | tcp_xmit_timer(tp, tp->t_rtt); | |
979 | acked = th->th_ack - tp->snd_una; | |
980 | tcpstat.tcps_rcvackpack++; | |
981 | tcpstat.tcps_rcvackbyte += acked; | |
982 | sbdrop(&so->so_snd, acked); | |
983 | tp->snd_una = th->th_ack; | |
984 | m_freem(m); | |
985 | #if INET6 | |
986 | /* some progress has been done */ | |
987 | if (isipv6) | |
988 | ND6_HINT(tp); | |
989 | #endif | |
990 | ||
991 | /* | |
992 | * If all outstanding data are acked, stop | |
993 | * retransmit timer, otherwise restart timer | |
994 | * using current (possibly backed-off) value. | |
995 | * If process is waiting for space, | |
996 | * wakeup/selwakeup/signal. If data | |
997 | * are ready to send, let tcp_output | |
998 | * decide between more output or persist. | |
999 | */ | |
1000 | if (tp->snd_una == tp->snd_max) | |
1001 | tp->t_timer[TCPT_REXMT] = 0; | |
1002 | else if (tp->t_timer[TCPT_PERSIST] == 0) | |
1003 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1004 | ||
1005 | if (so->so_snd.sb_cc) | |
1006 | (void) tcp_output(tp); | |
1007 | sowwakeup(so); | |
1008 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
1009 | return; | |
1010 | } | |
1011 | } else if (th->th_ack == tp->snd_una && | |
1012 | tp->segq.lh_first == NULL && | |
1013 | tilen <= sbspace(&so->so_rcv)) { | |
1014 | /* | |
1015 | * this is a pure, in-sequence data packet | |
1016 | * with nothing on the reassembly queue and | |
1017 | * we have enough buffer space to take it. | |
1018 | */ | |
1019 | ++tcpstat.tcps_preddat; | |
1020 | tp->rcv_nxt += tilen; | |
1021 | tcpstat.tcps_rcvpack++; | |
1022 | tcpstat.tcps_rcvbyte += tilen; | |
1023 | #if INET6 | |
1024 | /* some progress has been done */ | |
1025 | if (isipv6) | |
1026 | ND6_HINT(tp); | |
1027 | #endif | |
1028 | sbappend(&so->so_rcv, m); | |
1029 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
1030 | (((th->th_src.s_addr & 0xffff) << 16) | (th->th_dst.s_addr & 0xffff)), | |
1031 | th->th_seq, th->th_ack, th->th_win); | |
1032 | if (tcp_delack_enabled) { | |
1033 | if (last_active_conn_count > DELACK_BITMASK_THRESH) | |
1034 | TCP_DELACK_BITSET(tp->t_inpcb->hash_element); | |
1035 | tp->t_flags |= TF_DELACK; | |
1036 | } else { | |
1037 | tp->t_flags |= TF_ACKNOW; | |
1038 | tcp_output(tp); | |
1039 | } | |
1040 | sorwakeup(so); | |
1041 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
1042 | return; | |
1043 | } | |
1044 | } | |
1045 | ||
1046 | /* | |
1047 | * Calculate amount of space in receive window, | |
1048 | * and then do TCP input processing. | |
1049 | * Receive window is amount of space in rcv queue, | |
1050 | * but not less than advertised window. | |
1051 | */ | |
1052 | { int win; | |
1053 | ||
1054 | win = sbspace(&so->so_rcv); | |
1055 | if (win < 0) | |
1056 | win = 0; | |
1057 | tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt)); | |
1058 | } | |
1059 | ||
1060 | switch (tp->t_state) { | |
1061 | ||
1062 | /* | |
1063 | * If the state is LISTEN then ignore segment if it contains an RST. | |
1064 | * If the segment contains an ACK then it is bad and send a RST. | |
1065 | * If it does not contain a SYN then it is not interesting; drop it. | |
1066 | * If it is from this socket, drop it, it must be forged. | |
1067 | * Don't bother responding if the destination was a broadcast. | |
1068 | * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial | |
1069 | * tp->iss, and send a segment: | |
1070 | * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK> | |
1071 | * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss. | |
1072 | * Fill in remote peer address fields if not previously specified. | |
1073 | * Enter SYN_RECEIVED state, and process any other fields of this | |
1074 | * segment in this state. | |
1075 | */ | |
1076 | case TCPS_LISTEN: { | |
1077 | register struct sockaddr_in *sin; | |
1078 | #if 0 | |
1079 | register struct sockaddr_in6 *sin6; | |
1080 | #endif | |
1081 | ||
1082 | if (thflags & TH_RST) | |
1083 | goto drop; | |
1084 | if (thflags & TH_ACK) | |
1085 | goto dropwithreset; | |
1086 | if ((thflags & TH_SYN) == 0) | |
1087 | goto drop; | |
1088 | if (th->th_dport == th->th_sport) { | |
1089 | #if INET6 | |
1090 | if (isipv6) { | |
1091 | if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, | |
1092 | &ip6->ip6_src)) | |
1093 | goto drop; | |
1094 | } else | |
1095 | #endif /* INET6 */ | |
1096 | if (ip->ip_dst.s_addr == ip->ip_src.s_addr) | |
1097 | goto drop; | |
1098 | } | |
1099 | ||
1100 | #if INET6 | |
1101 | if (isipv6) { | |
1102 | if (m->m_flags & (M_BCAST|M_MCAST) || | |
1103 | IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) | |
1104 | goto drop; | |
1105 | #if 1 | |
1106 | /* | |
1107 | * Perhaps this should be a call/macro | |
1108 | * to a function like in6_pcbconnect(), but almost | |
1109 | * all of the checks have been done: we know | |
1110 | * that the association is unique, and the | |
1111 | * local address is always set here. | |
1112 | */ | |
1113 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) | |
1114 | inp->in6p_laddr = ip6->ip6_dst; | |
1115 | inp->in6p_faddr = ip6->ip6_src; | |
1116 | inp->inp_fport = th->th_sport; | |
1117 | ||
1118 | /* TODO: flowinfo initialization */ | |
1119 | ||
1120 | in_pcbrehash(inp); | |
1121 | #else | |
1122 | MALLOC(sin6, struct sockaddr_in6 *, sizeof *sin6, | |
1123 | M_SONAME, M_NOWAIT); | |
1124 | if (sin6 == NULL) | |
1125 | goto drop; | |
1126 | bzero(sin6, sizeof(*sin6)); | |
1127 | sin6->sin6_family = AF_INET6; | |
1128 | sin6->sin6_len = sizeof(*sin6); | |
1129 | sin6->sin6_addr = ip6->ip6_src; | |
1130 | sin6->sin6_port = th->th_sport; | |
1131 | laddr6 = inp->in6p_laddr; | |
1132 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr)) | |
1133 | inp->in6p_laddr = ip6->ip6_dst; | |
1134 | if (in6_pcbconnect(inp, (struct sockaddr *)sin6, | |
1135 | &proc0)) { | |
1136 | inp->in6p_laddr = laddr6; | |
1137 | FREE(sin6, M_SONAME); | |
1138 | goto drop; | |
1139 | } | |
1140 | FREE(sin6, M_SONAME); | |
1141 | #endif | |
1142 | } | |
1143 | else { | |
1144 | #endif /* INET6 */ | |
1145 | /* | |
1146 | * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN | |
1147 | * in_broadcast() should never return true on a received | |
1148 | * packet with M_BCAST not set. | |
1149 | */ | |
1150 | if (m->m_flags & (M_BCAST|M_MCAST) || | |
1151 | IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) | |
1152 | goto drop; | |
1153 | MALLOC(sin, struct sockaddr_in *, sizeof *sin, M_SONAME, | |
1154 | M_NOWAIT); | |
1155 | if (sin == NULL) | |
1156 | goto drop; | |
1157 | sin->sin_family = AF_INET; | |
1158 | sin->sin_len = sizeof(*sin); | |
1159 | sin->sin_addr = ip->ip_src; | |
1160 | sin->sin_port = th->th_sport; | |
1161 | bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero)); | |
1162 | laddr = inp->inp_laddr; | |
1163 | if (inp->inp_laddr.s_addr == INADDR_ANY) | |
1164 | inp->inp_laddr = ip->ip_dst; | |
1165 | if (in_pcbconnect(inp, (struct sockaddr *)sin, &proc0)) { | |
1166 | inp->inp_laddr = laddr; | |
1167 | FREE(sin, M_SONAME); | |
1168 | goto drop; | |
1169 | } | |
1170 | FREE(sin, M_SONAME); | |
1171 | #if INET6 | |
1172 | } | |
1173 | #endif /* INET6 */ | |
1174 | ||
1175 | tp->t_template = tcp_template(tp); | |
1176 | if (tp->t_template == 0) { | |
1177 | tp = tcp_drop(tp, ENOBUFS); | |
1178 | dropsocket = 0; /* socket is already gone */ | |
1179 | goto drop; | |
1180 | } | |
1181 | if ((taop = tcp_gettaocache(inp)) == NULL) { | |
1182 | taop = &tao_noncached; | |
1183 | bzero(taop, sizeof(*taop)); | |
1184 | } | |
1185 | tcp_dooptions(tp, optp, optlen, th, &to); | |
1186 | if (th->th_flags & TH_SYN) | |
1187 | tcp_mss(tp, to.to_maxseg, isipv6); /* sets t_maxseg */ | |
1188 | if (iss) | |
1189 | tp->iss = iss; | |
1190 | else | |
1191 | tp->iss = tcp_iss; | |
1192 | tcp_iss += TCP_ISSINCR/4; | |
1193 | tp->irs = th->th_seq; | |
1194 | tcp_sendseqinit(tp); | |
1195 | tcp_rcvseqinit(tp); | |
1196 | /* | |
1197 | * Initialization of the tcpcb for transaction; | |
1198 | * set SND.WND = SEG.WND, | |
1199 | * initialize CCsend and CCrecv. | |
1200 | */ | |
1201 | tp->snd_wnd = tiwin; /* initial send-window */ | |
1202 | tp->cc_send = CC_INC(tcp_ccgen); | |
1203 | tp->cc_recv = to.to_cc; | |
1204 | /* | |
1205 | * Perform TAO test on incoming CC (SEG.CC) option, if any. | |
1206 | * - compare SEG.CC against cached CC from the same host, | |
1207 | * if any. | |
1208 | * - if SEG.CC > chached value, SYN must be new and is accepted | |
1209 | * immediately: save new CC in the cache, mark the socket | |
1210 | * connected, enter ESTABLISHED state, turn on flag to | |
1211 | * send a SYN in the next segment. | |
1212 | * A virtual advertised window is set in rcv_adv to | |
1213 | * initialize SWS prevention. Then enter normal segment | |
1214 | * processing: drop SYN, process data and FIN. | |
1215 | * - otherwise do a normal 3-way handshake. | |
1216 | */ | |
1217 | if ((to.to_flag & TOF_CC) != 0) { | |
1218 | if (((tp->t_flags & TF_NOPUSH) != 0) && | |
1219 | taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) { | |
1220 | ||
1221 | taop->tao_cc = to.to_cc; | |
1222 | if (tp->t_state != TCPS_ESTABLISHED) | |
1223 | current_active_connections++; | |
1224 | ||
1225 | tp->t_state = TCPS_ESTABLISHED; | |
1226 | ||
1227 | /* | |
1228 | * If there is a FIN, or if there is data and the | |
1229 | * connection is local, then delay SYN,ACK(SYN) in | |
1230 | * the hope of piggy-backing it on a response | |
1231 | * segment. Otherwise must send ACK now in case | |
1232 | * the other side is slow starting. | |
1233 | */ | |
1234 | if (tcp_delack_enabled && | |
1235 | ((thflags & TH_FIN) || | |
1236 | (tilen != 0 && | |
1237 | #if INET6 | |
1238 | (isipv6 && in6_localaddr(&inp->in6p_faddr)) | |
1239 | || | |
1240 | (!isipv6 && | |
1241 | #endif /* INET6 */ | |
1242 | in_localaddr(inp->inp_faddr) | |
1243 | #if INET6 | |
1244 | ) | |
1245 | #endif /* INET6 */ | |
1246 | ))) { | |
1247 | if (last_active_conn_count > DELACK_BITMASK_THRESH) | |
1248 | TCP_DELACK_BITSET(tp->t_inpcb->hash_element); | |
1249 | ||
1250 | tp->t_flags |= (TF_DELACK | TF_NEEDSYN); | |
1251 | } | |
1252 | else | |
1253 | tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN); | |
1254 | ||
1255 | /* | |
1256 | * Limit the `virtual advertised window' to TCP_MAXWIN | |
1257 | * here. Even if we requested window scaling, it will | |
1258 | * become effective only later when our SYN is acked. | |
1259 | */ | |
1260 | tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN); | |
1261 | tcpstat.tcps_connects++; | |
1262 | soisconnected(so); | |
1263 | tp->t_timer[TCPT_KEEP] = tcp_keepinit; | |
1264 | dropsocket = 0; /* committed to socket */ | |
1265 | tcpstat.tcps_accepts++; | |
1266 | goto trimthenstep6; | |
1267 | } | |
1268 | /* else do standard 3-way handshake */ | |
1269 | } else { | |
1270 | /* | |
1271 | * No CC option, but maybe CC.NEW: | |
1272 | * invalidate cached value. | |
1273 | */ | |
1274 | taop->tao_cc = 0; | |
1275 | } | |
1276 | /* | |
1277 | * TAO test failed or there was no CC option, | |
1278 | * do a standard 3-way handshake. | |
1279 | */ | |
1280 | tp->t_flags |= TF_ACKNOW; | |
1281 | tp->t_state = TCPS_SYN_RECEIVED; | |
1282 | tp->t_timer[TCPT_KEEP] = tcp_keepinit; | |
1283 | dropsocket = 0; /* committed to socket */ | |
1284 | tcpstat.tcps_accepts++; | |
1285 | goto trimthenstep6; | |
1286 | } | |
1287 | ||
1288 | /* | |
1289 | * If the state is SYN_RECEIVED: | |
1290 | * if seg contains an ACK, but not for our SYN/ACK, send a RST. | |
1291 | */ | |
1292 | case TCPS_SYN_RECEIVED: | |
1293 | if ((thflags & TH_ACK) && | |
1294 | (SEQ_LEQ(th->th_ack, tp->snd_una) || | |
1295 | SEQ_GT(th->th_ack, tp->snd_max))) | |
1296 | goto dropwithreset; | |
1297 | break; | |
1298 | ||
1299 | /* | |
1300 | * If the state is SYN_SENT: | |
1301 | * if seg contains an ACK, but not for our SYN, drop the input. | |
1302 | * if seg contains a RST, then drop the connection. | |
1303 | * if seg does not contain SYN, then drop it. | |
1304 | * Otherwise this is an acceptable SYN segment | |
1305 | * initialize tp->rcv_nxt and tp->irs | |
1306 | * if seg contains ack then advance tp->snd_una | |
1307 | * if SYN has been acked change to ESTABLISHED else SYN_RCVD state | |
1308 | * arrange for segment to be acked (eventually) | |
1309 | * continue processing rest of data/controls, beginning with URG | |
1310 | */ | |
1311 | case TCPS_SYN_SENT: | |
1312 | if ((taop = tcp_gettaocache(inp)) == NULL) { | |
1313 | taop = &tao_noncached; | |
1314 | bzero(taop, sizeof(*taop)); | |
1315 | } | |
1316 | ||
1317 | if ((thflags & TH_ACK) && | |
1318 | (SEQ_LEQ(th->th_ack, tp->iss) || | |
1319 | SEQ_GT(th->th_ack, tp->snd_max))) { | |
1320 | /* | |
1321 | * If we have a cached CCsent for the remote host, | |
1322 | * hence we haven't just crashed and restarted, | |
1323 | * do not send a RST. This may be a retransmission | |
1324 | * from the other side after our earlier ACK was lost. | |
1325 | * Our new SYN, when it arrives, will serve as the | |
1326 | * needed ACK. | |
1327 | */ | |
1328 | if (taop->tao_ccsent != 0) | |
1329 | goto drop; | |
1330 | else | |
1331 | goto dropwithreset; | |
1332 | } | |
1333 | if (thflags & TH_RST) { | |
1334 | if (thflags & TH_ACK) { | |
1335 | tp = tcp_drop(tp, ECONNREFUSED); | |
1336 | postevent(so, 0, EV_RESET); | |
1337 | } | |
1338 | goto drop; | |
1339 | } | |
1340 | if ((thflags & TH_SYN) == 0) | |
1341 | goto drop; | |
1342 | tp->snd_wnd = th->th_win; /* initial send window */ | |
1343 | tp->cc_recv = to.to_cc; /* foreign CC */ | |
1344 | ||
1345 | tp->irs = th->th_seq; | |
1346 | tcp_rcvseqinit(tp); | |
1347 | if (thflags & TH_ACK) { | |
1348 | /* | |
1349 | * Our SYN was acked. If segment contains CC.ECHO | |
1350 | * option, check it to make sure this segment really | |
1351 | * matches our SYN. If not, just drop it as old | |
1352 | * duplicate, but send an RST if we're still playing | |
1353 | * by the old rules. If no CC.ECHO option, make sure | |
1354 | * we don't get fooled into using T/TCP. | |
1355 | */ | |
1356 | if (to.to_flag & TOF_CCECHO) { | |
1357 | if (tp->cc_send != to.to_ccecho) | |
1358 | if (taop->tao_ccsent != 0) | |
1359 | goto drop; | |
1360 | else | |
1361 | goto dropwithreset; | |
1362 | } else | |
1363 | tp->t_flags &= ~TF_RCVD_CC; | |
1364 | tcpstat.tcps_connects++; | |
1365 | soisconnected(so); | |
1366 | /* Do window scaling on this connection? */ | |
1367 | if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == | |
1368 | (TF_RCVD_SCALE|TF_REQ_SCALE)) { | |
1369 | tp->snd_scale = tp->requested_s_scale; | |
1370 | tp->rcv_scale = tp->request_r_scale; | |
1371 | } | |
1372 | /* Segment is acceptable, update cache if undefined. */ | |
1373 | if (taop->tao_ccsent == 0) | |
1374 | taop->tao_ccsent = to.to_ccecho; | |
1375 | ||
1376 | tp->rcv_adv += tp->rcv_wnd; | |
1377 | tp->snd_una++; /* SYN is acked */ | |
1378 | /* | |
1379 | * If there's data, delay ACK; if there's also a FIN | |
1380 | * ACKNOW will be turned on later. | |
1381 | */ | |
1382 | if (tcp_delack_enabled && tilen != 0) { | |
1383 | if (last_active_conn_count > DELACK_BITMASK_THRESH) | |
1384 | TCP_DELACK_BITSET(tp->t_inpcb->hash_element); | |
1385 | tp->t_flags |= TF_DELACK; | |
1386 | } | |
1387 | else | |
1388 | tp->t_flags |= TF_ACKNOW; | |
1389 | /* | |
1390 | * Received <SYN,ACK> in SYN_SENT[*] state. | |
1391 | * Transitions: | |
1392 | * SYN_SENT --> ESTABLISHED | |
1393 | * SYN_SENT* --> FIN_WAIT_1 | |
1394 | */ | |
1395 | if (tp->t_flags & TF_NEEDFIN) { | |
1396 | tp->t_state = TCPS_FIN_WAIT_1; | |
1397 | tp->t_flags &= ~TF_NEEDFIN; | |
1398 | thflags &= ~TH_SYN; | |
1399 | } else { | |
1400 | if (tp->t_state != TCPS_ESTABLISHED) | |
1401 | current_active_connections++; | |
1402 | tp->t_state = TCPS_ESTABLISHED; | |
1403 | tp->t_timer[TCPT_KEEP] = tcp_keepidle; | |
1404 | } | |
1405 | } else { | |
1406 | /* | |
1407 | * Received initial SYN in SYN-SENT[*] state => simul- | |
1408 | * taneous open. If segment contains CC option and there is | |
1409 | * a cached CC, apply TAO test; if it succeeds, connection is | |
1410 | * half-synchronized. Otherwise, do 3-way handshake: | |
1411 | * SYN-SENT -> SYN-RECEIVED | |
1412 | * SYN-SENT* -> SYN-RECEIVED* | |
1413 | * If there was no CC option, clear cached CC value. | |
1414 | */ | |
1415 | tp->t_flags |= TF_ACKNOW; | |
1416 | tp->t_timer[TCPT_REXMT] = 0; | |
1417 | if (to.to_flag & TOF_CC) { | |
1418 | if (taop->tao_cc != 0 && | |
1419 | CC_GT(to.to_cc, taop->tao_cc)) { | |
1420 | /* | |
1421 | * update cache and make transition: | |
1422 | * SYN-SENT -> ESTABLISHED* | |
1423 | * SYN-SENT* -> FIN-WAIT-1* | |
1424 | */ | |
1425 | taop->tao_cc = to.to_cc; | |
1426 | if (tp->t_flags & TF_NEEDFIN) { | |
1427 | tp->t_state = TCPS_FIN_WAIT_1; | |
1428 | tp->t_flags &= ~TF_NEEDFIN; | |
1429 | } else { | |
1430 | if (tp->t_state != TCPS_ESTABLISHED) | |
1431 | current_active_connections++; | |
1432 | tp->t_state = TCPS_ESTABLISHED; | |
1433 | tp->t_timer[TCPT_KEEP] = tcp_keepidle; | |
1434 | } | |
1435 | tp->t_flags |= TF_NEEDSYN; | |
1436 | } else | |
1437 | tp->t_state = TCPS_SYN_RECEIVED; | |
1438 | } else { | |
1439 | /* CC.NEW or no option => invalidate cache */ | |
1440 | taop->tao_cc = 0; | |
1441 | tp->t_state = TCPS_SYN_RECEIVED; | |
1442 | } | |
1443 | } | |
1444 | ||
1445 | trimthenstep6: | |
1446 | /* | |
1447 | * Advance th->th_seq to correspond to first data byte. | |
1448 | * If data, trim to stay within window, | |
1449 | * dropping FIN if necessary. | |
1450 | */ | |
1451 | th->th_seq++; | |
1452 | if (tilen > tp->rcv_wnd) { | |
1453 | todrop = tilen - tp->rcv_wnd; | |
1454 | m_adj(m, -todrop); | |
1455 | tilen = tp->rcv_wnd; | |
1456 | thflags &= ~TH_FIN; | |
1457 | tcpstat.tcps_rcvpackafterwin++; | |
1458 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
1459 | } | |
1460 | tp->snd_wl1 = th->th_seq - 1; | |
1461 | tp->rcv_up = th->th_seq; | |
1462 | /* | |
1463 | * Client side of transaction: already sent SYN and data. | |
1464 | * If the remote host used T/TCP to validate the SYN, | |
1465 | * our data will be ACK'd; if so, enter normal data segment | |
1466 | * processing in the middle of step 5, ack processing. | |
1467 | * Otherwise, goto step 6. | |
1468 | */ | |
1469 | if (thflags & TH_ACK) | |
1470 | goto process_ACK; | |
1471 | goto step6; | |
1472 | /* | |
1473 | * If the state is LAST_ACK or CLOSING or TIME_WAIT: | |
1474 | * if segment contains a SYN and CC [not CC.NEW] option: | |
1475 | * if state == TIME_WAIT and connection duration > MSL, | |
1476 | * drop packet and send RST; | |
1477 | * | |
1478 | * if SEG.CC > CCrecv then is new SYN, and can implicitly | |
1479 | * ack the FIN (and data) in retransmission queue. | |
1480 | * Complete close and delete TCPCB. Then reprocess | |
1481 | * segment, hoping to find new TCPCB in LISTEN state; | |
1482 | * | |
1483 | * else must be old SYN; drop it. | |
1484 | * else do normal processing. | |
1485 | */ | |
1486 | case TCPS_LAST_ACK: | |
1487 | case TCPS_CLOSING: | |
1488 | case TCPS_TIME_WAIT: | |
1489 | if ((thflags & TH_SYN) && | |
1490 | (to.to_flag & TOF_CC) && tp->cc_recv != 0) { | |
1491 | if (tp->t_state == TCPS_TIME_WAIT && | |
1492 | tp->t_duration > TCPTV_MSL) | |
1493 | goto dropwithreset; | |
1494 | if (CC_GT(to.to_cc, tp->cc_recv)) { | |
1495 | tp = tcp_close(tp); | |
1496 | goto findpcb; | |
1497 | } | |
1498 | else | |
1499 | goto drop; | |
1500 | } | |
1501 | break; /* continue normal processing */ | |
1502 | } | |
1503 | ||
1504 | /* | |
1505 | * States other than LISTEN or SYN_SENT. | |
1506 | * First check the RST flag and sequence number since reset segments | |
1507 | * are exempt from the timestamp and connection count tests. This | |
1508 | * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix | |
1509 | * below which allowed reset segments in half the sequence space | |
1510 | * to fall though and be processed (which gives forged reset | |
1511 | * segments with a random sequence number a 50 percent chance of | |
1512 | * killing a connection). | |
1513 | * Then check timestamp, if present. | |
1514 | * Then check the connection count, if present. | |
1515 | * Then check that at least some bytes of segment are within | |
1516 | * receive window. If segment begins before rcv_nxt, | |
1517 | * drop leading data (and SYN); if nothing left, just ack. | |
1518 | * | |
1519 | * | |
1520 | * If the RST bit is set, check the sequence number to see | |
1521 | * if this is a valid reset segment. | |
1522 | * RFC 793 page 37: | |
1523 | * In all states except SYN-SENT, all reset (RST) segments | |
1524 | * are validated by checking their SEQ-fields. A reset is | |
1525 | * valid if its sequence number is in the window. | |
1526 | * Note: this does not take into account delayed ACKs, so | |
1527 | * we should test against last_ack_sent instead of rcv_nxt. | |
1528 | * Also, it does not make sense to allow reset segments with | |
1529 | * sequence numbers greater than last_ack_sent to be processed | |
1530 | * since these sequence numbers are just the acknowledgement | |
1531 | * numbers in our outgoing packets being echoed back at us, | |
1532 | * and these acknowledgement numbers are monotonically | |
1533 | * increasing. | |
1534 | * If we have multiple segments in flight, the intial reset | |
1535 | * segment sequence numbers will be to the left of last_ack_sent, | |
1536 | * but they will eventually catch up. | |
1537 | * In any case, it never made sense to trim reset segments to | |
1538 | * fit the receive window since RFC 1122 says: | |
1539 | * 4.2.2.12 RST Segment: RFC-793 Section 3.4 | |
1540 | * | |
1541 | * A TCP SHOULD allow a received RST segment to include data. | |
1542 | * | |
1543 | * DISCUSSION | |
1544 | * It has been suggested that a RST segment could contain | |
1545 | * ASCII text that encoded and explained the cause of the | |
1546 | * RST. No standard has yet been established for such | |
1547 | * data. | |
1548 | * | |
1549 | * If the reset segment passes the sequence number test examine | |
1550 | * the state: | |
1551 | * SYN_RECEIVED STATE: | |
1552 | * If passive open, return to LISTEN state. | |
1553 | * If active open, inform user that connection was refused. | |
1554 | * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES: | |
1555 | * Inform user that connection was reset, and close tcb. | |
1556 | * CLOSING, LAST_ACK, TIME_WAIT STATES | |
1557 | * Close the tcb. | |
1558 | * TIME_WAIT state: | |
1559 | * Drop the segment - see Stevens, vol. 2, p. 964 and | |
1560 | * RFC 1337. | |
1561 | */ | |
1562 | if (thflags & TH_RST) { | |
1563 | if (tp->last_ack_sent == th->th_seq) { | |
1564 | switch (tp->t_state) { | |
1565 | ||
1566 | case TCPS_SYN_RECEIVED: | |
1567 | so->so_error = ECONNREFUSED; | |
1568 | goto close; | |
1569 | ||
1570 | case TCPS_ESTABLISHED: | |
1571 | case TCPS_FIN_WAIT_1: | |
1572 | case TCPS_CLOSE_WAIT: | |
1573 | current_active_connections--; | |
1574 | /* | |
1575 | Drop through ... | |
1576 | */ | |
1577 | case TCPS_FIN_WAIT_2: | |
1578 | so->so_error = ECONNRESET; | |
1579 | close: | |
1580 | postevent(so, 0, EV_RESET); | |
1581 | tp->t_state = TCPS_CLOSED; | |
1582 | tcpstat.tcps_drops++; | |
1583 | tp = tcp_close(tp); | |
1584 | break; | |
1585 | ||
1586 | case TCPS_CLOSING: | |
1587 | case TCPS_LAST_ACK: | |
1588 | current_active_connections--; | |
1589 | tp = tcp_close(tp); | |
1590 | break; | |
1591 | ||
1592 | case TCPS_TIME_WAIT: | |
1593 | break; | |
1594 | } | |
1595 | } | |
1596 | goto drop; | |
1597 | } | |
1598 | ||
1599 | /* | |
1600 | * RFC 1323 PAWS: If we have a timestamp reply on this segment | |
1601 | * and it's less than ts_recent, drop it. | |
1602 | */ | |
1603 | if ((to.to_flag & TOF_TS) != 0 && tp->ts_recent && | |
1604 | TSTMP_LT(to.to_tsval, tp->ts_recent)) { | |
1605 | ||
1606 | /* Check to see if ts_recent is over 24 days old. */ | |
1607 | if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) { | |
1608 | /* | |
1609 | * Invalidate ts_recent. If this segment updates | |
1610 | * ts_recent, the age will be reset later and ts_recent | |
1611 | * will get a valid value. If it does not, setting | |
1612 | * ts_recent to zero will at least satisfy the | |
1613 | * requirement that zero be placed in the timestamp | |
1614 | * echo reply when ts_recent isn't valid. The | |
1615 | * age isn't reset until we get a valid ts_recent | |
1616 | * because we don't want out-of-order segments to be | |
1617 | * dropped when ts_recent is old. | |
1618 | */ | |
1619 | tp->ts_recent = 0; | |
1620 | } else { | |
1621 | tcpstat.tcps_rcvduppack++; | |
1622 | tcpstat.tcps_rcvdupbyte += tilen; | |
1623 | tcpstat.tcps_pawsdrop++; | |
1624 | goto dropafterack; | |
1625 | } | |
1626 | } | |
1627 | ||
1628 | /* | |
1629 | * T/TCP mechanism | |
1630 | * If T/TCP was negotiated and the segment doesn't have CC, | |
1631 | * or if its CC is wrong then drop the segment. | |
1632 | * RST segments do not have to comply with this. | |
1633 | */ | |
1634 | if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) && | |
1635 | ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc)) | |
1636 | goto dropafterack; | |
1637 | ||
1638 | /* | |
1639 | * In the SYN-RECEIVED state, validate that the packet belongs to | |
1640 | * this connection before trimming the data to fit the receive | |
1641 | * window. Check the sequence number versus IRS since we know | |
1642 | * the sequence numbers haven't wrapped. This is a partial fix | |
1643 | * for the "LAND" DoS attack. | |
1644 | */ | |
1645 | if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) | |
1646 | goto dropwithreset; | |
1647 | ||
1648 | todrop = tp->rcv_nxt - th->th_seq; | |
1649 | if (todrop > 0) { | |
1650 | if (thflags & TH_SYN) { | |
1651 | thflags &= ~TH_SYN; | |
1652 | th->th_seq++; | |
1653 | if (th->th_urp > 1) | |
1654 | th->th_urp--; | |
1655 | else | |
1656 | thflags &= ~TH_URG; | |
1657 | todrop--; | |
1658 | } | |
1659 | /* | |
1660 | * Following if statement from Stevens, vol. 2, p. 960. | |
1661 | */ | |
1662 | if (todrop > tilen | |
1663 | || (todrop == tilen && (thflags & TH_FIN) == 0)) { | |
1664 | /* | |
1665 | * Any valid FIN must be to the left of the window. | |
1666 | * At this point the FIN must be a duplicate or out | |
1667 | * of sequence; drop it. | |
1668 | */ | |
1669 | thflags &= ~TH_FIN; | |
1670 | ||
1671 | /* | |
1672 | * Send an ACK to resynchronize and drop any data. | |
1673 | * But keep on processing for RST or ACK. | |
1674 | */ | |
1675 | tp->t_flags |= TF_ACKNOW; | |
1676 | todrop = tilen; | |
1677 | tcpstat.tcps_rcvduppack++; | |
1678 | tcpstat.tcps_rcvdupbyte += todrop; | |
1679 | } else { | |
1680 | tcpstat.tcps_rcvpartduppack++; | |
1681 | tcpstat.tcps_rcvpartdupbyte += todrop; | |
1682 | } | |
1683 | m_adj(m, todrop); | |
1684 | th->th_seq += todrop; | |
1685 | tilen -= todrop; | |
1686 | if (th->th_urp > todrop) | |
1687 | th->th_urp -= todrop; | |
1688 | else { | |
1689 | thflags &= ~TH_URG; | |
1690 | th->th_urp = 0; | |
1691 | } | |
1692 | } | |
1693 | ||
1694 | /* | |
1695 | * If new data are received on a connection after the | |
1696 | * user processes are gone, then RST the other end. | |
1697 | */ | |
1698 | if ((so->so_state & SS_NOFDREF) && | |
1699 | tp->t_state > TCPS_CLOSE_WAIT && tilen) { | |
1700 | tp = tcp_close(tp); | |
1701 | tcpstat.tcps_rcvafterclose++; | |
1702 | goto dropwithreset; | |
1703 | } | |
1704 | ||
1705 | /* | |
1706 | * If segment ends after window, drop trailing data | |
1707 | * (and PUSH and FIN); if nothing left, just ACK. | |
1708 | */ | |
1709 | todrop = (th->th_seq+tilen) - (tp->rcv_nxt+tp->rcv_wnd); | |
1710 | if (todrop > 0) { | |
1711 | tcpstat.tcps_rcvpackafterwin++; | |
1712 | if (todrop >= tilen) { | |
1713 | tcpstat.tcps_rcvbyteafterwin += tilen; | |
1714 | /* | |
1715 | * If a new connection request is received | |
1716 | * while in TIME_WAIT, drop the old connection | |
1717 | * and start over if the sequence numbers | |
1718 | * are above the previous ones. | |
1719 | */ | |
1720 | if (thflags & TH_SYN && | |
1721 | tp->t_state == TCPS_TIME_WAIT && | |
1722 | SEQ_GT(th->th_seq, tp->rcv_nxt)) { | |
1723 | iss = tp->rcv_nxt + TCP_ISSINCR; | |
1724 | tp = tcp_close(tp); | |
1725 | goto findpcb; | |
1726 | } | |
1727 | /* | |
1728 | * If window is closed can only take segments at | |
1729 | * window edge, and have to drop data and PUSH from | |
1730 | * incoming segments. Continue processing, but | |
1731 | * remember to ack. Otherwise, drop segment | |
1732 | * and ack. | |
1733 | */ | |
1734 | if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) { | |
1735 | tp->t_flags |= TF_ACKNOW; | |
1736 | tcpstat.tcps_rcvwinprobe++; | |
1737 | } else | |
1738 | goto dropafterack; | |
1739 | } else | |
1740 | tcpstat.tcps_rcvbyteafterwin += todrop; | |
1741 | m_adj(m, -todrop); | |
1742 | tilen -= todrop; | |
1743 | thflags &= ~(TH_PUSH|TH_FIN); | |
1744 | } | |
1745 | ||
1746 | /* | |
1747 | * If last ACK falls within this segment's sequence numbers, | |
1748 | * record its timestamp. | |
1749 | * NOTE that the test is modified according to the latest | |
1750 | * proposal of the tcplw@cray.com list (Braden 1993/04/26). | |
1751 | */ | |
1752 | if ((to.to_flag & TOF_TS) != 0 && | |
1753 | SEQ_LEQ(th->th_seq, tp->last_ack_sent)) { | |
1754 | tp->ts_recent_age = tcp_now; | |
1755 | tp->ts_recent = to.to_tsval; | |
1756 | } | |
1757 | ||
1758 | /* | |
1759 | * If a SYN is in the window, then this is an | |
1760 | * error and we send an RST and drop the connection. | |
1761 | */ | |
1762 | if (thflags & TH_SYN) { | |
1763 | tp = tcp_drop(tp, ECONNRESET); | |
1764 | postevent(so, 0, EV_RESET); | |
1765 | goto dropwithreset; | |
1766 | } | |
1767 | ||
1768 | /* | |
1769 | * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN | |
1770 | * flag is on (half-synchronized state), then queue data for | |
1771 | * later processing; else drop segment and return. | |
1772 | */ | |
1773 | if ((thflags & TH_ACK) == 0) { | |
1774 | if (tp->t_state == TCPS_SYN_RECEIVED || | |
1775 | (tp->t_flags & TF_NEEDSYN)) | |
1776 | goto step6; | |
1777 | else | |
1778 | goto drop; | |
1779 | } | |
1780 | ||
1781 | /* | |
1782 | * Ack processing. | |
1783 | */ | |
1784 | switch (tp->t_state) { | |
1785 | ||
1786 | /* | |
1787 | * In SYN_RECEIVED state, the ack ACKs our SYN, so enter | |
1788 | * ESTABLISHED state and continue processing. | |
1789 | * The ACK was checked above. | |
1790 | */ | |
1791 | case TCPS_SYN_RECEIVED: | |
1792 | ||
1793 | tcpstat.tcps_connects++; | |
1794 | soisconnected(so); | |
1795 | current_active_connections++; | |
1796 | ||
1797 | /* Do window scaling? */ | |
1798 | if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == | |
1799 | (TF_RCVD_SCALE|TF_REQ_SCALE)) { | |
1800 | tp->snd_scale = tp->requested_s_scale; | |
1801 | tp->rcv_scale = tp->request_r_scale; | |
1802 | } | |
1803 | /* | |
1804 | * Upon successful completion of 3-way handshake, | |
1805 | * update cache.CC if it was undefined, pass any queued | |
1806 | * data to the user, and advance state appropriately. | |
1807 | */ | |
1808 | if ((taop = tcp_gettaocache(inp)) != NULL && | |
1809 | taop->tao_cc == 0) | |
1810 | taop->tao_cc = tp->cc_recv; | |
1811 | ||
1812 | /* | |
1813 | * Make transitions: | |
1814 | * SYN-RECEIVED -> ESTABLISHED | |
1815 | * SYN-RECEIVED* -> FIN-WAIT-1 | |
1816 | */ | |
1817 | if (tp->t_flags & TF_NEEDFIN) { | |
1818 | tp->t_state = TCPS_FIN_WAIT_1; | |
1819 | tp->t_flags &= ~TF_NEEDFIN; | |
1820 | } else { | |
1821 | tp->t_state = TCPS_ESTABLISHED; | |
1822 | tp->t_timer[TCPT_KEEP] = tcp_keepidle; | |
1823 | } | |
1824 | /* | |
1825 | * If segment contains data or ACK, will call tcp_reass() | |
1826 | * later; if not, do so now to pass queued data to user. | |
1827 | */ | |
1828 | if (tilen == 0 && (thflags & TH_FIN) == 0) | |
1829 | (void) tcp_reass(tp, (struct tcphdr *)0, 0, | |
1830 | (struct mbuf *)0, isipv6); | |
1831 | tp->snd_wl1 = th->th_seq - 1; | |
1832 | /* fall into ... */ | |
1833 | ||
1834 | /* | |
1835 | * In ESTABLISHED state: drop duplicate ACKs; ACK out of range | |
1836 | * ACKs. If the ack is in the range | |
1837 | * tp->snd_una < th->th_ack <= tp->snd_max | |
1838 | * then advance tp->snd_una to th->th_ack and drop | |
1839 | * data from the retransmission queue. If this ACK reflects | |
1840 | * more up to date window information we update our window information. | |
1841 | */ | |
1842 | case TCPS_ESTABLISHED: | |
1843 | case TCPS_FIN_WAIT_1: | |
1844 | case TCPS_FIN_WAIT_2: | |
1845 | case TCPS_CLOSE_WAIT: | |
1846 | case TCPS_CLOSING: | |
1847 | case TCPS_LAST_ACK: | |
1848 | case TCPS_TIME_WAIT: | |
1849 | ||
1850 | if (SEQ_LEQ(th->th_ack, tp->snd_una)) { | |
1851 | if (tilen == 0 && tiwin == tp->snd_wnd) { | |
1852 | tcpstat.tcps_rcvdupack++; | |
1853 | /* | |
1854 | * If we have outstanding data (other than | |
1855 | * a window probe), this is a completely | |
1856 | * duplicate ack (ie, window info didn't | |
1857 | * change), the ack is the biggest we've | |
1858 | * seen and we've seen exactly our rexmt | |
1859 | * threshhold of them, assume a packet | |
1860 | * has been dropped and retransmit it. | |
1861 | * Kludge snd_nxt & the congestion | |
1862 | * window so we send only this one | |
1863 | * packet. | |
1864 | * | |
1865 | * We know we're losing at the current | |
1866 | * window size so do congestion avoidance | |
1867 | * (set ssthresh to half the current window | |
1868 | * and pull our congestion window back to | |
1869 | * the new ssthresh). | |
1870 | * | |
1871 | * Dup acks mean that packets have left the | |
1872 | * network (they're now cached at the receiver) | |
1873 | * so bump cwnd by the amount in the receiver | |
1874 | * to keep a constant cwnd packets in the | |
1875 | * network. | |
1876 | */ | |
1877 | if (tp->t_timer[TCPT_REXMT] == 0 || | |
1878 | th->th_ack != tp->snd_una) | |
1879 | tp->t_dupacks = 0; | |
1880 | else if (++tp->t_dupacks == tcprexmtthresh) { | |
1881 | tcp_seq onxt = tp->snd_nxt; | |
1882 | u_int win = | |
1883 | min(tp->snd_wnd, tp->snd_cwnd) / 2 / | |
1884 | tp->t_maxseg; | |
1885 | ||
1886 | if (win < 2) | |
1887 | win = 2; | |
1888 | tp->snd_ssthresh = win * tp->t_maxseg; | |
1889 | tp->t_timer[TCPT_REXMT] = 0; | |
1890 | tp->t_rtt = 0; | |
1891 | tp->snd_nxt = th->th_ack; | |
1892 | tp->snd_cwnd = tp->t_maxseg; | |
1893 | (void) tcp_output(tp); | |
1894 | tp->snd_cwnd = tp->snd_ssthresh + | |
1895 | tp->t_maxseg * tp->t_dupacks; | |
1896 | if (SEQ_GT(onxt, tp->snd_nxt)) | |
1897 | tp->snd_nxt = onxt; | |
1898 | goto drop; | |
1899 | } else if (tp->t_dupacks > tcprexmtthresh) { | |
1900 | tp->snd_cwnd += tp->t_maxseg; | |
1901 | (void) tcp_output(tp); | |
1902 | goto drop; | |
1903 | } | |
1904 | } else | |
1905 | tp->t_dupacks = 0; | |
1906 | break; | |
1907 | } | |
1908 | /* | |
1909 | * If the congestion window was inflated to account | |
1910 | * for the other side's cached packets, retract it. | |
1911 | */ | |
1912 | if (tp->t_dupacks >= tcprexmtthresh && | |
1913 | tp->snd_cwnd > tp->snd_ssthresh) | |
1914 | tp->snd_cwnd = tp->snd_ssthresh; | |
1915 | tp->t_dupacks = 0; | |
1916 | if (SEQ_GT(th->th_ack, tp->snd_max)) { | |
1917 | tcpstat.tcps_rcvacktoomuch++; | |
1918 | goto dropafterack; | |
1919 | } | |
1920 | /* | |
1921 | * If we reach this point, ACK is not a duplicate, | |
1922 | * i.e., it ACKs something we sent. | |
1923 | */ | |
1924 | if (tp->t_flags & TF_NEEDSYN) { | |
1925 | /* | |
1926 | * T/TCP: Connection was half-synchronized, and our | |
1927 | * SYN has been ACK'd (so connection is now fully | |
1928 | * synchronized). Go to non-starred state, | |
1929 | * increment snd_una for ACK of SYN, and check if | |
1930 | * we can do window scaling. | |
1931 | */ | |
1932 | tp->t_flags &= ~TF_NEEDSYN; | |
1933 | tp->snd_una++; | |
1934 | /* Do window scaling? */ | |
1935 | if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) == | |
1936 | (TF_RCVD_SCALE|TF_REQ_SCALE)) { | |
1937 | tp->snd_scale = tp->requested_s_scale; | |
1938 | tp->rcv_scale = tp->request_r_scale; | |
1939 | } | |
1940 | } | |
1941 | ||
1942 | process_ACK: | |
1943 | acked = th->th_ack - tp->snd_una; | |
1944 | tcpstat.tcps_rcvackpack++; | |
1945 | tcpstat.tcps_rcvackbyte += acked; | |
1946 | ||
1947 | /* | |
1948 | * If we have a timestamp reply, update smoothed | |
1949 | * round trip time. If no timestamp is present but | |
1950 | * transmit timer is running and timed sequence | |
1951 | * number was acked, update smoothed round trip time. | |
1952 | * Since we now have an rtt measurement, cancel the | |
1953 | * timer backoff (cf., Phil Karn's retransmit alg.). | |
1954 | * Recompute the initial retransmit timer. | |
1955 | */ | |
1956 | if (to.to_flag & TOF_TS) | |
1957 | tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1); | |
1958 | else if (tp->t_rtt && SEQ_GT(th->th_ack, tp->t_rtseq)) | |
1959 | tcp_xmit_timer(tp,tp->t_rtt); | |
1960 | ||
1961 | /* | |
1962 | * If all outstanding data is acked, stop retransmit | |
1963 | * timer and remember to restart (more output or persist). | |
1964 | * If there is more data to be acked, restart retransmit | |
1965 | * timer, using current (possibly backed-off) value. | |
1966 | */ | |
1967 | if (th->th_ack == tp->snd_max) { | |
1968 | tp->t_timer[TCPT_REXMT] = 0; | |
1969 | needoutput = 1; | |
1970 | } else if (tp->t_timer[TCPT_PERSIST] == 0) | |
1971 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1972 | ||
1973 | /* | |
1974 | * If no data (only SYN) was ACK'd, | |
1975 | * skip rest of ACK processing. | |
1976 | */ | |
1977 | if (acked == 0) | |
1978 | goto step6; | |
1979 | ||
1980 | /* | |
1981 | * When new data is acked, open the congestion window. | |
1982 | * If the window gives us less than ssthresh packets | |
1983 | * in flight, open exponentially (maxseg per packet). | |
1984 | * Otherwise open linearly: maxseg per window | |
1985 | * (maxseg^2 / cwnd per packet). | |
1986 | */ | |
1987 | { | |
1988 | register u_int cw = tp->snd_cwnd; | |
1989 | register u_int incr = tp->t_maxseg; | |
1990 | ||
1991 | if (cw > tp->snd_ssthresh) | |
1992 | incr = incr * incr / cw; | |
1993 | tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale); | |
1994 | } | |
1995 | if (acked > so->so_snd.sb_cc) { | |
1996 | tp->snd_wnd -= so->so_snd.sb_cc; | |
1997 | sbdrop(&so->so_snd, (int)so->so_snd.sb_cc); | |
1998 | ourfinisacked = 1; | |
1999 | } else { | |
2000 | sbdrop(&so->so_snd, acked); | |
2001 | tp->snd_wnd -= acked; | |
2002 | ourfinisacked = 0; | |
2003 | } | |
2004 | need_sowwakeup++; | |
2005 | tp->snd_una = th->th_ack; | |
2006 | if (SEQ_LT(tp->snd_nxt, tp->snd_una)) | |
2007 | tp->snd_nxt = tp->snd_una; | |
2008 | ||
2009 | switch (tp->t_state) { | |
2010 | ||
2011 | /* | |
2012 | * In FIN_WAIT_1 STATE in addition to the processing | |
2013 | * for the ESTABLISHED state if our FIN is now acknowledged | |
2014 | * then enter FIN_WAIT_2. | |
2015 | */ | |
2016 | case TCPS_FIN_WAIT_1: | |
2017 | if (ourfinisacked) { | |
2018 | /* | |
2019 | * If we can't receive any more | |
2020 | * data, then closing user can proceed. | |
2021 | * Starting the timer is contrary to the | |
2022 | * specification, but if we don't get a FIN | |
2023 | * we'll hang forever. | |
2024 | */ | |
2025 | if (so->so_state & SS_CANTRCVMORE) { | |
2026 | soisdisconnected(so); | |
2027 | tp->t_timer[TCPT_2MSL] = tcp_maxidle; | |
2028 | } | |
2029 | add_to_time_wait(tp); | |
2030 | current_active_connections--; | |
2031 | tp->t_state = TCPS_FIN_WAIT_2; | |
2032 | } | |
2033 | break; | |
2034 | ||
2035 | /* | |
2036 | * In CLOSING STATE in addition to the processing for | |
2037 | * the ESTABLISHED state if the ACK acknowledges our FIN | |
2038 | * then enter the TIME-WAIT state, otherwise ignore | |
2039 | * the segment. | |
2040 | */ | |
2041 | case TCPS_CLOSING: | |
2042 | if (ourfinisacked) { | |
2043 | tp->t_state = TCPS_TIME_WAIT; | |
2044 | tcp_canceltimers(tp); | |
2045 | /* Shorten TIME_WAIT [RFC-1644, p.28] */ | |
2046 | if (tp->cc_recv != 0 && | |
2047 | tp->t_duration < TCPTV_MSL) | |
2048 | tp->t_timer[TCPT_2MSL] = | |
2049 | tp->t_rxtcur * TCPTV_TWTRUNC; | |
2050 | else | |
2051 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
2052 | add_to_time_wait(tp); | |
2053 | current_active_connections--; | |
2054 | soisdisconnected(so); | |
2055 | } | |
2056 | break; | |
2057 | ||
2058 | /* | |
2059 | * In LAST_ACK, we may still be waiting for data to drain | |
2060 | * and/or to be acked, as well as for the ack of our FIN. | |
2061 | * If our FIN is now acknowledged, delete the TCB, | |
2062 | * enter the closed state and return. | |
2063 | */ | |
2064 | case TCPS_LAST_ACK: | |
2065 | if (ourfinisacked) { | |
2066 | tp = tcp_close(tp); | |
2067 | goto drop; | |
2068 | } | |
2069 | break; | |
2070 | ||
2071 | /* | |
2072 | * In TIME_WAIT state the only thing that should arrive | |
2073 | * is a retransmission of the remote FIN. Acknowledge | |
2074 | * it and restart the finack timer. | |
2075 | */ | |
2076 | case TCPS_TIME_WAIT: | |
2077 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
2078 | add_to_time_wait(tp); | |
2079 | goto dropafterack; | |
2080 | } | |
2081 | } | |
2082 | ||
2083 | step6: | |
2084 | /* | |
2085 | * Update window information. | |
2086 | * Don't look at window if no ACK: TAC's send garbage on first SYN. | |
2087 | */ | |
2088 | if ((thflags & TH_ACK) && | |
2089 | (SEQ_LT(tp->snd_wl1, th->th_seq) || | |
2090 | (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) || | |
2091 | (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) { | |
2092 | /* keep track of pure window updates */ | |
2093 | if (tilen == 0 && | |
2094 | tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd) | |
2095 | tcpstat.tcps_rcvwinupd++; | |
2096 | tp->snd_wnd = tiwin; | |
2097 | tp->snd_wl1 = th->th_seq; | |
2098 | tp->snd_wl2 = th->th_ack; | |
2099 | if (tp->snd_wnd > tp->max_sndwnd) | |
2100 | tp->max_sndwnd = tp->snd_wnd; | |
2101 | needoutput = 1; | |
2102 | } | |
2103 | ||
2104 | /* | |
2105 | * Process segments with URG. | |
2106 | */ | |
2107 | if ((thflags & TH_URG) && th->th_urp && | |
2108 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
2109 | /* | |
2110 | * This is a kludge, but if we receive and accept | |
2111 | * random urgent pointers, we'll crash in | |
2112 | * soreceive. It's hard to imagine someone | |
2113 | * actually wanting to send this much urgent data. | |
2114 | */ | |
2115 | if (th->th_urp + so->so_rcv.sb_cc > sb_max) { | |
2116 | th->th_urp = 0; /* XXX */ | |
2117 | thflags &= ~TH_URG; /* XXX */ | |
2118 | goto dodata; /* XXX */ | |
2119 | } | |
2120 | /* | |
2121 | * If this segment advances the known urgent pointer, | |
2122 | * then mark the data stream. This should not happen | |
2123 | * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since | |
2124 | * a FIN has been received from the remote side. | |
2125 | * In these states we ignore the URG. | |
2126 | * | |
2127 | * According to RFC961 (Assigned Protocols), | |
2128 | * the urgent pointer points to the last octet | |
2129 | * of urgent data. We continue, however, | |
2130 | * to consider it to indicate the first octet | |
2131 | * of data past the urgent section as the original | |
2132 | * spec states (in one of two places). | |
2133 | */ | |
2134 | if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) { | |
2135 | tp->rcv_up = th->th_seq + th->th_urp; | |
2136 | so->so_oobmark = so->so_rcv.sb_cc + | |
2137 | (tp->rcv_up - tp->rcv_nxt) - 1; | |
2138 | if (so->so_oobmark == 0) { | |
2139 | so->so_state |= SS_RCVATMARK; | |
2140 | postevent(so, 0, EV_OOB); | |
2141 | } | |
2142 | sohasoutofband(so); | |
2143 | tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA); | |
2144 | } | |
2145 | /* | |
2146 | * Remove out of band data so doesn't get presented to user. | |
2147 | * This can happen independent of advancing the URG pointer, | |
2148 | * but if two URG's are pending at once, some out-of-band | |
2149 | * data may creep in... ick. | |
2150 | */ | |
2151 | if (th->th_urp <= (u_long)tilen | |
2152 | #if SO_OOBINLINE | |
2153 | && (so->so_options & SO_OOBINLINE) == 0 | |
2154 | #endif | |
2155 | ) | |
2156 | tcp_pulloutofband(so, th, m); | |
2157 | } else | |
2158 | /* | |
2159 | * If no out of band data is expected, | |
2160 | * pull receive urgent pointer along | |
2161 | * with the receive window. | |
2162 | */ | |
2163 | if (SEQ_GT(tp->rcv_nxt, tp->rcv_up)) | |
2164 | tp->rcv_up = tp->rcv_nxt; | |
2165 | dodata: /* XXX */ | |
2166 | ||
2167 | /* | |
2168 | * Process the segment text, merging it into the TCP sequencing queue, | |
2169 | * and arranging for acknowledgment of receipt if necessary. | |
2170 | * This process logically involves adjusting tp->rcv_wnd as data | |
2171 | * is presented to the user (this happens in tcp_usrreq.c, | |
2172 | * case PRU_RCVD). If a FIN has already been received on this | |
2173 | * connection then we just ignore the text. | |
2174 | */ | |
2175 | if ((tilen || (thflags&TH_FIN)) && | |
2176 | TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
2177 | TCP_REASS(tp, th, tilen, m, so, thflags, isipv6, need_sorwakeup); | |
2178 | ||
2179 | if (tp->t_flags & TF_DELACK) | |
2180 | { | |
2181 | KERNEL_DEBUG(DBG_LAYER_END, ((th->th_dport << 16) | th->th_sport), | |
2182 | (((th->th_src.s_addr & 0xffff) << 16) | (th->th_dst.s_addr & 0xffff)), | |
2183 | th->th_seq, th->th_ack, th->th_win); | |
2184 | } | |
2185 | /* | |
2186 | * Note the amount of data that peer has sent into | |
2187 | * our window, in order to estimate the sender's | |
2188 | * buffer size. | |
2189 | */ | |
2190 | len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt); | |
2191 | } else { | |
2192 | m_freem(m); | |
2193 | thflags &= ~TH_FIN; | |
2194 | } | |
2195 | ||
2196 | /* | |
2197 | * If FIN is received ACK the FIN and let the user know | |
2198 | * that the connection is closing. | |
2199 | */ | |
2200 | if (thflags & TH_FIN) { | |
2201 | if (TCPS_HAVERCVDFIN(tp->t_state) == 0) { | |
2202 | socantrcvmore(so); | |
2203 | postevent(so, 0, EV_FIN); | |
2204 | /* | |
2205 | * If connection is half-synchronized | |
2206 | * (ie NEEDSYN flag on) then delay ACK, | |
2207 | * so it may be piggybacked when SYN is sent. | |
2208 | * Otherwise, since we received a FIN then no | |
2209 | * more input can be expected, send ACK now. | |
2210 | */ | |
2211 | if (tcp_delack_enabled && (tp->t_flags & TF_NEEDSYN)) { | |
2212 | if (last_active_conn_count > DELACK_BITMASK_THRESH) | |
2213 | TCP_DELACK_BITSET(tp->t_inpcb->hash_element); | |
2214 | ||
2215 | tp->t_flags |= TF_DELACK; | |
2216 | } | |
2217 | else | |
2218 | tp->t_flags |= TF_ACKNOW; | |
2219 | tp->rcv_nxt++; | |
2220 | } | |
2221 | switch (tp->t_state) { | |
2222 | ||
2223 | /* | |
2224 | * In SYN_RECEIVED and ESTABLISHED STATES | |
2225 | * enter the CLOSE_WAIT state. | |
2226 | */ | |
2227 | case TCPS_SYN_RECEIVED: | |
2228 | case TCPS_ESTABLISHED: | |
2229 | tp->t_state = TCPS_CLOSE_WAIT; | |
2230 | break; | |
2231 | ||
2232 | /* | |
2233 | * If still in FIN_WAIT_1 STATE FIN has not been acked so | |
2234 | * enter the CLOSING state. | |
2235 | */ | |
2236 | case TCPS_FIN_WAIT_1: | |
2237 | tp->t_state = TCPS_CLOSING; | |
2238 | break; | |
2239 | ||
2240 | /* | |
2241 | * In FIN_WAIT_2 state enter the TIME_WAIT state, | |
2242 | * starting the time-wait timer, turning off the other | |
2243 | * standard timers. | |
2244 | */ | |
2245 | case TCPS_FIN_WAIT_2: | |
2246 | tp->t_state = TCPS_TIME_WAIT; | |
2247 | tcp_canceltimers(tp); | |
2248 | /* Shorten TIME_WAIT [RFC-1644, p.28] */ | |
2249 | if (tp->cc_recv != 0 && | |
2250 | tp->t_duration < TCPTV_MSL) { | |
2251 | tp->t_timer[TCPT_2MSL] = | |
2252 | tp->t_rxtcur * TCPTV_TWTRUNC; | |
2253 | /* For transaction client, force ACK now. */ | |
2254 | tp->t_flags |= TF_ACKNOW; | |
2255 | } | |
2256 | else | |
2257 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
2258 | ||
2259 | add_to_time_wait(tp); | |
2260 | soisdisconnected(so); | |
2261 | break; | |
2262 | ||
2263 | /* | |
2264 | * In TIME_WAIT state restart the 2 MSL time_wait timer. | |
2265 | */ | |
2266 | case TCPS_TIME_WAIT: | |
2267 | tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL; | |
2268 | add_to_time_wait(tp); | |
2269 | break; | |
2270 | } | |
2271 | } | |
2272 | #if TCPDEBUG | |
2273 | if (so->so_options & SO_DEBUG) { | |
2274 | #if INET6 | |
2275 | if (isipv6) | |
2276 | tcp_saveip._tcp_si6.ip6_plen = tilen; | |
2277 | else | |
2278 | tcp_saveip._tcp_si4.ip_len = tilen; | |
2279 | #else /* INET6 */ | |
2280 | tcp_saveip.ip_len = tilen; | |
2281 | #endif /* INET6 */ | |
2282 | ||
2283 | tcp_trace(TA_INPUT, ostate, tp, (void *)&tcp_saveip, | |
2284 | &tcp_savetcp, 0); | |
2285 | } | |
2286 | #endif | |
2287 | ||
2288 | /* | |
2289 | * Return any desired output. | |
2290 | */ | |
2291 | if (needoutput || (tp->t_flags & TF_ACKNOW)) | |
2292 | (void) tcp_output(tp); | |
2293 | if (need_sorwakeup) | |
2294 | sorwakeup(so); | |
2295 | if (need_sowwakeup) | |
2296 | sowwakeup(so); | |
2297 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
2298 | return; | |
2299 | ||
2300 | dropafterack: | |
2301 | /* | |
2302 | * Generate an ACK dropping incoming segment if it occupies | |
2303 | * sequence space, where the ACK reflects our state. | |
2304 | * | |
2305 | * We can now skip the test for the RST flag since all | |
2306 | * paths to this code happen after packets containing | |
2307 | * RST have been dropped. | |
2308 | * | |
2309 | * In the SYN-RECEIVED state, don't send an ACK unless the | |
2310 | * segment we received passes the SYN-RECEIVED ACK test. | |
2311 | * If it fails send a RST. This breaks the loop in the | |
2312 | * "LAND" DoS attack, and also prevents an ACK storm | |
2313 | * between two listening ports that have been sent forged | |
2314 | * SYN segments, each with the source address of the other. | |
2315 | */ | |
2316 | if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) && | |
2317 | (SEQ_GT(tp->snd_una, th->th_ack) || | |
2318 | SEQ_GT(th->th_ack, tp->snd_max)) ) | |
2319 | goto dropwithreset; | |
2320 | #if TCPDEBUG | |
2321 | if (so->so_options & SO_DEBUG) { | |
2322 | #if INET6 | |
2323 | if (isipv6) | |
2324 | tcp_saveip._tcp_si6.ip6_plen = tilen; | |
2325 | else | |
2326 | tcp_saveip._tcp_si4.ip_len = tilen; | |
2327 | #else /* INET6 */ | |
2328 | tcp_saveip.ip_len = tilen; | |
2329 | #endif /* INET6 */ | |
2330 | tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip, | |
2331 | &tcp_savetcp, 0); | |
2332 | } | |
2333 | #endif | |
2334 | m_freem(m); | |
2335 | tp->t_flags |= TF_ACKNOW; | |
2336 | (void) tcp_output(tp); | |
2337 | if (need_sorwakeup) | |
2338 | sorwakeup(so); | |
2339 | if (need_sowwakeup) | |
2340 | sowwakeup(so); | |
2341 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
2342 | return; | |
2343 | ||
2344 | dropwithreset: | |
2345 | /* | |
2346 | * Generate a RST, dropping incoming segment. | |
2347 | * Make ACK acceptable to originator of segment. | |
2348 | * Don't bother to respond if destination was broadcast/multicast. | |
2349 | */ | |
2350 | if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST)) | |
2351 | goto drop; | |
2352 | #if INET6 | |
2353 | if (isipv6) { | |
2354 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) | |
2355 | goto drop; /* anycast check is done at the top */ | |
2356 | } else | |
2357 | #endif /* INET6 */ | |
2358 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) | |
2359 | goto drop; | |
2360 | #if TCPDEBUG | |
2361 | if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { | |
2362 | if (tp == 0) { | |
2363 | #if INET6 | |
2364 | if (isipv6) | |
2365 | tcp_saveip._tcp_si6 = *ip6; | |
2366 | else | |
2367 | tcp_saveip._tcp_si4 = *ip; | |
2368 | #else /* INET6 */ | |
2369 | tcp_saveip = *ip; | |
2370 | #endif /* INET6 */ | |
2371 | } | |
2372 | #if INET6 | |
2373 | if (isipv6) | |
2374 | tcp_saveip._tcp_si6.ip6_plen = tilen; | |
2375 | else | |
2376 | tcp_saveip._tcp_si4.ip_len = tilen; | |
2377 | #else /* INET6 */ | |
2378 | tcp_saveip.ip_len = tilen; | |
2379 | #endif /* INET6 */ | |
2380 | tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip, | |
2381 | &tcp_savetcp, 0); | |
2382 | } | |
2383 | #endif | |
2384 | if (thflags & TH_ACK) | |
2385 | #if INET6 | |
2386 | tcp_respond(tp, isipv6 ? (void *)ip6 : (void *)ip, th, m, | |
2387 | (tcp_seq)0, th->th_ack, TH_RST, isipv6); | |
2388 | #else /* INET6 */ | |
2389 | tcp_respond(tp, (void *)ip, th, m, | |
2390 | (tcp_seq)0, th->th_ack, TH_RST, isipv6); | |
2391 | #endif /* INET6 */ | |
2392 | else { | |
2393 | if (thflags & TH_SYN) | |
2394 | tilen++; | |
2395 | #if INET6 | |
2396 | tcp_respond(tp, isipv6 ? (void *)ip6 : (void *)ip, th, m, | |
2397 | th->th_seq+tilen, (tcp_seq)0, TH_RST|TH_ACK, | |
2398 | isipv6); | |
2399 | #else /* INET6 */ | |
2400 | tcp_respond(tp, (void *)ip, th, m, | |
2401 | th->th_seq+tilen, (tcp_seq)0, TH_RST|TH_ACK, | |
2402 | isipv6); | |
2403 | #endif /* INET6 */ | |
2404 | } | |
2405 | /* destroy temporarily created socket */ | |
2406 | if (need_sorwakeup) | |
2407 | sorwakeup(so); | |
2408 | if (need_sowwakeup) | |
2409 | sowwakeup(so); | |
2410 | if (dropsocket) | |
2411 | (void) soabort(so); | |
2412 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
2413 | return; | |
2414 | ||
2415 | drop: | |
2416 | /* | |
2417 | * Drop space held by incoming segment and return. | |
2418 | */ | |
2419 | #if TCPDEBUG | |
2420 | if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) { | |
2421 | if (tp == 0) { | |
2422 | #if INET6 | |
2423 | if (isipv6) | |
2424 | tcp_saveip._tcp_si6 = *ip6; | |
2425 | else | |
2426 | tcp_saveip._tcp_si4 = *ip; | |
2427 | #else /* INET6 */ | |
2428 | tcp_saveip = *ip; | |
2429 | #endif /* INET6 */ | |
2430 | } | |
2431 | #if INET6 | |
2432 | if (isipv6) | |
2433 | tcp_saveip._tcp_si6.ip6_plen = tilen; | |
2434 | else | |
2435 | tcp_saveip._tcp_si4.ip_len = tilen; | |
2436 | #else /* INET6 */ | |
2437 | tcp_saveip.ip_len = tilen; | |
2438 | #endif /* INET6 */ | |
2439 | tcp_trace(TA_DROP, ostate, tp, (void *)&tcp_saveip, | |
2440 | &tcp_savetcp, 0); | |
2441 | } | |
2442 | #endif | |
2443 | m_freem(m); | |
2444 | if (need_sorwakeup) | |
2445 | sorwakeup(so); | |
2446 | if (need_sowwakeup) | |
2447 | sowwakeup(so); | |
2448 | /* destroy temporarily created socket */ | |
2449 | if (dropsocket) | |
2450 | (void) soabort(so); | |
2451 | KERNEL_DEBUG(DBG_FNC_TCP_INPUT | DBG_FUNC_END,0,0,0,0,0); | |
2452 | return; | |
2453 | } | |
2454 | ||
2455 | static void | |
2456 | tcp_dooptions(tp, cp, cnt, th, to) | |
2457 | struct tcpcb *tp; | |
2458 | u_char *cp; | |
2459 | int cnt; | |
2460 | struct tcphdr *th; | |
2461 | struct tcpopt *to; | |
2462 | { | |
2463 | u_short mss = 0; | |
2464 | int opt, optlen; | |
2465 | ||
2466 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
2467 | opt = cp[0]; | |
2468 | if (opt == TCPOPT_EOL) | |
2469 | break; | |
2470 | if (opt == TCPOPT_NOP) | |
2471 | optlen = 1; | |
2472 | else { | |
2473 | optlen = cp[1]; | |
2474 | if (optlen <= 0) | |
2475 | break; | |
2476 | } | |
2477 | switch (opt) { | |
2478 | ||
2479 | default: | |
2480 | continue; | |
2481 | ||
2482 | case TCPOPT_MAXSEG: | |
2483 | if (optlen != TCPOLEN_MAXSEG) | |
2484 | continue; | |
2485 | if (!(th->th_flags & TH_SYN)) | |
2486 | continue; | |
2487 | bcopy((char *) cp + 2, (char *) &mss, sizeof(mss)); | |
2488 | to->to_maxseg = ntohs(mss); | |
2489 | break; | |
2490 | ||
2491 | case TCPOPT_WINDOW: | |
2492 | if (optlen != TCPOLEN_WINDOW) | |
2493 | continue; | |
2494 | if (!(th->th_flags & TH_SYN)) | |
2495 | continue; | |
2496 | tp->t_flags |= TF_RCVD_SCALE; | |
2497 | tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT); | |
2498 | break; | |
2499 | ||
2500 | case TCPOPT_TIMESTAMP: | |
2501 | if (optlen != TCPOLEN_TIMESTAMP) | |
2502 | continue; | |
2503 | to->to_flag |= TOF_TS; | |
2504 | bcopy((char *)cp + 2, | |
2505 | (char *)&to->to_tsval, sizeof(to->to_tsval)); | |
2506 | NTOHL(to->to_tsval); | |
2507 | bcopy((char *)cp + 6, | |
2508 | (char *)&to->to_tsecr, sizeof(to->to_tsecr)); | |
2509 | NTOHL(to->to_tsecr); | |
2510 | ||
2511 | /* | |
2512 | * A timestamp received in a SYN makes | |
2513 | * it ok to send timestamp requests and replies. | |
2514 | */ | |
2515 | if (th->th_flags & TH_SYN) { | |
2516 | tp->t_flags |= TF_RCVD_TSTMP; | |
2517 | tp->ts_recent = to->to_tsval; | |
2518 | tp->ts_recent_age = tcp_now; | |
2519 | } | |
2520 | break; | |
2521 | case TCPOPT_CC: | |
2522 | if (optlen != TCPOLEN_CC) | |
2523 | continue; | |
2524 | to->to_flag |= TOF_CC; | |
2525 | bcopy((char *)cp + 2, | |
2526 | (char *)&to->to_cc, sizeof(to->to_cc)); | |
2527 | NTOHL(to->to_cc); | |
2528 | /* | |
2529 | * A CC or CC.new option received in a SYN makes | |
2530 | * it ok to send CC in subsequent segments. | |
2531 | */ | |
2532 | if (th->th_flags & TH_SYN) | |
2533 | tp->t_flags |= TF_RCVD_CC; | |
2534 | break; | |
2535 | case TCPOPT_CCNEW: | |
2536 | if (optlen != TCPOLEN_CC) | |
2537 | continue; | |
2538 | if (!(th->th_flags & TH_SYN)) | |
2539 | continue; | |
2540 | to->to_flag |= TOF_CCNEW; | |
2541 | bcopy((char *)cp + 2, | |
2542 | (char *)&to->to_cc, sizeof(to->to_cc)); | |
2543 | NTOHL(to->to_cc); | |
2544 | /* | |
2545 | * A CC or CC.new option received in a SYN makes | |
2546 | * it ok to send CC in subsequent segments. | |
2547 | */ | |
2548 | tp->t_flags |= TF_RCVD_CC; | |
2549 | break; | |
2550 | case TCPOPT_CCECHO: | |
2551 | if (optlen != TCPOLEN_CC) | |
2552 | continue; | |
2553 | if (!(th->th_flags & TH_SYN)) | |
2554 | continue; | |
2555 | to->to_flag |= TOF_CCECHO; | |
2556 | bcopy((char *)cp + 2, | |
2557 | (char *)&to->to_ccecho, sizeof(to->to_ccecho)); | |
2558 | NTOHL(to->to_ccecho); | |
2559 | break; | |
2560 | } | |
2561 | } | |
2562 | } | |
2563 | ||
2564 | /* | |
2565 | * Pull out of band byte out of a segment so | |
2566 | * it doesn't appear in the user's data queue. | |
2567 | * It is still reflected in the segment length for | |
2568 | * sequencing purposes. | |
2569 | */ | |
2570 | static void | |
2571 | tcp_pulloutofband(so, th, m) | |
2572 | struct socket *so; | |
2573 | struct tcphdr *th; | |
2574 | register struct mbuf *m; | |
2575 | { | |
2576 | int cnt = th->th_urp - 1; | |
2577 | ||
2578 | while (cnt >= 0) { | |
2579 | if (m->m_len > cnt) { | |
2580 | char *cp = mtod(m, caddr_t) + cnt; | |
2581 | struct tcpcb *tp = sototcpcb(so); | |
2582 | ||
2583 | tp->t_iobc = *cp; | |
2584 | tp->t_oobflags |= TCPOOB_HAVEDATA; | |
2585 | bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1)); | |
2586 | m->m_len--; | |
2587 | return; | |
2588 | } | |
2589 | cnt -= m->m_len; | |
2590 | m = m->m_next; | |
2591 | if (m == 0) | |
2592 | break; | |
2593 | } | |
2594 | panic("tcp_pulloutofband"); | |
2595 | } | |
2596 | ||
2597 | /* | |
2598 | * Collect new round-trip time estimate | |
2599 | * and update averages and current timeout. | |
2600 | */ | |
2601 | static void | |
2602 | tcp_xmit_timer(tp, rtt) | |
2603 | register struct tcpcb *tp; | |
2604 | short rtt; | |
2605 | { | |
2606 | register int delta; | |
2607 | ||
2608 | tcpstat.tcps_rttupdated++; | |
2609 | tp->t_rttupdated++; | |
2610 | if (tp->t_srtt != 0) { | |
2611 | /* | |
2612 | * srtt is stored as fixed point with 5 bits after the | |
2613 | * binary point (i.e., scaled by 8). The following magic | |
2614 | * is equivalent to the smoothing algorithm in rfc793 with | |
2615 | * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed | |
2616 | * point). Adjust rtt to origin 0. | |
2617 | */ | |
2618 | delta = ((rtt - 1) << TCP_DELTA_SHIFT) | |
2619 | - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT)); | |
2620 | ||
2621 | if ((tp->t_srtt += delta) <= 0) | |
2622 | tp->t_srtt = 1; | |
2623 | ||
2624 | /* | |
2625 | * We accumulate a smoothed rtt variance (actually, a | |
2626 | * smoothed mean difference), then set the retransmit | |
2627 | * timer to smoothed rtt + 4 times the smoothed variance. | |
2628 | * rttvar is stored as fixed point with 4 bits after the | |
2629 | * binary point (scaled by 16). The following is | |
2630 | * equivalent to rfc793 smoothing with an alpha of .75 | |
2631 | * (rttvar = rttvar*3/4 + |delta| / 4). This replaces | |
2632 | * rfc793's wired-in beta. | |
2633 | */ | |
2634 | if (delta < 0) | |
2635 | delta = -delta; | |
2636 | delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT); | |
2637 | if ((tp->t_rttvar += delta) <= 0) | |
2638 | tp->t_rttvar = 1; | |
2639 | } else { | |
2640 | /* | |
2641 | * No rtt measurement yet - use the unsmoothed rtt. | |
2642 | * Set the variance to half the rtt (so our first | |
2643 | * retransmit happens at 3*rtt). | |
2644 | */ | |
2645 | tp->t_srtt = rtt << TCP_RTT_SHIFT; | |
2646 | tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1); | |
2647 | } | |
2648 | tp->t_rtt = 0; | |
2649 | tp->t_rxtshift = 0; | |
2650 | ||
2651 | /* | |
2652 | * the retransmit should happen at rtt + 4 * rttvar. | |
2653 | * Because of the way we do the smoothing, srtt and rttvar | |
2654 | * will each average +1/2 tick of bias. When we compute | |
2655 | * the retransmit timer, we want 1/2 tick of rounding and | |
2656 | * 1 extra tick because of +-1/2 tick uncertainty in the | |
2657 | * firing of the timer. The bias will give us exactly the | |
2658 | * 1.5 tick we need. But, because the bias is | |
2659 | * statistical, we have to test that we don't drop below | |
2660 | * the minimum feasible timer (which is 2 ticks). | |
2661 | */ | |
2662 | TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), | |
2663 | max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX); | |
2664 | ||
2665 | /* | |
2666 | * We received an ack for a packet that wasn't retransmitted; | |
2667 | * it is probably safe to discard any error indications we've | |
2668 | * received recently. This isn't quite right, but close enough | |
2669 | * for now (a route might have failed after we sent a segment, | |
2670 | * and the return path might not be symmetrical). | |
2671 | */ | |
2672 | tp->t_softerror = 0; | |
2673 | } | |
2674 | ||
2675 | /* | |
2676 | * Determine a reasonable value for maxseg size. | |
2677 | * If the route is known, check route for mtu. | |
2678 | * If none, use an mss that can be handled on the outgoing | |
2679 | * interface without forcing IP to fragment; if bigger than | |
2680 | * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES | |
2681 | * to utilize large mbufs. If no route is found, route has no mtu, | |
2682 | * or the destination isn't local, use a default, hopefully conservative | |
2683 | * size (usually 512 or the default IP max size, but no more than the mtu | |
2684 | * of the interface), as we can't discover anything about intervening | |
2685 | * gateways or networks. We also initialize the congestion/slow start | |
2686 | * window to be a single segment if the destination isn't local. | |
2687 | * While looking at the routing entry, we also initialize other path-dependent | |
2688 | * parameters from pre-set or cached values in the routing entry. | |
2689 | * | |
2690 | * Also take into account the space needed for options that we | |
2691 | * send regularly. Make maxseg shorter by that amount to assure | |
2692 | * that we can send maxseg amount of data even when the options | |
2693 | * are present. Store the upper limit of the length of options plus | |
2694 | * data in maxopd. | |
2695 | * | |
2696 | * NOTE that this routine is only called when we process an incoming | |
2697 | * segment, for outgoing segments only tcp_mssopt is called. | |
2698 | * | |
2699 | * In case of T/TCP, we call this routine during implicit connection | |
2700 | * setup as well (offer = -1), to initialize maxseg from the cached | |
2701 | * MSS of our peer. | |
2702 | */ | |
2703 | void | |
2704 | tcp_mss(tp, offer, isipv6) | |
2705 | struct tcpcb *tp; | |
2706 | int offer; | |
2707 | #if INET6 | |
2708 | int isipv6; | |
2709 | #endif | |
2710 | { | |
2711 | register struct rtentry *rt; | |
2712 | struct ifnet *ifp; | |
2713 | register int rtt, mss; | |
2714 | u_long bufsize; | |
2715 | struct inpcb *inp; | |
2716 | struct socket *so; | |
2717 | struct rmxp_tao *taop; | |
2718 | int origoffer = offer; | |
2719 | #if INET6 | |
2720 | int lgminh = isipv6 ? sizeof (struct tcpip6hdr) : | |
2721 | sizeof (struct tcpiphdr); | |
2722 | #else /* INET6 */ | |
2723 | #define lgminh (sizeof (struct tcpiphdr)) | |
2724 | #endif /* INET6 */ | |
2725 | ||
2726 | inp = tp->t_inpcb; | |
2727 | #if INET6 | |
2728 | if (isipv6) | |
2729 | rt = tcp_rtlookup6(inp); | |
2730 | else | |
2731 | #endif /* INET6 */ | |
2732 | rt = tcp_rtlookup(inp); | |
2733 | if (rt == NULL) { | |
2734 | tp->t_maxopd = tp->t_maxseg = | |
2735 | #if INET6 | |
2736 | isipv6 ? tcp_v6mssdflt : | |
2737 | #endif /* INET6 */ | |
2738 | tcp_mssdflt; | |
2739 | return; | |
2740 | } | |
2741 | ifp = rt->rt_ifp; | |
2742 | so = inp->inp_socket; | |
2743 | ||
2744 | taop = rmx_taop(rt->rt_rmx); | |
2745 | /* | |
2746 | * Offer == -1 means that we didn't receive SYN yet, | |
2747 | * use cached value in that case; | |
2748 | */ | |
2749 | if (offer == -1) | |
2750 | offer = taop->tao_mssopt; | |
2751 | /* | |
2752 | * Offer == 0 means that there was no MSS on the SYN segment, | |
2753 | * in this case we use tcp_mssdflt. | |
2754 | */ | |
2755 | if (offer == 0) | |
2756 | offer = | |
2757 | #if INET6 | |
2758 | isipv6 ? tcp_v6mssdflt : | |
2759 | #endif /* INET6 */ | |
2760 | tcp_mssdflt; | |
2761 | else | |
2762 | /* | |
2763 | * Sanity check: make sure that maxopd will be large | |
2764 | * enough to allow some data on segments even is the | |
2765 | * all the option space is used (40bytes). Otherwise | |
2766 | * funny things may happen in tcp_output. | |
2767 | */ | |
2768 | offer = max(offer, 64); | |
2769 | taop->tao_mssopt = offer; | |
2770 | ||
2771 | /* | |
2772 | * While we're here, check if there's an initial rtt | |
2773 | * or rttvar. Convert from the route-table units | |
2774 | * to scaled multiples of the slow timeout timer. | |
2775 | */ | |
2776 | if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) { | |
2777 | /* | |
2778 | * XXX the lock bit for RTT indicates that the value | |
2779 | * is also a minimum value; this is subject to time. | |
2780 | */ | |
2781 | if (rt->rt_rmx.rmx_locks & RTV_RTT) | |
2782 | tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ); | |
2783 | tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE)); | |
2784 | tcpstat.tcps_usedrtt++; | |
2785 | if (rt->rt_rmx.rmx_rttvar) { | |
2786 | tp->t_rttvar = rt->rt_rmx.rmx_rttvar / | |
2787 | (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE)); | |
2788 | tcpstat.tcps_usedrttvar++; | |
2789 | } else { | |
2790 | /* default variation is +- 1 rtt */ | |
2791 | tp->t_rttvar = | |
2792 | tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE; | |
2793 | } | |
2794 | TCPT_RANGESET(tp->t_rxtcur, | |
2795 | ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1, | |
2796 | tp->t_rttmin, TCPTV_REXMTMAX); | |
2797 | } | |
2798 | /* | |
2799 | * if there's an mtu associated with the route, use it | |
2800 | * else, use the link mtu. | |
2801 | */ | |
2802 | if (rt->rt_rmx.rmx_mtu) | |
2803 | mss = rt->rt_rmx.rmx_mtu - lgminh; | |
2804 | else | |
2805 | mss = | |
2806 | #if INET6 | |
2807 | isipv6 ? nd_ifinfo[rt->rt_ifp->if_index].linkmtu : | |
2808 | #endif | |
2809 | ifp->if_mtu - lgminh; | |
2810 | ||
2811 | if (rt->rt_rmx.rmx_mtu == 0) { | |
2812 | #if INET6 | |
2813 | if (isipv6) { | |
2814 | if (!in6_localaddr(&inp->in6p_faddr)) | |
2815 | mss = min(mss, tcp_v6mssdflt); | |
2816 | } else | |
2817 | #endif /* INET6 */ | |
2818 | if (!in_localaddr(inp->inp_faddr)) | |
2819 | mss = min(mss, tcp_mssdflt); | |
2820 | } | |
2821 | mss = min(mss, offer); | |
2822 | /* | |
2823 | * maxopd stores the maximum length of data AND options | |
2824 | * in a segment; maxseg is the amount of data in a normal | |
2825 | * segment. We need to store this value (maxopd) apart | |
2826 | * from maxseg, because now every segment carries options | |
2827 | * and thus we normally have somewhat less data in segments. | |
2828 | */ | |
2829 | tp->t_maxopd = mss; | |
2830 | ||
2831 | /* | |
2832 | * In case of T/TCP, origoffer==-1 indicates, that no segments | |
2833 | * were received yet. In this case we just guess, otherwise | |
2834 | * we do the same as before T/TCP. | |
2835 | */ | |
2836 | if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && | |
2837 | (origoffer == -1 || | |
2838 | (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)) | |
2839 | mss -= TCPOLEN_TSTAMP_APPA; | |
2840 | if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && | |
2841 | (origoffer == -1 || | |
2842 | (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC)) | |
2843 | mss -= TCPOLEN_CC_APPA; | |
2844 | ||
2845 | #if (MCLBYTES & (MCLBYTES - 1)) == 0 | |
2846 | if (mss > MCLBYTES) | |
2847 | mss &= ~(MCLBYTES-1); | |
2848 | #else | |
2849 | if (mss > MCLBYTES) | |
2850 | mss = mss / MCLBYTES * MCLBYTES; | |
2851 | #endif | |
2852 | /* | |
2853 | * If there's a pipesize, change the socket buffer | |
2854 | * to that size. Make the socket buffers an integral | |
2855 | * number of mss units; if the mss is larger than | |
2856 | * the socket buffer, decrease the mss. | |
2857 | */ | |
2858 | #if RTV_SPIPE | |
2859 | if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0) | |
2860 | #endif | |
2861 | bufsize = so->so_snd.sb_hiwat; | |
2862 | if (bufsize < mss) | |
2863 | mss = bufsize; | |
2864 | else { | |
2865 | bufsize = roundup(bufsize, mss); | |
2866 | if (bufsize > sb_max) | |
2867 | bufsize = sb_max; | |
2868 | (void)sbreserve(&so->so_snd, bufsize); | |
2869 | } | |
2870 | tp->t_maxseg = mss; | |
2871 | ||
2872 | #if RTV_RPIPE | |
2873 | if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0) | |
2874 | #endif | |
2875 | bufsize = so->so_rcv.sb_hiwat; | |
2876 | if (bufsize > mss) { | |
2877 | bufsize = roundup(bufsize, mss); | |
2878 | if (bufsize > sb_max) | |
2879 | bufsize = sb_max; | |
2880 | (void)sbreserve(&so->so_rcv, bufsize); | |
2881 | } | |
2882 | /* | |
2883 | * Don't force slow-start on local network. | |
2884 | */ | |
2885 | #if INET6 | |
2886 | if (isipv6) { | |
2887 | if (!in6_localaddr(&inp->in6p_faddr)) | |
2888 | tp->snd_cwnd = mss; | |
2889 | } else | |
2890 | #endif /* INET6 */ | |
2891 | if (!in_localaddr(inp->inp_faddr)) | |
2892 | tp->snd_cwnd = mss; | |
2893 | ||
2894 | if (rt->rt_rmx.rmx_ssthresh) { | |
2895 | /* | |
2896 | * There's some sort of gateway or interface | |
2897 | * buffer limit on the path. Use this to set | |
2898 | * the slow start threshhold, but set the | |
2899 | * threshold to no less than 2*mss. | |
2900 | */ | |
2901 | tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh); | |
2902 | tcpstat.tcps_usedssthresh++; | |
2903 | } | |
2904 | } | |
2905 | ||
2906 | /* | |
2907 | * Determine the MSS option to send on an outgoing SYN. | |
2908 | */ | |
2909 | int | |
2910 | tcp_mssopt(tp, isipv6) | |
2911 | struct tcpcb *tp; | |
2912 | #if INET6 | |
2913 | int isipv6; | |
2914 | #endif | |
2915 | { | |
2916 | struct rtentry *rt; | |
2917 | int mss; | |
2918 | #if INET6 | |
2919 | int lgminh = isipv6 ? sizeof (struct tcpip6hdr) : | |
2920 | sizeof (struct tcpiphdr); | |
2921 | #else /* INET6 */ | |
2922 | #define lgminh (sizeof (struct tcpiphdr)) | |
2923 | #endif /* INET6 */ | |
2924 | ||
2925 | #if INET6 | |
2926 | if (isipv6) | |
2927 | rt = tcp_rtlookup6(tp->t_inpcb); | |
2928 | else | |
2929 | #endif /* INET6 */ | |
2930 | rt = tcp_rtlookup(tp->t_inpcb); | |
2931 | if (rt == NULL) | |
2932 | return | |
2933 | #if INET6 | |
2934 | isipv6 ? tcp_v6mssdflt : | |
2935 | #endif /* INET6 */ | |
2936 | tcp_mssdflt; | |
2937 | ||
2938 | mss = rt->rt_ifp->if_mtu - lgminh; | |
2939 | ||
2940 | return mss; | |
2941 | } |