1 /** $MirOS: src/sys/netinet/tcp_input.c,v 1.11 2011/11/20 18:54:50 tg Exp $ */
2 /* $OpenBSD: tcp_input.c,v 1.168 2004/05/21 11:36:23 markus Exp $ */
3 /* $OpenBSD: tcp_input.c,v 1.158.2.3 2005/01/11 04:40:29 brad Exp $ */
4 /* $OpenBSD: tcp_input.c,v 1.178 2004/11/25 15:32:08 markus Exp $ */
5 /* $NetBSD: tcp_input.c,v 1.23 1996/02/13 23:43:44 christos Exp $ */
6
7 /*
8 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
9 * The Regents of the University of California. All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
36 *
37 * NRL grants permission for redistribution and use in source and binary
38 * forms, with or without modification, of the software and documentation
39 * created at NRL provided that the following conditions are met:
40 *
41 * 1. Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * 2. Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * 3. All advertising materials mentioning features or use of this software
47 * must display the following acknowledgements:
48 * This product includes software developed by the University of
49 * California, Berkeley and its contributors.
50 * This product includes software developed at the Information
51 * Technology Division, US Naval Research Laboratory.
52 * 4. Neither the name of the NRL nor the names of its contributors
53 * may be used to endorse or promote products derived from this software
54 * without specific prior written permission.
55 *
56 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
57 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
58 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
59 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
60 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
61 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
62 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
63 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
64 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
65 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
66 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
67 *
68 * The views and conclusions contained in the software and documentation
69 * are those of the authors and should not be interpreted as representing
70 * official policies, either expressed or implied, of the US Naval
71 * Research Laboratory (NRL).
72 */
73
74 /*
75 * Additional improvements
76 * copyright (c) 2005, 2006 by Marco Munari <mar.develops allerta.it>
77 *
78 * Redistribution and use in source and binary forms, with or without
79 * modification, are permitted provided that the following conditions
80 * are met:
81 * 1. Redistributions of source code must retain the above copyright
82 * notice, this list of conditions and the following disclaimer.
83 * 2. Redistributions in binary form must reproduce the above copyright
84 * notice, this list of conditions and the following disclaimer in the
85 * documentation and/or other materials provided with the distribution.
86 * 3. Neither the name of allerta nor the names of its contributors
87 * may be used to endorse or promote products derived from this software
88 * without specific prior written permission.
89 *
90 * SOURCE CODE PROVIDED ``AS IS'' WITHOUT WARRANTIES, BUT WITH CARE.
91 *
92 */
93
94 #ifndef TUBA_INCLUDE
95 #include <sys/param.h>
96 #include <sys/systm.h>
97 #include <sys/mbuf.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/kernel.h>
102
103 #include <dev/rndvar.h>
104
105 #include <net/if.h>
106 #include <net/route.h>
107
108 #include <netinet/in.h>
109 #include <netinet/in_systm.h>
110 #include <netinet/ip.h>
111 #include <netinet/in_pcb.h>
112 #include <netinet/ip_var.h>
113 #include <netinet/tcp.h>
114 #include <netinet/tcp_fsm.h>
115 #include <netinet/tcp_seq.h>
116 #include <netinet/tcp_timer.h>
117 #include <netinet/tcp_var.h>
118 #include <netinet/tcpip.h>
119 #include <netinet/tcp_debug.h>
120
121 struct tcpiphdr tcp_saveti;
122
123 #ifdef INET6
124 #include <netinet6/in6_var.h>
125 #include <netinet6/nd6.h>
126
127 struct tcpipv6hdr tcp_saveti6;
128
129 /* for the packet header length in the mbuf */
130 #define M_PH_LEN(m) (((struct mbuf *)(m))->m_pkthdr.len)
131 #define M_V6_LEN(m) (M_PH_LEN(m) - sizeof(struct ip6_hdr))
132 #define M_V4_LEN(m) (M_PH_LEN(m) - sizeof(struct ip))
133 #endif /* INET6 */
134
135 #ifdef TCP_SIGNATURE
136 #include <syskern/md5.h>
137 #endif
138
139 int tcprexmtthresh = 3;
140 int tcptv_keep_init = TCPTV_KEEP_INIT;
141
142 extern u_long sb_max;
143 extern int stdbsdtcp;
144
145 int tcp_rst_ppslim = 100; /* 100pps */
146 int tcp_rst_ppslim_count = 0;
147 struct timeval tcp_rst_ppslim_last;
148
149 int tcp_ackdrop_ppslim = 100; /* 100pps */
150 int tcp_ackdrop_ppslim_count = 0;
151 struct timeval tcp_ackdrop_ppslim_last;
152
153 #endif /* TUBA_INCLUDE */
154 #define TCP_PAWS_IDLE (24 * 24 * 60 * 60 * PR_SLOWHZ)
155
156 /* for modulo comparisons of timestamps */
157 #define TSTMP_LT(a,b) ((int)((a)-(b)) < 0)
158 #define TSTMP_GEQ(a,b) ((int)((a)-(b)) >= 0)
159
160 /* for TCP SACK comparisons */
161 #define SEQ_MIN(a,b) (SEQ_LT(a,b) ? (a) : (b))
162 #define SEQ_MAX(a,b) (SEQ_GT(a,b) ? (a) : (b))
163
164 /*
165 * Neighbor Discovery, Neighbor Unreachability Detection Upper layer hint.
166 */
167 #ifdef INET6
168 #define ND6_HINT(tp) \
169 do { \
170 if (tp && tp->t_inpcb && (tp->t_inpcb->inp_flags & INP_IPV6) && \
171 tp->t_inpcb->inp_route6.ro_rt) { \
172 nd6_nud_hint(tp->t_inpcb->inp_route6.ro_rt, NULL, 0); \
173 } \
174 } while (0)
175 #else
176 #define ND6_HINT(tp)
177 #endif
178
179 #ifdef TCP_ECN
180 /*
181 * ECN (Explicit Congestion Notification) support based on RFC3168
182 * implementation note:
183 * snd_last is used to track a recovery phase.
184 * when cwnd is reduced, snd_last is set to snd_max.
185 * while snd_last > snd_una, the sender is in a recovery phase and
186 * its cwnd should not be reduced again.
187 * snd_last follows snd_una when not in a recovery phase.
188 */
189 #endif
190
191 /*
192 * Macro to compute ACK transmission behavior. Delay the ACK unless
193 * we have already delayed an ACK (must send an ACK every two segments).
194 * We also ACK immediately if we received a PUSH and the ACK-on-PUSH
195 * option is enabled.
196 */
197 #define TCP_SETUP_ACK(tp, tiflags) \
198 do { \
199 if ((tp)->t_flags & TF_DELACK || \
200 (tcp_ack_on_push && (tiflags) & TH_PUSH)) \
201 tp->t_flags |= TF_ACKNOW; \
202 else \
203 TCP_SET_DELACK(tp); \
204 } while (0)
205
206 /*
207 * Insert segment ti into reassembly queue of tcp with
208 * control block tp. Return TH_FIN if reassembly now includes
209 * a segment with FIN. The macro form does the common case inline
210 * (segment is the next to be received on an established connection,
211 * and the queue is empty), avoiding linkage into and removal
212 * from the queue and repetition of various conversions.
213 * Set DELACK for segments received in order, but ack immediately
214 * when segments are out of order (so fast retransmit can work).
215 */
216
217 #ifndef TUBA_INCLUDE
218
219 int
tcp_reass(tp,th,m,tlen)220 tcp_reass(tp, th, m, tlen)
221 struct tcpcb *tp;
222 struct tcphdr *th;
223 struct mbuf *m;
224 int *tlen;
225 {
226 struct ipqent *p, *q, *nq, *tiqe;
227 struct socket *so = tp->t_inpcb->inp_socket;
228 int flags;
229
230 /*
231 * Call with th==0 after become established to
232 * force pre-ESTABLISHED data up to user socket.
233 */
234 if (th == 0)
235 goto present;
236
237 /*
238 * Allocate a new queue entry, before we throw away any data.
239 * If we can't, just drop the packet. XXX
240 */
241 tiqe = pool_get(&tcpqe_pool, PR_NOWAIT);
242 if (tiqe == NULL) {
243 tiqe = LIST_FIRST(&tp->segq);
244 if (tiqe != NULL && th->th_seq == tp->rcv_nxt) {
245 /* Reuse last entry since new segment fills a hole */
246 while ((p = LIST_NEXT(tiqe, ipqe_q)) != NULL)
247 tiqe = p;
248 m_freem(tiqe->ipqe_m);
249 LIST_REMOVE(tiqe, ipqe_q);
250 }
251 if (tiqe == NULL || th->th_seq != tp->rcv_nxt) {
252 /* Flush segment queue for this connection */
253 tcp_freeq(tp);
254 tcpstat.tcps_rcvmemdrop++;
255 m_freem(m);
256 return (0);
257 }
258 }
259
260 /*
261 * Find a segment which begins after this one does.
262 */
263 for (p = NULL, q = tp->segq.lh_first; q != NULL;
264 p = q, q = q->ipqe_q.le_next)
265 if (SEQ_GT(q->ipqe_tcp->th_seq, th->th_seq))
266 break;
267
268 /*
269 * If there is a preceding segment, it may provide some of
270 * our data already. If so, drop the data from the incoming
271 * segment. If it provides all of our data, drop us.
272 */
273 if (p != NULL) {
274 struct tcphdr *phdr = p->ipqe_tcp;
275 int i;
276
277 /* conversion to int (in i) handles seq wraparound */
278 i = phdr->th_seq + phdr->th_reseqlen - th->th_seq;
279 if (i > 0) {
280 if (i >= *tlen) {
281 tcpstat.tcps_rcvduppack++;
282 tcpstat.tcps_rcvdupbyte += *tlen;
283 m_freem(m);
284 pool_put(&tcpqe_pool, tiqe);
285 return (0);
286 }
287 m_adj(m, i);
288 *tlen -= i;
289 th->th_seq += i;
290 }
291 }
292 tcpstat.tcps_rcvoopack++;
293 tcpstat.tcps_rcvoobyte += *tlen;
294
295 /*
296 * While we overlap succeeding segments trim them or,
297 * if they are completely covered, dequeue them.
298 */
299 for (; q != NULL; q = nq) {
300 struct tcphdr *qhdr = q->ipqe_tcp;
301 int i = (th->th_seq + *tlen) - qhdr->th_seq;
302
303 if (i <= 0)
304 break;
305 if (i < qhdr->th_reseqlen) {
306 qhdr->th_seq += i;
307 qhdr->th_reseqlen -= i;
308 m_adj(q->ipqe_m, i);
309 break;
310 }
311 nq = q->ipqe_q.le_next;
312 m_freem(q->ipqe_m);
313 LIST_REMOVE(q, ipqe_q);
314 pool_put(&tcpqe_pool, q);
315 }
316
317 /* Insert the new segment queue entry into place. */
318 tiqe->ipqe_m = m;
319 th->th_reseqlen = *tlen;
320 tiqe->ipqe_tcp = th;
321 if (p == NULL) {
322 LIST_INSERT_HEAD(&tp->segq, tiqe, ipqe_q);
323 } else {
324 LIST_INSERT_AFTER(p, tiqe, ipqe_q);
325 }
326
327 present:
328 /*
329 * Present data to user, advancing rcv_nxt through
330 * completed sequence space.
331 */
332 if (TCPS_HAVEESTABLISHED(tp->t_state) == 0)
333 return (0);
334 q = tp->segq.lh_first;
335 if (q == NULL || q->ipqe_tcp->th_seq != tp->rcv_nxt)
336 return (0);
337 if (tp->t_state == TCPS_SYN_RECEIVED && q->ipqe_tcp->th_reseqlen)
338 return (0);
339 do {
340 tp->rcv_nxt += q->ipqe_tcp->th_reseqlen;
341 flags = q->ipqe_tcp->th_flags & TH_FIN;
342
343 nq = q->ipqe_q.le_next;
344 LIST_REMOVE(q, ipqe_q);
345 ND6_HINT(tp);
346 if (so->so_state & SS_CANTRCVMORE)
347 m_freem(q->ipqe_m);
348 else
349 sbappendstream(&so->so_rcv, q->ipqe_m);
350 pool_put(&tcpqe_pool, q);
351 q = nq;
352 } while (q != NULL && q->ipqe_tcp->th_seq == tp->rcv_nxt);
353 sorwakeup(so);
354 return (flags);
355 }
356
357 #ifdef INET6
358 int
tcp6_input(mp,offp,proto)359 tcp6_input(mp, offp, proto)
360 struct mbuf **mp;
361 int *offp, proto;
362 {
363 struct mbuf *m = *mp;
364
365 #if defined(NFAITH) && 0 < NFAITH
366 if (m->m_pkthdr.rcvif) {
367 if (m->m_pkthdr.rcvif->if_type == IFT_FAITH) {
368 /* XXX send icmp6 host/port unreach? */
369 m_freem(m);
370 return IPPROTO_DONE;
371 }
372 }
373 #endif
374
375 /*
376 * draft-itojun-ipv6-tcp-to-anycast
377 * better place to put this in?
378 */
379 if (m->m_flags & M_ANYCAST6) {
380 if (m->m_len >= sizeof(struct ip6_hdr)) {
381 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
382 icmp6_error(m, ICMP6_DST_UNREACH,
383 ICMP6_DST_UNREACH_ADDR,
384 (caddr_t)&ip6->ip6_dst - (caddr_t)ip6);
385 } else
386 m_freem(m);
387 return IPPROTO_DONE;
388 }
389
390 tcp_input(m, *offp, proto);
391 return IPPROTO_DONE;
392 }
393 #endif
394
395 /*
396 * TCP input routine, follows pages 65-76 of the
397 * protocol specification dated September, 1981 very closely.
398 */
399 void
tcp_input(struct mbuf * m,...)400 tcp_input(struct mbuf *m, ...)
401 {
402 struct ip *ip;
403 struct inpcb *inp;
404 u_int8_t *optp = NULL;
405 int optlen = 0;
406 int len, tlen, off;
407 struct tcpcb *tp = 0;
408 int tiflags;
409 struct socket *so = NULL;
410 int todrop, acked, ourfinisacked, needoutput = 0;
411 int hdroptlen = 0;
412 short ostate = 0;
413 int iss = 0;
414 u_long tiwin;
415 struct tcp_opt_info opti;
416 int iphlen;
417 va_list ap;
418 struct tcphdr *th;
419 #ifdef INET6
420 struct ip6_hdr *ip6 = NULL;
421 #endif /* INET6 */
422 #ifdef IPSEC
423 struct m_tag *mtag;
424 struct tdb_ident *tdbi;
425 struct tdb *tdb;
426 int error, s;
427 #endif /* IPSEC */
428 int af;
429 #ifdef TCP_ECN
430 u_char iptos;
431 #endif
432
433 va_start(ap, m);
434 iphlen = va_arg(ap, int);
435 va_end(ap);
436
437 tcpstat.tcps_rcvtotal++;
438
439 opti.ts_present = 0;
440 opti.maxseg = 0;
441
442 /*
443 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
444 * See below for AF specific multicast.
445 */
446 if (m->m_flags & (M_BCAST|M_MCAST))
447 goto drop;
448
449 /*
450 * Before we do ANYTHING, we have to figure out if it's TCP/IPv6 or
451 * TCP/IPv4.
452 */
453 switch (mtod(m, struct ip *)->ip_v) {
454 #ifdef INET6
455 case 6:
456 af = AF_INET6;
457 break;
458 #endif
459 case 4:
460 af = AF_INET;
461 break;
462 default:
463 m_freem(m);
464 return; /*EAFNOSUPPORT*/
465 }
466
467 /*
468 * Get IP and TCP header together in first mbuf.
469 * Note: IP leaves IP header in first mbuf.
470 */
471 switch (af) {
472 case AF_INET:
473 #ifdef DIAGNOSTIC
474 if (iphlen < sizeof(struct ip)) {
475 m_freem(m);
476 return;
477 }
478 #endif /* DIAGNOSTIC */
479 break;
480 #ifdef INET6
481 case AF_INET6:
482 #ifdef DIAGNOSTIC
483 if (iphlen < sizeof(struct ip6_hdr)) {
484 m_freem(m);
485 return;
486 }
487 #endif /* DIAGNOSTIC */
488 break;
489 #endif
490 default:
491 m_freem(m);
492 return;
493 }
494
495 IP6_EXTHDR_GET(th, struct tcphdr *, m, iphlen, sizeof(*th));
496 if (!th) {
497 tcpstat.tcps_rcvshort++;
498 return;
499 }
500
501 ip = NULL;
502 #ifdef INET6
503 ip6 = NULL;
504 #endif
505 switch (af) {
506 case AF_INET:
507 ip = mtod(m, struct ip *);
508 if (IN_MULTICAST(ip->ip_dst.s_addr) ||
509 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
510 goto drop;
511
512 tlen = m->m_pkthdr.len - iphlen;
513
514 #ifdef TCP_ECN
515 /* save ip_tos before clearing it for checksum */
516 iptos = ip->ip_tos;
517 #endif
518 /*
519 * Checksum extended TCP header and data.
520 */
521 if ((m->m_pkthdr.csum & M_TCP_CSUM_IN_OK) == 0) {
522 if (m->m_pkthdr.csum & M_TCP_CSUM_IN_BAD) {
523 tcpstat.tcps_inhwcsum++;
524 tcpstat.tcps_rcvbadsum++;
525 goto drop;
526 }
527 len = m->m_pkthdr.len - iphlen;
528 if (in4_cksum(m, IPPROTO_TCP, iphlen, len) != 0) {
529 tcpstat.tcps_rcvbadsum++;
530 goto drop;
531 }
532 } else {
533 m->m_pkthdr.csum &= ~M_TCP_CSUM_IN_OK;
534 tcpstat.tcps_inhwcsum++;
535 }
536 break;
537 #ifdef INET6
538 case AF_INET6:
539 ip6 = mtod(m, struct ip6_hdr *);
540 tlen = m->m_pkthdr.len - iphlen;
541 #ifdef TCP_ECN
542 iptos = (ntohl(ip6->ip6_flow) >> 20) & 0xff;
543 #endif
544
545 /* Be proactive about malicious use of IPv4 mapped address */
546 if (IN6_IS_ADDR_V4MAPPED(&ip6->ip6_src) ||
547 IN6_IS_ADDR_V4MAPPED(&ip6->ip6_dst)) {
548 /* XXX stat */
549 goto drop;
550 }
551
552 /*
553 * Be proactive about unspecified IPv6 address in source.
554 * As we use all-zero to indicate unbounded/unconnected pcb,
555 * unspecified IPv6 address can be used to confuse us.
556 *
557 * Note that packets with unspecified IPv6 destination is
558 * already dropped in ip6_input.
559 */
560 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src)) {
561 /* XXX stat */
562 goto drop;
563 }
564
565 /* Discard packets to multicast */
566 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
567 /* XXX stat */
568 goto drop;
569 }
570
571 /*
572 * Checksum extended TCP header and data.
573 */
574 if (in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), tlen)) {
575 tcpstat.tcps_rcvbadsum++;
576 goto drop;
577 }
578 break;
579 #endif
580 }
581 #endif /* TUBA_INCLUDE */
582
583 th = (struct tcphdr *)(mtod(m, caddr_t) + iphlen);
584
585 /*
586 * Check that TCP offset makes sense,
587 * pull out TCP options and adjust length. XXX
588 */
589 off = th->th_off << 2;
590 if (off < sizeof(struct tcphdr) || off > tlen) {
591 tcpstat.tcps_rcvbadoff++;
592 goto drop;
593 }
594 tlen -= off;
595 if (off > sizeof(struct tcphdr)) {
596 IP6_EXTHDR_GET(th, struct tcphdr *, m, iphlen, off);
597 if (!th) {
598 tcpstat.tcps_rcvshort++;
599 return;
600 }
601 optlen = off - sizeof(struct tcphdr);
602 optp = mtod(m, u_int8_t *) + iphlen + sizeof(struct tcphdr);
603 /*
604 * Do quick retrieval of timestamp options ("options
605 * prediction?"). If timestamp is the only option and it's
606 * formatted as recommended in RFC 1323 appendix A, we
607 * quickly get the values now and not bother calling
608 * tcp_dooptions(), etc.
609 * MARco doesn't recommend only the NOP aspect of appendix A,
610 * (u_int16_t should be appropriate and more efficient on most
611 * arch/ efficient enough on only-32-or-more bits arch.s,
612 * in addition to the 4 network bytes overload in each packet
613 * carring TCP options (with SackOK(len 2) and TIMESTAMP(len 10))
614 * before the default was a _double_ NOP NOP use)
615 */
616 if ((optlen == TCPOLEN_TSTAMP_APPA ||
617 (optlen > TCPOLEN_TSTAMP_APPA &&
618 optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
619 *((u_int16_t *)optp + 1) == htons(TCPOPT_TIMESTAMP << 8 |
620 TCPOLEN_TIMESTAMP) &&
621 (th->th_flags & TH_SYN) == 0) {
622 opti.ts_present = 1;
623 opti.ts_val = ntohl(*(u_int32_t *)(optp + 4));
624 opti.ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
625 optp = NULL; /* we've parsed the options */
626 }
627 }
628 tiflags = th->th_flags;
629
630 /*
631 * Convert TCP protocol specific fields to host format.
632 */
633 NTOHL(th->th_seq);
634 NTOHL(th->th_ack);
635 NTOHS(th->th_win);
636 NTOHS(th->th_urp);
637
638 /*
639 * Locate pcb for segment.
640 */
641 findpcb:
642 switch (af) {
643 #ifdef INET6
644 case AF_INET6:
645 inp = in6_pcbhashlookup(&tcbtable, &ip6->ip6_src, th->th_sport,
646 &ip6->ip6_dst, th->th_dport);
647 break;
648 #endif
649 case AF_INET:
650 inp = in_pcbhashlookup(&tcbtable, ip->ip_src, th->th_sport,
651 ip->ip_dst, th->th_dport);
652 break;
653 }
654 if (inp == 0) {
655 ++tcpstat.tcps_pcbhashmiss;
656 switch (af) {
657 #ifdef INET6
658 case AF_INET6:
659 inp = in6_pcblookup_listen(&tcbtable,
660 &ip6->ip6_dst, th->th_dport, m_tag_find(m,
661 PACKET_TAG_PF_TRANSLATE_LOCALHOST, NULL) != NULL);
662 break;
663 #endif /* INET6 */
664 case AF_INET:
665 inp = in_pcblookup_listen(&tcbtable,
666 ip->ip_dst, th->th_dport, m_tag_find(m,
667 PACKET_TAG_PF_TRANSLATE_LOCALHOST, NULL) != NULL);
668 break;
669 }
670 /*
671 * If the state is CLOSED (i.e., TCB does not exist) then
672 * all data in the incoming segment is discarded.
673 * If the TCB exists but is in CLOSED state, it is embryonic,
674 * but should either do a listen or a connect soon.
675 */
676 if (inp == 0) {
677 ++tcpstat.tcps_noport;
678 goto dropwithreset_ratelim;
679 }
680 }
681
682 tp = intotcpcb(inp);
683 if (tp == 0)
684 goto dropwithreset_ratelim;
685 if (tp->t_state == TCPS_CLOSED)
686 goto drop;
687
688 /* Unscale the window into a 32-bit value. */
689 if ((tiflags & TH_SYN) == 0)
690 tiwin = th->th_win << tp->snd_scale;
691 else
692 tiwin = th->th_win;
693
694 so = inp->inp_socket;
695 if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
696 union syn_cache_sa src;
697 union syn_cache_sa dst;
698
699 bzero(&src, sizeof(src));
700 bzero(&dst, sizeof(dst));
701 switch (af) {
702 #ifdef INET
703 case AF_INET:
704 src.sin.sin_len = sizeof(struct sockaddr_in);
705 src.sin.sin_family = AF_INET;
706 src.sin.sin_addr = ip->ip_src;
707 src.sin.sin_port = th->th_sport;
708
709 dst.sin.sin_len = sizeof(struct sockaddr_in);
710 dst.sin.sin_family = AF_INET;
711 dst.sin.sin_addr = ip->ip_dst;
712 dst.sin.sin_port = th->th_dport;
713 break;
714 #endif
715 #ifdef INET6
716 case AF_INET6:
717 src.sin6.sin6_len = sizeof(struct sockaddr_in6);
718 src.sin6.sin6_family = AF_INET6;
719 src.sin6.sin6_addr = ip6->ip6_src;
720 src.sin6.sin6_port = th->th_sport;
721
722 dst.sin6.sin6_len = sizeof(struct sockaddr_in6);
723 dst.sin6.sin6_family = AF_INET6;
724 dst.sin6.sin6_addr = ip6->ip6_dst;
725 dst.sin6.sin6_port = th->th_dport;
726 break;
727 #endif /* INET6 */
728 default:
729 goto badsyn; /*sanity*/
730 }
731
732 if (so->so_options & SO_DEBUG) {
733 ostate = tp->t_state;
734 switch (af) {
735 #ifdef INET6
736 case AF_INET6:
737 bcopy(ip6, &tcp_saveti6.ti6_i, sizeof(*ip6));
738 bcopy(th, &tcp_saveti6.ti6_t, sizeof(*th));
739 break;
740 #endif
741 case AF_INET:
742 bcopy(ip, &tcp_saveti.ti_i, sizeof(*ip));
743 bcopy(th, &tcp_saveti.ti_t, sizeof(*th));
744 break;
745 }
746 }
747 if (so->so_options & SO_ACCEPTCONN) {
748 if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
749 if (tiflags & TH_RST) {
750 syn_cache_reset(&src.sa, &dst.sa, th);
751 } else if ((tiflags & (TH_ACK|TH_SYN)) ==
752 (TH_ACK|TH_SYN)) {
753 /*
754 * Received a SYN,ACK. This should
755 * never happen while we are in
756 * LISTEN. Send an RST.
757 */
758 goto badsyn;
759 } else if (tiflags & TH_ACK) {
760 so = syn_cache_get(&src.sa, &dst.sa,
761 th, iphlen, tlen, so, m);
762 if (so == NULL) {
763 /*
764 * We don't have a SYN for
765 * this ACK; send an RST.
766 */
767 goto badsyn;
768 } else if (so ==
769 (struct socket *)(-1)) {
770 /*
771 * We were unable to create
772 * the connection. If the
773 * 3-way handshake was
774 * completed, and RST has
775 * been sent to the peer.
776 * Since the mbuf might be
777 * in use for the reply,
778 * do not free it.
779 */
780 m = NULL;
781 } else {
782 /*
783 * We have created a
784 * full-blown connection.
785 */
786 tp = NULL;
787 inp = (struct inpcb *)so->so_pcb;
788 tp = intotcpcb(inp);
789 if (tp == NULL)
790 goto badsyn; /*XXX*/
791
792 /*
793 * Compute proper scaling
794 * value from buffer space
795 */
796 tcp_rscale(tp, so->so_rcv.sb_hiwat);
797 goto after_listen;
798 }
799 } else {
800 /*
801 * None of RST, SYN or ACK was set.
802 * This is an invalid packet for a
803 * TCB in LISTEN state. Send a RST.
804 */
805 goto badsyn;
806 }
807 } else {
808 /*
809 * Received a SYN.
810 */
811 #ifdef INET6
812 /*
813 * If deprecated address is forbidden, we do
814 * not accept SYN to deprecated interface
815 * address to prevent any new inbound
816 * connection from getting established.
817 * When we do not accept SYN, we send a TCP
818 * RST, with deprecated source address (instead
819 * of dropping it). We compromise it as it is
820 * much better for peer to send a RST, and
821 * RST will be the final packet for the
822 * exchange.
823 *
824 * If we do not forbid deprecated addresses, we
825 * accept the SYN packet. RFC2462 does not
826 * suggest dropping SYN in this case.
827 * If we decipher RFC2462 5.5.4, it says like
828 * this:
829 * 1. use of deprecated addr with existing
830 * communication is okay - "SHOULD continue
831 * to be used"
832 * 2. use of it with new communication:
833 * (2a) "SHOULD NOT be used if alternate
834 * address with sufficient scope is
835 * available"
836 * (2b) nothing mentioned otherwise.
837 * Here we fall into (2b) case as we have no
838 * choice in our source address selection - we
839 * must obey the peer.
840 *
841 * The wording in RFC2462 is confusing, and
842 * there are multiple description text for
843 * deprecated address handling - worse, they
844 * are not exactly the same. I believe 5.5.4
845 * is the best one, so we follow 5.5.4.
846 */
847 if (ip6 && !ip6_use_deprecated) {
848 struct in6_ifaddr *ia6;
849
850 if ((ia6 = in6ifa_ifpwithaddr(m->m_pkthdr.rcvif,
851 &ip6->ip6_dst)) &&
852 (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
853 tp = NULL;
854 goto dropwithreset;
855 }
856 }
857 #endif
858
859 /*
860 * LISTEN socket received a SYN
861 * from itself? This can't possibly
862 * be valid; drop the packet.
863 */
864 if (th->th_dport == th->th_sport) {
865 switch (af) {
866 #ifdef INET6
867 case AF_INET6:
868 if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_src,
869 &ip6->ip6_dst)) {
870 tcpstat.tcps_badsyn++;
871 goto drop;
872 }
873 break;
874 #endif /* INET6 */
875 case AF_INET:
876 if (ip->ip_dst.s_addr == ip->ip_src.s_addr) {
877 tcpstat.tcps_badsyn++;
878 goto drop;
879 }
880 break;
881 }
882 }
883
884 /*
885 * SYN looks ok; create compressed TCP
886 * state for it.
887 */
888 if (so->so_qlen <= so->so_qlimit &&
889 syn_cache_add(&src.sa, &dst.sa, th, iphlen,
890 so, m, optp, optlen, &opti))
891 m = NULL;
892 }
893 goto drop;
894 }
895 }
896
897 after_listen:
898 #ifdef DIAGNOSTIC
899 /*
900 * Should not happen now that all embryonic connections
901 * are handled with compressed state.
902 */
903 if (tp->t_state == TCPS_LISTEN)
904 panic("tcp_input: TCPS_LISTEN");
905 #endif
906
907 #ifdef IPSEC
908 /* Find most recent IPsec tag */
909 mtag = m_tag_find(m, PACKET_TAG_IPSEC_IN_DONE, NULL);
910 s = splnet();
911 if (mtag != NULL) {
912 tdbi = (struct tdb_ident *)(mtag + 1);
913 tdb = gettdb(tdbi->spi, &tdbi->dst, tdbi->proto);
914 } else
915 tdb = NULL;
916 ipsp_spd_lookup(m, af, iphlen, &error, IPSP_DIRECTION_IN,
917 tdb, inp);
918 if (error) {
919 splx(s);
920 goto drop;
921 }
922
923 /* Latch SA */
924 if (inp->inp_tdb_in != tdb) {
925 if (tdb) {
926 tdb_add_inp(tdb, inp, 1);
927 if (inp->inp_ipo == NULL) {
928 inp->inp_ipo = ipsec_add_policy(inp, af,
929 IPSP_DIRECTION_OUT);
930 if (inp->inp_ipo == NULL) {
931 splx(s);
932 goto drop;
933 }
934 }
935 if (inp->inp_ipo->ipo_dstid == NULL &&
936 tdb->tdb_srcid != NULL) {
937 inp->inp_ipo->ipo_dstid = tdb->tdb_srcid;
938 tdb->tdb_srcid->ref_count++;
939 }
940 if (inp->inp_ipsec_remotecred == NULL &&
941 tdb->tdb_remote_cred != NULL) {
942 inp->inp_ipsec_remotecred =
943 tdb->tdb_remote_cred;
944 tdb->tdb_remote_cred->ref_count++;
945 }
946 if (inp->inp_ipsec_remoteauth == NULL &&
947 tdb->tdb_remote_auth != NULL) {
948 inp->inp_ipsec_remoteauth =
949 tdb->tdb_remote_auth;
950 tdb->tdb_remote_auth->ref_count++;
951 }
952 } else { /* Just reset */
953 TAILQ_REMOVE(&inp->inp_tdb_in->tdb_inp_in, inp,
954 inp_tdb_in_next);
955 inp->inp_tdb_in = NULL;
956 }
957 }
958 splx(s);
959 #endif /* IPSEC */
960
961 /*
962 * Segment received on connection.
963 * Reset idle time and keep-alive timer.
964 */
965 tp->t_rcvtime = tcp_now;
966 if (TCPS_HAVEESTABLISHED(tp->t_state))
967 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
968
969 #ifdef TCP_SACK
970 if (tp->sack_enable)
971 tcp_del_sackholes(tp, th); /* Delete stale SACK holes */
972 #endif /* TCP_SACK */
973
974 /*
975 * Process options.
976 */
977 #ifdef TCP_SIGNATURE
978 if (optp || (tp->t_flags & TF_SIGNATURE))
979 #else
980 if (optp)
981 #endif
982 if (tcp_dooptions(tp, optp, optlen, th, m, iphlen, &opti))
983 goto drop;
984
985 #ifdef TCP_SACK
986 if (tp->sack_enable) {
987 tp->rcv_laststart = th->th_seq; /* last rec'vd segment*/
988 tp->rcv_lastend = th->th_seq + tlen;
989 }
990 #endif /* TCP_SACK */
991 #ifdef TCP_ECN
992 /* if congestion experienced, set ECE bit in subsequent packets. */
993 if ((iptos & IPTOS_ECN_MASK) == IPTOS_ECN_CE) {
994 tp->t_flags |= TF_RCVD_CE;
995 tcpstat.tcps_ecn_rcvce++;
996 }
997 #endif
998 /*
999 * Header prediction: check for the two common cases
1000 * of a uni-directional data xfer. If the packet has
1001 * no control flags, is in-sequence, the window didn't
1002 * change and we're not retransmitting, it's a
1003 * candidate. If the length is zero and the ack moved
1004 * forward, we're the sender side of the xfer. Just
1005 * free the data acked & wake any higher level process
1006 * that was blocked waiting for space. If the length
1007 * is non-zero and the ack didn't move, we're the
1008 * receiver side. If we're getting packets in-order
1009 * (the reassembly queue is empty), add the data to
1010 * the socket buffer and note that we need a delayed ack.
1011 */
1012 if (tp->t_state == TCPS_ESTABLISHED &&
1013 #ifdef TCP_ECN
1014 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ECE|TH_CWR|TH_ACK)) == TH_ACK &&
1015 #else
1016 (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
1017 #endif
1018 (!opti.ts_present || TSTMP_GEQ(opti.ts_val, tp->ts_recent)) &&
1019 th->th_seq == tp->rcv_nxt &&
1020 tiwin && tiwin == tp->snd_wnd &&
1021 tp->snd_nxt == tp->snd_max) {
1022
1023 /*
1024 * If last ACK falls within this segment's sequence numbers,
1025 * record the timestamp.
1026 * Fix from Braden, see Stevens p. 870
1027 */
1028 if (opti.ts_present && SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1029 tp->ts_recent_age = tcp_now;
1030 tp->ts_recent = opti.ts_val;
1031 }
1032
1033 if (tlen == 0) {
1034 if (SEQ_GT(th->th_ack, tp->snd_una) &&
1035 SEQ_LEQ(th->th_ack, tp->snd_max) &&
1036 tp->snd_cwnd >= tp->snd_wnd &&
1037 tp->t_dupacks == 0) {
1038 /*
1039 * this is a pure ack for outstanding data.
1040 */
1041 ++tcpstat.tcps_predack;
1042 if (opti.ts_present)
1043 tcp_xmit_timer(tp, tcp_now-opti.ts_ecr+1);
1044 else if (tp->t_rtttime &&
1045 SEQ_GT(th->th_ack, tp->t_rtseq))
1046 tcp_xmit_timer(tp,
1047 tcp_now - tp->t_rtttime);
1048 acked = th->th_ack - tp->snd_una;
1049 tcpstat.tcps_rcvackpack++;
1050 tcpstat.tcps_rcvackbyte += acked;
1051 ND6_HINT(tp);
1052 sbdrop(&so->so_snd, acked);
1053
1054 /*
1055 * If we had a pending ICMP message that
1056 * referres to data that have just been
1057 * acknowledged, disregard the recorded ICMP
1058 * message.
1059 */
1060 if ((tp->t_flags & TF_PMTUD_PEND) &&
1061 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
1062 tp->t_flags &= ~TF_PMTUD_PEND;
1063
1064 /*
1065 * Keep track of the largest chunk of data
1066 * acknowledged since last PMTU update
1067 */
1068 if (tp->t_pmtud_mss_acked < acked)
1069 tp->t_pmtud_mss_acked = acked;
1070
1071 tp->snd_una = th->th_ack;
1072 #if defined(TCP_SACK) || defined(TCP_ECN)
1073 /*
1074 * We want snd_last to track snd_una so
1075 * as to avoid sequence wraparound problems
1076 * for very large transfers.
1077 */
1078 #ifdef TCP_ECN
1079 if (SEQ_GT(tp->snd_una, tp->snd_last))
1080 #endif
1081 tp->snd_last = tp->snd_una;
1082 #endif /* TCP_SACK */
1083 #if defined(TCP_SACK) && defined(TCP_FACK)
1084 tp->snd_fack = tp->snd_una;
1085 tp->retran_data = 0;
1086 #endif /* TCP_FACK */
1087 m_freem(m);
1088
1089 /*
1090 * If all outstanding data are acked, stop
1091 * retransmit timer, otherwise restart timer
1092 * using current (possibly backed-off) value.
1093 * If process is waiting for space,
1094 * wakeup/selwakeup/signal. If data
1095 * are ready to send, let tcp_output
1096 * decide between more output or persist.
1097 */
1098 if (tp->snd_una == tp->snd_max)
1099 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1100 else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1101 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1102
1103 if (sb_notify(&so->so_snd))
1104 sowwakeup(so);
1105 if (so->so_snd.sb_cc)
1106 (void) tcp_output(tp);
1107 return;
1108 }
1109 } else if (th->th_ack == tp->snd_una &&
1110 tp->segq.lh_first == NULL &&
1111 tlen <= sbspace(&so->so_rcv)) {
1112 /*
1113 * This is a pure, in-sequence data packet
1114 * with nothing on the reassembly queue and
1115 * we have enough buffer space to take it.
1116 */
1117 #ifdef TCP_SACK
1118 /* Clean receiver SACK report if present */
1119 if (tp->sack_enable && tp->rcv_numsacks)
1120 tcp_clean_sackreport(tp);
1121 #endif /* TCP_SACK */
1122 ++tcpstat.tcps_preddat;
1123 tp->rcv_nxt += tlen;
1124 tcpstat.tcps_rcvpack++;
1125 tcpstat.tcps_rcvbyte += tlen;
1126 ND6_HINT(tp);
1127 /*
1128 * Drop TCP, IP headers and TCP options then add data
1129 * to socket buffer.
1130 */
1131 if (so->so_state & SS_CANTRCVMORE)
1132 m_freem(m);
1133 else {
1134 m_adj(m, iphlen + off);
1135 sbappendstream(&so->so_rcv, m);
1136 }
1137 sorwakeup(so);
1138 TCP_SETUP_ACK(tp, tiflags);
1139 if (tp->t_flags & TF_ACKNOW)
1140 (void) tcp_output(tp);
1141 return;
1142 }
1143 }
1144
1145 /*
1146 * Compute mbuf offset to TCP data segment.
1147 */
1148 hdroptlen = iphlen + off;
1149
1150 /*
1151 * Calculate amount of space in receive window,
1152 * and then do TCP input processing.
1153 * Receive window is amount of space in rcv queue,
1154 * but not less than advertised window.
1155 */
1156 { int win;
1157
1158 win = sbspace(&so->so_rcv);
1159 if (win < 0)
1160 win = 0;
1161 tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1162 }
1163
1164 switch (tp->t_state) {
1165
1166 /*
1167 * If the state is SYN_RECEIVED:
1168 * if seg contains SYN/ACK, send an RST.
1169 * if seg contains an ACK, but not for our SYN/ACK, send an RST
1170 */
1171
1172 case TCPS_SYN_RECEIVED:
1173 if (tiflags & TH_ACK) {
1174 if (tiflags & TH_SYN) {
1175 tcpstat.tcps_badsyn++;
1176 goto dropwithreset;
1177 }
1178 if (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1179 SEQ_GT(th->th_ack, tp->snd_max))
1180 goto dropwithreset;
1181 }
1182 break;
1183
1184 /*
1185 * If the state is SYN_SENT:
1186 * if seg contains an ACK, but not for our SYN, drop the input.
1187 * if seg contains a RST, then drop the connection.
1188 * if seg does not contain SYN, then drop it.
1189 * Otherwise this is an acceptable SYN segment
1190 * initialize tp->rcv_nxt and tp->irs
1191 * if seg contains ack then advance tp->snd_una
1192 * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1193 * arrange for segment to be acked (eventually)
1194 * continue processing rest of data/controls, beginning with URG
1195 */
1196 case TCPS_SYN_SENT:
1197 if ((tiflags & TH_ACK) &&
1198 (SEQ_LEQ(th->th_ack, tp->iss) ||
1199 SEQ_GT(th->th_ack, tp->snd_max)))
1200 goto dropwithreset;
1201 if (tiflags & TH_RST) {
1202 #ifdef TCP_ECN
1203 /* if ECN is enabled, fall back to non-ecn at rexmit */
1204 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN))
1205 goto drop;
1206 #endif
1207 if (tiflags & TH_ACK)
1208 tp = tcp_drop(tp, ECONNREFUSED);
1209 goto drop;
1210 }
1211 if ((tiflags & TH_SYN) == 0)
1212 goto drop;
1213 if (tiflags & TH_ACK) {
1214 tp->snd_una = th->th_ack;
1215 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1216 tp->snd_nxt = tp->snd_una;
1217 }
1218 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1219 tp->irs = th->th_seq;
1220 tcp_mss(tp, opti.maxseg);
1221 /* Reset initial window to 1 segment for retransmit */
1222 if (tp->t_rxtshift > 0)
1223 tp->snd_cwnd = tp->t_maxseg;
1224 tcp_rcvseqinit(tp);
1225 tp->t_flags |= TF_ACKNOW;
1226 #ifdef TCP_SACK
1227 /*
1228 * If we've sent a SACK_PERMITTED option, and the peer
1229 * also replied with one, then TF_SACK_PERMIT should have
1230 * been set in tcp_dooptions(). If it was not, disable SACKs.
1231 */
1232 if (tp->sack_enable)
1233 tp->sack_enable = tp->t_flags & TF_SACK_PERMIT;
1234 #endif
1235 #ifdef TCP_ECN
1236 /*
1237 * if ECE is set but CWR is not set for SYN-ACK, or
1238 * both ECE and CWR are set for simultaneous open,
1239 * peer is ECN capable.
1240 */
1241 if (tcp_do_ecn) {
1242 if ((tiflags & (TH_ACK|TH_ECE|TH_CWR))
1243 == (TH_ACK|TH_ECE) ||
1244 (tiflags & (TH_ACK|TH_ECE|TH_CWR))
1245 == (TH_ECE|TH_CWR)) {
1246 tp->t_flags |= TF_ECN_PERMIT;
1247 tiflags &= ~(TH_ECE|TH_CWR);
1248 tcpstat.tcps_ecn_accepts++;
1249 }
1250 }
1251 #endif
1252
1253 if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
1254 tcpstat.tcps_connects++;
1255 soisconnected(so);
1256 tp->t_state = TCPS_ESTABLISHED;
1257 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1258 /* Do window scaling on this connection? */
1259 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1260 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1261 tp->snd_scale = tp->requested_s_scale;
1262 tp->rcv_scale = tp->request_r_scale;
1263 }
1264 tcp_reass_lock(tp);
1265 (void) tcp_reass(tp, (struct tcphdr *)0,
1266 (struct mbuf *)0, &tlen);
1267 tcp_reass_unlock(tp);
1268 /*
1269 * if we didn't have to retransmit the SYN,
1270 * use its rtt as our initial srtt & rtt var.
1271 */
1272 if (tp->t_rtttime)
1273 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
1274 } else
1275 tp->t_state = TCPS_SYN_RECEIVED;
1276
1277 #if 0
1278 trimthenstep6:
1279 #endif
1280 /*
1281 * Advance th->th_seq to correspond to first data byte.
1282 * If data, trim to stay within window,
1283 * dropping FIN if necessary.
1284 */
1285 th->th_seq++;
1286 if (tlen > tp->rcv_wnd) {
1287 todrop = tlen - tp->rcv_wnd;
1288 m_adj(m, -todrop);
1289 tlen = tp->rcv_wnd;
1290 tiflags &= ~TH_FIN;
1291 tcpstat.tcps_rcvpackafterwin++;
1292 tcpstat.tcps_rcvbyteafterwin += todrop;
1293 }
1294 tp->snd_wl1 = th->th_seq - 1;
1295 tp->rcv_up = th->th_seq;
1296 goto step6;
1297 }
1298
1299 /*
1300 * States other than LISTEN or SYN_SENT.
1301 * First check timestamp, if present.
1302 * Then check that at least some bytes of segment are within
1303 * receive window. If segment begins before rcv_nxt,
1304 * drop leading data (and SYN); if nothing left, just ack.
1305 *
1306 * RFC 1323 PAWS: If we have a timestamp reply on this segment
1307 * and it's less than opti.ts_recent, drop it.
1308 */
1309 if (opti.ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
1310 TSTMP_LT(opti.ts_val, tp->ts_recent)) {
1311
1312 /* Check to see if ts_recent is over 24 days old. */
1313 if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1314 /*
1315 * Invalidate ts_recent. If this segment updates
1316 * ts_recent, the age will be reset later and ts_recent
1317 * will get a valid value. If it does not, setting
1318 * ts_recent to zero will at least satisfy the
1319 * requirement that zero be placed in the timestamp
1320 * echo reply when ts_recent isn't valid. The
1321 * age isn't reset until we get a valid ts_recent
1322 * because we don't want out-of-order segments to be
1323 * dropped when ts_recent is old.
1324 */
1325 tp->ts_recent = 0;
1326 } else {
1327 tcpstat.tcps_rcvduppack++;
1328 tcpstat.tcps_rcvdupbyte += tlen;
1329 tcpstat.tcps_pawsdrop++;
1330 goto dropafterack;
1331 }
1332 }
1333
1334 todrop = tp->rcv_nxt - th->th_seq;
1335 if (todrop > 0) {
1336 if (tiflags & TH_SYN) {
1337 tiflags &= ~TH_SYN;
1338 th->th_seq++;
1339 if (th->th_urp > 1)
1340 th->th_urp--;
1341 else
1342 tiflags &= ~TH_URG;
1343 todrop--;
1344 }
1345 if (todrop > tlen ||
1346 (todrop == tlen && (tiflags & TH_FIN) == 0)) {
1347 /*
1348 * Any valid FIN must be to the left of the
1349 * window. At this point, FIN must be a
1350 * duplicate or out-of-sequence, so drop it.
1351 */
1352 tiflags &= ~TH_FIN;
1353 /*
1354 * Send ACK to resynchronize, and drop any data,
1355 * but keep on processing for RST or ACK.
1356 */
1357 tp->t_flags |= TF_ACKNOW;
1358 tcpstat.tcps_rcvdupbyte += todrop = tlen;
1359 tcpstat.tcps_rcvduppack++;
1360 } else {
1361 tcpstat.tcps_rcvpartduppack++;
1362 tcpstat.tcps_rcvpartdupbyte += todrop;
1363 }
1364 hdroptlen += todrop; /* drop from head afterwards */
1365 th->th_seq += todrop;
1366 tlen -= todrop;
1367 if (th->th_urp > todrop)
1368 th->th_urp -= todrop;
1369 else {
1370 tiflags &= ~TH_URG;
1371 th->th_urp = 0;
1372 }
1373 }
1374
1375 /*
1376 * If new data are received on a connection after the
1377 * user processes are gone, then RST the other end.
1378 */
1379 if ((so->so_state & SS_NOFDREF) &&
1380 tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1381 tp = tcp_close(tp);
1382 tcpstat.tcps_rcvafterclose++;
1383 goto dropwithreset;
1384 }
1385
1386 /*
1387 * If segment ends after window, drop trailing data
1388 * (and PUSH and FIN); if nothing left, just ACK.
1389 */
1390 todrop = (th->th_seq + tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1391 if (todrop > 0) {
1392 tcpstat.tcps_rcvpackafterwin++;
1393 if (todrop >= tlen) {
1394 tcpstat.tcps_rcvbyteafterwin += tlen;
1395 /*
1396 * If a new connection request is received
1397 * while in TIME_WAIT, drop the old connection
1398 * and start over if the sequence numbers
1399 * are above the previous ones.
1400 */
1401 if (tiflags & TH_SYN &&
1402 tp->t_state == TCPS_TIME_WAIT &&
1403 SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1404 iss = tp->snd_nxt + TCP_ISSINCR;
1405 tp = tcp_close(tp);
1406 goto findpcb;
1407 }
1408 /*
1409 * If window is closed can only take segments at
1410 * window edge, and have to drop data and PUSH from
1411 * incoming segments. Continue processing, but
1412 * remember to ack. Otherwise, drop segment
1413 * and ack.
1414 */
1415 if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1416 tp->t_flags |= TF_ACKNOW;
1417 tcpstat.tcps_rcvwinprobe++;
1418 } else
1419 goto dropafterack;
1420 } else
1421 tcpstat.tcps_rcvbyteafterwin += todrop;
1422 m_adj(m, -todrop);
1423 tlen -= todrop;
1424 tiflags &= ~(TH_PUSH|TH_FIN);
1425 }
1426
1427 /*
1428 * If last ACK falls within this segment's sequence numbers,
1429 * record its timestamp if it's more recent.
1430 * Cf fix from Braden, see Stevens p. 870
1431 */
1432 if (opti.ts_present && TSTMP_GEQ(opti.ts_val, tp->ts_recent) &&
1433 SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1434 if (SEQ_LEQ(tp->last_ack_sent, th->th_seq + tlen +
1435 ((tiflags & (TH_SYN|TH_FIN)) != 0)))
1436 tp->ts_recent = opti.ts_val;
1437 else
1438 tp->ts_recent = 0;
1439 tp->ts_recent_age = tcp_now;
1440 }
1441
1442 /*
1443 * If the RST bit is set examine the state:
1444 * SYN_RECEIVED STATE:
1445 * If passive open, return to LISTEN state.
1446 * If active open, inform user that connection was refused.
1447 * ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1448 * Inform user that connection was reset, and close tcb.
1449 * CLOSING, LAST_ACK, TIME_WAIT STATES
1450 * Close the tcb.
1451 */
1452 if (tiflags & TH_RST) {
1453 if (th->th_seq != tp->last_ack_sent)
1454 goto drop;
1455
1456 switch (tp->t_state) {
1457 case TCPS_SYN_RECEIVED:
1458 #ifdef TCP_ECN
1459 /* if ECN is enabled, fall back to non-ecn at rexmit */
1460 if (tcp_do_ecn && !(tp->t_flags & TF_DISABLE_ECN))
1461 goto drop;
1462 #endif
1463 so->so_error = ECONNREFUSED;
1464 goto close;
1465
1466 case TCPS_ESTABLISHED:
1467 case TCPS_FIN_WAIT_1:
1468 case TCPS_FIN_WAIT_2:
1469 case TCPS_CLOSE_WAIT:
1470 so->so_error = ECONNRESET;
1471 close:
1472 tp->t_state = TCPS_CLOSED;
1473 tcpstat.tcps_drops++;
1474 tp = tcp_close(tp);
1475 goto drop;
1476 case TCPS_CLOSING:
1477 case TCPS_LAST_ACK:
1478 case TCPS_TIME_WAIT:
1479 tp = tcp_close(tp);
1480 goto drop;
1481 }
1482 }
1483
1484 /*
1485 * If a SYN is in the window, then this is an
1486 * error and we ACK and drop the packet.
1487 */
1488 if (tiflags & TH_SYN)
1489 goto dropafterack_ratelim;
1490
1491 /*
1492 * If the ACK bit is off we drop the segment and return.
1493 */
1494 if ((tiflags & TH_ACK) == 0) {
1495 if (tp->t_flags & TF_ACKNOW)
1496 goto dropafterack;
1497 else
1498 goto drop;
1499 }
1500
1501 /*
1502 * Ack processing.
1503 */
1504 switch (tp->t_state) {
1505
1506 /*
1507 * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1508 * ESTABLISHED state and continue processing.
1509 * The ACK was checked above.
1510 */
1511 case TCPS_SYN_RECEIVED:
1512 tcpstat.tcps_connects++;
1513 soisconnected(so);
1514 tp->t_state = TCPS_ESTABLISHED;
1515 TCP_TIMER_ARM(tp, TCPT_KEEP, tcp_keepidle);
1516 /* Do window scaling? */
1517 if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1518 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1519 tp->snd_scale = tp->requested_s_scale;
1520 tp->rcv_scale = tp->request_r_scale;
1521 }
1522 tcp_reass_lock(tp);
1523 (void) tcp_reass(tp, (struct tcphdr *)0, (struct mbuf *)0,
1524 &tlen);
1525 tcp_reass_unlock(tp);
1526 tp->snd_wl1 = th->th_seq - 1;
1527 /* fall into ... */
1528
1529 /*
1530 * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1531 * ACKs. If the ack is in the range
1532 * tp->snd_una < th->th_ack <= tp->snd_max
1533 * then advance tp->snd_una to th->th_ack and drop
1534 * data from the retransmission queue. If this ACK reflects
1535 * more up to date window information we update our window information.
1536 */
1537 case TCPS_ESTABLISHED:
1538 case TCPS_FIN_WAIT_1:
1539 case TCPS_FIN_WAIT_2:
1540 case TCPS_CLOSE_WAIT:
1541 case TCPS_CLOSING:
1542 case TCPS_LAST_ACK:
1543 case TCPS_TIME_WAIT:
1544 #ifdef TCP_ECN
1545 /*
1546 * if we receive ECE and are not already in recovery phase,
1547 * reduce cwnd by half but don't slow-start.
1548 * advance snd_last to snd_max not to reduce cwnd again
1549 * until all outstanding packets are acked.
1550 */
1551 if (tcp_do_ecn && (tiflags & TH_ECE)) {
1552 if ((tp->t_flags & TF_ECN_PERMIT) &&
1553 SEQ_GEQ(tp->snd_una, tp->snd_last)) {
1554 u_int win;
1555
1556 win = min(tp->snd_wnd, tp->snd_cwnd) / tp->t_maxseg;
1557 if (win > 1) {
1558 tp->snd_ssthresh = win / 2 * tp->t_maxseg;
1559 tp->snd_cwnd = tp->snd_ssthresh;
1560 tp->snd_last = tp->snd_max;
1561 tp->t_flags |= TF_SEND_CWR;
1562 tcpstat.tcps_cwr_ecn++;
1563 }
1564 }
1565 tcpstat.tcps_ecn_rcvece++;
1566 }
1567 /*
1568 * if we receive CWR, we know that the peer has reduced
1569 * its congestion window. stop sending ecn-echo.
1570 */
1571 if ((tiflags & TH_CWR)) {
1572 tp->t_flags &= ~TF_RCVD_CE;
1573 tcpstat.tcps_ecn_rcvcwr++;
1574 }
1575 #endif /* TCP_ECN */
1576
1577 if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1578 /*
1579 * Duplicate/old ACK processing.
1580 * Increments t_dupacks:
1581 * Pure duplicate (same seq/ack/window, no data)
1582 * Doesn't affect t_dupacks:
1583 * Data packets.
1584 * Normal window updates (window opens)
1585 * Resets t_dupacks:
1586 * New data ACKed.
1587 * Window shrinks
1588 * Old ACK
1589 */
1590 if (tlen) {
1591 /* Drop very old ACKs unless th_seq matches */
1592 if (th->th_seq != tp->rcv_nxt &&
1593 SEQ_LT(th->th_ack,
1594 tp->snd_una - tp->max_sndwnd)) {
1595 tcpstat.tcps_rcvacktooold++;
1596 goto drop;
1597 }
1598 break;
1599 }
1600 /*
1601 * If we get an old ACK, there is probably packet
1602 * reordering going on. Be conservative and reset
1603 * t_dupacks so that we are less agressive in
1604 * doing a fast retransmit.
1605 */
1606 if (th->th_ack != tp->snd_una) {
1607 tp->t_dupacks = 0;
1608 break;
1609 }
1610 if (tiwin == tp->snd_wnd) {
1611 tcpstat.tcps_rcvdupack++;
1612 /*
1613 * If we have outstanding data (other than
1614 * a window probe), this is a completely
1615 * duplicate ack (ie, window info didn't
1616 * change), the ack is the biggest we've
1617 * seen and we've seen exactly our rexmt
1618 * threshhold of them, assume a packet
1619 * has been dropped and retransmit it.
1620 * Kludge snd_nxt & the congestion
1621 * window so we send only this one
1622 * packet.
1623 *
1624 * We know we're losing at the current
1625 * window size so do congestion avoidance
1626 * (set ssthresh to half the current window
1627 * and pull our congestion window back to
1628 * the new ssthresh).
1629 *
1630 * Dup acks mean that packets have left the
1631 * network (they're now cached at the receiver)
1632 * so bump cwnd by the amount in the receiver
1633 * to keep a constant cwnd packets in the
1634 * network.
1635 */
1636 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0)
1637 tp->t_dupacks = 0;
1638 #if defined(TCP_SACK) && defined(TCP_FACK)
1639 /*
1640 * In FACK, can enter fast rec. if the receiver
1641 * reports a reass. queue longer than 3 segs.
1642 */
1643 else if (++tp->t_dupacks == tcprexmtthresh ||
1644 ((SEQ_GT(tp->snd_fack, tcprexmtthresh *
1645 tp->t_maxseg + tp->snd_una)) &&
1646 SEQ_GT(tp->snd_una, tp->snd_last))) {
1647 #else
1648 else if (++tp->t_dupacks == tcprexmtthresh) {
1649 #endif /* TCP_FACK */
1650 tcp_seq onxt = tp->snd_nxt;
1651 u_long win =
1652 ulmin(tp->snd_wnd, tp->snd_cwnd) /
1653 2 / tp->t_maxseg;
1654
1655 #if defined(TCP_SACK) || defined(TCP_ECN)
1656 if (SEQ_LT(th->th_ack, tp->snd_last)){
1657 /*
1658 * False fast retx after
1659 * timeout. Do not cut window.
1660 */
1661 tp->t_dupacks = 0;
1662 goto drop;
1663 }
1664 #endif
1665 if (win < 2)
1666 win = 2;
1667 tp->snd_ssthresh = win * tp->t_maxseg;
1668 #if defined(TCP_SACK)
1669 tp->snd_last = tp->snd_max;
1670 #endif
1671 #ifdef TCP_SACK
1672 if (tp->sack_enable) {
1673 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1674 tp->t_rtttime = 0;
1675 #ifdef TCP_ECN
1676 tp->t_flags |= TF_SEND_CWR;
1677 #endif
1678 #if 1 /* TCP_ECN */
1679 tcpstat.tcps_cwr_frecovery++;
1680 #endif
1681 tcpstat.tcps_sndrexmitfast++;
1682 #if defined(TCP_SACK) && defined(TCP_FACK)
1683 tp->t_dupacks = tcprexmtthresh;
1684 (void) tcp_output(tp);
1685 /*
1686 * During FR, snd_cwnd is held
1687 * constant for FACK.
1688 */
1689 tp->snd_cwnd = tp->snd_ssthresh;
1690 #else
1691 /*
1692 * tcp_output() will send
1693 * oldest SACK-eligible rtx.
1694 */
1695 (void) tcp_output(tp);
1696 tp->snd_cwnd = tp->snd_ssthresh+
1697 tp->t_maxseg * tp->t_dupacks;
1698 #endif /* TCP_FACK */
1699 goto drop;
1700 }
1701 #endif /* TCP_SACK */
1702 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1703 tp->t_rtttime = 0;
1704 tp->snd_nxt = th->th_ack;
1705 tp->snd_cwnd = tp->t_maxseg;
1706 #ifdef TCP_ECN
1707 tp->t_flags |= TF_SEND_CWR;
1708 #endif
1709 #if 1 /* TCP_ECN */
1710 tcpstat.tcps_cwr_frecovery++;
1711 #endif
1712 tcpstat.tcps_sndrexmitfast++;
1713 (void) tcp_output(tp);
1714
1715 tp->snd_cwnd = tp->snd_ssthresh +
1716 tp->t_maxseg * tp->t_dupacks;
1717 if (SEQ_GT(onxt, tp->snd_nxt))
1718 tp->snd_nxt = onxt;
1719 goto drop;
1720 } else if (tp->t_dupacks > tcprexmtthresh) {
1721 #if defined(TCP_SACK) && defined(TCP_FACK)
1722 /*
1723 * while (awnd < cwnd)
1724 * sendsomething();
1725 */
1726 if (tp->sack_enable) {
1727 if (tp->snd_awnd < tp->snd_cwnd)
1728 tcp_output(tp);
1729 goto drop;
1730 }
1731 #endif /* TCP_FACK */
1732 tp->snd_cwnd += tp->t_maxseg;
1733 (void) tcp_output(tp);
1734 goto drop;
1735 }
1736 } else if (tiwin < tp->snd_wnd) {
1737 /*
1738 * The window was retracted! Previous dup
1739 * ACKs may have been due to packets arriving
1740 * after the shrunken window, not a missing
1741 * packet, so play it safe and reset t_dupacks
1742 */
1743 tp->t_dupacks = 0;
1744 }
1745 break;
1746 }
1747 /*
1748 * If the congestion window was inflated to account
1749 * for the other side's cached packets, retract it.
1750 */
1751 #if defined(TCP_SACK)
1752 if (tp->sack_enable) {
1753 if (tp->t_dupacks >= tcprexmtthresh) {
1754 /* Check for a partial ACK */
1755 if (tcp_sack_partialack(tp, th)) {
1756 #if defined(TCP_SACK) && defined(TCP_FACK)
1757 /* Force call to tcp_output */
1758 if (tp->snd_awnd < tp->snd_cwnd)
1759 needoutput = 1;
1760 #else
1761 tp->snd_cwnd += tp->t_maxseg;
1762 needoutput = 1;
1763 #endif /* TCP_FACK */
1764 } else {
1765 /* Out of fast recovery */
1766 tp->snd_cwnd = tp->snd_ssthresh;
1767 if (tcp_seq_subtract(tp->snd_max,
1768 th->th_ack) < tp->snd_ssthresh)
1769 tp->snd_cwnd =
1770 tcp_seq_subtract(tp->snd_max,
1771 th->th_ack);
1772 tp->t_dupacks = 0;
1773 #if defined(TCP_SACK) && defined(TCP_FACK)
1774 if (SEQ_GT(th->th_ack, tp->snd_fack))
1775 tp->snd_fack = th->th_ack;
1776 #endif /* TCP_FACK */
1777 }
1778 }
1779 } else {
1780 if (tp->t_dupacks >= tcprexmtthresh &&
1781 !tcp_newreno(tp, th)) {
1782 /* Out of fast recovery */
1783 tp->snd_cwnd = tp->snd_ssthresh;
1784 if (tcp_seq_subtract(tp->snd_max, th->th_ack) <
1785 tp->snd_ssthresh)
1786 tp->snd_cwnd =
1787 tcp_seq_subtract(tp->snd_max,
1788 th->th_ack);
1789 tp->t_dupacks = 0;
1790 }
1791 }
1792 if (tp->t_dupacks < tcprexmtthresh)
1793 tp->t_dupacks = 0;
1794 #else /* else no TCP_SACK */
1795 if (tp->t_dupacks >= tcprexmtthresh &&
1796 tp->snd_cwnd > tp->snd_ssthresh)
1797 tp->snd_cwnd = tp->snd_ssthresh;
1798 tp->t_dupacks = 0;
1799 #endif
1800 if (SEQ_GT(th->th_ack, tp->snd_max)) {
1801 tcpstat.tcps_rcvacktoomuch++;
1802 goto dropafterack_ratelim;
1803 }
1804 acked = th->th_ack - tp->snd_una;
1805 tcpstat.tcps_rcvackpack++;
1806 tcpstat.tcps_rcvackbyte += acked;
1807
1808 /*
1809 * If we have a timestamp reply, update smoothed
1810 * round trip time. If no timestamp is present but
1811 * transmit timer is running and timed sequence
1812 * number was acked, update smoothed round trip time.
1813 * Since we now have an rtt measurement, cancel the
1814 * timer backoff (cf., Phil Karn's retransmit alg.).
1815 * Recompute the initial retransmit timer.
1816 */
1817 if (opti.ts_present)
1818 tcp_xmit_timer(tp, tcp_now-opti.ts_ecr+1);
1819 else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq))
1820 tcp_xmit_timer(tp, tcp_now - tp->t_rtttime);
1821
1822 /*
1823 * If all outstanding data is acked, stop retransmit
1824 * timer and remember to restart (more output or persist).
1825 * If there is more data to be acked, restart retransmit
1826 * timer, using current (possibly backed-off) value.
1827 */
1828 if (th->th_ack == tp->snd_max) {
1829 TCP_TIMER_DISARM(tp, TCPT_REXMT);
1830 needoutput = 1;
1831 } else if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
1832 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1833 /*
1834 * When new data is acked, open the congestion window.
1835 * If the window gives us less than ssthresh packets
1836 * in flight, open exponentially (maxseg per packet).
1837 * Otherwise open linearly: maxseg per window
1838 * (maxseg^2 / cwnd per packet).
1839 */
1840 {
1841 u_int cw = tp->snd_cwnd;
1842 u_int incr = tp->t_maxseg;
1843
1844 if (cw > tp->snd_ssthresh)
1845 incr = incr * incr / cw;
1846 #if defined (TCP_SACK)
1847 if (tp->t_dupacks < tcprexmtthresh)
1848 #endif
1849 tp->snd_cwnd = ulmin(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1850 }
1851 ND6_HINT(tp);
1852 if (acked > so->so_snd.sb_cc) {
1853 tp->snd_wnd -= so->so_snd.sb_cc;
1854 sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1855 ourfinisacked = 1;
1856 } else {
1857 sbdrop(&so->so_snd, acked);
1858 tp->snd_wnd -= acked;
1859 ourfinisacked = 0;
1860 }
1861 if (sb_notify(&so->so_snd))
1862 sowwakeup(so);
1863
1864 /*
1865 * If we had a pending ICMP message that referred to data
1866 * that have just been acknowledged, disregard the recorded
1867 * ICMP message.
1868 */
1869 if ((tp->t_flags & TF_PMTUD_PEND) &&
1870 SEQ_GT(th->th_ack, tp->t_pmtud_th_seq))
1871 tp->t_flags &= ~TF_PMTUD_PEND;
1872
1873 /*
1874 * Keep track of the largest chunk of data acknowledged
1875 * since last PMTU update
1876 */
1877 if (tp->t_pmtud_mss_acked < acked)
1878 tp->t_pmtud_mss_acked = acked;
1879
1880 tp->snd_una = th->th_ack;
1881 #ifdef TCP_ECN
1882 /* sync snd_last with snd_una */
1883 if (SEQ_GT(tp->snd_una, tp->snd_last))
1884 tp->snd_last = tp->snd_una;
1885 #endif
1886 if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1887 tp->snd_nxt = tp->snd_una;
1888 #if defined (TCP_SACK) && defined (TCP_FACK)
1889 if (SEQ_GT(tp->snd_una, tp->snd_fack)) {
1890 tp->snd_fack = tp->snd_una;
1891 /* Update snd_awnd for partial ACK
1892 * without any SACK blocks.
1893 */
1894 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt,
1895 tp->snd_fack) + tp->retran_data;
1896 }
1897 #endif
1898
1899 switch (tp->t_state) {
1900
1901 /*
1902 * In FIN_WAIT_1 STATE in addition to the processing
1903 * for the ESTABLISHED state if our FIN is now acknowledged
1904 * then enter FIN_WAIT_2.
1905 */
1906 case TCPS_FIN_WAIT_1:
1907 if (ourfinisacked) {
1908 /*
1909 * If we can't receive any more
1910 * data, then closing user can proceed.
1911 * Starting the timer is contrary to the
1912 * specification, but if we don't get a FIN
1913 * we'll hang forever.
1914 */
1915 if (so->so_state & SS_CANTRCVMORE) {
1916 soisdisconnected(so);
1917 TCP_TIMER_ARM(tp, TCPT_2MSL, tcp_maxidle);
1918 }
1919 tp->t_state = TCPS_FIN_WAIT_2;
1920 }
1921 break;
1922
1923 /*
1924 * In CLOSING STATE in addition to the processing for
1925 * the ESTABLISHED state if the ACK acknowledges our FIN
1926 * then enter the TIME-WAIT state, otherwise ignore
1927 * the segment.
1928 */
1929 case TCPS_CLOSING:
1930 if (ourfinisacked) {
1931 tp->t_state = TCPS_TIME_WAIT;
1932 tcp_canceltimers(tp);
1933 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1934 soisdisconnected(so);
1935 }
1936 break;
1937
1938 /*
1939 * In LAST_ACK, we may still be waiting for data to drain
1940 * and/or to be acked, as well as for the ack of our FIN.
1941 * If our FIN is now acknowledged, delete the TCB,
1942 * enter the closed state and return.
1943 */
1944 case TCPS_LAST_ACK:
1945 if (ourfinisacked) {
1946 tp = tcp_close(tp);
1947 goto drop;
1948 }
1949 break;
1950
1951 /*
1952 * In TIME_WAIT state the only thing that should arrive
1953 * is a retransmission of the remote FIN. Acknowledge
1954 * it and restart the finack timer.
1955 */
1956 case TCPS_TIME_WAIT:
1957 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
1958 goto dropafterack;
1959 }
1960 }
1961
1962 step6:
1963 /*
1964 * Update window information.
1965 * Don't look at window if no ACK: TAC's send garbage on first SYN.
1966 */
1967 if ((tiflags & TH_ACK) && (SEQ_LT(tp->snd_wl1, th->th_seq) ||
1968 (tp->snd_wl1 == th->th_seq && SEQ_LT(tp->snd_wl2, th->th_ack)) ||
1969 (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))) {
1970 /* keep track of pure window updates */
1971 if (tlen == 0 &&
1972 tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
1973 tcpstat.tcps_rcvwinupd++;
1974 tp->snd_wnd = tiwin;
1975 tp->snd_wl1 = th->th_seq;
1976 tp->snd_wl2 = th->th_ack;
1977 if (tp->snd_wnd > tp->max_sndwnd)
1978 tp->max_sndwnd = tp->snd_wnd;
1979 needoutput = 1;
1980 }
1981
1982 /*
1983 * Process segments with URG.
1984 */
1985 if ((tiflags & TH_URG) && th->th_urp &&
1986 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1987 /*
1988 * This is a kludge, but if we receive and accept
1989 * random urgent pointers, we'll crash in
1990 * soreceive. It's hard to imagine someone
1991 * actually wanting to send this much urgent data.
1992 */
1993 if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
1994 th->th_urp = 0; /* XXX */
1995 tiflags &= ~TH_URG; /* XXX */
1996 goto dodata; /* XXX */
1997 }
1998 /*
1999 * If this segment advances the known urgent pointer,
2000 * then mark the data stream. This should not happen
2001 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2002 * a FIN has been received from the remote side.
2003 * In these states we ignore the URG.
2004 *
2005 * According to RFC961 (Assigned Protocols),
2006 * the urgent pointer points to the last octet
2007 * of urgent data. We continue, however,
2008 * to consider it to indicate the first octet
2009 * of data past the urgent section as the original
2010 * spec states (in one of two places).
2011 */
2012 if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2013 tp->rcv_up = th->th_seq + th->th_urp;
2014 so->so_oobmark = so->so_rcv.sb_cc +
2015 (tp->rcv_up - tp->rcv_nxt) - 1;
2016 if (so->so_oobmark == 0)
2017 so->so_state |= SS_RCVATMARK;
2018 sohasoutofband(so);
2019 tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2020 }
2021 /*
2022 * Remove out of band data so doesn't get presented to user.
2023 * This can happen independent of advancing the URG pointer,
2024 * but if two URG's are pending at once, some out-of-band
2025 * data may creep in... ick.
2026 */
2027 if (th->th_urp <= (u_int16_t) tlen
2028 #ifdef SO_OOBINLINE
2029 && (so->so_options & SO_OOBINLINE) == 0
2030 #endif
2031 )
2032 tcp_pulloutofband(so, th->th_urp, m, hdroptlen);
2033 } else
2034 /*
2035 * If no out of band data is expected,
2036 * pull receive urgent pointer along
2037 * with the receive window.
2038 */
2039 if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2040 tp->rcv_up = tp->rcv_nxt;
2041 dodata: /* XXX */
2042
2043 /*
2044 * Process the segment text, merging it into the TCP sequencing queue,
2045 * and arranging for acknowledgment of receipt if necessary.
2046 * This process logically involves adjusting tp->rcv_wnd as data
2047 * is presented to the user (this happens in tcp_usrreq.c,
2048 * case PRU_RCVD). If a FIN has already been received on this
2049 * connection then we just ignore the text.
2050 */
2051 if ((tlen || (tiflags & TH_FIN)) &&
2052 TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2053 tcp_reass_lock(tp);
2054 if (th->th_seq == tp->rcv_nxt && tp->segq.lh_first == NULL &&
2055 tp->t_state == TCPS_ESTABLISHED) {
2056 tcp_reass_unlock(tp);
2057 TCP_SETUP_ACK(tp, tiflags);
2058 tp->rcv_nxt += tlen;
2059 tiflags = th->th_flags & TH_FIN;
2060 tcpstat.tcps_rcvpack++;
2061 tcpstat.tcps_rcvbyte += tlen;
2062 ND6_HINT(tp);
2063 if (so->so_state & SS_CANTRCVMORE)
2064 m_freem(m);
2065 else {
2066 m_adj(m, hdroptlen);
2067 sbappendstream(&so->so_rcv, m);
2068 }
2069 sorwakeup(so);
2070 } else {
2071 m_adj(m, hdroptlen);
2072 tiflags = tcp_reass(tp, th, m, &tlen);
2073 tcp_reass_unlock(tp);
2074 tp->t_flags |= TF_ACKNOW;
2075 }
2076 #ifdef TCP_SACK
2077 if (tp->sack_enable)
2078 tcp_update_sack_list(tp);
2079 #endif
2080
2081 /*
2082 * variable len never referenced again in modern BSD,
2083 * so why bother computing it ??
2084 */
2085 #if 0
2086 /*
2087 * Note the amount of data that peer has sent into
2088 * our window, in order to estimate the sender's
2089 * buffer size.
2090 */
2091 len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2092 #endif /* 0 */
2093 } else {
2094 m_freem(m);
2095 tiflags &= ~TH_FIN;
2096 }
2097
2098 /*
2099 * If FIN is received ACK the FIN and let the user know
2100 * that the connection is closing. Ignore a FIN received before
2101 * the connection is fully established.
2102 */
2103 if ((tiflags & TH_FIN) && TCPS_HAVEESTABLISHED(tp->t_state)) {
2104 if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2105 socantrcvmore(so);
2106 tp->t_flags |= TF_ACKNOW;
2107 tp->rcv_nxt++;
2108 }
2109 switch (tp->t_state) {
2110
2111 /*
2112 * In ESTABLISHED STATE enter the CLOSE_WAIT state.
2113 */
2114 case TCPS_ESTABLISHED:
2115 tp->t_state = TCPS_CLOSE_WAIT;
2116 break;
2117
2118 /*
2119 * If still in FIN_WAIT_1 STATE FIN has not been acked so
2120 * enter the CLOSING state.
2121 */
2122 case TCPS_FIN_WAIT_1:
2123 tp->t_state = TCPS_CLOSING;
2124 break;
2125
2126 /*
2127 * In FIN_WAIT_2 state enter the TIME_WAIT state,
2128 * starting the time-wait timer, turning off the other
2129 * standard timers.
2130 */
2131 case TCPS_FIN_WAIT_2:
2132 tp->t_state = TCPS_TIME_WAIT;
2133 tcp_canceltimers(tp);
2134 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2135 soisdisconnected(so);
2136 break;
2137
2138 /*
2139 * In TIME_WAIT state restart the 2 MSL time_wait timer.
2140 */
2141 case TCPS_TIME_WAIT:
2142 TCP_TIMER_ARM(tp, TCPT_2MSL, 2 * TCPTV_MSL);
2143 break;
2144 }
2145 }
2146 if (so->so_options & SO_DEBUG) {
2147 switch (tp->pf) {
2148 #ifdef INET6
2149 case PF_INET6:
2150 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti6,
2151 0, tlen);
2152 break;
2153 #endif /* INET6 */
2154 case PF_INET:
2155 tcp_trace(TA_INPUT, ostate, tp, (caddr_t) &tcp_saveti,
2156 0, tlen);
2157 break;
2158 }
2159 }
2160
2161 /*
2162 * Return any desired output.
2163 */
2164 if (needoutput || (tp->t_flags & TF_ACKNOW)) {
2165 (void) tcp_output(tp);
2166 }
2167 return;
2168
2169 badsyn:
2170 /*
2171 * Received a bad SYN. Increment counters and dropwithreset.
2172 */
2173 tcpstat.tcps_badsyn++;
2174 tp = NULL;
2175 goto dropwithreset;
2176
2177 dropafterack_ratelim:
2178 if (ppsratecheck(&tcp_ackdrop_ppslim_last, &tcp_ackdrop_ppslim_count,
2179 tcp_ackdrop_ppslim) == 0) {
2180 /* XXX stat */
2181 goto drop;
2182 }
2183 /* ...fall into dropafterack... */
2184
2185 dropafterack:
2186 /*
2187 * Generate an ACK dropping incoming segment if it occupies
2188 * sequence space, where the ACK reflects our state.
2189 */
2190 if (tiflags & TH_RST)
2191 goto drop;
2192 m_freem(m);
2193 tp->t_flags |= TF_ACKNOW;
2194 (void) tcp_output(tp);
2195 return;
2196
2197 dropwithreset_ratelim:
2198 /*
2199 * We may want to rate-limit RSTs in certain situations,
2200 * particularly if we are sending an RST in response to
2201 * an attempt to connect to or otherwise communicate with
2202 * a port for which we have no socket.
2203 */
2204 if (ppsratecheck(&tcp_rst_ppslim_last, &tcp_rst_ppslim_count,
2205 tcp_rst_ppslim) == 0) {
2206 /* XXX stat */
2207 goto drop;
2208 }
2209 /* ...fall into dropwithreset... */
2210
2211 dropwithreset:
2212 /*
2213 * Generate a RST, dropping incoming segment.
2214 * Make ACK acceptable to originator of segment.
2215 * Don't bother to respond to RST.
2216 */
2217 if (tiflags & TH_RST)
2218 goto drop;
2219 if (tiflags & TH_ACK) {
2220 tcp_respond(tp, mtod(m, caddr_t), th, (tcp_seq)0, th->th_ack,
2221 TH_RST);
2222 } else {
2223 if (tiflags & TH_SYN)
2224 tlen++;
2225 tcp_respond(tp, mtod(m, caddr_t), th, th->th_seq + tlen,
2226 (tcp_seq)0, TH_RST|TH_ACK);
2227 }
2228 m_freem(m);
2229 return;
2230
2231 drop:
2232 /*
2233 * Drop space held by incoming segment and return.
2234 */
2235 if (tp && (tp->t_inpcb->inp_socket->so_options & SO_DEBUG)) {
2236 switch (tp->pf) {
2237 #ifdef INET6
2238 case PF_INET6:
2239 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti6,
2240 0, tlen);
2241 break;
2242 #endif /* INET6 */
2243 case PF_INET:
2244 tcp_trace(TA_DROP, ostate, tp, (caddr_t) &tcp_saveti,
2245 0, tlen);
2246 break;
2247 }
2248 }
2249
2250 m_freem(m);
2251 return;
2252 #ifndef TUBA_INCLUDE
2253 }
2254
2255 int
2256 tcp_dooptions(tp, cp, cnt, th, m, iphlen, oi)
2257 struct tcpcb *tp;
2258 u_char *cp;
2259 int cnt;
2260 struct tcphdr *th;
2261 struct mbuf *m;
2262 int iphlen;
2263 struct tcp_opt_info *oi;
2264 {
2265 u_int16_t mss = 0;
2266 int opt, optlen;
2267 #ifdef TCP_SIGNATURE
2268 caddr_t sigp = NULL;
2269 struct tdb *tdb = NULL;
2270 #endif /* TCP_SIGNATURE */
2271
2272 #ifdef TCP_SIGNATURE
2273 if (cp)
2274 #endif /* TCP_SIGNATURE */
2275 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2276 opt = cp[0];
2277 if (opt == TCPOPT_EOL)
2278 break;
2279 if (opt == TCPOPT_NOP)
2280 optlen = 1;
2281 else {
2282 if (cnt < 2)
2283 break;
2284 optlen = cp[1];
2285 if (optlen < 2 || optlen > cnt)
2286 break;
2287 }
2288 switch (opt) {
2289
2290 default:
2291 continue;
2292
2293 case TCPOPT_MAXSEG:
2294 if (optlen != TCPOLEN_MAXSEG)
2295 continue;
2296 if (!(th->th_flags & TH_SYN))
2297 continue;
2298 bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
2299 NTOHS(mss);
2300 oi->maxseg = mss;
2301 break;
2302
2303 case TCPOPT_WINDOW:
2304 if (optlen != TCPOLEN_WINDOW)
2305 continue;
2306 if (!(th->th_flags & TH_SYN))
2307 continue;
2308 tp->t_flags |= TF_RCVD_SCALE;
2309 tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2310 break;
2311
2312 case TCPOPT_TIMESTAMP:
2313 if (optlen != TCPOLEN_TIMESTAMP)
2314 continue;
2315 oi->ts_present = 1;
2316 bcopy(cp + 2, &oi->ts_val, sizeof(oi->ts_val));
2317 NTOHL(oi->ts_val);
2318 bcopy(cp + 6, &oi->ts_ecr, sizeof(oi->ts_ecr));
2319 NTOHL(oi->ts_ecr);
2320
2321 /*
2322 * A timestamp received in a SYN makes
2323 * it ok to send timestamp requests and replies.
2324 */
2325 if (th->th_flags & TH_SYN) {
2326 tp->t_flags |= TF_RCVD_TSTMP;
2327 tp->ts_recent = oi->ts_val;
2328 tp->ts_recent_age = tcp_now;
2329 }
2330 break;
2331
2332 #ifdef TCP_SACK
2333 case TCPOPT_SACK_PERMITTED:
2334 if (!tp->sack_enable || optlen!=TCPOLEN_SACK_PERMITTED)
2335 continue;
2336 if (th->th_flags & TH_SYN)
2337 /* MUST only be set on SYN */
2338 tp->t_flags |= TF_SACK_PERMIT;
2339 break;
2340 case TCPOPT_SACK:
2341 tcp_sack_option(tp, th, cp, optlen);
2342 break;
2343 #endif
2344 #ifdef TCP_SIGNATURE
2345 case TCPOPT_SIGNATURE:
2346 if (optlen != TCPOLEN_SIGNATURE)
2347 continue;
2348
2349 if (sigp && bcmp(sigp, cp + 2, 16))
2350 return (-1);
2351
2352 sigp = cp + 2;
2353 break;
2354 #endif /* TCP_SIGNATURE */
2355 }
2356 }
2357
2358 #ifdef TCP_SIGNATURE
2359 if (tp->t_flags & TF_SIGNATURE) {
2360 union sockaddr_union src, dst;
2361
2362 memset(&src, 0, sizeof(union sockaddr_union));
2363 memset(&dst, 0, sizeof(union sockaddr_union));
2364
2365 switch (tp->pf) {
2366 case 0:
2367 #ifdef INET
2368 case AF_INET:
2369 src.sa.sa_len = sizeof(struct sockaddr_in);
2370 src.sa.sa_family = AF_INET;
2371 src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
2372 dst.sa.sa_len = sizeof(struct sockaddr_in);
2373 dst.sa.sa_family = AF_INET;
2374 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
2375 break;
2376 #endif
2377 #ifdef INET6
2378 case AF_INET6:
2379 src.sa.sa_len = sizeof(struct sockaddr_in6);
2380 src.sa.sa_family = AF_INET6;
2381 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
2382 dst.sa.sa_len = sizeof(struct sockaddr_in6);
2383 dst.sa.sa_family = AF_INET6;
2384 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
2385 break;
2386 #endif /* INET6 */
2387 }
2388
2389 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP);
2390
2391 /*
2392 * We don't have an SA for this peer, so we turn off
2393 * TF_SIGNATURE on the listen socket
2394 */
2395 if (tdb == NULL && tp->t_state == TCPS_LISTEN)
2396 tp->t_flags &= ~TF_SIGNATURE;
2397
2398 }
2399
2400 if ((sigp ? TF_SIGNATURE : 0) ^ (tp->t_flags & TF_SIGNATURE)) {
2401 tcpstat.tcps_rcvbadsig++;
2402 return (-1);
2403 }
2404
2405 if (sigp) {
2406 MD5_CTX ctx;
2407 char sig[16];
2408
2409 if (tdb == NULL) {
2410 tcpstat.tcps_rcvbadsig++;
2411 return (-1);
2412 }
2413
2414 MD5Init(&ctx);
2415
2416 switch(tp->pf) {
2417 case 0:
2418 #ifdef INET
2419 case AF_INET:
2420 {
2421 struct ippseudo ippseudo;
2422
2423 ippseudo.ippseudo_src =
2424 mtod(m, struct ip *)->ip_src;
2425 ippseudo.ippseudo_dst =
2426 mtod(m, struct ip *)->ip_dst;
2427 ippseudo.ippseudo_pad = 0;
2428 ippseudo.ippseudo_p = IPPROTO_TCP;
2429 ippseudo.ippseudo_len = htons(
2430 m->m_pkthdr.len - iphlen);
2431
2432 MD5Update(&ctx, (char *)&ippseudo,
2433 sizeof(struct ippseudo));
2434 }
2435 break;
2436 #endif /* INET */
2437 #ifdef INET6
2438 case AF_INET6:
2439 {
2440 struct ip6_hdr_pseudo ip6pseudo;
2441
2442 bzero(&ip6pseudo, sizeof(ip6pseudo));
2443 ip6pseudo.ip6ph_src =
2444 mtod(m, struct ip6_hdr *)->ip6_src;
2445 ip6pseudo.ip6ph_dst =
2446 mtod(m, struct ip6_hdr *)->ip6_dst;
2447 in6_clearscope(&ip6pseudo.ip6ph_src);
2448 in6_clearscope(&ip6pseudo.ip6ph_dst);
2449 ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
2450 ip6pseudo.ip6ph_len = htonl(m->m_pkthdr.len -
2451 iphlen);
2452
2453 MD5Update(&ctx, (char *)&ip6pseudo,
2454 sizeof(ip6pseudo));
2455 }
2456 break;
2457 #endif /* INET6 */
2458 }
2459
2460 {
2461 struct tcphdr tcphdr;
2462
2463 tcphdr.th_sport = th->th_sport;
2464 tcphdr.th_dport = th->th_dport;
2465 tcphdr.th_seq = htonl(th->th_seq);
2466 tcphdr.th_ack = htonl(th->th_ack);
2467 tcphdr.th_off = th->th_off;
2468 tcphdr.th_x2 = th->th_x2;
2469 tcphdr.th_flags = th->th_flags;
2470 tcphdr.th_win = htons(th->th_win);
2471 tcphdr.th_sum = 0;
2472 tcphdr.th_urp = htons(th->th_urp);
2473
2474 MD5Update(&ctx, (char *)&tcphdr,
2475 sizeof(struct tcphdr));
2476 }
2477
2478 if (m_apply(m, iphlen + th->th_off * sizeof(uint32_t),
2479 m->m_pkthdr.len - (iphlen + th->th_off * sizeof(uint32_t)),
2480 tcp_signature_apply, (caddr_t)&ctx))
2481 return (-1);
2482
2483 MD5Update(&ctx, tdb->tdb_amxkey, tdb->tdb_amxkeylen);
2484 MD5Final(sig, &ctx);
2485
2486 if (bcmp(sig, sigp, 16)) {
2487 tcpstat.tcps_rcvbadsig++;
2488 return (-1);
2489 }
2490
2491 tcpstat.tcps_rcvgoodsig++;
2492 }
2493 #endif /* TCP_SIGNATURE */
2494
2495 return (0);
2496 }
2497
2498 #if defined(TCP_SACK)
2499 u_long
2500 tcp_seq_subtract(a, b)
2501 u_long a, b;
2502 {
2503 return ((long)(a - b));
2504 }
2505 #endif
2506
2507
2508 #ifdef TCP_SACK
2509 /*
2510 * This function is called upon receipt of new valid data (while not in header
2511 * prediction mode), and it updates the ordered list of sacks.
2512 */
2513 void
2514 tcp_update_sack_list(tp)
2515 struct tcpcb *tp;
2516 {
2517 /*
2518 * First reported block MUST be the most recent one. Subsequent
2519 * blocks SHOULD be in the order in which they arrived at the
2520 * receiver. These two conditions make the implementation fully
2521 * compliant with RFC 2018.
2522 */
2523 int i, j = 0, count = 0, lastpos = -1;
2524 struct sackblk sack, firstsack, temp[MAX_SACK_BLKS];
2525
2526 /* First clean up current list of sacks */
2527 for (i = 0; i < tp->rcv_numsacks; i++) {
2528 sack = tp->sackblks[i];
2529 if (sack.start == 0 && sack.end == 0) {
2530 count++; /* count = number of blocks to be discarded */
2531 continue;
2532 }
2533 if (SEQ_LEQ(sack.end, tp->rcv_nxt)) {
2534 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2535 count++;
2536 } else {
2537 temp[j].start = tp->sackblks[i].start;
2538 temp[j++].end = tp->sackblks[i].end;
2539 }
2540 }
2541 tp->rcv_numsacks -= count;
2542 if (tp->rcv_numsacks == 0) { /* no sack blocks currently (fast path) */
2543 tcp_clean_sackreport(tp);
2544 if (SEQ_LT(tp->rcv_nxt, tp->rcv_laststart)) {
2545 /* ==> need first sack block */
2546 tp->sackblks[0].start = tp->rcv_laststart;
2547 tp->sackblks[0].end = tp->rcv_lastend;
2548 tp->rcv_numsacks = 1;
2549 }
2550 return;
2551 }
2552 /* Otherwise, sack blocks are already present. */
2553 for (i = 0; i < tp->rcv_numsacks; i++)
2554 tp->sackblks[i] = temp[i]; /* first copy back sack list */
2555 if (SEQ_GEQ(tp->rcv_nxt, tp->rcv_lastend))
2556 return; /* sack list remains unchanged */
2557 /*
2558 * From here, segment just received should be (part of) the 1st sack.
2559 * Go through list, possibly coalescing sack block entries.
2560 */
2561 firstsack.start = tp->rcv_laststart;
2562 firstsack.end = tp->rcv_lastend;
2563 for (i = 0; i < tp->rcv_numsacks; i++) {
2564 sack = tp->sackblks[i];
2565 if (SEQ_LT(sack.end, firstsack.start) ||
2566 SEQ_GT(sack.start, firstsack.end))
2567 continue; /* no overlap */
2568 if (sack.start == firstsack.start && sack.end == firstsack.end){
2569 /*
2570 * identical block; delete it here since we will
2571 * move it to the front of the list.
2572 */
2573 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2574 lastpos = i; /* last posn with a zero entry */
2575 continue;
2576 }
2577 if (SEQ_LEQ(sack.start, firstsack.start))
2578 firstsack.start = sack.start; /* merge blocks */
2579 if (SEQ_GEQ(sack.end, firstsack.end))
2580 firstsack.end = sack.end; /* merge blocks */
2581 tp->sackblks[i].start = tp->sackblks[i].end = 0;
2582 lastpos = i; /* last posn with a zero entry */
2583 }
2584 if (lastpos != -1) { /* at least one merge */
2585 for (i = 0, j = 1; i < tp->rcv_numsacks; i++) {
2586 sack = tp->sackblks[i];
2587 if (sack.start == 0 && sack.end == 0)
2588 continue;
2589 temp[j++] = sack;
2590 }
2591 tp->rcv_numsacks = j; /* including first blk (added later) */
2592 for (i = 1; i < tp->rcv_numsacks; i++) /* now copy back */
2593 tp->sackblks[i] = temp[i];
2594 } else { /* no merges -- shift sacks by 1 */
2595 if (tp->rcv_numsacks < MAX_SACK_BLKS)
2596 tp->rcv_numsacks++;
2597 for (i = tp->rcv_numsacks-1; i > 0; i--)
2598 tp->sackblks[i] = tp->sackblks[i-1];
2599 }
2600 tp->sackblks[0] = firstsack;
2601 return;
2602 }
2603
2604 /*
2605 * Process the TCP SACK option. tp->snd_holes is an ordered list
2606 * of holes (oldest to newest, in terms of the sequence space).
2607 */
2608 void
2609 tcp_sack_option(struct tcpcb *tp, struct tcphdr *th, u_char *cp, int optlen)
2610 {
2611 int tmp_olen;
2612 u_char *tmp_cp;
2613 struct sackhole *cur, *p, *temp;
2614
2615 if (!tp->sack_enable)
2616 return;
2617 /* SACK without ACK doesn't make sense. */
2618 if ((th->th_flags & TH_ACK) == 0)
2619 return;
2620 /* Make sure the ACK on this segment is in [snd_una, snd_max]. */
2621 if (SEQ_LT(th->th_ack, tp->snd_una) ||
2622 SEQ_GT(th->th_ack, tp->snd_max))
2623 return;
2624 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */
2625 if (optlen <= 2 || (optlen - 2) % TCPOLEN_SACK != 0)
2626 return;
2627 /* Note: TCPOLEN_SACK must be 2*sizeof(tcp_seq) */
2628 tmp_cp = cp + 2;
2629 tmp_olen = optlen - 2;
2630 if (tp->snd_numholes < 0)
2631 tp->snd_numholes = 0;
2632 if (tp->t_maxseg == 0)
2633 panic("tcp_sack_option"); /* Should never happen */
2634 while (tmp_olen > 0) {
2635 struct sackblk sack;
2636
2637 bcopy(tmp_cp, (char *) &(sack.start), sizeof(tcp_seq));
2638 NTOHL(sack.start);
2639 bcopy(tmp_cp + sizeof(tcp_seq),
2640 (char *) &(sack.end), sizeof(tcp_seq));
2641 NTOHL(sack.end);
2642 tmp_olen -= TCPOLEN_SACK;
2643 tmp_cp += TCPOLEN_SACK;
2644 if (SEQ_LEQ(sack.end, sack.start))
2645 continue; /* bad SACK fields */
2646 if (SEQ_LEQ(sack.end, tp->snd_una))
2647 continue; /* old block */
2648 #if defined(TCP_SACK) && defined(TCP_FACK)
2649 /* Updates snd_fack. */
2650 if (SEQ_GT(sack.end, tp->snd_fack))
2651 tp->snd_fack = sack.end;
2652 #endif /* TCP_FACK */
2653 if (SEQ_GT(th->th_ack, tp->snd_una)) {
2654 if (SEQ_LT(sack.start, th->th_ack))
2655 continue;
2656 }
2657 if (SEQ_GT(sack.end, tp->snd_max))
2658 continue;
2659 if (tp->snd_holes == NULL) { /* first hole */
2660 tp->snd_holes = (struct sackhole *)
2661 pool_get(&sackhl_pool, PR_NOWAIT);
2662 if (tp->snd_holes == NULL) {
2663 /* ENOBUFS, so ignore SACKed block for now*/
2664 goto done;
2665 }
2666 cur = tp->snd_holes;
2667 cur->start = th->th_ack;
2668 cur->end = sack.start;
2669 cur->rxmit = cur->start;
2670 cur->next = NULL;
2671 tp->snd_numholes = 1;
2672 tp->rcv_lastsack = sack.end;
2673 /*
2674 * dups is at least one. If more data has been
2675 * SACKed, it can be greater than one.
2676 */
2677 cur->dups = min(tcprexmtthresh,
2678 ((sack.end - cur->end)/tp->t_maxseg));
2679 if (cur->dups < 1)
2680 cur->dups = 1;
2681 continue; /* with next sack block */
2682 }
2683 /* Go thru list of holes: p = previous, cur = current */
2684 p = cur = tp->snd_holes;
2685 while (cur) {
2686 if (SEQ_LEQ(sack.end, cur->start))
2687 /* SACKs data before the current hole */
2688 break; /* no use going through more holes */
2689 if (SEQ_GEQ(sack.start, cur->end)) {
2690 /* SACKs data beyond the current hole */
2691 cur->dups++;
2692 if (((sack.end - cur->end)/tp->t_maxseg) >=
2693 tcprexmtthresh)
2694 cur->dups = tcprexmtthresh;
2695 p = cur;
2696 cur = cur->next;
2697 continue;
2698 }
2699 if (SEQ_LEQ(sack.start, cur->start)) {
2700 /* Data acks at least the beginning of hole */
2701 #if defined(TCP_SACK) && defined(TCP_FACK)
2702 if (SEQ_GT(sack.end, cur->rxmit))
2703 tp->retran_data -=
2704 tcp_seq_subtract(cur->rxmit,
2705 cur->start);
2706 else
2707 tp->retran_data -=
2708 tcp_seq_subtract(sack.end,
2709 cur->start);
2710 #endif /* TCP_FACK */
2711 if (SEQ_GEQ(sack.end, cur->end)) {
2712 /* Acks entire hole, so delete hole */
2713 if (p != cur) {
2714 p->next = cur->next;
2715 pool_put(&sackhl_pool, cur);
2716 cur = p->next;
2717 } else {
2718 cur = cur->next;
2719 pool_put(&sackhl_pool, p);
2720 p = cur;
2721 tp->snd_holes = p;
2722 }
2723 tp->snd_numholes--;
2724 continue;
2725 }
2726 /* otherwise, move start of hole forward */
2727 cur->start = sack.end;
2728 cur->rxmit = SEQ_MAX(cur->rxmit, cur->start);
2729 p = cur;
2730 cur = cur->next;
2731 continue;
2732 }
2733 /* move end of hole backward */
2734 if (SEQ_GEQ(sack.end, cur->end)) {
2735 #if defined(TCP_SACK) && defined(TCP_FACK)
2736 if (SEQ_GT(cur->rxmit, sack.start))
2737 tp->retran_data -=
2738 tcp_seq_subtract(cur->rxmit,
2739 sack.start);
2740 #endif /* TCP_FACK */
2741 cur->end = sack.start;
2742 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
2743 cur->dups++;
2744 if (((sack.end - cur->end)/tp->t_maxseg) >=
2745 tcprexmtthresh)
2746 cur->dups = tcprexmtthresh;
2747 p = cur;
2748 cur = cur->next;
2749 continue;
2750 }
2751 if (SEQ_LT(cur->start, sack.start) &&
2752 SEQ_GT(cur->end, sack.end)) {
2753 /*
2754 * ACKs some data in middle of a hole; need to
2755 * split current hole
2756 */
2757 temp = (struct sackhole *)
2758 pool_get(&sackhl_pool, PR_NOWAIT);
2759 if (temp == NULL)
2760 goto done; /* ENOBUFS */
2761 #if defined(TCP_SACK) && defined(TCP_FACK)
2762 if (SEQ_GT(cur->rxmit, sack.end))
2763 tp->retran_data -=
2764 tcp_seq_subtract(sack.end,
2765 sack.start);
2766 else if (SEQ_GT(cur->rxmit, sack.start))
2767 tp->retran_data -=
2768 tcp_seq_subtract(cur->rxmit,
2769 sack.start);
2770 #endif /* TCP_FACK */
2771 temp->next = cur->next;
2772 temp->start = sack.end;
2773 temp->end = cur->end;
2774 temp->dups = cur->dups;
2775 temp->rxmit = SEQ_MAX(cur->rxmit, temp->start);
2776 cur->end = sack.start;
2777 cur->rxmit = SEQ_MIN(cur->rxmit, cur->end);
2778 cur->dups++;
2779 if (((sack.end - cur->end)/tp->t_maxseg) >=
2780 tcprexmtthresh)
2781 cur->dups = tcprexmtthresh;
2782 cur->next = temp;
2783 p = temp;
2784 cur = p->next;
2785 tp->snd_numholes++;
2786 }
2787 }
2788 /* At this point, p points to the last hole on the list */
2789 if (SEQ_LT(tp->rcv_lastsack, sack.start)) {
2790 /*
2791 * Need to append new hole at end.
2792 * Last hole is p (and it's not NULL).
2793 */
2794 temp = (struct sackhole *)
2795 pool_get(&sackhl_pool, PR_NOWAIT);
2796 if (temp == NULL)
2797 goto done; /* ENOBUFS */
2798 temp->start = tp->rcv_lastsack;
2799 temp->end = sack.start;
2800 temp->dups = min(tcprexmtthresh,
2801 ((sack.end - sack.start)/tp->t_maxseg));
2802 if (temp->dups < 1)
2803 temp->dups = 1;
2804 temp->rxmit = temp->start;
2805 temp->next = 0;
2806 p->next = temp;
2807 tp->rcv_lastsack = sack.end;
2808 tp->snd_numholes++;
2809 }
2810 }
2811 done:
2812 #if defined(TCP_SACK) && defined(TCP_FACK)
2813 /*
2814 * Update retran_data and snd_awnd. Go through the list of
2815 * holes. Increment retran_data by (hole->rxmit - hole->start).
2816 */
2817 tp->retran_data = 0;
2818 cur = tp->snd_holes;
2819 while (cur) {
2820 tp->retran_data += cur->rxmit - cur->start;
2821 cur = cur->next;
2822 }
2823 tp->snd_awnd = tcp_seq_subtract(tp->snd_nxt, tp->snd_fack) +
2824 tp->retran_data;
2825 #endif /* TCP_FACK */
2826
2827 return;
2828 }
2829
2830 /*
2831 * Delete stale (i.e, cumulatively ack'd) holes. Hole is deleted only if
2832 * it is completely acked; otherwise, tcp_sack_option(), called from
2833 * tcp_dooptions(), will fix up the hole.
2834 */
2835 void
2836 tcp_del_sackholes(tp, th)
2837 struct tcpcb *tp;
2838 struct tcphdr *th;
2839 {
2840 if (tp->sack_enable && tp->t_state != TCPS_LISTEN) {
2841 /* max because this could be an older ack just arrived */
2842 tcp_seq lastack = SEQ_GT(th->th_ack, tp->snd_una) ?
2843 th->th_ack : tp->snd_una;
2844 struct sackhole *cur = tp->snd_holes;
2845 struct sackhole *prev;
2846 while (cur)
2847 if (SEQ_LEQ(cur->end, lastack)) {
2848 prev = cur;
2849 cur = cur->next;
2850 pool_put(&sackhl_pool, prev);
2851 tp->snd_numholes--;
2852 } else if (SEQ_LT(cur->start, lastack)) {
2853 cur->start = lastack;
2854 if (SEQ_LT(cur->rxmit, cur->start))
2855 cur->rxmit = cur->start;
2856 break;
2857 } else
2858 break;
2859 tp->snd_holes = cur;
2860 }
2861 }
2862
2863 /*
2864 * Delete all receiver-side SACK information.
2865 */
2866 void
2867 tcp_clean_sackreport(tp)
2868 struct tcpcb *tp;
2869 {
2870 int i;
2871
2872 tp->rcv_numsacks = 0;
2873 for (i = 0; i < MAX_SACK_BLKS; i++)
2874 tp->sackblks[i].start = tp->sackblks[i].end=0;
2875
2876 }
2877
2878 /*
2879 * Checks for partial ack. If partial ack arrives, turn off retransmission
2880 * timer, deflate the window, do not clear tp->t_dupacks, and return 1.
2881 * If the ack advances at least to tp->snd_last, return 0.
2882 */
2883 int
2884 tcp_sack_partialack(tp, th)
2885 struct tcpcb *tp;
2886 struct tcphdr *th;
2887 {
2888 if (SEQ_LT(th->th_ack, tp->snd_last)) {
2889 /* Turn off retx. timer (will start again next segment) */
2890 TCP_TIMER_DISARM(tp, TCPT_REXMT);
2891 tp->t_rtttime = 0;
2892 #ifndef TCP_FACK
2893 /*
2894 * Partial window deflation. This statement relies on the
2895 * fact that tp->snd_una has not been updated yet. In FACK
2896 * hold snd_cwnd constant during fast recovery.
2897 */
2898 if (tp->snd_cwnd > (th->th_ack - tp->snd_una)) {
2899 tp->snd_cwnd -= th->th_ack - tp->snd_una;
2900 tp->snd_cwnd += tp->t_maxseg;
2901 } else
2902 tp->snd_cwnd = tp->t_maxseg;
2903 #endif
2904 return (1);
2905 }
2906 return (0);
2907 }
2908 #endif /* TCP_SACK */
2909
2910 /*
2911 * Pull out of band byte out of a segment so
2912 * it doesn't appear in the user's data queue.
2913 * It is still reflected in the segment length for
2914 * sequencing purposes.
2915 */
2916 void
2917 tcp_pulloutofband(so, urgent, m, off)
2918 struct socket *so;
2919 u_int urgent;
2920 struct mbuf *m;
2921 int off;
2922 {
2923 int cnt = off + urgent - 1;
2924
2925 while (cnt >= 0) {
2926 if (m->m_len > cnt) {
2927 char *cp = mtod(m, caddr_t) + cnt;
2928 struct tcpcb *tp = sototcpcb(so);
2929
2930 tp->t_iobc = *cp;
2931 tp->t_oobflags |= TCPOOB_HAVEDATA;
2932 bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2933 m->m_len--;
2934 return;
2935 }
2936 cnt -= m->m_len;
2937 m = m->m_next;
2938 if (m == 0)
2939 break;
2940 }
2941 panic("tcp_pulloutofband");
2942 }
2943
2944 /*
2945 * Collect new round-trip time estimate
2946 * and update averages and current timeout.
2947 */
2948 void
2949 tcp_xmit_timer(tp, rtt)
2950 struct tcpcb *tp;
2951 short rtt;
2952 {
2953 short delta;
2954 short rttmin;
2955
2956 --rtt;
2957 if (rtt < 0)
2958 rtt = 0;
2959 if (rtt > TCP_RTT_MAX)
2960 rtt = TCP_RTT_MAX;
2961
2962 tcpstat.tcps_rttupdated++;
2963 if (tp->t_srtt != 0) {
2964 /*
2965 * srtt is stored as fixed point with 3 bits after the
2966 * binary point (i.e., scaled by 8). The following magic
2967 * is equivalent to the smoothing algorithm in rfc793 with
2968 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2969 * point). Adjust rtt to origin 0.
2970 */
2971 delta = (rtt << 2) - (tp->t_srtt >> TCP_RTT_SHIFT);
2972 if ((tp->t_srtt += delta) <= 0)
2973 tp->t_srtt = 1;
2974 /*
2975 * We accumulate a smoothed rtt variance (actually, a
2976 * smoothed mean difference), then set the retransmit
2977 * timer to smoothed rtt + 4 times the smoothed variance.
2978 * rttvar is stored as fixed point with 2 bits after the
2979 * binary point (scaled by 4). The following is
2980 * equivalent to rfc793 smoothing with an alpha of .75
2981 * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2982 * rfc793's wired-in beta.
2983 */
2984 if (delta < 0)
2985 delta = -delta;
2986 delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
2987 if ((tp->t_rttvar += delta) <= 0)
2988 tp->t_rttvar = 1;
2989 } else {
2990 /*
2991 * No rtt measurement yet - use the unsmoothed rtt.
2992 * Set the variance to half the rtt (so our first
2993 * retransmit happens at 3*rtt).
2994 */
2995 tp->t_srtt = rtt << (TCP_RTT_SHIFT + 2);
2996 tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT + 2 - 1);
2997 }
2998 tp->t_rtttime = 0;
2999 tp->t_rxtshift = 0;
3000
3001 /*
3002 * the retransmit should happen at rtt + 4 * rttvar.
3003 * Because of the way we do the smoothing, srtt and rttvar
3004 * will each average +1/2 tick of bias. When we compute
3005 * the retransmit timer, we want 1/2 tick of rounding and
3006 * 1 extra tick because of +-1/2 tick uncertainty in the
3007 * firing of the timer. The bias will give us exactly the
3008 * 1.5 tick we need. But, because the bias is
3009 * statistical, we have to test that we don't drop below
3010 * the minimum feasible timer (which is 2 ticks).
3011 */
3012 if (tp->t_rttmin > rtt + 2)
3013 rttmin = tp->t_rttmin;
3014 else
3015 rttmin = rtt + 2;
3016 TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp), rttmin, TCPTV_REXMTMAX);
3017
3018 /*
3019 * We received an ack for a packet that wasn't retransmitted;
3020 * it is probably safe to discard any error indications we've
3021 * received recently. This isn't quite right, but close enough
3022 * for now (a route might have failed after we sent a segment,
3023 * and the return path might not be symmetrical).
3024 */
3025 tp->t_softerror = 0;
3026 }
3027
3028 /*
3029 * Determine a reasonable value for maxseg size.
3030 * If the route is known, check route for mtu.
3031 * If none, use an mss that can be handled on the outgoing
3032 * interface without forcing IP to fragment; if bigger than
3033 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
3034 * to utilize large mbufs. If no route is found, route has no mtu,
3035 * or the destination isn't local, use a default, hopefully conservative
3036 * size (usually 512 or the default IP max size, but no more than the mtu
3037 * of the interface), as we can't discover anything about intervening
3038 * gateways or networks. We also initialize the congestion/slow start
3039 * window to be a single segment if the destination isn't local.
3040 * While looking at the routing entry, we also initialize other path-dependent
3041 * parameters from pre-set or cached values in the routing entry.
3042 *
3043 * Also take into account the space needed for options that we
3044 * send regularly. Make maxseg shorter by that amount to assure
3045 * that we can send maxseg amount of data even when the options
3046 * are present. Store the upper limit of the length of options plus
3047 * data in maxopd.
3048 *
3049 * NOTE: offer == -1 indicates that the maxseg size changed due to
3050 * Path MTU discovery.
3051 */
3052 int
3053 tcp_mss(tp, offer)
3054 struct tcpcb *tp;
3055 int offer;
3056 {
3057 struct rtentry *rt;
3058 struct ifnet *ifp;
3059 int mss, mssopt;
3060 int iphlen;
3061 struct inpcb *inp;
3062
3063 inp = tp->t_inpcb;
3064
3065 mssopt = mss = tcp_mssdflt;
3066
3067 rt = in_pcbrtentry(inp);
3068
3069 if (rt == NULL)
3070 goto out;
3071
3072 ifp = rt->rt_ifp;
3073
3074 switch (tp->pf) {
3075 #ifdef INET6
3076 case AF_INET6:
3077 iphlen = sizeof(struct ip6_hdr);
3078 break;
3079 #endif
3080 case AF_INET:
3081 iphlen = sizeof(struct ip);
3082 break;
3083 default:
3084 /* the family does not support path MTU discovery */
3085 goto out;
3086 }
3087
3088 #ifdef RTV_MTU
3089 /*
3090 * if there's an mtu associated with the route and we support
3091 * path MTU discovery for the underlying protocol family, use it.
3092 */
3093 if (rt->rt_rmx.rmx_mtu) {
3094 /*
3095 * One may wish to lower MSS to take into account options,
3096 * especially security-related options.
3097 */
3098 if (tp->pf == AF_INET6 && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
3099 /*
3100 * RFC2460 section 5, last paragraph: if path MTU is
3101 * smaller than 1280, use 1280 as packet size and
3102 * attach fragment header.
3103 */
3104 mss = IPV6_MMTU - iphlen - sizeof(struct ip6_frag) -
3105 sizeof(struct tcphdr);
3106 } else
3107 mss = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
3108 } else
3109 #endif /* RTV_MTU */
3110 if (!ifp)
3111 /*
3112 * ifp may be null and rmx_mtu may be zero in certain
3113 * v6 cases (e.g., if ND wasn't able to resolve the
3114 * destination host.
3115 */
3116 goto out;
3117 else if (ifp->if_flags & IFF_LOOPBACK)
3118 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3119 else if (tp->pf == AF_INET) {
3120 if (ip_mtudisc)
3121 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3122 else if (inp && in_localaddr(inp->inp_faddr))
3123 mss = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3124 }
3125 #ifdef INET6
3126 else if (tp->pf == AF_INET6) {
3127 /*
3128 * for IPv6, path MTU discovery is always turned on,
3129 * or the node must use packet size <= 1280.
3130 */
3131 mss = IN6_LINKMTU(ifp) - iphlen - sizeof(struct tcphdr);
3132 }
3133 #endif /* INET6 */
3134
3135 /* Calculate the value that we offer in TCPOPT_MAXSEG */
3136 if (offer != -1) {
3137 #ifndef INET6
3138 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3139 #else
3140 if (tp->pf == AF_INET6)
3141 mssopt = IN6_LINKMTU(ifp) - iphlen -
3142 sizeof(struct tcphdr);
3143 else
3144 mssopt = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
3145 #endif
3146
3147 mssopt = max(tcp_mssdflt, mssopt);
3148 }
3149
3150 out:
3151 /*
3152 * The current mss, t_maxseg, is initialized to the default value.
3153 * If we compute a smaller value, reduce the current mss.
3154 * If we compute a larger value, return it for use in sending
3155 * a max seg size option, but don't store it for use
3156 * unless we received an offer at least that large from peer.
3157 *
3158 * However, do not accept offers lower than the minimum of
3159 * the interface MTU and 216.
3160 */
3161 if (offer > 0)
3162 tp->t_peermss = offer;
3163 if (tp->t_peermss)
3164 mss = min(mss, max(tp->t_peermss, 216));
3165
3166 /* sanity - at least max opt. space */
3167 mss = max(mss, 64);
3168
3169 /*
3170 * maxopd stores the maximum length of data AND options
3171 * in a segment; maxseg is the amount of data in a normal
3172 * segment. We need to store this value (maxopd) apart
3173 * from maxseg, because now every segment carries options
3174 * and thus we normally have somewhat less data in segments.
3175 */
3176 tp->t_maxopd = mss;
3177
3178 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3179 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
3180 mss -= TCPOLEN_TSTAMP_APPA;
3181 #ifdef TCP_SIGNATURE
3182 if (tp->t_flags & TF_SIGNATURE)
3183 mss -= TCPOLEN_SIGLEN;
3184 #endif
3185
3186 if (offer == -1) {
3187 /* mss changed due to Path MTU discovery */
3188 tp->t_flags &= ~TF_PMTUD_PEND;
3189 tp->t_pmtud_mtu_sent = 0;
3190 tp->t_pmtud_mss_acked = 0;
3191 if (mss < tp->t_maxseg) {
3192 /*
3193 * Follow suggestion in RFC 2414 to reduce the
3194 * congestion window by the ratio of the old
3195 * segment size to the new segment size.
3196 */
3197 tp->snd_cwnd = ulmax((tp->snd_cwnd / tp->t_maxseg) *
3198 mss, mss);
3199 }
3200 } else if (tcp_do_rfc3390) {
3201 /* increase initial window */
3202 tp->snd_cwnd = ulmin(4 * mss, ulmax(2 * mss, 4380));
3203 } else
3204 tp->snd_cwnd = mss;
3205
3206 tp->t_maxseg = mss;
3207
3208 return (offer != -1 ? mssopt : mss);
3209 }
3210
3211 u_int
3212 tcp_hdrsz(struct tcpcb *tp)
3213 {
3214 u_int hlen;
3215
3216 switch (tp->pf) {
3217 #ifdef INET6
3218 case AF_INET6:
3219 hlen = sizeof(struct ip6_hdr);
3220 break;
3221 #endif
3222 case AF_INET:
3223 hlen = sizeof(struct ip);
3224 break;
3225 default:
3226 hlen = 0;
3227 break;
3228 }
3229 hlen += sizeof(struct tcphdr);
3230
3231 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
3232 (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP)
3233 hlen += TCPOLEN_TSTAMP_APPA;
3234 #ifdef TCP_SIGNATURE
3235 if (tp->t_flags & TF_SIGNATURE)
3236 hlen += TCPOLEN_SIGLEN;
3237 #endif
3238 return (hlen);
3239 }
3240
3241 /*
3242 * Set connection variables based on the effective MSS.
3243 * We are passed the TCPCB for the actual connection. If we
3244 * are the server, we are called by the compressed state engine
3245 * when the 3-way handshake is complete. If we are the client,
3246 * we are called when we receive the SYN,ACK from the server.
3247 *
3248 * NOTE: The t_maxseg value must be initialized in the TCPCB
3249 * before this routine is called!
3250 */
3251 void
3252 tcp_mss_update(tp)
3253 struct tcpcb *tp;
3254 {
3255 int mss;
3256 u_long bufsize;
3257 struct rtentry *rt;
3258 struct socket *so;
3259
3260 so = tp->t_inpcb->inp_socket;
3261 mss = tp->t_maxseg;
3262
3263 rt = in_pcbrtentry(tp->t_inpcb);
3264
3265 if (rt == NULL)
3266 return;
3267
3268 bufsize = so->so_snd.sb_hiwat;
3269 if (bufsize < mss) {
3270 mss = bufsize;
3271 /* Update t_maxseg and t_maxopd */
3272 tcp_mss(tp, mss);
3273 } else {
3274 bufsize = roundup(bufsize, mss);
3275 if (bufsize > sb_max)
3276 bufsize = sb_max;
3277 (void)sbreserve(&so->so_snd, bufsize);
3278 }
3279
3280 bufsize = so->so_rcv.sb_hiwat;
3281 if (bufsize > mss) {
3282 bufsize = roundup(bufsize, mss);
3283 if (bufsize > sb_max)
3284 bufsize = sb_max;
3285 (void)sbreserve(&so->so_rcv, bufsize);
3286 }
3287
3288 }
3289 #endif /* TUBA_INCLUDE */
3290
3291 #if defined (TCP_SACK)
3292 /*
3293 * Checks for partial ack. If partial ack arrives, force the retransmission
3294 * of the next unacknowledged segment, do not clear tp->t_dupacks, and return
3295 * 1. By setting snd_nxt to ti_ack, this forces retransmission timer to
3296 * be started again. If the ack advances at least to tp->snd_last, return 0.
3297 */
3298 int
3299 tcp_newreno(tp, th)
3300 struct tcpcb *tp;
3301 struct tcphdr *th;
3302 {
3303 if (SEQ_LT(th->th_ack, tp->snd_last)) {
3304 /*
3305 * snd_una has not been updated and the socket send buffer
3306 * not yet drained of the acked data, so we have to leave
3307 * snd_una as it was to get the correct data offset in
3308 * tcp_output().
3309 */
3310 tcp_seq onxt = tp->snd_nxt;
3311 u_long ocwnd = tp->snd_cwnd;
3312 TCP_TIMER_DISARM(tp, TCPT_REXMT);
3313 tp->t_rtttime = 0;
3314 tp->snd_nxt = th->th_ack;
3315 /*
3316 * Set snd_cwnd to one segment beyond acknowledged offset
3317 * (tp->snd_una not yet updated when this function is called)
3318 */
3319 tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
3320 (void) tcp_output(tp);
3321 tp->snd_cwnd = ocwnd;
3322 if (SEQ_GT(onxt, tp->snd_nxt))
3323 tp->snd_nxt = onxt;
3324 /*
3325 * Partial window deflation. Relies on fact that tp->snd_una
3326 * not updated yet.
3327 */
3328 tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
3329 return 1;
3330 }
3331 return 0;
3332 }
3333 #endif /* TCP_SACK */
3334
3335 static int
3336 tcp_mss_adv(struct ifnet *ifp, int af)
3337 {
3338 int mss = 0;
3339 int iphlen;
3340
3341 switch (af) {
3342 case AF_INET:
3343 if (ifp != NULL)
3344 mss = ifp->if_mtu;
3345 iphlen = sizeof(struct ip);
3346 break;
3347 #ifdef INET6
3348 case AF_INET6:
3349 if (ifp != NULL)
3350 mss = IN6_LINKMTU(ifp);
3351 iphlen = sizeof(struct ip6_hdr);
3352 break;
3353 #endif
3354 }
3355 mss = mss - iphlen - sizeof(struct tcphdr);
3356 return (max(mss, tcp_mssdflt));
3357 }
3358
3359 /*
3360 * TCP compressed state engine. Currently used to hold compressed
3361 * state for SYN_RECEIVED.
3362 */
3363
3364 u_long syn_cache_count;
3365 u_int32_t syn_hash1, syn_hash2;
3366
3367 #define SYN_HASH(sa, sp, dp) \
3368 ((((sa)->s_addr^syn_hash1)*(((((u_int32_t)(dp))<<16) + \
3369 ((u_int32_t)(sp)))^syn_hash2)))
3370 #ifndef INET6
3371 #define SYN_HASHALL(hash, src, dst) \
3372 do { \
3373 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
3374 ((struct sockaddr_in *)(src))->sin_port, \
3375 ((struct sockaddr_in *)(dst))->sin_port); \
3376 } while (/*CONSTCOND*/ 0)
3377 #else
3378 #define SYN_HASH6(sa, sp, dp) \
3379 ((((sa)->s6_addr32[0] ^ (sa)->s6_addr32[3] ^ syn_hash1) * \
3380 (((((u_int32_t)(dp))<<16) + ((u_int32_t)(sp)))^syn_hash2)) \
3381 & 0x7fffffff)
3382
3383 #define SYN_HASHALL(hash, src, dst) \
3384 do { \
3385 switch ((src)->sa_family) { \
3386 case AF_INET: \
3387 hash = SYN_HASH(&((struct sockaddr_in *)(src))->sin_addr, \
3388 ((struct sockaddr_in *)(src))->sin_port, \
3389 ((struct sockaddr_in *)(dst))->sin_port); \
3390 break; \
3391 case AF_INET6: \
3392 hash = SYN_HASH6(&((struct sockaddr_in6 *)(src))->sin6_addr, \
3393 ((struct sockaddr_in6 *)(src))->sin6_port, \
3394 ((struct sockaddr_in6 *)(dst))->sin6_port); \
3395 break; \
3396 default: \
3397 hash = 0; \
3398 } \
3399 } while (/*CONSTCOND*/0)
3400 #endif /* INET6 */
3401
3402 #define SYN_CACHE_RM(sc) \
3403 do { \
3404 (sc)->sc_flags |= SCF_DEAD; \
3405 TAILQ_REMOVE(&tcp_syn_cache[(sc)->sc_bucketidx].sch_bucket, \
3406 (sc), sc_bucketq); \
3407 (sc)->sc_tp = NULL; \
3408 LIST_REMOVE((sc), sc_tpq); \
3409 tcp_syn_cache[(sc)->sc_bucketidx].sch_length--; \
3410 timeout_del(&(sc)->sc_timer); \
3411 syn_cache_count--; \
3412 } while (/*CONSTCOND*/0)
3413
3414 #define SYN_CACHE_PUT(sc) \
3415 do { \
3416 if ((sc)->sc_ipopts) \
3417 (void) m_free((sc)->sc_ipopts); \
3418 if ((sc)->sc_route4.ro_rt != NULL) \
3419 RTFREE((sc)->sc_route4.ro_rt); \
3420 timeout_set(&(sc)->sc_timer, syn_cache_reaper, (sc)); \
3421 timeout_add(&(sc)->sc_timer, 0); \
3422 } while (/*CONSTCOND*/0)
3423
3424 struct pool syn_cache_pool;
3425
3426 /*
3427 * We don't estimate RTT with SYNs, so each packet starts with the default
3428 * RTT and each timer step has a fixed timeout value.
3429 */
3430 #define SYN_CACHE_TIMER_ARM(sc) \
3431 do { \
3432 TCPT_RANGESET((sc)->sc_rxtcur, \
3433 TCPTV_SRTTDFLT * tcp_backoff[(sc)->sc_rxtshift], TCPTV_MIN, \
3434 TCPTV_REXMTMAX); \
3435 if (!timeout_initialized(&(sc)->sc_timer)) \
3436 timeout_set(&(sc)->sc_timer, syn_cache_timer, (sc)); \
3437 timeout_add(&(sc)->sc_timer, (sc)->sc_rxtcur * (hz / PR_SLOWHZ)); \
3438 } while (/*CONSTCOND*/0)
3439
3440 #define SYN_CACHE_TIMESTAMP(sc) tcp_now
3441
3442 void
3443 syn_cache_init()
3444 {
3445 int i;
3446
3447 /* Initialize the hash buckets. */
3448 for (i = 0; i < tcp_syn_cache_size; i++)
3449 TAILQ_INIT(&tcp_syn_cache[i].sch_bucket);
3450
3451 /* Initialize the syn cache pool. */
3452 pool_init(&syn_cache_pool, sizeof(struct syn_cache), 0, 0, 0,
3453 "synpl", NULL);
3454 }
3455
3456 void
3457 syn_cache_insert(sc, tp)
3458 struct syn_cache *sc;
3459 struct tcpcb *tp;
3460 {
3461 struct syn_cache_head *scp;
3462 struct syn_cache *sc2;
3463 int s;
3464
3465 /*
3466 * If there are no entries in the hash table, reinitialize
3467 * the hash secrets.
3468 */
3469 if (syn_cache_count == 0) {
3470 syn_hash1 = arc4random();
3471 syn_hash2 = arc4random();
3472 }
3473
3474 SYN_HASHALL(sc->sc_hash, &sc->sc_src.sa, &sc->sc_dst.sa);
3475 sc->sc_bucketidx = sc->sc_hash % tcp_syn_cache_size;
3476 scp = &tcp_syn_cache[sc->sc_bucketidx];
3477
3478 /*
3479 * Make sure that we don't overflow the per-bucket
3480 * limit or the total cache size limit.
3481 */
3482 s = splsoftnet();
3483 if (scp->sch_length >= tcp_syn_bucket_limit) {
3484 tcpstat.tcps_sc_bucketoverflow++;
3485 /*
3486 * The bucket is full. Toss the oldest element in the
3487 * bucket. This will be the first entry in the bucket.
3488 */
3489 sc2 = TAILQ_FIRST(&scp->sch_bucket);
3490 #ifdef DIAGNOSTIC
3491 /*
3492 * This should never happen; we should always find an
3493 * entry in our bucket.
3494 */
3495 if (sc2 == NULL)
3496 panic("syn_cache_insert: bucketoverflow: impossible");
3497 #endif
3498 SYN_CACHE_RM(sc2);
3499 SYN_CACHE_PUT(sc2);
3500 } else if (syn_cache_count >= tcp_syn_cache_limit) {
3501 struct syn_cache_head *scp2, *sce;
3502
3503 tcpstat.tcps_sc_overflowed++;
3504 /*
3505 * The cache is full. Toss the oldest entry in the
3506 * first non-empty bucket we can find.
3507 *
3508 * XXX We would really like to toss the oldest
3509 * entry in the cache, but we hope that this
3510 * condition doesn't happen very often.
3511 */
3512 scp2 = scp;
3513 if (TAILQ_EMPTY(&scp2->sch_bucket)) {
3514 sce = &tcp_syn_cache[tcp_syn_cache_size];
3515 for (++scp2; scp2 != scp; scp2++) {
3516 if (scp2 >= sce)
3517 scp2 = &tcp_syn_cache[0];
3518 if (! TAILQ_EMPTY(&scp2->sch_bucket))
3519 break;
3520 }
3521 #ifdef DIAGNOSTIC
3522 /*
3523 * This should never happen; we should always find a
3524 * non-empty bucket.
3525 */
3526 if (scp2 == scp)
3527 panic("syn_cache_insert: cacheoverflow: "
3528 "impossible");
3529 #endif
3530 }
3531 sc2 = TAILQ_FIRST(&scp2->sch_bucket);
3532 SYN_CACHE_RM(sc2);
3533 SYN_CACHE_PUT(sc2);
3534 }
3535
3536 /*
3537 * Initialize the entry's timer.
3538 */
3539 sc->sc_rxttot = 0;
3540 sc->sc_rxtshift = 0;
3541 SYN_CACHE_TIMER_ARM(sc);
3542
3543 /* Link it from tcpcb entry */
3544 LIST_INSERT_HEAD(&tp->t_sc, sc, sc_tpq);
3545
3546 /* Put it into the bucket. */
3547 TAILQ_INSERT_TAIL(&scp->sch_bucket, sc, sc_bucketq);
3548 scp->sch_length++;
3549 syn_cache_count++;
3550
3551 tcpstat.tcps_sc_added++;
3552 splx(s);
3553 }
3554
3555 /*
3556 * Walk the timer queues, looking for SYN,ACKs that need to be retransmitted.
3557 * If we have retransmitted an entry the maximum number of times, expire
3558 * that entry.
3559 */
3560 void
3561 syn_cache_timer(void *arg)
3562 {
3563 struct syn_cache *sc = arg;
3564 int s;
3565
3566 s = splsoftnet();
3567 if (sc->sc_flags & SCF_DEAD) {
3568 splx(s);
3569 return;
3570 }
3571
3572 if (__predict_false(sc->sc_rxtshift == TCP_MAXRXTSHIFT)) {
3573 /* Drop it -- too many retransmissions. */
3574 goto dropit;
3575 }
3576
3577 /*
3578 * Compute the total amount of time this entry has
3579 * been on a queue. If this entry has been on longer
3580 * than the keep alive timer would allow, expire it.
3581 */
3582 sc->sc_rxttot += sc->sc_rxtcur;
3583 if (sc->sc_rxttot >= tcptv_keep_init)
3584 goto dropit;
3585
3586 tcpstat.tcps_sc_retransmitted++;
3587 (void) syn_cache_respond(sc, NULL);
3588
3589 /* Advance the timer back-off. */
3590 sc->sc_rxtshift++;
3591 SYN_CACHE_TIMER_ARM(sc);
3592
3593 splx(s);
3594 return;
3595
3596 dropit:
3597 tcpstat.tcps_sc_timed_out++;
3598 SYN_CACHE_RM(sc);
3599 SYN_CACHE_PUT(sc);
3600 splx(s);
3601 }
3602
3603 void
3604 syn_cache_reaper(void *arg)
3605 {
3606 struct syn_cache *sc = arg;
3607 int s;
3608
3609 s = splsoftnet();
3610 pool_put(&syn_cache_pool, (sc));
3611 splx(s);
3612 return;
3613 }
3614
3615 /*
3616 * Remove syn cache created by the specified tcb entry,
3617 * because this does not make sense to keep them
3618 * (if there's no tcb entry, syn cache entry will never be used)
3619 */
3620 void
3621 syn_cache_cleanup(tp)
3622 struct tcpcb *tp;
3623 {
3624 struct syn_cache *sc, *nsc;
3625 int s;
3626
3627 s = splsoftnet();
3628
3629 for (sc = LIST_FIRST(&tp->t_sc); sc != NULL; sc = nsc) {
3630 nsc = LIST_NEXT(sc, sc_tpq);
3631
3632 #ifdef DIAGNOSTIC
3633 if (sc->sc_tp != tp)
3634 panic("invalid sc_tp in syn_cache_cleanup");
3635 #endif
3636 SYN_CACHE_RM(sc);
3637 SYN_CACHE_PUT(sc);
3638 }
3639 /* just for safety */
3640 LIST_INIT(&tp->t_sc);
3641
3642 splx(s);
3643 }
3644
3645 /*
3646 * Find an entry in the syn cache.
3647 */
3648 struct syn_cache *
3649 syn_cache_lookup(src, dst, headp)
3650 struct sockaddr *src;
3651 struct sockaddr *dst;
3652 struct syn_cache_head **headp;
3653 {
3654 struct syn_cache *sc;
3655 struct syn_cache_head *scp;
3656 u_int32_t hash;
3657 int s;
3658
3659 SYN_HASHALL(hash, src, dst);
3660
3661 scp = &tcp_syn_cache[hash % tcp_syn_cache_size];
3662 *headp = scp;
3663 s = splsoftnet();
3664 for (sc = TAILQ_FIRST(&scp->sch_bucket); sc != NULL;
3665 sc = TAILQ_NEXT(sc, sc_bucketq)) {
3666 if (sc->sc_hash != hash)
3667 continue;
3668 if (!bcmp(&sc->sc_src, src, src->sa_len) &&
3669 !bcmp(&sc->sc_dst, dst, dst->sa_len)) {
3670 splx(s);
3671 return (sc);
3672 }
3673 }
3674 splx(s);
3675 return (NULL);
3676 }
3677
3678 /*
3679 * This function gets called when we receive an ACK for a
3680 * socket in the LISTEN state. We look up the connection
3681 * in the syn cache, and if its there, we pull it out of
3682 * the cache and turn it into a full-blown connection in
3683 * the SYN-RECEIVED state.
3684 *
3685 * The return values may not be immediately obvious, and their effects
3686 * can be subtle, so here they are:
3687 *
3688 * NULL SYN was not found in cache; caller should drop the
3689 * packet and send an RST.
3690 *
3691 * -1 We were unable to create the new connection, and are
3692 * aborting it. An ACK,RST is being sent to the peer
3693 * (unless we got screwey sequence numbners; see below),
3694 * because the 3-way handshake has been completed. Caller
3695 * should not free the mbuf, since we may be using it. If
3696 * we are not, we will free it.
3697 *
3698 * Otherwise, the return value is a pointer to the new socket
3699 * associated with the connection.
3700 */
3701 struct socket *
3702 syn_cache_get(src, dst, th, hlen, tlen, so, m)
3703 struct sockaddr *src;
3704 struct sockaddr *dst;
3705 struct tcphdr *th;
3706 unsigned int hlen, tlen;
3707 struct socket *so;
3708 struct mbuf *m;
3709 {
3710 struct syn_cache *sc;
3711 struct syn_cache_head *scp;
3712 struct inpcb *inp = NULL;
3713 struct tcpcb *tp = 0;
3714 struct mbuf *am;
3715 int s;
3716 struct socket *oso;
3717
3718 s = splsoftnet();
3719 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3720 splx(s);
3721 return (NULL);
3722 }
3723
3724 /*
3725 * Verify the sequence and ack numbers. Try getting the correct
3726 * response again.
3727 */
3728 if ((th->th_ack != sc->sc_iss + 1) ||
3729 SEQ_LEQ(th->th_seq, sc->sc_irs) ||
3730 SEQ_GT(th->th_seq, sc->sc_irs + 1 + sc->sc_win)) {
3731 (void) syn_cache_respond(sc, m);
3732 splx(s);
3733 return ((struct socket *)(-1));
3734 }
3735
3736 /* Remove this cache entry */
3737 SYN_CACHE_RM(sc);
3738 splx(s);
3739
3740 /*
3741 * Ok, create the full blown connection, and set things up
3742 * as they would have been set up if we had created the
3743 * connection when the SYN arrived. If we can't create
3744 * the connection, abort it.
3745 */
3746 oso = so;
3747 so = sonewconn(so, SS_ISCONNECTED);
3748 if (so == NULL)
3749 goto resetandabort;
3750
3751 inp = sotoinpcb(oso);
3752 #ifdef IPSEC
3753 /*
3754 * We need to copy the required security levels
3755 * from the old pcb. Ditto for any other
3756 * IPsec-related information.
3757 */
3758 {
3759 struct inpcb *newinp = (struct inpcb *)so->so_pcb;
3760 bcopy(inp->inp_seclevel, newinp->inp_seclevel,
3761 sizeof(inp->inp_seclevel));
3762 newinp->inp_secrequire = inp->inp_secrequire;
3763 if (inp->inp_ipo != NULL) {
3764 newinp->inp_ipo = inp->inp_ipo;
3765 inp->inp_ipo->ipo_ref_count++;
3766 }
3767 if (inp->inp_ipsec_remotecred != NULL) {
3768 newinp->inp_ipsec_remotecred = inp->inp_ipsec_remotecred;
3769 inp->inp_ipsec_remotecred->ref_count++;
3770 }
3771 if (inp->inp_ipsec_remoteauth != NULL) {
3772 newinp->inp_ipsec_remoteauth
3773 = inp->inp_ipsec_remoteauth;
3774 inp->inp_ipsec_remoteauth->ref_count++;
3775 }
3776 }
3777 #endif /* IPSEC */
3778 #ifdef INET6
3779 /*
3780 * inp still has the OLD in_pcb stuff, set the
3781 * v6-related flags on the new guy, too.
3782 */
3783 {
3784 int flags = inp->inp_flags;
3785 struct inpcb *oldinpcb = inp;
3786
3787 inp = (struct inpcb *)so->so_pcb;
3788 inp->inp_flags |= (flags & INP_IPV6);
3789 if ((inp->inp_flags & INP_IPV6) != 0) {
3790 inp->inp_ipv6.ip6_hlim =
3791 oldinpcb->inp_ipv6.ip6_hlim;
3792 }
3793 }
3794 #else /* INET6 */
3795 inp = (struct inpcb *)so->so_pcb;
3796 #endif /* INET6 */
3797
3798 inp->inp_lport = th->th_dport;
3799 switch (src->sa_family) {
3800 #ifdef INET6
3801 case AF_INET6:
3802 inp->inp_laddr6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
3803 break;
3804 #endif /* INET6 */
3805 case AF_INET:
3806
3807 inp->inp_laddr = ((struct sockaddr_in *)dst)->sin_addr;
3808 inp->inp_options = ip_srcroute();
3809 if (inp->inp_options == NULL) {
3810 inp->inp_options = sc->sc_ipopts;
3811 sc->sc_ipopts = NULL;
3812 }
3813 break;
3814 }
3815 in_pcbrehash(inp);
3816
3817 /*
3818 * Give the new socket our cached route reference.
3819 */
3820 if (inp)
3821 inp->inp_route = sc->sc_route4; /* struct assignment */
3822 #ifdef INET6
3823 else
3824 inp->inp_route6 = sc->sc_route6;
3825 #endif
3826 sc->sc_route4.ro_rt = NULL;
3827
3828 am = m_get(M_DONTWAIT, MT_SONAME); /* XXX */
3829 if (am == NULL)
3830 goto resetandabort;
3831 am->m_len = src->sa_len;
3832 bcopy(src, mtod(am, caddr_t), src->sa_len);
3833
3834 switch (src->sa_family) {
3835 case AF_INET:
3836 /* drop IPv4 packet to AF_INET6 socket */
3837 if (inp->inp_flags & INP_IPV6) {
3838 (void) m_free(am);
3839 goto resetandabort;
3840 }
3841 if (in_pcbconnect(inp, am)) {
3842 (void) m_free(am);
3843 goto resetandabort;
3844 }
3845 break;
3846 #ifdef INET6
3847 case AF_INET6:
3848 if (in6_pcbconnect(inp, am)) {
3849 (void) m_free(am);
3850 goto resetandabort;
3851 }
3852 break;
3853 #endif
3854 }
3855 (void) m_free(am);
3856
3857 tp = intotcpcb(inp);
3858 tp->t_flags = sototcpcb(oso)->t_flags & TF_NODELAY;
3859 if (sc->sc_request_r_scale != 15) {
3860 tp->requested_s_scale = sc->sc_requested_s_scale;
3861 tp->request_r_scale = sc->sc_request_r_scale;
3862 tp->snd_scale = sc->sc_requested_s_scale;
3863 tp->rcv_scale = sc->sc_request_r_scale;
3864 tp->t_flags |= TF_REQ_SCALE|TF_RCVD_SCALE;
3865 }
3866 if (sc->sc_flags & SCF_TIMESTAMP)
3867 tp->t_flags |= TF_REQ_TSTMP|TF_RCVD_TSTMP;
3868
3869 tp->t_template = tcp_template(tp);
3870 if (tp->t_template == 0) {
3871 tp = tcp_drop(tp, ENOBUFS); /* destroys socket */
3872 so = NULL;
3873 m_freem(m);
3874 goto abort;
3875 }
3876 #ifdef TCP_SACK
3877 tp->sack_enable = sc->sc_flags & SCF_SACK_PERMIT;
3878 #endif
3879
3880 tp->iss = sc->sc_iss;
3881 tp->irs = sc->sc_irs;
3882 tcp_sendseqinit(tp);
3883 #if defined (TCP_SACK) || defined(TCP_ECN)
3884 tp->snd_last = tp->snd_una;
3885 #endif /* TCP_SACK */
3886 #if defined(TCP_SACK) && defined(TCP_FACK)
3887 tp->snd_fack = tp->snd_una;
3888 tp->retran_data = 0;
3889 tp->snd_awnd = 0;
3890 #endif /* TCP_FACK */
3891 #ifdef TCP_ECN
3892 if (sc->sc_flags & SCF_ECN_PERMIT) {
3893 tp->t_flags |= TF_ECN_PERMIT;
3894 tcpstat.tcps_ecn_accepts++;
3895 }
3896 #endif
3897 #ifdef TCP_SACK
3898 if (sc->sc_flags & SCF_SACK_PERMIT)
3899 tp->t_flags |= TF_SACK_PERMIT;
3900 #endif
3901 #ifdef TCP_SIGNATURE
3902 if (sc->sc_flags & SCF_SIGNATURE)
3903 tp->t_flags |= TF_SIGNATURE;
3904 #endif
3905 tcp_rcvseqinit(tp);
3906 tp->t_state = TCPS_SYN_RECEIVED;
3907 tp->t_rcvtime = tcp_now;
3908 TCP_TIMER_ARM(tp, TCPT_KEEP, tcptv_keep_init);
3909 tcpstat.tcps_accepts++;
3910
3911 tcp_mss(tp, sc->sc_peermaxseg); /* sets t_maxseg */
3912 if (sc->sc_peermaxseg)
3913 tcp_mss_update(tp);
3914 /* Reset initial window to 1 segment for retransmit */
3915 if (sc->sc_rxtshift > 0)
3916 tp->snd_cwnd = tp->t_maxseg;
3917 tp->snd_wl1 = sc->sc_irs;
3918 tp->rcv_up = sc->sc_irs + 1;
3919
3920 /*
3921 * This is what whould have happened in tcp_output() when
3922 * the SYN,ACK was sent.
3923 */
3924 tp->snd_up = tp->snd_una;
3925 tp->snd_max = tp->snd_nxt = tp->iss+1;
3926 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
3927 if (sc->sc_win > 0 && SEQ_GT(tp->rcv_nxt + sc->sc_win, tp->rcv_adv))
3928 tp->rcv_adv = tp->rcv_nxt + sc->sc_win;
3929 tp->last_ack_sent = tp->rcv_nxt;
3930
3931 tcpstat.tcps_sc_completed++;
3932 SYN_CACHE_PUT(sc);
3933 return (so);
3934
3935 resetandabort:
3936 tcp_respond(NULL, mtod(m, caddr_t), th, (tcp_seq)0, th->th_ack, TH_RST);
3937 m_freem(m);
3938 abort:
3939 if (so != NULL)
3940 (void) soabort(so);
3941 SYN_CACHE_PUT(sc);
3942 tcpstat.tcps_sc_aborted++;
3943 return ((struct socket *)(-1));
3944 }
3945
3946 /*
3947 * This function is called when we get a RST for a
3948 * non-existent connection, so that we can see if the
3949 * connection is in the syn cache. If it is, zap it.
3950 */
3951
3952 void
3953 syn_cache_reset(src, dst, th)
3954 struct sockaddr *src;
3955 struct sockaddr *dst;
3956 struct tcphdr *th;
3957 {
3958 struct syn_cache *sc;
3959 struct syn_cache_head *scp;
3960 int s = splsoftnet();
3961
3962 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3963 splx(s);
3964 return;
3965 }
3966 if (SEQ_LT(th->th_seq, sc->sc_irs) ||
3967 SEQ_GT(th->th_seq, sc->sc_irs+1)) {
3968 splx(s);
3969 return;
3970 }
3971 SYN_CACHE_RM(sc);
3972 splx(s);
3973 tcpstat.tcps_sc_reset++;
3974 SYN_CACHE_PUT(sc);
3975 }
3976
3977 void
3978 syn_cache_unreach(src, dst, th)
3979 struct sockaddr *src;
3980 struct sockaddr *dst;
3981 struct tcphdr *th;
3982 {
3983 struct syn_cache *sc;
3984 struct syn_cache_head *scp;
3985 int s;
3986
3987 s = splsoftnet();
3988 if ((sc = syn_cache_lookup(src, dst, &scp)) == NULL) {
3989 splx(s);
3990 return;
3991 }
3992 /* If the sequence number != sc_iss, then it's a bogus ICMP msg */
3993 if (ntohl (th->th_seq) != sc->sc_iss) {
3994 splx(s);
3995 return;
3996 }
3997
3998 /*
3999 * If we've retransmitted 3 times and this is our second error,
4000 * we remove the entry. Otherwise, we allow it to continue on.
4001 * This prevents us from incorrectly nuking an entry during a
4002 * spurious network outage.
4003 *
4004 * See tcp_notify().
4005 */
4006 if ((sc->sc_flags & SCF_UNREACH) == 0 || sc->sc_rxtshift < 3) {
4007 sc->sc_flags |= SCF_UNREACH;
4008 splx(s);
4009 return;
4010 }
4011
4012 SYN_CACHE_RM(sc);
4013 splx(s);
4014 tcpstat.tcps_sc_unreach++;
4015 SYN_CACHE_PUT(sc);
4016 }
4017
4018 /*
4019 * Given a LISTEN socket and an inbound SYN request, add
4020 * this to the syn cache, and send back a segment:
4021 * <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
4022 * to the source.
4023 *
4024 * IMPORTANT NOTE: We do _NOT_ ACK data that might accompany the SYN.
4025 * Doing so would require that we hold onto the data and deliver it
4026 * to the application. However, if we are the target of a SYN-flood
4027 * DoS attack, an attacker could send data which would eventually
4028 * consume all available buffer space if it were ACKed. By not ACKing
4029 * the data, we avoid this DoS scenario.
4030 */
4031
4032 int
4033 syn_cache_add(src, dst, th, iphlen, so, m, optp, optlen, oi)
4034 struct sockaddr *src;
4035 struct sockaddr *dst;
4036 struct tcphdr *th;
4037 unsigned int iphlen;
4038 struct socket *so;
4039 struct mbuf *m;
4040 u_char *optp;
4041 int optlen;
4042 struct tcp_opt_info *oi;
4043 {
4044 struct tcpcb tb, *tp;
4045 long win;
4046 struct syn_cache *sc;
4047 struct syn_cache_head *scp;
4048 struct mbuf *ipopts;
4049
4050 tp = sototcpcb(so);
4051
4052 /*
4053 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
4054 *
4055 * Note this check is performed in tcp_input() very early on.
4056 */
4057
4058 /*
4059 * Initialize some local state.
4060 */
4061 win = sbspace(&so->so_rcv);
4062 if (win > TCP_MAXWIN)
4063 win = TCP_MAXWIN;
4064
4065 #ifdef TCP_SIGNATURE
4066 if (optp || (tp->t_flags & TF_SIGNATURE)) {
4067 #else
4068 if (optp) {
4069 #endif
4070 tb.pf = tp->pf;
4071 #ifdef TCP_SACK
4072 tb.sack_enable = tcp_do_sack;
4073 #endif
4074 tb.t_flags = tcp_do_rfc1323 ? (TF_REQ_SCALE|TF_REQ_TSTMP) : 0;
4075 #ifdef TCP_SIGNATURE
4076 tb.t_state = TCPS_LISTEN;
4077 if (tp->t_flags & TF_SIGNATURE)
4078 tb.t_flags |= TF_SIGNATURE;
4079 #endif
4080 if (tcp_dooptions(&tb, optp, optlen, th, m, iphlen, oi))
4081 return (0);
4082 } else
4083 tb.t_flags = 0;
4084
4085 switch (src->sa_family) {
4086 #ifdef INET
4087 case AF_INET:
4088 /*
4089 * Remember the IP options, if any.
4090 */
4091 ipopts = ip_srcroute();
4092 break;
4093 #endif
4094 default:
4095 ipopts = NULL;
4096 }
4097
4098 /*
4099 * See if we already have an entry for this connection.
4100 * If we do, resend the SYN,ACK. We do not count this
4101 * as a retransmission (XXX though maybe we should).
4102 */
4103 if ((sc = syn_cache_lookup(src, dst, &scp)) != NULL) {
4104 tcpstat.tcps_sc_dupesyn++;
4105 if (ipopts) {
4106 /*
4107 * If we were remembering a previous source route,
4108 * forget it and use the new one we've been given.
4109 */
4110 if (sc->sc_ipopts)
4111 (void) m_free(sc->sc_ipopts);
4112 sc->sc_ipopts = ipopts;
4113 }
4114 sc->sc_timestamp = tb.ts_recent;
4115 if (syn_cache_respond(sc, m) == 0) {
4116 tcpstat.tcps_sndacks++;
4117 tcpstat.tcps_sndtotal++;
4118 }
4119 return (1);
4120 }
4121
4122 sc = pool_get(&syn_cache_pool, PR_NOWAIT);
4123 if (sc == NULL) {
4124 if (ipopts)
4125 (void) m_free(ipopts);
4126 return (0);
4127 }
4128
4129 /*
4130 * Fill in the cache, and put the necessary IP and TCP
4131 * options into the reply.
4132 */
4133 bzero(sc, sizeof(struct syn_cache));
4134 bzero(&sc->sc_timer, sizeof(sc->sc_timer));
4135 bcopy(src, &sc->sc_src, src->sa_len);
4136 bcopy(dst, &sc->sc_dst, dst->sa_len);
4137 sc->sc_flags = 0;
4138 sc->sc_ipopts = ipopts;
4139 sc->sc_irs = th->th_seq;
4140
4141 #ifdef TCP_COMPAT_42
4142 tcp_iss += TCP_ISSINCR/2;
4143 sc->sc_iss = tcp_iss;
4144 #else
4145 sc->sc_iss = tcp_rndiss_next();
4146 #endif
4147 sc->sc_peermaxseg = oi->maxseg;
4148 sc->sc_ourmaxseg = tcp_mss_adv(m->m_flags & M_PKTHDR ?
4149 m->m_pkthdr.rcvif : NULL, sc->sc_src.sa.sa_family);
4150 sc->sc_win = win;
4151 sc->sc_timestamp = tb.ts_recent;
4152 if ((tb.t_flags & (TF_REQ_TSTMP|TF_RCVD_TSTMP)) ==
4153 (TF_REQ_TSTMP|TF_RCVD_TSTMP))
4154 sc->sc_flags |= SCF_TIMESTAMP;
4155 if ((tb.t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
4156 (TF_RCVD_SCALE|TF_REQ_SCALE)) {
4157 sc->sc_requested_s_scale = tb.requested_s_scale;
4158 sc->sc_request_r_scale = 0;
4159 while (sc->sc_request_r_scale < TCP_MAX_WINSHIFT &&
4160 TCP_MAXWIN << sc->sc_request_r_scale <
4161 so->so_rcv.sb_hiwat)
4162 sc->sc_request_r_scale++;
4163 } else {
4164 sc->sc_requested_s_scale = 15;
4165 sc->sc_request_r_scale = 15;
4166 }
4167 #ifdef TCP_ECN
4168 /*
4169 * if both ECE and CWR flag bits are set, peer is ECN capable.
4170 */
4171 if (tcp_do_ecn &&
4172 (th->th_flags & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR))
4173 sc->sc_flags |= SCF_ECN_PERMIT;
4174 #endif
4175 #ifdef TCP_SACK
4176 /*
4177 * Set SCF_SACK_PERMIT if peer did send a SACK_PERMITTED option
4178 * (i.e., if tcp_dooptions() did set TF_SACK_PERMIT).
4179 */
4180 if (tb.sack_enable && (tb.t_flags & TF_SACK_PERMIT))
4181 sc->sc_flags |= SCF_SACK_PERMIT;
4182 #endif
4183 #ifdef TCP_SIGNATURE
4184 if (tb.t_flags & TF_SIGNATURE)
4185 sc->sc_flags |= SCF_SIGNATURE;
4186 #endif
4187 sc->sc_tp = tp;
4188 if (syn_cache_respond(sc, m) == 0) {
4189 syn_cache_insert(sc, tp);
4190 tcpstat.tcps_sndacks++;
4191 tcpstat.tcps_sndtotal++;
4192 } else {
4193 SYN_CACHE_PUT(sc);
4194 tcpstat.tcps_sc_dropped++;
4195 }
4196 return (1);
4197 }
4198
4199 int
4200 syn_cache_respond(sc, m)
4201 struct syn_cache *sc;
4202 struct mbuf *m;
4203 {
4204 struct route *ro;
4205 u_int8_t *optp;
4206 int optlen, error;
4207 u_int16_t tlen;
4208 struct ip *ip = NULL;
4209 #ifdef INET6
4210 struct ip6_hdr *ip6 = NULL;
4211 #endif
4212 struct tcphdr *th;
4213 u_int hlen;
4214 struct inpcb *inp;
4215
4216 switch (sc->sc_src.sa.sa_family) {
4217 case AF_INET:
4218 hlen = sizeof(struct ip);
4219 ro = &sc->sc_route4;
4220 break;
4221 #ifdef INET6
4222 case AF_INET6:
4223 hlen = sizeof(struct ip6_hdr);
4224 ro = (struct route *)&sc->sc_route6;
4225 break;
4226 #endif
4227 default:
4228 if (m)
4229 m_freem(m);
4230 return (EAFNOSUPPORT);
4231 }
4232
4233 /* Compute the size of the TCP options. */
4234 optlen = 4 + (sc->sc_request_r_scale != 15 ? 4 : 0) +
4235 #ifdef TCP_SACK
4236 ((sc->sc_flags & SCF_SACK_PERMIT &&
4237 (stdbsdtcp || !(sc->sc_flags & SCF_TIMESTAMP))) ? 4 : 0) +
4238 #endif
4239 #ifdef TCP_SIGNATURE
4240 ((sc->sc_flags & SCF_SIGNATURE) ? TCPOLEN_SIGLEN : 0) +
4241 #endif
4242 ((sc->sc_flags & SCF_TIMESTAMP) ? TCPOLEN_TSTAMP_APPA : 0);
4243
4244 tlen = hlen + sizeof(struct tcphdr) + optlen;
4245
4246 /*
4247 * Create the IP+TCP header from scratch.
4248 */
4249 if (m)
4250 m_freem(m);
4251 #ifdef DIAGNOSTIC
4252 if (max_linkhdr + tlen > MCLBYTES)
4253 return (ENOBUFS);
4254 #endif
4255 MGETHDR(m, M_DONTWAIT, MT_DATA);
4256 if (m && tlen > MHLEN) {
4257 MCLGET(m, M_DONTWAIT);
4258 if ((m->m_flags & M_EXT) == 0) {
4259 m_freem(m);
4260 m = NULL;
4261 }
4262 }
4263 if (m == NULL)
4264 return (ENOBUFS);
4265
4266 /* Fixup the mbuf. */
4267 m->m_data += max_linkhdr;
4268 m->m_len = m->m_pkthdr.len = tlen;
4269 m->m_pkthdr.rcvif = NULL;
4270 memset(mtod(m, u_char *), 0, tlen);
4271
4272 switch (sc->sc_src.sa.sa_family) {
4273 case AF_INET:
4274 ip = mtod(m, struct ip *);
4275 ip->ip_dst = sc->sc_src.sin.sin_addr;
4276 ip->ip_src = sc->sc_dst.sin.sin_addr;
4277 ip->ip_p = IPPROTO_TCP;
4278 th = (struct tcphdr *)(ip + 1);
4279 th->th_dport = sc->sc_src.sin.sin_port;
4280 th->th_sport = sc->sc_dst.sin.sin_port;
4281 break;
4282 #ifdef INET6
4283 case AF_INET6:
4284 ip6 = mtod(m, struct ip6_hdr *);
4285 ip6->ip6_dst = sc->sc_src.sin6.sin6_addr;
4286 ip6->ip6_src = sc->sc_dst.sin6.sin6_addr;
4287 ip6->ip6_nxt = IPPROTO_TCP;
4288 /* ip6_plen will be updated in ip6_output() */
4289 th = (struct tcphdr *)(ip6 + 1);
4290 th->th_dport = sc->sc_src.sin6.sin6_port;
4291 th->th_sport = sc->sc_dst.sin6.sin6_port;
4292 break;
4293 #endif
4294 default:
4295 th = NULL;
4296 }
4297
4298 th->th_seq = htonl(sc->sc_iss);
4299 th->th_ack = htonl(sc->sc_irs + 1);
4300 th->th_off = (sizeof(struct tcphdr) + optlen) >> 2;
4301 th->th_flags = TH_SYN|TH_ACK;
4302 #ifdef TCP_ECN
4303 /* Set ECE for SYN-ACK if peer supports ECN. */
4304 if (tcp_do_ecn && (sc->sc_flags & SCF_ECN_PERMIT))
4305 th->th_flags |= TH_ECE;
4306 #endif
4307 th->th_win = htons(sc->sc_win);
4308 /* th_sum already 0 */
4309 /* th_urp already 0 */
4310
4311 /* Tack on the TCP options. */
4312 optp = (u_int8_t *)(th + 1);
4313 *optp++ = TCPOPT_MAXSEG;
4314 *optp++ = 4;
4315 *optp++ = (sc->sc_ourmaxseg >> 8) & 0xff;
4316 *optp++ = sc->sc_ourmaxseg & 0xff;
4317
4318 #ifdef TCP_SACK
4319 /* Include SACK_PERMIT_HDR option if peer has already done so.
4320 * do here only if sackOK can't be packed in timestamp block */
4321 if (sc->sc_flags & SCF_SACK_PERMIT &&
4322 (stdbsdtcp || !(sc->sc_flags & SCF_TIMESTAMP))) {
4323 *((u_int32_t *)optp) = htonl(TCPOPT_SACK_PERMIT_HDR);
4324 optp += 4;
4325 }
4326 #endif
4327
4328 if (sc->sc_request_r_scale != 15) {
4329 *((u_int32_t *)optp) = htonl(TCPOPT_NOP << 24 |
4330 TCPOPT_WINDOW << 16 | TCPOLEN_WINDOW << 8 |
4331 sc->sc_request_r_scale);
4332 optp += 4;
4333 }
4334
4335 if (sc->sc_flags & SCF_TIMESTAMP) {
4336 u_int32_t *lp = (u_int32_t *)optp;
4337 /* Form timestamp option as shown in appendix A of RFC 1323. */
4338 *lp = htonl(TCPOPT_TSTAMP_HDR);
4339 #ifdef TCP_SACK
4340 if (!stdbsdtcp && sc->sc_flags & SCF_SACK_PERMIT)
4341 /* set SACK_PERMIT option insted of NOP NOP */
4342 *(u_int16_t*)lp = htons(TCPOPT_SACK_PERMITTED << 8 |
4343 TCPOLEN_SACK_PERMITTED);
4344 #endif
4345 *++lp = htonl(SYN_CACHE_TIMESTAMP(sc));
4346 *++lp = htonl(sc->sc_timestamp);
4347 optp += TCPOLEN_TSTAMP_APPA;
4348 }
4349
4350 #ifdef TCP_SIGNATURE
4351 if (sc->sc_flags & SCF_SIGNATURE) {
4352 MD5_CTX ctx;
4353 union sockaddr_union src, dst;
4354 struct tdb *tdb;
4355
4356 bzero(&src, sizeof(union sockaddr_union));
4357 bzero(&dst, sizeof(union sockaddr_union));
4358 src.sa.sa_len = sc->sc_src.sa.sa_len;
4359 src.sa.sa_family = sc->sc_src.sa.sa_family;
4360 dst.sa.sa_len = sc->sc_dst.sa.sa_len;
4361 dst.sa.sa_family = sc->sc_dst.sa.sa_family;
4362
4363 switch (sc->sc_src.sa.sa_family) {
4364 case 0: /*default to PF_INET*/
4365 #ifdef INET
4366 case AF_INET:
4367 src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
4368 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
4369 break;
4370 #endif /* INET */
4371 #ifdef INET6
4372 case AF_INET6:
4373 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
4374 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
4375 break;
4376 #endif /* INET6 */
4377 }
4378
4379 tdb = gettdbbysrcdst(0, &src, &dst, IPPROTO_TCP);
4380 if (tdb == NULL) {
4381 if (m)
4382 m_freem(m);
4383 return (EPERM);
4384 }
4385
4386 MD5Init(&ctx);
4387
4388 switch (sc->sc_src.sa.sa_family) {
4389 case 0: /*default to PF_INET*/
4390 #ifdef INET
4391 case AF_INET:
4392 {
4393 struct ippseudo ippseudo;
4394
4395 ippseudo.ippseudo_src = ip->ip_src;
4396 ippseudo.ippseudo_dst = ip->ip_dst;
4397 ippseudo.ippseudo_pad = 0;
4398 ippseudo.ippseudo_p = IPPROTO_TCP;
4399 ippseudo.ippseudo_len = htons(tlen - hlen);
4400
4401 MD5Update(&ctx, (char *)&ippseudo,
4402 sizeof(struct ippseudo));
4403
4404 }
4405 break;
4406 #endif /* INET */
4407 #ifdef INET6
4408 case AF_INET6:
4409 {
4410 struct ip6_hdr_pseudo ip6pseudo;
4411
4412 bzero(&ip6pseudo, sizeof(ip6pseudo));
4413 ip6pseudo.ip6ph_src = ip6->ip6_src;
4414 ip6pseudo.ip6ph_dst = ip6->ip6_dst;
4415 in6_clearscope(&ip6pseudo.ip6ph_src);
4416 in6_clearscope(&ip6pseudo.ip6ph_dst);
4417 ip6pseudo.ip6ph_nxt = IPPROTO_TCP;
4418 ip6pseudo.ip6ph_len = htonl(tlen - hlen);
4419
4420 MD5Update(&ctx, (char *)&ip6pseudo,
4421 sizeof(ip6pseudo));
4422 }
4423 break;
4424 #endif /* INET6 */
4425 }
4426
4427 th->th_sum = 0;
4428 MD5Update(&ctx, (char *)th, sizeof(struct tcphdr));
4429 MD5Update(&ctx, tdb->tdb_amxkey, tdb->tdb_amxkeylen);
4430
4431 /* Send signature option */
4432 *(optp++) = TCPOPT_SIGNATURE;
4433 *(optp++) = TCPOLEN_SIGNATURE;
4434
4435 MD5Final(optp, &ctx);
4436 optp += 16;
4437
4438 /* Pad options list to the next 32 bit boundary and
4439 * terminate it.
4440 */
4441 *optp++ = TCPOPT_NOP;
4442 *optp++ = TCPOPT_EOL;
4443 }
4444 #endif /* TCP_SIGNATURE */
4445
4446 /* Compute the packet's checksum. */
4447 switch (sc->sc_src.sa.sa_family) {
4448 case AF_INET:
4449 ip->ip_len = htons(tlen - hlen);
4450 th->th_sum = 0;
4451 th->th_sum = in_cksum(m, tlen);
4452 break;
4453 #ifdef INET6
4454 case AF_INET6:
4455 ip6->ip6_plen = htons(tlen - hlen);
4456 th->th_sum = 0;
4457 th->th_sum = in6_cksum(m, IPPROTO_TCP, hlen, tlen - hlen);
4458 break;
4459 #endif
4460 }
4461
4462 /*
4463 * Fill in some straggling IP bits. Note the stack expects
4464 * ip_len to be in host order, for convenience.
4465 */
4466 switch (sc->sc_src.sa.sa_family) {
4467 #ifdef INET
4468 case AF_INET:
4469 ip->ip_len = htons(tlen);
4470 ip->ip_ttl = ip_defttl;
4471 /* XXX tos? */
4472 break;
4473 #endif
4474 #ifdef INET6
4475 case AF_INET6:
4476 ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
4477 ip6->ip6_vfc |= IPV6_VERSION;
4478 ip6->ip6_plen = htons(tlen - hlen);
4479 /* ip6_hlim will be initialized afterwards */
4480 /* leave flowlabel = 0, it is legal and require no state mgmt */
4481 break;
4482 #endif
4483 }
4484
4485 /* use IPsec policy from listening socket, on SYN ACK */
4486 inp = sc->sc_tp ? sc->sc_tp->t_inpcb : NULL;
4487
4488 switch (sc->sc_src.sa.sa_family) {
4489 #ifdef INET
4490 case AF_INET:
4491 error = ip_output(m, sc->sc_ipopts, ro,
4492 (ip_mtudisc ? IP_MTUDISC : 0),
4493 (struct ip_moptions *)NULL, inp);
4494 break;
4495 #endif
4496 #ifdef INET6
4497 case AF_INET6:
4498 ip6->ip6_hlim = in6_selecthlim(NULL,
4499 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
4500
4501 error = ip6_output(m, NULL /*XXX*/, (struct route_in6 *)ro, 0,
4502 (struct ip6_moptions *)0, NULL);
4503 break;
4504 #endif
4505 default:
4506 error = EAFNOSUPPORT;
4507 break;
4508 }
4509 return (error);
4510 }
4511