]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* | |
23 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 | |
24 | * The Regents of the University of California. All rights reserved. | |
25 | * | |
26 | * Redistribution and use in source and binary forms, with or without | |
27 | * modification, are permitted provided that the following conditions | |
28 | * are met: | |
29 | * 1. Redistributions of source code must retain the above copyright | |
30 | * notice, this list of conditions and the following disclaimer. | |
31 | * 2. Redistributions in binary form must reproduce the above copyright | |
32 | * notice, this list of conditions and the following disclaimer in the | |
33 | * documentation and/or other materials provided with the distribution. | |
34 | * 3. All advertising materials mentioning features or use of this software | |
35 | * must display the following acknowledgement: | |
36 | * This product includes software developed by the University of | |
37 | * California, Berkeley and its contributors. | |
38 | * 4. Neither the name of the University nor the names of its contributors | |
39 | * may be used to endorse or promote products derived from this software | |
40 | * without specific prior written permission. | |
41 | * | |
42 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
43 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
44 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
45 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
46 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
47 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
48 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
49 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
50 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
51 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
52 | * SUCH DAMAGE. | |
53 | * | |
54 | * @(#)tcp_output.c 8.4 (Berkeley) 5/24/95 | |
9bccf70c | 55 | * $FreeBSD: src/sys/netinet/tcp_output.c,v 1.39.2.10 2001/07/07 04:30:38 silby Exp $ |
1c79356b A |
56 | */ |
57 | ||
1c79356b A |
58 | #define _IP_VHL |
59 | ||
1c79356b A |
60 | |
61 | #include <sys/param.h> | |
62 | #include <sys/systm.h> | |
9bccf70c A |
63 | #include <sys/kernel.h> |
64 | #include <sys/sysctl.h> | |
1c79356b A |
65 | #include <sys/mbuf.h> |
66 | #include <sys/domain.h> | |
67 | #include <sys/protosw.h> | |
68 | #include <sys/socket.h> | |
69 | #include <sys/socketvar.h> | |
70 | ||
71 | #include <net/route.h> | |
72 | ||
73 | #include <netinet/in.h> | |
74 | #include <netinet/in_systm.h> | |
75 | #include <netinet/ip.h> | |
9bccf70c | 76 | #include <netinet/in_pcb.h> |
1c79356b A |
77 | #include <netinet/ip_var.h> |
78 | #if INET6 | |
9bccf70c | 79 | #include <netinet6/in6_pcb.h> |
1c79356b | 80 | #include <netinet/ip6.h> |
1c79356b A |
81 | #include <netinet6/ip6_var.h> |
82 | #endif | |
1c79356b A |
83 | #include <netinet/tcp.h> |
84 | #define TCPOUTFLAGS | |
85 | #include <netinet/tcp_fsm.h> | |
86 | #include <netinet/tcp_seq.h> | |
87 | #include <netinet/tcp_timer.h> | |
88 | #include <netinet/tcp_var.h> | |
89 | #include <netinet/tcpip.h> | |
90 | #if TCPDEBUG | |
91 | #include <netinet/tcp_debug.h> | |
92 | #endif | |
93 | #include <sys/kdebug.h> | |
94 | ||
9bccf70c A |
95 | #if IPSEC |
96 | #include <netinet6/ipsec.h> | |
97 | #endif /*IPSEC*/ | |
fa4905b1 | 98 | |
1c79356b A |
99 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETTCP, 1) |
100 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETTCP, 3) | |
101 | #define DBG_FNC_TCP_OUTPUT NETDBG_CODE(DBG_NETTCP, (4 << 8) | 1) | |
102 | ||
103 | ||
104 | #ifdef notyet | |
105 | extern struct mbuf *m_copypack(); | |
106 | #endif | |
107 | ||
9bccf70c A |
108 | static int path_mtu_discovery = 1; |
109 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, path_mtu_discovery, CTLFLAG_RW, | |
110 | &path_mtu_discovery, 1, "Enable Path MTU Discovery"); | |
111 | ||
112 | int ss_fltsz = 1; | |
113 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, slowstart_flightsize, CTLFLAG_RW, | |
114 | &ss_fltsz, 1, "Slow start flight size"); | |
115 | ||
d12e1678 | 116 | int ss_fltsz_local = 4; /* starts with four segments max */ |
9bccf70c A |
117 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, local_slowstart_flightsize, CTLFLAG_RW, |
118 | &ss_fltsz_local, 1, "Slow start flight size for local networks"); | |
119 | ||
120 | int tcp_do_newreno = 0; | |
121 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, newreno, CTLFLAG_RW, &tcp_do_newreno, | |
122 | 0, "Enable NewReno Algorithms"); | |
123 | ||
91447636 A |
124 | int tcp_packet_chaining = 50; |
125 | SYSCTL_INT(_net_inet_tcp, OID_AUTO, packetchain, CTLFLAG_RW, &tcp_packet_chaining, | |
126 | 0, "Enable TCP output packet chaining"); | |
127 | ||
128 | struct mbuf *m_copym_with_hdrs(struct mbuf*, int, int, int, struct mbuf**, int*); | |
129 | static long packchain_newlist = 0; | |
130 | static long packchain_looped = 0; | |
131 | static long packchain_sent = 0; | |
9bccf70c A |
132 | |
133 | ||
134 | /* temporary: for testing */ | |
135 | #if IPSEC | |
136 | extern int ipsec_bypass; | |
137 | #endif | |
1c79356b | 138 | |
d12e1678 | 139 | extern int slowlink_wsize; /* window correction for slow links */ |
55e303ae | 140 | extern u_long route_generation; |
91447636 A |
141 | extern int fw_enable; /* firewall is on: disable packet chaining */ |
142 | extern int ipsec_bypass; | |
55e303ae | 143 | |
91447636 A |
144 | extern vm_size_t so_cache_zone_element_size; |
145 | ||
146 | static __inline__ u_int16_t | |
147 | get_socket_id(struct socket * s) | |
148 | { | |
149 | u_int16_t val; | |
150 | ||
151 | if (so_cache_zone_element_size == 0) { | |
152 | return (0); | |
153 | } | |
154 | val = (u_int16_t)(((u_int32_t)s) / so_cache_zone_element_size); | |
155 | if (val == 0) { | |
156 | val = 0xffff; | |
157 | } | |
158 | return (val); | |
159 | } | |
d12e1678 | 160 | |
1c79356b A |
161 | /* |
162 | * Tcp output routine: figure out what should be sent and send it. | |
163 | */ | |
164 | int | |
165 | tcp_output(tp) | |
166 | register struct tcpcb *tp; | |
167 | { | |
168 | register struct socket *so = tp->t_inpcb->inp_socket; | |
169 | register long len, win; | |
170 | int off, flags, error; | |
171 | register struct mbuf *m; | |
172 | struct ip *ip = NULL; | |
9bccf70c | 173 | register struct ipovly *ipov = NULL; |
1c79356b A |
174 | #if INET6 |
175 | struct ip6_hdr *ip6 = NULL; | |
176 | #endif /* INET6 */ | |
9bccf70c | 177 | register struct tcphdr *th; |
1c79356b A |
178 | u_char opt[TCP_MAXOLEN]; |
179 | unsigned ipoptlen, optlen, hdrlen; | |
91447636 | 180 | int idle, sendalot, howmuchsent = 0; |
9bccf70c | 181 | int maxburst = TCP_MAXBURST; |
1c79356b A |
182 | struct rmxp_tao *taop; |
183 | struct rmxp_tao tao_noncached; | |
55e303ae | 184 | int last_off = 0; |
fa4905b1 A |
185 | int m_off; |
186 | struct mbuf *m_last = 0; | |
187 | struct mbuf *m_head = 0; | |
91447636 A |
188 | struct mbuf *packetlist = 0; |
189 | struct mbuf *lastpacket = 0; | |
9bccf70c | 190 | #if INET6 |
55e303ae | 191 | int isipv6 = tp->t_inpcb->inp_vflag & INP_IPV6 ; |
9bccf70c | 192 | #endif |
91447636 A |
193 | short packchain_listadd = 0; |
194 | u_int16_t socket_id = get_socket_id(so); | |
fa4905b1 | 195 | |
55e303ae | 196 | |
1c79356b A |
197 | /* |
198 | * Determine length of data that should be transmitted, | |
199 | * and flags that will be used. | |
200 | * If there is some data or critical controls (SYN, RST) | |
201 | * to send, then transmit; otherwise, investigate further. | |
202 | */ | |
203 | idle = (tp->snd_max == tp->snd_una); | |
9bccf70c | 204 | if (idle && tp->t_rcvtime >= tp->t_rxtcur) { |
1c79356b A |
205 | /* |
206 | * We have been idle for "a while" and no acks are | |
207 | * expected to clock out any data we send -- | |
208 | * slow start to get ack "clock" running again. | |
9bccf70c A |
209 | * |
210 | * Set the slow-start flight size depending on whether | |
211 | * this is a local network or not. | |
212 | */ | |
213 | if ( | |
214 | #if INET6 | |
215 | (isipv6 && in6_localaddr(&tp->t_inpcb->in6p_faddr)) || | |
216 | (!isipv6 && | |
217 | #endif | |
218 | in_localaddr(tp->t_inpcb->inp_faddr) | |
219 | #if INET6 | |
220 | ) | |
221 | #endif | |
222 | ) | |
223 | tp->snd_cwnd = tp->t_maxseg * ss_fltsz_local; | |
224 | else | |
225 | tp->snd_cwnd = tp->t_maxseg * ss_fltsz; | |
226 | } | |
55e303ae | 227 | |
1c79356b | 228 | again: |
55e303ae A |
229 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0); |
230 | ||
231 | #if INET6 | |
232 | if (isipv6) { | |
233 | ||
234 | KERNEL_DEBUG(DBG_LAYER_BEG, | |
235 | ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport), | |
236 | (((tp->t_inpcb->in6p_laddr.s6_addr16[0] & 0xffff) << 16) | | |
237 | (tp->t_inpcb->in6p_faddr.s6_addr16[0] & 0xffff)), | |
238 | sendalot,0,0); | |
239 | } | |
240 | else | |
241 | #endif | |
242 | ||
243 | { | |
244 | KERNEL_DEBUG(DBG_LAYER_BEG, | |
245 | ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport), | |
246 | (((tp->t_inpcb->inp_laddr.s_addr & 0xffff) << 16) | | |
247 | (tp->t_inpcb->inp_faddr.s_addr & 0xffff)), | |
248 | sendalot,0,0); | |
249 | /* | |
250 | * If the route generation id changed, we need to check that our | |
251 | * local (source) IP address is still valid. If it isn't either | |
252 | * return error or silently do nothing (assuming the address will | |
253 | * come back before the TCP connection times out). | |
254 | */ | |
255 | ||
ab86ba33 A |
256 | if ((tp->t_inpcb->inp_route.ro_rt != NULL && |
257 | (tp->t_inpcb->inp_route.ro_rt->generation_id != route_generation)) || (tp->t_inpcb->inp_route.ro_rt == NULL)) { | |
55e303ae | 258 | /* check that the source address is still valid */ |
91447636 | 259 | if (ifa_foraddr(tp->t_inpcb->inp_laddr.s_addr) == 0) { |
55e303ae A |
260 | if (tp->t_state >= TCPS_CLOSE_WAIT) { |
261 | tcp_close(tp); | |
262 | return(EADDRNOTAVAIL); | |
263 | } | |
264 | ||
265 | /* set Retransmit timer if it wasn't set | |
266 | * reset Persist timer and shift register as the | |
267 | * adversed peer window may not be valid anymore | |
268 | */ | |
269 | ||
270 | if (!tp->t_timer[TCPT_REXMT]) { | |
271 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
272 | if (tp->t_timer[TCPT_PERSIST]) { | |
273 | tp->t_timer[TCPT_PERSIST] = 0; | |
274 | tp->t_rxtshift = 0; | |
275 | } | |
276 | } | |
277 | ||
91447636 A |
278 | if (packetlist) { |
279 | error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, | |
280 | (so->so_options & SO_DONTROUTE), 0); | |
281 | tp->t_lastchain = 0; | |
282 | } | |
55e303ae A |
283 | if (so->so_flags & SOF_NOADDRAVAIL) |
284 | return(EADDRNOTAVAIL); | |
285 | else | |
286 | return(0); /* silently ignore and keep data in socket */ | |
287 | } | |
55e303ae A |
288 | } |
289 | } | |
1c79356b A |
290 | sendalot = 0; |
291 | off = tp->snd_nxt - tp->snd_una; | |
292 | win = min(tp->snd_wnd, tp->snd_cwnd); | |
d12e1678 A |
293 | if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) |
294 | win = min(win, slowlink_wsize); | |
1c79356b A |
295 | |
296 | flags = tcp_outflags[tp->t_state]; | |
297 | /* | |
298 | * Get standard flags, and add SYN or FIN if requested by 'hidden' | |
299 | * state flags. | |
300 | */ | |
301 | if (tp->t_flags & TF_NEEDFIN) | |
302 | flags |= TH_FIN; | |
303 | if (tp->t_flags & TF_NEEDSYN) | |
304 | flags |= TH_SYN; | |
305 | ||
306 | /* | |
307 | * If in persist timeout with window of 0, send 1 byte. | |
308 | * Otherwise, if window is small but nonzero | |
309 | * and timer expired, we will send what we can | |
310 | * and go to transmit state. | |
311 | */ | |
312 | if (tp->t_force) { | |
313 | if (win == 0) { | |
314 | /* | |
315 | * If we still have some data to send, then | |
316 | * clear the FIN bit. Usually this would | |
317 | * happen below when it realizes that we | |
318 | * aren't sending all the data. However, | |
319 | * if we have exactly 1 byte of unsent data, | |
320 | * then it won't clear the FIN bit below, | |
321 | * and if we are in persist state, we wind | |
322 | * up sending the packet without recording | |
323 | * that we sent the FIN bit. | |
324 | * | |
325 | * We can't just blindly clear the FIN bit, | |
326 | * because if we don't have any more data | |
327 | * to send then the probe will be the FIN | |
328 | * itself. | |
329 | */ | |
330 | if (off < so->so_snd.sb_cc) | |
331 | flags &= ~TH_FIN; | |
332 | win = 1; | |
333 | } else { | |
334 | tp->t_timer[TCPT_PERSIST] = 0; | |
335 | tp->t_rxtshift = 0; | |
336 | } | |
337 | } | |
338 | ||
339 | len = (long)ulmin(so->so_snd.sb_cc, win) - off; | |
340 | ||
341 | if ((taop = tcp_gettaocache(tp->t_inpcb)) == NULL) { | |
342 | taop = &tao_noncached; | |
343 | bzero(taop, sizeof(*taop)); | |
344 | } | |
345 | ||
346 | /* | |
347 | * Lop off SYN bit if it has already been sent. However, if this | |
348 | * is SYN-SENT state and if segment contains data and if we don't | |
349 | * know that foreign host supports TAO, suppress sending segment. | |
350 | */ | |
351 | if ((flags & TH_SYN) && SEQ_GT(tp->snd_nxt, tp->snd_una)) { | |
352 | flags &= ~TH_SYN; | |
353 | off--, len++; | |
354 | if (len > 0 && tp->t_state == TCPS_SYN_SENT && | |
355 | taop->tao_ccsent == 0) { | |
91447636 A |
356 | if (packetlist) { |
357 | error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, | |
358 | (so->so_options & SO_DONTROUTE), 0); | |
359 | tp->t_lastchain = 0; | |
360 | } | |
1c79356b A |
361 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
362 | return 0; | |
363 | } | |
364 | } | |
365 | ||
366 | /* | |
367 | * Be careful not to send data and/or FIN on SYN segments | |
368 | * in cases when no CC option will be sent. | |
369 | * This measure is needed to prevent interoperability problems | |
370 | * with not fully conformant TCP implementations. | |
371 | */ | |
372 | if ((flags & TH_SYN) && | |
373 | ((tp->t_flags & TF_NOOPT) || !(tp->t_flags & TF_REQ_CC) || | |
374 | ((flags & TH_ACK) && !(tp->t_flags & TF_RCVD_CC)))) { | |
375 | len = 0; | |
376 | flags &= ~TH_FIN; | |
377 | } | |
378 | ||
379 | if (len < 0) { | |
380 | /* | |
381 | * If FIN has been sent but not acked, | |
382 | * but we haven't been called to retransmit, | |
383 | * len will be -1. Otherwise, window shrank | |
384 | * after we sent into it. If window shrank to 0, | |
385 | * cancel pending retransmit, pull snd_nxt back | |
386 | * to (closed) window, and set the persist timer | |
387 | * if it isn't already going. If the window didn't | |
388 | * close completely, just wait for an ACK. | |
389 | */ | |
390 | len = 0; | |
391 | if (win == 0) { | |
392 | tp->t_timer[TCPT_REXMT] = 0; | |
393 | tp->t_rxtshift = 0; | |
394 | tp->snd_nxt = tp->snd_una; | |
395 | if (tp->t_timer[TCPT_PERSIST] == 0) | |
396 | tcp_setpersist(tp); | |
397 | } | |
398 | } | |
399 | if (len > tp->t_maxseg) { | |
400 | len = tp->t_maxseg; | |
91447636 | 401 | howmuchsent += len; |
1c79356b A |
402 | sendalot = 1; |
403 | } | |
404 | if (SEQ_LT(tp->snd_nxt + len, tp->snd_una + so->so_snd.sb_cc)) | |
405 | flags &= ~TH_FIN; | |
406 | ||
d12e1678 A |
407 | if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0 ) /* Clips window size for slow links */ |
408 | win = min(sbspace(&so->so_rcv), slowlink_wsize); | |
409 | else | |
410 | win = sbspace(&so->so_rcv); | |
1c79356b A |
411 | |
412 | /* | |
413 | * Sender silly window avoidance. If connection is idle | |
414 | * and can send all data, a maximum segment, | |
415 | * at least a maximum default-size segment do it, | |
416 | * or are forced, do it; otherwise don't bother. | |
417 | * If peer's buffer is tiny, then send | |
418 | * when window is at least half open. | |
419 | * If retransmitting (possibly after persist timer forced us | |
420 | * to send into a small window), then must resend. | |
421 | */ | |
422 | if (len) { | |
423 | if (len == tp->t_maxseg) | |
424 | goto send; | |
425 | if (!(tp->t_flags & TF_MORETOCOME) && | |
426 | (idle || tp->t_flags & TF_NODELAY) && | |
427 | (tp->t_flags & TF_NOPUSH) == 0 && | |
428 | len + off >= so->so_snd.sb_cc) | |
429 | goto send; | |
430 | if (tp->t_force) | |
431 | goto send; | |
432 | if (len >= tp->max_sndwnd / 2 && tp->max_sndwnd > 0) | |
433 | goto send; | |
434 | if (SEQ_LT(tp->snd_nxt, tp->snd_max)) | |
435 | goto send; | |
436 | } | |
437 | ||
438 | /* | |
439 | * Compare available window to amount of window | |
440 | * known to peer (as advertised window less | |
441 | * next expected input). If the difference is at least two | |
442 | * max size segments, or at least 50% of the maximum possible | |
443 | * window, then want to send a window update to peer. | |
444 | */ | |
445 | if (win > 0) { | |
446 | /* | |
447 | * "adv" is the amount we can increase the window, | |
448 | * taking into account that we are limited by | |
449 | * TCP_MAXWIN << tp->rcv_scale. | |
450 | */ | |
451 | long adv = min(win, (long)TCP_MAXWIN << tp->rcv_scale) - | |
452 | (tp->rcv_adv - tp->rcv_nxt); | |
453 | ||
454 | if (adv >= (long) (2 * tp->t_maxseg)) | |
455 | goto send; | |
456 | if (2 * adv >= (long) so->so_rcv.sb_hiwat) | |
457 | goto send; | |
458 | } | |
459 | ||
460 | /* | |
461 | * Send if we owe peer an ACK. | |
462 | */ | |
463 | if (tp->t_flags & TF_ACKNOW) | |
464 | goto send; | |
465 | if ((flags & TH_RST) || | |
466 | ((flags & TH_SYN) && (tp->t_flags & TF_NEEDSYN) == 0)) | |
467 | goto send; | |
468 | if (SEQ_GT(tp->snd_up, tp->snd_una)) | |
469 | goto send; | |
470 | /* | |
471 | * If our state indicates that FIN should be sent | |
472 | * and we have not yet done so, or we're retransmitting the FIN, | |
473 | * then we need to send. | |
474 | */ | |
475 | if (flags & TH_FIN && | |
476 | ((tp->t_flags & TF_SENTFIN) == 0 || tp->snd_nxt == tp->snd_una)) | |
477 | goto send; | |
478 | ||
479 | /* | |
480 | * TCP window updates are not reliable, rather a polling protocol | |
481 | * using ``persist'' packets is used to insure receipt of window | |
482 | * updates. The three ``states'' for the output side are: | |
483 | * idle not doing retransmits or persists | |
484 | * persisting to move a small or zero window | |
485 | * (re)transmitting and thereby not persisting | |
486 | * | |
487 | * tp->t_timer[TCPT_PERSIST] | |
488 | * is set when we are in persist state. | |
489 | * tp->t_force | |
490 | * is set when we are called to send a persist packet. | |
491 | * tp->t_timer[TCPT_REXMT] | |
492 | * is set when we are retransmitting | |
493 | * The output side is idle when both timers are zero. | |
494 | * | |
495 | * If send window is too small, there is data to transmit, and no | |
496 | * retransmit or persist is pending, then go to persist state. | |
497 | * If nothing happens soon, send when timer expires: | |
498 | * if window is nonzero, transmit what we can, | |
499 | * otherwise force out a byte. | |
500 | */ | |
501 | if (so->so_snd.sb_cc && tp->t_timer[TCPT_REXMT] == 0 && | |
502 | tp->t_timer[TCPT_PERSIST] == 0) { | |
503 | tp->t_rxtshift = 0; | |
504 | tcp_setpersist(tp); | |
505 | } | |
506 | ||
507 | /* | |
91447636 A |
508 | * If there is no reason to send a segment, just return. |
509 | * but if there is some packets left in the packet list, send them now. | |
1c79356b | 510 | */ |
91447636 A |
511 | if (packetlist) { |
512 | error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, | |
513 | (so->so_options & SO_DONTROUTE), 0); | |
514 | } | |
1c79356b A |
515 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
516 | return (0); | |
517 | ||
518 | send: | |
519 | /* | |
520 | * Before ESTABLISHED, force sending of initial options | |
521 | * unless TCP set not to do any options. | |
522 | * NOTE: we assume that the IP/TCP header plus TCP options | |
523 | * always fit in a single mbuf, leaving room for a maximum | |
524 | * link header, i.e. | |
9bccf70c | 525 | * max_linkhdr + sizeof (struct tcpiphdr) + optlen <= MCLBYTES |
1c79356b A |
526 | */ |
527 | optlen = 0; | |
528 | #if INET6 | |
529 | if (isipv6) | |
9bccf70c | 530 | hdrlen = sizeof (struct ip6_hdr) + sizeof (struct tcphdr); |
1c79356b A |
531 | else |
532 | #endif | |
533 | hdrlen = sizeof (struct tcpiphdr); | |
534 | if (flags & TH_SYN) { | |
535 | tp->snd_nxt = tp->iss; | |
536 | if ((tp->t_flags & TF_NOOPT) == 0) { | |
537 | u_short mss; | |
538 | ||
539 | opt[0] = TCPOPT_MAXSEG; | |
540 | opt[1] = TCPOLEN_MAXSEG; | |
9bccf70c | 541 | mss = htons((u_short) tcp_mssopt(tp)); |
1c79356b A |
542 | (void)memcpy(opt + 2, &mss, sizeof(mss)); |
543 | optlen = TCPOLEN_MAXSEG; | |
544 | ||
545 | if ((tp->t_flags & TF_REQ_SCALE) && | |
546 | ((flags & TH_ACK) == 0 || | |
547 | (tp->t_flags & TF_RCVD_SCALE))) { | |
548 | *((u_int32_t *)(opt + optlen)) = htonl( | |
549 | TCPOPT_NOP << 24 | | |
550 | TCPOPT_WINDOW << 16 | | |
551 | TCPOLEN_WINDOW << 8 | | |
552 | tp->request_r_scale); | |
553 | optlen += 4; | |
554 | } | |
555 | } | |
556 | } | |
557 | ||
558 | /* | |
559 | * Send a timestamp and echo-reply if this is a SYN and our side | |
560 | * wants to use timestamps (TF_REQ_TSTMP is set) or both our side | |
561 | * and our peer have sent timestamps in our SYN's. | |
562 | */ | |
563 | if ((tp->t_flags & (TF_REQ_TSTMP|TF_NOOPT)) == TF_REQ_TSTMP && | |
564 | (flags & TH_RST) == 0 && | |
565 | ((flags & TH_ACK) == 0 || | |
566 | (tp->t_flags & TF_RCVD_TSTMP))) { | |
567 | u_int32_t *lp = (u_int32_t *)(opt + optlen); | |
568 | ||
569 | /* Form timestamp option as shown in appendix A of RFC 1323. */ | |
570 | *lp++ = htonl(TCPOPT_TSTAMP_HDR); | |
571 | *lp++ = htonl(tcp_now); | |
572 | *lp = htonl(tp->ts_recent); | |
573 | optlen += TCPOLEN_TSTAMP_APPA; | |
574 | } | |
575 | ||
576 | /* | |
577 | * Send `CC-family' options if our side wants to use them (TF_REQ_CC), | |
578 | * options are allowed (!TF_NOOPT) and it's not a RST. | |
579 | */ | |
580 | if ((tp->t_flags & (TF_REQ_CC|TF_NOOPT)) == TF_REQ_CC && | |
581 | (flags & TH_RST) == 0) { | |
582 | switch (flags & (TH_SYN|TH_ACK)) { | |
583 | /* | |
584 | * This is a normal ACK, send CC if we received CC before | |
585 | * from our peer. | |
586 | */ | |
587 | case TH_ACK: | |
588 | if (!(tp->t_flags & TF_RCVD_CC)) | |
589 | break; | |
590 | /*FALLTHROUGH*/ | |
591 | ||
592 | /* | |
593 | * We can only get here in T/TCP's SYN_SENT* state, when | |
594 | * we're a sending a non-SYN segment without waiting for | |
595 | * the ACK of our SYN. A check above assures that we only | |
596 | * do this if our peer understands T/TCP. | |
597 | */ | |
598 | case 0: | |
599 | opt[optlen++] = TCPOPT_NOP; | |
600 | opt[optlen++] = TCPOPT_NOP; | |
601 | opt[optlen++] = TCPOPT_CC; | |
602 | opt[optlen++] = TCPOLEN_CC; | |
603 | *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); | |
604 | ||
605 | optlen += 4; | |
606 | break; | |
607 | ||
608 | /* | |
609 | * This is our initial SYN, check whether we have to use | |
610 | * CC or CC.new. | |
611 | */ | |
612 | case TH_SYN: | |
613 | opt[optlen++] = TCPOPT_NOP; | |
614 | opt[optlen++] = TCPOPT_NOP; | |
615 | opt[optlen++] = tp->t_flags & TF_SENDCCNEW ? | |
616 | TCPOPT_CCNEW : TCPOPT_CC; | |
617 | opt[optlen++] = TCPOLEN_CC; | |
618 | *(u_int32_t *)&opt[optlen] = htonl(tp->cc_send); | |
619 | optlen += 4; | |
620 | break; | |
621 | ||
622 | /* | |
623 | * This is a SYN,ACK; send CC and CC.echo if we received | |
624 | * CC from our peer. | |
625 | */ | |
626 | case (TH_SYN|TH_ACK): | |
627 | if (tp->t_flags & TF_RCVD_CC) { | |
628 | opt[optlen++] = TCPOPT_NOP; | |
629 | opt[optlen++] = TCPOPT_NOP; | |
630 | opt[optlen++] = TCPOPT_CC; | |
631 | opt[optlen++] = TCPOLEN_CC; | |
632 | *(u_int32_t *)&opt[optlen] = | |
633 | htonl(tp->cc_send); | |
634 | optlen += 4; | |
635 | opt[optlen++] = TCPOPT_NOP; | |
636 | opt[optlen++] = TCPOPT_NOP; | |
637 | opt[optlen++] = TCPOPT_CCECHO; | |
638 | opt[optlen++] = TCPOLEN_CC; | |
639 | *(u_int32_t *)&opt[optlen] = | |
640 | htonl(tp->cc_recv); | |
641 | optlen += 4; | |
642 | } | |
643 | break; | |
644 | } | |
645 | } | |
646 | ||
647 | hdrlen += optlen; | |
648 | #if INET6 | |
649 | if (isipv6) | |
650 | ipoptlen = ip6_optlen(tp->t_inpcb); | |
651 | else | |
652 | #endif | |
9bccf70c A |
653 | { |
654 | if (tp->t_inpcb->inp_options) { | |
655 | ipoptlen = tp->t_inpcb->inp_options->m_len - | |
1c79356b | 656 | offsetof(struct ipoption, ipopt_list); |
9bccf70c A |
657 | } else { |
658 | ipoptlen = 0; | |
659 | } | |
1c79356b A |
660 | } |
661 | #if IPSEC | |
9bccf70c A |
662 | if (ipsec_bypass == 0) |
663 | ipoptlen += ipsec_hdrsiz_tcp(tp); | |
1c79356b A |
664 | #endif |
665 | ||
666 | /* | |
667 | * Adjust data length if insertion of options will | |
668 | * bump the packet length beyond the t_maxopd length. | |
669 | * Clear the FIN bit because we cut off the tail of | |
670 | * the segment. | |
671 | */ | |
672 | if (len + optlen + ipoptlen > tp->t_maxopd) { | |
673 | /* | |
674 | * If there is still more to send, don't close the connection. | |
675 | */ | |
676 | flags &= ~TH_FIN; | |
677 | len = tp->t_maxopd - optlen - ipoptlen; | |
91447636 | 678 | howmuchsent += len; |
1c79356b A |
679 | sendalot = 1; |
680 | } | |
681 | ||
682 | /*#ifdef DIAGNOSTIC*/ | |
9bccf70c A |
683 | #if INET6 |
684 | if (max_linkhdr + hdrlen > MCLBYTES) | |
685 | panic("tcphdr too big"); | |
686 | #else | |
1c79356b A |
687 | if (max_linkhdr + hdrlen > MHLEN) |
688 | panic("tcphdr too big"); | |
9bccf70c | 689 | #endif |
1c79356b A |
690 | /*#endif*/ |
691 | ||
692 | /* | |
693 | * Grab a header mbuf, attaching a copy of data to | |
694 | * be transmitted, and initialize the header from | |
695 | * the template for sends on this connection. | |
696 | */ | |
697 | if (len) { | |
698 | if (tp->t_force && len == 1) | |
699 | tcpstat.tcps_sndprobe++; | |
700 | else if (SEQ_LT(tp->snd_nxt, tp->snd_max)) { | |
701 | tcpstat.tcps_sndrexmitpack++; | |
702 | tcpstat.tcps_sndrexmitbyte += len; | |
703 | } else { | |
704 | tcpstat.tcps_sndpack++; | |
705 | tcpstat.tcps_sndbyte += len; | |
706 | } | |
707 | #ifdef notyet | |
708 | if ((m = m_copypack(so->so_snd.sb_mb, off, | |
709 | (int)len, max_linkhdr + hdrlen)) == 0) { | |
710 | error = ENOBUFS; | |
711 | goto out; | |
712 | } | |
713 | /* | |
714 | * m_copypack left space for our hdr; use it. | |
715 | */ | |
716 | m->m_len += hdrlen; | |
717 | m->m_data -= hdrlen; | |
718 | #else | |
9bccf70c A |
719 | /* |
720 | * try to use the new interface that allocates all | |
721 | * the necessary mbuf hdrs under 1 mbuf lock and | |
722 | * avoids rescanning the socket mbuf list if | |
723 | * certain conditions are met. This routine can't | |
724 | * be used in the following cases... | |
725 | * 1) the protocol headers exceed the capacity of | |
726 | * of a single mbuf header's data area (no cluster attached) | |
727 | * 2) the length of the data being transmitted plus | |
728 | * the protocol headers fits into a single mbuf header's | |
729 | * data area (no cluster attached) | |
730 | */ | |
fa4905b1 | 731 | m = NULL; |
1c79356b A |
732 | #if INET6 |
733 | if (MHLEN < hdrlen + max_linkhdr) { | |
fa4905b1 A |
734 | MGETHDR(m, M_DONTWAIT, MT_HEADER); |
735 | if (m == NULL) { | |
736 | error = ENOBUFS; | |
737 | goto out; | |
738 | } | |
1c79356b A |
739 | MCLGET(m, M_DONTWAIT); |
740 | if ((m->m_flags & M_EXT) == 0) { | |
741 | m_freem(m); | |
742 | error = ENOBUFS; | |
743 | goto out; | |
744 | } | |
fa4905b1 A |
745 | m->m_data += max_linkhdr; |
746 | m->m_len = hdrlen; | |
1c79356b A |
747 | } |
748 | #endif | |
1c79356b | 749 | if (len <= MHLEN - hdrlen - max_linkhdr) { |
fa4905b1 A |
750 | if (m == NULL) { |
751 | MGETHDR(m, M_DONTWAIT, MT_HEADER); | |
752 | if (m == NULL) { | |
753 | error = ENOBUFS; | |
754 | goto out; | |
755 | } | |
756 | m->m_data += max_linkhdr; | |
757 | m->m_len = hdrlen; | |
758 | } | |
55e303ae A |
759 | /* makes sure we still have data left to be sent at this point */ |
760 | if (so->so_snd.sb_mb == NULL || off == -1) { | |
761 | if (m != NULL) m_freem(m); | |
762 | error = 0; /* should we return an error? */ | |
763 | goto out; | |
764 | } | |
1c79356b A |
765 | m_copydata(so->so_snd.sb_mb, off, (int) len, |
766 | mtod(m, caddr_t) + hdrlen); | |
767 | m->m_len += len; | |
768 | } else { | |
fa4905b1 A |
769 | if (m != NULL) { |
770 | m->m_next = m_copy(so->so_snd.sb_mb, off, (int) len); | |
771 | if (m->m_next == 0) { | |
772 | (void) m_free(m); | |
773 | error = ENOBUFS; | |
774 | goto out; | |
775 | } | |
776 | } else { | |
9bccf70c A |
777 | /* |
778 | * determine whether the mbuf pointer and offset passed back by the 'last' call | |
779 | * to m_copym_with_hdrs are still valid... if the head of the socket chain has | |
780 | * changed (due to an incoming ACK for instance), or the offset into the chain we | |
781 | * just computed is different from the one last returned by m_copym_with_hdrs (perhaps | |
782 | * we're re-transmitting a packet sent earlier), than we can't pass the mbuf pointer and | |
783 | * offset into it as valid hints for m_copym_with_hdrs to use (if valid, these hints allow | |
784 | * m_copym_with_hdrs to avoid rescanning from the beginning of the socket buffer mbuf list. | |
785 | * setting the mbuf pointer to NULL is sufficient to disable the hint mechanism. | |
786 | */ | |
fa4905b1 A |
787 | if (m_head != so->so_snd.sb_mb || last_off != off) |
788 | m_last = NULL; | |
789 | last_off = off + len; | |
790 | m_head = so->so_snd.sb_mb; | |
55e303ae A |
791 | |
792 | /* makes sure we still have data left to be sent at this point */ | |
793 | if (m_head == NULL) { | |
794 | error = 0; /* should we return an error? */ | |
795 | goto out; | |
796 | } | |
797 | ||
9bccf70c A |
798 | /* |
799 | * m_copym_with_hdrs will always return the last mbuf pointer and the offset into it that | |
800 | * it acted on to fullfill the current request, whether a valid 'hint' was passed in or not | |
801 | */ | |
fa4905b1 A |
802 | if ((m = m_copym_with_hdrs(so->so_snd.sb_mb, off, (int) len, M_DONTWAIT, &m_last, &m_off)) == NULL) { |
803 | error = ENOBUFS; | |
804 | goto out; | |
805 | } | |
806 | m->m_data += max_linkhdr; | |
807 | m->m_len = hdrlen; | |
1c79356b A |
808 | } |
809 | } | |
810 | #endif | |
811 | /* | |
812 | * If we're sending everything we've got, set PUSH. | |
813 | * (This will keep happy those implementations which only | |
814 | * give data to the user when a buffer fills or | |
815 | * a PUSH comes in.) | |
816 | */ | |
817 | if (off + len == so->so_snd.sb_cc) | |
818 | flags |= TH_PUSH; | |
819 | } else { | |
820 | if (tp->t_flags & TF_ACKNOW) | |
821 | tcpstat.tcps_sndacks++; | |
822 | else if (flags & (TH_SYN|TH_FIN|TH_RST)) | |
823 | tcpstat.tcps_sndctrl++; | |
824 | else if (SEQ_GT(tp->snd_up, tp->snd_una)) | |
825 | tcpstat.tcps_sndurg++; | |
826 | else | |
827 | tcpstat.tcps_sndwinup++; | |
828 | ||
829 | MGETHDR(m, M_DONTWAIT, MT_HEADER); | |
830 | if (m == NULL) { | |
831 | error = ENOBUFS; | |
832 | goto out; | |
833 | } | |
834 | #if INET6 | |
9bccf70c A |
835 | if (isipv6 && (MHLEN < hdrlen + max_linkhdr) && |
836 | MHLEN >= hdrlen) { | |
1c79356b A |
837 | MH_ALIGN(m, hdrlen); |
838 | } else | |
839 | #endif | |
840 | m->m_data += max_linkhdr; | |
841 | m->m_len = hdrlen; | |
842 | } | |
91447636 | 843 | m->m_pkthdr.rcvif = 0; |
1c79356b A |
844 | #if INET6 |
845 | if (isipv6) { | |
846 | ip6 = mtod(m, struct ip6_hdr *); | |
847 | th = (struct tcphdr *)(ip6 + 1); | |
9bccf70c A |
848 | tcp_fillheaders(tp, ip6, th); |
849 | } else | |
1c79356b | 850 | #endif /* INET6 */ |
9bccf70c A |
851 | { |
852 | ip = mtod(m, struct ip *); | |
853 | ipov = (struct ipovly *)ip; | |
854 | th = (struct tcphdr *)(ip + 1); | |
855 | /* this picks up the pseudo header (w/o the length) */ | |
856 | tcp_fillheaders(tp, ip, th); | |
1c79356b | 857 | } |
1c79356b A |
858 | |
859 | /* | |
860 | * Fill in fields, remembering maximum advertised | |
861 | * window for use in delaying messages about window sizes. | |
862 | * If resending a FIN, be sure not to use a new sequence number. | |
863 | */ | |
864 | if (flags & TH_FIN && tp->t_flags & TF_SENTFIN && | |
865 | tp->snd_nxt == tp->snd_max) | |
866 | tp->snd_nxt--; | |
867 | /* | |
868 | * If we are doing retransmissions, then snd_nxt will | |
869 | * not reflect the first unsent octet. For ACK only | |
870 | * packets, we do not want the sequence number of the | |
871 | * retransmitted packet, we want the sequence number | |
872 | * of the next unsent octet. So, if there is no data | |
873 | * (and no SYN or FIN), use snd_max instead of snd_nxt | |
874 | * when filling in ti_seq. But if we are in persist | |
875 | * state, snd_max might reflect one byte beyond the | |
876 | * right edge of the window, so use snd_nxt in that | |
877 | * case, since we know we aren't doing a retransmission. | |
878 | * (retransmit and persist are mutually exclusive...) | |
879 | */ | |
880 | if (len || (flags & (TH_SYN|TH_FIN)) || tp->t_timer[TCPT_PERSIST]) | |
881 | th->th_seq = htonl(tp->snd_nxt); | |
882 | else | |
883 | th->th_seq = htonl(tp->snd_max); | |
884 | th->th_ack = htonl(tp->rcv_nxt); | |
885 | if (optlen) { | |
886 | bcopy(opt, th + 1, optlen); | |
887 | th->th_off = (sizeof (struct tcphdr) + optlen) >> 2; | |
888 | } | |
889 | th->th_flags = flags; | |
890 | /* | |
891 | * Calculate receive window. Don't shrink window, | |
892 | * but avoid silly window syndrome. | |
893 | */ | |
894 | if (win < (long)(so->so_rcv.sb_hiwat / 4) && win < (long)tp->t_maxseg) | |
895 | win = 0; | |
896 | if (win < (long)(tp->rcv_adv - tp->rcv_nxt)) | |
897 | win = (long)(tp->rcv_adv - tp->rcv_nxt); | |
d12e1678 A |
898 | if (tp->t_flags & TF_SLOWLINK && slowlink_wsize > 0) { |
899 | if (win > (long)slowlink_wsize) | |
900 | win = slowlink_wsize; | |
901 | th->th_win = htons((u_short) (win>>tp->rcv_scale)); | |
902 | } | |
903 | else { | |
904 | ||
905 | if (win > (long)TCP_MAXWIN << tp->rcv_scale) | |
1c79356b | 906 | win = (long)TCP_MAXWIN << tp->rcv_scale; |
d12e1678 A |
907 | th->th_win = htons((u_short) (win>>tp->rcv_scale)); |
908 | } | |
91447636 A |
909 | |
910 | /* | |
911 | * Adjust the RXWIN0SENT flag - indicate that we have advertised | |
912 | * a 0 window. This may cause the remote transmitter to stall. This | |
913 | * flag tells soreceive() to disable delayed acknowledgements when | |
914 | * draining the buffer. This can occur if the receiver is attempting | |
915 | * to read more data then can be buffered prior to transmitting on | |
916 | * the connection. | |
917 | */ | |
918 | if (win == 0) | |
919 | tp->t_flags |= TF_RXWIN0SENT; | |
920 | else | |
921 | tp->t_flags &= ~TF_RXWIN0SENT; | |
922 | ||
1c79356b A |
923 | if (SEQ_GT(tp->snd_up, tp->snd_nxt)) { |
924 | th->th_urp = htons((u_short)(tp->snd_up - tp->snd_nxt)); | |
925 | th->th_flags |= TH_URG; | |
926 | } else | |
927 | /* | |
928 | * If no urgent pointer to send, then we pull | |
929 | * the urgent pointer to the left edge of the send window | |
930 | * so that it doesn't drift into the send window on sequence | |
931 | * number wraparound. | |
932 | */ | |
933 | tp->snd_up = tp->snd_una; /* drag it along */ | |
934 | ||
935 | /* | |
936 | * Put TCP length in extended header, and then | |
937 | * checksum extended header and data. | |
938 | */ | |
9bccf70c | 939 | m->m_pkthdr.len = hdrlen + len; /* in6_cksum() need this */ |
1c79356b | 940 | #if INET6 |
9bccf70c A |
941 | if (isipv6) |
942 | /* | |
943 | * ip6_plen is not need to be filled now, and will be filled | |
944 | * in ip6_output. | |
945 | */ | |
1c79356b A |
946 | th->th_sum = in6_cksum(m, IPPROTO_TCP, sizeof(struct ip6_hdr), |
947 | sizeof(struct tcphdr) + optlen + len); | |
9bccf70c | 948 | else |
1c79356b | 949 | #endif /* INET6 */ |
0b4e3aa0 | 950 | { |
9bccf70c A |
951 | m->m_pkthdr.csum_flags = CSUM_TCP; |
952 | m->m_pkthdr.csum_data = offsetof(struct tcphdr, th_sum); | |
953 | if (len + optlen) | |
954 | th->th_sum = in_addword(th->th_sum, | |
955 | htons((u_short)(optlen + len))); | |
0b4e3aa0 | 956 | |
9bccf70c A |
957 | /* IP version must be set here for ipv4/ipv6 checking later */ |
958 | KASSERT(ip->ip_v == IPVERSION, | |
959 | ("%s: IP version incorrect: %d", __FUNCTION__, ip->ip_v)); | |
960 | } | |
1c79356b A |
961 | |
962 | /* | |
963 | * In transmit state, time the transmission and arrange for | |
964 | * the retransmit. In persist state, just set snd_max. | |
965 | */ | |
966 | if (tp->t_force == 0 || tp->t_timer[TCPT_PERSIST] == 0) { | |
967 | tcp_seq startseq = tp->snd_nxt; | |
968 | ||
969 | /* | |
970 | * Advance snd_nxt over sequence space of this segment. | |
971 | */ | |
972 | if (flags & (TH_SYN|TH_FIN)) { | |
973 | if (flags & TH_SYN) | |
974 | tp->snd_nxt++; | |
975 | if (flags & TH_FIN) { | |
976 | tp->snd_nxt++; | |
977 | tp->t_flags |= TF_SENTFIN; | |
978 | } | |
979 | } | |
980 | tp->snd_nxt += len; | |
981 | if (SEQ_GT(tp->snd_nxt, tp->snd_max)) { | |
982 | tp->snd_max = tp->snd_nxt; | |
983 | /* | |
984 | * Time this transmission if not a retransmission and | |
985 | * not currently timing anything. | |
986 | */ | |
9bccf70c A |
987 | if (tp->t_rtttime == 0) { |
988 | tp->t_rtttime = 1; | |
1c79356b A |
989 | tp->t_rtseq = startseq; |
990 | tcpstat.tcps_segstimed++; | |
991 | } | |
992 | } | |
993 | ||
994 | /* | |
995 | * Set retransmit timer if not currently set, | |
996 | * and not doing an ack or a keep-alive probe. | |
997 | * Initial value for retransmit timer is smoothed | |
998 | * round-trip time + 2 * round-trip time variance. | |
999 | * Initialize shift counter which is used for backoff | |
1000 | * of retransmit time. | |
1001 | */ | |
1002 | if (tp->t_timer[TCPT_REXMT] == 0 && | |
1003 | tp->snd_nxt != tp->snd_una) { | |
1004 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1005 | if (tp->t_timer[TCPT_PERSIST]) { | |
1006 | tp->t_timer[TCPT_PERSIST] = 0; | |
1007 | tp->t_rxtshift = 0; | |
1008 | } | |
1009 | } | |
1010 | } else | |
1011 | if (SEQ_GT(tp->snd_nxt + len, tp->snd_max)) | |
1012 | tp->snd_max = tp->snd_nxt + len; | |
1013 | ||
1014 | #if TCPDEBUG | |
1015 | /* | |
1016 | * Trace. | |
1017 | */ | |
9bccf70c A |
1018 | if (so->so_options & SO_DEBUG) |
1019 | tcp_trace(TA_OUTPUT, tp->t_state, tp, mtod(m, void *), th, 0); | |
1020 | #endif | |
1c79356b A |
1021 | |
1022 | /* | |
1023 | * Fill in IP length and desired time to live and | |
1024 | * send to IP level. There should be a better way | |
1025 | * to handle ttl and tos; we could keep them in | |
1026 | * the template, but need a way to checksum without them. | |
1027 | */ | |
9bccf70c A |
1028 | /* |
1029 | * m->m_pkthdr.len should have been set before cksum calcuration, | |
1030 | * because in6_cksum() need it. | |
1031 | */ | |
1c79356b A |
1032 | #if INET6 |
1033 | if (isipv6) { | |
9bccf70c | 1034 | /* |
1c79356b A |
1035 | * we separately set hoplimit for every segment, since the |
1036 | * user might want to change the value via setsockopt. | |
1037 | * Also, desired default hop limit might be changed via | |
1038 | * Neighbor Discovery. | |
1039 | */ | |
1040 | ip6->ip6_hlim = in6_selecthlim(tp->t_inpcb, | |
1041 | tp->t_inpcb->in6p_route.ro_rt ? | |
1042 | tp->t_inpcb->in6p_route.ro_rt->rt_ifp | |
1043 | : NULL); | |
1044 | ||
1045 | /* TODO: IPv6 IP6TOS_ECT bit on */ | |
1046 | #if IPSEC | |
9bccf70c A |
1047 | if (ipsec_bypass == 0 && ipsec_setsocket(m, so) != 0) { |
1048 | m_freem(m); | |
1049 | error = ENOBUFS; | |
1050 | goto out; | |
1051 | } | |
1c79356b | 1052 | #endif /*IPSEC*/ |
91447636 | 1053 | m->m_pkthdr.socket_id = socket_id; |
1c79356b A |
1054 | error = ip6_output(m, |
1055 | tp->t_inpcb->in6p_outputopts, | |
1056 | &tp->t_inpcb->in6p_route, | |
91447636 | 1057 | (so->so_options & SO_DONTROUTE), NULL, NULL, 0); |
1c79356b A |
1058 | } else |
1059 | #endif /* INET6 */ | |
9bccf70c | 1060 | { |
1c79356b | 1061 | struct rtentry *rt; |
1c79356b A |
1062 | ip->ip_len = m->m_pkthdr.len; |
1063 | #if INET6 | |
55e303ae | 1064 | if (isipv6) |
9bccf70c A |
1065 | ip->ip_ttl = in6_selecthlim(tp->t_inpcb, |
1066 | tp->t_inpcb->in6p_route.ro_rt ? | |
1067 | tp->t_inpcb->in6p_route.ro_rt->rt_ifp | |
1068 | : NULL); | |
1069 | else | |
1c79356b A |
1070 | #endif /* INET6 */ |
1071 | ip->ip_ttl = tp->t_inpcb->inp_ip_ttl; /* XXX */ | |
1072 | ip->ip_tos = tp->t_inpcb->inp_ip_tos; /* XXX */ | |
1073 | ||
0b4e3aa0 | 1074 | |
9bccf70c A |
1075 | #if INET6 |
1076 | if (isipv6) { | |
1077 | KERNEL_DEBUG(DBG_LAYER_BEG, | |
1078 | ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport), | |
1079 | (((tp->t_inpcb->in6p_laddr.s6_addr16[0] & 0xffff) << 16) | | |
1080 | (tp->t_inpcb->in6p_faddr.s6_addr16[0] & 0xffff)), | |
1081 | 0,0,0); | |
1082 | } | |
1083 | else | |
1084 | #endif | |
1085 | { | |
1086 | KERNEL_DEBUG(DBG_LAYER_BEG, | |
1087 | ((tp->t_inpcb->inp_fport << 16) | tp->t_inpcb->inp_lport), | |
1088 | (((tp->t_inpcb->inp_laddr.s_addr & 0xffff) << 16) | | |
1089 | (tp->t_inpcb->inp_faddr.s_addr & 0xffff)), | |
1090 | 0,0,0); | |
1091 | } | |
1092 | ||
1c79356b A |
1093 | /* |
1094 | * See if we should do MTU discovery. We do it only if the following | |
1095 | * are true: | |
1096 | * 1) we have a valid route to the destination | |
1097 | * 2) the MTU is not locked (if it is, then discovery has been | |
1098 | * disabled) | |
1099 | */ | |
9bccf70c A |
1100 | if (path_mtu_discovery |
1101 | && (rt = tp->t_inpcb->inp_route.ro_rt) | |
1c79356b A |
1102 | && rt->rt_flags & RTF_UP |
1103 | && !(rt->rt_rmx.rmx_locks & RTV_MTU)) { | |
1104 | ip->ip_off |= IP_DF; | |
1105 | } | |
1c79356b | 1106 | #if IPSEC |
9bccf70c A |
1107 | if (ipsec_bypass == 0) |
1108 | ipsec_setsocket(m, so); | |
1c79356b | 1109 | #endif /*IPSEC*/ |
91447636 A |
1110 | |
1111 | /* | |
1112 | * The socket is kept locked while sending out packets in ip_output, even if packet chaining is not active. | |
1113 | */ | |
1114 | ||
1115 | m->m_pkthdr.socket_id = socket_id; | |
1116 | if (packetlist) { | |
1117 | m->m_nextpkt = NULL; | |
1118 | lastpacket->m_nextpkt = m; | |
1119 | lastpacket = m; | |
1120 | packchain_listadd++; | |
1121 | } | |
1122 | else { | |
1123 | m->m_nextpkt = NULL; | |
1124 | packchain_newlist++; | |
1125 | packetlist = lastpacket = m; | |
1126 | packchain_listadd=0; | |
1127 | } | |
1128 | ||
1129 | if ((ipsec_bypass == 0) || fw_enable || sendalot == 0 || (tp->t_state != TCPS_ESTABLISHED) || | |
1130 | (tp->snd_cwnd <= (tp->snd_wnd / 4)) || | |
1131 | (tp->t_flags & (TH_PUSH | TF_ACKNOW)) || tp->t_force != 0 || | |
1132 | packchain_listadd >= tcp_packet_chaining) { | |
1133 | lastpacket->m_nextpkt = 0; | |
1134 | error = ip_output_list(packetlist, packchain_listadd, tp->t_inpcb->inp_options, &tp->t_inpcb->inp_route, | |
1135 | (so->so_options & SO_DONTROUTE), 0); | |
1136 | tp->t_lastchain = packchain_listadd; | |
1137 | packchain_sent++; | |
1138 | packetlist = NULL; | |
1139 | if (error == 0) | |
1140 | howmuchsent = 0; | |
1141 | } | |
1142 | else { | |
1143 | error = 0; | |
1144 | packchain_looped++; | |
1145 | tcpstat.tcps_sndtotal++; | |
1146 | if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) | |
1147 | tp->rcv_adv = tp->rcv_nxt + win; | |
1148 | tp->last_ack_sent = tp->rcv_nxt; | |
1149 | tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); | |
1150 | goto again; | |
1151 | } | |
1152 | } | |
1c79356b | 1153 | if (error) { |
9bccf70c A |
1154 | |
1155 | /* | |
1156 | * We know that the packet was lost, so back out the | |
1157 | * sequence number advance, if any. | |
1158 | */ | |
1159 | if (tp->t_force == 0 || !tp->t_timer[TCPT_PERSIST]) { | |
1160 | /* | |
1161 | * No need to check for TH_FIN here because | |
1162 | * the TF_SENTFIN flag handles that case. | |
1163 | */ | |
91447636 A |
1164 | if ((flags & TH_SYN) == 0) |
1165 | tp->snd_nxt -= howmuchsent; | |
9bccf70c | 1166 | } |
91447636 | 1167 | howmuchsent = 0; |
1c79356b A |
1168 | out: |
1169 | if (error == ENOBUFS) { | |
0b4e3aa0 A |
1170 | if (!tp->t_timer[TCPT_REXMT] && |
1171 | !tp->t_timer[TCPT_PERSIST]) | |
1172 | tp->t_timer[TCPT_REXMT] = tp->t_rxtcur; | |
1c79356b | 1173 | tcp_quench(tp->t_inpcb, 0); |
91447636 A |
1174 | if (packetlist) |
1175 | m_freem_list(packetlist); | |
1176 | tp->t_lastchain = 0; | |
1c79356b A |
1177 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
1178 | return (0); | |
1179 | } | |
1c79356b A |
1180 | if (error == EMSGSIZE) { |
1181 | /* | |
1182 | * ip_output() will have already fixed the route | |
1183 | * for us. tcp_mtudisc() will, as its last action, | |
1184 | * initiate retransmission, so it is important to | |
1185 | * not do so here. | |
1186 | */ | |
1187 | tcp_mtudisc(tp->t_inpcb, 0); | |
91447636 A |
1188 | if (packetlist) |
1189 | m_freem_list(packetlist); | |
1190 | tp->t_lastchain = 0; | |
1c79356b A |
1191 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
1192 | return 0; | |
1193 | } | |
1c79356b A |
1194 | if ((error == EHOSTUNREACH || error == ENETDOWN) |
1195 | && TCPS_HAVERCVDSYN(tp->t_state)) { | |
1196 | tp->t_softerror = error; | |
91447636 A |
1197 | if (packetlist) |
1198 | m_freem_list(packetlist); | |
1199 | tp->t_lastchain = 0; | |
1c79356b A |
1200 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
1201 | return (0); | |
1202 | } | |
91447636 A |
1203 | if (packetlist) |
1204 | m_freem_list(packetlist); | |
1205 | tp->t_lastchain = 0; | |
1c79356b A |
1206 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END, 0,0,0,0,0); |
1207 | return (error); | |
1208 | } | |
91447636 | 1209 | sentit: |
1c79356b A |
1210 | tcpstat.tcps_sndtotal++; |
1211 | ||
1212 | /* | |
1213 | * Data sent (as far as we can tell). | |
1214 | * If this advertises a larger window than any other segment, | |
1215 | * then remember the size of the advertised window. | |
1216 | * Any pending ACK has now been sent. | |
1217 | */ | |
1218 | if (win > 0 && SEQ_GT(tp->rcv_nxt+win, tp->rcv_adv)) | |
1219 | tp->rcv_adv = tp->rcv_nxt + win; | |
1220 | tp->last_ack_sent = tp->rcv_nxt; | |
1221 | tp->t_flags &= ~(TF_ACKNOW|TF_DELACK); | |
55e303ae | 1222 | |
91447636 A |
1223 | KERNEL_DEBUG(DBG_FNC_TCP_OUTPUT | DBG_FUNC_END,0,0,0,0,0); |
1224 | if (sendalot && (!tcp_do_newreno || --maxburst)) | |
1c79356b | 1225 | goto again; |
1c79356b A |
1226 | return (0); |
1227 | } | |
1228 | ||
1229 | void | |
1230 | tcp_setpersist(tp) | |
1231 | register struct tcpcb *tp; | |
1232 | { | |
9bccf70c | 1233 | int t = ((tp->t_srtt >> 2) + tp->t_rttvar) >> 1; |
1c79356b A |
1234 | |
1235 | if (tp->t_timer[TCPT_REXMT]) | |
9bccf70c | 1236 | panic("tcp_setpersist: retransmit pending"); |
1c79356b A |
1237 | /* |
1238 | * Start/restart persistance timer. | |
1239 | */ | |
1240 | TCPT_RANGESET(tp->t_timer[TCPT_PERSIST], | |
1241 | t * tcp_backoff[tp->t_rxtshift], | |
1242 | TCPTV_PERSMIN, TCPTV_PERSMAX); | |
1243 | if (tp->t_rxtshift < TCP_MAXRXTSHIFT) | |
1244 | tp->t_rxtshift++; | |
1245 | } |