tcp_output.c revision 1.144 1 /* $NetBSD: tcp_output.c,v 1.144 2006/09/28 04:32:47 dbj 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.144 2006/09/28 04:32:47 dbj 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/in6_pcb.h>
182 #include <netinet6/nd6.h>
183 #endif
184
185 #ifdef FAST_IPSEC
186 #include <netipsec/ipsec.h>
187 #include <netipsec/key.h>
188 #endif /* FAST_IPSEC*/
189 #ifdef IPSEC
190 #include <netinet6/ipsec.h>
191 #endif
192
193 #include <netinet/tcp.h>
194 #define TCPOUTFLAGS
195 #include <netinet/tcp_fsm.h>
196 #include <netinet/tcp_seq.h>
197 #include <netinet/tcp_timer.h>
198 #include <netinet/tcp_var.h>
199 #include <netinet/tcpip.h>
200 #include <netinet/tcp_debug.h>
201 #include <netinet/in_offload.h>
202
203 #ifdef IPSEC
204 #include <netkey/key.h>
205 #endif
206
207 #ifdef notyet
208 extern struct mbuf *m_copypack();
209 #endif
210
211 /*
212 * Knob to enable Congestion Window Monitoring, and control
213 * the burst size it allows. Default burst is 4 packets, per
214 * the Internet draft.
215 */
216 int tcp_cwm = 0;
217 int tcp_cwm_burstsize = 4;
218
219 #ifdef TCP_OUTPUT_COUNTERS
220 #include <sys/device.h>
221
222 extern struct evcnt tcp_output_bigheader;
223 extern struct evcnt tcp_output_predict_hit;
224 extern struct evcnt tcp_output_predict_miss;
225 extern struct evcnt tcp_output_copysmall;
226 extern struct evcnt tcp_output_copybig;
227 extern struct evcnt tcp_output_refbig;
228
229 #define TCP_OUTPUT_COUNTER_INCR(ev) (ev)->ev_count++
230 #else
231
232 #define TCP_OUTPUT_COUNTER_INCR(ev) /* nothing */
233
234 #endif /* TCP_OUTPUT_COUNTERS */
235
236 static
237 #ifndef GPROF
238 inline
239 #endif
240 int
241 tcp_segsize(struct tcpcb *tp, int *txsegsizep, int *rxsegsizep)
242 {
243 #ifdef INET
244 struct inpcb *inp = tp->t_inpcb;
245 #endif
246 #ifdef INET6
247 struct in6pcb *in6p = tp->t_in6pcb;
248 #endif
249 struct socket *so = NULL;
250 struct rtentry *rt;
251 struct ifnet *ifp;
252 int size;
253 int hdrlen;
254 int optlen;
255
256 #ifdef DIAGNOSTIC
257 if (tp->t_inpcb && tp->t_in6pcb)
258 panic("tcp_segsize: both t_inpcb and t_in6pcb are set");
259 #endif
260 switch (tp->t_family) {
261 #ifdef INET
262 case AF_INET:
263 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
264 break;
265 #endif
266 #ifdef INET6
267 case AF_INET6:
268 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
269 break;
270 #endif
271 default:
272 size = tcp_mssdflt;
273 goto out;
274 }
275
276 rt = NULL;
277 #ifdef INET
278 if (inp) {
279 rt = in_pcbrtentry(inp);
280 so = inp->inp_socket;
281 }
282 #endif
283 #ifdef INET6
284 if (in6p) {
285 rt = in6_pcbrtentry(in6p);
286 so = in6p->in6p_socket;
287 }
288 #endif
289 if (rt == NULL) {
290 size = tcp_mssdflt;
291 goto out;
292 }
293
294 ifp = rt->rt_ifp;
295
296 size = tcp_mssdflt;
297 if (tp->t_mtudisc && rt->rt_rmx.rmx_mtu != 0) {
298 #ifdef INET6
299 if (in6p && rt->rt_rmx.rmx_mtu < IPV6_MMTU) {
300 /*
301 * RFC2460 section 5, last paragraph: if path MTU is
302 * smaller than 1280, use 1280 as packet size and
303 * attach fragment header.
304 */
305 size = IPV6_MMTU - hdrlen - sizeof(struct ip6_frag);
306 } else
307 size = rt->rt_rmx.rmx_mtu - hdrlen;
308 #else
309 size = rt->rt_rmx.rmx_mtu - hdrlen;
310 #endif
311 } else if (ifp->if_flags & IFF_LOOPBACK)
312 size = ifp->if_mtu - hdrlen;
313 #ifdef INET
314 else if (inp && tp->t_mtudisc)
315 size = ifp->if_mtu - hdrlen;
316 else if (inp && in_localaddr(inp->inp_faddr))
317 size = ifp->if_mtu - hdrlen;
318 #endif
319 #ifdef INET6
320 else if (in6p) {
321 #ifdef INET
322 if (IN6_IS_ADDR_V4MAPPED(&in6p->in6p_faddr)) {
323 /* mapped addr case */
324 struct in_addr d;
325 bcopy(&in6p->in6p_faddr.s6_addr32[3], &d, sizeof(d));
326 if (tp->t_mtudisc || in_localaddr(d))
327 size = ifp->if_mtu - hdrlen;
328 } else
329 #endif
330 {
331 /*
332 * for IPv6, path MTU discovery is always turned on,
333 * or the node must use packet size <= 1280.
334 */
335 size = tp->t_mtudisc ? IN6_LINKMTU(ifp) : IPV6_MMTU;
336 size -= hdrlen;
337 }
338 }
339 #endif
340 out:
341 /*
342 * Now we must make room for whatever extra TCP/IP options are in
343 * the packet.
344 */
345 optlen = tcp_optlen(tp);
346
347 /*
348 * XXX tp->t_ourmss should have the right size, but without this code
349 * fragmentation will occur... need more investigation
350 */
351 #ifdef INET
352 if (inp) {
353 #if defined(IPSEC) || defined(FAST_IPSEC)
354 if (! IPSEC_PCB_SKIP_IPSEC(inp->inp_sp, IPSEC_DIR_OUTBOUND))
355 optlen += ipsec4_hdrsiz_tcp(tp);
356 #endif
357 optlen += ip_optlen(inp);
358 }
359 #endif
360 #ifdef INET6
361 #ifdef INET
362 if (in6p && tp->t_family == AF_INET) {
363 #if defined(IPSEC) || defined(FAST_IPSEC)
364 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
365 optlen += ipsec4_hdrsiz_tcp(tp);
366 #endif
367 /* XXX size -= ip_optlen(in6p); */
368 } else
369 #endif
370 if (in6p && tp->t_family == AF_INET6) {
371 #ifdef IPSEC
372 if (! IPSEC_PCB_SKIP_IPSEC(in6p->in6p_sp, IPSEC_DIR_OUTBOUND))
373 optlen += ipsec6_hdrsiz_tcp(tp);
374 #endif
375 optlen += ip6_optlen(in6p);
376 }
377 #endif
378 size -= optlen;
379
380 /* there may not be any room for data if mtu is too small */
381 if (size < 0)
382 return (EMSGSIZE);
383
384 /*
385 * *rxsegsizep holds *estimated* inbound segment size (estimation
386 * assumes that path MTU is the same for both ways). this is only
387 * for silly window avoidance, do not use the value for other purposes.
388 *
389 * ipseclen is subtracted from both sides, this may not be right.
390 * I'm not quite sure about this (could someone comment).
391 */
392 *txsegsizep = min(tp->t_peermss - optlen, size);
393 /*
394 * Never send more than half a buffer full. This insures that we can
395 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
396 * therefore acks will never be delayed unless we run out of data to
397 * transmit.
398 */
399 if (so)
400 *txsegsizep = min((so->so_snd.sb_hiwat -
401 so->so_snd.sb_lowat + 1) >> 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, packetlen;
555 unsigned int sack_numblks;
556 int idle, sendalot, txsegsize, rxsegsize;
557 int txsegsize_nosack;
558 int maxburst = TCP_MAXBURST;
559 int af; /* address family on the wire */
560 int iphdrlen;
561 int has_tso, use_tso;
562 int sack_rxmit;
563 int sack_bytes_rxmt;
564 struct sackhole *p;
565 #ifdef TCP_SIGNATURE
566 int sigoff = 0;
567 #endif
568
569 #ifdef DIAGNOSTIC
570 if (tp->t_inpcb && tp->t_in6pcb)
571 panic("tcp_output: both t_inpcb and t_in6pcb are set");
572 #endif
573 so = NULL;
574 ro = NULL;
575 if (tp->t_inpcb) {
576 so = tp->t_inpcb->inp_socket;
577 ro = &tp->t_inpcb->inp_route;
578 }
579 #ifdef INET6
580 else if (tp->t_in6pcb) {
581 so = tp->t_in6pcb->in6p_socket;
582 ro = (struct route *)&tp->t_in6pcb->in6p_route;
583 }
584 #endif
585
586 switch (af = tp->t_family) {
587 #ifdef INET
588 case AF_INET:
589 if (tp->t_inpcb)
590 break;
591 #ifdef INET6
592 /* mapped addr case */
593 if (tp->t_in6pcb)
594 break;
595 #endif
596 return (EINVAL);
597 #endif
598 #ifdef INET6
599 case AF_INET6:
600 if (tp->t_in6pcb)
601 break;
602 return (EINVAL);
603 #endif
604 default:
605 return (EAFNOSUPPORT);
606 }
607
608 if (tcp_segsize(tp, &txsegsize, &rxsegsize))
609 return (EMSGSIZE);
610
611 idle = (tp->snd_max == tp->snd_una);
612
613 /*
614 * Determine if we can use TCP segmentation offload:
615 * - If we're using IPv4
616 * - If there is not an IPsec policy that prevents it
617 * - If the interface can do it
618 */
619 has_tso = tp->t_inpcb != NULL &&
620 #if defined(IPSEC) || defined(FAST_IPSEC)
621 IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
622 IPSEC_DIR_OUTBOUND) &&
623 #endif
624 tp->t_inpcb->inp_route.ro_rt != NULL &&
625 (tp->t_inpcb->inp_route.ro_rt->rt_ifp->if_capenable &
626 IFCAP_TSOv4) != 0;
627
628 /*
629 * Restart Window computation. From draft-floyd-incr-init-win-03:
630 *
631 * Optionally, a TCP MAY set the restart window to the
632 * minimum of the value used for the initial window and
633 * the current value of cwnd (in other words, using a
634 * larger value for the restart window should never increase
635 * the size of cwnd).
636 */
637 if (tcp_cwm) {
638 /*
639 * Hughes/Touch/Heidemann Congestion Window Monitoring.
640 * Count the number of packets currently pending
641 * acknowledgement, and limit our congestion window
642 * to a pre-determined allowed burst size plus that count.
643 * This prevents bursting once all pending packets have
644 * been acknowledged (i.e. transmission is idle).
645 *
646 * XXX Link this to Initial Window?
647 */
648 tp->snd_cwnd = min(tp->snd_cwnd,
649 (tcp_cwm_burstsize * txsegsize) +
650 (tp->snd_nxt - tp->snd_una));
651 } else {
652 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
653 /*
654 * We have been idle for "a while" and no acks are
655 * expected to clock out any data we send --
656 * slow start to get ack "clock" running again.
657 */
658 int ss = tcp_init_win;
659 #ifdef INET
660 if (tp->t_inpcb &&
661 in_localaddr(tp->t_inpcb->inp_faddr))
662 ss = tcp_init_win_local;
663 #endif
664 #ifdef INET6
665 if (tp->t_in6pcb &&
666 in6_localaddr(&tp->t_in6pcb->in6p_faddr))
667 ss = tcp_init_win_local;
668 #endif
669 tp->snd_cwnd = min(tp->snd_cwnd,
670 TCP_INITIAL_WINDOW(ss, txsegsize));
671 }
672 }
673
674 txsegsize_nosack = txsegsize;
675 again:
676 use_tso = has_tso;
677 TCP_REASS_LOCK(tp);
678 sack_numblks = tcp_sack_numblks(tp);
679 if (sack_numblks) {
680 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
681 /* don't duplicate D-SACK. */
682 use_tso = 0;
683 }
684 txsegsize = txsegsize_nosack - TCP_SACK_OPTLEN(sack_numblks);
685 } else {
686 txsegsize = txsegsize_nosack;
687 }
688
689 /*
690 * Determine length of data that should be transmitted, and
691 * flags that should be used. If there is some data or critical
692 * controls (SYN, RST) to send, then transmit; otherwise,
693 * investigate further.
694 *
695 * Readjust SACK information to avoid resending duplicate data.
696 */
697 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
698 tcp_sack_adjust(tp);
699 sendalot = 0;
700 off = tp->snd_nxt - tp->snd_una;
701 win = min(tp->snd_wnd, tp->snd_cwnd);
702
703 flags = tcp_outflags[tp->t_state];
704
705 /*
706 * Send any SACK-generated retransmissions. If we're explicitly trying
707 * to send out new data (when sendalot is 1), bypass this function.
708 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
709 * we're replacing a (future) new transmission with a retransmission
710 * now, and we previously incremented snd_cwnd in tcp_input().
711 */
712 /*
713 * Still in sack recovery , reset rxmit flag to zero.
714 */
715 sack_rxmit = 0;
716 sack_bytes_rxmt = 0;
717 len = 0;
718 p = NULL;
719 do {
720 long cwin;
721 if (!TCP_SACK_ENABLED(tp))
722 break;
723 if (tp->t_partialacks < 0)
724 break;
725 p = tcp_sack_output(tp, &sack_bytes_rxmt);
726 if (p == NULL)
727 break;
728
729 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
730 if (cwin < 0)
731 cwin = 0;
732 /* Do not retransmit SACK segments beyond snd_recover */
733 if (SEQ_GT(p->end, tp->snd_recover)) {
734 /*
735 * (At least) part of sack hole extends beyond
736 * snd_recover. Check to see if we can rexmit data
737 * for this hole.
738 */
739 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
740 /*
741 * Can't rexmit any more data for this hole.
742 * That data will be rexmitted in the next
743 * sack recovery episode, when snd_recover
744 * moves past p->rxmit.
745 */
746 p = NULL;
747 break;
748 }
749 /* Can rexmit part of the current hole */
750 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
751 } else
752 len = ((long)ulmin(cwin, p->end - p->rxmit));
753 off = p->rxmit - tp->snd_una;
754 if (off + len > so->so_snd.sb_cc) {
755 /* 1 for TH_FIN */
756 KASSERT(off + len == so->so_snd.sb_cc + 1);
757 KASSERT(p->rxmit + len == tp->snd_max);
758 len = so->so_snd.sb_cc - off;
759 }
760 if (len > 0) {
761 sack_rxmit = 1;
762 sendalot = 1;
763 }
764 } while (/*CONSTCOND*/0);
765
766 /*
767 * If in persist timeout with window of 0, send 1 byte.
768 * Otherwise, if window is small but nonzero
769 * and timer expired, we will send what we can
770 * and go to transmit state.
771 */
772 if (tp->t_force) {
773 if (win == 0) {
774 /*
775 * If we still have some data to send, then
776 * clear the FIN bit. Usually this would
777 * happen below when it realizes that we
778 * aren't sending all the data. However,
779 * if we have exactly 1 byte of unset data,
780 * then it won't clear the FIN bit below,
781 * and if we are in persist state, we wind
782 * up sending the packet without recording
783 * that we sent the FIN bit.
784 *
785 * We can't just blindly clear the FIN bit,
786 * because if we don't have any more data
787 * to send then the probe will be the FIN
788 * itself.
789 */
790 if (off < so->so_snd.sb_cc)
791 flags &= ~TH_FIN;
792 win = 1;
793 } else {
794 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
795 tp->t_rxtshift = 0;
796 }
797 }
798
799 if (!TCP_SACK_ENABLED(tp)) {
800 if (win < so->so_snd.sb_cc) {
801 len = win - off;
802 flags &= ~TH_FIN;
803 } else
804 len = so->so_snd.sb_cc - off;
805 } else if (sack_rxmit == 0) {
806 if (sack_bytes_rxmt != 0) {
807 long cwin;
808
809 /*
810 * We are inside of a SACK recovery episode and are
811 * sending new data, having retransmitted all the
812 * data possible in the scoreboard.
813 */
814 if (tp->snd_wnd < so->so_snd.sb_cc) {
815 len = tp->snd_wnd - off;
816 flags &= ~TH_FIN;
817 } else {
818 len = so->so_snd.sb_cc - off;
819 }
820
821 /*
822 * From FreeBSD:
823 * Don't remove this (len > 0) check !
824 * We explicitly check for len > 0 here (although it
825 * isn't really necessary), to work around a gcc
826 * optimization issue - to force gcc to compute
827 * len above. Without this check, the computation
828 * of len is bungled by the optimizer.
829 */
830 if (len > 0) {
831 cwin = tp->snd_cwnd -
832 (tp->snd_nxt - tp->sack_newdata) -
833 sack_bytes_rxmt;
834 if (cwin < 0)
835 cwin = 0;
836 if (cwin < len) {
837 len = cwin;
838 flags &= ~TH_FIN;
839 }
840 }
841 } else if (win < so->so_snd.sb_cc) {
842 len = win - off;
843 flags &= ~TH_FIN;
844 } else
845 len = so->so_snd.sb_cc - off;
846 }
847
848 if (len < 0) {
849 /*
850 * If FIN has been sent but not acked,
851 * but we haven't been called to retransmit,
852 * len will be -1. Otherwise, window shrank
853 * after we sent into it. If window shrank to 0,
854 * cancel pending retransmit, pull snd_nxt back
855 * to (closed) window, and set the persist timer
856 * if it isn't already going. If the window didn't
857 * close completely, just wait for an ACK.
858 *
859 * If we have a pending FIN, either it has already been
860 * transmitted or it is outside the window, so drop it.
861 * If the FIN has been transmitted, but this is not a
862 * retransmission, then len must be -1. Therefore we also
863 * prevent here the sending of `gratuitous FINs'. This
864 * eliminates the need to check for that case below (e.g.
865 * to back up snd_nxt before the FIN so that the sequence
866 * number is correct).
867 */
868 len = 0;
869 flags &= ~TH_FIN;
870 if (win == 0) {
871 TCP_TIMER_DISARM(tp, TCPT_REXMT);
872 tp->t_rxtshift = 0;
873 tp->snd_nxt = tp->snd_una;
874 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
875 tcp_setpersist(tp);
876 }
877 }
878 if (len > txsegsize) {
879 if (use_tso) {
880 /*
881 * Truncate TSO transfers to IP_MAXPACKET, and make
882 * sure that we send equal size transfers down the
883 * stack (rather than big-small-big-small-...).
884 */
885 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
886 if (len <= txsegsize) {
887 use_tso = 0;
888 }
889 } else
890 len = txsegsize;
891 flags &= ~TH_FIN;
892 sendalot = 1;
893 } else
894 use_tso = 0;
895 if (sack_rxmit) {
896 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
897 flags &= ~TH_FIN;
898 }
899
900 win = sbspace(&so->so_rcv);
901
902 /*
903 * Sender silly window avoidance. If connection is idle
904 * and can send all data, a maximum segment,
905 * at least a maximum default-size segment do it,
906 * or are forced, do it; otherwise don't bother.
907 * If peer's buffer is tiny, then send
908 * when window is at least half open.
909 * If retransmitting (possibly after persist timer forced us
910 * to send into a small window), then must resend.
911 */
912 if (len) {
913 if (len >= txsegsize)
914 goto send;
915 if ((so->so_state & SS_MORETOCOME) == 0 &&
916 ((idle || tp->t_flags & TF_NODELAY) &&
917 len + off >= so->so_snd.sb_cc))
918 goto send;
919 if (tp->t_force)
920 goto send;
921 if (len >= tp->max_sndwnd / 2)
922 goto send;
923 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
924 goto send;
925 if (sack_rxmit)
926 goto send;
927 }
928
929 /*
930 * Compare available window to amount of window known to peer
931 * (as advertised window less next expected input). If the
932 * difference is at least twice the size of the largest segment
933 * we expect to receive (i.e. two segments) or at least 50% of
934 * the maximum possible window, then want to send a window update
935 * to peer.
936 */
937 if (win > 0) {
938 /*
939 * "adv" is the amount we can increase the window,
940 * taking into account that we are limited by
941 * TCP_MAXWIN << tp->rcv_scale.
942 */
943 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
944 (tp->rcv_adv - tp->rcv_nxt);
945
946 if (adv >= (long) (2 * rxsegsize))
947 goto send;
948 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
949 goto send;
950 }
951
952 /*
953 * Send if we owe peer an ACK.
954 */
955 if (tp->t_flags & TF_ACKNOW)
956 goto send;
957 if (flags & (TH_SYN|TH_FIN|TH_RST))
958 goto send;
959 if (SEQ_GT(tp->snd_up, tp->snd_una))
960 goto send;
961 /*
962 * In SACK, it is possible for tcp_output to fail to send a segment
963 * after the retransmission timer has been turned off. Make sure
964 * that the retransmission timer is set.
965 */
966 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
967 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
968 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
969 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
970 goto just_return;
971 }
972
973 /*
974 * TCP window updates are not reliable, rather a polling protocol
975 * using ``persist'' packets is used to insure receipt of window
976 * updates. The three ``states'' for the output side are:
977 * idle not doing retransmits or persists
978 * persisting to move a small or zero window
979 * (re)transmitting and thereby not persisting
980 *
981 * tp->t_timer[TCPT_PERSIST]
982 * is set when we are in persist state.
983 * tp->t_force
984 * is set when we are called to send a persist packet.
985 * tp->t_timer[TCPT_REXMT]
986 * is set when we are retransmitting
987 * The output side is idle when both timers are zero.
988 *
989 * If send window is too small, there is data to transmit, and no
990 * retransmit or persist is pending, then go to persist state.
991 * If nothing happens soon, send when timer expires:
992 * if window is nonzero, transmit what we can,
993 * otherwise force out a byte.
994 */
995 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
996 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
997 tp->t_rxtshift = 0;
998 tcp_setpersist(tp);
999 }
1000
1001 /*
1002 * No reason to send a segment, just return.
1003 */
1004 just_return:
1005 TCP_REASS_UNLOCK(tp);
1006 return (0);
1007
1008 send:
1009 /*
1010 * Before ESTABLISHED, force sending of initial options
1011 * unless TCP set not to do any options.
1012 * NOTE: we assume that the IP/TCP header plus TCP options
1013 * always fit in a single mbuf, leaving room for a maximum
1014 * link header, i.e.
1015 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1016 */
1017 optlen = 0;
1018 switch (af) {
1019 #ifdef INET
1020 case AF_INET:
1021 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
1022 break;
1023 #endif
1024 #ifdef INET6
1025 case AF_INET6:
1026 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1027 break;
1028 #endif
1029 default: /*pacify gcc*/
1030 iphdrlen = 0;
1031 break;
1032 }
1033 hdrlen = iphdrlen;
1034 if (flags & TH_SYN) {
1035 struct rtentry *rt;
1036
1037 rt = NULL;
1038 #ifdef INET
1039 if (tp->t_inpcb)
1040 rt = in_pcbrtentry(tp->t_inpcb);
1041 #endif
1042 #ifdef INET6
1043 if (tp->t_in6pcb)
1044 rt = in6_pcbrtentry(tp->t_in6pcb);
1045 #endif
1046
1047 tp->snd_nxt = tp->iss;
1048 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
1049 rt->rt_ifp : NULL, af);
1050 if ((tp->t_flags & TF_NOOPT) == 0) {
1051 opt[0] = TCPOPT_MAXSEG;
1052 opt[1] = 4;
1053 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1054 opt[3] = tp->t_ourmss & 0xff;
1055 optlen = 4;
1056
1057 if ((tp->t_flags & TF_REQ_SCALE) &&
1058 ((flags & TH_ACK) == 0 ||
1059 (tp->t_flags & TF_RCVD_SCALE))) {
1060 *((u_int32_t *) (opt + optlen)) = htonl(
1061 TCPOPT_NOP << 24 |
1062 TCPOPT_WINDOW << 16 |
1063 TCPOLEN_WINDOW << 8 |
1064 tp->request_r_scale);
1065 optlen += 4;
1066 }
1067 if (tcp_do_sack) {
1068 u_int8_t *cp = (u_int8_t *)(opt + optlen);
1069
1070 cp[0] = TCPOPT_SACK_PERMITTED;
1071 cp[1] = 2;
1072 cp[2] = TCPOPT_NOP;
1073 cp[3] = TCPOPT_NOP;
1074 optlen += 4;
1075 }
1076 }
1077 }
1078
1079 /*
1080 * Send a timestamp and echo-reply if this is a SYN and our side
1081 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1082 * and our peer have sent timestamps in our SYN's.
1083 */
1084 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1085 (flags & TH_RST) == 0 &&
1086 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1087 (tp->t_flags & TF_RCVD_TSTMP))) {
1088 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1089
1090 /* Form timestamp option as shown in appendix A of RFC 1323. */
1091 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1092 *lp++ = htonl(TCP_TIMESTAMP(tp));
1093 *lp = htonl(tp->ts_recent);
1094 optlen += TCPOLEN_TSTAMP_APPA;
1095 }
1096
1097 /*
1098 * Tack on the SACK block if it is necessary.
1099 */
1100 if (sack_numblks) {
1101 int sack_len;
1102 u_char *bp = (u_char *)(opt + optlen);
1103 u_int32_t *lp = (u_int32_t *)(bp + 4);
1104 struct ipqent *tiqe;
1105
1106 sack_len = sack_numblks * 8 + 2;
1107 bp[0] = TCPOPT_NOP;
1108 bp[1] = TCPOPT_NOP;
1109 bp[2] = TCPOPT_SACK;
1110 bp[3] = sack_len;
1111 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
1112 sack_numblks--;
1113 *lp++ = htonl(tp->rcv_dsack_block.left);
1114 *lp++ = htonl(tp->rcv_dsack_block.right);
1115 tp->rcv_sack_flags &= ~TCPSACK_HAVED;
1116 }
1117 for (tiqe = TAILQ_FIRST(&tp->timeq);
1118 sack_numblks > 0; tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
1119 KASSERT(tiqe != NULL);
1120 sack_numblks--;
1121 *lp++ = htonl(tiqe->ipqe_seq);
1122 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
1123 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
1124 }
1125 optlen += sack_len + 2;
1126 }
1127 TCP_REASS_UNLOCK(tp);
1128
1129 #ifdef TCP_SIGNATURE
1130 #if defined(INET6) && defined(FAST_IPSEC)
1131 if (tp->t_family == AF_INET)
1132 #endif
1133 if (tp->t_flags & TF_SIGNATURE) {
1134 u_char *bp;
1135 /*
1136 * Initialize TCP-MD5 option (RFC2385)
1137 */
1138 bp = (u_char *)opt + optlen;
1139 *bp++ = TCPOPT_SIGNATURE;
1140 *bp++ = TCPOLEN_SIGNATURE;
1141 sigoff = optlen + 2;
1142 bzero(bp, TCP_SIGLEN);
1143 bp += TCP_SIGLEN;
1144 optlen += TCPOLEN_SIGNATURE;
1145 /*
1146 * Terminate options list and maintain 32-bit alignment.
1147 */
1148 *bp++ = TCPOPT_NOP;
1149 *bp++ = TCPOPT_EOL;
1150 optlen += 2;
1151 }
1152 #endif /* TCP_SIGNATURE */
1153
1154 hdrlen += optlen;
1155
1156 #ifdef DIAGNOSTIC
1157 if (!use_tso && len > txsegsize)
1158 panic("tcp data to be sent is larger than segment");
1159 else if (use_tso && len > IP_MAXPACKET)
1160 panic("tcp data to be sent is larger than max TSO size");
1161 if (max_linkhdr + hdrlen > MCLBYTES)
1162 panic("tcphdr too big");
1163 #endif
1164
1165 /*
1166 * Grab a header mbuf, attaching a copy of data to
1167 * be transmitted, and initialize the header from
1168 * the template for sends on this connection.
1169 */
1170 if (len) {
1171 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1172 if (error)
1173 goto out;
1174 /*
1175 * If we're sending everything we've got, set PUSH.
1176 * (This will keep happy those implementations which only
1177 * give data to the user when a buffer fills or
1178 * a PUSH comes in.)
1179 */
1180 if (off + len == so->so_snd.sb_cc)
1181 flags |= TH_PUSH;
1182 } else {
1183 if (tp->t_flags & TF_ACKNOW)
1184 tcpstat.tcps_sndacks++;
1185 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1186 tcpstat.tcps_sndctrl++;
1187 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1188 tcpstat.tcps_sndurg++;
1189 else
1190 tcpstat.tcps_sndwinup++;
1191
1192 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1193 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1194 MCLGET(m, M_DONTWAIT);
1195 if ((m->m_flags & M_EXT) == 0) {
1196 m_freem(m);
1197 m = NULL;
1198 }
1199 }
1200 if (m == NULL) {
1201 error = ENOBUFS;
1202 goto out;
1203 }
1204 MCLAIM(m, &tcp_tx_mowner);
1205 m->m_data += max_linkhdr;
1206 m->m_len = hdrlen;
1207 }
1208 m->m_pkthdr.rcvif = (struct ifnet *)0;
1209 switch (af) {
1210 #ifdef INET
1211 case AF_INET:
1212 ip = mtod(m, struct ip *);
1213 #ifdef INET6
1214 ip6 = NULL;
1215 #endif
1216 th = (struct tcphdr *)(ip + 1);
1217 break;
1218 #endif
1219 #ifdef INET6
1220 case AF_INET6:
1221 ip = NULL;
1222 ip6 = mtod(m, struct ip6_hdr *);
1223 th = (struct tcphdr *)(ip6 + 1);
1224 break;
1225 #endif
1226 default: /*pacify gcc*/
1227 ip = NULL;
1228 #ifdef INET6
1229 ip6 = NULL;
1230 #endif
1231 th = NULL;
1232 break;
1233 }
1234 if (tp->t_template == 0)
1235 panic("tcp_output");
1236 if (tp->t_template->m_len < iphdrlen)
1237 panic("tcp_output");
1238 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
1239
1240 /*
1241 * If we are starting a connection, send ECN setup
1242 * SYN packet. If we are on a retransmit, we may
1243 * resend those bits a number of times as per
1244 * RFC 3168.
1245 */
1246 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
1247 if (tp->t_flags & TF_SYN_REXMT) {
1248 if (tp->t_ecn_retries--)
1249 flags |= TH_ECE|TH_CWR;
1250 } else {
1251 flags |= TH_ECE|TH_CWR;
1252 tp->t_ecn_retries = tcp_ecn_maxretries;
1253 }
1254 }
1255
1256 if (TCP_ECN_ALLOWED(tp)) {
1257 /*
1258 * If the peer has ECN, mark data packets
1259 * ECN capable. Ignore pure ack packets, retransmissions
1260 * and window probes.
1261 */
1262 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1263 !(tp->t_force && len == 1)) {
1264 switch (af) {
1265 #ifdef INET
1266 case AF_INET:
1267 tp->t_inpcb->inp_ip.ip_tos |= IPTOS_ECN_ECT0;
1268 break;
1269 #endif
1270 #ifdef INET6
1271 case AF_INET6:
1272 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1273 break;
1274 #endif
1275 }
1276 tcpstat.tcps_ecn_ect++;
1277 }
1278
1279 /*
1280 * Reply with proper ECN notifications.
1281 */
1282 if (tp->t_flags & TF_ECN_SND_CWR) {
1283 flags |= TH_CWR;
1284 tp->t_flags &= ~TF_ECN_SND_CWR;
1285 }
1286 if (tp->t_flags & TF_ECN_SND_ECE) {
1287 flags |= TH_ECE;
1288 }
1289 }
1290
1291
1292 /*
1293 * If we are doing retransmissions, then snd_nxt will
1294 * not reflect the first unsent octet. For ACK only
1295 * packets, we do not want the sequence number of the
1296 * retransmitted packet, we want the sequence number
1297 * of the next unsent octet. So, if there is no data
1298 * (and no SYN or FIN), use snd_max instead of snd_nxt
1299 * when filling in ti_seq. But if we are in persist
1300 * state, snd_max might reflect one byte beyond the
1301 * right edge of the window, so use snd_nxt in that
1302 * case, since we know we aren't doing a retransmission.
1303 * (retransmit and persist are mutually exclusive...)
1304 */
1305 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1306 th->th_seq = htonl(p->rxmit);
1307 p->rxmit += len;
1308 } else {
1309 if (len || (flags & (TH_SYN|TH_FIN)) ||
1310 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1311 th->th_seq = htonl(tp->snd_nxt);
1312 else
1313 th->th_seq = htonl(tp->snd_max);
1314 }
1315 th->th_ack = htonl(tp->rcv_nxt);
1316 if (optlen) {
1317 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
1318 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1319 }
1320 th->th_flags = flags;
1321 /*
1322 * Calculate receive window. Don't shrink window,
1323 * but avoid silly window syndrome.
1324 */
1325 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1326 win = 0;
1327 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1328 win = (long)TCP_MAXWIN << tp->rcv_scale;
1329 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1330 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1331 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1332 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1333 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1334 if (urp > IP_MAXPACKET)
1335 urp = IP_MAXPACKET;
1336 th->th_urp = htons((u_int16_t)urp);
1337 th->th_flags |= TH_URG;
1338 } else
1339 /*
1340 * If no urgent pointer to send, then we pull
1341 * the urgent pointer to the left edge of the send window
1342 * so that it doesn't drift into the send window on sequence
1343 * number wraparound.
1344 */
1345 tp->snd_up = tp->snd_una; /* drag it along */
1346
1347 #ifdef TCP_SIGNATURE
1348 #if defined(INET6) && defined(FAST_IPSEC)
1349 if (tp->t_family == AF_INET) /* XXX */
1350 #endif
1351 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1352 struct secasvar *sav;
1353 u_int8_t *sigp;
1354
1355 sav = tcp_signature_getsav(m, th);
1356
1357 if (sav == NULL) {
1358 if (m)
1359 m_freem(m);
1360 return (EPERM);
1361 }
1362
1363 m->m_pkthdr.len = hdrlen + len;
1364 sigp = (caddr_t)th + sizeof(*th) + sigoff;
1365 tcp_signature(m, th, (caddr_t)th - mtod(m, caddr_t), sav, sigp);
1366
1367 key_sa_recordxfer(sav, m);
1368 #ifdef FAST_IPSEC
1369 KEY_FREESAV(&sav);
1370 #else
1371 key_freesav(sav);
1372 #endif
1373 }
1374 #endif
1375
1376 /*
1377 * Set ourselves up to be checksummed just before the packet
1378 * hits the wire.
1379 */
1380 switch (af) {
1381 #ifdef INET
1382 case AF_INET:
1383 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1384 if (use_tso) {
1385 m->m_pkthdr.segsz = txsegsize;
1386 m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
1387 } else {
1388 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1389 if (len + optlen) {
1390 /* Fixup the pseudo-header checksum. */
1391 /* XXXJRT Not IP Jumbogram safe. */
1392 th->th_sum = in_cksum_addword(th->th_sum,
1393 htons((u_int16_t) (len + optlen)));
1394 }
1395 }
1396 break;
1397 #endif
1398 #ifdef INET6
1399 case AF_INET6:
1400 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1401 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1402 if (len + optlen) {
1403 /* Fixup the pseudo-header checksum. */
1404 /* XXXJRT: Not IPv6 Jumbogram safe. */
1405 th->th_sum = in_cksum_addword(th->th_sum,
1406 htons((u_int16_t) (len + optlen)));
1407 }
1408 break;
1409 #endif
1410 }
1411
1412 /*
1413 * In transmit state, time the transmission and arrange for
1414 * the retransmit. In persist state, just set snd_max.
1415 */
1416 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1417 tcp_seq startseq = tp->snd_nxt;
1418
1419 /*
1420 * Advance snd_nxt over sequence space of this segment.
1421 * There are no states in which we send both a SYN and a FIN,
1422 * so we collapse the tests for these flags.
1423 */
1424 if (flags & (TH_SYN|TH_FIN))
1425 tp->snd_nxt++;
1426 if (sack_rxmit)
1427 goto timer;
1428 tp->snd_nxt += len;
1429 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1430 tp->snd_max = tp->snd_nxt;
1431 /*
1432 * Time this transmission if not a retransmission and
1433 * not currently timing anything.
1434 */
1435 if (tp->t_rtttime == 0) {
1436 tp->t_rtttime = tcp_now;
1437 tp->t_rtseq = startseq;
1438 tcpstat.tcps_segstimed++;
1439 }
1440 }
1441
1442 /*
1443 * Set retransmit timer if not currently set,
1444 * and not doing an ack or a keep-alive probe.
1445 * Initial value for retransmit timer is smoothed
1446 * round-trip time + 2 * round-trip time variance.
1447 * Initialize shift counter which is used for backoff
1448 * of retransmit time.
1449 */
1450 timer:
1451 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1452 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1453 tp->snd_nxt != tp->snd_una)) {
1454 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1455 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1456 tp->t_rxtshift = 0;
1457 }
1458 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1459 }
1460 } else
1461 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1462 tp->snd_max = tp->snd_nxt + len;
1463
1464 #ifdef TCP_DEBUG
1465 /*
1466 * Trace.
1467 */
1468 if (so->so_options & SO_DEBUG)
1469 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1470 #endif
1471
1472 /*
1473 * Fill in IP length and desired time to live and
1474 * send to IP level. There should be a better way
1475 * to handle ttl and tos; we could keep them in
1476 * the template, but need a way to checksum without them.
1477 */
1478 m->m_pkthdr.len = hdrlen + len;
1479
1480 switch (af) {
1481 #ifdef INET
1482 case AF_INET:
1483 ip->ip_len = htons(m->m_pkthdr.len);
1484 packetlen = m->m_pkthdr.len;
1485 if (tp->t_inpcb) {
1486 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1487 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1488 }
1489 #ifdef INET6
1490 else if (tp->t_in6pcb) {
1491 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1492 ip->ip_tos = 0; /*XXX*/
1493 }
1494 #endif
1495 break;
1496 #endif
1497 #ifdef INET6
1498 case AF_INET6:
1499 packetlen = m->m_pkthdr.len;
1500 ip6->ip6_nxt = IPPROTO_TCP;
1501 if (tp->t_in6pcb) {
1502 /*
1503 * we separately set hoplimit for every segment, since
1504 * the user might want to change the value via
1505 * setsockopt. Also, desired default hop limit might
1506 * be changed via Neighbor Discovery.
1507 */
1508 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
1509 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
1510 }
1511 /* ip6->ip6_flow = ??? */
1512 /* ip6_plen will be filled in ip6_output(). */
1513 break;
1514 #endif
1515 default: /*pacify gcc*/
1516 packetlen = 0;
1517 break;
1518 }
1519
1520 switch (af) {
1521 #ifdef INET
1522 case AF_INET:
1523 {
1524 struct mbuf *opts;
1525
1526 if (tp->t_inpcb)
1527 opts = tp->t_inpcb->inp_options;
1528 else
1529 opts = NULL;
1530 error = ip_output(m, opts, ro,
1531 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1532 (so->so_options & SO_DONTROUTE),
1533 (struct ip_moptions *)0, so);
1534 break;
1535 }
1536 #endif
1537 #ifdef INET6
1538 case AF_INET6:
1539 {
1540 struct ip6_pktopts *opts;
1541
1542 if (tp->t_in6pcb)
1543 opts = tp->t_in6pcb->in6p_outputopts;
1544 else
1545 opts = NULL;
1546 error = ip6_output(m, opts, (struct route_in6 *)ro,
1547 so->so_options & SO_DONTROUTE,
1548 (struct ip6_moptions *)0, so, NULL);
1549 break;
1550 }
1551 #endif
1552 default:
1553 error = EAFNOSUPPORT;
1554 break;
1555 }
1556 if (error) {
1557 out:
1558 if (error == ENOBUFS) {
1559 tcpstat.tcps_selfquench++;
1560 #ifdef INET
1561 if (tp->t_inpcb)
1562 tcp_quench(tp->t_inpcb, 0);
1563 #endif
1564 #ifdef INET6
1565 if (tp->t_in6pcb)
1566 tcp6_quench(tp->t_in6pcb, 0);
1567 #endif
1568 error = 0;
1569 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1570 TCPS_HAVERCVDSYN(tp->t_state)) {
1571 tp->t_softerror = error;
1572 error = 0;
1573 }
1574
1575 /* Back out the seqence number advance. */
1576 if (sack_rxmit)
1577 p->rxmit -= len;
1578
1579 /* Restart the delayed ACK timer, if necessary. */
1580 if (tp->t_flags & TF_DELACK)
1581 TCP_RESTART_DELACK(tp);
1582
1583 return (error);
1584 }
1585
1586 if (packetlen > tp->t_pmtud_mtu_sent)
1587 tp->t_pmtud_mtu_sent = packetlen;
1588
1589 tcpstat.tcps_sndtotal++;
1590 if (tp->t_flags & TF_DELACK)
1591 tcpstat.tcps_delack++;
1592
1593 /*
1594 * Data sent (as far as we can tell).
1595 * If this advertises a larger window than any other segment,
1596 * then remember the size of the advertised window.
1597 * Any pending ACK has now been sent.
1598 */
1599 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1600 tp->rcv_adv = tp->rcv_nxt + win;
1601 tp->last_ack_sent = tp->rcv_nxt;
1602 tp->t_flags &= ~TF_ACKNOW;
1603 TCP_CLEAR_DELACK(tp);
1604 #ifdef DIAGNOSTIC
1605 if (maxburst < 0)
1606 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1607 #endif
1608 if (sendalot && (!tcp_do_newreno || --maxburst))
1609 goto again;
1610 return (0);
1611 }
1612
1613 void
1614 tcp_setpersist(struct tcpcb *tp)
1615 {
1616 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1617 int nticks;
1618
1619 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1620 panic("tcp_output REXMT");
1621 /*
1622 * Start/restart persistance timer.
1623 */
1624 if (t < tp->t_rttmin)
1625 t = tp->t_rttmin;
1626 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1627 TCPTV_PERSMIN, TCPTV_PERSMAX);
1628 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1629 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1630 tp->t_rxtshift++;
1631 }
1632
1633 #if defined(INET)
1634 /*
1635 * tcp4_segment: handle M_CSUM_TSOv4 by software.
1636 *
1637 * => always consume m.
1638 * => call output_func with output_arg for each segments.
1639 */
1640
1641 int
1642 tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
1643 void *output_arg)
1644 {
1645 int mss;
1646 int iphlen;
1647 int thlen;
1648 int hlen;
1649 int len;
1650 struct ip *iph;
1651 struct tcphdr *th;
1652 uint16_t ipid;
1653 uint32_t tcpseq;
1654 struct mbuf *hdr = NULL;
1655 struct mbuf *t;
1656 int error = 0;
1657
1658 KASSERT((m->m_flags & M_PKTHDR) != 0);
1659 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
1660
1661 m->m_pkthdr.csum_flags = 0;
1662
1663 len = m->m_pkthdr.len;
1664 KASSERT(len >= sizeof(*iph) + sizeof(*th));
1665
1666 if (m->m_len < sizeof(*iph)) {
1667 m = m_pullup(m, sizeof(*iph));
1668 if (m == NULL) {
1669 error = ENOMEM;
1670 goto quit;
1671 }
1672 }
1673 iph = mtod(m, struct ip *);
1674 iphlen = iph->ip_hl * 4;
1675 KASSERT(iph->ip_v == IPVERSION);
1676 KASSERT(iphlen >= sizeof(*iph));
1677 KASSERT(iph->ip_p == IPPROTO_TCP);
1678 ipid = ntohs(iph->ip_id);
1679
1680 hlen = iphlen + sizeof(*th);
1681 if (m->m_len < hlen) {
1682 m = m_pullup(m, hlen);
1683 if (m == NULL) {
1684 error = ENOMEM;
1685 goto quit;
1686 }
1687 }
1688 th = (void *)(mtod(m, char *) + iphlen);
1689 tcpseq = ntohl(th->th_seq);
1690 thlen = th->th_off * 4;
1691 hlen = iphlen + thlen;
1692
1693 mss = m->m_pkthdr.segsz;
1694 KASSERT(mss != 0);
1695 KASSERT(len > hlen);
1696
1697 t = m_split(m, hlen, M_NOWAIT);
1698 if (t == NULL) {
1699 error = ENOMEM;
1700 goto quit;
1701 }
1702 hdr = m;
1703 m = t;
1704 len -= hlen;
1705 KASSERT(len % mss == 0);
1706 while (len > 0) {
1707 struct mbuf *n;
1708
1709 n = m_dup(hdr, 0, hlen, M_NOWAIT);
1710 if (n == NULL) {
1711 error = ENOMEM;
1712 goto quit;
1713 }
1714 KASSERT(n->m_len == hlen); /* XXX */
1715
1716 t = m_split(m, mss, M_NOWAIT);
1717 if (t == NULL) {
1718 m_freem(n);
1719 error = ENOMEM;
1720 goto quit;
1721 }
1722 m_cat(n, m);
1723 m = t;
1724
1725 KASSERT(n->m_len >= hlen); /* XXX */
1726
1727 n->m_pkthdr.len = hlen + mss;
1728 iph = mtod(n, struct ip *);
1729 KASSERT(iph->ip_v == IPVERSION);
1730 iph->ip_len = htons(n->m_pkthdr.len);
1731 iph->ip_id = htons(ipid);
1732 th = (void *)(mtod(n, char *) + iphlen);
1733 th->th_seq = htonl(tcpseq);
1734 iph->ip_sum = 0;
1735 iph->ip_sum = in_cksum(n, iphlen);
1736 th->th_sum = 0;
1737 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
1738
1739 error = (*output_func)(output_arg, n);
1740 if (error) {
1741 goto quit;
1742 }
1743
1744 tcpseq += mss;
1745 ipid++;
1746 len -= mss;
1747 }
1748
1749 quit:
1750 if (hdr != NULL) {
1751 m_freem(hdr);
1752 }
1753 if (m != NULL) {
1754 m_freem(m);
1755 }
1756
1757 return error;
1758 }
1759 #endif /* defined(INET) */
1760