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