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