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