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