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.19 2004/03/08 19:44:32 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: /* It's new. Try find the ambushing socket. */
579: cpu = tcp_addrcpu(ip->ip_src.s_addr, th->th_sport,
580: next_hop->sin_addr.s_addr,
581: next_hop->sin_port ?
582: ntohs(next_hop->sin_port) :
583: th->th_dport);
584: inp = in_pcblookup_hash(&tcbinfo[cpu],
585: ip->ip_src, th->th_sport,
586: next_hop->sin_addr,
587: next_hop->sin_port ?
588: ntohs(next_hop->sin_port) :
589: th->th_dport,
590: 1, m->m_pkthdr.rcvif);
591: }
592: } else {
593: if (isipv6)
594: inp = in6_pcblookup_hash(&tcbinfo[0],
595: &ip6->ip6_src, th->th_sport,
596: &ip6->ip6_dst, th->th_dport,
597: 1, m->m_pkthdr.rcvif);
598: else
599: inp = in_pcblookup_hash(&tcbinfo[mycpu->gd_cpuid],
600: ip->ip_src, th->th_sport,
601: ip->ip_dst, th->th_dport,
602: 1, m->m_pkthdr.rcvif);
603: }
604:
605: #ifdef IPSEC
606: if (isipv6) {
607: if (inp != NULL && ipsec6_in_reject_so(m, inp->inp_socket)) {
608: ipsec6stat.in_polvio++;
609: goto drop;
610: }
611: } else {
612: if (inp != NULL && ipsec4_in_reject_so(m, inp->inp_socket)) {
613: ipsecstat.in_polvio++;
614: goto drop;
615: }
616: }
617: #endif
618: #ifdef FAST_IPSEC
619: if (isipv6) {
620: if (inp != NULL && ipsec6_in_reject(m, inp)) {
621: goto drop;
622: }
623: } else {
624: if (inp != NULL && ipsec4_in_reject(m, inp)) {
625: goto drop;
626: }
627: }
628: #endif
629:
630: /*
631: * If the state is CLOSED (i.e., TCB does not exist) then
632: * all data in the incoming segment is discarded.
633: * If the TCB exists but is in CLOSED state, it is embryonic,
634: * but should either do a listen or a connect soon.
635: */
636: if (inp == NULL) {
637: if (log_in_vain) {
638: #ifdef INET6
639: char dbuf[INET6_ADDRSTRLEN+2], sbuf[INET6_ADDRSTRLEN+2];
640: #else
641: char dbuf[4*sizeof "123"], sbuf[4*sizeof "123"];
642: #endif
643: if (isipv6) {
644: strcpy(dbuf, "[");
645: strcpy(sbuf, "[");
646: strcat(dbuf, ip6_sprintf(&ip6->ip6_dst));
647: strcat(sbuf, ip6_sprintf(&ip6->ip6_src));
648: strcat(dbuf, "]");
649: strcat(sbuf, "]");
650: } else {
651: strcpy(dbuf, inet_ntoa(ip->ip_dst));
652: strcpy(sbuf, inet_ntoa(ip->ip_src));
653: }
654: switch (log_in_vain) {
655: case 1:
656: if ((thflags & TH_SYN) == 0)
657: break;
658: case 2:
659: log(LOG_INFO,
660: "Connection attempt to TCP %s:%d "
661: "from %s:%d flags:0x%02x\n",
662: dbuf, ntohs(th->th_dport), sbuf,
663: ntohs(th->th_sport), thflags);
664: break;
665: default:
666: break;
667: }
668: }
669: if (blackhole) {
670: switch (blackhole) {
671: case 1:
672: if (thflags & TH_SYN)
673: goto drop;
674: break;
675: case 2:
676: goto drop;
677: default:
678: goto drop;
679: }
680: }
681: rstreason = BANDLIM_RST_CLOSEDPORT;
682: goto dropwithreset;
683: }
684: tp = intotcpcb(inp);
685: if (tp == NULL) {
686: rstreason = BANDLIM_RST_CLOSEDPORT;
687: goto dropwithreset;
688: }
689: if (tp->t_state == TCPS_CLOSED)
690: goto drop;
691:
692: /* Unscale the window into a 32-bit value. */
693: if ((thflags & TH_SYN) == 0)
694: tiwin = th->th_win << tp->snd_scale;
695: else
696: tiwin = th->th_win;
697:
698: so = inp->inp_socket;
699:
700: #ifdef TCPDEBUG
701: if (so->so_options & SO_DEBUG) {
702: ostate = tp->t_state;
703: if (isipv6)
704: bcopy((char *)ip6, (char *)tcp_saveipgen, sizeof(*ip6));
705: else
706: bcopy((char *)ip, (char *)tcp_saveipgen, sizeof(*ip));
707: tcp_savetcp = *th;
708: }
709: #endif
710:
711: if (so->so_options & SO_ACCEPTCONN) {
712: struct in_conninfo inc;
713:
714: #ifdef INET6
715: inc.inc_isipv6 = isipv6;
716: #endif
717: if (isipv6) {
718: inc.inc6_faddr = ip6->ip6_src;
719: inc.inc6_laddr = ip6->ip6_dst;
720: inc.inc6_route.ro_rt = NULL; /* XXX */
721: } else {
722: inc.inc_faddr = ip->ip_src;
723: inc.inc_laddr = ip->ip_dst;
724: inc.inc_route.ro_rt = NULL; /* XXX */
725: }
726: inc.inc_fport = th->th_sport;
727: inc.inc_lport = th->th_dport;
728:
729: /*
730: * If the state is LISTEN then ignore segment if it contains
731: * a RST. If the segment contains an ACK then it is bad and
732: * send a RST. If it does not contain a SYN then it is not
733: * interesting; drop it.
734: *
735: * If the state is SYN_RECEIVED (syncache) and seg contains
736: * an ACK, but not for our SYN/ACK, send a RST. If the seg
737: * contains a RST, check the sequence number to see if it
738: * is a valid reset segment.
739: */
740: if ((thflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
741: if ((thflags & (TH_RST|TH_ACK|TH_SYN)) == TH_ACK) {
742: if (!syncache_expand(&inc, th, &so, m)) {
743: /*
744: * No syncache entry, or ACK was not
745: * for our SYN/ACK. Send a RST.
746: */
747: tcpstat.tcps_badsyn++;
748: rstreason = BANDLIM_RST_OPENPORT;
749: goto dropwithreset;
750: }
751: if (so == NULL)
752: /*
753: * Could not complete 3-way handshake,
754: * connection is being closed down, and
755: * syncache will free mbuf.
756: */
757: return;
758: /*
759: * Socket is created in state SYN_RECEIVED.
760: * Continue processing segment.
761: */
762: inp = sotoinpcb(so);
763: tp = intotcpcb(inp);
764: /*
765: * This is what would have happened in
766: * tcp_output() when the SYN,ACK was sent.
767: */
768: tp->snd_up = tp->snd_una;
769: tp->snd_max = tp->snd_nxt = tp->iss + 1;
770: tp->last_ack_sent = tp->rcv_nxt;
771: /*
772: * XXX possible bug - it doesn't appear that tp->snd_wnd is unscaled
773: * until the _second_ ACK is received:
774: * rcv SYN (set wscale opts) --> send SYN/ACK, set snd_wnd = window.
775: * rcv ACK, calculate tiwin --> process SYN_RECEIVED, determine wscale,
776: * move to ESTAB, set snd_wnd to tiwin.
777: */
778: tp->snd_wnd = tiwin; /* unscaled */
779: goto after_listen;
780: }
781: if (thflags & TH_RST) {
782: syncache_chkrst(&inc, th);
783: goto drop;
784: }
785: if (thflags & TH_ACK) {
786: syncache_badack(&inc);
787: tcpstat.tcps_badsyn++;
788: rstreason = BANDLIM_RST_OPENPORT;
789: goto dropwithreset;
790: }
791: goto drop;
792: }
793:
794: /*
795: * Segment's flags are (SYN) or (SYN|FIN).
796: */
797: #ifdef INET6
798: /*
799: * If deprecated address is forbidden,
800: * we do not accept SYN to deprecated interface
801: * address to prevent any new inbound connection from
802: * getting established.
803: * When we do not accept SYN, we send a TCP RST,
804: * with deprecated source address (instead of dropping
805: * it). We compromise it as it is much better for peer
806: * to send a RST, and RST will be the final packet
807: * for the exchange.
808: *
809: * If we do not forbid deprecated addresses, we accept
810: * the SYN packet. RFC2462 does not suggest dropping
811: * SYN in this case.
812: * If we decipher RFC2462 5.5.4, it says like this:
813: * 1. use of deprecated addr with existing
814: * communication is okay - "SHOULD continue to be
815: * used"
816: * 2. use of it with new communication:
817: * (2a) "SHOULD NOT be used if alternate address
818: * with sufficient scope is available"
819: * (2b) nothing mentioned otherwise.
820: * Here we fall into (2b) case as we have no choice in
821: * our source address selection - we must obey the peer.
822: *
823: * The wording in RFC2462 is confusing, and there are
824: * multiple description text for deprecated address
825: * handling - worse, they are not exactly the same.
826: * I believe 5.5.4 is the best one, so we follow 5.5.4.
827: */
828: if (isipv6 && !ip6_use_deprecated) {
829: struct in6_ifaddr *ia6;
830:
831: if ((ia6 = ip6_getdstifaddr(m)) &&
832: (ia6->ia6_flags & IN6_IFF_DEPRECATED)) {
833: tp = NULL;
834: rstreason = BANDLIM_RST_OPENPORT;
835: goto dropwithreset;
836: }
837: }
838: #endif
839: /*
840: * If it is from this socket, drop it, it must be forged.
841: * Don't bother responding if the destination was a broadcast.
842: */
843: if (th->th_dport == th->th_sport) {
844: if (isipv6) {
845: if (IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst,
846: &ip6->ip6_src))
847: goto drop;
848: } else {
849: if (ip->ip_dst.s_addr == ip->ip_src.s_addr)
850: goto drop;
851: }
852: }
853: /*
854: * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
855: *
856: * Note that it is quite possible to receive unicast
857: * link-layer packets with a broadcast IP address. Use
858: * in_broadcast() to find them.
859: */
860: if (m->m_flags & (M_BCAST|M_MCAST))
861: goto drop;
862: if (isipv6) {
863: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
864: IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
865: goto drop;
866: } else {
867: if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
868: IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
869: ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
870: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
871: goto drop;
872: }
873: /*
874: * SYN appears to be valid; create compressed TCP state
875: * for syncache, or perform t/tcp connection.
876: */
877: if (so->so_qlen <= so->so_qlimit) {
878: tcp_dooptions(&to, optp, optlen, 1);
879: if (!syncache_add(&inc, &to, th, &so, m))
880: goto drop;
881: if (so == NULL)
882: /*
883: * Entry added to syncache, mbuf used to
884: * send SYN,ACK packet.
885: */
886: return;
887: /*
888: * Segment passed TAO tests.
889: */
890: inp = sotoinpcb(so);
891: tp = intotcpcb(inp);
892: tp->snd_wnd = tiwin;
893: tp->t_starttime = ticks;
894: tp->t_state = TCPS_ESTABLISHED;
895:
896: /*
897: * If there is a FIN, or if there is data and the
898: * connection is local, then delay SYN,ACK(SYN) in
899: * the hope of piggy-backing it on a response
900: * segment. Otherwise must send ACK now in case
901: * the other side is slow starting.
902: */
903: if (DELAY_ACK(tp) &&
904: ((thflags & TH_FIN) ||
905: (tlen != 0 &&
906: ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
907: (!isipv6 && in_localaddr(inp->inp_faddr)))))) {
908: callout_reset(tp->tt_delack, tcp_delacktime,
909: tcp_timer_delack, tp);
910: tp->t_flags |= TF_NEEDSYN;
911: } else
912: tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
913:
914: tcpstat.tcps_connects++;
915: soisconnected(so);
916: goto trimthenstep6;
917: }
918: goto drop;
919: }
920: after_listen:
921:
922: /* XXX temp debugging */
923: /* should not happen - syncache should pick up these connections */
924: if (tp->t_state == TCPS_LISTEN)
925: panic("tcp_input: TCPS_LISTEN");
926:
927: /*
928: * Segment received on connection.
929: * Reset idle time and keep-alive timer.
930: */
931: tp->t_rcvtime = ticks;
932: if (TCPS_HAVEESTABLISHED(tp->t_state))
933: callout_reset(tp->tt_keep, tcp_keepidle, tcp_timer_keep, tp);
934:
935: /*
936: * Process options.
937: * XXX this is tradtitional behavior, may need to be cleaned up.
938: */
939: tcp_dooptions(&to, optp, optlen, thflags & TH_SYN);
940: if (thflags & TH_SYN) {
941: if (to.to_flags & TOF_SCALE) {
942: tp->t_flags |= TF_RCVD_SCALE;
943: tp->requested_s_scale = to.to_requested_s_scale;
944: }
945: if (to.to_flags & TOF_TS) {
946: tp->t_flags |= TF_RCVD_TSTMP;
947: tp->ts_recent = to.to_tsval;
948: tp->ts_recent_age = ticks;
949: }
950: if (to.to_flags & (TOF_CC|TOF_CCNEW))
951: tp->t_flags |= TF_RCVD_CC;
952: if (to.to_flags & TOF_MSS)
953: tcp_mss(tp, to.to_mss);
954: }
955:
956: /*
957: * Header prediction: check for the two common cases
958: * of a uni-directional data xfer. If the packet has
959: * no control flags, is in-sequence, the window didn't
960: * change and we're not retransmitting, it's a
961: * candidate. If the length is zero and the ack moved
962: * forward, we're the sender side of the xfer. Just
963: * free the data acked & wake any higher level process
964: * that was blocked waiting for space. If the length
965: * is non-zero and the ack didn't move, we're the
966: * receiver side. If we're getting packets in-order
967: * (the reassembly queue is empty), add the data to
968: * the socket buffer and note that we need a delayed ack.
969: * Make sure that the hidden state-flags are also off.
970: * Since we check for TCPS_ESTABLISHED above, it can only
971: * be TH_NEEDSYN.
972: */
973: if (tp->t_state == TCPS_ESTABLISHED &&
974: (thflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
975: ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
976: ((to.to_flags & TOF_TS) == 0 ||
977: TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
978: /*
979: * Using the CC option is compulsory if once started:
980: * the segment is OK if no T/TCP was negotiated or
981: * if the segment has a CC option equal to CCrecv
982: */
983: ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
984: ((to.to_flags & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
985: th->th_seq == tp->rcv_nxt &&
986: tiwin && tiwin == tp->snd_wnd &&
987: tp->snd_nxt == tp->snd_max) {
988:
989: /*
990: * If last ACK falls within this segment's sequence numbers,
991: * record the timestamp.
992: * NOTE that the test is modified according to the latest
993: * proposal of the tcplw@cray.com list (Braden 1993/04/26).
994: */
995: if ((to.to_flags & TOF_TS) != 0 &&
996: SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
997: tp->ts_recent_age = ticks;
998: tp->ts_recent = to.to_tsval;
999: }
1000:
1001: if (tlen == 0) {
1002: if (SEQ_GT(th->th_ack, tp->snd_una) &&
1003: SEQ_LEQ(th->th_ack, tp->snd_max) &&
1004: tp->snd_cwnd >= tp->snd_wnd &&
1005: ((!tcp_do_newreno &&
1006: tp->t_dupacks < tcprexmtthresh) ||
1007: (tcp_do_newreno && !IN_FASTRECOVERY(tp)))) {
1008: /*
1009: * this is a pure ack for outstanding data.
1010: */
1011: ++tcpstat.tcps_predack;
1012: /*
1013: * "bad retransmit" recovery
1014: *
1015: * If Eifel detection applies, then
1016: * it is deterministic, so use it
1017: * unconditionally over the old heuristic.
1018: * Otherwise, fall back to the old heuristic.
1019: */
1020: if (tcp_do_eifel_detect &&
1021: (to.to_flags & TOF_TS) && to.to_tsecr &&
1022: (tp->t_flags & TF_FIRSTACCACK)) {
1023: /* Eifel detection applicable. */
1024: if (to.to_tsecr < tp->t_rexmtTS) {
1025: tcp_revert_congestion_state(tp);
1026: ++tcpstat.tcps_eifeldetected;
1027: }
1028: } else if (tp->t_rxtshift == 1 &&
1029: ticks < tp->t_badrxtwin) {
1030: tcp_revert_congestion_state(tp);
1031: ++tcpstat.tcps_rttdetected;
1032: }
1033: tp->t_flags &= ~(TF_FIRSTACCACK |
1034: TF_FASTREXMT | TF_EARLYREXMT);
1035: /*
1036: * Recalculate the retransmit timer / rtt.
1037: *
1038: * Some machines (certain windows boxes)
1039: * send broken timestamp replies during the
1040: * SYN+ACK phase, ignore timestamps of 0.
1041: */
1042: if ((to.to_flags & TOF_TS) != 0 &&
1043: to.to_tsecr) {
1044: tcp_xmit_timer(tp,
1045: ticks - to.to_tsecr + 1);
1046: } else if (tp->t_rtttime &&
1047: SEQ_GT(th->th_ack, tp->t_rtseq)) {
1048: tcp_xmit_timer(tp,
1049: ticks - tp->t_rtttime);
1050: }
1051: tcp_xmit_bandwidth_limit(tp, th->th_ack);
1052: acked = th->th_ack - tp->snd_una;
1053: tcpstat.tcps_rcvackpack++;
1054: tcpstat.tcps_rcvackbyte += acked;
1055: sbdrop(&so->so_snd, acked);
1056: if (SEQ_GT(tp->snd_una, tp->snd_recover) &&
1057: SEQ_LEQ(th->th_ack, tp->snd_recover))
1058: tp->snd_recover = th->th_ack - 1;
1059: tp->snd_una = th->th_ack;
1060: tp->t_dupacks = 0;
1061: m_freem(m);
1062: ND6_HINT(tp); /* some progress has been done */
1063:
1064: /*
1065: * If all outstanding data are acked, stop
1066: * retransmit timer, otherwise restart timer
1067: * using current (possibly backed-off) value.
1068: * If process is waiting for space,
1069: * wakeup/selwakeup/signal. If data
1070: * are ready to send, let tcp_output
1071: * decide between more output or persist.
1072: */
1073: if (tp->snd_una == tp->snd_max)
1074: callout_stop(tp->tt_rexmt);
1075: else if (!callout_active(tp->tt_persist))
1076: callout_reset(tp->tt_rexmt,
1077: tp->t_rxtcur,
1078: tcp_timer_rexmt, tp);
1079:
1080: sowwakeup(so);
1081: if (so->so_snd.sb_cc)
1082: (void) tcp_output(tp);
1083: return;
1084: }
1085: } else if (th->th_ack == tp->snd_una &&
1086: LIST_EMPTY(&tp->t_segq) &&
1087: tlen <= sbspace(&so->so_rcv)) {
1088: /*
1089: * this is a pure, in-sequence data packet
1090: * with nothing on the reassembly queue and
1091: * we have enough buffer space to take it.
1092: */
1093: ++tcpstat.tcps_preddat;
1094: tp->rcv_nxt += tlen;
1095: tcpstat.tcps_rcvpack++;
1096: tcpstat.tcps_rcvbyte += tlen;
1097: ND6_HINT(tp); /* some progress has been done */
1098: /*
1099: * Add data to socket buffer.
1100: */
1101: if (so->so_state & SS_CANTRCVMORE) {
1102: m_freem(m);
1103: } else {
1104: m_adj(m, drop_hdrlen); /* delayed header drop */
1105: sbappend(&so->so_rcv, m);
1106: }
1107: sorwakeup(so);
1108: if (DELAY_ACK(tp)) {
1109: callout_reset(tp->tt_delack, tcp_delacktime,
1110: tcp_timer_delack, tp);
1111: } else {
1112: tp->t_flags |= TF_ACKNOW;
1113: tcp_output(tp);
1114: }
1115: return;
1116: }
1117: }
1118:
1119: /*
1120: * Calculate amount of space in receive window,
1121: * and then do TCP input processing.
1122: * Receive window is amount of space in rcv queue,
1123: * but not less than advertised window.
1124: */
1125: { int win;
1126:
1127: win = sbspace(&so->so_rcv);
1128: if (win < 0)
1129: win = 0;
1130: tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
1131: }
1132:
1133: switch (tp->t_state) {
1134:
1135: /*
1136: * If the state is SYN_RECEIVED:
1137: * if seg contains an ACK, but not for our SYN/ACK, send a RST.
1138: */
1139: case TCPS_SYN_RECEIVED:
1140: if ((thflags & TH_ACK) &&
1141: (SEQ_LEQ(th->th_ack, tp->snd_una) ||
1142: SEQ_GT(th->th_ack, tp->snd_max))) {
1143: rstreason = BANDLIM_RST_OPENPORT;
1144: goto dropwithreset;
1145: }
1146: break;
1147:
1148: /*
1149: * If the state is SYN_SENT:
1150: * if seg contains an ACK, but not for our SYN, drop the input.
1151: * if seg contains a RST, then drop the connection.
1152: * if seg does not contain SYN, then drop it.
1153: * Otherwise this is an acceptable SYN segment
1154: * initialize tp->rcv_nxt and tp->irs
1155: * if seg contains ack then advance tp->snd_una
1156: * if SYN has been acked change to ESTABLISHED else SYN_RCVD state
1157: * arrange for segment to be acked (eventually)
1158: * continue processing rest of data/controls, beginning with URG
1159: */
1160: case TCPS_SYN_SENT:
1161: if ((taop = tcp_gettaocache(&inp->inp_inc)) == NULL) {
1162: taop = &tao_noncached;
1163: bzero(taop, sizeof(*taop));
1164: }
1165:
1166: if ((thflags & TH_ACK) &&
1167: (SEQ_LEQ(th->th_ack, tp->iss) ||
1168: SEQ_GT(th->th_ack, tp->snd_max))) {
1169: /*
1170: * If we have a cached CCsent for the remote host,
1171: * hence we haven't just crashed and restarted,
1172: * do not send a RST. This may be a retransmission
1173: * from the other side after our earlier ACK was lost.
1174: * Our new SYN, when it arrives, will serve as the
1175: * needed ACK.
1176: */
1177: if (taop->tao_ccsent != 0)
1178: goto drop;
1179: else {
1180: rstreason = BANDLIM_UNLIMITED;
1181: goto dropwithreset;
1182: }
1183: }
1184: if (thflags & TH_RST) {
1185: if (thflags & TH_ACK)
1186: tp = tcp_drop(tp, ECONNREFUSED);
1187: goto drop;
1188: }
1189: if ((thflags & TH_SYN) == 0)
1190: goto drop;
1191: tp->snd_wnd = th->th_win; /* initial send window */
1192: tp->cc_recv = to.to_cc; /* foreign CC */
1193:
1194: tp->irs = th->th_seq;
1195: tcp_rcvseqinit(tp);
1196: if (thflags & TH_ACK) {
1197: /*
1198: * Our SYN was acked. If segment contains CC.ECHO
1199: * option, check it to make sure this segment really
1200: * matches our SYN. If not, just drop it as old
1201: * duplicate, but send an RST if we're still playing
1202: * by the old rules. If no CC.ECHO option, make sure
1203: * we don't get fooled into using T/TCP.
1204: */
1205: if (to.to_flags & TOF_CCECHO) {
1206: if (tp->cc_send != to.to_ccecho) {
1207: if (taop->tao_ccsent != 0)
1208: goto drop;
1209: else {
1210: rstreason = BANDLIM_UNLIMITED;
1211: goto dropwithreset;
1212: }
1213: }
1214: } else
1215: tp->t_flags &= ~TF_RCVD_CC;
1216: tcpstat.tcps_connects++;
1217: soisconnected(so);
1218: /* Do window scaling on this connection? */
1219: if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1220: (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1221: tp->snd_scale = tp->requested_s_scale;
1222: tp->rcv_scale = tp->request_r_scale;
1223: }
1224: /* Segment is acceptable, update cache if undefined. */
1225: if (taop->tao_ccsent == 0)
1226: taop->tao_ccsent = to.to_ccecho;
1227:
1228: tp->rcv_adv += tp->rcv_wnd;
1229: tp->snd_una++; /* SYN is acked */
1230: /*
1231: * If there's data, delay ACK; if there's also a FIN
1232: * ACKNOW will be turned on later.
1233: */
1234: if (DELAY_ACK(tp) && tlen != 0)
1235: callout_reset(tp->tt_delack, tcp_delacktime,
1236: tcp_timer_delack, tp);
1237: else
1238: tp->t_flags |= TF_ACKNOW;
1239: /*
1240: * Received <SYN,ACK> in SYN_SENT[*] state.
1241: * Transitions:
1242: * SYN_SENT --> ESTABLISHED
1243: * SYN_SENT* --> FIN_WAIT_1
1244: */
1245: tp->t_starttime = ticks;
1246: if (tp->t_flags & TF_NEEDFIN) {
1247: tp->t_state = TCPS_FIN_WAIT_1;
1248: tp->t_flags &= ~TF_NEEDFIN;
1249: thflags &= ~TH_SYN;
1250: } else {
1251: tp->t_state = TCPS_ESTABLISHED;
1252: callout_reset(tp->tt_keep, tcp_keepidle,
1253: tcp_timer_keep, tp);
1254: }
1255: } else {
1256: /*
1257: * Received initial SYN in SYN-SENT[*] state =>
1258: * simultaneous open. If segment contains CC option
1259: * and there is a cached CC, apply TAO test.
1260: * If it succeeds, connection is * half-synchronized.
1261: * Otherwise, do 3-way handshake:
1262: * SYN-SENT -> SYN-RECEIVED
1263: * SYN-SENT* -> SYN-RECEIVED*
1264: * If there was no CC option, clear cached CC value.
1265: */
1266: tp->t_flags |= TF_ACKNOW;
1267: callout_stop(tp->tt_rexmt);
1268: if (to.to_flags & TOF_CC) {
1269: if (taop->tao_cc != 0 &&
1270: CC_GT(to.to_cc, taop->tao_cc)) {
1271: /*
1272: * update cache and make transition:
1273: * SYN-SENT -> ESTABLISHED*
1274: * SYN-SENT* -> FIN-WAIT-1*
1275: */
1276: taop->tao_cc = to.to_cc;
1277: tp->t_starttime = ticks;
1278: if (tp->t_flags & TF_NEEDFIN) {
1279: tp->t_state = TCPS_FIN_WAIT_1;
1280: tp->t_flags &= ~TF_NEEDFIN;
1281: } else {
1282: tp->t_state = TCPS_ESTABLISHED;
1283: callout_reset(tp->tt_keep,
1284: tcp_keepidle,
1285: tcp_timer_keep,
1286: tp);
1287: }
1288: tp->t_flags |= TF_NEEDSYN;
1289: } else
1290: tp->t_state = TCPS_SYN_RECEIVED;
1291: } else {
1292: /* CC.NEW or no option => invalidate cache */
1293: taop->tao_cc = 0;
1294: tp->t_state = TCPS_SYN_RECEIVED;
1295: }
1296: }
1297:
1298: trimthenstep6:
1299: /*
1300: * Advance th->th_seq to correspond to first data byte.
1301: * If data, trim to stay within window,
1302: * dropping FIN if necessary.
1303: */
1304: th->th_seq++;
1305: if (tlen > tp->rcv_wnd) {
1306: todrop = tlen - tp->rcv_wnd;
1307: m_adj(m, -todrop);
1308: tlen = tp->rcv_wnd;
1309: thflags &= ~TH_FIN;
1310: tcpstat.tcps_rcvpackafterwin++;
1311: tcpstat.tcps_rcvbyteafterwin += todrop;
1312: }
1313: tp->snd_wl1 = th->th_seq - 1;
1314: tp->rcv_up = th->th_seq;
1315: /*
1316: * Client side of transaction: already sent SYN and data.
1317: * If the remote host used T/TCP to validate the SYN,
1318: * our data will be ACK'd; if so, enter normal data segment
1319: * processing in the middle of step 5, ack processing.
1320: * Otherwise, goto step 6.
1321: */
1322: if (thflags & TH_ACK)
1323: goto process_ACK;
1324:
1325: goto step6;
1326:
1327: /*
1328: * If the state is LAST_ACK or CLOSING or TIME_WAIT:
1329: * if segment contains a SYN and CC [not CC.NEW] option:
1330: * if state == TIME_WAIT and connection duration > MSL,
1331: * drop packet and send RST;
1332: *
1333: * if SEG.CC > CCrecv then is new SYN, and can implicitly
1334: * ack the FIN (and data) in retransmission queue.
1335: * Complete close and delete TCPCB. Then reprocess
1336: * segment, hoping to find new TCPCB in LISTEN state;
1337: *
1338: * else must be old SYN; drop it.
1339: * else do normal processing.
1340: */
1341: case TCPS_LAST_ACK:
1342: case TCPS_CLOSING:
1343: case TCPS_TIME_WAIT:
1344: if ((thflags & TH_SYN) &&
1345: (to.to_flags & TOF_CC) && tp->cc_recv != 0) {
1346: if (tp->t_state == TCPS_TIME_WAIT &&
1347: (ticks - tp->t_starttime) > tcp_msl) {
1348: rstreason = BANDLIM_UNLIMITED;
1349: goto dropwithreset;
1350: }
1351: if (CC_GT(to.to_cc, tp->cc_recv)) {
1352: tp = tcp_close(tp);
1353: goto findpcb;
1354: }
1355: else
1356: goto drop;
1357: }
1358: break; /* continue normal processing */
1359: }
1360:
1361: /*
1362: * States other than LISTEN or SYN_SENT.
1363: * First check the RST flag and sequence number since reset segments
1364: * are exempt from the timestamp and connection count tests. This
1365: * fixes a bug introduced by the Stevens, vol. 2, p. 960 bugfix
1366: * below which allowed reset segments in half the sequence space
1367: * to fall though and be processed (which gives forged reset
1368: * segments with a random sequence number a 50 percent chance of
1369: * killing a connection).
1370: * Then check timestamp, if present.
1371: * Then check the connection count, if present.
1372: * Then check that at least some bytes of segment are within
1373: * receive window. If segment begins before rcv_nxt,
1374: * drop leading data (and SYN); if nothing left, just ack.
1375: *
1376: *
1377: * If the RST bit is set, check the sequence number to see
1378: * if this is a valid reset segment.
1379: * RFC 793 page 37:
1380: * In all states except SYN-SENT, all reset (RST) segments
1381: * are validated by checking their SEQ-fields. A reset is
1382: * valid if its sequence number is in the window.
1383: * Note: this does not take into account delayed ACKs, so
1384: * we should test against last_ack_sent instead of rcv_nxt.
1385: * The sequence number in the reset segment is normally an
1386: * echo of our outgoing acknowlegement numbers, but some hosts
1387: * send a reset with the sequence number at the rightmost edge
1388: * of our receive window, and we have to handle this case.
1389: * If we have multiple segments in flight, the intial reset
1390: * segment sequence numbers will be to the left of last_ack_sent,
1391: * but they will eventually catch up.
1392: * In any case, it never made sense to trim reset segments to
1393: * fit the receive window since RFC 1122 says:
1394: * 4.2.2.12 RST Segment: RFC-793 Section 3.4
1395: *
1396: * A TCP SHOULD allow a received RST segment to include data.
1397: *
1398: * DISCUSSION
1399: * It has been suggested that a RST segment could contain
1400: * ASCII text that encoded and explained the cause of the
1401: * RST. No standard has yet been established for such
1402: * data.
1403: *
1404: * If the reset segment passes the sequence number test examine
1405: * the state:
1406: * SYN_RECEIVED STATE:
1407: * If passive open, return to LISTEN state.
1408: * If active open, inform user that connection was refused.
1409: * ESTABLISHED, FIN_WAIT_1, FIN_WAIT_2, CLOSE_WAIT STATES:
1410: * Inform user that connection was reset, and close tcb.
1411: * CLOSING, LAST_ACK STATES:
1412: * Close the tcb.
1413: * TIME_WAIT STATE:
1414: * Drop the segment - see Stevens, vol. 2, p. 964 and
1415: * RFC 1337.
1416: */
1417: if (thflags & TH_RST) {
1418: if (SEQ_GEQ(th->th_seq, tp->last_ack_sent) &&
1419: SEQ_LT(th->th_seq, tp->last_ack_sent + tp->rcv_wnd)) {
1420: switch (tp->t_state) {
1421:
1422: case TCPS_SYN_RECEIVED:
1423: so->so_error = ECONNREFUSED;
1424: goto close;
1425:
1426: case TCPS_ESTABLISHED:
1427: case TCPS_FIN_WAIT_1:
1428: case TCPS_FIN_WAIT_2:
1429: case TCPS_CLOSE_WAIT:
1430: so->so_error = ECONNRESET;
1431: close:
1432: tp->t_state = TCPS_CLOSED;
1433: tcpstat.tcps_drops++;
1434: tp = tcp_close(tp);
1435: break;
1436:
1437: case TCPS_CLOSING:
1438: case TCPS_LAST_ACK:
1439: tp = tcp_close(tp);
1440: break;
1441:
1442: case TCPS_TIME_WAIT:
1443: break;
1444: }
1445: }
1446: goto drop;
1447: }
1448:
1449: /*
1450: * RFC 1323 PAWS: If we have a timestamp reply on this segment
1451: * and it's less than ts_recent, drop it.
1452: */
1453: if ((to.to_flags & TOF_TS) != 0 && tp->ts_recent &&
1454: TSTMP_LT(to.to_tsval, tp->ts_recent)) {
1455:
1456: /* Check to see if ts_recent is over 24 days old. */
1457: if ((int)(ticks - tp->ts_recent_age) > TCP_PAWS_IDLE) {
1458: /*
1459: * Invalidate ts_recent. If this segment updates
1460: * ts_recent, the age will be reset later and ts_recent
1461: * will get a valid value. If it does not, setting
1462: * ts_recent to zero will at least satisfy the
1463: * requirement that zero be placed in the timestamp
1464: * echo reply when ts_recent isn't valid. The
1465: * age isn't reset until we get a valid ts_recent
1466: * because we don't want out-of-order segments to be
1467: * dropped when ts_recent is old.
1468: */
1469: tp->ts_recent = 0;
1470: } else {
1471: tcpstat.tcps_rcvduppack++;
1472: tcpstat.tcps_rcvdupbyte += tlen;
1473: tcpstat.tcps_pawsdrop++;
1474: if (tlen)
1475: goto dropafterack;
1476: goto drop;
1477: }
1478: }
1479:
1480: /*
1481: * T/TCP mechanism
1482: * If T/TCP was negotiated and the segment doesn't have CC,
1483: * or if its CC is wrong then drop the segment.
1484: * RST segments do not have to comply with this.
1485: */
1486: if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1487: ((to.to_flags & TOF_CC) == 0 || tp->cc_recv != to.to_cc))
1488: goto dropafterack;
1489:
1490: /*
1491: * In the SYN-RECEIVED state, validate that the packet belongs to
1492: * this connection before trimming the data to fit the receive
1493: * window. Check the sequence number versus IRS since we know
1494: * the sequence numbers haven't wrapped. This is a partial fix
1495: * for the "LAND" DoS attack.
1496: */
1497: if (tp->t_state == TCPS_SYN_RECEIVED && SEQ_LT(th->th_seq, tp->irs)) {
1498: rstreason = BANDLIM_RST_OPENPORT;
1499: goto dropwithreset;
1500: }
1501:
1502: todrop = tp->rcv_nxt - th->th_seq;
1503: if (todrop > 0) {
1504: if (thflags & TH_SYN) {
1505: thflags &= ~TH_SYN;
1506: th->th_seq++;
1507: if (th->th_urp > 1)
1508: th->th_urp--;
1509: else
1510: thflags &= ~TH_URG;
1511: todrop--;
1512: }
1513: /*
1514: * Following if statement from Stevens, vol. 2, p. 960.
1515: */
1516: if (todrop > tlen
1517: || (todrop == tlen && (thflags & TH_FIN) == 0)) {
1518: /*
1519: * Any valid FIN must be to the left of the window.
1520: * At this point the FIN must be a duplicate or out
1521: * of sequence; drop it.
1522: */
1523: thflags &= ~TH_FIN;
1524:
1525: /*
1526: * Send an ACK to resynchronize and drop any data.
1527: * But keep on processing for RST or ACK.
1528: */
1529: tp->t_flags |= TF_ACKNOW;
1530: todrop = tlen;
1531: tcpstat.tcps_rcvduppack++;
1532: tcpstat.tcps_rcvdupbyte += todrop;
1533: } else {
1534: tcpstat.tcps_rcvpartduppack++;
1535: tcpstat.tcps_rcvpartdupbyte += todrop;
1536: }
1537: drop_hdrlen += todrop; /* drop from the top afterwards */
1538: th->th_seq += todrop;
1539: tlen -= todrop;
1540: if (th->th_urp > todrop)
1541: th->th_urp -= todrop;
1542: else {
1543: thflags &= ~TH_URG;
1544: th->th_urp = 0;
1545: }
1546: }
1547:
1548: /*
1549: * If new data are received on a connection after the
1550: * user processes are gone, then RST the other end.
1551: */
1552: if ((so->so_state & SS_NOFDREF) &&
1553: tp->t_state > TCPS_CLOSE_WAIT && tlen) {
1554: tp = tcp_close(tp);
1555: tcpstat.tcps_rcvafterclose++;
1556: rstreason = BANDLIM_UNLIMITED;
1557: goto dropwithreset;
1558: }
1559:
1560: /*
1561: * If segment ends after window, drop trailing data
1562: * (and PUSH and FIN); if nothing left, just ACK.
1563: */
1564: todrop = (th->th_seq+tlen) - (tp->rcv_nxt+tp->rcv_wnd);
1565: if (todrop > 0) {
1566: tcpstat.tcps_rcvpackafterwin++;
1567: if (todrop >= tlen) {
1568: tcpstat.tcps_rcvbyteafterwin += tlen;
1569: /*
1570: * If a new connection request is received
1571: * while in TIME_WAIT, drop the old connection
1572: * and start over if the sequence numbers
1573: * are above the previous ones.
1574: */
1575: if (thflags & TH_SYN &&
1576: tp->t_state == TCPS_TIME_WAIT &&
1577: SEQ_GT(th->th_seq, tp->rcv_nxt)) {
1578: tp = tcp_close(tp);
1579: goto findpcb;
1580: }
1581: /*
1582: * If window is closed can only take segments at
1583: * window edge, and have to drop data and PUSH from
1584: * incoming segments. Continue processing, but
1585: * remember to ack. Otherwise, drop segment
1586: * and ack.
1587: */
1588: if (tp->rcv_wnd == 0 && th->th_seq == tp->rcv_nxt) {
1589: tp->t_flags |= TF_ACKNOW;
1590: tcpstat.tcps_rcvwinprobe++;
1591: } else
1592: goto dropafterack;
1593: } else
1594: tcpstat.tcps_rcvbyteafterwin += todrop;
1595: m_adj(m, -todrop);
1596: tlen -= todrop;
1597: thflags &= ~(TH_PUSH|TH_FIN);
1598: }
1599:
1600: /*
1601: * If last ACK falls within this segment's sequence numbers,
1602: * record its timestamp.
1603: * NOTE that the test is modified according to the latest
1604: * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1605: */
1606: if ((to.to_flags & TOF_TS) != 0 &&
1607: SEQ_LEQ(th->th_seq, tp->last_ack_sent)) {
1608: tp->ts_recent_age = ticks;
1609: tp->ts_recent = to.to_tsval;
1610: }
1611:
1612: /*
1613: * If a SYN is in the window, then this is an
1614: * error and we send an RST and drop the connection.
1615: */
1616: if (thflags & TH_SYN) {
1617: tp = tcp_drop(tp, ECONNRESET);
1618: rstreason = BANDLIM_UNLIMITED;
1619: goto dropwithreset;
1620: }
1621:
1622: /*
1623: * If the ACK bit is off: if in SYN-RECEIVED state or SENDSYN
1624: * flag is on (half-synchronized state), then queue data for
1625: * later processing; else drop segment and return.
1626: */
1627: if ((thflags & TH_ACK) == 0) {
1628: if (tp->t_state == TCPS_SYN_RECEIVED ||
1629: (tp->t_flags & TF_NEEDSYN))
1630: goto step6;
1631: else
1632: goto drop;
1633: }
1634:
1635: /*
1636: * Ack processing.
1637: */
1638: switch (tp->t_state) {
1639:
1640: /*
1641: * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1642: * ESTABLISHED state and continue processing.
1643: * The ACK was checked above.
1644: */
1645: case TCPS_SYN_RECEIVED:
1646:
1647: tcpstat.tcps_connects++;
1648: soisconnected(so);
1649: /* Do window scaling? */
1650: if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1651: (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1652: tp->snd_scale = tp->requested_s_scale;
1653: tp->rcv_scale = tp->request_r_scale;
1654: }
1655: /*
1656: * Upon successful completion of 3-way handshake,
1657: * update cache.CC if it was undefined, pass any queued
1658: * data to the user, and advance state appropriately.
1659: */
1660: if ((taop = tcp_gettaocache(&inp->inp_inc)) != NULL &&
1661: taop->tao_cc == 0)
1662: taop->tao_cc = tp->cc_recv;
1663:
1664: /*
1665: * Make transitions:
1666: * SYN-RECEIVED -> ESTABLISHED
1667: * SYN-RECEIVED* -> FIN-WAIT-1
1668: */
1669: tp->t_starttime = ticks;
1670: if (tp->t_flags & TF_NEEDFIN) {
1671: tp->t_state = TCPS_FIN_WAIT_1;
1672: tp->t_flags &= ~TF_NEEDFIN;
1673: } else {
1674: tp->t_state = TCPS_ESTABLISHED;
1675: callout_reset(tp->tt_keep, tcp_keepidle,
1676: tcp_timer_keep, tp);
1677: }
1678: /*
1679: * If segment contains data or ACK, will call tcp_reass()
1680: * later; if not, do so now to pass queued data to user.
1681: */
1682: if (tlen == 0 && (thflags & TH_FIN) == 0)
1683: (void) tcp_reass(tp, (struct tcphdr *)0, 0,
1684: (struct mbuf *)0);
1685: tp->snd_wl1 = th->th_seq - 1;
1686: /* fall into ... */
1687:
1688: /*
1689: * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1690: * ACKs. If the ack is in the range
1691: * tp->snd_una < th->th_ack <= tp->snd_max
1692: * then advance tp->snd_una to th->th_ack and drop
1693: * data from the retransmission queue. If this ACK reflects
1694: * more up to date window information we update our window information.
1695: */
1696: case TCPS_ESTABLISHED:
1697: case TCPS_FIN_WAIT_1:
1698: case TCPS_FIN_WAIT_2:
1699: case TCPS_CLOSE_WAIT:
1700: case TCPS_CLOSING:
1701: case TCPS_LAST_ACK:
1702: case TCPS_TIME_WAIT:
1703:
1704: if (SEQ_LEQ(th->th_ack, tp->snd_una)) {
1705: if (tlen == 0 && tiwin == tp->snd_wnd) {
1706: tcpstat.tcps_rcvdupack++;
1707: /*
1708: * If we have outstanding data (other than
1709: * a window probe), this is a completely
1710: * duplicate ack (ie, window info didn't
1711: * change), the ack is the biggest we've
1712: * seen and we've seen exactly our rexmt
1713: * threshhold of them, assume a packet
1714: * has been dropped and retransmit it.
1715: * Kludge snd_nxt & the congestion
1716: * window so we send only this one
1717: * packet.
1718: *
1719: * We know we're losing at the current
1720: * window size so do congestion avoidance
1721: * (set ssthresh to half the current window
1722: * and pull our congestion window back to
1723: * the new ssthresh).
1724: *
1725: * Dup acks mean that packets have left the
1726: * network (they're now cached at the receiver)
1727: * so bump cwnd by the amount in the receiver
1728: * to keep a constant cwnd packets in the
1729: * network.
1730: */
1731: if (!callout_active(tp->tt_rexmt) ||
1732: th->th_ack != tp->snd_una)
1733: tp->t_dupacks = 0;
1734: else if (++tp->t_dupacks > tcprexmtthresh ||
1735: (tcp_do_newreno &&
1736: IN_FASTRECOVERY(tp))) {
1737: tp->snd_cwnd += tp->t_maxseg;
1738: (void) tcp_output(tp);
1739: goto drop;
1740: } else if (tp->t_dupacks == tcprexmtthresh) {
1741: tcp_seq onxt;
1742: u_int win;
1743:
1744: if (tcp_do_newreno &&
1745: SEQ_LEQ(th->th_ack,
1746: tp->snd_recover)) {
1747: tp->t_dupacks = 0;
1748: break;
1749: }
1750: fastretransmit:
1751: if (tcp_do_eifel_detect &&
1752: (tp->t_flags & TF_RCVD_TSTMP)) {
1753: tcp_save_congestion_state(tp);
1754: tp->t_flags |= TF_FASTREXMT;
1755: }
1756: win = min(tp->snd_wnd, tp->snd_cwnd) /
1757: 2 / tp->t_maxseg;
1758: if (win < 2)
1759: win = 2;
1760: tp->snd_ssthresh = win * tp->t_maxseg;
1761: ENTER_FASTRECOVERY(tp);
1762: tp->snd_recover = tp->snd_max;
1763: callout_stop(tp->tt_rexmt);
1764: tp->t_rtttime = 0;
1765: onxt = tp->snd_nxt;
1766: tp->snd_nxt = th->th_ack;
1767: tp->snd_cwnd = tp->t_maxseg;
1768: (void) tcp_output(tp);
1769: ++tcpstat.tcps_sndfastrexmit;
1770: KASSERT(tp->snd_limited <= 2,
1771: ("tp->snd_limited too big"));
1772: tp->snd_cwnd = tp->snd_ssthresh +
1773: (tp->t_maxseg *
1774: (tp->t_dupacks - tp->snd_limited));
1775: if (SEQ_GT(onxt, tp->snd_nxt))
1776: tp->snd_nxt = onxt;
1777: goto drop;
1778: } else if (tcp_do_limitedtransmit) {
1779: u_long oldcwnd = tp->snd_cwnd;
1780: tcp_seq oldsndmax = tp->snd_max;
1781: /* outstanding data */
1782: uint32_t ownd =
1783: tp->snd_max - tp->snd_una;
1784: u_int sent;
1785:
1786: #define iceildiv(n, d) (((n)+(d)-1) / (d))
1787:
1788: KASSERT(tp->t_dupacks == 1 ||
1789: tp->t_dupacks == 2,
1790: ("dupacks not 1 or 2"));
1791: if (tp->t_dupacks == 1)
1792: tp->snd_limited = 0;
1793: tp->snd_cwnd = ownd +
1794: (tp->t_dupacks - tp->snd_limited) *
1795: tp->t_maxseg;
1796: (void) tcp_output(tp);
1797: tp->snd_cwnd = oldcwnd;
1798: sent = tp->snd_max - oldsndmax;
1799: if (sent > tp->t_maxseg) {
1800: KASSERT((tp->t_dupacks == 2 &&
1801: tp->snd_limited == 0) ||
1802: (sent == tp->t_maxseg + 1 &&
1803: tp->t_flags & TF_SENTFIN),
1804: ("sent too much"));
1805: KASSERT(sent <=
1806: tp->t_maxseg * 2,
1807: ("sent too many segments"));
1808: tp->snd_limited = 2;
1809: tcpstat.tcps_sndlimited += 2;
1810: } else if (sent > 0) {
1811: ++tp->snd_limited;
1812: ++tcpstat.tcps_sndlimited;
1813: } else if (tcp_do_early_retransmit &&
1814: (tcp_do_eifel_detect &&
1815: (tp->t_flags & TF_RCVD_TSTMP)) &&
1816: tcp_do_newreno &&
1817: tp->t_dupacks + 1 >=
1818: iceildiv(ownd, tp->t_maxseg)) {
1819: ++tcpstat.tcps_sndearlyrexmit;
1820: tp->t_flags |= TF_EARLYREXMT;
1821: goto fastretransmit;
1822: }
1823: goto drop;
1824: }
1825: } else
1826: tp->t_dupacks = 0;
1827: break;
1828: }
1829:
1830: KASSERT(SEQ_GT(th->th_ack, tp->snd_una), ("th_ack <= snd_una"));
1831:
1832: /*
1833: * If the congestion window was inflated to account
1834: * for the other side's cached packets, retract it.
1835: */
1836: if (tcp_do_newreno) {
1837: if (IN_FASTRECOVERY(tp)) {
1838: if (SEQ_LT(th->th_ack, tp->snd_recover)) {
1839: tcp_newreno_partial_ack(tp, th);
1840: } else {
1841: /*
1842: * Window inflation should have left us
1843: * with approximately snd_ssthresh
1844: * outstanding data.
1845: * But in case we would be inclined to
1846: * send a burst, better to do it via
1847: * the slow start mechanism.
1848: */
1849: if (SEQ_GT(th->th_ack +
1850: tp->snd_ssthresh,
1851: tp->snd_max))
1852: tp->snd_cwnd = tp->snd_max -
1853: th->th_ack +
1854: tp->t_maxseg;
1855: else
1856: tp->snd_cwnd = tp->snd_ssthresh;
1857: }
1858: }
1859: } else {
1860: if (tp->t_dupacks >= tcprexmtthresh &&
1861: tp->snd_cwnd > tp->snd_ssthresh)
1862: tp->snd_cwnd = tp->snd_ssthresh;
1863: }
1864: tp->t_dupacks = 0;
1865: if (SEQ_GT(th->th_ack, tp->snd_max)) {
1866: tcpstat.tcps_rcvacktoomuch++;
1867: goto dropafterack;
1868: }
1869: /*
1870: * If we reach this point, ACK is not a duplicate,
1871: * i.e., it ACKs something we sent.
1872: */
1873: if (tp->t_flags & TF_NEEDSYN) {
1874: /*
1875: * T/TCP: Connection was half-synchronized, and our
1876: * SYN has been ACK'd (so connection is now fully
1877: * synchronized). Go to non-starred state,
1878: * increment snd_una for ACK of SYN, and check if
1879: * we can do window scaling.
1880: */
1881: tp->t_flags &= ~TF_NEEDSYN;
1882: tp->snd_una++;
1883: /* Do window scaling? */
1884: if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1885: (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1886: tp->snd_scale = tp->requested_s_scale;
1887: tp->rcv_scale = tp->request_r_scale;
1888: }
1889: }
1890:
1891: process_ACK:
1892: acked = th->th_ack - tp->snd_una;
1893: tcpstat.tcps_rcvackpack++;
1894: tcpstat.tcps_rcvackbyte += acked;
1895:
1896: /*
1897: * If we just performed our first retransmit, and the ACK
1898: * arrives within our recovery window, then it was a mistake
1899: * to do the retransmit in the first place. Recover our
1900: * original cwnd and ssthresh, and proceed to transmit where
1901: * we left off.
1902: */
1903: if (tcp_do_eifel_detect && acked &&
1904: (to.to_flags & TOF_TS) && to.to_tsecr &&
1905: (tp->t_flags & TF_FIRSTACCACK)) {
1906: /* Eifel detection applicable. */
1907: if (to.to_tsecr < tp->t_rexmtTS) {
1908: ++tcpstat.tcps_eifeldetected;
1909: tcp_revert_congestion_state(tp);
1910: if (tp->t_rxtshift == 1 &&
1911: ticks >= tp->t_badrxtwin)
1912: ++tcpstat.tcps_rttcantdetect;
1913: }
1914: } else if (tp->t_rxtshift == 1 && ticks < tp->t_badrxtwin) {
1915: tcp_revert_congestion_state(tp);
1916: ++tcpstat.tcps_rttdetected;
1917: }
1918:
1919: /*
1920: * If we have a timestamp reply, update smoothed
1921: * round trip time. If no timestamp is present but
1922: * transmit timer is running and timed sequence
1923: * number was acked, update smoothed round trip time.
1924: * Since we now have an rtt measurement, cancel the
1925: * timer backoff (cf., Phil Karn's retransmit alg.).
1926: * Recompute the initial retransmit timer.
1927: *
1928: * Some machines (certain windows boxes) send broken
1929: * timestamp replies during the SYN+ACK phase, ignore
1930: * timestamps of 0.
1931: */
1932: if ((to.to_flags & TOF_TS) != 0 &&
1933: to.to_tsecr) {
1934: tcp_xmit_timer(tp, ticks - to.to_tsecr + 1);
1935: } else if (tp->t_rtttime && SEQ_GT(th->th_ack, tp->t_rtseq)) {
1936: tcp_xmit_timer(tp, ticks - tp->t_rtttime);
1937: }
1938: tcp_xmit_bandwidth_limit(tp, th->th_ack);
1939:
1940: /*
1941: * If all outstanding data is acked, stop retransmit
1942: * timer and remember to restart (more output or persist).
1943: * If there is more data to be acked, restart retransmit
1944: * timer, using current (possibly backed-off) value.
1945: */
1946: if (th->th_ack == tp->snd_max) {
1947: callout_stop(tp->tt_rexmt);
1948: needoutput = 1;
1949: } else if (!callout_active(tp->tt_persist))
1950: callout_reset(tp->tt_rexmt, tp->t_rxtcur,
1951: tcp_timer_rexmt, tp);
1952:
1953: /*
1954: * If no data (only SYN) was ACK'd,
1955: * skip rest of ACK processing.
1956: */
1957: if (acked == 0)
1958: goto step6;
1959:
1960: /* Stop looking for an acceptable ACK since one was received. */
1961: tp->t_flags &= ~(TF_FIRSTACCACK | TF_FASTREXMT | TF_EARLYREXMT);
1962:
1963: /*
1964: * When new data is acked, open the congestion window.
1965: * If the window gives us less than ssthresh packets
1966: * in flight, open exponentially (maxseg per packet).
1967: * Otherwise open linearly: maxseg per window
1968: * (maxseg^2 / cwnd per packet).
1969: */
1970: if (!tcp_do_newreno || !IN_FASTRECOVERY(tp)) {
1971: u_int cw = tp->snd_cwnd;
1972: u_int incr = tp->t_maxseg;
1973: if (cw > tp->snd_ssthresh)
1974: incr = incr * incr / cw;
1975: tp->snd_cwnd = min(cw+incr, TCP_MAXWIN<<tp->snd_scale);
1976: }
1977: if (acked > so->so_snd.sb_cc) {
1978: tp->snd_wnd -= so->so_snd.sb_cc;
1979: sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1980: ourfinisacked = 1;
1981: } else {
1982: sbdrop(&so->so_snd, acked);
1983: tp->snd_wnd -= acked;
1984: ourfinisacked = 0;
1985: }
1986: sowwakeup(so);
1987: /* detect una wraparound */
1988: if (tcp_do_newreno && !IN_FASTRECOVERY(tp) &&
1989: SEQ_GT(tp->snd_una, tp->snd_recover) &&
1990: SEQ_LEQ(th->th_ack, tp->snd_recover))
1991: tp->snd_recover = th->th_ack - 1;
1992: if (tcp_do_newreno && IN_FASTRECOVERY(tp) &&
1993: SEQ_GEQ(th->th_ack, tp->snd_recover))
1994: EXIT_FASTRECOVERY(tp);
1995: tp->snd_una = th->th_ack;
1996: if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1997: tp->snd_nxt = tp->snd_una;
1998:
1999: switch (tp->t_state) {
2000:
2001: /*
2002: * In FIN_WAIT_1 STATE in addition to the processing
2003: * for the ESTABLISHED state if our FIN is now acknowledged
2004: * then enter FIN_WAIT_2.
2005: */
2006: case TCPS_FIN_WAIT_1:
2007: if (ourfinisacked) {
2008: /*
2009: * If we can't receive any more
2010: * data, then closing user can proceed.
2011: * Starting the timer is contrary to the
2012: * specification, but if we don't get a FIN
2013: * we'll hang forever.
2014: */
2015: if (so->so_state & SS_CANTRCVMORE) {
2016: soisdisconnected(so);
2017: callout_reset(tp->tt_2msl, tcp_maxidle,
2018: tcp_timer_2msl, tp);
2019: }
2020: tp->t_state = TCPS_FIN_WAIT_2;
2021: }
2022: break;
2023:
2024: /*
2025: * In CLOSING STATE in addition to the processing for
2026: * the ESTABLISHED state if the ACK acknowledges our FIN
2027: * then enter the TIME-WAIT state, otherwise ignore
2028: * the segment.
2029: */
2030: case TCPS_CLOSING:
2031: if (ourfinisacked) {
2032: tp->t_state = TCPS_TIME_WAIT;
2033: tcp_canceltimers(tp);
2034: /* Shorten TIME_WAIT [RFC-1644, p.28] */
2035: if (tp->cc_recv != 0 &&
2036: (ticks - tp->t_starttime) < tcp_msl)
2037: callout_reset(tp->tt_2msl,
2038: tp->t_rxtcur *
2039: TCPTV_TWTRUNC,
2040: tcp_timer_2msl, tp);
2041: else
2042: callout_reset(tp->tt_2msl, 2 * tcp_msl,
2043: tcp_timer_2msl, tp);
2044: soisdisconnected(so);
2045: }
2046: break;
2047:
2048: /*
2049: * In LAST_ACK, we may still be waiting for data to drain
2050: * and/or to be acked, as well as for the ack of our FIN.
2051: * If our FIN is now acknowledged, delete the TCB,
2052: * enter the closed state and return.
2053: */
2054: case TCPS_LAST_ACK:
2055: if (ourfinisacked) {
2056: tp = tcp_close(tp);
2057: goto drop;
2058: }
2059: break;
2060:
2061: /*
2062: * In TIME_WAIT state the only thing that should arrive
2063: * is a retransmission of the remote FIN. Acknowledge
2064: * it and restart the finack timer.
2065: */
2066: case TCPS_TIME_WAIT:
2067: callout_reset(tp->tt_2msl, 2 * tcp_msl,
2068: tcp_timer_2msl, tp);
2069: goto dropafterack;
2070: }
2071: }
2072:
2073: step6:
2074: /*
2075: * Update window information.
2076: * Don't look at window if no ACK: TAC's send garbage on first SYN.
2077: */
2078: if ((thflags & TH_ACK) &&
2079: (SEQ_LT(tp->snd_wl1, th->th_seq) ||
2080: (tp->snd_wl1 == th->th_seq && (SEQ_LT(tp->snd_wl2, th->th_ack) ||
2081: (tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd))))) {
2082: /* keep track of pure window updates */
2083: if (tlen == 0 &&
2084: tp->snd_wl2 == th->th_ack && tiwin > tp->snd_wnd)
2085: tcpstat.tcps_rcvwinupd++;
2086: tp->snd_wnd = tiwin;
2087: tp->snd_wl1 = th->th_seq;
2088: tp->snd_wl2 = th->th_ack;
2089: if (tp->snd_wnd > tp->max_sndwnd)
2090: tp->max_sndwnd = tp->snd_wnd;
2091: needoutput = 1;
2092: }
2093:
2094: /*
2095: * Process segments with URG.
2096: */
2097: if ((thflags & TH_URG) && th->th_urp &&
2098: TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2099: /*
2100: * This is a kludge, but if we receive and accept
2101: * random urgent pointers, we'll crash in
2102: * soreceive. It's hard to imagine someone
2103: * actually wanting to send this much urgent data.
2104: */
2105: if (th->th_urp + so->so_rcv.sb_cc > sb_max) {
2106: th->th_urp = 0; /* XXX */
2107: thflags &= ~TH_URG; /* XXX */
2108: goto dodata; /* XXX */
2109: }
2110: /*
2111: * If this segment advances the known urgent pointer,
2112: * then mark the data stream. This should not happen
2113: * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
2114: * a FIN has been received from the remote side.
2115: * In these states we ignore the URG.
2116: *
2117: * According to RFC961 (Assigned Protocols),
2118: * the urgent pointer points to the last octet
2119: * of urgent data. We continue, however,
2120: * to consider it to indicate the first octet
2121: * of data past the urgent section as the original
2122: * spec states (in one of two places).
2123: */
2124: if (SEQ_GT(th->th_seq+th->th_urp, tp->rcv_up)) {
2125: tp->rcv_up = th->th_seq + th->th_urp;
2126: so->so_oobmark = so->so_rcv.sb_cc +
2127: (tp->rcv_up - tp->rcv_nxt) - 1;
2128: if (so->so_oobmark == 0)
2129: so->so_state |= SS_RCVATMARK;
2130: sohasoutofband(so);
2131: tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
2132: }
2133: /*
2134: * Remove out of band data so doesn't get presented to user.
2135: * This can happen independent of advancing the URG pointer,
2136: * but if two URG's are pending at once, some out-of-band
2137: * data may creep in... ick.
2138: */
2139: if (th->th_urp <= (u_long)tlen
2140: #ifdef SO_OOBINLINE
2141: && (so->so_options & SO_OOBINLINE) == 0
2142: #endif
2143: )
2144: tcp_pulloutofband(so, th, m,
2145: drop_hdrlen); /* hdr drop is delayed */
2146: } else {
2147: /*
2148: * If no out of band data is expected,
2149: * pull receive urgent pointer along
2150: * with the receive window.
2151: */
2152: if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
2153: tp->rcv_up = tp->rcv_nxt;
2154: }
2155: dodata: /* XXX */
2156:
2157: /*
2158: * Process the segment text, merging it into the TCP sequencing queue,
2159: * and arranging for acknowledgment of receipt if necessary.
2160: * This process logically involves adjusting tp->rcv_wnd as data
2161: * is presented to the user (this happens in tcp_usrreq.c,
2162: * case PRU_RCVD). If a FIN has already been received on this
2163: * connection then we just ignore the text.
2164: */
2165: if ((tlen || (thflags & TH_FIN)) &&
2166: TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2167: m_adj(m, drop_hdrlen); /* delayed header drop */
2168: /*
2169: * Insert segment which includes th into TCP reassembly queue
2170: * with control block tp. Set thflags to whether reassembly now
2171: * includes a segment with FIN. This handles the common case
2172: * inline (segment is the next to be received on an established
2173: * connection, and the queue is empty), avoiding linkage into
2174: * and removal from the queue and repetition of various
2175: * conversions.
2176: * Set DELACK for segments received in order, but ack
2177: * immediately when segments are out of order (so
2178: * fast retransmit can work).
2179: */
2180: if (th->th_seq == tp->rcv_nxt &&
2181: LIST_EMPTY(&tp->t_segq) &&
2182: TCPS_HAVEESTABLISHED(tp->t_state)) {
2183: if (DELAY_ACK(tp))
2184: callout_reset(tp->tt_delack, tcp_delacktime,
2185: tcp_timer_delack, tp);
2186: else
2187: tp->t_flags |= TF_ACKNOW;
2188: tp->rcv_nxt += tlen;
2189: thflags = th->th_flags & TH_FIN;
2190: tcpstat.tcps_rcvpack++;
2191: tcpstat.tcps_rcvbyte += tlen;
2192: ND6_HINT(tp);
2193: if (so->so_state & SS_CANTRCVMORE)
2194: m_freem(m);
2195: else
2196: sbappend(&so->so_rcv, m);
2197: sorwakeup(so);
2198: } else {
2199: thflags = tcp_reass(tp, th, &tlen, m);
2200: tp->t_flags |= TF_ACKNOW;
2201: }
2202:
2203: /*
2204: * Note the amount of data that peer has sent into
2205: * our window, in order to estimate the sender's
2206: * buffer size.
2207: */
2208: len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
2209: } else {
2210: m_freem(m);
2211: thflags &= ~TH_FIN;
2212: }
2213:
2214: /*
2215: * If FIN is received ACK the FIN and let the user know
2216: * that the connection is closing.
2217: */
2218: if (thflags & TH_FIN) {
2219: if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
2220: socantrcvmore(so);
2221: /*
2222: * If connection is half-synchronized
2223: * (ie NEEDSYN flag on) then delay ACK,
2224: * so it may be piggybacked when SYN is sent.
2225: * Otherwise, since we received a FIN then no
2226: * more input can be expected, send ACK now.
2227: */
2228: if (DELAY_ACK(tp) && (tp->t_flags & TF_NEEDSYN))
2229: callout_reset(tp->tt_delack, tcp_delacktime,
2230: tcp_timer_delack, tp);
2231: else
2232: tp->t_flags |= TF_ACKNOW;
2233: tp->rcv_nxt++;
2234: }
2235: switch (tp->t_state) {
2236:
2237: /*
2238: * In SYN_RECEIVED and ESTABLISHED STATES
2239: * enter the CLOSE_WAIT state.
2240: */
2241: case TCPS_SYN_RECEIVED:
2242: tp->t_starttime = ticks;
2243: /*FALLTHROUGH*/
2244: case TCPS_ESTABLISHED:
2245: tp->t_state = TCPS_CLOSE_WAIT;
2246: break;
2247:
2248: /*
2249: * If still in FIN_WAIT_1 STATE FIN has not been acked so
2250: * enter the CLOSING state.
2251: */
2252: case TCPS_FIN_WAIT_1:
2253: tp->t_state = TCPS_CLOSING;
2254: break;
2255:
2256: /*
2257: * In FIN_WAIT_2 state enter the TIME_WAIT state,
2258: * starting the time-wait timer, turning off the other
2259: * standard timers.
2260: */
2261: case TCPS_FIN_WAIT_2:
2262: tp->t_state = TCPS_TIME_WAIT;
2263: tcp_canceltimers(tp);
2264: /* Shorten TIME_WAIT [RFC-1644, p.28] */
2265: if (tp->cc_recv != 0 &&
2266: (ticks - tp->t_starttime) < tcp_msl) {
2267: callout_reset(tp->tt_2msl,
2268: tp->t_rxtcur * TCPTV_TWTRUNC,
2269: tcp_timer_2msl, tp);
2270: /* For transaction client, force ACK now. */
2271: tp->t_flags |= TF_ACKNOW;
2272: }
2273: else
2274: callout_reset(tp->tt_2msl, 2 * tcp_msl,
2275: tcp_timer_2msl, tp);
2276: soisdisconnected(so);
2277: break;
2278:
2279: /*
2280: * In TIME_WAIT state restart the 2 MSL time_wait timer.
2281: */
2282: case TCPS_TIME_WAIT:
2283: callout_reset(tp->tt_2msl, 2 * tcp_msl,
2284: tcp_timer_2msl, tp);
2285: break;
2286: }
2287: }
2288: #ifdef TCPDEBUG
2289: if (so->so_options & SO_DEBUG)
2290: tcp_trace(TA_INPUT, ostate, tp, (void *)tcp_saveipgen,
2291: &tcp_savetcp, 0);
2292: #endif
2293:
2294: /*
2295: * Return any desired output.
2296: */
2297: if (needoutput || (tp->t_flags & TF_ACKNOW))
2298: (void) tcp_output(tp);
2299: return;
2300:
2301: dropafterack:
2302: /*
2303: * Generate an ACK dropping incoming segment if it occupies
2304: * sequence space, where the ACK reflects our state.
2305: *
2306: * We can now skip the test for the RST flag since all
2307: * paths to this code happen after packets containing
2308: * RST have been dropped.
2309: *
2310: * In the SYN-RECEIVED state, don't send an ACK unless the
2311: * segment we received passes the SYN-RECEIVED ACK test.
2312: * If it fails send a RST. This breaks the loop in the
2313: * "LAND" DoS attack, and also prevents an ACK storm
2314: * between two listening ports that have been sent forged
2315: * SYN segments, each with the source address of the other.
2316: */
2317: if (tp->t_state == TCPS_SYN_RECEIVED && (thflags & TH_ACK) &&
2318: (SEQ_GT(tp->snd_una, th->th_ack) ||
2319: SEQ_GT(th->th_ack, tp->snd_max)) ) {
2320: rstreason = BANDLIM_RST_OPENPORT;
2321: goto dropwithreset;
2322: }
2323: #ifdef TCPDEBUG
2324: if (so->so_options & SO_DEBUG)
2325: tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2326: &tcp_savetcp, 0);
2327: #endif
2328: m_freem(m);
2329: tp->t_flags |= TF_ACKNOW;
2330: (void) tcp_output(tp);
2331: return;
2332:
2333: dropwithreset:
2334: /*
2335: * Generate a RST, dropping incoming segment.
2336: * Make ACK acceptable to originator of segment.
2337: * Don't bother to respond if destination was broadcast/multicast.
2338: */
2339: if ((thflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST))
2340: goto drop;
2341: if (isipv6) {
2342: if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) ||
2343: IN6_IS_ADDR_MULTICAST(&ip6->ip6_src))
2344: goto drop;
2345: } else {
2346: if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
2347: IN_MULTICAST(ntohl(ip->ip_src.s_addr)) ||
2348: ip->ip_src.s_addr == htonl(INADDR_BROADCAST) ||
2349: in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif))
2350: goto drop;
2351: }
2352: /* IPv6 anycast check is done at tcp6_input() */
2353:
2354: /*
2355: * Perform bandwidth limiting.
2356: */
2357: #ifdef ICMP_BANDLIM
2358: if (badport_bandlim(rstreason) < 0)
2359: goto drop;
2360: #endif
2361:
2362: #ifdef TCPDEBUG
2363: if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2364: tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2365: &tcp_savetcp, 0);
2366: #endif
2367: if (thflags & TH_ACK)
2368: /* mtod() below is safe as long as hdr dropping is delayed */
2369: tcp_respond(tp, mtod(m, void *), th, m, (tcp_seq)0, th->th_ack,
2370: TH_RST);
2371: else {
2372: if (thflags & TH_SYN)
2373: tlen++;
2374: /* mtod() below is safe as long as hdr dropping is delayed */
2375: tcp_respond(tp, mtod(m, void *), th, m, th->th_seq+tlen,
2376: (tcp_seq)0, TH_RST|TH_ACK);
2377: }
2378: return;
2379:
2380: drop:
2381: /*
2382: * Drop space held by incoming segment and return.
2383: */
2384: #ifdef TCPDEBUG
2385: if (tp == NULL || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
2386: tcp_trace(TA_DROP, ostate, tp, (void *)tcp_saveipgen,
2387: &tcp_savetcp, 0);
2388: #endif
2389: m_freem(m);
2390: return;
2391: }
2392:
2393: /*
2394: * Parse TCP options and place in tcpopt.
2395: */
2396: static void
2397: tcp_dooptions(to, cp, cnt, is_syn)
2398: struct tcpopt *to;
2399: u_char *cp;
2400: int cnt;
2401: {
2402: int opt, optlen;
2403:
2404: to->to_flags = 0;
2405: for (; cnt > 0; cnt -= optlen, cp += optlen) {
2406: opt = cp[0];
2407: if (opt == TCPOPT_EOL)
2408: break;
2409: if (opt == TCPOPT_NOP)
2410: optlen = 1;
2411: else {
2412: if (cnt < 2)
2413: break;
2414: optlen = cp[1];
2415: if (optlen < 2 || optlen > cnt)
2416: break;
2417: }
2418: switch (opt) {
2419: case TCPOPT_MAXSEG:
2420: if (optlen != TCPOLEN_MAXSEG)
2421: continue;
2422: if (!is_syn)
2423: continue;
2424: to->to_flags |= TOF_MSS;
2425: bcopy((char *)cp + 2,
2426: (char *)&to->to_mss, sizeof(to->to_mss));
2427: to->to_mss = ntohs(to->to_mss);
2428: break;
2429: case TCPOPT_WINDOW:
2430: if (optlen != TCPOLEN_WINDOW)
2431: continue;
2432: if (! is_syn)
2433: continue;
2434: to->to_flags |= TOF_SCALE;
2435: to->to_requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
2436: break;
2437: case TCPOPT_TIMESTAMP:
2438: if (optlen != TCPOLEN_TIMESTAMP)
2439: continue;
2440: to->to_flags |= TOF_TS;
2441: bcopy((char *)cp + 2,
2442: (char *)&to->to_tsval, sizeof(to->to_tsval));
2443: to->to_tsval = ntohl(to->to_tsval);
2444: bcopy((char *)cp + 6,
2445: (char *)&to->to_tsecr, sizeof(to->to_tsecr));
2446: to->to_tsecr = ntohl(to->to_tsecr);
2447: break;
2448: case TCPOPT_CC:
2449: if (optlen != TCPOLEN_CC)
2450: continue;
2451: to->to_flags |= TOF_CC;
2452: bcopy((char *)cp + 2,
2453: (char *)&to->to_cc, sizeof(to->to_cc));
2454: to->to_cc = ntohl(to->to_cc);
2455: break;
2456: case TCPOPT_CCNEW:
2457: if (optlen != TCPOLEN_CC)
2458: continue;
2459: if (!is_syn)
2460: continue;
2461: to->to_flags |= TOF_CCNEW;
2462: bcopy((char *)cp + 2,
2463: (char *)&to->to_cc, sizeof(to->to_cc));
2464: to->to_cc = ntohl(to->to_cc);
2465: break;
2466: case TCPOPT_CCECHO:
2467: if (optlen != TCPOLEN_CC)
2468: continue;
2469: if (!is_syn)
2470: continue;
2471: to->to_flags |= TOF_CCECHO;
2472: bcopy((char *)cp + 2,
2473: (char *)&to->to_ccecho, sizeof(to->to_ccecho));
2474: to->to_ccecho = ntohl(to->to_ccecho);
2475: break;
2476: default:
2477: continue;
2478: }
2479: }
2480: }
2481:
2482: /*
2483: * Pull out of band byte out of a segment so
2484: * it doesn't appear in the user's data queue.
2485: * It is still reflected in the segment length for
2486: * sequencing purposes.
2487: */
2488: static void
2489: tcp_pulloutofband(so, th, m, off)
2490: struct socket *so;
2491: struct tcphdr *th;
2492: struct mbuf *m;
2493: int off; /* delayed to be droped hdrlen */
2494: {
2495: int cnt = off + th->th_urp - 1;
2496:
2497: while (cnt >= 0) {
2498: if (m->m_len > cnt) {
2499: char *cp = mtod(m, caddr_t) + cnt;
2500: struct tcpcb *tp = sototcpcb(so);
2501:
2502: tp->t_iobc = *cp;
2503: tp->t_oobflags |= TCPOOB_HAVEDATA;
2504: bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
2505: m->m_len--;
2506: if (m->m_flags & M_PKTHDR)
2507: m->m_pkthdr.len--;
2508: return;
2509: }
2510: cnt -= m->m_len;
2511: m = m->m_next;
2512: if (m == 0)
2513: break;
2514: }
2515: panic("tcp_pulloutofband");
2516: }
2517:
2518: /*
2519: * Collect new round-trip time estimate
2520: * and update averages and current timeout.
2521: */
2522: static void
2523: tcp_xmit_timer(tp, rtt)
2524: struct tcpcb *tp;
2525: int rtt;
2526: {
2527: int delta;
2528:
2529: tcpstat.tcps_rttupdated++;
2530: tp->t_rttupdated++;
2531: if (tp->t_srtt != 0) {
2532: /*
2533: * srtt is stored as fixed point with 5 bits after the
2534: * binary point (i.e., scaled by 8). The following magic
2535: * is equivalent to the smoothing algorithm in rfc793 with
2536: * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
2537: * point). Adjust rtt to origin 0.
2538: */
2539: delta = ((rtt - 1) << TCP_DELTA_SHIFT)
2540: - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
2541:
2542: if ((tp->t_srtt += delta) <= 0)
2543: tp->t_srtt = 1;
2544:
2545: /*
2546: * We accumulate a smoothed rtt variance (actually, a
2547: * smoothed mean difference), then set the retransmit
2548: * timer to smoothed rtt + 4 times the smoothed variance.
2549: * rttvar is stored as fixed point with 4 bits after the
2550: * binary point (scaled by 16). The following is
2551: * equivalent to rfc793 smoothing with an alpha of .75
2552: * (rttvar = rttvar*3/4 + |delta| / 4). This replaces
2553: * rfc793's wired-in beta.
2554: */
2555: if (delta < 0)
2556: delta = -delta;
2557: delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
2558: if ((tp->t_rttvar += delta) <= 0)
2559: tp->t_rttvar = 1;
2560: if (tp->t_rttbest > tp->t_srtt + tp->t_rttvar)
2561: tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2562: } else {
2563: /*
2564: * No rtt measurement yet - use the unsmoothed rtt.
2565: * Set the variance to half the rtt (so our first
2566: * retransmit happens at 3*rtt).
2567: */
2568: tp->t_srtt = rtt << TCP_RTT_SHIFT;
2569: tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
2570: tp->t_rttbest = tp->t_srtt + tp->t_rttvar;
2571: }
2572: tp->t_rtttime = 0;
2573: tp->t_rxtshift = 0;
2574:
2575: /*
2576: * the retransmit should happen at rtt + 4 * rttvar.
2577: * Because of the way we do the smoothing, srtt and rttvar
2578: * will each average +1/2 tick of bias. When we compute
2579: * the retransmit timer, we want 1/2 tick of rounding and
2580: * 1 extra tick because of +-1/2 tick uncertainty in the
2581: * firing of the timer. The bias will give us exactly the
2582: * 1.5 tick we need. But, because the bias is
2583: * statistical, we have to test that we don't drop below
2584: * the minimum feasible timer (which is 2 ticks).
2585: */
2586: TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
2587: max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
2588:
2589: /*
2590: * We received an ack for a packet that wasn't retransmitted;
2591: * it is probably safe to discard any error indications we've
2592: * received recently. This isn't quite right, but close enough
2593: * for now (a route might have failed after we sent a segment,
2594: * and the return path might not be symmetrical).
2595: */
2596: tp->t_softerror = 0;
2597: }
2598:
2599: /*
2600: * Determine a reasonable value for maxseg size.
2601: * If the route is known, check route for mtu.
2602: * If none, use an mss that can be handled on the outgoing
2603: * interface without forcing IP to fragment; if bigger than
2604: * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
2605: * to utilize large mbufs. If no route is found, route has no mtu,
2606: * or the destination isn't local, use a default, hopefully conservative
2607: * size (usually 512 or the default IP max size, but no more than the mtu
2608: * of the interface), as we can't discover anything about intervening
2609: * gateways or networks. We also initialize the congestion/slow start
2610: * window to be a single segment if the destination isn't local.
2611: * While looking at the routing entry, we also initialize other path-dependent
2612: * parameters from pre-set or cached values in the routing entry.
2613: *
2614: * Also take into account the space needed for options that we
2615: * send regularly. Make maxseg shorter by that amount to assure
2616: * that we can send maxseg amount of data even when the options
2617: * are present. Store the upper limit of the length of options plus
2618: * data in maxopd.
2619: *
2620: * NOTE that this routine is only called when we process an incoming
2621: * segment, for outgoing segments only tcp_mssopt is called.
2622: *
2623: * In case of T/TCP, we call this routine during implicit connection
2624: * setup as well (offer = -1), to initialize maxseg from the cached
2625: * MSS of our peer.
2626: */
2627: void
2628: tcp_mss(tp, offer)
2629: struct tcpcb *tp;
2630: int offer;
2631: {
2632: struct rtentry *rt;
2633: struct ifnet *ifp;
2634: int rtt, mss;
2635: u_long bufsize;
2636: struct inpcb *inp = tp->t_inpcb;
2637: struct socket *so;
2638: struct rmxp_tao *taop;
2639: int origoffer = offer;
2640: #ifdef INET6
2641: int isipv6 = ((inp->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2642: size_t min_protoh = isipv6 ?
2643: sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
2644: sizeof(struct tcpiphdr);
2645: #else
2646: const int isipv6 = 0;
2647: const size_t min_protoh = sizeof(struct tcpiphdr);
2648: #endif
2649:
2650: if (isipv6)
2651: rt = tcp_rtlookup6(&inp->inp_inc);
2652: else
2653: rt = tcp_rtlookup(&inp->inp_inc);
2654: if (rt == NULL) {
2655: tp->t_maxopd = tp->t_maxseg =
2656: isipv6 ? tcp_v6mssdflt : tcp_mssdflt;
2657: return;
2658: }
2659: ifp = rt->rt_ifp;
2660: so = inp->inp_socket;
2661:
2662: taop = rmx_taop(rt->rt_rmx);
2663: /*
2664: * Offer == -1 means that we didn't receive SYN yet,
2665: * use cached value in that case;
2666: */
2667: if (offer == -1)
2668: offer = taop->tao_mssopt;
2669: /*
2670: * Offer == 0 means that there was no MSS on the SYN segment,
2671: * in this case we use tcp_mssdflt.
2672: */
2673: if (offer == 0)
2674: offer = isipv6 ? tcp_v6mssdflt : tcp_mssdflt;
2675: else
2676: /*
2677: * Sanity check: make sure that maxopd will be large
2678: * enough to allow some data on segments even is the
2679: * all the option space is used (40bytes). Otherwise
2680: * funny things may happen in tcp_output.
2681: */
2682: offer = max(offer, 64);
2683: taop->tao_mssopt = offer;
2684:
2685: /*
2686: * While we're here, check if there's an initial rtt
2687: * or rttvar. Convert from the route-table units
2688: * to scaled multiples of the slow timeout timer.
2689: */
2690: if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2691: /*
2692: * XXX the lock bit for RTT indicates that the value
2693: * is also a minimum value; this is subject to time.
2694: */
2695: if (rt->rt_rmx.rmx_locks & RTV_RTT)
2696: tp->t_rttmin = rtt / (RTM_RTTUNIT / hz);
2697: tp->t_srtt = rtt / (RTM_RTTUNIT / (hz * TCP_RTT_SCALE));
2698: tp->t_rttbest = tp->t_srtt + TCP_RTT_SCALE;
2699: tcpstat.tcps_usedrtt++;
2700: if (rt->rt_rmx.rmx_rttvar) {
2701: tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2702: (RTM_RTTUNIT / (hz * TCP_RTTVAR_SCALE));
2703: tcpstat.tcps_usedrttvar++;
2704: } else {
2705: /* default variation is +- 1 rtt */
2706: tp->t_rttvar =
2707: tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2708: }
2709: TCPT_RANGESET(tp->t_rxtcur,
2710: ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2711: tp->t_rttmin, TCPTV_REXMTMAX);
2712: }
2713: /*
2714: * if there's an mtu associated with the route, use it
2715: * else, use the link mtu.
2716: */
2717: if (rt->rt_rmx.rmx_mtu)
2718: mss = rt->rt_rmx.rmx_mtu - min_protoh;
2719: else {
2720: if (isipv6) {
2721: mss = nd_ifinfo[rt->rt_ifp->if_index].linkmtu -
2722: min_protoh;
2723: if (!in6_localaddr(&inp->in6p_faddr))
2724: mss = min(mss, tcp_v6mssdflt);
2725: } else {
2726: mss = ifp->if_mtu - min_protoh;
2727: if (!in_localaddr(inp->inp_faddr))
2728: mss = min(mss, tcp_mssdflt);
2729: }
2730: }
2731: mss = min(mss, offer);
2732: /*
2733: * maxopd stores the maximum length of data AND options
2734: * in a segment; maxseg is the amount of data in a normal
2735: * segment. We need to store this value (maxopd) apart
2736: * from maxseg, because now every segment carries options
2737: * and thus we normally have somewhat less data in segments.
2738: */
2739: tp->t_maxopd = mss;
2740:
2741: /*
2742: * In case of T/TCP, origoffer==-1 indicates, that no segments
2743: * were received yet. In this case we just guess, otherwise
2744: * we do the same as before T/TCP.
2745: */
2746: if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2747: (origoffer == -1 ||
2748: (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2749: mss -= TCPOLEN_TSTAMP_APPA;
2750: if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2751: (origoffer == -1 ||
2752: (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2753: mss -= TCPOLEN_CC_APPA;
2754:
2755: #if (MCLBYTES & (MCLBYTES - 1)) == 0
2756: if (mss > MCLBYTES)
2757: mss &= ~(MCLBYTES-1);
2758: #else
2759: if (mss > MCLBYTES)
2760: mss = mss / MCLBYTES * MCLBYTES;
2761: #endif
2762: /*
2763: * If there's a pipesize, change the socket buffer
2764: * to that size. Make the socket buffers an integral
2765: * number of mss units; if the mss is larger than
2766: * the socket buffer, decrease the mss.
2767: */
2768: #ifdef RTV_SPIPE
2769: if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2770: #endif
2771: bufsize = so->so_snd.sb_hiwat;
2772: if (bufsize < mss)
2773: mss = bufsize;
2774: else {
2775: bufsize = roundup(bufsize, mss);
2776: if (bufsize > sb_max)
2777: bufsize = sb_max;
2778: if (bufsize > so->so_snd.sb_hiwat)
2779: (void)sbreserve(&so->so_snd, bufsize, so, NULL);
2780: }
2781: tp->t_maxseg = mss;
2782:
2783: #ifdef RTV_RPIPE
2784: if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2785: #endif
2786: bufsize = so->so_rcv.sb_hiwat;
2787: if (bufsize > mss) {
2788: bufsize = roundup(bufsize, mss);
2789: if (bufsize > sb_max)
2790: bufsize = sb_max;
2791: if (bufsize > so->so_rcv.sb_hiwat)
2792: (void)sbreserve(&so->so_rcv, bufsize, so, NULL);
2793: }
2794:
2795: /*
2796: * Set the slow-start flight size depending on whether this
2797: * is a local network or not.
2798: */
2799: if (tcp_do_rfc3390)
2800: tp->snd_cwnd = min(4 * mss, max(2 * mss, 4380));
2801: else if ((isipv6 && in6_localaddr(&inp->in6p_faddr)) ||
2802: (!isipv6 && in_localaddr(inp->inp_faddr)))
2803: tp->snd_cwnd = mss * ss_fltsz_local;
2804: else
2805: tp->snd_cwnd = mss * ss_fltsz;
2806:
2807: if (rt->rt_rmx.rmx_ssthresh) {
2808: /*
2809: * There's some sort of gateway or interface
2810: * buffer limit on the path. Use this to set
2811: * the slow start threshhold, but set the
2812: * threshold to no less than 2*mss.
2813: */
2814: tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2815: tcpstat.tcps_usedssthresh++;
2816: }
2817: }
2818:
2819: /*
2820: * Determine the MSS option to send on an outgoing SYN.
2821: */
2822: int
2823: tcp_mssopt(tp)
2824: struct tcpcb *tp;
2825: {
2826: struct rtentry *rt;
2827: #ifdef INET6
2828: int isipv6 = ((tp->t_inpcb->inp_vflag & INP_IPV6) != 0) ? 1 : 0;
2829: int min_protoh = isipv6 ?
2830: sizeof(struct ip6_hdr) + sizeof(struct tcphdr) :
2831: sizeof(struct tcpiphdr);
2832: #else
2833: const int isipv6 = 0;
2834: const size_t min_protoh = sizeof(struct tcpiphdr);
2835: #endif
2836:
2837: if (isipv6)
2838: rt = tcp_rtlookup6(&tp->t_inpcb->inp_inc);
2839: else
2840: rt = tcp_rtlookup(&tp->t_inpcb->inp_inc);
2841: if (rt == NULL)
2842: return (isipv6 ? tcp_v6mssdflt : tcp_mssdflt);
2843:
2844: return (rt->rt_ifp->if_mtu - min_protoh);
2845: }
2846:
2847:
2848: /*
2849: * When a partial ack arrives, force the retransmission of the
2850: * next unacknowledged segment. Do not clear tp->t_dupacks.
2851: * By setting snd_nxt to ti_ack, this forces retransmission timer to
2852: * be started again.
2853: */
2854: static void
2855: tcp_newreno_partial_ack(tp, th)
2856: struct tcpcb *tp;
2857: struct tcphdr *th;
2858: {
2859: tcp_seq onxt = tp->snd_nxt;
2860: u_long ocwnd = tp->snd_cwnd;
2861:
2862: callout_stop(tp->tt_rexmt);
2863: tp->t_rtttime = 0;
2864: tp->snd_nxt = th->th_ack;
2865: /*
2866: * Set snd_cwnd to one segment beyond acknowledged offset
2867: * (tp->snd_una has not yet been updated when this function is called.)
2868: */
2869: tp->snd_cwnd = tp->t_maxseg + (th->th_ack - tp->snd_una);
2870: tp->t_flags |= TF_ACKNOW;
2871: (void) tcp_output(tp);
2872: tp->snd_cwnd = ocwnd;
2873: if (SEQ_GT(onxt, tp->snd_nxt))
2874: tp->snd_nxt = onxt;
2875: /*
2876: * Partial window deflation. Relies on fact that tp->snd_una
2877: * not updated yet.
2878: */
2879: tp->snd_cwnd -= (th->th_ack - tp->snd_una - tp->t_maxseg);
2880: }