Statistics
| Branch: | Revision:

root / slirp / tcp_input.c @ f932b6ce

History | View | Annotate | Download (48.4 kB)

1
/*
2
 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1994
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. Neither the name of the University nor the names of its contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 *
29
 *        @(#)tcp_input.c        8.5 (Berkeley) 4/10/94
30
 * tcp_input.c,v 1.10 1994/10/13 18:36:32 wollman Exp
31
 */
32

    
33
/*
34
 * Changes and additions relating to SLiRP
35
 * Copyright (c) 1995 Danny Gasparovski.
36
 *
37
 * Please read the file COPYRIGHT for the
38
 * terms and conditions of the copyright.
39
 */
40

    
41
#include <slirp.h>
42
#include "ip_icmp.h"
43

    
44
struct socket tcb;
45

    
46
#define        TCPREXMTTHRESH 3
47
struct        socket *tcp_last_so = &tcb;
48

    
49
tcp_seq tcp_iss;                /* tcp initial send seq # */
50

    
51
#define TCP_PAWS_IDLE        (24 * 24 * 60 * 60 * PR_SLOWHZ)
52

    
53
/* for modulo comparisons of timestamps */
54
#define TSTMP_LT(a,b)        ((int)((a)-(b)) < 0)
55
#define TSTMP_GEQ(a,b)        ((int)((a)-(b)) >= 0)
56

    
57
/*
58
 * Insert segment ti into reassembly queue of tcp with
59
 * control block tp.  Return TH_FIN if reassembly now includes
60
 * a segment with FIN.  The macro form does the common case inline
61
 * (segment is the next to be received on an established connection,
62
 * and the queue is empty), avoiding linkage into and removal
63
 * from the queue and repetition of various conversions.
64
 * Set DELACK for segments received in order, but ack immediately
65
 * when segments are out of order (so fast retransmit can work).
66
 */
67
#ifdef TCP_ACK_HACK
68
#define TCP_REASS(tp, ti, m, so, flags) {\
69
       if ((ti)->ti_seq == (tp)->rcv_nxt && \
70
           tcpfrag_list_empty(tp) && \
71
           (tp)->t_state == TCPS_ESTABLISHED) {\
72
               if (ti->ti_flags & TH_PUSH) \
73
                       tp->t_flags |= TF_ACKNOW; \
74
               else \
75
                       tp->t_flags |= TF_DELACK; \
76
               (tp)->rcv_nxt += (ti)->ti_len; \
77
               flags = (ti)->ti_flags & TH_FIN; \
78
               STAT(tcpstat.tcps_rcvpack++);         \
79
               STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len);   \
80
               if (so->so_emu) { \
81
                       if (tcp_emu((so),(m))) sbappend((so), (m)); \
82
               } else \
83
                              sbappend((so), (m)); \
84
/*               sorwakeup(so); */ \
85
        } else {\
86
               (flags) = tcp_reass((tp), (ti), (m)); \
87
               tp->t_flags |= TF_ACKNOW; \
88
       } \
89
}
90
#else
91
#define        TCP_REASS(tp, ti, m, so, flags) { \
92
        if ((ti)->ti_seq == (tp)->rcv_nxt && \
93
        tcpfrag_list_empty(tp) && \
94
            (tp)->t_state == TCPS_ESTABLISHED) { \
95
                tp->t_flags |= TF_DELACK; \
96
                (tp)->rcv_nxt += (ti)->ti_len; \
97
                flags = (ti)->ti_flags & TH_FIN; \
98
                STAT(tcpstat.tcps_rcvpack++);        \
99
                STAT(tcpstat.tcps_rcvbyte += (ti)->ti_len);  \
100
                if (so->so_emu) { \
101
                        if (tcp_emu((so),(m))) sbappend(so, (m)); \
102
                } else \
103
                        sbappend((so), (m)); \
104
/*                sorwakeup(so); */ \
105
        } else { \
106
                (flags) = tcp_reass((tp), (ti), (m)); \
107
                tp->t_flags |= TF_ACKNOW; \
108
        } \
109
}
110
#endif
111
static void tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt,
112
                          struct tcpiphdr *ti);
113
static void tcp_xmit_timer(register struct tcpcb *tp, int rtt);
114

    
115
static int
116
tcp_reass(register struct tcpcb *tp, register struct tcpiphdr *ti,
117
          struct mbuf *m)
118
{
119
        register struct tcpiphdr *q;
120
        struct socket *so = tp->t_socket;
121
        int flags;
122

    
123
        /*
124
         * Call with ti==NULL after become established to
125
         * force pre-ESTABLISHED data up to user socket.
126
         */
127
        if (ti == NULL)
128
                goto present;
129

    
130
        /*
131
         * Find a segment which begins after this one does.
132
         */
133
        for (q = tcpfrag_list_first(tp); !tcpfrag_list_end(q, tp);
134
            q = tcpiphdr_next(q))
135
                if (SEQ_GT(q->ti_seq, ti->ti_seq))
136
                        break;
137

    
138
        /*
139
         * If there is a preceding segment, it may provide some of
140
         * our data already.  If so, drop the data from the incoming
141
         * segment.  If it provides all of our data, drop us.
142
         */
143
        if (!tcpfrag_list_end(tcpiphdr_prev(q), tp)) {
144
                register int i;
145
                q = tcpiphdr_prev(q);
146
                /* conversion to int (in i) handles seq wraparound */
147
                i = q->ti_seq + q->ti_len - ti->ti_seq;
148
                if (i > 0) {
149
                        if (i >= ti->ti_len) {
150
                                STAT(tcpstat.tcps_rcvduppack++);
151
                                STAT(tcpstat.tcps_rcvdupbyte += ti->ti_len);
152
                                m_freem(m);
153
                                /*
154
                                 * Try to present any queued data
155
                                 * at the left window edge to the user.
156
                                 * This is needed after the 3-WHS
157
                                 * completes.
158
                                 */
159
                                goto present;   /* ??? */
160
                        }
161
                        m_adj(m, i);
162
                        ti->ti_len -= i;
163
                        ti->ti_seq += i;
164
                }
165
                q = tcpiphdr_next(q);
166
        }
167
        STAT(tcpstat.tcps_rcvoopack++);
168
        STAT(tcpstat.tcps_rcvoobyte += ti->ti_len);
169
        ti->ti_mbuf = m;
170

    
171
        /*
172
         * While we overlap succeeding segments trim them or,
173
         * if they are completely covered, dequeue them.
174
         */
175
        while (!tcpfrag_list_end(q, tp)) {
176
                register int i = (ti->ti_seq + ti->ti_len) - q->ti_seq;
177
                if (i <= 0)
178
                        break;
179
                if (i < q->ti_len) {
180
                        q->ti_seq += i;
181
                        q->ti_len -= i;
182
                        m_adj(q->ti_mbuf, i);
183
                        break;
184
                }
185
                q = tcpiphdr_next(q);
186
                m = tcpiphdr_prev(q)->ti_mbuf;
187
                remque(tcpiphdr2qlink(tcpiphdr_prev(q)));
188
                m_freem(m);
189
        }
190

    
191
        /*
192
         * Stick new segment in its place.
193
         */
194
        insque(tcpiphdr2qlink(ti), tcpiphdr2qlink(tcpiphdr_prev(q)));
195

    
196
present:
197
        /*
198
         * Present data to user, advancing rcv_nxt through
199
         * completed sequence space.
200
         */
201
        if (!TCPS_HAVEESTABLISHED(tp->t_state))
202
                return (0);
203
        ti = tcpfrag_list_first(tp);
204
        if (tcpfrag_list_end(ti, tp) || ti->ti_seq != tp->rcv_nxt)
205
                return (0);
206
        if (tp->t_state == TCPS_SYN_RECEIVED && ti->ti_len)
207
                return (0);
208
        do {
209
                tp->rcv_nxt += ti->ti_len;
210
                flags = ti->ti_flags & TH_FIN;
211
                remque(tcpiphdr2qlink(ti));
212
                m = ti->ti_mbuf;
213
                ti = tcpiphdr_next(ti);
214
/*                if (so->so_state & SS_FCANTRCVMORE) */
215
                if (so->so_state & SS_FCANTSENDMORE)
216
                        m_freem(m);
217
                else {
218
                        if (so->so_emu) {
219
                                if (tcp_emu(so,m)) sbappend(so, m);
220
                        } else
221
                                sbappend(so, m);
222
                }
223
        } while (ti != (struct tcpiphdr *)tp && ti->ti_seq == tp->rcv_nxt);
224
/*        sorwakeup(so); */
225
        return (flags);
226
}
227

    
228
/*
229
 * TCP input routine, follows pages 65-76 of the
230
 * protocol specification dated September, 1981 very closely.
231
 */
232
void
233
tcp_input(struct mbuf *m, int iphlen, struct socket *inso)
234
{
235
          struct ip save_ip, *ip;
236
        register struct tcpiphdr *ti;
237
        caddr_t optp = NULL;
238
        int optlen = 0;
239
        int len, tlen, off;
240
        register struct tcpcb *tp = NULL;
241
        register int tiflags;
242
        struct socket *so = NULL;
243
        int todrop, acked, ourfinisacked, needoutput = 0;
244
/*        int dropsocket = 0; */
245
        int iss = 0;
246
        u_long tiwin;
247
        int ret;
248
/*        int ts_present = 0; */
249
    struct ex_list *ex_ptr;
250

    
251
        DEBUG_CALL("tcp_input");
252
        DEBUG_ARGS((dfd," m = %8lx  iphlen = %2d  inso = %lx\n",
253
                    (long )m, iphlen, (long )inso ));
254

    
255
        /*
256
         * If called with m == 0, then we're continuing the connect
257
         */
258
        if (m == NULL) {
259
                so = inso;
260

    
261
                /* Re-set a few variables */
262
                tp = sototcpcb(so);
263
                m = so->so_m;
264
                so->so_m = NULL;
265
                ti = so->so_ti;
266
                tiwin = ti->ti_win;
267
                tiflags = ti->ti_flags;
268

    
269
                goto cont_conn;
270
        }
271

    
272

    
273
        STAT(tcpstat.tcps_rcvtotal++);
274
        /*
275
         * Get IP and TCP header together in first mbuf.
276
         * Note: IP leaves IP header in first mbuf.
277
         */
278
        ti = mtod(m, struct tcpiphdr *);
279
        if (iphlen > sizeof(struct ip )) {
280
          ip_stripoptions(m, (struct mbuf *)0);
281
          iphlen=sizeof(struct ip );
282
        }
283
        /* XXX Check if too short */
284

    
285

    
286
        /*
287
         * Save a copy of the IP header in case we want restore it
288
         * for sending an ICMP error message in response.
289
         */
290
        ip=mtod(m, struct ip *);
291
        save_ip = *ip;
292
        save_ip.ip_len+= iphlen;
293

    
294
        /*
295
         * Checksum extended TCP header and data.
296
         */
297
        tlen = ((struct ip *)ti)->ip_len;
298
        tcpiphdr2qlink(ti)->next = tcpiphdr2qlink(ti)->prev = NULL;
299
        memset(&ti->ti_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr));
300
        ti->ti_x1 = 0;
301
        ti->ti_len = htons((u_int16_t)tlen);
302
        len = sizeof(struct ip ) + tlen;
303
        /* keep checksum for ICMP reply
304
         * ti->ti_sum = cksum(m, len);
305
         * if (ti->ti_sum) { */
306
        if(cksum(m, len)) {
307
          STAT(tcpstat.tcps_rcvbadsum++);
308
          goto drop;
309
        }
310

    
311
        /*
312
         * Check that TCP offset makes sense,
313
         * pull out TCP options and adjust length.                XXX
314
         */
315
        off = ti->ti_off << 2;
316
        if (off < sizeof (struct tcphdr) || off > tlen) {
317
          STAT(tcpstat.tcps_rcvbadoff++);
318
          goto drop;
319
        }
320
        tlen -= off;
321
        ti->ti_len = tlen;
322
        if (off > sizeof (struct tcphdr)) {
323
          optlen = off - sizeof (struct tcphdr);
324
          optp = mtod(m, caddr_t) + sizeof (struct tcpiphdr);
325

    
326
                /*
327
                 * Do quick retrieval of timestamp options ("options
328
                 * prediction?").  If timestamp is the only option and it's
329
                 * formatted as recommended in RFC 1323 appendix A, we
330
                 * quickly get the values now and not bother calling
331
                 * tcp_dooptions(), etc.
332
                 */
333
/*                if ((optlen == TCPOLEN_TSTAMP_APPA ||
334
 *                     (optlen > TCPOLEN_TSTAMP_APPA &&
335
 *                        optp[TCPOLEN_TSTAMP_APPA] == TCPOPT_EOL)) &&
336
 *                     *(u_int32_t *)optp == htonl(TCPOPT_TSTAMP_HDR) &&
337
 *                     (ti->ti_flags & TH_SYN) == 0) {
338
 *                        ts_present = 1;
339
 *                        ts_val = ntohl(*(u_int32_t *)(optp + 4));
340
 *                        ts_ecr = ntohl(*(u_int32_t *)(optp + 8));
341
 *                        optp = NULL;   / * we've parsed the options * /
342
 *                }
343
 */
344
        }
345
        tiflags = ti->ti_flags;
346

    
347
        /*
348
         * Convert TCP protocol specific fields to host format.
349
         */
350
        NTOHL(ti->ti_seq);
351
        NTOHL(ti->ti_ack);
352
        NTOHS(ti->ti_win);
353
        NTOHS(ti->ti_urp);
354

    
355
        /*
356
         * Drop TCP, IP headers and TCP options.
357
         */
358
        m->m_data += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
359
        m->m_len  -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
360

    
361
    if (slirp_restrict) {
362
        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
363
            if (ex_ptr->ex_fport == ti->ti_dport &&
364
                ti->ti_dst.s_addr == ex_ptr->ex_addr.s_addr) {
365
                break;
366
            }
367
        }
368
        if (!ex_ptr)
369
            goto drop;
370
    }
371
        /*
372
         * Locate pcb for segment.
373
         */
374
findso:
375
        so = tcp_last_so;
376
        if (so->so_fport != ti->ti_dport ||
377
            so->so_lport != ti->ti_sport ||
378
            so->so_laddr.s_addr != ti->ti_src.s_addr ||
379
            so->so_faddr.s_addr != ti->ti_dst.s_addr) {
380
                so = solookup(&tcb, ti->ti_src, ti->ti_sport,
381
                               ti->ti_dst, ti->ti_dport);
382
                if (so)
383
                        tcp_last_so = so;
384
                STAT(tcpstat.tcps_socachemiss++);
385
        }
386

    
387
        /*
388
         * If the state is CLOSED (i.e., TCB does not exist) then
389
         * all data in the incoming segment is discarded.
390
         * If the TCB exists but is in CLOSED state, it is embryonic,
391
         * but should either do a listen or a connect soon.
392
         *
393
         * state == CLOSED means we've done socreate() but haven't
394
         * attached it to a protocol yet...
395
         *
396
         * XXX If a TCB does not exist, and the TH_SYN flag is
397
         * the only flag set, then create a session, mark it
398
         * as if it was LISTENING, and continue...
399
         */
400
        if (so == NULL) {
401
          if ((tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) != TH_SYN)
402
            goto dropwithreset;
403

    
404
          if ((so = socreate()) == NULL)
405
            goto dropwithreset;
406
          if (tcp_attach(so) < 0) {
407
            free(so); /* Not sofree (if it failed, it's not insqued) */
408
            goto dropwithreset;
409
          }
410

    
411
          sbreserve(&so->so_snd, TCP_SNDSPACE);
412
          sbreserve(&so->so_rcv, TCP_RCVSPACE);
413

    
414
          /*                tcp_last_so = so; */  /* XXX ? */
415
          /*                tp = sototcpcb(so);    */
416

    
417
          so->so_laddr = ti->ti_src;
418
          so->so_lport = ti->ti_sport;
419
          so->so_faddr = ti->ti_dst;
420
          so->so_fport = ti->ti_dport;
421

    
422
          if ((so->so_iptos = tcp_tos(so)) == 0)
423
            so->so_iptos = ((struct ip *)ti)->ip_tos;
424

    
425
          tp = sototcpcb(so);
426
          tp->t_state = TCPS_LISTEN;
427
        }
428

    
429
        /*
430
         * If this is a still-connecting socket, this probably
431
         * a retransmit of the SYN.  Whether it's a retransmit SYN
432
         * or something else, we nuke it.
433
         */
434
        if (so->so_state & SS_ISFCONNECTING)
435
                goto drop;
436

    
437
        tp = sototcpcb(so);
438

    
439
        /* XXX Should never fail */
440
        if (tp == NULL)
441
                goto dropwithreset;
442
        if (tp->t_state == TCPS_CLOSED)
443
                goto drop;
444

    
445
        /* Unscale the window into a 32-bit value. */
446
/*        if ((tiflags & TH_SYN) == 0)
447
 *                tiwin = ti->ti_win << tp->snd_scale;
448
 *        else
449
 */
450
                tiwin = ti->ti_win;
451

    
452
        /*
453
         * Segment received on connection.
454
         * Reset idle time and keep-alive timer.
455
         */
456
        tp->t_idle = 0;
457
        if (SO_OPTIONS)
458
           tp->t_timer[TCPT_KEEP] = TCPTV_KEEPINTVL;
459
        else
460
           tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_IDLE;
461

    
462
        /*
463
         * Process options if not in LISTEN state,
464
         * else do it below (after getting remote address).
465
         */
466
        if (optp && tp->t_state != TCPS_LISTEN)
467
                tcp_dooptions(tp, (u_char *)optp, optlen, ti);
468
/* , */
469
/*                        &ts_present, &ts_val, &ts_ecr); */
470

    
471
        /*
472
         * Header prediction: check for the two common cases
473
         * of a uni-directional data xfer.  If the packet has
474
         * no control flags, is in-sequence, the window didn't
475
         * change and we're not retransmitting, it's a
476
         * candidate.  If the length is zero and the ack moved
477
         * forward, we're the sender side of the xfer.  Just
478
         * free the data acked & wake any higher level process
479
         * that was blocked waiting for space.  If the length
480
         * is non-zero and the ack didn't move, we're the
481
         * receiver side.  If we're getting packets in-order
482
         * (the reassembly queue is empty), add the data to
483
         * the socket buffer and note that we need a delayed ack.
484
         *
485
         * XXX Some of these tests are not needed
486
         * eg: the tiwin == tp->snd_wnd prevents many more
487
         * predictions.. with no *real* advantage..
488
         */
489
        if (tp->t_state == TCPS_ESTABLISHED &&
490
            (tiflags & (TH_SYN|TH_FIN|TH_RST|TH_URG|TH_ACK)) == TH_ACK &&
491
/*            (!ts_present || TSTMP_GEQ(ts_val, tp->ts_recent)) && */
492
            ti->ti_seq == tp->rcv_nxt &&
493
            tiwin && tiwin == tp->snd_wnd &&
494
            tp->snd_nxt == tp->snd_max) {
495
                /*
496
                 * If last ACK falls within this segment's sequence numbers,
497
                 *  record the timestamp.
498
                 */
499
/*                if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
500
 *                   SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len)) {
501
 *                        tp->ts_recent_age = tcp_now;
502
 *                        tp->ts_recent = ts_val;
503
 *                }
504
 */
505
                if (ti->ti_len == 0) {
506
                        if (SEQ_GT(ti->ti_ack, tp->snd_una) &&
507
                            SEQ_LEQ(ti->ti_ack, tp->snd_max) &&
508
                            tp->snd_cwnd >= tp->snd_wnd) {
509
                                /*
510
                                 * this is a pure ack for outstanding data.
511
                                 */
512
                                STAT(tcpstat.tcps_predack++);
513
/*                                if (ts_present)
514
 *                                        tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
515
 *                                else
516
 */                                     if (tp->t_rtt &&
517
                                            SEQ_GT(ti->ti_ack, tp->t_rtseq))
518
                                        tcp_xmit_timer(tp, tp->t_rtt);
519
                                acked = ti->ti_ack - tp->snd_una;
520
                                STAT(tcpstat.tcps_rcvackpack++);
521
                                STAT(tcpstat.tcps_rcvackbyte += acked);
522
                                sbdrop(&so->so_snd, acked);
523
                                tp->snd_una = ti->ti_ack;
524
                                m_freem(m);
525

    
526
                                /*
527
                                 * If all outstanding data are acked, stop
528
                                 * retransmit timer, otherwise restart timer
529
                                 * using current (possibly backed-off) value.
530
                                 * If process is waiting for space,
531
                                 * wakeup/selwakeup/signal.  If data
532
                                 * are ready to send, let tcp_output
533
                                 * decide between more output or persist.
534
                                 */
535
                                if (tp->snd_una == tp->snd_max)
536
                                        tp->t_timer[TCPT_REXMT] = 0;
537
                                else if (tp->t_timer[TCPT_PERSIST] == 0)
538
                                        tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
539

    
540
                                /*
541
                                 * There's room in so_snd, sowwakup will read()
542
                                 * from the socket if we can
543
                                 */
544
/*                                if (so->so_snd.sb_flags & SB_NOTIFY)
545
 *                                        sowwakeup(so);
546
 */
547
                                /*
548
                                 * This is called because sowwakeup might have
549
                                 * put data into so_snd.  Since we don't so sowwakeup,
550
                                 * we don't need this.. XXX???
551
                                 */
552
                                if (so->so_snd.sb_cc)
553
                                        (void) tcp_output(tp);
554

    
555
                                return;
556
                        }
557
                } else if (ti->ti_ack == tp->snd_una &&
558
                    tcpfrag_list_empty(tp) &&
559
                    ti->ti_len <= sbspace(&so->so_rcv)) {
560
                        /*
561
                         * this is a pure, in-sequence data packet
562
                         * with nothing on the reassembly queue and
563
                         * we have enough buffer space to take it.
564
                         */
565
                        STAT(tcpstat.tcps_preddat++);
566
                        tp->rcv_nxt += ti->ti_len;
567
                        STAT(tcpstat.tcps_rcvpack++);
568
                        STAT(tcpstat.tcps_rcvbyte += ti->ti_len);
569
                        /*
570
                         * Add data to socket buffer.
571
                         */
572
                        if (so->so_emu) {
573
                                if (tcp_emu(so,m)) sbappend(so, m);
574
                        } else
575
                                sbappend(so, m);
576

    
577
                        /*
578
                         * XXX This is called when data arrives.  Later, check
579
                         * if we can actually write() to the socket
580
                         * XXX Need to check? It's be NON_BLOCKING
581
                         */
582
/*                        sorwakeup(so); */
583

    
584
                        /*
585
                         * If this is a short packet, then ACK now - with Nagel
586
                         *        congestion avoidance sender won't send more until
587
                         *        he gets an ACK.
588
                         *
589
                         * It is better to not delay acks at all to maximize
590
                         * TCP throughput.  See RFC 2581.
591
                         */
592
                        tp->t_flags |= TF_ACKNOW;
593
                        tcp_output(tp);
594
                        return;
595
                }
596
        } /* header prediction */
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
          win = sbspace(&so->so_rcv);
605
          if (win < 0)
606
            win = 0;
607
          tp->rcv_wnd = max(win, (int)(tp->rcv_adv - tp->rcv_nxt));
608
        }
609

    
610
        switch (tp->t_state) {
611

    
612
        /*
613
         * If the state is LISTEN then ignore segment if it contains an RST.
614
         * If the segment contains an ACK then it is bad and send a RST.
615
         * If it does not contain a SYN then it is not interesting; drop it.
616
         * Don't bother responding if the destination was a broadcast.
617
         * Otherwise initialize tp->rcv_nxt, and tp->irs, select an initial
618
         * tp->iss, and send a segment:
619
         *     <SEQ=ISS><ACK=RCV_NXT><CTL=SYN,ACK>
620
         * Also initialize tp->snd_nxt to tp->iss+1 and tp->snd_una to tp->iss.
621
         * Fill in remote peer address fields if not previously specified.
622
         * Enter SYN_RECEIVED state, and process any other fields of this
623
         * segment in this state.
624
         */
625
        case TCPS_LISTEN: {
626

    
627
          if (tiflags & TH_RST)
628
            goto drop;
629
          if (tiflags & TH_ACK)
630
            goto dropwithreset;
631
          if ((tiflags & TH_SYN) == 0)
632
            goto drop;
633

    
634
          /*
635
           * This has way too many gotos...
636
           * But a bit of spaghetti code never hurt anybody :)
637
           */
638

    
639
          /*
640
           * If this is destined for the control address, then flag to
641
           * tcp_ctl once connected, otherwise connect
642
           */
643
          if ((so->so_faddr.s_addr & vnetwork_mask.s_addr) ==
644
              vnetwork_addr.s_addr) {
645
            if (so->so_faddr.s_addr != vhost_addr.s_addr &&
646
                so->so_faddr.s_addr != vnameserver_addr.s_addr) {
647
#if 0
648
              if(lastbyte==CTL_CMD || lastbyte==CTL_EXEC) {
649
                /* Command or exec adress */
650
                so->so_state |= SS_CTL;
651
              } else
652
#endif
653
              {
654
                /* May be an add exec */
655
                for(ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next) {
656
                  if(ex_ptr->ex_fport == so->so_fport &&
657
                     so->so_faddr.s_addr == ex_ptr->ex_addr.s_addr) {
658
                    so->so_state |= SS_CTL;
659
                    break;
660
                  }
661
                }
662
              }
663
              if(so->so_state & SS_CTL) goto cont_input;
664
            }
665
            /* CTL_ALIAS: Do nothing, tcp_fconnect will be called on it */
666
          }
667

    
668
          if (so->so_emu & EMU_NOCONNECT) {
669
            so->so_emu &= ~EMU_NOCONNECT;
670
            goto cont_input;
671
          }
672

    
673
          if((tcp_fconnect(so) == -1) && (errno != EINPROGRESS) && (errno != EWOULDBLOCK)) {
674
            u_char code=ICMP_UNREACH_NET;
675
            DEBUG_MISC((dfd," tcp fconnect errno = %d-%s\n",
676
                        errno,strerror(errno)));
677
            if(errno == ECONNREFUSED) {
678
              /* ACK the SYN, send RST to refuse the connection */
679
              tcp_respond(tp, ti, m, ti->ti_seq+1, (tcp_seq)0,
680
                          TH_RST|TH_ACK);
681
            } else {
682
              if(errno == EHOSTUNREACH) code=ICMP_UNREACH_HOST;
683
              HTONL(ti->ti_seq);             /* restore tcp header */
684
              HTONL(ti->ti_ack);
685
              HTONS(ti->ti_win);
686
              HTONS(ti->ti_urp);
687
              m->m_data -= sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
688
              m->m_len  += sizeof(struct tcpiphdr)+off-sizeof(struct tcphdr);
689
              *ip=save_ip;
690
              icmp_error(m, ICMP_UNREACH,code, 0,strerror(errno));
691
            }
692
            tp = tcp_close(tp);
693
            m_free(m);
694
          } else {
695
            /*
696
             * Haven't connected yet, save the current mbuf
697
             * and ti, and return
698
             * XXX Some OS's don't tell us whether the connect()
699
             * succeeded or not.  So we must time it out.
700
             */
701
            so->so_m = m;
702
            so->so_ti = ti;
703
            tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
704
            tp->t_state = TCPS_SYN_RECEIVED;
705
          }
706
          return;
707

    
708
        cont_conn:
709
          /* m==NULL
710
           * Check if the connect succeeded
711
           */
712
          if (so->so_state & SS_NOFDREF) {
713
            tp = tcp_close(tp);
714
            goto dropwithreset;
715
          }
716
        cont_input:
717
          tcp_template(tp);
718

    
719
          if (optp)
720
            tcp_dooptions(tp, (u_char *)optp, optlen, ti);
721
          /* , */
722
          /*                                &ts_present, &ts_val, &ts_ecr); */
723

    
724
          if (iss)
725
            tp->iss = iss;
726
          else
727
            tp->iss = tcp_iss;
728
          tcp_iss += TCP_ISSINCR/2;
729
          tp->irs = ti->ti_seq;
730
          tcp_sendseqinit(tp);
731
          tcp_rcvseqinit(tp);
732
          tp->t_flags |= TF_ACKNOW;
733
          tp->t_state = TCPS_SYN_RECEIVED;
734
          tp->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT;
735
          STAT(tcpstat.tcps_accepts++);
736
          goto trimthenstep6;
737
        } /* case TCPS_LISTEN */
738

    
739
        /*
740
         * If the state is SYN_SENT:
741
         *        if seg contains an ACK, but not for our SYN, drop the input.
742
         *        if seg contains a RST, then drop the connection.
743
         *        if seg does not contain SYN, then drop it.
744
         * Otherwise this is an acceptable SYN segment
745
         *        initialize tp->rcv_nxt and tp->irs
746
         *        if seg contains ack then advance tp->snd_una
747
         *        if SYN has been acked change to ESTABLISHED else SYN_RCVD state
748
         *        arrange for segment to be acked (eventually)
749
         *        continue processing rest of data/controls, beginning with URG
750
         */
751
        case TCPS_SYN_SENT:
752
                if ((tiflags & TH_ACK) &&
753
                    (SEQ_LEQ(ti->ti_ack, tp->iss) ||
754
                     SEQ_GT(ti->ti_ack, tp->snd_max)))
755
                        goto dropwithreset;
756

    
757
                if (tiflags & TH_RST) {
758
                        if (tiflags & TH_ACK)
759
                                tp = tcp_drop(tp,0); /* XXX Check t_softerror! */
760
                        goto drop;
761
                }
762

    
763
                if ((tiflags & TH_SYN) == 0)
764
                        goto drop;
765
                if (tiflags & TH_ACK) {
766
                        tp->snd_una = ti->ti_ack;
767
                        if (SEQ_LT(tp->snd_nxt, tp->snd_una))
768
                                tp->snd_nxt = tp->snd_una;
769
                }
770

    
771
                tp->t_timer[TCPT_REXMT] = 0;
772
                tp->irs = ti->ti_seq;
773
                tcp_rcvseqinit(tp);
774
                tp->t_flags |= TF_ACKNOW;
775
                if (tiflags & TH_ACK && SEQ_GT(tp->snd_una, tp->iss)) {
776
                        STAT(tcpstat.tcps_connects++);
777
                        soisfconnected(so);
778
                        tp->t_state = TCPS_ESTABLISHED;
779

    
780
                        /* Do window scaling on this connection? */
781
/*                        if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
782
 *                                (TF_RCVD_SCALE|TF_REQ_SCALE)) {
783
 *                                 tp->snd_scale = tp->requested_s_scale;
784
 *                                tp->rcv_scale = tp->request_r_scale;
785
 *                        }
786
 */
787
                        (void) tcp_reass(tp, (struct tcpiphdr *)0,
788
                                (struct mbuf *)0);
789
                        /*
790
                         * if we didn't have to retransmit the SYN,
791
                         * use its rtt as our initial srtt & rtt var.
792
                         */
793
                        if (tp->t_rtt)
794
                                tcp_xmit_timer(tp, tp->t_rtt);
795
                } else
796
                        tp->t_state = TCPS_SYN_RECEIVED;
797

    
798
trimthenstep6:
799
                /*
800
                 * Advance ti->ti_seq to correspond to first data byte.
801
                 * If data, trim to stay within window,
802
                 * dropping FIN if necessary.
803
                 */
804
                ti->ti_seq++;
805
                if (ti->ti_len > tp->rcv_wnd) {
806
                        todrop = ti->ti_len - tp->rcv_wnd;
807
                        m_adj(m, -todrop);
808
                        ti->ti_len = tp->rcv_wnd;
809
                        tiflags &= ~TH_FIN;
810
                        STAT(tcpstat.tcps_rcvpackafterwin++);
811
                        STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
812
                }
813
                tp->snd_wl1 = ti->ti_seq - 1;
814
                tp->rcv_up = ti->ti_seq;
815
                goto step6;
816
        } /* switch tp->t_state */
817
        /*
818
         * States other than LISTEN or SYN_SENT.
819
         * First check timestamp, if present.
820
         * Then check that at least some bytes of segment are within
821
         * receive window.  If segment begins before rcv_nxt,
822
         * drop leading data (and SYN); if nothing left, just ack.
823
         *
824
         * RFC 1323 PAWS: If we have a timestamp reply on this segment
825
         * and it's less than ts_recent, drop it.
826
         */
827
/*        if (ts_present && (tiflags & TH_RST) == 0 && tp->ts_recent &&
828
 *            TSTMP_LT(ts_val, tp->ts_recent)) {
829
 *
830
 */                /* Check to see if ts_recent is over 24 days old.  */
831
/*                if ((int)(tcp_now - tp->ts_recent_age) > TCP_PAWS_IDLE) {
832
 */                        /*
833
 *                         * Invalidate ts_recent.  If this segment updates
834
 *                         * ts_recent, the age will be reset later and ts_recent
835
 *                         * will get a valid value.  If it does not, setting
836
 *                         * ts_recent to zero will at least satisfy the
837
 *                         * requirement that zero be placed in the timestamp
838
 *                         * echo reply when ts_recent isn't valid.  The
839
 *                         * age isn't reset until we get a valid ts_recent
840
 *                         * because we don't want out-of-order segments to be
841
 *                         * dropped when ts_recent is old.
842
 *                         */
843
/*                        tp->ts_recent = 0;
844
 *                } else {
845
 *                        tcpstat.tcps_rcvduppack++;
846
 *                        tcpstat.tcps_rcvdupbyte += ti->ti_len;
847
 *                        tcpstat.tcps_pawsdrop++;
848
 *                        goto dropafterack;
849
 *                }
850
 *        }
851
 */
852

    
853
        todrop = tp->rcv_nxt - ti->ti_seq;
854
        if (todrop > 0) {
855
                if (tiflags & TH_SYN) {
856
                        tiflags &= ~TH_SYN;
857
                        ti->ti_seq++;
858
                        if (ti->ti_urp > 1)
859
                                ti->ti_urp--;
860
                        else
861
                                tiflags &= ~TH_URG;
862
                        todrop--;
863
                }
864
                /*
865
                 * Following if statement from Stevens, vol. 2, p. 960.
866
                 */
867
                if (todrop > ti->ti_len
868
                    || (todrop == ti->ti_len && (tiflags & TH_FIN) == 0)) {
869
                        /*
870
                         * Any valid FIN must be to the left of the window.
871
                         * At this point the FIN must be a duplicate or out
872
                         * of sequence; drop it.
873
                         */
874
                        tiflags &= ~TH_FIN;
875

    
876
                        /*
877
                         * Send an ACK to resynchronize and drop any data.
878
                         * But keep on processing for RST or ACK.
879
                         */
880
                        tp->t_flags |= TF_ACKNOW;
881
                        todrop = ti->ti_len;
882
                        STAT(tcpstat.tcps_rcvduppack++);
883
                        STAT(tcpstat.tcps_rcvdupbyte += todrop);
884
                } else {
885
                        STAT(tcpstat.tcps_rcvpartduppack++);
886
                        STAT(tcpstat.tcps_rcvpartdupbyte += todrop);
887
                }
888
                m_adj(m, todrop);
889
                ti->ti_seq += todrop;
890
                ti->ti_len -= todrop;
891
                if (ti->ti_urp > todrop)
892
                        ti->ti_urp -= todrop;
893
                else {
894
                        tiflags &= ~TH_URG;
895
                        ti->ti_urp = 0;
896
                }
897
        }
898
        /*
899
         * If new data are received on a connection after the
900
         * user processes are gone, then RST the other end.
901
         */
902
        if ((so->so_state & SS_NOFDREF) &&
903
            tp->t_state > TCPS_CLOSE_WAIT && ti->ti_len) {
904
                tp = tcp_close(tp);
905
                STAT(tcpstat.tcps_rcvafterclose++);
906
                goto dropwithreset;
907
        }
908

    
909
        /*
910
         * If segment ends after window, drop trailing data
911
         * (and PUSH and FIN); if nothing left, just ACK.
912
         */
913
        todrop = (ti->ti_seq+ti->ti_len) - (tp->rcv_nxt+tp->rcv_wnd);
914
        if (todrop > 0) {
915
                STAT(tcpstat.tcps_rcvpackafterwin++);
916
                if (todrop >= ti->ti_len) {
917
                        STAT(tcpstat.tcps_rcvbyteafterwin += ti->ti_len);
918
                        /*
919
                         * If a new connection request is received
920
                         * while in TIME_WAIT, drop the old connection
921
                         * and start over if the sequence numbers
922
                         * are above the previous ones.
923
                         */
924
                        if (tiflags & TH_SYN &&
925
                            tp->t_state == TCPS_TIME_WAIT &&
926
                            SEQ_GT(ti->ti_seq, tp->rcv_nxt)) {
927
                                iss = tp->rcv_nxt + TCP_ISSINCR;
928
                                tp = tcp_close(tp);
929
                                goto findso;
930
                        }
931
                        /*
932
                         * If window is closed can only take segments at
933
                         * window edge, and have to drop data and PUSH from
934
                         * incoming segments.  Continue processing, but
935
                         * remember to ack.  Otherwise, drop segment
936
                         * and ack.
937
                         */
938
                        if (tp->rcv_wnd == 0 && ti->ti_seq == tp->rcv_nxt) {
939
                                tp->t_flags |= TF_ACKNOW;
940
                                STAT(tcpstat.tcps_rcvwinprobe++);
941
                        } else
942
                                goto dropafterack;
943
                } else
944
                        STAT(tcpstat.tcps_rcvbyteafterwin += todrop);
945
                m_adj(m, -todrop);
946
                ti->ti_len -= todrop;
947
                tiflags &= ~(TH_PUSH|TH_FIN);
948
        }
949

    
950
        /*
951
         * If last ACK falls within this segment's sequence numbers,
952
         * record its timestamp.
953
         */
954
/*        if (ts_present && SEQ_LEQ(ti->ti_seq, tp->last_ack_sent) &&
955
 *            SEQ_LT(tp->last_ack_sent, ti->ti_seq + ti->ti_len +
956
 *                   ((tiflags & (TH_SYN|TH_FIN)) != 0))) {
957
 *                tp->ts_recent_age = tcp_now;
958
 *                tp->ts_recent = ts_val;
959
 *        }
960
 */
961

    
962
        /*
963
         * If the RST bit is set examine the state:
964
         *    SYN_RECEIVED STATE:
965
         *        If passive open, return to LISTEN state.
966
         *        If active open, inform user that connection was refused.
967
         *    ESTABLISHED, FIN_WAIT_1, FIN_WAIT2, CLOSE_WAIT STATES:
968
         *        Inform user that connection was reset, and close tcb.
969
         *    CLOSING, LAST_ACK, TIME_WAIT STATES
970
         *        Close the tcb.
971
         */
972
        if (tiflags&TH_RST) switch (tp->t_state) {
973

    
974
        case TCPS_SYN_RECEIVED:
975
/*                so->so_error = ECONNREFUSED; */
976
                goto close;
977

    
978
        case TCPS_ESTABLISHED:
979
        case TCPS_FIN_WAIT_1:
980
        case TCPS_FIN_WAIT_2:
981
        case TCPS_CLOSE_WAIT:
982
/*                so->so_error = ECONNRESET; */
983
        close:
984
                tp->t_state = TCPS_CLOSED;
985
                STAT(tcpstat.tcps_drops++);
986
                tp = tcp_close(tp);
987
                goto drop;
988

    
989
        case TCPS_CLOSING:
990
        case TCPS_LAST_ACK:
991
        case TCPS_TIME_WAIT:
992
                tp = tcp_close(tp);
993
                goto drop;
994
        }
995

    
996
        /*
997
         * If a SYN is in the window, then this is an
998
         * error and we send an RST and drop the connection.
999
         */
1000
        if (tiflags & TH_SYN) {
1001
                tp = tcp_drop(tp,0);
1002
                goto dropwithreset;
1003
        }
1004

    
1005
        /*
1006
         * If the ACK bit is off we drop the segment and return.
1007
         */
1008
        if ((tiflags & TH_ACK) == 0) goto drop;
1009

    
1010
        /*
1011
         * Ack processing.
1012
         */
1013
        switch (tp->t_state) {
1014
        /*
1015
         * In SYN_RECEIVED state if the ack ACKs our SYN then enter
1016
         * ESTABLISHED state and continue processing, otherwise
1017
         * send an RST.  una<=ack<=max
1018
         */
1019
        case TCPS_SYN_RECEIVED:
1020

    
1021
                if (SEQ_GT(tp->snd_una, ti->ti_ack) ||
1022
                    SEQ_GT(ti->ti_ack, tp->snd_max))
1023
                        goto dropwithreset;
1024
                STAT(tcpstat.tcps_connects++);
1025
                tp->t_state = TCPS_ESTABLISHED;
1026
                /*
1027
                 * The sent SYN is ack'ed with our sequence number +1
1028
                 * The first data byte already in the buffer will get
1029
                 * lost if no correction is made.  This is only needed for
1030
                 * SS_CTL since the buffer is empty otherwise.
1031
                 * tp->snd_una++; or:
1032
                 */
1033
                tp->snd_una=ti->ti_ack;
1034
                if (so->so_state & SS_CTL) {
1035
                  /* So tcp_ctl reports the right state */
1036
                  ret = tcp_ctl(so);
1037
                  if (ret == 1) {
1038
                    soisfconnected(so);
1039
                    so->so_state &= ~SS_CTL;   /* success XXX */
1040
                  } else if (ret == 2) {
1041
                    so->so_state &= SS_PERSISTENT_MASK;
1042
                    so->so_state |= SS_NOFDREF; /* CTL_CMD */
1043
                  } else {
1044
                    needoutput = 1;
1045
                    tp->t_state = TCPS_FIN_WAIT_1;
1046
                  }
1047
                } else {
1048
                  soisfconnected(so);
1049
                }
1050

    
1051
                /* Do window scaling? */
1052
/*                if ((tp->t_flags & (TF_RCVD_SCALE|TF_REQ_SCALE)) ==
1053
 *                        (TF_RCVD_SCALE|TF_REQ_SCALE)) {
1054
 *                        tp->snd_scale = tp->requested_s_scale;
1055
 *                        tp->rcv_scale = tp->request_r_scale;
1056
 *                }
1057
 */
1058
                (void) tcp_reass(tp, (struct tcpiphdr *)0, (struct mbuf *)0);
1059
                tp->snd_wl1 = ti->ti_seq - 1;
1060
                /* Avoid ack processing; snd_una==ti_ack  =>  dup ack */
1061
                goto synrx_to_est;
1062
                /* fall into ... */
1063

    
1064
        /*
1065
         * In ESTABLISHED state: drop duplicate ACKs; ACK out of range
1066
         * ACKs.  If the ack is in the range
1067
         *        tp->snd_una < ti->ti_ack <= tp->snd_max
1068
         * then advance tp->snd_una to ti->ti_ack and drop
1069
         * data from the retransmission queue.  If this ACK reflects
1070
         * more up to date window information we update our window information.
1071
         */
1072
        case TCPS_ESTABLISHED:
1073
        case TCPS_FIN_WAIT_1:
1074
        case TCPS_FIN_WAIT_2:
1075
        case TCPS_CLOSE_WAIT:
1076
        case TCPS_CLOSING:
1077
        case TCPS_LAST_ACK:
1078
        case TCPS_TIME_WAIT:
1079

    
1080
                if (SEQ_LEQ(ti->ti_ack, tp->snd_una)) {
1081
                        if (ti->ti_len == 0 && tiwin == tp->snd_wnd) {
1082
                          STAT(tcpstat.tcps_rcvdupack++);
1083
                          DEBUG_MISC((dfd," dup ack  m = %lx  so = %lx \n",
1084
                                      (long )m, (long )so));
1085
                                /*
1086
                                 * If we have outstanding data (other than
1087
                                 * a window probe), this is a completely
1088
                                 * duplicate ack (ie, window info didn't
1089
                                 * change), the ack is the biggest we've
1090
                                 * seen and we've seen exactly our rexmt
1091
                                 * threshold of them, assume a packet
1092
                                 * has been dropped and retransmit it.
1093
                                 * Kludge snd_nxt & the congestion
1094
                                 * window so we send only this one
1095
                                 * packet.
1096
                                 *
1097
                                 * We know we're losing at the current
1098
                                 * window size so do congestion avoidance
1099
                                 * (set ssthresh to half the current window
1100
                                 * and pull our congestion window back to
1101
                                 * the new ssthresh).
1102
                                 *
1103
                                 * Dup acks mean that packets have left the
1104
                                 * network (they're now cached at the receiver)
1105
                                 * so bump cwnd by the amount in the receiver
1106
                                 * to keep a constant cwnd packets in the
1107
                                 * network.
1108
                                 */
1109
                                if (tp->t_timer[TCPT_REXMT] == 0 ||
1110
                                    ti->ti_ack != tp->snd_una)
1111
                                        tp->t_dupacks = 0;
1112
                                else if (++tp->t_dupacks == TCPREXMTTHRESH) {
1113
                                        tcp_seq onxt = tp->snd_nxt;
1114
                                        u_int win =
1115
                                            min(tp->snd_wnd, tp->snd_cwnd) / 2 /
1116
                                                tp->t_maxseg;
1117

    
1118
                                        if (win < 2)
1119
                                                win = 2;
1120
                                        tp->snd_ssthresh = win * tp->t_maxseg;
1121
                                        tp->t_timer[TCPT_REXMT] = 0;
1122
                                        tp->t_rtt = 0;
1123
                                        tp->snd_nxt = ti->ti_ack;
1124
                                        tp->snd_cwnd = tp->t_maxseg;
1125
                                        (void) tcp_output(tp);
1126
                                        tp->snd_cwnd = tp->snd_ssthresh +
1127
                                               tp->t_maxseg * tp->t_dupacks;
1128
                                        if (SEQ_GT(onxt, tp->snd_nxt))
1129
                                                tp->snd_nxt = onxt;
1130
                                        goto drop;
1131
                                } else if (tp->t_dupacks > TCPREXMTTHRESH) {
1132
                                        tp->snd_cwnd += tp->t_maxseg;
1133
                                        (void) tcp_output(tp);
1134
                                        goto drop;
1135
                                }
1136
                        } else
1137
                                tp->t_dupacks = 0;
1138
                        break;
1139
                }
1140
        synrx_to_est:
1141
                /*
1142
                 * If the congestion window was inflated to account
1143
                 * for the other side's cached packets, retract it.
1144
                 */
1145
                if (tp->t_dupacks > TCPREXMTTHRESH &&
1146
                    tp->snd_cwnd > tp->snd_ssthresh)
1147
                        tp->snd_cwnd = tp->snd_ssthresh;
1148
                tp->t_dupacks = 0;
1149
                if (SEQ_GT(ti->ti_ack, tp->snd_max)) {
1150
                        STAT(tcpstat.tcps_rcvacktoomuch++);
1151
                        goto dropafterack;
1152
                }
1153
                acked = ti->ti_ack - tp->snd_una;
1154
                STAT(tcpstat.tcps_rcvackpack++);
1155
                STAT(tcpstat.tcps_rcvackbyte += acked);
1156

    
1157
                /*
1158
                 * If we have a timestamp reply, update smoothed
1159
                 * round trip time.  If no timestamp is present but
1160
                 * transmit timer is running and timed sequence
1161
                 * number was acked, update smoothed round trip time.
1162
                 * Since we now have an rtt measurement, cancel the
1163
                 * timer backoff (cf., Phil Karn's retransmit alg.).
1164
                 * Recompute the initial retransmit timer.
1165
                 */
1166
/*                if (ts_present)
1167
 *                        tcp_xmit_timer(tp, tcp_now-ts_ecr+1);
1168
 *                else
1169
 */
1170
                     if (tp->t_rtt && SEQ_GT(ti->ti_ack, tp->t_rtseq))
1171
                        tcp_xmit_timer(tp,tp->t_rtt);
1172

    
1173
                /*
1174
                 * If all outstanding data is acked, stop retransmit
1175
                 * timer and remember to restart (more output or persist).
1176
                 * If there is more data to be acked, restart retransmit
1177
                 * timer, using current (possibly backed-off) value.
1178
                 */
1179
                if (ti->ti_ack == tp->snd_max) {
1180
                        tp->t_timer[TCPT_REXMT] = 0;
1181
                        needoutput = 1;
1182
                } else if (tp->t_timer[TCPT_PERSIST] == 0)
1183
                        tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1184
                /*
1185
                 * When new data is acked, open the congestion window.
1186
                 * If the window gives us less than ssthresh packets
1187
                 * in flight, open exponentially (maxseg per packet).
1188
                 * Otherwise open linearly: maxseg per window
1189
                 * (maxseg^2 / cwnd per packet).
1190
                 */
1191
                {
1192
                  register u_int cw = tp->snd_cwnd;
1193
                  register u_int incr = tp->t_maxseg;
1194

    
1195
                  if (cw > tp->snd_ssthresh)
1196
                    incr = incr * incr / cw;
1197
                  tp->snd_cwnd = min(cw + incr, TCP_MAXWIN<<tp->snd_scale);
1198
                }
1199
                if (acked > so->so_snd.sb_cc) {
1200
                        tp->snd_wnd -= so->so_snd.sb_cc;
1201
                        sbdrop(&so->so_snd, (int )so->so_snd.sb_cc);
1202
                        ourfinisacked = 1;
1203
                } else {
1204
                        sbdrop(&so->so_snd, acked);
1205
                        tp->snd_wnd -= acked;
1206
                        ourfinisacked = 0;
1207
                }
1208
                /*
1209
                 * XXX sowwakup is called when data is acked and there's room for
1210
                 * for more data... it should read() the socket
1211
                 */
1212
/*                if (so->so_snd.sb_flags & SB_NOTIFY)
1213
 *                        sowwakeup(so);
1214
 */
1215
                tp->snd_una = ti->ti_ack;
1216
                if (SEQ_LT(tp->snd_nxt, tp->snd_una))
1217
                        tp->snd_nxt = tp->snd_una;
1218

    
1219
                switch (tp->t_state) {
1220

    
1221
                /*
1222
                 * In FIN_WAIT_1 STATE in addition to the processing
1223
                 * for the ESTABLISHED state if our FIN is now acknowledged
1224
                 * then enter FIN_WAIT_2.
1225
                 */
1226
                case TCPS_FIN_WAIT_1:
1227
                        if (ourfinisacked) {
1228
                                /*
1229
                                 * If we can't receive any more
1230
                                 * data, then closing user can proceed.
1231
                                 * Starting the timer is contrary to the
1232
                                 * specification, but if we don't get a FIN
1233
                                 * we'll hang forever.
1234
                                 */
1235
                                if (so->so_state & SS_FCANTRCVMORE) {
1236
                                        soisfdisconnected(so);
1237
                                        tp->t_timer[TCPT_2MSL] = TCP_MAXIDLE;
1238
                                }
1239
                                tp->t_state = TCPS_FIN_WAIT_2;
1240
                        }
1241
                        break;
1242

    
1243
                 /*
1244
                 * In CLOSING STATE in addition to the processing for
1245
                 * the ESTABLISHED state if the ACK acknowledges our FIN
1246
                 * then enter the TIME-WAIT state, otherwise ignore
1247
                 * the segment.
1248
                 */
1249
                case TCPS_CLOSING:
1250
                        if (ourfinisacked) {
1251
                                tp->t_state = TCPS_TIME_WAIT;
1252
                                tcp_canceltimers(tp);
1253
                                tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1254
                                soisfdisconnected(so);
1255
                        }
1256
                        break;
1257

    
1258
                /*
1259
                 * In LAST_ACK, we may still be waiting for data to drain
1260
                 * and/or to be acked, as well as for the ack of our FIN.
1261
                 * If our FIN is now acknowledged, delete the TCB,
1262
                 * enter the closed state and return.
1263
                 */
1264
                case TCPS_LAST_ACK:
1265
                        if (ourfinisacked) {
1266
                                tp = tcp_close(tp);
1267
                                goto drop;
1268
                        }
1269
                        break;
1270

    
1271
                /*
1272
                 * In TIME_WAIT state the only thing that should arrive
1273
                 * is a retransmission of the remote FIN.  Acknowledge
1274
                 * it and restart the finack timer.
1275
                 */
1276
                case TCPS_TIME_WAIT:
1277
                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1278
                        goto dropafterack;
1279
                }
1280
        } /* switch(tp->t_state) */
1281

    
1282
step6:
1283
        /*
1284
         * Update window information.
1285
         * Don't look at window if no ACK: TAC's send garbage on first SYN.
1286
         */
1287
        if ((tiflags & TH_ACK) &&
1288
            (SEQ_LT(tp->snd_wl1, ti->ti_seq) ||
1289
            (tp->snd_wl1 == ti->ti_seq && (SEQ_LT(tp->snd_wl2, ti->ti_ack) ||
1290
            (tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd))))) {
1291
                /* keep track of pure window updates */
1292
                if (ti->ti_len == 0 &&
1293
                    tp->snd_wl2 == ti->ti_ack && tiwin > tp->snd_wnd)
1294
                        STAT(tcpstat.tcps_rcvwinupd++);
1295
                tp->snd_wnd = tiwin;
1296
                tp->snd_wl1 = ti->ti_seq;
1297
                tp->snd_wl2 = ti->ti_ack;
1298
                if (tp->snd_wnd > tp->max_sndwnd)
1299
                        tp->max_sndwnd = tp->snd_wnd;
1300
                needoutput = 1;
1301
        }
1302

    
1303
        /*
1304
         * Process segments with URG.
1305
         */
1306
        if ((tiflags & TH_URG) && ti->ti_urp &&
1307
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1308
                /*
1309
                 * This is a kludge, but if we receive and accept
1310
                 * random urgent pointers, we'll crash in
1311
                 * soreceive.  It's hard to imagine someone
1312
                 * actually wanting to send this much urgent data.
1313
                 */
1314
                if (ti->ti_urp + so->so_rcv.sb_cc > so->so_rcv.sb_datalen) {
1315
                        ti->ti_urp = 0;
1316
                        tiflags &= ~TH_URG;
1317
                        goto dodata;
1318
                }
1319
                /*
1320
                 * If this segment advances the known urgent pointer,
1321
                 * then mark the data stream.  This should not happen
1322
                 * in CLOSE_WAIT, CLOSING, LAST_ACK or TIME_WAIT STATES since
1323
                 * a FIN has been received from the remote side.
1324
                 * In these states we ignore the URG.
1325
                 *
1326
                 * According to RFC961 (Assigned Protocols),
1327
                 * the urgent pointer points to the last octet
1328
                 * of urgent data.  We continue, however,
1329
                 * to consider it to indicate the first octet
1330
                 * of data past the urgent section as the original
1331
                 * spec states (in one of two places).
1332
                 */
1333
                if (SEQ_GT(ti->ti_seq+ti->ti_urp, tp->rcv_up)) {
1334
                        tp->rcv_up = ti->ti_seq + ti->ti_urp;
1335
                        so->so_urgc =  so->so_rcv.sb_cc +
1336
                                (tp->rcv_up - tp->rcv_nxt); /* -1; */
1337
                        tp->rcv_up = ti->ti_seq + ti->ti_urp;
1338

    
1339
                }
1340
        } else
1341
                /*
1342
                 * If no out of band data is expected,
1343
                 * pull receive urgent pointer along
1344
                 * with the receive window.
1345
                 */
1346
                if (SEQ_GT(tp->rcv_nxt, tp->rcv_up))
1347
                        tp->rcv_up = tp->rcv_nxt;
1348
dodata:
1349

    
1350
        /*
1351
         * Process the segment text, merging it into the TCP sequencing queue,
1352
         * and arranging for acknowledgment of receipt if necessary.
1353
         * This process logically involves adjusting tp->rcv_wnd as data
1354
         * is presented to the user (this happens in tcp_usrreq.c,
1355
         * case PRU_RCVD).  If a FIN has already been received on this
1356
         * connection then we just ignore the text.
1357
         */
1358
        if ((ti->ti_len || (tiflags&TH_FIN)) &&
1359
            TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1360
                TCP_REASS(tp, ti, m, so, tiflags);
1361
                /*
1362
                 * Note the amount of data that peer has sent into
1363
                 * our window, in order to estimate the sender's
1364
                 * buffer size.
1365
                 */
1366
                len = so->so_rcv.sb_datalen - (tp->rcv_adv - tp->rcv_nxt);
1367
        } else {
1368
                m_free(m);
1369
                tiflags &= ~TH_FIN;
1370
        }
1371

    
1372
        /*
1373
         * If FIN is received ACK the FIN and let the user know
1374
         * that the connection is closing.
1375
         */
1376
        if (tiflags & TH_FIN) {
1377
                if (TCPS_HAVERCVDFIN(tp->t_state) == 0) {
1378
                        /*
1379
                         * If we receive a FIN we can't send more data,
1380
                         * set it SS_FDRAIN
1381
                         * Shutdown the socket if there is no rx data in the
1382
                         * buffer.
1383
                         * soread() is called on completion of shutdown() and
1384
                         * will got to TCPS_LAST_ACK, and use tcp_output()
1385
                         * to send the FIN.
1386
                         */
1387
/*                        sofcantrcvmore(so); */
1388
                        sofwdrain(so);
1389

    
1390
                        tp->t_flags |= TF_ACKNOW;
1391
                        tp->rcv_nxt++;
1392
                }
1393
                switch (tp->t_state) {
1394

    
1395
                 /*
1396
                 * In SYN_RECEIVED and ESTABLISHED STATES
1397
                 * enter the CLOSE_WAIT state.
1398
                 */
1399
                case TCPS_SYN_RECEIVED:
1400
                case TCPS_ESTABLISHED:
1401
                  if(so->so_emu == EMU_CTL)        /* no shutdown on socket */
1402
                    tp->t_state = TCPS_LAST_ACK;
1403
                  else
1404
                    tp->t_state = TCPS_CLOSE_WAIT;
1405
                  break;
1406

    
1407
                 /*
1408
                 * If still in FIN_WAIT_1 STATE FIN has not been acked so
1409
                 * enter the CLOSING state.
1410
                 */
1411
                case TCPS_FIN_WAIT_1:
1412
                        tp->t_state = TCPS_CLOSING;
1413
                        break;
1414

    
1415
                 /*
1416
                 * In FIN_WAIT_2 state enter the TIME_WAIT state,
1417
                 * starting the time-wait timer, turning off the other
1418
                 * standard timers.
1419
                 */
1420
                case TCPS_FIN_WAIT_2:
1421
                        tp->t_state = TCPS_TIME_WAIT;
1422
                        tcp_canceltimers(tp);
1423
                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1424
                        soisfdisconnected(so);
1425
                        break;
1426

    
1427
                /*
1428
                 * In TIME_WAIT state restart the 2 MSL time_wait timer.
1429
                 */
1430
                case TCPS_TIME_WAIT:
1431
                        tp->t_timer[TCPT_2MSL] = 2 * TCPTV_MSL;
1432
                        break;
1433
                }
1434
        }
1435

    
1436
        /*
1437
         * If this is a small packet, then ACK now - with Nagel
1438
         *      congestion avoidance sender won't send more until
1439
         *      he gets an ACK.
1440
         *
1441
         * See above.
1442
         */
1443
/*        if (ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg) {
1444
 */
1445
/*        if ((ti->ti_len && (unsigned)ti->ti_len < tp->t_maxseg &&
1446
 *                (so->so_iptos & IPTOS_LOWDELAY) == 0) ||
1447
 *               ((so->so_iptos & IPTOS_LOWDELAY) &&
1448
 *               ((struct tcpiphdr_2 *)ti)->first_char == (char)27)) {
1449
 */
1450
        if (ti->ti_len && (unsigned)ti->ti_len <= 5 &&
1451
            ((struct tcpiphdr_2 *)ti)->first_char == (char)27) {
1452
                tp->t_flags |= TF_ACKNOW;
1453
        }
1454

    
1455
        /*
1456
         * Return any desired output.
1457
         */
1458
        if (needoutput || (tp->t_flags & TF_ACKNOW)) {
1459
                (void) tcp_output(tp);
1460
        }
1461
        return;
1462

    
1463
dropafterack:
1464
        /*
1465
         * Generate an ACK dropping incoming segment if it occupies
1466
         * sequence space, where the ACK reflects our state.
1467
         */
1468
        if (tiflags & TH_RST)
1469
                goto drop;
1470
        m_freem(m);
1471
        tp->t_flags |= TF_ACKNOW;
1472
        (void) tcp_output(tp);
1473
        return;
1474

    
1475
dropwithreset:
1476
        /* reuses m if m!=NULL, m_free() unnecessary */
1477
        if (tiflags & TH_ACK)
1478
                tcp_respond(tp, ti, m, (tcp_seq)0, ti->ti_ack, TH_RST);
1479
        else {
1480
                if (tiflags & TH_SYN) ti->ti_len++;
1481
                tcp_respond(tp, ti, m, ti->ti_seq+ti->ti_len, (tcp_seq)0,
1482
                    TH_RST|TH_ACK);
1483
        }
1484

    
1485
        return;
1486

    
1487
drop:
1488
        /*
1489
         * Drop space held by incoming segment and return.
1490
         */
1491
        m_free(m);
1492

    
1493
        return;
1494
}
1495

    
1496
 /* , ts_present, ts_val, ts_ecr) */
1497
/*        int *ts_present;
1498
 *        u_int32_t *ts_val, *ts_ecr;
1499
 */
1500
static void
1501
tcp_dooptions(struct tcpcb *tp, u_char *cp, int cnt, struct tcpiphdr *ti)
1502
{
1503
        u_int16_t mss;
1504
        int opt, optlen;
1505

    
1506
        DEBUG_CALL("tcp_dooptions");
1507
        DEBUG_ARGS((dfd," tp = %lx  cnt=%i \n", (long )tp, cnt));
1508

    
1509
        for (; cnt > 0; cnt -= optlen, cp += optlen) {
1510
                opt = cp[0];
1511
                if (opt == TCPOPT_EOL)
1512
                        break;
1513
                if (opt == TCPOPT_NOP)
1514
                        optlen = 1;
1515
                else {
1516
                        optlen = cp[1];
1517
                        if (optlen <= 0)
1518
                                break;
1519
                }
1520
                switch (opt) {
1521

    
1522
                default:
1523
                        continue;
1524

    
1525
                case TCPOPT_MAXSEG:
1526
                        if (optlen != TCPOLEN_MAXSEG)
1527
                                continue;
1528
                        if (!(ti->ti_flags & TH_SYN))
1529
                                continue;
1530
                        memcpy((char *) &mss, (char *) cp + 2, sizeof(mss));
1531
                        NTOHS(mss);
1532
                        (void) tcp_mss(tp, mss);        /* sets t_maxseg */
1533
                        break;
1534

    
1535
/*                case TCPOPT_WINDOW:
1536
 *                        if (optlen != TCPOLEN_WINDOW)
1537
 *                                continue;
1538
 *                        if (!(ti->ti_flags & TH_SYN))
1539
 *                                continue;
1540
 *                        tp->t_flags |= TF_RCVD_SCALE;
1541
 *                        tp->requested_s_scale = min(cp[2], TCP_MAX_WINSHIFT);
1542
 *                        break;
1543
 */
1544
/*                case TCPOPT_TIMESTAMP:
1545
 *                        if (optlen != TCPOLEN_TIMESTAMP)
1546
 *                                continue;
1547
 *                        *ts_present = 1;
1548
 *                        memcpy((char *) ts_val, (char *)cp + 2, sizeof(*ts_val));
1549
 *                        NTOHL(*ts_val);
1550
 *                        memcpy((char *) ts_ecr, (char *)cp + 6, sizeof(*ts_ecr));
1551
 *                        NTOHL(*ts_ecr);
1552
 *
1553
 */                        /*
1554
 *                         * A timestamp received in a SYN makes
1555
 *                         * it ok to send timestamp requests and replies.
1556
 *                         */
1557
/*                        if (ti->ti_flags & TH_SYN) {
1558
 *                                tp->t_flags |= TF_RCVD_TSTMP;
1559
 *                                tp->ts_recent = *ts_val;
1560
 *                                tp->ts_recent_age = tcp_now;
1561
 *                        }
1562
 */                        break;
1563
                }
1564
        }
1565
}
1566

    
1567

    
1568
/*
1569
 * Pull out of band byte out of a segment so
1570
 * it doesn't appear in the user's data queue.
1571
 * It is still reflected in the segment length for
1572
 * sequencing purposes.
1573
 */
1574

    
1575
#ifdef notdef
1576

    
1577
void
1578
tcp_pulloutofband(so, ti, m)
1579
        struct socket *so;
1580
        struct tcpiphdr *ti;
1581
        register struct mbuf *m;
1582
{
1583
        int cnt = ti->ti_urp - 1;
1584

    
1585
        while (cnt >= 0) {
1586
                if (m->m_len > cnt) {
1587
                        char *cp = mtod(m, caddr_t) + cnt;
1588
                        struct tcpcb *tp = sototcpcb(so);
1589

    
1590
                        tp->t_iobc = *cp;
1591
                        tp->t_oobflags |= TCPOOB_HAVEDATA;
1592
                        memcpy(sp, cp+1, (unsigned)(m->m_len - cnt - 1));
1593
                        m->m_len--;
1594
                        return;
1595
                }
1596
                cnt -= m->m_len;
1597
                m = m->m_next; /* XXX WRONG! Fix it! */
1598
                if (m == 0)
1599
                        break;
1600
        }
1601
        panic("tcp_pulloutofband");
1602
}
1603

    
1604
#endif /* notdef */
1605

    
1606
/*
1607
 * Collect new round-trip time estimate
1608
 * and update averages and current timeout.
1609
 */
1610

    
1611
static void
1612
tcp_xmit_timer(register struct tcpcb *tp, int rtt)
1613
{
1614
        register short delta;
1615

    
1616
        DEBUG_CALL("tcp_xmit_timer");
1617
        DEBUG_ARG("tp = %lx", (long)tp);
1618
        DEBUG_ARG("rtt = %d", rtt);
1619

    
1620
        STAT(tcpstat.tcps_rttupdated++);
1621
        if (tp->t_srtt != 0) {
1622
                /*
1623
                 * srtt is stored as fixed point with 3 bits after the
1624
                 * binary point (i.e., scaled by 8).  The following magic
1625
                 * is equivalent to the smoothing algorithm in rfc793 with
1626
                 * an alpha of .875 (srtt = rtt/8 + srtt*7/8 in fixed
1627
                 * point).  Adjust rtt to origin 0.
1628
                 */
1629
                delta = rtt - 1 - (tp->t_srtt >> TCP_RTT_SHIFT);
1630
                if ((tp->t_srtt += delta) <= 0)
1631
                        tp->t_srtt = 1;
1632
                /*
1633
                 * We accumulate a smoothed rtt variance (actually, a
1634
                 * smoothed mean difference), then set the retransmit
1635
                 * timer to smoothed rtt + 4 times the smoothed variance.
1636
                 * rttvar is stored as fixed point with 2 bits after the
1637
                 * binary point (scaled by 4).  The following is
1638
                 * equivalent to rfc793 smoothing with an alpha of .75
1639
                 * (rttvar = rttvar*3/4 + |delta| / 4).  This replaces
1640
                 * rfc793's wired-in beta.
1641
                 */
1642
                if (delta < 0)
1643
                        delta = -delta;
1644
                delta -= (tp->t_rttvar >> TCP_RTTVAR_SHIFT);
1645
                if ((tp->t_rttvar += delta) <= 0)
1646
                        tp->t_rttvar = 1;
1647
        } else {
1648
                /*
1649
                 * No rtt measurement yet - use the unsmoothed rtt.
1650
                 * Set the variance to half the rtt (so our first
1651
                 * retransmit happens at 3*rtt).
1652
                 */
1653
                tp->t_srtt = rtt << TCP_RTT_SHIFT;
1654
                tp->t_rttvar = rtt << (TCP_RTTVAR_SHIFT - 1);
1655
        }
1656
        tp->t_rtt = 0;
1657
        tp->t_rxtshift = 0;
1658

    
1659
        /*
1660
         * the retransmit should happen at rtt + 4 * rttvar.
1661
         * Because of the way we do the smoothing, srtt and rttvar
1662
         * will each average +1/2 tick of bias.  When we compute
1663
         * the retransmit timer, we want 1/2 tick of rounding and
1664
         * 1 extra tick because of +-1/2 tick uncertainty in the
1665
         * firing of the timer.  The bias will give us exactly the
1666
         * 1.5 tick we need.  But, because the bias is
1667
         * statistical, we have to test that we don't drop below
1668
         * the minimum feasible timer (which is 2 ticks).
1669
         */
1670
        TCPT_RANGESET(tp->t_rxtcur, TCP_REXMTVAL(tp),
1671
            (short)tp->t_rttmin, TCPTV_REXMTMAX); /* XXX */
1672

    
1673
        /*
1674
         * We received an ack for a packet that wasn't retransmitted;
1675
         * it is probably safe to discard any error indications we've
1676
         * received recently.  This isn't quite right, but close enough
1677
         * for now (a route might have failed after we sent a segment,
1678
         * and the return path might not be symmetrical).
1679
         */
1680
        tp->t_softerror = 0;
1681
}
1682

    
1683
/*
1684
 * Determine a reasonable value for maxseg size.
1685
 * If the route is known, check route for mtu.
1686
 * If none, use an mss that can be handled on the outgoing
1687
 * interface without forcing IP to fragment; if bigger than
1688
 * an mbuf cluster (MCLBYTES), round down to nearest multiple of MCLBYTES
1689
 * to utilize large mbufs.  If no route is found, route has no mtu,
1690
 * or the destination isn't local, use a default, hopefully conservative
1691
 * size (usually 512 or the default IP max size, but no more than the mtu
1692
 * of the interface), as we can't discover anything about intervening
1693
 * gateways or networks.  We also initialize the congestion/slow start
1694
 * window to be a single segment if the destination isn't local.
1695
 * While looking at the routing entry, we also initialize other path-dependent
1696
 * parameters from pre-set or cached values in the routing entry.
1697
 */
1698

    
1699
int
1700
tcp_mss(struct tcpcb *tp, u_int offer)
1701
{
1702
        struct socket *so = tp->t_socket;
1703
        int mss;
1704

    
1705
        DEBUG_CALL("tcp_mss");
1706
        DEBUG_ARG("tp = %lx", (long)tp);
1707
        DEBUG_ARG("offer = %d", offer);
1708

    
1709
        mss = min(IF_MTU, IF_MRU) - sizeof(struct tcpiphdr);
1710
        if (offer)
1711
                mss = min(mss, offer);
1712
        mss = max(mss, 32);
1713
        if (mss < tp->t_maxseg || offer != 0)
1714
           tp->t_maxseg = mss;
1715

    
1716
        tp->snd_cwnd = mss;
1717

    
1718
        sbreserve(&so->so_snd, TCP_SNDSPACE + ((TCP_SNDSPACE % mss) ?
1719
                                               (mss - (TCP_SNDSPACE % mss)) :
1720
                                               0));
1721
        sbreserve(&so->so_rcv, TCP_RCVSPACE + ((TCP_RCVSPACE % mss) ?
1722
                                               (mss - (TCP_RCVSPACE % mss)) :
1723
                                               0));
1724

    
1725
        DEBUG_MISC((dfd, " returning mss = %d\n", mss));
1726

    
1727
        return mss;
1728
}