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