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