4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 * Copyright (c) 2016 by Delphix. All rights reserved.
27 * tcp.c, Code implementing the TCP protocol.
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include <sys/types.h>
33 #include <socket_impl.h>
34 #include <socket_inet.h>
35 #include <sys/sysmacros.h>
36 #include <sys/promif.h>
37 #include <sys/socket.h>
38 #include <netinet/in_systm.h>
39 #include <netinet/in.h>
40 #include <netinet/ip.h>
41 #include <netinet/tcp.h>
42 #include <net/if_types.h>
43 #include <sys/salib.h>
46 #include "ipv4_impl.h"
49 #include "v4_sum_impl.h"
50 #include <sys/bootdebug.h>
53 #include <inet/common.h>
54 #include <inet/mib2.h>
57 * We need to redefine BUMP_MIB/UPDATE_MIB to not have DTrace probes.
60 #define BUMP_MIB(x) (x)++
63 #define UPDATE_MIB(x, y) x += y
66 * MIB-2 stuff for SNMP
68 mib2_tcp_t tcp_mib
; /* SNMP fixed size info */
70 /* The TCP mib does not include the following errors. */
71 static uint_t tcp_cksum_errors
;
72 static uint_t tcp_drops
;
74 /* Macros for timestamp comparisons */
75 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0)
76 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0)
79 * Parameters for TCP Initial Send Sequence number (ISS) generation.
80 * The ISS is calculated by adding three components: a time component
81 * which grows by 1 every 4096 nanoseconds (versus every 4 microseconds
82 * suggested by RFC 793, page 27);
83 * a per-connection component which grows by 125000 for every new connection;
84 * and an "extra" component that grows by a random amount centered
85 * approximately on 64000. This causes the the ISS generator to cycle every
86 * 4.89 hours if no TCP connections are made, and faster if connections are
89 #define ISS_INCR 250000
90 #define ISS_NSEC_SHT 0
92 static uint32_t tcp_iss_incr_extra
; /* Incremented for each connection */
94 #define TCP_XMIT_LOWATER 4096
95 #define TCP_XMIT_HIWATER 49152
96 #define TCP_RECV_LOWATER 2048
97 #define TCP_RECV_HIWATER 49152
100 * PAWS needs a timer for 24 days. This is the number of ms in 24 days
102 #define PAWS_TIMEOUT ((uint32_t)(24*24*60*60*1000))
105 * TCP options struct returned from tcp_parse_options.
107 typedef struct tcp_opt_s
{
108 uint32_t tcp_opt_mss
;
109 uint32_t tcp_opt_wscale
;
110 uint32_t tcp_opt_ts_val
;
111 uint32_t tcp_opt_ts_ecr
;
116 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing
120 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
121 (TCPOPT_TSTAMP << 8) | 10)
123 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
124 (TCPOPT_NOP << 8) | TCPOPT_NOP)
128 * Flags returned from tcp_parse_options.
130 #define TCP_OPT_MSS_PRESENT 1
131 #define TCP_OPT_WSCALE_PRESENT 2
132 #define TCP_OPT_TSTAMP_PRESENT 4
133 #define TCP_OPT_SACK_OK_PRESENT 8
134 #define TCP_OPT_SACK_PRESENT 16
136 /* TCP option length */
137 #define TCPOPT_NOP_LEN 1
138 #define TCPOPT_MAXSEG_LEN 4
139 #define TCPOPT_WS_LEN 3
140 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1)
141 #define TCPOPT_TSTAMP_LEN 10
142 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2)
143 #define TCPOPT_SACK_OK_LEN 2
144 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2)
145 #define TCPOPT_REAL_SACK_LEN 4
146 #define TCPOPT_MAX_SACK_LEN 36
147 #define TCPOPT_HEADER_LEN 2
149 /* TCP cwnd burst factor. */
150 #define TCP_CWND_INFINITE 65535
151 #define TCP_CWND_SS 3
152 #define TCP_CWND_NORMAL 5
154 /* Named Dispatch Parameter Management Structure */
155 typedef struct tcpparam_s
{
156 uint32_t tcp_param_min
;
157 uint32_t tcp_param_max
;
158 uint32_t tcp_param_val
;
159 char *tcp_param_name
;
162 /* Max size IP datagram is 64k - 1 */
163 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (struct ip) + \
166 /* Max of the above */
167 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4
169 /* Largest TCP port number */
170 #define TCP_MAX_PORT (64 * 1024 - 1)
172 /* Round up the value to the nearest mss. */
173 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss))
176 #define SECONDS (1000 * MS)
177 #define MINUTES (60 * SECONDS)
178 #define HOURS (60 * MINUTES)
179 #define DAYS (24 * HOURS)
181 /* All NDD params in the core TCP became static variables. */
182 static int tcp_time_wait_interval
= 1 * MINUTES
;
183 static int tcp_conn_req_max_q
= 128;
184 static int tcp_conn_req_max_q0
= 1024;
185 static int tcp_conn_req_min
= 1;
186 static int tcp_conn_grace_period
= 0 * SECONDS
;
187 static int tcp_cwnd_max_
= 1024 * 1024;
188 static int tcp_smallest_nonpriv_port
= 1024;
189 static int tcp_ip_abort_cinterval
= 3 * MINUTES
;
190 static int tcp_ip_abort_linterval
= 3 * MINUTES
;
191 static int tcp_ip_abort_interval
= 8 * MINUTES
;
192 static int tcp_ip_notify_cinterval
= 10 * SECONDS
;
193 static int tcp_ip_notify_interval
= 10 * SECONDS
;
194 static int tcp_ipv4_ttl
= 64;
195 static int tcp_mss_def_ipv4
= 536;
196 static int tcp_mss_max_ipv4
= TCP_MSS_MAX_IPV4
;
197 static int tcp_mss_min
= 108;
198 static int tcp_naglim_def
= (4*1024)-1;
199 static int tcp_rexmit_interval_initial
= 3 * SECONDS
;
200 static int tcp_rexmit_interval_max
= 60 * SECONDS
;
201 static int tcp_rexmit_interval_min
= 400 * MS
;
202 static int tcp_dupack_fast_retransmit
= 3;
203 static int tcp_smallest_anon_port
= 32 * 1024;
204 static int tcp_largest_anon_port
= TCP_MAX_PORT
;
205 static int tcp_xmit_lowat
= TCP_XMIT_LOWATER
;
206 static int tcp_recv_hiwat_minmss
= 4;
207 static int tcp_fin_wait_2_flush_interval
= 1 * MINUTES
;
208 static int tcp_max_buf
= 1024 * 1024;
209 static int tcp_wscale_always
= 1;
210 static int tcp_tstamp_always
= 1;
211 static int tcp_tstamp_if_wscale
= 1;
212 static int tcp_rexmit_interval_extra
= 0;
213 static int tcp_slow_start_after_idle
= 2;
214 static int tcp_slow_start_initial
= 2;
215 static int tcp_sack_permitted
= 2;
216 static int tcp_ecn_permitted
= 2;
218 /* Extra room to fit in headers. */
219 static uint_t tcp_wroff_xtra
;
221 /* Hint for next port to try. */
222 static in_port_t tcp_next_port_to_try
= 32*1024;
225 * Figure out the value of window scale opton. Note that the rwnd is
226 * ASSUMED to be rounded up to the nearest MSS before the calculation.
227 * We cannot find the scale value and then do a round up of tcp_rwnd
228 * because the scale value may not be correct after that.
230 #define SET_WS_VALUE(tcp) \
233 uint32_t rwnd = (tcp)->tcp_rwnd; \
234 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; \
237 (tcp)->tcp_rcv_ws = i; \
241 * Set ECN capable transport (ECT) code point in IP header.
243 * Note that there are 2 ECT code points '01' and '10', which are called
244 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code
245 * point ECT(0) for TCP as described in RFC 2481.
247 #define SET_ECT(tcp, iph) \
248 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \
249 /* We need to clear the code point first. */ \
250 ((struct ip *)(iph))->ip_tos &= 0xFC; \
251 ((struct ip *)(iph))->ip_tos |= IPH_ECN_ECT0; \
255 * The format argument to pass to tcp_display().
256 * DISP_PORT_ONLY means that the returned string has only port info.
257 * DISP_ADDR_AND_PORT means that the returned string also contains the
258 * remote and local IP address.
260 #define DISP_PORT_ONLY 1
261 #define DISP_ADDR_AND_PORT 2
264 * TCP reassembly macros. We hide starting and ending sequence numbers in
265 * b_next and b_prev of messages on the reassembly queue. The messages are
266 * chained using b_cont. These macros are used in tcp_reass() so we don't
267 * have to see the ugly casts and assignments.
268 * Note. use uintptr_t to suppress the gcc warning.
270 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next))
271 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \
272 (mblk_t *)((uintptr_t)(u)))
273 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev))
274 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \
275 (mblk_t *)((uintptr_t)(u)))
277 #define TCP_TIMER_RESTART(tcp, intvl) \
278 (tcp)->tcp_rto_timeout = prom_gettime() + intvl; \
279 (tcp)->tcp_timer_running = B_TRUE;
281 static int tcp_accept_comm(tcp_t
*, tcp_t
*, mblk_t
*, uint_t
);
282 static mblk_t
*tcp_ack_mp(tcp_t
*);
283 static in_port_t
tcp_bindi(in_port_t
, in_addr_t
*, boolean_t
, boolean_t
);
284 static uint16_t tcp_cksum(uint16_t *, uint32_t);
285 static void tcp_clean_death(int, tcp_t
*, int err
);
286 static tcp_t
*tcp_conn_request(tcp_t
*, mblk_t
*mp
, uint_t
, uint_t
);
287 static char *tcp_display(tcp_t
*, char *, char);
288 static int tcp_drain_input(tcp_t
*, int, int);
289 static void tcp_drain_needed(int, tcp_t
*);
290 static boolean_t
tcp_drop_q0(tcp_t
*);
291 static mblk_t
*tcp_get_seg_mp(tcp_t
*, uint32_t, int32_t *);
292 static int tcp_header_len(struct inetgram
*);
293 static in_port_t
tcp_report_ports(uint16_t *, enum Ports
);
294 static int tcp_input(int);
295 static void tcp_iss_init(tcp_t
*);
296 static tcp_t
*tcp_lookup_ipv4(struct ip
*, tcpha_t
*, int, int *);
297 static tcp_t
*tcp_lookup_listener_ipv4(in_addr_t
, in_port_t
, int *);
298 static int tcp_conn_check(tcp_t
*);
299 static int tcp_close(int);
300 static void tcp_close_detached(tcp_t
*);
301 static void tcp_eager_cleanup(tcp_t
*, boolean_t
, int);
302 static void tcp_eager_unlink(tcp_t
*);
303 static void tcp_free(tcp_t
*);
304 static int tcp_header_init_ipv4(tcp_t
*);
305 static void tcp_mss_set(tcp_t
*, uint32_t);
306 static int tcp_parse_options(tcph_t
*, tcp_opt_t
*);
307 static boolean_t
tcp_paws_check(tcp_t
*, tcph_t
*, tcp_opt_t
*);
308 static void tcp_process_options(tcp_t
*, tcph_t
*);
309 static int tcp_random(void);
310 static void tcp_random_init(void);
311 static mblk_t
*tcp_reass(tcp_t
*, mblk_t
*, uint32_t);
312 static void tcp_reass_elim_overlap(tcp_t
*, mblk_t
*);
313 static void tcp_rcv_drain(int sock_id
, tcp_t
*);
314 static void tcp_rcv_enqueue(tcp_t
*, mblk_t
*, uint_t
);
315 static void tcp_rput_data(tcp_t
*, mblk_t
*, int);
316 static int tcp_rwnd_set(tcp_t
*, uint32_t);
317 static int32_t tcp_sack_rxmit(tcp_t
*, int);
318 static void tcp_set_cksum(mblk_t
*);
319 static void tcp_set_rto(tcp_t
*, int32_t);
320 static void tcp_ss_rexmit(tcp_t
*, int);
321 static int tcp_state_wait(int, tcp_t
*, int);
322 static void tcp_timer(tcp_t
*, int);
323 static void tcp_time_wait_append(tcp_t
*);
324 static void tcp_time_wait_collector(void);
325 static void tcp_time_wait_processing(tcp_t
*, mblk_t
*, uint32_t,
326 uint32_t, int, tcph_t
*, int sock_id
);
327 static void tcp_time_wait_remove(tcp_t
*);
328 static in_port_t
tcp_update_next_port(in_port_t
);
329 static int tcp_verify_cksum(mblk_t
*);
330 static void tcp_wput_data(tcp_t
*, mblk_t
*, int);
331 static void tcp_xmit_ctl(char *, tcp_t
*, mblk_t
*, uint32_t, uint32_t,
333 static void tcp_xmit_early_reset(char *, int, mblk_t
*, uint32_t, uint32_t,
335 static int tcp_xmit_end(tcp_t
*, int);
336 static void tcp_xmit_listeners_reset(int, mblk_t
*, uint_t
);
337 static mblk_t
*tcp_xmit_mp(tcp_t
*, mblk_t
*, int32_t, int32_t *,
338 mblk_t
**, uint32_t, boolean_t
, uint32_t *, boolean_t
);
339 static int tcp_init_values(tcp_t
*, struct inetboot_socket
*);
342 #define TCP_DUMP_PACKET(str, mp) \
344 int len = (mp)->b_wptr - (mp)->b_rptr; \
346 printf("%s: dump TCP(%d): \n", (str), len); \
347 hexdump((char *)(mp)->b_rptr, len); \
350 #define TCP_DUMP_PACKET(str, mp)
354 #define DEBUG_1(str, arg) printf(str, (arg))
355 #define DEBUG_2(str, arg1, arg2) printf(str, (arg1), (arg2))
356 #define DEBUG_3(str, arg1, arg2, arg3) printf(str, (arg1), (arg2), (arg3))
358 #define DEBUG_1(str, arg)
359 #define DEBUG_2(str, arg1, arg2)
360 #define DEBUG_3(str, arg1, arg2, arg3)
363 /* Whether it is the first time TCP is used. */
364 static boolean_t tcp_initialized
= B_FALSE
;
366 /* TCP time wait list. */
367 static tcp_t
*tcp_time_wait_head
;
368 static tcp_t
*tcp_time_wait_tail
;
369 static uint32_t tcp_cum_timewait
;
370 /* When the tcp_time_wait_collector is run. */
371 static uint32_t tcp_time_wait_runtime
;
373 #define TCP_RUN_TIME_WAIT_COLLECTOR() \
374 if (prom_gettime() > tcp_time_wait_runtime) \
375 tcp_time_wait_collector();
378 * Accept will return with an error if there is no connection coming in
379 * after this (in ms).
381 static int tcp_accept_timeout
= 60000;
384 * Initialize the TCP-specific parts of a socket.
387 tcp_socket_init(struct inetboot_socket
*isp
)
389 /* Do some initializations. */
390 if (!tcp_initialized
) {
392 /* Extra head room for the MAC layer address. */
393 if ((tcp_wroff_xtra
= mac_get_hdr_len()) & 0x3) {
394 tcp_wroff_xtra
= (tcp_wroff_xtra
& ~0x3) + 0x4;
396 /* Schedule the first time wait cleanup time */
397 tcp_time_wait_runtime
= prom_gettime() + tcp_time_wait_interval
;
398 tcp_initialized
= B_TRUE
;
400 TCP_RUN_TIME_WAIT_COLLECTOR();
402 isp
->proto
= IPPROTO_TCP
;
403 isp
->input
[TRANSPORT_LVL
] = tcp_input
;
404 /* Socket layer should call tcp_send() directly. */
405 isp
->output
[TRANSPORT_LVL
] = NULL
;
406 isp
->close
[TRANSPORT_LVL
] = tcp_close
;
407 isp
->headerlen
[TRANSPORT_LVL
] = tcp_header_len
;
408 isp
->ports
= tcp_report_ports
;
409 if ((isp
->pcb
= bkmem_alloc(sizeof (tcp_t
))) == NULL
) {
413 if ((errno
= tcp_init_values((tcp_t
*)isp
->pcb
, isp
)) != 0) {
414 bkmem_free(isp
->pcb
, sizeof (tcp_t
));
418 * This is set last because this field is used to determine if
419 * a socket is in use or not.
421 isp
->type
= INETBOOT_STREAM
;
425 * Return the size of a TCP header including TCP option.
428 tcp_header_len(struct inetgram
*igm
)
433 /* Just returns the standard TCP header without option */
435 return (sizeof (tcph_t
));
437 if ((pkt
= igm
->igm_mp
) == NULL
)
440 ipvers
= ((struct ip
*)pkt
->b_rptr
)->ip_v
;
441 if (ipvers
== IPV4_VERSION
) {
442 return (TCP_HDR_LENGTH((tcph_t
*)(pkt
+ IPH_HDR_LENGTH(pkt
))));
444 dprintf("tcp_header_len: non-IPv4 packet.\n");
450 * Return the requested port number in network order.
453 tcp_report_ports(uint16_t *tcphp
, enum Ports request
)
455 if (request
== SOURCE
)
456 return (*(uint16_t *)(((tcph_t
*)tcphp
)->th_lport
));
457 return (*(uint16_t *)(((tcph_t
*)tcphp
)->th_fport
));
461 * Because inetboot is not interrupt driven, TCP can only poll. This
462 * means that there can be packets stuck in the NIC buffer waiting to
463 * be processed. Thus we need to drain them before, for example, sending
464 * anything because an ACK may actually be stuck there.
466 * The timeout arguments determine how long we should wait for draining.
469 tcp_drain_input(tcp_t
*tcp
, int sock_id
, int timeout
)
471 struct inetgram
*in_gram
;
472 struct inetgram
*old_in_gram
;
477 dprintf("tcp_drain_input(%d): %s\n", sock_id
,
478 tcp_display(tcp
, NULL
, DISP_ADDR_AND_PORT
));
481 * Since the driver uses the in_timeout value in the socket
482 * structure to determine the timeout value, we need to save
483 * the original one so that we can restore that after draining.
485 old_timeout
= sockets
[sock_id
].in_timeout
;
486 sockets
[sock_id
].in_timeout
= timeout
;
489 * We do this because the input queue may have some user
492 old_in_gram
= sockets
[sock_id
].inq
;
493 sockets
[sock_id
].inq
= NULL
;
495 /* Go out and check the wire */
496 for (i
= MEDIA_LVL
; i
< TRANSPORT_LVL
; i
++) {
497 if (sockets
[sock_id
].input
[i
] != NULL
) {
498 if (sockets
[sock_id
].input
[i
](sock_id
) < 0) {
499 sockets
[sock_id
].in_timeout
= old_timeout
;
500 if (sockets
[sock_id
].inq
!= NULL
)
501 nuke_grams(&sockets
[sock_id
].inq
);
502 sockets
[sock_id
].inq
= old_in_gram
;
508 printf("tcp_drain_input: done with checking packets\n");
510 while ((in_gram
= sockets
[sock_id
].inq
) != NULL
) {
511 /* Remove unknown inetgrams from the head of inq. */
512 if (in_gram
->igm_level
!= TRANSPORT_LVL
) {
514 printf("tcp_drain_input: unexpected packet "
515 "level %d frame found\n", in_gram
->igm_level
);
517 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_TRUE
);
520 mp
= in_gram
->igm_mp
;
521 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_FALSE
);
522 bkmem_free((caddr_t
)in_gram
, sizeof (struct inetgram
));
523 tcp_rput_data(tcp
, mp
, sock_id
);
524 sockets
[sock_id
].in_timeout
= old_timeout
;
527 * The other side may have closed this connection or
528 * RST us. But we need to continue to process other
529 * packets in the socket's queue because they may be
530 * belong to another TCP connections.
532 if (sockets
[sock_id
].pcb
== NULL
)
536 if (tcp
== NULL
|| sockets
[sock_id
].pcb
== NULL
) {
537 if (sockets
[sock_id
].so_error
!= 0)
543 printf("tcp_drain_input: done with processing packets\n");
545 sockets
[sock_id
].in_timeout
= old_timeout
;
546 sockets
[sock_id
].inq
= old_in_gram
;
549 * Data may have been received so indicate it is available
551 tcp_drain_needed(sock_id
, tcp
);
556 * The receive entry point for upper layer to call to get data. Note
557 * that this follows the current architecture that lower layer receive
558 * routines have been called already. Thus if the inq of socket is
559 * not NULL, the packets must be for us.
562 tcp_input(int sock_id
)
564 struct inetgram
*in_gram
;
568 TCP_RUN_TIME_WAIT_COLLECTOR();
570 if ((tcp
= sockets
[sock_id
].pcb
) == NULL
)
573 while ((in_gram
= sockets
[sock_id
].inq
) != NULL
) {
574 /* Remove unknown inetgrams from the head of inq. */
575 if (in_gram
->igm_level
!= TRANSPORT_LVL
) {
577 printf("tcp_input: unexpected packet "
578 "level %d frame found\n", in_gram
->igm_level
);
580 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_TRUE
);
583 mp
= in_gram
->igm_mp
;
584 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_FALSE
);
585 bkmem_free((caddr_t
)in_gram
, sizeof (struct inetgram
));
586 tcp_rput_data(tcp
, mp
, sock_id
);
587 /* The TCP may be gone because it gets a RST. */
588 if (sockets
[sock_id
].pcb
== NULL
)
592 /* Flush the receive list. */
593 if (tcp
->tcp_rcv_list
!= NULL
) {
594 tcp_rcv_drain(sock_id
, tcp
);
596 /* The other side has closed the connection, report this up. */
597 if (tcp
->tcp_state
== TCPS_CLOSE_WAIT
) {
598 sockets
[sock_id
].so_state
|= SS_CANTRCVMORE
;
606 * The send entry point for upper layer to call to send data. In order
607 * to minimize changes to the core TCP code, we need to put the
611 tcp_send(int sock_id
, tcp_t
*tcp
, const void *msg
, int len
)
616 int mss
= tcp
->tcp_mss
;
619 char *buf
= (char *)msg
;
621 TCP_RUN_TIME_WAIT_COLLECTOR();
623 /* We don't want to append 0 size mblk. */
631 * If we cannot allocate more buffer, stop here and
632 * the number of bytes buffered will be returned.
634 * Note that we follow the core TCP optimization that
635 * each mblk contains only MSS bytes data.
637 if ((mp
= allocb(mss
+ tcp
->tcp_ip_hdr_len
+
638 TCP_MAX_HDR_LENGTH
+ tcp_wroff_xtra
, 0)) == NULL
) {
641 mp
->b_rptr
+= tcp
->tcp_hdr_len
+ tcp_wroff_xtra
;
642 bcopy(buf
, mp
->b_rptr
, mss
);
643 mp
->b_wptr
= mp
->b_rptr
+ mss
;
658 * Since inetboot is not interrupt driven, there may be
659 * some ACKs in the MAC's buffer. Drain them first,
660 * otherwise, we may not be able to send.
662 * We expect an ACK in two cases:
664 * 1) We have un-ACK'ed data.
666 * 2) All ACK's have been received and the sender's window has been
667 * closed. We need an ACK back to open the window so that we can
668 * send. In this case, call tcp_drain_input() if the window size is
672 /* window size = MIN(swnd, cwnd) - unacked bytes */
673 win_size
= (tcp
->tcp_swnd
> tcp
->tcp_cwnd
) ? tcp
->tcp_cwnd
:
675 win_size
-= tcp
->tcp_snxt
;
676 win_size
+= tcp
->tcp_suna
;
677 if (win_size
< (2 * tcp
->tcp_mss
))
678 if (tcp_drain_input(tcp
, sock_id
, 5) < 0)
681 tcp_wput_data(tcp
, head
, sock_id
);
683 * errno should be reset here as it may be
684 * set to ETIMEDOUT. This may be set by
685 * the MAC driver in case it has timed out
686 * waiting for ARP reply. Any segment which
687 * was not transmitted because of ARP timeout
688 * will be retransmitted by TCP.
690 if (errno
== ETIMEDOUT
)
695 /* Free up all TCP related stuff */
699 if (tcp
->tcp_iphc
!= NULL
) {
700 bkmem_free((caddr_t
)tcp
->tcp_iphc
, tcp
->tcp_iphc_len
);
701 tcp
->tcp_iphc
= NULL
;
703 if (tcp
->tcp_xmit_head
!= NULL
) {
704 freemsg(tcp
->tcp_xmit_head
);
705 tcp
->tcp_xmit_head
= NULL
;
707 if (tcp
->tcp_rcv_list
!= NULL
) {
708 freemsg(tcp
->tcp_rcv_list
);
709 tcp
->tcp_rcv_list
= NULL
;
711 if (tcp
->tcp_reass_head
!= NULL
) {
712 freemsg(tcp
->tcp_reass_head
);
713 tcp
->tcp_reass_head
= NULL
;
715 if (tcp
->tcp_sack_info
!= NULL
) {
716 bkmem_free((caddr_t
)tcp
->tcp_sack_info
,
717 sizeof (tcp_sack_info_t
));
718 tcp
->tcp_sack_info
= NULL
;
723 tcp_close_detached(tcp_t
*tcp
)
725 if (tcp
->tcp_listener
!= NULL
)
726 tcp_eager_unlink(tcp
);
728 bkmem_free((caddr_t
)tcp
, sizeof (tcp_t
));
732 * If we are an eager connection hanging off a listener that hasn't
733 * formally accepted the connection yet, get off its list and blow off
734 * any data that we have accumulated.
737 tcp_eager_unlink(tcp_t
*tcp
)
739 tcp_t
*listener
= tcp
->tcp_listener
;
741 assert(listener
!= NULL
);
742 if (tcp
->tcp_eager_next_q0
!= NULL
) {
743 assert(tcp
->tcp_eager_prev_q0
!= NULL
);
745 /* Remove the eager tcp from q0 */
746 tcp
->tcp_eager_next_q0
->tcp_eager_prev_q0
=
747 tcp
->tcp_eager_prev_q0
;
748 tcp
->tcp_eager_prev_q0
->tcp_eager_next_q0
=
749 tcp
->tcp_eager_next_q0
;
750 listener
->tcp_conn_req_cnt_q0
--;
752 tcp_t
**tcpp
= &listener
->tcp_eager_next_q
;
755 for (; tcpp
[0]; tcpp
= &tcpp
[0]->tcp_eager_next_q
) {
756 if (tcpp
[0] == tcp
) {
757 if (listener
->tcp_eager_last_q
== tcp
) {
759 * If we are unlinking the last
760 * element on the list, adjust
761 * tail pointer. Set tail pointer
762 * to nil when list is empty.
764 assert(tcp
->tcp_eager_next_q
== NULL
);
765 if (listener
->tcp_eager_last_q
==
766 listener
->tcp_eager_next_q
) {
767 listener
->tcp_eager_last_q
=
771 * We won't get here if there
772 * is only one eager in the
775 assert(prev
!= NULL
);
776 listener
->tcp_eager_last_q
=
780 tcpp
[0] = tcp
->tcp_eager_next_q
;
781 tcp
->tcp_eager_next_q
= NULL
;
782 tcp
->tcp_eager_last_q
= NULL
;
783 listener
->tcp_conn_req_cnt_q
--;
789 tcp
->tcp_listener
= NULL
;
793 * Reset any eager connection hanging off this listener
794 * and then reclaim it's resources.
797 tcp_eager_cleanup(tcp_t
*listener
, boolean_t q0_only
, int sock_id
)
802 /* First cleanup q */
803 while ((eager
= listener
->tcp_eager_next_q
) != NULL
) {
804 assert(listener
->tcp_eager_last_q
!= NULL
);
805 tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
806 eager
, NULL
, eager
->tcp_snxt
, 0, TH_RST
, 0,
808 tcp_close_detached(eager
);
810 assert(listener
->tcp_eager_last_q
== NULL
);
812 /* Then cleanup q0 */
813 while ((eager
= listener
->tcp_eager_next_q0
) != listener
) {
814 tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
815 eager
, NULL
, eager
->tcp_snxt
, 0, TH_RST
, 0, sock_id
);
816 tcp_close_detached(eager
);
821 * To handle the shutdown request. Called from shutdown()
824 tcp_shutdown(int sock_id
)
828 DEBUG_1("tcp_shutdown: sock_id %x\n", sock_id
);
830 if ((tcp
= sockets
[sock_id
].pcb
) == NULL
) {
835 * Since inetboot is not interrupt driven, there may be
836 * some ACKs in the MAC's buffer. Drain them first,
837 * otherwise, we may not be able to send.
839 if (tcp_drain_input(tcp
, sock_id
, 5) < 0) {
841 * If we return now without freeing TCP, there will be
844 if (sockets
[sock_id
].pcb
!= NULL
)
845 tcp_clean_death(sock_id
, tcp
, 0);
849 DEBUG_1("tcp_shutdown: tcp_state %x\n", tcp
->tcp_state
);
850 switch (tcp
->tcp_state
) {
854 * Shutdown during the connect 3-way handshake
856 case TCPS_ESTABLISHED
:
859 * wait for the FIN to be ACKed,
860 * then remain in FIN_WAIT_2
862 dprintf("tcp_shutdown: sending fin\n");
863 if (tcp_xmit_end(tcp
, sock_id
) == 0 &&
864 tcp_state_wait(sock_id
, tcp
, TCPS_FIN_WAIT_2
) < 0) {
865 /* During the wait, TCP may be gone... */
866 if (sockets
[sock_id
].pcb
== NULL
)
869 dprintf("tcp_shutdown: done\n");
879 /* To handle closing of the socket */
881 tcp_close(int sock_id
)
887 if ((tcp
= sockets
[sock_id
].pcb
) == NULL
) {
891 TCP_RUN_TIME_WAIT_COLLECTOR();
894 * Since inetboot is not interrupt driven, there may be
895 * some ACKs in the MAC's buffer. Drain them first,
896 * otherwise, we may not be able to send.
898 if (tcp_drain_input(tcp
, sock_id
, 5) < 0) {
900 * If we return now without freeing TCP, there will be
903 if (sockets
[sock_id
].pcb
!= NULL
)
904 tcp_clean_death(sock_id
, tcp
, 0);
908 if (tcp
->tcp_conn_req_cnt_q0
!= 0 || tcp
->tcp_conn_req_cnt_q
!= 0) {
909 /* Cleanup for listener */
910 tcp_eager_cleanup(tcp
, 0, sock_id
);
914 switch (tcp
->tcp_state
) {
921 msg
= "tcp_close, during connect";
925 * Close during the connect 3-way handshake
926 * but here there may or may not be pending data
927 * already on queue. Process almost same as in
928 * the ESTABLISHED state.
933 * If SO_LINGER has set a zero linger time, abort the
934 * connection with a reset.
936 if (tcp
->tcp_linger
&& tcp
->tcp_lingertime
== 0) {
937 msg
= "tcp_close, zero lingertime";
942 * Abort connection if there is unread data queued.
944 if (tcp
->tcp_rcv_list
!= NULL
||
945 tcp
->tcp_reass_head
!= NULL
) {
946 msg
= "tcp_close, unread data";
949 if (tcp
->tcp_state
<= TCPS_LISTEN
)
953 * Transmit the FIN before detaching the tcp_t.
954 * After tcp_detach returns this queue/perimeter
955 * no longer owns the tcp_t thus others can modify it.
956 * The TCP could be closed in tcp_state_wait called by
957 * tcp_wput_data called by tcp_xmit_end.
959 (void) tcp_xmit_end(tcp
, sock_id
);
960 if (sockets
[sock_id
].pcb
== NULL
)
964 * If lingering on close then wait until the fin is acked,
965 * the SO_LINGER time passes, or a reset is sent/received.
967 if (tcp
->tcp_linger
&& tcp
->tcp_lingertime
> 0 &&
968 !(tcp
->tcp_fin_acked
) &&
969 tcp
->tcp_state
>= TCPS_ESTABLISHED
) {
970 uint32_t stoptime
; /* in ms */
972 tcp
->tcp_client_errno
= 0;
973 stoptime
= prom_gettime() +
974 (tcp
->tcp_lingertime
* 1000);
975 while (!(tcp
->tcp_fin_acked
) &&
976 tcp
->tcp_state
>= TCPS_ESTABLISHED
&&
977 tcp
->tcp_client_errno
== 0 &&
978 ((int32_t)(stoptime
- prom_gettime()) > 0)) {
979 if (tcp_drain_input(tcp
, sock_id
, 5) < 0) {
980 if (sockets
[sock_id
].pcb
!= NULL
) {
981 tcp_clean_death(sock_id
,
987 tcp
->tcp_client_errno
= 0;
989 if (tcp_state_wait(sock_id
, tcp
, TCPS_TIME_WAIT
) < 0) {
990 /* During the wait, TCP may be gone... */
991 if (sockets
[sock_id
].pcb
== NULL
)
993 msg
= "tcp_close, couldn't detach";
1000 /* Something went wrong... Send a RST and report the error */
1002 if (tcp
->tcp_state
== TCPS_ESTABLISHED
||
1003 tcp
->tcp_state
== TCPS_CLOSE_WAIT
)
1004 BUMP_MIB(tcp_mib
.tcpEstabResets
);
1005 if (tcp
->tcp_state
== TCPS_SYN_SENT
||
1006 tcp
->tcp_state
== TCPS_SYN_RCVD
)
1007 BUMP_MIB(tcp_mib
.tcpAttemptFails
);
1008 tcp_xmit_ctl(msg
, tcp
, NULL
, tcp
->tcp_snxt
, 0, TH_RST
, 0,
1013 bkmem_free((caddr_t
)tcp
, sizeof (tcp_t
));
1014 sockets
[sock_id
].pcb
= NULL
;
1018 /* To make an endpoint a listener. */
1020 tcp_listen(int sock_id
, int backlog
)
1024 if ((tcp
= (tcp_t
*)(sockets
[sock_id
].pcb
)) == NULL
) {
1028 /* We allow calling listen() multiple times to change the backlog. */
1029 if (tcp
->tcp_state
> TCPS_LISTEN
|| tcp
->tcp_state
< TCPS_BOUND
) {
1033 /* The following initialization should only be done once. */
1034 if (tcp
->tcp_state
!= TCPS_LISTEN
) {
1035 tcp
->tcp_eager_next_q0
= tcp
->tcp_eager_prev_q0
= tcp
;
1036 tcp
->tcp_eager_next_q
= NULL
;
1037 tcp
->tcp_state
= TCPS_LISTEN
;
1038 tcp
->tcp_second_ctimer_threshold
= tcp_ip_abort_linterval
;
1040 if ((tcp
->tcp_conn_req_max
= backlog
) > tcp_conn_req_max_q
) {
1041 tcp
->tcp_conn_req_max
= tcp_conn_req_max_q
;
1043 if (tcp
->tcp_conn_req_max
< tcp_conn_req_min
) {
1044 tcp
->tcp_conn_req_max
= tcp_conn_req_min
;
1049 /* To accept connections. */
1051 tcp_accept(int sock_id
, struct sockaddr
*addr
, socklen_t
*addr_len
)
1055 int sd
, new_sock_id
;
1056 struct sockaddr_in
*new_addr
= (struct sockaddr_in
*)addr
;
1060 if ((listener
= (tcp_t
*)(sockets
[sock_id
].pcb
)) == NULL
||
1061 new_addr
== NULL
|| addr_len
== NULL
||
1062 *addr_len
< sizeof (struct sockaddr_in
) ||
1063 listener
->tcp_state
!= TCPS_LISTEN
) {
1068 if (sockets
[sock_id
].in_timeout
> tcp_accept_timeout
)
1069 timeout
= prom_gettime() + sockets
[sock_id
].in_timeout
;
1071 timeout
= prom_gettime() + tcp_accept_timeout
;
1072 while (listener
->tcp_eager_next_q
== NULL
&&
1073 timeout
> prom_gettime()) {
1075 printf("tcp_accept: Waiting in tcp_accept()\n");
1077 if (tcp_drain_input(listener
, sock_id
, 5) < 0) {
1081 /* If there is an eager, don't timeout... */
1082 if (timeout
<= prom_gettime() && listener
->tcp_eager_next_q
== NULL
) {
1084 printf("tcp_accept: timeout\n");
1090 printf("tcp_accept: got a connection\n");
1093 /* Now create the socket for this new TCP. */
1094 if ((sd
= socket(AF_INET
, SOCK_STREAM
, 0)) < 0) {
1097 if ((new_sock_id
= so_check_fd(sd
, &errno
)) == -1)
1098 /* This should not happen! */
1099 prom_panic("so_check_fd() fails in tcp_accept()");
1100 /* Free the TCP PCB in the original socket. */
1101 bkmem_free((caddr_t
)(sockets
[new_sock_id
].pcb
), sizeof (tcp_t
));
1102 /* Dequeue the eager and attach it to the socket. */
1103 eager
= listener
->tcp_eager_next_q
;
1104 listener
->tcp_eager_next_q
= eager
->tcp_eager_next_q
;
1105 if (listener
->tcp_eager_last_q
== eager
)
1106 listener
->tcp_eager_last_q
= NULL
;
1107 eager
->tcp_eager_next_q
= NULL
;
1108 sockets
[new_sock_id
].pcb
= eager
;
1109 listener
->tcp_conn_req_cnt_q
--;
1111 /* Copy in the address info. */
1112 bcopy(&eager
->tcp_remote
, &new_addr
->sin_addr
.s_addr
,
1113 sizeof (in_addr_t
));
1114 bcopy(&eager
->tcp_fport
, &new_addr
->sin_port
, sizeof (in_port_t
));
1115 new_addr
->sin_family
= AF_INET
;
1118 printf("tcp_accept(), new sock_id: %d\n", sd
);
1123 /* Update the next anonymous port to use. */
1125 tcp_update_next_port(in_port_t port
)
1127 /* Don't allow the port to fall out of the anonymous port range. */
1128 if (port
< tcp_smallest_anon_port
|| port
> tcp_largest_anon_port
)
1129 port
= (in_port_t
)tcp_smallest_anon_port
;
1131 if (port
< tcp_smallest_nonpriv_port
)
1132 port
= (in_port_t
)tcp_smallest_nonpriv_port
;
1136 /* To check whether a bind to a port is allowed. */
1138 tcp_bindi(in_port_t port
, in_addr_t
*addr
, boolean_t reuseaddr
,
1139 boolean_t bind_to_req_port_only
)
1144 count
= tcp_largest_anon_port
- tcp_smallest_anon_port
;
1146 for (i
= 0; i
< MAXSOCKET
; i
++) {
1147 if (sockets
[i
].type
!= INETBOOT_STREAM
||
1148 ((tcp
= (tcp_t
*)sockets
[i
].pcb
) == NULL
) ||
1149 ntohs(tcp
->tcp_lport
) != port
) {
1153 * Both TCPs have the same port. If SO_REUSEDADDR is
1154 * set and the bound TCP has a state greater than
1155 * TCPS_LISTEN, it is fine.
1157 if (reuseaddr
&& tcp
->tcp_state
> TCPS_LISTEN
) {
1160 if (tcp
->tcp_bound_source
!= INADDR_ANY
&&
1161 *addr
!= INADDR_ANY
&&
1162 tcp
->tcp_bound_source
!= *addr
) {
1165 if (bind_to_req_port_only
) {
1169 port
= tcp_update_next_port(++port
);
1178 /* To handle the bind request. */
1180 tcp_bind(int sock_id
)
1183 in_port_t requested_port
, allocated_port
;
1184 boolean_t bind_to_req_port_only
;
1185 boolean_t reuseaddr
;
1187 if ((tcp
= (tcp_t
*)sockets
[sock_id
].pcb
) == NULL
) {
1192 if (tcp
->tcp_state
>= TCPS_BOUND
) {
1193 /* We don't allow multiple bind(). */
1198 requested_port
= ntohs(sockets
[sock_id
].bind
.sin_port
);
1200 /* The bound source can be INADDR_ANY. */
1201 tcp
->tcp_bound_source
= sockets
[sock_id
].bind
.sin_addr
.s_addr
;
1203 tcp
->tcp_ipha
->ip_src
.s_addr
= tcp
->tcp_bound_source
;
1205 /* Verify the port is available. */
1206 if (requested_port
== 0)
1207 bind_to_req_port_only
= B_FALSE
;
1208 else /* T_BIND_REQ and requested_port != 0 */
1209 bind_to_req_port_only
= B_TRUE
;
1211 if (requested_port
== 0) {
1212 requested_port
= tcp_update_next_port(++tcp_next_port_to_try
);
1214 reuseaddr
= sockets
[sock_id
].so_opt
& SO_REUSEADDR
;
1215 allocated_port
= tcp_bindi(requested_port
, &(tcp
->tcp_bound_source
),
1216 reuseaddr
, bind_to_req_port_only
);
1218 if (allocated_port
== 0) {
1222 tcp
->tcp_lport
= htons(allocated_port
);
1223 *(uint16_t *)tcp
->tcp_tcph
->th_lport
= tcp
->tcp_lport
;
1224 sockets
[sock_id
].bind
.sin_port
= tcp
->tcp_lport
;
1225 tcp
->tcp_state
= TCPS_BOUND
;
1230 * Check for duplicate TCP connections.
1233 tcp_conn_check(tcp_t
*tcp
)
1238 for (i
= 0; i
< MAXSOCKET
; i
++) {
1239 if (sockets
[i
].type
!= INETBOOT_STREAM
)
1241 /* Socket may not be closed but the TCP can be gone. */
1242 if ((tmp_tcp
= (tcp_t
*)sockets
[i
].pcb
) == NULL
)
1244 /* We only care about TCP in states later than SYN_SENT. */
1245 if (tmp_tcp
->tcp_state
< TCPS_SYN_SENT
)
1247 if (tmp_tcp
->tcp_lport
!= tcp
->tcp_lport
||
1248 tmp_tcp
->tcp_fport
!= tcp
->tcp_fport
||
1249 tmp_tcp
->tcp_bound_source
!= tcp
->tcp_bound_source
||
1250 tmp_tcp
->tcp_remote
!= tcp
->tcp_remote
) {
1259 /* To handle a connect request. */
1261 tcp_connect(int sock_id
)
1270 if ((tcp
= (tcp_t
*)(sockets
[sock_id
].pcb
)) == NULL
) {
1275 TCP_RUN_TIME_WAIT_COLLECTOR();
1277 dstaddr
= sockets
[sock_id
].remote
.sin_addr
.s_addr
;
1278 dstport
= sockets
[sock_id
].remote
.sin_port
;
1281 * Check for attempt to connect to INADDR_ANY or non-unicast addrress.
1282 * We don't have enough info to check for broadcast addr, except
1283 * for the all 1 broadcast.
1285 if (dstaddr
== INADDR_ANY
|| IN_CLASSD(ntohl(dstaddr
)) ||
1286 dstaddr
== INADDR_BROADCAST
) {
1288 * SunOS 4.x and 4.3 BSD allow an application
1289 * to connect a TCP socket to INADDR_ANY.
1290 * When they do this, the kernel picks the
1291 * address of one interface and uses it
1292 * instead. The kernel usually ends up
1293 * picking the address of the loopback
1294 * interface. This is an undocumented feature.
1295 * However, we provide the same thing here
1296 * in order to have source and binary
1297 * compatibility with SunOS 4.x.
1298 * Update the T_CONN_REQ (sin/sin6) since it is used to
1299 * generate the T_CONN_CON.
1301 * Fail this for inetboot TCP.
1307 /* It is not bound to any address yet... */
1308 if (tcp
->tcp_bound_source
== INADDR_ANY
) {
1309 ipv4_getipaddr(&(sockets
[sock_id
].bind
.sin_addr
));
1310 /* We don't have an address! */
1311 if (ntohl(sockets
[sock_id
].bind
.sin_addr
.s_addr
) ==
1316 tcp
->tcp_bound_source
= sockets
[sock_id
].bind
.sin_addr
.s_addr
;
1317 tcp
->tcp_ipha
->ip_src
.s_addr
= tcp
->tcp_bound_source
;
1321 * Don't let an endpoint connect to itself.
1323 if (dstaddr
== tcp
->tcp_ipha
->ip_src
.s_addr
&&
1324 dstport
== tcp
->tcp_lport
) {
1329 tcp
->tcp_ipha
->ip_dst
.s_addr
= dstaddr
;
1330 tcp
->tcp_remote
= dstaddr
;
1331 tcph
= tcp
->tcp_tcph
;
1332 *(uint16_t *)tcph
->th_fport
= dstport
;
1333 tcp
->tcp_fport
= dstport
;
1336 * Don't allow this connection to completely duplicate
1337 * an existing connection.
1339 if (tcp_conn_check(tcp
) < 0) {
1345 * Just make sure our rwnd is at
1346 * least tcp_recv_hiwat_mss * MSS
1347 * large, and round up to the nearest
1350 * We do the round up here because
1351 * we need to get the interface
1352 * MTU first before we can do the
1355 mss
= tcp
->tcp_mss
- tcp
->tcp_hdr_len
;
1356 tcp
->tcp_rwnd
= MAX(MSS_ROUNDUP(tcp
->tcp_rwnd
, mss
),
1357 tcp_recv_hiwat_minmss
* mss
);
1358 tcp
->tcp_rwnd_max
= tcp
->tcp_rwnd
;
1360 U32_TO_ABE16((tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
),
1361 tcp
->tcp_tcph
->th_win
);
1362 if (tcp
->tcp_rcv_ws
> 0 || tcp_wscale_always
)
1363 tcp
->tcp_snd_ws_ok
= B_TRUE
;
1366 * Set tcp_snd_ts_ok to true
1367 * so that tcp_xmit_mp will
1368 * include the timestamp
1369 * option in the SYN segment.
1371 if (tcp_tstamp_always
||
1372 (tcp
->tcp_rcv_ws
&& tcp_tstamp_if_wscale
)) {
1373 tcp
->tcp_snd_ts_ok
= B_TRUE
;
1376 if (tcp_sack_permitted
== 2 ||
1377 tcp
->tcp_snd_sack_ok
) {
1378 assert(tcp
->tcp_sack_info
== NULL
);
1379 if ((tcp
->tcp_sack_info
= (tcp_sack_info_t
*)bkmem_zalloc(
1380 sizeof (tcp_sack_info_t
))) == NULL
) {
1381 tcp
->tcp_snd_sack_ok
= B_FALSE
;
1383 tcp
->tcp_snd_sack_ok
= B_TRUE
;
1387 * Should we use ECN? Note that the current
1388 * default value (SunOS 5.9) of tcp_ecn_permitted
1389 * is 2. The reason for doing this is that there
1390 * are equipments out there that will drop ECN
1391 * enabled IP packets. Setting it to 1 avoids
1392 * compatibility problems.
1394 if (tcp_ecn_permitted
== 2)
1395 tcp
->tcp_ecn_ok
= B_TRUE
;
1398 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
1399 tcp
->tcp_active_open
= B_TRUE
;
1401 tcp
->tcp_state
= TCPS_SYN_SENT
;
1402 syn_mp
= tcp_xmit_mp(tcp
, NULL
, 0, NULL
, NULL
, tcp
->tcp_iss
, B_FALSE
,
1404 if (syn_mp
!= NULL
) {
1407 /* Dump the packet when debugging. */
1408 TCP_DUMP_PACKET("tcp_connect", syn_mp
);
1409 /* Send out the SYN packet. */
1410 ret
= ipv4_tcp_output(sock_id
, syn_mp
);
1413 * errno ETIMEDOUT is set by the mac driver
1414 * in case it is not able to receive ARP reply.
1415 * TCP will retransmit this segment so we can
1416 * ignore the ARP timeout.
1418 if ((ret
< 0) && (errno
!= ETIMEDOUT
)) {
1421 /* tcp_state_wait() will finish the 3 way handshake. */
1422 return (tcp_state_wait(sock_id
, tcp
, TCPS_ESTABLISHED
));
1430 * Common accept code. Called by tcp_conn_request.
1431 * cr_pkt is the SYN packet.
1434 tcp_accept_comm(tcp_t
*listener
, tcp_t
*acceptor
, mblk_t
*cr_pkt
,
1440 printf("tcp_accept_comm #######################\n");
1444 * When we get here, we know that the acceptor header template
1445 * has already been initialized.
1446 * However, it may not match the listener if the listener
1447 * includes options...
1448 * It may also not match the listener if the listener is v6 and
1449 * and the acceptor is v4
1451 acceptor
->tcp_lport
= listener
->tcp_lport
;
1453 if (listener
->tcp_ipversion
== acceptor
->tcp_ipversion
) {
1454 if (acceptor
->tcp_iphc_len
!= listener
->tcp_iphc_len
) {
1456 * Listener had options of some sort; acceptor inherits.
1457 * Free up the acceptor template and allocate one
1458 * of the right size.
1460 bkmem_free(acceptor
->tcp_iphc
, acceptor
->tcp_iphc_len
);
1461 acceptor
->tcp_iphc
= bkmem_zalloc(
1462 listener
->tcp_iphc_len
);
1463 if (acceptor
->tcp_iphc
== NULL
) {
1464 acceptor
->tcp_iphc_len
= 0;
1467 acceptor
->tcp_iphc_len
= listener
->tcp_iphc_len
;
1469 acceptor
->tcp_hdr_len
= listener
->tcp_hdr_len
;
1470 acceptor
->tcp_ip_hdr_len
= listener
->tcp_ip_hdr_len
;
1471 acceptor
->tcp_tcp_hdr_len
= listener
->tcp_tcp_hdr_len
;
1474 * Copy the IP+TCP header template from listener to acceptor
1476 bcopy(listener
->tcp_iphc
, acceptor
->tcp_iphc
,
1477 listener
->tcp_hdr_len
);
1478 acceptor
->tcp_ipha
= (struct ip
*)acceptor
->tcp_iphc
;
1479 acceptor
->tcp_tcph
= (tcph_t
*)(acceptor
->tcp_iphc
+
1480 acceptor
->tcp_ip_hdr_len
);
1482 prom_panic("tcp_accept_comm: version not equal");
1485 /* Copy our new dest and fport from the connection request packet */
1486 if (acceptor
->tcp_ipversion
== IPV4_VERSION
) {
1489 ipha
= (struct ip
*)cr_pkt
->b_rptr
;
1490 acceptor
->tcp_ipha
->ip_dst
= ipha
->ip_src
;
1491 acceptor
->tcp_remote
= ipha
->ip_src
.s_addr
;
1492 acceptor
->tcp_ipha
->ip_src
= ipha
->ip_dst
;
1493 acceptor
->tcp_bound_source
= ipha
->ip_dst
.s_addr
;
1494 tcph
= (tcph_t
*)&cr_pkt
->b_rptr
[ip_hdr_len
];
1496 prom_panic("tcp_accept_comm: not IPv4");
1498 bcopy(tcph
->th_lport
, acceptor
->tcp_tcph
->th_fport
, sizeof (in_port_t
));
1499 bcopy(acceptor
->tcp_tcph
->th_fport
, &acceptor
->tcp_fport
,
1500 sizeof (in_port_t
));
1502 * For an all-port proxy listener, the local port is determined by
1503 * the port number field in the SYN packet.
1505 if (listener
->tcp_lport
== 0) {
1506 acceptor
->tcp_lport
= *(in_port_t
*)tcph
->th_fport
;
1507 bcopy(tcph
->th_fport
, acceptor
->tcp_tcph
->th_lport
,
1508 sizeof (in_port_t
));
1510 /* Inherit various TCP parameters from the listener */
1511 acceptor
->tcp_naglim
= listener
->tcp_naglim
;
1512 acceptor
->tcp_first_timer_threshold
=
1513 listener
->tcp_first_timer_threshold
;
1514 acceptor
->tcp_second_timer_threshold
=
1515 listener
->tcp_second_timer_threshold
;
1517 acceptor
->tcp_first_ctimer_threshold
=
1518 listener
->tcp_first_ctimer_threshold
;
1519 acceptor
->tcp_second_ctimer_threshold
=
1520 listener
->tcp_second_ctimer_threshold
;
1522 acceptor
->tcp_xmit_hiwater
= listener
->tcp_xmit_hiwater
;
1524 acceptor
->tcp_state
= TCPS_LISTEN
;
1525 tcp_iss_init(acceptor
);
1527 /* Process all TCP options. */
1528 tcp_process_options(acceptor
, tcph
);
1530 /* Is the other end ECN capable? */
1531 if (tcp_ecn_permitted
>= 1 &&
1532 (tcph
->th_flags
[0] & (TH_ECE
|TH_CWR
)) == (TH_ECE
|TH_CWR
)) {
1533 acceptor
->tcp_ecn_ok
= B_TRUE
;
1537 * listener->tcp_rq->q_hiwat should be the default window size or a
1538 * window size changed via SO_RCVBUF option. First round up the
1539 * acceptor's tcp_rwnd to the nearest MSS. Then find out the window
1540 * scale option value if needed. Call tcp_rwnd_set() to finish the
1543 * Note if there is a rpipe metric associated with the remote host,
1544 * we should not inherit receive window size from listener.
1546 acceptor
->tcp_rwnd
= MSS_ROUNDUP(
1547 (acceptor
->tcp_rwnd
== 0 ? listener
->tcp_rwnd_max
:
1548 acceptor
->tcp_rwnd
), acceptor
->tcp_mss
);
1549 if (acceptor
->tcp_snd_ws_ok
)
1550 SET_WS_VALUE(acceptor
);
1552 * Note that this is the only place tcp_rwnd_set() is called for
1553 * accepting a connection. We need to call it here instead of
1554 * after the 3-way handshake because we need to tell the other
1555 * side our rwnd in the SYN-ACK segment.
1557 (void) tcp_rwnd_set(acceptor
, acceptor
->tcp_rwnd
);
1563 * Defense for the SYN attack -
1564 * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest
1565 * one that doesn't have the dontdrop bit set.
1566 * 2. Don't drop a SYN request before its first timeout. This gives every
1567 * request at least til the first timeout to complete its 3-way handshake.
1568 * 3. The current threshold is - # of timeout > q0len/4 => SYN alert on
1569 * # of timeout drops back to <= q0len/32 => SYN alert off
1572 tcp_drop_q0(tcp_t
*tcp
)
1576 assert(tcp
->tcp_eager_next_q0
!= tcp
->tcp_eager_prev_q0
);
1578 * New one is added after next_q0 so prev_q0 points to the oldest
1579 * Also do not drop any established connections that are deferred on
1580 * q0 due to q being full
1583 eager
= tcp
->tcp_eager_prev_q0
;
1584 while (eager
->tcp_dontdrop
|| eager
->tcp_conn_def_q0
) {
1585 /* XXX should move the eager to the head */
1586 eager
= eager
->tcp_eager_prev_q0
;
1588 eager
= tcp
->tcp_eager_prev_q0
;
1592 dprintf("tcp_drop_q0: listen half-open queue (max=%d) overflow"
1593 " (%d pending) on %s, drop one", tcp_conn_req_max_q0
,
1594 tcp
->tcp_conn_req_cnt_q0
,
1595 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
1597 BUMP_MIB(tcp_mib
.tcpHalfOpenDrop
);
1598 bkmem_free((caddr_t
)eager
, sizeof (tcp_t
));
1604 tcp_conn_request(tcp_t
*tcp
, mblk_t
*mp
, uint_t sock_id
, uint_t ip_hdr_len
)
1611 printf("tcp_conn_request ###################\n");
1614 if (tcp
->tcp_conn_req_cnt_q
>= tcp
->tcp_conn_req_max
) {
1615 BUMP_MIB(tcp_mib
.tcpListenDrop
);
1616 dprintf("tcp_conn_request: listen backlog (max=%d) "
1617 "overflow (%d pending) on %s",
1618 tcp
->tcp_conn_req_max
, tcp
->tcp_conn_req_cnt_q
,
1619 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
1623 assert(OK_32PTR(mp
->b_rptr
));
1625 if (tcp
->tcp_conn_req_cnt_q0
>=
1626 tcp
->tcp_conn_req_max
+ tcp_conn_req_max_q0
) {
1628 * Q0 is full. Drop a pending half-open req from the queue
1629 * to make room for the new SYN req. Also mark the time we
1632 tcp
->tcp_last_rcv_lbolt
= prom_gettime();
1633 if (!tcp_drop_q0(tcp
)) {
1635 BUMP_MIB(tcp_mib
.tcpListenDropQ0
);
1636 dprintf("tcp_conn_request: listen half-open queue "
1637 "(max=%d) full (%d pending) on %s",
1638 tcp_conn_req_max_q0
,
1639 tcp
->tcp_conn_req_cnt_q0
,
1640 tcp_display(tcp
, NULL
, DISP_PORT_ONLY
));
1645 ipha
= (struct ip
*)mp
->b_rptr
;
1646 if (IN_CLASSD(ntohl(ipha
->ip_src
.s_addr
)) ||
1647 ipha
->ip_src
.s_addr
== INADDR_BROADCAST
||
1648 ipha
->ip_src
.s_addr
== INADDR_ANY
||
1649 ipha
->ip_dst
.s_addr
== INADDR_BROADCAST
) {
1654 * We allow the connection to proceed
1655 * by generating a detached tcp state vector and put it in
1656 * the eager queue. When an accept happens, it will be
1657 * dequeued sequentially.
1659 if ((eager
= (tcp_t
*)bkmem_alloc(sizeof (tcp_t
))) == NULL
) {
1664 if ((errno
= tcp_init_values(eager
, NULL
)) != 0) {
1666 bkmem_free((caddr_t
)eager
, sizeof (tcp_t
));
1671 * Eager connection inherits address form from its listener,
1672 * but its packet form comes from the version of the received
1675 eager
->tcp_family
= tcp
->tcp_family
;
1677 err
= tcp_accept_comm(tcp
, eager
, mp
, ip_hdr_len
);
1679 bkmem_free((caddr_t
)eager
, sizeof (tcp_t
));
1683 tcp
->tcp_eager_next_q0
->tcp_eager_prev_q0
= eager
;
1684 eager
->tcp_eager_next_q0
= tcp
->tcp_eager_next_q0
;
1685 tcp
->tcp_eager_next_q0
= eager
;
1686 eager
->tcp_eager_prev_q0
= tcp
;
1688 /* Set tcp_listener before adding it to tcp_conn_fanout */
1689 eager
->tcp_listener
= tcp
;
1690 tcp
->tcp_conn_req_cnt_q0
++;
1696 * To get around the non-interrupt problem of inetboot.
1697 * Keep on processing packets until a certain state is reached or the
1698 * TCP is destroyed because of getting a RST packet.
1701 tcp_state_wait(int sock_id
, tcp_t
*tcp
, int state
)
1704 struct inetgram
*in_gram
;
1707 boolean_t changed
= B_FALSE
;
1710 * We need to make sure that the MAC does not wait longer
1711 * than RTO for any packet so that TCP can do retransmission.
1712 * But if the MAC timeout is less than tcp_rto, we are fine
1713 * and do not need to change it.
1715 timeout
= sockets
[sock_id
].in_timeout
;
1716 if (timeout
> tcp
->tcp_rto
) {
1717 sockets
[sock_id
].in_timeout
= tcp
->tcp_rto
;
1721 if (sockets
[sock_id
].inq
== NULL
) {
1722 /* Go out and check the wire */
1723 for (i
= MEDIA_LVL
; i
< TRANSPORT_LVL
; i
++) {
1724 if (sockets
[sock_id
].input
[i
] != NULL
) {
1725 if (sockets
[sock_id
].input
[i
](sock_id
) < 0) {
1727 sockets
[sock_id
].in_timeout
=
1736 while ((in_gram
= sockets
[sock_id
].inq
) != NULL
) {
1737 if (tcp
!= NULL
&& tcp
->tcp_state
== state
)
1740 /* Remove unknown inetgrams from the head of inq. */
1741 if (in_gram
->igm_level
!= TRANSPORT_LVL
) {
1743 printf("tcp_state_wait for state %d: unexpected "
1744 "packet level %d frame found\n", state
,
1745 in_gram
->igm_level
);
1747 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_TRUE
);
1750 mp
= in_gram
->igm_mp
;
1751 del_gram(&sockets
[sock_id
].inq
, in_gram
, B_FALSE
);
1752 bkmem_free((caddr_t
)in_gram
, sizeof (struct inetgram
));
1753 tcp_rput_data(tcp
, mp
, sock_id
);
1756 * The other side may have closed this connection or
1757 * RST us. But we need to continue to process other
1758 * packets in the socket's queue because they may be
1759 * belong to another TCP connections.
1761 if (sockets
[sock_id
].pcb
== NULL
) {
1766 /* If the other side has closed the connection, just return. */
1767 if (tcp
== NULL
|| sockets
[sock_id
].pcb
== NULL
) {
1769 printf("tcp_state_wait other side dead: state %d "
1770 "error %d\n", state
, sockets
[sock_id
].so_error
);
1772 if (sockets
[sock_id
].so_error
!= 0)
1778 * TCPS_ALL_ACKED is not a valid TCP state, it is just used as an
1779 * indicator to tcp_state_wait to mean that it is being called
1780 * to wait till we have received acks for all the new segments sent.
1782 if ((state
== TCPS_ALL_ACKED
) && (tcp
->tcp_suna
== tcp
->tcp_snxt
)) {
1785 if (tcp
->tcp_state
!= state
) {
1786 if (prom_gettime() > tcp
->tcp_rto_timeout
)
1787 tcp_timer(tcp
, sock_id
);
1792 sockets
[sock_id
].in_timeout
= timeout
;
1794 tcp_drain_needed(sock_id
, tcp
);
1798 /* Verify the checksum of a segment. */
1800 tcp_verify_cksum(mblk_t
*mp
)
1807 iph
= (struct ip
*)mp
->b_rptr
;
1808 tcph
= (tcpha_t
*)(iph
+ 1);
1809 len
= ntohs(iph
->ip_len
);
1812 * Calculate the TCP checksum. Need to include the psuedo header,
1813 * which is similar to the real IP header starting at the TTL field.
1815 iph
->ip_sum
= htons(len
- IP_SIMPLE_HDR_LENGTH
);
1816 old_sum
= tcph
->tha_sum
;
1819 if (old_sum
== tcp_cksum((uint16_t *)&(iph
->ip_ttl
),
1820 len
- IP_SIMPLE_HDR_LENGTH
+ 12)) {
1828 /* To find a TCP connection matching the incoming segment. */
1830 tcp_lookup_ipv4(struct ip
*iph
, tcpha_t
*tcph
, int min_state
, int *sock_id
)
1835 for (i
= 0; i
< MAXSOCKET
; i
++) {
1836 if (sockets
[i
].type
== INETBOOT_STREAM
&&
1837 (tcp
= (tcp_t
*)sockets
[i
].pcb
) != NULL
) {
1838 if (tcph
->tha_lport
== tcp
->tcp_fport
&&
1839 tcph
->tha_fport
== tcp
->tcp_lport
&&
1840 iph
->ip_src
.s_addr
== tcp
->tcp_remote
&&
1841 iph
->ip_dst
.s_addr
== tcp
->tcp_bound_source
&&
1842 tcp
->tcp_state
>= min_state
) {
1848 /* Find it in the time wait list. */
1849 for (tcp
= tcp_time_wait_head
; tcp
!= NULL
;
1850 tcp
= tcp
->tcp_time_wait_next
) {
1851 if (tcph
->tha_lport
== tcp
->tcp_fport
&&
1852 tcph
->tha_fport
== tcp
->tcp_lport
&&
1853 iph
->ip_src
.s_addr
== tcp
->tcp_remote
&&
1854 iph
->ip_dst
.s_addr
== tcp
->tcp_bound_source
&&
1855 tcp
->tcp_state
>= min_state
) {
1863 /* To find a TCP listening connection matching the incoming segment. */
1865 tcp_lookup_listener_ipv4(in_addr_t addr
, in_port_t port
, int *sock_id
)
1870 for (i
= 0; i
< MAXSOCKET
; i
++) {
1871 if (sockets
[i
].type
== INETBOOT_STREAM
&&
1872 (tcp
= (tcp_t
*)sockets
[i
].pcb
) != NULL
) {
1873 if (tcp
->tcp_lport
== port
&&
1874 (tcp
->tcp_bound_source
== addr
||
1875 tcp
->tcp_bound_source
== INADDR_ANY
)) {
1885 /* To find a TCP eager matching the incoming segment. */
1887 tcp_lookup_eager_ipv4(tcp_t
*listener
, struct ip
*iph
, tcpha_t
*tcph
)
1892 printf("tcp_lookup_eager_ipv4 ###############\n");
1894 for (tcp
= listener
->tcp_eager_next_q
; tcp
!= NULL
;
1895 tcp
= tcp
->tcp_eager_next_q
) {
1896 if (tcph
->tha_lport
== tcp
->tcp_fport
&&
1897 tcph
->tha_fport
== tcp
->tcp_lport
&&
1898 iph
->ip_src
.s_addr
== tcp
->tcp_remote
&&
1899 iph
->ip_dst
.s_addr
== tcp
->tcp_bound_source
) {
1904 for (tcp
= listener
->tcp_eager_next_q0
; tcp
!= listener
;
1905 tcp
= tcp
->tcp_eager_next_q0
) {
1906 if (tcph
->tha_lport
== tcp
->tcp_fport
&&
1907 tcph
->tha_fport
== tcp
->tcp_lport
&&
1908 iph
->ip_src
.s_addr
== tcp
->tcp_remote
&&
1909 iph
->ip_dst
.s_addr
== tcp
->tcp_bound_source
) {
1914 printf("No eager found\n");
1919 /* To destroy a TCP control block. */
1921 tcp_clean_death(int sock_id
, tcp_t
*tcp
, int err
)
1924 if (tcp
->tcp_state
== TCPS_TIME_WAIT
)
1925 tcp_time_wait_remove(tcp
);
1928 sockets
[sock_id
].pcb
= NULL
;
1930 sockets
[sock_id
].so_error
= err
;
1932 bkmem_free((caddr_t
)tcp
, sizeof (tcp_t
));
1936 * tcp_rwnd_set() is called to adjust the receive window to a desired value.
1937 * We do not allow the receive window to shrink. After setting rwnd,
1938 * set the flow control hiwat of the stream.
1940 * This function is called in 2 cases:
1942 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a
1943 * connection (passive open) and in tcp_rput_data() for active connect.
1944 * This is called after tcp_mss_set() when the desired MSS value is known.
1945 * This makes sure that our window size is a mutiple of the other side's
1947 * 2) Handling SO_RCVBUF option.
1949 * It is ASSUMED that the requested size is a multiple of the current MSS.
1951 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
1955 tcp_rwnd_set(tcp_t
*tcp
, uint32_t rwnd
)
1957 uint32_t mss
= tcp
->tcp_mss
;
1958 uint32_t old_max_rwnd
;
1959 uint32_t max_transmittable_rwnd
;
1961 if (tcp
->tcp_rwnd_max
!= 0)
1962 old_max_rwnd
= tcp
->tcp_rwnd_max
;
1964 old_max_rwnd
= tcp
->tcp_rwnd
;
1967 * Insist on a receive window that is at least
1968 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
1969 * funny TCP interactions of Nagle algorithm, SWS avoidance
1970 * and delayed acknowledgement.
1972 rwnd
= MAX(rwnd
, tcp_recv_hiwat_minmss
* mss
);
1975 * If window size info has already been exchanged, TCP should not
1976 * shrink the window. Shrinking window is doable if done carefully.
1977 * We may add that support later. But so far there is not a real
1980 if (rwnd
< old_max_rwnd
&& tcp
->tcp_state
> TCPS_SYN_SENT
) {
1981 /* MSS may have changed, do a round up again. */
1982 rwnd
= MSS_ROUNDUP(old_max_rwnd
, mss
);
1986 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
1987 * can be applied even before the window scale option is decided.
1989 max_transmittable_rwnd
= TCP_MAXWIN
<< tcp
->tcp_rcv_ws
;
1990 if (rwnd
> max_transmittable_rwnd
) {
1991 rwnd
= max_transmittable_rwnd
-
1992 (max_transmittable_rwnd
% mss
);
1994 rwnd
= max_transmittable_rwnd
;
1996 * If we're over the limit we may have to back down tcp_rwnd.
1997 * The increment below won't work for us. So we set all three
1998 * here and the increment below will have no effect.
2000 tcp
->tcp_rwnd
= old_max_rwnd
= rwnd
;
2004 * Increment the current rwnd by the amount the maximum grew (we
2005 * can not overwrite it since we might be in the middle of a
2008 tcp
->tcp_rwnd
+= rwnd
- old_max_rwnd
;
2009 U32_TO_ABE16(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
, tcp
->tcp_tcph
->th_win
);
2010 if ((tcp
->tcp_rcv_ws
> 0) && rwnd
> tcp
->tcp_cwnd_max
)
2011 tcp
->tcp_cwnd_max
= rwnd
;
2012 tcp
->tcp_rwnd_max
= rwnd
;
2018 * Extract option values from a tcp header. We put any found values into the
2019 * tcpopt struct and return a bitmask saying which options were found.
2022 tcp_parse_options(tcph_t
*tcph
, tcp_opt_t
*tcpopt
)
2027 uchar_t
*up
= (uchar_t
*)tcph
;
2030 tcp_seq sack_begin
, sack_end
;
2033 endp
= up
+ TCP_HDR_LENGTH(tcph
);
2034 up
+= TCP_MIN_HEADER_LENGTH
;
2046 if (len
< TCPOPT_MAXSEG_LEN
||
2047 up
[1] != TCPOPT_MAXSEG_LEN
)
2050 mss
= BE16_TO_U16(up
+2);
2051 /* Caller must handle tcp_mss_min and tcp_mss_max_* */
2052 tcpopt
->tcp_opt_mss
= mss
;
2053 found
|= TCP_OPT_MSS_PRESENT
;
2055 up
+= TCPOPT_MAXSEG_LEN
;
2059 if (len
< TCPOPT_WS_LEN
|| up
[1] != TCPOPT_WS_LEN
)
2062 if (up
[2] > TCP_MAX_WINSHIFT
)
2063 tcpopt
->tcp_opt_wscale
= TCP_MAX_WINSHIFT
;
2065 tcpopt
->tcp_opt_wscale
= up
[2];
2066 found
|= TCP_OPT_WSCALE_PRESENT
;
2068 up
+= TCPOPT_WS_LEN
;
2071 case TCPOPT_SACK_PERMITTED
:
2072 if (len
< TCPOPT_SACK_OK_LEN
||
2073 up
[1] != TCPOPT_SACK_OK_LEN
)
2075 found
|= TCP_OPT_SACK_OK_PRESENT
;
2076 up
+= TCPOPT_SACK_OK_LEN
;
2080 if (len
<= 2 || up
[1] <= 2 || len
< up
[1])
2083 /* If TCP is not interested in SACK blks... */
2084 if ((tcp
= tcpopt
->tcp
) == NULL
) {
2088 sack_len
= up
[1] - TCPOPT_HEADER_LEN
;
2089 up
+= TCPOPT_HEADER_LEN
;
2092 * If the list is empty, allocate one and assume
2093 * nothing is sack'ed.
2095 assert(tcp
->tcp_sack_info
!= NULL
);
2096 if (tcp
->tcp_notsack_list
== NULL
) {
2097 tcp_notsack_update(&(tcp
->tcp_notsack_list
),
2098 tcp
->tcp_suna
, tcp
->tcp_snxt
,
2099 &(tcp
->tcp_num_notsack_blk
),
2100 &(tcp
->tcp_cnt_notsack_list
));
2103 * Make sure tcp_notsack_list is not NULL.
2104 * This happens when kmem_alloc(KM_NOSLEEP)
2107 if (tcp
->tcp_notsack_list
== NULL
) {
2111 tcp
->tcp_fack
= tcp
->tcp_suna
;
2114 while (sack_len
> 0) {
2115 if (up
+ 8 > endp
) {
2119 sack_begin
= BE32_TO_U32(up
);
2121 sack_end
= BE32_TO_U32(up
);
2125 * Bounds checking. Make sure the SACK
2126 * info is within tcp_suna and tcp_snxt.
2127 * If this SACK blk is out of bound, ignore
2128 * it but continue to parse the following
2131 if (SEQ_LEQ(sack_end
, sack_begin
) ||
2132 SEQ_LT(sack_begin
, tcp
->tcp_suna
) ||
2133 SEQ_GT(sack_end
, tcp
->tcp_snxt
)) {
2136 tcp_notsack_insert(&(tcp
->tcp_notsack_list
),
2137 sack_begin
, sack_end
,
2138 &(tcp
->tcp_num_notsack_blk
),
2139 &(tcp
->tcp_cnt_notsack_list
));
2140 if (SEQ_GT(sack_end
, tcp
->tcp_fack
)) {
2141 tcp
->tcp_fack
= sack_end
;
2144 found
|= TCP_OPT_SACK_PRESENT
;
2148 if (len
< TCPOPT_TSTAMP_LEN
||
2149 up
[1] != TCPOPT_TSTAMP_LEN
)
2152 tcpopt
->tcp_opt_ts_val
= BE32_TO_U32(up
+2);
2153 tcpopt
->tcp_opt_ts_ecr
= BE32_TO_U32(up
+6);
2155 found
|= TCP_OPT_TSTAMP_PRESENT
;
2157 up
+= TCPOPT_TSTAMP_LEN
;
2161 if (len
<= 1 || len
< (int)up
[1] || up
[1] == 0)
2172 * Set the mss associated with a particular tcp based on its current value,
2173 * and a new one passed in. Observe minimums and maximums, and reset
2174 * other state variables that we want to view as multiples of mss.
2176 * This function is called in various places mainly because
2177 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the
2178 * other side's SYN/SYN-ACK packet arrives.
2179 * 2) PMTUd may get us a new MSS.
2180 * 3) If the other side stops sending us timestamp option, we need to
2181 * increase the MSS size to use the extra bytes available.
2184 tcp_mss_set(tcp_t
*tcp
, uint32_t mss
)
2188 mss_max
= tcp_mss_max_ipv4
;
2190 if (mss
< tcp_mss_min
)
2195 * Unless naglim has been set by our client to
2196 * a non-mss value, force naglim to track mss.
2197 * This can help to aggregate small writes.
2199 if (mss
< tcp
->tcp_naglim
|| tcp
->tcp_mss
== tcp
->tcp_naglim
)
2200 tcp
->tcp_naglim
= mss
;
2202 * TCP should be able to buffer at least 4 MSS data for obvious
2203 * performance reason.
2205 if ((mss
<< 2) > tcp
->tcp_xmit_hiwater
)
2206 tcp
->tcp_xmit_hiwater
= mss
<< 2;
2209 * Initialize cwnd according to draft-floyd-incr-init-win-01.txt.
2210 * Previously, we use tcp_slow_start_initial to control the size
2211 * of the initial cwnd. Now, when tcp_slow_start_initial * mss
2212 * is smaller than the cwnd calculated from the formula suggested in
2213 * the draft, we use tcp_slow_start_initial * mss as the cwnd.
2214 * Otherwise, use the cwnd from the draft's formula. The default
2215 * of tcp_slow_start_initial is 2.
2217 tcp
->tcp_cwnd
= MIN(tcp_slow_start_initial
* mss
,
2218 MIN(4 * mss
, MAX(2 * mss
, 4380 / mss
* mss
)));
2219 tcp
->tcp_cwnd_cnt
= 0;
2223 * Process all TCP option in SYN segment.
2225 * This function sets up the correct tcp_mss value according to the
2226 * MSS option value and our header size. It also sets up the window scale
2227 * and timestamp values, and initialize SACK info blocks. But it does not
2228 * change receive window size after setting the tcp_mss value. The caller
2229 * should do the appropriate change.
2232 tcp_process_options(tcp_t
*tcp
, tcph_t
*tcph
)
2240 options
= tcp_parse_options(tcph
, &tcpopt
);
2243 * Process MSS option. Note that MSS option value does not account
2244 * for IP or TCP options. This means that it is equal to MTU - minimum
2245 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
2248 if (!(options
& TCP_OPT_MSS_PRESENT
)) {
2249 tcpopt
.tcp_opt_mss
= tcp_mss_def_ipv4
;
2251 if (tcp
->tcp_ipversion
== IPV4_VERSION
)
2252 mss_max
= tcp_mss_max_ipv4
;
2253 if (tcpopt
.tcp_opt_mss
< tcp_mss_min
)
2254 tcpopt
.tcp_opt_mss
= tcp_mss_min
;
2255 else if (tcpopt
.tcp_opt_mss
> mss_max
)
2256 tcpopt
.tcp_opt_mss
= mss_max
;
2259 /* Process Window Scale option. */
2260 if (options
& TCP_OPT_WSCALE_PRESENT
) {
2261 tcp
->tcp_snd_ws
= tcpopt
.tcp_opt_wscale
;
2262 tcp
->tcp_snd_ws_ok
= B_TRUE
;
2264 tcp
->tcp_snd_ws
= B_FALSE
;
2265 tcp
->tcp_snd_ws_ok
= B_FALSE
;
2266 tcp
->tcp_rcv_ws
= B_FALSE
;
2269 /* Process Timestamp option. */
2270 if ((options
& TCP_OPT_TSTAMP_PRESENT
) &&
2271 (tcp
->tcp_snd_ts_ok
|| !tcp
->tcp_active_open
)) {
2272 tmp_tcph
= (char *)tcp
->tcp_tcph
;
2274 tcp
->tcp_snd_ts_ok
= B_TRUE
;
2275 tcp
->tcp_ts_recent
= tcpopt
.tcp_opt_ts_val
;
2276 tcp
->tcp_last_rcv_lbolt
= prom_gettime();
2277 assert(OK_32PTR(tmp_tcph
));
2278 assert(tcp
->tcp_tcp_hdr_len
== TCP_MIN_HEADER_LENGTH
);
2280 /* Fill in our template header with basic timestamp option. */
2281 tmp_tcph
+= tcp
->tcp_tcp_hdr_len
;
2282 tmp_tcph
[0] = TCPOPT_NOP
;
2283 tmp_tcph
[1] = TCPOPT_NOP
;
2284 tmp_tcph
[2] = TCPOPT_TSTAMP
;
2285 tmp_tcph
[3] = TCPOPT_TSTAMP_LEN
;
2286 tcp
->tcp_hdr_len
+= TCPOPT_REAL_TS_LEN
;
2287 tcp
->tcp_tcp_hdr_len
+= TCPOPT_REAL_TS_LEN
;
2288 tcp
->tcp_tcph
->th_offset_and_rsrvd
[0] += (3 << 4);
2290 tcp
->tcp_snd_ts_ok
= B_FALSE
;
2294 * Process SACK options. If SACK is enabled for this connection,
2295 * then allocate the SACK info structure.
2297 if ((options
& TCP_OPT_SACK_OK_PRESENT
) &&
2298 (tcp
->tcp_snd_sack_ok
||
2299 (tcp_sack_permitted
!= 0 && !tcp
->tcp_active_open
))) {
2300 /* This should be true only in the passive case. */
2301 if (tcp
->tcp_sack_info
== NULL
) {
2302 tcp
->tcp_sack_info
= (tcp_sack_info_t
*)bkmem_zalloc(
2303 sizeof (tcp_sack_info_t
));
2305 if (tcp
->tcp_sack_info
== NULL
) {
2306 tcp
->tcp_snd_sack_ok
= B_FALSE
;
2308 tcp
->tcp_snd_sack_ok
= B_TRUE
;
2309 if (tcp
->tcp_snd_ts_ok
) {
2310 tcp
->tcp_max_sack_blk
= 3;
2312 tcp
->tcp_max_sack_blk
= 4;
2317 * Resetting tcp_snd_sack_ok to B_FALSE so that
2318 * no SACK info will be used for this
2319 * connection. This assumes that SACK usage
2320 * permission is negotiated. This may need
2321 * to be changed once this is clarified.
2323 if (tcp
->tcp_sack_info
!= NULL
) {
2324 bkmem_free((caddr_t
)tcp
->tcp_sack_info
,
2325 sizeof (tcp_sack_info_t
));
2326 tcp
->tcp_sack_info
= NULL
;
2328 tcp
->tcp_snd_sack_ok
= B_FALSE
;
2332 * Now we know the exact TCP/IP header length, subtract
2333 * that from tcp_mss to get our side's MSS.
2335 tcp
->tcp_mss
-= tcp
->tcp_hdr_len
;
2337 * Here we assume that the other side's header size will be equal to
2338 * our header size. We calculate the real MSS accordingly. Need to
2339 * take into additional stuffs IPsec puts in.
2341 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
2343 tcpopt
.tcp_opt_mss
-= tcp
->tcp_hdr_len
-
2344 (IP_SIMPLE_HDR_LENGTH
+ TCP_MIN_HEADER_LENGTH
);
2347 * Set MSS to the smaller one of both ends of the connection.
2348 * We should not have called tcp_mss_set() before, but our
2349 * side of the MSS should have been set to a proper value
2350 * by tcp_adapt_ire(). tcp_mss_set() will also set up the
2351 * STREAM head parameters properly.
2353 * If we have a larger-than-16-bit window but the other side
2354 * didn't want to do window scale, tcp_rwnd_set() will take
2357 tcp_mss_set(tcp
, MIN(tcpopt
.tcp_opt_mss
, tcp
->tcp_mss
));
2361 * This function does PAWS protection check. Returns B_TRUE if the
2362 * segment passes the PAWS test, else returns B_FALSE.
2365 tcp_paws_check(tcp_t
*tcp
, tcph_t
*tcph
, tcp_opt_t
*tcpoptp
)
2371 flags
= (unsigned int)tcph
->th_flags
[0] & 0xFF;
2373 * If timestamp option is aligned nicely, get values inline,
2374 * otherwise call general routine to parse. Only do that
2375 * if timestamp is the only option.
2377 if (TCP_HDR_LENGTH(tcph
) == (uint32_t)TCP_MIN_HEADER_LENGTH
+
2378 TCPOPT_REAL_TS_LEN
&&
2379 OK_32PTR((up
= ((uint8_t *)tcph
) +
2380 TCP_MIN_HEADER_LENGTH
)) &&
2381 *(uint32_t *)up
== TCPOPT_NOP_NOP_TSTAMP
) {
2382 tcpoptp
->tcp_opt_ts_val
= ABE32_TO_U32((up
+4));
2383 tcpoptp
->tcp_opt_ts_ecr
= ABE32_TO_U32((up
+8));
2385 options
= TCP_OPT_TSTAMP_PRESENT
;
2387 if (tcp
->tcp_snd_sack_ok
) {
2390 tcpoptp
->tcp
= NULL
;
2392 options
= tcp_parse_options(tcph
, tcpoptp
);
2395 if (options
& TCP_OPT_TSTAMP_PRESENT
) {
2397 * Do PAWS per RFC 1323 section 4.2. Accept RST
2398 * regardless of the timestamp, page 18 RFC 1323.bis.
2400 if ((flags
& TH_RST
) == 0 &&
2401 TSTMP_LT(tcpoptp
->tcp_opt_ts_val
,
2402 tcp
->tcp_ts_recent
)) {
2403 if (TSTMP_LT(prom_gettime(),
2404 tcp
->tcp_last_rcv_lbolt
+ PAWS_TIMEOUT
)) {
2405 /* This segment is not acceptable. */
2409 * Connection has been idle for
2410 * too long. Reset the timestamp
2411 * and assume the segment is valid.
2413 tcp
->tcp_ts_recent
=
2414 tcpoptp
->tcp_opt_ts_val
;
2419 * If we don't get a timestamp on every packet, we
2420 * figure we can't really trust 'em, so we stop sending
2423 tcp
->tcp_snd_ts_ok
= B_FALSE
;
2425 tcp
->tcp_hdr_len
-= TCPOPT_REAL_TS_LEN
;
2426 tcp
->tcp_tcp_hdr_len
-= TCPOPT_REAL_TS_LEN
;
2427 tcp
->tcp_tcph
->th_offset_and_rsrvd
[0] -= (3 << 4);
2428 tcp_mss_set(tcp
, tcp
->tcp_mss
+ TCPOPT_REAL_TS_LEN
);
2429 if (tcp
->tcp_snd_sack_ok
) {
2430 assert(tcp
->tcp_sack_info
!= NULL
);
2431 tcp
->tcp_max_sack_blk
= 4;
2438 * tcp_get_seg_mp() is called to get the pointer to a segment in the
2439 * send queue which starts at the given seq. no.
2442 * tcp_t *tcp: the tcp instance pointer.
2443 * uint32_t seq: the starting seq. no of the requested segment.
2444 * int32_t *off: after the execution, *off will be the offset to
2445 * the returned mblk which points to the requested seq no.
2448 * A mblk_t pointer pointing to the requested segment in send queue.
2451 tcp_get_seg_mp(tcp_t
*tcp
, uint32_t seq
, int32_t *off
)
2456 /* Defensive coding. Make sure we don't send incorrect data. */
2457 if (SEQ_LT(seq
, tcp
->tcp_suna
) || SEQ_GEQ(seq
, tcp
->tcp_snxt
) ||
2461 cnt
= seq
- tcp
->tcp_suna
;
2462 mp
= tcp
->tcp_xmit_head
;
2463 while (cnt
> 0 && mp
) {
2464 cnt
-= mp
->b_wptr
- mp
->b_rptr
;
2466 cnt
+= mp
->b_wptr
- mp
->b_rptr
;
2477 * This function handles all retransmissions if SACK is enabled for this
2478 * connection. First it calculates how many segments can be retransmitted
2479 * based on tcp_pipe. Then it goes thru the notsack list to find eligible
2480 * segments. A segment is eligible if sack_cnt for that segment is greater
2481 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
2482 * all eligible segments, it checks to see if TCP can send some new segments
2483 * (fast recovery). If it can, it returns 1. Otherwise it returns 0.
2486 * tcp_t *tcp: the tcp structure of the connection.
2489 * 1 if the pipe is not full (new data can be sent), 0 otherwise
2492 tcp_sack_rxmit(tcp_t
*tcp
, int sock_id
)
2494 notsack_blk_t
*notsack_blk
;
2495 int32_t usable_swnd
;
2500 assert(tcp
->tcp_sack_info
!= NULL
);
2501 assert(tcp
->tcp_notsack_list
!= NULL
);
2502 assert(tcp
->tcp_rexmit
== B_FALSE
);
2504 /* Defensive coding in case there is a bug... */
2505 if (tcp
->tcp_notsack_list
== NULL
) {
2508 notsack_blk
= tcp
->tcp_notsack_list
;
2512 * Limit the num of outstanding data in the network to be
2513 * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
2515 usable_swnd
= tcp
->tcp_cwnd_ssthresh
- tcp
->tcp_pipe
;
2517 /* At least retransmit 1 MSS of data. */
2518 if (usable_swnd
<= 0) {
2522 /* Make sure no new RTT samples will be taken. */
2523 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
2525 notsack_blk
= tcp
->tcp_notsack_list
;
2526 while (usable_swnd
> 0) {
2527 mblk_t
*snxt_mp
, *tmp_mp
;
2528 tcp_seq begin
= tcp
->tcp_sack_snxt
;
2532 for (; notsack_blk
!= NULL
; notsack_blk
= notsack_blk
->next
) {
2533 if (SEQ_GT(notsack_blk
->end
, begin
) &&
2534 (notsack_blk
->sack_cnt
>=
2535 tcp_dupack_fast_retransmit
)) {
2536 end
= notsack_blk
->end
;
2537 if (SEQ_LT(begin
, notsack_blk
->begin
)) {
2538 begin
= notsack_blk
->begin
;
2544 * All holes are filled. Manipulate tcp_cwnd to send more
2545 * if we can. Note that after the SACK recovery, tcp_cwnd is
2546 * set to tcp_cwnd_ssthresh.
2548 if (notsack_blk
== NULL
) {
2549 usable_swnd
= tcp
->tcp_cwnd_ssthresh
- tcp
->tcp_pipe
;
2550 if (usable_swnd
<= 0) {
2551 tcp
->tcp_cwnd
= tcp
->tcp_snxt
- tcp
->tcp_suna
;
2552 assert(tcp
->tcp_cwnd
> 0);
2555 usable_swnd
= usable_swnd
/ mss
;
2556 tcp
->tcp_cwnd
= tcp
->tcp_snxt
- tcp
->tcp_suna
+
2557 MAX(usable_swnd
* mss
, mss
);
2563 * Note that we may send more than usable_swnd allows here
2564 * because of round off, but no more than 1 MSS of data.
2566 seg_len
= end
- begin
;
2569 snxt_mp
= tcp_get_seg_mp(tcp
, begin
, &off
);
2570 assert(snxt_mp
!= NULL
);
2571 /* This should not happen. Defensive coding again... */
2572 if (snxt_mp
== NULL
) {
2576 xmit_mp
= tcp_xmit_mp(tcp
, snxt_mp
, seg_len
, &off
,
2577 &tmp_mp
, begin
, B_TRUE
, &seg_len
, B_TRUE
);
2579 if (xmit_mp
== NULL
)
2582 usable_swnd
-= seg_len
;
2583 tcp
->tcp_pipe
+= seg_len
;
2584 tcp
->tcp_sack_snxt
= begin
+ seg_len
;
2585 TCP_DUMP_PACKET("tcp_sack_rxmit", xmit_mp
);
2586 (void) ipv4_tcp_output(sock_id
, xmit_mp
);
2590 * Update the send timestamp to avoid false retransmission.
2591 * Note. use uintptr_t to suppress the gcc warning.
2593 snxt_mp
->b_prev
= (mblk_t
*)(uintptr_t)prom_gettime();
2595 BUMP_MIB(tcp_mib
.tcpRetransSegs
);
2596 UPDATE_MIB(tcp_mib
.tcpRetransBytes
, seg_len
);
2597 BUMP_MIB(tcp_mib
.tcpOutSackRetransSegs
);
2599 * Update tcp_rexmit_max to extend this SACK recovery phase.
2600 * This happens when new data sent during fast recovery is
2601 * also lost. If TCP retransmits those new data, it needs
2602 * to extend SACK recover phase to avoid starting another
2603 * fast retransmit/recovery unnecessarily.
2605 if (SEQ_GT(tcp
->tcp_sack_snxt
, tcp
->tcp_rexmit_max
)) {
2606 tcp
->tcp_rexmit_max
= tcp
->tcp_sack_snxt
;
2613 tcp_rput_data(tcp_t
*tcp
, mblk_t
*mp
, int sock_id
)
2625 uint32_t new_swnd
= 0;
2627 boolean_t ofo_seg
= B_FALSE
; /* Out of order segment */
2631 int32_t bytes_acked
;
2637 printf("tcp_rput_data sock %d mp %x mp_datap %x #################\n",
2638 sock_id
, mp
, mp
->b_datap
);
2641 /* Dump the packet when debugging. */
2642 TCP_DUMP_PACKET("tcp_rput_data", mp
);
2644 assert(OK_32PTR(mp
->b_rptr
));
2647 iph
= (struct ip
*)rptr
;
2648 ip_hdr_len
= IPH_HDR_LENGTH(rptr
);
2649 if (ip_hdr_len
!= IP_SIMPLE_HDR_LENGTH
) {
2651 printf("Not simple IP header\n");
2653 /* We cannot handle IP option yet... */
2658 /* The TCP header must be aligned. */
2659 tcph
= (tcpha_t
*)&rptr
[ip_hdr_len
];
2660 seg_seq
= ntohl(tcph
->tha_seq
);
2661 seg_ack
= ntohl(tcph
->tha_ack
);
2662 assert((uintptr_t)(mp
->b_wptr
- rptr
) <= (uintptr_t)INT_MAX
);
2663 seg_len
= (int)(mp
->b_wptr
- rptr
) -
2664 (ip_hdr_len
+ TCP_HDR_LENGTH(((tcph_t
*)tcph
)));
2665 /* In inetboot, b_cont should always be NULL. */
2666 assert(mp
->b_cont
== NULL
);
2668 /* Verify the checksum. */
2669 if (tcp_verify_cksum(mp
) < 0) {
2671 printf("tcp_rput_data: wrong cksum\n");
2678 * This segment is not for us, try to find its
2679 * intended receiver.
2682 tcph
->tha_lport
!= tcp
->tcp_fport
||
2683 tcph
->tha_fport
!= tcp
->tcp_lport
||
2684 iph
->ip_src
.s_addr
!= tcp
->tcp_remote
||
2685 iph
->ip_dst
.s_addr
!= tcp
->tcp_bound_source
) {
2687 printf("tcp_rput_data: not for us, state %d\n",
2691 * First try to find a established connection. If none
2692 * is found, look for a listener.
2694 * If a listener is found, we need to check to see if the
2695 * incoming segment is for one of its eagers. If it is,
2696 * give it to the eager. If not, listener should take care
2699 if ((tcp1
= tcp_lookup_ipv4(iph
, tcph
, TCPS_SYN_SENT
,
2700 &sock_id
)) != NULL
||
2701 (tcp1
= tcp_lookup_listener_ipv4(iph
->ip_dst
.s_addr
,
2702 tcph
->tha_fport
, &sock_id
)) != NULL
) {
2703 if (tcp1
->tcp_state
== TCPS_LISTEN
) {
2704 if ((tcp
= tcp_lookup_eager_ipv4(tcp1
,
2705 iph
, tcph
)) == NULL
) {
2706 /* No eager... sent to listener */
2708 printf("found the listener: %s\n",
2709 tcp_display(tcp1
, NULL
,
2710 DISP_ADDR_AND_PORT
));
2716 printf("found the eager: %s\n",
2717 tcp_display(tcp
, NULL
,
2718 DISP_ADDR_AND_PORT
));
2722 /* Non listener found... */
2724 printf("found the connection: %s\n",
2725 tcp_display(tcp1
, NULL
,
2726 DISP_ADDR_AND_PORT
));
2732 * No connection for this segment...
2733 * Send a RST to the other side.
2735 tcp_xmit_listeners_reset(sock_id
, mp
, ip_hdr_len
);
2740 flags
= tcph
->tha_flags
& 0xFF;
2741 BUMP_MIB(tcp_mib
.tcpInSegs
);
2742 if (tcp
->tcp_state
== TCPS_TIME_WAIT
) {
2743 tcp_time_wait_processing(tcp
, mp
, seg_seq
, seg_ack
,
2744 seg_len
, (tcph_t
*)tcph
, sock_id
);
2748 * From this point we can assume that the tcp is not compressed,
2749 * since we would have branched off to tcp_time_wait_processing()
2752 assert(tcp
!= NULL
&& tcp
->tcp_state
!= TCPS_TIME_WAIT
);
2755 * After this point, we know we have the correct TCP, so update
2758 tcp
->tcp_last_recv_time
= prom_gettime();
2760 /* In inetboot, we do not handle urgent pointer... */
2761 if (flags
& TH_URG
) {
2763 DEBUG_1("tcp_rput_data(%d): received segment with urgent "
2764 "pointer\n", sock_id
);
2769 switch (tcp
->tcp_state
) {
2771 if ((flags
& (TH_RST
| TH_ACK
| TH_SYN
)) != TH_SYN
) {
2772 if (flags
& TH_RST
) {
2776 if (flags
& TH_ACK
) {
2777 tcp_xmit_early_reset("TCPS_LISTEN-TH_ACK",
2778 sock_id
, mp
, seg_ack
, 0, TH_RST
,
2782 if (!(flags
& TH_SYN
)) {
2786 printf("tcp_rput_data: %d\n", __LINE__
);
2787 prom_panic("inetboot");
2789 if (tcp
->tcp_conn_req_max
> 0) {
2790 tcp
= tcp_conn_request(tcp
, mp
, sock_id
, ip_hdr_len
);
2796 printf("tcp_rput_data: new tcp created\n");
2799 tcp
->tcp_irs
= seg_seq
;
2800 tcp
->tcp_rack
= seg_seq
;
2801 tcp
->tcp_rnxt
= seg_seq
+ 1;
2802 U32_TO_ABE32(tcp
->tcp_rnxt
, tcp
->tcp_tcph
->th_ack
);
2803 BUMP_MIB(tcp_mib
.tcpPassiveOpens
);
2806 if (flags
& TH_ACK
) {
2808 * Note that our stack cannot send data before a
2809 * connection is established, therefore the
2810 * following check is valid. Otherwise, it has
2813 if (SEQ_LEQ(seg_ack
, tcp
->tcp_iss
) ||
2814 SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
2815 if (flags
& TH_RST
) {
2819 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
2820 tcp
, mp
, seg_ack
, 0, TH_RST
,
2821 ip_hdr_len
, sock_id
);
2824 assert(tcp
->tcp_suna
+ 1 == seg_ack
);
2826 if (flags
& TH_RST
) {
2828 if (flags
& TH_ACK
) {
2829 tcp_clean_death(sock_id
, tcp
, ECONNREFUSED
);
2833 if (!(flags
& TH_SYN
)) {
2838 /* Process all TCP options. */
2839 tcp_process_options(tcp
, (tcph_t
*)tcph
);
2841 * The following changes our rwnd to be a multiple of the
2842 * MIN(peer MSS, our MSS) for performance reason.
2844 (void) tcp_rwnd_set(tcp
, MSS_ROUNDUP(tcp
->tcp_rwnd
,
2847 /* Is the other end ECN capable? */
2848 if (tcp
->tcp_ecn_ok
) {
2849 if ((flags
& (TH_ECE
|TH_CWR
)) != TH_ECE
) {
2850 tcp
->tcp_ecn_ok
= B_FALSE
;
2854 * Clear ECN flags because it may interfere with later
2857 flags
&= ~(TH_ECE
|TH_CWR
);
2859 tcp
->tcp_irs
= seg_seq
;
2860 tcp
->tcp_rack
= seg_seq
;
2861 tcp
->tcp_rnxt
= seg_seq
+ 1;
2862 U32_TO_ABE32(tcp
->tcp_rnxt
, tcp
->tcp_tcph
->th_ack
);
2864 if (flags
& TH_ACK
) {
2865 /* One for the SYN */
2866 tcp
->tcp_suna
= tcp
->tcp_iss
+ 1;
2867 tcp
->tcp_valid_bits
&= ~TCP_ISS_VALID
;
2868 tcp
->tcp_state
= TCPS_ESTABLISHED
;
2871 * If SYN was retransmitted, need to reset all
2872 * retransmission info. This is because this
2873 * segment will be treated as a dup ACK.
2875 if (tcp
->tcp_rexmit
) {
2876 tcp
->tcp_rexmit
= B_FALSE
;
2877 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
2878 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
2879 tcp
->tcp_snd_burst
= TCP_CWND_NORMAL
;
2882 * Set tcp_cwnd back to 1 MSS, per
2883 * recommendation from
2884 * draft-floyd-incr-init-win-01.txt,
2885 * Increasing TCP's Initial Window.
2887 tcp
->tcp_cwnd
= tcp
->tcp_mss
;
2890 tcp
->tcp_swl1
= seg_seq
;
2891 tcp
->tcp_swl2
= seg_ack
;
2893 new_swnd
= BE16_TO_U16(((tcph_t
*)tcph
)->th_win
);
2894 tcp
->tcp_swnd
= new_swnd
;
2895 if (new_swnd
> tcp
->tcp_max_swnd
)
2896 tcp
->tcp_max_swnd
= new_swnd
;
2899 * Always send the three-way handshake ack immediately
2900 * in order to make the connection complete as soon as
2901 * possible on the accepting host.
2903 flags
|= TH_ACK_NEEDED
;
2905 * Check to see if there is data to be sent. If
2906 * yes, set the transmit flag. Then check to see
2907 * if received data processing needs to be done.
2908 * If not, go straight to xmit_check. This short
2909 * cut is OK as we don't support T/TCP.
2911 if (tcp
->tcp_unsent
)
2912 flags
|= TH_XMIT_NEEDED
;
2924 tcp
->tcp_state
= TCPS_SYN_RCVD
;
2925 mp1
= tcp_xmit_mp(tcp
, tcp
->tcp_xmit_head
, tcp
->tcp_mss
,
2926 NULL
, NULL
, tcp
->tcp_iss
, B_FALSE
, NULL
, B_FALSE
);
2928 TCP_DUMP_PACKET("tcp_rput_data replying SYN", mp1
);
2929 (void) ipv4_tcp_output(sock_id
, mp1
);
2930 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
2933 * Let's wait till our SYN has been ACKED since we
2934 * don't have a timer.
2936 if (tcp_state_wait(sock_id
, tcp
, TCPS_ALL_ACKED
) < 0) {
2946 mp
->b_rptr
= (uchar_t
*)tcph
+ TCP_HDR_LENGTH((tcph_t
*)tcph
);
2947 new_swnd
= ntohs(tcph
->tha_win
) <<
2948 ((flags
& TH_SYN
) ? 0 : tcp
->tcp_snd_ws
);
2951 if (tcp
->tcp_snd_ts_ok
) {
2952 if (!tcp_paws_check(tcp
, (tcph_t
*)tcph
, &tcpopt
)) {
2954 * This segment is not acceptable.
2955 * Drop it and send back an ACK.
2958 flags
|= TH_ACK_NEEDED
;
2961 } else if (tcp
->tcp_snd_sack_ok
) {
2962 assert(tcp
->tcp_sack_info
!= NULL
);
2965 * SACK info in already updated in tcp_parse_options. Ignore
2966 * all other TCP options...
2968 (void) tcp_parse_options((tcph_t
*)tcph
, &tcpopt
);
2971 gap
= seg_seq
- tcp
->tcp_rnxt
;
2972 rgap
= tcp
->tcp_rwnd
- (gap
+ seg_len
);
2974 * gap is the amount of sequence space between what we expect to see
2975 * and what we got for seg_seq. A positive value for gap means
2976 * something got lost. A negative value means we got some old stuff.
2979 /* Old stuff present. Is the SYN in there? */
2980 if (seg_seq
== tcp
->tcp_irs
&& (flags
& TH_SYN
) &&
2984 /* Recompute the gaps after noting the SYN. */
2987 BUMP_MIB(tcp_mib
.tcpInDataDupSegs
);
2988 UPDATE_MIB(tcp_mib
.tcpInDataDupBytes
,
2989 (seg_len
> -gap
? -gap
: seg_len
));
2990 /* Remove the old stuff from seg_len. */
2994 * Make sure to check for unack'd FIN when rest of data
2995 * has been previously ack'd.
2997 if (seg_len
< 0 || (seg_len
== 0 && !(flags
& TH_FIN
))) {
2999 * Resets are only valid if they lie within our offered
3000 * window. If the RST bit is set, we just ignore this
3003 if (flags
& TH_RST
) {
3009 * This segment is "unacceptable". None of its
3010 * sequence space lies within our advertized window.
3012 * Adjust seg_len to the original value for tracing.
3016 printf("tcp_rput: unacceptable, gap %d, rgap "
3017 "%d, flags 0x%x, seg_seq %u, seg_ack %u, "
3018 "seg_len %d, rnxt %u, snxt %u, %s",
3019 gap
, rgap
, flags
, seg_seq
, seg_ack
,
3020 seg_len
, tcp
->tcp_rnxt
, tcp
->tcp_snxt
,
3021 tcp_display(tcp
, NULL
, DISP_ADDR_AND_PORT
));
3025 * Arrange to send an ACK in response to the
3026 * unacceptable segment per RFC 793 page 69. There
3027 * is only one small difference between ours and the
3028 * acceptability test in the RFC - we accept ACK-only
3029 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
3030 * will be generated.
3032 * Note that we have to ACK an ACK-only packet at least
3033 * for stacks that send 0-length keep-alives with
3034 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
3035 * section 4.2.3.6. As long as we don't ever generate
3036 * an unacceptable packet in response to an incoming
3037 * packet that is unacceptable, it should not cause
3040 flags
|= TH_ACK_NEEDED
;
3043 * Continue processing this segment in order to use the
3044 * ACK information it contains, but skip all other
3045 * sequence-number processing. Processing the ACK
3046 * information is necessary in order to
3047 * re-synchronize connections that may have lost
3050 * We clear seg_len and flag fields related to
3051 * sequence number processing as they are not
3052 * to be trusted for an unacceptable segment.
3055 flags
&= ~(TH_SYN
| TH_FIN
| TH_URG
);
3059 /* Fix seg_seq, and chew the gap off the front. */
3060 seg_seq
= tcp
->tcp_rnxt
;
3063 assert((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
3064 (uintptr_t)UINT_MAX
);
3065 gap
+= (uint_t
)(mp
->b_wptr
- mp
->b_rptr
);
3067 mp
->b_rptr
= mp
->b_wptr
- gap
;
3076 * rgap is the amount of stuff received out of window. A negative
3077 * value is the amount out of window.
3082 if (tcp
->tcp_rwnd
== 0)
3083 BUMP_MIB(tcp_mib
.tcpInWinProbe
);
3085 BUMP_MIB(tcp_mib
.tcpInDataPastWinSegs
);
3086 UPDATE_MIB(tcp_mib
.tcpInDataPastWinBytes
, -rgap
);
3090 * seg_len does not include the FIN, so if more than
3091 * just the FIN is out of window, we act like we don't
3092 * see it. (If just the FIN is out of window, rgap
3093 * will be zero and we will go ahead and acknowledge
3098 /* Fix seg_len and make sure there is something left. */
3102 * Resets are only valid if they lie within our offered
3103 * window. If the RST bit is set, we just ignore this
3106 if (flags
& TH_RST
) {
3111 /* Per RFC 793, we need to send back an ACK. */
3112 flags
|= TH_ACK_NEEDED
;
3115 * If this is a zero window probe, continue to
3116 * process the ACK part. But we need to set seg_len
3117 * to 0 to avoid data processing. Otherwise just
3118 * drop the segment and send back an ACK.
3120 if (tcp
->tcp_rwnd
== 0 && seg_seq
== tcp
->tcp_rnxt
) {
3121 flags
&= ~(TH_SYN
| TH_URG
);
3123 /* Let's see if we can update our rwnd */
3124 tcp_rcv_drain(sock_id
, tcp
);
3131 /* Pitch out of window stuff off the end. */
3135 assert((uintptr_t)(mp2
->b_wptr
-
3136 mp2
->b_rptr
) <= (uintptr_t)INT_MAX
);
3137 rgap
-= (int)(mp2
->b_wptr
- mp2
->b_rptr
);
3139 mp2
->b_wptr
+= rgap
;
3140 if ((mp1
= mp2
->b_cont
) != NULL
) {
3146 } while ((mp2
= mp2
->b_cont
) != NULL
);
3150 * TCP should check ECN info for segments inside the window only.
3151 * Therefore the check should be done here.
3153 if (tcp
->tcp_ecn_ok
) {
3154 uchar_t tos
= ((struct ip
*)rptr
)->ip_tos
;
3156 if (flags
& TH_CWR
) {
3157 tcp
->tcp_ecn_echo_on
= B_FALSE
;
3160 * Note that both ECN_CE and CWR can be set in the
3161 * same segment. In this case, we once again turn
3164 if ((tos
& IPH_ECN_CE
) == IPH_ECN_CE
) {
3165 tcp
->tcp_ecn_echo_on
= B_TRUE
;
3170 * Check whether we can update tcp_ts_recent. This test is
3171 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
3172 * Extensions for High Performance: An Update", Internet Draft.
3174 if (tcp
->tcp_snd_ts_ok
&&
3175 TSTMP_GEQ(tcpopt
.tcp_opt_ts_val
, tcp
->tcp_ts_recent
) &&
3176 SEQ_LEQ(seg_seq
, tcp
->tcp_rack
)) {
3177 tcp
->tcp_ts_recent
= tcpopt
.tcp_opt_ts_val
;
3178 tcp
->tcp_last_rcv_lbolt
= prom_gettime();
3181 if (seg_seq
!= tcp
->tcp_rnxt
|| tcp
->tcp_reass_head
) {
3183 * FIN in an out of order segment. We record this in
3184 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
3185 * Clear the FIN so that any check on FIN flag will fail.
3186 * Remember that FIN also counts in the sequence number
3187 * space. So we need to ack out of order FIN only segments.
3189 if (flags
& TH_FIN
) {
3190 tcp
->tcp_valid_bits
|= TCP_OFO_FIN_VALID
;
3191 tcp
->tcp_ofo_fin_seq
= seg_seq
+ seg_len
;
3193 flags
|= TH_ACK_NEEDED
;
3196 /* Fill in the SACK blk list. */
3197 if (tcp
->tcp_snd_sack_ok
) {
3198 assert(tcp
->tcp_sack_info
!= NULL
);
3199 tcp_sack_insert(tcp
->tcp_sack_list
,
3200 seg_seq
, seg_seq
+ seg_len
,
3201 &(tcp
->tcp_num_sack_blk
));
3205 * Attempt reassembly and see if we have something
3208 mp
= tcp_reass(tcp
, mp
, seg_seq
);
3209 /* Always ack out of order packets */
3210 flags
|= TH_ACK_NEEDED
| TH_PUSH
;
3212 assert((uintptr_t)(mp
->b_wptr
-
3213 mp
->b_rptr
) <= (uintptr_t)INT_MAX
);
3214 seg_len
= mp
->b_cont
? msgdsize(mp
) :
3215 (int)(mp
->b_wptr
- mp
->b_rptr
);
3216 seg_seq
= tcp
->tcp_rnxt
;
3218 * A gap is filled and the seq num and len
3219 * of the gap match that of a previously
3220 * received FIN, put the FIN flag back in.
3222 if ((tcp
->tcp_valid_bits
& TCP_OFO_FIN_VALID
) &&
3223 seg_seq
+ seg_len
== tcp
->tcp_ofo_fin_seq
) {
3225 tcp
->tcp_valid_bits
&=
3230 * Keep going even with NULL mp.
3231 * There may be a useful ACK or something else
3232 * we don't want to miss.
3234 * But TCP should not perform fast retransmit
3235 * because of the ack number. TCP uses
3236 * seg_len == 0 to determine if it is a pure
3237 * ACK. And this is not a pure ACK.
3243 } else if (seg_len
> 0) {
3244 BUMP_MIB(tcp_mib
.tcpInDataInorderSegs
);
3245 UPDATE_MIB(tcp_mib
.tcpInDataInorderBytes
, seg_len
);
3247 * If an out of order FIN was received before, and the seq
3248 * num and len of the new segment match that of the FIN,
3249 * put the FIN flag back in.
3251 if ((tcp
->tcp_valid_bits
& TCP_OFO_FIN_VALID
) &&
3252 seg_seq
+ seg_len
== tcp
->tcp_ofo_fin_seq
) {
3254 tcp
->tcp_valid_bits
&= ~TCP_OFO_FIN_VALID
;
3257 if ((flags
& (TH_RST
| TH_SYN
| TH_URG
| TH_ACK
)) != TH_ACK
) {
3258 if (flags
& TH_RST
) {
3260 switch (tcp
->tcp_state
) {
3262 (void) tcp_clean_death(sock_id
, tcp
, ECONNREFUSED
);
3264 case TCPS_ESTABLISHED
:
3265 case TCPS_FIN_WAIT_1
:
3266 case TCPS_FIN_WAIT_2
:
3267 case TCPS_CLOSE_WAIT
:
3268 (void) tcp_clean_death(sock_id
, tcp
, ECONNRESET
);
3272 (void) tcp_clean_death(sock_id
, tcp
, 0);
3275 assert(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
3276 (void) tcp_clean_death(sock_id
, tcp
, ENXIO
);
3281 if (flags
& TH_SYN
) {
3283 * See RFC 793, Page 71
3285 * The seq number must be in the window as it should
3286 * be "fixed" above. If it is outside window, it should
3287 * be already rejected. Note that we allow seg_seq to be
3288 * rnxt + rwnd because we want to accept 0 window probe.
3290 assert(SEQ_GEQ(seg_seq
, tcp
->tcp_rnxt
) &&
3291 SEQ_LEQ(seg_seq
, tcp
->tcp_rnxt
+ tcp
->tcp_rwnd
));
3294 * If the ACK flag is not set, just use our snxt as the
3295 * seq number of the RST segment.
3297 if (!(flags
& TH_ACK
)) {
3298 seg_ack
= tcp
->tcp_snxt
;
3300 tcp_xmit_ctl("TH_SYN", tcp
, NULL
, seg_ack
,
3301 seg_seq
+ 1, TH_RST
|TH_ACK
, 0, sock_id
);
3302 assert(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
3303 (void) tcp_clean_death(sock_id
, tcp
, ECONNRESET
);
3308 if (!(flags
& TH_ACK
)) {
3310 printf("No ack in segment, dropped it, seq:%x\n", seg_seq
);
3316 bytes_acked
= (int)(seg_ack
- tcp
->tcp_suna
);
3318 if (tcp
->tcp_state
== TCPS_SYN_RCVD
) {
3319 tcp_t
*listener
= tcp
->tcp_listener
;
3321 printf("Done with eager 3-way handshake\n");
3324 * NOTE: RFC 793 pg. 72 says this should be 'bytes_acked < 0'
3325 * but that would mean we have an ack that ignored our SYN.
3327 if (bytes_acked
< 1 || SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
3329 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
3330 tcp
, NULL
, seg_ack
, 0, TH_RST
, 0, sock_id
);
3335 * if the conn_req_q is full defer processing
3336 * until space is availabe after accept()
3339 if (listener
->tcp_conn_req_cnt_q
<
3340 listener
->tcp_conn_req_max
) {
3343 listener
->tcp_conn_req_cnt_q0
--;
3344 listener
->tcp_conn_req_cnt_q
++;
3346 /* Move from SYN_RCVD to ESTABLISHED list */
3347 tcp
->tcp_eager_next_q0
->tcp_eager_prev_q0
=
3348 tcp
->tcp_eager_prev_q0
;
3349 tcp
->tcp_eager_prev_q0
->tcp_eager_next_q0
=
3350 tcp
->tcp_eager_next_q0
;
3351 tcp
->tcp_eager_prev_q0
= NULL
;
3352 tcp
->tcp_eager_next_q0
= NULL
;
3355 * Insert at end of the queue because sockfs
3356 * sends down T_CONN_RES in chronological
3357 * order. Leaving the older conn indications
3358 * at front of the queue helps reducing search
3361 tail
= listener
->tcp_eager_last_q
;
3363 tail
->tcp_eager_next_q
= tcp
;
3365 listener
->tcp_eager_next_q
= tcp
;
3367 listener
->tcp_eager_last_q
= tcp
;
3368 tcp
->tcp_eager_next_q
= NULL
;
3371 * Defer connection on q0 and set deferred
3372 * connection bit true
3374 tcp
->tcp_conn_def_q0
= B_TRUE
;
3376 /* take tcp out of q0 ... */
3377 tcp
->tcp_eager_prev_q0
->tcp_eager_next_q0
=
3378 tcp
->tcp_eager_next_q0
;
3379 tcp
->tcp_eager_next_q0
->tcp_eager_prev_q0
=
3380 tcp
->tcp_eager_prev_q0
;
3382 /* ... and place it at the end of q0 */
3383 tcp
->tcp_eager_prev_q0
= listener
->tcp_eager_prev_q0
;
3384 tcp
->tcp_eager_next_q0
= listener
;
3385 listener
->tcp_eager_prev_q0
->tcp_eager_next_q0
= tcp
;
3386 listener
->tcp_eager_prev_q0
= tcp
;
3389 tcp
->tcp_suna
= tcp
->tcp_iss
+ 1; /* One for the SYN */
3393 * If SYN was retransmitted, need to reset all
3394 * retransmission info as this segment will be
3395 * treated as a dup ACK.
3397 if (tcp
->tcp_rexmit
) {
3398 tcp
->tcp_rexmit
= B_FALSE
;
3399 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
3400 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
3401 tcp
->tcp_snd_burst
= TCP_CWND_NORMAL
;
3402 tcp
->tcp_ms_we_have_waited
= 0;
3403 tcp
->tcp_cwnd
= mss
;
3407 * We set the send window to zero here.
3408 * This is needed if there is data to be
3409 * processed already on the queue.
3410 * Later (at swnd_update label), the
3411 * "new_swnd > tcp_swnd" condition is satisfied
3412 * the XMIT_NEEDED flag is set in the current
3413 * (SYN_RCVD) state. This ensures tcp_wput_data() is
3414 * called if there is already data on queue in
3419 if (new_swnd
> tcp
->tcp_max_swnd
)
3420 tcp
->tcp_max_swnd
= new_swnd
;
3421 tcp
->tcp_swl1
= seg_seq
;
3422 tcp
->tcp_swl2
= seg_ack
;
3423 tcp
->tcp_state
= TCPS_ESTABLISHED
;
3424 tcp
->tcp_valid_bits
&= ~TCP_ISS_VALID
;
3426 /* This code follows 4.4BSD-Lite2 mostly. */
3427 if (bytes_acked
< 0)
3431 * If TCP is ECN capable and the congestion experience bit is
3432 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be
3433 * done once per window (or more loosely, per RTT).
3435 if (tcp
->tcp_cwr
&& SEQ_GT(seg_ack
, tcp
->tcp_cwr_snd_max
))
3436 tcp
->tcp_cwr
= B_FALSE
;
3437 if (tcp
->tcp_ecn_ok
&& (flags
& TH_ECE
)) {
3438 if (!tcp
->tcp_cwr
) {
3439 npkt
= (MIN(tcp
->tcp_cwnd
, tcp
->tcp_swnd
) >> 1) / mss
;
3440 tcp
->tcp_cwnd_ssthresh
= MAX(npkt
, 2) * mss
;
3441 tcp
->tcp_cwnd
= npkt
* mss
;
3443 * If the cwnd is 0, use the timer to clock out
3444 * new segments. This is required by the ECN spec.
3447 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
3449 * This makes sure that when the ACK comes
3450 * back, we will increase tcp_cwnd by 1 MSS.
3452 tcp
->tcp_cwnd_cnt
= 0;
3454 tcp
->tcp_cwr
= B_TRUE
;
3456 * This marks the end of the current window of in
3457 * flight data. That is why we don't use
3458 * tcp_suna + tcp_swnd. Only data in flight can
3461 tcp
->tcp_cwr_snd_max
= tcp
->tcp_snxt
;
3462 tcp
->tcp_ecn_cwr_sent
= B_FALSE
;
3466 mp1
= tcp
->tcp_xmit_head
;
3467 if (bytes_acked
== 0) {
3468 if (!ofo_seg
&& seg_len
== 0 && new_swnd
== tcp
->tcp_swnd
) {
3471 BUMP_MIB(tcp_mib
.tcpInDupAck
);
3473 * Fast retransmit. When we have seen exactly three
3474 * identical ACKs while we have unacked data
3475 * outstanding we take it as a hint that our peer
3476 * dropped something.
3478 * If TCP is retransmitting, don't do fast retransmit.
3480 if (mp1
!= NULL
&& tcp
->tcp_suna
!= tcp
->tcp_snxt
&&
3481 ! tcp
->tcp_rexmit
) {
3482 /* Do Limited Transmit */
3483 if ((dupack_cnt
= ++tcp
->tcp_dupack_cnt
) <
3484 tcp_dupack_fast_retransmit
) {
3488 * What we need to do is temporarily
3489 * increase tcp_cwnd so that new
3490 * data can be sent if it is allowed
3491 * by the receive window (tcp_rwnd).
3492 * tcp_wput_data() will take care of
3495 * If the connection is SACK capable,
3496 * only do limited xmit when there
3499 * Note how tcp_cwnd is incremented.
3500 * The first dup ACK will increase
3501 * it by 1 MSS. The second dup ACK
3502 * will increase it by 2 MSS. This
3503 * means that only 1 new segment will
3504 * be sent for each dup ACK.
3506 if (tcp
->tcp_unsent
> 0 &&
3507 (!tcp
->tcp_snd_sack_ok
||
3508 (tcp
->tcp_snd_sack_ok
&&
3509 tcp
->tcp_notsack_list
!= NULL
))) {
3510 tcp
->tcp_cwnd
+= mss
<<
3511 (tcp
->tcp_dupack_cnt
- 1);
3512 flags
|= TH_LIMIT_XMIT
;
3514 } else if (dupack_cnt
==
3515 tcp_dupack_fast_retransmit
) {
3517 BUMP_MIB(tcp_mib
.tcpOutFastRetrans
);
3519 * If we have reduced tcp_ssthresh
3520 * because of ECN, do not reduce it again
3521 * unless it is already one window of data
3522 * away. After one window of data, tcp_cwr
3523 * should then be cleared. Note that
3524 * for non ECN capable connection, tcp_cwr
3525 * should always be false.
3527 * Adjust cwnd since the duplicate
3528 * ack indicates that a packet was
3529 * dropped (due to congestion.)
3531 if (!tcp
->tcp_cwr
) {
3532 npkt
= (MIN(tcp
->tcp_cwnd
,
3533 tcp
->tcp_swnd
) >> 1) / mss
;
3536 tcp
->tcp_cwnd_ssthresh
= npkt
* mss
;
3537 tcp
->tcp_cwnd
= (npkt
+
3538 tcp
->tcp_dupack_cnt
) * mss
;
3540 if (tcp
->tcp_ecn_ok
) {
3541 tcp
->tcp_cwr
= B_TRUE
;
3542 tcp
->tcp_cwr_snd_max
= tcp
->tcp_snxt
;
3543 tcp
->tcp_ecn_cwr_sent
= B_FALSE
;
3547 * We do Hoe's algorithm. Refer to her
3548 * paper "Improving the Start-up Behavior
3549 * of a Congestion Control Scheme for TCP,"
3550 * appeared in SIGCOMM'96.
3552 * Save highest seq no we have sent so far.
3553 * Be careful about the invisible FIN byte.
3555 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
3556 (tcp
->tcp_unsent
== 0)) {
3557 tcp
->tcp_rexmit_max
= tcp
->tcp_fss
;
3559 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
3563 * Do not allow bursty traffic during.
3564 * fast recovery. Refer to Fall and Floyd's
3565 * paper "Simulation-based Comparisons of
3566 * Tahoe, Reno and SACK TCP" (in CCR ??)
3567 * This is a best current practise.
3569 tcp
->tcp_snd_burst
= TCP_CWND_SS
;
3573 * Calculate tcp_pipe, which is the
3574 * estimated number of bytes in
3577 * tcp_fack is the highest sack'ed seq num
3580 * tcp_pipe is explained in the above quoted
3581 * Fall and Floyd's paper. tcp_fack is
3582 * explained in Mathis and Mahdavi's
3583 * "Forward Acknowledgment: Refining TCP
3584 * Congestion Control" in SIGCOMM '96.
3586 if (tcp
->tcp_snd_sack_ok
) {
3587 assert(tcp
->tcp_sack_info
!= NULL
);
3588 if (tcp
->tcp_notsack_list
!= NULL
) {
3589 tcp
->tcp_pipe
= tcp
->tcp_snxt
-
3591 tcp
->tcp_sack_snxt
= seg_ack
;
3592 flags
|= TH_NEED_SACK_REXMIT
;
3595 * Always initialize tcp_pipe
3596 * even though we don't have
3597 * any SACK info. If later
3598 * we get SACK info and
3599 * tcp_pipe is not initialized,
3600 * funny things will happen.
3603 tcp
->tcp_cwnd_ssthresh
;
3606 flags
|= TH_REXMIT_NEEDED
;
3607 } /* tcp_snd_sack_ok */
3611 * Here we perform congestion
3612 * avoidance, but NOT slow start.
3613 * This is known as the Fast
3614 * Recovery Algorithm.
3616 if (tcp
->tcp_snd_sack_ok
&&
3617 tcp
->tcp_notsack_list
!= NULL
) {
3618 flags
|= TH_NEED_SACK_REXMIT
;
3619 tcp
->tcp_pipe
-= mss
;
3620 if (tcp
->tcp_pipe
< 0)
3624 * We know that one more packet has
3625 * left the pipe thus we can update
3628 cwnd
= tcp
->tcp_cwnd
+ mss
;
3629 if (cwnd
> tcp
->tcp_cwnd_max
)
3630 cwnd
= tcp
->tcp_cwnd_max
;
3631 tcp
->tcp_cwnd
= cwnd
;
3632 flags
|= TH_XMIT_NEEDED
;
3636 } else if (tcp
->tcp_zero_win_probe
) {
3638 * If the window has opened, need to arrange
3639 * to send additional data.
3641 if (new_swnd
!= 0) {
3642 /* tcp_suna != tcp_snxt */
3643 /* Packet contains a window update */
3644 BUMP_MIB(tcp_mib
.tcpInWinUpdate
);
3645 tcp
->tcp_zero_win_probe
= 0;
3646 tcp
->tcp_timer_backoff
= 0;
3647 tcp
->tcp_ms_we_have_waited
= 0;
3650 * Transmit starting with tcp_suna since
3651 * the one byte probe is not ack'ed.
3652 * If TCP has sent more than one identical
3653 * probe, tcp_rexmit will be set. That means
3654 * tcp_ss_rexmit() will send out the one
3655 * byte along with new data. Otherwise,
3656 * fake the retransmission.
3658 flags
|= TH_XMIT_NEEDED
;
3659 if (!tcp
->tcp_rexmit
) {
3660 tcp
->tcp_rexmit
= B_TRUE
;
3661 tcp
->tcp_dupack_cnt
= 0;
3662 tcp
->tcp_rexmit_nxt
= tcp
->tcp_suna
;
3663 tcp
->tcp_rexmit_max
= tcp
->tcp_suna
+ 1;
3671 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
3672 * If the ACK value acks something that we have not yet sent, it might
3673 * be an old duplicate segment. Send an ACK to re-synchronize the
3675 * Note: reset in response to unacceptable ACK in SYN_RECEIVE
3676 * state is handled above, so we can always just drop the segment and
3679 * Should we send ACKs in response to ACK only segments?
3681 if (SEQ_GT(seg_ack
, tcp
->tcp_snxt
)) {
3682 BUMP_MIB(tcp_mib
.tcpInAckUnsent
);
3683 /* drop the received segment */
3686 /* Send back an ACK. */
3687 mp
= tcp_ack_mp(tcp
);
3692 BUMP_MIB(tcp_mib
.tcpOutAck
);
3693 (void) ipv4_tcp_output(sock_id
, mp
);
3699 * TCP gets a new ACK, update the notsack'ed list to delete those
3700 * blocks that are covered by this ACK.
3702 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_notsack_list
!= NULL
) {
3703 tcp_notsack_remove(&(tcp
->tcp_notsack_list
), seg_ack
,
3704 &(tcp
->tcp_num_notsack_blk
), &(tcp
->tcp_cnt_notsack_list
));
3708 * If we got an ACK after fast retransmit, check to see
3709 * if it is a partial ACK. If it is not and the congestion
3710 * window was inflated to account for the other side's
3711 * cached packets, retract it. If it is, do Hoe's algorithm.
3713 if (tcp
->tcp_dupack_cnt
>= tcp_dupack_fast_retransmit
) {
3714 assert(tcp
->tcp_rexmit
== B_FALSE
);
3715 if (SEQ_GEQ(seg_ack
, tcp
->tcp_rexmit_max
)) {
3716 tcp
->tcp_dupack_cnt
= 0;
3718 * Restore the orig tcp_cwnd_ssthresh after
3719 * fast retransmit phase.
3721 if (tcp
->tcp_cwnd
> tcp
->tcp_cwnd_ssthresh
) {
3722 tcp
->tcp_cwnd
= tcp
->tcp_cwnd_ssthresh
;
3724 tcp
->tcp_rexmit_max
= seg_ack
;
3725 tcp
->tcp_cwnd_cnt
= 0;
3726 tcp
->tcp_snd_burst
= TCP_CWND_NORMAL
;
3729 * Remove all notsack info to avoid confusion with
3730 * the next fast retrasnmit/recovery phase.
3732 if (tcp
->tcp_snd_sack_ok
&&
3733 tcp
->tcp_notsack_list
!= NULL
) {
3734 TCP_NOTSACK_REMOVE_ALL(tcp
->tcp_notsack_list
);
3737 if (tcp
->tcp_snd_sack_ok
&&
3738 tcp
->tcp_notsack_list
!= NULL
) {
3739 flags
|= TH_NEED_SACK_REXMIT
;
3740 tcp
->tcp_pipe
-= mss
;
3741 if (tcp
->tcp_pipe
< 0)
3747 * Retransmit the unack'ed segment and
3748 * restart fast recovery. Note that we
3749 * need to scale back tcp_cwnd to the
3750 * original value when we started fast
3751 * recovery. This is to prevent overly
3752 * aggressive behaviour in sending new
3755 tcp
->tcp_cwnd
= tcp
->tcp_cwnd_ssthresh
+
3756 tcp_dupack_fast_retransmit
* mss
;
3757 tcp
->tcp_cwnd_cnt
= tcp
->tcp_cwnd
;
3758 BUMP_MIB(tcp_mib
.tcpOutFastRetrans
);
3759 flags
|= TH_REXMIT_NEEDED
;
3763 tcp
->tcp_dupack_cnt
= 0;
3764 if (tcp
->tcp_rexmit
) {
3766 * TCP is retranmitting. If the ACK ack's all
3767 * outstanding data, update tcp_rexmit_max and
3768 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt
3769 * to the correct value.
3771 * Note that SEQ_LEQ() is used. This is to avoid
3772 * unnecessary fast retransmit caused by dup ACKs
3773 * received when TCP does slow start retransmission
3774 * after a time out. During this phase, TCP may
3775 * send out segments which are already received.
3776 * This causes dup ACKs to be sent back.
3778 if (SEQ_LEQ(seg_ack
, tcp
->tcp_rexmit_max
)) {
3779 if (SEQ_GT(seg_ack
, tcp
->tcp_rexmit_nxt
)) {
3780 tcp
->tcp_rexmit_nxt
= seg_ack
;
3782 if (seg_ack
!= tcp
->tcp_rexmit_max
) {
3783 flags
|= TH_XMIT_NEEDED
;
3786 tcp
->tcp_rexmit
= B_FALSE
;
3787 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
3788 tcp
->tcp_snd_burst
= TCP_CWND_NORMAL
;
3790 tcp
->tcp_ms_we_have_waited
= 0;
3794 BUMP_MIB(tcp_mib
.tcpInAckSegs
);
3795 UPDATE_MIB(tcp_mib
.tcpInAckBytes
, bytes_acked
);
3796 tcp
->tcp_suna
= seg_ack
;
3797 if (tcp
->tcp_zero_win_probe
!= 0) {
3798 tcp
->tcp_zero_win_probe
= 0;
3799 tcp
->tcp_timer_backoff
= 0;
3803 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
3804 * Note that it cannot be the SYN being ack'ed. The code flow
3805 * will not reach here.
3812 * Update the congestion window.
3814 * If TCP is not ECN capable or TCP is ECN capable but the
3815 * congestion experience bit is not set, increase the tcp_cwnd as
3818 if (!tcp
->tcp_ecn_ok
|| !(flags
& TH_ECE
)) {
3819 cwnd
= tcp
->tcp_cwnd
;
3822 if (cwnd
>= tcp
->tcp_cwnd_ssthresh
) {
3824 * This is to prevent an increase of less than 1 MSS of
3825 * tcp_cwnd. With partial increase, tcp_wput_data()
3826 * may send out tinygrams in order to preserve mblk
3829 * By initializing tcp_cwnd_cnt to new tcp_cwnd and
3830 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
3831 * increased by 1 MSS for every RTTs.
3833 if (tcp
->tcp_cwnd_cnt
<= 0) {
3834 tcp
->tcp_cwnd_cnt
= cwnd
+ add
;
3836 tcp
->tcp_cwnd_cnt
-= add
;
3840 tcp
->tcp_cwnd
= MIN(cwnd
+ add
, tcp
->tcp_cwnd_max
);
3843 /* Can we update the RTT estimates? */
3844 if (tcp
->tcp_snd_ts_ok
) {
3845 /* Ignore zero timestamp echo-reply. */
3846 if (tcpopt
.tcp_opt_ts_ecr
!= 0) {
3847 tcp_set_rto(tcp
, (int32_t)(prom_gettime() -
3848 tcpopt
.tcp_opt_ts_ecr
));
3851 /* If needed, restart the timer. */
3852 if (tcp
->tcp_set_timer
== 1) {
3853 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
3854 tcp
->tcp_set_timer
= 0;
3857 * Update tcp_csuna in case the other side stops sending
3860 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
3861 } else if (SEQ_GT(seg_ack
, tcp
->tcp_csuna
)) {
3863 * An ACK sequence we haven't seen before, so get the RTT
3864 * and update the RTO.
3865 * Note. use uintptr_t to suppress the gcc warning.
3867 tcp_set_rto(tcp
, (int32_t)(prom_gettime() -
3868 (uint32_t)(uintptr_t)mp1
->b_prev
));
3870 /* Remeber the last sequence to be ACKed */
3871 tcp
->tcp_csuna
= seg_ack
;
3872 if (tcp
->tcp_set_timer
== 1) {
3873 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
3874 tcp
->tcp_set_timer
= 0;
3877 BUMP_MIB(tcp_mib
.tcpRttNoUpdate
);
3880 /* Eat acknowledged bytes off the xmit queue. */
3886 assert((uintptr_t)(wptr
- mp1
->b_rptr
) <= (uintptr_t)INT_MAX
);
3887 bytes_acked
-= (int)(wptr
- mp1
->b_rptr
);
3888 if (bytes_acked
< 0) {
3889 mp1
->b_rptr
= wptr
+ bytes_acked
;
3896 if (bytes_acked
== 0) {
3898 /* Everything is ack'ed, clear the tail. */
3899 tcp
->tcp_xmit_tail
= NULL
;
3900 goto pre_swnd_update
;
3902 if (mp2
!= tcp
->tcp_xmit_tail
)
3904 tcp
->tcp_xmit_tail
= mp1
;
3905 assert((uintptr_t)(mp1
->b_wptr
-
3906 mp1
->b_rptr
) <= (uintptr_t)INT_MAX
);
3907 tcp
->tcp_xmit_tail_unsent
= (int)(mp1
->b_wptr
-
3913 * More was acked but there is nothing more
3914 * outstanding. This means that the FIN was
3915 * just acked or that we're talking to a clown.
3918 assert(tcp
->tcp_fin_sent
);
3919 tcp
->tcp_xmit_tail
= NULL
;
3920 if (tcp
->tcp_fin_sent
) {
3921 tcp
->tcp_fin_acked
= B_TRUE
;
3924 * We should never got here because
3925 * we have already checked that the
3926 * number of bytes ack'ed should be
3927 * smaller than or equal to what we
3928 * have sent so far (it is the
3929 * acceptability check of the ACK).
3930 * We can only get here if the send
3931 * queue is corrupted.
3933 * Terminate the connection and
3934 * panic the system. It is better
3935 * for us to panic instead of
3936 * continuing to avoid other disaster.
3938 tcp_xmit_ctl(NULL
, tcp
, NULL
, tcp
->tcp_snxt
,
3939 tcp
->tcp_rnxt
, TH_RST
|TH_ACK
, 0, sock_id
);
3940 printf("Memory corruption "
3941 "detected for connection %s.\n",
3942 tcp_display(tcp
, NULL
,
3943 DISP_ADDR_AND_PORT
));
3944 /* We should never get here... */
3945 prom_panic("tcp_rput_data");
3948 goto pre_swnd_update
;
3950 assert(mp2
!= tcp
->tcp_xmit_tail
);
3952 if (tcp
->tcp_unsent
) {
3953 flags
|= TH_XMIT_NEEDED
;
3956 tcp
->tcp_xmit_head
= mp1
;
3959 * The following check is different from most other implementations.
3960 * For bi-directional transfer, when segments are dropped, the
3961 * "normal" check will not accept a window update in those
3962 * retransmitted segemnts. Failing to do that, TCP may send out
3963 * segments which are outside receiver's window. As TCP accepts
3964 * the ack in those retransmitted segments, if the window update in
3965 * the same segment is not accepted, TCP will incorrectly calculates
3966 * that it can send more segments. This can create a deadlock
3967 * with the receiver if its window becomes zero.
3969 if (SEQ_LT(tcp
->tcp_swl2
, seg_ack
) ||
3970 SEQ_LT(tcp
->tcp_swl1
, seg_seq
) ||
3971 (tcp
->tcp_swl1
== seg_seq
&& new_swnd
> tcp
->tcp_swnd
)) {
3973 * The criteria for update is:
3975 * 1. the segment acknowledges some data. Or
3976 * 2. the segment is new, i.e. it has a higher seq num. Or
3977 * 3. the segment is not old and the advertised window is
3978 * larger than the previous advertised window.
3980 if (tcp
->tcp_unsent
&& new_swnd
> tcp
->tcp_swnd
)
3981 flags
|= TH_XMIT_NEEDED
;
3982 tcp
->tcp_swnd
= new_swnd
;
3983 if (new_swnd
> tcp
->tcp_max_swnd
)
3984 tcp
->tcp_max_swnd
= new_swnd
;
3985 tcp
->tcp_swl1
= seg_seq
;
3986 tcp
->tcp_swl2
= seg_ack
;
3989 if (tcp
->tcp_state
> TCPS_ESTABLISHED
) {
3990 switch (tcp
->tcp_state
) {
3991 case TCPS_FIN_WAIT_1
:
3992 if (tcp
->tcp_fin_acked
) {
3993 tcp
->tcp_state
= TCPS_FIN_WAIT_2
;
3995 * We implement the non-standard BSD/SunOS
3996 * FIN_WAIT_2 flushing algorithm.
3997 * If there is no user attached to this
3998 * TCP endpoint, then this TCP struct
3999 * could hang around forever in FIN_WAIT_2
4000 * state if the peer forgets to send us
4001 * a FIN. To prevent this, we wait only
4002 * 2*MSL (a convenient time value) for
4003 * the FIN to arrive. If it doesn't show up,
4004 * we flush the TCP endpoint. This algorithm,
4005 * though a violation of RFC-793, has worked
4006 * for over 10 years in BSD systems.
4007 * Note: SunOS 4.x waits 675 seconds before
4008 * flushing the FIN_WAIT_2 connection.
4010 TCP_TIMER_RESTART(tcp
,
4011 tcp_fin_wait_2_flush_interval
);
4014 case TCPS_FIN_WAIT_2
:
4015 break; /* Shutdown hook? */
4018 if (tcp
->tcp_fin_acked
) {
4019 (void) tcp_clean_death(sock_id
, tcp
, 0);
4024 if (tcp
->tcp_fin_acked
) {
4025 tcp
->tcp_state
= TCPS_TIME_WAIT
;
4026 tcp_time_wait_append(tcp
);
4027 TCP_TIMER_RESTART(tcp
, tcp_time_wait_interval
);
4030 case TCPS_CLOSE_WAIT
:
4034 assert(tcp
->tcp_state
!= TCPS_TIME_WAIT
);
4038 if (flags
& TH_FIN
) {
4039 /* Make sure we ack the fin */
4040 flags
|= TH_ACK_NEEDED
;
4041 if (!tcp
->tcp_fin_rcvd
) {
4042 tcp
->tcp_fin_rcvd
= B_TRUE
;
4044 U32_TO_ABE32(tcp
->tcp_rnxt
, tcp
->tcp_tcph
->th_ack
);
4046 switch (tcp
->tcp_state
) {
4048 case TCPS_ESTABLISHED
:
4049 tcp
->tcp_state
= TCPS_CLOSE_WAIT
;
4052 case TCPS_FIN_WAIT_1
:
4053 if (!tcp
->tcp_fin_acked
) {
4054 tcp
->tcp_state
= TCPS_CLOSING
;
4058 case TCPS_FIN_WAIT_2
:
4059 tcp
->tcp_state
= TCPS_TIME_WAIT
;
4060 tcp_time_wait_append(tcp
);
4061 TCP_TIMER_RESTART(tcp
, tcp_time_wait_interval
);
4064 * implies data piggybacked on FIN.
4065 * break to handle data.
4080 if (mp
->b_rptr
== mp
->b_wptr
) {
4082 * The header has been consumed, so we remove the
4083 * zero-length mblk here.
4090 * ACK every other segments, unless the input queue is empty
4091 * as we don't have a timer available.
4093 if (++tcp
->tcp_rack_cnt
== 2 || sockets
[sock_id
].inq
== NULL
) {
4094 flags
|= TH_ACK_NEEDED
;
4095 tcp
->tcp_rack_cnt
= 0;
4097 tcp
->tcp_rnxt
+= seg_len
;
4098 U32_TO_ABE32(tcp
->tcp_rnxt
, tcp
->tcp_tcph
->th_ack
);
4100 /* Update SACK list */
4101 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
4102 tcp_sack_remove(tcp
->tcp_sack_list
, tcp
->tcp_rnxt
,
4103 &(tcp
->tcp_num_sack_blk
));
4106 if (tcp
->tcp_listener
) {
4108 * Side queue inbound data until the accept happens.
4109 * tcp_accept/tcp_rput drains this when the accept happens.
4111 tcp_rcv_enqueue(tcp
, mp
, seg_len
);
4113 /* Just queue the data until the app calls read. */
4114 tcp_rcv_enqueue(tcp
, mp
, seg_len
);
4116 * Make sure the timer is running if we have data waiting
4117 * for a push bit. This provides resiliency against
4118 * implementations that do not correctly generate push bits.
4120 if (tcp
->tcp_rcv_list
!= NULL
)
4121 flags
|= TH_TIMER_NEEDED
;
4125 /* Is there anything left to do? */
4126 if ((flags
& (TH_REXMIT_NEEDED
|TH_XMIT_NEEDED
|TH_ACK_NEEDED
|
4127 TH_NEED_SACK_REXMIT
|TH_LIMIT_XMIT
|TH_TIMER_NEEDED
)) == 0)
4130 /* Any transmit work to do and a non-zero window? */
4131 if ((flags
& (TH_REXMIT_NEEDED
|TH_XMIT_NEEDED
|TH_NEED_SACK_REXMIT
|
4132 TH_LIMIT_XMIT
)) && tcp
->tcp_swnd
!= 0) {
4133 if (flags
& TH_REXMIT_NEEDED
) {
4134 uint32_t snd_size
= tcp
->tcp_snxt
- tcp
->tcp_suna
;
4138 if (snd_size
> tcp
->tcp_swnd
)
4139 snd_size
= tcp
->tcp_swnd
;
4140 mp1
= tcp_xmit_mp(tcp
, tcp
->tcp_xmit_head
, snd_size
,
4141 NULL
, NULL
, tcp
->tcp_suna
, B_TRUE
, &snd_size
,
4145 /* use uintptr_t to suppress the gcc warning */
4146 tcp
->tcp_xmit_head
->b_prev
=
4147 (mblk_t
*)(uintptr_t)prom_gettime();
4148 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
4149 BUMP_MIB(tcp_mib
.tcpRetransSegs
);
4150 UPDATE_MIB(tcp_mib
.tcpRetransBytes
, snd_size
);
4151 (void) ipv4_tcp_output(sock_id
, mp1
);
4155 if (flags
& TH_NEED_SACK_REXMIT
) {
4156 if (tcp_sack_rxmit(tcp
, sock_id
) != 0) {
4157 flags
|= TH_XMIT_NEEDED
;
4161 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
4162 * out new segment. Note that tcp_rexmit should not be
4163 * set, otherwise TH_LIMIT_XMIT should not be set.
4165 if (flags
& (TH_XMIT_NEEDED
|TH_LIMIT_XMIT
)) {
4166 if (!tcp
->tcp_rexmit
) {
4167 tcp_wput_data(tcp
, NULL
, sock_id
);
4169 tcp_ss_rexmit(tcp
, sock_id
);
4172 * The TCP could be closed in tcp_state_wait via
4173 * tcp_wput_data (tcp_ss_rexmit could call
4174 * tcp_wput_data as well).
4176 if (sockets
[sock_id
].pcb
== NULL
)
4180 * Adjust tcp_cwnd back to normal value after sending
4181 * new data segments.
4183 if (flags
& TH_LIMIT_XMIT
) {
4184 tcp
->tcp_cwnd
-= mss
<< (tcp
->tcp_dupack_cnt
- 1);
4187 /* Anything more to do? */
4188 if ((flags
& (TH_ACK_NEEDED
|TH_TIMER_NEEDED
)) == 0)
4192 if (flags
& TH_ACK_NEEDED
) {
4194 * Time to send an ack for some reason.
4196 if ((mp1
= tcp_ack_mp(tcp
)) != NULL
) {
4197 TCP_DUMP_PACKET("tcp_rput_data: ack mp", mp1
);
4198 (void) ipv4_tcp_output(sock_id
, mp1
);
4199 BUMP_MIB(tcp_mib
.tcpOutAck
);
4206 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start
4207 * retransmission after a timeout.
4209 * To limit the number of duplicate segments, we limit the number of segment
4210 * to be sent in one time to tcp_snd_burst, the burst variable.
4213 tcp_ss_rexmit(tcp_t
*tcp
, int sock_id
)
4220 int32_t burst
= tcp
->tcp_snd_burst
;
4224 * Note that tcp_rexmit can be set even though TCP has retransmitted
4225 * all unack'ed segments.
4227 if (SEQ_LT(tcp
->tcp_rexmit_nxt
, tcp
->tcp_rexmit_max
)) {
4228 smax
= tcp
->tcp_rexmit_max
;
4229 snxt
= tcp
->tcp_rexmit_nxt
;
4230 if (SEQ_LT(snxt
, tcp
->tcp_suna
)) {
4231 snxt
= tcp
->tcp_suna
;
4233 win
= MIN(tcp
->tcp_cwnd
, tcp
->tcp_swnd
);
4234 win
-= snxt
- tcp
->tcp_suna
;
4236 snxt_mp
= tcp_get_seg_mp(tcp
, snxt
, &off
);
4238 while (SEQ_LT(snxt
, smax
) && (win
> 0) &&
4239 (burst
> 0) && (snxt_mp
!= NULL
)) {
4241 mblk_t
*old_snxt_mp
= snxt_mp
;
4247 if (SEQ_GT(snxt
+ cnt
, smax
)) {
4250 xmit_mp
= tcp_xmit_mp(tcp
, snxt_mp
, cnt
, &off
,
4251 &snxt_mp
, snxt
, B_TRUE
, &cnt
, B_TRUE
);
4253 if (xmit_mp
== NULL
)
4256 (void) ipv4_tcp_output(sock_id
, xmit_mp
);
4262 * Update the send timestamp to avoid false
4264 * Note. use uintptr_t to suppress the gcc warning.
4266 old_snxt_mp
->b_prev
=
4267 (mblk_t
*)(uintptr_t)prom_gettime();
4268 BUMP_MIB(tcp_mib
.tcpRetransSegs
);
4269 UPDATE_MIB(tcp_mib
.tcpRetransBytes
, cnt
);
4271 tcp
->tcp_rexmit_nxt
= snxt
;
4275 * If we have transmitted all we have at the time
4276 * we started the retranmission, we can leave
4277 * the rest of the job to tcp_wput_data(). But we
4278 * need to check the send window first. If the
4279 * win is not 0, go on with tcp_wput_data().
4281 if (SEQ_LT(snxt
, smax
) || win
== 0) {
4285 /* Only call tcp_wput_data() if there is data to be sent. */
4286 if (tcp
->tcp_unsent
) {
4287 tcp_wput_data(tcp
, NULL
, sock_id
);
4292 * tcp_timer is the timer service routine. It handles all timer events for
4293 * a tcp instance except keepalives. It figures out from the state of the
4294 * tcp instance what kind of action needs to be done at the time it is called.
4297 tcp_timer(tcp_t
*tcp
, int sock_id
)
4300 uint32_t first_threshold
;
4301 uint32_t second_threshold
;
4305 first_threshold
= tcp
->tcp_first_timer_threshold
;
4306 second_threshold
= tcp
->tcp_second_timer_threshold
;
4307 switch (tcp
->tcp_state
) {
4314 first_threshold
= tcp
->tcp_first_ctimer_threshold
;
4315 second_threshold
= tcp
->tcp_second_ctimer_threshold
;
4317 case TCPS_ESTABLISHED
:
4318 case TCPS_FIN_WAIT_1
:
4320 case TCPS_CLOSE_WAIT
:
4322 /* If we have data to rexmit */
4323 if (tcp
->tcp_suna
!= tcp
->tcp_snxt
) {
4324 int32_t time_to_wait
;
4326 BUMP_MIB(tcp_mib
.tcpTimRetrans
);
4327 if (tcp
->tcp_xmit_head
== NULL
)
4329 /* use uintptr_t to suppress the gcc warning */
4330 time_to_wait
= (int32_t)(prom_gettime() -
4331 (uint32_t)(uintptr_t)tcp
->tcp_xmit_head
->b_prev
);
4332 time_to_wait
= tcp
->tcp_rto
- time_to_wait
;
4333 if (time_to_wait
> 0) {
4335 * Timer fired too early, so restart it.
4337 TCP_TIMER_RESTART(tcp
, time_to_wait
);
4341 * When we probe zero windows, we force the swnd open.
4342 * If our peer acks with a closed window swnd will be
4343 * set to zero by tcp_rput(). As long as we are
4344 * receiving acks tcp_rput will
4345 * reset 'tcp_ms_we_have_waited' so as not to trip the
4346 * first and second interval actions. NOTE: the timer
4347 * interval is allowed to continue its exponential
4350 if (tcp
->tcp_swnd
== 0 || tcp
->tcp_zero_win_probe
) {
4351 DEBUG_1("tcp_timer (%d): zero win", sock_id
);
4355 * After retransmission, we need to do
4356 * slow start. Set the ssthresh to one
4357 * half of current effective window and
4358 * cwnd to one MSS. Also reset
4361 * Note that if tcp_ssthresh is reduced because
4362 * of ECN, do not reduce it again unless it is
4363 * already one window of data away (tcp_cwr
4364 * should then be cleared) or this is a
4365 * timeout for a retransmitted segment.
4369 if (!tcp
->tcp_cwr
|| tcp
->tcp_rexmit
) {
4370 npkt
= (MIN((tcp
->tcp_timer_backoff
?
4371 tcp
->tcp_cwnd_ssthresh
:
4373 tcp
->tcp_swnd
) >> 1) /
4377 tcp
->tcp_cwnd_ssthresh
= npkt
*
4380 tcp
->tcp_cwnd
= tcp
->tcp_mss
;
4381 tcp
->tcp_cwnd_cnt
= 0;
4382 if (tcp
->tcp_ecn_ok
) {
4383 tcp
->tcp_cwr
= B_TRUE
;
4384 tcp
->tcp_cwr_snd_max
= tcp
->tcp_snxt
;
4385 tcp
->tcp_ecn_cwr_sent
= B_FALSE
;
4391 * We have something to send yet we cannot send. The
4394 * 1. Zero send window: we need to do zero window probe.
4395 * 2. Zero cwnd: because of ECN, we need to "clock out
4397 * 3. SWS avoidance: receiver may have shrunk window,
4398 * reset our knowledge.
4400 * Note that condition 2 can happen with either 1 or
4401 * 3. But 1 and 3 are exclusive.
4403 if (tcp
->tcp_unsent
!= 0) {
4404 if (tcp
->tcp_cwnd
== 0) {
4406 * Set tcp_cwnd to 1 MSS so that a
4407 * new segment can be sent out. We
4408 * are "clocking out" new data when
4409 * the network is really congested.
4411 assert(tcp
->tcp_ecn_ok
);
4412 tcp
->tcp_cwnd
= tcp
->tcp_mss
;
4414 if (tcp
->tcp_swnd
== 0) {
4415 /* Extend window for zero window probe */
4417 tcp
->tcp_zero_win_probe
= B_TRUE
;
4418 BUMP_MIB(tcp_mib
.tcpOutWinProbe
);
4421 * Handle timeout from sender SWS avoidance.
4422 * Reset our knowledge of the max send window
4423 * since the receiver might have reduced its
4424 * receive buffer. Avoid setting tcp_max_swnd
4425 * to one since that will essentially disable
4428 * Note that since we don't have a SWS
4429 * state variable, if the timeout is set
4430 * for ECN but not for SWS, this
4431 * code will also be executed. This is
4432 * fine as tcp_max_swnd is updated
4433 * constantly and it will not affect
4436 tcp
->tcp_max_swnd
= MAX(tcp
->tcp_swnd
, 2);
4438 tcp_wput_data(tcp
, NULL
, sock_id
);
4441 /* Is there a FIN that needs to be to re retransmitted? */
4442 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
4443 !tcp
->tcp_fin_acked
)
4445 /* Nothing to do, return without restarting timer. */
4447 case TCPS_FIN_WAIT_2
:
4449 * User closed the TCP endpoint and peer ACK'ed our FIN.
4450 * We waited some time for for peer's FIN, but it hasn't
4451 * arrived. We flush the connection now to avoid
4452 * case where the peer has rebooted.
4455 case TCPS_TIME_WAIT
:
4456 (void) tcp_clean_death(sock_id
, tcp
, 0);
4459 DEBUG_3("tcp_timer (%d): strange state (%d) %s", sock_id
,
4460 tcp
->tcp_state
, tcp_display(tcp
, NULL
,
4464 if ((ms
= tcp
->tcp_ms_we_have_waited
) > second_threshold
) {
4466 * For zero window probe, we need to send indefinitely,
4467 * unless we have not heard from the other side for some
4470 if ((tcp
->tcp_zero_win_probe
== 0) ||
4471 ((prom_gettime() - tcp
->tcp_last_recv_time
) >
4472 second_threshold
)) {
4473 BUMP_MIB(tcp_mib
.tcpTimRetransDrop
);
4475 * If TCP is in SYN_RCVD state, send back a
4476 * RST|ACK as BSD does. Note that tcp_zero_win_probe
4477 * should be zero in TCPS_SYN_RCVD state.
4479 if (tcp
->tcp_state
== TCPS_SYN_RCVD
) {
4480 tcp_xmit_ctl("tcp_timer: RST sent on timeout "
4482 tcp
, NULL
, tcp
->tcp_snxt
,
4483 tcp
->tcp_rnxt
, TH_RST
| TH_ACK
, 0, sock_id
);
4485 (void) tcp_clean_death(sock_id
, tcp
,
4486 tcp
->tcp_client_errno
?
4487 tcp
->tcp_client_errno
: ETIMEDOUT
);
4491 * Set tcp_ms_we_have_waited to second_threshold
4492 * so that in next timeout, we will do the above
4493 * check (lbolt - tcp_last_recv_time). This is
4494 * also to avoid overflow.
4496 * We don't need to decrement tcp_timer_backoff
4497 * to avoid overflow because it will be decremented
4498 * later if new timeout value is greater than
4499 * tcp_rexmit_interval_max. In the case when
4500 * tcp_rexmit_interval_max is greater than
4501 * second_threshold, it means that we will wait
4502 * longer than second_threshold to send the next
4505 tcp
->tcp_ms_we_have_waited
= second_threshold
;
4507 } else if (ms
> first_threshold
&& tcp
->tcp_rtt_sa
!= 0) {
4509 * We have been retransmitting for too long... The RTT
4510 * we calculated is probably incorrect. Reinitialize it.
4511 * Need to compensate for 0 tcp_rtt_sa. Reset
4512 * tcp_rtt_update so that we won't accidentally cache a
4513 * bad value. But only do this if this is not a zero
4516 if (tcp
->tcp_zero_win_probe
== 0) {
4517 tcp
->tcp_rtt_sd
+= (tcp
->tcp_rtt_sa
>> 3) +
4518 (tcp
->tcp_rtt_sa
>> 5);
4519 tcp
->tcp_rtt_sa
= 0;
4520 tcp
->tcp_rtt_update
= 0;
4523 tcp
->tcp_timer_backoff
++;
4524 if ((ms
= (tcp
->tcp_rtt_sa
>> 3) + tcp
->tcp_rtt_sd
+
4525 tcp_rexmit_interval_extra
+ (tcp
->tcp_rtt_sa
>> 5)) <
4526 tcp_rexmit_interval_min
) {
4528 * This means the original RTO is tcp_rexmit_interval_min.
4529 * So we will use tcp_rexmit_interval_min as the RTO value
4530 * and do the backoff.
4532 ms
= tcp_rexmit_interval_min
<< tcp
->tcp_timer_backoff
;
4534 ms
<<= tcp
->tcp_timer_backoff
;
4536 if (ms
> tcp_rexmit_interval_max
) {
4537 ms
= tcp_rexmit_interval_max
;
4539 * ms is at max, decrement tcp_timer_backoff to avoid
4542 tcp
->tcp_timer_backoff
--;
4544 tcp
->tcp_ms_we_have_waited
+= ms
;
4545 if (tcp
->tcp_zero_win_probe
== 0) {
4548 TCP_TIMER_RESTART(tcp
, ms
);
4550 * This is after a timeout and tcp_rto is backed off. Set
4551 * tcp_set_timer to 1 so that next time RTO is updated, we will
4552 * restart the timer with a correct value.
4554 tcp
->tcp_set_timer
= 1;
4555 mss
= tcp
->tcp_snxt
- tcp
->tcp_suna
;
4556 if (mss
> tcp
->tcp_mss
)
4558 if (mss
> tcp
->tcp_swnd
&& tcp
->tcp_swnd
!= 0)
4559 mss
= tcp
->tcp_swnd
;
4561 if ((mp
= tcp
->tcp_xmit_head
) != NULL
) {
4562 /* use uintptr_t to suppress the gcc warning */
4563 mp
->b_prev
= (mblk_t
*)(uintptr_t)prom_gettime();
4565 mp
= tcp_xmit_mp(tcp
, mp
, mss
, NULL
, NULL
, tcp
->tcp_suna
, B_TRUE
, &mss
,
4569 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
4570 BUMP_MIB(tcp_mib
.tcpRetransSegs
);
4571 UPDATE_MIB(tcp_mib
.tcpRetransBytes
, mss
);
4572 /* Dump the packet when debugging. */
4573 TCP_DUMP_PACKET("tcp_timer", mp
);
4575 (void) ipv4_tcp_output(sock_id
, mp
);
4579 * When slow start after retransmission begins, start with
4580 * this seq no. tcp_rexmit_max marks the end of special slow
4581 * start phase. tcp_snd_burst controls how many segments
4582 * can be sent because of an ack.
4584 tcp
->tcp_rexmit_nxt
= tcp
->tcp_suna
;
4585 tcp
->tcp_snd_burst
= TCP_CWND_SS
;
4586 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
4587 (tcp
->tcp_unsent
== 0)) {
4588 tcp
->tcp_rexmit_max
= tcp
->tcp_fss
;
4590 tcp
->tcp_rexmit_max
= tcp
->tcp_snxt
;
4592 tcp
->tcp_rexmit
= B_TRUE
;
4593 tcp
->tcp_dupack_cnt
= 0;
4596 * Remove all rexmit SACK blk to start from fresh.
4598 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_notsack_list
!= NULL
) {
4599 TCP_NOTSACK_REMOVE_ALL(tcp
->tcp_notsack_list
);
4600 tcp
->tcp_num_notsack_blk
= 0;
4601 tcp
->tcp_cnt_notsack_list
= 0;
4606 * The TCP normal data output path.
4607 * NOTE: the logic of the fast path is duplicated from this function.
4610 tcp_wput_data(tcp_t
*tcp
, mblk_t
*mp
, int sock_id
)
4621 int32_t num_burst_seg
;
4623 int32_t num_sack_blk
= 0;
4624 int32_t tcp_hdr_len
;
4629 printf("tcp_wput_data(%d) ##############################\n", sock_id
);
4631 tcpstate
= tcp
->tcp_state
;
4633 /* Really tacky... but we need this for detached closes. */
4634 len
= tcp
->tcp_unsent
;
4639 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
4640 * or before a connection attempt has begun.
4642 * The following should not happen in inetboot....
4644 if (tcpstate
< TCPS_SYN_SENT
|| tcpstate
> TCPS_CLOSE_WAIT
||
4645 (tcp
->tcp_valid_bits
& TCP_FSS_VALID
) != 0) {
4646 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) != 0) {
4647 printf("tcp_wput_data: data after ordrel, %s\n",
4648 tcp_display(tcp
, NULL
, DISP_ADDR_AND_PORT
));
4656 assert((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
4657 (uintptr_t)INT_MAX
);
4658 len
= (int)(mp
->b_wptr
- mp
->b_rptr
);
4669 /* If we are the first on the list ... */
4670 if (tcp
->tcp_xmit_head
== NULL
) {
4671 tcp
->tcp_xmit_head
= mp
;
4672 tcp
->tcp_xmit_tail
= mp
;
4673 tcp
->tcp_xmit_tail_unsent
= len
;
4675 tcp
->tcp_xmit_last
->b_cont
= mp
;
4676 len
+= tcp
->tcp_unsent
;
4679 /* Tack on however many more positive length mblks we have */
4680 if ((mp1
= mp
->b_cont
) != NULL
) {
4683 assert((uintptr_t)(mp1
->b_wptr
-
4684 mp1
->b_rptr
) <= (uintptr_t)INT_MAX
);
4685 tlen
= (int)(mp1
->b_wptr
- mp1
->b_rptr
);
4687 mp
->b_cont
= mp1
->b_cont
;
4693 } while ((mp1
= mp
->b_cont
) != NULL
);
4695 tcp
->tcp_xmit_last
= mp
;
4696 tcp
->tcp_unsent
= len
;
4699 snxt
= tcp
->tcp_snxt
;
4700 xmit_tail
= tcp
->tcp_xmit_tail
;
4701 tail_unsent
= tcp
->tcp_xmit_tail_unsent
;
4704 * Note that tcp_mss has been adjusted to take into account the
4705 * timestamp option if applicable. Because SACK options do not
4706 * appear in every TCP segments and they are of variable lengths,
4707 * they cannot be included in tcp_mss. Thus we need to calculate
4708 * the actual segment length when we need to send a segment which
4709 * includes SACK options.
4711 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
4714 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
4715 tcp
->tcp_num_sack_blk
);
4716 opt_len
= num_sack_blk
* sizeof (sack_blk_t
) + TCPOPT_NOP_LEN
*
4717 2 + TCPOPT_HEADER_LEN
;
4718 mss
= tcp
->tcp_mss
- opt_len
;
4719 tcp_hdr_len
= tcp
->tcp_hdr_len
+ opt_len
;
4722 tcp_hdr_len
= tcp
->tcp_hdr_len
;
4725 if ((tcp
->tcp_suna
== snxt
) &&
4726 (prom_gettime() - tcp
->tcp_last_recv_time
) >= tcp
->tcp_rto
) {
4727 tcp
->tcp_cwnd
= MIN(tcp_slow_start_after_idle
* mss
,
4728 MIN(4 * mss
, MAX(2 * mss
, 4380 / mss
* mss
)));
4730 if (tcpstate
== TCPS_SYN_RCVD
) {
4732 * The three-way connection establishment handshake is not
4733 * complete yet. We want to queue the data for transmission
4734 * after entering ESTABLISHED state (RFC793). Setting usable to
4735 * zero cause a jump to "done" label effectively leaving data
4741 int usable_r
= tcp
->tcp_swnd
;
4744 * In the special case when cwnd is zero, which can only
4745 * happen if the connection is ECN capable, return now.
4746 * New segments is sent using tcp_timer(). The timer
4747 * is set in tcp_rput_data().
4749 if (tcp
->tcp_cwnd
== 0) {
4751 * Note that tcp_cwnd is 0 before 3-way handshake is
4754 assert(tcp
->tcp_ecn_ok
||
4755 tcp
->tcp_state
< TCPS_ESTABLISHED
);
4759 /* usable = MIN(swnd, cwnd) - unacked_bytes */
4760 if (usable_r
> tcp
->tcp_cwnd
)
4761 usable_r
= tcp
->tcp_cwnd
;
4763 /* NOTE: trouble if xmitting while SYN not acked? */
4765 usable_r
+= tcp
->tcp_suna
;
4767 /* usable = MIN(usable, unsent) */
4771 /* usable = MAX(usable, {1 for urgent, 0 for data}) */
4776 /* use uintptr_t to suppress the gcc warning */
4777 local_time
= (mblk_t
*)(uintptr_t)prom_gettime();
4780 * "Our" Nagle Algorithm. This is not the same as in the old
4781 * BSD. This is more in line with the true intent of Nagle.
4783 * The conditions are:
4784 * 1. The amount of unsent data (or amount of data which can be
4785 * sent, whichever is smaller) is less than Nagle limit.
4786 * 2. The last sent size is also less than Nagle limit.
4787 * 3. There is unack'ed data.
4788 * 4. Urgent pointer is not set. Send urgent data ignoring the
4789 * Nagle algorithm. This reduces the probability that urgent
4790 * bytes get "merged" together.
4791 * 5. The app has not closed the connection. This eliminates the
4792 * wait time of the receiving side waiting for the last piece of
4795 * If all are satisified, exit without sending anything. Note
4796 * that Nagle limit can be smaller than 1 MSS. Nagle limit is
4797 * the smaller of 1 MSS and global tcp_naglim_def (default to be
4800 if (usable
< (int)tcp
->tcp_naglim
&&
4801 tcp
->tcp_naglim
> tcp
->tcp_last_sent_len
&&
4802 snxt
!= tcp
->tcp_suna
&&
4803 !(tcp
->tcp_valid_bits
& TCP_URG_VALID
))
4806 num_burst_seg
= tcp
->tcp_snd_burst
;
4811 if (num_burst_seg
-- == 0)
4818 /* Terminate the loop */
4822 * Sender silly-window avoidance.
4823 * Ignore this if we are going to send a
4824 * zero window probe out.
4826 * TODO: force data into microscopic window ??
4827 * ==> (!pushed || (unsent > usable))
4829 if (len
< (tcp
->tcp_max_swnd
>> 1) &&
4830 (tcp
->tcp_unsent
- (snxt
- tcp
->tcp_snxt
)) > len
&&
4831 !((tcp
->tcp_valid_bits
& TCP_URG_VALID
) &&
4832 len
== 1) && (! tcp
->tcp_zero_win_probe
)) {
4834 * If the retransmit timer is not running
4835 * we start it so that we will retransmit
4836 * in the case when the the receiver has
4837 * decremented the window.
4839 if (snxt
== tcp
->tcp_snxt
&&
4840 snxt
== tcp
->tcp_suna
) {
4842 * We are not supposed to send
4843 * anything. So let's wait a little
4844 * bit longer before breaking SWS
4847 * What should the value be?
4848 * Suggestion: MAX(init rexmit time,
4851 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
4857 tcph
= tcp
->tcp_tcph
;
4859 usable
-= len
; /* Approximate - can be adjusted later */
4861 tcph
->th_flags
[0] = TH_ACK
;
4863 tcph
->th_flags
[0] = (TH_ACK
| TH_PUSH
);
4865 U32_TO_ABE32(snxt
, tcph
->th_seq
);
4867 if (tcp
->tcp_valid_bits
) {
4868 uchar_t
*prev_rptr
= xmit_tail
->b_rptr
;
4869 uint32_t prev_snxt
= tcp
->tcp_snxt
;
4871 if (tail_unsent
== 0) {
4872 assert(xmit_tail
->b_cont
!= NULL
);
4873 xmit_tail
= xmit_tail
->b_cont
;
4874 prev_rptr
= xmit_tail
->b_rptr
;
4875 tail_unsent
= (int)(xmit_tail
->b_wptr
-
4878 xmit_tail
->b_rptr
= xmit_tail
->b_wptr
-
4881 mp
= tcp_xmit_mp(tcp
, xmit_tail
, len
, NULL
, NULL
,
4882 snxt
, B_FALSE
, (uint32_t *)&len
, B_FALSE
);
4883 /* Restore tcp_snxt so we get amount sent right. */
4884 tcp
->tcp_snxt
= prev_snxt
;
4885 if (prev_rptr
== xmit_tail
->b_rptr
)
4886 xmit_tail
->b_prev
= local_time
;
4888 xmit_tail
->b_rptr
= prev_rptr
;
4896 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
4897 while (mp1
->b_cont
) {
4898 xmit_tail
= xmit_tail
->b_cont
;
4899 xmit_tail
->b_prev
= local_time
;
4902 tail_unsent
= xmit_tail
->b_wptr
- mp1
->b_wptr
;
4903 BUMP_MIB(tcp_mib
.tcpOutDataSegs
);
4904 UPDATE_MIB(tcp_mib
.tcpOutDataBytes
, len
);
4905 /* Dump the packet when debugging. */
4906 TCP_DUMP_PACKET("tcp_wput_data (valid bits)", mp
);
4907 (void) ipv4_tcp_output(sock_id
, mp
);
4912 snxt
+= len
; /* Adjust later if we don't send all of len */
4913 BUMP_MIB(tcp_mib
.tcpOutDataSegs
);
4914 UPDATE_MIB(tcp_mib
.tcpOutDataBytes
, len
);
4917 /* Are the bytes above us in flight? */
4918 rptr
= xmit_tail
->b_wptr
- tail_unsent
;
4919 if (rptr
!= xmit_tail
->b_rptr
) {
4922 tcp
->tcp_ipha
->ip_len
= htons(len
);
4923 mp
= dupb(xmit_tail
);
4930 xmit_tail
= xmit_tail
->b_cont
;
4931 assert((uintptr_t)(xmit_tail
->b_wptr
-
4932 xmit_tail
->b_rptr
) <= (uintptr_t)INT_MAX
);
4933 tail_unsent
= (int)(xmit_tail
->b_wptr
-
4938 tcp
->tcp_last_sent_len
= (ushort_t
)len
;
4941 if (tcp
->tcp_ipversion
== IPV4_VERSION
)
4942 tcp
->tcp_ipha
->ip_len
= htons(len
);
4944 xmit_tail
->b_prev
= local_time
;
4946 mp
= dupb(xmit_tail
);
4952 * There are four reasons to allocate a new hdr mblk:
4953 * 1) The bytes above us are in use by another packet
4954 * 2) We don't have good alignment
4955 * 3) The mblk is being shared
4956 * 4) We don't have enough room for a header
4958 rptr
= mp
->b_rptr
- len
;
4959 if (!OK_32PTR(rptr
) ||
4960 rptr
< mp
->b_datap
) {
4961 /* NOTE: we assume allocb returns an OK_32PTR */
4964 mp1
= allocb(tcp
->tcp_ip_hdr_len
+ TCP_MAX_HDR_LENGTH
+
4972 /* Leave room for Link Level header */
4974 rptr
= &mp
->b_rptr
[tcp_wroff_xtra
];
4975 mp
->b_wptr
= &rptr
[len
];
4978 if (tcp
->tcp_snd_ts_ok
) {
4979 /* use uintptr_t to suppress the gcc warning */
4980 U32_TO_BE32((uint32_t)(uintptr_t)local_time
,
4981 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+4);
4982 U32_TO_BE32(tcp
->tcp_ts_recent
,
4983 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+8);
4985 assert(tcp
->tcp_tcp_hdr_len
== TCP_MIN_HEADER_LENGTH
);
4990 /* Copy the template header. */
4991 dst
= (ipaddr_t
*)rptr
;
4992 src
= (ipaddr_t
*)tcp
->tcp_iphc
;
5003 len
= tcp
->tcp_hdr_len
;
5014 * Set tcph to point to the header of the outgoing packet,
5015 * not to the template header.
5017 tcph
= (tcph_t
*)(rptr
+ tcp
->tcp_ip_hdr_len
);
5020 * Set the ECN info in the TCP header if it is not a zero
5021 * window probe. Zero window probe is only sent in
5022 * tcp_wput_data() and tcp_timer().
5024 if (tcp
->tcp_ecn_ok
&& !tcp
->tcp_zero_win_probe
) {
5027 if (tcp
->tcp_ecn_echo_on
)
5028 tcph
->th_flags
[0] |= TH_ECE
;
5029 if (tcp
->tcp_cwr
&& !tcp
->tcp_ecn_cwr_sent
) {
5030 tcph
->th_flags
[0] |= TH_CWR
;
5031 tcp
->tcp_ecn_cwr_sent
= B_TRUE
;
5035 /* Fill in SACK options */
5036 if (num_sack_blk
> 0) {
5037 uchar_t
*wptr
= rptr
+ tcp
->tcp_hdr_len
;
5041 wptr
[0] = TCPOPT_NOP
;
5042 wptr
[1] = TCPOPT_NOP
;
5043 wptr
[2] = TCPOPT_SACK
;
5044 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
5045 sizeof (sack_blk_t
);
5046 wptr
+= TCPOPT_REAL_SACK_LEN
;
5048 tmp
= tcp
->tcp_sack_list
;
5049 for (i
= 0; i
< num_sack_blk
; i
++) {
5050 U32_TO_BE32(tmp
[i
].begin
, wptr
);
5051 wptr
+= sizeof (tcp_seq
);
5052 U32_TO_BE32(tmp
[i
].end
, wptr
);
5053 wptr
+= sizeof (tcp_seq
);
5055 tcph
->th_offset_and_rsrvd
[0] += ((num_sack_blk
* 2 + 1)
5064 * If we're a little short, tack on more mblks
5065 * as long as we don't need to split an mblk.
5067 while (tail_unsent
< 0 &&
5068 tail_unsent
+ (int)(xmit_tail
->b_cont
->b_wptr
-
5069 xmit_tail
->b_cont
->b_rptr
) <= 0) {
5070 xmit_tail
= xmit_tail
->b_cont
;
5071 /* Stash for rtt use later */
5072 xmit_tail
->b_prev
= local_time
;
5073 mp1
->b_cont
= dupb(xmit_tail
);
5075 assert((uintptr_t)(xmit_tail
->b_wptr
-
5076 xmit_tail
->b_rptr
) <= (uintptr_t)INT_MAX
);
5077 tail_unsent
+= (int)(xmit_tail
->b_wptr
-
5084 /* Trim back any surplus on the last mblk */
5085 if (tail_unsent
> 0)
5086 mp1
->b_wptr
-= tail_unsent
;
5087 if (tail_unsent
< 0) {
5091 * We did not send everything we could in
5092 * order to preserve mblk boundaries.
5094 usable
-= tail_unsent
;
5095 snxt
+= tail_unsent
;
5096 tcp
->tcp_last_sent_len
+= tail_unsent
;
5097 UPDATE_MIB(tcp_mib
.tcpOutDataBytes
,
5099 /* Adjust the IP length field. */
5100 ip_len
= ntohs(((struct ip
*)rptr
)->ip_len
) +
5102 ((struct ip
*)rptr
)->ip_len
= htons(ip_len
);
5111 * Performance hit! We need to pullup the whole message
5112 * in order to do checksum and for the MAC output routine.
5114 if (mp
->b_cont
!= NULL
) {
5117 printf("Multiple mblk %d\n", msgdsize(mp
));
5119 new_mp
= allocb(msgdsize(mp
) + tcp_wroff_xtra
, 0);
5120 new_mp
->b_rptr
+= tcp_wroff_xtra
;
5121 new_mp
->b_wptr
= new_mp
->b_rptr
;
5122 while (mp
!= NULL
) {
5123 mp_size
= mp
->b_wptr
- mp
->b_rptr
;
5124 bcopy(mp
->b_rptr
, new_mp
->b_wptr
, mp_size
);
5125 new_mp
->b_wptr
+= mp_size
;
5132 ((struct ip
*)mp
->b_rptr
)->ip_ttl
= (uint8_t)tcp_ipv4_ttl
;
5133 TCP_DUMP_PACKET("tcp_wput_data", mp
);
5134 (void) ipv4_tcp_output(sock_id
, mp
);
5138 /* Pretend that all we were trying to send really got sent */
5139 if (tail_unsent
< 0) {
5141 xmit_tail
= xmit_tail
->b_cont
;
5142 xmit_tail
->b_prev
= local_time
;
5143 assert((uintptr_t)(xmit_tail
->b_wptr
-
5144 xmit_tail
->b_rptr
) <= (uintptr_t)INT_MAX
);
5145 tail_unsent
+= (int)(xmit_tail
->b_wptr
-
5147 } while (tail_unsent
< 0);
5150 tcp
->tcp_xmit_tail
= xmit_tail
;
5151 tcp
->tcp_xmit_tail_unsent
= tail_unsent
;
5152 len
= tcp
->tcp_snxt
- snxt
;
5155 * If new data was sent, need to update the notsack
5156 * list, which is, afterall, data blocks that have
5157 * not been sack'ed by the receiver. New data is
5160 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_notsack_list
!= NULL
) {
5161 /* len is a negative value. */
5162 tcp
->tcp_pipe
-= len
;
5163 tcp_notsack_update(&(tcp
->tcp_notsack_list
),
5164 tcp
->tcp_snxt
, snxt
,
5165 &(tcp
->tcp_num_notsack_blk
),
5166 &(tcp
->tcp_cnt_notsack_list
));
5168 tcp
->tcp_snxt
= snxt
+ tcp
->tcp_fin_sent
;
5169 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
5170 tcp
->tcp_rack_cnt
= 0;
5171 if ((snxt
+ len
) == tcp
->tcp_suna
) {
5172 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
5175 * Note that len is the amount we just sent but with a negative
5176 * sign. We update tcp_unsent here since we may come back to
5177 * tcp_wput_data from tcp_state_wait.
5179 len
+= tcp
->tcp_unsent
;
5180 tcp
->tcp_unsent
= len
;
5183 * Let's wait till all the segments have been acked, since we
5184 * don't have a timer.
5186 (void) tcp_state_wait(sock_id
, tcp
, TCPS_ALL_ACKED
);
5188 } else if (snxt
== tcp
->tcp_suna
&& tcp
->tcp_swnd
== 0) {
5190 * Didn't send anything. Make sure the timer is running
5191 * so that we will probe a zero window.
5193 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
5196 /* Note that len is the amount we just sent but with a negative sign */
5197 len
+= tcp
->tcp_unsent
;
5198 tcp
->tcp_unsent
= len
;
5203 tcp_time_wait_processing(tcp_t
*tcp
, mblk_t
*mp
,
5204 uint32_t seg_seq
, uint32_t seg_ack
, int seg_len
, tcph_t
*tcph
,
5207 int32_t bytes_acked
;
5212 uint32_t new_swnd
= 0;
5215 printf("Time wait processing called ###############3\n");
5218 /* Just make sure we send the right sock_id to tcp_clean_death */
5219 if ((sockets
[sock_id
].pcb
== NULL
) || (sockets
[sock_id
].pcb
!= tcp
))
5222 flags
= (unsigned int)tcph
->th_flags
[0] & 0xFF;
5223 new_swnd
= BE16_TO_U16(tcph
->th_win
) <<
5224 ((tcph
->th_flags
[0] & TH_SYN
) ? 0 : tcp
->tcp_snd_ws
);
5225 if (tcp
->tcp_snd_ts_ok
) {
5226 if (!tcp_paws_check(tcp
, tcph
, &tcpopt
)) {
5228 tcp_xmit_ctl(NULL
, tcp
, NULL
, tcp
->tcp_snxt
,
5229 tcp
->tcp_rnxt
, TH_ACK
, 0, -1);
5233 gap
= seg_seq
- tcp
->tcp_rnxt
;
5234 rgap
= tcp
->tcp_rwnd
- (gap
+ seg_len
);
5236 BUMP_MIB(tcp_mib
.tcpInDataDupSegs
);
5237 UPDATE_MIB(tcp_mib
.tcpInDataDupBytes
,
5238 (seg_len
> -gap
? -gap
: seg_len
));
5240 if (seg_len
< 0 || (seg_len
== 0 && !(flags
& TH_FIN
))) {
5241 if (flags
& TH_RST
) {
5245 if ((flags
& TH_FIN
) && seg_len
== -1) {
5247 * When TCP receives a duplicate FIN in
5248 * TIME_WAIT state, restart the 2 MSL timer.
5249 * See page 73 in RFC 793. Make sure this TCP
5250 * is already on the TIME_WAIT list. If not,
5251 * just restart the timer.
5253 tcp_time_wait_remove(tcp
);
5254 tcp_time_wait_append(tcp
);
5255 TCP_TIMER_RESTART(tcp
, tcp_time_wait_interval
);
5256 tcp_xmit_ctl(NULL
, tcp
, NULL
, tcp
->tcp_snxt
,
5257 tcp
->tcp_rnxt
, TH_ACK
, 0, -1);
5261 flags
|= TH_ACK_NEEDED
;
5266 /* Fix seg_seq, and chew the gap off the front. */
5267 seg_seq
= tcp
->tcp_rnxt
;
5270 if ((flags
& TH_SYN
) && gap
> 0 && rgap
< 0) {
5272 * Make sure that when we accept the connection, pick
5273 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the
5276 * The next ISS generated is equal to tcp_iss_incr_extra
5277 * + ISS_INCR/2 + other components depending on the
5278 * value of tcp_strong_iss. We pre-calculate the new
5279 * ISS here and compare with tcp_snxt to determine if
5280 * we need to make adjustment to tcp_iss_incr_extra.
5282 * Note that since we are now in the global queue
5283 * perimeter and need to do a lateral_put() to the
5284 * listener queue, there can be other connection requests/
5285 * attempts while the lateral_put() is going on. That
5286 * means what we calculate here may not be correct. This
5287 * is extremely difficult to solve unless TCP and IP
5288 * modules are merged and there is no perimeter, but just
5289 * locks. The above calculation is ugly and is a
5290 * waste of CPU cycles...
5292 uint32_t new_iss
= tcp_iss_incr_extra
;
5295 /* Add time component and min random (i.e. 1). */
5296 new_iss
+= (prom_gettime() >> ISS_NSEC_SHT
) + 1;
5297 if ((adj
= (int32_t)(tcp
->tcp_snxt
- new_iss
)) > 0) {
5299 * New ISS not guaranteed to be ISS_INCR/2
5300 * ahead of the current tcp_snxt, so add the
5301 * difference to tcp_iss_incr_extra.
5303 tcp_iss_incr_extra
+= adj
;
5305 tcp_clean_death(sock_id
, tcp
, 0);
5308 * This is a passive open. Right now we do not
5316 * rgap is the amount of stuff received out of window. A negative
5317 * value is the amount out of window.
5320 BUMP_MIB(tcp_mib
.tcpInDataPastWinSegs
);
5321 UPDATE_MIB(tcp_mib
.tcpInDataPastWinBytes
, -rgap
);
5322 /* Fix seg_len and make sure there is something left. */
5325 if (flags
& TH_RST
) {
5329 flags
|= TH_ACK_NEEDED
;
5335 * Check whether we can update tcp_ts_recent. This test is
5336 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
5337 * Extensions for High Performance: An Update", Internet Draft.
5339 if (tcp
->tcp_snd_ts_ok
&&
5340 TSTMP_GEQ(tcpopt
.tcp_opt_ts_val
, tcp
->tcp_ts_recent
) &&
5341 SEQ_LEQ(seg_seq
, tcp
->tcp_rack
)) {
5342 tcp
->tcp_ts_recent
= tcpopt
.tcp_opt_ts_val
;
5343 tcp
->tcp_last_rcv_lbolt
= prom_gettime();
5346 if (seg_seq
!= tcp
->tcp_rnxt
&& seg_len
> 0) {
5347 /* Always ack out of order packets */
5348 flags
|= TH_ACK_NEEDED
;
5350 } else if (seg_len
> 0) {
5351 BUMP_MIB(tcp_mib
.tcpInDataInorderSegs
);
5352 UPDATE_MIB(tcp_mib
.tcpInDataInorderBytes
, seg_len
);
5354 if (flags
& TH_RST
) {
5356 (void) tcp_clean_death(sock_id
, tcp
, 0);
5359 if (flags
& TH_SYN
) {
5361 tcp_xmit_ctl("TH_SYN", tcp
, NULL
, seg_ack
, seg_seq
+ 1,
5362 TH_RST
|TH_ACK
, 0, -1);
5364 * Do not delete the TCP structure if it is in
5365 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13.
5370 if (flags
& TH_ACK
) {
5371 bytes_acked
= (int)(seg_ack
- tcp
->tcp_suna
);
5372 if (bytes_acked
<= 0) {
5373 if (bytes_acked
== 0 && seg_len
== 0 &&
5374 new_swnd
== tcp
->tcp_swnd
)
5375 BUMP_MIB(tcp_mib
.tcpInDupAck
);
5377 /* Acks something not sent */
5378 flags
|= TH_ACK_NEEDED
;
5382 if (flags
& TH_ACK_NEEDED
) {
5384 * Time to send an ack for some reason.
5386 tcp_xmit_ctl(NULL
, tcp
, NULL
, tcp
->tcp_snxt
,
5387 tcp
->tcp_rnxt
, TH_ACK
, 0, -1);
5392 tcp_init_values(tcp_t
*tcp
, struct inetboot_socket
*isp
)
5396 tcp
->tcp_family
= AF_INET
;
5397 tcp
->tcp_ipversion
= IPV4_VERSION
;
5400 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
5401 * will be close to tcp_rexmit_interval_initial. By doing this, we
5402 * allow the algorithm to adjust slowly to large fluctuations of RTT
5403 * during first few transmissions of a connection as seen in slow
5406 tcp
->tcp_rtt_sa
= tcp_rexmit_interval_initial
<< 2;
5407 tcp
->tcp_rtt_sd
= tcp_rexmit_interval_initial
>> 1;
5408 tcp
->tcp_rto
= (tcp
->tcp_rtt_sa
>> 3) + tcp
->tcp_rtt_sd
+
5409 tcp_rexmit_interval_extra
+ (tcp
->tcp_rtt_sa
>> 5) +
5410 tcp_conn_grace_period
;
5411 if (tcp
->tcp_rto
< tcp_rexmit_interval_min
)
5412 tcp
->tcp_rto
= tcp_rexmit_interval_min
;
5413 tcp
->tcp_timer_backoff
= 0;
5414 tcp
->tcp_ms_we_have_waited
= 0;
5415 tcp
->tcp_last_recv_time
= prom_gettime();
5416 tcp
->tcp_cwnd_max
= tcp_cwnd_max_
;
5417 tcp
->tcp_snd_burst
= TCP_CWND_INFINITE
;
5418 tcp
->tcp_cwnd_ssthresh
= TCP_MAX_LARGEWIN
;
5419 /* For Ethernet, the mtu returned is actually 1550... */
5420 if (mac_get_type() == IFT_ETHER
) {
5421 tcp
->tcp_if_mtu
= mac_get_mtu() - 50;
5423 tcp
->tcp_if_mtu
= mac_get_mtu();
5425 tcp
->tcp_mss
= tcp
->tcp_if_mtu
;
5427 tcp
->tcp_first_timer_threshold
= tcp_ip_notify_interval
;
5428 tcp
->tcp_first_ctimer_threshold
= tcp_ip_notify_cinterval
;
5429 tcp
->tcp_second_timer_threshold
= tcp_ip_abort_interval
;
5431 * Fix it to tcp_ip_abort_linterval later if it turns out to be a
5434 tcp
->tcp_second_ctimer_threshold
= tcp_ip_abort_cinterval
;
5436 tcp
->tcp_naglim
= tcp_naglim_def
;
5438 /* NOTE: ISS is now set in tcp_adapt_ire(). */
5440 /* Initialize the header template */
5441 if (tcp
->tcp_ipversion
== IPV4_VERSION
) {
5442 err
= tcp_header_init_ipv4(tcp
);
5448 * Init the window scale to the max so tcp_rwnd_set() won't pare
5449 * down tcp_rwnd. tcp_adapt_ire() will set the right value later.
5451 tcp
->tcp_rcv_ws
= TCP_MAX_WINSHIFT
;
5452 tcp
->tcp_xmit_lowater
= tcp_xmit_lowat
;
5454 tcp
->tcp_xmit_hiwater
= isp
->so_sndbuf
;
5455 tcp
->tcp_rwnd
= isp
->so_rcvbuf
;
5456 tcp
->tcp_rwnd_max
= isp
->so_rcvbuf
;
5458 tcp
->tcp_state
= TCPS_IDLE
;
5463 * Initialize the IPv4 header. Loses any record of any IP options.
5466 tcp_header_init_ipv4(tcp_t
*tcp
)
5471 * This is a simple initialization. If there's
5472 * already a template, it should never be too small,
5473 * so reuse it. Otherwise, allocate space for the new one.
5475 if (tcp
->tcp_iphc
!= NULL
) {
5476 assert(tcp
->tcp_iphc_len
>= TCP_MAX_COMBINED_HEADER_LENGTH
);
5477 bzero(tcp
->tcp_iphc
, tcp
->tcp_iphc_len
);
5479 tcp
->tcp_iphc_len
= TCP_MAX_COMBINED_HEADER_LENGTH
;
5480 tcp
->tcp_iphc
= bkmem_zalloc(tcp
->tcp_iphc_len
);
5481 if (tcp
->tcp_iphc
== NULL
) {
5482 tcp
->tcp_iphc_len
= 0;
5486 tcp
->tcp_ipha
= (struct ip
*)tcp
->tcp_iphc
;
5487 tcp
->tcp_ipversion
= IPV4_VERSION
;
5490 * Note that it does not include TCP options yet. It will
5491 * after the connection is established.
5493 tcp
->tcp_hdr_len
= sizeof (struct ip
) + sizeof (tcph_t
);
5494 tcp
->tcp_tcp_hdr_len
= sizeof (tcph_t
);
5495 tcp
->tcp_ip_hdr_len
= sizeof (struct ip
);
5496 tcp
->tcp_ipha
->ip_v
= IP_VERSION
;
5497 /* We don't support IP options... */
5498 tcp
->tcp_ipha
->ip_hl
= IP_SIMPLE_HDR_LENGTH_IN_WORDS
;
5499 tcp
->tcp_ipha
->ip_p
= IPPROTO_TCP
;
5500 /* We are not supposed to do PMTU discovery... */
5501 tcp
->tcp_ipha
->ip_sum
= 0;
5503 tcph
= (tcph_t
*)(tcp
->tcp_iphc
+ sizeof (struct ip
));
5504 tcp
->tcp_tcph
= tcph
;
5505 tcph
->th_offset_and_rsrvd
[0] = (5 << 4);
5510 * Send out a control packet on the tcp connection specified. This routine
5511 * is typically called where we need a simple ACK or RST generated.
5513 * This function is called with or without a mp.
5516 tcp_xmit_ctl(char *str
, tcp_t
*tcp
, mblk_t
*mp
, uint32_t seq
,
5517 uint32_t ack
, int ctl
, uint_t ip_hdr_len
, int sock_id
)
5521 struct ip
*iph
= NULL
;
5525 tcp_hdr_len
= tcp
->tcp_hdr_len
;
5526 tcp_ip_hdr_len
= tcp
->tcp_ip_hdr_len
;
5529 assert(ip_hdr_len
!= 0);
5531 tcph
= (tcph_t
*)(rptr
+ ip_hdr_len
);
5532 /* Don't reply to a RST segment. */
5533 if (tcph
->th_flags
[0] & TH_RST
) {
5540 assert(ip_hdr_len
== 0);
5542 /* If a text string is passed in with the request, print it out. */
5544 dprintf("tcp_xmit_ctl(%d): '%s', seq 0x%x, ack 0x%x, "
5545 "ctl 0x%x\n", sock_id
, str
, seq
, ack
, ctl
);
5547 mp
= allocb(tcp_ip_hdr_len
+ TCP_MAX_HDR_LENGTH
+ tcp_wroff_xtra
, 0);
5549 dprintf("tcp_xmit_ctl(%d): Cannot allocate memory\n", sock_id
);
5552 rptr
= &mp
->b_rptr
[tcp_wroff_xtra
];
5554 mp
->b_wptr
= &rptr
[tcp_hdr_len
];
5555 bcopy(tcp
->tcp_iphc
, rptr
, tcp_hdr_len
);
5557 iph
= (struct ip
*)rptr
;
5558 iph
->ip_len
= htons(tcp_hdr_len
);
5560 tcph
= (tcph_t
*)&rptr
[tcp_ip_hdr_len
];
5561 tcph
->th_flags
[0] = (uint8_t)ctl
;
5563 BUMP_MIB(tcp_mib
.tcpOutRsts
);
5564 BUMP_MIB(tcp_mib
.tcpOutControl
);
5566 * Don't send TSopt w/ TH_RST packets per RFC 1323.
5568 if (tcp
->tcp_snd_ts_ok
&& tcp
->tcp_state
> TCPS_SYN_SENT
) {
5569 mp
->b_wptr
= &rptr
[tcp_hdr_len
- TCPOPT_REAL_TS_LEN
];
5570 *(mp
->b_wptr
) = TCPOPT_EOL
;
5571 iph
->ip_len
= htons(tcp_hdr_len
-
5572 TCPOPT_REAL_TS_LEN
);
5573 tcph
->th_offset_and_rsrvd
[0] -= (3 << 4);
5577 uint32_t now
= prom_gettime();
5579 if (tcp
->tcp_snd_ts_ok
) {
5581 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+4);
5582 U32_TO_BE32(tcp
->tcp_ts_recent
,
5583 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+8);
5585 tcp
->tcp_rack
= ack
;
5586 tcp
->tcp_rack_cnt
= 0;
5587 BUMP_MIB(tcp_mib
.tcpOutAck
);
5589 BUMP_MIB(tcp_mib
.tcpOutSegs
);
5590 U32_TO_BE32(seq
, tcph
->th_seq
);
5591 U32_TO_BE32(ack
, tcph
->th_ack
);
5594 iph
->ip_ttl
= (uint8_t)tcp_ipv4_ttl
;
5595 TCP_DUMP_PACKET("tcp_xmit_ctl", mp
);
5596 (void) ipv4_tcp_output(sock_id
, mp
);
5600 /* Generate an ACK-only (no data) segment for a TCP endpoint */
5602 tcp_ack_mp(tcp_t
*tcp
)
5604 if (tcp
->tcp_valid_bits
) {
5606 * For the complex case where we have to send some
5607 * controls (FIN or SYN), let tcp_xmit_mp do it.
5608 * When sending an ACK-only segment (no data)
5609 * into a zero window, always set the seq number to
5610 * suna, since snxt will be extended past the window.
5611 * If we used snxt, the receiver might consider the ACK
5614 return (tcp_xmit_mp(tcp
, NULL
, 0, NULL
, NULL
,
5615 (tcp
->tcp_zero_win_probe
) ?
5617 tcp
->tcp_snxt
, B_FALSE
, NULL
, B_FALSE
));
5619 /* Generate a simple ACK */
5623 int32_t tcp_hdr_len
;
5624 int32_t num_sack_blk
= 0;
5625 int32_t sack_opt_len
;
5628 * Allocate space for TCP + IP headers
5629 * and link-level header
5631 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
5632 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
5633 tcp
->tcp_num_sack_blk
);
5634 sack_opt_len
= num_sack_blk
* sizeof (sack_blk_t
) +
5635 TCPOPT_NOP_LEN
* 2 + TCPOPT_HEADER_LEN
;
5636 tcp_hdr_len
= tcp
->tcp_hdr_len
+ sack_opt_len
;
5638 tcp_hdr_len
= tcp
->tcp_hdr_len
;
5640 mp1
= allocb(tcp_hdr_len
+ tcp_wroff_xtra
, 0);
5644 /* copy in prototype TCP + IP header */
5645 rptr
= mp1
->b_rptr
+ tcp_wroff_xtra
;
5647 mp1
->b_wptr
= rptr
+ tcp_hdr_len
;
5648 bcopy(tcp
->tcp_iphc
, rptr
, tcp
->tcp_hdr_len
);
5650 tcph
= (tcph_t
*)&rptr
[tcp
->tcp_ip_hdr_len
];
5653 * Set the TCP sequence number.
5654 * When sending an ACK-only segment (no data)
5655 * into a zero window, always set the seq number to
5656 * suna, since snxt will be extended past the window.
5657 * If we used snxt, the receiver might consider the ACK
5660 U32_TO_ABE32((tcp
->tcp_zero_win_probe
) ?
5661 tcp
->tcp_suna
: tcp
->tcp_snxt
, tcph
->th_seq
);
5663 /* Set up the TCP flag field. */
5664 tcph
->th_flags
[0] = (uchar_t
)TH_ACK
;
5665 if (tcp
->tcp_ecn_echo_on
)
5666 tcph
->th_flags
[0] |= TH_ECE
;
5668 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
5669 tcp
->tcp_rack_cnt
= 0;
5671 /* fill in timestamp option if in use */
5672 if (tcp
->tcp_snd_ts_ok
) {
5673 uint32_t llbolt
= (uint32_t)prom_gettime();
5676 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+4);
5677 U32_TO_BE32(tcp
->tcp_ts_recent
,
5678 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+8);
5681 /* Fill in SACK options */
5682 if (num_sack_blk
> 0) {
5683 uchar_t
*wptr
= (uchar_t
*)tcph
+ tcp
->tcp_tcp_hdr_len
;
5687 wptr
[0] = TCPOPT_NOP
;
5688 wptr
[1] = TCPOPT_NOP
;
5689 wptr
[2] = TCPOPT_SACK
;
5690 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
5691 sizeof (sack_blk_t
);
5692 wptr
+= TCPOPT_REAL_SACK_LEN
;
5694 tmp
= tcp
->tcp_sack_list
;
5695 for (i
= 0; i
< num_sack_blk
; i
++) {
5696 U32_TO_BE32(tmp
[i
].begin
, wptr
);
5697 wptr
+= sizeof (tcp_seq
);
5698 U32_TO_BE32(tmp
[i
].end
, wptr
);
5699 wptr
+= sizeof (tcp_seq
);
5701 tcph
->th_offset_and_rsrvd
[0] += ((num_sack_blk
* 2 + 1)
5705 ((struct ip
*)rptr
)->ip_len
= htons(tcp_hdr_len
);
5707 ((struct ip
*)rptr
)->ip_ttl
= (uint8_t)tcp_ipv4_ttl
;
5713 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
5714 * ip and tcp header ready to pass down to IP. If the mp passed in is
5715 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
5716 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
5717 * otherwise it will dup partial mblks.)
5718 * Otherwise, an appropriate ACK packet will be generated. This
5719 * routine is not usually called to send new data for the first time. It
5720 * is mostly called out of the timer for retransmits, and to generate ACKs.
5722 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
5723 * be adjusted by *offset. And after dupb(), the offset and the ending mblk
5724 * of the original mblk chain will be returned in *offset and *end_mp.
5727 tcp_xmit_mp(tcp_t
*tcp
, mblk_t
*mp
, int32_t max_to_send
, int32_t *offset
,
5728 mblk_t
**end_mp
, uint32_t seq
, boolean_t sendall
, uint32_t *seg_len
,
5739 int32_t num_sack_blk
= 0;
5740 int32_t sack_opt_len
= 0;
5742 /* Allocate for our maximum TCP header + link-level */
5743 mp1
= allocb(tcp
->tcp_ip_hdr_len
+ TCP_MAX_HDR_LENGTH
+
5750 * Note that tcp_mss has been adjusted to take into account the
5751 * timestamp option if applicable. Because SACK options do not
5752 * appear in every TCP segments and they are of variable lengths,
5753 * they cannot be included in tcp_mss. Thus we need to calculate
5754 * the actual segment length when we need to send a segment which
5755 * includes SACK options.
5757 if (tcp
->tcp_snd_sack_ok
&& tcp
->tcp_num_sack_blk
> 0) {
5758 num_sack_blk
= MIN(tcp
->tcp_max_sack_blk
,
5759 tcp
->tcp_num_sack_blk
);
5760 sack_opt_len
= num_sack_blk
* sizeof (sack_blk_t
) +
5761 TCPOPT_NOP_LEN
* 2 + TCPOPT_HEADER_LEN
;
5762 if (max_to_send
+ sack_opt_len
> tcp
->tcp_mss
)
5763 max_to_send
-= sack_opt_len
;
5766 if (offset
!= NULL
) {
5768 /* We use offset as an indicator that end_mp is not NULL. */
5771 for (mp2
= mp1
; mp
&& data_length
!= max_to_send
; mp
= mp
->b_cont
) {
5772 /* This could be faster with cooperation from downstream */
5773 if (mp2
!= mp1
&& !sendall
&&
5774 data_length
+ (int)(mp
->b_wptr
- mp
->b_rptr
) >
5777 * Don't send the next mblk since the whole mblk
5781 mp2
->b_cont
= dupb(mp
);
5788 assert((uintptr_t)(mp2
->b_wptr
- mp2
->b_rptr
) <=
5789 (uintptr_t)INT_MAX
);
5791 data_length
+= (int)(mp2
->b_wptr
- mp2
->b_rptr
);
5792 if (data_length
> max_to_send
) {
5793 mp2
->b_wptr
-= data_length
- max_to_send
;
5794 data_length
= max_to_send
;
5795 off
= mp2
->b_wptr
- mp
->b_rptr
;
5801 if (offset
!= NULL
) {
5805 if (seg_len
!= NULL
) {
5806 *seg_len
= data_length
;
5809 rptr
= mp1
->b_rptr
+ tcp_wroff_xtra
;
5811 mp1
->b_wptr
= rptr
+ tcp
->tcp_hdr_len
+ sack_opt_len
;
5812 bcopy(tcp
->tcp_iphc
, rptr
, tcp
->tcp_hdr_len
);
5813 tcph
= (tcph_t
*)&rptr
[tcp
->tcp_ip_hdr_len
];
5814 U32_TO_ABE32(seq
, tcph
->th_seq
);
5817 * Use tcp_unsent to determine if the PUSH bit should be used assumes
5818 * that this function was called from tcp_wput_data. Thus, when called
5819 * to retransmit data the setting of the PUSH bit may appear some
5820 * what random in that it might get set when it should not. This
5821 * should not pose any performance issues.
5823 if (data_length
!= 0 && (tcp
->tcp_unsent
== 0 ||
5824 tcp
->tcp_unsent
== data_length
)) {
5825 flags
= TH_ACK
| TH_PUSH
;
5830 if (tcp
->tcp_ecn_ok
) {
5831 if (tcp
->tcp_ecn_echo_on
)
5835 * Only set ECT bit and ECN_CWR if a segment contains new data.
5836 * There is no TCP flow control for non-data segments, and
5837 * only data segment is transmitted reliably.
5839 if (data_length
> 0 && !rexmit
) {
5841 if (tcp
->tcp_cwr
&& !tcp
->tcp_ecn_cwr_sent
) {
5843 tcp
->tcp_ecn_cwr_sent
= B_TRUE
;
5848 if (tcp
->tcp_valid_bits
) {
5851 if ((tcp
->tcp_valid_bits
& TCP_ISS_VALID
) &&
5852 seq
== tcp
->tcp_iss
) {
5856 * Tack on the MSS option. It is always needed
5857 * for both active and passive open.
5860 wptr
[0] = TCPOPT_MAXSEG
;
5861 wptr
[1] = TCPOPT_MAXSEG_LEN
;
5864 * MSS option value should be interface MTU - MIN
5867 u1
= tcp
->tcp_if_mtu
- IP_SIMPLE_HDR_LENGTH
-
5868 TCP_MIN_HEADER_LENGTH
;
5869 U16_TO_BE16(u1
, wptr
);
5870 mp1
->b_wptr
= wptr
+ 2;
5871 /* Update the offset to cover the additional word */
5872 tcph
->th_offset_and_rsrvd
[0] += (1 << 4);
5875 * Note that the following way of filling in
5876 * TCP options are not optimal. Some NOPs can
5877 * be saved. But there is no need at this time
5878 * to optimize it. When it is needed, we will
5881 switch (tcp
->tcp_state
) {
5885 if (tcp
->tcp_snd_ws_ok
) {
5887 wptr
[0] = TCPOPT_NOP
;
5888 wptr
[1] = TCPOPT_WSCALE
;
5889 wptr
[2] = TCPOPT_WS_LEN
;
5890 wptr
[3] = (uchar_t
)tcp
->tcp_rcv_ws
;
5891 mp1
->b_wptr
+= TCPOPT_REAL_WS_LEN
;
5892 tcph
->th_offset_and_rsrvd
[0] +=
5896 if (tcp
->tcp_snd_ts_ok
) {
5899 llbolt
= prom_gettime();
5901 wptr
[0] = TCPOPT_NOP
;
5902 wptr
[1] = TCPOPT_NOP
;
5903 wptr
[2] = TCPOPT_TSTAMP
;
5904 wptr
[3] = TCPOPT_TSTAMP_LEN
;
5906 U32_TO_BE32(llbolt
, wptr
);
5908 assert(tcp
->tcp_ts_recent
== 0);
5909 U32_TO_BE32(0L, wptr
);
5910 mp1
->b_wptr
+= TCPOPT_REAL_TS_LEN
;
5911 tcph
->th_offset_and_rsrvd
[0] +=
5915 if (tcp
->tcp_snd_sack_ok
) {
5917 wptr
[0] = TCPOPT_NOP
;
5918 wptr
[1] = TCPOPT_NOP
;
5919 wptr
[2] = TCPOPT_SACK_PERMITTED
;
5920 wptr
[3] = TCPOPT_SACK_OK_LEN
;
5921 mp1
->b_wptr
+= TCPOPT_REAL_SACK_OK_LEN
;
5922 tcph
->th_offset_and_rsrvd
[0] +=
5927 * Set up all the bits to tell other side
5928 * we are ECN capable.
5930 if (tcp
->tcp_ecn_ok
) {
5931 flags
|= (TH_ECE
| TH_CWR
);
5937 if (tcp
->tcp_snd_ws_ok
) {
5939 wptr
[0] = TCPOPT_NOP
;
5940 wptr
[1] = TCPOPT_WSCALE
;
5941 wptr
[2] = TCPOPT_WS_LEN
;
5942 wptr
[3] = (uchar_t
)tcp
->tcp_rcv_ws
;
5943 mp1
->b_wptr
+= TCPOPT_REAL_WS_LEN
;
5944 tcph
->th_offset_and_rsrvd
[0] += (1 << 4);
5947 if (tcp
->tcp_snd_sack_ok
) {
5949 wptr
[0] = TCPOPT_NOP
;
5950 wptr
[1] = TCPOPT_NOP
;
5951 wptr
[2] = TCPOPT_SACK_PERMITTED
;
5952 wptr
[3] = TCPOPT_SACK_OK_LEN
;
5953 mp1
->b_wptr
+= TCPOPT_REAL_SACK_OK_LEN
;
5954 tcph
->th_offset_and_rsrvd
[0] +=
5959 * If the other side is ECN capable, reply
5960 * that we are also ECN capable.
5962 if (tcp
->tcp_ecn_ok
) {
5969 /* allocb() of adequate mblk assures space */
5970 assert((uintptr_t)(mp1
->b_wptr
-
5971 mp1
->b_rptr
) <= (uintptr_t)INT_MAX
);
5973 BUMP_MIB(tcp_mib
.tcpOutControl
);
5975 if ((tcp
->tcp_valid_bits
& TCP_FSS_VALID
) &&
5976 (seq
+ data_length
) == tcp
->tcp_fss
) {
5977 if (!tcp
->tcp_fin_acked
) {
5979 BUMP_MIB(tcp_mib
.tcpOutControl
);
5981 if (!tcp
->tcp_fin_sent
) {
5982 tcp
->tcp_fin_sent
= B_TRUE
;
5983 switch (tcp
->tcp_state
) {
5985 case TCPS_ESTABLISHED
:
5986 tcp
->tcp_state
= TCPS_FIN_WAIT_1
;
5988 case TCPS_CLOSE_WAIT
:
5989 tcp
->tcp_state
= TCPS_LAST_ACK
;
5992 if (tcp
->tcp_suna
== tcp
->tcp_snxt
)
5993 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
5994 tcp
->tcp_snxt
= tcp
->tcp_fss
+ 1;
5998 tcph
->th_flags
[0] = (uchar_t
)flags
;
5999 tcp
->tcp_rack
= tcp
->tcp_rnxt
;
6000 tcp
->tcp_rack_cnt
= 0;
6002 if (tcp
->tcp_snd_ts_ok
) {
6003 if (tcp
->tcp_state
!= TCPS_SYN_SENT
) {
6004 uint32_t llbolt
= prom_gettime();
6007 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+4);
6008 U32_TO_BE32(tcp
->tcp_ts_recent
,
6009 (char *)tcph
+TCP_MIN_HEADER_LENGTH
+8);
6013 if (num_sack_blk
> 0) {
6014 uchar_t
*wptr
= (uchar_t
*)tcph
+ tcp
->tcp_tcp_hdr_len
;
6018 wptr
[0] = TCPOPT_NOP
;
6019 wptr
[1] = TCPOPT_NOP
;
6020 wptr
[2] = TCPOPT_SACK
;
6021 wptr
[3] = TCPOPT_HEADER_LEN
+ num_sack_blk
*
6022 sizeof (sack_blk_t
);
6023 wptr
+= TCPOPT_REAL_SACK_LEN
;
6025 tmp
= tcp
->tcp_sack_list
;
6026 for (i
= 0; i
< num_sack_blk
; i
++) {
6027 U32_TO_BE32(tmp
[i
].begin
, wptr
);
6028 wptr
+= sizeof (tcp_seq
);
6029 U32_TO_BE32(tmp
[i
].end
, wptr
);
6030 wptr
+= sizeof (tcp_seq
);
6032 tcph
->th_offset_and_rsrvd
[0] += ((num_sack_blk
* 2 + 1) << 4);
6034 assert((uintptr_t)(mp1
->b_wptr
- rptr
) <= (uintptr_t)INT_MAX
);
6035 data_length
+= (int)(mp1
->b_wptr
- rptr
);
6036 if (tcp
->tcp_ipversion
== IPV4_VERSION
)
6037 ((struct ip
*)rptr
)->ip_len
= htons(data_length
);
6040 * Performance hit! We need to pullup the whole message
6041 * in order to do checksum and for the MAC output routine.
6043 if (mp1
->b_cont
!= NULL
) {
6046 printf("Multiple mblk %d\n", msgdsize(mp1
));
6049 new_mp
= allocb(msgdsize(mp1
) + tcp_wroff_xtra
, 0);
6050 new_mp
->b_rptr
+= tcp_wroff_xtra
;
6051 new_mp
->b_wptr
= new_mp
->b_rptr
;
6052 while (mp1
!= NULL
) {
6053 mp_size
= mp1
->b_wptr
- mp1
->b_rptr
;
6054 bcopy(mp1
->b_rptr
, new_mp
->b_wptr
, mp_size
);
6055 new_mp
->b_wptr
+= mp_size
;
6062 /* Fill in the TTL field as it is 0 in the header template. */
6063 ((struct ip
*)mp1
->b_rptr
)->ip_ttl
= (uint8_t)tcp_ipv4_ttl
;
6069 * Generate a "no listener here" reset in response to the
6070 * connection request contained within 'mp'
6073 tcp_xmit_listeners_reset(int sock_id
, mblk_t
*mp
, uint_t ip_hdr_len
)
6084 tcph
= (tcph_t
*)&rptr
[ip_hdr_len
];
6085 seg_seq
= BE32_TO_U32(tcph
->th_seq
);
6086 seg_ack
= BE32_TO_U32(tcph
->th_ack
);
6087 flags
= tcph
->th_flags
[0];
6089 seg_len
= msgdsize(mp
) - (TCP_HDR_LENGTH(tcph
) + ip_hdr_len
);
6090 if (flags
& TH_RST
) {
6092 } else if (flags
& TH_ACK
) {
6093 tcp_xmit_early_reset("no tcp, reset",
6094 sock_id
, mp
, seg_ack
, 0, TH_RST
, ip_hdr_len
);
6098 tcp_xmit_early_reset("no tcp, reset/ack", sock_id
,
6099 mp
, 0, seg_seq
+ seg_len
,
6100 TH_RST
| TH_ACK
, ip_hdr_len
);
6104 /* Non overlapping byte exchanger */
6106 tcp_xchg(uchar_t
*a
, uchar_t
*b
, int len
)
6118 * Generate a reset based on an inbound packet for which there is no active
6119 * tcp state that we can find.
6122 tcp_xmit_early_reset(char *str
, int sock_id
, mblk_t
*mp
, uint32_t seq
,
6123 uint32_t ack
, int ctl
, uint_t ip_hdr_len
)
6125 struct ip
*iph
= NULL
;
6133 dprintf("tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
6134 "flags 0x%x\n", str
, seq
, ack
, ctl
);
6138 * We skip reversing source route here.
6139 * (for now we replace all IP options with EOL)
6141 iph
= (struct ip
*)mp
->b_rptr
;
6142 for (i
= IP_SIMPLE_HDR_LENGTH
; i
< (int)ip_hdr_len
; i
++)
6143 mp
->b_rptr
[i
] = IPOPT_EOL
;
6145 * Make sure that src address is not a limited broadcast
6146 * address. Not all broadcast address checking for the
6147 * src address is possible, since we don't know the
6148 * netmask of the src addr.
6149 * No check for destination address is done, since
6150 * IP will not pass up a packet with a broadcast dest address
6153 if (iph
->ip_src
.s_addr
== INADDR_ANY
||
6154 iph
->ip_src
.s_addr
== INADDR_BROADCAST
) {
6159 tcph
= (tcph_t
*)&mp
->b_rptr
[ip_hdr_len
];
6160 if (tcph
->th_flags
[0] & TH_RST
) {
6165 * Now copy the original header to a new buffer. The reason
6166 * for doing this is that we need to put extra room before
6167 * the header for the MAC layer address. The original mblk
6168 * does not have this extra head room.
6170 len
= ip_hdr_len
+ sizeof (tcph_t
);
6171 if ((new_mp
= allocb(len
+ tcp_wroff_xtra
, 0)) == NULL
) {
6175 new_mp
->b_rptr
+= tcp_wroff_xtra
;
6176 bcopy(mp
->b_rptr
, new_mp
->b_rptr
, len
);
6177 new_mp
->b_wptr
= new_mp
->b_rptr
+ len
;
6180 iph
= (struct ip
*)mp
->b_rptr
;
6181 tcph
= (tcph_t
*)&mp
->b_rptr
[ip_hdr_len
];
6183 tcph
->th_offset_and_rsrvd
[0] = (5 << 4);
6184 tcp_xchg(tcph
->th_fport
, tcph
->th_lport
, 2);
6185 U32_TO_BE32(ack
, tcph
->th_ack
);
6186 U32_TO_BE32(seq
, tcph
->th_seq
);
6187 U16_TO_BE16(0, tcph
->th_win
);
6188 bzero(tcph
->th_sum
, sizeof (int16_t));
6189 tcph
->th_flags
[0] = (uint8_t)ctl
;
6191 BUMP_MIB(tcp_mib
.tcpOutRsts
);
6192 BUMP_MIB(tcp_mib
.tcpOutControl
);
6195 iph
->ip_len
= htons(len
);
6196 /* Swap addresses */
6197 addr
= iph
->ip_src
.s_addr
;
6198 iph
->ip_src
= iph
->ip_dst
;
6199 iph
->ip_dst
.s_addr
= addr
;
6203 iph
->ip_ttl
= (uint8_t)tcp_ipv4_ttl
;
6205 /* Dump the packet when debugging. */
6206 TCP_DUMP_PACKET("tcp_xmit_early_reset", mp
);
6207 (void) ipv4_tcp_output(sock_id
, mp
);
6212 tcp_set_cksum(mblk_t
*mp
)
6218 iph
= (struct ip
*)mp
->b_rptr
;
6219 tcph
= (tcpha_t
*)(iph
+ 1);
6220 len
= ntohs(iph
->ip_len
);
6222 * Calculate the TCP checksum. Need to include the psuedo header,
6223 * which is similar to the real IP header starting at the TTL field.
6225 iph
->ip_sum
= htons(len
- IP_SIMPLE_HDR_LENGTH
);
6227 tcph
->tha_sum
= tcp_cksum((uint16_t *)&(iph
->ip_ttl
),
6228 len
- IP_SIMPLE_HDR_LENGTH
+ 12);
6233 tcp_cksum(uint16_t *buf
, uint32_t len
)
6236 * Compute Internet Checksum for "count" bytes
6237 * beginning at location "addr".
6242 /* This is the inner loop */
6247 /* Add left-over byte, if any */
6249 sum
+= *(unsigned char *)buf
* 256;
6251 /* Fold 32-bit sum to 16 bits */
6253 sum
= (sum
& 0xffff) + (sum
>> 16);
6255 return ((uint16_t)~sum
);
6259 * Type three generator adapted from the random() function in 4.4 BSD:
6263 * Copyright (c) 1983, 1993
6264 * The Regents of the University of California. All rights reserved.
6266 * Redistribution and use in source and binary forms, with or without
6267 * modification, are permitted provided that the following conditions
6269 * 1. Redistributions of source code must retain the above copyright
6270 * notice, this list of conditions and the following disclaimer.
6271 * 2. Redistributions in binary form must reproduce the above copyright
6272 * notice, this list of conditions and the following disclaimer in the
6273 * documentation and/or other materials provided with the distribution.
6274 * 3. All advertising materials mentioning features or use of this software
6275 * must display the following acknowledgement:
6276 * This product includes software developed by the University of
6277 * California, Berkeley and its contributors.
6278 * 4. Neither the name of the University nor the names of its contributors
6279 * may be used to endorse or promote products derived from this software
6280 * without specific prior written permission.
6282 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6283 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6284 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6285 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6286 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6287 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6288 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6289 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6290 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6291 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6295 /* Type 3 -- x**31 + x**3 + 1 */
6300 /* Protected by tcp_random_lock */
6301 static int tcp_randtbl
[DEG_3
+ 1];
6303 static int *tcp_random_fptr
= &tcp_randtbl
[SEP_3
+ 1];
6304 static int *tcp_random_rptr
= &tcp_randtbl
[1];
6306 static int *tcp_random_state
= &tcp_randtbl
[1];
6307 static int *tcp_random_end_ptr
= &tcp_randtbl
[DEG_3
+ 1];
6310 tcp_random_init(void)
6319 * XXX We don't have high resolution time in standalone... The
6320 * following is just some approximation on the comment below.
6322 * Use high-res timer and current time for seed. Gethrtime() returns
6323 * a longlong, which may contain resolution down to nanoseconds.
6324 * The current time will either be a 32-bit or a 64-bit quantity.
6325 * XOR the two together in a 64-bit result variable.
6326 * Convert the result to a 32-bit value by multiplying the high-order
6327 * 32-bits by the low-order 32-bits.
6329 * XXX We don't have gethrtime() in prom and the wallclock....
6332 hrt
= prom_gettime();
6333 wallclock
= (uint32_t)time(NULL
);
6334 result
= wallclock
^ hrt
;
6335 tcp_random_state
[0] = result
;
6337 for (i
= 1; i
< DEG_3
; i
++)
6338 tcp_random_state
[i
] = 1103515245 * tcp_random_state
[i
- 1]
6340 tcp_random_fptr
= &tcp_random_state
[SEP_3
];
6341 tcp_random_rptr
= &tcp_random_state
[0];
6342 for (i
= 0; i
< 10 * DEG_3
; i
++)
6343 (void) tcp_random();
6347 * tcp_random: Return a random number in the range [1 - (128K + 1)].
6348 * This range is selected to be approximately centered on TCP_ISS / 2,
6349 * and easy to compute. We get this value by generating a 32-bit random
6350 * number, selecting out the high-order 17 bits, and then adding one so
6351 * that we never return zero.
6358 *tcp_random_fptr
+= *tcp_random_rptr
;
6361 * The high-order bits are more random than the low-order bits,
6362 * so we select out the high-order 17 bits and add one so that
6363 * we never return zero.
6365 i
= ((*tcp_random_fptr
>> 15) & 0x1ffff) + 1;
6366 if (++tcp_random_fptr
>= tcp_random_end_ptr
) {
6367 tcp_random_fptr
= tcp_random_state
;
6369 } else if (++tcp_random_rptr
>= tcp_random_end_ptr
)
6370 tcp_random_rptr
= tcp_random_state
;
6376 * Generate ISS, taking into account NDD changes may happen halfway through.
6377 * (If the iss is not zero, set it.)
6380 tcp_iss_init(tcp_t
*tcp
)
6382 tcp_iss_incr_extra
+= (ISS_INCR
>> 1);
6383 tcp
->tcp_iss
= tcp_iss_incr_extra
;
6384 tcp
->tcp_iss
+= (prom_gettime() >> ISS_NSEC_SHT
) + tcp_random();
6385 tcp
->tcp_valid_bits
= TCP_ISS_VALID
;
6386 tcp
->tcp_fss
= tcp
->tcp_iss
- 1;
6387 tcp
->tcp_suna
= tcp
->tcp_iss
;
6388 tcp
->tcp_snxt
= tcp
->tcp_iss
+ 1;
6389 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
6390 tcp
->tcp_csuna
= tcp
->tcp_snxt
;
6394 * Diagnostic routine used to return a string associated with the tcp state.
6395 * Note that if the caller does not supply a buffer, it will use an internal
6396 * static string. This means that if multiple threads call this function at
6397 * the same time, output can be corrupted... Note also that this function
6398 * does not check the size of the supplied buffer. The caller has to make
6399 * sure that it is big enough.
6402 tcp_display(tcp_t
*tcp
, char *sup_buf
, char format
)
6405 static char priv_buf
[INET_ADDRSTRLEN
* 2 + 80];
6408 char local_addrbuf
[INET_ADDRSTRLEN
];
6409 char remote_addrbuf
[INET_ADDRSTRLEN
];
6410 struct in_addr addr
;
6412 if (sup_buf
!= NULL
)
6418 return ("NULL_TCP");
6419 switch (tcp
->tcp_state
) {
6433 cp
= "TCP_SYN_SENT";
6436 cp
= "TCP_SYN_RCVD";
6438 case TCPS_ESTABLISHED
:
6439 cp
= "TCP_ESTABLISHED";
6441 case TCPS_CLOSE_WAIT
:
6442 cp
= "TCP_CLOSE_WAIT";
6444 case TCPS_FIN_WAIT_1
:
6445 cp
= "TCP_FIN_WAIT_1";
6451 cp
= "TCP_LAST_ACK";
6453 case TCPS_FIN_WAIT_2
:
6454 cp
= "TCP_FIN_WAIT_2";
6456 case TCPS_TIME_WAIT
:
6457 cp
= "TCP_TIME_WAIT";
6460 (void) sprintf(buf1
, "TCPUnkState(%d)", tcp
->tcp_state
);
6465 case DISP_ADDR_AND_PORT
:
6467 * Note that we use the remote address in the tcp_b
6468 * structure. This means that it will print out
6469 * the real destination address, not the next hop's
6470 * address if source routing is used.
6472 addr
.s_addr
= tcp
->tcp_bound_source
;
6473 bcopy(inet_ntoa(addr
), local_addrbuf
, sizeof (local_addrbuf
));
6474 addr
.s_addr
= tcp
->tcp_remote
;
6475 bcopy(inet_ntoa(addr
), remote_addrbuf
, sizeof (remote_addrbuf
));
6476 (void) snprintf(buf
, sizeof (priv_buf
), "[%s.%u, %s.%u] %s",
6477 local_addrbuf
, ntohs(tcp
->tcp_lport
), remote_addrbuf
,
6478 ntohs(tcp
->tcp_fport
), cp
);
6480 case DISP_PORT_ONLY
:
6482 (void) snprintf(buf
, sizeof (priv_buf
), "[%u, %u] %s",
6483 ntohs(tcp
->tcp_lport
), ntohs(tcp
->tcp_fport
), cp
);
6491 * Add a new piece to the tcp reassembly queue. If the gap at the beginning
6492 * is filled, return as much as we can. The message passed in may be
6493 * multi-part, chained using b_cont. "start" is the starting sequence
6494 * number for this piece.
6497 tcp_reass(tcp_t
*tcp
, mblk_t
*mp
, uint32_t start
)
6505 /* Walk through all the new pieces. */
6507 assert((uintptr_t)(mp
->b_wptr
- mp
->b_rptr
) <=
6508 (uintptr_t)INT_MAX
);
6509 end
= start
+ (int)(mp
->b_wptr
- mp
->b_rptr
);
6510 next_mp
= mp
->b_cont
;
6512 /* Empty. Blast it. */
6517 TCP_REASS_SET_SEQ(mp
, start
);
6518 TCP_REASS_SET_END(mp
, end
);
6519 mp1
= tcp
->tcp_reass_tail
;
6521 tcp
->tcp_reass_tail
= mp
;
6522 tcp
->tcp_reass_head
= mp
;
6523 BUMP_MIB(tcp_mib
.tcpInDataUnorderSegs
);
6524 UPDATE_MIB(tcp_mib
.tcpInDataUnorderBytes
, end
- start
);
6527 /* New stuff completely beyond tail? */
6528 if (SEQ_GEQ(start
, TCP_REASS_END(mp1
))) {
6529 /* Link it on end. */
6531 tcp
->tcp_reass_tail
= mp
;
6532 BUMP_MIB(tcp_mib
.tcpInDataUnorderSegs
);
6533 UPDATE_MIB(tcp_mib
.tcpInDataUnorderBytes
, end
- start
);
6536 mp1
= tcp
->tcp_reass_head
;
6537 u1
= TCP_REASS_SEQ(mp1
);
6538 /* New stuff at the front? */
6539 if (SEQ_LT(start
, u1
)) {
6540 /* Yes... Check for overlap. */
6542 tcp
->tcp_reass_head
= mp
;
6543 tcp_reass_elim_overlap(tcp
, mp
);
6547 * The new piece fits somewhere between the head and tail.
6548 * We find our slot, where mp1 precedes us and mp2 trails.
6550 for (; (mp2
= mp1
->b_cont
) != NULL
; mp1
= mp2
) {
6551 u1
= TCP_REASS_SEQ(mp2
);
6552 if (SEQ_LEQ(start
, u1
))
6555 /* Link ourselves in */
6559 /* Trim overlap with following mblk(s) first */
6560 tcp_reass_elim_overlap(tcp
, mp
);
6562 /* Trim overlap with preceding mblk */
6563 tcp_reass_elim_overlap(tcp
, mp1
);
6565 } while (start
= end
, mp
= next_mp
);
6566 mp1
= tcp
->tcp_reass_head
;
6567 /* Anything ready to go? */
6568 if (TCP_REASS_SEQ(mp1
) != tcp
->tcp_rnxt
)
6570 /* Eat what we can off the queue */
6573 end
= TCP_REASS_END(mp1
);
6574 TCP_REASS_SET_SEQ(mp1
, 0);
6575 TCP_REASS_SET_END(mp1
, 0);
6577 tcp
->tcp_reass_tail
= NULL
;
6580 if (end
!= TCP_REASS_SEQ(mp
)) {
6586 mp1
= tcp
->tcp_reass_head
;
6587 tcp
->tcp_reass_head
= mp
;
6591 /* Eliminate any overlap that mp may have over later mblks */
6593 tcp_reass_elim_overlap(tcp_t
*tcp
, mblk_t
*mp
)
6599 end
= TCP_REASS_END(mp
);
6600 while ((mp1
= mp
->b_cont
) != NULL
) {
6601 u1
= TCP_REASS_SEQ(mp1
);
6602 if (!SEQ_GT(end
, u1
))
6604 if (!SEQ_GEQ(end
, TCP_REASS_END(mp1
))) {
6605 mp
->b_wptr
-= end
- u1
;
6606 TCP_REASS_SET_END(mp
, u1
);
6607 BUMP_MIB(tcp_mib
.tcpInDataPartDupSegs
);
6608 UPDATE_MIB(tcp_mib
.tcpInDataPartDupBytes
, end
- u1
);
6611 mp
->b_cont
= mp1
->b_cont
;
6613 BUMP_MIB(tcp_mib
.tcpInDataDupSegs
);
6614 UPDATE_MIB(tcp_mib
.tcpInDataDupBytes
, end
- u1
);
6617 tcp
->tcp_reass_tail
= mp
;
6621 * Remove a connection from the list of detached TIME_WAIT connections.
6624 tcp_time_wait_remove(tcp_t
*tcp
)
6626 if (tcp
->tcp_time_wait_expire
== 0) {
6627 assert(tcp
->tcp_time_wait_next
== NULL
);
6628 assert(tcp
->tcp_time_wait_prev
== NULL
);
6631 assert(tcp
->tcp_state
== TCPS_TIME_WAIT
);
6632 if (tcp
== tcp_time_wait_head
) {
6633 assert(tcp
->tcp_time_wait_prev
== NULL
);
6634 tcp_time_wait_head
= tcp
->tcp_time_wait_next
;
6635 if (tcp_time_wait_head
!= NULL
) {
6636 tcp_time_wait_head
->tcp_time_wait_prev
= NULL
;
6638 tcp_time_wait_tail
= NULL
;
6640 } else if (tcp
== tcp_time_wait_tail
) {
6641 assert(tcp
!= tcp_time_wait_head
);
6642 assert(tcp
->tcp_time_wait_next
== NULL
);
6643 tcp_time_wait_tail
= tcp
->tcp_time_wait_prev
;
6644 assert(tcp_time_wait_tail
!= NULL
);
6645 tcp_time_wait_tail
->tcp_time_wait_next
= NULL
;
6647 assert(tcp
->tcp_time_wait_prev
->tcp_time_wait_next
== tcp
);
6648 assert(tcp
->tcp_time_wait_next
->tcp_time_wait_prev
== tcp
);
6649 tcp
->tcp_time_wait_prev
->tcp_time_wait_next
=
6650 tcp
->tcp_time_wait_next
;
6651 tcp
->tcp_time_wait_next
->tcp_time_wait_prev
=
6652 tcp
->tcp_time_wait_prev
;
6654 tcp
->tcp_time_wait_next
= NULL
;
6655 tcp
->tcp_time_wait_prev
= NULL
;
6656 tcp
->tcp_time_wait_expire
= 0;
6660 * Add a connection to the list of detached TIME_WAIT connections
6661 * and set its time to expire ...
6664 tcp_time_wait_append(tcp_t
*tcp
)
6666 tcp
->tcp_time_wait_expire
= prom_gettime() + tcp_time_wait_interval
;
6667 if (tcp
->tcp_time_wait_expire
== 0)
6668 tcp
->tcp_time_wait_expire
= 1;
6670 if (tcp_time_wait_head
== NULL
) {
6671 assert(tcp_time_wait_tail
== NULL
);
6672 tcp_time_wait_head
= tcp
;
6674 assert(tcp_time_wait_tail
!= NULL
);
6675 assert(tcp_time_wait_tail
->tcp_state
== TCPS_TIME_WAIT
);
6676 tcp_time_wait_tail
->tcp_time_wait_next
= tcp
;
6677 tcp
->tcp_time_wait_prev
= tcp_time_wait_tail
;
6679 tcp_time_wait_tail
= tcp
;
6681 /* for ndd stats about compression */
6686 * Periodic qtimeout routine run on the default queue.
6687 * Performs 2 functions.
6688 * 1. Does TIME_WAIT compression on all recently added tcps. List
6689 * traversal is done backwards from the tail.
6690 * 2. Blows away all tcps whose TIME_WAIT has expired. List traversal
6691 * is done forwards from the head.
6694 tcp_time_wait_collector(void)
6700 * In order to reap time waits reliably, we should use a
6701 * source of time that is not adjustable by the user
6703 now
= prom_gettime();
6704 while ((tcp
= tcp_time_wait_head
) != NULL
) {
6706 * Compare times using modular arithmetic, since
6707 * lbolt can wrapover.
6709 if ((int32_t)(now
- tcp
->tcp_time_wait_expire
) < 0) {
6713 * Note that the err must be 0 as there is no socket
6714 * associated with this TCP...
6716 (void) tcp_clean_death(-1, tcp
, 0);
6718 /* Schedule next run time. */
6719 tcp_time_wait_runtime
= prom_gettime() + 10000;
6723 tcp_time_wait_report(void)
6727 printf("Current time %u\n", prom_gettime());
6728 for (tcp
= tcp_time_wait_head
; tcp
!= NULL
;
6729 tcp
= tcp
->tcp_time_wait_next
) {
6730 printf("%s expires at %u\n", tcp_display(tcp
, NULL
,
6731 DISP_ADDR_AND_PORT
), tcp
->tcp_time_wait_expire
);
6736 * Send up all messages queued on tcp_rcv_list.
6737 * Have to set tcp_co_norm since we use putnext.
6740 tcp_rcv_drain(int sock_id
, tcp_t
*tcp
)
6743 struct inetgram
*in_gram
;
6747 /* Don't drain if the app has not finished reading all the data. */
6748 if (sockets
[sock_id
].so_rcvbuf
<= 0)
6751 /* We might have come here just to updated the rwnd */
6752 if (tcp
->tcp_rcv_list
== NULL
)
6755 if ((in_gram
= (struct inetgram
*)bkmem_zalloc(
6756 sizeof (struct inetgram
))) == NULL
) {
6759 if ((in_mp
= allocb(tcp
->tcp_rcv_cnt
, 0)) == NULL
) {
6760 bkmem_free((caddr_t
)in_gram
, sizeof (struct inetgram
));
6763 in_gram
->igm_level
= APP_LVL
;
6764 in_gram
->igm_mp
= in_mp
;
6765 in_gram
->igm_id
= 0;
6767 while ((mp
= tcp
->tcp_rcv_list
) != NULL
) {
6768 tcp
->tcp_rcv_list
= mp
->b_cont
;
6769 len
= mp
->b_wptr
- mp
->b_rptr
;
6770 bcopy(mp
->b_rptr
, in_mp
->b_wptr
, len
);
6771 in_mp
->b_wptr
+= len
;
6775 tcp
->tcp_rcv_last_tail
= NULL
;
6776 tcp
->tcp_rcv_cnt
= 0;
6777 add_grams(&sockets
[sock_id
].inq
, in_gram
);
6779 /* This means that so_rcvbuf can be less than 0. */
6780 sockets
[sock_id
].so_rcvbuf
-= in_mp
->b_wptr
- in_mp
->b_rptr
;
6783 * Increase the receive window to max. But we need to do receiver
6784 * SWS avoidance. This means that we need to check the increase of
6785 * of receive window is at least 1 MSS.
6787 if (sockets
[sock_id
].so_rcvbuf
> 0 &&
6788 (tcp
->tcp_rwnd_max
- tcp
->tcp_rwnd
>= tcp
->tcp_mss
)) {
6789 tcp
->tcp_rwnd
= tcp
->tcp_rwnd_max
;
6790 U32_TO_ABE16(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
,
6791 tcp
->tcp_tcph
->th_win
);
6796 * Wrapper for recvfrom to call
6799 tcp_rcv_drain_sock(int sock_id
)
6802 if ((tcp
= sockets
[sock_id
].pcb
) == NULL
)
6804 tcp_rcv_drain(sock_id
, tcp
);
6808 * If the inq == NULL and the tcp_rcv_list != NULL, we have data that
6809 * recvfrom could read. Place a magic message in the inq to let recvfrom
6810 * know that it needs to call tcp_rcv_drain_sock to pullup the data.
6813 tcp_drain_needed(int sock_id
, tcp_t
*tcp
)
6815 struct inetgram
*in_gram
;
6817 printf("tcp_drain_needed: inq %x, tcp_rcv_list %x\n",
6818 sockets
[sock_id
].inq
, tcp
->tcp_rcv_list
);
6820 if ((sockets
[sock_id
].inq
!= NULL
) ||
6821 (tcp
->tcp_rcv_list
== NULL
))
6824 if ((in_gram
= (struct inetgram
*)bkmem_zalloc(
6825 sizeof (struct inetgram
))) == NULL
)
6828 in_gram
->igm_level
= APP_LVL
;
6829 in_gram
->igm_mp
= NULL
;
6830 in_gram
->igm_id
= TCP_CALLB_MAGIC_ID
;
6832 add_grams(&sockets
[sock_id
].inq
, in_gram
);
6836 * Queue data on tcp_rcv_list which is a b_next chain.
6837 * Each element of the chain is a b_cont chain.
6839 * M_DATA messages are added to the current element.
6840 * Other messages are added as new (b_next) elements.
6843 tcp_rcv_enqueue(tcp_t
*tcp
, mblk_t
*mp
, uint_t seg_len
)
6845 assert(seg_len
== msgdsize(mp
));
6846 if (tcp
->tcp_rcv_list
== NULL
) {
6847 tcp
->tcp_rcv_list
= mp
;
6849 tcp
->tcp_rcv_last_tail
->b_cont
= mp
;
6853 tcp
->tcp_rcv_last_tail
= mp
;
6854 tcp
->tcp_rcv_cnt
+= seg_len
;
6855 tcp
->tcp_rwnd
-= seg_len
;
6857 printf("tcp_rcv_enqueue rwnd %d\n", tcp
->tcp_rwnd
);
6859 U32_TO_ABE16(tcp
->tcp_rwnd
>> tcp
->tcp_rcv_ws
, tcp
->tcp_tcph
->th_win
);
6862 /* The minimum of smoothed mean deviation in RTO calculation. */
6863 #define TCP_SD_MIN 400
6866 * Set RTO for this connection. The formula is from Jacobson and Karels'
6867 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names
6868 * are the same as those in Appendix A.2 of that paper.
6870 * m = new measurement
6871 * sa = smoothed RTT average (8 * average estimates).
6872 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
6875 tcp_set_rto(tcp_t
*tcp
, int32_t rtt
)
6878 uint32_t sa
= tcp
->tcp_rtt_sa
;
6879 uint32_t sv
= tcp
->tcp_rtt_sd
;
6882 BUMP_MIB(tcp_mib
.tcpRttUpdate
);
6883 tcp
->tcp_rtt_update
++;
6885 /* tcp_rtt_sa is not 0 means this is a new sample. */
6888 * Update average estimator:
6889 * new rtt = 7/8 old rtt + 1/8 Error
6892 /* m is now Error in estimate. */
6894 if ((int32_t)(sa
+= m
) <= 0) {
6896 * Don't allow the smoothed average to be negative.
6897 * We use 0 to denote reinitialization of the
6904 * Update deviation estimator:
6905 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
6913 * This follows BSD's implementation. So the reinitialized
6914 * RTO is 3 * m. We cannot go less than 2 because if the
6915 * link is bandwidth dominated, doubling the window size
6916 * during slow start means doubling the RTT. We want to be
6917 * more conservative when we reinitialize our estimates. 3
6918 * is just a convenient number.
6923 if (sv
< TCP_SD_MIN
) {
6925 * We do not know that if sa captures the delay ACK
6926 * effect as in a long train of segments, a receiver
6927 * does not delay its ACKs. So set the minimum of sv
6928 * to be TCP_SD_MIN, which is default to 400 ms, twice
6929 * of BSD DATO. That means the minimum of mean
6930 * deviation is 100 ms.
6935 tcp
->tcp_rtt_sa
= sa
;
6936 tcp
->tcp_rtt_sd
= sv
;
6938 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
6940 * Add tcp_rexmit_interval extra in case of extreme environment
6941 * where the algorithm fails to work. The default value of
6942 * tcp_rexmit_interval_extra should be 0.
6944 * As we use a finer grained clock than BSD and update
6945 * RTO for every ACKs, add in another .25 of RTT to the
6946 * deviation of RTO to accomodate burstiness of 1/4 of
6949 rto
= (sa
>> 3) + sv
+ tcp_rexmit_interval_extra
+ (sa
>> 5);
6951 if (rto
> tcp_rexmit_interval_max
) {
6952 tcp
->tcp_rto
= tcp_rexmit_interval_max
;
6953 } else if (rto
< tcp_rexmit_interval_min
) {
6954 tcp
->tcp_rto
= tcp_rexmit_interval_min
;
6959 /* Now, we can reset tcp_timer_backoff to use the new RTO... */
6960 tcp
->tcp_timer_backoff
= 0;
6964 * Initiate closedown sequence on an active connection.
6965 * Return value zero for OK return, non-zero for error return.
6968 tcp_xmit_end(tcp_t
*tcp
, int sock_id
)
6972 if (tcp
->tcp_state
< TCPS_SYN_RCVD
||
6973 tcp
->tcp_state
> TCPS_CLOSE_WAIT
) {
6975 * Invalid state, only states TCPS_SYN_RCVD,
6976 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
6981 tcp
->tcp_fss
= tcp
->tcp_snxt
+ tcp
->tcp_unsent
;
6982 tcp
->tcp_valid_bits
|= TCP_FSS_VALID
;
6984 * If there is nothing more unsent, send the FIN now.
6985 * Otherwise, it will go out with the last segment.
6987 if (tcp
->tcp_unsent
== 0) {
6988 mp
= tcp_xmit_mp(tcp
, NULL
, 0, NULL
, NULL
,
6989 tcp
->tcp_fss
, B_FALSE
, NULL
, B_FALSE
);
6992 /* Dump the packet when debugging. */
6993 TCP_DUMP_PACKET("tcp_xmit_end", mp
);
6994 (void) ipv4_tcp_output(sock_id
, mp
);
6998 * Couldn't allocate msg. Pretend we got it out.
6999 * Wait for rexmit timeout.
7001 tcp
->tcp_snxt
= tcp
->tcp_fss
+ 1;
7002 TCP_TIMER_RESTART(tcp
, tcp
->tcp_rto
);
7006 * If needed, update tcp_rexmit_snxt as tcp_snxt is
7009 if (tcp
->tcp_rexmit
&& tcp
->tcp_rexmit_nxt
== tcp
->tcp_fss
) {
7010 tcp
->tcp_rexmit_nxt
= tcp
->tcp_snxt
;
7013 tcp_wput_data(tcp
, NULL
, B_FALSE
);
7020 tcp_opt_set(tcp_t
*tcp
, int level
, int option
, const void *optval
,
7027 if (optlen
== sizeof (int)) {
7028 int val
= *(int *)optval
;
7030 if (val
> tcp_max_buf
) {
7034 /* Silently ignore zero */
7036 val
= MSS_ROUNDUP(val
, tcp
->tcp_mss
);
7037 (void) tcp_rwnd_set(tcp
, val
);
7044 if (optlen
== sizeof (int)) {
7045 tcp
->tcp_xmit_hiwater
= *(int *)optval
;
7046 if (tcp
->tcp_xmit_hiwater
> tcp_max_buf
)
7047 tcp
->tcp_xmit_hiwater
= tcp_max_buf
;
7053 if (optlen
== sizeof (struct linger
)) {
7054 struct linger
*lgr
= (struct linger
*)optval
;
7057 tcp
->tcp_linger
= 1;
7058 tcp
->tcp_lingertime
= lgr
->l_linger
;
7060 tcp
->tcp_linger
= 0;
7061 tcp
->tcp_lingertime
= 0;
7068 errno
= ENOPROTOOPT
;
7072 } /* case SOL_SOCKET */
7076 errno
= ENOPROTOOPT
;
7080 } /* case IPPROTO_TCP */
7084 errno
= ENOPROTOOPT
;
7088 } /* case IPPROTO_IP */
7090 errno
= ENOPROTOOPT
;
7092 } /* switch (level) */