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