tcp_output.c revision 1.148 1 /* $NetBSD: tcp_output.c,v 1.148 2006/10/08 11:10:59 yamt Exp $ */
2
3 /*
4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the project nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 /*
33 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
34 *
35 * NRL grants permission for redistribution and use in source and binary
36 * forms, with or without modification, of the software and documentation
37 * created at NRL provided that the following conditions are met:
38 *
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgements:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * This product includes software developed at the Information
49 * Technology Division, US Naval Research Laboratory.
50 * 4. Neither the name of the NRL nor the names of its contributors
51 * may be used to endorse or promote products derived from this software
52 * without specific prior written permission.
53 *
54 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
55 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
56 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
57 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
58 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
59 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
60 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
61 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
64 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65 *
66 * The views and conclusions contained in the software and documentation
67 * are those of the authors and should not be interpreted as representing
68 * official policies, either expressed or implied, of the US Naval
69 * Research Laboratory (NRL).
70 */
71
72 /*-
73 * Copyright (c) 1997, 1998, 2001, 2005, 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.148 2006/10/08 11:10:59 yamt 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 >> 1, *txsegsizep);
401 *rxsegsizep = min(tp->t_ourmss - optlen, size);
402
403 if (*txsegsizep != tp->t_segsz) {
404 /*
405 * If the new segment size is larger, we don't want to
406 * mess up the congestion window, but if it is smaller
407 * we'll have to reduce the congestion window to ensure
408 * that we don't get into trouble with initial windows
409 * and the rest. In any case, if the segment size
410 * has changed, chances are the path has, too, and
411 * our congestion window will be different.
412 */
413 if (*txsegsizep < tp->t_segsz) {
414 tp->snd_cwnd = max((tp->snd_cwnd / tp->t_segsz)
415 * *txsegsizep, *txsegsizep);
416 tp->snd_ssthresh = max((tp->snd_ssthresh / tp->t_segsz)
417 * *txsegsizep, *txsegsizep);
418 }
419 tp->t_segsz = *txsegsizep;
420 }
421
422 return (0);
423 }
424
425 static
426 #ifndef GPROF
427 inline
428 #endif
429 int
430 tcp_build_datapkt(struct tcpcb *tp, struct socket *so, int off,
431 long len, int hdrlen, struct mbuf **mp)
432 {
433 struct mbuf *m, *m0;
434
435 if (tp->t_force && len == 1)
436 tcpstat.tcps_sndprobe++;
437 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
438 tcpstat.tcps_sndrexmitpack++;
439 tcpstat.tcps_sndrexmitbyte += len;
440 } else {
441 tcpstat.tcps_sndpack++;
442 tcpstat.tcps_sndbyte += len;
443 }
444 #ifdef notyet
445 if ((m = m_copypack(so->so_snd.sb_mb, off,
446 (int)len, max_linkhdr + hdrlen)) == 0)
447 return (ENOBUFS);
448 /*
449 * m_copypack left space for our hdr; use it.
450 */
451 m->m_len += hdrlen;
452 m->m_data -= hdrlen;
453 #else
454 MGETHDR(m, M_DONTWAIT, MT_HEADER);
455 if (__predict_false(m == NULL))
456 return (ENOBUFS);
457 MCLAIM(m, &tcp_tx_mowner);
458
459 /*
460 * XXX Because other code assumes headers will fit in
461 * XXX one header mbuf.
462 *
463 * (This code should almost *never* be run.)
464 */
465 if (__predict_false((max_linkhdr + hdrlen) > MHLEN)) {
466 TCP_OUTPUT_COUNTER_INCR(&tcp_output_bigheader);
467 MCLGET(m, M_DONTWAIT);
468 if ((m->m_flags & M_EXT) == 0) {
469 m_freem(m);
470 return (ENOBUFS);
471 }
472 }
473
474 m->m_data += max_linkhdr;
475 m->m_len = hdrlen;
476
477 /*
478 * To avoid traversing the whole sb_mb chain for correct
479 * data to send, remember last sent mbuf, its offset and
480 * the sent size. When called the next time, see if the
481 * data to send is directly following the previous transfer.
482 * This is important for large TCP windows.
483 */
484 if (off == 0 || tp->t_lastm == NULL ||
485 (tp->t_lastoff + tp->t_lastlen) != off) {
486 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_miss);
487 /*
488 * Either a new packet or a retransmit.
489 * Start from the beginning.
490 */
491 tp->t_lastm = so->so_snd.sb_mb;
492 tp->t_inoff = off;
493 } else {
494 TCP_OUTPUT_COUNTER_INCR(&tcp_output_predict_hit);
495 tp->t_inoff += tp->t_lastlen;
496 }
497
498 /* Traverse forward to next packet */
499 while (tp->t_inoff > 0) {
500 if (tp->t_lastm == NULL)
501 panic("tp->t_lastm == NULL");
502 if (tp->t_inoff < tp->t_lastm->m_len)
503 break;
504 tp->t_inoff -= tp->t_lastm->m_len;
505 tp->t_lastm = tp->t_lastm->m_next;
506 }
507
508 tp->t_lastoff = off;
509 tp->t_lastlen = len;
510 m0 = tp->t_lastm;
511 off = tp->t_inoff;
512
513 if (len <= M_TRAILINGSPACE(m)) {
514 m_copydata(m0, off, (int) len, mtod(m, caddr_t) + hdrlen);
515 m->m_len += len;
516 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copysmall);
517 } else {
518 m->m_next = m_copy(m0, off, (int) len);
519 if (m->m_next == NULL) {
520 m_freem(m);
521 return (ENOBUFS);
522 }
523 #ifdef TCP_OUTPUT_COUNTERS
524 if (m->m_next->m_flags & M_EXT)
525 TCP_OUTPUT_COUNTER_INCR(&tcp_output_refbig);
526 else
527 TCP_OUTPUT_COUNTER_INCR(&tcp_output_copybig);
528 #endif /* TCP_OUTPUT_COUNTERS */
529 }
530 #endif
531
532 *mp = m;
533 return (0);
534 }
535
536 /*
537 * Tcp output routine: figure out what should be sent and send it.
538 */
539 int
540 tcp_output(struct tcpcb *tp)
541 {
542 struct socket *so;
543 struct route *ro;
544 long len, win;
545 int off, flags, error;
546 struct mbuf *m;
547 struct ip *ip;
548 #ifdef INET6
549 struct ip6_hdr *ip6;
550 #endif
551 struct tcphdr *th;
552 u_char opt[MAX_TCPOPTLEN];
553 unsigned optlen, hdrlen, packetlen;
554 unsigned int sack_numblks;
555 int idle, sendalot, txsegsize, rxsegsize;
556 int txsegsize_nosack;
557 int maxburst = TCP_MAXBURST;
558 int af; /* address family on the wire */
559 int iphdrlen;
560 int has_tso, use_tso;
561 int sack_rxmit;
562 int sack_bytes_rxmt;
563 struct sackhole *p;
564 #ifdef TCP_SIGNATURE
565 int sigoff = 0;
566 #endif
567
568 #ifdef DIAGNOSTIC
569 if (tp->t_inpcb && tp->t_in6pcb)
570 panic("tcp_output: both t_inpcb and t_in6pcb are set");
571 #endif
572 so = NULL;
573 ro = NULL;
574 if (tp->t_inpcb) {
575 so = tp->t_inpcb->inp_socket;
576 ro = &tp->t_inpcb->inp_route;
577 }
578 #ifdef INET6
579 else if (tp->t_in6pcb) {
580 so = tp->t_in6pcb->in6p_socket;
581 ro = (struct route *)&tp->t_in6pcb->in6p_route;
582 }
583 #endif
584
585 switch (af = tp->t_family) {
586 #ifdef INET
587 case AF_INET:
588 if (tp->t_inpcb)
589 break;
590 #ifdef INET6
591 /* mapped addr case */
592 if (tp->t_in6pcb)
593 break;
594 #endif
595 return (EINVAL);
596 #endif
597 #ifdef INET6
598 case AF_INET6:
599 if (tp->t_in6pcb)
600 break;
601 return (EINVAL);
602 #endif
603 default:
604 return (EAFNOSUPPORT);
605 }
606
607 if (tcp_segsize(tp, &txsegsize, &rxsegsize))
608 return (EMSGSIZE);
609
610 idle = (tp->snd_max == tp->snd_una);
611
612 /*
613 * Determine if we can use TCP segmentation offload:
614 * - If we're using IPv4
615 * - If there is not an IPsec policy that prevents it
616 * - If the interface can do it
617 */
618 has_tso = tp->t_inpcb != NULL &&
619 #if defined(IPSEC) || defined(FAST_IPSEC)
620 IPSEC_PCB_SKIP_IPSEC(tp->t_inpcb->inp_sp,
621 IPSEC_DIR_OUTBOUND) &&
622 #endif
623 tp->t_inpcb->inp_route.ro_rt != NULL &&
624 (tp->t_inpcb->inp_route.ro_rt->rt_ifp->if_capenable &
625 IFCAP_TSOv4) != 0;
626
627 /*
628 * Restart Window computation. From draft-floyd-incr-init-win-03:
629 *
630 * Optionally, a TCP MAY set the restart window to the
631 * minimum of the value used for the initial window and
632 * the current value of cwnd (in other words, using a
633 * larger value for the restart window should never increase
634 * the size of cwnd).
635 */
636 if (tcp_cwm) {
637 /*
638 * Hughes/Touch/Heidemann Congestion Window Monitoring.
639 * Count the number of packets currently pending
640 * acknowledgement, and limit our congestion window
641 * to a pre-determined allowed burst size plus that count.
642 * This prevents bursting once all pending packets have
643 * been acknowledged (i.e. transmission is idle).
644 *
645 * XXX Link this to Initial Window?
646 */
647 tp->snd_cwnd = min(tp->snd_cwnd,
648 (tcp_cwm_burstsize * txsegsize) +
649 (tp->snd_nxt - tp->snd_una));
650 } else {
651 if (idle && (tcp_now - tp->t_rcvtime) >= tp->t_rxtcur) {
652 /*
653 * We have been idle for "a while" and no acks are
654 * expected to clock out any data we send --
655 * slow start to get ack "clock" running again.
656 */
657 int ss = tcp_init_win;
658 #ifdef INET
659 if (tp->t_inpcb &&
660 in_localaddr(tp->t_inpcb->inp_faddr))
661 ss = tcp_init_win_local;
662 #endif
663 #ifdef INET6
664 if (tp->t_in6pcb &&
665 in6_localaddr(&tp->t_in6pcb->in6p_faddr))
666 ss = tcp_init_win_local;
667 #endif
668 tp->snd_cwnd = min(tp->snd_cwnd,
669 TCP_INITIAL_WINDOW(ss, txsegsize));
670 }
671 }
672
673 txsegsize_nosack = txsegsize;
674 again:
675 use_tso = has_tso;
676 if ((tp->t_flags & (TF_ECN_SND_CWR|TF_ECN_SND_ECE)) != 0) {
677 /* don't duplicate CWR/ECE. */
678 use_tso = 0;
679 }
680 TCP_REASS_LOCK(tp);
681 sack_numblks = tcp_sack_numblks(tp);
682 if (sack_numblks) {
683 int sackoptlen;
684
685 sackoptlen = TCP_SACK_OPTLEN(sack_numblks);
686 if (sackoptlen > txsegsize_nosack) {
687 sack_numblks = 0; /* give up SACK */
688 txsegsize = txsegsize_nosack;
689 } else {
690 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
691 /* don't duplicate D-SACK. */
692 use_tso = 0;
693 }
694 txsegsize = txsegsize_nosack - sackoptlen;
695 }
696 } else {
697 txsegsize = txsegsize_nosack;
698 }
699
700 /*
701 * Determine length of data that should be transmitted, and
702 * flags that should be used. If there is some data or critical
703 * controls (SYN, RST) to send, then transmit; otherwise,
704 * investigate further.
705 *
706 * Readjust SACK information to avoid resending duplicate data.
707 */
708 if (TCP_SACK_ENABLED(tp) && SEQ_LT(tp->snd_nxt, tp->snd_max))
709 tcp_sack_adjust(tp);
710 sendalot = 0;
711 off = tp->snd_nxt - tp->snd_una;
712 win = min(tp->snd_wnd, tp->snd_cwnd);
713
714 flags = tcp_outflags[tp->t_state];
715
716 /*
717 * Send any SACK-generated retransmissions. If we're explicitly trying
718 * to send out new data (when sendalot is 1), bypass this function.
719 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
720 * we're replacing a (future) new transmission with a retransmission
721 * now, and we previously incremented snd_cwnd in tcp_input().
722 */
723 /*
724 * Still in sack recovery , reset rxmit flag to zero.
725 */
726 sack_rxmit = 0;
727 sack_bytes_rxmt = 0;
728 len = 0;
729 p = NULL;
730 do {
731 long cwin;
732 if (!TCP_SACK_ENABLED(tp))
733 break;
734 if (tp->t_partialacks < 0)
735 break;
736 p = tcp_sack_output(tp, &sack_bytes_rxmt);
737 if (p == NULL)
738 break;
739
740 cwin = min(tp->snd_wnd, tp->snd_cwnd) - sack_bytes_rxmt;
741 if (cwin < 0)
742 cwin = 0;
743 /* Do not retransmit SACK segments beyond snd_recover */
744 if (SEQ_GT(p->end, tp->snd_recover)) {
745 /*
746 * (At least) part of sack hole extends beyond
747 * snd_recover. Check to see if we can rexmit data
748 * for this hole.
749 */
750 if (SEQ_GEQ(p->rxmit, tp->snd_recover)) {
751 /*
752 * Can't rexmit any more data for this hole.
753 * That data will be rexmitted in the next
754 * sack recovery episode, when snd_recover
755 * moves past p->rxmit.
756 */
757 p = NULL;
758 break;
759 }
760 /* Can rexmit part of the current hole */
761 len = ((long)ulmin(cwin, tp->snd_recover - p->rxmit));
762 } else
763 len = ((long)ulmin(cwin, p->end - p->rxmit));
764 off = p->rxmit - tp->snd_una;
765 if (off + len > so->so_snd.sb_cc) {
766 /* 1 for TH_FIN */
767 KASSERT(off + len == so->so_snd.sb_cc + 1);
768 KASSERT(p->rxmit + len == tp->snd_max);
769 len = so->so_snd.sb_cc - off;
770 }
771 if (len > 0) {
772 sack_rxmit = 1;
773 sendalot = 1;
774 }
775 } while (/*CONSTCOND*/0);
776
777 /*
778 * If in persist timeout with window of 0, send 1 byte.
779 * Otherwise, if window is small but nonzero
780 * and timer expired, we will send what we can
781 * and go to transmit state.
782 */
783 if (tp->t_force) {
784 if (win == 0) {
785 /*
786 * If we still have some data to send, then
787 * clear the FIN bit. Usually this would
788 * happen below when it realizes that we
789 * aren't sending all the data. However,
790 * if we have exactly 1 byte of unset data,
791 * then it won't clear the FIN bit below,
792 * and if we are in persist state, we wind
793 * up sending the packet without recording
794 * that we sent the FIN bit.
795 *
796 * We can't just blindly clear the FIN bit,
797 * because if we don't have any more data
798 * to send then the probe will be the FIN
799 * itself.
800 */
801 if (off < so->so_snd.sb_cc)
802 flags &= ~TH_FIN;
803 win = 1;
804 } else {
805 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
806 tp->t_rxtshift = 0;
807 }
808 }
809
810 if (sack_rxmit == 0) {
811 if (sack_bytes_rxmt != 0) {
812 long cwin;
813
814 /*
815 * We are inside of a SACK recovery episode and are
816 * sending new data, having retransmitted all the
817 * data possible in the scoreboard.
818 */
819 if (tp->snd_wnd < so->so_snd.sb_cc) {
820 len = tp->snd_wnd - off;
821 flags &= ~TH_FIN;
822 } else {
823 len = so->so_snd.sb_cc - off;
824 }
825
826 /*
827 * From FreeBSD:
828 * Don't remove this (len > 0) check !
829 * We explicitly check for len > 0 here (although it
830 * isn't really necessary), to work around a gcc
831 * optimization issue - to force gcc to compute
832 * len above. Without this check, the computation
833 * of len is bungled by the optimizer.
834 */
835 if (len > 0) {
836 cwin = tp->snd_cwnd -
837 (tp->snd_nxt - tp->sack_newdata) -
838 sack_bytes_rxmt;
839 if (cwin < 0)
840 cwin = 0;
841 if (cwin < len) {
842 len = cwin;
843 flags &= ~TH_FIN;
844 }
845 }
846 } else if (win < so->so_snd.sb_cc) {
847 len = win - off;
848 flags &= ~TH_FIN;
849 } else {
850 len = so->so_snd.sb_cc - off;
851 }
852 }
853
854 if (len < 0) {
855 /*
856 * If FIN has been sent but not acked,
857 * but we haven't been called to retransmit,
858 * len will be -1. Otherwise, window shrank
859 * after we sent into it. If window shrank to 0,
860 * cancel pending retransmit, pull snd_nxt back
861 * to (closed) window, and set the persist timer
862 * if it isn't already going. If the window didn't
863 * close completely, just wait for an ACK.
864 *
865 * If we have a pending FIN, either it has already been
866 * transmitted or it is outside the window, so drop it.
867 * If the FIN has been transmitted, but this is not a
868 * retransmission, then len must be -1. Therefore we also
869 * prevent here the sending of `gratuitous FINs'. This
870 * eliminates the need to check for that case below (e.g.
871 * to back up snd_nxt before the FIN so that the sequence
872 * number is correct).
873 */
874 len = 0;
875 flags &= ~TH_FIN;
876 if (win == 0) {
877 TCP_TIMER_DISARM(tp, TCPT_REXMT);
878 tp->t_rxtshift = 0;
879 tp->snd_nxt = tp->snd_una;
880 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
881 tcp_setpersist(tp);
882 }
883 }
884 if (len > txsegsize) {
885 if (use_tso) {
886 /*
887 * Truncate TSO transfers to IP_MAXPACKET, and make
888 * sure that we send equal size transfers down the
889 * stack (rather than big-small-big-small-...).
890 */
891 len = (min(len, IP_MAXPACKET) / txsegsize) * txsegsize;
892 if (len <= txsegsize) {
893 use_tso = 0;
894 }
895 } else
896 len = txsegsize;
897 flags &= ~TH_FIN;
898 sendalot = 1;
899 } else
900 use_tso = 0;
901 if (sack_rxmit) {
902 if (SEQ_LT(p->rxmit + len, tp->snd_una + so->so_snd.sb_cc))
903 flags &= ~TH_FIN;
904 }
905
906 win = sbspace(&so->so_rcv);
907
908 /*
909 * Sender silly window avoidance. If connection is idle
910 * and can send all data, a maximum segment,
911 * at least a maximum default-size segment do it,
912 * or are forced, do it; otherwise don't bother.
913 * If peer's buffer is tiny, then send
914 * when window is at least half open.
915 * If retransmitting (possibly after persist timer forced us
916 * to send into a small window), then must resend.
917 */
918 if (len) {
919 if (len >= txsegsize)
920 goto send;
921 if ((so->so_state & SS_MORETOCOME) == 0 &&
922 ((idle || tp->t_flags & TF_NODELAY) &&
923 len + off >= so->so_snd.sb_cc))
924 goto send;
925 if (tp->t_force)
926 goto send;
927 if (len >= tp->max_sndwnd / 2)
928 goto send;
929 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
930 goto send;
931 if (sack_rxmit)
932 goto send;
933 }
934
935 /*
936 * Compare available window to amount of window known to peer
937 * (as advertised window less next expected input). If the
938 * difference is at least twice the size of the largest segment
939 * we expect to receive (i.e. two segments) or at least 50% of
940 * the maximum possible window, then want to send a window update
941 * to peer.
942 */
943 if (win > 0) {
944 /*
945 * "adv" is the amount we can increase the window,
946 * taking into account that we are limited by
947 * TCP_MAXWIN << tp->rcv_scale.
948 */
949 long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) -
950 (tp->rcv_adv - tp->rcv_nxt);
951
952 if (adv >= (long) (2 * rxsegsize))
953 goto send;
954 if (2 * adv >= (long) so->so_rcv.sb_hiwat)
955 goto send;
956 }
957
958 /*
959 * Send if we owe peer an ACK.
960 */
961 if (tp->t_flags & TF_ACKNOW)
962 goto send;
963 if (flags & (TH_SYN|TH_FIN|TH_RST))
964 goto send;
965 if (SEQ_GT(tp->snd_up, tp->snd_una))
966 goto send;
967 /*
968 * In SACK, it is possible for tcp_output to fail to send a segment
969 * after the retransmission timer has been turned off. Make sure
970 * that the retransmission timer is set.
971 */
972 if (TCP_SACK_ENABLED(tp) && SEQ_GT(tp->snd_max, tp->snd_una) &&
973 !TCP_TIMER_ISARMED(tp, TCPT_REXMT) &&
974 !TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
975 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
976 goto just_return;
977 }
978
979 /*
980 * TCP window updates are not reliable, rather a polling protocol
981 * using ``persist'' packets is used to insure receipt of window
982 * updates. The three ``states'' for the output side are:
983 * idle not doing retransmits or persists
984 * persisting to move a small or zero window
985 * (re)transmitting and thereby not persisting
986 *
987 * tp->t_timer[TCPT_PERSIST]
988 * is set when we are in persist state.
989 * tp->t_force
990 * is set when we are called to send a persist packet.
991 * tp->t_timer[TCPT_REXMT]
992 * is set when we are retransmitting
993 * The output side is idle when both timers are zero.
994 *
995 * If send window is too small, there is data to transmit, and no
996 * retransmit or persist is pending, then go to persist state.
997 * If nothing happens soon, send when timer expires:
998 * if window is nonzero, transmit what we can,
999 * otherwise force out a byte.
1000 */
1001 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1002 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1003 tp->t_rxtshift = 0;
1004 tcp_setpersist(tp);
1005 }
1006
1007 /*
1008 * No reason to send a segment, just return.
1009 */
1010 just_return:
1011 TCP_REASS_UNLOCK(tp);
1012 return (0);
1013
1014 send:
1015 /*
1016 * Before ESTABLISHED, force sending of initial options
1017 * unless TCP set not to do any options.
1018 * NOTE: we assume that the IP/TCP header plus TCP options
1019 * always fit in a single mbuf, leaving room for a maximum
1020 * link header, i.e.
1021 * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES
1022 */
1023 optlen = 0;
1024 switch (af) {
1025 #ifdef INET
1026 case AF_INET:
1027 iphdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
1028 break;
1029 #endif
1030 #ifdef INET6
1031 case AF_INET6:
1032 iphdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
1033 break;
1034 #endif
1035 default: /*pacify gcc*/
1036 iphdrlen = 0;
1037 break;
1038 }
1039 hdrlen = iphdrlen;
1040 if (flags & TH_SYN) {
1041 struct rtentry *rt;
1042
1043 rt = NULL;
1044 #ifdef INET
1045 if (tp->t_inpcb)
1046 rt = in_pcbrtentry(tp->t_inpcb);
1047 #endif
1048 #ifdef INET6
1049 if (tp->t_in6pcb)
1050 rt = in6_pcbrtentry(tp->t_in6pcb);
1051 #endif
1052
1053 tp->snd_nxt = tp->iss;
1054 tp->t_ourmss = tcp_mss_to_advertise(rt != NULL ?
1055 rt->rt_ifp : NULL, af);
1056 if ((tp->t_flags & TF_NOOPT) == 0) {
1057 opt[0] = TCPOPT_MAXSEG;
1058 opt[1] = 4;
1059 opt[2] = (tp->t_ourmss >> 8) & 0xff;
1060 opt[3] = tp->t_ourmss & 0xff;
1061 optlen = 4;
1062
1063 if ((tp->t_flags & TF_REQ_SCALE) &&
1064 ((flags & TH_ACK) == 0 ||
1065 (tp->t_flags & TF_RCVD_SCALE))) {
1066 *((u_int32_t *) (opt + optlen)) = htonl(
1067 TCPOPT_NOP << 24 |
1068 TCPOPT_WINDOW << 16 |
1069 TCPOLEN_WINDOW << 8 |
1070 tp->request_r_scale);
1071 optlen += 4;
1072 }
1073 if (tcp_do_sack) {
1074 u_int8_t *cp = (u_int8_t *)(opt + optlen);
1075
1076 cp[0] = TCPOPT_SACK_PERMITTED;
1077 cp[1] = 2;
1078 cp[2] = TCPOPT_NOP;
1079 cp[3] = TCPOPT_NOP;
1080 optlen += 4;
1081 }
1082 }
1083 }
1084
1085 /*
1086 * Send a timestamp and echo-reply if this is a SYN and our side
1087 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
1088 * and our peer have sent timestamps in our SYN's.
1089 */
1090 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
1091 (flags & TH_RST) == 0 &&
1092 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
1093 (tp->t_flags & TF_RCVD_TSTMP))) {
1094 u_int32_t *lp = (u_int32_t *)(opt + optlen);
1095
1096 /* Form timestamp option as shown in appendix A of RFC 1323. */
1097 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
1098 *lp++ = htonl(TCP_TIMESTAMP(tp));
1099 *lp = htonl(tp->ts_recent);
1100 optlen += TCPOLEN_TSTAMP_APPA;
1101 }
1102
1103 /*
1104 * Tack on the SACK block if it is necessary.
1105 */
1106 if (sack_numblks) {
1107 int sack_len;
1108 u_char *bp = (u_char *)(opt + optlen);
1109 u_int32_t *lp = (u_int32_t *)(bp + 4);
1110 struct ipqent *tiqe;
1111
1112 sack_len = sack_numblks * 8 + 2;
1113 bp[0] = TCPOPT_NOP;
1114 bp[1] = TCPOPT_NOP;
1115 bp[2] = TCPOPT_SACK;
1116 bp[3] = sack_len;
1117 if ((tp->rcv_sack_flags & TCPSACK_HAVED) != 0) {
1118 sack_numblks--;
1119 *lp++ = htonl(tp->rcv_dsack_block.left);
1120 *lp++ = htonl(tp->rcv_dsack_block.right);
1121 tp->rcv_sack_flags &= ~TCPSACK_HAVED;
1122 }
1123 for (tiqe = TAILQ_FIRST(&tp->timeq);
1124 sack_numblks > 0; tiqe = TAILQ_NEXT(tiqe, ipqe_timeq)) {
1125 KASSERT(tiqe != NULL);
1126 sack_numblks--;
1127 *lp++ = htonl(tiqe->ipqe_seq);
1128 *lp++ = htonl(tiqe->ipqe_seq + tiqe->ipqe_len +
1129 ((tiqe->ipqe_flags & TH_FIN) != 0 ? 1 : 0));
1130 }
1131 optlen += sack_len + 2;
1132 }
1133 TCP_REASS_UNLOCK(tp);
1134
1135 #ifdef TCP_SIGNATURE
1136 #if defined(INET6) && defined(FAST_IPSEC)
1137 if (tp->t_family == AF_INET)
1138 #endif
1139 if (tp->t_flags & TF_SIGNATURE) {
1140 u_char *bp;
1141 /*
1142 * Initialize TCP-MD5 option (RFC2385)
1143 */
1144 bp = (u_char *)opt + optlen;
1145 *bp++ = TCPOPT_SIGNATURE;
1146 *bp++ = TCPOLEN_SIGNATURE;
1147 sigoff = optlen + 2;
1148 bzero(bp, TCP_SIGLEN);
1149 bp += TCP_SIGLEN;
1150 optlen += TCPOLEN_SIGNATURE;
1151 /*
1152 * Terminate options list and maintain 32-bit alignment.
1153 */
1154 *bp++ = TCPOPT_NOP;
1155 *bp++ = TCPOPT_EOL;
1156 optlen += 2;
1157 }
1158 #endif /* TCP_SIGNATURE */
1159
1160 hdrlen += optlen;
1161
1162 #ifdef DIAGNOSTIC
1163 if (!use_tso && len > txsegsize)
1164 panic("tcp data to be sent is larger than segment");
1165 else if (use_tso && len > IP_MAXPACKET)
1166 panic("tcp data to be sent is larger than max TSO size");
1167 if (max_linkhdr + hdrlen > MCLBYTES)
1168 panic("tcphdr too big");
1169 #endif
1170
1171 /*
1172 * Grab a header mbuf, attaching a copy of data to
1173 * be transmitted, and initialize the header from
1174 * the template for sends on this connection.
1175 */
1176 if (len) {
1177 error = tcp_build_datapkt(tp, so, off, len, hdrlen, &m);
1178 if (error)
1179 goto out;
1180 /*
1181 * If we're sending everything we've got, set PUSH.
1182 * (This will keep happy those implementations which only
1183 * give data to the user when a buffer fills or
1184 * a PUSH comes in.)
1185 */
1186 if (off + len == so->so_snd.sb_cc)
1187 flags |= TH_PUSH;
1188 } else {
1189 if (tp->t_flags & TF_ACKNOW)
1190 tcpstat.tcps_sndacks++;
1191 else if (flags & (TH_SYN|TH_FIN|TH_RST))
1192 tcpstat.tcps_sndctrl++;
1193 else if (SEQ_GT(tp->snd_up, tp->snd_una))
1194 tcpstat.tcps_sndurg++;
1195 else
1196 tcpstat.tcps_sndwinup++;
1197
1198 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1199 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
1200 MCLGET(m, M_DONTWAIT);
1201 if ((m->m_flags & M_EXT) == 0) {
1202 m_freem(m);
1203 m = NULL;
1204 }
1205 }
1206 if (m == NULL) {
1207 error = ENOBUFS;
1208 goto out;
1209 }
1210 MCLAIM(m, &tcp_tx_mowner);
1211 m->m_data += max_linkhdr;
1212 m->m_len = hdrlen;
1213 }
1214 m->m_pkthdr.rcvif = (struct ifnet *)0;
1215 switch (af) {
1216 #ifdef INET
1217 case AF_INET:
1218 ip = mtod(m, struct ip *);
1219 #ifdef INET6
1220 ip6 = NULL;
1221 #endif
1222 th = (struct tcphdr *)(ip + 1);
1223 break;
1224 #endif
1225 #ifdef INET6
1226 case AF_INET6:
1227 ip = NULL;
1228 ip6 = mtod(m, struct ip6_hdr *);
1229 th = (struct tcphdr *)(ip6 + 1);
1230 break;
1231 #endif
1232 default: /*pacify gcc*/
1233 ip = NULL;
1234 #ifdef INET6
1235 ip6 = NULL;
1236 #endif
1237 th = NULL;
1238 break;
1239 }
1240 if (tp->t_template == 0)
1241 panic("tcp_output");
1242 if (tp->t_template->m_len < iphdrlen)
1243 panic("tcp_output");
1244 bcopy(mtod(tp->t_template, caddr_t), mtod(m, caddr_t), iphdrlen);
1245
1246 /*
1247 * If we are starting a connection, send ECN setup
1248 * SYN packet. If we are on a retransmit, we may
1249 * resend those bits a number of times as per
1250 * RFC 3168.
1251 */
1252 if (tp->t_state == TCPS_SYN_SENT && tcp_do_ecn) {
1253 if (tp->t_flags & TF_SYN_REXMT) {
1254 if (tp->t_ecn_retries--)
1255 flags |= TH_ECE|TH_CWR;
1256 } else {
1257 flags |= TH_ECE|TH_CWR;
1258 tp->t_ecn_retries = tcp_ecn_maxretries;
1259 }
1260 }
1261
1262 if (TCP_ECN_ALLOWED(tp)) {
1263 /*
1264 * If the peer has ECN, mark data packets
1265 * ECN capable. Ignore pure ack packets, retransmissions
1266 * and window probes.
1267 */
1268 if (len > 0 && SEQ_GEQ(tp->snd_nxt, tp->snd_max) &&
1269 !(tp->t_force && len == 1)) {
1270 switch (af) {
1271 #ifdef INET
1272 case AF_INET:
1273 tp->t_inpcb->inp_ip.ip_tos |= IPTOS_ECN_ECT0;
1274 break;
1275 #endif
1276 #ifdef INET6
1277 case AF_INET6:
1278 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1279 break;
1280 #endif
1281 }
1282 tcpstat.tcps_ecn_ect++;
1283 }
1284
1285 /*
1286 * Reply with proper ECN notifications.
1287 */
1288 if (tp->t_flags & TF_ECN_SND_CWR) {
1289 flags |= TH_CWR;
1290 tp->t_flags &= ~TF_ECN_SND_CWR;
1291 }
1292 if (tp->t_flags & TF_ECN_SND_ECE) {
1293 flags |= TH_ECE;
1294 }
1295 }
1296
1297
1298 /*
1299 * If we are doing retransmissions, then snd_nxt will
1300 * not reflect the first unsent octet. For ACK only
1301 * packets, we do not want the sequence number of the
1302 * retransmitted packet, we want the sequence number
1303 * of the next unsent octet. So, if there is no data
1304 * (and no SYN or FIN), use snd_max instead of snd_nxt
1305 * when filling in ti_seq. But if we are in persist
1306 * state, snd_max might reflect one byte beyond the
1307 * right edge of the window, so use snd_nxt in that
1308 * case, since we know we aren't doing a retransmission.
1309 * (retransmit and persist are mutually exclusive...)
1310 */
1311 if (TCP_SACK_ENABLED(tp) && sack_rxmit) {
1312 th->th_seq = htonl(p->rxmit);
1313 p->rxmit += len;
1314 } else {
1315 if (len || (flags & (TH_SYN|TH_FIN)) ||
1316 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
1317 th->th_seq = htonl(tp->snd_nxt);
1318 else
1319 th->th_seq = htonl(tp->snd_max);
1320 }
1321 th->th_ack = htonl(tp->rcv_nxt);
1322 if (optlen) {
1323 bcopy((caddr_t)opt, (caddr_t)(th + 1), optlen);
1324 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
1325 }
1326 th->th_flags = flags;
1327 /*
1328 * Calculate receive window. Don't shrink window,
1329 * but avoid silly window syndrome.
1330 */
1331 if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)rxsegsize)
1332 win = 0;
1333 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
1334 win = (long)TCP_MAXWIN << tp->rcv_scale;
1335 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
1336 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
1337 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
1338 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
1339 u_int32_t urp = tp->snd_up - tp->snd_nxt;
1340 if (urp > IP_MAXPACKET)
1341 urp = IP_MAXPACKET;
1342 th->th_urp = htons((u_int16_t)urp);
1343 th->th_flags |= TH_URG;
1344 } else
1345 /*
1346 * If no urgent pointer to send, then we pull
1347 * the urgent pointer to the left edge of the send window
1348 * so that it doesn't drift into the send window on sequence
1349 * number wraparound.
1350 */
1351 tp->snd_up = tp->snd_una; /* drag it along */
1352
1353 #ifdef TCP_SIGNATURE
1354 #if defined(INET6) && defined(FAST_IPSEC)
1355 if (tp->t_family == AF_INET) /* XXX */
1356 #endif
1357 if (sigoff && (tp->t_flags & TF_SIGNATURE)) {
1358 struct secasvar *sav;
1359 u_int8_t *sigp;
1360
1361 sav = tcp_signature_getsav(m, th);
1362
1363 if (sav == NULL) {
1364 if (m)
1365 m_freem(m);
1366 return (EPERM);
1367 }
1368
1369 m->m_pkthdr.len = hdrlen + len;
1370 sigp = (caddr_t)th + sizeof(*th) + sigoff;
1371 tcp_signature(m, th, (caddr_t)th - mtod(m, caddr_t), sav, sigp);
1372
1373 key_sa_recordxfer(sav, m);
1374 #ifdef FAST_IPSEC
1375 KEY_FREESAV(&sav);
1376 #else
1377 key_freesav(sav);
1378 #endif
1379 }
1380 #endif
1381
1382 /*
1383 * Set ourselves up to be checksummed just before the packet
1384 * hits the wire.
1385 */
1386 switch (af) {
1387 #ifdef INET
1388 case AF_INET:
1389 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1390 if (use_tso) {
1391 m->m_pkthdr.segsz = txsegsize;
1392 m->m_pkthdr.csum_flags = M_CSUM_TSOv4;
1393 } else {
1394 m->m_pkthdr.csum_flags = M_CSUM_TCPv4;
1395 if (len + optlen) {
1396 /* Fixup the pseudo-header checksum. */
1397 /* XXXJRT Not IP Jumbogram safe. */
1398 th->th_sum = in_cksum_addword(th->th_sum,
1399 htons((u_int16_t) (len + optlen)));
1400 }
1401 }
1402 break;
1403 #endif
1404 #ifdef INET6
1405 case AF_INET6:
1406 m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum);
1407 m->m_pkthdr.csum_flags = M_CSUM_TCPv6;
1408 if (len + optlen) {
1409 /* Fixup the pseudo-header checksum. */
1410 /* XXXJRT: Not IPv6 Jumbogram safe. */
1411 th->th_sum = in_cksum_addword(th->th_sum,
1412 htons((u_int16_t) (len + optlen)));
1413 }
1414 break;
1415 #endif
1416 }
1417
1418 /*
1419 * In transmit state, time the transmission and arrange for
1420 * the retransmit. In persist state, just set snd_max.
1421 */
1422 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1423 tcp_seq startseq = tp->snd_nxt;
1424
1425 /*
1426 * Advance snd_nxt over sequence space of this segment.
1427 * There are no states in which we send both a SYN and a FIN,
1428 * so we collapse the tests for these flags.
1429 */
1430 if (flags & (TH_SYN|TH_FIN))
1431 tp->snd_nxt++;
1432 if (sack_rxmit)
1433 goto timer;
1434 tp->snd_nxt += len;
1435 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
1436 tp->snd_max = tp->snd_nxt;
1437 /*
1438 * Time this transmission if not a retransmission and
1439 * not currently timing anything.
1440 */
1441 if (tp->t_rtttime == 0) {
1442 tp->t_rtttime = tcp_now;
1443 tp->t_rtseq = startseq;
1444 tcpstat.tcps_segstimed++;
1445 }
1446 }
1447
1448 /*
1449 * Set retransmit timer if not currently set,
1450 * and not doing an ack or a keep-alive probe.
1451 * Initial value for retransmit timer is smoothed
1452 * round-trip time + 2 * round-trip time variance.
1453 * Initialize shift counter which is used for backoff
1454 * of retransmit time.
1455 */
1456 timer:
1457 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1458 ((sack_rxmit && tp->snd_nxt != tp->snd_max) ||
1459 tp->snd_nxt != tp->snd_una)) {
1460 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1461 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1462 tp->t_rxtshift = 0;
1463 }
1464 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1465 }
1466 } else
1467 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1468 tp->snd_max = tp->snd_nxt + len;
1469
1470 #ifdef TCP_DEBUG
1471 /*
1472 * Trace.
1473 */
1474 if (so->so_options & SO_DEBUG)
1475 tcp_trace(TA_OUTPUT, tp->t_state, tp, m, 0);
1476 #endif
1477
1478 /*
1479 * Fill in IP length and desired time to live and
1480 * send to IP level. There should be a better way
1481 * to handle ttl and tos; we could keep them in
1482 * the template, but need a way to checksum without them.
1483 */
1484 m->m_pkthdr.len = hdrlen + len;
1485
1486 switch (af) {
1487 #ifdef INET
1488 case AF_INET:
1489 ip->ip_len = htons(m->m_pkthdr.len);
1490 packetlen = m->m_pkthdr.len;
1491 if (tp->t_inpcb) {
1492 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1493 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1494 }
1495 #ifdef INET6
1496 else if (tp->t_in6pcb) {
1497 ip->ip_ttl = in6_selecthlim(tp->t_in6pcb, NULL); /*XXX*/
1498 ip->ip_tos = 0; /*XXX*/
1499 }
1500 #endif
1501 break;
1502 #endif
1503 #ifdef INET6
1504 case AF_INET6:
1505 packetlen = m->m_pkthdr.len;
1506 ip6->ip6_nxt = IPPROTO_TCP;
1507 if (tp->t_in6pcb) {
1508 /*
1509 * we separately set hoplimit for every segment, since
1510 * the user might want to change the value via
1511 * setsockopt. Also, desired default hop limit might
1512 * be changed via Neighbor Discovery.
1513 */
1514 ip6->ip6_hlim = in6_selecthlim(tp->t_in6pcb,
1515 ro->ro_rt ? ro->ro_rt->rt_ifp : NULL);
1516 }
1517 /* ip6->ip6_flow = ??? */
1518 /* ip6_plen will be filled in ip6_output(). */
1519 break;
1520 #endif
1521 default: /*pacify gcc*/
1522 packetlen = 0;
1523 break;
1524 }
1525
1526 switch (af) {
1527 #ifdef INET
1528 case AF_INET:
1529 {
1530 struct mbuf *opts;
1531
1532 if (tp->t_inpcb)
1533 opts = tp->t_inpcb->inp_options;
1534 else
1535 opts = NULL;
1536 error = ip_output(m, opts, ro,
1537 (tp->t_mtudisc ? IP_MTUDISC : 0) |
1538 (so->so_options & SO_DONTROUTE),
1539 (struct ip_moptions *)0, so);
1540 break;
1541 }
1542 #endif
1543 #ifdef INET6
1544 case AF_INET6:
1545 {
1546 struct ip6_pktopts *opts;
1547
1548 if (tp->t_in6pcb)
1549 opts = tp->t_in6pcb->in6p_outputopts;
1550 else
1551 opts = NULL;
1552 error = ip6_output(m, opts, (struct route_in6 *)ro,
1553 so->so_options & SO_DONTROUTE,
1554 (struct ip6_moptions *)0, so, NULL);
1555 break;
1556 }
1557 #endif
1558 default:
1559 error = EAFNOSUPPORT;
1560 break;
1561 }
1562 if (error) {
1563 out:
1564 if (error == ENOBUFS) {
1565 tcpstat.tcps_selfquench++;
1566 #ifdef INET
1567 if (tp->t_inpcb)
1568 tcp_quench(tp->t_inpcb, 0);
1569 #endif
1570 #ifdef INET6
1571 if (tp->t_in6pcb)
1572 tcp6_quench(tp->t_in6pcb, 0);
1573 #endif
1574 error = 0;
1575 } else if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1576 TCPS_HAVERCVDSYN(tp->t_state)) {
1577 tp->t_softerror = error;
1578 error = 0;
1579 }
1580
1581 /* Back out the seqence number advance. */
1582 if (sack_rxmit)
1583 p->rxmit -= len;
1584
1585 /* Restart the delayed ACK timer, if necessary. */
1586 if (tp->t_flags & TF_DELACK)
1587 TCP_RESTART_DELACK(tp);
1588
1589 return (error);
1590 }
1591
1592 if (packetlen > tp->t_pmtud_mtu_sent)
1593 tp->t_pmtud_mtu_sent = packetlen;
1594
1595 tcpstat.tcps_sndtotal++;
1596 if (tp->t_flags & TF_DELACK)
1597 tcpstat.tcps_delack++;
1598
1599 /*
1600 * Data sent (as far as we can tell).
1601 * If this advertises a larger window than any other segment,
1602 * then remember the size of the advertised window.
1603 * Any pending ACK has now been sent.
1604 */
1605 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1606 tp->rcv_adv = tp->rcv_nxt + win;
1607 tp->last_ack_sent = tp->rcv_nxt;
1608 tp->t_flags &= ~TF_ACKNOW;
1609 TCP_CLEAR_DELACK(tp);
1610 #ifdef DIAGNOSTIC
1611 if (maxburst < 0)
1612 printf("tcp_output: maxburst exceeded by %d\n", -maxburst);
1613 #endif
1614 if (sendalot && (!tcp_do_newreno || --maxburst))
1615 goto again;
1616 return (0);
1617 }
1618
1619 void
1620 tcp_setpersist(struct tcpcb *tp)
1621 {
1622 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + 2);
1623 int nticks;
1624
1625 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1626 panic("tcp_output REXMT");
1627 /*
1628 * Start/restart persistance timer.
1629 */
1630 if (t < tp->t_rttmin)
1631 t = tp->t_rttmin;
1632 TCPT_RANGESET(nticks, t * tcp_backoff[tp->t_rxtshift],
1633 TCPTV_PERSMIN, TCPTV_PERSMAX);
1634 TCP_TIMER_ARM(tp, TCPT_PERSIST, nticks);
1635 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1636 tp->t_rxtshift++;
1637 }
1638
1639 #if defined(INET)
1640 /*
1641 * tcp4_segment: handle M_CSUM_TSOv4 by software.
1642 *
1643 * => always consume m.
1644 * => call output_func with output_arg for each segments.
1645 */
1646
1647 int
1648 tcp4_segment(struct mbuf *m, int (*output_func)(void *, struct mbuf *),
1649 void *output_arg)
1650 {
1651 int mss;
1652 int iphlen;
1653 int thlen;
1654 int hlen;
1655 int len;
1656 struct ip *iph;
1657 struct tcphdr *th;
1658 uint16_t ipid;
1659 uint32_t tcpseq;
1660 struct mbuf *hdr = NULL;
1661 struct mbuf *t;
1662 int error = 0;
1663
1664 KASSERT((m->m_flags & M_PKTHDR) != 0);
1665 KASSERT((m->m_pkthdr.csum_flags & M_CSUM_TSOv4) != 0);
1666
1667 m->m_pkthdr.csum_flags = 0;
1668
1669 len = m->m_pkthdr.len;
1670 KASSERT(len >= sizeof(*iph) + sizeof(*th));
1671
1672 if (m->m_len < sizeof(*iph)) {
1673 m = m_pullup(m, sizeof(*iph));
1674 if (m == NULL) {
1675 error = ENOMEM;
1676 goto quit;
1677 }
1678 }
1679 iph = mtod(m, struct ip *);
1680 iphlen = iph->ip_hl * 4;
1681 KASSERT(iph->ip_v == IPVERSION);
1682 KASSERT(iphlen >= sizeof(*iph));
1683 KASSERT(iph->ip_p == IPPROTO_TCP);
1684 ipid = ntohs(iph->ip_id);
1685
1686 hlen = iphlen + sizeof(*th);
1687 if (m->m_len < hlen) {
1688 m = m_pullup(m, hlen);
1689 if (m == NULL) {
1690 error = ENOMEM;
1691 goto quit;
1692 }
1693 }
1694 th = (void *)(mtod(m, char *) + iphlen);
1695 tcpseq = ntohl(th->th_seq);
1696 thlen = th->th_off * 4;
1697 hlen = iphlen + thlen;
1698
1699 mss = m->m_pkthdr.segsz;
1700 KASSERT(mss != 0);
1701 KASSERT(len > hlen);
1702
1703 t = m_split(m, hlen, M_NOWAIT);
1704 if (t == NULL) {
1705 error = ENOMEM;
1706 goto quit;
1707 }
1708 hdr = m;
1709 m = t;
1710 len -= hlen;
1711 KASSERT(len % mss == 0);
1712 while (len > 0) {
1713 struct mbuf *n;
1714
1715 n = m_dup(hdr, 0, hlen, M_NOWAIT);
1716 if (n == NULL) {
1717 error = ENOMEM;
1718 goto quit;
1719 }
1720 KASSERT(n->m_len == hlen); /* XXX */
1721
1722 t = m_split(m, mss, M_NOWAIT);
1723 if (t == NULL) {
1724 m_freem(n);
1725 error = ENOMEM;
1726 goto quit;
1727 }
1728 m_cat(n, m);
1729 m = t;
1730
1731 KASSERT(n->m_len >= hlen); /* XXX */
1732
1733 n->m_pkthdr.len = hlen + mss;
1734 iph = mtod(n, struct ip *);
1735 KASSERT(iph->ip_v == IPVERSION);
1736 iph->ip_len = htons(n->m_pkthdr.len);
1737 iph->ip_id = htons(ipid);
1738 th = (void *)(mtod(n, char *) + iphlen);
1739 th->th_seq = htonl(tcpseq);
1740 iph->ip_sum = 0;
1741 iph->ip_sum = in_cksum(n, iphlen);
1742 th->th_sum = 0;
1743 th->th_sum = in4_cksum(n, IPPROTO_TCP, iphlen, thlen + mss);
1744
1745 error = (*output_func)(output_arg, n);
1746 if (error) {
1747 goto quit;
1748 }
1749
1750 tcpseq += mss;
1751 ipid++;
1752 len -= mss;
1753 }
1754
1755 quit:
1756 if (hdr != NULL) {
1757 m_freem(hdr);
1758 }
1759 if (m != NULL) {
1760 m_freem(m);
1761 }
1762
1763 return error;
1764 }
1765 #endif /* defined(INET) */
1766