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

Subversion Repositories openrisc

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

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      src/sys/netinet/tcp_output.c
4
//
5
//==========================================================================
6
//####BSDCOPYRIGHTBEGIN####
7
//
8
// -------------------------------------------
9
//
10
// Portions of this software may have been derived from OpenBSD, 
11
// FreeBSD or other sources, and are covered by the appropriate
12
// copyright disclaimers included herein.
13
//
14
// Portions created by Red Hat are
15
// Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16
//
17
// -------------------------------------------
18
//
19
//####BSDCOPYRIGHTEND####
20
//==========================================================================
21
 
22
/*
23
 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24
 *      The Regents of the University of California.  All rights reserved.
25
 *
26
 * Redistribution and use in source and binary forms, with or without
27
 * modification, are permitted provided that the following conditions
28
 * are met:
29
 * 1. Redistributions of source code must retain the above copyright
30
 *    notice, this list of conditions and the following disclaimer.
31
 * 2. Redistributions in binary form must reproduce the above copyright
32
 *    notice, this list of conditions and the following disclaimer in the
33
 *    documentation and/or other materials provided with the distribution.
34
 * 3. All advertising materials mentioning features or use of this software
35
 *    must display the following acknowledgement:
36
 *      This product includes software developed by the University of
37
 *      California, Berkeley and its contributors.
38
 * 4. Neither the name of the University nor the names of its contributors
39
 *    may be used to endorse or promote products derived from this software
40
 *    without specific prior written permission.
41
 *
42
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52
 * SUCH DAMAGE.
53
 *
54
 *      @(#)tcp_output.c        8.4 (Berkeley) 5/24/95
55
 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
56
 */
57
 
58
#include <sys/param.h>
59
#include <sys/mbuf.h>
60
#include <sys/domain.h>
61
#include <sys/protosw.h>
62
#include <sys/socket.h>
63
#include <sys/socketvar.h>
64
 
65
#include <net/route.h>
66
 
67
#include <netinet/in.h>
68
#include <netinet/in_systm.h>
69
#include <netinet/ip.h>
70
#include <netinet/in_pcb.h>
71
#include <netinet/ip_var.h>
72
#ifdef INET6
73
#include <netinet6/in6_pcb.h>
74
#include <netinet/ip6.h>
75
#include <netinet6/ip6_var.h>
76
#endif
77
#include <netinet/tcp.h>
78
#define TCPOUTFLAGS
79
#include <netinet/tcp_fsm.h>
80
#include <netinet/tcp_seq.h>
81
#include <netinet/tcp_timer.h>
82
#include <netinet/tcp_var.h>
83
#include <netinet/tcpip.h>
84
#ifdef TCPDEBUG
85
#include <netinet/tcp_debug.h>
86
#endif
87
 
88
#ifdef IPSEC
89
#include <netinet6/ipsec.h>
90
#endif /*IPSEC*/
91
 
92
#ifdef notyet
93
extern struct mbuf *m_copypack();
94
#endif
95
 
96
static int path_mtu_discovery = 1;
97
int ss_fltsz = 1;
98
int ss_fltsz_local = TCP_MAXWIN;               /* something large */
99
int     tcp_do_newreno = 1;
100
 
101
/*
102
 * Tcp output routine: figure out what should be sent and send it.
103
 */
104
int
105
tcp_output(tp)
106
        register struct tcpcb *tp;
107
{
108
        register struct socket *so = tp->t_inpcb->inp_socket;
109
        register long len, win;
110
        int off, flags, error;
111
        register struct mbuf *m;
112
        struct ip *ip = NULL;
113
        register struct ipovly *ipov = NULL;
114
#ifdef INET6
115
        struct ip6_hdr *ip6 = NULL;
116
#endif /* INET6 */
117
        register struct tcphdr *th;
118
        u_char opt[TCP_MAXOLEN];
119
        unsigned ipoptlen, optlen, hdrlen;
120
        int idle, sendalot;
121
        int maxburst = TCP_MAXBURST;
122
        struct rmxp_tao *taop;
123
        struct rmxp_tao tao_noncached;
124
#ifdef INET6
125
        int isipv6;
126
#endif
127
 
128
#ifdef INET6
129
        isipv6 = (tp->t_inpcb->inp_vflag & INP_IPV6) != 0;
130
#endif
131
 
132
        /*
133
         * Determine length of data that should be transmitted,
134
         * and flags that will be used.
135
         * If there is some data or critical controls (SYN, RST)
136
         * to send, then transmit; otherwise, investigate further.
137
         */
138
        idle = (tp->snd_max == tp->snd_una);
139
        if (idle && (ticks - tp->t_rcvtime) >= tp->t_rxtcur) {
140
                /*
141
                 * We have been idle for "a while" and no acks are
142
                 * expected to clock out any data we send --
143
                 * slow start to get ack "clock" running again.
144
                 *
145
                 * Set the slow-start flight size depending on whether
146
                 * this is a local network or not.
147
                 */
148
                if (
149
#ifdef INET6
150
                    (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) ||
151
                    (!isipv6 &&
152
#endif
153
                     in_localaddr(tp->t_inpcb->inp_faddr)
154
#ifdef INET6
155
                     )
156
#endif
157
                    )
158
                        tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local;
159
                else
160
                        tp->snd_cwnd = tp->t_maxseg * ss_fltsz;
161
        }
162
again:
163
        sendalot = 0;
164
        off = tp->snd_nxt - tp->snd_una;
165
        win = min(tp->snd_wnd, tp->snd_cwnd);
166
 
167
        flags = tcp_outflags[tp->t_state];
168
        /*
169
         * Get standard flags, and add SYN or FIN if requested by 'hidden'
170
         * state flags.
171
         */
172
        if (tp->t_flags & TF_NEEDFIN)
173
                flags |= TH_FIN;
174
        if (tp->t_flags & TF_NEEDSYN)
175
                flags |= TH_SYN;
176
 
177
        /*
178
         * If in persist timeout with window of 0, send 1 byte.
179
         * Otherwise, if window is small but nonzero
180
         * and timer expired, we will send what we can
181
         * and go to transmit state.
182
         */
183
        if (tp->t_force) {
184
                if (win == 0) {
185
                        /*
186
                         * If we still have some data to send, then
187
                         * clear the FIN bit.  Usually this would
188
                         * happen below when it realizes that we
189
                         * aren't sending all the data.  However,
190
                         * if we have exactly 1 byte of unsent data,
191
                         * then it won't clear the FIN bit below,
192
                         * and if we are in persist state, we wind
193
                         * up sending the packet without recording
194
                         * that we sent the FIN bit.
195
                         *
196
                         * We can't just blindly clear the FIN bit,
197
                         * because if we don't have any more data
198
                         * to send then the probe will be the FIN
199
                         * itself.
200
                         */
201
                        if (off < so->so_snd.sb_cc)
202
                                flags &= ~TH_FIN;
203
                        win = 1;
204
                } else {
205
                        callout_stop(tp->tt_persist);
206
                        tp->t_rxtshift = 0;
207
                }
208
        }
209
 
210
        len = (long)ulmin(so->so_snd.sb_cc, win) - off;
211
 
212
        if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) {
213
                taop = &tao_noncached;
214
                bzero(taop, sizeof(*taop));
215
        }
216
 
217
        /*
218
         * Lop off SYN bit if it has already been sent.  However, if this
219
         * is SYN-SENT state and if segment contains data and if we don't
220
         * know that foreign host supports TAO, suppress sending segment.
221
         */
222
        if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
223
                flags &= ~TH_SYN;
224
                off--, len++;
225
                if (len > 0 && tp->t_state == TCPS_SYN_SENT &&
226
                    taop->tao_ccsent == 0)
227
                        return 0;
228
        }
229
 
230
        /*
231
         * Be careful not to send data and/or FIN on SYN segments
232
         * in cases when no CC option will be sent.
233
         * This measure is needed to prevent interoperability problems
234
         * with not fully conformant TCP implementations.
235
         */
236
        if ((flags & TH_SYN) &&
237
            ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) ||
238
             ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) {
239
                len = 0;
240
                flags &= ~TH_FIN;
241
        }
242
 
243
        if (len < 0) {
244
                /*
245
                 * If FIN has been sent but not acked,
246
                 * but we haven't been called to retransmit,
247
                 * len will be -1.  Otherwise, window shrank
248
                 * after we sent into it.  If window shrank to 0,
249
                 * cancel pending retransmit, pull snd_nxt back
250
                 * to (closed) window, and set the persist timer
251
                 * if it isn't already going.  If the window didn't
252
                 * close completely, just wait for an ACK.
253
                 */
254
                len = 0;
255
                if (win == 0) {
256
                        callout_stop(tp->tt_rexmt);
257
                        tp->t_rxtshift = 0;
258
                        tp->snd_nxt = tp->snd_una;
259
                        if (!callout_active(tp->tt_persist))
260
                                tcp_setpersist(tp);
261
                }
262
        }
263
        if (len > tp->t_maxseg) {
264
                len = tp->t_maxseg;
265
                sendalot = 1;
266
        }
267
        if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
268
                flags &= ~TH_FIN;
269
 
270
        win = sbspace(&so->so_rcv);
271
 
272
        /*
273
         * Sender silly window avoidance.  If connection is idle
274
         * and can send all data, a maximum segment,
275
         * at least a maximum default-size segment do it,
276
         * or are forced, do it; otherwise don't bother.
277
         * If peer's buffer is tiny, then send
278
         * when window is at least half open.
279
         * If retransmitting (possibly after persist timer forced us
280
         * to send into a small window), then must resend.
281
         */
282
        if (len) {
283
                if (len == tp->t_maxseg)
284
                        goto send;
285
                if (!(tp->t_flags & TF_MORETOCOME) &&
286
                    (idle || tp->t_flags & TF_NODELAY) &&
287
                    (tp->t_flags & TF_NOPUSH) == 0 &&
288
                    len + off >= so->so_snd.sb_cc)
289
                        goto send;
290
                if (tp->t_force)
291
                        goto send;
292
                if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
293
                        goto send;
294
                if (SEQ_LT(tp->snd_nxt, tp->snd_max))
295
                        goto send;
296
        }
297
 
298
        /*
299
         * Compare available window to amount of window
300
         * known to peer (as advertised window less
301
         * next expected input).  If the difference is at least two
302
         * max size segments, or at least 50% of the maximum possible
303
         * window, then want to send a window update to peer.
304
         */
305
        if (win > 0) {
306
                /*
307
                 * "adv" is the amount we can increase the window,
308
                 * taking into account that we are limited by
309
                 * TCP_MAXWIN << tp->rcv_scale.
310
                 */
311
                long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
312
                        (tp->rcv_adv - tp->rcv_nxt);
313
 
314
                if (adv >= (long) (2 * tp->t_maxseg))
315
                        goto send;
316
                if (2 * adv >= (long) so->so_rcv.sb_hiwat)
317
                        goto send;
318
        }
319
 
320
        /*
321
         * Send if we owe peer an ACK.
322
         */
323
        if (tp->t_flags & TF_ACKNOW)
324
                goto send;
325
        if ((flags & TH_RST) ||
326
            ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
327
                goto send;
328
        if (SEQ_GT(tp->snd_up, tp->snd_una))
329
                goto send;
330
        /*
331
         * If our state indicates that FIN should be sent
332
         * and we have not yet done so, or we're retransmitting the FIN,
333
         * then we need to send.
334
         */
335
        if (flags & TH_FIN &&
336
            ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
337
                goto send;
338
 
339
        /*
340
         * TCP window updates are not reliable, rather a polling protocol
341
         * using ``persist'' packets is used to insure receipt of window
342
         * updates.  The three ``states'' for the output side are:
343
         *      idle                    not doing retransmits or persists
344
         *      persisting              to move a small or zero window
345
         *      (re)transmitting        and thereby not persisting
346
         *
347
         * callout_active(tp->tt_persist)
348
         *      is true when we are in persist state.
349
         * tp->t_force
350
         *      is set when we are called to send a persist packet.
351
         * callout_active(tp->tt_rexmt)
352
         *      is set when we are retransmitting
353
         * The output side is idle when both timers are zero.
354
         *
355
         * If send window is too small, there is data to transmit, and no
356
         * retransmit or persist is pending, then go to persist state.
357
         * If nothing happens soon, send when timer expires:
358
         * if window is nonzero, transmit what we can,
359
         * otherwise force out a byte.
360
         */
361
        if (so->so_snd.sb_cc && !callout_active(tp->tt_rexmt) &&
362
            !callout_active(tp->tt_persist)) {
363
                tp->t_rxtshift = 0;
364
                tcp_setpersist(tp);
365
        }
366
 
367
        /*
368
         * No reason to send a segment, just return.
369
         */
370
        return (0);
371
 
372
send:
373
        /*
374
         * Before ESTABLISHED, force sending of initial options
375
         * unless TCP set not to do any options.
376
         * NOTE: we assume that the IP/TCP header plus TCP options
377
         * always fit in a single mbuf, leaving room for a maximum
378
         * link header, i.e.
379
         *      max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
380
         */
381
        optlen = 0;
382
#ifdef INET6
383
        if (isipv6)
384
                hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
385
        else
386
#endif
387
        hdrlen = sizeof (struct tcpiphdr);
388
        if (flags & TH_SYN) {
389
                tp->snd_nxt = tp->iss;
390
                if ((tp->t_flags & TF_NOOPT) == 0) {
391
                        u_short mss;
392
 
393
                        opt[0] = TCPOPT_MAXSEG;
394
                        opt[1] = TCPOLEN_MAXSEG;
395
                        mss = htons((u_short) tcp_mssopt(tp));
396
                        (void)memcpy(opt + 2, &mss, sizeof(mss));
397
                        optlen = TCPOLEN_MAXSEG;
398
 
399
                        if ((tp->t_flags & TF_REQ_SCALE) &&
400
                            ((flags & TH_ACK) == 0 ||
401
                            (tp->t_flags & TF_RCVD_SCALE))) {
402
                                *((u_int32_t *)(opt + optlen)) = htonl(
403
                                        TCPOPT_NOP << 24 |
404
                                        TCPOPT_WINDOW << 16 |
405
                                        TCPOLEN_WINDOW << 8 |
406
                                        tp->request_r_scale);
407
                                optlen += 4;
408
                        }
409
                }
410
        }
411
 
412
        /*
413
         * Send a timestamp and echo-reply if this is a SYN and our side
414
         * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
415
         * and our peer have sent timestamps in our SYN's.
416
         */
417
        if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
418
            (flags & TH_RST) == 0 &&
419
            ((flags & TH_ACK) == 0 ||
420
             (tp->t_flags & TF_RCVD_TSTMP))) {
421
                u_int32_t *lp = (u_int32_t *)(opt + optlen);
422
 
423
                /* Form timestamp option as shown in appendix A of RFC 1323. */
424
                *lp++ = htonl(TCPOPT_TSTAMP_HDR);
425
                *lp++ = htonl(ticks);
426
                *lp   = htonl(tp->ts_recent);
427
                optlen += TCPOLEN_TSTAMP_APPA;
428
        }
429
 
430
        /*
431
         * Send `CC-family' options if our side wants to use them (TF_REQ_CC),
432
         * options are allowed (!TF_NOOPT) and it's not a RST.
433
         */
434
        if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC &&
435
             (flags & TH_RST) == 0) {
436
                switch (flags & (TH_SYN|TH_ACK)) {
437
                /*
438
                 * This is a normal ACK, send CC if we received CC before
439
                 * from our peer.
440
                 */
441
                case TH_ACK:
442
                        if (!(tp->t_flags & TF_RCVD_CC))
443
                                break;
444
                        /*FALLTHROUGH*/
445
 
446
                /*
447
                 * We can only get here in T/TCP's SYN_SENT* state, when
448
                 * we're a sending a non-SYN segment without waiting for
449
                 * the ACK of our SYN.  A check above assures that we only
450
                 * do this if our peer understands T/TCP.
451
                 */
452
                case 0:
453
                        opt[optlen++] = TCPOPT_NOP;
454
                        opt[optlen++] = TCPOPT_NOP;
455
                        opt[optlen++] = TCPOPT_CC;
456
                        opt[optlen++] = TCPOLEN_CC;
457
                        *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
458
 
459
                        optlen += 4;
460
                        break;
461
 
462
                /*
463
                 * This is our initial SYN, check whether we have to use
464
                 * CC or CC.new.
465
                 */
466
                case TH_SYN:
467
                        opt[optlen++] = TCPOPT_NOP;
468
                        opt[optlen++] = TCPOPT_NOP;
469
                        opt[optlen++] = tp->t_flags & TF_SENDCCNEW ?
470
                                                TCPOPT_CCNEW : TCPOPT_CC;
471
                        opt[optlen++] = TCPOLEN_CC;
472
                        *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send);
473
                        optlen += 4;
474
                        break;
475
 
476
                /*
477
                 * This is a SYN,ACK; send CC and CC.echo if we received
478
                 * CC from our peer.
479
                 */
480
                case (TH_SYN|TH_ACK):
481
                        if (tp->t_flags & TF_RCVD_CC) {
482
                                opt[optlen++] = TCPOPT_NOP;
483
                                opt[optlen++] = TCPOPT_NOP;
484
                                opt[optlen++] = TCPOPT_CC;
485
                                opt[optlen++] = TCPOLEN_CC;
486
                                *(u_int32_t *)&opt[optlen] =
487
                                        htonl(tp->cc_send);
488
                                optlen += 4;
489
                                opt[optlen++] = TCPOPT_NOP;
490
                                opt[optlen++] = TCPOPT_NOP;
491
                                opt[optlen++] = TCPOPT_CCECHO;
492
                                opt[optlen++] = TCPOLEN_CC;
493
                                *(u_int32_t *)&opt[optlen] =
494
                                        htonl(tp->cc_recv);
495
                                optlen += 4;
496
                        }
497
                        break;
498
                }
499
        }
500
 
501
        hdrlen += optlen;
502
 
503
#ifdef INET6
504
        if (isipv6)
505
                ipoptlen = ip6_optlen(tp->t_inpcb);
506
        else
507
#endif
508
      {
509
        if (tp->t_inpcb->inp_options) {
510
                ipoptlen = tp->t_inpcb->inp_options->m_len -
511
                                offsetof(struct ipoption, ipopt_list);
512
        } else {
513
                ipoptlen = 0;
514
        }
515
      }
516
#ifdef IPSEC
517
        ipoptlen += ipsec_hdrsiz_tcp(tp);
518
#endif
519
 
520
        /*
521
         * Adjust data length if insertion of options will
522
         * bump the packet length beyond the t_maxopd length.
523
         * Clear the FIN bit because we cut off the tail of
524
         * the segment.
525
         */
526
        if (len + optlen + ipoptlen > tp->t_maxopd) {
527
                /*
528
                 * If there is still more to send, don't close the connection.
529
                 */
530
                flags &= ~TH_FIN;
531
                len = tp->t_maxopd - optlen - ipoptlen;
532
                sendalot = 1;
533
        }
534
 
535
/*#ifdef DIAGNOSTIC*/
536
#ifdef INET6
537
        if (max_linkhdr + hdrlen > MCLBYTES)
538
                panic("tcphdr too big");
539
#else
540
        if (max_linkhdr + hdrlen > MHLEN)
541
                panic("tcphdr too big");
542
#endif
543
/*#endif*/
544
 
545
        /*
546
         * Grab a header mbuf, attaching a copy of data to
547
         * be transmitted, and initialize the header from
548
         * the template for sends on this connection.
549
         */
550
        if (len) {
551
                if (tp->t_force && len == 1)
552
                        tcpstat.tcps_sndprobe++;
553
                else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
554
                        tcpstat.tcps_sndrexmitpack++;
555
                        tcpstat.tcps_sndrexmitbyte += len;
556
                } else {
557
                        tcpstat.tcps_sndpack++;
558
                        tcpstat.tcps_sndbyte += len;
559
                }
560
#ifdef notyet
561
                if ((m = m_copypack(so->so_snd.sb_mb, off,
562
                    (int)len, max_linkhdr + hdrlen)) == 0) {
563
                        error = ENOBUFS;
564
                        goto out;
565
                }
566
                /*
567
                 * m_copypack left space for our hdr; use it.
568
                 */
569
                m->m_len += hdrlen;
570
                m->m_data -= hdrlen;
571
#else
572
                MGETHDR(m, M_DONTWAIT, MT_HEADER);
573
                if (m == NULL) {
574
                        error = ENOBUFS;
575
                        goto out;
576
                }
577
#ifdef INET6
578
                if (MHLEN < hdrlen + max_linkhdr) {
579
                        MCLGET(m, M_DONTWAIT);
580
                        if ((m->m_flags & M_EXT) == 0) {
581
                                m_freem(m);
582
                                error = ENOBUFS;
583
                                goto out;
584
                        }
585
                }
586
#endif
587
                m->m_data += max_linkhdr;
588
                m->m_len = hdrlen;
589
                if (len <= MHLEN - hdrlen - max_linkhdr) {
590
                        m_copydata(so->so_snd.sb_mb, off, (int) len,
591
                            mtod(m, caddr_t) + hdrlen);
592
                        m->m_len += len;
593
                } else {
594
                        m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
595
                        if (m->m_next == 0) {
596
                                (void) m_free(m);
597
                                error = ENOBUFS;
598
                                goto out;
599
                        }
600
                }
601
#endif
602
                /*
603
                 * If we're sending everything we've got, set PUSH.
604
                 * (This will keep happy those implementations which only
605
                 * give data to the user when a buffer fills or
606
                 * a PUSH comes in.)
607
                 */
608
                if (off + len == so->so_snd.sb_cc)
609
                        flags |= TH_PUSH;
610
        } else {
611
                if (tp->t_flags & TF_ACKNOW)
612
                        tcpstat.tcps_sndacks++;
613
                else if (flags & (TH_SYN|TH_FIN|TH_RST))
614
                        tcpstat.tcps_sndctrl++;
615
                else if (SEQ_GT(tp->snd_up, tp->snd_una))
616
                        tcpstat.tcps_sndurg++;
617
                else
618
                        tcpstat.tcps_sndwinup++;
619
 
620
                MGETHDR(m, M_DONTWAIT, MT_HEADER);
621
                if (m == NULL) {
622
                        error = ENOBUFS;
623
                        goto out;
624
                }
625
#ifdef INET6
626
                if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
627
                    MHLEN >= hdrlen) {
628
                        MH_ALIGN(m, hdrlen);
629
                } else
630
#endif
631
                m->m_data += max_linkhdr;
632
                m->m_len = hdrlen;
633
        }
634
        m->m_pkthdr.rcvif = (struct ifnet *)0;
635
#ifdef INET6
636
        if (isipv6) {
637
                ip6 = mtod(m, struct ip6_hdr *);
638
                th = (struct tcphdr *)(ip6 + 1);
639
                tcp_fillheaders(tp, ip6, th);
640
        } else
641
#endif /* INET6 */
642
      {
643
        ip = mtod(m, struct ip *);
644
        ipov = (struct ipovly *)ip;
645
        th = (struct tcphdr *)(ip + 1);
646
        /* this picks up the pseudo header (w/o the length) */
647
        tcp_fillheaders(tp, ip, th);
648
      }
649
 
650
        /*
651
         * Fill in fields, remembering maximum advertised
652
         * window for use in delaying messages about window sizes.
653
         * If resending a FIN, be sure not to use a new sequence number.
654
         */
655
        if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
656
            tp->snd_nxt == tp->snd_max)
657
                tp->snd_nxt--;
658
        /*
659
         * If we are doing retransmissions, then snd_nxt will
660
         * not reflect the first unsent octet.  For ACK only
661
         * packets, we do not want the sequence number of the
662
         * retransmitted packet, we want the sequence number
663
         * of the next unsent octet.  So, if there is no data
664
         * (and no SYN or FIN), use snd_max instead of snd_nxt
665
         * when filling in ti_seq.  But if we are in persist
666
         * state, snd_max might reflect one byte beyond the
667
         * right edge of the window, so use snd_nxt in that
668
         * case, since we know we aren't doing a retransmission.
669
         * (retransmit and persist are mutually exclusive...)
670
         */
671
        if (len || (flags & (TH_SYN|TH_FIN))
672
            || callout_active(tp->tt_persist))
673
                th->th_seq = htonl(tp->snd_nxt);
674
        else
675
                th->th_seq = htonl(tp->snd_max);
676
        th->th_ack = htonl(tp->rcv_nxt);
677
        if (optlen) {
678
                bcopy(opt, th + 1, optlen);
679
                th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
680
        }
681
        th->th_flags = flags;
682
        /*
683
         * Calculate receive window.  Don't shrink window,
684
         * but avoid silly window syndrome.
685
         */
686
        if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg)
687
                win = 0;
688
        if (win < (long)(tp->rcv_adv - tp->rcv_nxt))
689
                win = (long)(tp->rcv_adv - tp->rcv_nxt);
690
        if (win > (long)TCP_MAXWIN << tp->rcv_scale)
691
                win = (long)TCP_MAXWIN << tp->rcv_scale;
692
        th->th_win = htons((u_short) (win>>tp->rcv_scale));
693
        if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
694
                th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
695
                th->th_flags |= TH_URG;
696
        } else
697
                /*
698
                 * If no urgent pointer to send, then we pull
699
                 * the urgent pointer to the left edge of the send window
700
                 * so that it doesn't drift into the send window on sequence
701
                 * number wraparound.
702
                 */
703
                tp->snd_up = tp->snd_una;               /* drag it along */
704
 
705
        /*
706
         * Put TCP length in extended header, and then
707
         * checksum extended header and data.
708
         */
709
        m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
710
#ifdef INET6
711
        if (isipv6)
712
                /*
713
                 * ip6_plen is not need to be filled now, and will be filled
714
                 * in ip6_output.
715
                 */
716
                th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
717
                                       sizeof(struct tcphdr) + optlen + len);
718
        else
719
#endif /* INET6 */
720
      {
721
        m->m_pkthdr.csum_flags = CSUM_TCP;
722
        m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
723
        if (len + optlen)
724
                th->th_sum = in_addword(th->th_sum,
725
                    htons((u_short)(optlen + len)));
726
 
727
        /* IP version must be set here for ipv4/ipv6 checking later */
728
      }
729
 
730
        /*
731
         * In transmit state, time the transmission and arrange for
732
         * the retransmit.  In persist state, just set snd_max.
733
         */
734
        if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
735
                tcp_seq startseq = tp->snd_nxt;
736
 
737
                /*
738
                 * Advance snd_nxt over sequence space of this segment.
739
                 */
740
                if (flags & (TH_SYN|TH_FIN)) {
741
                        if (flags & TH_SYN)
742
                                tp->snd_nxt++;
743
                        if (flags & TH_FIN) {
744
                                tp->snd_nxt++;
745
                                tp->t_flags |= TF_SENTFIN;
746
                        }
747
                }
748
                tp->snd_nxt += len;
749
                if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
750
                        tp->snd_max = tp->snd_nxt;
751
                        /*
752
                         * Time this transmission if not a retransmission and
753
                         * not currently timing anything.
754
                         */
755
                        if (tp->t_rtttime == 0) {
756
                                tp->t_rtttime = ticks;
757
                                tp->t_rtseq = startseq;
758
                                tcpstat.tcps_segstimed++;
759
                        }
760
                }
761
 
762
                /*
763
                 * Set retransmit timer if not currently set,
764
                 * and not doing an ack or a keep-alive probe.
765
                 * Initial value for retransmit timer is smoothed
766
                 * round-trip time + 2 * round-trip time variance.
767
                 * Initialize shift counter which is used for backoff
768
                 * of retransmit time.
769
                 */
770
                if (!callout_active(tp->tt_rexmt) &&
771
                    tp->snd_nxt != tp->snd_una) {
772
                        if (callout_active(tp->tt_persist)) {
773
                                callout_stop(tp->tt_persist);
774
                                tp->t_rxtshift = 0;
775
                        }
776
                        callout_reset(tp->tt_rexmt, tp->t_rxtcur,
777
                                      tcp_timer_rexmt, tp);
778
                }
779
        } else
780
                if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
781
                        tp->snd_max = tp->snd_nxt + len;
782
 
783
#ifdef TCPDEBUG
784
        /*
785
         * Trace.
786
         */
787
        if (so->so_options & SO_DEBUG)
788
                tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
789
#endif
790
 
791
        /*
792
         * Fill in IP length and desired time to live and
793
         * send to IP level.  There should be a better way
794
         * to handle ttl and tos; we could keep them in
795
         * the template, but need a way to checksum without them.
796
         */
797
        /*
798
         * m->m_pkthdr.len should have been set before cksum calcuration,
799
         * because in6_cksum() need it.
800
         */
801
#ifdef INET6
802
        if (isipv6) {
803
                /*
804
                 * we separately set hoplimit for every segment, since the
805
                 * user might want to change the value via setsockopt.
806
                 * Also, desired default hop limit might be changed via
807
                 * Neighbor Discovery.
808
                 */
809
                ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb,
810
                                               tp->t_inpcb->in6p_route.ro_rt ?
811
                                               tp->t_inpcb->in6p_route.ro_rt->rt_ifp
812
                                               : NULL);
813
 
814
                /* TODO: IPv6 IP6TOS_ECT bit on */
815
#ifdef IPSEC
816
                if (ipsec_setsocket(m, so) != 0) {
817
                        m_freem(m);
818
                        error = ENOBUFS;
819
                        goto out;
820
                }
821
#endif /*IPSEC*/
822
                error = ip6_output(m,
823
                            tp->t_inpcb->in6p_outputopts,
824
                            &tp->t_inpcb->in6p_route,
825
                            (so->so_options & SO_DONTROUTE), NULL, NULL);
826
        } else
827
#endif /* INET6 */
828
    {
829
        struct rtentry *rt;
830
        ip->ip_len = m->m_pkthdr.len;
831
#ifdef INET6
832
        if (INP_CHECK_SOCKAF(so, AF_INET6))
833
                ip->ip_ttl = in6_selecthlim(tp->t_inpcb,
834
                                            tp->t_inpcb->in6p_route.ro_rt ?
835
                                            tp->t_inpcb->in6p_route.ro_rt->rt_ifp
836
                                            : NULL);
837
        else
838
#endif /* INET6 */
839
        ip->ip_ttl = tp->t_inpcb->inp_ip_ttl;   /* XXX */
840
        ip->ip_tos = tp->t_inpcb->inp_ip_tos;   /* XXX */
841
        /*
842
         * See if we should do MTU discovery.  We do it only if the following
843
         * are true:
844
         *      1) we have a valid route to the destination
845
         *      2) the MTU is not locked (if it is, then discovery has been
846
         *         disabled)
847
         */
848
        if (path_mtu_discovery
849
            && (rt = tp->t_inpcb->inp_route.ro_rt)
850
            && rt->rt_flags & RTF_UP
851
            && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
852
                ip->ip_off |= IP_DF;
853
        }
854
#ifdef IPSEC
855
        ipsec_setsocket(m, so);
856
#endif /*IPSEC*/
857
        error = ip_output(m, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
858
            (so->so_options & SO_DONTROUTE), 0);
859
    }
860
        if (error) {
861
 
862
                /*
863
                 * We know that the packet was lost, so back out the
864
                 * sequence number advance, if any.
865
                 */
866
                if (tp->t_force == 0 || !callout_active(tp->tt_persist)) {
867
                        /*
868
                         * No need to check for TH_FIN here because
869
                         * the TF_SENTFIN flag handles that case.
870
                         */
871
                        if ((flags & TH_SYN) == 0)
872
                                tp->snd_nxt -= len;
873
                }
874
 
875
out:
876
                if (error == ENOBUFS) {
877
                        if (!callout_active(tp->tt_rexmt) &&
878
                            !callout_active(tp->tt_persist))
879
                                callout_reset(tp->tt_rexmt, tp->t_rxtcur,
880
                                      tcp_timer_rexmt, tp);
881
                        tcp_quench(tp->t_inpcb, 0);
882
                        return (0);
883
                }
884
                if (error == EMSGSIZE) {
885
                        /*
886
                         * ip_output() will have already fixed the route
887
                         * for us.  tcp_mtudisc() will, as its last action,
888
                         * initiate retransmission, so it is important to
889
                         * not do so here.
890
                         */
891
                        tcp_mtudisc(tp->t_inpcb, 0);
892
                        return 0;
893
                }
894
                if ((error == EHOSTUNREACH || error == ENETDOWN)
895
                    && TCPS_HAVERCVDSYN(tp->t_state)) {
896
                        tp->t_softerror = error;
897
                        return (0);
898
                }
899
                return (error);
900
        }
901
        tcpstat.tcps_sndtotal++;
902
 
903
        /*
904
         * Data sent (as far as we can tell).
905
         * If this advertises a larger window than any other segment,
906
         * then remember the size of the advertised window.
907
         * Any pending ACK has now been sent.
908
         */
909
        if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
910
                tp->rcv_adv = tp->rcv_nxt + win;
911
        tp->last_ack_sent = tp->rcv_nxt;
912
        tp->t_flags &= ~TF_ACKNOW;
913
        if (tcp_delack_enabled)
914
                callout_stop(tp->tt_delack);
915
        if (sendalot && (!tcp_do_newreno || --maxburst))
916
                goto again;
917
        return (0);
918
}
919
 
920
void
921
tcp_setpersist(tp)
922
        register struct tcpcb *tp;
923
{
924
        int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
925
        int tt;
926
 
927
        if (callout_active(tp->tt_rexmt))
928
                panic("tcp_setpersist: retransmit pending");
929
        /*
930
         * Start/restart persistance timer.
931
         */
932
        TCPT_RANGESET(tt, t * tcp_backoff[tp->t_rxtshift],
933
                      TCPTV_PERSMIN, TCPTV_PERSMAX);
934
        callout_reset(tp->tt_persist, tt, tcp_timer_persist, tp);
935
        if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
936
                tp->t_rxtshift++;
937
}

powered by: WebSVN 2.1.0

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