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