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