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