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