tcp_output.c revision 1.124 1 /* $NetBSD: tcp_output.c,v 1.124 2005/03/07 09:32:51 yamt Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
34 *
35 * NRL grants permission for redistribution and use in source and binary
36 * forms, with or without modification, of the software and documentation
37 * created at NRL provided that the following conditions are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgements:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * This product includes software developed at the Information
49 * Technology Division, US Naval Research Laboratory.
50 * 4. Neither the name of the NRL nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * The views and conclusions contained in the software and documentation
67 * are those of the authors and should not be interpreted as representing
68 * official policies, either expressed or implied, of the US Naval
69 * Research Laboratory (NRL).
70 */
71
72 /*-
73 * Copyright (c) 1997, 1998, 2001, 2005 The NetBSD Foundation, Inc.
74 * All rights reserved.
75 *
76 * This code is derived from software contributed to The NetBSD Foundation
77 * by Jason R. Thorpe and Kevin M. Lahey of the Numerical Aerospace Simulation
78 * Facility, NASA Ames Research Center.
79 * This code is derived from software contributed to The NetBSD Foundation
80 * by Charles M. Hannum.
81 *
82 * Redistribution and use in source and binary forms, with or without
83 * modification, are permitted provided that the following conditions
84 * are met:
85 * 1. Redistributions of source code must retain the above copyright
86 * notice, this list of conditions and the following disclaimer.
87 * 2. Redistributions in binary form must reproduce the above copyright
88 * notice, this list of conditions and the following disclaimer in the
89 * documentation and/or other materials provided with the distribution.
90 * 3. All advertising materials mentioning features or use of this software
91 * must display the following acknowledgement:
92 * This product includes software developed by the NetBSD
93 * Foundation, Inc. and its contributors.
94 * 4. Neither the name of The NetBSD Foundation nor the names of its
95 * contributors may be used to endorse or promote products derived
96 * from this software without specific prior written permission.
97 *
98 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
99 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
100 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
101 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
102 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
103 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
104 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
105 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
106 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
107 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
108 * POSSIBILITY OF SUCH DAMAGE.
109 */
110
111 /*
112 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
113 * The Regents of the University of California. All rights reserved.
114 *
115 * Redistribution and use in source and binary forms, with or without
116 * modification, are permitted provided that the following conditions
117 * are met:
118 * 1. Redistributions of source code must retain the above copyright
119 * notice, this list of conditions and the following disclaimer.
120 * 2. Redistributions in binary form must reproduce the above copyright
121 * notice, this list of conditions and the following disclaimer in the
122 * documentation and/or other materials provided with the distribution.
123 * 3. Neither the name of the University nor the names of its contributors
124 * may be used to endorse or promote products derived from this software
125 * without specific prior written permission.
126 *
127 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
128 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
129 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
130 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
131 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
132 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
133 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
134 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
135 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
136 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
137 * SUCH DAMAGE.
138 *
139 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
140 */
141
142 #include <sys/cdefs.h>
143 __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.124 2005/03/07 09:32:51 yamt Exp $");
144
145 #include "opt_inet.h"
146 #include "opt_ipsec.h"
147 #include "opt_tcp_debug.h"
148
149 #include <sys/param.h>
150 #include <sys/systm.h>
151 #include <sys/malloc.h>
152 #include <sys/mbuf.h>
153 #include <sys/protosw.h>
154 #include <sys/socket.h>
155 #include <sys/socketvar.h>
156 #include <sys/errno.h>
157 #include <sys/domain.h>
158 #include <sys/kernel.h>
159 #ifdef TCP_SIGNATURE
160 #include <sys/md5.h>
161 #endif
162
163 #include <net/if.h>
164 #include <net/route.h>
165
166 #include <netinet/in.h>
167 #include <netinet/in_systm.h>
168 #include <netinet/ip.h>
169 #include <netinet/in_pcb.h>
170 #include <netinet/ip_var.h>
171
172 #ifdef INET6
173 #ifndef INET
174 #include <netinet/in.h>
175 #endif
176 #include <netinet/ip6.h>
177 #include <netinet6/in6_var.h>
178 #include <netinet6/ip6_var.h>
179 #include <netinet6/in6_pcb.h>
180 #include <netinet6/nd6.h>
181 #endif
182
183 #ifdef FAST_IPSEC
184 #include <netipsec/ipsec.h>
185 #include <netipsec/key.h>
186 #endif /* FAST_IPSEC*/
187 #ifdef IPSEC
188 #include <netinet6/ipsec.h>
189 #endif
190
191 #include <netinet/tcp.h>
192 #define TCPOUTFLAGS
193 #include <netinet/tcp_fsm.h>
194 #include <netinet/tcp_seq.h>
195 #include <netinet/tcp_timer.h>
196 #include <netinet/tcp_var.h>
197 #include <netinet/tcpip.h>
198 #include <netinet/tcp_debug.h>
199
200 #ifdef IPSEC
201 #include <netkey/key.h>
202 #endif
203
204 #ifdef notyet
205 extern struct mbuf *m_copypack();
206 #endif
207
208 /*
209 * Knob to enable Congestion Window Monitoring, and control the
210 * the burst size it allows. Default burst is 4 packets, per
211 * the Internet draft.
212 */
213 int tcp_cwm = 0;
214 int tcp_cwm_burstsize = 4;
215
216 #ifdef TCP_OUTPUT_COUNTERS
217 #include <sys/device.h>
218
219 extern struct evcnt tcp_output_bigheader;
220 extern struct evcnt tcp_output_predict_hit;
221 extern struct evcnt tcp_output_predict_miss;
222 extern struct evcnt tcp_output_copysmall;
223 extern struct evcnt tcp_output_copybig;
224 extern struct evcnt tcp_output_refbig;
225
226 #define TCP_OUTPUT_COUNTER_INCR(ev) (ev)->ev_count++
227 #else
228
229 #define TCP_OUTPUT_COUNTER_INCR(ev) /* nothing */
230
231 #endif /* TCP_OUTPUT_COUNTERS */
232
233 static
234 #ifndef GPROF
235 __inline
236 #endif
237 int
238 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep)
239 {
240 #ifdef INET
241 struct inpcb *inp = tp->t_inpcb;
242 #endif
243 #ifdef INET6
244 struct in6pcb *in6p = tp->t_in6pcb;
245 #endif
246 struct socket *so = NULL;
247 struct rtentry *rt;
248 struct ifnet *ifp;
249 int size;
250 int iphlen;
251 int optlen;
252
253 #ifdef DIAGNOSTIC
254 if (tp->t_inpcb && tp->t_in6pcb)
255 panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
256 #endif
257 switch (tp->t_family) {
258 #ifdef INET
259 case AF_INET:
260 iphlen = sizeof(struct ip);
261 break;
262 #endif
263 #ifdef INET6
264 case AF_INET6:
265 iphlen = sizeof(struct ip6_hdr);
266 break;
267 #endif
268 default:
269 size = tcp_mssdflt;
270 goto out;
271 }
272
273 rt = NULL;
274 #ifdef INET
275 if (inp) {
276 rt = in_pcbrtentry(inp);
277 so = inp->inp_socket;
278 }
279 #endif
280 #ifdef INET6
281 if (in6p) {
282 rt = in6_pcbrtentry(in6p);
283 so = in6p->in6p_socket;
284 }
285 #endif
286 if (rt == NULL) {
287 size = tcp_mssdflt;
288 goto out;
289 }
290
291 ifp = rt->rt_ifp;
292
293 size = tcp_mssdflt;
294 if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
295 #ifdef INET6
296 if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
297 /*
298 * RFC2460 section 5, last paragraph: if path MTU is
299 * smaller than 1280, use 1280 as packet size and
300 * attach fragment header.
301 */
302 size = IPV6_MMTU - iphlen - sizeof(struct ip6_frag) -
303 sizeof(struct tcphdr);
304 } else
305 size = rt->rt_rmx.rmx_mtu - iphlen -
306 sizeof(struct tcphdr);
307 #else
308 size = rt->rt_rmx.rmx_mtu - iphlen - sizeof(struct tcphdr);
309 #endif
310 } else if (ifp->if_flags & IFF_LOOPBACK)
311 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
312 #ifdef INET
313 else if (inp && tp->t_mtudisc)
314 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
315 else if (inp && in_localaddr(inp->inp_faddr))
316 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
317 #endif
318 #ifdef INET6
319 else if (in6p) {
320 #ifdef INET
321 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
322 /* mapped addr case */
323 struct in_addr d;
324 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
325 if (tp->t_mtudisc || in_localaddr(d))
326 size = ifp->if_mtu - iphlen - sizeof(struct tcphdr);
327 } else
328 #endif
329 {
330 /*
331 * for IPv6, path MTU discovery is always turned on,
332 * or the node must use packet size <= 1280.
333 */
334 size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
335 size -= (iphlen + sizeof(struct tcphdr));
336 }
337 }
338 #endif
339 out:
340 /*
341 * Now we must make room for whatever extra TCP/IP options are in
342 * the packet.
343 */
344 optlen = tcp_optlen(tp);
345
346 /*
347 * XXX tp->t_ourmss should have the right size, but without this code
348 * fragmentation will occur... need more investigation
349 */
350 #ifdef INET
351 if (inp) {
352 #if defined(IPSEC) || defined(FAST_IPSEC)
353 if (! IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
354 optlen += ipsec4_hdrsiz_tcp(tp);
355 #endif
356 optlen += ip_optlen(inp);
357 }
358 #endif
359 #ifdef INET6
360 #ifdef INET
361 if (in6p && tp->t_family == AF_INET) {
362 #if defined(IPSEC) || defined(FAST_IPSEC)
363 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
364 optlen += ipsec4_hdrsiz_tcp(tp);
365 #endif
366 /* XXX size -= ip_optlen(in6p); */
367 } else
368 #endif
369 if (in6p && tp->t_family == AF_INET6) {
370 #ifdef IPSEC
371 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
372 optlen += ipsec6_hdrsiz_tcp(tp);
373 #endif
374 optlen += ip6_optlen(in6p);
375 }
376 #endif
377 size -= optlen;
378
379 /* there may not be any room for data if mtu is too small */
380 if (size < 0)
381 return (EMSGSIZE);
382
383 /*
384 * *rxsegsizep holds *estimated* inbound segment size (estimation
385 * assumes that path MTU is the same for both ways). this is only
386 * for silly window avoidance, do not use the value for other purposes.
387 *
388 * ipseclen is subtracted from both sides, this may not be right.
389 * I'm not quite sure about this (could someone comment).
390 */
391 *txsegsizep = min(tp->t_peermss - optlen, size);
392 /*
393 * Never send more than half a buffer full. This insures that we can
394 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
395 * therefore acks will never be delayed unless we run out of data to
396 * transmit.
397 */
398 if (so)
399 *txsegsizep = min(so->so_snd.sb_hiwat >> 1, *txsegsizep);
400 *rxsegsizep = min(tp->t_ourmss - optlen, size);
401
402 if (*txsegsizep != tp->t_segsz) {
403 /*
404 * If the new segment size is larger, we don't want to
405 * mess up the congestion window, but if it is smaller
406 * we'll have to reduce the congestion window to ensure
407 * that we don't get into trouble with initial windows
408 * and the rest. In any case, if the segment size
409 * has changed, chances are the path has, too, and
410 * our congestion window will be different.
411 */
412 if (*txsegsizep < tp->t_segsz) {
413 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
414 * *txsegsizep, *txsegsizep);
415 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
416 * *txsegsizep, *txsegsizep);
417 }
418 tp->t_segsz = *txsegsizep;
419 }
420
421 return (0);
422 }
423
424 static
425 #ifndef GPROF
426 __inline
427 #endif
428 int
429 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
430 long len, int hdrlen, struct mbuf **mp)
431 {
432 struct mbuf *m, *m0;
433
434 if (tp->t_force && len == 1)
435 tcpstat.tcps_sndprobe++;
436 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
437 tcpstat.tcps_sndrexmitpack++;
438 tcpstat.tcps_sndrexmitbyte += len;
439 } else {
440 tcpstat.tcps_sndpack++;
441 tcpstat.tcps_sndbyte += len;
442 }
443 #ifdef notyet
444 if ((m = m_copypack(so->so_snd.sb_mb, off,
445 (int)len, max_linkhdr + hdrlen)) == 0)
446 return (ENOBUFS);
447 /*
448 * m_copypack left space for our hdr; use it.
449 */
450 m->m_len += hdrlen;
451 m->m_data -= hdrlen;
452 #else
453 MGETHDR(m, M_DONTWAIT, MT_HEADER);
454 if (__predict_false(m == NULL))
455 return (ENOBUFS);
456 MCLAIM(m, &tcp_tx_mowner);
457
458 /*
459 * XXX Because other code assumes headers will fit in
460 * XXX one header mbuf.
461 *
462 * (This code should almost *never* be run.)
463 */
464 if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
465 TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
466 MCLGET(m, M_DONTWAIT);
467 if ((m->m_flags & M_EXT) == 0) {
468 m_freem(m);
469 return (ENOBUFS);
470 }
471 }
472
473 m->m_data += max_linkhdr;
474 m->m_len = hdrlen;
475
476 /*
477 * To avoid traversing the whole sb_mb chain for correct
478 * data to send, remember last sent mbuf, its offset and
479 * the sent size. When called the next time, see if the
480 * data to send is directly following the previous transfer.
481 * This is important for large TCP windows.
482 */
483 if (off == 0 || tp->t_lastm == NULL ||
484 (tp->t_lastoff + tp->t_lastlen) != off) {
485 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
486 /*
487 * Either a new packet or a retransmit.
488 * Start from the beginning.
489 */
490 tp->t_lastm = so->so_snd.sb_mb;
491 tp->t_inoff = off;
492 } else {
493 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
494 tp->t_inoff += tp->t_lastlen;
495 }
496
497 /* Traverse forward to next packet */
498 while (tp->t_inoff > 0) {
499 if (tp->t_lastm == NULL)
500 panic("tp->t_lastm == NULL");
501 if (tp->t_inoff < tp->t_lastm->m_len)
502 break;
503 tp->t_inoff -= tp->t_lastm->m_len;
504 tp->t_lastm = tp->t_lastm->m_next;
505 }
506
507 tp->t_lastoff = off;
508 tp->t_lastlen = len;
509 m0 = tp->t_lastm;
510 off = tp->t_inoff;
511
512 if (len <= M_TRAILINGSPACE(m)) {
513 m_copydata(m0, off, (int) len, mtod(m, caddr_t) + hdrlen);
514 m->m_len += len;
515 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
516 } else {
517 m->m_next = m_copy(m0, off, (int) len);
518 if (m->m_next == NULL) {
519 m_freem(m);
520 return (ENOBUFS);
521 }
522 #ifdef TCP_OUTPUT_COUNTERS
523 if (m->m_next->m_flags & M_EXT)
524 TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
525 else
526 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
527 #endif /* TCP_OUTPUT_COUNTERS */
528 }
529 #endif
530
531 *mp = m;
532 return (0);
533 }
534
535 /*
536 * Tcp output routine: figure out what should be sent and send it.
537 */
538 int
539 tcp_output(struct tcpcb *tp)
540 {
541 struct socket *so;
542 struct route *ro;
543 long len, win;
544 int off, flags, error;
545 struct mbuf *m;
546 struct ip *ip;
547 #ifdef INET6
548 struct ip6_hdr *ip6;
549 #endif
550 struct tcphdr *th;
551 u_char opt[MAX_TCPOPTLEN];
552 unsigned optlen, hdrlen;
553 int idle, sendalot, txsegsize, rxsegsize;
554 int maxburst = TCP_MAXBURST;
555 int af; /* address family on the wire */
556 int iphdrlen;
557 int use_tso;
558 int sack_rxmit;
559 int sack_bytes_rxmt;
560 struct sackhole *p;
561 #ifdef TCP_SIGNATURE
562 int sigoff = 0;
563 #endif
564
565 #ifdef DIAGNOSTIC
566 if (tp->t_inpcb && tp->t_in6pcb)
567 panic("tcp_output: both t_inpcb and t_in6pcb are set");
568 #endif
569 so = NULL;
570 ro = NULL;
571 if (tp->t_inpcb) {
572 so = tp->t_inpcb->inp_socket;
573 ro = &tp->t_inpcb->inp_route;
574 }
575 #ifdef INET6
576 else if (tp->t_in6pcb) {
577 so = tp->t_in6pcb->in6p_socket;
578 ro = (struct route *)&tp->t_in6pcb->in6p_route;
579 }
580 #endif
581
582 switch (af = tp->t_family) {
583 #ifdef INET
584 case AF_INET:
585 if (tp->t_inpcb)
586 break;
587 #ifdef INET6
588 /* mapped addr case */
589 if (tp->t_in6pcb)
590 break;
591 #endif
592 return (EINVAL);
593 #endif
594 #ifdef INET6
595 case AF_INET6:
596 if (tp->t_in6pcb)
597 break;
598 return (EINVAL);
599 #endif
600 default:
601 return (EAFNOSUPPORT);
602 }
603
604 if (tcp_segsize(tp, &txsegsize, &rxsegsize))
605 return (EMSGSIZE);
606
607 idle = (tp->snd_max == tp->snd_una);
608
609 /*
610 * Determine if we can use TCP segmentation offload:
611 * - If we're using IPv4
612 * - If there is not an IPsec policy that prevents it
613 * - If the interface can do it
614 */
615 use_tso = tp->t_inpcb != NULL &&
616 #if defined(IPSEC) || defined(FAST_IPSEC)
617 IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
618 IPSEC_DIR_OUTBOUND) &&
619 #endif
620 tp->t_inpcb->inp_route.ro_rt != NULL &&
621 (tp->t_inpcb->inp_route.ro_rt->rt_ifp->if_capenable &
622 IFCAP_TSOv4) != 0;
623
624 /*
625 * Restart Window computation. From draft-floyd-incr-init-win-03:
626 *
627 * Optionally, a TCP MAY set the restart window to the
628 * minimum of the value used for the initial window and
629 * the current value of cwnd (in other words, using a
630 * larger value for the restart window should never increase
631 * the size of cwnd).
632 */
633 if (tcp_cwm) {
634 /*
635 * Hughes/Touch/Heidemann Congestion Window Monitoring.
636 * Count the number of packets currently pending
637 * acknowledgement, and limit our congestion window
638 * to a pre-determined allowed burst size plus that count.
639 * This prevents bursting once all pending packets have
640 * been acknowledged (i.e. transmission is idle).
641 *
642 * XXX Link this to Initial Window?
643 */
644 tp->snd_cwnd = min(tp->snd_cwnd,
645 (tcp_cwm_burstsize * txsegsize) +
646 (tp->snd_nxt - tp->snd_una));
647 } else {
648 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
649 /*
650 * We have been idle for "a while" and no acks are
651 * expected to clock out any data we send --
652 * slow start to get ack "clock" running again.
653 */
654 int ss = tcp_init_win;
655 #ifdef INET
656 if (tp->t_inpcb &&
657 in_localaddr(tp->t_inpcb->inp_faddr))
658 ss = tcp_init_win_local;
659 #endif
660 #ifdef INET6
661 if (tp->t_in6pcb &&
662 in6_localaddr(&tp->t_in6pcb->in6p_faddr))
663 ss = tcp_init_win_local;
664 #endif
665 tp->snd_cwnd = min(tp->snd_cwnd,
666 TCP_INITIAL_WINDOW(ss, txsegsize));
667 }
668 }
669
670 again:
671 /*
672 * Determine length of data that should be transmitted, and
673 * flags that should be used. If there is some data or critical
674 * controls (SYN, RST) to send, then transmit; otherwise,
675 * investigate further.
676 *
677 * Readjust SACK information to avoid resending duplicate data.
678 */
679 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
680 tcp_sack_adjust(tp);
681 sendalot = 0;
682 off = tp->snd_nxt - tp->snd_una;
683 win = min(tp->snd_wnd, tp->snd_cwnd);
684
685 flags = tcp_outflags[tp->t_state];
686
687 /*
688 * Send any SACK-generated retransmissions. If we're explicitly trying
689 * to send out new data (when sendalot is 1), bypass this function.
690 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
691 * we're replacing a (future) new transmission with a retransmission
692 * now, and we previously incremented snd_cwnd in tcp_input().
693 */
694 /*
695 * Still in sack recovery , reset rxmit flag to zero.
696 */
697 sack_rxmit = 0;
698 sack_bytes_rxmt = 0;
699 len = 0;
700 p = NULL;
701 do {
702 long cwin;
703 if (!TCP_SACK_ENABLED(tp))
704 break;
705 if (tp->t_partialacks < 0)
706 break;
707 p = tcp_sack_output(tp, &sack_bytes_rxmt);
708 if (p == NULL)
709 break;
710
711 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
712 if (cwin < 0)
713 cwin = 0;
714 /* Do not retransmit SACK segments beyond snd_recover */
715 if (SEQ_GT(p->end, tp->snd_recover)) {
716 /*
717 * (At least) part of sack hole extends beyond
718 * snd_recover. Check to see if we can rexmit data
719 * for this hole.
720 */
721 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
722 /*
723 * Can't rexmit any more data for this hole.
724 * That data will be rexmitted in the next
725 * sack recovery episode, when snd_recover
726 * moves past p->rxmit.
727 */
728 p = NULL;
729 break;
730 }
731 /* Can rexmit part of the current hole */
732 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
733 } else
734 len = ((long)ulmin(cwin, p->end - p->rxmit));
735 off = p->rxmit - tp->snd_una;
736 if (len > 0) {
737 sack_rxmit = 1;
738 sendalot = 1;
739 }
740 } while (/*CONSTCOND*/0);
741
742 /*
743 * If in persist timeout with window of 0, send 1 byte.
744 * Otherwise, if window is small but nonzero
745 * and timer expired, we will send what we can
746 * and go to transmit state.
747 */
748 if (tp->t_force) {
749 if (win == 0) {
750 /*
751 * If we still have some data to send, then
752 * clear the FIN bit. Usually this would
753 * happen below when it realizes that we
754 * aren't sending all the data. However,
755 * if we have exactly 1 byte of unset data,
756 * then it won't clear the FIN bit below,
757 * and if we are in persist state, we wind
758 * up sending the packet without recording
759 * that we sent the FIN bit.
760 *
761 * We can't just blindly clear the FIN bit,
762 * because if we don't have any more data
763 * to send then the probe will be the FIN
764 * itself.
765 */
766 if (off < so->so_snd.sb_cc)
767 flags &= ~TH_FIN;
768 win = 1;
769 } else {
770 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
771 tp->t_rxtshift = 0;
772 }
773 }
774
775 if (!TCP_SACK_ENABLED(tp)) {
776 if (win < so->so_snd.sb_cc) {
777 len = win - off;
778 flags &= ~TH_FIN;
779 } else
780 len = so->so_snd.sb_cc - off;
781 } else if (sack_rxmit == 0) {
782 if (sack_bytes_rxmt != 0) {
783 long cwin;
784
785 /*
786 * We are inside of a SACK recovery episode and are
787 * sending new data, having retransmitted all the
788 * data possible in the scoreboard.
789 */
790 len = ((long)ulmin(so->so_snd.sb_cc, tp->snd_wnd)
791 - off);
792 /*
793 * From FreeBSD:
794 * Don't remove this (len > 0) check !
795 * We explicitly check for len > 0 here (although it
796 * isn't really necessary), to work around a gcc
797 * optimization issue - to force gcc to compute
798 * len above. Without this check, the computation
799 * of len is bungled by the optimizer.
800 */
801 if (len > 0) {
802 cwin = tp->snd_cwnd -
803 (tp->snd_nxt - tp->sack_newdata) -
804 sack_bytes_rxmt;
805 if (cwin < 0)
806 cwin = 0;
807 len = lmin(len, cwin);
808 }
809 } else if (win < so->so_snd.sb_cc) {
810 len = win - off;
811 flags &= ~TH_FIN;
812 } else
813 len = so->so_snd.sb_cc - off;
814 }
815
816 if (len < 0) {
817 /*
818 * If FIN has been sent but not acked,
819 * but we haven't been called to retransmit,
820 * len will be -1. Otherwise, window shrank
821 * after we sent into it. If window shrank to 0,
822 * cancel pending retransmit, pull snd_nxt back
823 * to (closed) window, and set the persist timer
824 * if it isn't already going. If the window didn't
825 * close completely, just wait for an ACK.
826 *
827 * If we have a pending FIN, either it has already been
828 * transmitted or it is outside the window, so drop it.
829 * If the FIN has been transmitted, but this is not a
830 * retransmission, then len must be -1. Therefore we also
831 * prevent here the sending of `gratuitous FINs'. This
832 * eliminates the need to check for that case below (e.g.
833 * to back up snd_nxt before the FIN so that the sequence
834 * number is correct).
835 */
836 len = 0;
837 flags &= ~TH_FIN;
838 if (win == 0) {
839 TCP_TIMER_DISARM(tp, TCPT_REXMT);
840 tp->t_rxtshift = 0;
841 tp->snd_nxt = tp->snd_una;
842 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
843 tcp_setpersist(tp);
844 }
845 }
846 if (len > txsegsize) {
847 if (use_tso) {
848 /*
849 * Truncate TSO transfers to IP_MAXPACKET, and make
850 * sure that we send equal size transfers down the
851 * stack (rather than big-small-big-small-...).
852 */
853 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
854 } else
855 len = txsegsize;
856 flags &= ~TH_FIN;
857 sendalot = 1;
858 } else
859 use_tso = 0;
860 if (sack_rxmit) {
861 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
862 flags &= ~TH_FIN;
863 }
864
865 win = sbspace(&so->so_rcv);
866
867 /*
868 * Sender silly window avoidance. If connection is idle
869 * and can send all data, a maximum segment,
870 * at least a maximum default-size segment do it,
871 * or are forced, do it; otherwise don't bother.
872 * If peer's buffer is tiny, then send
873 * when window is at least half open.
874 * If retransmitting (possibly after persist timer forced us
875 * to send into a small window), then must resend.
876 */
877 if (len) {
878 if (len >= txsegsize)
879 goto send;
880 if ((so->so_state & SS_MORETOCOME) == 0 &&
881 ((idle || tp->t_flags & TF_NODELAY) &&
882 len + off >= so->so_snd.sb_cc))
883 goto send;
884 if (tp->t_force)
885 goto send;
886 if (len >= tp->max_sndwnd / 2)
887 goto send;
888 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
889 goto send;
890 if (sack_rxmit)
891 goto send;
892 }
893
894 /*
895 * Compare available window to amount of window known to peer
896 * (as advertised window less next expected input). If the
897 * difference is at least twice the size of the largest segment
898 * we expect to receive (i.e. two segments) or at least 50% of
899 * the maximum possible window, then want to send a window update
900 * to peer.
901 */
902 if (win > 0) {
903 /*
904 * "adv" is the amount we can increase the window,
905 * taking into account that we are limited by
906 * TCP_MAXWIN << tp->rcv_scale.
907 */
908 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
909 (tp->rcv_adv - tp->rcv_nxt);
910
911 if (adv >= (long) (2 * rxsegsize))
912 goto send;
913 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
914 goto send;
915 }
916
917 /*
918 * Send if we owe peer an ACK.
919 */
920 if (tp->t_flags & TF_ACKNOW)
921 goto send;
922 if (flags & (TH_SYN|TH_FIN|TH_RST))
923 goto send;
924 if (SEQ_GT(tp->snd_up, tp->snd_una))
925 goto send;
926 /*
927 * In SACK, it is possible for tcp_output to fail to send a segment
928 * after the retransmission timer has been turned off. Make sure
929 * that the retransmission timer is set.
930 */
931 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
932 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
933 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
934 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
935 goto just_return;
936 }
937
938 /*
939 * TCP window updates are not reliable, rather a polling protocol
940 * using ``persist'' packets is used to insure receipt of window
941 * updates. The three ``states'' for the output side are:
942 * idle not doing retransmits or persists
943 * persisting to move a small or zero window
944 * (re)transmitting and thereby not persisting
945 *
946 * tp->t_timer[TCPT_PERSIST]
947 * is set when we are in persist state.
948 * tp->t_force
949 * is set when we are called to send a persist packet.
950 * tp->t_timer[TCPT_REXMT]
951 * is set when we are retransmitting
952 * The output side is idle when both timers are zero.
953 *
954 * If send window is too small, there is data to transmit, and no
955 * retransmit or persist is pending, then go to persist state.
956 * If nothing happens soon, send when timer expires:
957 * if window is nonzero, transmit what we can,
958 * otherwise force out a byte.
959 */
960 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
961 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
962 tp->t_rxtshift = 0;
963 tcp_setpersist(tp);
964 }
965
966 /*
967 * No reason to send a segment, just return.
968 */
969 just_return:
970 return (0);
971
972 send:
973 /*
974 * Before ESTABLISHED, force sending of initial options
975 * unless TCP set not to do any options.
976 * NOTE: we assume that the IP/TCP header plus TCP options
977 * always fit in a single mbuf, leaving room for a maximum
978 * link header, i.e.
979 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
980 */
981 optlen = 0;
982 switch (af) {
983 #ifdef INET
984 case AF_INET:
985 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
986 break;
987 #endif
988 #ifdef INET6
989 case AF_INET6:
990 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
991 break;
992 #endif
993 default: /*pacify gcc*/
994 iphdrlen = 0;
995 break;
996 }
997 hdrlen = iphdrlen;
998 if (flags & TH_SYN) {
999 struct rtentry *rt;
1000
1001 rt = NULL;
1002 #ifdef INET
1003 if (tp->t_inpcb)
1004 rt = in_pcbrtentry(tp->t_inpcb);
1005 #endif
1006 #ifdef INET6
1007 if (tp->t_in6pcb)
1008 rt = in6_pcbrtentry(tp->t_in6pcb);
1009 #endif
1010
1011 tp->snd_nxt = tp->iss;
1012 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
1013 rt->rt_ifp : NULL, af);
1014 if ((tp->t_flags & TF_NOOPT) == 0) {
1015 opt[0] = TCPOPT_MAXSEG;
1016 opt[1] = 4;
1017 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1018 opt[3] = tp->t_ourmss & 0xff;
1019 optlen = 4;
1020
1021 if ((tp->t_flags & TF_REQ_SCALE) &&
1022 ((flags & TH_ACK) == 0 ||
1023 (tp->t_flags & TF_RCVD_SCALE))) {
1024 *((u_int32_t *) (opt + optlen)) = htonl(
1025 TCPOPT_NOP << 24 |
1026 TCPOPT_WINDOW << 16 |
1027 TCPOLEN_WINDOW << 8 |
1028 tp->request_r_scale);
1029 optlen += 4;
1030 }
1031 if (tcp_do_sack) {
1032 u_int8_t *p = (u_int8_t *)(opt + optlen);
1033
1034 p[0] = TCPOPT_SACK_PERMITTED;
1035 p[1] = 2;
1036 p[2] = TCPOPT_NOP;
1037 p[3] = TCPOPT_NOP;
1038 optlen += 4;
1039 }
1040 }
1041 }
1042
1043 /*
1044 * Send a timestamp and echo-reply if this is a SYN and our side
1045 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1046 * and our peer have sent timestamps in our SYN's.
1047 */
1048 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1049 (flags & TH_RST) == 0 &&
1050 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1051 (tp->t_flags & TF_RCVD_TSTMP))) {
1052 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1053
1054 /* Form timestamp option as shown in appendix A of RFC 1323. */
1055 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1056 *lp++ = htonl(TCP_TIMESTAMP(tp));
1057 *lp = htonl(tp->ts_recent);
1058 optlen += TCPOLEN_TSTAMP_APPA;
1059 }
1060
1061 /*
1062 * Tack on the SACK block if it is necessary.
1063 */
1064 if (TCP_SACK_ENABLED(tp) && (tp->t_flags & TF_ACKNOW)
1065 && (tp->rcv_sack_num > 0)) {
1066 int sack_len, i;
1067 u_char *bp = (u_char *)(opt + optlen);
1068 u_int32_t *lp = (u_int32_t *)(bp + 4);
1069
1070 sack_len = tp->rcv_sack_num * 8 + 2;
1071 bp[0] = TCPOPT_NOP;
1072 bp[1] = TCPOPT_NOP;
1073 bp[2] = TCPOPT_SACK;
1074 bp[3] = sack_len;
1075 for (i = 0; i < tp->rcv_sack_num; i++) {
1076 *lp++ = htonl(tp->rcv_sack_block[i].left);
1077 *lp++ = htonl(tp->rcv_sack_block[i].right);
1078 }
1079 tp->rcv_sack_num = 0;
1080 optlen += sack_len + 2;
1081 }
1082
1083 #ifdef TCP_SIGNATURE
1084 #if defined(INET6) && defined(FAST_IPSEC)
1085 if (tp->t_family == AF_INET)
1086 #endif
1087 if (tp->t_flags & TF_SIGNATURE) {
1088 u_char *bp;
1089 /*
1090 * Initialize TCP-MD5 option (RFC2385)
1091 */
1092 bp = (u_char *)opt + optlen;
1093 *bp++ = TCPOPT_SIGNATURE;
1094 *bp++ = TCPOLEN_SIGNATURE;
1095 sigoff = optlen + 2;
1096 bzero(bp, TCP_SIGLEN);
1097 bp += TCP_SIGLEN;
1098 optlen += TCPOLEN_SIGNATURE;
1099 /*
1100 * Terminate options list and maintain 32-bit alignment.
1101 */
1102 *bp++ = TCPOPT_NOP;
1103 *bp++ = TCPOPT_EOL;
1104 optlen += 2;
1105 }
1106 #endif /* TCP_SIGNATURE */
1107
1108 hdrlen += optlen;
1109
1110 #ifdef DIAGNOSTIC
1111 if (!use_tso && len > txsegsize)
1112 panic("tcp data to be sent is larger than segment");
1113 else if (use_tso && len > IP_MAXPACKET)
1114 panic("tcp data to be sent is larger than max TSO size");
1115 if (max_linkhdr + hdrlen > MCLBYTES)
1116 panic("tcphdr too big");
1117 #endif
1118
1119 /*
1120 * Grab a header mbuf, attaching a copy of data to
1121 * be transmitted, and initialize the header from
1122 * the template for sends on this connection.
1123 */
1124 if (len) {
1125 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1126 if (error)
1127 goto out;
1128 /*
1129 * If we're sending everything we've got, set PUSH.
1130 * (This will keep happy those implementations which only
1131 * give data to the user when a buffer fills or
1132 * a PUSH comes in.)
1133 */
1134 if (off + len == so->so_snd.sb_cc)
1135 flags |= TH_PUSH;
1136 } else {
1137 if (tp->t_flags & TF_ACKNOW)
1138 tcpstat.tcps_sndacks++;
1139 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1140 tcpstat.tcps_sndctrl++;
1141 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1142 tcpstat.tcps_sndurg++;
1143 else
1144 tcpstat.tcps_sndwinup++;
1145
1146 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1147 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1148 MCLGET(m, M_DONTWAIT);
1149 if ((m->m_flags & M_EXT) == 0) {
1150 m_freem(m);
1151 m = NULL;
1152 }
1153 }
1154 if (m == NULL) {
1155 error = ENOBUFS;
1156 goto out;
1157 }
1158 MCLAIM(m, &tcp_tx_mowner);
1159 m->m_data += max_linkhdr;
1160 m->m_len = hdrlen;
1161 }
1162 m->m_pkthdr.rcvif = (struct ifnet *)0;
1163 switch (af) {
1164 #ifdef INET
1165 case AF_INET:
1166 ip = mtod(m, struct ip *);
1167 #ifdef INET6
1168 ip6 = NULL;
1169 #endif
1170 th = (struct tcphdr *)(ip + 1);
1171 break;
1172 #endif
1173 #ifdef INET6
1174 case AF_INET6:
1175 ip = NULL;
1176 ip6 = mtod(m, struct ip6_hdr *);
1177 th = (struct tcphdr *)(ip6 + 1);
1178 break;
1179 #endif
1180 default: /*pacify gcc*/
1181 ip = NULL;
1182 #ifdef INET6
1183 ip6 = NULL;
1184 #endif
1185 th = NULL;
1186 break;
1187 }
1188 if (tp->t_template == 0)
1189 panic("tcp_output");
1190 if (tp->t_template->m_len < iphdrlen)
1191 panic("tcp_output");
1192 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
1193
1194 /*
1195 * If we are doing retransmissions, then snd_nxt will
1196 * not reflect the first unsent octet. For ACK only
1197 * packets, we do not want the sequence number of the
1198 * retransmitted packet, we want the sequence number
1199 * of the next unsent octet. So, if there is no data
1200 * (and no SYN or FIN), use snd_max instead of snd_nxt
1201 * when filling in ti_seq. But if we are in persist
1202 * state, snd_max might reflect one byte beyond the
1203 * right edge of the window, so use snd_nxt in that
1204 * case, since we know we aren't doing a retransmission.
1205 * (retransmit and persist are mutually exclusive...)
1206 */
1207 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1208 th->th_seq = htonl(p->rxmit);
1209 p->rxmit += len;
1210 } else {
1211 if (len || (flags & (TH_SYN|TH_FIN)) ||
1212 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1213 th->th_seq = htonl(tp->snd_nxt);
1214 else
1215 th->th_seq = htonl(tp->snd_max);
1216 }
1217 th->th_ack = htonl(tp->rcv_nxt);
1218 if (optlen) {
1219 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
1220 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1221 }
1222 th->th_flags = flags;
1223 /*
1224 * Calculate receive window. Don't shrink window,
1225 * but avoid silly window syndrome.
1226 */
1227 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1228 win = 0;
1229 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1230 win = (long)TCP_MAXWIN << tp->rcv_scale;
1231 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1232 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1233 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1234 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1235 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1236 if (urp > IP_MAXPACKET)
1237 urp = IP_MAXPACKET;
1238 th->th_urp = htons((u_int16_t)urp);
1239 th->th_flags |= TH_URG;
1240 } else
1241 /*
1242 * If no urgent pointer to send, then we pull
1243 * the urgent pointer to the left edge of the send window
1244 * so that it doesn't drift into the send window on sequence
1245 * number wraparound.
1246 */
1247 tp->snd_up = tp->snd_una; /* drag it along */
1248
1249 #ifdef TCP_SIGNATURE
1250 #if defined(INET6) && defined(FAST_IPSEC)
1251 if (tp->t_family == AF_INET) /* XXX */
1252 #endif
1253 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1254 struct secasvar *sav;
1255 u_int8_t *sigp;
1256
1257 sav = tcp_signature_getsav(m, th);
1258
1259 if (sav == NULL) {
1260 if (m)
1261 m_freem(m);
1262 return (EPERM);
1263 }
1264
1265 m->m_pkthdr.len = hdrlen + len;
1266 sigp = (caddr_t)th + sizeof(*th) + sigoff;
1267 tcp_signature(m, th, (caddr_t)th - mtod(m, caddr_t), sav, sigp);
1268
1269 key_sa_recordxfer(sav, m);
1270 #ifdef FAST_IPSEC
1271 KEY_FREESAV(&sav);
1272 #else
1273 key_freesav(sav);
1274 #endif
1275 }
1276 #endif
1277
1278 /*
1279 * Set ourselves up to be checksummed just before the packet
1280 * hits the wire. Maybe skip checksums on loopback interfaces.
1281 */
1282 switch (af) {
1283 #ifdef INET
1284 case AF_INET:
1285 if (use_tso) {
1286 m->m_pkthdr.segsz = txsegsize;
1287 m->m_pkthdr.csum_flags |= M_CSUM_TSOv4;
1288 } else {
1289 if (__predict_true(ro->ro_rt == NULL ||
1290 !(ro->ro_rt->rt_ifp->if_flags &
1291 IFF_LOOPBACK) ||
1292 tcp_do_loopback_cksum))
1293 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1294 else
1295 m->m_pkthdr.csum_flags = 0;
1296 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1297 if (len + optlen) {
1298 /* Fixup the pseudo-header checksum. */
1299 /* XXXJRT Not IP Jumbogram safe. */
1300 th->th_sum = in_cksum_addword(th->th_sum,
1301 htons((u_int16_t) (len + optlen)));
1302 }
1303 }
1304 break;
1305 #endif
1306 #ifdef INET6
1307 case AF_INET6:
1308 /*
1309 * XXX Actually delaying the checksum is Hard
1310 * XXX (well, maybe not for Itojun, but it is
1311 * XXX for me), but we can still take advantage
1312 * XXX of the cached pseudo-header checksum.
1313 */
1314 /* equals to hdrlen + len */
1315 m->m_pkthdr.len = sizeof(struct ip6_hdr)
1316 + sizeof(struct tcphdr) + optlen + len;
1317 #ifdef notyet
1318 if (__predict_true(ro->ro_rt == NULL ||
1319 !(ro->ro_rt->rt_ifp->if_flags &
1320 IFF_LOOPBACK) ||
1321 tcp_do_loopback_cksum))
1322 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1323 else
1324 m->m_pkthdr.csum_flags = 0;
1325 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1326 #endif
1327 if (len + optlen) {
1328 /* Fixup the pseudo-header checksum. */
1329 /* XXXJRT: Not IPv6 Jumbogram safe. */
1330 th->th_sum = in_cksum_addword(th->th_sum,
1331 htons((u_int16_t) (len + optlen)));
1332 }
1333 #ifndef notyet
1334 if (__predict_true(ro->ro_rt == NULL ||
1335 !(ro->ro_rt->rt_ifp->if_flags &
1336 IFF_LOOPBACK) ||
1337 tcp_do_loopback_cksum))
1338 th->th_sum = in6_cksum(m, 0, sizeof(struct ip6_hdr),
1339 sizeof(struct tcphdr) + optlen + len);
1340 #endif
1341 break;
1342 #endif
1343 }
1344
1345 /*
1346 * In transmit state, time the transmission and arrange for
1347 * the retransmit. In persist state, just set snd_max.
1348 */
1349 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1350 tcp_seq startseq = tp->snd_nxt;
1351
1352 /*
1353 * Advance snd_nxt over sequence space of this segment.
1354 * There are no states in which we send both a SYN and a FIN,
1355 * so we collapse the tests for these flags.
1356 */
1357 if (flags & (TH_SYN|TH_FIN))
1358 tp->snd_nxt++;
1359 if (sack_rxmit)
1360 goto timer;
1361 tp->snd_nxt += len;
1362 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1363 tp->snd_max = tp->snd_nxt;
1364 /*
1365 * Time this transmission if not a retransmission and
1366 * not currently timing anything.
1367 */
1368 if (tp->t_rtttime == 0) {
1369 tp->t_rtttime = tcp_now;
1370 tp->t_rtseq = startseq;
1371 tcpstat.tcps_segstimed++;
1372 }
1373 }
1374
1375 /*
1376 * Set retransmit timer if not currently set,
1377 * and not doing an ack or a keep-alive probe.
1378 * Initial value for retransmit timer is smoothed
1379 * round-trip time + 2 * round-trip time variance.
1380 * Initialize shift counter which is used for backoff
1381 * of retransmit time.
1382 */
1383 timer:
1384 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1385 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1386 tp->snd_nxt != tp->snd_una)) {
1387 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1388 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1389 tp->t_rxtshift = 0;
1390 }
1391 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1392 }
1393 } else
1394 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1395 tp->snd_max = tp->snd_nxt + len;
1396
1397 #ifdef TCP_DEBUG
1398 /*
1399 * Trace.
1400 */
1401 if (so->so_options & SO_DEBUG)
1402 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1403 #endif
1404
1405 /*
1406 * Fill in IP length and desired time to live and
1407 * send to IP level. There should be a better way
1408 * to handle ttl and tos; we could keep them in
1409 * the template, but need a way to checksum without them.
1410 */
1411 m->m_pkthdr.len = hdrlen + len;
1412
1413 switch (af) {
1414 #ifdef INET
1415 case AF_INET:
1416 ip->ip_len = htons(m->m_pkthdr.len);
1417 if (tp->t_inpcb) {
1418 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1419 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1420 }
1421 #ifdef INET6
1422 else if (tp->t_in6pcb) {
1423 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1424 ip->ip_tos = 0; /*XXX*/
1425 }
1426 #endif
1427 break;
1428 #endif
1429 #ifdef INET6
1430 case AF_INET6:
1431 ip6->ip6_nxt = IPPROTO_TCP;
1432 if (tp->t_in6pcb) {
1433 /*
1434 * we separately set hoplimit for every segment, since
1435 * the user might want to change the value via
1436 * setsockopt. Also, desired default hop limit might
1437 * be changed via Neighbor Discovery.
1438 */
1439 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
1440 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
1441 }
1442 /* ip6->ip6_flow = ??? */
1443 /* ip6_plen will be filled in ip6_output(). */
1444 break;
1445 #endif
1446 }
1447
1448 switch (af) {
1449 #ifdef INET
1450 case AF_INET:
1451 {
1452 struct mbuf *opts;
1453
1454 if (tp->t_inpcb)
1455 opts = tp->t_inpcb->inp_options;
1456 else
1457 opts = NULL;
1458 error = ip_output(m, opts, ro,
1459 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1460 (so->so_options & SO_DONTROUTE),
1461 (struct ip_moptions *)0, so);
1462 break;
1463 }
1464 #endif
1465 #ifdef INET6
1466 case AF_INET6:
1467 {
1468 struct ip6_pktopts *opts;
1469
1470 if (tp->t_in6pcb)
1471 opts = tp->t_in6pcb->in6p_outputopts;
1472 else
1473 opts = NULL;
1474 error = ip6_output(m, opts, (struct route_in6 *)ro,
1475 so->so_options & SO_DONTROUTE,
1476 (struct ip6_moptions *)0, so, NULL);
1477 break;
1478 }
1479 #endif
1480 default:
1481 error = EAFNOSUPPORT;
1482 break;
1483 }
1484 if (error) {
1485 out:
1486 if (error == ENOBUFS) {
1487 tcpstat.tcps_selfquench++;
1488 #ifdef INET
1489 if (tp->t_inpcb)
1490 tcp_quench(tp->t_inpcb, 0);
1491 #endif
1492 #ifdef INET6
1493 if (tp->t_in6pcb)
1494 tcp6_quench(tp->t_in6pcb, 0);
1495 #endif
1496 error = 0;
1497 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1498 TCPS_HAVERCVDSYN(tp->t_state)) {
1499 tp->t_softerror = error;
1500 error = 0;
1501 }
1502
1503 /* Back out the seqence number advance. */
1504 if (sack_rxmit)
1505 p->rxmit -= len;
1506
1507 /* Restart the delayed ACK timer, if necessary. */
1508 if (tp->t_flags & TF_DELACK)
1509 TCP_RESTART_DELACK(tp);
1510
1511 return (error);
1512 }
1513 tcpstat.tcps_sndtotal++;
1514 if (tp->t_flags & TF_DELACK)
1515 tcpstat.tcps_delack++;
1516
1517 /*
1518 * Data sent (as far as we can tell).
1519 * If this advertises a larger window than any other segment,
1520 * then remember the size of the advertised window.
1521 * Any pending ACK has now been sent.
1522 */
1523 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1524 tp->rcv_adv = tp->rcv_nxt + win;
1525 tp->last_ack_sent = tp->rcv_nxt;
1526 tp->t_flags &= ~TF_ACKNOW;
1527 TCP_CLEAR_DELACK(tp);
1528 #ifdef DIAGNOSTIC
1529 if (maxburst < 0)
1530 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1531 #endif
1532 if (sendalot && (!tcp_do_newreno || --maxburst))
1533 goto again;
1534 return (0);
1535 }
1536
1537 void
1538 tcp_setpersist(struct tcpcb *tp)
1539 {
1540 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1541 int nticks;
1542
1543 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1544 panic("tcp_output REXMT");
1545 /*
1546 * Start/restart persistance timer.
1547 */
1548 if (t < tp->t_rttmin)
1549 t = tp->t_rttmin;
1550 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1551 TCPTV_PERSMIN, TCPTV_PERSMAX);
1552 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1553 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1554 tp->t_rxtshift++;
1555 }
1556