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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [libnetworking/] [netinet/] [tcp_input.c] - Blame information for rev 173

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
2
 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994, 1995
3
 *      The Regents of the University of California.  All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. All advertising materials mentioning features or use of this software
14
 *    must display the following acknowledgement:
15
 *      This product includes software developed by the University of
16
 *      California, Berkeley and its contributors.
17
 * 4. Neither the name of the University nor the names of its contributors
18
 *    may be used to endorse or promote products derived from this software
19
 *    without specific prior written permission.
20
 *
21
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31
 * SUCH DAMAGE.
32
 *
33
 *      @(#)tcp_input.c 8.12 (Berkeley) 5/24/95
34
 *      $Id: tcp_input.c,v 1.2 2001-09-27 12:01:56 chris Exp $
35
 */
36
 
37
#include "opt_tcpdebug.h"
38
 
39
#ifndef TUBA_INCLUDE
40
#include <sys/param.h>
41
#include <sys/queue.h>
42
#include <sys/systm.h>
43
#include <sys/kernel.h>
44
#include <sys/sysctl.h>
45
#include <sys/malloc.h>
46
#include <sys/mbuf.h>
47
#include <sys/protosw.h>
48
#include <sys/socket.h>
49
#include <sys/socketvar.h>
50
#include <sys/errno.h>
51
#include <sys/syslog.h>
52
 
53
#include <machine/cpu.h>        /* before tcp_seq.h, for tcp_random18() */
54
 
55
#include <net/if.h>
56
#include <net/route.h>
57
 
58
#include <netinet/in.h>
59
#include <netinet/in_systm.h>
60
#include <netinet/ip.h>
61
#include <netinet/in_pcb.h>
62
#include <netinet/ip_var.h>
63
#include <netinet/tcp.h>
64
#include <netinet/tcp_fsm.h>
65
#include <netinet/tcp_seq.h>
66
#include <netinet/tcp_timer.h>
67
#include <netinet/tcp_var.h>
68
#include <netinet/tcpip.h>
69
#ifdef TCPDEBUG
70
#include <netinet/tcp_debug.h>
71
static struct   tcpiphdr tcp_saveti;
72
#endif
73
 
74
static int      tcprexmtthresh = 3;
75
tcp_seq tcp_iss;
76
tcp_cc  tcp_ccgen;
77
 
78
struct  tcpstat tcpstat;
79
SYSCTL_STRUCT(_net_inet_tcp, TCPCTL_STATS, stats,
80
        CTLFLAG_RD, &tcpstat , tcpstat, "");
81
 
82
static int log_in_vain = 0;
83
SYSCTL_INT(_net_inet_tcp, OID_AUTO, log_in_vain, CTLFLAG_RW,
84
        &log_in_vain, 0, "");
85
 
86
u_long  tcp_now;
87
struct inpcbhead tcb;
88
struct inpcbinfo tcbinfo;
89
 
90
static void      tcp_dooptions __P((struct tcpcb *,
91
            u_char *, int, struct tcpiphdr *, struct tcpopt *));
92
static void      tcp_pulloutofband __P((struct socket *,
93
            struct tcpiphdr *, struct mbuf *));
94
static int       tcp_reass __P((struct tcpcb *, struct tcpiphdr *, struct mbuf *));
95
static void      tcp_xmit_timer __P((struct tcpcb *, int));
96
 
97
#endif /* TUBA_INCLUDE */
98
 
99
/*
100
 * Insert segment ti into reassembly queue of tcp with
101
 * control block tp.  Return TH_FIN if reassembly now includes
102
 * a segment with FIN.  The macro form does the common case inline
103
 * (segment is the next to be received on an established connection,
104
 * and the queue is empty), avoiding linkage into and removal
105
 * from the queue and repetition of various conversions.
106
 * Set DELACK for segments received in order, but ack immediately
107
 * when segments are out of order (so fast retransmit can work).
108
 */
109
#ifdef TCP_ACK_HACK
110
#define TCP_REASS(tp, ti, m, so, flags) { \
111
        if ((ti)->ti_seq == (tp)->rcv_nxt && \
112
            (tp)->seg_next == (struct tcpiphdr *)(tp) && \
113
            (tp)->t_state == TCPS_ESTABLISHED) { \
114
                if (ti->ti_flags & TH_PUSH) \
115
                        tp->t_flags |= TF_ACKNOW; \
116
                else \
117
                        tp->t_flags |= TF_DELACK; \
118
                (tp)->rcv_nxt += (ti)->ti_len; \
119
                flags = (ti)->ti_flags & TH_FIN; \
120
                tcpstat.tcps_rcvpack++;\
121
                tcpstat.tcps_rcvbyte += (ti)->ti_len;\
122
                sbappend(&(so)->so_rcv, (m)); \
123
                sorwakeup(so); \
124
        } else { \
125
                (flags) = tcp_reass((tp), (ti), (m)); \
126
                tp->t_flags |= TF_ACKNOW; \
127
        } \
128
}
129
#else
130
#define TCP_REASS(tp, ti, m, so, flags) { \
131
        if ((ti)->ti_seq == (tp)->rcv_nxt && \
132
            (tp)->seg_next == (struct tcpiphdr *)(tp) && \
133
            (tp)->t_state == TCPS_ESTABLISHED) { \
134
                tp->t_flags |= TF_DELACK; \
135
                (tp)->rcv_nxt += (ti)->ti_len; \
136
                flags = (ti)->ti_flags & TH_FIN; \
137
                tcpstat.tcps_rcvpack++;\
138
                tcpstat.tcps_rcvbyte += (ti)->ti_len;\
139
                sbappend(&(so)->so_rcv, (m)); \
140
                sorwakeup(so); \
141
        } else { \
142
                (flags) = tcp_reass((tp), (ti), (m)); \
143
                tp->t_flags |= TF_ACKNOW; \
144
        } \
145
}
146
#endif
147
#ifndef TUBA_INCLUDE
148
 
149
static int
150
tcp_reass(tp, ti, m)
151
        register struct tcpcb *tp;
152
        register struct tcpiphdr *ti;
153
        struct mbuf *m;
154
{
155
        register struct tcpiphdr *q;
156
        struct socket *so = tp->t_inpcb->inp_socket;
157
        int flags;
158
 
159
        /*
160
         * Call with ti==0 after become established to
161
         * force pre-ESTABLISHED data up to user socket.
162
         */
163
        if (ti == 0)
164
                goto present;
165
 
166
        /*
167
         * Find a segment which begins after this one does.
168
         */
169
        for (q = tp->seg_next; q != (struct tcpiphdr *)tp;
170
            q = (struct tcpiphdr *)q->ti_next)
171
                if (SEQ_GT(q->ti_seq, ti->ti_seq))
172
                        break;
173
 
174
        /*
175
         * If there is a preceding segment, it may provide some of
176
         * our data already.  If so, drop the data from the incoming
177
         * segment.  If it provides all of our data, drop us.
178
         */
179
        if ((struct tcpiphdr *)q->ti_prev != (struct tcpiphdr *)tp) {
180
                register int i;
181
                q = (struct tcpiphdr *)q->ti_prev;
182
                /* conversion to int (in i) handles seq wraparound */
183
                i = q->ti_seq + q->ti_len - ti->ti_seq;
184
                if (i > 0) {
185
                        if (i >= ti->ti_len) {
186
                                tcpstat.tcps_rcvduppack++;
187
                                tcpstat.tcps_rcvdupbyte += ti->ti_len;
188
                                m_freem(m);
189
                                /*
190
                                 * Try to present any queued data
191
                                 * at the left window edge to the user.
192
                                 * This is needed after the 3-WHS
193
                                 * completes.
194
                                 */
195
                                goto present;   /* ??? */
196
                        }
197
                        m_adj(m, i);
198
                        ti->ti_len -= i;
199
                        ti->ti_seq += i;
200
                }
201
                q = (struct tcpiphdr *)(q->ti_next);
202
        }
203
        tcpstat.tcps_rcvoopack++;
204
        tcpstat.tcps_rcvoobyte += ti->ti_len;
205
        REASS_MBUF(ti) = m;             /* XXX */
206
 
207
        /*
208
         * While we overlap succeeding segments trim them or,
209
         * if they are completely covered, dequeue them.
210
         */
211
        while (q != (struct tcpiphdr *)tp) {
212
                register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
213
                if (i <= 0)
214
                        break;
215
                if (i < q->ti_len) {
216
                        q->ti_seq += i;
217
                        q->ti_len -= i;
218
                        m_adj(REASS_MBUF(q), i);
219
                        break;
220
                }
221
                q = (struct tcpiphdr *)q->ti_next;
222
                m = REASS_MBUF((struct tcpiphdr *)q->ti_prev);
223
                remque(q->ti_prev);
224
                m_freem(m);
225
        }
226
 
227
        /*
228
         * Stick new segment in its place.
229
         */
230
        insque(ti, q->ti_prev);
231
 
232
present:
233
        /*
234
         * Present data to user, advancing rcv_nxt through
235
         * completed sequence space.
236
         */
237
        if (!TCPS_HAVEESTABLISHED(tp->t_state))
238
                return (0);
239
        ti = tp->seg_next;
240
        if (ti == (struct tcpiphdr *)tp || ti->ti_seq != tp->rcv_nxt)
241
                return (0);
242
        do {
243
                tp->rcv_nxt += ti->ti_len;
244
                flags = ti->ti_flags & TH_FIN;
245
                remque(ti);
246
                m = REASS_MBUF(ti);
247
                ti = (struct tcpiphdr *)ti->ti_next;
248
                if (so->so_state & SS_CANTRCVMORE)
249
                        m_freem(m);
250
                else
251
                        sbappend(&so->so_rcv, m);
252
        } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
253
        sorwakeup(so);
254
        return (flags);
255
}
256
 
257
/*
258
 * TCP input routine, follows pages 65-76 of the
259
 * protocol specification dated September, 1981 very closely.
260
 */
261
void
262
tcp_input(m, iphlen)
263
        register struct mbuf *m;
264
        int iphlen;
265
{
266
        register struct tcpiphdr *ti;
267
        register struct inpcb *inp;
268
        u_char *optp = NULL;
269
        int optlen = 0;
270
        int len, tlen, off;
271
        register struct tcpcb *tp = 0;
272
        register int tiflags;
273
        struct socket *so = 0;
274
        int todrop, acked, ourfinisacked, needoutput = 0;
275
        struct in_addr laddr;
276
        int dropsocket = 0;
277
        int iss = 0;
278
        u_long tiwin;
279
        struct tcpopt to;               /* options in this segment */
280
        struct rmxp_tao *taop;          /* pointer to our TAO cache entry */
281
        struct rmxp_tao tao_noncached;  /* in case there's no cached entry */
282
#ifdef TCPDEBUG
283
        short ostate = 0;
284
#endif
285
 
286
        bzero((char *)&to, sizeof(to));
287
 
288
        tcpstat.tcps_rcvtotal++;
289
        /*
290
         * Get IP and TCP header together in first mbuf.
291
         * Note: IP leaves IP header in first mbuf.
292
         */
293
        ti = mtod(m, struct tcpiphdr *);
294
        if (iphlen > sizeof (struct ip))
295
                ip_stripoptions(m, (struct mbuf *)0);
296
        if (m->m_len < sizeof (struct tcpiphdr)) {
297
                if ((m = m_pullup(m, sizeof (struct tcpiphdr))) == 0) {
298
                        tcpstat.tcps_rcvshort++;
299
                        return;
300
                }
301
                ti = mtod(m, struct tcpiphdr *);
302
        }
303
 
304
        /*
305
         * Checksum extended TCP header and data.
306
         */
307
        tlen = ((struct ip *)ti)->ip_len;
308
        len = sizeof (struct ip) + tlen;
309
        ti->ti_next = ti->ti_prev = 0;
310
        ti->ti_x1 = 0;
311
        ti->ti_len = (u_short)tlen;
312
        HTONS(ti->ti_len);
313
        ti->ti_sum = in_cksum(m, len);
314
        if (ti->ti_sum) {
315
                tcpstat.tcps_rcvbadsum++;
316
                goto drop;
317
        }
318
#endif /* TUBA_INCLUDE */
319
 
320
        /*
321
         * Check that TCP offset makes sense,
322
         * pull out TCP options and adjust length.              XXX
323
         */
324
        off = ti->ti_off << 2;
325
        if (off < sizeof (struct tcphdr) || off > tlen) {
326
                tcpstat.tcps_rcvbadoff++;
327
                goto drop;
328
        }
329
        tlen -= off;
330
        ti->ti_len = tlen;
331
        if (off > sizeof (struct tcphdr)) {
332
                if (m->m_len < sizeof(struct ip) + off) {
333
                        if ((m = m_pullup(m, sizeof (struct ip) + off)) == 0) {
334
                                tcpstat.tcps_rcvshort++;
335
                                return;
336
                        }
337
                        ti = mtod(m, struct tcpiphdr *);
338
                }
339
                optlen = off - sizeof (struct tcphdr);
340
                optp = mtod(m, u_char *) + sizeof (struct tcpiphdr);
341
        }
342
        tiflags = ti->ti_flags;
343
 
344
        /*
345
         * Convert TCP protocol specific fields to host format.
346
         */
347
        NTOHL(ti->ti_seq);
348
        NTOHL(ti->ti_ack);
349
        NTOHS(ti->ti_win);
350
        NTOHS(ti->ti_urp);
351
 
352
        /*
353
         * Drop TCP, IP headers and TCP options.
354
         */
355
        m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
356
        m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
357
 
358
        /*
359
         * Locate pcb for segment.
360
         */
361
findpcb:
362
        inp = in_pcblookuphash(&tcbinfo, ti->ti_src, ti->ti_sport,
363
            ti->ti_dst, ti->ti_dport, 1);
364
 
365
        /*
366
         * If the state is CLOSED (i.e., TCB does not exist) then
367
         * all data in the incoming segment is discarded.
368
         * If the TCB exists but is in CLOSED state, it is embryonic,
369
         * but should either do a listen or a connect soon.
370
         */
371
        if (inp == NULL) {
372
                if (log_in_vain && tiflags & TH_SYN) {
373
                        char buf[4*sizeof "123"];
374
 
375
                        strcpy(buf, inet_ntoa(ti->ti_dst));
376
                        log(LOG_INFO, "Connection attempt to TCP %s:%d"
377
                            " from %s:%d\n",
378
                            buf, ntohs(ti->ti_dport),
379
                            inet_ntoa(ti->ti_src), ntohs(ti->ti_sport));
380
                }
381
                goto dropwithreset;
382
        }
383
        tp = intotcpcb(inp);
384
        if (tp == 0)
385
                goto dropwithreset;
386
        if (tp->t_state == TCPS_CLOSED)
387
                goto drop;
388
 
389
        /* Unscale the window into a 32-bit value. */
390
        if ((tiflags & TH_SYN) == 0)
391
                tiwin = ti->ti_win << tp->snd_scale;
392
        else
393
                tiwin = ti->ti_win;
394
 
395
        so = inp->inp_socket;
396
        if (so->so_options & (SO_DEBUG|SO_ACCEPTCONN)) {
397
#ifdef TCPDEBUG
398
                if (so->so_options & SO_DEBUG) {
399
                        ostate = tp->t_state;
400
                        tcp_saveti = *ti;
401
                }
402
#endif
403
                if (so->so_options & SO_ACCEPTCONN) {
404
                        register struct tcpcb *tp0 = tp;
405
                        struct socket *so2;
406
                        if ((tiflags & (TH_RST|TH_ACK|TH_SYN)) != TH_SYN) {
407
                                /*
408
                                 * Note: dropwithreset makes sure we don't
409
                                 * send a RST in response to a RST.
410
                                 */
411
                                if (tiflags & TH_ACK) {
412
                                        tcpstat.tcps_badsyn++;
413
                                        goto dropwithreset;
414
                                }
415
                                goto drop;
416
                        }
417
                        so2 = sonewconn(so, 0);
418
                        if (so2 == 0) {
419
                                tcpstat.tcps_listendrop++;
420
                                so2 = sodropablereq(so);
421
                                if (so2) {
422
                                        tcp_drop(sototcpcb(so2), ETIMEDOUT);
423
                                        so2 = sonewconn(so, 0);
424
                                }
425
                                if (!so2)
426
                                        goto drop;
427
                        }
428
                        so = so2;
429
                        /*
430
                         * This is ugly, but ....
431
                         *
432
                         * Mark socket as temporary until we're
433
                         * committed to keeping it.  The code at
434
                         * ``drop'' and ``dropwithreset'' check the
435
                         * flag dropsocket to see if the temporary
436
                         * socket created here should be discarded.
437
                         * We mark the socket as discardable until
438
                         * we're committed to it below in TCPS_LISTEN.
439
                         */
440
                        dropsocket++;
441
                        inp = (struct inpcb *)so->so_pcb;
442
                        inp->inp_laddr = ti->ti_dst;
443
                        inp->inp_lport = ti->ti_dport;
444
                        in_pcbrehash(inp);
445
#if BSD>=43
446
                        inp->inp_options = ip_srcroute();
447
#endif
448
                        tp = intotcpcb(inp);
449
                        tp->t_state = TCPS_LISTEN;
450
                        tp->t_flags |= tp0->t_flags & (TF_NOPUSH|TF_NOOPT);
451
 
452
                        /* Compute proper scaling value from buffer space */
453
                        while (tp->request_r_scale < TCP_MAX_WINSHIFT &&
454
                           TCP_MAXWIN << tp->request_r_scale < so->so_rcv.sb_hiwat)
455
                                tp->request_r_scale++;
456
                }
457
        }
458
 
459
        /*
460
         * Segment received on connection.
461
         * Reset idle time and keep-alive timer.
462
         */
463
        tp->t_idle = 0;
464
        if (TCPS_HAVEESTABLISHED(tp->t_state))
465
                tp->t_timer[TCPT_KEEP] = tcp_keepidle;
466
 
467
        /*
468
         * Process options if not in LISTEN state,
469
         * else do it below (after getting remote address).
470
         */
471
        if (tp->t_state != TCPS_LISTEN)
472
                tcp_dooptions(tp, optp, optlen, ti, &to);
473
 
474
        /*
475
         * Header prediction: check for the two common cases
476
         * of a uni-directional data xfer.  If the packet has
477
         * no control flags, is in-sequence, the window didn't
478
         * change and we're not retransmitting, it's a
479
         * candidate.  If the length is zero and the ack moved
480
         * forward, we're the sender side of the xfer.  Just
481
         * free the data acked & wake any higher level process
482
         * that was blocked waiting for space.  If the length
483
         * is non-zero and the ack didn't move, we're the
484
         * receiver side.  If we're getting packets in-order
485
         * (the reassembly queue is empty), add the data to
486
         * the socket buffer and note that we need a delayed ack.
487
         * Make sure that the hidden state-flags are also off.
488
         * Since we check for TCPS_ESTABLISHED above, it can only
489
         * be TH_NEEDSYN.
490
         */
491
        if (tp->t_state == TCPS_ESTABLISHED &&
492
            (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
493
            ((tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN)) == 0) &&
494
            ((to.to_flag & TOF_TS) == 0 ||
495
             TSTMP_GEQ(to.to_tsval, tp->ts_recent)) &&
496
            /*
497
             * Using the CC option is compulsory if once started:
498
             *   the segment is OK if no T/TCP was negotiated or
499
             *   if the segment has a CC option equal to CCrecv
500
             */
501
            ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) != (TF_REQ_CC|TF_RCVD_CC) ||
502
             ((to.to_flag & TOF_CC) != 0 && to.to_cc == tp->cc_recv)) &&
503
            ti->ti_seq == tp->rcv_nxt &&
504
            tiwin && tiwin == tp->snd_wnd &&
505
            tp->snd_nxt == tp->snd_max) {
506
 
507
                /*
508
                 * If last ACK falls within this segment's sequence numbers,
509
                 * record the timestamp.
510
                 * NOTE that the test is modified according to the latest
511
                 * proposal of the tcplw@cray.com list (Braden 1993/04/26).
512
                 */
513
                if ((to.to_flag & TOF_TS) != 0 &&
514
                   SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
515
                        tp->ts_recent_age = tcp_now;
516
                        tp->ts_recent = to.to_tsval;
517
                }
518
 
519
                if (ti->ti_len == 0) {
520
                        if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
521
                            SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
522
                            tp->snd_cwnd >= tp->snd_wnd &&
523
                            tp->t_dupacks < tcprexmtthresh) {
524
                                /*
525
                                 * this is a pure ack for outstanding data.
526
                                 */
527
                                ++tcpstat.tcps_predack;
528
                                if ((to.to_flag & TOF_TS) != 0)
529
                                        tcp_xmit_timer(tp,
530
                                            tcp_now - to.to_tsecr + 1);
531
                                else if (tp->t_rtt &&
532
                                            SEQ_GT(ti->ti_ack, tp->t_rtseq))
533
                                        tcp_xmit_timer(tp, tp->t_rtt);
534
                                acked = ti->ti_ack - tp->snd_una;
535
                                tcpstat.tcps_rcvackpack++;
536
                                tcpstat.tcps_rcvackbyte += acked;
537
                                sbdrop(&so->so_snd, acked);
538
                                tp->snd_una = ti->ti_ack;
539
                                m_freem(m);
540
 
541
                                /*
542
                                 * If all outstanding data are acked, stop
543
                                 * retransmit timer, otherwise restart timer
544
                                 * using current (possibly backed-off) value.
545
                                 * If process is waiting for space,
546
                                 * wakeup/selwakeup/signal.  If data
547
                                 * are ready to send, let tcp_output
548
                                 * decide between more output or persist.
549
                                 */
550
                                if (tp->snd_una == tp->snd_max)
551
                                        tp->t_timer[TCPT_REXMT] = 0;
552
                                else if (tp->t_timer[TCPT_PERSIST] == 0)
553
                                        tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
554
 
555
                                if (so->so_snd.sb_flags & SB_NOTIFY)
556
                                        sowwakeup(so);
557
                                if (so->so_snd.sb_cc)
558
                                        (void) tcp_output(tp);
559
                                return;
560
                        }
561
                } else if (ti->ti_ack == tp->snd_una &&
562
                    tp->seg_next == (struct tcpiphdr *)tp &&
563
                    ti->ti_len <= sbspace(&so->so_rcv)) {
564
                        /*
565
                         * this is a pure, in-sequence data packet
566
                         * with nothing on the reassembly queue and
567
                         * we have enough buffer space to take it.
568
                         */
569
                        ++tcpstat.tcps_preddat;
570
                        tp->rcv_nxt += ti->ti_len;
571
                        tcpstat.tcps_rcvpack++;
572
                        tcpstat.tcps_rcvbyte += ti->ti_len;
573
                        /*
574
                         * Add data to socket buffer.
575
                         */
576
                        sbappend(&so->so_rcv, m);
577
                        sorwakeup(so);
578
#ifdef TCP_ACK_HACK
579
                        /*
580
                         * If this is a short packet, then ACK now - with Nagel
581
                         *      congestion avoidance sender won't send more until
582
                         *      he gets an ACK.
583
                         */
584
                        if (tiflags & TH_PUSH) {
585
                                tp->t_flags |= TF_ACKNOW;
586
                                tcp_output(tp);
587
                        } else {
588
                                tp->t_flags |= TF_DELACK;
589
                        }
590
#else
591
                        tp->t_flags |= TF_DELACK;
592
#endif
593
                        return;
594
                }
595
        }
596
 
597
        /*
598
         * Calculate amount of space in receive window,
599
         * and then do TCP input processing.
600
         * Receive window is amount of space in rcv queue,
601
         * but not less than advertised window.
602
         */
603
        { int win;
604
 
605
        win = sbspace(&so->so_rcv);
606
        if (win < 0)
607
                win = 0;
608
        tp->rcv_wnd = imax(win, (int)(tp->rcv_adv - tp->rcv_nxt));
609
        }
610
 
611
        switch (tp->t_state) {
612
 
613
        /*
614
         * If the state is LISTEN then ignore segment if it contains an RST.
615
         * If the segment contains an ACK then it is bad and send a RST.
616
         * If it does not contain a SYN then it is not interesting; drop it.
617
         * If it is from this socket, drop it, it must be forged.
618
         * Don't bother responding if the destination was a broadcast.
619
         * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
620
         * tp->iss, and send a segment:
621
         *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
622
         * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
623
         * Fill in remote peer address fields if not previously specified.
624
         * Enter SYN_RECEIVED state, and process any other fields of this
625
         * segment in this state.
626
         */
627
        case TCPS_LISTEN: {
628
                struct mbuf *am;
629
                register struct sockaddr_in *sin;
630
 
631
                if (tiflags & TH_RST)
632
                        goto drop;
633
                if (tiflags & TH_ACK)
634
                        goto dropwithreset;
635
                if ((tiflags & TH_SYN) == 0)
636
                        goto drop;
637
                if ((ti->ti_dport == ti->ti_sport) &&
638
                    (ti->ti_dst.s_addr == ti->ti_src.s_addr))
639
                        goto drop;
640
                /*
641
                 * RFC1122 4.2.3.10, p. 104: discard bcast/mcast SYN
642
                 * in_broadcast() should never return true on a received
643
                 * packet with M_BCAST not set.
644
                 */
645
                if (m->m_flags & (M_BCAST|M_MCAST) ||
646
                    IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
647
                        goto drop;
648
                am = m_get(M_DONTWAIT, MT_SONAME);      /* XXX */
649
                if (am == NULL)
650
                        goto drop;
651
                am->m_len = sizeof (struct sockaddr_in);
652
                sin = mtod(am, struct sockaddr_in *);
653
                sin->sin_family = AF_INET;
654
                sin->sin_len = sizeof(*sin);
655
                sin->sin_addr = ti->ti_src;
656
                sin->sin_port = ti->ti_sport;
657
                bzero((caddr_t)sin->sin_zero, sizeof(sin->sin_zero));
658
                laddr = inp->inp_laddr;
659
                if (inp->inp_laddr.s_addr == INADDR_ANY)
660
                        inp->inp_laddr = ti->ti_dst;
661
                if (in_pcbconnect(inp, am)) {
662
                        inp->inp_laddr = laddr;
663
                        (void) m_free(am);
664
                        goto drop;
665
                }
666
                (void) m_free(am);
667
                tp->t_template = tcp_template(tp);
668
                if (tp->t_template == 0) {
669
                        tp = tcp_drop(tp, ENOBUFS);
670
                        dropsocket = 0;          /* socket is already gone */
671
                        goto drop;
672
                }
673
                if ((taop = tcp_gettaocache(inp)) == NULL) {
674
                        taop = &tao_noncached;
675
                        bzero(taop, sizeof(*taop));
676
                }
677
                tcp_dooptions(tp, optp, optlen, ti, &to);
678
                if (iss)
679
                        tp->iss = iss;
680
                else
681
                        tp->iss = tcp_iss;
682
                tcp_iss += TCP_ISSINCR/4;
683
                tp->irs = ti->ti_seq;
684
                tcp_sendseqinit(tp);
685
                tcp_rcvseqinit(tp);
686
                /*
687
                 * Initialization of the tcpcb for transaction;
688
                 *   set SND.WND = SEG.WND,
689
                 *   initialize CCsend and CCrecv.
690
                 */
691
                tp->snd_wnd = tiwin;    /* initial send-window */
692
                tp->cc_send = CC_INC(tcp_ccgen);
693
                tp->cc_recv = to.to_cc;
694
                /*
695
                 * Perform TAO test on incoming CC (SEG.CC) option, if any.
696
                 * - compare SEG.CC against cached CC from the same host,
697
                 *      if any.
698
                 * - if SEG.CC > chached value, SYN must be new and is accepted
699
                 *      immediately: save new CC in the cache, mark the socket
700
                 *      connected, enter ESTABLISHED state, turn on flag to
701
                 *      send a SYN in the next segment.
702
                 *      A virtual advertised window is set in rcv_adv to
703
                 *      initialize SWS prevention.  Then enter normal segment
704
                 *      processing: drop SYN, process data and FIN.
705
                 * - otherwise do a normal 3-way handshake.
706
                 */
707
                if ((to.to_flag & TOF_CC) != 0) {
708
                    if (taop->tao_cc != 0 && CC_GT(to.to_cc, taop->tao_cc)) {
709
                        taop->tao_cc = to.to_cc;
710
                        tp->t_state = TCPS_ESTABLISHED;
711
 
712
                        /*
713
                         * If there is a FIN, or if there is data and the
714
                         * connection is local, then delay SYN,ACK(SYN) in
715
                         * the hope of piggy-backing it on a response
716
                         * segment.  Otherwise must send ACK now in case
717
                         * the other side is slow starting.
718
                         */
719
                        if ((tiflags & TH_FIN) || (ti->ti_len != 0 &&
720
                            in_localaddr(inp->inp_faddr)))
721
                                tp->t_flags |= (TF_DELACK | TF_NEEDSYN);
722
                        else
723
                                tp->t_flags |= (TF_ACKNOW | TF_NEEDSYN);
724
 
725
                        /*
726
                         * Limit the `virtual advertised window' to TCP_MAXWIN
727
                         * here.  Even if we requested window scaling, it will
728
                         * become effective only later when our SYN is acked.
729
                         */
730
                        tp->rcv_adv += min(tp->rcv_wnd, TCP_MAXWIN);
731
                        tcpstat.tcps_connects++;
732
                        soisconnected(so);
733
                        tp->t_timer[TCPT_KEEP] = tcp_keepinit;
734
                        dropsocket = 0;          /* committed to socket */
735
                        tcpstat.tcps_accepts++;
736
                        goto trimthenstep6;
737
                    }
738
                /* else do standard 3-way handshake */
739
                } else {
740
                    /*
741
                     * No CC option, but maybe CC.NEW:
742
                     *   invalidate cached value.
743
                     */
744
                     taop->tao_cc = 0;
745
                }
746
                /*
747
                 * TAO test failed or there was no CC option,
748
                 *    do a standard 3-way handshake.
749
                 */
750
                tp->t_flags |= TF_ACKNOW;
751
                tp->t_state = TCPS_SYN_RECEIVED;
752
                tp->t_timer[TCPT_KEEP] = tcp_keepinit;
753
                dropsocket = 0;          /* committed to socket */
754
                tcpstat.tcps_accepts++;
755
                goto trimthenstep6;
756
                }
757
 
758
        /*
759
         * If the state is SYN_RECEIVED:
760
         *      if seg contains SYN/ACK, send a RST.
761
         *      if seg contains an ACK, but not for our SYN/ACK, send a RST.
762
         */
763
        case TCPS_SYN_RECEIVED:
764
                if (tiflags & TH_ACK) {
765
                        if (tiflags & TH_SYN) {
766
                                tcpstat.tcps_badsyn++;
767
                                goto dropwithreset;
768
                        }
769
                        if (SEQ_LEQ(ti->ti_ack, tp->snd_una) ||
770
                            SEQ_GT(ti->ti_ack, tp->snd_max))
771
                                goto dropwithreset;
772
                }
773
                break;
774
 
775
        /*
776
         * If the state is SYN_SENT:
777
         *      if seg contains an ACK, but not for our SYN, drop the input.
778
         *      if seg contains a RST, then drop the connection.
779
         *      if seg does not contain SYN, then drop it.
780
         * Otherwise this is an acceptable SYN segment
781
         *      initialize tp->rcv_nxt and tp->irs
782
         *      if seg contains ack then advance tp->snd_una
783
         *      if SYN has been acked change to ESTABLISHED else SYN_RCVD state
784
         *      arrange for segment to be acked (eventually)
785
         *      continue processing rest of data/controls, beginning with URG
786
         */
787
        case TCPS_SYN_SENT:
788
                if ((taop = tcp_gettaocache(inp)) == NULL) {
789
                        taop = &tao_noncached;
790
                        bzero(taop, sizeof(*taop));
791
                }
792
 
793
                if ((tiflags & TH_ACK) &&
794
                    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
795
                     SEQ_GT(ti->ti_ack, tp->snd_max))) {
796
                        /*
797
                         * If we have a cached CCsent for the remote host,
798
                         * hence we haven't just crashed and restarted,
799
                         * do not send a RST.  This may be a retransmission
800
                         * from the other side after our earlier ACK was lost.
801
                         * Our new SYN, when it arrives, will serve as the
802
                         * needed ACK.
803
                         */
804
                        if (taop->tao_ccsent != 0)
805
                                goto drop;
806
                        else
807
                                goto dropwithreset;
808
                }
809
                if (tiflags & TH_RST) {
810
                        if (tiflags & TH_ACK)
811
                                tp = tcp_drop(tp, ECONNREFUSED);
812
                        goto drop;
813
                }
814
                if ((tiflags & TH_SYN) == 0)
815
                        goto drop;
816
                tp->snd_wnd = ti->ti_win;       /* initial send window */
817
                tp->cc_recv = to.to_cc;         /* foreign CC */
818
 
819
                tp->irs = ti->ti_seq;
820
                tcp_rcvseqinit(tp);
821
                if (tiflags & TH_ACK) {
822
                        /*
823
                         * Our SYN was acked.  If segment contains CC.ECHO
824
                         * option, check it to make sure this segment really
825
                         * matches our SYN.  If not, just drop it as old
826
                         * duplicate, but send an RST if we're still playing
827
                         * by the old rules.  If no CC.ECHO option, make sure
828
                         * we don't get fooled into using T/TCP.
829
                         */
830
                        if (to.to_flag & TOF_CCECHO) {
831
                                if (tp->cc_send != to.to_ccecho) {
832
                                        if (taop->tao_ccsent != 0)
833
                                                goto drop;
834
                                        else
835
                                                goto dropwithreset;
836
                                }
837
                        } else
838
                                tp->t_flags &= ~TF_RCVD_CC;
839
                        tcpstat.tcps_connects++;
840
                        soisconnected(so);
841
                        /* Do window scaling on this connection? */
842
                        if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
843
                                (TF_RCVD_SCALE|TF_REQ_SCALE)) {
844
                                tp->snd_scale = tp->requested_s_scale;
845
                                tp->rcv_scale = tp->request_r_scale;
846
                        }
847
                        /* Segment is acceptable, update cache if undefined. */
848
                        if (taop->tao_ccsent == 0)
849
                                taop->tao_ccsent = to.to_ccecho;
850
 
851
                        tp->rcv_adv += tp->rcv_wnd;
852
                        tp->snd_una++;          /* SYN is acked */
853
                        /*
854
                         * If there's data, delay ACK; if there's also a FIN
855
                         * ACKNOW will be turned on later.
856
                         */
857
                        if (ti->ti_len != 0)
858
                                tp->t_flags |= TF_DELACK;
859
                        else
860
                                tp->t_flags |= TF_ACKNOW;
861
                        /*
862
                         * Received <SYN,ACK> in SYN_SENT[*] state.
863
                         * Transitions:
864
                         *      SYN_SENT  --> ESTABLISHED
865
                         *      SYN_SENT* --> FIN_WAIT_1
866
                         */
867
                        if (tp->t_flags & TF_NEEDFIN) {
868
                                tp->t_state = TCPS_FIN_WAIT_1;
869
                                tp->t_flags &= ~TF_NEEDFIN;
870
                                tiflags &= ~TH_SYN;
871
                        } else {
872
                                tp->t_state = TCPS_ESTABLISHED;
873
                                tp->t_timer[TCPT_KEEP] = tcp_keepidle;
874
                        }
875
                } else {
876
                /*
877
                 *  Received initial SYN in SYN-SENT[*] state => simul-
878
                 *  taneous open.  If segment contains CC option and there is
879
                 *  a cached CC, apply TAO test; if it succeeds, connection is
880
                 *  half-synchronized.  Otherwise, do 3-way handshake:
881
                 *        SYN-SENT -> SYN-RECEIVED
882
                 *        SYN-SENT* -> SYN-RECEIVED*
883
                 *  If there was no CC option, clear cached CC value.
884
                 */
885
                        tp->t_flags |= TF_ACKNOW;
886
                        tp->t_timer[TCPT_REXMT] = 0;
887
                        if (to.to_flag & TOF_CC) {
888
                                if (taop->tao_cc != 0 &&
889
                                    CC_GT(to.to_cc, taop->tao_cc)) {
890
                                        /*
891
                                         * update cache and make transition:
892
                                         *        SYN-SENT -> ESTABLISHED*
893
                                         *        SYN-SENT* -> FIN-WAIT-1*
894
                                         */
895
                                        taop->tao_cc = to.to_cc;
896
                                        if (tp->t_flags & TF_NEEDFIN) {
897
                                                tp->t_state = TCPS_FIN_WAIT_1;
898
                                                tp->t_flags &= ~TF_NEEDFIN;
899
                                        } else {
900
                                                tp->t_state = TCPS_ESTABLISHED;
901
                                                tp->t_timer[TCPT_KEEP] = tcp_keepidle;
902
                                        }
903
                                        tp->t_flags |= TF_NEEDSYN;
904
                                } else
905
                                        tp->t_state = TCPS_SYN_RECEIVED;
906
                        } else {
907
                                /* CC.NEW or no option => invalidate cache */
908
                                taop->tao_cc = 0;
909
                                tp->t_state = TCPS_SYN_RECEIVED;
910
                        }
911
                }
912
 
913
trimthenstep6:
914
                /*
915
                 * Advance ti->ti_seq to correspond to first data byte.
916
                 * If data, trim to stay within window,
917
                 * dropping FIN if necessary.
918
                 */
919
                ti->ti_seq++;
920
                if (ti->ti_len > tp->rcv_wnd) {
921
                        todrop = ti->ti_len - tp->rcv_wnd;
922
                        m_adj(m, -todrop);
923
                        ti->ti_len = tp->rcv_wnd;
924
                        tiflags &= ~TH_FIN;
925
                        tcpstat.tcps_rcvpackafterwin++;
926
                        tcpstat.tcps_rcvbyteafterwin += todrop;
927
                }
928
                tp->snd_wl1 = ti->ti_seq - 1;
929
                tp->rcv_up = ti->ti_seq;
930
                /*
931
                 *  Client side of transaction: already sent SYN and data.
932
                 *  If the remote host used T/TCP to validate the SYN,
933
                 *  our data will be ACK'd; if so, enter normal data segment
934
                 *  processing in the middle of step 5, ack processing.
935
                 *  Otherwise, goto step 6.
936
                 */
937
                if (tiflags & TH_ACK)
938
                        goto process_ACK;
939
                goto step6;
940
        /*
941
         * If the state is LAST_ACK or CLOSING or TIME_WAIT:
942
         *      if segment contains a SYN and CC [not CC.NEW] option:
943
         *              if state == TIME_WAIT and connection duration > MSL,
944
         *                  drop packet and send RST;
945
         *
946
         *              if SEG.CC > CCrecv then is new SYN, and can implicitly
947
         *                  ack the FIN (and data) in retransmission queue.
948
         *                  Complete close and delete TCPCB.  Then reprocess
949
         *                  segment, hoping to find new TCPCB in LISTEN state;
950
         *
951
         *              else must be old SYN; drop it.
952
         *      else do normal processing.
953
         */
954
        case TCPS_LAST_ACK:
955
        case TCPS_CLOSING:
956
        case TCPS_TIME_WAIT:
957
                if ((tiflags & TH_SYN) &&
958
                    (to.to_flag & TOF_CC) && tp->cc_recv != 0) {
959
                        if (tp->t_state == TCPS_TIME_WAIT &&
960
                                        tp->t_duration > TCPTV_MSL)
961
                                goto dropwithreset;
962
                        if (CC_GT(to.to_cc, tp->cc_recv)) {
963
                                tp = tcp_close(tp);
964
                                goto findpcb;
965
                        }
966
                        else
967
                                goto drop;
968
                }
969
                break;  /* continue normal processing */
970
        }
971
 
972
        /*
973
         * States other than LISTEN or SYN_SENT.
974
         * First check timestamp, if present.
975
         * Then check the connection count, if present.
976
         * Then check that at least some bytes of segment are within
977
         * receive window.  If segment begins before rcv_nxt,
978
         * drop leading data (and SYN); if nothing left, just ack.
979
         *
980
         * RFC 1323 PAWS: If we have a timestamp reply on this segment
981
         * and it's less than ts_recent, drop it.
982
         */
983
        if ((to.to_flag & TOF_TS) != 0 && (tiflags & TH_RST) == 0 &&
984
            tp->ts_recent && TSTMP_LT(to.to_tsval, tp->ts_recent)) {
985
 
986
                /* Check to see if ts_recent is over 24 days old.  */
987
                if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
988
                        /*
989
                         * Invalidate ts_recent.  If this segment updates
990
                         * ts_recent, the age will be reset later and ts_recent
991
                         * will get a valid value.  If it does not, setting
992
                         * ts_recent to zero will at least satisfy the
993
                         * requirement that zero be placed in the timestamp
994
                         * echo reply when ts_recent isn't valid.  The
995
                         * age isn't reset until we get a valid ts_recent
996
                         * because we don't want out-of-order segments to be
997
                         * dropped when ts_recent is old.
998
                         */
999
                        tp->ts_recent = 0;
1000
                } else {
1001
                        tcpstat.tcps_rcvduppack++;
1002
                        tcpstat.tcps_rcvdupbyte += ti->ti_len;
1003
                        tcpstat.tcps_pawsdrop++;
1004
                        goto dropafterack;
1005
                }
1006
        }
1007
 
1008
        /*
1009
         * T/TCP mechanism
1010
         *   If T/TCP was negotiated and the segment doesn't have CC,
1011
         *   or if it's CC is wrong then drop the segment.
1012
         *   RST segments do not have to comply with this.
1013
         */
1014
        if ((tp->t_flags & (TF_REQ_CC|TF_RCVD_CC)) == (TF_REQ_CC|TF_RCVD_CC) &&
1015
            ((to.to_flag & TOF_CC) == 0 || tp->cc_recv != to.to_cc) &&
1016
            (tiflags & TH_RST) == 0)
1017
                goto dropafterack;
1018
 
1019
        todrop = tp->rcv_nxt - ti->ti_seq;
1020
        if (todrop > 0) {
1021
                if (tiflags & TH_SYN) {
1022
                        tiflags &= ~TH_SYN;
1023
                        ti->ti_seq++;
1024
                        if (ti->ti_urp > 1)
1025
                                ti->ti_urp--;
1026
                        else
1027
                                tiflags &= ~TH_URG;
1028
                        todrop--;
1029
                }
1030
                /*
1031
                 * Following if statement from Stevens, vol. 2, p. 960.
1032
                 */
1033
                if (todrop > ti->ti_len
1034
                    || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
1035
                        /*
1036
                         * Any valid FIN must be to the left of the window.
1037
                         * At this point the FIN must be a duplicate or out
1038
                         * of sequence; drop it.
1039
                         */
1040
                        tiflags &= ~TH_FIN;
1041
 
1042
                        /*
1043
                         * Send an ACK to resynchronize and drop any data.
1044
                         * But keep on processing for RST or ACK.
1045
                         */
1046
                        tp->t_flags |= TF_ACKNOW;
1047
                        todrop = ti->ti_len;
1048
                        tcpstat.tcps_rcvduppack++;
1049
                        tcpstat.tcps_rcvdupbyte += todrop;
1050
                } else {
1051
                        tcpstat.tcps_rcvpartduppack++;
1052
                        tcpstat.tcps_rcvpartdupbyte += todrop;
1053
                }
1054
                m_adj(m, todrop);
1055
                ti->ti_seq += todrop;
1056
                ti->ti_len -= todrop;
1057
                if (ti->ti_urp > todrop)
1058
                        ti->ti_urp -= todrop;
1059
                else {
1060
                        tiflags &= ~TH_URG;
1061
                        ti->ti_urp = 0;
1062
                }
1063
        }
1064
 
1065
        /*
1066
         * If new data are received on a connection after the
1067
         * user processes are gone, then RST the other end.
1068
         */
1069
        if ((so->so_state & SS_NOFDREF) &&
1070
            tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
1071
                tp = tcp_close(tp);
1072
                tcpstat.tcps_rcvafterclose++;
1073
                goto dropwithreset;
1074
        }
1075
 
1076
        /*
1077
         * If segment ends after window, drop trailing data
1078
         * (and PUSH and FIN); if nothing left, just ACK.
1079
         */
1080
        todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
1081
        if (todrop > 0) {
1082
                tcpstat.tcps_rcvpackafterwin++;
1083
                if (todrop >= ti->ti_len) {
1084
                        tcpstat.tcps_rcvbyteafterwin += ti->ti_len;
1085
                        /*
1086
                         * If a new connection request is received
1087
                         * while in TIME_WAIT, drop the old connection
1088
                         * and start over if the sequence numbers
1089
                         * are above the previous ones.
1090
                         */
1091
                        if (tiflags & TH_SYN &&
1092
                            tp->t_state == TCPS_TIME_WAIT &&
1093
                            SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
1094
                                iss = tp->rcv_nxt + TCP_ISSINCR;
1095
                                tp = tcp_close(tp);
1096
                                goto findpcb;
1097
                        }
1098
                        /*
1099
                         * If window is closed can only take segments at
1100
                         * window edge, and have to drop data and PUSH from
1101
                         * incoming segments.  Continue processing, but
1102
                         * remember to ack.  Otherwise, drop segment
1103
                         * and ack.
1104
                         */
1105
                        if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
1106
                                tp->t_flags |= TF_ACKNOW;
1107
                                tcpstat.tcps_rcvwinprobe++;
1108
                        } else
1109
                                goto dropafterack;
1110
                } else
1111
                        tcpstat.tcps_rcvbyteafterwin += todrop;
1112
                m_adj(m, -todrop);
1113
                ti->ti_len -= todrop;
1114
                tiflags &= ~(TH_PUSH|TH_FIN);
1115
        }
1116
 
1117
        /*
1118
         * If last ACK falls within this segment's sequence numbers,
1119
         * record its timestamp.
1120
         * NOTE that the test is modified according to the latest
1121
         * proposal of the tcplw@cray.com list (Braden 1993/04/26).
1122
         */
1123
        if ((to.to_flag & TOF_TS) != 0 &&
1124
            SEQ_LEQ(ti->ti_seq, tp->last_ack_sent)) {
1125
                tp->ts_recent_age = tcp_now;
1126
                tp->ts_recent = to.to_tsval;
1127
        }
1128
 
1129
        /*
1130
         * If the RST bit is set examine the state:
1131
         *    SYN_RECEIVED STATE:
1132
         *      If passive open, return to LISTEN state.
1133
         *      If active open, inform user that connection was refused.
1134
         *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
1135
         *      Inform user that connection was reset, and close tcb.
1136
         *    CLOSING, LAST_ACK, TIME_WAIT STATES
1137
         *      Close the tcb.
1138
         */
1139
        if (tiflags&TH_RST) switch (tp->t_state) {
1140
 
1141
        case TCPS_SYN_RECEIVED:
1142
                so->so_error = ECONNREFUSED;
1143
                goto close;
1144
 
1145
        case TCPS_ESTABLISHED:
1146
        case TCPS_FIN_WAIT_1:
1147
        case TCPS_FIN_WAIT_2:
1148
        case TCPS_CLOSE_WAIT:
1149
                so->so_error = ECONNRESET;
1150
        close:
1151
                tp->t_state = TCPS_CLOSED;
1152
                tcpstat.tcps_drops++;
1153
                tp = tcp_close(tp);
1154
                goto drop;
1155
 
1156
        case TCPS_CLOSING:
1157
        case TCPS_LAST_ACK:
1158
        case TCPS_TIME_WAIT:
1159
                tp = tcp_close(tp);
1160
                goto drop;
1161
        }
1162
 
1163
        /*
1164
         * If a SYN is in the window, then this is an
1165
         * error and we send an RST and drop the connection.
1166
         */
1167
        if (tiflags & TH_SYN) {
1168
                tp = tcp_drop(tp, ECONNRESET);
1169
                goto dropwithreset;
1170
        }
1171
 
1172
        /*
1173
         * If the ACK bit is off:  if in SYN-RECEIVED state or SENDSYN
1174
         * flag is on (half-synchronized state), then queue data for
1175
         * later processing; else drop segment and return.
1176
         */
1177
        if ((tiflags & TH_ACK) == 0) {
1178
                if (tp->t_state == TCPS_SYN_RECEIVED ||
1179
                    (tp->t_flags & TF_NEEDSYN))
1180
                        goto step6;
1181
                else
1182
                        goto drop;
1183
        }
1184
 
1185
        /*
1186
         * Ack processing.
1187
         */
1188
        switch (tp->t_state) {
1189
 
1190
        /*
1191
         * In SYN_RECEIVED state, the ack ACKs our SYN, so enter
1192
         * ESTABLISHED state and continue processing.
1193
         * The ACK was checked above.
1194
         */
1195
        case TCPS_SYN_RECEIVED:
1196
 
1197
                tcpstat.tcps_connects++;
1198
                soisconnected(so);
1199
                /* Do window scaling? */
1200
                if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1201
                        (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1202
                        tp->snd_scale = tp->requested_s_scale;
1203
                        tp->rcv_scale = tp->request_r_scale;
1204
                }
1205
                /*
1206
                 * Upon successful completion of 3-way handshake,
1207
                 * update cache.CC if it was undefined, pass any queued
1208
                 * data to the user, and advance state appropriately.
1209
                 */
1210
                if ((taop = tcp_gettaocache(inp)) != NULL &&
1211
                    taop->tao_cc == 0)
1212
                        taop->tao_cc = tp->cc_recv;
1213
 
1214
                /*
1215
                 * Make transitions:
1216
                 *      SYN-RECEIVED  -> ESTABLISHED
1217
                 *      SYN-RECEIVED* -> FIN-WAIT-1
1218
                 */
1219
                if (tp->t_flags & TF_NEEDFIN) {
1220
                        tp->t_state = TCPS_FIN_WAIT_1;
1221
                        tp->t_flags &= ~TF_NEEDFIN;
1222
                } else {
1223
                        tp->t_state = TCPS_ESTABLISHED;
1224
                        tp->t_timer[TCPT_KEEP] = tcp_keepidle;
1225
                }
1226
                /*
1227
                 * If segment contains data or ACK, will call tcp_reass()
1228
                 * later; if not, do so now to pass queued data to user.
1229
                 */
1230
                if (ti->ti_len == 0 && (tiflags & TH_FIN) == 0)
1231
                        (void) tcp_reass(tp, (struct tcpiphdr *)0,
1232
                            (struct mbuf *)0);
1233
                tp->snd_wl1 = ti->ti_seq - 1;
1234
                /* fall into ... */
1235
 
1236
        /*
1237
         * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1238
         * ACKs.  If the ack is in the range
1239
         *      tp->snd_una < ti->ti_ack <= tp->snd_max
1240
         * then advance tp->snd_una to ti->ti_ack and drop
1241
         * data from the retransmission queue.  If this ACK reflects
1242
         * more up to date window information we update our window information.
1243
         */
1244
        case TCPS_ESTABLISHED:
1245
        case TCPS_FIN_WAIT_1:
1246
        case TCPS_FIN_WAIT_2:
1247
        case TCPS_CLOSE_WAIT:
1248
        case TCPS_CLOSING:
1249
        case TCPS_LAST_ACK:
1250
        case TCPS_TIME_WAIT:
1251
 
1252
                if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1253
                        if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1254
                                tcpstat.tcps_rcvdupack++;
1255
                                /*
1256
                                 * If we have outstanding data (other than
1257
                                 * a window probe), this is a completely
1258
                                 * duplicate ack (ie, window info didn't
1259
                                 * change), the ack is the biggest we've
1260
                                 * seen and we've seen exactly our rexmt
1261
                                 * threshhold of them, assume a packet
1262
                                 * has been dropped and retransmit it.
1263
                                 * Kludge snd_nxt & the congestion
1264
                                 * window so we send only this one
1265
                                 * packet.
1266
                                 *
1267
                                 * We know we're losing at the current
1268
                                 * window size so do congestion avoidance
1269
                                 * (set ssthresh to half the current window
1270
                                 * and pull our congestion window back to
1271
                                 * the new ssthresh).
1272
                                 *
1273
                                 * Dup acks mean that packets have left the
1274
                                 * network (they're now cached at the receiver)
1275
                                 * so bump cwnd by the amount in the receiver
1276
                                 * to keep a constant cwnd packets in the
1277
                                 * network.
1278
                                 */
1279
                                if (tp->t_timer[TCPT_REXMT] == 0 ||
1280
                                    ti->ti_ack != tp->snd_una)
1281
                                        tp->t_dupacks = 0;
1282
                                else if (++tp->t_dupacks == tcprexmtthresh) {
1283
                                        tcp_seq onxt = tp->snd_nxt;
1284
                                        u_int win =
1285
                                            min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1286
                                                tp->t_maxseg;
1287
 
1288
                                        if (win < 2)
1289
                                                win = 2;
1290
                                        tp->snd_ssthresh = win * tp->t_maxseg;
1291
                                        tp->t_timer[TCPT_REXMT] = 0;
1292
                                        tp->t_rtt = 0;
1293
                                        tp->snd_nxt = ti->ti_ack;
1294
                                        tp->snd_cwnd = tp->t_maxseg;
1295
                                        (void) tcp_output(tp);
1296
                                        tp->snd_cwnd = tp->snd_ssthresh +
1297
                                               tp->t_maxseg * tp->t_dupacks;
1298
                                        if (SEQ_GT(onxt, tp->snd_nxt))
1299
                                                tp->snd_nxt = onxt;
1300
                                        goto drop;
1301
                                } else if (tp->t_dupacks > tcprexmtthresh) {
1302
                                        tp->snd_cwnd += tp->t_maxseg;
1303
                                        (void) tcp_output(tp);
1304
                                        goto drop;
1305
                                }
1306
                        } else
1307
                                tp->t_dupacks = 0;
1308
                        break;
1309
                }
1310
                /*
1311
                 * If the congestion window was inflated to account
1312
                 * for the other side's cached packets, retract it.
1313
                 */
1314
                if (tp->t_dupacks >= tcprexmtthresh &&
1315
                    tp->snd_cwnd > tp->snd_ssthresh)
1316
                        tp->snd_cwnd = tp->snd_ssthresh;
1317
                tp->t_dupacks = 0;
1318
                if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1319
                        tcpstat.tcps_rcvacktoomuch++;
1320
                        goto dropafterack;
1321
                }
1322
                /*
1323
                 *  If we reach this point, ACK is not a duplicate,
1324
                 *     i.e., it ACKs something we sent.
1325
                 */
1326
                if (tp->t_flags & TF_NEEDSYN) {
1327
                        /*
1328
                         * T/TCP: Connection was half-synchronized, and our
1329
                         * SYN has been ACK'd (so connection is now fully
1330
                         * synchronized).  Go to non-starred state,
1331
                         * increment snd_una for ACK of SYN, and check if
1332
                         * we can do window scaling.
1333
                         */
1334
                        tp->t_flags &= ~TF_NEEDSYN;
1335
                        tp->snd_una++;
1336
                        /* Do window scaling? */
1337
                        if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1338
                                (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1339
                                tp->snd_scale = tp->requested_s_scale;
1340
                                tp->rcv_scale = tp->request_r_scale;
1341
                        }
1342
                }
1343
 
1344
process_ACK:
1345
                acked = ti->ti_ack - tp->snd_una;
1346
                tcpstat.tcps_rcvackpack++;
1347
                tcpstat.tcps_rcvackbyte += acked;
1348
 
1349
                /*
1350
                 * If we have a timestamp reply, update smoothed
1351
                 * round trip time.  If no timestamp is present but
1352
                 * transmit timer is running and timed sequence
1353
                 * number was acked, update smoothed round trip time.
1354
                 * Since we now have an rtt measurement, cancel the
1355
                 * timer backoff (cf., Phil Karn's retransmit alg.).
1356
                 * Recompute the initial retransmit timer.
1357
                 */
1358
                if (to.to_flag & TOF_TS)
1359
                        tcp_xmit_timer(tp, tcp_now - to.to_tsecr + 1);
1360
                else if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1361
                        tcp_xmit_timer(tp,tp->t_rtt);
1362
 
1363
                /*
1364
                 * If all outstanding data is acked, stop retransmit
1365
                 * timer and remember to restart (more output or persist).
1366
                 * If there is more data to be acked, restart retransmit
1367
                 * timer, using current (possibly backed-off) value.
1368
                 */
1369
                if (ti->ti_ack == tp->snd_max) {
1370
                        tp->t_timer[TCPT_REXMT] = 0;
1371
                        needoutput = 1;
1372
                } else if (tp->t_timer[TCPT_PERSIST] == 0)
1373
                        tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1374
 
1375
                /*
1376
                 * If no data (only SYN) was ACK'd,
1377
                 *    skip rest of ACK processing.
1378
                 */
1379
                if (acked == 0)
1380
                        goto step6;
1381
 
1382
                /*
1383
                 * When new data is acked, open the congestion window.
1384
                 * If the window gives us less than ssthresh packets
1385
                 * in flight, open exponentially (maxseg per packet).
1386
                 * Otherwise open linearly: maxseg per window
1387
                 * (maxseg^2 / cwnd per packet).
1388
                 */
1389
                {
1390
                register u_int cw = tp->snd_cwnd;
1391
                register u_int incr = tp->t_maxseg;
1392
 
1393
                if (cw > tp->snd_ssthresh)
1394
                        incr = incr * incr / cw;
1395
                tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1396
                }
1397
                if (acked > so->so_snd.sb_cc) {
1398
                        tp->snd_wnd -= so->so_snd.sb_cc;
1399
                        sbdrop(&so->so_snd, (int)so->so_snd.sb_cc);
1400
                        ourfinisacked = 1;
1401
                } else {
1402
                        sbdrop(&so->so_snd, acked);
1403
                        tp->snd_wnd -= acked;
1404
                        ourfinisacked = 0;
1405
                }
1406
                if (so->so_snd.sb_flags & SB_NOTIFY)
1407
                        sowwakeup(so);
1408
                tp->snd_una = ti->ti_ack;
1409
                if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1410
                        tp->snd_nxt = tp->snd_una;
1411
 
1412
                switch (tp->t_state) {
1413
 
1414
                /*
1415
                 * In FIN_WAIT_1 STATE in addition to the processing
1416
                 * for the ESTABLISHED state if our FIN is now acknowledged
1417
                 * then enter FIN_WAIT_2.
1418
                 */
1419
                case TCPS_FIN_WAIT_1:
1420
                        if (ourfinisacked) {
1421
                                /*
1422
                                 * If we can't receive any more
1423
                                 * data, then closing user can proceed.
1424
                                 * Starting the timer is contrary to the
1425
                                 * specification, but if we don't get a FIN
1426
                                 * we'll hang forever.
1427
                                 */
1428
                                if (so->so_state & SS_CANTRCVMORE) {
1429
                                        soisdisconnected(so);
1430
                                        tp->t_timer[TCPT_2MSL] = tcp_maxidle;
1431
                                }
1432
                                tp->t_state = TCPS_FIN_WAIT_2;
1433
                        }
1434
                        break;
1435
 
1436
                /*
1437
                 * In CLOSING STATE in addition to the processing for
1438
                 * the ESTABLISHED state if the ACK acknowledges our FIN
1439
                 * then enter the TIME-WAIT state, otherwise ignore
1440
                 * the segment.
1441
                 */
1442
                case TCPS_CLOSING:
1443
                        if (ourfinisacked) {
1444
                                tp->t_state = TCPS_TIME_WAIT;
1445
                                tcp_canceltimers(tp);
1446
                                /* Shorten TIME_WAIT [RFC-1644, p.28] */
1447
                                if (tp->cc_recv != 0 &&
1448
                                    tp->t_duration < TCPTV_MSL)
1449
                                        tp->t_timer[TCPT_2MSL] =
1450
                                            tp->t_rxtcur * TCPTV_TWTRUNC;
1451
                                else
1452
                                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1453
                                soisdisconnected(so);
1454
                        }
1455
                        break;
1456
 
1457
                /*
1458
                 * In LAST_ACK, we may still be waiting for data to drain
1459
                 * and/or to be acked, as well as for the ack of our FIN.
1460
                 * If our FIN is now acknowledged, delete the TCB,
1461
                 * enter the closed state and return.
1462
                 */
1463
                case TCPS_LAST_ACK:
1464
                        if (ourfinisacked) {
1465
                                tp = tcp_close(tp);
1466
                                goto drop;
1467
                        }
1468
                        break;
1469
 
1470
                /*
1471
                 * In TIME_WAIT state the only thing that should arrive
1472
                 * is a retransmission of the remote FIN.  Acknowledge
1473
                 * it and restart the finack timer.
1474
                 */
1475
                case TCPS_TIME_WAIT:
1476
                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1477
                        goto dropafterack;
1478
                }
1479
        }
1480
 
1481
step6:
1482
        /*
1483
         * Update window information.
1484
         * Don't look at window if no ACK: TAC's send garbage on first SYN.
1485
         */
1486
        if ((tiflags & TH_ACK) &&
1487
            (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1488
            (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1489
             (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1490
                /* keep track of pure window updates */
1491
                if (ti->ti_len == 0 &&
1492
                    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1493
                        tcpstat.tcps_rcvwinupd++;
1494
                tp->snd_wnd = tiwin;
1495
                tp->snd_wl1 = ti->ti_seq;
1496
                tp->snd_wl2 = ti->ti_ack;
1497
                if (tp->snd_wnd > tp->max_sndwnd)
1498
                        tp->max_sndwnd = tp->snd_wnd;
1499
                needoutput = 1;
1500
        }
1501
 
1502
        /*
1503
         * Process segments with URG.
1504
         */
1505
        if ((tiflags & TH_URG) && ti->ti_urp &&
1506
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1507
                /*
1508
                 * This is a kludge, but if we receive and accept
1509
                 * random urgent pointers, we'll crash in
1510
                 * soreceive.  It's hard to imagine someone
1511
                 * actually wanting to send this much urgent data.
1512
                 */
1513
                if (ti->ti_urp + so->so_rcv.sb_cc > sb_max) {
1514
                        ti->ti_urp = 0;                  /* XXX */
1515
                        tiflags &= ~TH_URG;             /* XXX */
1516
                        goto dodata;                    /* XXX */
1517
                }
1518
                /*
1519
                 * If this segment advances the known urgent pointer,
1520
                 * then mark the data stream.  This should not happen
1521
                 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1522
                 * a FIN has been received from the remote side.
1523
                 * In these states we ignore the URG.
1524
                 *
1525
                 * According to RFC961 (Assigned Protocols),
1526
                 * the urgent pointer points to the last octet
1527
                 * of urgent data.  We continue, however,
1528
                 * to consider it to indicate the first octet
1529
                 * of data past the urgent section as the original
1530
                 * spec states (in one of two places).
1531
                 */
1532
                if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1533
                        tp->rcv_up = ti->ti_seq + ti->ti_urp;
1534
                        so->so_oobmark = so->so_rcv.sb_cc +
1535
                            (tp->rcv_up - tp->rcv_nxt) - 1;
1536
                        if (so->so_oobmark == 0)
1537
                                so->so_state |= SS_RCVATMARK;
1538
                        sohasoutofband(so);
1539
                        tp->t_oobflags &= ~(TCPOOB_HAVEDATA | TCPOOB_HADDATA);
1540
                }
1541
                /*
1542
                 * Remove out of band data so doesn't get presented to user.
1543
                 * This can happen independent of advancing the URG pointer,
1544
                 * but if two URG's are pending at once, some out-of-band
1545
                 * data may creep in... ick.
1546
                 */
1547
                if (ti->ti_urp <= (u_long)ti->ti_len
1548
#ifdef SO_OOBINLINE
1549
                     && (so->so_options & SO_OOBINLINE) == 0
1550
#endif
1551
                     )
1552
                        tcp_pulloutofband(so, ti, m);
1553
        } else
1554
                /*
1555
                 * If no out of band data is expected,
1556
                 * pull receive urgent pointer along
1557
                 * with the receive window.
1558
                 */
1559
                if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1560
                        tp->rcv_up = tp->rcv_nxt;
1561
dodata:                                                 /* XXX */
1562
 
1563
        /*
1564
         * Process the segment text, merging it into the TCP sequencing queue,
1565
         * and arranging for acknowledgment of receipt if necessary.
1566
         * This process logically involves adjusting tp->rcv_wnd as data
1567
         * is presented to the user (this happens in tcp_usrreq.c,
1568
         * case PRU_RCVD).  If a FIN has already been received on this
1569
         * connection then we just ignore the text.
1570
         */
1571
        if ((ti->ti_len || (tiflags&TH_FIN)) &&
1572
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1573
                TCP_REASS(tp, ti, m, so, tiflags);
1574
                /*
1575
                 * Note the amount of data that peer has sent into
1576
                 * our window, in order to estimate the sender's
1577
                 * buffer size.
1578
                 */
1579
                len = so->so_rcv.sb_hiwat - (tp->rcv_adv - tp->rcv_nxt);
1580
        } else {
1581
                m_freem(m);
1582
                tiflags &= ~TH_FIN;
1583
        }
1584
 
1585
        /*
1586
         * If FIN is received ACK the FIN and let the user know
1587
         * that the connection is closing.
1588
         */
1589
        if (tiflags & TH_FIN) {
1590
                if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1591
                        socantrcvmore(so);
1592
                        /*
1593
                         *  If connection is half-synchronized
1594
                         *  (ie NEEDSYN flag on) then delay ACK,
1595
                         *  so it may be piggybacked when SYN is sent.
1596
                         *  Otherwise, since we received a FIN then no
1597
                         *  more input can be expected, send ACK now.
1598
                         */
1599
                        if (tp->t_flags & TF_NEEDSYN)
1600
                                tp->t_flags |= TF_DELACK;
1601
                        else
1602
                                tp->t_flags |= TF_ACKNOW;
1603
                        tp->rcv_nxt++;
1604
                }
1605
                switch (tp->t_state) {
1606
 
1607
                /*
1608
                 * In SYN_RECEIVED and ESTABLISHED STATES
1609
                 * enter the CLOSE_WAIT state.
1610
                 */
1611
                case TCPS_SYN_RECEIVED:
1612
                case TCPS_ESTABLISHED:
1613
                        tp->t_state = TCPS_CLOSE_WAIT;
1614
                        break;
1615
 
1616
                /*
1617
                 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1618
                 * enter the CLOSING state.
1619
                 */
1620
                case TCPS_FIN_WAIT_1:
1621
                        tp->t_state = TCPS_CLOSING;
1622
                        break;
1623
 
1624
                /*
1625
                 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1626
                 * starting the time-wait timer, turning off the other
1627
                 * standard timers.
1628
                 */
1629
                case TCPS_FIN_WAIT_2:
1630
                        tp->t_state = TCPS_TIME_WAIT;
1631
                        tcp_canceltimers(tp);
1632
                        /* Shorten TIME_WAIT [RFC-1644, p.28] */
1633
                        if (tp->cc_recv != 0 &&
1634
                            tp->t_duration < TCPTV_MSL) {
1635
                                tp->t_timer[TCPT_2MSL] =
1636
                                    tp->t_rxtcur * TCPTV_TWTRUNC;
1637
                                /* For transaction client, force ACK now. */
1638
                                tp->t_flags |= TF_ACKNOW;
1639
                        }
1640
                        else
1641
                                tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1642
                        soisdisconnected(so);
1643
                        break;
1644
 
1645
                /*
1646
                 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1647
                 */
1648
                case TCPS_TIME_WAIT:
1649
                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1650
                        break;
1651
                }
1652
        }
1653
#ifdef TCPDEBUG
1654
        if (so->so_options & SO_DEBUG)
1655
                tcp_trace(TA_INPUT, ostate, tp, &tcp_saveti, 0);
1656
#endif
1657
 
1658
        /*
1659
         * Return any desired output.
1660
         */
1661
        if (needoutput || (tp->t_flags & TF_ACKNOW))
1662
                (void) tcp_output(tp);
1663
        return;
1664
 
1665
dropafterack:
1666
        /*
1667
         * Generate an ACK dropping incoming segment if it occupies
1668
         * sequence space, where the ACK reflects our state.
1669
         */
1670
        if (tiflags & TH_RST)
1671
                goto drop;
1672
#ifdef TCPDEBUG
1673
        if (so->so_options & SO_DEBUG)
1674
                tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1675
#endif
1676
        m_freem(m);
1677
        tp->t_flags |= TF_ACKNOW;
1678
        (void) tcp_output(tp);
1679
        return;
1680
 
1681
dropwithreset:
1682
        /*
1683
         * Generate a RST, dropping incoming segment.
1684
         * Make ACK acceptable to originator of segment.
1685
         * Don't bother to respond if destination was broadcast/multicast.
1686
         */
1687
        if ((tiflags & TH_RST) || m->m_flags & (M_BCAST|M_MCAST) ||
1688
            IN_MULTICAST(ntohl(ti->ti_dst.s_addr)))
1689
                goto drop;
1690
#ifdef TCPDEBUG
1691
        if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1692
                tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1693
#endif
1694
        if (tiflags & TH_ACK)
1695
                tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1696
        else {
1697
                if (tiflags & TH_SYN)
1698
                        ti->ti_len++;
1699
                tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1700
                    TH_RST|TH_ACK);
1701
        }
1702
        /* destroy temporarily created socket */
1703
        if (dropsocket)
1704
                (void) soabort(so);
1705
        return;
1706
 
1707
drop:
1708
        /*
1709
         * Drop space held by incoming segment and return.
1710
         */
1711
#ifdef TCPDEBUG
1712
        if (tp == 0 || (tp->t_inpcb->inp_socket->so_options & SO_DEBUG))
1713
                tcp_trace(TA_DROP, ostate, tp, &tcp_saveti, 0);
1714
#endif
1715
        m_freem(m);
1716
        /* destroy temporarily created socket */
1717
        if (dropsocket)
1718
                (void) soabort(so);
1719
        return;
1720
#ifndef TUBA_INCLUDE
1721
}
1722
 
1723
static void
1724
tcp_dooptions(tp, cp, cnt, ti, to)
1725
        struct tcpcb *tp;
1726
        u_char *cp;
1727
        int cnt;
1728
        struct tcpiphdr *ti;
1729
        struct tcpopt *to;
1730
{
1731
        u_short mss = 0;
1732
        int opt, optlen;
1733
 
1734
        for (; cnt > 0; cnt -= optlen, cp += optlen) {
1735
                opt = cp[0];
1736
                if (opt == TCPOPT_EOL)
1737
                        break;
1738
                if (opt == TCPOPT_NOP)
1739
                        optlen = 1;
1740
                else {
1741
                        optlen = cp[1];
1742
                        if (optlen <= 0)
1743
                                break;
1744
                }
1745
                switch (opt) {
1746
 
1747
                default:
1748
                        continue;
1749
 
1750
                case TCPOPT_MAXSEG:
1751
                        if (optlen != TCPOLEN_MAXSEG)
1752
                                continue;
1753
                        if (!(ti->ti_flags & TH_SYN))
1754
                                continue;
1755
                        bcopy((char *) cp + 2, (char *) &mss, sizeof(mss));
1756
                        NTOHS(mss);
1757
                        break;
1758
 
1759
                case TCPOPT_WINDOW:
1760
                        if (optlen != TCPOLEN_WINDOW)
1761
                                continue;
1762
                        if (!(ti->ti_flags & TH_SYN))
1763
                                continue;
1764
                        tp->t_flags |= TF_RCVD_SCALE;
1765
                        tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1766
                        break;
1767
 
1768
                case TCPOPT_TIMESTAMP:
1769
                        if (optlen != TCPOLEN_TIMESTAMP)
1770
                                continue;
1771
                        to->to_flag |= TOF_TS;
1772
                        bcopy((char *)cp + 2,
1773
                            (char *)&to->to_tsval, sizeof(to->to_tsval));
1774
                        NTOHL(to->to_tsval);
1775
                        bcopy((char *)cp + 6,
1776
                            (char *)&to->to_tsecr, sizeof(to->to_tsecr));
1777
                        NTOHL(to->to_tsecr);
1778
 
1779
                        /*
1780
                         * A timestamp received in a SYN makes
1781
                         * it ok to send timestamp requests and replies.
1782
                         */
1783
                        if (ti->ti_flags & TH_SYN) {
1784
                                tp->t_flags |= TF_RCVD_TSTMP;
1785
                                tp->ts_recent = to->to_tsval;
1786
                                tp->ts_recent_age = tcp_now;
1787
                        }
1788
                        break;
1789
                case TCPOPT_CC:
1790
                        if (optlen != TCPOLEN_CC)
1791
                                continue;
1792
                        to->to_flag |= TOF_CC;
1793
                        bcopy((char *)cp + 2,
1794
                            (char *)&to->to_cc, sizeof(to->to_cc));
1795
                        NTOHL(to->to_cc);
1796
                        /*
1797
                         * A CC or CC.new option received in a SYN makes
1798
                         * it ok to send CC in subsequent segments.
1799
                         */
1800
                        if (ti->ti_flags & TH_SYN)
1801
                                tp->t_flags |= TF_RCVD_CC;
1802
                        break;
1803
                case TCPOPT_CCNEW:
1804
                        if (optlen != TCPOLEN_CC)
1805
                                continue;
1806
                        if (!(ti->ti_flags & TH_SYN))
1807
                                continue;
1808
                        to->to_flag |= TOF_CCNEW;
1809
                        bcopy((char *)cp + 2,
1810
                            (char *)&to->to_cc, sizeof(to->to_cc));
1811
                        NTOHL(to->to_cc);
1812
                        /*
1813
                         * A CC or CC.new option received in a SYN makes
1814
                         * it ok to send CC in subsequent segments.
1815
                         */
1816
                        tp->t_flags |= TF_RCVD_CC;
1817
                        break;
1818
                case TCPOPT_CCECHO:
1819
                        if (optlen != TCPOLEN_CC)
1820
                                continue;
1821
                        if (!(ti->ti_flags & TH_SYN))
1822
                                continue;
1823
                        to->to_flag |= TOF_CCECHO;
1824
                        bcopy((char *)cp + 2,
1825
                            (char *)&to->to_ccecho, sizeof(to->to_ccecho));
1826
                        NTOHL(to->to_ccecho);
1827
                        break;
1828
                }
1829
        }
1830
        if (ti->ti_flags & TH_SYN)
1831
                tcp_mss(tp, mss);       /* sets t_maxseg */
1832
}
1833
 
1834
/*
1835
 * Pull out of band byte out of a segment so
1836
 * it doesn't appear in the user's data queue.
1837
 * It is still reflected in the segment length for
1838
 * sequencing purposes.
1839
 */
1840
static void
1841
tcp_pulloutofband(so, ti, m)
1842
        struct socket *so;
1843
        struct tcpiphdr *ti;
1844
        register struct mbuf *m;
1845
{
1846
        int cnt = ti->ti_urp - 1;
1847
 
1848
        while (cnt >= 0) {
1849
                if (m->m_len > cnt) {
1850
                        char *cp = mtod(m, caddr_t) + cnt;
1851
                        struct tcpcb *tp = sototcpcb(so);
1852
 
1853
                        tp->t_iobc = *cp;
1854
                        tp->t_oobflags |= TCPOOB_HAVEDATA;
1855
                        bcopy(cp+1, cp, (unsigned)(m->m_len - cnt - 1));
1856
                        m->m_len--;
1857
                        return;
1858
                }
1859
                cnt -= m->m_len;
1860
                m = m->m_next;
1861
                if (m == 0)
1862
                        break;
1863
        }
1864
        panic("tcp_pulloutofband");
1865
}
1866
 
1867
/*
1868
 * Collect new round-trip time estimate
1869
 * and update averages and current timeout.
1870
 */
1871
static void
1872
tcp_xmit_timer(tp, rtt)
1873
        register struct tcpcb *tp;
1874
        short rtt;
1875
{
1876
        register int delta;
1877
 
1878
        tcpstat.tcps_rttupdated++;
1879
        tp->t_rttupdated++;
1880
        if (tp->t_srtt != 0) {
1881
                /*
1882
                 * srtt is stored as fixed point with 5 bits after the
1883
                 * binary point (i.e., scaled by 8).  The following magic
1884
                 * is equivalent to the smoothing algorithm in rfc793 with
1885
                 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1886
                 * point).  Adjust rtt to origin 0.
1887
                 */
1888
                delta = ((rtt - 1) << TCP_DELTA_SHIFT)
1889
                        - (tp->t_srtt >> (TCP_RTT_SHIFT - TCP_DELTA_SHIFT));
1890
 
1891
                if ((tp->t_srtt += delta) <= 0)
1892
                        tp->t_srtt = 1;
1893
 
1894
                /*
1895
                 * We accumulate a smoothed rtt variance (actually, a
1896
                 * smoothed mean difference), then set the retransmit
1897
                 * timer to smoothed rtt + 4 times the smoothed variance.
1898
                 * rttvar is stored as fixed point with 4 bits after the
1899
                 * binary point (scaled by 16).  The following is
1900
                 * equivalent to rfc793 smoothing with an alpha of .75
1901
                 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
1902
                 * rfc793's wired-in beta.
1903
                 */
1904
                if (delta < 0)
1905
                        delta = -delta;
1906
                delta -= tp->t_rttvar >> (TCP_RTTVAR_SHIFT - TCP_DELTA_SHIFT);
1907
                if ((tp->t_rttvar += delta) <= 0)
1908
                        tp->t_rttvar = 1;
1909
        } else {
1910
                /*
1911
                 * No rtt measurement yet - use the unsmoothed rtt.
1912
                 * Set the variance to half the rtt (so our first
1913
                 * retransmit happens at 3*rtt).
1914
                 */
1915
                tp->t_srtt = rtt << TCP_RTT_SHIFT;
1916
                tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1917
        }
1918
        tp->t_rtt = 0;
1919
        tp->t_rxtshift = 0;
1920
 
1921
        /*
1922
         * the retransmit should happen at rtt + 4 * rttvar.
1923
         * Because of the way we do the smoothing, srtt and rttvar
1924
         * will each average +1/2 tick of bias.  When we compute
1925
         * the retransmit timer, we want 1/2 tick of rounding and
1926
         * 1 extra tick because of +-1/2 tick uncertainty in the
1927
         * firing of the timer.  The bias will give us exactly the
1928
         * 1.5 tick we need.  But, because the bias is
1929
         * statistical, we have to test that we don't drop below
1930
         * the minimum feasible timer (which is 2 ticks).
1931
         */
1932
        TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1933
                      max(tp->t_rttmin, rtt + 2), TCPTV_REXMTMAX);
1934
 
1935
        /*
1936
         * We received an ack for a packet that wasn't retransmitted;
1937
         * it is probably safe to discard any error indications we've
1938
         * received recently.  This isn't quite right, but close enough
1939
         * for now (a route might have failed after we sent a segment,
1940
         * and the return path might not be symmetrical).
1941
         */
1942
        tp->t_softerror = 0;
1943
}
1944
 
1945
/*
1946
 * Determine a reasonable value for maxseg size.
1947
 * If the route is known, check route for mtu.
1948
 * If none, use an mss that can be handled on the outgoing
1949
 * interface without forcing IP to fragment; if bigger than
1950
 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1951
 * to utilize large mbufs.  If no route is found, route has no mtu,
1952
 * or the destination isn't local, use a default, hopefully conservative
1953
 * size (usually 512 or the default IP max size, but no more than the mtu
1954
 * of the interface), as we can't discover anything about intervening
1955
 * gateways or networks.  We also initialize the congestion/slow start
1956
 * window to be a single segment if the destination isn't local.
1957
 * While looking at the routing entry, we also initialize other path-dependent
1958
 * parameters from pre-set or cached values in the routing entry.
1959
 *
1960
 * Also take into account the space needed for options that we
1961
 * send regularly.  Make maxseg shorter by that amount to assure
1962
 * that we can send maxseg amount of data even when the options
1963
 * are present.  Store the upper limit of the length of options plus
1964
 * data in maxopd.
1965
 *
1966
 * NOTE that this routine is only called when we process an incoming
1967
 * segment, for outgoing segments only tcp_mssopt is called.
1968
 *
1969
 * In case of T/TCP, we call this routine during implicit connection
1970
 * setup as well (offer = -1), to initialize maxseg from the cached
1971
 * MSS of our peer.
1972
 */
1973
void
1974
tcp_mss(tp, offer)
1975
        struct tcpcb *tp;
1976
        int offer;
1977
{
1978
        register struct rtentry *rt;
1979
        struct ifnet *ifp;
1980
        register int rtt, mss;
1981
        u_long bufsize;
1982
        struct inpcb *inp;
1983
        struct socket *so;
1984
        struct rmxp_tao *taop;
1985
        int origoffer = offer;
1986
 
1987
        inp = tp->t_inpcb;
1988
        if ((rt = tcp_rtlookup(inp)) == NULL) {
1989
                tp->t_maxopd = tp->t_maxseg = tcp_mssdflt;
1990
                return;
1991
        }
1992
        ifp = rt->rt_ifp;
1993
        so = inp->inp_socket;
1994
 
1995
        taop = rmx_taop(rt->rt_rmx);
1996
        /*
1997
         * Offer == -1 means that we didn't receive SYN yet,
1998
         * use cached value in that case;
1999
         */
2000
        if (offer == -1)
2001
                offer = taop->tao_mssopt;
2002
        /*
2003
         * Offer == 0 means that there was no MSS on the SYN segment,
2004
         * in this case we use tcp_mssdflt.
2005
         */
2006
        if (offer == 0)
2007
                offer = tcp_mssdflt;
2008
        else
2009
                /*
2010
                 * Sanity check: make sure that maxopd will be large
2011
                 * enough to allow some data on segments even is the
2012
                 * all the option space is used (40bytes).  Otherwise
2013
                 * funny things may happen in tcp_output.
2014
                 */
2015
                offer = max(offer, 64);
2016
        taop->tao_mssopt = offer;
2017
 
2018
        /*
2019
         * While we're here, check if there's an initial rtt
2020
         * or rttvar.  Convert from the route-table units
2021
         * to scaled multiples of the slow timeout timer.
2022
         */
2023
        if (tp->t_srtt == 0 && (rtt = rt->rt_rmx.rmx_rtt)) {
2024
                /*
2025
                 * XXX the lock bit for RTT indicates that the value
2026
                 * is also a minimum value; this is subject to time.
2027
                 */
2028
                if (rt->rt_rmx.rmx_locks & RTV_RTT)
2029
                        tp->t_rttmin = rtt / (RTM_RTTUNIT / PR_SLOWHZ);
2030
                tp->t_srtt = rtt / (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTT_SCALE));
2031
                tcpstat.tcps_usedrtt++;
2032
                if (rt->rt_rmx.rmx_rttvar) {
2033
                        tp->t_rttvar = rt->rt_rmx.rmx_rttvar /
2034
                            (RTM_RTTUNIT / (PR_SLOWHZ * TCP_RTTVAR_SCALE));
2035
                        tcpstat.tcps_usedrttvar++;
2036
                } else {
2037
                        /* default variation is +- 1 rtt */
2038
                        tp->t_rttvar =
2039
                            tp->t_srtt * TCP_RTTVAR_SCALE / TCP_RTT_SCALE;
2040
                }
2041
                TCPT_RANGESET(tp->t_rxtcur,
2042
                    ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1,
2043
                    tp->t_rttmin, TCPTV_REXMTMAX);
2044
        }
2045
        /*
2046
         * if there's an mtu associated with the route, use it
2047
         */
2048
        if (rt->rt_rmx.rmx_mtu)
2049
                mss = rt->rt_rmx.rmx_mtu - sizeof(struct tcpiphdr);
2050
        else
2051
        {
2052
                mss = ifp->if_mtu - sizeof(struct tcpiphdr);
2053
                if (!in_localaddr(inp->inp_faddr))
2054
                        mss = min(mss, tcp_mssdflt);
2055
        }
2056
        mss = min(mss, offer);
2057
        /*
2058
         * maxopd stores the maximum length of data AND options
2059
         * in a segment; maxseg is the amount of data in a normal
2060
         * segment.  We need to store this value (maxopd) apart
2061
         * from maxseg, because now every segment carries options
2062
         * and thus we normally have somewhat less data in segments.
2063
         */
2064
        tp->t_maxopd = mss;
2065
 
2066
        /*
2067
         * In case of T/TCP, origoffer==-1 indicates, that no segments
2068
         * were received yet.  In this case we just guess, otherwise
2069
         * we do the same as before T/TCP.
2070
         */
2071
        if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
2072
            (origoffer == -1 ||
2073
             (tp->t_flags & TF_RCVD_TSTMP) == TF_RCVD_TSTMP))
2074
                mss -= TCPOLEN_TSTAMP_APPA;
2075
        if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
2076
            (origoffer == -1 ||
2077
             (tp->t_flags & TF_RCVD_CC) == TF_RCVD_CC))
2078
                mss -= TCPOLEN_CC_APPA;
2079
 
2080
#if     (MCLBYTES & (MCLBYTES - 1)) == 0
2081
                if (mss > MCLBYTES)
2082
                        mss &= ~(MCLBYTES-1);
2083
#else
2084
                if (mss > MCLBYTES)
2085
                        mss = mss / MCLBYTES * MCLBYTES;
2086
#endif
2087
        /*
2088
         * If there's a pipesize, change the socket buffer
2089
         * to that size.  Make the socket buffers an integral
2090
         * number of mss units; if the mss is larger than
2091
         * the socket buffer, decrease the mss.
2092
         */
2093
#ifdef RTV_SPIPE
2094
        if ((bufsize = rt->rt_rmx.rmx_sendpipe) == 0)
2095
#endif
2096
                bufsize = so->so_snd.sb_hiwat;
2097
        if (bufsize < mss)
2098
                mss = bufsize;
2099
        else {
2100
                bufsize = roundup(bufsize, mss);
2101
                if (bufsize > sb_max)
2102
                        bufsize = sb_max;
2103
                (void)sbreserve(&so->so_snd, bufsize);
2104
        }
2105
        tp->t_maxseg = mss;
2106
 
2107
#ifdef RTV_RPIPE
2108
        if ((bufsize = rt->rt_rmx.rmx_recvpipe) == 0)
2109
#endif
2110
                bufsize = so->so_rcv.sb_hiwat;
2111
        if (bufsize > mss) {
2112
                bufsize = roundup(bufsize, mss);
2113
                if (bufsize > sb_max)
2114
                        bufsize = sb_max;
2115
                (void)sbreserve(&so->so_rcv, bufsize);
2116
        }
2117
        /*
2118
         * Don't force slow-start on local network.
2119
         */
2120
        if (!in_localaddr(inp->inp_faddr))
2121
                tp->snd_cwnd = mss;
2122
 
2123
        if (rt->rt_rmx.rmx_ssthresh) {
2124
                /*
2125
                 * There's some sort of gateway or interface
2126
                 * buffer limit on the path.  Use this to set
2127
                 * the slow start threshhold, but set the
2128
                 * threshold to no less than 2*mss.
2129
                 */
2130
                tp->snd_ssthresh = max(2 * mss, rt->rt_rmx.rmx_ssthresh);
2131
                tcpstat.tcps_usedssthresh++;
2132
        }
2133
}
2134
 
2135
/*
2136
 * Determine the MSS option to send on an outgoing SYN.
2137
 */
2138
int
2139
tcp_mssopt(tp)
2140
        struct tcpcb *tp;
2141
{
2142
        struct rtentry *rt;
2143
 
2144
        rt = tcp_rtlookup(tp->t_inpcb);
2145
        if (rt == NULL)
2146
                return tcp_mssdflt;
2147
 
2148
        return rt->rt_ifp->if_mtu - sizeof(struct tcpiphdr);
2149
}
2150
#endif /* TUBA_INCLUDE */

powered by: WebSVN 2.1.0

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