tcp_output.c revision 1.141.4.2 1 /* $NetBSD: tcp_output.c,v 1.141.4.2 2006/09/09 02:58:47 rpaulo 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, 2006 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 * This code is derived from software contributed to The NetBSD Foundation
82 * by Rui Paulo.
83 *
84 * Redistribution and use in source and binary forms, with or without
85 * modification, are permitted provided that the following conditions
86 * are met:
87 * 1. Redistributions of source code must retain the above copyright
88 * notice, this list of conditions and the following disclaimer.
89 * 2. Redistributions in binary form must reproduce the above copyright
90 * notice, this list of conditions and the following disclaimer in the
91 * documentation and/or other materials provided with the distribution.
92 * 3. All advertising materials mentioning features or use of this software
93 * must display the following acknowledgement:
94 * This product includes software developed by the NetBSD
95 * Foundation, Inc. and its contributors.
96 * 4. Neither the name of The NetBSD Foundation nor the names of its
97 * contributors may be used to endorse or promote products derived
98 * from this software without specific prior written permission.
99 *
100 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
101 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
102 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
103 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
104 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
105 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
106 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
107 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
108 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
109 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
110 * POSSIBILITY OF SUCH DAMAGE.
111 */
112
113 /*
114 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
115 * The Regents of the University of California. All rights reserved.
116 *
117 * Redistribution and use in source and binary forms, with or without
118 * modification, are permitted provided that the following conditions
119 * are met:
120 * 1. Redistributions of source code must retain the above copyright
121 * notice, this list of conditions and the following disclaimer.
122 * 2. Redistributions in binary form must reproduce the above copyright
123 * notice, this list of conditions and the following disclaimer in the
124 * documentation and/or other materials provided with the distribution.
125 * 3. Neither the name of the University nor the names of its contributors
126 * may be used to endorse or promote products derived from this software
127 * without specific prior written permission.
128 *
129 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
130 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
131 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
132 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
133 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
134 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
135 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
136 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
137 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
138 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
139 * SUCH DAMAGE.
140 *
141 * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95
142 */
143
144 #include <sys/cdefs.h>
145 __KERNEL_RCSID(0, "$NetBSD: tcp_output.c,v 1.141.4.2 2006/09/09 02:58:47 rpaulo Exp $");
146
147 #include "opt_inet.h"
148 #include "opt_ipsec.h"
149 #include "opt_tcp_debug.h"
150
151 #include <sys/param.h>
152 #include <sys/systm.h>
153 #include <sys/malloc.h>
154 #include <sys/mbuf.h>
155 #include <sys/protosw.h>
156 #include <sys/socket.h>
157 #include <sys/socketvar.h>
158 #include <sys/errno.h>
159 #include <sys/domain.h>
160 #include <sys/kernel.h>
161 #ifdef TCP_SIGNATURE
162 #include <sys/md5.h>
163 #endif
164
165 #include <net/if.h>
166 #include <net/route.h>
167
168 #include <netinet/in.h>
169 #include <netinet/in_systm.h>
170 #include <netinet/ip.h>
171 #include <netinet/in_pcb.h>
172 #include <netinet/ip_var.h>
173
174 #ifdef INET6
175 #ifndef INET
176 #include <netinet/in.h>
177 #endif
178 #include <netinet/ip6.h>
179 #include <netinet6/in6_var.h>
180 #include <netinet6/ip6_var.h>
181 #include <netinet6/nd6.h>
182 #endif
183
184 #ifdef FAST_IPSEC
185 #include <netipsec/ipsec.h>
186 #include <netipsec/key.h>
187 #endif /* FAST_IPSEC*/
188 #ifdef IPSEC
189 #include <netinet6/ipsec.h>
190 #endif
191
192 #include <netinet/tcp.h>
193 #define TCPOUTFLAGS
194 #include <netinet/tcp_fsm.h>
195 #include <netinet/tcp_seq.h>
196 #include <netinet/tcp_timer.h>
197 #include <netinet/tcp_var.h>
198 #include <netinet/tcpip.h>
199 #include <netinet/tcp_debug.h>
200 #include <netinet/in_offload.h>
201
202 #ifdef IPSEC
203 #include <netkey/key.h>
204 #endif
205
206 #ifdef notyet
207 extern struct mbuf *m_copypack();
208 #endif
209
210 /*
211 * Knob to enable Congestion Window Monitoring, and control
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 hdrlen;
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 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
263 break;
264 #endif
265 #ifdef INET6
266 case AF_INET6:
267 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
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 - hdrlen - sizeof(struct ip6_frag);
305 } else
306 size = rt->rt_rmx.rmx_mtu - hdrlen;
307 #else
308 size = rt->rt_rmx.rmx_mtu - hdrlen;
309 #endif
310 } else if (ifp->if_flags & IFF_LOOPBACK)
311 size = ifp->if_mtu - hdrlen;
312 #ifdef INET
313 else if (inp && tp->t_mtudisc)
314 size = ifp->if_mtu - hdrlen;
315 else if (inp && in_localaddr(inp->inp_faddr))
316 size = ifp->if_mtu - hdrlen;
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 - hdrlen;
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 -= hdrlen;
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, packetlen;
553 unsigned int sack_numblks;
554 int idle, sendalot, txsegsize, rxsegsize;
555 int txsegsize_nosack;
556 int maxburst = TCP_MAXBURST;
557 int af; /* address family on the wire */
558 int iphdrlen;
559 int has_tso, 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 has_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 txsegsize_nosack = txsegsize;
673 again:
674 use_tso = has_tso;
675 TCP_REASS_LOCK(tp);
676 sack_numblks = tcp_sack_numblks(tp);
677 if (sack_numblks) {
678 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
679 /* don't duplicate D-SACK. */
680 use_tso = 0;
681 }
682 txsegsize = txsegsize_nosack - TCP_SACK_OPTLEN(sack_numblks);
683 } else {
684 txsegsize = txsegsize_nosack;
685 }
686
687 /*
688 * Determine length of data that should be transmitted, and
689 * flags that should be used. If there is some data or critical
690 * controls (SYN, RST) to send, then transmit; otherwise,
691 * investigate further.
692 *
693 * Readjust SACK information to avoid resending duplicate data.
694 */
695 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
696 tcp_sack_adjust(tp);
697 sendalot = 0;
698 off = tp->snd_nxt - tp->snd_una;
699 win = min(tp->snd_wnd, tp->snd_cwnd);
700
701 flags = tcp_outflags[tp->t_state];
702
703 /*
704 * Send any SACK-generated retransmissions. If we're explicitly trying
705 * to send out new data (when sendalot is 1), bypass this function.
706 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
707 * we're replacing a (future) new transmission with a retransmission
708 * now, and we previously incremented snd_cwnd in tcp_input().
709 */
710 /*
711 * Still in sack recovery , reset rxmit flag to zero.
712 */
713 sack_rxmit = 0;
714 sack_bytes_rxmt = 0;
715 len = 0;
716 p = NULL;
717 do {
718 long cwin;
719 if (!TCP_SACK_ENABLED(tp))
720 break;
721 if (tp->t_partialacks < 0)
722 break;
723 p = tcp_sack_output(tp, &sack_bytes_rxmt);
724 if (p == NULL)
725 break;
726
727 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
728 if (cwin < 0)
729 cwin = 0;
730 /* Do not retransmit SACK segments beyond snd_recover */
731 if (SEQ_GT(p->end, tp->snd_recover)) {
732 /*
733 * (At least) part of sack hole extends beyond
734 * snd_recover. Check to see if we can rexmit data
735 * for this hole.
736 */
737 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
738 /*
739 * Can't rexmit any more data for this hole.
740 * That data will be rexmitted in the next
741 * sack recovery episode, when snd_recover
742 * moves past p->rxmit.
743 */
744 p = NULL;
745 break;
746 }
747 /* Can rexmit part of the current hole */
748 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
749 } else
750 len = ((long)ulmin(cwin, p->end - p->rxmit));
751 off = p->rxmit - tp->snd_una;
752 if (off + len > so->so_snd.sb_cc) {
753 /* 1 for TH_FIN */
754 KASSERT(off + len == so->so_snd.sb_cc + 1);
755 KASSERT(p->rxmit + len == tp->snd_max);
756 len = so->so_snd.sb_cc - off;
757 }
758 if (len > 0) {
759 sack_rxmit = 1;
760 sendalot = 1;
761 }
762 } while (/*CONSTCOND*/0);
763
764 /*
765 * If in persist timeout with window of 0, send 1 byte.
766 * Otherwise, if window is small but nonzero
767 * and timer expired, we will send what we can
768 * and go to transmit state.
769 */
770 if (tp->t_force) {
771 if (win == 0) {
772 /*
773 * If we still have some data to send, then
774 * clear the FIN bit. Usually this would
775 * happen below when it realizes that we
776 * aren't sending all the data. However,
777 * if we have exactly 1 byte of unset data,
778 * then it won't clear the FIN bit below,
779 * and if we are in persist state, we wind
780 * up sending the packet without recording
781 * that we sent the FIN bit.
782 *
783 * We can't just blindly clear the FIN bit,
784 * because if we don't have any more data
785 * to send then the probe will be the FIN
786 * itself.
787 */
788 if (off < so->so_snd.sb_cc)
789 flags &= ~TH_FIN;
790 win = 1;
791 } else {
792 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
793 tp->t_rxtshift = 0;
794 }
795 }
796
797 if (!TCP_SACK_ENABLED(tp)) {
798 if (win < so->so_snd.sb_cc) {
799 len = win - off;
800 flags &= ~TH_FIN;
801 } else
802 len = so->so_snd.sb_cc - off;
803 } else if (sack_rxmit == 0) {
804 if (sack_bytes_rxmt != 0) {
805 long cwin;
806
807 /*
808 * We are inside of a SACK recovery episode and are
809 * sending new data, having retransmitted all the
810 * data possible in the scoreboard.
811 */
812 if (tp->snd_wnd < so->so_snd.sb_cc) {
813 len = tp->snd_wnd - off;
814 flags &= ~TH_FIN;
815 } else {
816 len = so->so_snd.sb_cc - off;
817 }
818
819 /*
820 * From FreeBSD:
821 * Don't remove this (len > 0) check !
822 * We explicitly check for len > 0 here (although it
823 * isn't really necessary), to work around a gcc
824 * optimization issue - to force gcc to compute
825 * len above. Without this check, the computation
826 * of len is bungled by the optimizer.
827 */
828 if (len > 0) {
829 cwin = tp->snd_cwnd -
830 (tp->snd_nxt - tp->sack_newdata) -
831 sack_bytes_rxmt;
832 if (cwin < 0)
833 cwin = 0;
834 if (cwin < len) {
835 len = cwin;
836 flags &= ~TH_FIN;
837 }
838 }
839 } else if (win < so->so_snd.sb_cc) {
840 len = win - off;
841 flags &= ~TH_FIN;
842 } else
843 len = so->so_snd.sb_cc - off;
844 }
845
846 if (len < 0) {
847 /*
848 * If FIN has been sent but not acked,
849 * but we haven't been called to retransmit,
850 * len will be -1. Otherwise, window shrank
851 * after we sent into it. If window shrank to 0,
852 * cancel pending retransmit, pull snd_nxt back
853 * to (closed) window, and set the persist timer
854 * if it isn't already going. If the window didn't
855 * close completely, just wait for an ACK.
856 *
857 * If we have a pending FIN, either it has already been
858 * transmitted or it is outside the window, so drop it.
859 * If the FIN has been transmitted, but this is not a
860 * retransmission, then len must be -1. Therefore we also
861 * prevent here the sending of `gratuitous FINs'. This
862 * eliminates the need to check for that case below (e.g.
863 * to back up snd_nxt before the FIN so that the sequence
864 * number is correct).
865 */
866 len = 0;
867 flags &= ~TH_FIN;
868 if (win == 0) {
869 TCP_TIMER_DISARM(tp, TCPT_REXMT);
870 tp->t_rxtshift = 0;
871 tp->snd_nxt = tp->snd_una;
872 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
873 tcp_setpersist(tp);
874 }
875 }
876 if (len > txsegsize) {
877 if (use_tso) {
878 /*
879 * Truncate TSO transfers to IP_MAXPACKET, and make
880 * sure that we send equal size transfers down the
881 * stack (rather than big-small-big-small-...).
882 */
883 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
884 if (len <= txsegsize) {
885 use_tso = 0;
886 }
887 } else
888 len = txsegsize;
889 flags &= ~TH_FIN;
890 sendalot = 1;
891 } else
892 use_tso = 0;
893 if (sack_rxmit) {
894 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
895 flags &= ~TH_FIN;
896 }
897
898 win = sbspace(&so->so_rcv);
899
900 /*
901 * Sender silly window avoidance. If connection is idle
902 * and can send all data, a maximum segment,
903 * at least a maximum default-size segment do it,
904 * or are forced, do it; otherwise don't bother.
905 * If peer's buffer is tiny, then send
906 * when window is at least half open.
907 * If retransmitting (possibly after persist timer forced us
908 * to send into a small window), then must resend.
909 */
910 if (len) {
911 if (len >= txsegsize)
912 goto send;
913 if ((so->so_state & SS_MORETOCOME) == 0 &&
914 ((idle || tp->t_flags & TF_NODELAY) &&
915 len + off >= so->so_snd.sb_cc))
916 goto send;
917 if (tp->t_force)
918 goto send;
919 if (len >= tp->max_sndwnd / 2)
920 goto send;
921 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
922 goto send;
923 if (sack_rxmit)
924 goto send;
925 }
926
927 /*
928 * Compare available window to amount of window known to peer
929 * (as advertised window less next expected input). If the
930 * difference is at least twice the size of the largest segment
931 * we expect to receive (i.e. two segments) or at least 50% of
932 * the maximum possible window, then want to send a window update
933 * to peer.
934 */
935 if (win > 0) {
936 /*
937 * "adv" is the amount we can increase the window,
938 * taking into account that we are limited by
939 * TCP_MAXWIN << tp->rcv_scale.
940 */
941 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
942 (tp->rcv_adv - tp->rcv_nxt);
943
944 if (adv >= (long) (2 * rxsegsize))
945 goto send;
946 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
947 goto send;
948 }
949
950 /*
951 * Send if we owe peer an ACK.
952 */
953 if (tp->t_flags & TF_ACKNOW)
954 goto send;
955 if (flags & (TH_SYN|TH_FIN|TH_RST))
956 goto send;
957 if (SEQ_GT(tp->snd_up, tp->snd_una))
958 goto send;
959 /*
960 * In SACK, it is possible for tcp_output to fail to send a segment
961 * after the retransmission timer has been turned off. Make sure
962 * that the retransmission timer is set.
963 */
964 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
965 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
966 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
967 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
968 goto just_return;
969 }
970
971 /*
972 * TCP window updates are not reliable, rather a polling protocol
973 * using ``persist'' packets is used to insure receipt of window
974 * updates. The three ``states'' for the output side are:
975 * idle not doing retransmits or persists
976 * persisting to move a small or zero window
977 * (re)transmitting and thereby not persisting
978 *
979 * tp->t_timer[TCPT_PERSIST]
980 * is set when we are in persist state.
981 * tp->t_force
982 * is set when we are called to send a persist packet.
983 * tp->t_timer[TCPT_REXMT]
984 * is set when we are retransmitting
985 * The output side is idle when both timers are zero.
986 *
987 * If send window is too small, there is data to transmit, and no
988 * retransmit or persist is pending, then go to persist state.
989 * If nothing happens soon, send when timer expires:
990 * if window is nonzero, transmit what we can,
991 * otherwise force out a byte.
992 */
993 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
994 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
995 tp->t_rxtshift = 0;
996 tcp_setpersist(tp);
997 }
998
999 /*
1000 * No reason to send a segment, just return.
1001 */
1002 just_return:
1003 TCP_REASS_UNLOCK(tp);
1004 return (0);
1005
1006 send:
1007 /*
1008 * Before ESTABLISHED, force sending of initial options
1009 * unless TCP set not to do any options.
1010 * NOTE: we assume that the IP/TCP header plus TCP options
1011 * always fit in a single mbuf, leaving room for a maximum
1012 * link header, i.e.
1013 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1014 */
1015 optlen = 0;
1016 switch (af) {
1017 #ifdef INET
1018 case AF_INET:
1019 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
1020 break;
1021 #endif
1022 #ifdef INET6
1023 case AF_INET6:
1024 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1025 break;
1026 #endif
1027 default: /*pacify gcc*/
1028 iphdrlen = 0;
1029 break;
1030 }
1031 hdrlen = iphdrlen;
1032 if (flags & TH_SYN) {
1033 struct rtentry *rt;
1034
1035 rt = NULL;
1036 #ifdef INET
1037 if (tp->t_inpcb)
1038 rt = in_pcbrtentry(tp->t_inpcb);
1039 #endif
1040 #ifdef INET6
1041 if (tp->t_in6pcb)
1042 rt = in6_pcbrtentry(tp->t_in6pcb);
1043 #endif
1044
1045 tp->snd_nxt = tp->iss;
1046 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
1047 rt->rt_ifp : NULL, af);
1048 if ((tp->t_flags & TF_NOOPT) == 0) {
1049 opt[0] = TCPOPT_MAXSEG;
1050 opt[1] = 4;
1051 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1052 opt[3] = tp->t_ourmss & 0xff;
1053 optlen = 4;
1054
1055 if ((tp->t_flags & TF_REQ_SCALE) &&
1056 ((flags & TH_ACK) == 0 ||
1057 (tp->t_flags & TF_RCVD_SCALE))) {
1058 *((u_int32_t *) (opt + optlen)) = htonl(
1059 TCPOPT_NOP << 24 |
1060 TCPOPT_WINDOW << 16 |
1061 TCPOLEN_WINDOW << 8 |
1062 tp->request_r_scale);
1063 optlen += 4;
1064 }
1065 if (tcp_do_sack) {
1066 u_int8_t *cp = (u_int8_t *)(opt + optlen);
1067
1068 cp[0] = TCPOPT_SACK_PERMITTED;
1069 cp[1] = 2;
1070 cp[2] = TCPOPT_NOP;
1071 cp[3] = TCPOPT_NOP;
1072 optlen += 4;
1073 }
1074 }
1075 }
1076
1077 /*
1078 * Send a timestamp and echo-reply if this is a SYN and our side
1079 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1080 * and our peer have sent timestamps in our SYN's.
1081 */
1082 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1083 (flags & TH_RST) == 0 &&
1084 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1085 (tp->t_flags & TF_RCVD_TSTMP))) {
1086 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1087
1088 /* Form timestamp option as shown in appendix A of RFC 1323. */
1089 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1090 *lp++ = htonl(TCP_TIMESTAMP(tp));
1091 *lp = htonl(tp->ts_recent);
1092 optlen += TCPOLEN_TSTAMP_APPA;
1093 }
1094
1095 /*
1096 * Tack on the SACK block if it is necessary.
1097 */
1098 if (sack_numblks) {
1099 int sack_len;
1100 u_char *bp = (u_char *)(opt + optlen);
1101 u_int32_t *lp = (u_int32_t *)(bp + 4);
1102 struct ipqent *tiqe;
1103
1104 sack_len = sack_numblks * 8 + 2;
1105 bp[0] = TCPOPT_NOP;
1106 bp[1] = TCPOPT_NOP;
1107 bp[2] = TCPOPT_SACK;
1108 bp[3] = sack_len;
1109 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
1110 sack_numblks--;
1111 *lp++ = htonl(tp->rcv_dsack_block.left);
1112 *lp++ = htonl(tp->rcv_dsack_block.right);
1113 tp->rcv_sack_flags &= ~TCPSACK_HAVED;
1114 }
1115 for (tiqe = TAILQ_FIRST(&tp->timeq);
1116 sack_numblks > 0; tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
1117 KASSERT(tiqe != NULL);
1118 sack_numblks--;
1119 *lp++ = htonl(tiqe->ipqe_seq);
1120 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
1121 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
1122 }
1123 optlen += sack_len + 2;
1124 }
1125 TCP_REASS_UNLOCK(tp);
1126
1127 #ifdef TCP_SIGNATURE
1128 #if defined(INET6) && defined(FAST_IPSEC)
1129 if (tp->t_family == AF_INET)
1130 #endif
1131 if (tp->t_flags & TF_SIGNATURE) {
1132 u_char *bp;
1133 /*
1134 * Initialize TCP-MD5 option (RFC2385)
1135 */
1136 bp = (u_char *)opt + optlen;
1137 *bp++ = TCPOPT_SIGNATURE;
1138 *bp++ = TCPOLEN_SIGNATURE;
1139 sigoff = optlen + 2;
1140 bzero(bp, TCP_SIGLEN);
1141 bp += TCP_SIGLEN;
1142 optlen += TCPOLEN_SIGNATURE;
1143 /*
1144 * Terminate options list and maintain 32-bit alignment.
1145 */
1146 *bp++ = TCPOPT_NOP;
1147 *bp++ = TCPOPT_EOL;
1148 optlen += 2;
1149 }
1150 #endif /* TCP_SIGNATURE */
1151
1152 hdrlen += optlen;
1153
1154 #ifdef DIAGNOSTIC
1155 if (!use_tso && len > txsegsize)
1156 panic("tcp data to be sent is larger than segment");
1157 else if (use_tso && len > IP_MAXPACKET)
1158 panic("tcp data to be sent is larger than max TSO size");
1159 if (max_linkhdr + hdrlen > MCLBYTES)
1160 panic("tcphdr too big");
1161 #endif
1162
1163 /*
1164 * Grab a header mbuf, attaching a copy of data to
1165 * be transmitted, and initialize the header from
1166 * the template for sends on this connection.
1167 */
1168 if (len) {
1169 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1170 if (error)
1171 goto out;
1172 /*
1173 * If we're sending everything we've got, set PUSH.
1174 * (This will keep happy those implementations which only
1175 * give data to the user when a buffer fills or
1176 * a PUSH comes in.)
1177 */
1178 if (off + len == so->so_snd.sb_cc)
1179 flags |= TH_PUSH;
1180 } else {
1181 if (tp->t_flags & TF_ACKNOW)
1182 tcpstat.tcps_sndacks++;
1183 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1184 tcpstat.tcps_sndctrl++;
1185 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1186 tcpstat.tcps_sndurg++;
1187 else
1188 tcpstat.tcps_sndwinup++;
1189
1190 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1191 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1192 MCLGET(m, M_DONTWAIT);
1193 if ((m->m_flags & M_EXT) == 0) {
1194 m_freem(m);
1195 m = NULL;
1196 }
1197 }
1198 if (m == NULL) {
1199 error = ENOBUFS;
1200 goto out;
1201 }
1202 MCLAIM(m, &tcp_tx_mowner);
1203 m->m_data += max_linkhdr;
1204 m->m_len = hdrlen;
1205 }
1206 m->m_pkthdr.rcvif = (struct ifnet *)0;
1207 switch (af) {
1208 #ifdef INET
1209 case AF_INET:
1210 ip = mtod(m, struct ip *);
1211 #ifdef INET6
1212 ip6 = NULL;
1213 #endif
1214 th = (struct tcphdr *)(ip + 1);
1215 break;
1216 #endif
1217 #ifdef INET6
1218 case AF_INET6:
1219 ip = NULL;
1220 ip6 = mtod(m, struct ip6_hdr *);
1221 th = (struct tcphdr *)(ip6 + 1);
1222 break;
1223 #endif
1224 default: /*pacify gcc*/
1225 ip = NULL;
1226 #ifdef INET6
1227 ip6 = NULL;
1228 #endif
1229 th = NULL;
1230 break;
1231 }
1232 if (tp->t_template == 0)
1233 panic("tcp_output");
1234 if (tp->t_template->m_len < iphdrlen)
1235 panic("tcp_output");
1236 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
1237
1238 /*
1239 * If we are starting a connection, send ECN setup
1240 * SYN packet. If we are on a retransmit, we may
1241 * resend those bits a number of times as per
1242 * RFC 3168.
1243 */
1244 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
1245 if (tp->t_flags & TF_SYN_REXMT) {
1246 if (tp->t_ecn_retries--)
1247 flags |= TH_ECE|TH_CWR;
1248 } else {
1249 flags |= TH_ECE|TH_CWR;
1250 tp->t_ecn_retries = tcp_ecn_maxretries;
1251 }
1252 }
1253
1254 if (TCP_ECN_ALLOWED(tp)) {
1255 /*
1256 * If the peer has ECN, mark data packets
1257 * ECN capable. Ignore pure ack packets, retransmissions
1258 * and window probes.
1259 */
1260 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1261 !(tp->t_force && len == 1)) {
1262 switch (af) {
1263 #ifdef INET
1264 case AF_INET:
1265 tp->t_inpcb->inp_ip.ip_tos |= IPTOS_ECN_ECT0;
1266 break;
1267 #endif
1268 #ifdef INET6
1269 case AF_INET6:
1270 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1271 break;
1272 #endif
1273 }
1274 tcpstat.tcps_ecn_ect++;
1275 }
1276
1277 /*
1278 * Reply with proper ECN notifications.
1279 */
1280 if (tp->t_flags & TF_ECN_SND_CWR) {
1281 flags |= TH_CWR;
1282 tp->t_flags &= ~TF_ECN_SND_CWR;
1283 }
1284 if (tp->t_flags & TF_ECN_SND_ECE) {
1285 flags |= TH_ECE;
1286 }
1287 }
1288
1289
1290 /*
1291 * If we are doing retransmissions, then snd_nxt will
1292 * not reflect the first unsent octet. For ACK only
1293 * packets, we do not want the sequence number of the
1294 * retransmitted packet, we want the sequence number
1295 * of the next unsent octet. So, if there is no data
1296 * (and no SYN or FIN), use snd_max instead of snd_nxt
1297 * when filling in ti_seq. But if we are in persist
1298 * state, snd_max might reflect one byte beyond the
1299 * right edge of the window, so use snd_nxt in that
1300 * case, since we know we aren't doing a retransmission.
1301 * (retransmit and persist are mutually exclusive...)
1302 */
1303 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1304 th->th_seq = htonl(p->rxmit);
1305 p->rxmit += len;
1306 } else {
1307 if (len || (flags & (TH_SYN|TH_FIN)) ||
1308 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1309 th->th_seq = htonl(tp->snd_nxt);
1310 else
1311 th->th_seq = htonl(tp->snd_max);
1312 }
1313 th->th_ack = htonl(tp->rcv_nxt);
1314 if (optlen) {
1315 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
1316 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1317 }
1318 th->th_flags = flags;
1319 /*
1320 * Calculate receive window. Don't shrink window,
1321 * but avoid silly window syndrome.
1322 */
1323 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1324 win = 0;
1325 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1326 win = (long)TCP_MAXWIN << tp->rcv_scale;
1327 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1328 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1329 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1330 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1331 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1332 if (urp > IP_MAXPACKET)
1333 urp = IP_MAXPACKET;
1334 th->th_urp = htons((u_int16_t)urp);
1335 th->th_flags |= TH_URG;
1336 } else
1337 /*
1338 * If no urgent pointer to send, then we pull
1339 * the urgent pointer to the left edge of the send window
1340 * so that it doesn't drift into the send window on sequence
1341 * number wraparound.
1342 */
1343 tp->snd_up = tp->snd_una; /* drag it along */
1344
1345 #ifdef TCP_SIGNATURE
1346 #if defined(INET6) && defined(FAST_IPSEC)
1347 if (tp->t_family == AF_INET) /* XXX */
1348 #endif
1349 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1350 struct secasvar *sav;
1351 u_int8_t *sigp;
1352
1353 sav = tcp_signature_getsav(m, th);
1354
1355 if (sav == NULL) {
1356 if (m)
1357 m_freem(m);
1358 return (EPERM);
1359 }
1360
1361 m->m_pkthdr.len = hdrlen + len;
1362 sigp = (caddr_t)th + sizeof(*th) + sigoff;
1363 tcp_signature(m, th, (caddr_t)th - mtod(m, caddr_t), sav, sigp);
1364
1365 key_sa_recordxfer(sav, m);
1366 #ifdef FAST_IPSEC
1367 KEY_FREESAV(&sav);
1368 #else
1369 key_freesav(sav);
1370 #endif
1371 }
1372 #endif
1373
1374 /*
1375 * Set ourselves up to be checksummed just before the packet
1376 * hits the wire.
1377 */
1378 switch (af) {
1379 #ifdef INET
1380 case AF_INET:
1381 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1382 if (use_tso) {
1383 m->m_pkthdr.segsz = txsegsize;
1384 m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
1385 } else {
1386 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1387 if (len + optlen) {
1388 /* Fixup the pseudo-header checksum. */
1389 /* XXXJRT Not IP Jumbogram safe. */
1390 th->th_sum = in_cksum_addword(th->th_sum,
1391 htons((u_int16_t) (len + optlen)));
1392 }
1393 }
1394 break;
1395 #endif
1396 #ifdef INET6
1397 case AF_INET6:
1398 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1399 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1400 if (len + optlen) {
1401 /* Fixup the pseudo-header checksum. */
1402 /* XXXJRT: Not IPv6 Jumbogram safe. */
1403 th->th_sum = in_cksum_addword(th->th_sum,
1404 htons((u_int16_t) (len + optlen)));
1405 }
1406 break;
1407 #endif
1408 }
1409
1410 /*
1411 * In transmit state, time the transmission and arrange for
1412 * the retransmit. In persist state, just set snd_max.
1413 */
1414 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1415 tcp_seq startseq = tp->snd_nxt;
1416
1417 /*
1418 * Advance snd_nxt over sequence space of this segment.
1419 * There are no states in which we send both a SYN and a FIN,
1420 * so we collapse the tests for these flags.
1421 */
1422 if (flags & (TH_SYN|TH_FIN))
1423 tp->snd_nxt++;
1424 if (sack_rxmit)
1425 goto timer;
1426 tp->snd_nxt += len;
1427 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1428 tp->snd_max = tp->snd_nxt;
1429 /*
1430 * Time this transmission if not a retransmission and
1431 * not currently timing anything.
1432 */
1433 if (tp->t_rtttime == 0) {
1434 tp->t_rtttime = tcp_now;
1435 tp->t_rtseq = startseq;
1436 tcpstat.tcps_segstimed++;
1437 }
1438 }
1439
1440 /*
1441 * Set retransmit timer if not currently set,
1442 * and not doing an ack or a keep-alive probe.
1443 * Initial value for retransmit timer is smoothed
1444 * round-trip time + 2 * round-trip time variance.
1445 * Initialize shift counter which is used for backoff
1446 * of retransmit time.
1447 */
1448 timer:
1449 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1450 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1451 tp->snd_nxt != tp->snd_una)) {
1452 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1453 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1454 tp->t_rxtshift = 0;
1455 }
1456 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1457 }
1458 } else
1459 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1460 tp->snd_max = tp->snd_nxt + len;
1461
1462 #ifdef TCP_DEBUG
1463 /*
1464 * Trace.
1465 */
1466 if (so->so_options & SO_DEBUG)
1467 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1468 #endif
1469
1470 /*
1471 * Fill in IP length and desired time to live and
1472 * send to IP level. There should be a better way
1473 * to handle ttl and tos; we could keep them in
1474 * the template, but need a way to checksum without them.
1475 */
1476 m->m_pkthdr.len = hdrlen + len;
1477
1478 switch (af) {
1479 #ifdef INET
1480 case AF_INET:
1481 ip->ip_len = htons(m->m_pkthdr.len);
1482 packetlen = m->m_pkthdr.len;
1483 if (tp->t_inpcb) {
1484 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1485 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1486 }
1487 #ifdef INET6
1488 else if (tp->t_in6pcb) {
1489 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1490 ip->ip_tos = 0; /*XXX*/
1491 }
1492 #endif
1493 break;
1494 #endif
1495 #ifdef INET6
1496 case AF_INET6:
1497 packetlen = m->m_pkthdr.len;
1498 ip6->ip6_nxt = IPPROTO_TCP;
1499 if (tp->t_in6pcb) {
1500 /*
1501 * we separately set hoplimit for every segment, since
1502 * the user might want to change the value via
1503 * setsockopt. Also, desired default hop limit might
1504 * be changed via Neighbor Discovery.
1505 */
1506 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
1507 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
1508 }
1509 /* ip6->ip6_flow = ??? */
1510 /* ip6_plen will be filled in ip6_output(). */
1511 break;
1512 #endif
1513 default: /*pacify gcc*/
1514 packetlen = 0;
1515 break;
1516 }
1517
1518 switch (af) {
1519 #ifdef INET
1520 case AF_INET:
1521 {
1522 struct mbuf *opts;
1523
1524 if (tp->t_inpcb)
1525 opts = tp->t_inpcb->inp_options;
1526 else
1527 opts = NULL;
1528 error = ip_output(m, opts, ro,
1529 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1530 (so->so_options & SO_DONTROUTE),
1531 (struct ip_moptions *)0, so);
1532 break;
1533 }
1534 #endif
1535 #ifdef INET6
1536 case AF_INET6:
1537 {
1538 struct ip6_pktopts *opts;
1539
1540 if (tp->t_in6pcb)
1541 opts = tp->t_in6pcb->in6p_outputopts;
1542 else
1543 opts = NULL;
1544 error = ip6_output(m, opts, (struct route_in6 *)ro,
1545 so->so_options & SO_DONTROUTE,
1546 (struct ip6_moptions *)0, so, NULL);
1547 break;
1548 }
1549 #endif
1550 default:
1551 error = EAFNOSUPPORT;
1552 break;
1553 }
1554 if (error) {
1555 out:
1556 if (error == ENOBUFS) {
1557 tcpstat.tcps_selfquench++;
1558 #ifdef INET
1559 if (tp->t_inpcb)
1560 tcp_quench(tp->t_inpcb, 0);
1561 #endif
1562 #ifdef INET6
1563 if (tp->t_in6pcb)
1564 tcp6_quench(tp->t_in6pcb, 0);
1565 #endif
1566 error = 0;
1567 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1568 TCPS_HAVERCVDSYN(tp->t_state)) {
1569 tp->t_softerror = error;
1570 error = 0;
1571 }
1572
1573 /* Back out the seqence number advance. */
1574 if (sack_rxmit)
1575 p->rxmit -= len;
1576
1577 /* Restart the delayed ACK timer, if necessary. */
1578 if (tp->t_flags & TF_DELACK)
1579 TCP_RESTART_DELACK(tp);
1580
1581 return (error);
1582 }
1583
1584 if (packetlen > tp->t_pmtud_mtu_sent)
1585 tp->t_pmtud_mtu_sent = packetlen;
1586
1587 tcpstat.tcps_sndtotal++;
1588 if (tp->t_flags & TF_DELACK)
1589 tcpstat.tcps_delack++;
1590
1591 /*
1592 * Data sent (as far as we can tell).
1593 * If this advertises a larger window than any other segment,
1594 * then remember the size of the advertised window.
1595 * Any pending ACK has now been sent.
1596 */
1597 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1598 tp->rcv_adv = tp->rcv_nxt + win;
1599 tp->last_ack_sent = tp->rcv_nxt;
1600 tp->t_flags &= ~TF_ACKNOW;
1601 TCP_CLEAR_DELACK(tp);
1602 #ifdef DIAGNOSTIC
1603 if (maxburst < 0)
1604 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1605 #endif
1606 if (sendalot && (!tcp_do_newreno || --maxburst))
1607 goto again;
1608 return (0);
1609 }
1610
1611 void
1612 tcp_setpersist(struct tcpcb *tp)
1613 {
1614 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1615 int nticks;
1616
1617 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1618 panic("tcp_output REXMT");
1619 /*
1620 * Start/restart persistance timer.
1621 */
1622 if (t < tp->t_rttmin)
1623 t = tp->t_rttmin;
1624 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1625 TCPTV_PERSMIN, TCPTV_PERSMAX);
1626 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1627 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1628 tp->t_rxtshift++;
1629 }
1630
1631 #if defined(INET)
1632 /*
1633 * tcp4_segment: handle M_CSUM_TSOv4 by software.
1634 *
1635 * => always consume m.
1636 * => call output_func with output_arg for each segments.
1637 */
1638
1639 int
1640 tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
1641 void *output_arg)
1642 {
1643 int mss;
1644 int iphlen;
1645 int thlen;
1646 int hlen;
1647 int len;
1648 struct ip *iph;
1649 struct tcphdr *th;
1650 uint16_t ipid;
1651 uint32_t tcpseq;
1652 struct mbuf *hdr = NULL;
1653 struct mbuf *t;
1654 int error = 0;
1655
1656 KASSERT((m->m_flags & M_PKTHDR) != 0);
1657 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
1658
1659 m->m_pkthdr.csum_flags = 0;
1660
1661 len = m->m_pkthdr.len;
1662 KASSERT(len >= sizeof(*iph) + sizeof(*th));
1663
1664 if (m->m_len < sizeof(*iph)) {
1665 m = m_pullup(m, sizeof(*iph));
1666 if (m == NULL) {
1667 error = ENOMEM;
1668 goto quit;
1669 }
1670 }
1671 iph = mtod(m, struct ip *);
1672 iphlen = iph->ip_hl * 4;
1673 KASSERT(iph->ip_v == IPVERSION);
1674 KASSERT(iphlen >= sizeof(*iph));
1675 KASSERT(iph->ip_p == IPPROTO_TCP);
1676 ipid = ntohs(iph->ip_id);
1677
1678 hlen = iphlen + sizeof(*th);
1679 if (m->m_len < hlen) {
1680 m = m_pullup(m, hlen);
1681 if (m == NULL) {
1682 error = ENOMEM;
1683 goto quit;
1684 }
1685 }
1686 th = (void *)(mtod(m, char *) + iphlen);
1687 tcpseq = ntohl(th->th_seq);
1688 thlen = th->th_off * 4;
1689 hlen = iphlen + thlen;
1690
1691 mss = m->m_pkthdr.segsz;
1692 KASSERT(mss != 0);
1693 KASSERT(len > hlen);
1694
1695 t = m_split(m, hlen, M_NOWAIT);
1696 if (t == NULL) {
1697 error = ENOMEM;
1698 goto quit;
1699 }
1700 hdr = m;
1701 m = t;
1702 len -= hlen;
1703 KASSERT(len % mss == 0);
1704 while (len > 0) {
1705 struct mbuf *n;
1706
1707 n = m_dup(hdr, 0, hlen, M_NOWAIT);
1708 if (n == NULL) {
1709 error = ENOMEM;
1710 goto quit;
1711 }
1712 KASSERT(n->m_len == hlen); /* XXX */
1713
1714 t = m_split(m, mss, M_NOWAIT);
1715 if (t == NULL) {
1716 m_freem(n);
1717 error = ENOMEM;
1718 goto quit;
1719 }
1720 m_cat(n, m);
1721 m = t;
1722
1723 KASSERT(n->m_len >= hlen); /* XXX */
1724
1725 n->m_pkthdr.len = hlen + mss;
1726 iph = mtod(n, struct ip *);
1727 KASSERT(iph->ip_v == IPVERSION);
1728 iph->ip_len = htons(n->m_pkthdr.len);
1729 iph->ip_id = htons(ipid);
1730 th = (void *)(mtod(n, char *) + iphlen);
1731 th->th_seq = htonl(tcpseq);
1732 iph->ip_sum = 0;
1733 iph->ip_sum = in_cksum(n, iphlen);
1734 th->th_sum = 0;
1735 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
1736
1737 error = (*output_func)(output_arg, n);
1738 if (error) {
1739 goto quit;
1740 }
1741
1742 tcpseq += mss;
1743 ipid++;
1744 len -= mss;
1745 }
1746
1747 quit:
1748 if (hdr != NULL) {
1749 m_freem(hdr);
1750 }
1751 if (m != NULL) {
1752 m_freem(m);
1753 }
1754
1755 return error;
1756 }
1757 #endif /* defined(INET) */
1758