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