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