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