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