1 /* $OpenBSD: tcp_output.c,v 1.151 2025/01/05 12:18:48 bluhm Exp $ */
2 /* $NetBSD: tcp_output.c,v 1.16 1997/06/03 16:17:09 kml Exp $ */
3
4 /*
5 * Copyright (c) 1982, 1986, 1988, 1990, 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * @(#)COPYRIGHT 1.1 (NRL) 17 January 1995
33 *
34 * NRL grants permission for redistribution and use in source and binary
35 * forms, with or without modification, of the software and documentation
36 * created at NRL provided that the following conditions are met:
37 *
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgements:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * This product includes software developed at the Information
48 * Technology Division, US Naval Research Laboratory.
49 * 4. Neither the name of the NRL nor the names of its contributors
50 * may be used to endorse or promote products derived from this software
51 * without specific prior written permission.
52 *
53 * THE SOFTWARE PROVIDED BY NRL IS PROVIDED BY NRL AND CONTRIBUTORS ``AS
54 * IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
55 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
56 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NRL OR
57 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
58 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
59 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
60 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
61 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
62 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
63 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 *
65 * The views and conclusions contained in the software and documentation
66 * are those of the authors and should not be interpreted as representing
67 * official policies, either expressed or implied, of the US Naval
68 * Research Laboratory (NRL).
69 */
70
71 #include "pf.h"
72 #include "stoeplitz.h"
73
74 #include <sys/param.h>
75 #include <sys/systm.h>
76 #include <sys/mbuf.h>
77 #include <sys/protosw.h>
78 #include <sys/socket.h>
79 #include <sys/socketvar.h>
80 #include <sys/kernel.h>
81
82 #include <net/if.h>
83 #include <net/if_var.h>
84 #include <net/route.h>
85 #if NPF > 0
86 #include <net/pfvar.h>
87 #endif
88
89 #include <netinet/in.h>
90 #include <netinet/ip.h>
91 #include <netinet/in_pcb.h>
92 #include <netinet/ip_var.h>
93 #include <netinet6/ip6_var.h>
94 #include <netinet/tcp.h>
95 #define TCPOUTFLAGS
96 #include <netinet/tcp_fsm.h>
97 #include <netinet/tcp_seq.h>
98 #include <netinet/tcp_timer.h>
99 #include <netinet/tcp_var.h>
100 #include <netinet/tcp_debug.h>
101
102 #ifdef notyet
103 extern struct mbuf *m_copypack();
104 #endif
105
106 #ifdef TCP_SACK_DEBUG
107 void tcp_print_holes(struct tcpcb *tp);
108
109 void
tcp_print_holes(struct tcpcb * tp)110 tcp_print_holes(struct tcpcb *tp)
111 {
112 struct sackhole *p = tp->snd_holes;
113 if (p == NULL)
114 return;
115 printf("Hole report: start--end dups rxmit\n");
116 while (p) {
117 printf("%x--%x d %d r %x\n", p->start, p->end, p->dups,
118 p->rxmit);
119 p = p->next;
120 }
121 printf("\n");
122 }
123 #endif /* TCP_SACK_DEBUG */
124
125 /*
126 * Returns pointer to a sackhole if there are any pending retransmissions;
127 * NULL otherwise.
128 */
129 struct sackhole *
tcp_sack_output(struct tcpcb * tp)130 tcp_sack_output(struct tcpcb *tp)
131 {
132 struct sackhole *p;
133
134 if (!tp->sack_enable)
135 return (NULL);
136 p = tp->snd_holes;
137 while (p) {
138 if (p->dups >= tcprexmtthresh && SEQ_LT(p->rxmit, p->end)) {
139 if (SEQ_LT(p->rxmit, tp->snd_una)) {/* old SACK hole */
140 p = p->next;
141 continue;
142 }
143 #ifdef TCP_SACK_DEBUG
144 if (p)
145 tcp_print_holes(tp);
146 #endif
147 return (p);
148 }
149 p = p->next;
150 }
151 return (NULL);
152 }
153
154 /*
155 * After a timeout, the SACK list may be rebuilt. This SACK information
156 * should be used to avoid retransmitting SACKed data. This function
157 * traverses the SACK list to see if snd_nxt should be moved forward.
158 */
159
160 void
tcp_sack_adjust(struct tcpcb * tp)161 tcp_sack_adjust(struct tcpcb *tp)
162 {
163 struct sackhole *cur = tp->snd_holes;
164 if (cur == NULL)
165 return; /* No holes */
166 if (SEQ_GEQ(tp->snd_nxt, tp->rcv_lastsack))
167 return; /* We're already beyond any SACKed blocks */
168 /*
169 * Two cases for which we want to advance snd_nxt:
170 * i) snd_nxt lies between end of one hole and beginning of another
171 * ii) snd_nxt lies between end of last hole and rcv_lastsack
172 */
173 while (cur->next) {
174 if (SEQ_LT(tp->snd_nxt, cur->end))
175 return;
176 if (SEQ_GEQ(tp->snd_nxt, cur->next->start))
177 cur = cur->next;
178 else {
179 tp->snd_nxt = cur->next->start;
180 return;
181 }
182 }
183 if (SEQ_LT(tp->snd_nxt, cur->end))
184 return;
185 tp->snd_nxt = tp->rcv_lastsack;
186 return;
187 }
188
189 /*
190 * Tcp output routine: figure out what should be sent and send it.
191 */
192 int
tcp_output(struct tcpcb * tp)193 tcp_output(struct tcpcb *tp)
194 {
195 struct socket *so = tp->t_inpcb->inp_socket;
196 long len, win, rcv_hiwat, txmaxseg;
197 int off, flags, error;
198 struct mbuf *m;
199 struct tcphdr *th;
200 u_int32_t optbuf[howmany(MAX_TCPOPTLEN, sizeof(u_int32_t))];
201 u_char *opt = (u_char *)optbuf;
202 unsigned int optlen, hdrlen, packetlen;
203 int doing_sosend, idle, sendalot = 0;
204 int i, sack_rxmit = 0;
205 struct sackhole *p;
206 uint64_t now;
207 #ifdef TCP_SIGNATURE
208 unsigned int sigoff;
209 #endif /* TCP_SIGNATURE */
210 #ifdef TCP_ECN
211 int do_ecn = atomic_load_int(&tcp_do_ecn);
212 int needect;
213 #endif
214 int tso;
215
216 if (tp->t_flags & TF_BLOCKOUTPUT) {
217 tp->t_flags |= TF_NEEDOUTPUT;
218 return (0);
219 } else
220 tp->t_flags &= ~TF_NEEDOUTPUT;
221
222 #if defined(TCP_SIGNATURE) && defined(DIAGNOSTIC)
223 if (tp->sack_enable && (tp->t_flags & TF_SIGNATURE))
224 return (EINVAL);
225 #endif /* defined(TCP_SIGNATURE) && defined(DIAGNOSTIC) */
226
227 now = tcp_now();
228
229 mtx_enter(&so->so_snd.sb_mtx);
230 doing_sosend = soissending(so);
231 mtx_leave(&so->so_snd.sb_mtx);
232
233 /*
234 * Determine length of data that should be transmitted,
235 * and flags that will be used.
236 * If there is some data or critical controls (SYN, RST)
237 * to send, then transmit; otherwise, investigate further.
238 */
239 idle = (tp->t_flags & TF_LASTIDLE) || (tp->snd_max == tp->snd_una);
240 if (idle && (now - tp->t_rcvtime) >= tp->t_rxtcur)
241 /*
242 * We have been idle for "a while" and no acks are
243 * expected to clock out any data we send --
244 * slow start to get ack "clock" running again.
245 */
246 tp->snd_cwnd = 2 * tp->t_maxseg;
247
248 /* remember 'idle' for next invocation of tcp_output */
249 if (idle && doing_sosend) {
250 tp->t_flags |= TF_LASTIDLE;
251 idle = 0;
252 } else
253 tp->t_flags &= ~TF_LASTIDLE;
254
255 again:
256 /*
257 * If we've recently taken a timeout, snd_max will be greater than
258 * snd_nxt. There may be SACK information that allows us to avoid
259 * resending already delivered data. Adjust snd_nxt accordingly.
260 */
261 if (tp->sack_enable && SEQ_LT(tp->snd_nxt, tp->snd_max))
262 tcp_sack_adjust(tp);
263 off = tp->snd_nxt - tp->snd_una;
264 win = ulmin(tp->snd_wnd, tp->snd_cwnd);
265
266 flags = tcp_outflags[tp->t_state];
267
268 /*
269 * Send any SACK-generated retransmissions. If we're explicitly trying
270 * to send out new data (when sendalot is 1), bypass this function.
271 * If we retransmit in fast recovery mode, decrement snd_cwnd, since
272 * we're replacing a (future) new transmission with a retransmission
273 * now, and we previously incremented snd_cwnd in tcp_input().
274 */
275 if (tp->sack_enable && !sendalot) {
276 if (tp->t_dupacks >= tcprexmtthresh &&
277 (p = tcp_sack_output(tp))) {
278 off = p->rxmit - tp->snd_una;
279 sack_rxmit = 1;
280 /* Coalesce holes into a single retransmission */
281 len = min(tp->t_maxseg, p->end - p->rxmit);
282 if (SEQ_LT(tp->snd_una, tp->snd_last))
283 tp->snd_cwnd -= tp->t_maxseg;
284 }
285 }
286
287 sendalot = 0;
288 tso = 0;
289 /*
290 * If in persist timeout with window of 0, send 1 byte.
291 * Otherwise, if window is small but nonzero
292 * and timer expired, we will send what we can
293 * and go to transmit state.
294 */
295 if (tp->t_force) {
296 if (win == 0) {
297 /*
298 * If we still have some data to send, then
299 * clear the FIN bit. Usually this would
300 * happen below when it realizes that we
301 * aren't sending all the data. However,
302 * if we have exactly 1 byte of unset data,
303 * then it won't clear the FIN bit below,
304 * and if we are in persist state, we wind
305 * up sending the packet without recording
306 * that we sent the FIN bit.
307 *
308 * We can't just blindly clear the FIN bit,
309 * because if we don't have any more data
310 * to send then the probe will be the FIN
311 * itself.
312 */
313 if (off < so->so_snd.sb_cc)
314 flags &= ~TH_FIN;
315 win = 1;
316 } else {
317 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
318 tp->t_rxtshift = 0;
319 }
320 }
321
322 if (!sack_rxmit) {
323 len = ulmin(so->so_snd.sb_cc, win) - off;
324 }
325
326 if (len < 0) {
327 /*
328 * If FIN has been sent but not acked,
329 * but we haven't been called to retransmit,
330 * len will be -1. Otherwise, window shrank
331 * after we sent into it. If window shrank to 0,
332 * cancel pending retransmit, pull snd_nxt back
333 * to (closed) window, and set the persist timer
334 * if it isn't already going. If the window didn't
335 * close completely, just wait for an ACK.
336 */
337 len = 0;
338 if (win == 0) {
339 TCP_TIMER_DISARM(tp, TCPT_REXMT);
340 tp->t_rxtshift = 0;
341 tp->snd_nxt = tp->snd_una;
342 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0)
343 tcp_setpersist(tp);
344 }
345 }
346
347 /*
348 * Never send more than half a buffer full. This insures that we can
349 * always keep 2 packets on the wire, no matter what SO_SNDBUF is, and
350 * therefore acks will never be delayed unless we run out of data to
351 * transmit.
352 */
353 txmaxseg = ulmin(so->so_snd.sb_hiwat / 2, tp->t_maxseg);
354
355 if (len > txmaxseg) {
356 if (atomic_load_int(&tcp_do_tso) &&
357 tp->t_inpcb->inp_options == NULL &&
358 tp->t_inpcb->inp_outputopts6 == NULL &&
359 #ifdef TCP_SIGNATURE
360 ((tp->t_flags & TF_SIGNATURE) == 0) &&
361 #endif
362 len >= 2 * tp->t_maxseg &&
363 tp->rcv_numsacks == 0 && sack_rxmit == 0 &&
364 !(flags & (TH_SYN|TH_RST|TH_FIN))) {
365 tso = 1;
366 /* avoid small chopped packets */
367 if (len > (len / tp->t_maxseg) * tp->t_maxseg) {
368 len = (len / tp->t_maxseg) * tp->t_maxseg;
369 sendalot = 1;
370 }
371 } else {
372 len = txmaxseg;
373 sendalot = 1;
374 }
375 }
376 if (off + len < so->so_snd.sb_cc)
377 flags &= ~TH_FIN;
378
379 mtx_enter(&so->so_rcv.sb_mtx);
380 win = sbspace_locked(so, &so->so_rcv);
381 rcv_hiwat = (long) so->so_rcv.sb_hiwat;
382 mtx_leave(&so->so_rcv.sb_mtx);
383
384 /*
385 * Sender silly window avoidance. If connection is idle
386 * and can send all data, a maximum segment,
387 * at least a maximum default-size segment do it,
388 * or are forced, do it; otherwise don't bother.
389 * If peer's buffer is tiny, then send
390 * when window is at least half open.
391 * If retransmitting (possibly after persist timer forced us
392 * to send into a small window), then must resend.
393 */
394 if (len) {
395 if (len >= txmaxseg)
396 goto send;
397 if ((idle || (tp->t_flags & TF_NODELAY)) &&
398 len + off >= so->so_snd.sb_cc && !doing_sosend &&
399 (tp->t_flags & TF_NOPUSH) == 0)
400 goto send;
401 if (tp->t_force)
402 goto send;
403 if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0)
404 goto send;
405 if (SEQ_LT(tp->snd_nxt, tp->snd_max))
406 goto send;
407 if (sack_rxmit)
408 goto send;
409 }
410
411 /*
412 * Compare available window to amount of window
413 * known to peer (as advertised window less
414 * next expected input). If the difference is at least two
415 * max size segments, or at least 50% of the maximum possible
416 * window, then want to send a window update to peer.
417 */
418 if (win > 0) {
419 /*
420 * "adv" is the amount we can increase the window,
421 * taking into account that we are limited by
422 * TCP_MAXWIN << tp->rcv_scale.
423 */
424 long adv = lmin(win, (long)TCP_MAXWIN << tp->rcv_scale) -
425 (tp->rcv_adv - tp->rcv_nxt);
426
427 if (adv >= (long) (2 * tp->t_maxseg))
428 goto send;
429 if (2 * adv >= rcv_hiwat)
430 goto send;
431 }
432
433 /*
434 * Send if we owe peer an ACK.
435 */
436 if (tp->t_flags & TF_ACKNOW)
437 goto send;
438 if (flags & (TH_SYN|TH_RST))
439 goto send;
440 if (SEQ_GT(tp->snd_up, tp->snd_una))
441 goto send;
442 /*
443 * If our state indicates that FIN should be sent
444 * and we have not yet done so, or we're retransmitting the FIN,
445 * then we need to send.
446 */
447 if (flags & TH_FIN &&
448 ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una))
449 goto send;
450 /*
451 * In SACK, it is possible for tcp_output to fail to send a segment
452 * after the retransmission timer has been turned off. Make sure
453 * that the retransmission timer is set.
454 */
455 if (SEQ_GT(tp->snd_max, tp->snd_una) &&
456 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
457 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
458 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
459 return (0);
460 }
461
462 /*
463 * TCP window updates are not reliable, rather a polling protocol
464 * using ``persist'' packets is used to insure receipt of window
465 * updates. The three ``states'' for the output side are:
466 * idle not doing retransmits or persists
467 * persisting to move a small or zero window
468 * (re)transmitting and thereby not persisting
469 *
470 * tp->t_timer[TCPT_PERSIST]
471 * is set when we are in persist state.
472 * tp->t_force
473 * is set when we are called to send a persist packet.
474 * tp->t_timer[TCPT_REXMT]
475 * is set when we are retransmitting
476 * The output side is idle when both timers are zero.
477 *
478 * If send window is too small, there is data to transmit, and no
479 * retransmit or persist is pending, then go to persist state.
480 * If nothing happens soon, send when timer expires:
481 * if window is nonzero, transmit what we can,
482 * otherwise force out a byte.
483 */
484 if (so->so_snd.sb_cc && TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
485 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
486 tp->t_rxtshift = 0;
487 tcp_setpersist(tp);
488 }
489
490 /*
491 * No reason to send a segment, just return.
492 */
493 return (0);
494
495 send:
496 /*
497 * Before ESTABLISHED, force sending of initial options
498 * unless TCP set not to do any options.
499 * NOTE: we assume that the IP/TCP header plus TCP options
500 * always fit in a single mbuf, leaving room for a maximum
501 * link header, i.e.
502 * max_linkhdr + sizeof(network header) + sizeof(struct tcphdr +
503 * optlen <= MHLEN
504 */
505 optlen = 0;
506
507 switch (tp->pf) {
508 case 0: /*default to PF_INET*/
509 case PF_INET:
510 hdrlen = sizeof(struct ip) + sizeof(struct tcphdr);
511 break;
512 #ifdef INET6
513 case PF_INET6:
514 hdrlen = sizeof(struct ip6_hdr) + sizeof(struct tcphdr);
515 break;
516 #endif /* INET6 */
517 default:
518 return (EPFNOSUPPORT);
519 }
520
521 if (flags & TH_SYN) {
522 tp->snd_nxt = tp->iss;
523 if ((tp->t_flags & TF_NOOPT) == 0) {
524 u_int16_t mss;
525
526 opt[0] = TCPOPT_MAXSEG;
527 opt[1] = 4;
528 mss = htons((u_int16_t) tcp_mss(tp, 0));
529 memcpy(opt + 2, &mss, sizeof(mss));
530 optlen = 4;
531
532 if (flags & TH_ACK)
533 tcp_mss_update(tp);
534 /*
535 * If this is the first SYN of connection (not a SYN
536 * ACK), include SACK_PERMIT_HDR option. If this is a
537 * SYN ACK, include SACK_PERMIT_HDR option if peer has
538 * already done so.
539 */
540 if (tp->sack_enable && ((flags & TH_ACK) == 0 ||
541 (tp->t_flags & TF_SACK_PERMIT))) {
542 *((u_int32_t *) (opt + optlen)) =
543 htonl(TCPOPT_SACK_PERMIT_HDR);
544 optlen += 4;
545 }
546 if ((tp->t_flags & TF_REQ_SCALE) &&
547 ((flags & TH_ACK) == 0 ||
548 (tp->t_flags & TF_RCVD_SCALE))) {
549 *((u_int32_t *) (opt + optlen)) = htonl(
550 TCPOPT_NOP << 24 |
551 TCPOPT_WINDOW << 16 |
552 TCPOLEN_WINDOW << 8 |
553 tp->request_r_scale);
554 optlen += 4;
555 }
556 }
557 }
558
559 /*
560 * Send a timestamp and echo-reply if this is a SYN and our side
561 * wants to use timestamps (TF_REQ_TSTMP is set) or both our side
562 * and our peer have sent timestamps in our SYN's.
563 */
564 if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP &&
565 (flags & TH_RST) == 0 &&
566 ((flags & (TH_SYN|TH_ACK)) == TH_SYN ||
567 (tp->t_flags & TF_RCVD_TSTMP))) {
568 u_int32_t *lp = (u_int32_t *)(opt + optlen);
569
570 /* Form timestamp option as shown in appendix A of RFC 1323. */
571 *lp++ = htonl(TCPOPT_TSTAMP_HDR);
572 *lp++ = htonl(now + tp->ts_modulate);
573 *lp = htonl(tp->ts_recent);
574 optlen += TCPOLEN_TSTAMP_APPA;
575 }
576 /* Set receive buffer autosizing timestamp. */
577 if (tp->rfbuf_ts == 0) {
578 tp->rfbuf_ts = now;
579 tp->rfbuf_cnt = 0;
580 }
581
582 #ifdef TCP_SIGNATURE
583 if (tp->t_flags & TF_SIGNATURE) {
584 u_int8_t *bp = (u_int8_t *)(opt + optlen);
585
586 /* Send signature option */
587 *(bp++) = TCPOPT_SIGNATURE;
588 *(bp++) = TCPOLEN_SIGNATURE;
589 sigoff = optlen + 2;
590
591 {
592 unsigned int i;
593
594 for (i = 0; i < 16; i++)
595 *(bp++) = 0;
596 }
597
598
599 /* Pad options list to the next 32 bit boundary and
600 * terminate it.
601 */
602 *bp++ = TCPOPT_NOP;
603 *bp++ = TCPOPT_NOP;
604
605 optlen += TCPOLEN_SIGLEN;
606 }
607 #endif /* TCP_SIGNATURE */
608
609 /*
610 * Send SACKs if necessary. This should be the last option processed.
611 * Only as many SACKs are sent as are permitted by the maximum options
612 * size. No more than three SACKs are sent.
613 */
614 if (tp->sack_enable && tp->t_state == TCPS_ESTABLISHED &&
615 (tp->t_flags & (TF_SACK_PERMIT|TF_NOOPT)) == TF_SACK_PERMIT &&
616 tp->rcv_numsacks) {
617 u_int32_t *lp = (u_int32_t *)(opt + optlen);
618 u_int32_t *olp = lp++;
619 int count = 0; /* actual number of SACKs inserted */
620 int maxsack = (MAX_TCPOPTLEN - (optlen + 4))/TCPOLEN_SACK;
621
622 tcpstat_inc(tcps_sack_snd_opts);
623 maxsack = min(maxsack, TCP_MAX_SACK);
624 for (i = 0; (i < tp->rcv_numsacks && count < maxsack); i++) {
625 struct sackblk sack = tp->sackblks[i];
626 if (sack.start == 0 && sack.end == 0)
627 continue;
628 *lp++ = htonl(sack.start);
629 *lp++ = htonl(sack.end);
630 count++;
631 }
632 *olp = htonl(TCPOPT_SACK_HDR|(TCPOLEN_SACK*count+2));
633 optlen += TCPOLEN_SACK*count + 4; /* including leading NOPs */
634 }
635
636 #ifdef DIAGNOSTIC
637 if (optlen > MAX_TCPOPTLEN)
638 panic("tcp_output: options too long");
639 #endif /* DIAGNOSTIC */
640
641 hdrlen += optlen;
642
643 /*
644 * Adjust data length if insertion of options will
645 * bump the packet length beyond the t_maxopd length.
646 * Clear the FIN bit because we cut off the tail of
647 * the segment.
648 */
649 if (len > tp->t_maxopd - optlen) {
650 if (tso) {
651 if (len + hdrlen + max_linkhdr > MAXMCLBYTES) {
652 len = MAXMCLBYTES - hdrlen - max_linkhdr;
653 sendalot = 1;
654 }
655 } else {
656 len = tp->t_maxopd - optlen;
657 sendalot = 1;
658 }
659 flags &= ~TH_FIN;
660 }
661
662 #ifdef DIAGNOSTIC
663 if (max_linkhdr + hdrlen > MCLBYTES)
664 panic("tcphdr too big");
665 #endif
666
667 /*
668 * Grab a header mbuf, attaching a copy of data to
669 * be transmitted, and initialize the header from
670 * the template for sends on this connection.
671 */
672 if (len) {
673 if (tp->t_force && len == 1)
674 tcpstat_inc(tcps_sndprobe);
675 else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) {
676 tcpstat_pkt(tcps_sndrexmitpack, tcps_sndrexmitbyte,
677 len);
678 tp->t_sndrexmitpack++;
679 } else {
680 tcpstat_pkt(tcps_sndpack, tcps_sndbyte, len);
681 }
682 #ifdef notyet
683 if ((m = m_copypack(so->so_snd.sb_mb, off,
684 (int)len, max_linkhdr + hdrlen)) == 0) {
685 error = ENOBUFS;
686 goto out;
687 }
688 /*
689 * m_copypack left space for our hdr; use it.
690 */
691 m->m_len += hdrlen;
692 m->m_data -= hdrlen;
693 #else
694 MGETHDR(m, M_DONTWAIT, MT_HEADER);
695 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
696 MCLGET(m, M_DONTWAIT);
697 if ((m->m_flags & M_EXT) == 0) {
698 m_freem(m);
699 m = NULL;
700 }
701 }
702 if (m == NULL) {
703 error = ENOBUFS;
704 goto out;
705 }
706 m->m_data += max_linkhdr;
707 m->m_len = hdrlen;
708 if (len <= m_trailingspace(m)) {
709 m_copydata(so->so_snd.sb_mb, off, (int) len,
710 mtod(m, caddr_t) + hdrlen);
711 m->m_len += len;
712 } else {
713 m->m_next = m_copym(so->so_snd.sb_mb, off, (int) len,
714 M_NOWAIT);
715 if (m->m_next == 0) {
716 (void) m_free(m);
717 error = ENOBUFS;
718 goto out;
719 }
720 }
721 if (so->so_snd.sb_mb->m_flags & M_PKTHDR)
722 m->m_pkthdr.ph_loopcnt =
723 so->so_snd.sb_mb->m_pkthdr.ph_loopcnt;
724 #endif
725 /*
726 * If we're sending everything we've got, set PUSH.
727 * (This will keep happy those implementations which only
728 * give data to the user when a buffer fills or
729 * a PUSH comes in.)
730 */
731 if (off + len == so->so_snd.sb_cc && !doing_sosend)
732 flags |= TH_PUSH;
733 tp->t_sndtime = now;
734 } else {
735 if (tp->t_flags & TF_ACKNOW)
736 tcpstat_inc(tcps_sndacks);
737 else if (flags & (TH_SYN|TH_FIN|TH_RST))
738 tcpstat_inc(tcps_sndctrl);
739 else if (SEQ_GT(tp->snd_up, tp->snd_una))
740 tcpstat_inc(tcps_sndurg);
741 else
742 tcpstat_inc(tcps_sndwinup);
743
744 MGETHDR(m, M_DONTWAIT, MT_HEADER);
745 if (m != NULL && max_linkhdr + hdrlen > MHLEN) {
746 MCLGET(m, M_DONTWAIT);
747 if ((m->m_flags & M_EXT) == 0) {
748 m_freem(m);
749 m = NULL;
750 }
751 }
752 if (m == NULL) {
753 error = ENOBUFS;
754 goto out;
755 }
756 m->m_data += max_linkhdr;
757 m->m_len = hdrlen;
758 }
759 m->m_pkthdr.ph_ifidx = 0;
760 m->m_pkthdr.len = hdrlen + len;
761
762 /* Enable TSO and specify the size of the resulting segments. */
763 if (tso) {
764 SET(m->m_pkthdr.csum_flags, M_TCP_TSO);
765 m->m_pkthdr.ph_mss = tp->t_maxseg;
766 }
767
768 if (!tp->t_template)
769 panic("tcp_output");
770 #ifdef DIAGNOSTIC
771 if (tp->t_template->m_len != hdrlen - optlen)
772 panic("tcp_output: template len != hdrlen - optlen");
773 #endif /* DIAGNOSTIC */
774 memcpy(mtod(m, caddr_t), mtod(tp->t_template, caddr_t),
775 tp->t_template->m_len);
776 th = (struct tcphdr *)(mtod(m, caddr_t) + tp->t_template->m_len -
777 sizeof(struct tcphdr));
778
779 /*
780 * Fill in fields, remembering maximum advertised
781 * window for use in delaying messages about window sizes.
782 * If resending a FIN, be sure not to use a new sequence number.
783 */
784 if ((flags & TH_FIN) && (tp->t_flags & TF_SENTFIN) &&
785 (tp->snd_nxt == tp->snd_max))
786 tp->snd_nxt--;
787 /*
788 * If we are doing retransmissions, then snd_nxt will
789 * not reflect the first unsent octet. For ACK only
790 * packets, we do not want the sequence number of the
791 * retransmitted packet, we want the sequence number
792 * of the next unsent octet. So, if there is no data
793 * (and no SYN or FIN), use snd_max instead of snd_nxt
794 * when filling in ti_seq. But if we are in persist
795 * state, snd_max might reflect one byte beyond the
796 * right edge of the window, so use snd_nxt in that
797 * case, since we know we aren't doing a retransmission.
798 * (retransmit and persist are mutually exclusive...)
799 */
800 if (len || (flags & (TH_SYN|TH_FIN)) ||
801 TCP_TIMER_ISARMED(tp, TCPT_PERSIST))
802 th->th_seq = htonl(tp->snd_nxt);
803 else
804 th->th_seq = htonl(tp->snd_max);
805
806 if (sack_rxmit) {
807 /*
808 * If sendalot was turned on (due to option stuffing), turn it
809 * off. Properly set th_seq field. Advance the ret'x pointer
810 * by len.
811 */
812 if (sendalot)
813 sendalot = 0;
814 th->th_seq = htonl(p->rxmit);
815 p->rxmit += len;
816 tcpstat_pkt(tcps_sack_rexmits, tcps_sack_rexmit_bytes, len);
817 }
818
819 th->th_ack = htonl(tp->rcv_nxt);
820 if (optlen) {
821 memcpy(th + 1, opt, optlen);
822 th->th_off = (sizeof (struct tcphdr) + optlen) >> 2;
823 }
824 #ifdef TCP_ECN
825 if (do_ecn) {
826 /*
827 * if we have received congestion experienced segs,
828 * set ECE bit.
829 */
830 if (tp->t_flags & TF_RCVD_CE) {
831 flags |= TH_ECE;
832 tcpstat_inc(tcps_ecn_sndece);
833 }
834 if (!(tp->t_flags & TF_DISABLE_ECN)) {
835 /*
836 * if this is a SYN seg, set ECE and CWR.
837 * set only ECE for SYN-ACK if peer supports ECN.
838 */
839 if ((flags & (TH_SYN|TH_ACK)) == TH_SYN)
840 flags |= (TH_ECE|TH_CWR);
841 else if ((tp->t_flags & TF_ECN_PERMIT) &&
842 (flags & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK))
843 flags |= TH_ECE;
844 }
845 /*
846 * if we have reduced the congestion window, notify
847 * the peer by setting CWR bit.
848 */
849 if ((tp->t_flags & TF_ECN_PERMIT) &&
850 (tp->t_flags & TF_SEND_CWR)) {
851 flags |= TH_CWR;
852 tp->t_flags &= ~TF_SEND_CWR;
853 tcpstat_inc(tcps_ecn_sndcwr);
854 }
855 }
856 #endif
857 th->th_flags = flags;
858
859 /*
860 * Calculate receive window. Don't shrink window,
861 * but avoid silly window syndrome.
862 */
863 if (win < (rcv_hiwat / 4) && win < (long)tp->t_maxseg)
864 win = 0;
865 if (win > (long)TCP_MAXWIN << tp->rcv_scale)
866 win = (long)TCP_MAXWIN << tp->rcv_scale;
867 if (win < (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt))
868 win = (long)(int32_t)(tp->rcv_adv - tp->rcv_nxt);
869 if (flags & TH_RST)
870 win = 0;
871 th->th_win = htons((u_int16_t) (win>>tp->rcv_scale));
872 if (th->th_win == 0)
873 tp->t_sndzerowin++;
874 if (SEQ_GT(tp->snd_up, tp->snd_nxt)) {
875 u_int32_t urp = tp->snd_up - tp->snd_nxt;
876 if (urp > IP_MAXPACKET)
877 urp = IP_MAXPACKET;
878 th->th_urp = htons((u_int16_t)urp);
879 th->th_flags |= TH_URG;
880 } else
881 /*
882 * If no urgent pointer to send, then we pull
883 * the urgent pointer to the left edge of the send window
884 * so that it doesn't drift into the send window on sequence
885 * number wraparound.
886 */
887 tp->snd_up = tp->snd_una; /* drag it along */
888
889 #ifdef TCP_SIGNATURE
890 if (tp->t_flags & TF_SIGNATURE) {
891 int iphlen;
892 union sockaddr_union src, dst;
893 struct tdb *tdb;
894
895 bzero(&src, sizeof(union sockaddr_union));
896 bzero(&dst, sizeof(union sockaddr_union));
897
898 switch (tp->pf) {
899 case 0: /*default to PF_INET*/
900 case AF_INET:
901 iphlen = sizeof(struct ip);
902 src.sa.sa_len = sizeof(struct sockaddr_in);
903 src.sa.sa_family = AF_INET;
904 src.sin.sin_addr = mtod(m, struct ip *)->ip_src;
905 dst.sa.sa_len = sizeof(struct sockaddr_in);
906 dst.sa.sa_family = AF_INET;
907 dst.sin.sin_addr = mtod(m, struct ip *)->ip_dst;
908 break;
909 #ifdef INET6
910 case AF_INET6:
911 iphlen = sizeof(struct ip6_hdr);
912 src.sa.sa_len = sizeof(struct sockaddr_in6);
913 src.sa.sa_family = AF_INET6;
914 src.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_src;
915 dst.sa.sa_len = sizeof(struct sockaddr_in6);
916 dst.sa.sa_family = AF_INET6;
917 dst.sin6.sin6_addr = mtod(m, struct ip6_hdr *)->ip6_dst;
918 break;
919 #endif /* INET6 */
920 }
921
922 tdb = gettdbbysrcdst(rtable_l2(tp->t_inpcb->inp_rtableid),
923 0, &src, &dst, IPPROTO_TCP);
924 if (tdb == NULL) {
925 m_freem(m);
926 return (EPERM);
927 }
928
929 if (tcp_signature(tdb, tp->pf, m, th, iphlen, 0,
930 mtod(m, caddr_t) + hdrlen - optlen + sigoff) < 0) {
931 m_freem(m);
932 tdb_unref(tdb);
933 return (EINVAL);
934 }
935 tdb_unref(tdb);
936 }
937 #endif /* TCP_SIGNATURE */
938
939 /* Defer checksumming until later (ip_output() or hardware) */
940 m->m_pkthdr.csum_flags |= M_TCP_CSUM_OUT;
941
942 /*
943 * In transmit state, time the transmission and arrange for
944 * the retransmit. In persist state, just set snd_max.
945 */
946 if (tp->t_force == 0 || TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
947 tcp_seq startseq = tp->snd_nxt;
948
949 /*
950 * Advance snd_nxt over sequence space of this segment.
951 */
952 if (flags & (TH_SYN|TH_FIN)) {
953 if (flags & TH_SYN)
954 tp->snd_nxt++;
955 if (flags & TH_FIN) {
956 tp->snd_nxt++;
957 tp->t_flags |= TF_SENTFIN;
958 }
959 }
960 if (tp->sack_enable) {
961 if (sack_rxmit && (p->rxmit != tp->snd_nxt)) {
962 goto timer;
963 }
964 }
965 tp->snd_nxt += len;
966 if (SEQ_GT(tp->snd_nxt, tp->snd_max)) {
967 tp->snd_max = tp->snd_nxt;
968 /*
969 * Time this transmission if not a retransmission and
970 * not currently timing anything.
971 */
972 if (tp->t_rtttime == 0) {
973 tp->t_rtttime = now;
974 tp->t_rtseq = startseq;
975 tcpstat_inc(tcps_segstimed);
976 }
977 }
978
979 /*
980 * Set retransmit timer if not currently set,
981 * and not doing an ack or a keep-alive probe.
982 * Initial value for retransmit timer is smoothed
983 * round-trip time + 2 * round-trip time variance.
984 * Initialize shift counter which is used for backoff
985 * of retransmit time.
986 */
987 timer:
988 if (tp->sack_enable && sack_rxmit &&
989 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
990 tp->snd_nxt != tp->snd_max) {
991 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
992 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
993 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
994 tp->t_rxtshift = 0;
995 }
996 }
997
998 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
999 tp->snd_nxt != tp->snd_una) {
1000 TCP_TIMER_ARM(tp, TCPT_REXMT, tp->t_rxtcur);
1001 if (TCP_TIMER_ISARMED(tp, TCPT_PERSIST)) {
1002 TCP_TIMER_DISARM(tp, TCPT_PERSIST);
1003 tp->t_rxtshift = 0;
1004 }
1005 }
1006
1007 if (len == 0 && so->so_snd.sb_cc &&
1008 TCP_TIMER_ISARMED(tp, TCPT_REXMT) == 0 &&
1009 TCP_TIMER_ISARMED(tp, TCPT_PERSIST) == 0) {
1010 /*
1011 * Avoid a situation where we do not set persist timer
1012 * after a zero window condition. For example:
1013 * 1) A -> B: packet with enough data to fill the window
1014 * 2) B -> A: ACK for #1 + new data (0 window
1015 * advertisement)
1016 * 3) A -> B: ACK for #2, 0 len packet
1017 *
1018 * In this case, A will not activate the persist timer,
1019 * because it chose to send a packet. Unless tcp_output
1020 * is called for some other reason (delayed ack timer,
1021 * another input packet from B, socket syscall), A will
1022 * not send zero window probes.
1023 *
1024 * So, if you send a 0-length packet, but there is data
1025 * in the socket buffer, and neither the rexmt or
1026 * persist timer is already set, then activate the
1027 * persist timer.
1028 */
1029 tp->t_rxtshift = 0;
1030 tcp_setpersist(tp);
1031 }
1032 } else
1033 if (SEQ_GT(tp->snd_nxt + len, tp->snd_max))
1034 tp->snd_max = tp->snd_nxt + len;
1035
1036 tcp_update_sndspace(tp);
1037
1038 /*
1039 * Trace.
1040 */
1041 if (so->so_options & SO_DEBUG)
1042 tcp_trace(TA_OUTPUT, tp->t_state, tp, tp, mtod(m, caddr_t), 0,
1043 len);
1044
1045 /*
1046 * Fill in IP length and desired time to live and
1047 * send to IP level. There should be a better way
1048 * to handle ttl and tos; we could keep them in
1049 * the template, but need a way to checksum without them.
1050 */
1051
1052 #ifdef TCP_ECN
1053 /*
1054 * if peer is ECN capable, set the ECT bit in the IP header.
1055 * but don't set ECT for a pure ack, a retransmit or a window probe.
1056 */
1057 needect = 0;
1058 if (do_ecn && (tp->t_flags & TF_ECN_PERMIT)) {
1059 if (len == 0 || SEQ_LT(tp->snd_nxt, tp->snd_max) ||
1060 (tp->t_force && len == 1)) {
1061 /* don't set ECT */
1062 } else {
1063 needect = 1;
1064 tcpstat_inc(tcps_ecn_sndect);
1065 }
1066 }
1067 #endif
1068
1069 /* force routing table */
1070 m->m_pkthdr.ph_rtableid = tp->t_inpcb->inp_rtableid;
1071
1072 #if NPF > 0
1073 pf_mbuf_link_inpcb(m, tp->t_inpcb);
1074 #endif
1075
1076 switch (tp->pf) {
1077 case 0: /*default to PF_INET*/
1078 case AF_INET:
1079 {
1080 struct ip *ip;
1081
1082 ip = mtod(m, struct ip *);
1083 ip->ip_len = htons(m->m_pkthdr.len);
1084 packetlen = m->m_pkthdr.len;
1085 ip->ip_ttl = tp->t_inpcb->inp_ip.ip_ttl;
1086 ip->ip_tos = tp->t_inpcb->inp_ip.ip_tos;
1087 #ifdef TCP_ECN
1088 if (needect)
1089 ip->ip_tos |= IPTOS_ECN_ECT0;
1090 #endif
1091 }
1092 #if NSTOEPLITZ > 0
1093 m->m_pkthdr.ph_flowid = tp->t_inpcb->inp_flowid;
1094 SET(m->m_pkthdr.csum_flags, M_FLOWID);
1095 #endif
1096 error = ip_output(m, tp->t_inpcb->inp_options,
1097 &tp->t_inpcb->inp_route,
1098 (ip_mtudisc ? IP_MTUDISC : 0), NULL,
1099 &tp->t_inpcb->inp_seclevel, 0);
1100 break;
1101 #ifdef INET6
1102 case AF_INET6:
1103 {
1104 struct ip6_hdr *ip6;
1105
1106 ip6 = mtod(m, struct ip6_hdr *);
1107 ip6->ip6_plen = m->m_pkthdr.len -
1108 sizeof(struct ip6_hdr);
1109 packetlen = m->m_pkthdr.len;
1110 ip6->ip6_nxt = IPPROTO_TCP;
1111 ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb);
1112 #ifdef TCP_ECN
1113 if (needect)
1114 ip6->ip6_flow |= htonl(IPTOS_ECN_ECT0 << 20);
1115 #endif
1116 }
1117 error = ip6_output(m, tp->t_inpcb->inp_outputopts6,
1118 &tp->t_inpcb->inp_route, 0, NULL,
1119 &tp->t_inpcb->inp_seclevel);
1120 break;
1121 #endif /* INET6 */
1122 }
1123
1124 if (error) {
1125 out:
1126 if (error == ENOBUFS) {
1127 /*
1128 * If the interface queue is full, or IP cannot
1129 * get an mbuf, trigger TCP slow start.
1130 */
1131 tp->snd_cwnd = tp->t_maxseg;
1132 return (0);
1133 }
1134 if (error == EMSGSIZE) {
1135 /*
1136 * ip_output() will have already fixed the route
1137 * for us. tcp_mtudisc() will, as its last action,
1138 * initiate retransmission, so it is important to
1139 * not do so here.
1140 */
1141 tcp_mtudisc(tp->t_inpcb, -1);
1142 return (0);
1143 }
1144 if ((error == EHOSTUNREACH || error == ENETDOWN) &&
1145 TCPS_HAVERCVDSYN(tp->t_state)) {
1146 tp->t_softerror = error;
1147 return (0);
1148 }
1149
1150 /* Restart the delayed ACK timer, if necessary. */
1151 if (TCP_TIMER_ISARMED(tp, TCPT_DELACK))
1152 TCP_TIMER_ARM(tp, TCPT_DELACK, tcp_delack_msecs);
1153
1154 return (error);
1155 }
1156
1157 if (packetlen > tp->t_pmtud_mtu_sent)
1158 tp->t_pmtud_mtu_sent = packetlen;
1159
1160 tcpstat_inc(tcps_sndtotal);
1161 if (TCP_TIMER_ISARMED(tp, TCPT_DELACK))
1162 tcpstat_inc(tcps_delack);
1163
1164 /*
1165 * Data sent (as far as we can tell).
1166 * If this advertises a larger window than any other segment,
1167 * then remember the size of the advertised window.
1168 * Any pending ACK has now been sent.
1169 */
1170 if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv))
1171 tp->rcv_adv = tp->rcv_nxt + win;
1172 tp->last_ack_sent = tp->rcv_nxt;
1173 tp->t_sndacktime = now;
1174 tp->t_flags &= ~TF_ACKNOW;
1175 TCP_TIMER_DISARM(tp, TCPT_DELACK);
1176 if (sendalot)
1177 goto again;
1178 return (0);
1179 }
1180
1181 void
tcp_setpersist(struct tcpcb * tp)1182 tcp_setpersist(struct tcpcb *tp)
1183 {
1184 int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> (1 + TCP_RTT_BASE_SHIFT);
1185 int msec;
1186
1187 if (TCP_TIMER_ISARMED(tp, TCPT_REXMT))
1188 panic("tcp_output REXMT");
1189 /*
1190 * Start/restart persistence timer.
1191 */
1192 if (t < tp->t_rttmin)
1193 t = tp->t_rttmin;
1194 TCPT_RANGESET(msec, t * tcp_backoff[tp->t_rxtshift],
1195 TCPTV_PERSMIN, TCPTV_PERSMAX);
1196 TCP_TIMER_ARM(tp, TCPT_PERSIST, msec);
1197 if (tp->t_rxtshift < TCP_MAXRXTSHIFT)
1198 tp->t_rxtshift++;
1199 }
1200
1201 int
tcp_chopper(struct mbuf * m0,struct mbuf_list * ml,struct ifnet * ifp,u_int mss)1202 tcp_chopper(struct mbuf *m0, struct mbuf_list *ml, struct ifnet *ifp,
1203 u_int mss)
1204 {
1205 struct ip *ip = NULL;
1206 #ifdef INET6
1207 struct ip6_hdr *ip6 = NULL;
1208 #endif
1209 struct tcphdr *th;
1210 int firstlen, iphlen, hlen, tlen, off;
1211 int error;
1212
1213 ml_init(ml);
1214 ml_enqueue(ml, m0);
1215
1216 if (mss == 0) {
1217 error = EINVAL;
1218 goto bad;
1219 }
1220
1221 ip = mtod(m0, struct ip *);
1222 switch (ip->ip_v) {
1223 case 4:
1224 iphlen = ip->ip_hl << 2;
1225 if (ISSET(ip->ip_off, htons(IP_OFFMASK | IP_MF)) ||
1226 iphlen != sizeof(struct ip) || ip->ip_p != IPPROTO_TCP) {
1227 /* only TCP without fragment or IP option supported */
1228 error = EPROTOTYPE;
1229 goto bad;
1230 }
1231 break;
1232 #ifdef INET6
1233 case 6:
1234 ip = NULL;
1235 ip6 = mtod(m0, struct ip6_hdr *);
1236 iphlen = sizeof(struct ip6_hdr);
1237 if (ip6->ip6_nxt != IPPROTO_TCP) {
1238 /* only TCP without IPv6 header chain supported */
1239 error = EPROTOTYPE;
1240 goto bad;
1241 }
1242 break;
1243 #endif
1244 default:
1245 panic("%s: unknown ip version %d", __func__, ip->ip_v);
1246 }
1247
1248 tlen = m0->m_pkthdr.len;
1249 if (tlen < iphlen + sizeof(struct tcphdr)) {
1250 error = ENOPROTOOPT;
1251 goto bad;
1252 }
1253 /* IP and TCP header should be contiguous, this check is paranoia */
1254 if (m0->m_len < iphlen + sizeof(*th)) {
1255 ml_dequeue(ml);
1256 if ((m0 = m_pullup(m0, iphlen + sizeof(*th))) == NULL) {
1257 error = ENOBUFS;
1258 goto bad;
1259 }
1260 ml_enqueue(ml, m0);
1261 }
1262 th = (struct tcphdr *)(mtod(m0, caddr_t) + iphlen);
1263 hlen = iphlen + (th->th_off << 2);
1264 if (tlen < hlen) {
1265 error = ENOPROTOOPT;
1266 goto bad;
1267 }
1268 firstlen = MIN(tlen - hlen, mss);
1269
1270 CLR(m0->m_pkthdr.csum_flags, M_TCP_TSO);
1271
1272 /*
1273 * Loop through length of payload after first segment,
1274 * make new header and copy data of each part and link onto chain.
1275 */
1276 for (off = hlen + firstlen; off < tlen; off += mss) {
1277 struct mbuf *m;
1278 struct tcphdr *mhth;
1279 int len;
1280
1281 len = MIN(tlen - off, mss);
1282
1283 MGETHDR(m, M_DONTWAIT, MT_HEADER);
1284 if (m == NULL) {
1285 error = ENOBUFS;
1286 goto bad;
1287 }
1288 ml_enqueue(ml, m);
1289 if ((error = m_dup_pkthdr(m, m0, M_DONTWAIT)) != 0)
1290 goto bad;
1291
1292 /* IP and TCP header to the end, space for link layer header */
1293 m->m_len = hlen;
1294 m_align(m, hlen);
1295
1296 /* copy and adjust TCP header */
1297 mhth = (struct tcphdr *)(mtod(m, caddr_t) + iphlen);
1298 memcpy(mhth, th, hlen - iphlen);
1299 mhth->th_seq = htonl(ntohl(th->th_seq) + (off - hlen));
1300 if (off + len < tlen)
1301 CLR(mhth->th_flags, TH_PUSH|TH_FIN);
1302
1303 /* add mbuf chain with payload */
1304 m->m_pkthdr.len = hlen + len;
1305 if ((m->m_next = m_copym(m0, off, len, M_DONTWAIT)) == NULL) {
1306 error = ENOBUFS;
1307 goto bad;
1308 }
1309
1310 /* copy and adjust IP header, calculate checksum */
1311 SET(m->m_pkthdr.csum_flags, M_TCP_CSUM_OUT);
1312 if (ip) {
1313 struct ip *mhip;
1314
1315 mhip = mtod(m, struct ip *);
1316 *mhip = *ip;
1317 mhip->ip_len = htons(hlen + len);
1318 mhip->ip_id = htons(ip_randomid());
1319 in_hdr_cksum_out(m, ifp);
1320 in_proto_cksum_out(m, ifp);
1321 }
1322 #ifdef INET6
1323 if (ip6) {
1324 struct ip6_hdr *mhip6;
1325
1326 mhip6 = mtod(m, struct ip6_hdr *);
1327 *mhip6 = *ip6;
1328 mhip6->ip6_plen = htons(hlen - iphlen + len);
1329 in6_proto_cksum_out(m, ifp);
1330 }
1331 #endif
1332 }
1333
1334 /*
1335 * Update first segment by trimming what's been copied out
1336 * and updating header, then send each segment (in order).
1337 */
1338 if (hlen + firstlen < tlen) {
1339 m_adj(m0, hlen + firstlen - tlen);
1340 CLR(th->th_flags, TH_PUSH|TH_FIN);
1341 }
1342 /* adjust IP header, calculate checksum */
1343 SET(m0->m_pkthdr.csum_flags, M_TCP_CSUM_OUT);
1344 if (ip) {
1345 ip->ip_len = htons(m0->m_pkthdr.len);
1346 in_hdr_cksum_out(m0, ifp);
1347 in_proto_cksum_out(m0, ifp);
1348 }
1349 #ifdef INET6
1350 if (ip6) {
1351 ip6->ip6_plen = htons(m0->m_pkthdr.len - iphlen);
1352 in6_proto_cksum_out(m0, ifp);
1353 }
1354 #endif
1355
1356 tcpstat_add(tcps_outpkttso, ml_len(ml));
1357 return 0;
1358
1359 bad:
1360 tcpstat_inc(tcps_outbadtso);
1361 ml_purge(ml);
1362 return error;
1363 }
1364
1365 int
tcp_if_output_tso(struct ifnet * ifp,struct mbuf ** mp,struct sockaddr * dst,struct rtentry * rt,uint32_t ifcap,u_int mtu)1366 tcp_if_output_tso(struct ifnet *ifp, struct mbuf **mp, struct sockaddr *dst,
1367 struct rtentry *rt, uint32_t ifcap, u_int mtu)
1368 {
1369 struct mbuf_list ml;
1370 int error;
1371
1372 /* caller must fail later or fragment */
1373 if (!ISSET((*mp)->m_pkthdr.csum_flags, M_TCP_TSO))
1374 return 0;
1375 if ((*mp)->m_pkthdr.ph_mss > mtu) {
1376 CLR((*mp)->m_pkthdr.csum_flags, M_TCP_TSO);
1377 return 0;
1378 }
1379
1380 /* network interface hardware will do TSO */
1381 if (in_ifcap_cksum(*mp, ifp, ifcap)) {
1382 if (ISSET(ifcap, IFCAP_TSOv4)) {
1383 in_hdr_cksum_out(*mp, ifp);
1384 in_proto_cksum_out(*mp, ifp);
1385 }
1386 #ifdef INET6
1387 if (ISSET(ifcap, IFCAP_TSOv6))
1388 in6_proto_cksum_out(*mp, ifp);
1389 #endif
1390 error = ifp->if_output(ifp, *mp, dst, rt);
1391 if (!error)
1392 tcpstat_inc(tcps_outhwtso);
1393 goto done;
1394 }
1395
1396 /* as fallback do TSO in software */
1397 if ((error = tcp_chopper(*mp, &ml, ifp, (*mp)->m_pkthdr.ph_mss)) ||
1398 (error = if_output_ml(ifp, &ml, dst, rt)))
1399 goto done;
1400 tcpstat_inc(tcps_outswtso);
1401
1402 done:
1403 *mp = NULL;
1404 return error;
1405 }
1406