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