OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [net/] [bsd_tcpip/] [v2_0/] [src/] [sys/] [netinet/] [tcp_input.c] - Blame information for rev 308

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.