]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/tcp_output.c
xnu-792.22.5.tar.gz
[apple/xnu.git] / bsd / netinet / tcp_output.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
8f6c56a5 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
8f6c56a5
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
8ad349bb 24 * limitations under the License.
8f6c56a5
A
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
9bccf70c 61 * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $
1c79356b
A
62 */
63
1c79356b
A
64#define _IP_VHL
65
1c79356b
A
66
67#include <sys/param.h>
68#include <sys/systm.h>
9bccf70c
A
69#include <sys/kernel.h>
70#include <sys/sysctl.h>
1c79356b
A
71#include <sys/mbuf.h>
72#include <sys/domain.h>
73#include <sys/protosw.h>
74#include <sys/socket.h>
75#include <sys/socketvar.h>
76
77#include <net/route.h>
78
79#include <netinet/in.h>
80#include <netinet/in_systm.h>
81#include <netinet/ip.h>
9bccf70c 82#include <netinet/in_pcb.h>
1c79356b
A
83#include <netinet/ip_var.h>
84#if INET6
9bccf70c 85#include <netinet6/in6_pcb.h>
1c79356b 86#include <netinet/ip6.h>
1c79356b
A
87#include <netinet6/ip6_var.h>
88#endif
1c79356b
A
89#include <netinet/tcp.h>
90#define TCPOUTFLAGS
91#include <netinet/tcp_fsm.h>
92#include <netinet/tcp_seq.h>
93#include <netinet/tcp_timer.h>
94#include <netinet/tcp_var.h>
95#include <netinet/tcpip.h>
96#if TCPDEBUG
97#include <netinet/tcp_debug.h>
98#endif
99#include <sys/kdebug.h>
100
9bccf70c
A
101#if IPSEC
102#include <netinet6/ipsec.h>
103#endif /*IPSEC*/
fa4905b1 104
1c79356b
A
105#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1)
106#define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3)
107#define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1)
108
109
110#ifdef notyet
111extern struct mbuf *m_copypack();
112#endif
113
9bccf70c
A
114static int path_mtu_discovery = 1;
115SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW,
116 &path_mtu_discovery, 1, "Enable Path MTU Discovery");
117
118int ss_fltsz = 1;
119SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW,
120 &ss_fltsz, 1, "Slow start flight size");
121
d12e1678 122int ss_fltsz_local = 4; /* starts with four segments max */
9bccf70c
A
123SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW,
124 &ss_fltsz_local, 1, "Slow start flight size for local networks");
125
126int tcp_do_newreno = 0;
127SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno,
128 0, "Enable NewReno Algorithms");
129
91447636
A
130int tcp_packet_chaining = 50;
131SYSCTL_INT(_net_inet_tcp, OID_AUTO, packetchain, CTLFLAG_RW, &tcp_packet_chaining,
132 0, "Enable TCP output packet chaining");
133
134struct mbuf *m_copym_with_hdrs(struct mbuf*, int, int, int, struct mbuf**, int*);
135static long packchain_newlist = 0;
136static long packchain_looped = 0;
137static long packchain_sent = 0;
9bccf70c
A
138
139
140/* temporary: for testing */
141#if IPSEC
142extern int ipsec_bypass;
143#endif
1c79356b 144
d12e1678 145extern int slowlink_wsize; /* window correction for slow links */
55e303ae 146extern u_long route_generation;
91447636
A
147extern int fw_enable; /* firewall is on: disable packet chaining */
148extern int ipsec_bypass;
55e303ae 149
91447636
A
150extern vm_size_t so_cache_zone_element_size;
151
152static __inline__ u_int16_t
153get_socket_id(struct socket * s)
154{
155 u_int16_t val;
156
157 if (so_cache_zone_element_size == 0) {
158 return (0);
159 }
160 val = (u_int16_t)(((u_int32_t)s) / so_cache_zone_element_size);
161 if (val == 0) {
162 val = 0xffff;
163 }
164 return (val);
165}
d12e1678 166
1c79356b
A
167/*
168 * Tcp output routine: figure out what should be sent and send it.
169 */
170int
8ad349bb 171tcp_output(struct tcpcb *tp)
1c79356b 172{
8ad349bb
A
173 struct socket *so = tp->t_inpcb->inp_socket;
174 long len, recwin, sendwin;
1c79356b
A
175 int off, flags, error;
176 register struct mbuf *m;
177 struct ip *ip = NULL;
9bccf70c 178 register struct ipovly *ipov = NULL;
1c79356b
A
179#if INET6
180 struct ip6_hdr *ip6 = NULL;
181#endif /* INET6 */
9bccf70c 182 register struct tcphdr *th;
1c79356b
A
183 u_char opt[TCP_MAXOLEN];
184 unsigned ipoptlen, optlen, hdrlen;
91447636 185 int idle, sendalot, howmuchsent = 0;
8ad349bb
A
186 int i, sack_rxmit;
187 int sack_bytes_rxmt;
188 struct sackhole *p;
189
9bccf70c 190 int maxburst = TCP_MAXBURST;
1c79356b
A
191 struct rmxp_tao *taop;
192 struct rmxp_tao tao_noncached;
55e303ae 193 int last_off = 0;
fa4905b1
A
194 int m_off;
195 struct mbuf *m_last = 0;
196 struct mbuf *m_head = 0;
91447636
A
197 struct mbuf *packetlist = 0;
198 struct mbuf *lastpacket = 0;
9bccf70c 199#if INET6
55e303ae 200 int isipv6 = tp->t_inpcb->inp_vflag & INP_IPV6 ;
9bccf70c 201#endif
91447636
A
202 short packchain_listadd = 0;
203 u_int16_t socket_id = get_socket_id(so);
fa4905b1 204
55e303ae 205
1c79356b
A
206 /*
207 * Determine length of data that should be transmitted,
208 * and flags that will be used.
209 * If there is some data or critical controls (SYN, RST)
210 * to send, then transmit; otherwise, investigate further.
211 */
8ad349bb 212 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
9bccf70c 213 if (idle && tp->t_rcvtime >= tp->t_rxtcur) {
1c79356b
A
214 /*
215 * We have been idle for "a while" and no acks are
216 * expected to clock out any data we send --
217 * slow start to get ack "clock" running again.
8ad349bb 218 *
9bccf70c
A
219 * Set the slow-start flight size depending on whether
220 * this is a local network or not.
8ad349bb 221 */
9bccf70c
A
222 if (
223#if INET6
224 (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) ||
225 (!isipv6 &&
226#endif
227 in_localaddr(tp->t_inpcb->inp_faddr)
228#if INET6
229 )
230#endif
231 )
232 tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local;
233 else
234 tp->snd_cwnd = tp->t_maxseg * ss_fltsz;
235 }
8ad349bb
A
236 tp->t_flags &= ~TF_LASTIDLE;
237 if (idle) {
238 if (tp->t_flags & TF_MORETOCOME) {
239 tp->t_flags |= TF_LASTIDLE;
240 idle = 0;
241 }
242 }
1c79356b 243again:
55e303ae
A
244 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0);
245
246#if INET6
247 if (isipv6) {
248
249 KERNEL_DEBUG(DBG_LAYER_BEG,
250 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
251 (((tp->t_inpcb->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
252 (tp->t_inpcb->in6p_faddr.s6_addr16[0] & 0xffff)),
253 sendalot,0,0);
254 }
255 else
256#endif
257
258 {
259 KERNEL_DEBUG(DBG_LAYER_BEG,
260 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
261 (((tp->t_inpcb->inp_laddr.s_addr & 0xffff) << 16) |
262 (tp->t_inpcb->inp_faddr.s_addr & 0xffff)),
263 sendalot,0,0);
264 /*
265 * If the route generation id changed, we need to check that our
266 * local (source) IP address is still valid. If it isn't either
267 * return error or silently do nothing (assuming the address will
268 * come back before the TCP connection times out).
269 */
270
ab86ba33
A
271 if ((tp->t_inpcb->inp_route.ro_rt != NULL &&
272 (tp->t_inpcb->inp_route.ro_rt->generation_id != route_generation)) || (tp->t_inpcb->inp_route.ro_rt == NULL)) {
55e303ae 273 /* check that the source address is still valid */
91447636 274 if (ifa_foraddr(tp->t_inpcb->inp_laddr.s_addr) == 0) {
55e303ae
A
275 if (tp->t_state >= TCPS_CLOSE_WAIT) {
276 tcp_close(tp);
277 return(EADDRNOTAVAIL);
278 }
279
280 /* set Retransmit timer if it wasn't set
281 * reset Persist timer and shift register as the
282 * adversed peer window may not be valid anymore
283 */
284
285 if (!tp->t_timer[TCPT_REXMT]) {
286 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
287 if (tp->t_timer[TCPT_PERSIST]) {
288 tp->t_timer[TCPT_PERSIST] = 0;
289 tp->t_rxtshift = 0;
290 }
291 }
292
91447636
A
293 if (packetlist) {
294 error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
295 (so->so_options & SO_DONTROUTE), 0);
296 tp->t_lastchain = 0;
297 }
55e303ae
A
298 if (so->so_flags & SOF_NOADDRAVAIL)
299 return(EADDRNOTAVAIL);
300 else
301 return(0); /* silently ignore and keep data in socket */
302 }
55e303ae
A
303 }
304 }
8ad349bb
A
305
306 /*
307 * If we've recently taken a timeout, snd_max will be greater than
308 * snd_nxt. There may be SACK information that allows us to avoid
309 * resending already delivered data. Adjust snd_nxt accordingly.
310 */
311 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
312 tcp_sack_adjust(tp);
1c79356b
A
313 sendalot = 0;
314 off = tp->snd_nxt - tp->snd_una;
8ad349bb
A
315 sendwin = min(tp->snd_wnd, tp->snd_cwnd);
316
d12e1678 317 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0)
8ad349bb 318 sendwin = min(sendwin, slowlink_wsize);
1c79356b
A
319
320 flags = tcp_outflags[tp->t_state];
8ad349bb
A
321 /*
322 * Send any SACK-generated retransmissions. If we're explicitly trying
323 * to send out new data (when sendalot is 1), bypass this function.
324 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
325 * we're replacing a (future) new transmission with a retransmission
326 * now, and we previously incremented snd_cwnd in tcp_input().
327 */
328 /*
329 * Still in sack recovery , reset rxmit flag to zero.
330 */
331 sack_rxmit = 0;
332 sack_bytes_rxmt = 0;
333 len = 0;
334 p = NULL;
335 if (tp->sack_enable && IN_FASTRECOVERY(tp) &&
336 (p = tcp_sack_output(tp, &sack_bytes_rxmt))) {
337 long cwin;
338
339 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
340 if (cwin < 0)
341 cwin = 0;
342 /* Do not retransmit SACK segments beyond snd_recover */
343 if (SEQ_GT(p->end, tp->snd_recover)) {
344 /*
345 * (At least) part of sack hole extends beyond
346 * snd_recover. Check to see if we can rexmit data
347 * for this hole.
348 */
349 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
350 /*
351 * Can't rexmit any more data for this hole.
352 * That data will be rexmitted in the next
353 * sack recovery episode, when snd_recover
354 * moves past p->rxmit.
355 */
356 p = NULL;
357 goto after_sack_rexmit;
358 } else
359 /* Can rexmit part of the current hole */
360 len = ((long)ulmin(cwin,
361 tp->snd_recover - p->rxmit));
362 } else
363 len = ((long)ulmin(cwin, p->end - p->rxmit));
364 off = p->rxmit - tp->snd_una;
365 if (len > 0) {
366 sack_rxmit = 1;
367 sendalot = 1;
368 tcpstat.tcps_sack_rexmits++;
369 tcpstat.tcps_sack_rexmit_bytes +=
370 min(len, tp->t_maxseg);
371 }
372 }
373after_sack_rexmit:
1c79356b
A
374 /*
375 * Get standard flags, and add SYN or FIN if requested by 'hidden'
376 * state flags.
377 */
378 if (tp->t_flags & TF_NEEDFIN)
379 flags |= TH_FIN;
380 if (tp->t_flags & TF_NEEDSYN)
381 flags |= TH_SYN;
382
383 /*
384 * If in persist timeout with window of 0, send 1 byte.
385 * Otherwise, if window is small but nonzero
386 * and timer expired, we will send what we can
387 * and go to transmit state.
388 */
389 if (tp->t_force) {
8ad349bb 390 if (sendwin == 0) {
1c79356b
A
391 /*
392 * If we still have some data to send, then
393 * clear the FIN bit. Usually this would
394 * happen below when it realizes that we
395 * aren't sending all the data. However,
396 * if we have exactly 1 byte of unsent data,
397 * then it won't clear the FIN bit below,
398 * and if we are in persist state, we wind
399 * up sending the packet without recording
400 * that we sent the FIN bit.
401 *
402 * We can't just blindly clear the FIN bit,
403 * because if we don't have any more data
404 * to send then the probe will be the FIN
405 * itself.
406 */
407 if (off < so->so_snd.sb_cc)
408 flags &= ~TH_FIN;
8ad349bb 409 sendwin = 1;
1c79356b
A
410 } else {
411 tp->t_timer[TCPT_PERSIST] = 0;
412 tp->t_rxtshift = 0;
413 }
414 }
415
8ad349bb
A
416 /*
417 * If snd_nxt == snd_max and we have transmitted a FIN, the
418 * offset will be > 0 even if so_snd.sb_cc is 0, resulting in
419 * a negative length. This can also occur when TCP opens up
420 * its congestion window while receiving additional duplicate
421 * acks after fast-retransmit because TCP will reset snd_nxt
422 * to snd_max after the fast-retransmit.
423 *
424 * In the normal retransmit-FIN-only case, however, snd_nxt will
425 * be set to snd_una, the offset will be 0, and the length may
426 * wind up 0.
427 *
428 * If sack_rxmit is true we are retransmitting from the scoreboard
429 * in which case len is already set.
430 */
431 if (sack_rxmit == 0) {
432 if (sack_bytes_rxmt == 0)
433 len = ((long)ulmin(so->so_snd.sb_cc, sendwin) - off);
434 else {
435 long cwin;
436
437 /*
438 * We are inside of a SACK recovery episode and are
439 * sending new data, having retransmitted all the
440 * data possible in the scoreboard.
441 */
442 len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
443 - off);
444 /*
445 * Don't remove this (len > 0) check !
446 * We explicitly check for len > 0 here (although it
447 * isn't really necessary), to work around a gcc
448 * optimization issue - to force gcc to compute
449 * len above. Without this check, the computation
450 * of len is bungled by the optimizer.
451 */
452 if (len > 0) {
453 cwin = tp->snd_cwnd -
454 (tp->snd_nxt - tp->sack_newdata) -
455 sack_bytes_rxmt;
456 if (cwin < 0)
457 cwin = 0;
458 len = lmin(len, cwin);
459 }
460 }
1c79356b
A
461 }
462
463 /*
464 * Lop off SYN bit if it has already been sent. However, if this
465 * is SYN-SENT state and if segment contains data and if we don't
466 * know that foreign host supports TAO, suppress sending segment.
467 */
468 if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) {
469 flags &= ~TH_SYN;
470 off--, len++;
8ad349bb 471 if (len > 0 && tp->t_state == TCPS_SYN_SENT) {
91447636
A
472 if (packetlist) {
473 error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
474 (so->so_options & SO_DONTROUTE), 0);
475 tp->t_lastchain = 0;
476 }
1c79356b
A
477 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
478 return 0;
479 }
480 }
481
482 /*
8ad349bb 483 * Be careful not to send data and/or FIN on SYN segments.
1c79356b
A
484 * This measure is needed to prevent interoperability problems
485 * with not fully conformant TCP implementations.
486 */
8ad349bb 487 if ((flags & TH_SYN) && (tp->t_flags & TF_NOOPT)) {
1c79356b
A
488 len = 0;
489 flags &= ~TH_FIN;
490 }
491
492 if (len < 0) {
493 /*
494 * If FIN has been sent but not acked,
495 * but we haven't been called to retransmit,
8ad349bb 496 * len will be < 0. Otherwise, window shrank
1c79356b
A
497 * after we sent into it. If window shrank to 0,
498 * cancel pending retransmit, pull snd_nxt back
499 * to (closed) window, and set the persist timer
500 * if it isn't already going. If the window didn't
501 * close completely, just wait for an ACK.
502 */
503 len = 0;
8ad349bb 504 if (sendwin == 0) {
1c79356b
A
505 tp->t_timer[TCPT_REXMT] = 0;
506 tp->t_rxtshift = 0;
507 tp->snd_nxt = tp->snd_una;
508 if (tp->t_timer[TCPT_PERSIST] == 0)
509 tcp_setpersist(tp);
510 }
511 }
8ad349bb
A
512
513 /*
514 * len will be >= 0 after this point. Truncate to the maximum
515 * segment length and ensure that FIN is removed if the length
516 * no longer contains the last data byte.
517 */
1c79356b
A
518 if (len > tp->t_maxseg) {
519 len = tp->t_maxseg;
91447636 520 howmuchsent += len;
1c79356b
A
521 sendalot = 1;
522 }
8ad349bb
A
523 if (sack_rxmit) {
524 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
525 flags &= ~TH_FIN;
526 } else {
527 if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc))
528 flags &= ~TH_FIN;
529 }
1c79356b 530
d12e1678 531 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0 ) /* Clips window size for slow links */
8ad349bb 532 recwin = min(sbspace(&so->so_rcv), slowlink_wsize);
d12e1678 533 else
8ad349bb 534 recwin = sbspace(&so->so_rcv);
1c79356b
A
535
536 /*
8ad349bb
A
537 * Sender silly window avoidance. We transmit under the following
538 * conditions when len is non-zero:
539 *
540 * - We have a full segment
541 * - This is the last buffer in a write()/send() and we are
542 * either idle or running NODELAY
543 * - we've timed out (e.g. persist timer)
544 * - we have more then 1/2 the maximum send window's worth of
545 * data (receiver may be limited the window size)
546 * - we need to retransmit
1c79356b
A
547 */
548 if (len) {
549 if (len == tp->t_maxseg)
550 goto send;
551 if (!(tp->t_flags & TF_MORETOCOME) &&
552 (idle || tp->t_flags & TF_NODELAY) &&
553 (tp->t_flags & TF_NOPUSH) == 0 &&
554 len + off >= so->so_snd.sb_cc)
555 goto send;
556 if (tp->t_force)
557 goto send;
558 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
559 goto send;
8ad349bb
A
560 if (SEQ_LT(tp->snd_nxt, tp->snd_max)) /* retransmit case */
561 goto send;
562 if (sack_rxmit)
1c79356b
A
563 goto send;
564 }
565
566 /*
567 * Compare available window to amount of window
568 * known to peer (as advertised window less
569 * next expected input). If the difference is at least two
570 * max size segments, or at least 50% of the maximum possible
571 * window, then want to send a window update to peer.
8ad349bb 572 * Skip this if the connection is in T/TCP half-open state.
1c79356b 573 */
8ad349bb 574 if (recwin > 0 && !(tp->t_flags & TF_NEEDSYN)) {
1c79356b
A
575 /*
576 * "adv" is the amount we can increase the window,
577 * taking into account that we are limited by
578 * TCP_MAXWIN << tp->rcv_scale.
579 */
8ad349bb 580 long adv = min(recwin, (long)TCP_MAXWIN << tp->rcv_scale) -
1c79356b
A
581 (tp->rcv_adv - tp->rcv_nxt);
582
583 if (adv >= (long) (2 * tp->t_maxseg))
584 goto send;
585 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
586 goto send;
587 }
588
589 /*
8ad349bb
A
590 * Send if we owe the peer an ACK, RST, SYN, or urgent data. ACKNOW
591 * is also a catch-all for the retransmit timer timeout case.
1c79356b
A
592 */
593 if (tp->t_flags & TF_ACKNOW)
594 goto send;
595 if ((flags & TH_RST) ||
596 ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0))
597 goto send;
598 if (SEQ_GT(tp->snd_up, tp->snd_una))
599 goto send;
600 /*
601 * If our state indicates that FIN should be sent
8ad349bb 602 * and we have not yet done so, then we need to send.
1c79356b
A
603 */
604 if (flags & TH_FIN &&
605 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
606 goto send;
8ad349bb
A
607 /*
608 * In SACK, it is possible for tcp_output to fail to send a segment
609 * after the retransmission timer has been turned off. Make sure
610 * that the retransmission timer is set.
611 */
612 if (tp->sack_enable && SEQ_GT(tp->snd_max, tp->snd_una) &&
613 tp->t_timer[TCPT_REXMT] == 0 &&
614 tp->t_timer[TCPT_PERSIST] == 0) {
615 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
616 goto just_return;
617 }
1c79356b
A
618 /*
619 * TCP window updates are not reliable, rather a polling protocol
620 * using ``persist'' packets is used to insure receipt of window
621 * updates. The three ``states'' for the output side are:
622 * idle not doing retransmits or persists
623 * persisting to move a small or zero window
624 * (re)transmitting and thereby not persisting
625 *
626 * tp->t_timer[TCPT_PERSIST]
627 * is set when we are in persist state.
628 * tp->t_force
629 * is set when we are called to send a persist packet.
630 * tp->t_timer[TCPT_REXMT]
631 * is set when we are retransmitting
632 * The output side is idle when both timers are zero.
633 *
634 * If send window is too small, there is data to transmit, and no
635 * retransmit or persist is pending, then go to persist state.
636 * If nothing happens soon, send when timer expires:
637 * if window is nonzero, transmit what we can,
638 * otherwise force out a byte.
639 */
640 if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 &&
641 tp->t_timer[TCPT_PERSIST] == 0) {
642 tp->t_rxtshift = 0;
643 tcp_setpersist(tp);
644 }
8ad349bb 645just_return:
1c79356b 646 /*
91447636
A
647 * If there is no reason to send a segment, just return.
648 * but if there is some packets left in the packet list, send them now.
1c79356b 649 */
91447636
A
650 if (packetlist) {
651 error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
652 (so->so_options & SO_DONTROUTE), 0);
653 }
1c79356b
A
654 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
655 return (0);
656
657send:
658 /*
659 * Before ESTABLISHED, force sending of initial options
660 * unless TCP set not to do any options.
661 * NOTE: we assume that the IP/TCP header plus TCP options
662 * always fit in a single mbuf, leaving room for a maximum
663 * link header, i.e.
9bccf70c 664 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1c79356b
A
665 */
666 optlen = 0;
667#if INET6
668 if (isipv6)
9bccf70c 669 hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr);
1c79356b
A
670 else
671#endif
672 hdrlen = sizeof (struct tcpiphdr);
673 if (flags & TH_SYN) {
674 tp->snd_nxt = tp->iss;
675 if ((tp->t_flags & TF_NOOPT) == 0) {
676 u_short mss;
677
678 opt[0] = TCPOPT_MAXSEG;
679 opt[1] = TCPOLEN_MAXSEG;
9bccf70c 680 mss = htons((u_short) tcp_mssopt(tp));
1c79356b
A
681 (void)memcpy(opt + 2, &mss, sizeof(mss));
682 optlen = TCPOLEN_MAXSEG;
683
684 if ((tp->t_flags & TF_REQ_SCALE) &&
685 ((flags & TH_ACK) == 0 ||
686 (tp->t_flags & TF_RCVD_SCALE))) {
687 *((u_int32_t *)(opt + optlen)) = htonl(
688 TCPOPT_NOP << 24 |
689 TCPOPT_WINDOW << 16 |
690 TCPOLEN_WINDOW << 8 |
691 tp->request_r_scale);
692 optlen += 4;
693 }
694 }
695 }
696
697 /*
698 * Send a timestamp and echo-reply if this is a SYN and our side
699 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
700 * and our peer have sent timestamps in our SYN's.
701 */
702 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
703 (flags & TH_RST) == 0 &&
704 ((flags & TH_ACK) == 0 ||
705 (tp->t_flags & TF_RCVD_TSTMP))) {
706 u_int32_t *lp = (u_int32_t *)(opt + optlen);
707
708 /* Form timestamp option as shown in appendix A of RFC 1323. */
709 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
710 *lp++ = htonl(tcp_now);
711 *lp = htonl(tp->ts_recent);
712 optlen += TCPOLEN_TSTAMP_APPA;
713 }
714
8ad349bb
A
715 if (tp->sack_enable && ((tp->t_flags & TF_NOOPT) == 0)) {
716 /*
717 * Tack on the SACK permitted option *last*.
718 * And do padding of options after tacking this on.
719 * This is because of MSS, TS, WinScale and Signatures are
720 * all present, we have just 2 bytes left for the SACK
721 * permitted option, which is just enough.
1c79356b 722 */
1c79356b 723 /*
8ad349bb
A
724 * If this is the first SYN of connection (not a SYN
725 * ACK), include SACK permitted option. If this is a
726 * SYN ACK, include SACK permitted option if peer has
727 * already done so. This is only for active connect,
728 * since the syncache takes care of the passive connect.
1c79356b 729 */
8ad349bb
A
730 if ((flags & TH_SYN) &&
731 (!(flags & TH_ACK) || (tp->t_flags & TF_SACK_PERMIT))) {
732 u_char *bp;
733 bp = (u_char *)opt + optlen;
734
735 *bp++ = TCPOPT_SACK_PERMITTED;
736 *bp++ = TCPOLEN_SACK_PERMITTED;
737 optlen += TCPOLEN_SACK_PERMITTED;
738 }
1c79356b
A
739
740 /*
8ad349bb
A
741 * Send SACKs if necessary. This should be the last
742 * option processed. Only as many SACKs are sent as
743 * are permitted by the maximum options size.
744 *
745 * In general, SACK blocks consume 8*n+2 bytes.
746 * So a full size SACK blocks option is 34 bytes
747 * (to generate 4 SACK blocks). At a minimum,
748 * we need 10 bytes (to generate 1 SACK block).
749 * If TCP Timestamps (12 bytes) and TCP Signatures
750 * (18 bytes) are both present, we'll just have
751 * 10 bytes for SACK options 40 - (12 + 18).
1c79356b 752 */
8ad349bb
A
753 if (TCPS_HAVEESTABLISHED(tp->t_state) &&
754 (tp->t_flags & TF_SACK_PERMIT) && tp->rcv_numsacks > 0 &&
755 MAX_TCPOPTLEN - optlen - 2 >= TCPOLEN_SACK) {
756 int nsack, sackoptlen, padlen;
757 u_char *bp = (u_char *)opt + optlen;
758 u_int32_t *lp;
1c79356b 759
8ad349bb
A
760 nsack = (MAX_TCPOPTLEN - optlen - 2) / TCPOLEN_SACK;
761 nsack = min(nsack, tp->rcv_numsacks);
762 sackoptlen = (2 + nsack * TCPOLEN_SACK);
763
764 /*
765 * First we need to pad options so that the
766 * SACK blocks can start at a 4-byte boundary
767 * (sack option and length are at a 2 byte offset).
768 */
769 padlen = (MAX_TCPOPTLEN - optlen - sackoptlen) % 4;
770 optlen += padlen;
771 while (padlen-- > 0)
772 *bp++ = TCPOPT_NOP;
773
774 tcpstat.tcps_sack_send_blocks++;
775 *bp++ = TCPOPT_SACK;
776 *bp++ = sackoptlen;
777 lp = (u_int32_t *)bp;
778 for (i = 0; i < nsack; i++) {
779 struct sackblk sack = tp->sackblks[i];
780 *lp++ = htonl(sack.start);
781 *lp++ = htonl(sack.end);
1c79356b 782 }
8ad349bb 783 optlen += sackoptlen;
1c79356b 784 }
8ad349bb
A
785 }
786
787 /* Pad TCP options to a 4 byte boundary */
788 if (optlen < MAX_TCPOPTLEN && (optlen % sizeof(u_int32_t))) {
789 int pad = sizeof(u_int32_t) - (optlen % sizeof(u_int32_t));
790 u_char *bp = (u_char *)opt + optlen;
791
792 optlen += pad;
793 while (pad) {
794 *bp++ = TCPOPT_EOL;
795 pad--;
796 }
797 }
798
799 hdrlen += optlen;
1c79356b 800
1c79356b
A
801#if INET6
802 if (isipv6)
803 ipoptlen = ip6_optlen(tp->t_inpcb);
804 else
805#endif
9bccf70c
A
806 {
807 if (tp->t_inpcb->inp_options) {
808 ipoptlen = tp->t_inpcb->inp_options->m_len -
1c79356b 809 offsetof(struct ipoption, ipopt_list);
8ad349bb 810 } else
9bccf70c 811 ipoptlen = 0;
1c79356b
A
812 }
813#if IPSEC
9bccf70c
A
814 if (ipsec_bypass == 0)
815 ipoptlen += ipsec_hdrsiz_tcp(tp);
1c79356b
A
816#endif
817
818 /*
819 * Adjust data length if insertion of options will
820 * bump the packet length beyond the t_maxopd length.
821 * Clear the FIN bit because we cut off the tail of
822 * the segment.
823 */
824 if (len + optlen + ipoptlen > tp->t_maxopd) {
825 /*
826 * If there is still more to send, don't close the connection.
827 */
828 flags &= ~TH_FIN;
829 len = tp->t_maxopd - optlen - ipoptlen;
91447636 830 howmuchsent += len;
1c79356b
A
831 sendalot = 1;
832 }
833
834/*#ifdef DIAGNOSTIC*/
9bccf70c
A
835#if INET6
836 if (max_linkhdr + hdrlen > MCLBYTES)
837 panic("tcphdr too big");
838#else
1c79356b
A
839 if (max_linkhdr + hdrlen > MHLEN)
840 panic("tcphdr too big");
9bccf70c 841#endif
1c79356b
A
842/*#endif*/
843
844 /*
845 * Grab a header mbuf, attaching a copy of data to
846 * be transmitted, and initialize the header from
847 * the template for sends on this connection.
848 */
849 if (len) {
850 if (tp->t_force && len == 1)
851 tcpstat.tcps_sndprobe++;
852 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
853 tcpstat.tcps_sndrexmitpack++;
854 tcpstat.tcps_sndrexmitbyte += len;
855 } else {
856 tcpstat.tcps_sndpack++;
857 tcpstat.tcps_sndbyte += len;
858 }
859#ifdef notyet
860 if ((m = m_copypack(so->so_snd.sb_mb, off,
861 (int)len, max_linkhdr + hdrlen)) == 0) {
862 error = ENOBUFS;
863 goto out;
864 }
865 /*
866 * m_copypack left space for our hdr; use it.
867 */
868 m->m_len += hdrlen;
869 m->m_data -= hdrlen;
870#else
9bccf70c
A
871 /*
872 * try to use the new interface that allocates all
873 * the necessary mbuf hdrs under 1 mbuf lock and
874 * avoids rescanning the socket mbuf list if
875 * certain conditions are met. This routine can't
876 * be used in the following cases...
877 * 1) the protocol headers exceed the capacity of
878 * of a single mbuf header's data area (no cluster attached)
879 * 2) the length of the data being transmitted plus
880 * the protocol headers fits into a single mbuf header's
881 * data area (no cluster attached)
882 */
fa4905b1 883 m = NULL;
1c79356b
A
884#if INET6
885 if (MHLEN < hdrlen + max_linkhdr) {
fa4905b1
A
886 MGETHDR(m, M_DONTWAIT, MT_HEADER);
887 if (m == NULL) {
888 error = ENOBUFS;
889 goto out;
890 }
1c79356b
A
891 MCLGET(m, M_DONTWAIT);
892 if ((m->m_flags & M_EXT) == 0) {
893 m_freem(m);
894 error = ENOBUFS;
895 goto out;
896 }
fa4905b1
A
897 m->m_data += max_linkhdr;
898 m->m_len = hdrlen;
1c79356b
A
899 }
900#endif
1c79356b 901 if (len <= MHLEN - hdrlen - max_linkhdr) {
fa4905b1
A
902 if (m == NULL) {
903 MGETHDR(m, M_DONTWAIT, MT_HEADER);
904 if (m == NULL) {
905 error = ENOBUFS;
906 goto out;
907 }
908 m->m_data += max_linkhdr;
909 m->m_len = hdrlen;
910 }
55e303ae
A
911 /* makes sure we still have data left to be sent at this point */
912 if (so->so_snd.sb_mb == NULL || off == -1) {
913 if (m != NULL) m_freem(m);
914 error = 0; /* should we return an error? */
915 goto out;
916 }
1c79356b
A
917 m_copydata(so->so_snd.sb_mb, off, (int) len,
918 mtod(m, caddr_t) + hdrlen);
919 m->m_len += len;
920 } else {
fa4905b1
A
921 if (m != NULL) {
922 m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len);
923 if (m->m_next == 0) {
924 (void) m_free(m);
925 error = ENOBUFS;
926 goto out;
927 }
928 } else {
9bccf70c
A
929 /*
930 * determine whether the mbuf pointer and offset passed back by the 'last' call
931 * to m_copym_with_hdrs are still valid... if the head of the socket chain has
932 * changed (due to an incoming ACK for instance), or the offset into the chain we
933 * just computed is different from the one last returned by m_copym_with_hdrs (perhaps
934 * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and
935 * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow
936 * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list.
937 * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism.
938 */
fa4905b1
A
939 if (m_head != so->so_snd.sb_mb || last_off != off)
940 m_last = NULL;
941 last_off = off + len;
942 m_head = so->so_snd.sb_mb;
55e303ae
A
943
944 /* makes sure we still have data left to be sent at this point */
945 if (m_head == NULL) {
946 error = 0; /* should we return an error? */
947 goto out;
948 }
949
9bccf70c
A
950 /*
951 * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that
952 * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not
953 */
fa4905b1
A
954 if ((m = m_copym_with_hdrs(so->so_snd.sb_mb, off, (int) len, M_DONTWAIT, &m_last, &m_off)) == NULL) {
955 error = ENOBUFS;
956 goto out;
957 }
958 m->m_data += max_linkhdr;
959 m->m_len = hdrlen;
1c79356b
A
960 }
961 }
962#endif
963 /*
964 * If we're sending everything we've got, set PUSH.
965 * (This will keep happy those implementations which only
966 * give data to the user when a buffer fills or
967 * a PUSH comes in.)
968 */
969 if (off + len == so->so_snd.sb_cc)
970 flags |= TH_PUSH;
971 } else {
972 if (tp->t_flags & TF_ACKNOW)
973 tcpstat.tcps_sndacks++;
974 else if (flags & (TH_SYN|TH_FIN|TH_RST))
975 tcpstat.tcps_sndctrl++;
976 else if (SEQ_GT(tp->snd_up, tp->snd_una))
977 tcpstat.tcps_sndurg++;
978 else
979 tcpstat.tcps_sndwinup++;
980
981 MGETHDR(m, M_DONTWAIT, MT_HEADER);
982 if (m == NULL) {
983 error = ENOBUFS;
984 goto out;
985 }
986#if INET6
9bccf70c
A
987 if (isipv6 && (MHLEN < hdrlen + max_linkhdr) &&
988 MHLEN >= hdrlen) {
1c79356b
A
989 MH_ALIGN(m, hdrlen);
990 } else
991#endif
992 m->m_data += max_linkhdr;
993 m->m_len = hdrlen;
994 }
91447636 995 m->m_pkthdr.rcvif = 0;
1c79356b
A
996#if INET6
997 if (isipv6) {
998 ip6 = mtod(m, struct ip6_hdr *);
999 th = (struct tcphdr *)(ip6 + 1);
9bccf70c
A
1000 tcp_fillheaders(tp, ip6, th);
1001 } else
1c79356b 1002#endif /* INET6 */
9bccf70c
A
1003 {
1004 ip = mtod(m, struct ip *);
1005 ipov = (struct ipovly *)ip;
1006 th = (struct tcphdr *)(ip + 1);
1007 /* this picks up the pseudo header (w/o the length) */
1008 tcp_fillheaders(tp, ip, th);
1c79356b 1009 }
1c79356b
A
1010
1011 /*
1012 * Fill in fields, remembering maximum advertised
1013 * window for use in delaying messages about window sizes.
1014 * If resending a FIN, be sure not to use a new sequence number.
1015 */
1016 if (flags & TH_FIN && tp->t_flags & TF_SENTFIN &&
1017 tp->snd_nxt == tp->snd_max)
1018 tp->snd_nxt--;
1019 /*
1020 * If we are doing retransmissions, then snd_nxt will
1021 * not reflect the first unsent octet. For ACK only
1022 * packets, we do not want the sequence number of the
1023 * retransmitted packet, we want the sequence number
1024 * of the next unsent octet. So, if there is no data
1025 * (and no SYN or FIN), use snd_max instead of snd_nxt
1026 * when filling in ti_seq. But if we are in persist
1027 * state, snd_max might reflect one byte beyond the
1028 * right edge of the window, so use snd_nxt in that
1029 * case, since we know we aren't doing a retransmission.
1030 * (retransmit and persist are mutually exclusive...)
1031 */
8ad349bb
A
1032 if (sack_rxmit == 0) {
1033 if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST])
1034 th->th_seq = htonl(tp->snd_nxt);
1035 else
1036 th->th_seq = htonl(tp->snd_max);
1037 } else {
1038 th->th_seq = htonl(p->rxmit);
1039 p->rxmit += len;
1040 tp->sackhint.sack_bytes_rexmit += len;
1041 }
1c79356b
A
1042 th->th_ack = htonl(tp->rcv_nxt);
1043 if (optlen) {
1044 bcopy(opt, th + 1, optlen);
1045 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1046 }
1047 th->th_flags = flags;
1048 /*
1049 * Calculate receive window. Don't shrink window,
1050 * but avoid silly window syndrome.
1051 */
8ad349bb
A
1052 if (recwin < (long)(so->so_rcv.sb_hiwat / 4) && recwin < (long)tp->t_maxseg)
1053 recwin = 0;
1054 if (recwin < (long)(tp->rcv_adv - tp->rcv_nxt))
1055 recwin = (long)(tp->rcv_adv - tp->rcv_nxt);
d12e1678 1056 if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) {
8ad349bb
A
1057 if (recwin > (long)slowlink_wsize)
1058 recwin = slowlink_wsize;
1059 th->th_win = htons((u_short) (recwin>>tp->rcv_scale));
d12e1678
A
1060 }
1061 else {
8ad349bb
A
1062 if (recwin > (long)TCP_MAXWIN << tp->rcv_scale)
1063 recwin = (long)TCP_MAXWIN << tp->rcv_scale;
1064 th->th_win = htons((u_short) (recwin>>tp->rcv_scale));
d12e1678 1065 }
91447636 1066
8ad349bb
A
1067 /*
1068 * Adjust the RXWIN0SENT flag - indicate that we have advertised
1069 * a 0 window. This may cause the remote transmitter to stall. This
1070 * flag tells soreceive() to disable delayed acknowledgements when
1071 * draining the buffer. This can occur if the receiver is attempting
1072 * to read more data then can be buffered prior to transmitting on
1073 * the connection.
1074 */
1075 if (recwin == 0)
1076 tp->t_flags |= TF_RXWIN0SENT;
1077 else
1078 tp->t_flags &= ~TF_RXWIN0SENT;
1c79356b
A
1079 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1080 th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt));
1081 th->th_flags |= TH_URG;
1082 } else
1083 /*
1084 * If no urgent pointer to send, then we pull
1085 * the urgent pointer to the left edge of the send window
1086 * so that it doesn't drift into the send window on sequence
1087 * number wraparound.
1088 */
1089 tp->snd_up = tp->snd_una; /* drag it along */
1090
1091 /*
1092 * Put TCP length in extended header, and then
1093 * checksum extended header and data.
1094 */
9bccf70c 1095 m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */
1c79356b 1096#if INET6
9bccf70c
A
1097 if (isipv6)
1098 /*
1099 * ip6_plen is not need to be filled now, and will be filled
1100 * in ip6_output.
1101 */
1c79356b
A
1102 th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr),
1103 sizeof(struct tcphdr) + optlen + len);
9bccf70c 1104 else
1c79356b 1105#endif /* INET6 */
0b4e3aa0 1106 {
9bccf70c
A
1107 m->m_pkthdr.csum_flags = CSUM_TCP;
1108 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1109 if (len + optlen)
1110 th->th_sum = in_addword(th->th_sum,
1111 htons((u_short)(optlen + len)));
9bccf70c 1112 }
1c79356b
A
1113
1114 /*
1115 * In transmit state, time the transmission and arrange for
1116 * the retransmit. In persist state, just set snd_max.
1117 */
1118 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
1119 tcp_seq startseq = tp->snd_nxt;
1120
1121 /*
1122 * Advance snd_nxt over sequence space of this segment.
1123 */
1124 if (flags & (TH_SYN|TH_FIN)) {
1125 if (flags & TH_SYN)
1126 tp->snd_nxt++;
1127 if (flags & TH_FIN) {
1128 tp->snd_nxt++;
1129 tp->t_flags |= TF_SENTFIN;
1130 }
1131 }
8ad349bb
A
1132 if (sack_rxmit)
1133 goto timer;
1c79356b
A
1134 tp->snd_nxt += len;
1135 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1136 tp->snd_max = tp->snd_nxt;
1137 /*
1138 * Time this transmission if not a retransmission and
1139 * not currently timing anything.
1140 */
9bccf70c
A
1141 if (tp->t_rtttime == 0) {
1142 tp->t_rtttime = 1;
1c79356b
A
1143 tp->t_rtseq = startseq;
1144 tcpstat.tcps_segstimed++;
1145 }
1146 }
1147
1148 /*
1149 * Set retransmit timer if not currently set,
1150 * and not doing an ack or a keep-alive probe.
1151 * Initial value for retransmit timer is smoothed
1152 * round-trip time + 2 * round-trip time variance.
1153 * Initialize shift counter which is used for backoff
1154 * of retransmit time.
1155 */
8ad349bb 1156timer:
1c79356b 1157 if (tp->t_timer[TCPT_REXMT] == 0 &&
8ad349bb
A
1158 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1159 tp->snd_nxt != tp->snd_una)) {
1c79356b
A
1160 if (tp->t_timer[TCPT_PERSIST]) {
1161 tp->t_timer[TCPT_PERSIST] = 0;
1162 tp->t_rxtshift = 0;
1163 }
8ad349bb 1164 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1c79356b 1165 }
8ad349bb
A
1166 } else {
1167 /*
1168 * Persist case, update snd_max but since we are in
1169 * persist mode (no window) we do not update snd_nxt.
1170 */
1171 int xlen = len;
1172 if (flags & TH_SYN)
1173 ++xlen;
1174 if (flags & TH_FIN) {
1175 ++xlen;
1176 tp->t_flags |= TF_SENTFIN;
1177 }
1178 if (SEQ_GT(tp->snd_nxt + xlen, tp->snd_max))
1c79356b 1179 tp->snd_max = tp->snd_nxt + len;
8ad349bb 1180 }
1c79356b
A
1181
1182#if TCPDEBUG
1183 /*
1184 * Trace.
1185 */
9bccf70c
A
1186 if (so->so_options & SO_DEBUG)
1187 tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0);
1188#endif
1c79356b
A
1189
1190 /*
1191 * Fill in IP length and desired time to live and
1192 * send to IP level. There should be a better way
1193 * to handle ttl and tos; we could keep them in
1194 * the template, but need a way to checksum without them.
1195 */
9bccf70c
A
1196 /*
1197 * m->m_pkthdr.len should have been set before cksum calcuration,
1198 * because in6_cksum() need it.
1199 */
1c79356b
A
1200#if INET6
1201 if (isipv6) {
9bccf70c 1202 /*
1c79356b
A
1203 * we separately set hoplimit for every segment, since the
1204 * user might want to change the value via setsockopt.
1205 * Also, desired default hop limit might be changed via
1206 * Neighbor Discovery.
1207 */
1208 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb,
1209 tp->t_inpcb->in6p_route.ro_rt ?
1210 tp->t_inpcb->in6p_route.ro_rt->rt_ifp
1211 : NULL);
1212
1213 /* TODO: IPv6 IP6TOS_ECT bit on */
1214#if IPSEC
9bccf70c
A
1215 if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) {
1216 m_freem(m);
1217 error = ENOBUFS;
1218 goto out;
1219 }
1c79356b 1220#endif /*IPSEC*/
91447636 1221 m->m_pkthdr.socket_id = socket_id;
1c79356b
A
1222 error = ip6_output(m,
1223 tp->t_inpcb->in6p_outputopts,
1224 &tp->t_inpcb->in6p_route,
91447636 1225 (so->so_options & SO_DONTROUTE), NULL, NULL, 0);
1c79356b
A
1226 } else
1227#endif /* INET6 */
9bccf70c 1228 {
1c79356b 1229 struct rtentry *rt;
1c79356b
A
1230 ip->ip_len = m->m_pkthdr.len;
1231#if INET6
55e303ae 1232 if (isipv6)
9bccf70c
A
1233 ip->ip_ttl = in6_selecthlim(tp->t_inpcb,
1234 tp->t_inpcb->in6p_route.ro_rt ?
1235 tp->t_inpcb->in6p_route.ro_rt->rt_ifp
1236 : NULL);
1237 else
1c79356b
A
1238#endif /* INET6 */
1239 ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */
1240 ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */
1241
0b4e3aa0 1242
9bccf70c
A
1243#if INET6
1244 if (isipv6) {
1245 KERNEL_DEBUG(DBG_LAYER_BEG,
1246 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
1247 (((tp->t_inpcb->in6p_laddr.s6_addr16[0] & 0xffff) << 16) |
1248 (tp->t_inpcb->in6p_faddr.s6_addr16[0] & 0xffff)),
1249 0,0,0);
1250 }
1251 else
1252#endif
1253 {
1254 KERNEL_DEBUG(DBG_LAYER_BEG,
1255 ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport),
1256 (((tp->t_inpcb->inp_laddr.s_addr & 0xffff) << 16) |
1257 (tp->t_inpcb->inp_faddr.s_addr & 0xffff)),
1258 0,0,0);
1259 }
1260
1c79356b
A
1261 /*
1262 * See if we should do MTU discovery. We do it only if the following
1263 * are true:
1264 * 1) we have a valid route to the destination
1265 * 2) the MTU is not locked (if it is, then discovery has been
1266 * disabled)
1267 */
9bccf70c
A
1268 if (path_mtu_discovery
1269 && (rt = tp->t_inpcb->inp_route.ro_rt)
1c79356b
A
1270 && rt->rt_flags & RTF_UP
1271 && !(rt->rt_rmx.rmx_locks & RTV_MTU)) {
1272 ip->ip_off |= IP_DF;
1273 }
1c79356b 1274#if IPSEC
9bccf70c
A
1275 if (ipsec_bypass == 0)
1276 ipsec_setsocket(m, so);
1c79356b 1277#endif /*IPSEC*/
91447636
A
1278
1279 /*
1280 * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active.
1281 */
1282
1283 m->m_pkthdr.socket_id = socket_id;
1284 if (packetlist) {
1285 m->m_nextpkt = NULL;
1286 lastpacket->m_nextpkt = m;
1287 lastpacket = m;
1288 packchain_listadd++;
1289 }
1290 else {
1291 m->m_nextpkt = NULL;
1292 packchain_newlist++;
1293 packetlist = lastpacket = m;
1294 packchain_listadd=0;
1295 }
1296
1297 if ((ipsec_bypass == 0) || fw_enable || sendalot == 0 || (tp->t_state != TCPS_ESTABLISHED) ||
1298 (tp->snd_cwnd <= (tp->snd_wnd / 4)) ||
1299 (tp->t_flags & (TH_PUSH | TF_ACKNOW)) || tp->t_force != 0 ||
1300 packchain_listadd >= tcp_packet_chaining) {
1301 lastpacket->m_nextpkt = 0;
1302 error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route,
1303 (so->so_options & SO_DONTROUTE), 0);
1304 tp->t_lastchain = packchain_listadd;
1305 packchain_sent++;
1306 packetlist = NULL;
1307 if (error == 0)
1308 howmuchsent = 0;
1309 }
1310 else {
1311 error = 0;
1312 packchain_looped++;
1313 tcpstat.tcps_sndtotal++;
8ad349bb
A
1314 if (recwin > 0 && SEQ_GT(tp->rcv_nxt+recwin, tp->rcv_adv))
1315 tp->rcv_adv = tp->rcv_nxt + recwin;
91447636
A
1316 tp->last_ack_sent = tp->rcv_nxt;
1317 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
1318 goto again;
1319 }
1320 }
1c79356b 1321 if (error) {
9bccf70c
A
1322
1323 /*
1324 * We know that the packet was lost, so back out the
1325 * sequence number advance, if any.
1326 */
8ad349bb 1327 if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) {
9bccf70c
A
1328 /*
1329 * No need to check for TH_FIN here because
1330 * the TF_SENTFIN flag handles that case.
1331 */
8ad349bb
A
1332 if ((flags & TH_SYN) == 0) {
1333 if (sack_rxmit) {
1334 p->rxmit -= howmuchsent;
1335 tp->sackhint.sack_bytes_rexmit -= howmuchsent;
1336 } else
1337 tp->snd_nxt -= howmuchsent;
1338 }
9bccf70c 1339 }
91447636 1340 howmuchsent = 0;
1c79356b
A
1341out:
1342 if (error == ENOBUFS) {
0b4e3aa0
A
1343 if (!tp->t_timer[TCPT_REXMT] &&
1344 !tp->t_timer[TCPT_PERSIST])
1345 tp->t_timer[TCPT_REXMT] = tp->t_rxtcur;
1c79356b 1346 tcp_quench(tp->t_inpcb, 0);
91447636
A
1347 if (packetlist)
1348 m_freem_list(packetlist);
1349 tp->t_lastchain = 0;
1c79356b
A
1350 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
1351 return (0);
1352 }
1c79356b
A
1353 if (error == EMSGSIZE) {
1354 /*
1355 * ip_output() will have already fixed the route
1356 * for us. tcp_mtudisc() will, as its last action,
1357 * initiate retransmission, so it is important to
1358 * not do so here.
1359 */
1360 tcp_mtudisc(tp->t_inpcb, 0);
91447636
A
1361 if (packetlist)
1362 m_freem_list(packetlist);
1363 tp->t_lastchain = 0;
1c79356b
A
1364 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
1365 return 0;
1366 }
1c79356b
A
1367 if ((error == EHOSTUNREACH || error == ENETDOWN)
1368 && TCPS_HAVERCVDSYN(tp->t_state)) {
1369 tp->t_softerror = error;
91447636
A
1370 if (packetlist)
1371 m_freem_list(packetlist);
1372 tp->t_lastchain = 0;
1c79356b
A
1373 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
1374 return (0);
1375 }
91447636
A
1376 if (packetlist)
1377 m_freem_list(packetlist);
1378 tp->t_lastchain = 0;
1c79356b
A
1379 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0);
1380 return (error);
1381 }
91447636 1382sentit:
1c79356b
A
1383 tcpstat.tcps_sndtotal++;
1384
1385 /*
1386 * Data sent (as far as we can tell).
1387 * If this advertises a larger window than any other segment,
1388 * then remember the size of the advertised window.
1389 * Any pending ACK has now been sent.
1390 */
8ad349bb
A
1391 if (recwin > 0 && SEQ_GT(tp->rcv_nxt+recwin, tp->rcv_adv))
1392 tp->rcv_adv = tp->rcv_nxt + recwin;
1c79356b
A
1393 tp->last_ack_sent = tp->rcv_nxt;
1394 tp->t_flags &= ~(TF_ACKNOW|TF_DELACK);
55e303ae 1395
91447636
A
1396 KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END,0,0,0,0,0);
1397 if (sendalot && (!tcp_do_newreno || --maxburst))
1c79356b 1398 goto again;
1c79356b
A
1399 return (0);
1400}
1401
1402void
1403tcp_setpersist(tp)
1404 register struct tcpcb *tp;
1405{
9bccf70c 1406 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1;
1c79356b
A
1407
1408 if (tp->t_timer[TCPT_REXMT])
9bccf70c 1409 panic("tcp_setpersist: retransmit pending");
1c79356b
A
1410 /*
1411 * Start/restart persistance timer.
1412 */
1413 TCPT_RANGESET(tp->t_timer[TCPT_PERSIST],
1414 t * tcp_backoff[tp->t_rxtshift],
1415 TCPTV_PERSMIN, TCPTV_PERSMAX);
1416 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1417 tp->t_rxtshift++;
1418}