17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
59f1fc992Sss146032 * Common Development and Distribution License (the "License").
69f1fc992Sss146032 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
219f1fc992Sss146032
227c478bd9Sstevel@tonic-gate /*
239f1fc992Sss146032 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
247c478bd9Sstevel@tonic-gate * Use is subject to license terms.
2548bbca81SDaniel Hoffman * Copyright (c) 2016 by Delphix. All rights reserved.
267c478bd9Sstevel@tonic-gate *
277c478bd9Sstevel@tonic-gate * tcp.c, Code implementing the TCP protocol.
287c478bd9Sstevel@tonic-gate */
297c478bd9Sstevel@tonic-gate
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <socket_impl.h>
327c478bd9Sstevel@tonic-gate #include <socket_inet.h>
337c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
347c478bd9Sstevel@tonic-gate #include <sys/promif.h>
357c478bd9Sstevel@tonic-gate #include <sys/socket.h>
367c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h>
377c478bd9Sstevel@tonic-gate #include <netinet/in.h>
387c478bd9Sstevel@tonic-gate #include <netinet/ip.h>
397c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
407c478bd9Sstevel@tonic-gate #include <net/if_types.h>
417c478bd9Sstevel@tonic-gate #include <sys/salib.h>
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #include "ipv4.h"
447c478bd9Sstevel@tonic-gate #include "ipv4_impl.h"
457c478bd9Sstevel@tonic-gate #include "mac.h"
467c478bd9Sstevel@tonic-gate #include "mac_impl.h"
477c478bd9Sstevel@tonic-gate #include "v4_sum_impl.h"
487c478bd9Sstevel@tonic-gate #include <sys/bootdebug.h>
497c478bd9Sstevel@tonic-gate #include "tcp_inet.h"
507c478bd9Sstevel@tonic-gate #include "tcp_sack.h"
517c478bd9Sstevel@tonic-gate #include <inet/common.h>
527c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate * We need to redefine BUMP_MIB/UPDATE_MIB to not have DTrace probes.
567c478bd9Sstevel@tonic-gate */
577c478bd9Sstevel@tonic-gate #undef BUMP_MIB
587c478bd9Sstevel@tonic-gate #define BUMP_MIB(x) (x)++
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #undef UPDATE_MIB
617c478bd9Sstevel@tonic-gate #define UPDATE_MIB(x, y) x += y
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * MIB-2 stuff for SNMP
657c478bd9Sstevel@tonic-gate */
667c478bd9Sstevel@tonic-gate mib2_tcp_t tcp_mib; /* SNMP fixed size info */
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate /* The TCP mib does not include the following errors. */
697c478bd9Sstevel@tonic-gate static uint_t tcp_cksum_errors;
707c478bd9Sstevel@tonic-gate static uint_t tcp_drops;
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /* Macros for timestamp comparisons */
737c478bd9Sstevel@tonic-gate #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0)
747c478bd9Sstevel@tonic-gate #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0)
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate /*
777c478bd9Sstevel@tonic-gate * Parameters for TCP Initial Send Sequence number (ISS) generation.
787c478bd9Sstevel@tonic-gate * The ISS is calculated by adding three components: a time component
797c478bd9Sstevel@tonic-gate * which grows by 1 every 4096 nanoseconds (versus every 4 microseconds
807c478bd9Sstevel@tonic-gate * suggested by RFC 793, page 27);
817c478bd9Sstevel@tonic-gate * a per-connection component which grows by 125000 for every new connection;
827c478bd9Sstevel@tonic-gate * and an "extra" component that grows by a random amount centered
837c478bd9Sstevel@tonic-gate * approximately on 64000. This causes the the ISS generator to cycle every
847c478bd9Sstevel@tonic-gate * 4.89 hours if no TCP connections are made, and faster if connections are
857c478bd9Sstevel@tonic-gate * made.
867c478bd9Sstevel@tonic-gate */
877c478bd9Sstevel@tonic-gate #define ISS_INCR 250000
887c478bd9Sstevel@tonic-gate #define ISS_NSEC_SHT 0
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate #define TCP_XMIT_LOWATER 4096
937c478bd9Sstevel@tonic-gate #define TCP_XMIT_HIWATER 49152
947c478bd9Sstevel@tonic-gate #define TCP_RECV_LOWATER 2048
957c478bd9Sstevel@tonic-gate #define TCP_RECV_HIWATER 49152
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * PAWS needs a timer for 24 days. This is the number of ms in 24 days
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate #define PAWS_TIMEOUT ((uint32_t)(24*24*60*60*1000))
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * TCP options struct returned from tcp_parse_options.
1047c478bd9Sstevel@tonic-gate */
1057c478bd9Sstevel@tonic-gate typedef struct tcp_opt_s {
1067c478bd9Sstevel@tonic-gate uint32_t tcp_opt_mss;
1077c478bd9Sstevel@tonic-gate uint32_t tcp_opt_wscale;
1087c478bd9Sstevel@tonic-gate uint32_t tcp_opt_ts_val;
1097c478bd9Sstevel@tonic-gate uint32_t tcp_opt_ts_ecr;
1107c478bd9Sstevel@tonic-gate tcp_t *tcp;
1117c478bd9Sstevel@tonic-gate } tcp_opt_t;
1127c478bd9Sstevel@tonic-gate
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate * RFC1323-recommended phrasing of TSTAMP option, for easier parsing
1157c478bd9Sstevel@tonic-gate */
1167c478bd9Sstevel@tonic-gate
1177c478bd9Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1187c478bd9Sstevel@tonic-gate #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
1197c478bd9Sstevel@tonic-gate (TCPOPT_TSTAMP << 8) | 10)
1207c478bd9Sstevel@tonic-gate #else
1217c478bd9Sstevel@tonic-gate #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
1227c478bd9Sstevel@tonic-gate (TCPOPT_NOP << 8) | TCPOPT_NOP)
1237c478bd9Sstevel@tonic-gate #endif
1247c478bd9Sstevel@tonic-gate
1257c478bd9Sstevel@tonic-gate /*
1267c478bd9Sstevel@tonic-gate * Flags returned from tcp_parse_options.
1277c478bd9Sstevel@tonic-gate */
1287c478bd9Sstevel@tonic-gate #define TCP_OPT_MSS_PRESENT 1
1297c478bd9Sstevel@tonic-gate #define TCP_OPT_WSCALE_PRESENT 2
1307c478bd9Sstevel@tonic-gate #define TCP_OPT_TSTAMP_PRESENT 4
1317c478bd9Sstevel@tonic-gate #define TCP_OPT_SACK_OK_PRESENT 8
1327c478bd9Sstevel@tonic-gate #define TCP_OPT_SACK_PRESENT 16
1337c478bd9Sstevel@tonic-gate
1347c478bd9Sstevel@tonic-gate /* TCP option length */
1357c478bd9Sstevel@tonic-gate #define TCPOPT_NOP_LEN 1
1367c478bd9Sstevel@tonic-gate #define TCPOPT_MAXSEG_LEN 4
1377c478bd9Sstevel@tonic-gate #define TCPOPT_WS_LEN 3
1387c478bd9Sstevel@tonic-gate #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1)
1397c478bd9Sstevel@tonic-gate #define TCPOPT_TSTAMP_LEN 10
1407c478bd9Sstevel@tonic-gate #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2)
1417c478bd9Sstevel@tonic-gate #define TCPOPT_SACK_OK_LEN 2
1427c478bd9Sstevel@tonic-gate #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2)
1437c478bd9Sstevel@tonic-gate #define TCPOPT_REAL_SACK_LEN 4
1447c478bd9Sstevel@tonic-gate #define TCPOPT_MAX_SACK_LEN 36
1457c478bd9Sstevel@tonic-gate #define TCPOPT_HEADER_LEN 2
1467c478bd9Sstevel@tonic-gate
1477c478bd9Sstevel@tonic-gate /* TCP cwnd burst factor. */
1487c478bd9Sstevel@tonic-gate #define TCP_CWND_INFINITE 65535
1497c478bd9Sstevel@tonic-gate #define TCP_CWND_SS 3
1507c478bd9Sstevel@tonic-gate #define TCP_CWND_NORMAL 5
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1537c478bd9Sstevel@tonic-gate typedef struct tcpparam_s {
1547c478bd9Sstevel@tonic-gate uint32_t tcp_param_min;
1557c478bd9Sstevel@tonic-gate uint32_t tcp_param_max;
1567c478bd9Sstevel@tonic-gate uint32_t tcp_param_val;
1577c478bd9Sstevel@tonic-gate char *tcp_param_name;
1587c478bd9Sstevel@tonic-gate } tcpparam_t;
1597c478bd9Sstevel@tonic-gate
1607c478bd9Sstevel@tonic-gate /* Max size IP datagram is 64k - 1 */
1617c478bd9Sstevel@tonic-gate #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (struct ip) + \
1627c478bd9Sstevel@tonic-gate sizeof (tcph_t)))
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate /* Max of the above */
1657c478bd9Sstevel@tonic-gate #define TCP_MSS_MAX TCP_MSS_MAX_IPV4
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate /* Largest TCP port number */
1687c478bd9Sstevel@tonic-gate #define TCP_MAX_PORT (64 * 1024 - 1)
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate /* Round up the value to the nearest mss. */
1717c478bd9Sstevel@tonic-gate #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss))
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate #define MS 1L
1747c478bd9Sstevel@tonic-gate #define SECONDS (1000 * MS)
1757c478bd9Sstevel@tonic-gate #define MINUTES (60 * SECONDS)
1767c478bd9Sstevel@tonic-gate #define HOURS (60 * MINUTES)
1777c478bd9Sstevel@tonic-gate #define DAYS (24 * HOURS)
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate /* All NDD params in the core TCP became static variables. */
1807c478bd9Sstevel@tonic-gate static int tcp_time_wait_interval = 1 * MINUTES;
1817c478bd9Sstevel@tonic-gate static int tcp_conn_req_max_q = 128;
1827c478bd9Sstevel@tonic-gate static int tcp_conn_req_max_q0 = 1024;
1837c478bd9Sstevel@tonic-gate static int tcp_conn_req_min = 1;
1847c478bd9Sstevel@tonic-gate static int tcp_conn_grace_period = 0 * SECONDS;
1857c478bd9Sstevel@tonic-gate static int tcp_cwnd_max_ = 1024 * 1024;
1867c478bd9Sstevel@tonic-gate static int tcp_smallest_nonpriv_port = 1024;
1877c478bd9Sstevel@tonic-gate static int tcp_ip_abort_cinterval = 3 * MINUTES;
1887c478bd9Sstevel@tonic-gate static int tcp_ip_abort_linterval = 3 * MINUTES;
1897c478bd9Sstevel@tonic-gate static int tcp_ip_abort_interval = 8 * MINUTES;
1907c478bd9Sstevel@tonic-gate static int tcp_ip_notify_cinterval = 10 * SECONDS;
1917c478bd9Sstevel@tonic-gate static int tcp_ip_notify_interval = 10 * SECONDS;
1927c478bd9Sstevel@tonic-gate static int tcp_ipv4_ttl = 64;
1937c478bd9Sstevel@tonic-gate static int tcp_mss_def_ipv4 = 536;
1947c478bd9Sstevel@tonic-gate static int tcp_mss_max_ipv4 = TCP_MSS_MAX_IPV4;
1957c478bd9Sstevel@tonic-gate static int tcp_mss_min = 108;
1967c478bd9Sstevel@tonic-gate static int tcp_naglim_def = (4*1024)-1;
1977c478bd9Sstevel@tonic-gate static int tcp_rexmit_interval_initial = 3 * SECONDS;
1987c478bd9Sstevel@tonic-gate static int tcp_rexmit_interval_max = 60 * SECONDS;
1997c478bd9Sstevel@tonic-gate static int tcp_rexmit_interval_min = 400 * MS;
2007c478bd9Sstevel@tonic-gate static int tcp_dupack_fast_retransmit = 3;
2017c478bd9Sstevel@tonic-gate static int tcp_smallest_anon_port = 32 * 1024;
2027c478bd9Sstevel@tonic-gate static int tcp_largest_anon_port = TCP_MAX_PORT;
2037c478bd9Sstevel@tonic-gate static int tcp_xmit_lowat = TCP_XMIT_LOWATER;
2047c478bd9Sstevel@tonic-gate static int tcp_recv_hiwat_minmss = 4;
2057c478bd9Sstevel@tonic-gate static int tcp_fin_wait_2_flush_interval = 1 * MINUTES;
2067c478bd9Sstevel@tonic-gate static int tcp_max_buf = 1024 * 1024;
2077c478bd9Sstevel@tonic-gate static int tcp_wscale_always = 1;
2087c478bd9Sstevel@tonic-gate static int tcp_tstamp_always = 1;
2097c478bd9Sstevel@tonic-gate static int tcp_tstamp_if_wscale = 1;
2107c478bd9Sstevel@tonic-gate static int tcp_rexmit_interval_extra = 0;
2117c478bd9Sstevel@tonic-gate static int tcp_slow_start_after_idle = 2;
2127c478bd9Sstevel@tonic-gate static int tcp_slow_start_initial = 2;
2137c478bd9Sstevel@tonic-gate static int tcp_sack_permitted = 2;
2147c478bd9Sstevel@tonic-gate static int tcp_ecn_permitted = 2;
2157c478bd9Sstevel@tonic-gate
2167c478bd9Sstevel@tonic-gate /* Extra room to fit in headers. */
2177c478bd9Sstevel@tonic-gate static uint_t tcp_wroff_xtra;
2187c478bd9Sstevel@tonic-gate
2197c478bd9Sstevel@tonic-gate /* Hint for next port to try. */
2207c478bd9Sstevel@tonic-gate static in_port_t tcp_next_port_to_try = 32*1024;
2217c478bd9Sstevel@tonic-gate
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate * Figure out the value of window scale opton. Note that the rwnd is
2247c478bd9Sstevel@tonic-gate * ASSUMED to be rounded up to the nearest MSS before the calculation.
2257c478bd9Sstevel@tonic-gate * We cannot find the scale value and then do a round up of tcp_rwnd
2267c478bd9Sstevel@tonic-gate * because the scale value may not be correct after that.
2277c478bd9Sstevel@tonic-gate */
2287c478bd9Sstevel@tonic-gate #define SET_WS_VALUE(tcp) \
2297c478bd9Sstevel@tonic-gate { \
2307c478bd9Sstevel@tonic-gate int i; \
2317c478bd9Sstevel@tonic-gate uint32_t rwnd = (tcp)->tcp_rwnd; \
2327c478bd9Sstevel@tonic-gate for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; \
2337c478bd9Sstevel@tonic-gate i++, rwnd >>= 1) \
2347c478bd9Sstevel@tonic-gate ; \
2357c478bd9Sstevel@tonic-gate (tcp)->tcp_rcv_ws = i; \
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /*
2397c478bd9Sstevel@tonic-gate * Set ECN capable transport (ECT) code point in IP header.
2407c478bd9Sstevel@tonic-gate *
2417c478bd9Sstevel@tonic-gate * Note that there are 2 ECT code points '01' and '10', which are called
2427c478bd9Sstevel@tonic-gate * ECT(1) and ECT(0) respectively. Here we follow the original ECT code
2437c478bd9Sstevel@tonic-gate * point ECT(0) for TCP as described in RFC 2481.
2447c478bd9Sstevel@tonic-gate */
2457c478bd9Sstevel@tonic-gate #define SET_ECT(tcp, iph) \
2467c478bd9Sstevel@tonic-gate if ((tcp)->tcp_ipversion == IPV4_VERSION) { \
2477c478bd9Sstevel@tonic-gate /* We need to clear the code point first. */ \
2487c478bd9Sstevel@tonic-gate ((struct ip *)(iph))->ip_tos &= 0xFC; \
2497c478bd9Sstevel@tonic-gate ((struct ip *)(iph))->ip_tos |= IPH_ECN_ECT0; \
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * The format argument to pass to tcp_display().
2547c478bd9Sstevel@tonic-gate * DISP_PORT_ONLY means that the returned string has only port info.
2557c478bd9Sstevel@tonic-gate * DISP_ADDR_AND_PORT means that the returned string also contains the
2567c478bd9Sstevel@tonic-gate * remote and local IP address.
2577c478bd9Sstevel@tonic-gate */
2587c478bd9Sstevel@tonic-gate #define DISP_PORT_ONLY 1
2597c478bd9Sstevel@tonic-gate #define DISP_ADDR_AND_PORT 2
2607c478bd9Sstevel@tonic-gate
2617c478bd9Sstevel@tonic-gate /*
2627c478bd9Sstevel@tonic-gate * TCP reassembly macros. We hide starting and ending sequence numbers in
2637c478bd9Sstevel@tonic-gate * b_next and b_prev of messages on the reassembly queue. The messages are
2647c478bd9Sstevel@tonic-gate * chained using b_cont. These macros are used in tcp_reass() so we don't
2657c478bd9Sstevel@tonic-gate * have to see the ugly casts and assignments.
26653391bafSeota * Note. use uintptr_t to suppress the gcc warning.
2677c478bd9Sstevel@tonic-gate */
26853391bafSeota #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next))
26953391bafSeota #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \
27053391bafSeota (mblk_t *)((uintptr_t)(u)))
27153391bafSeota #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev))
27253391bafSeota #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \
27353391bafSeota (mblk_t *)((uintptr_t)(u)))
2747c478bd9Sstevel@tonic-gate
2757c478bd9Sstevel@tonic-gate #define TCP_TIMER_RESTART(tcp, intvl) \
2767c478bd9Sstevel@tonic-gate (tcp)->tcp_rto_timeout = prom_gettime() + intvl; \
2777c478bd9Sstevel@tonic-gate (tcp)->tcp_timer_running = B_TRUE;
2787c478bd9Sstevel@tonic-gate
2797c478bd9Sstevel@tonic-gate static int tcp_accept_comm(tcp_t *, tcp_t *, mblk_t *, uint_t);
2807c478bd9Sstevel@tonic-gate static mblk_t *tcp_ack_mp(tcp_t *);
2817c478bd9Sstevel@tonic-gate static in_port_t tcp_bindi(in_port_t, in_addr_t *, boolean_t, boolean_t);
2827c478bd9Sstevel@tonic-gate static uint16_t tcp_cksum(uint16_t *, uint32_t);
2837c478bd9Sstevel@tonic-gate static void tcp_clean_death(int, tcp_t *, int err);
2847c478bd9Sstevel@tonic-gate static tcp_t *tcp_conn_request(tcp_t *, mblk_t *mp, uint_t, uint_t);
2857c478bd9Sstevel@tonic-gate static char *tcp_display(tcp_t *, char *, char);
2867c478bd9Sstevel@tonic-gate static int tcp_drain_input(tcp_t *, int, int);
2877c478bd9Sstevel@tonic-gate static void tcp_drain_needed(int, tcp_t *);
2887c478bd9Sstevel@tonic-gate static boolean_t tcp_drop_q0(tcp_t *);
2897c478bd9Sstevel@tonic-gate static mblk_t *tcp_get_seg_mp(tcp_t *, uint32_t, int32_t *);
2907c478bd9Sstevel@tonic-gate static int tcp_header_len(struct inetgram *);
2917c478bd9Sstevel@tonic-gate static in_port_t tcp_report_ports(uint16_t *, enum Ports);
2927c478bd9Sstevel@tonic-gate static int tcp_input(int);
2937c478bd9Sstevel@tonic-gate static void tcp_iss_init(tcp_t *);
2947c478bd9Sstevel@tonic-gate static tcp_t *tcp_lookup_ipv4(struct ip *, tcpha_t *, int, int *);
2957c478bd9Sstevel@tonic-gate static tcp_t *tcp_lookup_listener_ipv4(in_addr_t, in_port_t, int *);
2967c478bd9Sstevel@tonic-gate static int tcp_conn_check(tcp_t *);
2977c478bd9Sstevel@tonic-gate static int tcp_close(int);
2987c478bd9Sstevel@tonic-gate static void tcp_close_detached(tcp_t *);
2997c478bd9Sstevel@tonic-gate static void tcp_eager_cleanup(tcp_t *, boolean_t, int);
3007c478bd9Sstevel@tonic-gate static void tcp_eager_unlink(tcp_t *);
3017c478bd9Sstevel@tonic-gate static void tcp_free(tcp_t *);
3027c478bd9Sstevel@tonic-gate static int tcp_header_init_ipv4(tcp_t *);
3037c478bd9Sstevel@tonic-gate static void tcp_mss_set(tcp_t *, uint32_t);
3047c478bd9Sstevel@tonic-gate static int tcp_parse_options(tcph_t *, tcp_opt_t *);
3057c478bd9Sstevel@tonic-gate static boolean_t tcp_paws_check(tcp_t *, tcph_t *, tcp_opt_t *);
3067c478bd9Sstevel@tonic-gate static void tcp_process_options(tcp_t *, tcph_t *);
3077c478bd9Sstevel@tonic-gate static int tcp_random(void);
3087c478bd9Sstevel@tonic-gate static void tcp_random_init(void);
3097c478bd9Sstevel@tonic-gate static mblk_t *tcp_reass(tcp_t *, mblk_t *, uint32_t);
3107c478bd9Sstevel@tonic-gate static void tcp_reass_elim_overlap(tcp_t *, mblk_t *);
3117c478bd9Sstevel@tonic-gate static void tcp_rcv_drain(int sock_id, tcp_t *);
3127c478bd9Sstevel@tonic-gate static void tcp_rcv_enqueue(tcp_t *, mblk_t *, uint_t);
3137c478bd9Sstevel@tonic-gate static void tcp_rput_data(tcp_t *, mblk_t *, int);
3147c478bd9Sstevel@tonic-gate static int tcp_rwnd_set(tcp_t *, uint32_t);
3157c478bd9Sstevel@tonic-gate static int32_t tcp_sack_rxmit(tcp_t *, int);
3167c478bd9Sstevel@tonic-gate static void tcp_set_cksum(mblk_t *);
3177c478bd9Sstevel@tonic-gate static void tcp_set_rto(tcp_t *, int32_t);
3187c478bd9Sstevel@tonic-gate static void tcp_ss_rexmit(tcp_t *, int);
3197c478bd9Sstevel@tonic-gate static int tcp_state_wait(int, tcp_t *, int);
3207c478bd9Sstevel@tonic-gate static void tcp_timer(tcp_t *, int);
3217c478bd9Sstevel@tonic-gate static void tcp_time_wait_append(tcp_t *);
3227c478bd9Sstevel@tonic-gate static void tcp_time_wait_collector(void);
3237c478bd9Sstevel@tonic-gate static void tcp_time_wait_processing(tcp_t *, mblk_t *, uint32_t,
3247c478bd9Sstevel@tonic-gate uint32_t, int, tcph_t *, int sock_id);
3257c478bd9Sstevel@tonic-gate static void tcp_time_wait_remove(tcp_t *);
3267c478bd9Sstevel@tonic-gate static in_port_t tcp_update_next_port(in_port_t);
3277c478bd9Sstevel@tonic-gate static int tcp_verify_cksum(mblk_t *);
3287c478bd9Sstevel@tonic-gate static void tcp_wput_data(tcp_t *, mblk_t *, int);
3297c478bd9Sstevel@tonic-gate static void tcp_xmit_ctl(char *, tcp_t *, mblk_t *, uint32_t, uint32_t,
3307c478bd9Sstevel@tonic-gate int, uint_t, int);
3317c478bd9Sstevel@tonic-gate static void tcp_xmit_early_reset(char *, int, mblk_t *, uint32_t, uint32_t,
3327c478bd9Sstevel@tonic-gate int, uint_t);
3337c478bd9Sstevel@tonic-gate static int tcp_xmit_end(tcp_t *, int);
3347c478bd9Sstevel@tonic-gate static void tcp_xmit_listeners_reset(int, mblk_t *, uint_t);
3357c478bd9Sstevel@tonic-gate static mblk_t *tcp_xmit_mp(tcp_t *, mblk_t *, int32_t, int32_t *,
3367c478bd9Sstevel@tonic-gate mblk_t **, uint32_t, boolean_t, uint32_t *, boolean_t);
3377c478bd9Sstevel@tonic-gate static int tcp_init_values(tcp_t *, struct inetboot_socket *);
3387c478bd9Sstevel@tonic-gate
3397c478bd9Sstevel@tonic-gate #if DEBUG > 1
3407c478bd9Sstevel@tonic-gate #define TCP_DUMP_PACKET(str, mp) \
3417c478bd9Sstevel@tonic-gate { \
3427c478bd9Sstevel@tonic-gate int len = (mp)->b_wptr - (mp)->b_rptr; \
3437c478bd9Sstevel@tonic-gate \
3447c478bd9Sstevel@tonic-gate printf("%s: dump TCP(%d): \n", (str), len); \
3457c478bd9Sstevel@tonic-gate hexdump((char *)(mp)->b_rptr, len); \
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate #else
3487c478bd9Sstevel@tonic-gate #define TCP_DUMP_PACKET(str, mp)
3497c478bd9Sstevel@tonic-gate #endif
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate #ifdef DEBUG
3527c478bd9Sstevel@tonic-gate #define DEBUG_1(str, arg) printf(str, (arg))
3537c478bd9Sstevel@tonic-gate #define DEBUG_2(str, arg1, arg2) printf(str, (arg1), (arg2))
3547c478bd9Sstevel@tonic-gate #define DEBUG_3(str, arg1, arg2, arg3) printf(str, (arg1), (arg2), (arg3))
3557c478bd9Sstevel@tonic-gate #else
3567c478bd9Sstevel@tonic-gate #define DEBUG_1(str, arg)
3577c478bd9Sstevel@tonic-gate #define DEBUG_2(str, arg1, arg2)
3587c478bd9Sstevel@tonic-gate #define DEBUG_3(str, arg1, arg2, arg3)
3597c478bd9Sstevel@tonic-gate #endif
3607c478bd9Sstevel@tonic-gate
3617c478bd9Sstevel@tonic-gate /* Whether it is the first time TCP is used. */
3627c478bd9Sstevel@tonic-gate static boolean_t tcp_initialized = B_FALSE;
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /* TCP time wait list. */
3657c478bd9Sstevel@tonic-gate static tcp_t *tcp_time_wait_head;
3667c478bd9Sstevel@tonic-gate static tcp_t *tcp_time_wait_tail;
3677c478bd9Sstevel@tonic-gate static uint32_t tcp_cum_timewait;
3687c478bd9Sstevel@tonic-gate /* When the tcp_time_wait_collector is run. */
3697c478bd9Sstevel@tonic-gate static uint32_t tcp_time_wait_runtime;
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate #define TCP_RUN_TIME_WAIT_COLLECTOR() \
3727c478bd9Sstevel@tonic-gate if (prom_gettime() > tcp_time_wait_runtime) \
3737c478bd9Sstevel@tonic-gate tcp_time_wait_collector();
3747c478bd9Sstevel@tonic-gate
3757c478bd9Sstevel@tonic-gate /*
3767c478bd9Sstevel@tonic-gate * Accept will return with an error if there is no connection coming in
3777c478bd9Sstevel@tonic-gate * after this (in ms).
3787c478bd9Sstevel@tonic-gate */
3797c478bd9Sstevel@tonic-gate static int tcp_accept_timeout = 60000;
3807c478bd9Sstevel@tonic-gate
3817c478bd9Sstevel@tonic-gate /*
3827c478bd9Sstevel@tonic-gate * Initialize the TCP-specific parts of a socket.
3837c478bd9Sstevel@tonic-gate */
3847c478bd9Sstevel@tonic-gate void
tcp_socket_init(struct inetboot_socket * isp)3857c478bd9Sstevel@tonic-gate tcp_socket_init(struct inetboot_socket *isp)
3867c478bd9Sstevel@tonic-gate {
3877c478bd9Sstevel@tonic-gate /* Do some initializations. */
3887c478bd9Sstevel@tonic-gate if (!tcp_initialized) {
3897c478bd9Sstevel@tonic-gate tcp_random_init();
3907c478bd9Sstevel@tonic-gate /* Extra head room for the MAC layer address. */
3917c478bd9Sstevel@tonic-gate if ((tcp_wroff_xtra = mac_get_hdr_len()) & 0x3) {
3927c478bd9Sstevel@tonic-gate tcp_wroff_xtra = (tcp_wroff_xtra & ~0x3) + 0x4;
3937c478bd9Sstevel@tonic-gate }
3947c478bd9Sstevel@tonic-gate /* Schedule the first time wait cleanup time */
3957c478bd9Sstevel@tonic-gate tcp_time_wait_runtime = prom_gettime() + tcp_time_wait_interval;
3967c478bd9Sstevel@tonic-gate tcp_initialized = B_TRUE;
3977c478bd9Sstevel@tonic-gate }
3987c478bd9Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate isp->proto = IPPROTO_TCP;
4017c478bd9Sstevel@tonic-gate isp->input[TRANSPORT_LVL] = tcp_input;
4027c478bd9Sstevel@tonic-gate /* Socket layer should call tcp_send() directly. */
4037c478bd9Sstevel@tonic-gate isp->output[TRANSPORT_LVL] = NULL;
4047c478bd9Sstevel@tonic-gate isp->close[TRANSPORT_LVL] = tcp_close;
4057c478bd9Sstevel@tonic-gate isp->headerlen[TRANSPORT_LVL] = tcp_header_len;
4067c478bd9Sstevel@tonic-gate isp->ports = tcp_report_ports;
4077c478bd9Sstevel@tonic-gate if ((isp->pcb = bkmem_alloc(sizeof (tcp_t))) == NULL) {
4087c478bd9Sstevel@tonic-gate errno = ENOBUFS;
4097c478bd9Sstevel@tonic-gate return;
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate if ((errno = tcp_init_values((tcp_t *)isp->pcb, isp)) != 0) {
4127c478bd9Sstevel@tonic-gate bkmem_free(isp->pcb, sizeof (tcp_t));
4137c478bd9Sstevel@tonic-gate return;
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate /*
4167c478bd9Sstevel@tonic-gate * This is set last because this field is used to determine if
4177c478bd9Sstevel@tonic-gate * a socket is in use or not.
4187c478bd9Sstevel@tonic-gate */
4197c478bd9Sstevel@tonic-gate isp->type = INETBOOT_STREAM;
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate /*
4237c478bd9Sstevel@tonic-gate * Return the size of a TCP header including TCP option.
4247c478bd9Sstevel@tonic-gate */
4257c478bd9Sstevel@tonic-gate static int
tcp_header_len(struct inetgram * igm)4267c478bd9Sstevel@tonic-gate tcp_header_len(struct inetgram *igm)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate mblk_t *pkt;
4297c478bd9Sstevel@tonic-gate int ipvers;
4307c478bd9Sstevel@tonic-gate
4317c478bd9Sstevel@tonic-gate /* Just returns the standard TCP header without option */
4327c478bd9Sstevel@tonic-gate if (igm == NULL)
4337c478bd9Sstevel@tonic-gate return (sizeof (tcph_t));
4347c478bd9Sstevel@tonic-gate
4357c478bd9Sstevel@tonic-gate if ((pkt = igm->igm_mp) == NULL)
4367c478bd9Sstevel@tonic-gate return (0);
4377c478bd9Sstevel@tonic-gate
4387c478bd9Sstevel@tonic-gate ipvers = ((struct ip *)pkt->b_rptr)->ip_v;
4397c478bd9Sstevel@tonic-gate if (ipvers == IPV4_VERSION) {
4407c478bd9Sstevel@tonic-gate return (TCP_HDR_LENGTH((tcph_t *)(pkt + IPH_HDR_LENGTH(pkt))));
4417c478bd9Sstevel@tonic-gate } else {
4427c478bd9Sstevel@tonic-gate dprintf("tcp_header_len: non-IPv4 packet.\n");
4437c478bd9Sstevel@tonic-gate return (0);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate }
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate /*
4487c478bd9Sstevel@tonic-gate * Return the requested port number in network order.
4497c478bd9Sstevel@tonic-gate */
4507c478bd9Sstevel@tonic-gate static in_port_t
tcp_report_ports(uint16_t * tcphp,enum Ports request)4517c478bd9Sstevel@tonic-gate tcp_report_ports(uint16_t *tcphp, enum Ports request)
4527c478bd9Sstevel@tonic-gate {
4537c478bd9Sstevel@tonic-gate if (request == SOURCE)
4547c478bd9Sstevel@tonic-gate return (*(uint16_t *)(((tcph_t *)tcphp)->th_lport));
4557c478bd9Sstevel@tonic-gate return (*(uint16_t *)(((tcph_t *)tcphp)->th_fport));
4567c478bd9Sstevel@tonic-gate }
4577c478bd9Sstevel@tonic-gate
4587c478bd9Sstevel@tonic-gate /*
4597c478bd9Sstevel@tonic-gate * Because inetboot is not interrupt driven, TCP can only poll. This
4607c478bd9Sstevel@tonic-gate * means that there can be packets stuck in the NIC buffer waiting to
4617c478bd9Sstevel@tonic-gate * be processed. Thus we need to drain them before, for example, sending
4627c478bd9Sstevel@tonic-gate * anything because an ACK may actually be stuck there.
4637c478bd9Sstevel@tonic-gate *
4647c478bd9Sstevel@tonic-gate * The timeout arguments determine how long we should wait for draining.
4657c478bd9Sstevel@tonic-gate */
4667c478bd9Sstevel@tonic-gate static int
tcp_drain_input(tcp_t * tcp,int sock_id,int timeout)4677c478bd9Sstevel@tonic-gate tcp_drain_input(tcp_t *tcp, int sock_id, int timeout)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate struct inetgram *in_gram;
4707c478bd9Sstevel@tonic-gate struct inetgram *old_in_gram;
4717c478bd9Sstevel@tonic-gate int old_timeout;
4727c478bd9Sstevel@tonic-gate mblk_t *mp;
4737c478bd9Sstevel@tonic-gate int i;
4747c478bd9Sstevel@tonic-gate
4757c478bd9Sstevel@tonic-gate dprintf("tcp_drain_input(%d): %s\n", sock_id,
4767c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
4777c478bd9Sstevel@tonic-gate
4787c478bd9Sstevel@tonic-gate /*
4797c478bd9Sstevel@tonic-gate * Since the driver uses the in_timeout value in the socket
4807c478bd9Sstevel@tonic-gate * structure to determine the timeout value, we need to save
4817c478bd9Sstevel@tonic-gate * the original one so that we can restore that after draining.
4827c478bd9Sstevel@tonic-gate */
4837c478bd9Sstevel@tonic-gate old_timeout = sockets[sock_id].in_timeout;
4847c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = timeout;
4857c478bd9Sstevel@tonic-gate
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate * We do this because the input queue may have some user
4887c478bd9Sstevel@tonic-gate * data already.
4897c478bd9Sstevel@tonic-gate */
4907c478bd9Sstevel@tonic-gate old_in_gram = sockets[sock_id].inq;
4917c478bd9Sstevel@tonic-gate sockets[sock_id].inq = NULL;
4927c478bd9Sstevel@tonic-gate
4937c478bd9Sstevel@tonic-gate /* Go out and check the wire */
4947c478bd9Sstevel@tonic-gate for (i = MEDIA_LVL; i < TRANSPORT_LVL; i++) {
4957c478bd9Sstevel@tonic-gate if (sockets[sock_id].input[i] != NULL) {
4967c478bd9Sstevel@tonic-gate if (sockets[sock_id].input[i](sock_id) < 0) {
4977c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
4987c478bd9Sstevel@tonic-gate if (sockets[sock_id].inq != NULL)
4997c478bd9Sstevel@tonic-gate nuke_grams(&sockets[sock_id].inq);
5007c478bd9Sstevel@tonic-gate sockets[sock_id].inq = old_in_gram;
5017c478bd9Sstevel@tonic-gate return (-1);
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate }
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate #if DEBUG
5067c478bd9Sstevel@tonic-gate printf("tcp_drain_input: done with checking packets\n");
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
5097c478bd9Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
5107c478bd9Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
5117c478bd9Sstevel@tonic-gate #if DEBUG
5127c478bd9Sstevel@tonic-gate printf("tcp_drain_input: unexpected packet "
5137c478bd9Sstevel@tonic-gate "level %d frame found\n", in_gram->igm_level);
5147c478bd9Sstevel@tonic-gate #endif
5157c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
5167c478bd9Sstevel@tonic-gate continue;
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate mp = in_gram->igm_mp;
5197c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
5207c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
5217c478bd9Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
5227c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
5237c478bd9Sstevel@tonic-gate
5247c478bd9Sstevel@tonic-gate /*
5257c478bd9Sstevel@tonic-gate * The other side may have closed this connection or
5267c478bd9Sstevel@tonic-gate * RST us. But we need to continue to process other
5277c478bd9Sstevel@tonic-gate * packets in the socket's queue because they may be
5287c478bd9Sstevel@tonic-gate * belong to another TCP connections.
5297c478bd9Sstevel@tonic-gate */
5307c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
5317c478bd9Sstevel@tonic-gate tcp = NULL;
5327c478bd9Sstevel@tonic-gate }
5337c478bd9Sstevel@tonic-gate
5347c478bd9Sstevel@tonic-gate if (tcp == NULL || sockets[sock_id].pcb == NULL) {
5357c478bd9Sstevel@tonic-gate if (sockets[sock_id].so_error != 0)
5367c478bd9Sstevel@tonic-gate return (-1);
5377c478bd9Sstevel@tonic-gate else
5387c478bd9Sstevel@tonic-gate return (0);
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate #if DEBUG
5417c478bd9Sstevel@tonic-gate printf("tcp_drain_input: done with processing packets\n");
5427c478bd9Sstevel@tonic-gate #endif
5437c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
5447c478bd9Sstevel@tonic-gate sockets[sock_id].inq = old_in_gram;
5457c478bd9Sstevel@tonic-gate
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate * Data may have been received so indicate it is available
5487c478bd9Sstevel@tonic-gate */
5497c478bd9Sstevel@tonic-gate tcp_drain_needed(sock_id, tcp);
5507c478bd9Sstevel@tonic-gate return (0);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate * The receive entry point for upper layer to call to get data. Note
5557c478bd9Sstevel@tonic-gate * that this follows the current architecture that lower layer receive
5567c478bd9Sstevel@tonic-gate * routines have been called already. Thus if the inq of socket is
5577c478bd9Sstevel@tonic-gate * not NULL, the packets must be for us.
5587c478bd9Sstevel@tonic-gate */
5597c478bd9Sstevel@tonic-gate static int
tcp_input(int sock_id)5607c478bd9Sstevel@tonic-gate tcp_input(int sock_id)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate struct inetgram *in_gram;
5637c478bd9Sstevel@tonic-gate mblk_t *mp;
5647c478bd9Sstevel@tonic-gate tcp_t *tcp;
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
5677c478bd9Sstevel@tonic-gate
5687c478bd9Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL)
5697c478bd9Sstevel@tonic-gate return (-1);
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
5727c478bd9Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
5737c478bd9Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
5747c478bd9Sstevel@tonic-gate #ifdef DEBUG
5757c478bd9Sstevel@tonic-gate printf("tcp_input: unexpected packet "
5767c478bd9Sstevel@tonic-gate "level %d frame found\n", in_gram->igm_level);
5777c478bd9Sstevel@tonic-gate #endif
5787c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
5797c478bd9Sstevel@tonic-gate continue;
5807c478bd9Sstevel@tonic-gate }
5817c478bd9Sstevel@tonic-gate mp = in_gram->igm_mp;
5827c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
5837c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
5847c478bd9Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
5857c478bd9Sstevel@tonic-gate /* The TCP may be gone because it gets a RST. */
5867c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
5877c478bd9Sstevel@tonic-gate return (-1);
5887c478bd9Sstevel@tonic-gate }
5897c478bd9Sstevel@tonic-gate
5907c478bd9Sstevel@tonic-gate /* Flush the receive list. */
5917c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
5927c478bd9Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
5937c478bd9Sstevel@tonic-gate } else {
5947c478bd9Sstevel@tonic-gate /* The other side has closed the connection, report this up. */
5957c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_CLOSE_WAIT) {
5967c478bd9Sstevel@tonic-gate sockets[sock_id].so_state |= SS_CANTRCVMORE;
5977c478bd9Sstevel@tonic-gate return (0);
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate }
6007c478bd9Sstevel@tonic-gate return (0);
6017c478bd9Sstevel@tonic-gate }
6027c478bd9Sstevel@tonic-gate
6037c478bd9Sstevel@tonic-gate /*
6047c478bd9Sstevel@tonic-gate * The send entry point for upper layer to call to send data. In order
6057c478bd9Sstevel@tonic-gate * to minimize changes to the core TCP code, we need to put the
6067c478bd9Sstevel@tonic-gate * data into mblks.
6077c478bd9Sstevel@tonic-gate */
6087c478bd9Sstevel@tonic-gate int
tcp_send(int sock_id,tcp_t * tcp,const void * msg,int len)6097c478bd9Sstevel@tonic-gate tcp_send(int sock_id, tcp_t *tcp, const void *msg, int len)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate mblk_t *mp;
6127c478bd9Sstevel@tonic-gate mblk_t *head = NULL;
6137c478bd9Sstevel@tonic-gate mblk_t *tail;
6147c478bd9Sstevel@tonic-gate int mss = tcp->tcp_mss;
6157c478bd9Sstevel@tonic-gate int cnt = 0;
6167c478bd9Sstevel@tonic-gate int win_size;
6177c478bd9Sstevel@tonic-gate char *buf = (char *)msg;
6187c478bd9Sstevel@tonic-gate
6197c478bd9Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
6207c478bd9Sstevel@tonic-gate
6217c478bd9Sstevel@tonic-gate /* We don't want to append 0 size mblk. */
6227c478bd9Sstevel@tonic-gate if (len == 0)
6237c478bd9Sstevel@tonic-gate return (0);
6247c478bd9Sstevel@tonic-gate while (len > 0) {
6257c478bd9Sstevel@tonic-gate if (len < mss) {
6267c478bd9Sstevel@tonic-gate mss = len;
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate /*
6297c478bd9Sstevel@tonic-gate * If we cannot allocate more buffer, stop here and
6307c478bd9Sstevel@tonic-gate * the number of bytes buffered will be returned.
6317c478bd9Sstevel@tonic-gate *
6327c478bd9Sstevel@tonic-gate * Note that we follow the core TCP optimization that
6337c478bd9Sstevel@tonic-gate * each mblk contains only MSS bytes data.
6347c478bd9Sstevel@tonic-gate */
6357c478bd9Sstevel@tonic-gate if ((mp = allocb(mss + tcp->tcp_ip_hdr_len +
6367c478bd9Sstevel@tonic-gate TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 0)) == NULL) {
6377c478bd9Sstevel@tonic-gate break;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate mp->b_rptr += tcp->tcp_hdr_len + tcp_wroff_xtra;
6407c478bd9Sstevel@tonic-gate bcopy(buf, mp->b_rptr, mss);
6417c478bd9Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + mss;
6427c478bd9Sstevel@tonic-gate buf += mss;
6437c478bd9Sstevel@tonic-gate cnt += mss;
6447c478bd9Sstevel@tonic-gate len -= mss;
6457c478bd9Sstevel@tonic-gate
6467c478bd9Sstevel@tonic-gate if (head == NULL) {
6477c478bd9Sstevel@tonic-gate head = mp;
6487c478bd9Sstevel@tonic-gate tail = mp;
6497c478bd9Sstevel@tonic-gate } else {
6507c478bd9Sstevel@tonic-gate tail->b_cont = mp;
6517c478bd9Sstevel@tonic-gate tail = mp;
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate }
6547c478bd9Sstevel@tonic-gate
6557c478bd9Sstevel@tonic-gate /*
6567c478bd9Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
6577c478bd9Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
6587c478bd9Sstevel@tonic-gate * otherwise, we may not be able to send.
6597c478bd9Sstevel@tonic-gate *
6607c478bd9Sstevel@tonic-gate * We expect an ACK in two cases:
6617c478bd9Sstevel@tonic-gate *
6627c478bd9Sstevel@tonic-gate * 1) We have un-ACK'ed data.
6637c478bd9Sstevel@tonic-gate *
6647c478bd9Sstevel@tonic-gate * 2) All ACK's have been received and the sender's window has been
6657c478bd9Sstevel@tonic-gate * closed. We need an ACK back to open the window so that we can
6667c478bd9Sstevel@tonic-gate * send. In this case, call tcp_drain_input() if the window size is
6677c478bd9Sstevel@tonic-gate * less than 2 * MSS.
6687c478bd9Sstevel@tonic-gate */
6697c478bd9Sstevel@tonic-gate
6707c478bd9Sstevel@tonic-gate /* window size = MIN(swnd, cwnd) - unacked bytes */
6717c478bd9Sstevel@tonic-gate win_size = (tcp->tcp_swnd > tcp->tcp_cwnd) ? tcp->tcp_cwnd :
6727c478bd9Sstevel@tonic-gate tcp->tcp_swnd;
6737c478bd9Sstevel@tonic-gate win_size -= tcp->tcp_snxt;
6747c478bd9Sstevel@tonic-gate win_size += tcp->tcp_suna;
6757c478bd9Sstevel@tonic-gate if (win_size < (2 * tcp->tcp_mss))
6767c478bd9Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0)
6777c478bd9Sstevel@tonic-gate return (-1);
6787c478bd9Sstevel@tonic-gate
6797c478bd9Sstevel@tonic-gate tcp_wput_data(tcp, head, sock_id);
6809f1fc992Sss146032 /*
6819f1fc992Sss146032 * errno should be reset here as it may be
6829f1fc992Sss146032 * set to ETIMEDOUT. This may be set by
6839f1fc992Sss146032 * the MAC driver in case it has timed out
6849f1fc992Sss146032 * waiting for ARP reply. Any segment which
6859f1fc992Sss146032 * was not transmitted because of ARP timeout
6869f1fc992Sss146032 * will be retransmitted by TCP.
6879f1fc992Sss146032 */
6889f1fc992Sss146032 if (errno == ETIMEDOUT)
6899f1fc992Sss146032 errno = 0;
6907c478bd9Sstevel@tonic-gate return (cnt);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate
6937c478bd9Sstevel@tonic-gate /* Free up all TCP related stuff */
6947c478bd9Sstevel@tonic-gate static void
tcp_free(tcp_t * tcp)6957c478bd9Sstevel@tonic-gate tcp_free(tcp_t *tcp)
6967c478bd9Sstevel@tonic-gate {
6977c478bd9Sstevel@tonic-gate if (tcp->tcp_iphc != NULL) {
6987c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_iphc, tcp->tcp_iphc_len);
6997c478bd9Sstevel@tonic-gate tcp->tcp_iphc = NULL;
7007c478bd9Sstevel@tonic-gate }
7017c478bd9Sstevel@tonic-gate if (tcp->tcp_xmit_head != NULL) {
7027c478bd9Sstevel@tonic-gate freemsg(tcp->tcp_xmit_head);
7037c478bd9Sstevel@tonic-gate tcp->tcp_xmit_head = NULL;
7047c478bd9Sstevel@tonic-gate }
7057c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
7067c478bd9Sstevel@tonic-gate freemsg(tcp->tcp_rcv_list);
7077c478bd9Sstevel@tonic-gate tcp->tcp_rcv_list = NULL;
7087c478bd9Sstevel@tonic-gate }
7097c478bd9Sstevel@tonic-gate if (tcp->tcp_reass_head != NULL) {
7107c478bd9Sstevel@tonic-gate freemsg(tcp->tcp_reass_head);
7117c478bd9Sstevel@tonic-gate tcp->tcp_reass_head = NULL;
7127c478bd9Sstevel@tonic-gate }
7137c478bd9Sstevel@tonic-gate if (tcp->tcp_sack_info != NULL) {
7147c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_sack_info,
7157c478bd9Sstevel@tonic-gate sizeof (tcp_sack_info_t));
7167c478bd9Sstevel@tonic-gate tcp->tcp_sack_info = NULL;
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate static void
tcp_close_detached(tcp_t * tcp)7217c478bd9Sstevel@tonic-gate tcp_close_detached(tcp_t *tcp)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate if (tcp->tcp_listener != NULL)
7247c478bd9Sstevel@tonic-gate tcp_eager_unlink(tcp);
7257c478bd9Sstevel@tonic-gate tcp_free(tcp);
7267c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate
7297c478bd9Sstevel@tonic-gate /*
7307c478bd9Sstevel@tonic-gate * If we are an eager connection hanging off a listener that hasn't
73148bbca81SDaniel Hoffman * formally accepted the connection yet, get off its list and blow off
7327c478bd9Sstevel@tonic-gate * any data that we have accumulated.
7337c478bd9Sstevel@tonic-gate */
7347c478bd9Sstevel@tonic-gate static void
tcp_eager_unlink(tcp_t * tcp)7357c478bd9Sstevel@tonic-gate tcp_eager_unlink(tcp_t *tcp)
7367c478bd9Sstevel@tonic-gate {
7377c478bd9Sstevel@tonic-gate tcp_t *listener = tcp->tcp_listener;
7387c478bd9Sstevel@tonic-gate
7397c478bd9Sstevel@tonic-gate assert(listener != NULL);
7407c478bd9Sstevel@tonic-gate if (tcp->tcp_eager_next_q0 != NULL) {
7417c478bd9Sstevel@tonic-gate assert(tcp->tcp_eager_prev_q0 != NULL);
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate /* Remove the eager tcp from q0 */
7447c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
7457c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
7467c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
7477c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0;
7487c478bd9Sstevel@tonic-gate listener->tcp_conn_req_cnt_q0--;
7497c478bd9Sstevel@tonic-gate } else {
7507c478bd9Sstevel@tonic-gate tcp_t **tcpp = &listener->tcp_eager_next_q;
7517c478bd9Sstevel@tonic-gate tcp_t *prev = NULL;
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) {
7547c478bd9Sstevel@tonic-gate if (tcpp[0] == tcp) {
7557c478bd9Sstevel@tonic-gate if (listener->tcp_eager_last_q == tcp) {
7567c478bd9Sstevel@tonic-gate /*
7577c478bd9Sstevel@tonic-gate * If we are unlinking the last
7587c478bd9Sstevel@tonic-gate * element on the list, adjust
7597c478bd9Sstevel@tonic-gate * tail pointer. Set tail pointer
7607c478bd9Sstevel@tonic-gate * to nil when list is empty.
7617c478bd9Sstevel@tonic-gate */
7627c478bd9Sstevel@tonic-gate assert(tcp->tcp_eager_next_q == NULL);
7637c478bd9Sstevel@tonic-gate if (listener->tcp_eager_last_q ==
7647c478bd9Sstevel@tonic-gate listener->tcp_eager_next_q) {
7657c478bd9Sstevel@tonic-gate listener->tcp_eager_last_q =
7667c478bd9Sstevel@tonic-gate NULL;
7677c478bd9Sstevel@tonic-gate } else {
7687c478bd9Sstevel@tonic-gate /*
7697c478bd9Sstevel@tonic-gate * We won't get here if there
7707c478bd9Sstevel@tonic-gate * is only one eager in the
7717c478bd9Sstevel@tonic-gate * list.
7727c478bd9Sstevel@tonic-gate */
7737c478bd9Sstevel@tonic-gate assert(prev != NULL);
7747c478bd9Sstevel@tonic-gate listener->tcp_eager_last_q =
7757c478bd9Sstevel@tonic-gate prev;
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate tcpp[0] = tcp->tcp_eager_next_q;
7797c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
7807c478bd9Sstevel@tonic-gate tcp->tcp_eager_last_q = NULL;
7817c478bd9Sstevel@tonic-gate listener->tcp_conn_req_cnt_q--;
7827c478bd9Sstevel@tonic-gate break;
7837c478bd9Sstevel@tonic-gate }
7847c478bd9Sstevel@tonic-gate prev = tcpp[0];
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate tcp->tcp_listener = NULL;
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate * Reset any eager connection hanging off this listener
7927c478bd9Sstevel@tonic-gate * and then reclaim it's resources.
7937c478bd9Sstevel@tonic-gate */
7947c478bd9Sstevel@tonic-gate static void
tcp_eager_cleanup(tcp_t * listener,boolean_t q0_only,int sock_id)7957c478bd9Sstevel@tonic-gate tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only, int sock_id)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate tcp_t *eager;
7987c478bd9Sstevel@tonic-gate
7997c478bd9Sstevel@tonic-gate if (!q0_only) {
8007c478bd9Sstevel@tonic-gate /* First cleanup q */
8017c478bd9Sstevel@tonic-gate while ((eager = listener->tcp_eager_next_q) != NULL) {
8027c478bd9Sstevel@tonic-gate assert(listener->tcp_eager_last_q != NULL);
8037c478bd9Sstevel@tonic-gate tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
8047c478bd9Sstevel@tonic-gate eager, NULL, eager->tcp_snxt, 0, TH_RST, 0,
8057c478bd9Sstevel@tonic-gate sock_id);
8067c478bd9Sstevel@tonic-gate tcp_close_detached(eager);
8077c478bd9Sstevel@tonic-gate }
8087c478bd9Sstevel@tonic-gate assert(listener->tcp_eager_last_q == NULL);
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate /* Then cleanup q0 */
8117c478bd9Sstevel@tonic-gate while ((eager = listener->tcp_eager_next_q0) != listener) {
8127c478bd9Sstevel@tonic-gate tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
8137c478bd9Sstevel@tonic-gate eager, NULL, eager->tcp_snxt, 0, TH_RST, 0, sock_id);
8147c478bd9Sstevel@tonic-gate tcp_close_detached(eager);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate * To handle the shutdown request. Called from shutdown()
8207c478bd9Sstevel@tonic-gate */
8217c478bd9Sstevel@tonic-gate int
tcp_shutdown(int sock_id)8227c478bd9Sstevel@tonic-gate tcp_shutdown(int sock_id)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate tcp_t *tcp;
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate DEBUG_1("tcp_shutdown: sock_id %x\n", sock_id);
8277c478bd9Sstevel@tonic-gate
8287c478bd9Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL) {
8297c478bd9Sstevel@tonic-gate return (-1);
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate
8327c478bd9Sstevel@tonic-gate /*
8337c478bd9Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
8347c478bd9Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
8357c478bd9Sstevel@tonic-gate * otherwise, we may not be able to send.
8367c478bd9Sstevel@tonic-gate */
8377c478bd9Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
8387c478bd9Sstevel@tonic-gate /*
8397c478bd9Sstevel@tonic-gate * If we return now without freeing TCP, there will be
8407c478bd9Sstevel@tonic-gate * a memory leak.
8417c478bd9Sstevel@tonic-gate */
8427c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL)
8437c478bd9Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
8447c478bd9Sstevel@tonic-gate return (-1);
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate
8477c478bd9Sstevel@tonic-gate DEBUG_1("tcp_shutdown: tcp_state %x\n", tcp->tcp_state);
8487c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
8497c478bd9Sstevel@tonic-gate
8507c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
8517c478bd9Sstevel@tonic-gate /*
8527c478bd9Sstevel@tonic-gate * Shutdown during the connect 3-way handshake
8537c478bd9Sstevel@tonic-gate */
8547c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
8557c478bd9Sstevel@tonic-gate /*
8567c478bd9Sstevel@tonic-gate * Transmit the FIN
8577c478bd9Sstevel@tonic-gate * wait for the FIN to be ACKed,
8587c478bd9Sstevel@tonic-gate * then remain in FIN_WAIT_2
8597c478bd9Sstevel@tonic-gate */
8607c478bd9Sstevel@tonic-gate dprintf("tcp_shutdown: sending fin\n");
8617c478bd9Sstevel@tonic-gate if (tcp_xmit_end(tcp, sock_id) == 0 &&
8627c478bd9Sstevel@tonic-gate tcp_state_wait(sock_id, tcp, TCPS_FIN_WAIT_2) < 0) {
8637c478bd9Sstevel@tonic-gate /* During the wait, TCP may be gone... */
8647c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
8657c478bd9Sstevel@tonic-gate return (-1);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate dprintf("tcp_shutdown: done\n");
8687c478bd9Sstevel@tonic-gate break;
8697c478bd9Sstevel@tonic-gate
8707c478bd9Sstevel@tonic-gate default:
8717c478bd9Sstevel@tonic-gate break;
8727c478bd9Sstevel@tonic-gate
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate return (0);
8757c478bd9Sstevel@tonic-gate }
8767c478bd9Sstevel@tonic-gate
8777c478bd9Sstevel@tonic-gate /* To handle closing of the socket */
8787c478bd9Sstevel@tonic-gate static int
tcp_close(int sock_id)8797c478bd9Sstevel@tonic-gate tcp_close(int sock_id)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate char *msg;
8827c478bd9Sstevel@tonic-gate tcp_t *tcp;
8837c478bd9Sstevel@tonic-gate int error = 0;
8847c478bd9Sstevel@tonic-gate
8857c478bd9Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL) {
8867c478bd9Sstevel@tonic-gate return (-1);
8877c478bd9Sstevel@tonic-gate }
8887c478bd9Sstevel@tonic-gate
8897c478bd9Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
8907c478bd9Sstevel@tonic-gate
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
8937c478bd9Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
8947c478bd9Sstevel@tonic-gate * otherwise, we may not be able to send.
8957c478bd9Sstevel@tonic-gate */
8967c478bd9Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
8977c478bd9Sstevel@tonic-gate /*
8987c478bd9Sstevel@tonic-gate * If we return now without freeing TCP, there will be
8997c478bd9Sstevel@tonic-gate * a memory leak.
9007c478bd9Sstevel@tonic-gate */
9017c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL)
9027c478bd9Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
9037c478bd9Sstevel@tonic-gate return (-1);
9047c478bd9Sstevel@tonic-gate }
9057c478bd9Sstevel@tonic-gate
9067c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
9077c478bd9Sstevel@tonic-gate /* Cleanup for listener */
9087c478bd9Sstevel@tonic-gate tcp_eager_cleanup(tcp, 0, sock_id);
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate
9117c478bd9Sstevel@tonic-gate msg = NULL;
9127c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
9137c478bd9Sstevel@tonic-gate case TCPS_CLOSED:
9147c478bd9Sstevel@tonic-gate case TCPS_IDLE:
9157c478bd9Sstevel@tonic-gate case TCPS_BOUND:
9167c478bd9Sstevel@tonic-gate case TCPS_LISTEN:
9177c478bd9Sstevel@tonic-gate break;
9187c478bd9Sstevel@tonic-gate case TCPS_SYN_SENT:
9197c478bd9Sstevel@tonic-gate msg = "tcp_close, during connect";
9207c478bd9Sstevel@tonic-gate break;
9217c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
9227c478bd9Sstevel@tonic-gate /*
9237c478bd9Sstevel@tonic-gate * Close during the connect 3-way handshake
9247c478bd9Sstevel@tonic-gate * but here there may or may not be pending data
9257c478bd9Sstevel@tonic-gate * already on queue. Process almost same as in
9267c478bd9Sstevel@tonic-gate * the ESTABLISHED state.
9277c478bd9Sstevel@tonic-gate */
9287c478bd9Sstevel@tonic-gate /* FALLTHRU */
9297c478bd9Sstevel@tonic-gate default:
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate * If SO_LINGER has set a zero linger time, abort the
9327c478bd9Sstevel@tonic-gate * connection with a reset.
9337c478bd9Sstevel@tonic-gate */
9347c478bd9Sstevel@tonic-gate if (tcp->tcp_linger && tcp->tcp_lingertime == 0) {
9357c478bd9Sstevel@tonic-gate msg = "tcp_close, zero lingertime";
9367c478bd9Sstevel@tonic-gate break;
9377c478bd9Sstevel@tonic-gate }
9387c478bd9Sstevel@tonic-gate
9397c478bd9Sstevel@tonic-gate /*
9407c478bd9Sstevel@tonic-gate * Abort connection if there is unread data queued.
9417c478bd9Sstevel@tonic-gate */
9427c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL ||
9437c478bd9Sstevel@tonic-gate tcp->tcp_reass_head != NULL) {
9447c478bd9Sstevel@tonic-gate msg = "tcp_close, unread data";
9457c478bd9Sstevel@tonic-gate break;
9467c478bd9Sstevel@tonic-gate }
9477c478bd9Sstevel@tonic-gate if (tcp->tcp_state <= TCPS_LISTEN)
9487c478bd9Sstevel@tonic-gate break;
9497c478bd9Sstevel@tonic-gate
9507c478bd9Sstevel@tonic-gate /*
9517c478bd9Sstevel@tonic-gate * Transmit the FIN before detaching the tcp_t.
9527c478bd9Sstevel@tonic-gate * After tcp_detach returns this queue/perimeter
9537c478bd9Sstevel@tonic-gate * no longer owns the tcp_t thus others can modify it.
9547c478bd9Sstevel@tonic-gate * The TCP could be closed in tcp_state_wait called by
9557c478bd9Sstevel@tonic-gate * tcp_wput_data called by tcp_xmit_end.
9567c478bd9Sstevel@tonic-gate */
9577c478bd9Sstevel@tonic-gate (void) tcp_xmit_end(tcp, sock_id);
9587c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
9597c478bd9Sstevel@tonic-gate return (0);
9607c478bd9Sstevel@tonic-gate
9617c478bd9Sstevel@tonic-gate /*
9627c478bd9Sstevel@tonic-gate * If lingering on close then wait until the fin is acked,
9637c478bd9Sstevel@tonic-gate * the SO_LINGER time passes, or a reset is sent/received.
9647c478bd9Sstevel@tonic-gate */
9657c478bd9Sstevel@tonic-gate if (tcp->tcp_linger && tcp->tcp_lingertime > 0 &&
9667c478bd9Sstevel@tonic-gate !(tcp->tcp_fin_acked) &&
9677c478bd9Sstevel@tonic-gate tcp->tcp_state >= TCPS_ESTABLISHED) {
9687c478bd9Sstevel@tonic-gate uint32_t stoptime; /* in ms */
9697c478bd9Sstevel@tonic-gate
9707c478bd9Sstevel@tonic-gate tcp->tcp_client_errno = 0;
9717c478bd9Sstevel@tonic-gate stoptime = prom_gettime() +
9727c478bd9Sstevel@tonic-gate (tcp->tcp_lingertime * 1000);
9737c478bd9Sstevel@tonic-gate while (!(tcp->tcp_fin_acked) &&
9747c478bd9Sstevel@tonic-gate tcp->tcp_state >= TCPS_ESTABLISHED &&
9757c478bd9Sstevel@tonic-gate tcp->tcp_client_errno == 0 &&
9767c478bd9Sstevel@tonic-gate ((int32_t)(stoptime - prom_gettime()) > 0)) {
9777c478bd9Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
9787c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL) {
9797c478bd9Sstevel@tonic-gate tcp_clean_death(sock_id,
9807c478bd9Sstevel@tonic-gate tcp, 0);
9817c478bd9Sstevel@tonic-gate }
9827c478bd9Sstevel@tonic-gate return (-1);
9837c478bd9Sstevel@tonic-gate }
9847c478bd9Sstevel@tonic-gate }
9857c478bd9Sstevel@tonic-gate tcp->tcp_client_errno = 0;
9867c478bd9Sstevel@tonic-gate }
9877c478bd9Sstevel@tonic-gate if (tcp_state_wait(sock_id, tcp, TCPS_TIME_WAIT) < 0) {
9887c478bd9Sstevel@tonic-gate /* During the wait, TCP may be gone... */
9897c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
9907c478bd9Sstevel@tonic-gate return (0);
9917c478bd9Sstevel@tonic-gate msg = "tcp_close, couldn't detach";
9927c478bd9Sstevel@tonic-gate } else {
9937c478bd9Sstevel@tonic-gate return (0);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate break;
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate /* Something went wrong... Send a RST and report the error */
9997c478bd9Sstevel@tonic-gate if (msg != NULL) {
10007c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_ESTABLISHED ||
10017c478bd9Sstevel@tonic-gate tcp->tcp_state == TCPS_CLOSE_WAIT)
10027c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpEstabResets);
10037c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_SENT ||
10047c478bd9Sstevel@tonic-gate tcp->tcp_state == TCPS_SYN_RCVD)
10057c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpAttemptFails);
10067c478bd9Sstevel@tonic-gate tcp_xmit_ctl(msg, tcp, NULL, tcp->tcp_snxt, 0, TH_RST, 0,
10077c478bd9Sstevel@tonic-gate sock_id);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate
10107c478bd9Sstevel@tonic-gate tcp_free(tcp);
10117c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
10127c478bd9Sstevel@tonic-gate sockets[sock_id].pcb = NULL;
10137c478bd9Sstevel@tonic-gate return (error);
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate
10167c478bd9Sstevel@tonic-gate /* To make an endpoint a listener. */
10177c478bd9Sstevel@tonic-gate int
tcp_listen(int sock_id,int backlog)10187c478bd9Sstevel@tonic-gate tcp_listen(int sock_id, int backlog)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate tcp_t *tcp;
10217c478bd9Sstevel@tonic-gate
10227c478bd9Sstevel@tonic-gate if ((tcp = (tcp_t *)(sockets[sock_id].pcb)) == NULL) {
10237c478bd9Sstevel@tonic-gate errno = EINVAL;
10247c478bd9Sstevel@tonic-gate return (-1);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate /* We allow calling listen() multiple times to change the backlog. */
10277c478bd9Sstevel@tonic-gate if (tcp->tcp_state > TCPS_LISTEN || tcp->tcp_state < TCPS_BOUND) {
10287c478bd9Sstevel@tonic-gate errno = EOPNOTSUPP;
10297c478bd9Sstevel@tonic-gate return (-1);
10307c478bd9Sstevel@tonic-gate }
10317c478bd9Sstevel@tonic-gate /* The following initialization should only be done once. */
10327c478bd9Sstevel@tonic-gate if (tcp->tcp_state != TCPS_LISTEN) {
10337c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
10347c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
10357c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_LISTEN;
10367c478bd9Sstevel@tonic-gate tcp->tcp_second_ctimer_threshold = tcp_ip_abort_linterval;
10377c478bd9Sstevel@tonic-gate }
10387c478bd9Sstevel@tonic-gate if ((tcp->tcp_conn_req_max = backlog) > tcp_conn_req_max_q) {
10397c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_max = tcp_conn_req_max_q;
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_max < tcp_conn_req_min) {
10427c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_max = tcp_conn_req_min;
10437c478bd9Sstevel@tonic-gate }
10447c478bd9Sstevel@tonic-gate return (0);
10457c478bd9Sstevel@tonic-gate }
10467c478bd9Sstevel@tonic-gate
10477c478bd9Sstevel@tonic-gate /* To accept connections. */
10487c478bd9Sstevel@tonic-gate int
tcp_accept(int sock_id,struct sockaddr * addr,socklen_t * addr_len)10497c478bd9Sstevel@tonic-gate tcp_accept(int sock_id, struct sockaddr *addr, socklen_t *addr_len)
10507c478bd9Sstevel@tonic-gate {
10517c478bd9Sstevel@tonic-gate tcp_t *listener;
10527c478bd9Sstevel@tonic-gate tcp_t *eager;
10537c478bd9Sstevel@tonic-gate int sd, new_sock_id;
10547c478bd9Sstevel@tonic-gate struct sockaddr_in *new_addr = (struct sockaddr_in *)addr;
10557c478bd9Sstevel@tonic-gate int timeout;
10567c478bd9Sstevel@tonic-gate
10577c478bd9Sstevel@tonic-gate /* Sanity check. */
10587c478bd9Sstevel@tonic-gate if ((listener = (tcp_t *)(sockets[sock_id].pcb)) == NULL ||
10597c478bd9Sstevel@tonic-gate new_addr == NULL || addr_len == NULL ||
10607c478bd9Sstevel@tonic-gate *addr_len < sizeof (struct sockaddr_in) ||
10617c478bd9Sstevel@tonic-gate listener->tcp_state != TCPS_LISTEN) {
10627c478bd9Sstevel@tonic-gate errno = EINVAL;
10637c478bd9Sstevel@tonic-gate return (-1);
10647c478bd9Sstevel@tonic-gate }
10657c478bd9Sstevel@tonic-gate
10667c478bd9Sstevel@tonic-gate if (sockets[sock_id].in_timeout > tcp_accept_timeout)
10677c478bd9Sstevel@tonic-gate timeout = prom_gettime() + sockets[sock_id].in_timeout;
10687c478bd9Sstevel@tonic-gate else
10697c478bd9Sstevel@tonic-gate timeout = prom_gettime() + tcp_accept_timeout;
10707c478bd9Sstevel@tonic-gate while (listener->tcp_eager_next_q == NULL &&
10717c478bd9Sstevel@tonic-gate timeout > prom_gettime()) {
10727c478bd9Sstevel@tonic-gate #if DEBUG
10737c478bd9Sstevel@tonic-gate printf("tcp_accept: Waiting in tcp_accept()\n");
10747c478bd9Sstevel@tonic-gate #endif
10757c478bd9Sstevel@tonic-gate if (tcp_drain_input(listener, sock_id, 5) < 0) {
10767c478bd9Sstevel@tonic-gate return (-1);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate }
10797c478bd9Sstevel@tonic-gate /* If there is an eager, don't timeout... */
10807c478bd9Sstevel@tonic-gate if (timeout <= prom_gettime() && listener->tcp_eager_next_q == NULL) {
10817c478bd9Sstevel@tonic-gate #if DEBUG
10827c478bd9Sstevel@tonic-gate printf("tcp_accept: timeout\n");
10837c478bd9Sstevel@tonic-gate #endif
10847c478bd9Sstevel@tonic-gate errno = ETIMEDOUT;
10857c478bd9Sstevel@tonic-gate return (-1);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate #if DEBUG
10887c478bd9Sstevel@tonic-gate printf("tcp_accept: got a connection\n");
10897c478bd9Sstevel@tonic-gate #endif
10907c478bd9Sstevel@tonic-gate
10917c478bd9Sstevel@tonic-gate /* Now create the socket for this new TCP. */
10927c478bd9Sstevel@tonic-gate if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
10937c478bd9Sstevel@tonic-gate return (-1);
10947c478bd9Sstevel@tonic-gate }
10957c478bd9Sstevel@tonic-gate if ((new_sock_id = so_check_fd(sd, &errno)) == -1)
10967c478bd9Sstevel@tonic-gate /* This should not happen! */
10977c478bd9Sstevel@tonic-gate prom_panic("so_check_fd() fails in tcp_accept()");
10987c478bd9Sstevel@tonic-gate /* Free the TCP PCB in the original socket. */
10997c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)(sockets[new_sock_id].pcb), sizeof (tcp_t));
11007c478bd9Sstevel@tonic-gate /* Dequeue the eager and attach it to the socket. */
11017c478bd9Sstevel@tonic-gate eager = listener->tcp_eager_next_q;
11027c478bd9Sstevel@tonic-gate listener->tcp_eager_next_q = eager->tcp_eager_next_q;
11037c478bd9Sstevel@tonic-gate if (listener->tcp_eager_last_q == eager)
11047c478bd9Sstevel@tonic-gate listener->tcp_eager_last_q = NULL;
11057c478bd9Sstevel@tonic-gate eager->tcp_eager_next_q = NULL;
11067c478bd9Sstevel@tonic-gate sockets[new_sock_id].pcb = eager;
11077c478bd9Sstevel@tonic-gate listener->tcp_conn_req_cnt_q--;
11087c478bd9Sstevel@tonic-gate
11097c478bd9Sstevel@tonic-gate /* Copy in the address info. */
11107c478bd9Sstevel@tonic-gate bcopy(&eager->tcp_remote, &new_addr->sin_addr.s_addr,
11117c478bd9Sstevel@tonic-gate sizeof (in_addr_t));
11127c478bd9Sstevel@tonic-gate bcopy(&eager->tcp_fport, &new_addr->sin_port, sizeof (in_port_t));
11137c478bd9Sstevel@tonic-gate new_addr->sin_family = AF_INET;
11147c478bd9Sstevel@tonic-gate
11157c478bd9Sstevel@tonic-gate #ifdef DEBUG
11167c478bd9Sstevel@tonic-gate printf("tcp_accept(), new sock_id: %d\n", sd);
11177c478bd9Sstevel@tonic-gate #endif
11187c478bd9Sstevel@tonic-gate return (sd);
11197c478bd9Sstevel@tonic-gate }
11207c478bd9Sstevel@tonic-gate
11217c478bd9Sstevel@tonic-gate /* Update the next anonymous port to use. */
11227c478bd9Sstevel@tonic-gate static in_port_t
tcp_update_next_port(in_port_t port)11237c478bd9Sstevel@tonic-gate tcp_update_next_port(in_port_t port)
11247c478bd9Sstevel@tonic-gate {
11257c478bd9Sstevel@tonic-gate /* Don't allow the port to fall out of the anonymous port range. */
11267c478bd9Sstevel@tonic-gate if (port < tcp_smallest_anon_port || port > tcp_largest_anon_port)
11277c478bd9Sstevel@tonic-gate port = (in_port_t)tcp_smallest_anon_port;
11287c478bd9Sstevel@tonic-gate
11297c478bd9Sstevel@tonic-gate if (port < tcp_smallest_nonpriv_port)
11307c478bd9Sstevel@tonic-gate port = (in_port_t)tcp_smallest_nonpriv_port;
11317c478bd9Sstevel@tonic-gate return (port);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate
11347c478bd9Sstevel@tonic-gate /* To check whether a bind to a port is allowed. */
11357c478bd9Sstevel@tonic-gate static in_port_t
tcp_bindi(in_port_t port,in_addr_t * addr,boolean_t reuseaddr,boolean_t bind_to_req_port_only)11367c478bd9Sstevel@tonic-gate tcp_bindi(in_port_t port, in_addr_t *addr, boolean_t reuseaddr,
11377c478bd9Sstevel@tonic-gate boolean_t bind_to_req_port_only)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate int i, count;
11407c478bd9Sstevel@tonic-gate tcp_t *tcp;
11417c478bd9Sstevel@tonic-gate
11427c478bd9Sstevel@tonic-gate count = tcp_largest_anon_port - tcp_smallest_anon_port;
11437c478bd9Sstevel@tonic-gate try_again:
11447c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
11457c478bd9Sstevel@tonic-gate if (sockets[i].type != INETBOOT_STREAM ||
11467c478bd9Sstevel@tonic-gate ((tcp = (tcp_t *)sockets[i].pcb) == NULL) ||
11477c478bd9Sstevel@tonic-gate ntohs(tcp->tcp_lport) != port) {
11487c478bd9Sstevel@tonic-gate continue;
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate * Both TCPs have the same port. If SO_REUSEDADDR is
11527c478bd9Sstevel@tonic-gate * set and the bound TCP has a state greater than
11537c478bd9Sstevel@tonic-gate * TCPS_LISTEN, it is fine.
11547c478bd9Sstevel@tonic-gate */
11557c478bd9Sstevel@tonic-gate if (reuseaddr && tcp->tcp_state > TCPS_LISTEN) {
11567c478bd9Sstevel@tonic-gate continue;
11577c478bd9Sstevel@tonic-gate }
11587c478bd9Sstevel@tonic-gate if (tcp->tcp_bound_source != INADDR_ANY &&
11597c478bd9Sstevel@tonic-gate *addr != INADDR_ANY &&
11607c478bd9Sstevel@tonic-gate tcp->tcp_bound_source != *addr) {
11617c478bd9Sstevel@tonic-gate continue;
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate if (bind_to_req_port_only) {
11647c478bd9Sstevel@tonic-gate return (0);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate if (--count > 0) {
11677c478bd9Sstevel@tonic-gate port = tcp_update_next_port(++port);
11687c478bd9Sstevel@tonic-gate goto try_again;
11697c478bd9Sstevel@tonic-gate } else {
11707c478bd9Sstevel@tonic-gate return (0);
11717c478bd9Sstevel@tonic-gate }
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate return (port);
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate
11767c478bd9Sstevel@tonic-gate /* To handle the bind request. */
11777c478bd9Sstevel@tonic-gate int
tcp_bind(int sock_id)11787c478bd9Sstevel@tonic-gate tcp_bind(int sock_id)
11797c478bd9Sstevel@tonic-gate {
11807c478bd9Sstevel@tonic-gate tcp_t *tcp;
11817c478bd9Sstevel@tonic-gate in_port_t requested_port, allocated_port;
11827c478bd9Sstevel@tonic-gate boolean_t bind_to_req_port_only;
11837c478bd9Sstevel@tonic-gate boolean_t reuseaddr;
11847c478bd9Sstevel@tonic-gate
11857c478bd9Sstevel@tonic-gate if ((tcp = (tcp_t *)sockets[sock_id].pcb) == NULL) {
11867c478bd9Sstevel@tonic-gate errno = EINVAL;
11877c478bd9Sstevel@tonic-gate return (-1);
11887c478bd9Sstevel@tonic-gate }
11897c478bd9Sstevel@tonic-gate
11907c478bd9Sstevel@tonic-gate if (tcp->tcp_state >= TCPS_BOUND) {
11917c478bd9Sstevel@tonic-gate /* We don't allow multiple bind(). */
11927c478bd9Sstevel@tonic-gate errno = EPROTO;
11937c478bd9Sstevel@tonic-gate return (-1);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate
11967c478bd9Sstevel@tonic-gate requested_port = ntohs(sockets[sock_id].bind.sin_port);
11977c478bd9Sstevel@tonic-gate
11987c478bd9Sstevel@tonic-gate /* The bound source can be INADDR_ANY. */
11997c478bd9Sstevel@tonic-gate tcp->tcp_bound_source = sockets[sock_id].bind.sin_addr.s_addr;
12007c478bd9Sstevel@tonic-gate
12017c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_src.s_addr = tcp->tcp_bound_source;
12027c478bd9Sstevel@tonic-gate
12037c478bd9Sstevel@tonic-gate /* Verify the port is available. */
12047c478bd9Sstevel@tonic-gate if (requested_port == 0)
12057c478bd9Sstevel@tonic-gate bind_to_req_port_only = B_FALSE;
12067c478bd9Sstevel@tonic-gate else /* T_BIND_REQ and requested_port != 0 */
12077c478bd9Sstevel@tonic-gate bind_to_req_port_only = B_TRUE;
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate if (requested_port == 0) {
12107c478bd9Sstevel@tonic-gate requested_port = tcp_update_next_port(++tcp_next_port_to_try);
12117c478bd9Sstevel@tonic-gate }
12127c478bd9Sstevel@tonic-gate reuseaddr = sockets[sock_id].so_opt & SO_REUSEADDR;
12137c478bd9Sstevel@tonic-gate allocated_port = tcp_bindi(requested_port, &(tcp->tcp_bound_source),
12147c478bd9Sstevel@tonic-gate reuseaddr, bind_to_req_port_only);
12157c478bd9Sstevel@tonic-gate
12167c478bd9Sstevel@tonic-gate if (allocated_port == 0) {
12177c478bd9Sstevel@tonic-gate errno = EADDRINUSE;
12187c478bd9Sstevel@tonic-gate return (-1);
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate tcp->tcp_lport = htons(allocated_port);
12217c478bd9Sstevel@tonic-gate *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport;
12227c478bd9Sstevel@tonic-gate sockets[sock_id].bind.sin_port = tcp->tcp_lport;
12237c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_BOUND;
12247c478bd9Sstevel@tonic-gate return (0);
12257c478bd9Sstevel@tonic-gate }
12267c478bd9Sstevel@tonic-gate
12277c478bd9Sstevel@tonic-gate /*
12287c478bd9Sstevel@tonic-gate * Check for duplicate TCP connections.
12297c478bd9Sstevel@tonic-gate */
12307c478bd9Sstevel@tonic-gate static int
tcp_conn_check(tcp_t * tcp)12317c478bd9Sstevel@tonic-gate tcp_conn_check(tcp_t *tcp)
12327c478bd9Sstevel@tonic-gate {
12337c478bd9Sstevel@tonic-gate int i;
12347c478bd9Sstevel@tonic-gate tcp_t *tmp_tcp;
12357c478bd9Sstevel@tonic-gate
12367c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
12377c478bd9Sstevel@tonic-gate if (sockets[i].type != INETBOOT_STREAM)
12387c478bd9Sstevel@tonic-gate continue;
12397c478bd9Sstevel@tonic-gate /* Socket may not be closed but the TCP can be gone. */
12407c478bd9Sstevel@tonic-gate if ((tmp_tcp = (tcp_t *)sockets[i].pcb) == NULL)
12417c478bd9Sstevel@tonic-gate continue;
12427c478bd9Sstevel@tonic-gate /* We only care about TCP in states later than SYN_SENT. */
12437c478bd9Sstevel@tonic-gate if (tmp_tcp->tcp_state < TCPS_SYN_SENT)
12447c478bd9Sstevel@tonic-gate continue;
12457c478bd9Sstevel@tonic-gate if (tmp_tcp->tcp_lport != tcp->tcp_lport ||
12467c478bd9Sstevel@tonic-gate tmp_tcp->tcp_fport != tcp->tcp_fport ||
12477c478bd9Sstevel@tonic-gate tmp_tcp->tcp_bound_source != tcp->tcp_bound_source ||
12487c478bd9Sstevel@tonic-gate tmp_tcp->tcp_remote != tcp->tcp_remote) {
12497c478bd9Sstevel@tonic-gate continue;
12507c478bd9Sstevel@tonic-gate } else {
12517c478bd9Sstevel@tonic-gate return (-1);
12527c478bd9Sstevel@tonic-gate }
12537c478bd9Sstevel@tonic-gate }
12547c478bd9Sstevel@tonic-gate return (0);
12557c478bd9Sstevel@tonic-gate }
12567c478bd9Sstevel@tonic-gate
12577c478bd9Sstevel@tonic-gate /* To handle a connect request. */
12587c478bd9Sstevel@tonic-gate int
tcp_connect(int sock_id)12597c478bd9Sstevel@tonic-gate tcp_connect(int sock_id)
12607c478bd9Sstevel@tonic-gate {
12617c478bd9Sstevel@tonic-gate tcp_t *tcp;
12627c478bd9Sstevel@tonic-gate in_addr_t dstaddr;
12637c478bd9Sstevel@tonic-gate in_port_t dstport;
12647c478bd9Sstevel@tonic-gate tcph_t *tcph;
12657c478bd9Sstevel@tonic-gate int mss;
12667c478bd9Sstevel@tonic-gate mblk_t *syn_mp;
12677c478bd9Sstevel@tonic-gate
12687c478bd9Sstevel@tonic-gate if ((tcp = (tcp_t *)(sockets[sock_id].pcb)) == NULL) {
12697c478bd9Sstevel@tonic-gate errno = EINVAL;
12707c478bd9Sstevel@tonic-gate return (-1);
12717c478bd9Sstevel@tonic-gate }
12727c478bd9Sstevel@tonic-gate
12737c478bd9Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
12747c478bd9Sstevel@tonic-gate
12757c478bd9Sstevel@tonic-gate dstaddr = sockets[sock_id].remote.sin_addr.s_addr;
12767c478bd9Sstevel@tonic-gate dstport = sockets[sock_id].remote.sin_port;
12777c478bd9Sstevel@tonic-gate
12787c478bd9Sstevel@tonic-gate /*
12797c478bd9Sstevel@tonic-gate * Check for attempt to connect to INADDR_ANY or non-unicast addrress.
12807c478bd9Sstevel@tonic-gate * We don't have enough info to check for broadcast addr, except
12817c478bd9Sstevel@tonic-gate * for the all 1 broadcast.
12827c478bd9Sstevel@tonic-gate */
12837c478bd9Sstevel@tonic-gate if (dstaddr == INADDR_ANY || IN_CLASSD(ntohl(dstaddr)) ||
12847c478bd9Sstevel@tonic-gate dstaddr == INADDR_BROADCAST) {
12857c478bd9Sstevel@tonic-gate /*
12867c478bd9Sstevel@tonic-gate * SunOS 4.x and 4.3 BSD allow an application
12877c478bd9Sstevel@tonic-gate * to connect a TCP socket to INADDR_ANY.
12887c478bd9Sstevel@tonic-gate * When they do this, the kernel picks the
12897c478bd9Sstevel@tonic-gate * address of one interface and uses it
12907c478bd9Sstevel@tonic-gate * instead. The kernel usually ends up
12917c478bd9Sstevel@tonic-gate * picking the address of the loopback
12927c478bd9Sstevel@tonic-gate * interface. This is an undocumented feature.
12937c478bd9Sstevel@tonic-gate * However, we provide the same thing here
12947c478bd9Sstevel@tonic-gate * in order to have source and binary
12957c478bd9Sstevel@tonic-gate * compatibility with SunOS 4.x.
12967c478bd9Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to
12977c478bd9Sstevel@tonic-gate * generate the T_CONN_CON.
12987c478bd9Sstevel@tonic-gate *
12997c478bd9Sstevel@tonic-gate * Fail this for inetboot TCP.
13007c478bd9Sstevel@tonic-gate */
13017c478bd9Sstevel@tonic-gate errno = EINVAL;
13027c478bd9Sstevel@tonic-gate return (-1);
13037c478bd9Sstevel@tonic-gate }
13047c478bd9Sstevel@tonic-gate
13057c478bd9Sstevel@tonic-gate /* It is not bound to any address yet... */
13067c478bd9Sstevel@tonic-gate if (tcp->tcp_bound_source == INADDR_ANY) {
13077c478bd9Sstevel@tonic-gate ipv4_getipaddr(&(sockets[sock_id].bind.sin_addr));
13087c478bd9Sstevel@tonic-gate /* We don't have an address! */
13097c478bd9Sstevel@tonic-gate if (ntohl(sockets[sock_id].bind.sin_addr.s_addr) ==
13107c478bd9Sstevel@tonic-gate INADDR_ANY) {
13117c478bd9Sstevel@tonic-gate errno = EPROTO;
13127c478bd9Sstevel@tonic-gate return (-1);
13137c478bd9Sstevel@tonic-gate }
13147c478bd9Sstevel@tonic-gate tcp->tcp_bound_source = sockets[sock_id].bind.sin_addr.s_addr;
13157c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_src.s_addr = tcp->tcp_bound_source;
13167c478bd9Sstevel@tonic-gate }
13177c478bd9Sstevel@tonic-gate
13187c478bd9Sstevel@tonic-gate /*
13197c478bd9Sstevel@tonic-gate * Don't let an endpoint connect to itself.
13207c478bd9Sstevel@tonic-gate */
13217c478bd9Sstevel@tonic-gate if (dstaddr == tcp->tcp_ipha->ip_src.s_addr &&
13227c478bd9Sstevel@tonic-gate dstport == tcp->tcp_lport) {
13237c478bd9Sstevel@tonic-gate errno = EINVAL;
13247c478bd9Sstevel@tonic-gate return (-1);
13257c478bd9Sstevel@tonic-gate }
13267c478bd9Sstevel@tonic-gate
13277c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_dst.s_addr = dstaddr;
13287c478bd9Sstevel@tonic-gate tcp->tcp_remote = dstaddr;
13297c478bd9Sstevel@tonic-gate tcph = tcp->tcp_tcph;
13307c478bd9Sstevel@tonic-gate *(uint16_t *)tcph->th_fport = dstport;
13317c478bd9Sstevel@tonic-gate tcp->tcp_fport = dstport;
13327c478bd9Sstevel@tonic-gate
13337c478bd9Sstevel@tonic-gate /*
13347c478bd9Sstevel@tonic-gate * Don't allow this connection to completely duplicate
13357c478bd9Sstevel@tonic-gate * an existing connection.
13367c478bd9Sstevel@tonic-gate */
13377c478bd9Sstevel@tonic-gate if (tcp_conn_check(tcp) < 0) {
13387c478bd9Sstevel@tonic-gate errno = EADDRINUSE;
13397c478bd9Sstevel@tonic-gate return (-1);
13407c478bd9Sstevel@tonic-gate }
13417c478bd9Sstevel@tonic-gate
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate * Just make sure our rwnd is at
13447c478bd9Sstevel@tonic-gate * least tcp_recv_hiwat_mss * MSS
13457c478bd9Sstevel@tonic-gate * large, and round up to the nearest
13467c478bd9Sstevel@tonic-gate * MSS.
13477c478bd9Sstevel@tonic-gate *
13487c478bd9Sstevel@tonic-gate * We do the round up here because
13497c478bd9Sstevel@tonic-gate * we need to get the interface
13507c478bd9Sstevel@tonic-gate * MTU first before we can do the
13517c478bd9Sstevel@tonic-gate * round up.
13527c478bd9Sstevel@tonic-gate */
13537c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss - tcp->tcp_hdr_len;
13547c478bd9Sstevel@tonic-gate tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
13557c478bd9Sstevel@tonic-gate tcp_recv_hiwat_minmss * mss);
13567c478bd9Sstevel@tonic-gate tcp->tcp_rwnd_max = tcp->tcp_rwnd;
13577c478bd9Sstevel@tonic-gate SET_WS_VALUE(tcp);
13587c478bd9Sstevel@tonic-gate U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws),
13597c478bd9Sstevel@tonic-gate tcp->tcp_tcph->th_win);
13607c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always)
13617c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_TRUE;
13627c478bd9Sstevel@tonic-gate
13637c478bd9Sstevel@tonic-gate /*
13647c478bd9Sstevel@tonic-gate * Set tcp_snd_ts_ok to true
13657c478bd9Sstevel@tonic-gate * so that tcp_xmit_mp will
13667c478bd9Sstevel@tonic-gate * include the timestamp
13677c478bd9Sstevel@tonic-gate * option in the SYN segment.
13687c478bd9Sstevel@tonic-gate */
13697c478bd9Sstevel@tonic-gate if (tcp_tstamp_always ||
13707c478bd9Sstevel@tonic-gate (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) {
13717c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_TRUE;
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate
13747c478bd9Sstevel@tonic-gate if (tcp_sack_permitted == 2 ||
13757c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok) {
13767c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info == NULL);
13777c478bd9Sstevel@tonic-gate if ((tcp->tcp_sack_info = (tcp_sack_info_t *)bkmem_zalloc(
13787c478bd9Sstevel@tonic-gate sizeof (tcp_sack_info_t))) == NULL) {
13797c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
13807c478bd9Sstevel@tonic-gate } else {
13817c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
13827c478bd9Sstevel@tonic-gate }
13837c478bd9Sstevel@tonic-gate }
13847c478bd9Sstevel@tonic-gate /*
13857c478bd9Sstevel@tonic-gate * Should we use ECN? Note that the current
13867c478bd9Sstevel@tonic-gate * default value (SunOS 5.9) of tcp_ecn_permitted
13877c478bd9Sstevel@tonic-gate * is 2. The reason for doing this is that there
13887c478bd9Sstevel@tonic-gate * are equipments out there that will drop ECN
13897c478bd9Sstevel@tonic-gate * enabled IP packets. Setting it to 1 avoids
13907c478bd9Sstevel@tonic-gate * compatibility problems.
13917c478bd9Sstevel@tonic-gate */
13927c478bd9Sstevel@tonic-gate if (tcp_ecn_permitted == 2)
13937c478bd9Sstevel@tonic-gate tcp->tcp_ecn_ok = B_TRUE;
13947c478bd9Sstevel@tonic-gate
13957c478bd9Sstevel@tonic-gate tcp_iss_init(tcp);
13967c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
13977c478bd9Sstevel@tonic-gate tcp->tcp_active_open = B_TRUE;
13987c478bd9Sstevel@tonic-gate
13997c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_SYN_SENT;
14007c478bd9Sstevel@tonic-gate syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, tcp->tcp_iss, B_FALSE,
14017c478bd9Sstevel@tonic-gate NULL, B_FALSE);
14027c478bd9Sstevel@tonic-gate if (syn_mp != NULL) {
14037c478bd9Sstevel@tonic-gate int ret;
14047c478bd9Sstevel@tonic-gate
14057c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
14067c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_connect", syn_mp);
14077c478bd9Sstevel@tonic-gate /* Send out the SYN packet. */
14087c478bd9Sstevel@tonic-gate ret = ipv4_tcp_output(sock_id, syn_mp);
14097c478bd9Sstevel@tonic-gate freeb(syn_mp);
14109f1fc992Sss146032 /*
14119f1fc992Sss146032 * errno ETIMEDOUT is set by the mac driver
14129f1fc992Sss146032 * in case it is not able to receive ARP reply.
14139f1fc992Sss146032 * TCP will retransmit this segment so we can
14149f1fc992Sss146032 * ignore the ARP timeout.
14159f1fc992Sss146032 */
14169f1fc992Sss146032 if ((ret < 0) && (errno != ETIMEDOUT)) {
14177c478bd9Sstevel@tonic-gate return (-1);
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate /* tcp_state_wait() will finish the 3 way handshake. */
14207c478bd9Sstevel@tonic-gate return (tcp_state_wait(sock_id, tcp, TCPS_ESTABLISHED));
14217c478bd9Sstevel@tonic-gate } else {
14227c478bd9Sstevel@tonic-gate errno = ENOBUFS;
14237c478bd9Sstevel@tonic-gate return (-1);
14247c478bd9Sstevel@tonic-gate }
14257c478bd9Sstevel@tonic-gate }
14267c478bd9Sstevel@tonic-gate
14277c478bd9Sstevel@tonic-gate /*
14287c478bd9Sstevel@tonic-gate * Common accept code. Called by tcp_conn_request.
14297c478bd9Sstevel@tonic-gate * cr_pkt is the SYN packet.
14307c478bd9Sstevel@tonic-gate */
14317c478bd9Sstevel@tonic-gate static int
tcp_accept_comm(tcp_t * listener,tcp_t * acceptor,mblk_t * cr_pkt,uint_t ip_hdr_len)14327c478bd9Sstevel@tonic-gate tcp_accept_comm(tcp_t *listener, tcp_t *acceptor, mblk_t *cr_pkt,
14337c478bd9Sstevel@tonic-gate uint_t ip_hdr_len)
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate tcph_t *tcph;
14367c478bd9Sstevel@tonic-gate
14377c478bd9Sstevel@tonic-gate #ifdef DEBUG
14387c478bd9Sstevel@tonic-gate printf("tcp_accept_comm #######################\n");
14397c478bd9Sstevel@tonic-gate #endif
14407c478bd9Sstevel@tonic-gate
14417c478bd9Sstevel@tonic-gate /*
14427c478bd9Sstevel@tonic-gate * When we get here, we know that the acceptor header template
14437c478bd9Sstevel@tonic-gate * has already been initialized.
14447c478bd9Sstevel@tonic-gate * However, it may not match the listener if the listener
14457c478bd9Sstevel@tonic-gate * includes options...
14467c478bd9Sstevel@tonic-gate * It may also not match the listener if the listener is v6 and
14477c478bd9Sstevel@tonic-gate * and the acceptor is v4
14487c478bd9Sstevel@tonic-gate */
14497c478bd9Sstevel@tonic-gate acceptor->tcp_lport = listener->tcp_lport;
14507c478bd9Sstevel@tonic-gate
14517c478bd9Sstevel@tonic-gate if (listener->tcp_ipversion == acceptor->tcp_ipversion) {
14527c478bd9Sstevel@tonic-gate if (acceptor->tcp_iphc_len != listener->tcp_iphc_len) {
14537c478bd9Sstevel@tonic-gate /*
14547c478bd9Sstevel@tonic-gate * Listener had options of some sort; acceptor inherits.
14557c478bd9Sstevel@tonic-gate * Free up the acceptor template and allocate one
14567c478bd9Sstevel@tonic-gate * of the right size.
14577c478bd9Sstevel@tonic-gate */
14587c478bd9Sstevel@tonic-gate bkmem_free(acceptor->tcp_iphc, acceptor->tcp_iphc_len);
14597c478bd9Sstevel@tonic-gate acceptor->tcp_iphc = bkmem_zalloc(
14607c478bd9Sstevel@tonic-gate listener->tcp_iphc_len);
14617c478bd9Sstevel@tonic-gate if (acceptor->tcp_iphc == NULL) {
14627c478bd9Sstevel@tonic-gate acceptor->tcp_iphc_len = 0;
14637c478bd9Sstevel@tonic-gate return (ENOMEM);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate acceptor->tcp_iphc_len = listener->tcp_iphc_len;
14667c478bd9Sstevel@tonic-gate }
14677c478bd9Sstevel@tonic-gate acceptor->tcp_hdr_len = listener->tcp_hdr_len;
14687c478bd9Sstevel@tonic-gate acceptor->tcp_ip_hdr_len = listener->tcp_ip_hdr_len;
14697c478bd9Sstevel@tonic-gate acceptor->tcp_tcp_hdr_len = listener->tcp_tcp_hdr_len;
14707c478bd9Sstevel@tonic-gate
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate * Copy the IP+TCP header template from listener to acceptor
14737c478bd9Sstevel@tonic-gate */
14747c478bd9Sstevel@tonic-gate bcopy(listener->tcp_iphc, acceptor->tcp_iphc,
14757c478bd9Sstevel@tonic-gate listener->tcp_hdr_len);
14767c478bd9Sstevel@tonic-gate acceptor->tcp_ipha = (struct ip *)acceptor->tcp_iphc;
14777c478bd9Sstevel@tonic-gate acceptor->tcp_tcph = (tcph_t *)(acceptor->tcp_iphc +
14787c478bd9Sstevel@tonic-gate acceptor->tcp_ip_hdr_len);
14797c478bd9Sstevel@tonic-gate } else {
14807c478bd9Sstevel@tonic-gate prom_panic("tcp_accept_comm: version not equal");
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate
14837c478bd9Sstevel@tonic-gate /* Copy our new dest and fport from the connection request packet */
14847c478bd9Sstevel@tonic-gate if (acceptor->tcp_ipversion == IPV4_VERSION) {
14857c478bd9Sstevel@tonic-gate struct ip *ipha;
14867c478bd9Sstevel@tonic-gate
14877c478bd9Sstevel@tonic-gate ipha = (struct ip *)cr_pkt->b_rptr;
14887c478bd9Sstevel@tonic-gate acceptor->tcp_ipha->ip_dst = ipha->ip_src;
14897c478bd9Sstevel@tonic-gate acceptor->tcp_remote = ipha->ip_src.s_addr;
14907c478bd9Sstevel@tonic-gate acceptor->tcp_ipha->ip_src = ipha->ip_dst;
14917c478bd9Sstevel@tonic-gate acceptor->tcp_bound_source = ipha->ip_dst.s_addr;
14927c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&cr_pkt->b_rptr[ip_hdr_len];
14937c478bd9Sstevel@tonic-gate } else {
14947c478bd9Sstevel@tonic-gate prom_panic("tcp_accept_comm: not IPv4");
14957c478bd9Sstevel@tonic-gate }
14967c478bd9Sstevel@tonic-gate bcopy(tcph->th_lport, acceptor->tcp_tcph->th_fport, sizeof (in_port_t));
14977c478bd9Sstevel@tonic-gate bcopy(acceptor->tcp_tcph->th_fport, &acceptor->tcp_fport,
14987c478bd9Sstevel@tonic-gate sizeof (in_port_t));
14997c478bd9Sstevel@tonic-gate /*
15007c478bd9Sstevel@tonic-gate * For an all-port proxy listener, the local port is determined by
15017c478bd9Sstevel@tonic-gate * the port number field in the SYN packet.
15027c478bd9Sstevel@tonic-gate */
15037c478bd9Sstevel@tonic-gate if (listener->tcp_lport == 0) {
15047c478bd9Sstevel@tonic-gate acceptor->tcp_lport = *(in_port_t *)tcph->th_fport;
15057c478bd9Sstevel@tonic-gate bcopy(tcph->th_fport, acceptor->tcp_tcph->th_lport,
15067c478bd9Sstevel@tonic-gate sizeof (in_port_t));
15077c478bd9Sstevel@tonic-gate }
15087c478bd9Sstevel@tonic-gate /* Inherit various TCP parameters from the listener */
15097c478bd9Sstevel@tonic-gate acceptor->tcp_naglim = listener->tcp_naglim;
15107c478bd9Sstevel@tonic-gate acceptor->tcp_first_timer_threshold =
15117c478bd9Sstevel@tonic-gate listener->tcp_first_timer_threshold;
15127c478bd9Sstevel@tonic-gate acceptor->tcp_second_timer_threshold =
15137c478bd9Sstevel@tonic-gate listener->tcp_second_timer_threshold;
15147c478bd9Sstevel@tonic-gate
15157c478bd9Sstevel@tonic-gate acceptor->tcp_first_ctimer_threshold =
15167c478bd9Sstevel@tonic-gate listener->tcp_first_ctimer_threshold;
15177c478bd9Sstevel@tonic-gate acceptor->tcp_second_ctimer_threshold =
15187c478bd9Sstevel@tonic-gate listener->tcp_second_ctimer_threshold;
15197c478bd9Sstevel@tonic-gate
15207c478bd9Sstevel@tonic-gate acceptor->tcp_xmit_hiwater = listener->tcp_xmit_hiwater;
15217c478bd9Sstevel@tonic-gate
15227c478bd9Sstevel@tonic-gate acceptor->tcp_state = TCPS_LISTEN;
15237c478bd9Sstevel@tonic-gate tcp_iss_init(acceptor);
15247c478bd9Sstevel@tonic-gate
15257c478bd9Sstevel@tonic-gate /* Process all TCP options. */
15267c478bd9Sstevel@tonic-gate tcp_process_options(acceptor, tcph);
15277c478bd9Sstevel@tonic-gate
15287c478bd9Sstevel@tonic-gate /* Is the other end ECN capable? */
15297c478bd9Sstevel@tonic-gate if (tcp_ecn_permitted >= 1 &&
15307c478bd9Sstevel@tonic-gate (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) {
15317c478bd9Sstevel@tonic-gate acceptor->tcp_ecn_ok = B_TRUE;
15327c478bd9Sstevel@tonic-gate }
15337c478bd9Sstevel@tonic-gate
15347c478bd9Sstevel@tonic-gate /*
15357c478bd9Sstevel@tonic-gate * listener->tcp_rq->q_hiwat should be the default window size or a
15367c478bd9Sstevel@tonic-gate * window size changed via SO_RCVBUF option. First round up the
15377c478bd9Sstevel@tonic-gate * acceptor's tcp_rwnd to the nearest MSS. Then find out the window
15387c478bd9Sstevel@tonic-gate * scale option value if needed. Call tcp_rwnd_set() to finish the
15397c478bd9Sstevel@tonic-gate * setting.
15407c478bd9Sstevel@tonic-gate *
15417c478bd9Sstevel@tonic-gate * Note if there is a rpipe metric associated with the remote host,
15427c478bd9Sstevel@tonic-gate * we should not inherit receive window size from listener.
15437c478bd9Sstevel@tonic-gate */
15447c478bd9Sstevel@tonic-gate acceptor->tcp_rwnd = MSS_ROUNDUP(
15457c478bd9Sstevel@tonic-gate (acceptor->tcp_rwnd == 0 ? listener->tcp_rwnd_max :
15467c478bd9Sstevel@tonic-gate acceptor->tcp_rwnd), acceptor->tcp_mss);
15477c478bd9Sstevel@tonic-gate if (acceptor->tcp_snd_ws_ok)
15487c478bd9Sstevel@tonic-gate SET_WS_VALUE(acceptor);
15497c478bd9Sstevel@tonic-gate /*
15507c478bd9Sstevel@tonic-gate * Note that this is the only place tcp_rwnd_set() is called for
15517c478bd9Sstevel@tonic-gate * accepting a connection. We need to call it here instead of
15527c478bd9Sstevel@tonic-gate * after the 3-way handshake because we need to tell the other
15537c478bd9Sstevel@tonic-gate * side our rwnd in the SYN-ACK segment.
15547c478bd9Sstevel@tonic-gate */
15557c478bd9Sstevel@tonic-gate (void) tcp_rwnd_set(acceptor, acceptor->tcp_rwnd);
15567c478bd9Sstevel@tonic-gate
15577c478bd9Sstevel@tonic-gate return (0);
15587c478bd9Sstevel@tonic-gate }
15597c478bd9Sstevel@tonic-gate
15607c478bd9Sstevel@tonic-gate /*
15617c478bd9Sstevel@tonic-gate * Defense for the SYN attack -
15627c478bd9Sstevel@tonic-gate * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest
15637c478bd9Sstevel@tonic-gate * one that doesn't have the dontdrop bit set.
15647c478bd9Sstevel@tonic-gate * 2. Don't drop a SYN request before its first timeout. This gives every
15657c478bd9Sstevel@tonic-gate * request at least til the first timeout to complete its 3-way handshake.
15667c478bd9Sstevel@tonic-gate * 3. The current threshold is - # of timeout > q0len/4 => SYN alert on
15677c478bd9Sstevel@tonic-gate * # of timeout drops back to <= q0len/32 => SYN alert off
15687c478bd9Sstevel@tonic-gate */
15697c478bd9Sstevel@tonic-gate static boolean_t
tcp_drop_q0(tcp_t * tcp)15707c478bd9Sstevel@tonic-gate tcp_drop_q0(tcp_t *tcp)
15717c478bd9Sstevel@tonic-gate {
15727c478bd9Sstevel@tonic-gate tcp_t *eager;
15737c478bd9Sstevel@tonic-gate
15747c478bd9Sstevel@tonic-gate assert(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
15757c478bd9Sstevel@tonic-gate /*
15767c478bd9Sstevel@tonic-gate * New one is added after next_q0 so prev_q0 points to the oldest
15777c478bd9Sstevel@tonic-gate * Also do not drop any established connections that are deferred on
15787c478bd9Sstevel@tonic-gate * q0 due to q being full
15797c478bd9Sstevel@tonic-gate */
15807c478bd9Sstevel@tonic-gate
15817c478bd9Sstevel@tonic-gate eager = tcp->tcp_eager_prev_q0;
15827c478bd9Sstevel@tonic-gate while (eager->tcp_dontdrop || eager->tcp_conn_def_q0) {
15837c478bd9Sstevel@tonic-gate /* XXX should move the eager to the head */
15847c478bd9Sstevel@tonic-gate eager = eager->tcp_eager_prev_q0;
15857c478bd9Sstevel@tonic-gate if (eager == tcp) {
15867c478bd9Sstevel@tonic-gate eager = tcp->tcp_eager_prev_q0;
15877c478bd9Sstevel@tonic-gate break;
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate }
15907c478bd9Sstevel@tonic-gate dprintf("tcp_drop_q0: listen half-open queue (max=%d) overflow"
15917c478bd9Sstevel@tonic-gate " (%d pending) on %s, drop one", tcp_conn_req_max_q0,
15927c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0,
15937c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
15947c478bd9Sstevel@tonic-gate
15957c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpHalfOpenDrop);
15967c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
15977c478bd9Sstevel@tonic-gate return (B_TRUE);
15987c478bd9Sstevel@tonic-gate }
15997c478bd9Sstevel@tonic-gate
16007c478bd9Sstevel@tonic-gate /* ARGSUSED */
16017c478bd9Sstevel@tonic-gate static tcp_t *
tcp_conn_request(tcp_t * tcp,mblk_t * mp,uint_t sock_id,uint_t ip_hdr_len)16027c478bd9Sstevel@tonic-gate tcp_conn_request(tcp_t *tcp, mblk_t *mp, uint_t sock_id, uint_t ip_hdr_len)
16037c478bd9Sstevel@tonic-gate {
16047c478bd9Sstevel@tonic-gate tcp_t *eager;
16057c478bd9Sstevel@tonic-gate struct ip *ipha;
16067c478bd9Sstevel@tonic-gate int err;
16077c478bd9Sstevel@tonic-gate
16087c478bd9Sstevel@tonic-gate #ifdef DEBUG
16097c478bd9Sstevel@tonic-gate printf("tcp_conn_request ###################\n");
16107c478bd9Sstevel@tonic-gate #endif
16117c478bd9Sstevel@tonic-gate
16127c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) {
16137c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpListenDrop);
16147c478bd9Sstevel@tonic-gate dprintf("tcp_conn_request: listen backlog (max=%d) "
16157c478bd9Sstevel@tonic-gate "overflow (%d pending) on %s",
16167c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q,
16177c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
16187c478bd9Sstevel@tonic-gate return (NULL);
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate
16217c478bd9Sstevel@tonic-gate assert(OK_32PTR(mp->b_rptr));
16227c478bd9Sstevel@tonic-gate
16237c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q0 >=
16247c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_max + tcp_conn_req_max_q0) {
16257c478bd9Sstevel@tonic-gate /*
16267c478bd9Sstevel@tonic-gate * Q0 is full. Drop a pending half-open req from the queue
16277c478bd9Sstevel@tonic-gate * to make room for the new SYN req. Also mark the time we
16287c478bd9Sstevel@tonic-gate * drop a SYN.
16297c478bd9Sstevel@tonic-gate */
16307c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
16317c478bd9Sstevel@tonic-gate if (!tcp_drop_q0(tcp)) {
16327c478bd9Sstevel@tonic-gate freemsg(mp);
16337c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpListenDropQ0);
16347c478bd9Sstevel@tonic-gate dprintf("tcp_conn_request: listen half-open queue "
16357c478bd9Sstevel@tonic-gate "(max=%d) full (%d pending) on %s",
16367c478bd9Sstevel@tonic-gate tcp_conn_req_max_q0,
16377c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0,
16387c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
16397c478bd9Sstevel@tonic-gate return (NULL);
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate }
16427c478bd9Sstevel@tonic-gate
16437c478bd9Sstevel@tonic-gate ipha = (struct ip *)mp->b_rptr;
16447c478bd9Sstevel@tonic-gate if (IN_CLASSD(ntohl(ipha->ip_src.s_addr)) ||
16457c478bd9Sstevel@tonic-gate ipha->ip_src.s_addr == INADDR_BROADCAST ||
16467c478bd9Sstevel@tonic-gate ipha->ip_src.s_addr == INADDR_ANY ||
16477c478bd9Sstevel@tonic-gate ipha->ip_dst.s_addr == INADDR_BROADCAST) {
16487c478bd9Sstevel@tonic-gate freemsg(mp);
16497c478bd9Sstevel@tonic-gate return (NULL);
16507c478bd9Sstevel@tonic-gate }
16517c478bd9Sstevel@tonic-gate /*
16527c478bd9Sstevel@tonic-gate * We allow the connection to proceed
16537c478bd9Sstevel@tonic-gate * by generating a detached tcp state vector and put it in
16547c478bd9Sstevel@tonic-gate * the eager queue. When an accept happens, it will be
16557c478bd9Sstevel@tonic-gate * dequeued sequentially.
16567c478bd9Sstevel@tonic-gate */
16577c478bd9Sstevel@tonic-gate if ((eager = (tcp_t *)bkmem_alloc(sizeof (tcp_t))) == NULL) {
16587c478bd9Sstevel@tonic-gate freemsg(mp);
16597c478bd9Sstevel@tonic-gate errno = ENOBUFS;
16607c478bd9Sstevel@tonic-gate return (NULL);
16617c478bd9Sstevel@tonic-gate }
16627c478bd9Sstevel@tonic-gate if ((errno = tcp_init_values(eager, NULL)) != 0) {
16637c478bd9Sstevel@tonic-gate freemsg(mp);
16647c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
16657c478bd9Sstevel@tonic-gate return (NULL);
16667c478bd9Sstevel@tonic-gate }
16677c478bd9Sstevel@tonic-gate
16687c478bd9Sstevel@tonic-gate /*
16697c478bd9Sstevel@tonic-gate * Eager connection inherits address form from its listener,
16707c478bd9Sstevel@tonic-gate * but its packet form comes from the version of the received
16717c478bd9Sstevel@tonic-gate * SYN segment.
16727c478bd9Sstevel@tonic-gate */
16737c478bd9Sstevel@tonic-gate eager->tcp_family = tcp->tcp_family;
16747c478bd9Sstevel@tonic-gate
16757c478bd9Sstevel@tonic-gate err = tcp_accept_comm(tcp, eager, mp, ip_hdr_len);
16767c478bd9Sstevel@tonic-gate if (err) {
16777c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
16787c478bd9Sstevel@tonic-gate return (NULL);
16797c478bd9Sstevel@tonic-gate }
16807c478bd9Sstevel@tonic-gate
16817c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager;
16827c478bd9Sstevel@tonic-gate eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0;
16837c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 = eager;
16847c478bd9Sstevel@tonic-gate eager->tcp_eager_prev_q0 = tcp;
16857c478bd9Sstevel@tonic-gate
16867c478bd9Sstevel@tonic-gate /* Set tcp_listener before adding it to tcp_conn_fanout */
16877c478bd9Sstevel@tonic-gate eager->tcp_listener = tcp;
16887c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0++;
16897c478bd9Sstevel@tonic-gate
16907c478bd9Sstevel@tonic-gate return (eager);
16917c478bd9Sstevel@tonic-gate }
16927c478bd9Sstevel@tonic-gate
16937c478bd9Sstevel@tonic-gate /*
16947c478bd9Sstevel@tonic-gate * To get around the non-interrupt problem of inetboot.
16957c478bd9Sstevel@tonic-gate * Keep on processing packets until a certain state is reached or the
16967c478bd9Sstevel@tonic-gate * TCP is destroyed because of getting a RST packet.
16977c478bd9Sstevel@tonic-gate */
16987c478bd9Sstevel@tonic-gate static int
tcp_state_wait(int sock_id,tcp_t * tcp,int state)16997c478bd9Sstevel@tonic-gate tcp_state_wait(int sock_id, tcp_t *tcp, int state)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate int i;
17027c478bd9Sstevel@tonic-gate struct inetgram *in_gram;
17037c478bd9Sstevel@tonic-gate mblk_t *mp;
17047c478bd9Sstevel@tonic-gate int timeout;
17057c478bd9Sstevel@tonic-gate boolean_t changed = B_FALSE;
17067c478bd9Sstevel@tonic-gate
17077c478bd9Sstevel@tonic-gate /*
17087c478bd9Sstevel@tonic-gate * We need to make sure that the MAC does not wait longer
17097c478bd9Sstevel@tonic-gate * than RTO for any packet so that TCP can do retransmission.
17107c478bd9Sstevel@tonic-gate * But if the MAC timeout is less than tcp_rto, we are fine
17117c478bd9Sstevel@tonic-gate * and do not need to change it.
17127c478bd9Sstevel@tonic-gate */
17137c478bd9Sstevel@tonic-gate timeout = sockets[sock_id].in_timeout;
17147c478bd9Sstevel@tonic-gate if (timeout > tcp->tcp_rto) {
17157c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = tcp->tcp_rto;
17167c478bd9Sstevel@tonic-gate changed = B_TRUE;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate retry:
17197c478bd9Sstevel@tonic-gate if (sockets[sock_id].inq == NULL) {
17207c478bd9Sstevel@tonic-gate /* Go out and check the wire */
17217c478bd9Sstevel@tonic-gate for (i = MEDIA_LVL; i < TRANSPORT_LVL; i++) {
17227c478bd9Sstevel@tonic-gate if (sockets[sock_id].input[i] != NULL) {
17237c478bd9Sstevel@tonic-gate if (sockets[sock_id].input[i](sock_id) < 0) {
17247c478bd9Sstevel@tonic-gate if (changed) {
17257c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout =
17267c478bd9Sstevel@tonic-gate timeout;
17277c478bd9Sstevel@tonic-gate }
17287c478bd9Sstevel@tonic-gate return (-1);
17297c478bd9Sstevel@tonic-gate }
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate }
17337c478bd9Sstevel@tonic-gate
17347c478bd9Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
1735*b531f6d1SToomas Soome if (tcp->tcp_state == state)
17367c478bd9Sstevel@tonic-gate break;
17377c478bd9Sstevel@tonic-gate
17387c478bd9Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
17397c478bd9Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
17407c478bd9Sstevel@tonic-gate #ifdef DEBUG
17417c478bd9Sstevel@tonic-gate printf("tcp_state_wait for state %d: unexpected "
17427c478bd9Sstevel@tonic-gate "packet level %d frame found\n", state,
17437c478bd9Sstevel@tonic-gate in_gram->igm_level);
17447c478bd9Sstevel@tonic-gate #endif
17457c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
17467c478bd9Sstevel@tonic-gate continue;
17477c478bd9Sstevel@tonic-gate }
17487c478bd9Sstevel@tonic-gate mp = in_gram->igm_mp;
17497c478bd9Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
17507c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
17517c478bd9Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
17527c478bd9Sstevel@tonic-gate
17537c478bd9Sstevel@tonic-gate /*
17547c478bd9Sstevel@tonic-gate * The other side may have closed this connection or
17557c478bd9Sstevel@tonic-gate * RST us. But we need to continue to process other
17567c478bd9Sstevel@tonic-gate * packets in the socket's queue because they may be
17577c478bd9Sstevel@tonic-gate * belong to another TCP connections.
17587c478bd9Sstevel@tonic-gate */
17597c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL) {
17607c478bd9Sstevel@tonic-gate tcp = NULL;
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate }
17637c478bd9Sstevel@tonic-gate
17647c478bd9Sstevel@tonic-gate /* If the other side has closed the connection, just return. */
17657c478bd9Sstevel@tonic-gate if (tcp == NULL || sockets[sock_id].pcb == NULL) {
17667c478bd9Sstevel@tonic-gate #ifdef DEBUG
17677c478bd9Sstevel@tonic-gate printf("tcp_state_wait other side dead: state %d "
17687c478bd9Sstevel@tonic-gate "error %d\n", state, sockets[sock_id].so_error);
17697c478bd9Sstevel@tonic-gate #endif
17707c478bd9Sstevel@tonic-gate if (sockets[sock_id].so_error != 0)
17717c478bd9Sstevel@tonic-gate return (-1);
17727c478bd9Sstevel@tonic-gate else
17737c478bd9Sstevel@tonic-gate return (0);
17747c478bd9Sstevel@tonic-gate }
17757c478bd9Sstevel@tonic-gate /*
17767c478bd9Sstevel@tonic-gate * TCPS_ALL_ACKED is not a valid TCP state, it is just used as an
17777c478bd9Sstevel@tonic-gate * indicator to tcp_state_wait to mean that it is being called
17787c478bd9Sstevel@tonic-gate * to wait till we have received acks for all the new segments sent.
17797c478bd9Sstevel@tonic-gate */
17807c478bd9Sstevel@tonic-gate if ((state == TCPS_ALL_ACKED) && (tcp->tcp_suna == tcp->tcp_snxt)) {
17817c478bd9Sstevel@tonic-gate goto done;
17827c478bd9Sstevel@tonic-gate }
17837c478bd9Sstevel@tonic-gate if (tcp->tcp_state != state) {
17847c478bd9Sstevel@tonic-gate if (prom_gettime() > tcp->tcp_rto_timeout)
17857c478bd9Sstevel@tonic-gate tcp_timer(tcp, sock_id);
17867c478bd9Sstevel@tonic-gate goto retry;
17877c478bd9Sstevel@tonic-gate }
17887c478bd9Sstevel@tonic-gate done:
17897c478bd9Sstevel@tonic-gate if (changed)
17907c478bd9Sstevel@tonic-gate sockets[sock_id].in_timeout = timeout;
17917c478bd9Sstevel@tonic-gate
17927c478bd9Sstevel@tonic-gate tcp_drain_needed(sock_id, tcp);
17937c478bd9Sstevel@tonic-gate return (0);
17947c478bd9Sstevel@tonic-gate }
17957c478bd9Sstevel@tonic-gate
17967c478bd9Sstevel@tonic-gate /* Verify the checksum of a segment. */
17977c478bd9Sstevel@tonic-gate static int
tcp_verify_cksum(mblk_t * mp)17987c478bd9Sstevel@tonic-gate tcp_verify_cksum(mblk_t *mp)
17997c478bd9Sstevel@tonic-gate {
18007c478bd9Sstevel@tonic-gate struct ip *iph;
18017c478bd9Sstevel@tonic-gate tcpha_t *tcph;
18027c478bd9Sstevel@tonic-gate int len;
18037c478bd9Sstevel@tonic-gate uint16_t old_sum;
18047c478bd9Sstevel@tonic-gate
18057c478bd9Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
18067c478bd9Sstevel@tonic-gate tcph = (tcpha_t *)(iph + 1);
18077c478bd9Sstevel@tonic-gate len = ntohs(iph->ip_len);
18087c478bd9Sstevel@tonic-gate
18097c478bd9Sstevel@tonic-gate /*
18107c478bd9Sstevel@tonic-gate * Calculate the TCP checksum. Need to include the psuedo header,
18117c478bd9Sstevel@tonic-gate * which is similar to the real IP header starting at the TTL field.
18127c478bd9Sstevel@tonic-gate */
18137c478bd9Sstevel@tonic-gate iph->ip_sum = htons(len - IP_SIMPLE_HDR_LENGTH);
18147c478bd9Sstevel@tonic-gate old_sum = tcph->tha_sum;
18157c478bd9Sstevel@tonic-gate tcph->tha_sum = 0;
18167c478bd9Sstevel@tonic-gate iph->ip_ttl = 0;
18177c478bd9Sstevel@tonic-gate if (old_sum == tcp_cksum((uint16_t *)&(iph->ip_ttl),
18187c478bd9Sstevel@tonic-gate len - IP_SIMPLE_HDR_LENGTH + 12)) {
18197c478bd9Sstevel@tonic-gate return (0);
18207c478bd9Sstevel@tonic-gate } else {
18217c478bd9Sstevel@tonic-gate tcp_cksum_errors++;
18227c478bd9Sstevel@tonic-gate return (-1);
18237c478bd9Sstevel@tonic-gate }
18247c478bd9Sstevel@tonic-gate }
18257c478bd9Sstevel@tonic-gate
18267c478bd9Sstevel@tonic-gate /* To find a TCP connection matching the incoming segment. */
18277c478bd9Sstevel@tonic-gate static tcp_t *
tcp_lookup_ipv4(struct ip * iph,tcpha_t * tcph,int min_state,int * sock_id)18287c478bd9Sstevel@tonic-gate tcp_lookup_ipv4(struct ip *iph, tcpha_t *tcph, int min_state, int *sock_id)
18297c478bd9Sstevel@tonic-gate {
18307c478bd9Sstevel@tonic-gate int i;
18317c478bd9Sstevel@tonic-gate tcp_t *tcp;
18327c478bd9Sstevel@tonic-gate
18337c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
18347c478bd9Sstevel@tonic-gate if (sockets[i].type == INETBOOT_STREAM &&
18357c478bd9Sstevel@tonic-gate (tcp = (tcp_t *)sockets[i].pcb) != NULL) {
18367c478bd9Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18377c478bd9Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18387c478bd9Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18397c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source &&
18407c478bd9Sstevel@tonic-gate tcp->tcp_state >= min_state) {
18417c478bd9Sstevel@tonic-gate *sock_id = i;
18427c478bd9Sstevel@tonic-gate return (tcp);
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate }
18457c478bd9Sstevel@tonic-gate }
18467c478bd9Sstevel@tonic-gate /* Find it in the time wait list. */
18477c478bd9Sstevel@tonic-gate for (tcp = tcp_time_wait_head; tcp != NULL;
18487c478bd9Sstevel@tonic-gate tcp = tcp->tcp_time_wait_next) {
18497c478bd9Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18507c478bd9Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18517c478bd9Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18527c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source &&
18537c478bd9Sstevel@tonic-gate tcp->tcp_state >= min_state) {
18547c478bd9Sstevel@tonic-gate *sock_id = -1;
18557c478bd9Sstevel@tonic-gate return (tcp);
18567c478bd9Sstevel@tonic-gate }
18577c478bd9Sstevel@tonic-gate }
18587c478bd9Sstevel@tonic-gate return (NULL);
18597c478bd9Sstevel@tonic-gate }
18607c478bd9Sstevel@tonic-gate
18617c478bd9Sstevel@tonic-gate /* To find a TCP listening connection matching the incoming segment. */
18627c478bd9Sstevel@tonic-gate static tcp_t *
tcp_lookup_listener_ipv4(in_addr_t addr,in_port_t port,int * sock_id)18637c478bd9Sstevel@tonic-gate tcp_lookup_listener_ipv4(in_addr_t addr, in_port_t port, int *sock_id)
18647c478bd9Sstevel@tonic-gate {
18657c478bd9Sstevel@tonic-gate int i;
18667c478bd9Sstevel@tonic-gate tcp_t *tcp;
18677c478bd9Sstevel@tonic-gate
18687c478bd9Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
18697c478bd9Sstevel@tonic-gate if (sockets[i].type == INETBOOT_STREAM &&
18707c478bd9Sstevel@tonic-gate (tcp = (tcp_t *)sockets[i].pcb) != NULL) {
18717c478bd9Sstevel@tonic-gate if (tcp->tcp_lport == port &&
18727c478bd9Sstevel@tonic-gate (tcp->tcp_bound_source == addr ||
18737c478bd9Sstevel@tonic-gate tcp->tcp_bound_source == INADDR_ANY)) {
18747c478bd9Sstevel@tonic-gate *sock_id = i;
18757c478bd9Sstevel@tonic-gate return (tcp);
18767c478bd9Sstevel@tonic-gate }
18777c478bd9Sstevel@tonic-gate }
18787c478bd9Sstevel@tonic-gate }
18797c478bd9Sstevel@tonic-gate
18807c478bd9Sstevel@tonic-gate return (NULL);
18817c478bd9Sstevel@tonic-gate }
18827c478bd9Sstevel@tonic-gate
18837c478bd9Sstevel@tonic-gate /* To find a TCP eager matching the incoming segment. */
18847c478bd9Sstevel@tonic-gate static tcp_t *
tcp_lookup_eager_ipv4(tcp_t * listener,struct ip * iph,tcpha_t * tcph)18857c478bd9Sstevel@tonic-gate tcp_lookup_eager_ipv4(tcp_t *listener, struct ip *iph, tcpha_t *tcph)
18867c478bd9Sstevel@tonic-gate {
18877c478bd9Sstevel@tonic-gate tcp_t *tcp;
18887c478bd9Sstevel@tonic-gate
18897c478bd9Sstevel@tonic-gate #ifdef DEBUG
18907c478bd9Sstevel@tonic-gate printf("tcp_lookup_eager_ipv4 ###############\n");
18917c478bd9Sstevel@tonic-gate #endif
18927c478bd9Sstevel@tonic-gate for (tcp = listener->tcp_eager_next_q; tcp != NULL;
18937c478bd9Sstevel@tonic-gate tcp = tcp->tcp_eager_next_q) {
18947c478bd9Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18957c478bd9Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18967c478bd9Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18977c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source) {
18987c478bd9Sstevel@tonic-gate return (tcp);
18997c478bd9Sstevel@tonic-gate }
19007c478bd9Sstevel@tonic-gate }
19017c478bd9Sstevel@tonic-gate
19027c478bd9Sstevel@tonic-gate for (tcp = listener->tcp_eager_next_q0; tcp != listener;
19037c478bd9Sstevel@tonic-gate tcp = tcp->tcp_eager_next_q0) {
19047c478bd9Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
19057c478bd9Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
19067c478bd9Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
19077c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source) {
19087c478bd9Sstevel@tonic-gate return (tcp);
19097c478bd9Sstevel@tonic-gate }
19107c478bd9Sstevel@tonic-gate }
19117c478bd9Sstevel@tonic-gate #ifdef DEBUG
19127c478bd9Sstevel@tonic-gate printf("No eager found\n");
19137c478bd9Sstevel@tonic-gate #endif
19147c478bd9Sstevel@tonic-gate return (NULL);
19157c478bd9Sstevel@tonic-gate }
19167c478bd9Sstevel@tonic-gate
19177c478bd9Sstevel@tonic-gate /* To destroy a TCP control block. */
19187c478bd9Sstevel@tonic-gate static void
tcp_clean_death(int sock_id,tcp_t * tcp,int err)19197c478bd9Sstevel@tonic-gate tcp_clean_death(int sock_id, tcp_t *tcp, int err)
19207c478bd9Sstevel@tonic-gate {
19217c478bd9Sstevel@tonic-gate tcp_free(tcp);
19227c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT)
19237c478bd9Sstevel@tonic-gate tcp_time_wait_remove(tcp);
19247c478bd9Sstevel@tonic-gate
19257c478bd9Sstevel@tonic-gate if (sock_id >= 0) {
19267c478bd9Sstevel@tonic-gate sockets[sock_id].pcb = NULL;
19277c478bd9Sstevel@tonic-gate if (err != 0)
19287c478bd9Sstevel@tonic-gate sockets[sock_id].so_error = err;
19297c478bd9Sstevel@tonic-gate }
19307c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
19317c478bd9Sstevel@tonic-gate }
19327c478bd9Sstevel@tonic-gate
19337c478bd9Sstevel@tonic-gate /*
19347c478bd9Sstevel@tonic-gate * tcp_rwnd_set() is called to adjust the receive window to a desired value.
19357c478bd9Sstevel@tonic-gate * We do not allow the receive window to shrink. After setting rwnd,
19367c478bd9Sstevel@tonic-gate * set the flow control hiwat of the stream.
19377c478bd9Sstevel@tonic-gate *
19387c478bd9Sstevel@tonic-gate * This function is called in 2 cases:
19397c478bd9Sstevel@tonic-gate *
19407c478bd9Sstevel@tonic-gate * 1) Before data transfer begins, in tcp_accept_comm() for accepting a
19417c478bd9Sstevel@tonic-gate * connection (passive open) and in tcp_rput_data() for active connect.
19427c478bd9Sstevel@tonic-gate * This is called after tcp_mss_set() when the desired MSS value is known.
19437c478bd9Sstevel@tonic-gate * This makes sure that our window size is a mutiple of the other side's
19447c478bd9Sstevel@tonic-gate * MSS.
19457c478bd9Sstevel@tonic-gate * 2) Handling SO_RCVBUF option.
19467c478bd9Sstevel@tonic-gate *
19477c478bd9Sstevel@tonic-gate * It is ASSUMED that the requested size is a multiple of the current MSS.
19487c478bd9Sstevel@tonic-gate *
19497c478bd9Sstevel@tonic-gate * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
19507c478bd9Sstevel@tonic-gate * user requests so.
19517c478bd9Sstevel@tonic-gate */
19527c478bd9Sstevel@tonic-gate static int
tcp_rwnd_set(tcp_t * tcp,uint32_t rwnd)19537c478bd9Sstevel@tonic-gate tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
19547c478bd9Sstevel@tonic-gate {
19557c478bd9Sstevel@tonic-gate uint32_t mss = tcp->tcp_mss;
19567c478bd9Sstevel@tonic-gate uint32_t old_max_rwnd;
19577c478bd9Sstevel@tonic-gate uint32_t max_transmittable_rwnd;
19587c478bd9Sstevel@tonic-gate
19597c478bd9Sstevel@tonic-gate if (tcp->tcp_rwnd_max != 0)
19607c478bd9Sstevel@tonic-gate old_max_rwnd = tcp->tcp_rwnd_max;
19617c478bd9Sstevel@tonic-gate else
19627c478bd9Sstevel@tonic-gate old_max_rwnd = tcp->tcp_rwnd;
19637c478bd9Sstevel@tonic-gate
19647c478bd9Sstevel@tonic-gate /*
19657c478bd9Sstevel@tonic-gate * Insist on a receive window that is at least
19667c478bd9Sstevel@tonic-gate * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
19677c478bd9Sstevel@tonic-gate * funny TCP interactions of Nagle algorithm, SWS avoidance
19687c478bd9Sstevel@tonic-gate * and delayed acknowledgement.
19697c478bd9Sstevel@tonic-gate */
19707c478bd9Sstevel@tonic-gate rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss);
19717c478bd9Sstevel@tonic-gate
19727c478bd9Sstevel@tonic-gate /*
19737c478bd9Sstevel@tonic-gate * If window size info has already been exchanged, TCP should not
19747c478bd9Sstevel@tonic-gate * shrink the window. Shrinking window is doable if done carefully.
19757c478bd9Sstevel@tonic-gate * We may add that support later. But so far there is not a real
19767c478bd9Sstevel@tonic-gate * need to do that.
19777c478bd9Sstevel@tonic-gate */
19787c478bd9Sstevel@tonic-gate if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
19797c478bd9Sstevel@tonic-gate /* MSS may have changed, do a round up again. */
19807c478bd9Sstevel@tonic-gate rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
19817c478bd9Sstevel@tonic-gate }
19827c478bd9Sstevel@tonic-gate
19837c478bd9Sstevel@tonic-gate /*
19847c478bd9Sstevel@tonic-gate * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
19857c478bd9Sstevel@tonic-gate * can be applied even before the window scale option is decided.
19867c478bd9Sstevel@tonic-gate */
19877c478bd9Sstevel@tonic-gate max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
19887c478bd9Sstevel@tonic-gate if (rwnd > max_transmittable_rwnd) {
19897c478bd9Sstevel@tonic-gate rwnd = max_transmittable_rwnd -
19907c478bd9Sstevel@tonic-gate (max_transmittable_rwnd % mss);
19917c478bd9Sstevel@tonic-gate if (rwnd < mss)
19927c478bd9Sstevel@tonic-gate rwnd = max_transmittable_rwnd;
19937c478bd9Sstevel@tonic-gate /*
19947c478bd9Sstevel@tonic-gate * If we're over the limit we may have to back down tcp_rwnd.
19957c478bd9Sstevel@tonic-gate * The increment below won't work for us. So we set all three
19967c478bd9Sstevel@tonic-gate * here and the increment below will have no effect.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate tcp->tcp_rwnd = old_max_rwnd = rwnd;
19997c478bd9Sstevel@tonic-gate }
20007c478bd9Sstevel@tonic-gate
20017c478bd9Sstevel@tonic-gate /*
20027c478bd9Sstevel@tonic-gate * Increment the current rwnd by the amount the maximum grew (we
20037c478bd9Sstevel@tonic-gate * can not overwrite it since we might be in the middle of a
20047c478bd9Sstevel@tonic-gate * connection.)
20057c478bd9Sstevel@tonic-gate */
20067c478bd9Sstevel@tonic-gate tcp->tcp_rwnd += rwnd - old_max_rwnd;
20077c478bd9Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win);
20087c478bd9Sstevel@tonic-gate if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
20097c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_max = rwnd;
20107c478bd9Sstevel@tonic-gate tcp->tcp_rwnd_max = rwnd;
20117c478bd9Sstevel@tonic-gate
20127c478bd9Sstevel@tonic-gate return (rwnd);
20137c478bd9Sstevel@tonic-gate }
20147c478bd9Sstevel@tonic-gate
20157c478bd9Sstevel@tonic-gate /*
20167c478bd9Sstevel@tonic-gate * Extract option values from a tcp header. We put any found values into the
20177c478bd9Sstevel@tonic-gate * tcpopt struct and return a bitmask saying which options were found.
20187c478bd9Sstevel@tonic-gate */
20197c478bd9Sstevel@tonic-gate static int
tcp_parse_options(tcph_t * tcph,tcp_opt_t * tcpopt)20207c478bd9Sstevel@tonic-gate tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt)
20217c478bd9Sstevel@tonic-gate {
20227c478bd9Sstevel@tonic-gate uchar_t *endp;
20237c478bd9Sstevel@tonic-gate int len;
20247c478bd9Sstevel@tonic-gate uint32_t mss;
20257c478bd9Sstevel@tonic-gate uchar_t *up = (uchar_t *)tcph;
20267c478bd9Sstevel@tonic-gate int found = 0;
20277c478bd9Sstevel@tonic-gate int32_t sack_len;
20287c478bd9Sstevel@tonic-gate tcp_seq sack_begin, sack_end;
20297c478bd9Sstevel@tonic-gate tcp_t *tcp;
20307c478bd9Sstevel@tonic-gate
20317c478bd9Sstevel@tonic-gate endp = up + TCP_HDR_LENGTH(tcph);
20327c478bd9Sstevel@tonic-gate up += TCP_MIN_HEADER_LENGTH;
20337c478bd9Sstevel@tonic-gate while (up < endp) {
20347c478bd9Sstevel@tonic-gate len = endp - up;
20357c478bd9Sstevel@tonic-gate switch (*up) {
20367c478bd9Sstevel@tonic-gate case TCPOPT_EOL:
20377c478bd9Sstevel@tonic-gate break;
20387c478bd9Sstevel@tonic-gate
20397c478bd9Sstevel@tonic-gate case TCPOPT_NOP:
20407c478bd9Sstevel@tonic-gate up++;
20417c478bd9Sstevel@tonic-gate continue;
20427c478bd9Sstevel@tonic-gate
20437c478bd9Sstevel@tonic-gate case TCPOPT_MAXSEG:
20447c478bd9Sstevel@tonic-gate if (len < TCPOPT_MAXSEG_LEN ||
20457c478bd9Sstevel@tonic-gate up[1] != TCPOPT_MAXSEG_LEN)
20467c478bd9Sstevel@tonic-gate break;
20477c478bd9Sstevel@tonic-gate
20487c478bd9Sstevel@tonic-gate mss = BE16_TO_U16(up+2);
20497c478bd9Sstevel@tonic-gate /* Caller must handle tcp_mss_min and tcp_mss_max_* */
20507c478bd9Sstevel@tonic-gate tcpopt->tcp_opt_mss = mss;
20517c478bd9Sstevel@tonic-gate found |= TCP_OPT_MSS_PRESENT;
20527c478bd9Sstevel@tonic-gate
20537c478bd9Sstevel@tonic-gate up += TCPOPT_MAXSEG_LEN;
20547c478bd9Sstevel@tonic-gate continue;
20557c478bd9Sstevel@tonic-gate
20567c478bd9Sstevel@tonic-gate case TCPOPT_WSCALE:
20577c478bd9Sstevel@tonic-gate if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
20587c478bd9Sstevel@tonic-gate break;
20597c478bd9Sstevel@tonic-gate
20607c478bd9Sstevel@tonic-gate if (up[2] > TCP_MAX_WINSHIFT)
20617c478bd9Sstevel@tonic-gate tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
20627c478bd9Sstevel@tonic-gate else
20637c478bd9Sstevel@tonic-gate tcpopt->tcp_opt_wscale = up[2];
20647c478bd9Sstevel@tonic-gate found |= TCP_OPT_WSCALE_PRESENT;
20657c478bd9Sstevel@tonic-gate
20667c478bd9Sstevel@tonic-gate up += TCPOPT_WS_LEN;
20677c478bd9Sstevel@tonic-gate continue;
20687c478bd9Sstevel@tonic-gate
20697c478bd9Sstevel@tonic-gate case TCPOPT_SACK_PERMITTED:
20707c478bd9Sstevel@tonic-gate if (len < TCPOPT_SACK_OK_LEN ||
20717c478bd9Sstevel@tonic-gate up[1] != TCPOPT_SACK_OK_LEN)
20727c478bd9Sstevel@tonic-gate break;
20737c478bd9Sstevel@tonic-gate found |= TCP_OPT_SACK_OK_PRESENT;
20747c478bd9Sstevel@tonic-gate up += TCPOPT_SACK_OK_LEN;
20757c478bd9Sstevel@tonic-gate continue;
20767c478bd9Sstevel@tonic-gate
20777c478bd9Sstevel@tonic-gate case TCPOPT_SACK:
20787c478bd9Sstevel@tonic-gate if (len <= 2 || up[1] <= 2 || len < up[1])
20797c478bd9Sstevel@tonic-gate break;
20807c478bd9Sstevel@tonic-gate
20817c478bd9Sstevel@tonic-gate /* If TCP is not interested in SACK blks... */
20827c478bd9Sstevel@tonic-gate if ((tcp = tcpopt->tcp) == NULL) {
20837c478bd9Sstevel@tonic-gate up += up[1];
20847c478bd9Sstevel@tonic-gate continue;
20857c478bd9Sstevel@tonic-gate }
20867c478bd9Sstevel@tonic-gate sack_len = up[1] - TCPOPT_HEADER_LEN;
20877c478bd9Sstevel@tonic-gate up += TCPOPT_HEADER_LEN;
20887c478bd9Sstevel@tonic-gate
20897c478bd9Sstevel@tonic-gate /*
20907c478bd9Sstevel@tonic-gate * If the list is empty, allocate one and assume
20917c478bd9Sstevel@tonic-gate * nothing is sack'ed.
20927c478bd9Sstevel@tonic-gate */
20937c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
20947c478bd9Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
20957c478bd9Sstevel@tonic-gate tcp_notsack_update(&(tcp->tcp_notsack_list),
20967c478bd9Sstevel@tonic-gate tcp->tcp_suna, tcp->tcp_snxt,
20977c478bd9Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
20987c478bd9Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
20997c478bd9Sstevel@tonic-gate
21007c478bd9Sstevel@tonic-gate /*
21017c478bd9Sstevel@tonic-gate * Make sure tcp_notsack_list is not NULL.
21027c478bd9Sstevel@tonic-gate * This happens when kmem_alloc(KM_NOSLEEP)
21037c478bd9Sstevel@tonic-gate * returns NULL.
21047c478bd9Sstevel@tonic-gate */
21057c478bd9Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
21067c478bd9Sstevel@tonic-gate up += sack_len;
21077c478bd9Sstevel@tonic-gate continue;
21087c478bd9Sstevel@tonic-gate }
21097c478bd9Sstevel@tonic-gate tcp->tcp_fack = tcp->tcp_suna;
21107c478bd9Sstevel@tonic-gate }
21117c478bd9Sstevel@tonic-gate
21127c478bd9Sstevel@tonic-gate while (sack_len > 0) {
21137c478bd9Sstevel@tonic-gate if (up + 8 > endp) {
21147c478bd9Sstevel@tonic-gate up = endp;
21157c478bd9Sstevel@tonic-gate break;
21167c478bd9Sstevel@tonic-gate }
21177c478bd9Sstevel@tonic-gate sack_begin = BE32_TO_U32(up);
21187c478bd9Sstevel@tonic-gate up += 4;
21197c478bd9Sstevel@tonic-gate sack_end = BE32_TO_U32(up);
21207c478bd9Sstevel@tonic-gate up += 4;
21217c478bd9Sstevel@tonic-gate sack_len -= 8;
21227c478bd9Sstevel@tonic-gate /*
21237c478bd9Sstevel@tonic-gate * Bounds checking. Make sure the SACK
21247c478bd9Sstevel@tonic-gate * info is within tcp_suna and tcp_snxt.
21257c478bd9Sstevel@tonic-gate * If this SACK blk is out of bound, ignore
21267c478bd9Sstevel@tonic-gate * it but continue to parse the following
21277c478bd9Sstevel@tonic-gate * blks.
21287c478bd9Sstevel@tonic-gate */
21297c478bd9Sstevel@tonic-gate if (SEQ_LEQ(sack_end, sack_begin) ||
21307c478bd9Sstevel@tonic-gate SEQ_LT(sack_begin, tcp->tcp_suna) ||
21317c478bd9Sstevel@tonic-gate SEQ_GT(sack_end, tcp->tcp_snxt)) {
21327c478bd9Sstevel@tonic-gate continue;
21337c478bd9Sstevel@tonic-gate }
21347c478bd9Sstevel@tonic-gate tcp_notsack_insert(&(tcp->tcp_notsack_list),
21357c478bd9Sstevel@tonic-gate sack_begin, sack_end,
21367c478bd9Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
21377c478bd9Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
21387c478bd9Sstevel@tonic-gate if (SEQ_GT(sack_end, tcp->tcp_fack)) {
21397c478bd9Sstevel@tonic-gate tcp->tcp_fack = sack_end;
21407c478bd9Sstevel@tonic-gate }
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate found |= TCP_OPT_SACK_PRESENT;
21437c478bd9Sstevel@tonic-gate continue;
21447c478bd9Sstevel@tonic-gate
21457c478bd9Sstevel@tonic-gate case TCPOPT_TSTAMP:
21467c478bd9Sstevel@tonic-gate if (len < TCPOPT_TSTAMP_LEN ||
21477c478bd9Sstevel@tonic-gate up[1] != TCPOPT_TSTAMP_LEN)
21487c478bd9Sstevel@tonic-gate break;
21497c478bd9Sstevel@tonic-gate
21507c478bd9Sstevel@tonic-gate tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
21517c478bd9Sstevel@tonic-gate tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
21527c478bd9Sstevel@tonic-gate
21537c478bd9Sstevel@tonic-gate found |= TCP_OPT_TSTAMP_PRESENT;
21547c478bd9Sstevel@tonic-gate
21557c478bd9Sstevel@tonic-gate up += TCPOPT_TSTAMP_LEN;
21567c478bd9Sstevel@tonic-gate continue;
21577c478bd9Sstevel@tonic-gate
21587c478bd9Sstevel@tonic-gate default:
21597c478bd9Sstevel@tonic-gate if (len <= 1 || len < (int)up[1] || up[1] == 0)
21607c478bd9Sstevel@tonic-gate break;
21617c478bd9Sstevel@tonic-gate up += up[1];
21627c478bd9Sstevel@tonic-gate continue;
21637c478bd9Sstevel@tonic-gate }
21647c478bd9Sstevel@tonic-gate break;
21657c478bd9Sstevel@tonic-gate }
21667c478bd9Sstevel@tonic-gate return (found);
21677c478bd9Sstevel@tonic-gate }
21687c478bd9Sstevel@tonic-gate
21697c478bd9Sstevel@tonic-gate /*
21707c478bd9Sstevel@tonic-gate * Set the mss associated with a particular tcp based on its current value,
21717c478bd9Sstevel@tonic-gate * and a new one passed in. Observe minimums and maximums, and reset
21727c478bd9Sstevel@tonic-gate * other state variables that we want to view as multiples of mss.
21737c478bd9Sstevel@tonic-gate *
21747c478bd9Sstevel@tonic-gate * This function is called in various places mainly because
21757c478bd9Sstevel@tonic-gate * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the
21767c478bd9Sstevel@tonic-gate * other side's SYN/SYN-ACK packet arrives.
21777c478bd9Sstevel@tonic-gate * 2) PMTUd may get us a new MSS.
21787c478bd9Sstevel@tonic-gate * 3) If the other side stops sending us timestamp option, we need to
21797c478bd9Sstevel@tonic-gate * increase the MSS size to use the extra bytes available.
21807c478bd9Sstevel@tonic-gate */
21817c478bd9Sstevel@tonic-gate static void
tcp_mss_set(tcp_t * tcp,uint32_t mss)21827c478bd9Sstevel@tonic-gate tcp_mss_set(tcp_t *tcp, uint32_t mss)
21837c478bd9Sstevel@tonic-gate {
21847c478bd9Sstevel@tonic-gate uint32_t mss_max;
21857c478bd9Sstevel@tonic-gate
21867c478bd9Sstevel@tonic-gate mss_max = tcp_mss_max_ipv4;
21877c478bd9Sstevel@tonic-gate
21887c478bd9Sstevel@tonic-gate if (mss < tcp_mss_min)
21897c478bd9Sstevel@tonic-gate mss = tcp_mss_min;
21907c478bd9Sstevel@tonic-gate if (mss > mss_max)
21917c478bd9Sstevel@tonic-gate mss = mss_max;
21927c478bd9Sstevel@tonic-gate /*
21937c478bd9Sstevel@tonic-gate * Unless naglim has been set by our client to
21947c478bd9Sstevel@tonic-gate * a non-mss value, force naglim to track mss.
21957c478bd9Sstevel@tonic-gate * This can help to aggregate small writes.
21967c478bd9Sstevel@tonic-gate */
21977c478bd9Sstevel@tonic-gate if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
21987c478bd9Sstevel@tonic-gate tcp->tcp_naglim = mss;
21997c478bd9Sstevel@tonic-gate /*
22007c478bd9Sstevel@tonic-gate * TCP should be able to buffer at least 4 MSS data for obvious
22017c478bd9Sstevel@tonic-gate * performance reason.
22027c478bd9Sstevel@tonic-gate */
22037c478bd9Sstevel@tonic-gate if ((mss << 2) > tcp->tcp_xmit_hiwater)
22047c478bd9Sstevel@tonic-gate tcp->tcp_xmit_hiwater = mss << 2;
22057c478bd9Sstevel@tonic-gate tcp->tcp_mss = mss;
22067c478bd9Sstevel@tonic-gate /*
22077c478bd9Sstevel@tonic-gate * Initialize cwnd according to draft-floyd-incr-init-win-01.txt.
22087c478bd9Sstevel@tonic-gate * Previously, we use tcp_slow_start_initial to control the size
22097c478bd9Sstevel@tonic-gate * of the initial cwnd. Now, when tcp_slow_start_initial * mss
22107c478bd9Sstevel@tonic-gate * is smaller than the cwnd calculated from the formula suggested in
22117c478bd9Sstevel@tonic-gate * the draft, we use tcp_slow_start_initial * mss as the cwnd.
22127c478bd9Sstevel@tonic-gate * Otherwise, use the cwnd from the draft's formula. The default
22137c478bd9Sstevel@tonic-gate * of tcp_slow_start_initial is 2.
22147c478bd9Sstevel@tonic-gate */
22157c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss,
22167c478bd9Sstevel@tonic-gate MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss)));
22177c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
22187c478bd9Sstevel@tonic-gate }
22197c478bd9Sstevel@tonic-gate
22207c478bd9Sstevel@tonic-gate /*
22217c478bd9Sstevel@tonic-gate * Process all TCP option in SYN segment.
22227c478bd9Sstevel@tonic-gate *
22237c478bd9Sstevel@tonic-gate * This function sets up the correct tcp_mss value according to the
22247c478bd9Sstevel@tonic-gate * MSS option value and our header size. It also sets up the window scale
22257c478bd9Sstevel@tonic-gate * and timestamp values, and initialize SACK info blocks. But it does not
22267c478bd9Sstevel@tonic-gate * change receive window size after setting the tcp_mss value. The caller
22277c478bd9Sstevel@tonic-gate * should do the appropriate change.
22287c478bd9Sstevel@tonic-gate */
22297c478bd9Sstevel@tonic-gate void
tcp_process_options(tcp_t * tcp,tcph_t * tcph)22307c478bd9Sstevel@tonic-gate tcp_process_options(tcp_t *tcp, tcph_t *tcph)
22317c478bd9Sstevel@tonic-gate {
22327c478bd9Sstevel@tonic-gate int options;
22337c478bd9Sstevel@tonic-gate tcp_opt_t tcpopt;
22347c478bd9Sstevel@tonic-gate uint32_t mss_max;
22357c478bd9Sstevel@tonic-gate char *tmp_tcph;
22367c478bd9Sstevel@tonic-gate
22377c478bd9Sstevel@tonic-gate tcpopt.tcp = NULL;
22387c478bd9Sstevel@tonic-gate options = tcp_parse_options(tcph, &tcpopt);
22397c478bd9Sstevel@tonic-gate
22407c478bd9Sstevel@tonic-gate /*
22417c478bd9Sstevel@tonic-gate * Process MSS option. Note that MSS option value does not account
22427c478bd9Sstevel@tonic-gate * for IP or TCP options. This means that it is equal to MTU - minimum
22437c478bd9Sstevel@tonic-gate * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
22447c478bd9Sstevel@tonic-gate * IPv6.
22457c478bd9Sstevel@tonic-gate */
22467c478bd9Sstevel@tonic-gate if (!(options & TCP_OPT_MSS_PRESENT)) {
22477c478bd9Sstevel@tonic-gate tcpopt.tcp_opt_mss = tcp_mss_def_ipv4;
22487c478bd9Sstevel@tonic-gate } else {
22497c478bd9Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
22507c478bd9Sstevel@tonic-gate mss_max = tcp_mss_max_ipv4;
22517c478bd9Sstevel@tonic-gate if (tcpopt.tcp_opt_mss < tcp_mss_min)
22527c478bd9Sstevel@tonic-gate tcpopt.tcp_opt_mss = tcp_mss_min;
22537c478bd9Sstevel@tonic-gate else if (tcpopt.tcp_opt_mss > mss_max)
22547c478bd9Sstevel@tonic-gate tcpopt.tcp_opt_mss = mss_max;
22557c478bd9Sstevel@tonic-gate }
22567c478bd9Sstevel@tonic-gate
22577c478bd9Sstevel@tonic-gate /* Process Window Scale option. */
22587c478bd9Sstevel@tonic-gate if (options & TCP_OPT_WSCALE_PRESENT) {
22597c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
22607c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_TRUE;
22617c478bd9Sstevel@tonic-gate } else {
22627c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws = B_FALSE;
22637c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_FALSE;
22647c478bd9Sstevel@tonic-gate tcp->tcp_rcv_ws = B_FALSE;
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate
22677c478bd9Sstevel@tonic-gate /* Process Timestamp option. */
22687c478bd9Sstevel@tonic-gate if ((options & TCP_OPT_TSTAMP_PRESENT) &&
22697c478bd9Sstevel@tonic-gate (tcp->tcp_snd_ts_ok || !tcp->tcp_active_open)) {
22707c478bd9Sstevel@tonic-gate tmp_tcph = (char *)tcp->tcp_tcph;
22717c478bd9Sstevel@tonic-gate
22727c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_TRUE;
22737c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
22747c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
22757c478bd9Sstevel@tonic-gate assert(OK_32PTR(tmp_tcph));
22767c478bd9Sstevel@tonic-gate assert(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH);
22777c478bd9Sstevel@tonic-gate
22787c478bd9Sstevel@tonic-gate /* Fill in our template header with basic timestamp option. */
22797c478bd9Sstevel@tonic-gate tmp_tcph += tcp->tcp_tcp_hdr_len;
22807c478bd9Sstevel@tonic-gate tmp_tcph[0] = TCPOPT_NOP;
22817c478bd9Sstevel@tonic-gate tmp_tcph[1] = TCPOPT_NOP;
22827c478bd9Sstevel@tonic-gate tmp_tcph[2] = TCPOPT_TSTAMP;
22837c478bd9Sstevel@tonic-gate tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
22847c478bd9Sstevel@tonic-gate tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN;
22857c478bd9Sstevel@tonic-gate tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN;
22867c478bd9Sstevel@tonic-gate tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4);
22877c478bd9Sstevel@tonic-gate } else {
22887c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_FALSE;
22897c478bd9Sstevel@tonic-gate }
22907c478bd9Sstevel@tonic-gate
22917c478bd9Sstevel@tonic-gate /*
22927c478bd9Sstevel@tonic-gate * Process SACK options. If SACK is enabled for this connection,
22937c478bd9Sstevel@tonic-gate * then allocate the SACK info structure.
22947c478bd9Sstevel@tonic-gate */
22957c478bd9Sstevel@tonic-gate if ((options & TCP_OPT_SACK_OK_PRESENT) &&
22967c478bd9Sstevel@tonic-gate (tcp->tcp_snd_sack_ok ||
22977c478bd9Sstevel@tonic-gate (tcp_sack_permitted != 0 && !tcp->tcp_active_open))) {
22987c478bd9Sstevel@tonic-gate /* This should be true only in the passive case. */
22997c478bd9Sstevel@tonic-gate if (tcp->tcp_sack_info == NULL) {
23007c478bd9Sstevel@tonic-gate tcp->tcp_sack_info = (tcp_sack_info_t *)bkmem_zalloc(
23017c478bd9Sstevel@tonic-gate sizeof (tcp_sack_info_t));
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate if (tcp->tcp_sack_info == NULL) {
23047c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
23057c478bd9Sstevel@tonic-gate } else {
23067c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
23077c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
23087c478bd9Sstevel@tonic-gate tcp->tcp_max_sack_blk = 3;
23097c478bd9Sstevel@tonic-gate } else {
23107c478bd9Sstevel@tonic-gate tcp->tcp_max_sack_blk = 4;
23117c478bd9Sstevel@tonic-gate }
23127c478bd9Sstevel@tonic-gate }
23137c478bd9Sstevel@tonic-gate } else {
23147c478bd9Sstevel@tonic-gate /*
23157c478bd9Sstevel@tonic-gate * Resetting tcp_snd_sack_ok to B_FALSE so that
23167c478bd9Sstevel@tonic-gate * no SACK info will be used for this
23177c478bd9Sstevel@tonic-gate * connection. This assumes that SACK usage
23187c478bd9Sstevel@tonic-gate * permission is negotiated. This may need
23197c478bd9Sstevel@tonic-gate * to be changed once this is clarified.
23207c478bd9Sstevel@tonic-gate */
23217c478bd9Sstevel@tonic-gate if (tcp->tcp_sack_info != NULL) {
23227c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_sack_info,
23237c478bd9Sstevel@tonic-gate sizeof (tcp_sack_info_t));
23247c478bd9Sstevel@tonic-gate tcp->tcp_sack_info = NULL;
23257c478bd9Sstevel@tonic-gate }
23267c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
23277c478bd9Sstevel@tonic-gate }
23287c478bd9Sstevel@tonic-gate
23297c478bd9Sstevel@tonic-gate /*
23307c478bd9Sstevel@tonic-gate * Now we know the exact TCP/IP header length, subtract
23317c478bd9Sstevel@tonic-gate * that from tcp_mss to get our side's MSS.
23327c478bd9Sstevel@tonic-gate */
23337c478bd9Sstevel@tonic-gate tcp->tcp_mss -= tcp->tcp_hdr_len;
23347c478bd9Sstevel@tonic-gate /*
23357c478bd9Sstevel@tonic-gate * Here we assume that the other side's header size will be equal to
23367c478bd9Sstevel@tonic-gate * our header size. We calculate the real MSS accordingly. Need to
23377c478bd9Sstevel@tonic-gate * take into additional stuffs IPsec puts in.
23387c478bd9Sstevel@tonic-gate *
23397c478bd9Sstevel@tonic-gate * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
23407c478bd9Sstevel@tonic-gate */
23417c478bd9Sstevel@tonic-gate tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len -
23427c478bd9Sstevel@tonic-gate (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH);
23437c478bd9Sstevel@tonic-gate
23447c478bd9Sstevel@tonic-gate /*
23457c478bd9Sstevel@tonic-gate * Set MSS to the smaller one of both ends of the connection.
23467c478bd9Sstevel@tonic-gate * We should not have called tcp_mss_set() before, but our
23477c478bd9Sstevel@tonic-gate * side of the MSS should have been set to a proper value
23487c478bd9Sstevel@tonic-gate * by tcp_adapt_ire(). tcp_mss_set() will also set up the
23497c478bd9Sstevel@tonic-gate * STREAM head parameters properly.
23507c478bd9Sstevel@tonic-gate *
23517c478bd9Sstevel@tonic-gate * If we have a larger-than-16-bit window but the other side
23527c478bd9Sstevel@tonic-gate * didn't want to do window scale, tcp_rwnd_set() will take
23537c478bd9Sstevel@tonic-gate * care of that.
23547c478bd9Sstevel@tonic-gate */
23557c478bd9Sstevel@tonic-gate tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
23567c478bd9Sstevel@tonic-gate }
23577c478bd9Sstevel@tonic-gate
23587c478bd9Sstevel@tonic-gate /*
23597c478bd9Sstevel@tonic-gate * This function does PAWS protection check. Returns B_TRUE if the
23607c478bd9Sstevel@tonic-gate * segment passes the PAWS test, else returns B_FALSE.
23617c478bd9Sstevel@tonic-gate */
23627c478bd9Sstevel@tonic-gate boolean_t
tcp_paws_check(tcp_t * tcp,tcph_t * tcph,tcp_opt_t * tcpoptp)23637c478bd9Sstevel@tonic-gate tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp)
23647c478bd9Sstevel@tonic-gate {
23657c478bd9Sstevel@tonic-gate uint8_t flags;
23667c478bd9Sstevel@tonic-gate int options;
23677c478bd9Sstevel@tonic-gate uint8_t *up;
23687c478bd9Sstevel@tonic-gate
23697c478bd9Sstevel@tonic-gate flags = (unsigned int)tcph->th_flags[0] & 0xFF;
23707c478bd9Sstevel@tonic-gate /*
23717c478bd9Sstevel@tonic-gate * If timestamp option is aligned nicely, get values inline,
23727c478bd9Sstevel@tonic-gate * otherwise call general routine to parse. Only do that
23737c478bd9Sstevel@tonic-gate * if timestamp is the only option.
23747c478bd9Sstevel@tonic-gate */
23757c478bd9Sstevel@tonic-gate if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH +
23767c478bd9Sstevel@tonic-gate TCPOPT_REAL_TS_LEN &&
23777c478bd9Sstevel@tonic-gate OK_32PTR((up = ((uint8_t *)tcph) +
23787c478bd9Sstevel@tonic-gate TCP_MIN_HEADER_LENGTH)) &&
23797c478bd9Sstevel@tonic-gate *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
23807c478bd9Sstevel@tonic-gate tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4));
23817c478bd9Sstevel@tonic-gate tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
23827c478bd9Sstevel@tonic-gate
23837c478bd9Sstevel@tonic-gate options = TCP_OPT_TSTAMP_PRESENT;
23847c478bd9Sstevel@tonic-gate } else {
23857c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
23867c478bd9Sstevel@tonic-gate tcpoptp->tcp = tcp;
23877c478bd9Sstevel@tonic-gate } else {
23887c478bd9Sstevel@tonic-gate tcpoptp->tcp = NULL;
23897c478bd9Sstevel@tonic-gate }
23907c478bd9Sstevel@tonic-gate options = tcp_parse_options(tcph, tcpoptp);
23917c478bd9Sstevel@tonic-gate }
23927c478bd9Sstevel@tonic-gate
23937c478bd9Sstevel@tonic-gate if (options & TCP_OPT_TSTAMP_PRESENT) {
23947c478bd9Sstevel@tonic-gate /*
23957c478bd9Sstevel@tonic-gate * Do PAWS per RFC 1323 section 4.2. Accept RST
23967c478bd9Sstevel@tonic-gate * regardless of the timestamp, page 18 RFC 1323.bis.
23977c478bd9Sstevel@tonic-gate */
23987c478bd9Sstevel@tonic-gate if ((flags & TH_RST) == 0 &&
23997c478bd9Sstevel@tonic-gate TSTMP_LT(tcpoptp->tcp_opt_ts_val,
24007c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent)) {
24017c478bd9Sstevel@tonic-gate if (TSTMP_LT(prom_gettime(),
24027c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
24037c478bd9Sstevel@tonic-gate /* This segment is not acceptable. */
24047c478bd9Sstevel@tonic-gate return (B_FALSE);
24057c478bd9Sstevel@tonic-gate } else {
24067c478bd9Sstevel@tonic-gate /*
24077c478bd9Sstevel@tonic-gate * Connection has been idle for
24087c478bd9Sstevel@tonic-gate * too long. Reset the timestamp
24097c478bd9Sstevel@tonic-gate * and assume the segment is valid.
24107c478bd9Sstevel@tonic-gate */
24117c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent =
24127c478bd9Sstevel@tonic-gate tcpoptp->tcp_opt_ts_val;
24137c478bd9Sstevel@tonic-gate }
24147c478bd9Sstevel@tonic-gate }
24157c478bd9Sstevel@tonic-gate } else {
24167c478bd9Sstevel@tonic-gate /*
24177c478bd9Sstevel@tonic-gate * If we don't get a timestamp on every packet, we
24187c478bd9Sstevel@tonic-gate * figure we can't really trust 'em, so we stop sending
24197c478bd9Sstevel@tonic-gate * and parsing them.
24207c478bd9Sstevel@tonic-gate */
24217c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_FALSE;
24227c478bd9Sstevel@tonic-gate
24237c478bd9Sstevel@tonic-gate tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN;
24247c478bd9Sstevel@tonic-gate tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN;
24257c478bd9Sstevel@tonic-gate tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4);
24267c478bd9Sstevel@tonic-gate tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN);
24277c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
24287c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
24297c478bd9Sstevel@tonic-gate tcp->tcp_max_sack_blk = 4;
24307c478bd9Sstevel@tonic-gate }
24317c478bd9Sstevel@tonic-gate }
24327c478bd9Sstevel@tonic-gate return (B_TRUE);
24337c478bd9Sstevel@tonic-gate }
24347c478bd9Sstevel@tonic-gate
24357c478bd9Sstevel@tonic-gate /*
24367c478bd9Sstevel@tonic-gate * tcp_get_seg_mp() is called to get the pointer to a segment in the
24377c478bd9Sstevel@tonic-gate * send queue which starts at the given seq. no.
24387c478bd9Sstevel@tonic-gate *
24397c478bd9Sstevel@tonic-gate * Parameters:
24407c478bd9Sstevel@tonic-gate * tcp_t *tcp: the tcp instance pointer.
24417c478bd9Sstevel@tonic-gate * uint32_t seq: the starting seq. no of the requested segment.
24427c478bd9Sstevel@tonic-gate * int32_t *off: after the execution, *off will be the offset to
24437c478bd9Sstevel@tonic-gate * the returned mblk which points to the requested seq no.
24447c478bd9Sstevel@tonic-gate *
24457c478bd9Sstevel@tonic-gate * Return:
24467c478bd9Sstevel@tonic-gate * A mblk_t pointer pointing to the requested segment in send queue.
24477c478bd9Sstevel@tonic-gate */
24487c478bd9Sstevel@tonic-gate static mblk_t *
tcp_get_seg_mp(tcp_t * tcp,uint32_t seq,int32_t * off)24497c478bd9Sstevel@tonic-gate tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off)
24507c478bd9Sstevel@tonic-gate {
24517c478bd9Sstevel@tonic-gate int32_t cnt;
24527c478bd9Sstevel@tonic-gate mblk_t *mp;
24537c478bd9Sstevel@tonic-gate
24547c478bd9Sstevel@tonic-gate /* Defensive coding. Make sure we don't send incorrect data. */
24557c478bd9Sstevel@tonic-gate if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt) ||
24567c478bd9Sstevel@tonic-gate off == NULL) {
24577c478bd9Sstevel@tonic-gate return (NULL);
24587c478bd9Sstevel@tonic-gate }
24597c478bd9Sstevel@tonic-gate cnt = seq - tcp->tcp_suna;
24607c478bd9Sstevel@tonic-gate mp = tcp->tcp_xmit_head;
24617c478bd9Sstevel@tonic-gate while (cnt > 0 && mp) {
24627c478bd9Sstevel@tonic-gate cnt -= mp->b_wptr - mp->b_rptr;
24637c478bd9Sstevel@tonic-gate if (cnt < 0) {
24647c478bd9Sstevel@tonic-gate cnt += mp->b_wptr - mp->b_rptr;
24657c478bd9Sstevel@tonic-gate break;
24667c478bd9Sstevel@tonic-gate }
24677c478bd9Sstevel@tonic-gate mp = mp->b_cont;
24687c478bd9Sstevel@tonic-gate }
24697c478bd9Sstevel@tonic-gate assert(mp != NULL);
24707c478bd9Sstevel@tonic-gate *off = cnt;
24717c478bd9Sstevel@tonic-gate return (mp);
24727c478bd9Sstevel@tonic-gate }
24737c478bd9Sstevel@tonic-gate
24747c478bd9Sstevel@tonic-gate /*
24757c478bd9Sstevel@tonic-gate * This function handles all retransmissions if SACK is enabled for this
24767c478bd9Sstevel@tonic-gate * connection. First it calculates how many segments can be retransmitted
24777c478bd9Sstevel@tonic-gate * based on tcp_pipe. Then it goes thru the notsack list to find eligible
24787c478bd9Sstevel@tonic-gate * segments. A segment is eligible if sack_cnt for that segment is greater
24797c478bd9Sstevel@tonic-gate * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
24807c478bd9Sstevel@tonic-gate * all eligible segments, it checks to see if TCP can send some new segments
24817c478bd9Sstevel@tonic-gate * (fast recovery). If it can, it returns 1. Otherwise it returns 0.
24827c478bd9Sstevel@tonic-gate *
24837c478bd9Sstevel@tonic-gate * Parameters:
24847c478bd9Sstevel@tonic-gate * tcp_t *tcp: the tcp structure of the connection.
24857c478bd9Sstevel@tonic-gate *
24867c478bd9Sstevel@tonic-gate * Return:
24877c478bd9Sstevel@tonic-gate * 1 if the pipe is not full (new data can be sent), 0 otherwise
24887c478bd9Sstevel@tonic-gate */
24897c478bd9Sstevel@tonic-gate static int32_t
tcp_sack_rxmit(tcp_t * tcp,int sock_id)24907c478bd9Sstevel@tonic-gate tcp_sack_rxmit(tcp_t *tcp, int sock_id)
24917c478bd9Sstevel@tonic-gate {
24927c478bd9Sstevel@tonic-gate notsack_blk_t *notsack_blk;
24937c478bd9Sstevel@tonic-gate int32_t usable_swnd;
24947c478bd9Sstevel@tonic-gate int32_t mss;
24957c478bd9Sstevel@tonic-gate uint32_t seg_len;
24967c478bd9Sstevel@tonic-gate mblk_t *xmit_mp;
24977c478bd9Sstevel@tonic-gate
24987c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
24997c478bd9Sstevel@tonic-gate assert(tcp->tcp_notsack_list != NULL);
25007c478bd9Sstevel@tonic-gate assert(tcp->tcp_rexmit == B_FALSE);
25017c478bd9Sstevel@tonic-gate
25027c478bd9Sstevel@tonic-gate /* Defensive coding in case there is a bug... */
25037c478bd9Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
25047c478bd9Sstevel@tonic-gate return (0);
25057c478bd9Sstevel@tonic-gate }
25067c478bd9Sstevel@tonic-gate notsack_blk = tcp->tcp_notsack_list;
25077c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss;
25087c478bd9Sstevel@tonic-gate
25097c478bd9Sstevel@tonic-gate /*
25107c478bd9Sstevel@tonic-gate * Limit the num of outstanding data in the network to be
25117c478bd9Sstevel@tonic-gate * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
25127c478bd9Sstevel@tonic-gate */
25137c478bd9Sstevel@tonic-gate usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
25147c478bd9Sstevel@tonic-gate
25157c478bd9Sstevel@tonic-gate /* At least retransmit 1 MSS of data. */
25167c478bd9Sstevel@tonic-gate if (usable_swnd <= 0) {
25177c478bd9Sstevel@tonic-gate usable_swnd = mss;
25187c478bd9Sstevel@tonic-gate }
25197c478bd9Sstevel@tonic-gate
25207c478bd9Sstevel@tonic-gate /* Make sure no new RTT samples will be taken. */
25217c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
25227c478bd9Sstevel@tonic-gate
25237c478bd9Sstevel@tonic-gate notsack_blk = tcp->tcp_notsack_list;
25247c478bd9Sstevel@tonic-gate while (usable_swnd > 0) {
25257c478bd9Sstevel@tonic-gate mblk_t *snxt_mp, *tmp_mp;
25267c478bd9Sstevel@tonic-gate tcp_seq begin = tcp->tcp_sack_snxt;
25277c478bd9Sstevel@tonic-gate tcp_seq end;
25287c478bd9Sstevel@tonic-gate int32_t off;
25297c478bd9Sstevel@tonic-gate
25307c478bd9Sstevel@tonic-gate for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) {
25317c478bd9Sstevel@tonic-gate if (SEQ_GT(notsack_blk->end, begin) &&
25327c478bd9Sstevel@tonic-gate (notsack_blk->sack_cnt >=
25337c478bd9Sstevel@tonic-gate tcp_dupack_fast_retransmit)) {
25347c478bd9Sstevel@tonic-gate end = notsack_blk->end;
25357c478bd9Sstevel@tonic-gate if (SEQ_LT(begin, notsack_blk->begin)) {
25367c478bd9Sstevel@tonic-gate begin = notsack_blk->begin;
25377c478bd9Sstevel@tonic-gate }
25387c478bd9Sstevel@tonic-gate break;
25397c478bd9Sstevel@tonic-gate }
25407c478bd9Sstevel@tonic-gate }
25417c478bd9Sstevel@tonic-gate /*
25427c478bd9Sstevel@tonic-gate * All holes are filled. Manipulate tcp_cwnd to send more
25437c478bd9Sstevel@tonic-gate * if we can. Note that after the SACK recovery, tcp_cwnd is
25447c478bd9Sstevel@tonic-gate * set to tcp_cwnd_ssthresh.
25457c478bd9Sstevel@tonic-gate */
25467c478bd9Sstevel@tonic-gate if (notsack_blk == NULL) {
25477c478bd9Sstevel@tonic-gate usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
25487c478bd9Sstevel@tonic-gate if (usable_swnd <= 0) {
25497c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna;
25507c478bd9Sstevel@tonic-gate assert(tcp->tcp_cwnd > 0);
25517c478bd9Sstevel@tonic-gate return (0);
25527c478bd9Sstevel@tonic-gate } else {
25537c478bd9Sstevel@tonic-gate usable_swnd = usable_swnd / mss;
25547c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna +
25557c478bd9Sstevel@tonic-gate MAX(usable_swnd * mss, mss);
25567c478bd9Sstevel@tonic-gate return (1);
25577c478bd9Sstevel@tonic-gate }
25587c478bd9Sstevel@tonic-gate }
25597c478bd9Sstevel@tonic-gate
25607c478bd9Sstevel@tonic-gate /*
25617c478bd9Sstevel@tonic-gate * Note that we may send more than usable_swnd allows here
25627c478bd9Sstevel@tonic-gate * because of round off, but no more than 1 MSS of data.
25637c478bd9Sstevel@tonic-gate */
25647c478bd9Sstevel@tonic-gate seg_len = end - begin;
25657c478bd9Sstevel@tonic-gate if (seg_len > mss)
25667c478bd9Sstevel@tonic-gate seg_len = mss;
25677c478bd9Sstevel@tonic-gate snxt_mp = tcp_get_seg_mp(tcp, begin, &off);
25687c478bd9Sstevel@tonic-gate assert(snxt_mp != NULL);
25697c478bd9Sstevel@tonic-gate /* This should not happen. Defensive coding again... */
25707c478bd9Sstevel@tonic-gate if (snxt_mp == NULL) {
25717c478bd9Sstevel@tonic-gate return (0);
25727c478bd9Sstevel@tonic-gate }
25737c478bd9Sstevel@tonic-gate
25747c478bd9Sstevel@tonic-gate xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off,
25757c478bd9Sstevel@tonic-gate &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE);
25767c478bd9Sstevel@tonic-gate
25777c478bd9Sstevel@tonic-gate if (xmit_mp == NULL)
25787c478bd9Sstevel@tonic-gate return (0);
25797c478bd9Sstevel@tonic-gate
25807c478bd9Sstevel@tonic-gate usable_swnd -= seg_len;
25817c478bd9Sstevel@tonic-gate tcp->tcp_pipe += seg_len;
25827c478bd9Sstevel@tonic-gate tcp->tcp_sack_snxt = begin + seg_len;
25837c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_sack_rxmit", xmit_mp);
25847c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, xmit_mp);
25857c478bd9Sstevel@tonic-gate freeb(xmit_mp);
25867c478bd9Sstevel@tonic-gate
25877c478bd9Sstevel@tonic-gate /*
25887c478bd9Sstevel@tonic-gate * Update the send timestamp to avoid false retransmission.
258953391bafSeota * Note. use uintptr_t to suppress the gcc warning.
25907c478bd9Sstevel@tonic-gate */
259153391bafSeota snxt_mp->b_prev = (mblk_t *)(uintptr_t)prom_gettime();
25927c478bd9Sstevel@tonic-gate
25937c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
25947c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, seg_len);
25957c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutSackRetransSegs);
25967c478bd9Sstevel@tonic-gate /*
25977c478bd9Sstevel@tonic-gate * Update tcp_rexmit_max to extend this SACK recovery phase.
25987c478bd9Sstevel@tonic-gate * This happens when new data sent during fast recovery is
25997c478bd9Sstevel@tonic-gate * also lost. If TCP retransmits those new data, it needs
26007c478bd9Sstevel@tonic-gate * to extend SACK recover phase to avoid starting another
26017c478bd9Sstevel@tonic-gate * fast retransmit/recovery unnecessarily.
26027c478bd9Sstevel@tonic-gate */
26037c478bd9Sstevel@tonic-gate if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) {
26047c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_sack_snxt;
26057c478bd9Sstevel@tonic-gate }
26067c478bd9Sstevel@tonic-gate }
26077c478bd9Sstevel@tonic-gate return (0);
26087c478bd9Sstevel@tonic-gate }
26097c478bd9Sstevel@tonic-gate
26107c478bd9Sstevel@tonic-gate static void
tcp_rput_data(tcp_t * tcp,mblk_t * mp,int sock_id)26117c478bd9Sstevel@tonic-gate tcp_rput_data(tcp_t *tcp, mblk_t *mp, int sock_id)
26127c478bd9Sstevel@tonic-gate {
26137c478bd9Sstevel@tonic-gate uchar_t *rptr;
26147c478bd9Sstevel@tonic-gate struct ip *iph;
26157c478bd9Sstevel@tonic-gate tcp_t *tcp1;
26167c478bd9Sstevel@tonic-gate tcpha_t *tcph;
26177c478bd9Sstevel@tonic-gate uint32_t seg_ack;
26187c478bd9Sstevel@tonic-gate int seg_len;
26197c478bd9Sstevel@tonic-gate uint_t ip_hdr_len;
26207c478bd9Sstevel@tonic-gate uint32_t seg_seq;
26217c478bd9Sstevel@tonic-gate mblk_t *mp1;
26227c478bd9Sstevel@tonic-gate uint_t flags;
26237c478bd9Sstevel@tonic-gate uint32_t new_swnd = 0;
26247c478bd9Sstevel@tonic-gate int mss;
26257c478bd9Sstevel@tonic-gate boolean_t ofo_seg = B_FALSE; /* Out of order segment */
26267c478bd9Sstevel@tonic-gate int32_t gap;
26277c478bd9Sstevel@tonic-gate int32_t rgap;
26287c478bd9Sstevel@tonic-gate tcp_opt_t tcpopt;
26297c478bd9Sstevel@tonic-gate int32_t bytes_acked;
26307c478bd9Sstevel@tonic-gate int npkt;
26317c478bd9Sstevel@tonic-gate uint32_t cwnd;
26327c478bd9Sstevel@tonic-gate uint32_t add;
26337c478bd9Sstevel@tonic-gate
26347c478bd9Sstevel@tonic-gate #ifdef DEBUG
26357c478bd9Sstevel@tonic-gate printf("tcp_rput_data sock %d mp %x mp_datap %x #################\n",
26367c478bd9Sstevel@tonic-gate sock_id, mp, mp->b_datap);
26377c478bd9Sstevel@tonic-gate #endif
26387c478bd9Sstevel@tonic-gate
26397c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
26407c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data", mp);
26417c478bd9Sstevel@tonic-gate
26427c478bd9Sstevel@tonic-gate assert(OK_32PTR(mp->b_rptr));
26437c478bd9Sstevel@tonic-gate
26447c478bd9Sstevel@tonic-gate rptr = mp->b_rptr;
26457c478bd9Sstevel@tonic-gate iph = (struct ip *)rptr;
26467c478bd9Sstevel@tonic-gate ip_hdr_len = IPH_HDR_LENGTH(rptr);
26477c478bd9Sstevel@tonic-gate if (ip_hdr_len != IP_SIMPLE_HDR_LENGTH) {
26487c478bd9Sstevel@tonic-gate #ifdef DEBUG
26497c478bd9Sstevel@tonic-gate printf("Not simple IP header\n");
26507c478bd9Sstevel@tonic-gate #endif
26517c478bd9Sstevel@tonic-gate /* We cannot handle IP option yet... */
26527c478bd9Sstevel@tonic-gate tcp_drops++;
26537c478bd9Sstevel@tonic-gate freeb(mp);
26547c478bd9Sstevel@tonic-gate return;
26557c478bd9Sstevel@tonic-gate }
26567c478bd9Sstevel@tonic-gate /* The TCP header must be aligned. */
26577c478bd9Sstevel@tonic-gate tcph = (tcpha_t *)&rptr[ip_hdr_len];
26587c478bd9Sstevel@tonic-gate seg_seq = ntohl(tcph->tha_seq);
26597c478bd9Sstevel@tonic-gate seg_ack = ntohl(tcph->tha_ack);
26607c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
26617c478bd9Sstevel@tonic-gate seg_len = (int)(mp->b_wptr - rptr) -
26627c478bd9Sstevel@tonic-gate (ip_hdr_len + TCP_HDR_LENGTH(((tcph_t *)tcph)));
26637c478bd9Sstevel@tonic-gate /* In inetboot, b_cont should always be NULL. */
26647c478bd9Sstevel@tonic-gate assert(mp->b_cont == NULL);
26657c478bd9Sstevel@tonic-gate
26667c478bd9Sstevel@tonic-gate /* Verify the checksum. */
26677c478bd9Sstevel@tonic-gate if (tcp_verify_cksum(mp) < 0) {
26687c478bd9Sstevel@tonic-gate #ifdef DEBUG
26697c478bd9Sstevel@tonic-gate printf("tcp_rput_data: wrong cksum\n");
26707c478bd9Sstevel@tonic-gate #endif
26717c478bd9Sstevel@tonic-gate freemsg(mp);
26727c478bd9Sstevel@tonic-gate return;
26737c478bd9Sstevel@tonic-gate }
26747c478bd9Sstevel@tonic-gate
26757c478bd9Sstevel@tonic-gate /*
26767c478bd9Sstevel@tonic-gate * This segment is not for us, try to find its
26777c478bd9Sstevel@tonic-gate * intended receiver.
26787c478bd9Sstevel@tonic-gate */
26797c478bd9Sstevel@tonic-gate if (tcp == NULL ||
26807c478bd9Sstevel@tonic-gate tcph->tha_lport != tcp->tcp_fport ||
26817c478bd9Sstevel@tonic-gate tcph->tha_fport != tcp->tcp_lport ||
26827c478bd9Sstevel@tonic-gate iph->ip_src.s_addr != tcp->tcp_remote ||
26837c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr != tcp->tcp_bound_source) {
26847c478bd9Sstevel@tonic-gate #ifdef DEBUG
26857c478bd9Sstevel@tonic-gate printf("tcp_rput_data: not for us, state %d\n",
26867c478bd9Sstevel@tonic-gate tcp->tcp_state);
26877c478bd9Sstevel@tonic-gate #endif
26887c478bd9Sstevel@tonic-gate /*
26897c478bd9Sstevel@tonic-gate * First try to find a established connection. If none
26907c478bd9Sstevel@tonic-gate * is found, look for a listener.
26917c478bd9Sstevel@tonic-gate *
26927c478bd9Sstevel@tonic-gate * If a listener is found, we need to check to see if the
26937c478bd9Sstevel@tonic-gate * incoming segment is for one of its eagers. If it is,
26947c478bd9Sstevel@tonic-gate * give it to the eager. If not, listener should take care
26957c478bd9Sstevel@tonic-gate * of it.
26967c478bd9Sstevel@tonic-gate */
26977c478bd9Sstevel@tonic-gate if ((tcp1 = tcp_lookup_ipv4(iph, tcph, TCPS_SYN_SENT,
26987c478bd9Sstevel@tonic-gate &sock_id)) != NULL ||
26997c478bd9Sstevel@tonic-gate (tcp1 = tcp_lookup_listener_ipv4(iph->ip_dst.s_addr,
27007c478bd9Sstevel@tonic-gate tcph->tha_fport, &sock_id)) != NULL) {
27017c478bd9Sstevel@tonic-gate if (tcp1->tcp_state == TCPS_LISTEN) {
27027c478bd9Sstevel@tonic-gate if ((tcp = tcp_lookup_eager_ipv4(tcp1,
27037c478bd9Sstevel@tonic-gate iph, tcph)) == NULL) {
27047c478bd9Sstevel@tonic-gate /* No eager... sent to listener */
27057c478bd9Sstevel@tonic-gate #ifdef DEBUG
27067c478bd9Sstevel@tonic-gate printf("found the listener: %s\n",
27077c478bd9Sstevel@tonic-gate tcp_display(tcp1, NULL,
27087c478bd9Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27097c478bd9Sstevel@tonic-gate #endif
27107c478bd9Sstevel@tonic-gate tcp = tcp1;
27117c478bd9Sstevel@tonic-gate }
27127c478bd9Sstevel@tonic-gate #ifdef DEBUG
27137c478bd9Sstevel@tonic-gate else {
27147c478bd9Sstevel@tonic-gate printf("found the eager: %s\n",
27157c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL,
27167c478bd9Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27177c478bd9Sstevel@tonic-gate }
27187c478bd9Sstevel@tonic-gate #endif
27197c478bd9Sstevel@tonic-gate } else {
27207c478bd9Sstevel@tonic-gate /* Non listener found... */
27217c478bd9Sstevel@tonic-gate #ifdef DEBUG
27227c478bd9Sstevel@tonic-gate printf("found the connection: %s\n",
27237c478bd9Sstevel@tonic-gate tcp_display(tcp1, NULL,
27247c478bd9Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27257c478bd9Sstevel@tonic-gate #endif
27267c478bd9Sstevel@tonic-gate tcp = tcp1;
27277c478bd9Sstevel@tonic-gate }
27287c478bd9Sstevel@tonic-gate } else {
27297c478bd9Sstevel@tonic-gate /*
27307c478bd9Sstevel@tonic-gate * No connection for this segment...
27317c478bd9Sstevel@tonic-gate * Send a RST to the other side.
27327c478bd9Sstevel@tonic-gate */
27337c478bd9Sstevel@tonic-gate tcp_xmit_listeners_reset(sock_id, mp, ip_hdr_len);
27347c478bd9Sstevel@tonic-gate return;
27357c478bd9Sstevel@tonic-gate }
27367c478bd9Sstevel@tonic-gate }
27377c478bd9Sstevel@tonic-gate
27387c478bd9Sstevel@tonic-gate flags = tcph->tha_flags & 0xFF;
27397c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInSegs);
27407c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT) {
27417c478bd9Sstevel@tonic-gate tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack,
27427c478bd9Sstevel@tonic-gate seg_len, (tcph_t *)tcph, sock_id);
27437c478bd9Sstevel@tonic-gate return;
27447c478bd9Sstevel@tonic-gate }
27457c478bd9Sstevel@tonic-gate /*
27467c478bd9Sstevel@tonic-gate * From this point we can assume that the tcp is not compressed,
27477c478bd9Sstevel@tonic-gate * since we would have branched off to tcp_time_wait_processing()
27487c478bd9Sstevel@tonic-gate * in such a case.
27497c478bd9Sstevel@tonic-gate */
27507c478bd9Sstevel@tonic-gate assert(tcp != NULL && tcp->tcp_state != TCPS_TIME_WAIT);
27517c478bd9Sstevel@tonic-gate
27527c478bd9Sstevel@tonic-gate /*
27537c478bd9Sstevel@tonic-gate * After this point, we know we have the correct TCP, so update
27547c478bd9Sstevel@tonic-gate * the receive time.
27557c478bd9Sstevel@tonic-gate */
27567c478bd9Sstevel@tonic-gate tcp->tcp_last_recv_time = prom_gettime();
27577c478bd9Sstevel@tonic-gate
27587c478bd9Sstevel@tonic-gate /* In inetboot, we do not handle urgent pointer... */
27597c478bd9Sstevel@tonic-gate if (flags & TH_URG) {
27607c478bd9Sstevel@tonic-gate freemsg(mp);
27617c478bd9Sstevel@tonic-gate DEBUG_1("tcp_rput_data(%d): received segment with urgent "
27627c478bd9Sstevel@tonic-gate "pointer\n", sock_id);
27637c478bd9Sstevel@tonic-gate tcp_drops++;
27647c478bd9Sstevel@tonic-gate return;
27657c478bd9Sstevel@tonic-gate }
27667c478bd9Sstevel@tonic-gate
27677c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
27687c478bd9Sstevel@tonic-gate case TCPS_LISTEN:
27697c478bd9Sstevel@tonic-gate if ((flags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) {
27707c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
27717c478bd9Sstevel@tonic-gate freemsg(mp);
27727c478bd9Sstevel@tonic-gate return;
27737c478bd9Sstevel@tonic-gate }
27747c478bd9Sstevel@tonic-gate if (flags & TH_ACK) {
27757c478bd9Sstevel@tonic-gate tcp_xmit_early_reset("TCPS_LISTEN-TH_ACK",
27767c478bd9Sstevel@tonic-gate sock_id, mp, seg_ack, 0, TH_RST,
27777c478bd9Sstevel@tonic-gate ip_hdr_len);
27787c478bd9Sstevel@tonic-gate return;
27797c478bd9Sstevel@tonic-gate }
27807c478bd9Sstevel@tonic-gate if (!(flags & TH_SYN)) {
27817c478bd9Sstevel@tonic-gate freemsg(mp);
27827c478bd9Sstevel@tonic-gate return;
27837c478bd9Sstevel@tonic-gate }
27847c478bd9Sstevel@tonic-gate printf("tcp_rput_data: %d\n", __LINE__);
27857c478bd9Sstevel@tonic-gate prom_panic("inetboot");
27867c478bd9Sstevel@tonic-gate }
27877c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_max > 0) {
27887c478bd9Sstevel@tonic-gate tcp = tcp_conn_request(tcp, mp, sock_id, ip_hdr_len);
27897c478bd9Sstevel@tonic-gate if (tcp == NULL) {
27907c478bd9Sstevel@tonic-gate freemsg(mp);
27917c478bd9Sstevel@tonic-gate return;
27927c478bd9Sstevel@tonic-gate }
27937c478bd9Sstevel@tonic-gate #ifdef DEBUG
27947c478bd9Sstevel@tonic-gate printf("tcp_rput_data: new tcp created\n");
27957c478bd9Sstevel@tonic-gate #endif
27967c478bd9Sstevel@tonic-gate }
27977c478bd9Sstevel@tonic-gate tcp->tcp_irs = seg_seq;
27987c478bd9Sstevel@tonic-gate tcp->tcp_rack = seg_seq;
27997c478bd9Sstevel@tonic-gate tcp->tcp_rnxt = seg_seq + 1;
28007c478bd9Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
28017c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpPassiveOpens);
28027c478bd9Sstevel@tonic-gate goto syn_rcvd;
28037c478bd9Sstevel@tonic-gate case TCPS_SYN_SENT:
28047c478bd9Sstevel@tonic-gate if (flags & TH_ACK) {
28057c478bd9Sstevel@tonic-gate /*
28067c478bd9Sstevel@tonic-gate * Note that our stack cannot send data before a
28077c478bd9Sstevel@tonic-gate * connection is established, therefore the
28087c478bd9Sstevel@tonic-gate * following check is valid. Otherwise, it has
28097c478bd9Sstevel@tonic-gate * to be changed.
28107c478bd9Sstevel@tonic-gate */
28117c478bd9Sstevel@tonic-gate if (SEQ_LEQ(seg_ack, tcp->tcp_iss) ||
28127c478bd9Sstevel@tonic-gate SEQ_GT(seg_ack, tcp->tcp_snxt)) {
28137c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
28147c478bd9Sstevel@tonic-gate freemsg(mp);
28157c478bd9Sstevel@tonic-gate return;
28167c478bd9Sstevel@tonic-gate }
28177c478bd9Sstevel@tonic-gate tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
28187c478bd9Sstevel@tonic-gate tcp, mp, seg_ack, 0, TH_RST,
28197c478bd9Sstevel@tonic-gate ip_hdr_len, sock_id);
28207c478bd9Sstevel@tonic-gate return;
28217c478bd9Sstevel@tonic-gate }
28227c478bd9Sstevel@tonic-gate assert(tcp->tcp_suna + 1 == seg_ack);
28237c478bd9Sstevel@tonic-gate }
28247c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
28257c478bd9Sstevel@tonic-gate freemsg(mp);
28267c478bd9Sstevel@tonic-gate if (flags & TH_ACK) {
28277c478bd9Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, ECONNREFUSED);
28287c478bd9Sstevel@tonic-gate }
28297c478bd9Sstevel@tonic-gate return;
28307c478bd9Sstevel@tonic-gate }
28317c478bd9Sstevel@tonic-gate if (!(flags & TH_SYN)) {
28327c478bd9Sstevel@tonic-gate freemsg(mp);
28337c478bd9Sstevel@tonic-gate return;
28347c478bd9Sstevel@tonic-gate }
28357c478bd9Sstevel@tonic-gate
28367c478bd9Sstevel@tonic-gate /* Process all TCP options. */
28377c478bd9Sstevel@tonic-gate tcp_process_options(tcp, (tcph_t *)tcph);
28387c478bd9Sstevel@tonic-gate /*
28397c478bd9Sstevel@tonic-gate * The following changes our rwnd to be a multiple of the
28407c478bd9Sstevel@tonic-gate * MIN(peer MSS, our MSS) for performance reason.
28417c478bd9Sstevel@tonic-gate */
28427c478bd9Sstevel@tonic-gate (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rwnd,
28437c478bd9Sstevel@tonic-gate tcp->tcp_mss));
28447c478bd9Sstevel@tonic-gate
28457c478bd9Sstevel@tonic-gate /* Is the other end ECN capable? */
28467c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
28477c478bd9Sstevel@tonic-gate if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) {
28487c478bd9Sstevel@tonic-gate tcp->tcp_ecn_ok = B_FALSE;
28497c478bd9Sstevel@tonic-gate }
28507c478bd9Sstevel@tonic-gate }
28517c478bd9Sstevel@tonic-gate /*
28527c478bd9Sstevel@tonic-gate * Clear ECN flags because it may interfere with later
28537c478bd9Sstevel@tonic-gate * processing.
28547c478bd9Sstevel@tonic-gate */
28557c478bd9Sstevel@tonic-gate flags &= ~(TH_ECE|TH_CWR);
28567c478bd9Sstevel@tonic-gate
28577c478bd9Sstevel@tonic-gate tcp->tcp_irs = seg_seq;
28587c478bd9Sstevel@tonic-gate tcp->tcp_rack = seg_seq;
28597c478bd9Sstevel@tonic-gate tcp->tcp_rnxt = seg_seq + 1;
28607c478bd9Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
28617c478bd9Sstevel@tonic-gate
28627c478bd9Sstevel@tonic-gate if (flags & TH_ACK) {
28637c478bd9Sstevel@tonic-gate /* One for the SYN */
28647c478bd9Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss + 1;
28657c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
28667c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_ESTABLISHED;
28677c478bd9Sstevel@tonic-gate
28687c478bd9Sstevel@tonic-gate /*
28697c478bd9Sstevel@tonic-gate * If SYN was retransmitted, need to reset all
28707c478bd9Sstevel@tonic-gate * retransmission info. This is because this
28717c478bd9Sstevel@tonic-gate * segment will be treated as a dup ACK.
28727c478bd9Sstevel@tonic-gate */
28737c478bd9Sstevel@tonic-gate if (tcp->tcp_rexmit) {
28747c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
28757c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
28767c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
28777c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
28787c478bd9Sstevel@tonic-gate
28797c478bd9Sstevel@tonic-gate /*
28807c478bd9Sstevel@tonic-gate * Set tcp_cwnd back to 1 MSS, per
28817c478bd9Sstevel@tonic-gate * recommendation from
28827c478bd9Sstevel@tonic-gate * draft-floyd-incr-init-win-01.txt,
28837c478bd9Sstevel@tonic-gate * Increasing TCP's Initial Window.
28847c478bd9Sstevel@tonic-gate */
28857c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
28867c478bd9Sstevel@tonic-gate }
28877c478bd9Sstevel@tonic-gate
28887c478bd9Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
28897c478bd9Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
28907c478bd9Sstevel@tonic-gate
28917c478bd9Sstevel@tonic-gate new_swnd = BE16_TO_U16(((tcph_t *)tcph)->th_win);
28927c478bd9Sstevel@tonic-gate tcp->tcp_swnd = new_swnd;
28937c478bd9Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
28947c478bd9Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
28957c478bd9Sstevel@tonic-gate
28967c478bd9Sstevel@tonic-gate /*
28977c478bd9Sstevel@tonic-gate * Always send the three-way handshake ack immediately
28987c478bd9Sstevel@tonic-gate * in order to make the connection complete as soon as
28997c478bd9Sstevel@tonic-gate * possible on the accepting host.
29007c478bd9Sstevel@tonic-gate */
29017c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
29027c478bd9Sstevel@tonic-gate /*
29037c478bd9Sstevel@tonic-gate * Check to see if there is data to be sent. If
29047c478bd9Sstevel@tonic-gate * yes, set the transmit flag. Then check to see
29057c478bd9Sstevel@tonic-gate * if received data processing needs to be done.
29067c478bd9Sstevel@tonic-gate * If not, go straight to xmit_check. This short
29077c478bd9Sstevel@tonic-gate * cut is OK as we don't support T/TCP.
29087c478bd9Sstevel@tonic-gate */
29097c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent)
29107c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
29117c478bd9Sstevel@tonic-gate
29127c478bd9Sstevel@tonic-gate if (seg_len == 0) {
29137c478bd9Sstevel@tonic-gate freemsg(mp);
29147c478bd9Sstevel@tonic-gate goto xmit_check;
29157c478bd9Sstevel@tonic-gate }
29167c478bd9Sstevel@tonic-gate
29177c478bd9Sstevel@tonic-gate flags &= ~TH_SYN;
29187c478bd9Sstevel@tonic-gate seg_seq++;
29197c478bd9Sstevel@tonic-gate break;
29207c478bd9Sstevel@tonic-gate }
29217c478bd9Sstevel@tonic-gate syn_rcvd:
29227c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_SYN_RCVD;
29237c478bd9Sstevel@tonic-gate mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss,
29247c478bd9Sstevel@tonic-gate NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
29257c478bd9Sstevel@tonic-gate if (mp1 != NULL) {
29267c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data replying SYN", mp1);
29277c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
29287c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
29297c478bd9Sstevel@tonic-gate freeb(mp1);
29307c478bd9Sstevel@tonic-gate /*
29317c478bd9Sstevel@tonic-gate * Let's wait till our SYN has been ACKED since we
29327c478bd9Sstevel@tonic-gate * don't have a timer.
29337c478bd9Sstevel@tonic-gate */
29347c478bd9Sstevel@tonic-gate if (tcp_state_wait(sock_id, tcp, TCPS_ALL_ACKED) < 0) {
29357c478bd9Sstevel@tonic-gate freemsg(mp);
29367c478bd9Sstevel@tonic-gate return;
29377c478bd9Sstevel@tonic-gate }
29387c478bd9Sstevel@tonic-gate }
29397c478bd9Sstevel@tonic-gate freemsg(mp);
29407c478bd9Sstevel@tonic-gate return;
29417c478bd9Sstevel@tonic-gate default:
29427c478bd9Sstevel@tonic-gate break;
29437c478bd9Sstevel@tonic-gate }
29447c478bd9Sstevel@tonic-gate mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH((tcph_t *)tcph);
29457c478bd9Sstevel@tonic-gate new_swnd = ntohs(tcph->tha_win) <<
29467c478bd9Sstevel@tonic-gate ((flags & TH_SYN) ? 0 : tcp->tcp_snd_ws);
29477c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss;
29487c478bd9Sstevel@tonic-gate
29497c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
29507c478bd9Sstevel@tonic-gate if (!tcp_paws_check(tcp, (tcph_t *)tcph, &tcpopt)) {
29517c478bd9Sstevel@tonic-gate /*
29527c478bd9Sstevel@tonic-gate * This segment is not acceptable.
29537c478bd9Sstevel@tonic-gate * Drop it and send back an ACK.
29547c478bd9Sstevel@tonic-gate */
29557c478bd9Sstevel@tonic-gate freemsg(mp);
29567c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
29577c478bd9Sstevel@tonic-gate goto ack_check;
29587c478bd9Sstevel@tonic-gate }
29597c478bd9Sstevel@tonic-gate } else if (tcp->tcp_snd_sack_ok) {
29607c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
29617c478bd9Sstevel@tonic-gate tcpopt.tcp = tcp;
29627c478bd9Sstevel@tonic-gate /*
29637c478bd9Sstevel@tonic-gate * SACK info in already updated in tcp_parse_options. Ignore
29647c478bd9Sstevel@tonic-gate * all other TCP options...
29657c478bd9Sstevel@tonic-gate */
29667c478bd9Sstevel@tonic-gate (void) tcp_parse_options((tcph_t *)tcph, &tcpopt);
29677c478bd9Sstevel@tonic-gate }
29687c478bd9Sstevel@tonic-gate try_again:;
29697c478bd9Sstevel@tonic-gate gap = seg_seq - tcp->tcp_rnxt;
29707c478bd9Sstevel@tonic-gate rgap = tcp->tcp_rwnd - (gap + seg_len);
29717c478bd9Sstevel@tonic-gate /*
29727c478bd9Sstevel@tonic-gate * gap is the amount of sequence space between what we expect to see
29737c478bd9Sstevel@tonic-gate * and what we got for seg_seq. A positive value for gap means
29747c478bd9Sstevel@tonic-gate * something got lost. A negative value means we got some old stuff.
29757c478bd9Sstevel@tonic-gate */
29767c478bd9Sstevel@tonic-gate if (gap < 0) {
29777c478bd9Sstevel@tonic-gate /* Old stuff present. Is the SYN in there? */
29787c478bd9Sstevel@tonic-gate if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) &&
29797c478bd9Sstevel@tonic-gate (seg_len != 0)) {
29807c478bd9Sstevel@tonic-gate flags &= ~TH_SYN;
29817c478bd9Sstevel@tonic-gate seg_seq++;
29827c478bd9Sstevel@tonic-gate /* Recompute the gaps after noting the SYN. */
29837c478bd9Sstevel@tonic-gate goto try_again;
29847c478bd9Sstevel@tonic-gate }
29857c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
29867c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes,
29877c478bd9Sstevel@tonic-gate (seg_len > -gap ? -gap : seg_len));
29887c478bd9Sstevel@tonic-gate /* Remove the old stuff from seg_len. */
29897c478bd9Sstevel@tonic-gate seg_len += gap;
29907c478bd9Sstevel@tonic-gate /*
29917c478bd9Sstevel@tonic-gate * Anything left?
29927c478bd9Sstevel@tonic-gate * Make sure to check for unack'd FIN when rest of data
29937c478bd9Sstevel@tonic-gate * has been previously ack'd.
29947c478bd9Sstevel@tonic-gate */
29957c478bd9Sstevel@tonic-gate if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
29967c478bd9Sstevel@tonic-gate /*
29977c478bd9Sstevel@tonic-gate * Resets are only valid if they lie within our offered
29987c478bd9Sstevel@tonic-gate * window. If the RST bit is set, we just ignore this
29997c478bd9Sstevel@tonic-gate * segment.
30007c478bd9Sstevel@tonic-gate */
30017c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
30027c478bd9Sstevel@tonic-gate freemsg(mp);
30037c478bd9Sstevel@tonic-gate return;
30047c478bd9Sstevel@tonic-gate }
30057c478bd9Sstevel@tonic-gate
30067c478bd9Sstevel@tonic-gate /*
30077c478bd9Sstevel@tonic-gate * This segment is "unacceptable". None of its
30087c478bd9Sstevel@tonic-gate * sequence space lies within our advertized window.
30097c478bd9Sstevel@tonic-gate *
30107c478bd9Sstevel@tonic-gate * Adjust seg_len to the original value for tracing.
30117c478bd9Sstevel@tonic-gate */
30127c478bd9Sstevel@tonic-gate seg_len -= gap;
30137c478bd9Sstevel@tonic-gate #ifdef DEBUG
30147c478bd9Sstevel@tonic-gate printf("tcp_rput: unacceptable, gap %d, rgap "
30157c478bd9Sstevel@tonic-gate "%d, flags 0x%x, seg_seq %u, seg_ack %u, "
30167c478bd9Sstevel@tonic-gate "seg_len %d, rnxt %u, snxt %u, %s",
30177c478bd9Sstevel@tonic-gate gap, rgap, flags, seg_seq, seg_ack,
30187c478bd9Sstevel@tonic-gate seg_len, tcp->tcp_rnxt, tcp->tcp_snxt,
30197c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
30207c478bd9Sstevel@tonic-gate #endif
30217c478bd9Sstevel@tonic-gate
30227c478bd9Sstevel@tonic-gate /*
30237c478bd9Sstevel@tonic-gate * Arrange to send an ACK in response to the
30247c478bd9Sstevel@tonic-gate * unacceptable segment per RFC 793 page 69. There
30257c478bd9Sstevel@tonic-gate * is only one small difference between ours and the
30267c478bd9Sstevel@tonic-gate * acceptability test in the RFC - we accept ACK-only
30277c478bd9Sstevel@tonic-gate * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
30287c478bd9Sstevel@tonic-gate * will be generated.
30297c478bd9Sstevel@tonic-gate *
30307c478bd9Sstevel@tonic-gate * Note that we have to ACK an ACK-only packet at least
30317c478bd9Sstevel@tonic-gate * for stacks that send 0-length keep-alives with
30327c478bd9Sstevel@tonic-gate * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
30337c478bd9Sstevel@tonic-gate * section 4.2.3.6. As long as we don't ever generate
30347c478bd9Sstevel@tonic-gate * an unacceptable packet in response to an incoming
30357c478bd9Sstevel@tonic-gate * packet that is unacceptable, it should not cause
30367c478bd9Sstevel@tonic-gate * "ACK wars".
30377c478bd9Sstevel@tonic-gate */
30387c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
30397c478bd9Sstevel@tonic-gate
30407c478bd9Sstevel@tonic-gate /*
30417c478bd9Sstevel@tonic-gate * Continue processing this segment in order to use the
30427c478bd9Sstevel@tonic-gate * ACK information it contains, but skip all other
30437c478bd9Sstevel@tonic-gate * sequence-number processing. Processing the ACK
30447c478bd9Sstevel@tonic-gate * information is necessary in order to
30457c478bd9Sstevel@tonic-gate * re-synchronize connections that may have lost
30467c478bd9Sstevel@tonic-gate * synchronization.
30477c478bd9Sstevel@tonic-gate *
30487c478bd9Sstevel@tonic-gate * We clear seg_len and flag fields related to
30497c478bd9Sstevel@tonic-gate * sequence number processing as they are not
30507c478bd9Sstevel@tonic-gate * to be trusted for an unacceptable segment.
30517c478bd9Sstevel@tonic-gate */
30527c478bd9Sstevel@tonic-gate seg_len = 0;
30537c478bd9Sstevel@tonic-gate flags &= ~(TH_SYN | TH_FIN | TH_URG);
30547c478bd9Sstevel@tonic-gate goto process_ack;
30557c478bd9Sstevel@tonic-gate }
30567c478bd9Sstevel@tonic-gate
30577c478bd9Sstevel@tonic-gate /* Fix seg_seq, and chew the gap off the front. */
30587c478bd9Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
30597c478bd9Sstevel@tonic-gate do {
30607c478bd9Sstevel@tonic-gate mblk_t *mp2;
30617c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
30627c478bd9Sstevel@tonic-gate (uintptr_t)UINT_MAX);
30637c478bd9Sstevel@tonic-gate gap += (uint_t)(mp->b_wptr - mp->b_rptr);
30647c478bd9Sstevel@tonic-gate if (gap > 0) {
30657c478bd9Sstevel@tonic-gate mp->b_rptr = mp->b_wptr - gap;
30667c478bd9Sstevel@tonic-gate break;
30677c478bd9Sstevel@tonic-gate }
30687c478bd9Sstevel@tonic-gate mp2 = mp;
30697c478bd9Sstevel@tonic-gate mp = mp->b_cont;
30707c478bd9Sstevel@tonic-gate freeb(mp2);
30717c478bd9Sstevel@tonic-gate } while (gap < 0);
30727c478bd9Sstevel@tonic-gate }
30737c478bd9Sstevel@tonic-gate /*
30747c478bd9Sstevel@tonic-gate * rgap is the amount of stuff received out of window. A negative
30757c478bd9Sstevel@tonic-gate * value is the amount out of window.
30767c478bd9Sstevel@tonic-gate */
30777c478bd9Sstevel@tonic-gate if (rgap < 0) {
30787c478bd9Sstevel@tonic-gate mblk_t *mp2;
30797c478bd9Sstevel@tonic-gate
30807c478bd9Sstevel@tonic-gate if (tcp->tcp_rwnd == 0)
30817c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInWinProbe);
30827c478bd9Sstevel@tonic-gate else {
30837c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPastWinSegs);
30847c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPastWinBytes, -rgap);
30857c478bd9Sstevel@tonic-gate }
30867c478bd9Sstevel@tonic-gate
30877c478bd9Sstevel@tonic-gate /*
30887c478bd9Sstevel@tonic-gate * seg_len does not include the FIN, so if more than
30897c478bd9Sstevel@tonic-gate * just the FIN is out of window, we act like we don't
30907c478bd9Sstevel@tonic-gate * see it. (If just the FIN is out of window, rgap
30917c478bd9Sstevel@tonic-gate * will be zero and we will go ahead and acknowledge
30927c478bd9Sstevel@tonic-gate * the FIN.)
30937c478bd9Sstevel@tonic-gate */
30947c478bd9Sstevel@tonic-gate flags &= ~TH_FIN;
30957c478bd9Sstevel@tonic-gate
30967c478bd9Sstevel@tonic-gate /* Fix seg_len and make sure there is something left. */
30977c478bd9Sstevel@tonic-gate seg_len += rgap;
30987c478bd9Sstevel@tonic-gate if (seg_len <= 0) {
30997c478bd9Sstevel@tonic-gate /*
31007c478bd9Sstevel@tonic-gate * Resets are only valid if they lie within our offered
31017c478bd9Sstevel@tonic-gate * window. If the RST bit is set, we just ignore this
31027c478bd9Sstevel@tonic-gate * segment.
31037c478bd9Sstevel@tonic-gate */
31047c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
31057c478bd9Sstevel@tonic-gate freemsg(mp);
31067c478bd9Sstevel@tonic-gate return;
31077c478bd9Sstevel@tonic-gate }
31087c478bd9Sstevel@tonic-gate
31097c478bd9Sstevel@tonic-gate /* Per RFC 793, we need to send back an ACK. */
31107c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
31117c478bd9Sstevel@tonic-gate
31127c478bd9Sstevel@tonic-gate /*
31137c478bd9Sstevel@tonic-gate * If this is a zero window probe, continue to
31147c478bd9Sstevel@tonic-gate * process the ACK part. But we need to set seg_len
31157c478bd9Sstevel@tonic-gate * to 0 to avoid data processing. Otherwise just
31167c478bd9Sstevel@tonic-gate * drop the segment and send back an ACK.
31177c478bd9Sstevel@tonic-gate */
31187c478bd9Sstevel@tonic-gate if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) {
31197c478bd9Sstevel@tonic-gate flags &= ~(TH_SYN | TH_URG);
31207c478bd9Sstevel@tonic-gate seg_len = 0;
31217c478bd9Sstevel@tonic-gate /* Let's see if we can update our rwnd */
31227c478bd9Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
31237c478bd9Sstevel@tonic-gate goto process_ack;
31247c478bd9Sstevel@tonic-gate } else {
31257c478bd9Sstevel@tonic-gate freemsg(mp);
31267c478bd9Sstevel@tonic-gate goto ack_check;
31277c478bd9Sstevel@tonic-gate }
31287c478bd9Sstevel@tonic-gate }
31297c478bd9Sstevel@tonic-gate /* Pitch out of window stuff off the end. */
31307c478bd9Sstevel@tonic-gate rgap = seg_len;
31317c478bd9Sstevel@tonic-gate mp2 = mp;
31327c478bd9Sstevel@tonic-gate do {
31337c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp2->b_wptr -
31347c478bd9Sstevel@tonic-gate mp2->b_rptr) <= (uintptr_t)INT_MAX);
31357c478bd9Sstevel@tonic-gate rgap -= (int)(mp2->b_wptr - mp2->b_rptr);
31367c478bd9Sstevel@tonic-gate if (rgap < 0) {
31377c478bd9Sstevel@tonic-gate mp2->b_wptr += rgap;
31387c478bd9Sstevel@tonic-gate if ((mp1 = mp2->b_cont) != NULL) {
31397c478bd9Sstevel@tonic-gate mp2->b_cont = NULL;
31407c478bd9Sstevel@tonic-gate freemsg(mp1);
31417c478bd9Sstevel@tonic-gate }
31427c478bd9Sstevel@tonic-gate break;
31437c478bd9Sstevel@tonic-gate }
31447c478bd9Sstevel@tonic-gate } while ((mp2 = mp2->b_cont) != NULL);
31457c478bd9Sstevel@tonic-gate }
31467c478bd9Sstevel@tonic-gate ok:;
31477c478bd9Sstevel@tonic-gate /*
31487c478bd9Sstevel@tonic-gate * TCP should check ECN info for segments inside the window only.
31497c478bd9Sstevel@tonic-gate * Therefore the check should be done here.
31507c478bd9Sstevel@tonic-gate */
31517c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
31527c478bd9Sstevel@tonic-gate uchar_t tos = ((struct ip *)rptr)->ip_tos;
31537c478bd9Sstevel@tonic-gate
31547c478bd9Sstevel@tonic-gate if (flags & TH_CWR) {
31557c478bd9Sstevel@tonic-gate tcp->tcp_ecn_echo_on = B_FALSE;
31567c478bd9Sstevel@tonic-gate }
31577c478bd9Sstevel@tonic-gate /*
31587c478bd9Sstevel@tonic-gate * Note that both ECN_CE and CWR can be set in the
31597c478bd9Sstevel@tonic-gate * same segment. In this case, we once again turn
31607c478bd9Sstevel@tonic-gate * on ECN_ECHO.
31617c478bd9Sstevel@tonic-gate */
31627c478bd9Sstevel@tonic-gate if ((tos & IPH_ECN_CE) == IPH_ECN_CE) {
31637c478bd9Sstevel@tonic-gate tcp->tcp_ecn_echo_on = B_TRUE;
31647c478bd9Sstevel@tonic-gate }
31657c478bd9Sstevel@tonic-gate }
31667c478bd9Sstevel@tonic-gate
31677c478bd9Sstevel@tonic-gate /*
31687c478bd9Sstevel@tonic-gate * Check whether we can update tcp_ts_recent. This test is
31697c478bd9Sstevel@tonic-gate * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
31707c478bd9Sstevel@tonic-gate * Extensions for High Performance: An Update", Internet Draft.
31717c478bd9Sstevel@tonic-gate */
31727c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok &&
31737c478bd9Sstevel@tonic-gate TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
31747c478bd9Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
31757c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
31767c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
31777c478bd9Sstevel@tonic-gate }
31787c478bd9Sstevel@tonic-gate
31797c478bd9Sstevel@tonic-gate if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) {
31807c478bd9Sstevel@tonic-gate /*
31817c478bd9Sstevel@tonic-gate * FIN in an out of order segment. We record this in
31827c478bd9Sstevel@tonic-gate * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
31837c478bd9Sstevel@tonic-gate * Clear the FIN so that any check on FIN flag will fail.
31847c478bd9Sstevel@tonic-gate * Remember that FIN also counts in the sequence number
31857c478bd9Sstevel@tonic-gate * space. So we need to ack out of order FIN only segments.
31867c478bd9Sstevel@tonic-gate */
31877c478bd9Sstevel@tonic-gate if (flags & TH_FIN) {
31887c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID;
31897c478bd9Sstevel@tonic-gate tcp->tcp_ofo_fin_seq = seg_seq + seg_len;
31907c478bd9Sstevel@tonic-gate flags &= ~TH_FIN;
31917c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
31927c478bd9Sstevel@tonic-gate }
31937c478bd9Sstevel@tonic-gate if (seg_len > 0) {
31947c478bd9Sstevel@tonic-gate /* Fill in the SACK blk list. */
31957c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
31967c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
31977c478bd9Sstevel@tonic-gate tcp_sack_insert(tcp->tcp_sack_list,
31987c478bd9Sstevel@tonic-gate seg_seq, seg_seq + seg_len,
31997c478bd9Sstevel@tonic-gate &(tcp->tcp_num_sack_blk));
32007c478bd9Sstevel@tonic-gate }
32017c478bd9Sstevel@tonic-gate
32027c478bd9Sstevel@tonic-gate /*
32037c478bd9Sstevel@tonic-gate * Attempt reassembly and see if we have something
32047c478bd9Sstevel@tonic-gate * ready to go.
32057c478bd9Sstevel@tonic-gate */
32067c478bd9Sstevel@tonic-gate mp = tcp_reass(tcp, mp, seg_seq);
32077c478bd9Sstevel@tonic-gate /* Always ack out of order packets */
32087c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED | TH_PUSH;
32097c478bd9Sstevel@tonic-gate if (mp != NULL) {
32107c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr -
32117c478bd9Sstevel@tonic-gate mp->b_rptr) <= (uintptr_t)INT_MAX);
32127c478bd9Sstevel@tonic-gate seg_len = mp->b_cont ? msgdsize(mp) :
32137c478bd9Sstevel@tonic-gate (int)(mp->b_wptr - mp->b_rptr);
32147c478bd9Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
32157c478bd9Sstevel@tonic-gate /*
32167c478bd9Sstevel@tonic-gate * A gap is filled and the seq num and len
32177c478bd9Sstevel@tonic-gate * of the gap match that of a previously
32187c478bd9Sstevel@tonic-gate * received FIN, put the FIN flag back in.
32197c478bd9Sstevel@tonic-gate */
32207c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
32217c478bd9Sstevel@tonic-gate seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
32227c478bd9Sstevel@tonic-gate flags |= TH_FIN;
32237c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits &=
32247c478bd9Sstevel@tonic-gate ~TCP_OFO_FIN_VALID;
32257c478bd9Sstevel@tonic-gate }
32267c478bd9Sstevel@tonic-gate } else {
32277c478bd9Sstevel@tonic-gate /*
32287c478bd9Sstevel@tonic-gate * Keep going even with NULL mp.
32297c478bd9Sstevel@tonic-gate * There may be a useful ACK or something else
32307c478bd9Sstevel@tonic-gate * we don't want to miss.
32317c478bd9Sstevel@tonic-gate *
32327c478bd9Sstevel@tonic-gate * But TCP should not perform fast retransmit
32337c478bd9Sstevel@tonic-gate * because of the ack number. TCP uses
32347c478bd9Sstevel@tonic-gate * seg_len == 0 to determine if it is a pure
32357c478bd9Sstevel@tonic-gate * ACK. And this is not a pure ACK.
32367c478bd9Sstevel@tonic-gate */
32377c478bd9Sstevel@tonic-gate seg_len = 0;
32387c478bd9Sstevel@tonic-gate ofo_seg = B_TRUE;
32397c478bd9Sstevel@tonic-gate }
32407c478bd9Sstevel@tonic-gate }
32417c478bd9Sstevel@tonic-gate } else if (seg_len > 0) {
32427c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataInorderSegs);
32437c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataInorderBytes, seg_len);
32447c478bd9Sstevel@tonic-gate /*
32457c478bd9Sstevel@tonic-gate * If an out of order FIN was received before, and the seq
32467c478bd9Sstevel@tonic-gate * num and len of the new segment match that of the FIN,
32477c478bd9Sstevel@tonic-gate * put the FIN flag back in.
32487c478bd9Sstevel@tonic-gate */
32497c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
32507c478bd9Sstevel@tonic-gate seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
32517c478bd9Sstevel@tonic-gate flags |= TH_FIN;
32527c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID;
32537c478bd9Sstevel@tonic-gate }
32547c478bd9Sstevel@tonic-gate }
32557c478bd9Sstevel@tonic-gate if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) {
32567c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
32577c478bd9Sstevel@tonic-gate freemsg(mp);
32587c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
32597c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
32607c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNREFUSED);
32617c478bd9Sstevel@tonic-gate break;
32627c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
32637c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
32647c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
32657c478bd9Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
32667c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNRESET);
32677c478bd9Sstevel@tonic-gate break;
32687c478bd9Sstevel@tonic-gate case TCPS_CLOSING:
32697c478bd9Sstevel@tonic-gate case TCPS_LAST_ACK:
32707c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
32717c478bd9Sstevel@tonic-gate break;
32727c478bd9Sstevel@tonic-gate default:
32737c478bd9Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
32747c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ENXIO);
32757c478bd9Sstevel@tonic-gate break;
32767c478bd9Sstevel@tonic-gate }
32777c478bd9Sstevel@tonic-gate return;
32787c478bd9Sstevel@tonic-gate }
32797c478bd9Sstevel@tonic-gate if (flags & TH_SYN) {
32807c478bd9Sstevel@tonic-gate /*
32817c478bd9Sstevel@tonic-gate * See RFC 793, Page 71
32827c478bd9Sstevel@tonic-gate *
32837c478bd9Sstevel@tonic-gate * The seq number must be in the window as it should
32847c478bd9Sstevel@tonic-gate * be "fixed" above. If it is outside window, it should
32857c478bd9Sstevel@tonic-gate * be already rejected. Note that we allow seg_seq to be
32867c478bd9Sstevel@tonic-gate * rnxt + rwnd because we want to accept 0 window probe.
32877c478bd9Sstevel@tonic-gate */
32887c478bd9Sstevel@tonic-gate assert(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) &&
32897c478bd9Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd));
32907c478bd9Sstevel@tonic-gate freemsg(mp);
32917c478bd9Sstevel@tonic-gate /*
32927c478bd9Sstevel@tonic-gate * If the ACK flag is not set, just use our snxt as the
32937c478bd9Sstevel@tonic-gate * seq number of the RST segment.
32947c478bd9Sstevel@tonic-gate */
32957c478bd9Sstevel@tonic-gate if (!(flags & TH_ACK)) {
32967c478bd9Sstevel@tonic-gate seg_ack = tcp->tcp_snxt;
32977c478bd9Sstevel@tonic-gate }
32987c478bd9Sstevel@tonic-gate tcp_xmit_ctl("TH_SYN", tcp, NULL, seg_ack,
32997c478bd9Sstevel@tonic-gate seg_seq + 1, TH_RST|TH_ACK, 0, sock_id);
33007c478bd9Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
33017c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNRESET);
33027c478bd9Sstevel@tonic-gate return;
33037c478bd9Sstevel@tonic-gate }
33047c478bd9Sstevel@tonic-gate
33057c478bd9Sstevel@tonic-gate process_ack:
33067c478bd9Sstevel@tonic-gate if (!(flags & TH_ACK)) {
33077c478bd9Sstevel@tonic-gate #ifdef DEBUG
33087c478bd9Sstevel@tonic-gate printf("No ack in segment, dropped it, seq:%x\n", seg_seq);
33097c478bd9Sstevel@tonic-gate #endif
33107c478bd9Sstevel@tonic-gate freemsg(mp);
33117c478bd9Sstevel@tonic-gate goto xmit_check;
33127c478bd9Sstevel@tonic-gate }
33137c478bd9Sstevel@tonic-gate }
33147c478bd9Sstevel@tonic-gate bytes_acked = (int)(seg_ack - tcp->tcp_suna);
33157c478bd9Sstevel@tonic-gate
33167c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_RCVD) {
33177c478bd9Sstevel@tonic-gate tcp_t *listener = tcp->tcp_listener;
33187c478bd9Sstevel@tonic-gate #ifdef DEBUG
33197c478bd9Sstevel@tonic-gate printf("Done with eager 3-way handshake\n");
33207c478bd9Sstevel@tonic-gate #endif
33217c478bd9Sstevel@tonic-gate /*
33227c478bd9Sstevel@tonic-gate * NOTE: RFC 793 pg. 72 says this should be 'bytes_acked < 0'
33237c478bd9Sstevel@tonic-gate * but that would mean we have an ack that ignored our SYN.
33247c478bd9Sstevel@tonic-gate */
33257c478bd9Sstevel@tonic-gate if (bytes_acked < 1 || SEQ_GT(seg_ack, tcp->tcp_snxt)) {
33267c478bd9Sstevel@tonic-gate freemsg(mp);
33277c478bd9Sstevel@tonic-gate tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
33287c478bd9Sstevel@tonic-gate tcp, NULL, seg_ack, 0, TH_RST, 0, sock_id);
33297c478bd9Sstevel@tonic-gate return;
33307c478bd9Sstevel@tonic-gate }
33317c478bd9Sstevel@tonic-gate
33327c478bd9Sstevel@tonic-gate /*
33337c478bd9Sstevel@tonic-gate * if the conn_req_q is full defer processing
33347c478bd9Sstevel@tonic-gate * until space is availabe after accept()
33357c478bd9Sstevel@tonic-gate * processing
33367c478bd9Sstevel@tonic-gate */
33377c478bd9Sstevel@tonic-gate if (listener->tcp_conn_req_cnt_q <
33387c478bd9Sstevel@tonic-gate listener->tcp_conn_req_max) {
33397c478bd9Sstevel@tonic-gate tcp_t *tail;
33407c478bd9Sstevel@tonic-gate
33417c478bd9Sstevel@tonic-gate listener->tcp_conn_req_cnt_q0--;
33427c478bd9Sstevel@tonic-gate listener->tcp_conn_req_cnt_q++;
33437c478bd9Sstevel@tonic-gate
33447c478bd9Sstevel@tonic-gate /* Move from SYN_RCVD to ESTABLISHED list */
33457c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
33467c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
33477c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
33487c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0;
33497c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0 = NULL;
33507c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 = NULL;
33517c478bd9Sstevel@tonic-gate
33527c478bd9Sstevel@tonic-gate /*
33537c478bd9Sstevel@tonic-gate * Insert at end of the queue because sockfs
33547c478bd9Sstevel@tonic-gate * sends down T_CONN_RES in chronological
33557c478bd9Sstevel@tonic-gate * order. Leaving the older conn indications
33567c478bd9Sstevel@tonic-gate * at front of the queue helps reducing search
33577c478bd9Sstevel@tonic-gate * time.
33587c478bd9Sstevel@tonic-gate */
33597c478bd9Sstevel@tonic-gate tail = listener->tcp_eager_last_q;
33607c478bd9Sstevel@tonic-gate if (tail != NULL) {
33617c478bd9Sstevel@tonic-gate tail->tcp_eager_next_q = tcp;
33627c478bd9Sstevel@tonic-gate } else {
33637c478bd9Sstevel@tonic-gate listener->tcp_eager_next_q = tcp;
33647c478bd9Sstevel@tonic-gate }
33657c478bd9Sstevel@tonic-gate listener->tcp_eager_last_q = tcp;
33667c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
33677c478bd9Sstevel@tonic-gate } else {
33687c478bd9Sstevel@tonic-gate /*
33697c478bd9Sstevel@tonic-gate * Defer connection on q0 and set deferred
33707c478bd9Sstevel@tonic-gate * connection bit true
33717c478bd9Sstevel@tonic-gate */
33727c478bd9Sstevel@tonic-gate tcp->tcp_conn_def_q0 = B_TRUE;
33737c478bd9Sstevel@tonic-gate
33747c478bd9Sstevel@tonic-gate /* take tcp out of q0 ... */
33757c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
33767c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0;
33777c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
33787c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
33797c478bd9Sstevel@tonic-gate
33807c478bd9Sstevel@tonic-gate /* ... and place it at the end of q0 */
33817c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0;
33827c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 = listener;
33837c478bd9Sstevel@tonic-gate listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp;
33847c478bd9Sstevel@tonic-gate listener->tcp_eager_prev_q0 = tcp;
33857c478bd9Sstevel@tonic-gate }
33867c478bd9Sstevel@tonic-gate
33877c478bd9Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */
33887c478bd9Sstevel@tonic-gate bytes_acked--;
33897c478bd9Sstevel@tonic-gate
33907c478bd9Sstevel@tonic-gate /*
33917c478bd9Sstevel@tonic-gate * If SYN was retransmitted, need to reset all
33927c478bd9Sstevel@tonic-gate * retransmission info as this segment will be
33937c478bd9Sstevel@tonic-gate * treated as a dup ACK.
33947c478bd9Sstevel@tonic-gate */
33957c478bd9Sstevel@tonic-gate if (tcp->tcp_rexmit) {
33967c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
33977c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
33987c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
33997c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
34007c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
34017c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = mss;
34027c478bd9Sstevel@tonic-gate }
34037c478bd9Sstevel@tonic-gate
34047c478bd9Sstevel@tonic-gate /*
34057c478bd9Sstevel@tonic-gate * We set the send window to zero here.
34067c478bd9Sstevel@tonic-gate * This is needed if there is data to be
34077c478bd9Sstevel@tonic-gate * processed already on the queue.
34087c478bd9Sstevel@tonic-gate * Later (at swnd_update label), the
34097c478bd9Sstevel@tonic-gate * "new_swnd > tcp_swnd" condition is satisfied
34107c478bd9Sstevel@tonic-gate * the XMIT_NEEDED flag is set in the current
34117c478bd9Sstevel@tonic-gate * (SYN_RCVD) state. This ensures tcp_wput_data() is
34127c478bd9Sstevel@tonic-gate * called if there is already data on queue in
34137c478bd9Sstevel@tonic-gate * this state.
34147c478bd9Sstevel@tonic-gate */
34157c478bd9Sstevel@tonic-gate tcp->tcp_swnd = 0;
34167c478bd9Sstevel@tonic-gate
34177c478bd9Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
34187c478bd9Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
34197c478bd9Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
34207c478bd9Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
34217c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_ESTABLISHED;
34227c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
34237c478bd9Sstevel@tonic-gate }
34247c478bd9Sstevel@tonic-gate /* This code follows 4.4BSD-Lite2 mostly. */
34257c478bd9Sstevel@tonic-gate if (bytes_acked < 0)
34267c478bd9Sstevel@tonic-gate goto est;
34277c478bd9Sstevel@tonic-gate
34287c478bd9Sstevel@tonic-gate /*
34297c478bd9Sstevel@tonic-gate * If TCP is ECN capable and the congestion experience bit is
34307c478bd9Sstevel@tonic-gate * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be
34317c478bd9Sstevel@tonic-gate * done once per window (or more loosely, per RTT).
34327c478bd9Sstevel@tonic-gate */
34337c478bd9Sstevel@tonic-gate if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max))
34347c478bd9Sstevel@tonic-gate tcp->tcp_cwr = B_FALSE;
34357c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok && (flags & TH_ECE)) {
34367c478bd9Sstevel@tonic-gate if (!tcp->tcp_cwr) {
34377c478bd9Sstevel@tonic-gate npkt = (MIN(tcp->tcp_cwnd, tcp->tcp_swnd) >> 1) / mss;
34387c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss;
34397c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = npkt * mss;
34407c478bd9Sstevel@tonic-gate /*
34417c478bd9Sstevel@tonic-gate * If the cwnd is 0, use the timer to clock out
34427c478bd9Sstevel@tonic-gate * new segments. This is required by the ECN spec.
34437c478bd9Sstevel@tonic-gate */
34447c478bd9Sstevel@tonic-gate if (npkt == 0) {
34457c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
34467c478bd9Sstevel@tonic-gate /*
34477c478bd9Sstevel@tonic-gate * This makes sure that when the ACK comes
34487c478bd9Sstevel@tonic-gate * back, we will increase tcp_cwnd by 1 MSS.
34497c478bd9Sstevel@tonic-gate */
34507c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
34517c478bd9Sstevel@tonic-gate }
34527c478bd9Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
34537c478bd9Sstevel@tonic-gate /*
34547c478bd9Sstevel@tonic-gate * This marks the end of the current window of in
34557c478bd9Sstevel@tonic-gate * flight data. That is why we don't use
34567c478bd9Sstevel@tonic-gate * tcp_suna + tcp_swnd. Only data in flight can
34577c478bd9Sstevel@tonic-gate * provide ECN info.
34587c478bd9Sstevel@tonic-gate */
34597c478bd9Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
34607c478bd9Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
34617c478bd9Sstevel@tonic-gate }
34627c478bd9Sstevel@tonic-gate }
34637c478bd9Sstevel@tonic-gate
34647c478bd9Sstevel@tonic-gate mp1 = tcp->tcp_xmit_head;
34657c478bd9Sstevel@tonic-gate if (bytes_acked == 0) {
34667c478bd9Sstevel@tonic-gate if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) {
34677c478bd9Sstevel@tonic-gate int dupack_cnt;
34687c478bd9Sstevel@tonic-gate
34697c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDupAck);
34707c478bd9Sstevel@tonic-gate /*
34717c478bd9Sstevel@tonic-gate * Fast retransmit. When we have seen exactly three
34727c478bd9Sstevel@tonic-gate * identical ACKs while we have unacked data
34737c478bd9Sstevel@tonic-gate * outstanding we take it as a hint that our peer
34747c478bd9Sstevel@tonic-gate * dropped something.
34757c478bd9Sstevel@tonic-gate *
34767c478bd9Sstevel@tonic-gate * If TCP is retransmitting, don't do fast retransmit.
34777c478bd9Sstevel@tonic-gate */
34787c478bd9Sstevel@tonic-gate if (mp1 != NULL && tcp->tcp_suna != tcp->tcp_snxt &&
34797c478bd9Sstevel@tonic-gate ! tcp->tcp_rexmit) {
34807c478bd9Sstevel@tonic-gate /* Do Limited Transmit */
34817c478bd9Sstevel@tonic-gate if ((dupack_cnt = ++tcp->tcp_dupack_cnt) <
34827c478bd9Sstevel@tonic-gate tcp_dupack_fast_retransmit) {
34837c478bd9Sstevel@tonic-gate /*
34847c478bd9Sstevel@tonic-gate * RFC 3042
34857c478bd9Sstevel@tonic-gate *
34867c478bd9Sstevel@tonic-gate * What we need to do is temporarily
34877c478bd9Sstevel@tonic-gate * increase tcp_cwnd so that new
34887c478bd9Sstevel@tonic-gate * data can be sent if it is allowed
34897c478bd9Sstevel@tonic-gate * by the receive window (tcp_rwnd).
34907c478bd9Sstevel@tonic-gate * tcp_wput_data() will take care of
34917c478bd9Sstevel@tonic-gate * the rest.
34927c478bd9Sstevel@tonic-gate *
34937c478bd9Sstevel@tonic-gate * If the connection is SACK capable,
34947c478bd9Sstevel@tonic-gate * only do limited xmit when there
34957c478bd9Sstevel@tonic-gate * is SACK info.
34967c478bd9Sstevel@tonic-gate *
34977c478bd9Sstevel@tonic-gate * Note how tcp_cwnd is incremented.
34987c478bd9Sstevel@tonic-gate * The first dup ACK will increase
34997c478bd9Sstevel@tonic-gate * it by 1 MSS. The second dup ACK
35007c478bd9Sstevel@tonic-gate * will increase it by 2 MSS. This
35017c478bd9Sstevel@tonic-gate * means that only 1 new segment will
35027c478bd9Sstevel@tonic-gate * be sent for each dup ACK.
35037c478bd9Sstevel@tonic-gate */
35047c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent > 0 &&
35057c478bd9Sstevel@tonic-gate (!tcp->tcp_snd_sack_ok ||
35067c478bd9Sstevel@tonic-gate (tcp->tcp_snd_sack_ok &&
35077c478bd9Sstevel@tonic-gate tcp->tcp_notsack_list != NULL))) {
35087c478bd9Sstevel@tonic-gate tcp->tcp_cwnd += mss <<
35097c478bd9Sstevel@tonic-gate (tcp->tcp_dupack_cnt - 1);
35107c478bd9Sstevel@tonic-gate flags |= TH_LIMIT_XMIT;
35117c478bd9Sstevel@tonic-gate }
35127c478bd9Sstevel@tonic-gate } else if (dupack_cnt ==
35137c478bd9Sstevel@tonic-gate tcp_dupack_fast_retransmit) {
35147c478bd9Sstevel@tonic-gate
35157c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutFastRetrans);
35167c478bd9Sstevel@tonic-gate /*
35177c478bd9Sstevel@tonic-gate * If we have reduced tcp_ssthresh
35187c478bd9Sstevel@tonic-gate * because of ECN, do not reduce it again
35197c478bd9Sstevel@tonic-gate * unless it is already one window of data
35207c478bd9Sstevel@tonic-gate * away. After one window of data, tcp_cwr
35217c478bd9Sstevel@tonic-gate * should then be cleared. Note that
35227c478bd9Sstevel@tonic-gate * for non ECN capable connection, tcp_cwr
35237c478bd9Sstevel@tonic-gate * should always be false.
35247c478bd9Sstevel@tonic-gate *
35257c478bd9Sstevel@tonic-gate * Adjust cwnd since the duplicate
35267c478bd9Sstevel@tonic-gate * ack indicates that a packet was
35277c478bd9Sstevel@tonic-gate * dropped (due to congestion.)
35287c478bd9Sstevel@tonic-gate */
35297c478bd9Sstevel@tonic-gate if (!tcp->tcp_cwr) {
35307c478bd9Sstevel@tonic-gate npkt = (MIN(tcp->tcp_cwnd,
35317c478bd9Sstevel@tonic-gate tcp->tcp_swnd) >> 1) / mss;
35327c478bd9Sstevel@tonic-gate if (npkt < 2)
35337c478bd9Sstevel@tonic-gate npkt = 2;
35347c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = npkt * mss;
35357c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = (npkt +
35367c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt) * mss;
35377c478bd9Sstevel@tonic-gate }
35387c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
35397c478bd9Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
35407c478bd9Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
35417c478bd9Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
35427c478bd9Sstevel@tonic-gate }
35437c478bd9Sstevel@tonic-gate
35447c478bd9Sstevel@tonic-gate /*
35457c478bd9Sstevel@tonic-gate * We do Hoe's algorithm. Refer to her
35467c478bd9Sstevel@tonic-gate * paper "Improving the Start-up Behavior
35477c478bd9Sstevel@tonic-gate * of a Congestion Control Scheme for TCP,"
35487c478bd9Sstevel@tonic-gate * appeared in SIGCOMM'96.
35497c478bd9Sstevel@tonic-gate *
35507c478bd9Sstevel@tonic-gate * Save highest seq no we have sent so far.
35517c478bd9Sstevel@tonic-gate * Be careful about the invisible FIN byte.
35527c478bd9Sstevel@tonic-gate */
35537c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
35547c478bd9Sstevel@tonic-gate (tcp->tcp_unsent == 0)) {
35557c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_fss;
35567c478bd9Sstevel@tonic-gate } else {
35577c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
35587c478bd9Sstevel@tonic-gate }
35597c478bd9Sstevel@tonic-gate
35607c478bd9Sstevel@tonic-gate /*
35617c478bd9Sstevel@tonic-gate * Do not allow bursty traffic during.
35627c478bd9Sstevel@tonic-gate * fast recovery. Refer to Fall and Floyd's
35637c478bd9Sstevel@tonic-gate * paper "Simulation-based Comparisons of
35647c478bd9Sstevel@tonic-gate * Tahoe, Reno and SACK TCP" (in CCR ??)
35657c478bd9Sstevel@tonic-gate * This is a best current practise.
35667c478bd9Sstevel@tonic-gate */
35677c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_SS;
35687c478bd9Sstevel@tonic-gate
35697c478bd9Sstevel@tonic-gate /*
35707c478bd9Sstevel@tonic-gate * For SACK:
35717c478bd9Sstevel@tonic-gate * Calculate tcp_pipe, which is the
35727c478bd9Sstevel@tonic-gate * estimated number of bytes in
35737c478bd9Sstevel@tonic-gate * network.
35747c478bd9Sstevel@tonic-gate *
35757c478bd9Sstevel@tonic-gate * tcp_fack is the highest sack'ed seq num
35767c478bd9Sstevel@tonic-gate * TCP has received.
35777c478bd9Sstevel@tonic-gate *
35787c478bd9Sstevel@tonic-gate * tcp_pipe is explained in the above quoted
35797c478bd9Sstevel@tonic-gate * Fall and Floyd's paper. tcp_fack is
35807c478bd9Sstevel@tonic-gate * explained in Mathis and Mahdavi's
35817c478bd9Sstevel@tonic-gate * "Forward Acknowledgment: Refining TCP
35827c478bd9Sstevel@tonic-gate * Congestion Control" in SIGCOMM '96.
35837c478bd9Sstevel@tonic-gate */
35847c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
35857c478bd9Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
35867c478bd9Sstevel@tonic-gate if (tcp->tcp_notsack_list != NULL) {
35877c478bd9Sstevel@tonic-gate tcp->tcp_pipe = tcp->tcp_snxt -
35887c478bd9Sstevel@tonic-gate tcp->tcp_fack;
35897c478bd9Sstevel@tonic-gate tcp->tcp_sack_snxt = seg_ack;
35907c478bd9Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
35917c478bd9Sstevel@tonic-gate } else {
35927c478bd9Sstevel@tonic-gate /*
35937c478bd9Sstevel@tonic-gate * Always initialize tcp_pipe
35947c478bd9Sstevel@tonic-gate * even though we don't have
35957c478bd9Sstevel@tonic-gate * any SACK info. If later
35967c478bd9Sstevel@tonic-gate * we get SACK info and
35977c478bd9Sstevel@tonic-gate * tcp_pipe is not initialized,
35987c478bd9Sstevel@tonic-gate * funny things will happen.
35997c478bd9Sstevel@tonic-gate */
36007c478bd9Sstevel@tonic-gate tcp->tcp_pipe =
36017c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh;
36027c478bd9Sstevel@tonic-gate }
36037c478bd9Sstevel@tonic-gate } else {
36047c478bd9Sstevel@tonic-gate flags |= TH_REXMIT_NEEDED;
36057c478bd9Sstevel@tonic-gate } /* tcp_snd_sack_ok */
36067c478bd9Sstevel@tonic-gate
36077c478bd9Sstevel@tonic-gate } else {
36087c478bd9Sstevel@tonic-gate /*
36097c478bd9Sstevel@tonic-gate * Here we perform congestion
36107c478bd9Sstevel@tonic-gate * avoidance, but NOT slow start.
36117c478bd9Sstevel@tonic-gate * This is known as the Fast
36127c478bd9Sstevel@tonic-gate * Recovery Algorithm.
36137c478bd9Sstevel@tonic-gate */
36147c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
36157c478bd9Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
36167c478bd9Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
36177c478bd9Sstevel@tonic-gate tcp->tcp_pipe -= mss;
36187c478bd9Sstevel@tonic-gate if (tcp->tcp_pipe < 0)
36197c478bd9Sstevel@tonic-gate tcp->tcp_pipe = 0;
36207c478bd9Sstevel@tonic-gate } else {
36217c478bd9Sstevel@tonic-gate /*
36227c478bd9Sstevel@tonic-gate * We know that one more packet has
36237c478bd9Sstevel@tonic-gate * left the pipe thus we can update
36247c478bd9Sstevel@tonic-gate * cwnd.
36257c478bd9Sstevel@tonic-gate */
36267c478bd9Sstevel@tonic-gate cwnd = tcp->tcp_cwnd + mss;
36277c478bd9Sstevel@tonic-gate if (cwnd > tcp->tcp_cwnd_max)
36287c478bd9Sstevel@tonic-gate cwnd = tcp->tcp_cwnd_max;
36297c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = cwnd;
36307c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
36317c478bd9Sstevel@tonic-gate }
36327c478bd9Sstevel@tonic-gate }
36337c478bd9Sstevel@tonic-gate }
36347c478bd9Sstevel@tonic-gate } else if (tcp->tcp_zero_win_probe) {
36357c478bd9Sstevel@tonic-gate /*
36367c478bd9Sstevel@tonic-gate * If the window has opened, need to arrange
36377c478bd9Sstevel@tonic-gate * to send additional data.
36387c478bd9Sstevel@tonic-gate */
36397c478bd9Sstevel@tonic-gate if (new_swnd != 0) {
36407c478bd9Sstevel@tonic-gate /* tcp_suna != tcp_snxt */
36417c478bd9Sstevel@tonic-gate /* Packet contains a window update */
36427c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInWinUpdate);
36437c478bd9Sstevel@tonic-gate tcp->tcp_zero_win_probe = 0;
36447c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
36457c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
36467c478bd9Sstevel@tonic-gate
36477c478bd9Sstevel@tonic-gate /*
36487c478bd9Sstevel@tonic-gate * Transmit starting with tcp_suna since
36497c478bd9Sstevel@tonic-gate * the one byte probe is not ack'ed.
36507c478bd9Sstevel@tonic-gate * If TCP has sent more than one identical
36517c478bd9Sstevel@tonic-gate * probe, tcp_rexmit will be set. That means
36527c478bd9Sstevel@tonic-gate * tcp_ss_rexmit() will send out the one
36537c478bd9Sstevel@tonic-gate * byte along with new data. Otherwise,
36547c478bd9Sstevel@tonic-gate * fake the retransmission.
36557c478bd9Sstevel@tonic-gate */
36567c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
36577c478bd9Sstevel@tonic-gate if (!tcp->tcp_rexmit) {
36587c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_TRUE;
36597c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
36607c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_suna;
36617c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_suna + 1;
36627c478bd9Sstevel@tonic-gate }
36637c478bd9Sstevel@tonic-gate }
36647c478bd9Sstevel@tonic-gate }
36657c478bd9Sstevel@tonic-gate goto swnd_update;
36667c478bd9Sstevel@tonic-gate }
36677c478bd9Sstevel@tonic-gate
36687c478bd9Sstevel@tonic-gate /*
36697c478bd9Sstevel@tonic-gate * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
36707c478bd9Sstevel@tonic-gate * If the ACK value acks something that we have not yet sent, it might
36717c478bd9Sstevel@tonic-gate * be an old duplicate segment. Send an ACK to re-synchronize the
36727c478bd9Sstevel@tonic-gate * other side.
36737c478bd9Sstevel@tonic-gate * Note: reset in response to unacceptable ACK in SYN_RECEIVE
36747c478bd9Sstevel@tonic-gate * state is handled above, so we can always just drop the segment and
36757c478bd9Sstevel@tonic-gate * send an ACK here.
36767c478bd9Sstevel@tonic-gate *
36777c478bd9Sstevel@tonic-gate * Should we send ACKs in response to ACK only segments?
36787c478bd9Sstevel@tonic-gate */
36797c478bd9Sstevel@tonic-gate if (SEQ_GT(seg_ack, tcp->tcp_snxt)) {
36807c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInAckUnsent);
36817c478bd9Sstevel@tonic-gate /* drop the received segment */
36827c478bd9Sstevel@tonic-gate freemsg(mp);
36837c478bd9Sstevel@tonic-gate
36847c478bd9Sstevel@tonic-gate /* Send back an ACK. */
36857c478bd9Sstevel@tonic-gate mp = tcp_ack_mp(tcp);
36867c478bd9Sstevel@tonic-gate
36877c478bd9Sstevel@tonic-gate if (mp == NULL) {
36887c478bd9Sstevel@tonic-gate return;
36897c478bd9Sstevel@tonic-gate }
36907c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
36917c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
36927c478bd9Sstevel@tonic-gate freeb(mp);
36937c478bd9Sstevel@tonic-gate return;
36947c478bd9Sstevel@tonic-gate }
36957c478bd9Sstevel@tonic-gate
36967c478bd9Sstevel@tonic-gate /*
36977c478bd9Sstevel@tonic-gate * TCP gets a new ACK, update the notsack'ed list to delete those
36987c478bd9Sstevel@tonic-gate * blocks that are covered by this ACK.
36997c478bd9Sstevel@tonic-gate */
37007c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
37017c478bd9Sstevel@tonic-gate tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack,
37027c478bd9Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list));
37037c478bd9Sstevel@tonic-gate }
37047c478bd9Sstevel@tonic-gate
37057c478bd9Sstevel@tonic-gate /*
37067c478bd9Sstevel@tonic-gate * If we got an ACK after fast retransmit, check to see
37077c478bd9Sstevel@tonic-gate * if it is a partial ACK. If it is not and the congestion
37087c478bd9Sstevel@tonic-gate * window was inflated to account for the other side's
37097c478bd9Sstevel@tonic-gate * cached packets, retract it. If it is, do Hoe's algorithm.
37107c478bd9Sstevel@tonic-gate */
37117c478bd9Sstevel@tonic-gate if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) {
37127c478bd9Sstevel@tonic-gate assert(tcp->tcp_rexmit == B_FALSE);
37137c478bd9Sstevel@tonic-gate if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) {
37147c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
37157c478bd9Sstevel@tonic-gate /*
37167c478bd9Sstevel@tonic-gate * Restore the orig tcp_cwnd_ssthresh after
37177c478bd9Sstevel@tonic-gate * fast retransmit phase.
37187c478bd9Sstevel@tonic-gate */
37197c478bd9Sstevel@tonic-gate if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
37207c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh;
37217c478bd9Sstevel@tonic-gate }
37227c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = seg_ack;
37237c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
37247c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
37257c478bd9Sstevel@tonic-gate
37267c478bd9Sstevel@tonic-gate /*
37277c478bd9Sstevel@tonic-gate * Remove all notsack info to avoid confusion with
37287c478bd9Sstevel@tonic-gate * the next fast retrasnmit/recovery phase.
37297c478bd9Sstevel@tonic-gate */
37307c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
37317c478bd9Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
37327c478bd9Sstevel@tonic-gate TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list);
37337c478bd9Sstevel@tonic-gate }
37347c478bd9Sstevel@tonic-gate } else {
37357c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
37367c478bd9Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
37377c478bd9Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
37387c478bd9Sstevel@tonic-gate tcp->tcp_pipe -= mss;
37397c478bd9Sstevel@tonic-gate if (tcp->tcp_pipe < 0)
37407c478bd9Sstevel@tonic-gate tcp->tcp_pipe = 0;
37417c478bd9Sstevel@tonic-gate } else {
37427c478bd9Sstevel@tonic-gate /*
37437c478bd9Sstevel@tonic-gate * Hoe's algorithm:
37447c478bd9Sstevel@tonic-gate *
37457c478bd9Sstevel@tonic-gate * Retransmit the unack'ed segment and
37467c478bd9Sstevel@tonic-gate * restart fast recovery. Note that we
37477c478bd9Sstevel@tonic-gate * need to scale back tcp_cwnd to the
37487c478bd9Sstevel@tonic-gate * original value when we started fast
37497c478bd9Sstevel@tonic-gate * recovery. This is to prevent overly
37507c478bd9Sstevel@tonic-gate * aggressive behaviour in sending new
37517c478bd9Sstevel@tonic-gate * segments.
37527c478bd9Sstevel@tonic-gate */
37537c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh +
37547c478bd9Sstevel@tonic-gate tcp_dupack_fast_retransmit * mss;
37557c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = tcp->tcp_cwnd;
37567c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutFastRetrans);
37577c478bd9Sstevel@tonic-gate flags |= TH_REXMIT_NEEDED;
37587c478bd9Sstevel@tonic-gate }
37597c478bd9Sstevel@tonic-gate }
37607c478bd9Sstevel@tonic-gate } else {
37617c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
37627c478bd9Sstevel@tonic-gate if (tcp->tcp_rexmit) {
37637c478bd9Sstevel@tonic-gate /*
37647c478bd9Sstevel@tonic-gate * TCP is retranmitting. If the ACK ack's all
37657c478bd9Sstevel@tonic-gate * outstanding data, update tcp_rexmit_max and
37667c478bd9Sstevel@tonic-gate * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt
37677c478bd9Sstevel@tonic-gate * to the correct value.
37687c478bd9Sstevel@tonic-gate *
37697c478bd9Sstevel@tonic-gate * Note that SEQ_LEQ() is used. This is to avoid
37707c478bd9Sstevel@tonic-gate * unnecessary fast retransmit caused by dup ACKs
37717c478bd9Sstevel@tonic-gate * received when TCP does slow start retransmission
37727c478bd9Sstevel@tonic-gate * after a time out. During this phase, TCP may
37737c478bd9Sstevel@tonic-gate * send out segments which are already received.
37747c478bd9Sstevel@tonic-gate * This causes dup ACKs to be sent back.
37757c478bd9Sstevel@tonic-gate */
37767c478bd9Sstevel@tonic-gate if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) {
37777c478bd9Sstevel@tonic-gate if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) {
37787c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = seg_ack;
37797c478bd9Sstevel@tonic-gate }
37807c478bd9Sstevel@tonic-gate if (seg_ack != tcp->tcp_rexmit_max) {
37817c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
37827c478bd9Sstevel@tonic-gate }
37837c478bd9Sstevel@tonic-gate } else {
37847c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
37857c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
37867c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
37877c478bd9Sstevel@tonic-gate }
37887c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
37897c478bd9Sstevel@tonic-gate }
37907c478bd9Sstevel@tonic-gate }
37917c478bd9Sstevel@tonic-gate
37927c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInAckSegs);
37937c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInAckBytes, bytes_acked);
37947c478bd9Sstevel@tonic-gate tcp->tcp_suna = seg_ack;
37957c478bd9Sstevel@tonic-gate if (tcp->tcp_zero_win_probe != 0) {
37967c478bd9Sstevel@tonic-gate tcp->tcp_zero_win_probe = 0;
37977c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
37987c478bd9Sstevel@tonic-gate }
37997c478bd9Sstevel@tonic-gate
38007c478bd9Sstevel@tonic-gate /*
38017c478bd9Sstevel@tonic-gate * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
38027c478bd9Sstevel@tonic-gate * Note that it cannot be the SYN being ack'ed. The code flow
38037c478bd9Sstevel@tonic-gate * will not reach here.
38047c478bd9Sstevel@tonic-gate */
38057c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
38067c478bd9Sstevel@tonic-gate goto fin_acked;
38077c478bd9Sstevel@tonic-gate }
38087c478bd9Sstevel@tonic-gate
38097c478bd9Sstevel@tonic-gate /*
38107c478bd9Sstevel@tonic-gate * Update the congestion window.
38117c478bd9Sstevel@tonic-gate *
38127c478bd9Sstevel@tonic-gate * If TCP is not ECN capable or TCP is ECN capable but the
38137c478bd9Sstevel@tonic-gate * congestion experience bit is not set, increase the tcp_cwnd as
38147c478bd9Sstevel@tonic-gate * usual.
38157c478bd9Sstevel@tonic-gate */
38167c478bd9Sstevel@tonic-gate if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) {
38177c478bd9Sstevel@tonic-gate cwnd = tcp->tcp_cwnd;
38187c478bd9Sstevel@tonic-gate add = mss;
38197c478bd9Sstevel@tonic-gate
38207c478bd9Sstevel@tonic-gate if (cwnd >= tcp->tcp_cwnd_ssthresh) {
38217c478bd9Sstevel@tonic-gate /*
38227c478bd9Sstevel@tonic-gate * This is to prevent an increase of less than 1 MSS of
38237c478bd9Sstevel@tonic-gate * tcp_cwnd. With partial increase, tcp_wput_data()
38247c478bd9Sstevel@tonic-gate * may send out tinygrams in order to preserve mblk
38257c478bd9Sstevel@tonic-gate * boundaries.
38267c478bd9Sstevel@tonic-gate *
38277c478bd9Sstevel@tonic-gate * By initializing tcp_cwnd_cnt to new tcp_cwnd and
38287c478bd9Sstevel@tonic-gate * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
38297c478bd9Sstevel@tonic-gate * increased by 1 MSS for every RTTs.
38307c478bd9Sstevel@tonic-gate */
38317c478bd9Sstevel@tonic-gate if (tcp->tcp_cwnd_cnt <= 0) {
38327c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = cwnd + add;
38337c478bd9Sstevel@tonic-gate } else {
38347c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt -= add;
38357c478bd9Sstevel@tonic-gate add = 0;
38367c478bd9Sstevel@tonic-gate }
38377c478bd9Sstevel@tonic-gate }
38387c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max);
38397c478bd9Sstevel@tonic-gate }
38407c478bd9Sstevel@tonic-gate
38417c478bd9Sstevel@tonic-gate /* Can we update the RTT estimates? */
38427c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
38437c478bd9Sstevel@tonic-gate /* Ignore zero timestamp echo-reply. */
38447c478bd9Sstevel@tonic-gate if (tcpopt.tcp_opt_ts_ecr != 0) {
38457c478bd9Sstevel@tonic-gate tcp_set_rto(tcp, (int32_t)(prom_gettime() -
38467c478bd9Sstevel@tonic-gate tcpopt.tcp_opt_ts_ecr));
38477c478bd9Sstevel@tonic-gate }
38487c478bd9Sstevel@tonic-gate
38497c478bd9Sstevel@tonic-gate /* If needed, restart the timer. */
38507c478bd9Sstevel@tonic-gate if (tcp->tcp_set_timer == 1) {
38517c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
38527c478bd9Sstevel@tonic-gate tcp->tcp_set_timer = 0;
38537c478bd9Sstevel@tonic-gate }
38547c478bd9Sstevel@tonic-gate /*
38557c478bd9Sstevel@tonic-gate * Update tcp_csuna in case the other side stops sending
38567c478bd9Sstevel@tonic-gate * us timestamps.
38577c478bd9Sstevel@tonic-gate */
38587c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
38597c478bd9Sstevel@tonic-gate } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
38607c478bd9Sstevel@tonic-gate /*
38617c478bd9Sstevel@tonic-gate * An ACK sequence we haven't seen before, so get the RTT
38627c478bd9Sstevel@tonic-gate * and update the RTO.
386353391bafSeota * Note. use uintptr_t to suppress the gcc warning.
38647c478bd9Sstevel@tonic-gate */
38657c478bd9Sstevel@tonic-gate tcp_set_rto(tcp, (int32_t)(prom_gettime() -
386653391bafSeota (uint32_t)(uintptr_t)mp1->b_prev));
38677c478bd9Sstevel@tonic-gate
38687c478bd9Sstevel@tonic-gate /* Remeber the last sequence to be ACKed */
38697c478bd9Sstevel@tonic-gate tcp->tcp_csuna = seg_ack;
38707c478bd9Sstevel@tonic-gate if (tcp->tcp_set_timer == 1) {
38717c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
38727c478bd9Sstevel@tonic-gate tcp->tcp_set_timer = 0;
38737c478bd9Sstevel@tonic-gate }
38747c478bd9Sstevel@tonic-gate } else {
38757c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRttNoUpdate);
38767c478bd9Sstevel@tonic-gate }
38777c478bd9Sstevel@tonic-gate
38787c478bd9Sstevel@tonic-gate /* Eat acknowledged bytes off the xmit queue. */
38797c478bd9Sstevel@tonic-gate for (;;) {
38807c478bd9Sstevel@tonic-gate mblk_t *mp2;
38817c478bd9Sstevel@tonic-gate uchar_t *wptr;
38827c478bd9Sstevel@tonic-gate
38837c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
38847c478bd9Sstevel@tonic-gate assert((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX);
38857c478bd9Sstevel@tonic-gate bytes_acked -= (int)(wptr - mp1->b_rptr);
38867c478bd9Sstevel@tonic-gate if (bytes_acked < 0) {
38877c478bd9Sstevel@tonic-gate mp1->b_rptr = wptr + bytes_acked;
38887c478bd9Sstevel@tonic-gate break;
38897c478bd9Sstevel@tonic-gate }
38907c478bd9Sstevel@tonic-gate mp1->b_prev = NULL;
38917c478bd9Sstevel@tonic-gate mp2 = mp1;
38927c478bd9Sstevel@tonic-gate mp1 = mp1->b_cont;
38937c478bd9Sstevel@tonic-gate freeb(mp2);
38947c478bd9Sstevel@tonic-gate if (bytes_acked == 0) {
38957c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
38967c478bd9Sstevel@tonic-gate /* Everything is ack'ed, clear the tail. */
38977c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail = NULL;
38987c478bd9Sstevel@tonic-gate goto pre_swnd_update;
38997c478bd9Sstevel@tonic-gate }
39007c478bd9Sstevel@tonic-gate if (mp2 != tcp->tcp_xmit_tail)
39017c478bd9Sstevel@tonic-gate break;
39027c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail = mp1;
39037c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
39047c478bd9Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
39057c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr -
39067c478bd9Sstevel@tonic-gate mp1->b_rptr);
39077c478bd9Sstevel@tonic-gate break;
39087c478bd9Sstevel@tonic-gate }
39097c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
39107c478bd9Sstevel@tonic-gate /*
39117c478bd9Sstevel@tonic-gate * More was acked but there is nothing more
39127c478bd9Sstevel@tonic-gate * outstanding. This means that the FIN was
39137c478bd9Sstevel@tonic-gate * just acked or that we're talking to a clown.
39147c478bd9Sstevel@tonic-gate */
39157c478bd9Sstevel@tonic-gate fin_acked:
39167c478bd9Sstevel@tonic-gate assert(tcp->tcp_fin_sent);
39177c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail = NULL;
39187c478bd9Sstevel@tonic-gate if (tcp->tcp_fin_sent) {
39197c478bd9Sstevel@tonic-gate tcp->tcp_fin_acked = B_TRUE;
39207c478bd9Sstevel@tonic-gate } else {
39217c478bd9Sstevel@tonic-gate /*
39227c478bd9Sstevel@tonic-gate * We should never got here because
39237c478bd9Sstevel@tonic-gate * we have already checked that the
39247c478bd9Sstevel@tonic-gate * number of bytes ack'ed should be
39257c478bd9Sstevel@tonic-gate * smaller than or equal to what we
39267c478bd9Sstevel@tonic-gate * have sent so far (it is the
39277c478bd9Sstevel@tonic-gate * acceptability check of the ACK).
39287c478bd9Sstevel@tonic-gate * We can only get here if the send
39297c478bd9Sstevel@tonic-gate * queue is corrupted.
39307c478bd9Sstevel@tonic-gate *
39317c478bd9Sstevel@tonic-gate * Terminate the connection and
39327c478bd9Sstevel@tonic-gate * panic the system. It is better
39337c478bd9Sstevel@tonic-gate * for us to panic instead of
39347c478bd9Sstevel@tonic-gate * continuing to avoid other disaster.
39357c478bd9Sstevel@tonic-gate */
39367c478bd9Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
39377c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_RST|TH_ACK, 0, sock_id);
39387c478bd9Sstevel@tonic-gate printf("Memory corruption "
39397c478bd9Sstevel@tonic-gate "detected for connection %s.\n",
39407c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL,
39417c478bd9Sstevel@tonic-gate DISP_ADDR_AND_PORT));
39427c478bd9Sstevel@tonic-gate /* We should never get here... */
39437c478bd9Sstevel@tonic-gate prom_panic("tcp_rput_data");
39447c478bd9Sstevel@tonic-gate }
39457c478bd9Sstevel@tonic-gate goto pre_swnd_update;
39467c478bd9Sstevel@tonic-gate }
39477c478bd9Sstevel@tonic-gate assert(mp2 != tcp->tcp_xmit_tail);
39487c478bd9Sstevel@tonic-gate }
39497c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent) {
39507c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
39517c478bd9Sstevel@tonic-gate }
39527c478bd9Sstevel@tonic-gate pre_swnd_update:
39537c478bd9Sstevel@tonic-gate tcp->tcp_xmit_head = mp1;
39547c478bd9Sstevel@tonic-gate swnd_update:
39557c478bd9Sstevel@tonic-gate /*
39567c478bd9Sstevel@tonic-gate * The following check is different from most other implementations.
39577c478bd9Sstevel@tonic-gate * For bi-directional transfer, when segments are dropped, the
39587c478bd9Sstevel@tonic-gate * "normal" check will not accept a window update in those
39597c478bd9Sstevel@tonic-gate * retransmitted segemnts. Failing to do that, TCP may send out
39607c478bd9Sstevel@tonic-gate * segments which are outside receiver's window. As TCP accepts
39617c478bd9Sstevel@tonic-gate * the ack in those retransmitted segments, if the window update in
39627c478bd9Sstevel@tonic-gate * the same segment is not accepted, TCP will incorrectly calculates
39637c478bd9Sstevel@tonic-gate * that it can send more segments. This can create a deadlock
39647c478bd9Sstevel@tonic-gate * with the receiver if its window becomes zero.
39657c478bd9Sstevel@tonic-gate */
39667c478bd9Sstevel@tonic-gate if (SEQ_LT(tcp->tcp_swl2, seg_ack) ||
39677c478bd9Sstevel@tonic-gate SEQ_LT(tcp->tcp_swl1, seg_seq) ||
39687c478bd9Sstevel@tonic-gate (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) {
39697c478bd9Sstevel@tonic-gate /*
39707c478bd9Sstevel@tonic-gate * The criteria for update is:
39717c478bd9Sstevel@tonic-gate *
39727c478bd9Sstevel@tonic-gate * 1. the segment acknowledges some data. Or
39737c478bd9Sstevel@tonic-gate * 2. the segment is new, i.e. it has a higher seq num. Or
39747c478bd9Sstevel@tonic-gate * 3. the segment is not old and the advertised window is
39757c478bd9Sstevel@tonic-gate * larger than the previous advertised window.
39767c478bd9Sstevel@tonic-gate */
39777c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd)
39787c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
39797c478bd9Sstevel@tonic-gate tcp->tcp_swnd = new_swnd;
39807c478bd9Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
39817c478bd9Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
39827c478bd9Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
39837c478bd9Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
39847c478bd9Sstevel@tonic-gate }
39857c478bd9Sstevel@tonic-gate est:
39867c478bd9Sstevel@tonic-gate if (tcp->tcp_state > TCPS_ESTABLISHED) {
39877c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
39887c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
39897c478bd9Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
39907c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_FIN_WAIT_2;
39917c478bd9Sstevel@tonic-gate /*
39927c478bd9Sstevel@tonic-gate * We implement the non-standard BSD/SunOS
39937c478bd9Sstevel@tonic-gate * FIN_WAIT_2 flushing algorithm.
39947c478bd9Sstevel@tonic-gate * If there is no user attached to this
39957c478bd9Sstevel@tonic-gate * TCP endpoint, then this TCP struct
39967c478bd9Sstevel@tonic-gate * could hang around forever in FIN_WAIT_2
39977c478bd9Sstevel@tonic-gate * state if the peer forgets to send us
39987c478bd9Sstevel@tonic-gate * a FIN. To prevent this, we wait only
39997c478bd9Sstevel@tonic-gate * 2*MSL (a convenient time value) for
40007c478bd9Sstevel@tonic-gate * the FIN to arrive. If it doesn't show up,
40017c478bd9Sstevel@tonic-gate * we flush the TCP endpoint. This algorithm,
40027c478bd9Sstevel@tonic-gate * though a violation of RFC-793, has worked
40037c478bd9Sstevel@tonic-gate * for over 10 years in BSD systems.
40047c478bd9Sstevel@tonic-gate * Note: SunOS 4.x waits 675 seconds before
40057c478bd9Sstevel@tonic-gate * flushing the FIN_WAIT_2 connection.
40067c478bd9Sstevel@tonic-gate */
40077c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp,
40087c478bd9Sstevel@tonic-gate tcp_fin_wait_2_flush_interval);
40097c478bd9Sstevel@tonic-gate }
40107c478bd9Sstevel@tonic-gate break;
40117c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
40127c478bd9Sstevel@tonic-gate break; /* Shutdown hook? */
40137c478bd9Sstevel@tonic-gate case TCPS_LAST_ACK:
40147c478bd9Sstevel@tonic-gate freemsg(mp);
40157c478bd9Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
40167c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
40177c478bd9Sstevel@tonic-gate return;
40187c478bd9Sstevel@tonic-gate }
40197c478bd9Sstevel@tonic-gate goto xmit_check;
40207c478bd9Sstevel@tonic-gate case TCPS_CLOSING:
40217c478bd9Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
40227c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_TIME_WAIT;
40237c478bd9Sstevel@tonic-gate tcp_time_wait_append(tcp);
40247c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
40257c478bd9Sstevel@tonic-gate }
40267c478bd9Sstevel@tonic-gate /*FALLTHRU*/
40277c478bd9Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
40287c478bd9Sstevel@tonic-gate freemsg(mp);
40297c478bd9Sstevel@tonic-gate goto xmit_check;
40307c478bd9Sstevel@tonic-gate default:
40317c478bd9Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
40327c478bd9Sstevel@tonic-gate break;
40337c478bd9Sstevel@tonic-gate }
40347c478bd9Sstevel@tonic-gate }
40357c478bd9Sstevel@tonic-gate if (flags & TH_FIN) {
40367c478bd9Sstevel@tonic-gate /* Make sure we ack the fin */
40377c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
40387c478bd9Sstevel@tonic-gate if (!tcp->tcp_fin_rcvd) {
40397c478bd9Sstevel@tonic-gate tcp->tcp_fin_rcvd = B_TRUE;
40407c478bd9Sstevel@tonic-gate tcp->tcp_rnxt++;
40417c478bd9Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
40427c478bd9Sstevel@tonic-gate
40437c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
40447c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
40457c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
40467c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_CLOSE_WAIT;
40477c478bd9Sstevel@tonic-gate /* Keepalive? */
40487c478bd9Sstevel@tonic-gate break;
40497c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
40507c478bd9Sstevel@tonic-gate if (!tcp->tcp_fin_acked) {
40517c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_CLOSING;
40527c478bd9Sstevel@tonic-gate break;
40537c478bd9Sstevel@tonic-gate }
40547c478bd9Sstevel@tonic-gate /* FALLTHRU */
40557c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
40567c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_TIME_WAIT;
40577c478bd9Sstevel@tonic-gate tcp_time_wait_append(tcp);
40587c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
40597c478bd9Sstevel@tonic-gate if (seg_len) {
40607c478bd9Sstevel@tonic-gate /*
40617c478bd9Sstevel@tonic-gate * implies data piggybacked on FIN.
40627c478bd9Sstevel@tonic-gate * break to handle data.
40637c478bd9Sstevel@tonic-gate */
40647c478bd9Sstevel@tonic-gate break;
40657c478bd9Sstevel@tonic-gate }
40667c478bd9Sstevel@tonic-gate freemsg(mp);
40677c478bd9Sstevel@tonic-gate goto ack_check;
40687c478bd9Sstevel@tonic-gate }
40697c478bd9Sstevel@tonic-gate }
40707c478bd9Sstevel@tonic-gate }
40717c478bd9Sstevel@tonic-gate if (mp == NULL)
40727c478bd9Sstevel@tonic-gate goto xmit_check;
40737c478bd9Sstevel@tonic-gate if (seg_len == 0) {
40747c478bd9Sstevel@tonic-gate freemsg(mp);
40757c478bd9Sstevel@tonic-gate goto xmit_check;
40767c478bd9Sstevel@tonic-gate }
40777c478bd9Sstevel@tonic-gate if (mp->b_rptr == mp->b_wptr) {
40787c478bd9Sstevel@tonic-gate /*
40797c478bd9Sstevel@tonic-gate * The header has been consumed, so we remove the
40807c478bd9Sstevel@tonic-gate * zero-length mblk here.
40817c478bd9Sstevel@tonic-gate */
40827c478bd9Sstevel@tonic-gate mp1 = mp;
40837c478bd9Sstevel@tonic-gate mp = mp->b_cont;
40847c478bd9Sstevel@tonic-gate freeb(mp1);
40857c478bd9Sstevel@tonic-gate }
40867c478bd9Sstevel@tonic-gate /*
40877c478bd9Sstevel@tonic-gate * ACK every other segments, unless the input queue is empty
40887c478bd9Sstevel@tonic-gate * as we don't have a timer available.
40897c478bd9Sstevel@tonic-gate */
40907c478bd9Sstevel@tonic-gate if (++tcp->tcp_rack_cnt == 2 || sockets[sock_id].inq == NULL) {
40917c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
40927c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
40937c478bd9Sstevel@tonic-gate }
40947c478bd9Sstevel@tonic-gate tcp->tcp_rnxt += seg_len;
40957c478bd9Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
40967c478bd9Sstevel@tonic-gate
40977c478bd9Sstevel@tonic-gate /* Update SACK list */
40987c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
40997c478bd9Sstevel@tonic-gate tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt,
41007c478bd9Sstevel@tonic-gate &(tcp->tcp_num_sack_blk));
41017c478bd9Sstevel@tonic-gate }
41027c478bd9Sstevel@tonic-gate
41037c478bd9Sstevel@tonic-gate if (tcp->tcp_listener) {
41047c478bd9Sstevel@tonic-gate /*
41057c478bd9Sstevel@tonic-gate * Side queue inbound data until the accept happens.
41067c478bd9Sstevel@tonic-gate * tcp_accept/tcp_rput drains this when the accept happens.
41077c478bd9Sstevel@tonic-gate */
41087c478bd9Sstevel@tonic-gate tcp_rcv_enqueue(tcp, mp, seg_len);
41097c478bd9Sstevel@tonic-gate } else {
41107c478bd9Sstevel@tonic-gate /* Just queue the data until the app calls read. */
41117c478bd9Sstevel@tonic-gate tcp_rcv_enqueue(tcp, mp, seg_len);
41127c478bd9Sstevel@tonic-gate /*
41137c478bd9Sstevel@tonic-gate * Make sure the timer is running if we have data waiting
41147c478bd9Sstevel@tonic-gate * for a push bit. This provides resiliency against
41157c478bd9Sstevel@tonic-gate * implementations that do not correctly generate push bits.
41167c478bd9Sstevel@tonic-gate */
41177c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL)
41187c478bd9Sstevel@tonic-gate flags |= TH_TIMER_NEEDED;
41197c478bd9Sstevel@tonic-gate }
41207c478bd9Sstevel@tonic-gate
41217c478bd9Sstevel@tonic-gate xmit_check:
41227c478bd9Sstevel@tonic-gate /* Is there anything left to do? */
41237c478bd9Sstevel@tonic-gate if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED|
41247c478bd9Sstevel@tonic-gate TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_TIMER_NEEDED)) == 0)
41257c478bd9Sstevel@tonic-gate return;
41267c478bd9Sstevel@tonic-gate
41277c478bd9Sstevel@tonic-gate /* Any transmit work to do and a non-zero window? */
41287c478bd9Sstevel@tonic-gate if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT|
41297c478bd9Sstevel@tonic-gate TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) {
41307c478bd9Sstevel@tonic-gate if (flags & TH_REXMIT_NEEDED) {
41317c478bd9Sstevel@tonic-gate uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna;
41327c478bd9Sstevel@tonic-gate
41337c478bd9Sstevel@tonic-gate if (snd_size > mss)
41347c478bd9Sstevel@tonic-gate snd_size = mss;
41357c478bd9Sstevel@tonic-gate if (snd_size > tcp->tcp_swnd)
41367c478bd9Sstevel@tonic-gate snd_size = tcp->tcp_swnd;
41377c478bd9Sstevel@tonic-gate mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size,
41387c478bd9Sstevel@tonic-gate NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size,
41397c478bd9Sstevel@tonic-gate B_TRUE);
41407c478bd9Sstevel@tonic-gate
41417c478bd9Sstevel@tonic-gate if (mp1 != NULL) {
414253391bafSeota /* use uintptr_t to suppress the gcc warning */
41437c478bd9Sstevel@tonic-gate tcp->tcp_xmit_head->b_prev =
414453391bafSeota (mblk_t *)(uintptr_t)prom_gettime();
41457c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
41467c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
41477c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, snd_size);
41487c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
41497c478bd9Sstevel@tonic-gate freeb(mp1);
41507c478bd9Sstevel@tonic-gate }
41517c478bd9Sstevel@tonic-gate }
41527c478bd9Sstevel@tonic-gate if (flags & TH_NEED_SACK_REXMIT) {
41537c478bd9Sstevel@tonic-gate if (tcp_sack_rxmit(tcp, sock_id) != 0) {
41547c478bd9Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
41557c478bd9Sstevel@tonic-gate }
41567c478bd9Sstevel@tonic-gate }
41577c478bd9Sstevel@tonic-gate /*
41587c478bd9Sstevel@tonic-gate * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
41597c478bd9Sstevel@tonic-gate * out new segment. Note that tcp_rexmit should not be
41607c478bd9Sstevel@tonic-gate * set, otherwise TH_LIMIT_XMIT should not be set.
41617c478bd9Sstevel@tonic-gate */
41627c478bd9Sstevel@tonic-gate if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) {
41637c478bd9Sstevel@tonic-gate if (!tcp->tcp_rexmit) {
41647c478bd9Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
41657c478bd9Sstevel@tonic-gate } else {
41667c478bd9Sstevel@tonic-gate tcp_ss_rexmit(tcp, sock_id);
41677c478bd9Sstevel@tonic-gate }
41687c478bd9Sstevel@tonic-gate /*
41697c478bd9Sstevel@tonic-gate * The TCP could be closed in tcp_state_wait via
41707c478bd9Sstevel@tonic-gate * tcp_wput_data (tcp_ss_rexmit could call
41717c478bd9Sstevel@tonic-gate * tcp_wput_data as well).
41727c478bd9Sstevel@tonic-gate */
41737c478bd9Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
41747c478bd9Sstevel@tonic-gate return;
41757c478bd9Sstevel@tonic-gate }
41767c478bd9Sstevel@tonic-gate /*
41777c478bd9Sstevel@tonic-gate * Adjust tcp_cwnd back to normal value after sending
41787c478bd9Sstevel@tonic-gate * new data segments.
41797c478bd9Sstevel@tonic-gate */
41807c478bd9Sstevel@tonic-gate if (flags & TH_LIMIT_XMIT) {
41817c478bd9Sstevel@tonic-gate tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1);
41827c478bd9Sstevel@tonic-gate }
41837c478bd9Sstevel@tonic-gate
41847c478bd9Sstevel@tonic-gate /* Anything more to do? */
41857c478bd9Sstevel@tonic-gate if ((flags & (TH_ACK_NEEDED|TH_TIMER_NEEDED)) == 0)
41867c478bd9Sstevel@tonic-gate return;
41877c478bd9Sstevel@tonic-gate }
41887c478bd9Sstevel@tonic-gate ack_check:
41897c478bd9Sstevel@tonic-gate if (flags & TH_ACK_NEEDED) {
41907c478bd9Sstevel@tonic-gate /*
41917c478bd9Sstevel@tonic-gate * Time to send an ack for some reason.
41927c478bd9Sstevel@tonic-gate */
41937c478bd9Sstevel@tonic-gate if ((mp1 = tcp_ack_mp(tcp)) != NULL) {
41947c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data: ack mp", mp1);
41957c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
41967c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
41977c478bd9Sstevel@tonic-gate freeb(mp1);
41987c478bd9Sstevel@tonic-gate }
41997c478bd9Sstevel@tonic-gate }
42007c478bd9Sstevel@tonic-gate }
42017c478bd9Sstevel@tonic-gate
42027c478bd9Sstevel@tonic-gate /*
42037c478bd9Sstevel@tonic-gate * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start
42047c478bd9Sstevel@tonic-gate * retransmission after a timeout.
42057c478bd9Sstevel@tonic-gate *
42067c478bd9Sstevel@tonic-gate * To limit the number of duplicate segments, we limit the number of segment
42077c478bd9Sstevel@tonic-gate * to be sent in one time to tcp_snd_burst, the burst variable.
42087c478bd9Sstevel@tonic-gate */
42097c478bd9Sstevel@tonic-gate static void
tcp_ss_rexmit(tcp_t * tcp,int sock_id)42107c478bd9Sstevel@tonic-gate tcp_ss_rexmit(tcp_t *tcp, int sock_id)
42117c478bd9Sstevel@tonic-gate {
42127c478bd9Sstevel@tonic-gate uint32_t snxt;
42137c478bd9Sstevel@tonic-gate uint32_t smax;
42147c478bd9Sstevel@tonic-gate int32_t win;
42157c478bd9Sstevel@tonic-gate int32_t mss;
42167c478bd9Sstevel@tonic-gate int32_t off;
42177c478bd9Sstevel@tonic-gate int32_t burst = tcp->tcp_snd_burst;
42187c478bd9Sstevel@tonic-gate mblk_t *snxt_mp;
42197c478bd9Sstevel@tonic-gate
42207c478bd9Sstevel@tonic-gate /*
42217c478bd9Sstevel@tonic-gate * Note that tcp_rexmit can be set even though TCP has retransmitted
42227c478bd9Sstevel@tonic-gate * all unack'ed segments.
42237c478bd9Sstevel@tonic-gate */
42247c478bd9Sstevel@tonic-gate if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) {
42257c478bd9Sstevel@tonic-gate smax = tcp->tcp_rexmit_max;
42267c478bd9Sstevel@tonic-gate snxt = tcp->tcp_rexmit_nxt;
42277c478bd9Sstevel@tonic-gate if (SEQ_LT(snxt, tcp->tcp_suna)) {
42287c478bd9Sstevel@tonic-gate snxt = tcp->tcp_suna;
42297c478bd9Sstevel@tonic-gate }
42307c478bd9Sstevel@tonic-gate win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd);
42317c478bd9Sstevel@tonic-gate win -= snxt - tcp->tcp_suna;
42327c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss;
42337c478bd9Sstevel@tonic-gate snxt_mp = tcp_get_seg_mp(tcp, snxt, &off);
42347c478bd9Sstevel@tonic-gate
42357c478bd9Sstevel@tonic-gate while (SEQ_LT(snxt, smax) && (win > 0) &&
42367c478bd9Sstevel@tonic-gate (burst > 0) && (snxt_mp != NULL)) {
42377c478bd9Sstevel@tonic-gate mblk_t *xmit_mp;
42387c478bd9Sstevel@tonic-gate mblk_t *old_snxt_mp = snxt_mp;
42397c478bd9Sstevel@tonic-gate uint32_t cnt = mss;
42407c478bd9Sstevel@tonic-gate
42417c478bd9Sstevel@tonic-gate if (win < cnt) {
42427c478bd9Sstevel@tonic-gate cnt = win;
42437c478bd9Sstevel@tonic-gate }
42447c478bd9Sstevel@tonic-gate if (SEQ_GT(snxt + cnt, smax)) {
42457c478bd9Sstevel@tonic-gate cnt = smax - snxt;
42467c478bd9Sstevel@tonic-gate }
42477c478bd9Sstevel@tonic-gate xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off,
42487c478bd9Sstevel@tonic-gate &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE);
42497c478bd9Sstevel@tonic-gate
42507c478bd9Sstevel@tonic-gate if (xmit_mp == NULL)
42517c478bd9Sstevel@tonic-gate return;
42527c478bd9Sstevel@tonic-gate
42537c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, xmit_mp);
42547c478bd9Sstevel@tonic-gate freeb(xmit_mp);
42557c478bd9Sstevel@tonic-gate
42567c478bd9Sstevel@tonic-gate snxt += cnt;
42577c478bd9Sstevel@tonic-gate win -= cnt;
42587c478bd9Sstevel@tonic-gate /*
42597c478bd9Sstevel@tonic-gate * Update the send timestamp to avoid false
42607c478bd9Sstevel@tonic-gate * retransmission.
426153391bafSeota * Note. use uintptr_t to suppress the gcc warning.
42627c478bd9Sstevel@tonic-gate */
426353391bafSeota old_snxt_mp->b_prev =
426453391bafSeota (mblk_t *)(uintptr_t)prom_gettime();
42657c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
42667c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, cnt);
42677c478bd9Sstevel@tonic-gate
42687c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = snxt;
42697c478bd9Sstevel@tonic-gate burst--;
42707c478bd9Sstevel@tonic-gate }
42717c478bd9Sstevel@tonic-gate /*
42727c478bd9Sstevel@tonic-gate * If we have transmitted all we have at the time
42737c478bd9Sstevel@tonic-gate * we started the retranmission, we can leave
42747c478bd9Sstevel@tonic-gate * the rest of the job to tcp_wput_data(). But we
42757c478bd9Sstevel@tonic-gate * need to check the send window first. If the
42767c478bd9Sstevel@tonic-gate * win is not 0, go on with tcp_wput_data().
42777c478bd9Sstevel@tonic-gate */
42787c478bd9Sstevel@tonic-gate if (SEQ_LT(snxt, smax) || win == 0) {
42797c478bd9Sstevel@tonic-gate return;
42807c478bd9Sstevel@tonic-gate }
42817c478bd9Sstevel@tonic-gate }
42827c478bd9Sstevel@tonic-gate /* Only call tcp_wput_data() if there is data to be sent. */
42837c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent) {
42847c478bd9Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
42857c478bd9Sstevel@tonic-gate }
42867c478bd9Sstevel@tonic-gate }
42877c478bd9Sstevel@tonic-gate
42887c478bd9Sstevel@tonic-gate /*
42897c478bd9Sstevel@tonic-gate * tcp_timer is the timer service routine. It handles all timer events for
42907c478bd9Sstevel@tonic-gate * a tcp instance except keepalives. It figures out from the state of the
42917c478bd9Sstevel@tonic-gate * tcp instance what kind of action needs to be done at the time it is called.
42927c478bd9Sstevel@tonic-gate */
42937c478bd9Sstevel@tonic-gate static void
tcp_timer(tcp_t * tcp,int sock_id)42947c478bd9Sstevel@tonic-gate tcp_timer(tcp_t *tcp, int sock_id)
42957c478bd9Sstevel@tonic-gate {
42967c478bd9Sstevel@tonic-gate mblk_t *mp;
42977c478bd9Sstevel@tonic-gate uint32_t first_threshold;
42987c478bd9Sstevel@tonic-gate uint32_t second_threshold;
42997c478bd9Sstevel@tonic-gate uint32_t ms;
43007c478bd9Sstevel@tonic-gate uint32_t mss;
43017c478bd9Sstevel@tonic-gate
43027c478bd9Sstevel@tonic-gate first_threshold = tcp->tcp_first_timer_threshold;
43037c478bd9Sstevel@tonic-gate second_threshold = tcp->tcp_second_timer_threshold;
43047c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
43057c478bd9Sstevel@tonic-gate case TCPS_IDLE:
43067c478bd9Sstevel@tonic-gate case TCPS_BOUND:
43077c478bd9Sstevel@tonic-gate case TCPS_LISTEN:
43087c478bd9Sstevel@tonic-gate return;
43097c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
43107c478bd9Sstevel@tonic-gate case TCPS_SYN_SENT:
43117c478bd9Sstevel@tonic-gate first_threshold = tcp->tcp_first_ctimer_threshold;
43127c478bd9Sstevel@tonic-gate second_threshold = tcp->tcp_second_ctimer_threshold;
43137c478bd9Sstevel@tonic-gate break;
43147c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
43157c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
43167c478bd9Sstevel@tonic-gate case TCPS_CLOSING:
43177c478bd9Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
43187c478bd9Sstevel@tonic-gate case TCPS_LAST_ACK:
43197c478bd9Sstevel@tonic-gate /* If we have data to rexmit */
43207c478bd9Sstevel@tonic-gate if (tcp->tcp_suna != tcp->tcp_snxt) {
43217c478bd9Sstevel@tonic-gate int32_t time_to_wait;
43227c478bd9Sstevel@tonic-gate
43237c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpTimRetrans);
43247c478bd9Sstevel@tonic-gate if (tcp->tcp_xmit_head == NULL)
43257c478bd9Sstevel@tonic-gate break;
432653391bafSeota /* use uintptr_t to suppress the gcc warning */
43277c478bd9Sstevel@tonic-gate time_to_wait = (int32_t)(prom_gettime() -
432853391bafSeota (uint32_t)(uintptr_t)tcp->tcp_xmit_head->b_prev);
43297c478bd9Sstevel@tonic-gate time_to_wait = tcp->tcp_rto - time_to_wait;
43307c478bd9Sstevel@tonic-gate if (time_to_wait > 0) {
43317c478bd9Sstevel@tonic-gate /*
43327c478bd9Sstevel@tonic-gate * Timer fired too early, so restart it.
43337c478bd9Sstevel@tonic-gate */
43347c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, time_to_wait);
43357c478bd9Sstevel@tonic-gate return;
43367c478bd9Sstevel@tonic-gate }
43377c478bd9Sstevel@tonic-gate /*
43387c478bd9Sstevel@tonic-gate * When we probe zero windows, we force the swnd open.
43397c478bd9Sstevel@tonic-gate * If our peer acks with a closed window swnd will be
43407c478bd9Sstevel@tonic-gate * set to zero by tcp_rput(). As long as we are
43417c478bd9Sstevel@tonic-gate * receiving acks tcp_rput will
43427c478bd9Sstevel@tonic-gate * reset 'tcp_ms_we_have_waited' so as not to trip the
43437c478bd9Sstevel@tonic-gate * first and second interval actions. NOTE: the timer
43447c478bd9Sstevel@tonic-gate * interval is allowed to continue its exponential
43457c478bd9Sstevel@tonic-gate * backoff.
43467c478bd9Sstevel@tonic-gate */
43477c478bd9Sstevel@tonic-gate if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) {
43487c478bd9Sstevel@tonic-gate DEBUG_1("tcp_timer (%d): zero win", sock_id);
43497c478bd9Sstevel@tonic-gate break;
43507c478bd9Sstevel@tonic-gate } else {
43517c478bd9Sstevel@tonic-gate /*
43527c478bd9Sstevel@tonic-gate * After retransmission, we need to do
43537c478bd9Sstevel@tonic-gate * slow start. Set the ssthresh to one
43547c478bd9Sstevel@tonic-gate * half of current effective window and
43557c478bd9Sstevel@tonic-gate * cwnd to one MSS. Also reset
43567c478bd9Sstevel@tonic-gate * tcp_cwnd_cnt.
43577c478bd9Sstevel@tonic-gate *
43587c478bd9Sstevel@tonic-gate * Note that if tcp_ssthresh is reduced because
43597c478bd9Sstevel@tonic-gate * of ECN, do not reduce it again unless it is
43607c478bd9Sstevel@tonic-gate * already one window of data away (tcp_cwr
43617c478bd9Sstevel@tonic-gate * should then be cleared) or this is a
43627c478bd9Sstevel@tonic-gate * timeout for a retransmitted segment.
43637c478bd9Sstevel@tonic-gate */
43647c478bd9Sstevel@tonic-gate uint32_t npkt;
43657c478bd9Sstevel@tonic-gate
43667c478bd9Sstevel@tonic-gate if (!tcp->tcp_cwr || tcp->tcp_rexmit) {
43677c478bd9Sstevel@tonic-gate npkt = (MIN((tcp->tcp_timer_backoff ?
43687c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh :
43697c478bd9Sstevel@tonic-gate tcp->tcp_cwnd),
43707c478bd9Sstevel@tonic-gate tcp->tcp_swnd) >> 1) /
43717c478bd9Sstevel@tonic-gate tcp->tcp_mss;
43727c478bd9Sstevel@tonic-gate if (npkt < 2)
43737c478bd9Sstevel@tonic-gate npkt = 2;
43747c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = npkt *
43757c478bd9Sstevel@tonic-gate tcp->tcp_mss;
43767c478bd9Sstevel@tonic-gate }
43777c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
43787c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
43797c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
43807c478bd9Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
43817c478bd9Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
43827c478bd9Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
43837c478bd9Sstevel@tonic-gate }
43847c478bd9Sstevel@tonic-gate }
43857c478bd9Sstevel@tonic-gate break;
43867c478bd9Sstevel@tonic-gate }
43877c478bd9Sstevel@tonic-gate /*
43887c478bd9Sstevel@tonic-gate * We have something to send yet we cannot send. The
43897c478bd9Sstevel@tonic-gate * reason can be:
43907c478bd9Sstevel@tonic-gate *
43917c478bd9Sstevel@tonic-gate * 1. Zero send window: we need to do zero window probe.
43927c478bd9Sstevel@tonic-gate * 2. Zero cwnd: because of ECN, we need to "clock out
43937c478bd9Sstevel@tonic-gate * segments.
43947c478bd9Sstevel@tonic-gate * 3. SWS avoidance: receiver may have shrunk window,
43957c478bd9Sstevel@tonic-gate * reset our knowledge.
43967c478bd9Sstevel@tonic-gate *
43977c478bd9Sstevel@tonic-gate * Note that condition 2 can happen with either 1 or
43987c478bd9Sstevel@tonic-gate * 3. But 1 and 3 are exclusive.
43997c478bd9Sstevel@tonic-gate */
44007c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent != 0) {
44017c478bd9Sstevel@tonic-gate if (tcp->tcp_cwnd == 0) {
44027c478bd9Sstevel@tonic-gate /*
44037c478bd9Sstevel@tonic-gate * Set tcp_cwnd to 1 MSS so that a
44047c478bd9Sstevel@tonic-gate * new segment can be sent out. We
44057c478bd9Sstevel@tonic-gate * are "clocking out" new data when
44067c478bd9Sstevel@tonic-gate * the network is really congested.
44077c478bd9Sstevel@tonic-gate */
44087c478bd9Sstevel@tonic-gate assert(tcp->tcp_ecn_ok);
44097c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
44107c478bd9Sstevel@tonic-gate }
44117c478bd9Sstevel@tonic-gate if (tcp->tcp_swnd == 0) {
44127c478bd9Sstevel@tonic-gate /* Extend window for zero window probe */
44137c478bd9Sstevel@tonic-gate tcp->tcp_swnd++;
44147c478bd9Sstevel@tonic-gate tcp->tcp_zero_win_probe = B_TRUE;
44157c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutWinProbe);
44167c478bd9Sstevel@tonic-gate } else {
44177c478bd9Sstevel@tonic-gate /*
44187c478bd9Sstevel@tonic-gate * Handle timeout from sender SWS avoidance.
44197c478bd9Sstevel@tonic-gate * Reset our knowledge of the max send window
44207c478bd9Sstevel@tonic-gate * since the receiver might have reduced its
44217c478bd9Sstevel@tonic-gate * receive buffer. Avoid setting tcp_max_swnd
44227c478bd9Sstevel@tonic-gate * to one since that will essentially disable
44237c478bd9Sstevel@tonic-gate * the SWS checks.
44247c478bd9Sstevel@tonic-gate *
44257c478bd9Sstevel@tonic-gate * Note that since we don't have a SWS
44267c478bd9Sstevel@tonic-gate * state variable, if the timeout is set
44277c478bd9Sstevel@tonic-gate * for ECN but not for SWS, this
44287c478bd9Sstevel@tonic-gate * code will also be executed. This is
44297c478bd9Sstevel@tonic-gate * fine as tcp_max_swnd is updated
44307c478bd9Sstevel@tonic-gate * constantly and it will not affect
44317c478bd9Sstevel@tonic-gate * anything.
44327c478bd9Sstevel@tonic-gate */
44337c478bd9Sstevel@tonic-gate tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2);
44347c478bd9Sstevel@tonic-gate }
44357c478bd9Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
44367c478bd9Sstevel@tonic-gate return;
44377c478bd9Sstevel@tonic-gate }
44387c478bd9Sstevel@tonic-gate /* Is there a FIN that needs to be to re retransmitted? */
44397c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
44407c478bd9Sstevel@tonic-gate !tcp->tcp_fin_acked)
44417c478bd9Sstevel@tonic-gate break;
44427c478bd9Sstevel@tonic-gate /* Nothing to do, return without restarting timer. */
44437c478bd9Sstevel@tonic-gate return;
44447c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
44457c478bd9Sstevel@tonic-gate /*
44467c478bd9Sstevel@tonic-gate * User closed the TCP endpoint and peer ACK'ed our FIN.
44477c478bd9Sstevel@tonic-gate * We waited some time for for peer's FIN, but it hasn't
44487c478bd9Sstevel@tonic-gate * arrived. We flush the connection now to avoid
44497c478bd9Sstevel@tonic-gate * case where the peer has rebooted.
44507c478bd9Sstevel@tonic-gate */
44517c478bd9Sstevel@tonic-gate /* FALLTHRU */
44527c478bd9Sstevel@tonic-gate case TCPS_TIME_WAIT:
44537c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
44547c478bd9Sstevel@tonic-gate return;
44557c478bd9Sstevel@tonic-gate default:
44567c478bd9Sstevel@tonic-gate DEBUG_3("tcp_timer (%d): strange state (%d) %s", sock_id,
44577c478bd9Sstevel@tonic-gate tcp->tcp_state, tcp_display(tcp, NULL,
44587c478bd9Sstevel@tonic-gate DISP_PORT_ONLY));
44597c478bd9Sstevel@tonic-gate return;
44607c478bd9Sstevel@tonic-gate }
44617c478bd9Sstevel@tonic-gate if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) {
44627c478bd9Sstevel@tonic-gate /*
44637c478bd9Sstevel@tonic-gate * For zero window probe, we need to send indefinitely,
44647c478bd9Sstevel@tonic-gate * unless we have not heard from the other side for some
44657c478bd9Sstevel@tonic-gate * time...
44667c478bd9Sstevel@tonic-gate */
44677c478bd9Sstevel@tonic-gate if ((tcp->tcp_zero_win_probe == 0) ||
44687c478bd9Sstevel@tonic-gate ((prom_gettime() - tcp->tcp_last_recv_time) >
44697c478bd9Sstevel@tonic-gate second_threshold)) {
44707c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpTimRetransDrop);
44717c478bd9Sstevel@tonic-gate /*
44727c478bd9Sstevel@tonic-gate * If TCP is in SYN_RCVD state, send back a
44737c478bd9Sstevel@tonic-gate * RST|ACK as BSD does. Note that tcp_zero_win_probe
44747c478bd9Sstevel@tonic-gate * should be zero in TCPS_SYN_RCVD state.
44757c478bd9Sstevel@tonic-gate */
44767c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_RCVD) {
44777c478bd9Sstevel@tonic-gate tcp_xmit_ctl("tcp_timer: RST sent on timeout "
44787c478bd9Sstevel@tonic-gate "in SYN_RCVD",
44797c478bd9Sstevel@tonic-gate tcp, NULL, tcp->tcp_snxt,
44807c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_RST | TH_ACK, 0, sock_id);
44817c478bd9Sstevel@tonic-gate }
44827c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp,
44837c478bd9Sstevel@tonic-gate tcp->tcp_client_errno ?
44847c478bd9Sstevel@tonic-gate tcp->tcp_client_errno : ETIMEDOUT);
44857c478bd9Sstevel@tonic-gate return;
44867c478bd9Sstevel@tonic-gate } else {
44877c478bd9Sstevel@tonic-gate /*
44887c478bd9Sstevel@tonic-gate * Set tcp_ms_we_have_waited to second_threshold
44897c478bd9Sstevel@tonic-gate * so that in next timeout, we will do the above
44907c478bd9Sstevel@tonic-gate * check (lbolt - tcp_last_recv_time). This is
44917c478bd9Sstevel@tonic-gate * also to avoid overflow.
44927c478bd9Sstevel@tonic-gate *
44937c478bd9Sstevel@tonic-gate * We don't need to decrement tcp_timer_backoff
44947c478bd9Sstevel@tonic-gate * to avoid overflow because it will be decremented
44957c478bd9Sstevel@tonic-gate * later if new timeout value is greater than
44967c478bd9Sstevel@tonic-gate * tcp_rexmit_interval_max. In the case when
44977c478bd9Sstevel@tonic-gate * tcp_rexmit_interval_max is greater than
44987c478bd9Sstevel@tonic-gate * second_threshold, it means that we will wait
44997c478bd9Sstevel@tonic-gate * longer than second_threshold to send the next
45007c478bd9Sstevel@tonic-gate * window probe.
45017c478bd9Sstevel@tonic-gate */
45027c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = second_threshold;
45037c478bd9Sstevel@tonic-gate }
45047c478bd9Sstevel@tonic-gate } else if (ms > first_threshold && tcp->tcp_rtt_sa != 0) {
45057c478bd9Sstevel@tonic-gate /*
45067c478bd9Sstevel@tonic-gate * We have been retransmitting for too long... The RTT
45077c478bd9Sstevel@tonic-gate * we calculated is probably incorrect. Reinitialize it.
45087c478bd9Sstevel@tonic-gate * Need to compensate for 0 tcp_rtt_sa. Reset
45097c478bd9Sstevel@tonic-gate * tcp_rtt_update so that we won't accidentally cache a
45107c478bd9Sstevel@tonic-gate * bad value. But only do this if this is not a zero
45117c478bd9Sstevel@tonic-gate * window probe.
45127c478bd9Sstevel@tonic-gate */
45137c478bd9Sstevel@tonic-gate if (tcp->tcp_zero_win_probe == 0) {
45147c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) +
45157c478bd9Sstevel@tonic-gate (tcp->tcp_rtt_sa >> 5);
45167c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sa = 0;
45177c478bd9Sstevel@tonic-gate tcp->tcp_rtt_update = 0;
45187c478bd9Sstevel@tonic-gate }
45197c478bd9Sstevel@tonic-gate }
45207c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff++;
45217c478bd9Sstevel@tonic-gate if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
45227c478bd9Sstevel@tonic-gate tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) <
45237c478bd9Sstevel@tonic-gate tcp_rexmit_interval_min) {
45247c478bd9Sstevel@tonic-gate /*
45257c478bd9Sstevel@tonic-gate * This means the original RTO is tcp_rexmit_interval_min.
45267c478bd9Sstevel@tonic-gate * So we will use tcp_rexmit_interval_min as the RTO value
45277c478bd9Sstevel@tonic-gate * and do the backoff.
45287c478bd9Sstevel@tonic-gate */
45297c478bd9Sstevel@tonic-gate ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff;
45307c478bd9Sstevel@tonic-gate } else {
45317c478bd9Sstevel@tonic-gate ms <<= tcp->tcp_timer_backoff;
45327c478bd9Sstevel@tonic-gate }
45337c478bd9Sstevel@tonic-gate if (ms > tcp_rexmit_interval_max) {
45347c478bd9Sstevel@tonic-gate ms = tcp_rexmit_interval_max;
45357c478bd9Sstevel@tonic-gate /*
45367c478bd9Sstevel@tonic-gate * ms is at max, decrement tcp_timer_backoff to avoid
45377c478bd9Sstevel@tonic-gate * overflow.
45387c478bd9Sstevel@tonic-gate */
45397c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff--;
45407c478bd9Sstevel@tonic-gate }
45417c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited += ms;
45427c478bd9Sstevel@tonic-gate if (tcp->tcp_zero_win_probe == 0) {
45437c478bd9Sstevel@tonic-gate tcp->tcp_rto = ms;
45447c478bd9Sstevel@tonic-gate }
45457c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, ms);
45467c478bd9Sstevel@tonic-gate /*
45477c478bd9Sstevel@tonic-gate * This is after a timeout and tcp_rto is backed off. Set
45487c478bd9Sstevel@tonic-gate * tcp_set_timer to 1 so that next time RTO is updated, we will
45497c478bd9Sstevel@tonic-gate * restart the timer with a correct value.
45507c478bd9Sstevel@tonic-gate */
45517c478bd9Sstevel@tonic-gate tcp->tcp_set_timer = 1;
45527c478bd9Sstevel@tonic-gate mss = tcp->tcp_snxt - tcp->tcp_suna;
45537c478bd9Sstevel@tonic-gate if (mss > tcp->tcp_mss)
45547c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss;
45557c478bd9Sstevel@tonic-gate if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0)
45567c478bd9Sstevel@tonic-gate mss = tcp->tcp_swnd;
45577c478bd9Sstevel@tonic-gate
455853391bafSeota if ((mp = tcp->tcp_xmit_head) != NULL) {
455953391bafSeota /* use uintptr_t to suppress the gcc warning */
456053391bafSeota mp->b_prev = (mblk_t *)(uintptr_t)prom_gettime();
456153391bafSeota }
45627c478bd9Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss,
45637c478bd9Sstevel@tonic-gate B_TRUE);
45647c478bd9Sstevel@tonic-gate if (mp == NULL)
45657c478bd9Sstevel@tonic-gate return;
45667c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
45677c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
45687c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, mss);
45697c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
45707c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_timer", mp);
45717c478bd9Sstevel@tonic-gate
45727c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
45737c478bd9Sstevel@tonic-gate freeb(mp);
45747c478bd9Sstevel@tonic-gate
45757c478bd9Sstevel@tonic-gate /*
45767c478bd9Sstevel@tonic-gate * When slow start after retransmission begins, start with
45777c478bd9Sstevel@tonic-gate * this seq no. tcp_rexmit_max marks the end of special slow
45787c478bd9Sstevel@tonic-gate * start phase. tcp_snd_burst controls how many segments
45797c478bd9Sstevel@tonic-gate * can be sent because of an ack.
45807c478bd9Sstevel@tonic-gate */
45817c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_suna;
45827c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_SS;
45837c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
45847c478bd9Sstevel@tonic-gate (tcp->tcp_unsent == 0)) {
45857c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_fss;
45867c478bd9Sstevel@tonic-gate } else {
45877c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
45887c478bd9Sstevel@tonic-gate }
45897c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_TRUE;
45907c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
45917c478bd9Sstevel@tonic-gate
45927c478bd9Sstevel@tonic-gate /*
45937c478bd9Sstevel@tonic-gate * Remove all rexmit SACK blk to start from fresh.
45947c478bd9Sstevel@tonic-gate */
45957c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
45967c478bd9Sstevel@tonic-gate TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list);
45977c478bd9Sstevel@tonic-gate tcp->tcp_num_notsack_blk = 0;
45987c478bd9Sstevel@tonic-gate tcp->tcp_cnt_notsack_list = 0;
45997c478bd9Sstevel@tonic-gate }
46007c478bd9Sstevel@tonic-gate }
46017c478bd9Sstevel@tonic-gate
46027c478bd9Sstevel@tonic-gate /*
46037c478bd9Sstevel@tonic-gate * The TCP normal data output path.
46047c478bd9Sstevel@tonic-gate * NOTE: the logic of the fast path is duplicated from this function.
46057c478bd9Sstevel@tonic-gate */
46067c478bd9Sstevel@tonic-gate static void
tcp_wput_data(tcp_t * tcp,mblk_t * mp,int sock_id)46077c478bd9Sstevel@tonic-gate tcp_wput_data(tcp_t *tcp, mblk_t *mp, int sock_id)
46087c478bd9Sstevel@tonic-gate {
46097c478bd9Sstevel@tonic-gate int len;
46107c478bd9Sstevel@tonic-gate mblk_t *local_time;
46117c478bd9Sstevel@tonic-gate mblk_t *mp1;
46127c478bd9Sstevel@tonic-gate uchar_t *rptr;
46137c478bd9Sstevel@tonic-gate uint32_t snxt;
46147c478bd9Sstevel@tonic-gate int tail_unsent;
46157c478bd9Sstevel@tonic-gate int tcpstate;
46167c478bd9Sstevel@tonic-gate int usable = 0;
46177c478bd9Sstevel@tonic-gate mblk_t *xmit_tail;
46187c478bd9Sstevel@tonic-gate int32_t num_burst_seg;
46197c478bd9Sstevel@tonic-gate int32_t mss;
46207c478bd9Sstevel@tonic-gate int32_t num_sack_blk = 0;
46217c478bd9Sstevel@tonic-gate int32_t tcp_hdr_len;
46227c478bd9Sstevel@tonic-gate ipaddr_t *dst;
46237c478bd9Sstevel@tonic-gate ipaddr_t *src;
46247c478bd9Sstevel@tonic-gate
46257c478bd9Sstevel@tonic-gate #ifdef DEBUG
46267c478bd9Sstevel@tonic-gate printf("tcp_wput_data(%d) ##############################\n", sock_id);
46277c478bd9Sstevel@tonic-gate #endif
46287c478bd9Sstevel@tonic-gate tcpstate = tcp->tcp_state;
46297c478bd9Sstevel@tonic-gate if (mp == NULL) {
46307c478bd9Sstevel@tonic-gate /* Really tacky... but we need this for detached closes. */
46317c478bd9Sstevel@tonic-gate len = tcp->tcp_unsent;
46327c478bd9Sstevel@tonic-gate goto data_null;
46337c478bd9Sstevel@tonic-gate }
46347c478bd9Sstevel@tonic-gate
46357c478bd9Sstevel@tonic-gate /*
46367c478bd9Sstevel@tonic-gate * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
46377c478bd9Sstevel@tonic-gate * or before a connection attempt has begun.
46387c478bd9Sstevel@tonic-gate *
46397c478bd9Sstevel@tonic-gate * The following should not happen in inetboot....
46407c478bd9Sstevel@tonic-gate */
46417c478bd9Sstevel@tonic-gate if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT ||
46427c478bd9Sstevel@tonic-gate (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
46437c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
46447c478bd9Sstevel@tonic-gate printf("tcp_wput_data: data after ordrel, %s\n",
46457c478bd9Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
46467c478bd9Sstevel@tonic-gate }
46477c478bd9Sstevel@tonic-gate freemsg(mp);
46487c478bd9Sstevel@tonic-gate return;
46497c478bd9Sstevel@tonic-gate }
46507c478bd9Sstevel@tonic-gate
46517c478bd9Sstevel@tonic-gate /* Strip empties */
46527c478bd9Sstevel@tonic-gate for (;;) {
46537c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
46547c478bd9Sstevel@tonic-gate (uintptr_t)INT_MAX);
46557c478bd9Sstevel@tonic-gate len = (int)(mp->b_wptr - mp->b_rptr);
46567c478bd9Sstevel@tonic-gate if (len > 0)
46577c478bd9Sstevel@tonic-gate break;
46587c478bd9Sstevel@tonic-gate mp1 = mp;
46597c478bd9Sstevel@tonic-gate mp = mp->b_cont;
46607c478bd9Sstevel@tonic-gate freeb(mp1);
46617c478bd9Sstevel@tonic-gate if (mp == NULL) {
46627c478bd9Sstevel@tonic-gate return;
46637c478bd9Sstevel@tonic-gate }
46647c478bd9Sstevel@tonic-gate }
46657c478bd9Sstevel@tonic-gate
46667c478bd9Sstevel@tonic-gate /* If we are the first on the list ... */
46677c478bd9Sstevel@tonic-gate if (tcp->tcp_xmit_head == NULL) {
46687c478bd9Sstevel@tonic-gate tcp->tcp_xmit_head = mp;
46697c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail = mp;
46707c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = len;
46717c478bd9Sstevel@tonic-gate } else {
46727c478bd9Sstevel@tonic-gate tcp->tcp_xmit_last->b_cont = mp;
46737c478bd9Sstevel@tonic-gate len += tcp->tcp_unsent;
46747c478bd9Sstevel@tonic-gate }
46757c478bd9Sstevel@tonic-gate
46767c478bd9Sstevel@tonic-gate /* Tack on however many more positive length mblks we have */
46777c478bd9Sstevel@tonic-gate if ((mp1 = mp->b_cont) != NULL) {
46787c478bd9Sstevel@tonic-gate do {
46797c478bd9Sstevel@tonic-gate int tlen;
46807c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
46817c478bd9Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
46827c478bd9Sstevel@tonic-gate tlen = (int)(mp1->b_wptr - mp1->b_rptr);
46837c478bd9Sstevel@tonic-gate if (tlen <= 0) {
46847c478bd9Sstevel@tonic-gate mp->b_cont = mp1->b_cont;
46857c478bd9Sstevel@tonic-gate freeb(mp1);
46867c478bd9Sstevel@tonic-gate } else {
46877c478bd9Sstevel@tonic-gate len += tlen;
46887c478bd9Sstevel@tonic-gate mp = mp1;
46897c478bd9Sstevel@tonic-gate }
46907c478bd9Sstevel@tonic-gate } while ((mp1 = mp->b_cont) != NULL);
46917c478bd9Sstevel@tonic-gate }
46927c478bd9Sstevel@tonic-gate tcp->tcp_xmit_last = mp;
46937c478bd9Sstevel@tonic-gate tcp->tcp_unsent = len;
46947c478bd9Sstevel@tonic-gate
46957c478bd9Sstevel@tonic-gate data_null:
46967c478bd9Sstevel@tonic-gate snxt = tcp->tcp_snxt;
46977c478bd9Sstevel@tonic-gate xmit_tail = tcp->tcp_xmit_tail;
46987c478bd9Sstevel@tonic-gate tail_unsent = tcp->tcp_xmit_tail_unsent;
46997c478bd9Sstevel@tonic-gate
47007c478bd9Sstevel@tonic-gate /*
47017c478bd9Sstevel@tonic-gate * Note that tcp_mss has been adjusted to take into account the
47027c478bd9Sstevel@tonic-gate * timestamp option if applicable. Because SACK options do not
47037c478bd9Sstevel@tonic-gate * appear in every TCP segments and they are of variable lengths,
47047c478bd9Sstevel@tonic-gate * they cannot be included in tcp_mss. Thus we need to calculate
47057c478bd9Sstevel@tonic-gate * the actual segment length when we need to send a segment which
47067c478bd9Sstevel@tonic-gate * includes SACK options.
47077c478bd9Sstevel@tonic-gate */
47087c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
47097c478bd9Sstevel@tonic-gate int32_t opt_len;
47107c478bd9Sstevel@tonic-gate
47117c478bd9Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
47127c478bd9Sstevel@tonic-gate tcp->tcp_num_sack_blk);
47137c478bd9Sstevel@tonic-gate opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
47147c478bd9Sstevel@tonic-gate 2 + TCPOPT_HEADER_LEN;
47157c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss - opt_len;
47167c478bd9Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len + opt_len;
47177c478bd9Sstevel@tonic-gate } else {
47187c478bd9Sstevel@tonic-gate mss = tcp->tcp_mss;
47197c478bd9Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
47207c478bd9Sstevel@tonic-gate }
47217c478bd9Sstevel@tonic-gate
47227c478bd9Sstevel@tonic-gate if ((tcp->tcp_suna == snxt) &&
47237c478bd9Sstevel@tonic-gate (prom_gettime() - tcp->tcp_last_recv_time) >= tcp->tcp_rto) {
47247c478bd9Sstevel@tonic-gate tcp->tcp_cwnd = MIN(tcp_slow_start_after_idle * mss,
47257c478bd9Sstevel@tonic-gate MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss)));
47267c478bd9Sstevel@tonic-gate }
47277c478bd9Sstevel@tonic-gate if (tcpstate == TCPS_SYN_RCVD) {
47287c478bd9Sstevel@tonic-gate /*
47297c478bd9Sstevel@tonic-gate * The three-way connection establishment handshake is not
47307c478bd9Sstevel@tonic-gate * complete yet. We want to queue the data for transmission
47317c478bd9Sstevel@tonic-gate * after entering ESTABLISHED state (RFC793). Setting usable to
47327c478bd9Sstevel@tonic-gate * zero cause a jump to "done" label effectively leaving data
47337c478bd9Sstevel@tonic-gate * on the queue.
47347c478bd9Sstevel@tonic-gate */
47357c478bd9Sstevel@tonic-gate
47367c478bd9Sstevel@tonic-gate usable = 0;
47377c478bd9Sstevel@tonic-gate } else {
47387c478bd9Sstevel@tonic-gate int usable_r = tcp->tcp_swnd;
47397c478bd9Sstevel@tonic-gate
47407c478bd9Sstevel@tonic-gate /*
47417c478bd9Sstevel@tonic-gate * In the special case when cwnd is zero, which can only
47427c478bd9Sstevel@tonic-gate * happen if the connection is ECN capable, return now.
47437c478bd9Sstevel@tonic-gate * New segments is sent using tcp_timer(). The timer
47447c478bd9Sstevel@tonic-gate * is set in tcp_rput_data().
47457c478bd9Sstevel@tonic-gate */
47467c478bd9Sstevel@tonic-gate if (tcp->tcp_cwnd == 0) {
47477c478bd9Sstevel@tonic-gate /*
47487c478bd9Sstevel@tonic-gate * Note that tcp_cwnd is 0 before 3-way handshake is
47497c478bd9Sstevel@tonic-gate * finished.
47507c478bd9Sstevel@tonic-gate */
47517c478bd9Sstevel@tonic-gate assert(tcp->tcp_ecn_ok ||
47527c478bd9Sstevel@tonic-gate tcp->tcp_state < TCPS_ESTABLISHED);
47537c478bd9Sstevel@tonic-gate return;
47547c478bd9Sstevel@tonic-gate }
47557c478bd9Sstevel@tonic-gate
47567c478bd9Sstevel@tonic-gate /* usable = MIN(swnd, cwnd) - unacked_bytes */
47577c478bd9Sstevel@tonic-gate if (usable_r > tcp->tcp_cwnd)
47587c478bd9Sstevel@tonic-gate usable_r = tcp->tcp_cwnd;
47597c478bd9Sstevel@tonic-gate
47607c478bd9Sstevel@tonic-gate /* NOTE: trouble if xmitting while SYN not acked? */
47617c478bd9Sstevel@tonic-gate usable_r -= snxt;
47627c478bd9Sstevel@tonic-gate usable_r += tcp->tcp_suna;
47637c478bd9Sstevel@tonic-gate
47647c478bd9Sstevel@tonic-gate /* usable = MIN(usable, unsent) */
47657c478bd9Sstevel@tonic-gate if (usable_r > len)
47667c478bd9Sstevel@tonic-gate usable_r = len;
47677c478bd9Sstevel@tonic-gate
47687c478bd9Sstevel@tonic-gate /* usable = MAX(usable, {1 for urgent, 0 for data}) */
47697c478bd9Sstevel@tonic-gate if (usable_r != 0)
47707c478bd9Sstevel@tonic-gate usable = usable_r;
47717c478bd9Sstevel@tonic-gate }
47727c478bd9Sstevel@tonic-gate
477353391bafSeota /* use uintptr_t to suppress the gcc warning */
477453391bafSeota local_time = (mblk_t *)(uintptr_t)prom_gettime();
47757c478bd9Sstevel@tonic-gate
47767c478bd9Sstevel@tonic-gate /*
47777c478bd9Sstevel@tonic-gate * "Our" Nagle Algorithm. This is not the same as in the old
47787c478bd9Sstevel@tonic-gate * BSD. This is more in line with the true intent of Nagle.
47797c478bd9Sstevel@tonic-gate *
47807c478bd9Sstevel@tonic-gate * The conditions are:
47817c478bd9Sstevel@tonic-gate * 1. The amount of unsent data (or amount of data which can be
47827c478bd9Sstevel@tonic-gate * sent, whichever is smaller) is less than Nagle limit.
47837c478bd9Sstevel@tonic-gate * 2. The last sent size is also less than Nagle limit.
47847c478bd9Sstevel@tonic-gate * 3. There is unack'ed data.
47857c478bd9Sstevel@tonic-gate * 4. Urgent pointer is not set. Send urgent data ignoring the
47867c478bd9Sstevel@tonic-gate * Nagle algorithm. This reduces the probability that urgent
47877c478bd9Sstevel@tonic-gate * bytes get "merged" together.
47887c478bd9Sstevel@tonic-gate * 5. The app has not closed the connection. This eliminates the
47897c478bd9Sstevel@tonic-gate * wait time of the receiving side waiting for the last piece of
47907c478bd9Sstevel@tonic-gate * (small) data.
47917c478bd9Sstevel@tonic-gate *
47927c478bd9Sstevel@tonic-gate * If all are satisified, exit without sending anything. Note
47937c478bd9Sstevel@tonic-gate * that Nagle limit can be smaller than 1 MSS. Nagle limit is
47947c478bd9Sstevel@tonic-gate * the smaller of 1 MSS and global tcp_naglim_def (default to be
47957c478bd9Sstevel@tonic-gate * 4095).
47967c478bd9Sstevel@tonic-gate */
47977c478bd9Sstevel@tonic-gate if (usable < (int)tcp->tcp_naglim &&
47987c478bd9Sstevel@tonic-gate tcp->tcp_naglim > tcp->tcp_last_sent_len &&
47997c478bd9Sstevel@tonic-gate snxt != tcp->tcp_suna &&
48007c478bd9Sstevel@tonic-gate !(tcp->tcp_valid_bits & TCP_URG_VALID))
48017c478bd9Sstevel@tonic-gate goto done;
48027c478bd9Sstevel@tonic-gate
48037c478bd9Sstevel@tonic-gate num_burst_seg = tcp->tcp_snd_burst;
48047c478bd9Sstevel@tonic-gate for (;;) {
48057c478bd9Sstevel@tonic-gate tcph_t *tcph;
48067c478bd9Sstevel@tonic-gate mblk_t *new_mp;
48077c478bd9Sstevel@tonic-gate
48087c478bd9Sstevel@tonic-gate if (num_burst_seg-- == 0)
48097c478bd9Sstevel@tonic-gate goto done;
48107c478bd9Sstevel@tonic-gate
48117c478bd9Sstevel@tonic-gate len = mss;
48127c478bd9Sstevel@tonic-gate if (len > usable) {
48137c478bd9Sstevel@tonic-gate len = usable;
48147c478bd9Sstevel@tonic-gate if (len <= 0) {
48157c478bd9Sstevel@tonic-gate /* Terminate the loop */
48167c478bd9Sstevel@tonic-gate goto done;
48177c478bd9Sstevel@tonic-gate }
48187c478bd9Sstevel@tonic-gate /*
48197c478bd9Sstevel@tonic-gate * Sender silly-window avoidance.
48207c478bd9Sstevel@tonic-gate * Ignore this if we are going to send a
48217c478bd9Sstevel@tonic-gate * zero window probe out.
48227c478bd9Sstevel@tonic-gate *
48237c478bd9Sstevel@tonic-gate * TODO: force data into microscopic window ??
48247c478bd9Sstevel@tonic-gate * ==> (!pushed || (unsent > usable))
48257c478bd9Sstevel@tonic-gate */
48267c478bd9Sstevel@tonic-gate if (len < (tcp->tcp_max_swnd >> 1) &&
48277c478bd9Sstevel@tonic-gate (tcp->tcp_unsent - (snxt - tcp->tcp_snxt)) > len &&
48287c478bd9Sstevel@tonic-gate !((tcp->tcp_valid_bits & TCP_URG_VALID) &&
48297c478bd9Sstevel@tonic-gate len == 1) && (! tcp->tcp_zero_win_probe)) {
48307c478bd9Sstevel@tonic-gate /*
48317c478bd9Sstevel@tonic-gate * If the retransmit timer is not running
48327c478bd9Sstevel@tonic-gate * we start it so that we will retransmit
48337c478bd9Sstevel@tonic-gate * in the case when the the receiver has
48347c478bd9Sstevel@tonic-gate * decremented the window.
48357c478bd9Sstevel@tonic-gate */
48367c478bd9Sstevel@tonic-gate if (snxt == tcp->tcp_snxt &&
48377c478bd9Sstevel@tonic-gate snxt == tcp->tcp_suna) {
48387c478bd9Sstevel@tonic-gate /*
48397c478bd9Sstevel@tonic-gate * We are not supposed to send
48407c478bd9Sstevel@tonic-gate * anything. So let's wait a little
48417c478bd9Sstevel@tonic-gate * bit longer before breaking SWS
48427c478bd9Sstevel@tonic-gate * avoidance.
48437c478bd9Sstevel@tonic-gate *
48447c478bd9Sstevel@tonic-gate * What should the value be?
48457c478bd9Sstevel@tonic-gate * Suggestion: MAX(init rexmit time,
48467c478bd9Sstevel@tonic-gate * tcp->tcp_rto)
48477c478bd9Sstevel@tonic-gate */
48487c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
48497c478bd9Sstevel@tonic-gate }
48507c478bd9Sstevel@tonic-gate goto done;
48517c478bd9Sstevel@tonic-gate }
48527c478bd9Sstevel@tonic-gate }
48537c478bd9Sstevel@tonic-gate
48547c478bd9Sstevel@tonic-gate tcph = tcp->tcp_tcph;
48557c478bd9Sstevel@tonic-gate
48567c478bd9Sstevel@tonic-gate usable -= len; /* Approximate - can be adjusted later */
48577c478bd9Sstevel@tonic-gate if (usable > 0)
48587c478bd9Sstevel@tonic-gate tcph->th_flags[0] = TH_ACK;
48597c478bd9Sstevel@tonic-gate else
48607c478bd9Sstevel@tonic-gate tcph->th_flags[0] = (TH_ACK | TH_PUSH);
48617c478bd9Sstevel@tonic-gate
48627c478bd9Sstevel@tonic-gate U32_TO_ABE32(snxt, tcph->th_seq);
48637c478bd9Sstevel@tonic-gate
48647c478bd9Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
48657c478bd9Sstevel@tonic-gate uchar_t *prev_rptr = xmit_tail->b_rptr;
48667c478bd9Sstevel@tonic-gate uint32_t prev_snxt = tcp->tcp_snxt;
48677c478bd9Sstevel@tonic-gate
48687c478bd9Sstevel@tonic-gate if (tail_unsent == 0) {
48697c478bd9Sstevel@tonic-gate assert(xmit_tail->b_cont != NULL);
48707c478bd9Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
48717c478bd9Sstevel@tonic-gate prev_rptr = xmit_tail->b_rptr;
48727c478bd9Sstevel@tonic-gate tail_unsent = (int)(xmit_tail->b_wptr -
48737c478bd9Sstevel@tonic-gate xmit_tail->b_rptr);
48747c478bd9Sstevel@tonic-gate } else {
48757c478bd9Sstevel@tonic-gate xmit_tail->b_rptr = xmit_tail->b_wptr -
48767c478bd9Sstevel@tonic-gate tail_unsent;
48777c478bd9Sstevel@tonic-gate }
48787c478bd9Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, xmit_tail, len, NULL, NULL,
48797c478bd9Sstevel@tonic-gate snxt, B_FALSE, (uint32_t *)&len, B_FALSE);
48807c478bd9Sstevel@tonic-gate /* Restore tcp_snxt so we get amount sent right. */
48817c478bd9Sstevel@tonic-gate tcp->tcp_snxt = prev_snxt;
48827c478bd9Sstevel@tonic-gate if (prev_rptr == xmit_tail->b_rptr)
48837c478bd9Sstevel@tonic-gate xmit_tail->b_prev = local_time;
48847c478bd9Sstevel@tonic-gate else
48857c478bd9Sstevel@tonic-gate xmit_tail->b_rptr = prev_rptr;
48867c478bd9Sstevel@tonic-gate
48877c478bd9Sstevel@tonic-gate if (mp == NULL)
48887c478bd9Sstevel@tonic-gate break;
48897c478bd9Sstevel@tonic-gate
48907c478bd9Sstevel@tonic-gate mp1 = mp->b_cont;
48917c478bd9Sstevel@tonic-gate
48927c478bd9Sstevel@tonic-gate snxt += len;
48937c478bd9Sstevel@tonic-gate tcp->tcp_last_sent_len = (ushort_t)len;
48947c478bd9Sstevel@tonic-gate while (mp1->b_cont) {
48957c478bd9Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
48967c478bd9Sstevel@tonic-gate xmit_tail->b_prev = local_time;
48977c478bd9Sstevel@tonic-gate mp1 = mp1->b_cont;
48987c478bd9Sstevel@tonic-gate }
48997c478bd9Sstevel@tonic-gate tail_unsent = xmit_tail->b_wptr - mp1->b_wptr;
49007c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutDataSegs);
49017c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes, len);
49027c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
49037c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_wput_data (valid bits)", mp);
49047c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
49057c478bd9Sstevel@tonic-gate freeb(mp);
49067c478bd9Sstevel@tonic-gate continue;
49077c478bd9Sstevel@tonic-gate }
49087c478bd9Sstevel@tonic-gate
49097c478bd9Sstevel@tonic-gate snxt += len; /* Adjust later if we don't send all of len */
49107c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutDataSegs);
49117c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes, len);
49127c478bd9Sstevel@tonic-gate
49137c478bd9Sstevel@tonic-gate if (tail_unsent) {
49147c478bd9Sstevel@tonic-gate /* Are the bytes above us in flight? */
49157c478bd9Sstevel@tonic-gate rptr = xmit_tail->b_wptr - tail_unsent;
49167c478bd9Sstevel@tonic-gate if (rptr != xmit_tail->b_rptr) {
49177c478bd9Sstevel@tonic-gate tail_unsent -= len;
49187c478bd9Sstevel@tonic-gate len += tcp_hdr_len;
49197c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_len = htons(len);
49207c478bd9Sstevel@tonic-gate mp = dupb(xmit_tail);
49217c478bd9Sstevel@tonic-gate if (!mp)
49227c478bd9Sstevel@tonic-gate break;
49237c478bd9Sstevel@tonic-gate mp->b_rptr = rptr;
49247c478bd9Sstevel@tonic-gate goto must_alloc;
49257c478bd9Sstevel@tonic-gate }
49267c478bd9Sstevel@tonic-gate } else {
49277c478bd9Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
49287c478bd9Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
49297c478bd9Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
49307c478bd9Sstevel@tonic-gate tail_unsent = (int)(xmit_tail->b_wptr -
49317c478bd9Sstevel@tonic-gate xmit_tail->b_rptr);
49327c478bd9Sstevel@tonic-gate }
49337c478bd9Sstevel@tonic-gate
49347c478bd9Sstevel@tonic-gate tail_unsent -= len;
49357c478bd9Sstevel@tonic-gate tcp->tcp_last_sent_len = (ushort_t)len;
49367c478bd9Sstevel@tonic-gate
49377c478bd9Sstevel@tonic-gate len += tcp_hdr_len;
49387c478bd9Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
49397c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_len = htons(len);
49407c478bd9Sstevel@tonic-gate
49417c478bd9Sstevel@tonic-gate xmit_tail->b_prev = local_time;
49427c478bd9Sstevel@tonic-gate
49437c478bd9Sstevel@tonic-gate mp = dupb(xmit_tail);
49447c478bd9Sstevel@tonic-gate if (mp == NULL)
49457c478bd9Sstevel@tonic-gate goto out_of_mem;
49467c478bd9Sstevel@tonic-gate
49477c478bd9Sstevel@tonic-gate len = tcp_hdr_len;
49487c478bd9Sstevel@tonic-gate /*
49497c478bd9Sstevel@tonic-gate * There are four reasons to allocate a new hdr mblk:
49507c478bd9Sstevel@tonic-gate * 1) The bytes above us are in use by another packet
49517c478bd9Sstevel@tonic-gate * 2) We don't have good alignment
49527c478bd9Sstevel@tonic-gate * 3) The mblk is being shared
49537c478bd9Sstevel@tonic-gate * 4) We don't have enough room for a header
49547c478bd9Sstevel@tonic-gate */
49557c478bd9Sstevel@tonic-gate rptr = mp->b_rptr - len;
49567c478bd9Sstevel@tonic-gate if (!OK_32PTR(rptr) ||
49577c478bd9Sstevel@tonic-gate rptr < mp->b_datap) {
49587c478bd9Sstevel@tonic-gate /* NOTE: we assume allocb returns an OK_32PTR */
49597c478bd9Sstevel@tonic-gate
49607c478bd9Sstevel@tonic-gate must_alloc:;
49617c478bd9Sstevel@tonic-gate mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH +
49627c478bd9Sstevel@tonic-gate tcp_wroff_xtra, 0);
49637c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
49647c478bd9Sstevel@tonic-gate freemsg(mp);
49657c478bd9Sstevel@tonic-gate goto out_of_mem;
49667c478bd9Sstevel@tonic-gate }
49677c478bd9Sstevel@tonic-gate mp1->b_cont = mp;
49687c478bd9Sstevel@tonic-gate mp = mp1;
49697c478bd9Sstevel@tonic-gate /* Leave room for Link Level header */
49707c478bd9Sstevel@tonic-gate len = tcp_hdr_len;
49717c478bd9Sstevel@tonic-gate rptr = &mp->b_rptr[tcp_wroff_xtra];
49727c478bd9Sstevel@tonic-gate mp->b_wptr = &rptr[len];
49737c478bd9Sstevel@tonic-gate }
49747c478bd9Sstevel@tonic-gate
49757c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
497653391bafSeota /* use uintptr_t to suppress the gcc warning */
497753391bafSeota U32_TO_BE32((uint32_t)(uintptr_t)local_time,
49787c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
49797c478bd9Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
49807c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
49817c478bd9Sstevel@tonic-gate } else {
49827c478bd9Sstevel@tonic-gate assert(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH);
49837c478bd9Sstevel@tonic-gate }
49847c478bd9Sstevel@tonic-gate
49857c478bd9Sstevel@tonic-gate mp->b_rptr = rptr;
49867c478bd9Sstevel@tonic-gate
49877c478bd9Sstevel@tonic-gate /* Copy the template header. */
49887c478bd9Sstevel@tonic-gate dst = (ipaddr_t *)rptr;
49897c478bd9Sstevel@tonic-gate src = (ipaddr_t *)tcp->tcp_iphc;
49907c478bd9Sstevel@tonic-gate dst[0] = src[0];
49917c478bd9Sstevel@tonic-gate dst[1] = src[1];
49927c478bd9Sstevel@tonic-gate dst[2] = src[2];
49937c478bd9Sstevel@tonic-gate dst[3] = src[3];
49947c478bd9Sstevel@tonic-gate dst[4] = src[4];
49957c478bd9Sstevel@tonic-gate dst[5] = src[5];
49967c478bd9Sstevel@tonic-gate dst[6] = src[6];
49977c478bd9Sstevel@tonic-gate dst[7] = src[7];
49987c478bd9Sstevel@tonic-gate dst[8] = src[8];
49997c478bd9Sstevel@tonic-gate dst[9] = src[9];
50007c478bd9Sstevel@tonic-gate len = tcp->tcp_hdr_len;
50017c478bd9Sstevel@tonic-gate if (len -= 40) {
50027c478bd9Sstevel@tonic-gate len >>= 2;
50037c478bd9Sstevel@tonic-gate dst += 10;
50047c478bd9Sstevel@tonic-gate src += 10;
50057c478bd9Sstevel@tonic-gate do {
50067c478bd9Sstevel@tonic-gate *dst++ = *src++;
50077c478bd9Sstevel@tonic-gate } while (--len);
50087c478bd9Sstevel@tonic-gate }
50097c478bd9Sstevel@tonic-gate
50107c478bd9Sstevel@tonic-gate /*
50117c478bd9Sstevel@tonic-gate * Set tcph to point to the header of the outgoing packet,
50127c478bd9Sstevel@tonic-gate * not to the template header.
50137c478bd9Sstevel@tonic-gate */
50147c478bd9Sstevel@tonic-gate tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len);
50157c478bd9Sstevel@tonic-gate
50167c478bd9Sstevel@tonic-gate /*
50177c478bd9Sstevel@tonic-gate * Set the ECN info in the TCP header if it is not a zero
50187c478bd9Sstevel@tonic-gate * window probe. Zero window probe is only sent in
50197c478bd9Sstevel@tonic-gate * tcp_wput_data() and tcp_timer().
50207c478bd9Sstevel@tonic-gate */
50217c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) {
50227c478bd9Sstevel@tonic-gate SET_ECT(tcp, rptr);
50237c478bd9Sstevel@tonic-gate
50247c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
50257c478bd9Sstevel@tonic-gate tcph->th_flags[0] |= TH_ECE;
50267c478bd9Sstevel@tonic-gate if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
50277c478bd9Sstevel@tonic-gate tcph->th_flags[0] |= TH_CWR;
50287c478bd9Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_TRUE;
50297c478bd9Sstevel@tonic-gate }
50307c478bd9Sstevel@tonic-gate }
50317c478bd9Sstevel@tonic-gate
50327c478bd9Sstevel@tonic-gate /* Fill in SACK options */
50337c478bd9Sstevel@tonic-gate if (num_sack_blk > 0) {
50347c478bd9Sstevel@tonic-gate uchar_t *wptr = rptr + tcp->tcp_hdr_len;
50357c478bd9Sstevel@tonic-gate sack_blk_t *tmp;
50367c478bd9Sstevel@tonic-gate int32_t i;
50377c478bd9Sstevel@tonic-gate
50387c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
50397c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
50407c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
50417c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
50427c478bd9Sstevel@tonic-gate sizeof (sack_blk_t);
50437c478bd9Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
50447c478bd9Sstevel@tonic-gate
50457c478bd9Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
50467c478bd9Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
50477c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
50487c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
50497c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
50507c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
50517c478bd9Sstevel@tonic-gate }
50527c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1)
50537c478bd9Sstevel@tonic-gate << 4);
50547c478bd9Sstevel@tonic-gate }
50557c478bd9Sstevel@tonic-gate
50567c478bd9Sstevel@tonic-gate if (tail_unsent) {
50577c478bd9Sstevel@tonic-gate mp1 = mp->b_cont;
50587c478bd9Sstevel@tonic-gate if (mp1 == NULL)
50597c478bd9Sstevel@tonic-gate mp1 = mp;
50607c478bd9Sstevel@tonic-gate /*
50617c478bd9Sstevel@tonic-gate * If we're a little short, tack on more mblks
50627c478bd9Sstevel@tonic-gate * as long as we don't need to split an mblk.
50637c478bd9Sstevel@tonic-gate */
50647c478bd9Sstevel@tonic-gate while (tail_unsent < 0 &&
50657c478bd9Sstevel@tonic-gate tail_unsent + (int)(xmit_tail->b_cont->b_wptr -
50667c478bd9Sstevel@tonic-gate xmit_tail->b_cont->b_rptr) <= 0) {
50677c478bd9Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
50687c478bd9Sstevel@tonic-gate /* Stash for rtt use later */
50697c478bd9Sstevel@tonic-gate xmit_tail->b_prev = local_time;
50707c478bd9Sstevel@tonic-gate mp1->b_cont = dupb(xmit_tail);
50717c478bd9Sstevel@tonic-gate mp1 = mp1->b_cont;
50727c478bd9Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
50737c478bd9Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
50747c478bd9Sstevel@tonic-gate tail_unsent += (int)(xmit_tail->b_wptr -
50757c478bd9Sstevel@tonic-gate xmit_tail->b_rptr);
50767c478bd9Sstevel@tonic-gate if (mp1 == NULL) {
50777c478bd9Sstevel@tonic-gate freemsg(mp);
50787c478bd9Sstevel@tonic-gate goto out_of_mem;
50797c478bd9Sstevel@tonic-gate }
50807c478bd9Sstevel@tonic-gate }
50817c478bd9Sstevel@tonic-gate /* Trim back any surplus on the last mblk */
50827c478bd9Sstevel@tonic-gate if (tail_unsent > 0)
50837c478bd9Sstevel@tonic-gate mp1->b_wptr -= tail_unsent;
50847c478bd9Sstevel@tonic-gate if (tail_unsent < 0) {
50857c478bd9Sstevel@tonic-gate uint32_t ip_len;
50867c478bd9Sstevel@tonic-gate
50877c478bd9Sstevel@tonic-gate /*
50887c478bd9Sstevel@tonic-gate * We did not send everything we could in
50897c478bd9Sstevel@tonic-gate * order to preserve mblk boundaries.
50907c478bd9Sstevel@tonic-gate */
50917c478bd9Sstevel@tonic-gate usable -= tail_unsent;
50927c478bd9Sstevel@tonic-gate snxt += tail_unsent;
50937c478bd9Sstevel@tonic-gate tcp->tcp_last_sent_len += tail_unsent;
50947c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes,
50957c478bd9Sstevel@tonic-gate tail_unsent);
50967c478bd9Sstevel@tonic-gate /* Adjust the IP length field. */
50977c478bd9Sstevel@tonic-gate ip_len = ntohs(((struct ip *)rptr)->ip_len) +
50987c478bd9Sstevel@tonic-gate tail_unsent;
50997c478bd9Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(ip_len);
51007c478bd9Sstevel@tonic-gate tail_unsent = 0;
51017c478bd9Sstevel@tonic-gate }
51027c478bd9Sstevel@tonic-gate }
51037c478bd9Sstevel@tonic-gate
51047c478bd9Sstevel@tonic-gate if (mp == NULL)
51057c478bd9Sstevel@tonic-gate goto out_of_mem;
51067c478bd9Sstevel@tonic-gate
51077c478bd9Sstevel@tonic-gate /*
51087c478bd9Sstevel@tonic-gate * Performance hit! We need to pullup the whole message
51097c478bd9Sstevel@tonic-gate * in order to do checksum and for the MAC output routine.
51107c478bd9Sstevel@tonic-gate */
51117c478bd9Sstevel@tonic-gate if (mp->b_cont != NULL) {
51127c478bd9Sstevel@tonic-gate int mp_size;
51137c478bd9Sstevel@tonic-gate #ifdef DEBUG
51147c478bd9Sstevel@tonic-gate printf("Multiple mblk %d\n", msgdsize(mp));
51157c478bd9Sstevel@tonic-gate #endif
51167c478bd9Sstevel@tonic-gate new_mp = allocb(msgdsize(mp) + tcp_wroff_xtra, 0);
51177c478bd9Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
51187c478bd9Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr;
51197c478bd9Sstevel@tonic-gate while (mp != NULL) {
51207c478bd9Sstevel@tonic-gate mp_size = mp->b_wptr - mp->b_rptr;
51217c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr, new_mp->b_wptr, mp_size);
51227c478bd9Sstevel@tonic-gate new_mp->b_wptr += mp_size;
51237c478bd9Sstevel@tonic-gate mp = mp->b_cont;
51247c478bd9Sstevel@tonic-gate }
51257c478bd9Sstevel@tonic-gate freemsg(mp);
51267c478bd9Sstevel@tonic-gate mp = new_mp;
51277c478bd9Sstevel@tonic-gate }
51287c478bd9Sstevel@tonic-gate tcp_set_cksum(mp);
51297c478bd9Sstevel@tonic-gate ((struct ip *)mp->b_rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
51307c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_wput_data", mp);
51317c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
51327c478bd9Sstevel@tonic-gate freemsg(mp);
51337c478bd9Sstevel@tonic-gate }
51347c478bd9Sstevel@tonic-gate out_of_mem:;
51357c478bd9Sstevel@tonic-gate /* Pretend that all we were trying to send really got sent */
51367c478bd9Sstevel@tonic-gate if (tail_unsent < 0) {
51377c478bd9Sstevel@tonic-gate do {
51387c478bd9Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
51397c478bd9Sstevel@tonic-gate xmit_tail->b_prev = local_time;
51407c478bd9Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
51417c478bd9Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
51427c478bd9Sstevel@tonic-gate tail_unsent += (int)(xmit_tail->b_wptr -
51437c478bd9Sstevel@tonic-gate xmit_tail->b_rptr);
51447c478bd9Sstevel@tonic-gate } while (tail_unsent < 0);
51457c478bd9Sstevel@tonic-gate }
51467c478bd9Sstevel@tonic-gate done:;
51477c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail = xmit_tail;
51487c478bd9Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = tail_unsent;
51497c478bd9Sstevel@tonic-gate len = tcp->tcp_snxt - snxt;
51507c478bd9Sstevel@tonic-gate if (len) {
51517c478bd9Sstevel@tonic-gate /*
51527c478bd9Sstevel@tonic-gate * If new data was sent, need to update the notsack
51537c478bd9Sstevel@tonic-gate * list, which is, afterall, data blocks that have
51547c478bd9Sstevel@tonic-gate * not been sack'ed by the receiver. New data is
51557c478bd9Sstevel@tonic-gate * not sack'ed.
51567c478bd9Sstevel@tonic-gate */
51577c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
51587c478bd9Sstevel@tonic-gate /* len is a negative value. */
51597c478bd9Sstevel@tonic-gate tcp->tcp_pipe -= len;
51607c478bd9Sstevel@tonic-gate tcp_notsack_update(&(tcp->tcp_notsack_list),
51617c478bd9Sstevel@tonic-gate tcp->tcp_snxt, snxt,
51627c478bd9Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
51637c478bd9Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
51647c478bd9Sstevel@tonic-gate }
51657c478bd9Sstevel@tonic-gate tcp->tcp_snxt = snxt + tcp->tcp_fin_sent;
51667c478bd9Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
51677c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
51687c478bd9Sstevel@tonic-gate if ((snxt + len) == tcp->tcp_suna) {
51697c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
51707c478bd9Sstevel@tonic-gate }
51717c478bd9Sstevel@tonic-gate /*
51727c478bd9Sstevel@tonic-gate * Note that len is the amount we just sent but with a negative
51737c478bd9Sstevel@tonic-gate * sign. We update tcp_unsent here since we may come back to
51747c478bd9Sstevel@tonic-gate * tcp_wput_data from tcp_state_wait.
51757c478bd9Sstevel@tonic-gate */
51767c478bd9Sstevel@tonic-gate len += tcp->tcp_unsent;
51777c478bd9Sstevel@tonic-gate tcp->tcp_unsent = len;
51787c478bd9Sstevel@tonic-gate
51797c478bd9Sstevel@tonic-gate /*
51807c478bd9Sstevel@tonic-gate * Let's wait till all the segments have been acked, since we
51817c478bd9Sstevel@tonic-gate * don't have a timer.
51827c478bd9Sstevel@tonic-gate */
51837c478bd9Sstevel@tonic-gate (void) tcp_state_wait(sock_id, tcp, TCPS_ALL_ACKED);
51847c478bd9Sstevel@tonic-gate return;
51857c478bd9Sstevel@tonic-gate } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) {
51867c478bd9Sstevel@tonic-gate /*
51877c478bd9Sstevel@tonic-gate * Didn't send anything. Make sure the timer is running
51887c478bd9Sstevel@tonic-gate * so that we will probe a zero window.
51897c478bd9Sstevel@tonic-gate */
51907c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
51917c478bd9Sstevel@tonic-gate }
51927c478bd9Sstevel@tonic-gate
51937c478bd9Sstevel@tonic-gate /* Note that len is the amount we just sent but with a negative sign */
51947c478bd9Sstevel@tonic-gate len += tcp->tcp_unsent;
51957c478bd9Sstevel@tonic-gate tcp->tcp_unsent = len;
51967c478bd9Sstevel@tonic-gate
51977c478bd9Sstevel@tonic-gate }
51987c478bd9Sstevel@tonic-gate
51997c478bd9Sstevel@tonic-gate static void
tcp_time_wait_processing(tcp_t * tcp,mblk_t * mp,uint32_t seg_seq,uint32_t seg_ack,int seg_len,tcph_t * tcph,int sock_id)52007c478bd9Sstevel@tonic-gate tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp,
52017c478bd9Sstevel@tonic-gate uint32_t seg_seq, uint32_t seg_ack, int seg_len, tcph_t *tcph,
52027c478bd9Sstevel@tonic-gate int sock_id)
52037c478bd9Sstevel@tonic-gate {
52047c478bd9Sstevel@tonic-gate int32_t bytes_acked;
52057c478bd9Sstevel@tonic-gate int32_t gap;
52067c478bd9Sstevel@tonic-gate int32_t rgap;
52077c478bd9Sstevel@tonic-gate tcp_opt_t tcpopt;
52087c478bd9Sstevel@tonic-gate uint_t flags;
52097c478bd9Sstevel@tonic-gate uint32_t new_swnd = 0;
52107c478bd9Sstevel@tonic-gate
52117c478bd9Sstevel@tonic-gate #ifdef DEBUG
52127c478bd9Sstevel@tonic-gate printf("Time wait processing called ###############3\n");
52137c478bd9Sstevel@tonic-gate #endif
52147c478bd9Sstevel@tonic-gate
52157c478bd9Sstevel@tonic-gate /* Just make sure we send the right sock_id to tcp_clean_death */
52167c478bd9Sstevel@tonic-gate if ((sockets[sock_id].pcb == NULL) || (sockets[sock_id].pcb != tcp))
52177c478bd9Sstevel@tonic-gate sock_id = -1;
52187c478bd9Sstevel@tonic-gate
52197c478bd9Sstevel@tonic-gate flags = (unsigned int)tcph->th_flags[0] & 0xFF;
52207c478bd9Sstevel@tonic-gate new_swnd = BE16_TO_U16(tcph->th_win) <<
52217c478bd9Sstevel@tonic-gate ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws);
52227c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
52237c478bd9Sstevel@tonic-gate if (!tcp_paws_check(tcp, tcph, &tcpopt)) {
52247c478bd9Sstevel@tonic-gate freemsg(mp);
52257c478bd9Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
52267c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
52277c478bd9Sstevel@tonic-gate return;
52287c478bd9Sstevel@tonic-gate }
52297c478bd9Sstevel@tonic-gate }
52307c478bd9Sstevel@tonic-gate gap = seg_seq - tcp->tcp_rnxt;
52317c478bd9Sstevel@tonic-gate rgap = tcp->tcp_rwnd - (gap + seg_len);
52327c478bd9Sstevel@tonic-gate if (gap < 0) {
52337c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
52347c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes,
52357c478bd9Sstevel@tonic-gate (seg_len > -gap ? -gap : seg_len));
52367c478bd9Sstevel@tonic-gate seg_len += gap;
52377c478bd9Sstevel@tonic-gate if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
52387c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
52397c478bd9Sstevel@tonic-gate freemsg(mp);
52407c478bd9Sstevel@tonic-gate return;
52417c478bd9Sstevel@tonic-gate }
52427c478bd9Sstevel@tonic-gate if ((flags & TH_FIN) && seg_len == -1) {
52437c478bd9Sstevel@tonic-gate /*
52447c478bd9Sstevel@tonic-gate * When TCP receives a duplicate FIN in
52457c478bd9Sstevel@tonic-gate * TIME_WAIT state, restart the 2 MSL timer.
52467c478bd9Sstevel@tonic-gate * See page 73 in RFC 793. Make sure this TCP
52477c478bd9Sstevel@tonic-gate * is already on the TIME_WAIT list. If not,
52487c478bd9Sstevel@tonic-gate * just restart the timer.
52497c478bd9Sstevel@tonic-gate */
52507c478bd9Sstevel@tonic-gate tcp_time_wait_remove(tcp);
52517c478bd9Sstevel@tonic-gate tcp_time_wait_append(tcp);
52527c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
52537c478bd9Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
52547c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
52557c478bd9Sstevel@tonic-gate freemsg(mp);
52567c478bd9Sstevel@tonic-gate return;
52577c478bd9Sstevel@tonic-gate }
52587c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
52597c478bd9Sstevel@tonic-gate seg_len = 0;
52607c478bd9Sstevel@tonic-gate goto process_ack;
52617c478bd9Sstevel@tonic-gate }
52627c478bd9Sstevel@tonic-gate
52637c478bd9Sstevel@tonic-gate /* Fix seg_seq, and chew the gap off the front. */
52647c478bd9Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
52657c478bd9Sstevel@tonic-gate }
52667c478bd9Sstevel@tonic-gate
52677c478bd9Sstevel@tonic-gate if ((flags & TH_SYN) && gap > 0 && rgap < 0) {
52687c478bd9Sstevel@tonic-gate /*
52697c478bd9Sstevel@tonic-gate * Make sure that when we accept the connection, pick
52707c478bd9Sstevel@tonic-gate * an ISS greater than (tcp_snxt + ISS_INCR/2) for the
52717c478bd9Sstevel@tonic-gate * old connection.
52727c478bd9Sstevel@tonic-gate *
52737c478bd9Sstevel@tonic-gate * The next ISS generated is equal to tcp_iss_incr_extra
52747c478bd9Sstevel@tonic-gate * + ISS_INCR/2 + other components depending on the
52757c478bd9Sstevel@tonic-gate * value of tcp_strong_iss. We pre-calculate the new
52767c478bd9Sstevel@tonic-gate * ISS here and compare with tcp_snxt to determine if
52777c478bd9Sstevel@tonic-gate * we need to make adjustment to tcp_iss_incr_extra.
52787c478bd9Sstevel@tonic-gate *
52797c478bd9Sstevel@tonic-gate * Note that since we are now in the global queue
52807c478bd9Sstevel@tonic-gate * perimeter and need to do a lateral_put() to the
52817c478bd9Sstevel@tonic-gate * listener queue, there can be other connection requests/
52827c478bd9Sstevel@tonic-gate * attempts while the lateral_put() is going on. That
52837c478bd9Sstevel@tonic-gate * means what we calculate here may not be correct. This
52847c478bd9Sstevel@tonic-gate * is extremely difficult to solve unless TCP and IP
52857c478bd9Sstevel@tonic-gate * modules are merged and there is no perimeter, but just
52867c478bd9Sstevel@tonic-gate * locks. The above calculation is ugly and is a
52877c478bd9Sstevel@tonic-gate * waste of CPU cycles...
52887c478bd9Sstevel@tonic-gate */
52897c478bd9Sstevel@tonic-gate uint32_t new_iss = tcp_iss_incr_extra;
52907c478bd9Sstevel@tonic-gate int32_t adj;
52917c478bd9Sstevel@tonic-gate
52927c478bd9Sstevel@tonic-gate /* Add time component and min random (i.e. 1). */
52937c478bd9Sstevel@tonic-gate new_iss += (prom_gettime() >> ISS_NSEC_SHT) + 1;
52947c478bd9Sstevel@tonic-gate if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) {
52957c478bd9Sstevel@tonic-gate /*
52967c478bd9Sstevel@tonic-gate * New ISS not guaranteed to be ISS_INCR/2
52977c478bd9Sstevel@tonic-gate * ahead of the current tcp_snxt, so add the
52987c478bd9Sstevel@tonic-gate * difference to tcp_iss_incr_extra.
52997c478bd9Sstevel@tonic-gate */
53007c478bd9Sstevel@tonic-gate tcp_iss_incr_extra += adj;
53017c478bd9Sstevel@tonic-gate }
53027c478bd9Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
53037c478bd9Sstevel@tonic-gate
53047c478bd9Sstevel@tonic-gate /*
53057c478bd9Sstevel@tonic-gate * This is a passive open. Right now we do not
53067c478bd9Sstevel@tonic-gate * do anything...
53077c478bd9Sstevel@tonic-gate */
53087c478bd9Sstevel@tonic-gate freemsg(mp);
53097c478bd9Sstevel@tonic-gate return;
53107c478bd9Sstevel@tonic-gate }
53117c478bd9Sstevel@tonic-gate
53127c478bd9Sstevel@tonic-gate /*
53137c478bd9Sstevel@tonic-gate * rgap is the amount of stuff received out of window. A negative
53147c478bd9Sstevel@tonic-gate * value is the amount out of window.
53157c478bd9Sstevel@tonic-gate */
53167c478bd9Sstevel@tonic-gate if (rgap < 0) {
53177c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPastWinSegs);
53187c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPastWinBytes, -rgap);
53197c478bd9Sstevel@tonic-gate /* Fix seg_len and make sure there is something left. */
53207c478bd9Sstevel@tonic-gate seg_len += rgap;
53217c478bd9Sstevel@tonic-gate if (seg_len <= 0) {
53227c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
53237c478bd9Sstevel@tonic-gate freemsg(mp);
53247c478bd9Sstevel@tonic-gate return;
53257c478bd9Sstevel@tonic-gate }
53267c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53277c478bd9Sstevel@tonic-gate seg_len = 0;
53287c478bd9Sstevel@tonic-gate goto process_ack;
53297c478bd9Sstevel@tonic-gate }
53307c478bd9Sstevel@tonic-gate }
53317c478bd9Sstevel@tonic-gate /*
53327c478bd9Sstevel@tonic-gate * Check whether we can update tcp_ts_recent. This test is
53337c478bd9Sstevel@tonic-gate * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
53347c478bd9Sstevel@tonic-gate * Extensions for High Performance: An Update", Internet Draft.
53357c478bd9Sstevel@tonic-gate */
53367c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok &&
53377c478bd9Sstevel@tonic-gate TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
53387c478bd9Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
53397c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
53407c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
53417c478bd9Sstevel@tonic-gate }
53427c478bd9Sstevel@tonic-gate
53437c478bd9Sstevel@tonic-gate if (seg_seq != tcp->tcp_rnxt && seg_len > 0) {
53447c478bd9Sstevel@tonic-gate /* Always ack out of order packets */
53457c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53467c478bd9Sstevel@tonic-gate seg_len = 0;
53477c478bd9Sstevel@tonic-gate } else if (seg_len > 0) {
53487c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataInorderSegs);
53497c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataInorderBytes, seg_len);
53507c478bd9Sstevel@tonic-gate }
53517c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
53527c478bd9Sstevel@tonic-gate freemsg(mp);
53537c478bd9Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
53547c478bd9Sstevel@tonic-gate return;
53557c478bd9Sstevel@tonic-gate }
53567c478bd9Sstevel@tonic-gate if (flags & TH_SYN) {
53577c478bd9Sstevel@tonic-gate freemsg(mp);
53587c478bd9Sstevel@tonic-gate tcp_xmit_ctl("TH_SYN", tcp, NULL, seg_ack, seg_seq + 1,
53597c478bd9Sstevel@tonic-gate TH_RST|TH_ACK, 0, -1);
53607c478bd9Sstevel@tonic-gate /*
53617c478bd9Sstevel@tonic-gate * Do not delete the TCP structure if it is in
53627c478bd9Sstevel@tonic-gate * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13.
53637c478bd9Sstevel@tonic-gate */
53647c478bd9Sstevel@tonic-gate return;
53657c478bd9Sstevel@tonic-gate }
53667c478bd9Sstevel@tonic-gate process_ack:
53677c478bd9Sstevel@tonic-gate if (flags & TH_ACK) {
53687c478bd9Sstevel@tonic-gate bytes_acked = (int)(seg_ack - tcp->tcp_suna);
53697c478bd9Sstevel@tonic-gate if (bytes_acked <= 0) {
53707c478bd9Sstevel@tonic-gate if (bytes_acked == 0 && seg_len == 0 &&
53717c478bd9Sstevel@tonic-gate new_swnd == tcp->tcp_swnd)
53727c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDupAck);
53737c478bd9Sstevel@tonic-gate } else {
53747c478bd9Sstevel@tonic-gate /* Acks something not sent */
53757c478bd9Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53767c478bd9Sstevel@tonic-gate }
53777c478bd9Sstevel@tonic-gate }
53787c478bd9Sstevel@tonic-gate freemsg(mp);
53797c478bd9Sstevel@tonic-gate if (flags & TH_ACK_NEEDED) {
53807c478bd9Sstevel@tonic-gate /*
53817c478bd9Sstevel@tonic-gate * Time to send an ack for some reason.
53827c478bd9Sstevel@tonic-gate */
53837c478bd9Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
53847c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
53857c478bd9Sstevel@tonic-gate }
53867c478bd9Sstevel@tonic-gate }
53877c478bd9Sstevel@tonic-gate
53887c478bd9Sstevel@tonic-gate static int
tcp_init_values(tcp_t * tcp,struct inetboot_socket * isp)53897c478bd9Sstevel@tonic-gate tcp_init_values(tcp_t *tcp, struct inetboot_socket *isp)
53907c478bd9Sstevel@tonic-gate {
53917c478bd9Sstevel@tonic-gate int err;
53927c478bd9Sstevel@tonic-gate
53937c478bd9Sstevel@tonic-gate tcp->tcp_family = AF_INET;
53947c478bd9Sstevel@tonic-gate tcp->tcp_ipversion = IPV4_VERSION;
53957c478bd9Sstevel@tonic-gate
53967c478bd9Sstevel@tonic-gate /*
53977c478bd9Sstevel@tonic-gate * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
53987c478bd9Sstevel@tonic-gate * will be close to tcp_rexmit_interval_initial. By doing this, we
53997c478bd9Sstevel@tonic-gate * allow the algorithm to adjust slowly to large fluctuations of RTT
54007c478bd9Sstevel@tonic-gate * during first few transmissions of a connection as seen in slow
54017c478bd9Sstevel@tonic-gate * links.
54027c478bd9Sstevel@tonic-gate */
54037c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2;
54047c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1;
54057c478bd9Sstevel@tonic-gate tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
54067c478bd9Sstevel@tonic-gate tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) +
54077c478bd9Sstevel@tonic-gate tcp_conn_grace_period;
54087c478bd9Sstevel@tonic-gate if (tcp->tcp_rto < tcp_rexmit_interval_min)
54097c478bd9Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_min;
54107c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
54117c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
54127c478bd9Sstevel@tonic-gate tcp->tcp_last_recv_time = prom_gettime();
54137c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_max = tcp_cwnd_max_;
54147c478bd9Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_INFINITE;
54157c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
54167c478bd9Sstevel@tonic-gate /* For Ethernet, the mtu returned is actually 1550... */
54177c478bd9Sstevel@tonic-gate if (mac_get_type() == IFT_ETHER) {
54187c478bd9Sstevel@tonic-gate tcp->tcp_if_mtu = mac_get_mtu() - 50;
54197c478bd9Sstevel@tonic-gate } else {
54207c478bd9Sstevel@tonic-gate tcp->tcp_if_mtu = mac_get_mtu();
54217c478bd9Sstevel@tonic-gate }
54227c478bd9Sstevel@tonic-gate tcp->tcp_mss = tcp->tcp_if_mtu;
54237c478bd9Sstevel@tonic-gate
54247c478bd9Sstevel@tonic-gate tcp->tcp_first_timer_threshold = tcp_ip_notify_interval;
54257c478bd9Sstevel@tonic-gate tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval;
54267c478bd9Sstevel@tonic-gate tcp->tcp_second_timer_threshold = tcp_ip_abort_interval;
54277c478bd9Sstevel@tonic-gate /*
54287c478bd9Sstevel@tonic-gate * Fix it to tcp_ip_abort_linterval later if it turns out to be a
54297c478bd9Sstevel@tonic-gate * passive open.
54307c478bd9Sstevel@tonic-gate */
54317c478bd9Sstevel@tonic-gate tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval;
54327c478bd9Sstevel@tonic-gate
54337c478bd9Sstevel@tonic-gate tcp->tcp_naglim = tcp_naglim_def;
54347c478bd9Sstevel@tonic-gate
54357c478bd9Sstevel@tonic-gate /* NOTE: ISS is now set in tcp_adapt_ire(). */
54367c478bd9Sstevel@tonic-gate
54377c478bd9Sstevel@tonic-gate /* Initialize the header template */
54387c478bd9Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION) {
54397c478bd9Sstevel@tonic-gate err = tcp_header_init_ipv4(tcp);
54407c478bd9Sstevel@tonic-gate }
54417c478bd9Sstevel@tonic-gate if (err)
54427c478bd9Sstevel@tonic-gate return (err);
54437c478bd9Sstevel@tonic-gate
54447c478bd9Sstevel@tonic-gate /*
54457c478bd9Sstevel@tonic-gate * Init the window scale to the max so tcp_rwnd_set() won't pare
54467c478bd9Sstevel@tonic-gate * down tcp_rwnd. tcp_adapt_ire() will set the right value later.
54477c478bd9Sstevel@tonic-gate */
54487c478bd9Sstevel@tonic-gate tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
54497c478bd9Sstevel@tonic-gate tcp->tcp_xmit_lowater = tcp_xmit_lowat;
54507c478bd9Sstevel@tonic-gate if (isp != NULL) {
54517c478bd9Sstevel@tonic-gate tcp->tcp_xmit_hiwater = isp->so_sndbuf;
54527c478bd9Sstevel@tonic-gate tcp->tcp_rwnd = isp->so_rcvbuf;
54537c478bd9Sstevel@tonic-gate tcp->tcp_rwnd_max = isp->so_rcvbuf;
54547c478bd9Sstevel@tonic-gate }
54557c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_IDLE;
54567c478bd9Sstevel@tonic-gate return (0);
54577c478bd9Sstevel@tonic-gate }
54587c478bd9Sstevel@tonic-gate
54597c478bd9Sstevel@tonic-gate /*
54607c478bd9Sstevel@tonic-gate * Initialize the IPv4 header. Loses any record of any IP options.
54617c478bd9Sstevel@tonic-gate */
54627c478bd9Sstevel@tonic-gate static int
tcp_header_init_ipv4(tcp_t * tcp)54637c478bd9Sstevel@tonic-gate tcp_header_init_ipv4(tcp_t *tcp)
54647c478bd9Sstevel@tonic-gate {
54657c478bd9Sstevel@tonic-gate tcph_t *tcph;
54667c478bd9Sstevel@tonic-gate
54677c478bd9Sstevel@tonic-gate /*
54687c478bd9Sstevel@tonic-gate * This is a simple initialization. If there's
54697c478bd9Sstevel@tonic-gate * already a template, it should never be too small,
54707c478bd9Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one.
54717c478bd9Sstevel@tonic-gate */
54727c478bd9Sstevel@tonic-gate if (tcp->tcp_iphc != NULL) {
54737c478bd9Sstevel@tonic-gate assert(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH);
54747c478bd9Sstevel@tonic-gate bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
54757c478bd9Sstevel@tonic-gate } else {
54767c478bd9Sstevel@tonic-gate tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH;
54777c478bd9Sstevel@tonic-gate tcp->tcp_iphc = bkmem_zalloc(tcp->tcp_iphc_len);
54787c478bd9Sstevel@tonic-gate if (tcp->tcp_iphc == NULL) {
54797c478bd9Sstevel@tonic-gate tcp->tcp_iphc_len = 0;
54807c478bd9Sstevel@tonic-gate return (ENOMEM);
54817c478bd9Sstevel@tonic-gate }
54827c478bd9Sstevel@tonic-gate }
54837c478bd9Sstevel@tonic-gate tcp->tcp_ipha = (struct ip *)tcp->tcp_iphc;
54847c478bd9Sstevel@tonic-gate tcp->tcp_ipversion = IPV4_VERSION;
54857c478bd9Sstevel@tonic-gate
54867c478bd9Sstevel@tonic-gate /*
54877c478bd9Sstevel@tonic-gate * Note that it does not include TCP options yet. It will
54887c478bd9Sstevel@tonic-gate * after the connection is established.
54897c478bd9Sstevel@tonic-gate */
54907c478bd9Sstevel@tonic-gate tcp->tcp_hdr_len = sizeof (struct ip) + sizeof (tcph_t);
54917c478bd9Sstevel@tonic-gate tcp->tcp_tcp_hdr_len = sizeof (tcph_t);
54927c478bd9Sstevel@tonic-gate tcp->tcp_ip_hdr_len = sizeof (struct ip);
54937c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_v = IP_VERSION;
54947c478bd9Sstevel@tonic-gate /* We don't support IP options... */
54957c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_hl = IP_SIMPLE_HDR_LENGTH_IN_WORDS;
54967c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_p = IPPROTO_TCP;
54977c478bd9Sstevel@tonic-gate /* We are not supposed to do PMTU discovery... */
54987c478bd9Sstevel@tonic-gate tcp->tcp_ipha->ip_sum = 0;
54997c478bd9Sstevel@tonic-gate
55007c478bd9Sstevel@tonic-gate tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (struct ip));
55017c478bd9Sstevel@tonic-gate tcp->tcp_tcph = tcph;
55027c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] = (5 << 4);
55037c478bd9Sstevel@tonic-gate return (0);
55047c478bd9Sstevel@tonic-gate }
55057c478bd9Sstevel@tonic-gate
55067c478bd9Sstevel@tonic-gate /*
55077c478bd9Sstevel@tonic-gate * Send out a control packet on the tcp connection specified. This routine
55087c478bd9Sstevel@tonic-gate * is typically called where we need a simple ACK or RST generated.
55097c478bd9Sstevel@tonic-gate *
55107c478bd9Sstevel@tonic-gate * This function is called with or without a mp.
55117c478bd9Sstevel@tonic-gate */
55127c478bd9Sstevel@tonic-gate static void
tcp_xmit_ctl(char * str,tcp_t * tcp,mblk_t * mp,uint32_t seq,uint32_t ack,int ctl,uint_t ip_hdr_len,int sock_id)55137c478bd9Sstevel@tonic-gate tcp_xmit_ctl(char *str, tcp_t *tcp, mblk_t *mp, uint32_t seq,
55147c478bd9Sstevel@tonic-gate uint32_t ack, int ctl, uint_t ip_hdr_len, int sock_id)
55157c478bd9Sstevel@tonic-gate {
55167c478bd9Sstevel@tonic-gate uchar_t *rptr;
55177c478bd9Sstevel@tonic-gate tcph_t *tcph;
55187c478bd9Sstevel@tonic-gate struct ip *iph = NULL;
55197c478bd9Sstevel@tonic-gate int tcp_hdr_len;
55207c478bd9Sstevel@tonic-gate int tcp_ip_hdr_len;
55217c478bd9Sstevel@tonic-gate
55227c478bd9Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
55237c478bd9Sstevel@tonic-gate tcp_ip_hdr_len = tcp->tcp_ip_hdr_len;
55247c478bd9Sstevel@tonic-gate
55257c478bd9Sstevel@tonic-gate if (mp) {
55267c478bd9Sstevel@tonic-gate assert(ip_hdr_len != 0);
55277c478bd9Sstevel@tonic-gate rptr = mp->b_rptr;
55287c478bd9Sstevel@tonic-gate tcph = (tcph_t *)(rptr + ip_hdr_len);
55297c478bd9Sstevel@tonic-gate /* Don't reply to a RST segment. */
55307c478bd9Sstevel@tonic-gate if (tcph->th_flags[0] & TH_RST) {
55317c478bd9Sstevel@tonic-gate freeb(mp);
55327c478bd9Sstevel@tonic-gate return;
55337c478bd9Sstevel@tonic-gate }
55347c478bd9Sstevel@tonic-gate freemsg(mp);
55357c478bd9Sstevel@tonic-gate rptr = NULL;
55367c478bd9Sstevel@tonic-gate } else {
55377c478bd9Sstevel@tonic-gate assert(ip_hdr_len == 0);
55387c478bd9Sstevel@tonic-gate }
55397c478bd9Sstevel@tonic-gate /* If a text string is passed in with the request, print it out. */
55407c478bd9Sstevel@tonic-gate if (str != NULL) {
55417c478bd9Sstevel@tonic-gate dprintf("tcp_xmit_ctl(%d): '%s', seq 0x%x, ack 0x%x, "
55427c478bd9Sstevel@tonic-gate "ctl 0x%x\n", sock_id, str, seq, ack, ctl);
55437c478bd9Sstevel@tonic-gate }
55447c478bd9Sstevel@tonic-gate mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 0);
55457c478bd9Sstevel@tonic-gate if (mp == NULL) {
55467c478bd9Sstevel@tonic-gate dprintf("tcp_xmit_ctl(%d): Cannot allocate memory\n", sock_id);
55477c478bd9Sstevel@tonic-gate return;
55487c478bd9Sstevel@tonic-gate }
55497c478bd9Sstevel@tonic-gate rptr = &mp->b_rptr[tcp_wroff_xtra];
55507c478bd9Sstevel@tonic-gate mp->b_rptr = rptr;
55517c478bd9Sstevel@tonic-gate mp->b_wptr = &rptr[tcp_hdr_len];
55527c478bd9Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len);
55537c478bd9Sstevel@tonic-gate
55547c478bd9Sstevel@tonic-gate iph = (struct ip *)rptr;
55557c478bd9Sstevel@tonic-gate iph->ip_len = htons(tcp_hdr_len);
55567c478bd9Sstevel@tonic-gate
55577c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp_ip_hdr_len];
55587c478bd9Sstevel@tonic-gate tcph->th_flags[0] = (uint8_t)ctl;
55597c478bd9Sstevel@tonic-gate if (ctl & TH_RST) {
55607c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutRsts);
55617c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
55627c478bd9Sstevel@tonic-gate /*
55637c478bd9Sstevel@tonic-gate * Don't send TSopt w/ TH_RST packets per RFC 1323.
55647c478bd9Sstevel@tonic-gate */
55657c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok && tcp->tcp_state > TCPS_SYN_SENT) {
55667c478bd9Sstevel@tonic-gate mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN];
55677c478bd9Sstevel@tonic-gate *(mp->b_wptr) = TCPOPT_EOL;
55687c478bd9Sstevel@tonic-gate iph->ip_len = htons(tcp_hdr_len -
55697c478bd9Sstevel@tonic-gate TCPOPT_REAL_TS_LEN);
55707c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] -= (3 << 4);
55717c478bd9Sstevel@tonic-gate }
55727c478bd9Sstevel@tonic-gate }
55737c478bd9Sstevel@tonic-gate if (ctl & TH_ACK) {
55747c478bd9Sstevel@tonic-gate uint32_t now = prom_gettime();
55757c478bd9Sstevel@tonic-gate
55767c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
55777c478bd9Sstevel@tonic-gate U32_TO_BE32(now,
55787c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
55797c478bd9Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
55807c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
55817c478bd9Sstevel@tonic-gate }
55827c478bd9Sstevel@tonic-gate tcp->tcp_rack = ack;
55837c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
55847c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
55857c478bd9Sstevel@tonic-gate }
55867c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutSegs);
55877c478bd9Sstevel@tonic-gate U32_TO_BE32(seq, tcph->th_seq);
55887c478bd9Sstevel@tonic-gate U32_TO_BE32(ack, tcph->th_ack);
55897c478bd9Sstevel@tonic-gate
55907c478bd9Sstevel@tonic-gate tcp_set_cksum(mp);
55917c478bd9Sstevel@tonic-gate iph->ip_ttl = (uint8_t)tcp_ipv4_ttl;
55927c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_ctl", mp);
55937c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
55947c478bd9Sstevel@tonic-gate freeb(mp);
55957c478bd9Sstevel@tonic-gate }
55967c478bd9Sstevel@tonic-gate
55977c478bd9Sstevel@tonic-gate /* Generate an ACK-only (no data) segment for a TCP endpoint */
55987c478bd9Sstevel@tonic-gate static mblk_t *
tcp_ack_mp(tcp_t * tcp)55997c478bd9Sstevel@tonic-gate tcp_ack_mp(tcp_t *tcp)
56007c478bd9Sstevel@tonic-gate {
56017c478bd9Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
56027c478bd9Sstevel@tonic-gate /*
56037c478bd9Sstevel@tonic-gate * For the complex case where we have to send some
56047c478bd9Sstevel@tonic-gate * controls (FIN or SYN), let tcp_xmit_mp do it.
56057c478bd9Sstevel@tonic-gate * When sending an ACK-only segment (no data)
56067c478bd9Sstevel@tonic-gate * into a zero window, always set the seq number to
56077c478bd9Sstevel@tonic-gate * suna, since snxt will be extended past the window.
56087c478bd9Sstevel@tonic-gate * If we used snxt, the receiver might consider the ACK
56097c478bd9Sstevel@tonic-gate * unacceptable.
56107c478bd9Sstevel@tonic-gate */
56117c478bd9Sstevel@tonic-gate return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
56127c478bd9Sstevel@tonic-gate (tcp->tcp_zero_win_probe) ?
56137c478bd9Sstevel@tonic-gate tcp->tcp_suna :
56147c478bd9Sstevel@tonic-gate tcp->tcp_snxt, B_FALSE, NULL, B_FALSE));
56157c478bd9Sstevel@tonic-gate } else {
56167c478bd9Sstevel@tonic-gate /* Generate a simple ACK */
56177c478bd9Sstevel@tonic-gate uchar_t *rptr;
56187c478bd9Sstevel@tonic-gate tcph_t *tcph;
56197c478bd9Sstevel@tonic-gate mblk_t *mp1;
56207c478bd9Sstevel@tonic-gate int32_t tcp_hdr_len;
56217c478bd9Sstevel@tonic-gate int32_t num_sack_blk = 0;
56227c478bd9Sstevel@tonic-gate int32_t sack_opt_len;
56237c478bd9Sstevel@tonic-gate
56247c478bd9Sstevel@tonic-gate /*
56257c478bd9Sstevel@tonic-gate * Allocate space for TCP + IP headers
56267c478bd9Sstevel@tonic-gate * and link-level header
56277c478bd9Sstevel@tonic-gate */
56287c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
56297c478bd9Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
56307c478bd9Sstevel@tonic-gate tcp->tcp_num_sack_blk);
56317c478bd9Sstevel@tonic-gate sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
56327c478bd9Sstevel@tonic-gate TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
56337c478bd9Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len;
56347c478bd9Sstevel@tonic-gate } else {
56357c478bd9Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
56367c478bd9Sstevel@tonic-gate }
56377c478bd9Sstevel@tonic-gate mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, 0);
56387c478bd9Sstevel@tonic-gate if (mp1 == NULL)
56397c478bd9Sstevel@tonic-gate return (NULL);
56407c478bd9Sstevel@tonic-gate
56417c478bd9Sstevel@tonic-gate /* copy in prototype TCP + IP header */
56427c478bd9Sstevel@tonic-gate rptr = mp1->b_rptr + tcp_wroff_xtra;
56437c478bd9Sstevel@tonic-gate mp1->b_rptr = rptr;
56447c478bd9Sstevel@tonic-gate mp1->b_wptr = rptr + tcp_hdr_len;
56457c478bd9Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len);
56467c478bd9Sstevel@tonic-gate
56477c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len];
56487c478bd9Sstevel@tonic-gate
56497c478bd9Sstevel@tonic-gate /*
56507c478bd9Sstevel@tonic-gate * Set the TCP sequence number.
56517c478bd9Sstevel@tonic-gate * When sending an ACK-only segment (no data)
56527c478bd9Sstevel@tonic-gate * into a zero window, always set the seq number to
56537c478bd9Sstevel@tonic-gate * suna, since snxt will be extended past the window.
56547c478bd9Sstevel@tonic-gate * If we used snxt, the receiver might consider the ACK
56557c478bd9Sstevel@tonic-gate * unacceptable.
56567c478bd9Sstevel@tonic-gate */
56577c478bd9Sstevel@tonic-gate U32_TO_ABE32((tcp->tcp_zero_win_probe) ?
56587c478bd9Sstevel@tonic-gate tcp->tcp_suna : tcp->tcp_snxt, tcph->th_seq);
56597c478bd9Sstevel@tonic-gate
56607c478bd9Sstevel@tonic-gate /* Set up the TCP flag field. */
56617c478bd9Sstevel@tonic-gate tcph->th_flags[0] = (uchar_t)TH_ACK;
56627c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
56637c478bd9Sstevel@tonic-gate tcph->th_flags[0] |= TH_ECE;
56647c478bd9Sstevel@tonic-gate
56657c478bd9Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
56667c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
56677c478bd9Sstevel@tonic-gate
56687c478bd9Sstevel@tonic-gate /* fill in timestamp option if in use */
56697c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
56707c478bd9Sstevel@tonic-gate uint32_t llbolt = (uint32_t)prom_gettime();
56717c478bd9Sstevel@tonic-gate
56727c478bd9Sstevel@tonic-gate U32_TO_BE32(llbolt,
56737c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
56747c478bd9Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
56757c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
56767c478bd9Sstevel@tonic-gate }
56777c478bd9Sstevel@tonic-gate
56787c478bd9Sstevel@tonic-gate /* Fill in SACK options */
56797c478bd9Sstevel@tonic-gate if (num_sack_blk > 0) {
56807c478bd9Sstevel@tonic-gate uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len;
56817c478bd9Sstevel@tonic-gate sack_blk_t *tmp;
56827c478bd9Sstevel@tonic-gate int32_t i;
56837c478bd9Sstevel@tonic-gate
56847c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
56857c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
56867c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
56877c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
56887c478bd9Sstevel@tonic-gate sizeof (sack_blk_t);
56897c478bd9Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
56907c478bd9Sstevel@tonic-gate
56917c478bd9Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
56927c478bd9Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
56937c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
56947c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
56957c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
56967c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
56977c478bd9Sstevel@tonic-gate }
56987c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1)
56997c478bd9Sstevel@tonic-gate << 4);
57007c478bd9Sstevel@tonic-gate }
57017c478bd9Sstevel@tonic-gate
57027c478bd9Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(tcp_hdr_len);
57037c478bd9Sstevel@tonic-gate tcp_set_cksum(mp1);
57047c478bd9Sstevel@tonic-gate ((struct ip *)rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
57057c478bd9Sstevel@tonic-gate return (mp1);
57067c478bd9Sstevel@tonic-gate }
57077c478bd9Sstevel@tonic-gate }
57087c478bd9Sstevel@tonic-gate
57097c478bd9Sstevel@tonic-gate /*
57107c478bd9Sstevel@tonic-gate * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
57117c478bd9Sstevel@tonic-gate * ip and tcp header ready to pass down to IP. If the mp passed in is
57127c478bd9Sstevel@tonic-gate * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
57137c478bd9Sstevel@tonic-gate * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
57147c478bd9Sstevel@tonic-gate * otherwise it will dup partial mblks.)
57157c478bd9Sstevel@tonic-gate * Otherwise, an appropriate ACK packet will be generated. This
57167c478bd9Sstevel@tonic-gate * routine is not usually called to send new data for the first time. It
57177c478bd9Sstevel@tonic-gate * is mostly called out of the timer for retransmits, and to generate ACKs.
57187c478bd9Sstevel@tonic-gate *
57197c478bd9Sstevel@tonic-gate * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
57207c478bd9Sstevel@tonic-gate * be adjusted by *offset. And after dupb(), the offset and the ending mblk
57217c478bd9Sstevel@tonic-gate * of the original mblk chain will be returned in *offset and *end_mp.
57227c478bd9Sstevel@tonic-gate */
57237c478bd9Sstevel@tonic-gate static mblk_t *
tcp_xmit_mp(tcp_t * tcp,mblk_t * mp,int32_t max_to_send,int32_t * offset,mblk_t ** end_mp,uint32_t seq,boolean_t sendall,uint32_t * seg_len,boolean_t rexmit)57247c478bd9Sstevel@tonic-gate tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset,
57257c478bd9Sstevel@tonic-gate mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len,
57267c478bd9Sstevel@tonic-gate boolean_t rexmit)
57277c478bd9Sstevel@tonic-gate {
57287c478bd9Sstevel@tonic-gate int data_length;
57297c478bd9Sstevel@tonic-gate int32_t off = 0;
57307c478bd9Sstevel@tonic-gate uint_t flags;
57317c478bd9Sstevel@tonic-gate mblk_t *mp1;
57327c478bd9Sstevel@tonic-gate mblk_t *mp2;
57337c478bd9Sstevel@tonic-gate mblk_t *new_mp;
57347c478bd9Sstevel@tonic-gate uchar_t *rptr;
57357c478bd9Sstevel@tonic-gate tcph_t *tcph;
57367c478bd9Sstevel@tonic-gate int32_t num_sack_blk = 0;
57377c478bd9Sstevel@tonic-gate int32_t sack_opt_len = 0;
57387c478bd9Sstevel@tonic-gate
57397c478bd9Sstevel@tonic-gate /* Allocate for our maximum TCP header + link-level */
57407c478bd9Sstevel@tonic-gate mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH +
57417c478bd9Sstevel@tonic-gate tcp_wroff_xtra, 0);
57427c478bd9Sstevel@tonic-gate if (mp1 == NULL)
57437c478bd9Sstevel@tonic-gate return (NULL);
57447c478bd9Sstevel@tonic-gate data_length = 0;
57457c478bd9Sstevel@tonic-gate
57467c478bd9Sstevel@tonic-gate /*
57477c478bd9Sstevel@tonic-gate * Note that tcp_mss has been adjusted to take into account the
57487c478bd9Sstevel@tonic-gate * timestamp option if applicable. Because SACK options do not
57497c478bd9Sstevel@tonic-gate * appear in every TCP segments and they are of variable lengths,
57507c478bd9Sstevel@tonic-gate * they cannot be included in tcp_mss. Thus we need to calculate
57517c478bd9Sstevel@tonic-gate * the actual segment length when we need to send a segment which
57527c478bd9Sstevel@tonic-gate * includes SACK options.
57537c478bd9Sstevel@tonic-gate */
57547c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
57557c478bd9Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
57567c478bd9Sstevel@tonic-gate tcp->tcp_num_sack_blk);
57577c478bd9Sstevel@tonic-gate sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
57587c478bd9Sstevel@tonic-gate TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
57597c478bd9Sstevel@tonic-gate if (max_to_send + sack_opt_len > tcp->tcp_mss)
57607c478bd9Sstevel@tonic-gate max_to_send -= sack_opt_len;
57617c478bd9Sstevel@tonic-gate }
57627c478bd9Sstevel@tonic-gate
57637c478bd9Sstevel@tonic-gate if (offset != NULL) {
57647c478bd9Sstevel@tonic-gate off = *offset;
57657c478bd9Sstevel@tonic-gate /* We use offset as an indicator that end_mp is not NULL. */
57667c478bd9Sstevel@tonic-gate *end_mp = NULL;
57677c478bd9Sstevel@tonic-gate }
57687c478bd9Sstevel@tonic-gate for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) {
57697c478bd9Sstevel@tonic-gate /* This could be faster with cooperation from downstream */
57707c478bd9Sstevel@tonic-gate if (mp2 != mp1 && !sendall &&
57717c478bd9Sstevel@tonic-gate data_length + (int)(mp->b_wptr - mp->b_rptr) >
57727c478bd9Sstevel@tonic-gate max_to_send)
57737c478bd9Sstevel@tonic-gate /*
57747c478bd9Sstevel@tonic-gate * Don't send the next mblk since the whole mblk
57757c478bd9Sstevel@tonic-gate * does not fit.
57767c478bd9Sstevel@tonic-gate */
57777c478bd9Sstevel@tonic-gate break;
57787c478bd9Sstevel@tonic-gate mp2->b_cont = dupb(mp);
57797c478bd9Sstevel@tonic-gate mp2 = mp2->b_cont;
57807c478bd9Sstevel@tonic-gate if (mp2 == NULL) {
57817c478bd9Sstevel@tonic-gate freemsg(mp1);
57827c478bd9Sstevel@tonic-gate return (NULL);
57837c478bd9Sstevel@tonic-gate }
57847c478bd9Sstevel@tonic-gate mp2->b_rptr += off;
57857c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
57867c478bd9Sstevel@tonic-gate (uintptr_t)INT_MAX);
57877c478bd9Sstevel@tonic-gate
57887c478bd9Sstevel@tonic-gate data_length += (int)(mp2->b_wptr - mp2->b_rptr);
57897c478bd9Sstevel@tonic-gate if (data_length > max_to_send) {
57907c478bd9Sstevel@tonic-gate mp2->b_wptr -= data_length - max_to_send;
57917c478bd9Sstevel@tonic-gate data_length = max_to_send;
57927c478bd9Sstevel@tonic-gate off = mp2->b_wptr - mp->b_rptr;
57937c478bd9Sstevel@tonic-gate break;
57947c478bd9Sstevel@tonic-gate } else {
57957c478bd9Sstevel@tonic-gate off = 0;
57967c478bd9Sstevel@tonic-gate }
57977c478bd9Sstevel@tonic-gate }
57987c478bd9Sstevel@tonic-gate if (offset != NULL) {
57997c478bd9Sstevel@tonic-gate *offset = off;
58007c478bd9Sstevel@tonic-gate *end_mp = mp;
58017c478bd9Sstevel@tonic-gate }
58027c478bd9Sstevel@tonic-gate if (seg_len != NULL) {
58037c478bd9Sstevel@tonic-gate *seg_len = data_length;
58047c478bd9Sstevel@tonic-gate }
58057c478bd9Sstevel@tonic-gate
58067c478bd9Sstevel@tonic-gate rptr = mp1->b_rptr + tcp_wroff_xtra;
58077c478bd9Sstevel@tonic-gate mp1->b_rptr = rptr;
58087c478bd9Sstevel@tonic-gate mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len;
58097c478bd9Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len);
58107c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len];
58117c478bd9Sstevel@tonic-gate U32_TO_ABE32(seq, tcph->th_seq);
58127c478bd9Sstevel@tonic-gate
58137c478bd9Sstevel@tonic-gate /*
58147c478bd9Sstevel@tonic-gate * Use tcp_unsent to determine if the PUSH bit should be used assumes
58157c478bd9Sstevel@tonic-gate * that this function was called from tcp_wput_data. Thus, when called
58167c478bd9Sstevel@tonic-gate * to retransmit data the setting of the PUSH bit may appear some
58177c478bd9Sstevel@tonic-gate * what random in that it might get set when it should not. This
58187c478bd9Sstevel@tonic-gate * should not pose any performance issues.
58197c478bd9Sstevel@tonic-gate */
58207c478bd9Sstevel@tonic-gate if (data_length != 0 && (tcp->tcp_unsent == 0 ||
58217c478bd9Sstevel@tonic-gate tcp->tcp_unsent == data_length)) {
58227c478bd9Sstevel@tonic-gate flags = TH_ACK | TH_PUSH;
58237c478bd9Sstevel@tonic-gate } else {
58247c478bd9Sstevel@tonic-gate flags = TH_ACK;
58257c478bd9Sstevel@tonic-gate }
58267c478bd9Sstevel@tonic-gate
58277c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
58287c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
58297c478bd9Sstevel@tonic-gate flags |= TH_ECE;
58307c478bd9Sstevel@tonic-gate
58317c478bd9Sstevel@tonic-gate /*
58327c478bd9Sstevel@tonic-gate * Only set ECT bit and ECN_CWR if a segment contains new data.
58337c478bd9Sstevel@tonic-gate * There is no TCP flow control for non-data segments, and
58347c478bd9Sstevel@tonic-gate * only data segment is transmitted reliably.
58357c478bd9Sstevel@tonic-gate */
58367c478bd9Sstevel@tonic-gate if (data_length > 0 && !rexmit) {
58377c478bd9Sstevel@tonic-gate SET_ECT(tcp, rptr);
58387c478bd9Sstevel@tonic-gate if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
58397c478bd9Sstevel@tonic-gate flags |= TH_CWR;
58407c478bd9Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_TRUE;
58417c478bd9Sstevel@tonic-gate }
58427c478bd9Sstevel@tonic-gate }
58437c478bd9Sstevel@tonic-gate }
58447c478bd9Sstevel@tonic-gate
58457c478bd9Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
58467c478bd9Sstevel@tonic-gate uint32_t u1;
58477c478bd9Sstevel@tonic-gate
58487c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_ISS_VALID) &&
58497c478bd9Sstevel@tonic-gate seq == tcp->tcp_iss) {
58507c478bd9Sstevel@tonic-gate uchar_t *wptr;
58517c478bd9Sstevel@tonic-gate
58527c478bd9Sstevel@tonic-gate /*
58537c478bd9Sstevel@tonic-gate * Tack on the MSS option. It is always needed
58547c478bd9Sstevel@tonic-gate * for both active and passive open.
58557c478bd9Sstevel@tonic-gate */
58567c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
58577c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_MAXSEG;
58587c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_MAXSEG_LEN;
58597c478bd9Sstevel@tonic-gate wptr += 2;
58607c478bd9Sstevel@tonic-gate /*
58617c478bd9Sstevel@tonic-gate * MSS option value should be interface MTU - MIN
58627c478bd9Sstevel@tonic-gate * TCP/IP header.
58637c478bd9Sstevel@tonic-gate */
58647c478bd9Sstevel@tonic-gate u1 = tcp->tcp_if_mtu - IP_SIMPLE_HDR_LENGTH -
58657c478bd9Sstevel@tonic-gate TCP_MIN_HEADER_LENGTH;
58667c478bd9Sstevel@tonic-gate U16_TO_BE16(u1, wptr);
58677c478bd9Sstevel@tonic-gate mp1->b_wptr = wptr + 2;
58687c478bd9Sstevel@tonic-gate /* Update the offset to cover the additional word */
58697c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += (1 << 4);
58707c478bd9Sstevel@tonic-gate
58717c478bd9Sstevel@tonic-gate /*
58727c478bd9Sstevel@tonic-gate * Note that the following way of filling in
58737c478bd9Sstevel@tonic-gate * TCP options are not optimal. Some NOPs can
58747c478bd9Sstevel@tonic-gate * be saved. But there is no need at this time
58757c478bd9Sstevel@tonic-gate * to optimize it. When it is needed, we will
58767c478bd9Sstevel@tonic-gate * do it.
58777c478bd9Sstevel@tonic-gate */
58787c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
58797c478bd9Sstevel@tonic-gate case TCPS_SYN_SENT:
58807c478bd9Sstevel@tonic-gate flags = TH_SYN;
58817c478bd9Sstevel@tonic-gate
58827c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ws_ok) {
58837c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
58847c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
58857c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_WSCALE;
58867c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_WS_LEN;
58877c478bd9Sstevel@tonic-gate wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
58887c478bd9Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_WS_LEN;
58897c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
58907c478bd9Sstevel@tonic-gate (1 << 4);
58917c478bd9Sstevel@tonic-gate }
58927c478bd9Sstevel@tonic-gate
58937c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
58947c478bd9Sstevel@tonic-gate uint32_t llbolt;
58957c478bd9Sstevel@tonic-gate
58967c478bd9Sstevel@tonic-gate llbolt = prom_gettime();
58977c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
58987c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
58997c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59007c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_TSTAMP;
59017c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_TSTAMP_LEN;
59027c478bd9Sstevel@tonic-gate wptr += 4;
59037c478bd9Sstevel@tonic-gate U32_TO_BE32(llbolt, wptr);
59047c478bd9Sstevel@tonic-gate wptr += 4;
59057c478bd9Sstevel@tonic-gate assert(tcp->tcp_ts_recent == 0);
59067c478bd9Sstevel@tonic-gate U32_TO_BE32(0L, wptr);
59077c478bd9Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_TS_LEN;
59087c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59097c478bd9Sstevel@tonic-gate (3 << 4);
59107c478bd9Sstevel@tonic-gate }
59117c478bd9Sstevel@tonic-gate
59127c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
59137c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
59147c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59157c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59167c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_SACK_PERMITTED;
59177c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_SACK_OK_LEN;
59187c478bd9Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN;
59197c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59207c478bd9Sstevel@tonic-gate (1 << 4);
59217c478bd9Sstevel@tonic-gate }
59227c478bd9Sstevel@tonic-gate
59237c478bd9Sstevel@tonic-gate /*
59247c478bd9Sstevel@tonic-gate * Set up all the bits to tell other side
59257c478bd9Sstevel@tonic-gate * we are ECN capable.
59267c478bd9Sstevel@tonic-gate */
59277c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
59287c478bd9Sstevel@tonic-gate flags |= (TH_ECE | TH_CWR);
59297c478bd9Sstevel@tonic-gate }
59307c478bd9Sstevel@tonic-gate break;
59317c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
59327c478bd9Sstevel@tonic-gate flags |= TH_SYN;
59337c478bd9Sstevel@tonic-gate
59347c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ws_ok) {
59357c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
59367c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59377c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_WSCALE;
59387c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_WS_LEN;
59397c478bd9Sstevel@tonic-gate wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
59407c478bd9Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_WS_LEN;
59417c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += (1 << 4);
59427c478bd9Sstevel@tonic-gate }
59437c478bd9Sstevel@tonic-gate
59447c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
59457c478bd9Sstevel@tonic-gate wptr = mp1->b_wptr;
59467c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59477c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59487c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_SACK_PERMITTED;
59497c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_SACK_OK_LEN;
59507c478bd9Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN;
59517c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59527c478bd9Sstevel@tonic-gate (1 << 4);
59537c478bd9Sstevel@tonic-gate }
59547c478bd9Sstevel@tonic-gate
59557c478bd9Sstevel@tonic-gate /*
59567c478bd9Sstevel@tonic-gate * If the other side is ECN capable, reply
59577c478bd9Sstevel@tonic-gate * that we are also ECN capable.
59587c478bd9Sstevel@tonic-gate */
59597c478bd9Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
59607c478bd9Sstevel@tonic-gate flags |= TH_ECE;
59617c478bd9Sstevel@tonic-gate }
59627c478bd9Sstevel@tonic-gate break;
59637c478bd9Sstevel@tonic-gate default:
59647c478bd9Sstevel@tonic-gate break;
59657c478bd9Sstevel@tonic-gate }
59667c478bd9Sstevel@tonic-gate /* allocb() of adequate mblk assures space */
59677c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
59687c478bd9Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
59697c478bd9Sstevel@tonic-gate if (flags & TH_SYN)
59707c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
59717c478bd9Sstevel@tonic-gate }
59727c478bd9Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
59737c478bd9Sstevel@tonic-gate (seq + data_length) == tcp->tcp_fss) {
59747c478bd9Sstevel@tonic-gate if (!tcp->tcp_fin_acked) {
59757c478bd9Sstevel@tonic-gate flags |= TH_FIN;
59767c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
59777c478bd9Sstevel@tonic-gate }
59787c478bd9Sstevel@tonic-gate if (!tcp->tcp_fin_sent) {
59797c478bd9Sstevel@tonic-gate tcp->tcp_fin_sent = B_TRUE;
59807c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
59817c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
59827c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
59837c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_FIN_WAIT_1;
59847c478bd9Sstevel@tonic-gate break;
59857c478bd9Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
59867c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_LAST_ACK;
59877c478bd9Sstevel@tonic-gate break;
59887c478bd9Sstevel@tonic-gate }
5989*b531f6d1SToomas Soome if (tcp->tcp_suna == tcp->tcp_snxt) {
59907c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
5991*b531f6d1SToomas Soome }
59927c478bd9Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_fss + 1;
59937c478bd9Sstevel@tonic-gate }
59947c478bd9Sstevel@tonic-gate }
59957c478bd9Sstevel@tonic-gate }
59967c478bd9Sstevel@tonic-gate tcph->th_flags[0] = (uchar_t)flags;
59977c478bd9Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
59987c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
59997c478bd9Sstevel@tonic-gate
60007c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
60017c478bd9Sstevel@tonic-gate if (tcp->tcp_state != TCPS_SYN_SENT) {
60027c478bd9Sstevel@tonic-gate uint32_t llbolt = prom_gettime();
60037c478bd9Sstevel@tonic-gate
60047c478bd9Sstevel@tonic-gate U32_TO_BE32(llbolt,
60057c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
60067c478bd9Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
60077c478bd9Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
60087c478bd9Sstevel@tonic-gate }
60097c478bd9Sstevel@tonic-gate }
60107c478bd9Sstevel@tonic-gate
60117c478bd9Sstevel@tonic-gate if (num_sack_blk > 0) {
60127c478bd9Sstevel@tonic-gate uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len;
60137c478bd9Sstevel@tonic-gate sack_blk_t *tmp;
60147c478bd9Sstevel@tonic-gate int32_t i;
60157c478bd9Sstevel@tonic-gate
60167c478bd9Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
60177c478bd9Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
60187c478bd9Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
60197c478bd9Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
60207c478bd9Sstevel@tonic-gate sizeof (sack_blk_t);
60217c478bd9Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
60227c478bd9Sstevel@tonic-gate
60237c478bd9Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
60247c478bd9Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
60257c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
60267c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
60277c478bd9Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
60287c478bd9Sstevel@tonic-gate wptr += sizeof (tcp_seq);
60297c478bd9Sstevel@tonic-gate }
60307c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4);
60317c478bd9Sstevel@tonic-gate }
60327c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX);
60337c478bd9Sstevel@tonic-gate data_length += (int)(mp1->b_wptr - rptr);
60347c478bd9Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
60357c478bd9Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(data_length);
60367c478bd9Sstevel@tonic-gate
60377c478bd9Sstevel@tonic-gate /*
60387c478bd9Sstevel@tonic-gate * Performance hit! We need to pullup the whole message
60397c478bd9Sstevel@tonic-gate * in order to do checksum and for the MAC output routine.
60407c478bd9Sstevel@tonic-gate */
60417c478bd9Sstevel@tonic-gate if (mp1->b_cont != NULL) {
60427c478bd9Sstevel@tonic-gate int mp_size;
60437c478bd9Sstevel@tonic-gate #ifdef DEBUG
60447c478bd9Sstevel@tonic-gate printf("Multiple mblk %d\n", msgdsize(mp1));
60457c478bd9Sstevel@tonic-gate #endif
60461915be19Sjgj mp2 = mp1;
60477c478bd9Sstevel@tonic-gate new_mp = allocb(msgdsize(mp1) + tcp_wroff_xtra, 0);
60487c478bd9Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
60497c478bd9Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr;
60507c478bd9Sstevel@tonic-gate while (mp1 != NULL) {
60517c478bd9Sstevel@tonic-gate mp_size = mp1->b_wptr - mp1->b_rptr;
60527c478bd9Sstevel@tonic-gate bcopy(mp1->b_rptr, new_mp->b_wptr, mp_size);
60537c478bd9Sstevel@tonic-gate new_mp->b_wptr += mp_size;
60547c478bd9Sstevel@tonic-gate mp1 = mp1->b_cont;
60557c478bd9Sstevel@tonic-gate }
60561915be19Sjgj freemsg(mp2);
60577c478bd9Sstevel@tonic-gate mp1 = new_mp;
60587c478bd9Sstevel@tonic-gate }
60597c478bd9Sstevel@tonic-gate tcp_set_cksum(mp1);
60607c478bd9Sstevel@tonic-gate /* Fill in the TTL field as it is 0 in the header template. */
60617c478bd9Sstevel@tonic-gate ((struct ip *)mp1->b_rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
60627c478bd9Sstevel@tonic-gate
60637c478bd9Sstevel@tonic-gate return (mp1);
60647c478bd9Sstevel@tonic-gate }
60657c478bd9Sstevel@tonic-gate
60667c478bd9Sstevel@tonic-gate /*
60677c478bd9Sstevel@tonic-gate * Generate a "no listener here" reset in response to the
60687c478bd9Sstevel@tonic-gate * connection request contained within 'mp'
60697c478bd9Sstevel@tonic-gate */
60707c478bd9Sstevel@tonic-gate static void
tcp_xmit_listeners_reset(int sock_id,mblk_t * mp,uint_t ip_hdr_len)60717c478bd9Sstevel@tonic-gate tcp_xmit_listeners_reset(int sock_id, mblk_t *mp, uint_t ip_hdr_len)
60727c478bd9Sstevel@tonic-gate {
60737c478bd9Sstevel@tonic-gate uchar_t *rptr;
60747c478bd9Sstevel@tonic-gate uint32_t seg_len;
60757c478bd9Sstevel@tonic-gate tcph_t *tcph;
60767c478bd9Sstevel@tonic-gate uint32_t seg_seq;
60777c478bd9Sstevel@tonic-gate uint32_t seg_ack;
60787c478bd9Sstevel@tonic-gate uint_t flags;
60797c478bd9Sstevel@tonic-gate
60807c478bd9Sstevel@tonic-gate rptr = mp->b_rptr;
60817c478bd9Sstevel@tonic-gate
60827c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&rptr[ip_hdr_len];
60837c478bd9Sstevel@tonic-gate seg_seq = BE32_TO_U32(tcph->th_seq);
60847c478bd9Sstevel@tonic-gate seg_ack = BE32_TO_U32(tcph->th_ack);
60857c478bd9Sstevel@tonic-gate flags = tcph->th_flags[0];
60867c478bd9Sstevel@tonic-gate
60877c478bd9Sstevel@tonic-gate seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len);
60887c478bd9Sstevel@tonic-gate if (flags & TH_RST) {
60897c478bd9Sstevel@tonic-gate freeb(mp);
60907c478bd9Sstevel@tonic-gate } else if (flags & TH_ACK) {
60917c478bd9Sstevel@tonic-gate tcp_xmit_early_reset("no tcp, reset",
60927c478bd9Sstevel@tonic-gate sock_id, mp, seg_ack, 0, TH_RST, ip_hdr_len);
60937c478bd9Sstevel@tonic-gate } else {
60947c478bd9Sstevel@tonic-gate if (flags & TH_SYN)
60957c478bd9Sstevel@tonic-gate seg_len++;
60967c478bd9Sstevel@tonic-gate tcp_xmit_early_reset("no tcp, reset/ack", sock_id,
60977c478bd9Sstevel@tonic-gate mp, 0, seg_seq + seg_len,
60987c478bd9Sstevel@tonic-gate TH_RST | TH_ACK, ip_hdr_len);
60997c478bd9Sstevel@tonic-gate }
61007c478bd9Sstevel@tonic-gate }
61017c478bd9Sstevel@tonic-gate
61027c478bd9Sstevel@tonic-gate /* Non overlapping byte exchanger */
61037c478bd9Sstevel@tonic-gate static void
tcp_xchg(uchar_t * a,uchar_t * b,int len)61047c478bd9Sstevel@tonic-gate tcp_xchg(uchar_t *a, uchar_t *b, int len)
61057c478bd9Sstevel@tonic-gate {
61067c478bd9Sstevel@tonic-gate uchar_t uch;
61077c478bd9Sstevel@tonic-gate
61087c478bd9Sstevel@tonic-gate while (len-- > 0) {
61097c478bd9Sstevel@tonic-gate uch = a[len];
61107c478bd9Sstevel@tonic-gate a[len] = b[len];
61117c478bd9Sstevel@tonic-gate b[len] = uch;
61127c478bd9Sstevel@tonic-gate }
61137c478bd9Sstevel@tonic-gate }
61147c478bd9Sstevel@tonic-gate
61157c478bd9Sstevel@tonic-gate /*
61167c478bd9Sstevel@tonic-gate * Generate a reset based on an inbound packet for which there is no active
61177c478bd9Sstevel@tonic-gate * tcp state that we can find.
61187c478bd9Sstevel@tonic-gate */
61197c478bd9Sstevel@tonic-gate static void
tcp_xmit_early_reset(char * str,int sock_id,mblk_t * mp,uint32_t seq,uint32_t ack,int ctl,uint_t ip_hdr_len)61207c478bd9Sstevel@tonic-gate tcp_xmit_early_reset(char *str, int sock_id, mblk_t *mp, uint32_t seq,
61217c478bd9Sstevel@tonic-gate uint32_t ack, int ctl, uint_t ip_hdr_len)
61227c478bd9Sstevel@tonic-gate {
61237c478bd9Sstevel@tonic-gate struct ip *iph = NULL;
61247c478bd9Sstevel@tonic-gate ushort_t len;
61257c478bd9Sstevel@tonic-gate tcph_t *tcph;
61267c478bd9Sstevel@tonic-gate int i;
61277c478bd9Sstevel@tonic-gate ipaddr_t addr;
61287c478bd9Sstevel@tonic-gate mblk_t *new_mp;
61297c478bd9Sstevel@tonic-gate
61307c478bd9Sstevel@tonic-gate if (str != NULL) {
61317c478bd9Sstevel@tonic-gate dprintf("tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
61327c478bd9Sstevel@tonic-gate "flags 0x%x\n", str, seq, ack, ctl);
61337c478bd9Sstevel@tonic-gate }
61347c478bd9Sstevel@tonic-gate
61357c478bd9Sstevel@tonic-gate /*
61367c478bd9Sstevel@tonic-gate * We skip reversing source route here.
61377c478bd9Sstevel@tonic-gate * (for now we replace all IP options with EOL)
61387c478bd9Sstevel@tonic-gate */
61397c478bd9Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
61407c478bd9Sstevel@tonic-gate for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
61417c478bd9Sstevel@tonic-gate mp->b_rptr[i] = IPOPT_EOL;
61427c478bd9Sstevel@tonic-gate /*
61437c478bd9Sstevel@tonic-gate * Make sure that src address is not a limited broadcast
61447c478bd9Sstevel@tonic-gate * address. Not all broadcast address checking for the
61457c478bd9Sstevel@tonic-gate * src address is possible, since we don't know the
61467c478bd9Sstevel@tonic-gate * netmask of the src addr.
61477c478bd9Sstevel@tonic-gate * No check for destination address is done, since
61487c478bd9Sstevel@tonic-gate * IP will not pass up a packet with a broadcast dest address
61497c478bd9Sstevel@tonic-gate * to TCP.
61507c478bd9Sstevel@tonic-gate */
61517c478bd9Sstevel@tonic-gate if (iph->ip_src.s_addr == INADDR_ANY ||
61527c478bd9Sstevel@tonic-gate iph->ip_src.s_addr == INADDR_BROADCAST) {
61537c478bd9Sstevel@tonic-gate freemsg(mp);
61547c478bd9Sstevel@tonic-gate return;
61557c478bd9Sstevel@tonic-gate }
61567c478bd9Sstevel@tonic-gate
61577c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
61587c478bd9Sstevel@tonic-gate if (tcph->th_flags[0] & TH_RST) {
61597c478bd9Sstevel@tonic-gate freemsg(mp);
61607c478bd9Sstevel@tonic-gate return;
61617c478bd9Sstevel@tonic-gate }
61627c478bd9Sstevel@tonic-gate /*
61637c478bd9Sstevel@tonic-gate * Now copy the original header to a new buffer. The reason
61647c478bd9Sstevel@tonic-gate * for doing this is that we need to put extra room before
61657c478bd9Sstevel@tonic-gate * the header for the MAC layer address. The original mblk
61667c478bd9Sstevel@tonic-gate * does not have this extra head room.
61677c478bd9Sstevel@tonic-gate */
61687c478bd9Sstevel@tonic-gate len = ip_hdr_len + sizeof (tcph_t);
61697c478bd9Sstevel@tonic-gate if ((new_mp = allocb(len + tcp_wroff_xtra, 0)) == NULL) {
61707c478bd9Sstevel@tonic-gate freemsg(mp);
61717c478bd9Sstevel@tonic-gate return;
61727c478bd9Sstevel@tonic-gate }
61737c478bd9Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
61747c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr, new_mp->b_rptr, len);
61757c478bd9Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr + len;
61767c478bd9Sstevel@tonic-gate freemsg(mp);
61777c478bd9Sstevel@tonic-gate mp = new_mp;
61787c478bd9Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
61797c478bd9Sstevel@tonic-gate tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
61807c478bd9Sstevel@tonic-gate
61817c478bd9Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] = (5 << 4);
61827c478bd9Sstevel@tonic-gate tcp_xchg(tcph->th_fport, tcph->th_lport, 2);
61837c478bd9Sstevel@tonic-gate U32_TO_BE32(ack, tcph->th_ack);
61847c478bd9Sstevel@tonic-gate U32_TO_BE32(seq, tcph->th_seq);
61857c478bd9Sstevel@tonic-gate U16_TO_BE16(0, tcph->th_win);
61867c478bd9Sstevel@tonic-gate bzero(tcph->th_sum, sizeof (int16_t));
61877c478bd9Sstevel@tonic-gate tcph->th_flags[0] = (uint8_t)ctl;
61887c478bd9Sstevel@tonic-gate if (ctl & TH_RST) {
61897c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutRsts);
61907c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
61917c478bd9Sstevel@tonic-gate }
61927c478bd9Sstevel@tonic-gate
61937c478bd9Sstevel@tonic-gate iph->ip_len = htons(len);
61947c478bd9Sstevel@tonic-gate /* Swap addresses */
61957c478bd9Sstevel@tonic-gate addr = iph->ip_src.s_addr;
61967c478bd9Sstevel@tonic-gate iph->ip_src = iph->ip_dst;
61977c478bd9Sstevel@tonic-gate iph->ip_dst.s_addr = addr;
61987c478bd9Sstevel@tonic-gate iph->ip_id = 0;
61997c478bd9Sstevel@tonic-gate iph->ip_ttl = 0;
62007c478bd9Sstevel@tonic-gate tcp_set_cksum(mp);
62017c478bd9Sstevel@tonic-gate iph->ip_ttl = (uint8_t)tcp_ipv4_ttl;
62027c478bd9Sstevel@tonic-gate
62037c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
62047c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_early_reset", mp);
62057c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
62067c478bd9Sstevel@tonic-gate freemsg(mp);
62077c478bd9Sstevel@tonic-gate }
62087c478bd9Sstevel@tonic-gate
62097c478bd9Sstevel@tonic-gate static void
tcp_set_cksum(mblk_t * mp)62107c478bd9Sstevel@tonic-gate tcp_set_cksum(mblk_t *mp)
62117c478bd9Sstevel@tonic-gate {
62127c478bd9Sstevel@tonic-gate struct ip *iph;
62137c478bd9Sstevel@tonic-gate tcpha_t *tcph;
62147c478bd9Sstevel@tonic-gate int len;
62157c478bd9Sstevel@tonic-gate
62167c478bd9Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
62177c478bd9Sstevel@tonic-gate tcph = (tcpha_t *)(iph + 1);
62187c478bd9Sstevel@tonic-gate len = ntohs(iph->ip_len);
62197c478bd9Sstevel@tonic-gate /*
62207c478bd9Sstevel@tonic-gate * Calculate the TCP checksum. Need to include the psuedo header,
62217c478bd9Sstevel@tonic-gate * which is similar to the real IP header starting at the TTL field.
62227c478bd9Sstevel@tonic-gate */
62237c478bd9Sstevel@tonic-gate iph->ip_sum = htons(len - IP_SIMPLE_HDR_LENGTH);
62247c478bd9Sstevel@tonic-gate tcph->tha_sum = 0;
62257c478bd9Sstevel@tonic-gate tcph->tha_sum = tcp_cksum((uint16_t *)&(iph->ip_ttl),
62267c478bd9Sstevel@tonic-gate len - IP_SIMPLE_HDR_LENGTH + 12);
62277c478bd9Sstevel@tonic-gate iph->ip_sum = 0;
62287c478bd9Sstevel@tonic-gate }
62297c478bd9Sstevel@tonic-gate
62307c478bd9Sstevel@tonic-gate static uint16_t
tcp_cksum(uint16_t * buf,uint32_t len)62317c478bd9Sstevel@tonic-gate tcp_cksum(uint16_t *buf, uint32_t len)
62327c478bd9Sstevel@tonic-gate {
62337c478bd9Sstevel@tonic-gate /*
62347c478bd9Sstevel@tonic-gate * Compute Internet Checksum for "count" bytes
62357c478bd9Sstevel@tonic-gate * beginning at location "addr".
62367c478bd9Sstevel@tonic-gate */
62377c478bd9Sstevel@tonic-gate int32_t sum = 0;
62387c478bd9Sstevel@tonic-gate
62397c478bd9Sstevel@tonic-gate while (len > 1) {
62407c478bd9Sstevel@tonic-gate /* This is the inner loop */
62417c478bd9Sstevel@tonic-gate sum += *buf++;
62427c478bd9Sstevel@tonic-gate len -= 2;
62437c478bd9Sstevel@tonic-gate }
62447c478bd9Sstevel@tonic-gate
62457c478bd9Sstevel@tonic-gate /* Add left-over byte, if any */
62467c478bd9Sstevel@tonic-gate if (len > 0)
62477c478bd9Sstevel@tonic-gate sum += *(unsigned char *)buf * 256;
62487c478bd9Sstevel@tonic-gate
62497c478bd9Sstevel@tonic-gate /* Fold 32-bit sum to 16 bits */
62507c478bd9Sstevel@tonic-gate while (sum >> 16)
62517c478bd9Sstevel@tonic-gate sum = (sum & 0xffff) + (sum >> 16);
62527c478bd9Sstevel@tonic-gate
62537c478bd9Sstevel@tonic-gate return ((uint16_t)~sum);
62547c478bd9Sstevel@tonic-gate }
62557c478bd9Sstevel@tonic-gate
62567c478bd9Sstevel@tonic-gate /*
62577c478bd9Sstevel@tonic-gate * Type three generator adapted from the random() function in 4.4 BSD:
62587c478bd9Sstevel@tonic-gate */
62597c478bd9Sstevel@tonic-gate
62607c478bd9Sstevel@tonic-gate /*
62617c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1993
62627c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
62637c478bd9Sstevel@tonic-gate *
62647c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
62657c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
62667c478bd9Sstevel@tonic-gate * are met:
62677c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
62687c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
62697c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
62707c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
62717c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
62727c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
62737c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
62747c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
62757c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
62767c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
62777c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
62787c478bd9Sstevel@tonic-gate * without specific prior written permission.
62797c478bd9Sstevel@tonic-gate *
62807c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62817c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62827c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62837c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62847c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62857c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62867c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62877c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62887c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62897c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62907c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
62917c478bd9Sstevel@tonic-gate */
62927c478bd9Sstevel@tonic-gate
62937c478bd9Sstevel@tonic-gate /* Type 3 -- x**31 + x**3 + 1 */
62947c478bd9Sstevel@tonic-gate #define DEG_3 31
62957c478bd9Sstevel@tonic-gate #define SEP_3 3
62967c478bd9Sstevel@tonic-gate
62977c478bd9Sstevel@tonic-gate
62987c478bd9Sstevel@tonic-gate /* Protected by tcp_random_lock */
62997c478bd9Sstevel@tonic-gate static int tcp_randtbl[DEG_3 + 1];
63007c478bd9Sstevel@tonic-gate
63017c478bd9Sstevel@tonic-gate static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
63027c478bd9Sstevel@tonic-gate static int *tcp_random_rptr = &tcp_randtbl[1];
63037c478bd9Sstevel@tonic-gate
63047c478bd9Sstevel@tonic-gate static int *tcp_random_state = &tcp_randtbl[1];
63057c478bd9Sstevel@tonic-gate static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
63067c478bd9Sstevel@tonic-gate
63077c478bd9Sstevel@tonic-gate static void
tcp_random_init(void)63087c478bd9Sstevel@tonic-gate tcp_random_init(void)
63097c478bd9Sstevel@tonic-gate {
63107c478bd9Sstevel@tonic-gate int i;
63117c478bd9Sstevel@tonic-gate uint32_t hrt;
63127c478bd9Sstevel@tonic-gate uint32_t wallclock;
63137c478bd9Sstevel@tonic-gate uint32_t result;
63147c478bd9Sstevel@tonic-gate
63157c478bd9Sstevel@tonic-gate /*
63167c478bd9Sstevel@tonic-gate *
63177c478bd9Sstevel@tonic-gate * XXX We don't have high resolution time in standalone... The
63187c478bd9Sstevel@tonic-gate * following is just some approximation on the comment below.
63197c478bd9Sstevel@tonic-gate *
63207c478bd9Sstevel@tonic-gate * Use high-res timer and current time for seed. Gethrtime() returns
63217c478bd9Sstevel@tonic-gate * a longlong, which may contain resolution down to nanoseconds.
63227c478bd9Sstevel@tonic-gate * The current time will either be a 32-bit or a 64-bit quantity.
63237c478bd9Sstevel@tonic-gate * XOR the two together in a 64-bit result variable.
63247c478bd9Sstevel@tonic-gate * Convert the result to a 32-bit value by multiplying the high-order
63257c478bd9Sstevel@tonic-gate * 32-bits by the low-order 32-bits.
63267c478bd9Sstevel@tonic-gate *
63277c478bd9Sstevel@tonic-gate * XXX We don't have gethrtime() in prom and the wallclock....
63287c478bd9Sstevel@tonic-gate */
63297c478bd9Sstevel@tonic-gate
63307c478bd9Sstevel@tonic-gate hrt = prom_gettime();
63317c478bd9Sstevel@tonic-gate wallclock = (uint32_t)time(NULL);
63327c478bd9Sstevel@tonic-gate result = wallclock ^ hrt;
63337c478bd9Sstevel@tonic-gate tcp_random_state[0] = result;
63347c478bd9Sstevel@tonic-gate
63357c478bd9Sstevel@tonic-gate for (i = 1; i < DEG_3; i++)
63367c478bd9Sstevel@tonic-gate tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
63377c478bd9Sstevel@tonic-gate + 12345;
63387c478bd9Sstevel@tonic-gate tcp_random_fptr = &tcp_random_state[SEP_3];
63397c478bd9Sstevel@tonic-gate tcp_random_rptr = &tcp_random_state[0];
63407c478bd9Sstevel@tonic-gate for (i = 0; i < 10 * DEG_3; i++)
63417c478bd9Sstevel@tonic-gate (void) tcp_random();
63427c478bd9Sstevel@tonic-gate }
63437c478bd9Sstevel@tonic-gate
63447c478bd9Sstevel@tonic-gate /*
63457c478bd9Sstevel@tonic-gate * tcp_random: Return a random number in the range [1 - (128K + 1)].
63467c478bd9Sstevel@tonic-gate * This range is selected to be approximately centered on TCP_ISS / 2,
63477c478bd9Sstevel@tonic-gate * and easy to compute. We get this value by generating a 32-bit random
63487c478bd9Sstevel@tonic-gate * number, selecting out the high-order 17 bits, and then adding one so
63497c478bd9Sstevel@tonic-gate * that we never return zero.
63507c478bd9Sstevel@tonic-gate */
63517c478bd9Sstevel@tonic-gate static int
tcp_random(void)63527c478bd9Sstevel@tonic-gate tcp_random(void)
63537c478bd9Sstevel@tonic-gate {
63547c478bd9Sstevel@tonic-gate int i;
63557c478bd9Sstevel@tonic-gate
63567c478bd9Sstevel@tonic-gate *tcp_random_fptr += *tcp_random_rptr;
63577c478bd9Sstevel@tonic-gate
63587c478bd9Sstevel@tonic-gate /*
63597c478bd9Sstevel@tonic-gate * The high-order bits are more random than the low-order bits,
63607c478bd9Sstevel@tonic-gate * so we select out the high-order 17 bits and add one so that
63617c478bd9Sstevel@tonic-gate * we never return zero.
63627c478bd9Sstevel@tonic-gate */
63637c478bd9Sstevel@tonic-gate i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
63647c478bd9Sstevel@tonic-gate if (++tcp_random_fptr >= tcp_random_end_ptr) {
63657c478bd9Sstevel@tonic-gate tcp_random_fptr = tcp_random_state;
63667c478bd9Sstevel@tonic-gate ++tcp_random_rptr;
63677c478bd9Sstevel@tonic-gate } else if (++tcp_random_rptr >= tcp_random_end_ptr)
63687c478bd9Sstevel@tonic-gate tcp_random_rptr = tcp_random_state;
63697c478bd9Sstevel@tonic-gate
63707c478bd9Sstevel@tonic-gate return (i);
63717c478bd9Sstevel@tonic-gate }
63727c478bd9Sstevel@tonic-gate
63737c478bd9Sstevel@tonic-gate /*
63747c478bd9Sstevel@tonic-gate * Generate ISS, taking into account NDD changes may happen halfway through.
63757c478bd9Sstevel@tonic-gate * (If the iss is not zero, set it.)
63767c478bd9Sstevel@tonic-gate */
63777c478bd9Sstevel@tonic-gate static void
tcp_iss_init(tcp_t * tcp)63787c478bd9Sstevel@tonic-gate tcp_iss_init(tcp_t *tcp)
63797c478bd9Sstevel@tonic-gate {
63807c478bd9Sstevel@tonic-gate tcp_iss_incr_extra += (ISS_INCR >> 1);
63817c478bd9Sstevel@tonic-gate tcp->tcp_iss = tcp_iss_incr_extra;
63827c478bd9Sstevel@tonic-gate tcp->tcp_iss += (prom_gettime() >> ISS_NSEC_SHT) + tcp_random();
63837c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits = TCP_ISS_VALID;
63847c478bd9Sstevel@tonic-gate tcp->tcp_fss = tcp->tcp_iss - 1;
63857c478bd9Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss;
63867c478bd9Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_iss + 1;
63877c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
63887c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
63897c478bd9Sstevel@tonic-gate }
63907c478bd9Sstevel@tonic-gate
63917c478bd9Sstevel@tonic-gate /*
63927c478bd9Sstevel@tonic-gate * Diagnostic routine used to return a string associated with the tcp state.
63937c478bd9Sstevel@tonic-gate * Note that if the caller does not supply a buffer, it will use an internal
63947c478bd9Sstevel@tonic-gate * static string. This means that if multiple threads call this function at
63957c478bd9Sstevel@tonic-gate * the same time, output can be corrupted... Note also that this function
63967c478bd9Sstevel@tonic-gate * does not check the size of the supplied buffer. The caller has to make
63977c478bd9Sstevel@tonic-gate * sure that it is big enough.
63987c478bd9Sstevel@tonic-gate */
63997c478bd9Sstevel@tonic-gate static char *
tcp_display(tcp_t * tcp,char * sup_buf,char format)64007c478bd9Sstevel@tonic-gate tcp_display(tcp_t *tcp, char *sup_buf, char format)
64017c478bd9Sstevel@tonic-gate {
64027c478bd9Sstevel@tonic-gate char buf1[30];
64037c478bd9Sstevel@tonic-gate static char priv_buf[INET_ADDRSTRLEN * 2 + 80];
64047c478bd9Sstevel@tonic-gate char *buf;
64057c478bd9Sstevel@tonic-gate char *cp;
64067c478bd9Sstevel@tonic-gate char local_addrbuf[INET_ADDRSTRLEN];
64077c478bd9Sstevel@tonic-gate char remote_addrbuf[INET_ADDRSTRLEN];
64087c478bd9Sstevel@tonic-gate struct in_addr addr;
64097c478bd9Sstevel@tonic-gate
64107c478bd9Sstevel@tonic-gate if (sup_buf != NULL)
64117c478bd9Sstevel@tonic-gate buf = sup_buf;
64127c478bd9Sstevel@tonic-gate else
64137c478bd9Sstevel@tonic-gate buf = priv_buf;
64147c478bd9Sstevel@tonic-gate
64157c478bd9Sstevel@tonic-gate if (tcp == NULL)
64167c478bd9Sstevel@tonic-gate return ("NULL_TCP");
64177c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
64187c478bd9Sstevel@tonic-gate case TCPS_CLOSED:
64197c478bd9Sstevel@tonic-gate cp = "TCP_CLOSED";
64207c478bd9Sstevel@tonic-gate break;
64217c478bd9Sstevel@tonic-gate case TCPS_IDLE:
64227c478bd9Sstevel@tonic-gate cp = "TCP_IDLE";
64237c478bd9Sstevel@tonic-gate break;
64247c478bd9Sstevel@tonic-gate case TCPS_BOUND:
64257c478bd9Sstevel@tonic-gate cp = "TCP_BOUND";
64267c478bd9Sstevel@tonic-gate break;
64277c478bd9Sstevel@tonic-gate case TCPS_LISTEN:
64287c478bd9Sstevel@tonic-gate cp = "TCP_LISTEN";
64297c478bd9Sstevel@tonic-gate break;
64307c478bd9Sstevel@tonic-gate case TCPS_SYN_SENT:
64317c478bd9Sstevel@tonic-gate cp = "TCP_SYN_SENT";
64327c478bd9Sstevel@tonic-gate break;
64337c478bd9Sstevel@tonic-gate case TCPS_SYN_RCVD:
64347c478bd9Sstevel@tonic-gate cp = "TCP_SYN_RCVD";
64357c478bd9Sstevel@tonic-gate break;
64367c478bd9Sstevel@tonic-gate case TCPS_ESTABLISHED:
64377c478bd9Sstevel@tonic-gate cp = "TCP_ESTABLISHED";
64387c478bd9Sstevel@tonic-gate break;
64397c478bd9Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
64407c478bd9Sstevel@tonic-gate cp = "TCP_CLOSE_WAIT";
64417c478bd9Sstevel@tonic-gate break;
64427c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
64437c478bd9Sstevel@tonic-gate cp = "TCP_FIN_WAIT_1";
64447c478bd9Sstevel@tonic-gate break;
64457c478bd9Sstevel@tonic-gate case TCPS_CLOSING:
64467c478bd9Sstevel@tonic-gate cp = "TCP_CLOSING";
64477c478bd9Sstevel@tonic-gate break;
64487c478bd9Sstevel@tonic-gate case TCPS_LAST_ACK:
64497c478bd9Sstevel@tonic-gate cp = "TCP_LAST_ACK";
64507c478bd9Sstevel@tonic-gate break;
64517c478bd9Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
64527c478bd9Sstevel@tonic-gate cp = "TCP_FIN_WAIT_2";
64537c478bd9Sstevel@tonic-gate break;
64547c478bd9Sstevel@tonic-gate case TCPS_TIME_WAIT:
64557c478bd9Sstevel@tonic-gate cp = "TCP_TIME_WAIT";
64567c478bd9Sstevel@tonic-gate break;
64577c478bd9Sstevel@tonic-gate default:
64587c478bd9Sstevel@tonic-gate (void) sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state);
64597c478bd9Sstevel@tonic-gate cp = buf1;
64607c478bd9Sstevel@tonic-gate break;
64617c478bd9Sstevel@tonic-gate }
64627c478bd9Sstevel@tonic-gate switch (format) {
64637c478bd9Sstevel@tonic-gate case DISP_ADDR_AND_PORT:
64647c478bd9Sstevel@tonic-gate /*
64657c478bd9Sstevel@tonic-gate * Note that we use the remote address in the tcp_b
64667c478bd9Sstevel@tonic-gate * structure. This means that it will print out
64677c478bd9Sstevel@tonic-gate * the real destination address, not the next hop's
64687c478bd9Sstevel@tonic-gate * address if source routing is used.
64697c478bd9Sstevel@tonic-gate */
64707c478bd9Sstevel@tonic-gate addr.s_addr = tcp->tcp_bound_source;
64717c478bd9Sstevel@tonic-gate bcopy(inet_ntoa(addr), local_addrbuf, sizeof (local_addrbuf));
64727c478bd9Sstevel@tonic-gate addr.s_addr = tcp->tcp_remote;
64737c478bd9Sstevel@tonic-gate bcopy(inet_ntoa(addr), remote_addrbuf, sizeof (remote_addrbuf));
64747c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (priv_buf), "[%s.%u, %s.%u] %s",
64757c478bd9Sstevel@tonic-gate local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf,
64767c478bd9Sstevel@tonic-gate ntohs(tcp->tcp_fport), cp);
64777c478bd9Sstevel@tonic-gate break;
64787c478bd9Sstevel@tonic-gate case DISP_PORT_ONLY:
64797c478bd9Sstevel@tonic-gate default:
64807c478bd9Sstevel@tonic-gate (void) snprintf(buf, sizeof (priv_buf), "[%u, %u] %s",
64817c478bd9Sstevel@tonic-gate ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp);
64827c478bd9Sstevel@tonic-gate break;
64837c478bd9Sstevel@tonic-gate }
64847c478bd9Sstevel@tonic-gate
64857c478bd9Sstevel@tonic-gate return (buf);
64867c478bd9Sstevel@tonic-gate }
64877c478bd9Sstevel@tonic-gate
64887c478bd9Sstevel@tonic-gate /*
64897c478bd9Sstevel@tonic-gate * Add a new piece to the tcp reassembly queue. If the gap at the beginning
64907c478bd9Sstevel@tonic-gate * is filled, return as much as we can. The message passed in may be
64917c478bd9Sstevel@tonic-gate * multi-part, chained using b_cont. "start" is the starting sequence
64927c478bd9Sstevel@tonic-gate * number for this piece.
64937c478bd9Sstevel@tonic-gate */
64947c478bd9Sstevel@tonic-gate static mblk_t *
tcp_reass(tcp_t * tcp,mblk_t * mp,uint32_t start)64957c478bd9Sstevel@tonic-gate tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
64967c478bd9Sstevel@tonic-gate {
64977c478bd9Sstevel@tonic-gate uint32_t end;
64987c478bd9Sstevel@tonic-gate mblk_t *mp1;
64997c478bd9Sstevel@tonic-gate mblk_t *mp2;
65007c478bd9Sstevel@tonic-gate mblk_t *next_mp;
65017c478bd9Sstevel@tonic-gate uint32_t u1;
65027c478bd9Sstevel@tonic-gate
65037c478bd9Sstevel@tonic-gate /* Walk through all the new pieces. */
65047c478bd9Sstevel@tonic-gate do {
65057c478bd9Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
65067c478bd9Sstevel@tonic-gate (uintptr_t)INT_MAX);
65077c478bd9Sstevel@tonic-gate end = start + (int)(mp->b_wptr - mp->b_rptr);
65087c478bd9Sstevel@tonic-gate next_mp = mp->b_cont;
65097c478bd9Sstevel@tonic-gate if (start == end) {
65107c478bd9Sstevel@tonic-gate /* Empty. Blast it. */
65117c478bd9Sstevel@tonic-gate freeb(mp);
65127c478bd9Sstevel@tonic-gate continue;
65137c478bd9Sstevel@tonic-gate }
65147c478bd9Sstevel@tonic-gate mp->b_cont = NULL;
65157c478bd9Sstevel@tonic-gate TCP_REASS_SET_SEQ(mp, start);
65167c478bd9Sstevel@tonic-gate TCP_REASS_SET_END(mp, end);
65177c478bd9Sstevel@tonic-gate mp1 = tcp->tcp_reass_tail;
65187c478bd9Sstevel@tonic-gate if (!mp1) {
65197c478bd9Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
65207c478bd9Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65217c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataUnorderSegs);
65227c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataUnorderBytes, end - start);
65237c478bd9Sstevel@tonic-gate continue;
65247c478bd9Sstevel@tonic-gate }
65257c478bd9Sstevel@tonic-gate /* New stuff completely beyond tail? */
65267c478bd9Sstevel@tonic-gate if (SEQ_GEQ(start, TCP_REASS_END(mp1))) {
65277c478bd9Sstevel@tonic-gate /* Link it on end. */
65287c478bd9Sstevel@tonic-gate mp1->b_cont = mp;
65297c478bd9Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
65307c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataUnorderSegs);
65317c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataUnorderBytes, end - start);
65327c478bd9Sstevel@tonic-gate continue;
65337c478bd9Sstevel@tonic-gate }
65347c478bd9Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65357c478bd9Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp1);
65367c478bd9Sstevel@tonic-gate /* New stuff at the front? */
65377c478bd9Sstevel@tonic-gate if (SEQ_LT(start, u1)) {
65387c478bd9Sstevel@tonic-gate /* Yes... Check for overlap. */
65397c478bd9Sstevel@tonic-gate mp->b_cont = mp1;
65407c478bd9Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65417c478bd9Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp);
65427c478bd9Sstevel@tonic-gate continue;
65437c478bd9Sstevel@tonic-gate }
65447c478bd9Sstevel@tonic-gate /*
65457c478bd9Sstevel@tonic-gate * The new piece fits somewhere between the head and tail.
65467c478bd9Sstevel@tonic-gate * We find our slot, where mp1 precedes us and mp2 trails.
65477c478bd9Sstevel@tonic-gate */
65487c478bd9Sstevel@tonic-gate for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
65497c478bd9Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp2);
65507c478bd9Sstevel@tonic-gate if (SEQ_LEQ(start, u1))
65517c478bd9Sstevel@tonic-gate break;
65527c478bd9Sstevel@tonic-gate }
65537c478bd9Sstevel@tonic-gate /* Link ourselves in */
65547c478bd9Sstevel@tonic-gate mp->b_cont = mp2;
65557c478bd9Sstevel@tonic-gate mp1->b_cont = mp;
65567c478bd9Sstevel@tonic-gate
65577c478bd9Sstevel@tonic-gate /* Trim overlap with following mblk(s) first */
65587c478bd9Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp);
65597c478bd9Sstevel@tonic-gate
65607c478bd9Sstevel@tonic-gate /* Trim overlap with preceding mblk */
65617c478bd9Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp1);
65627c478bd9Sstevel@tonic-gate
65637c478bd9Sstevel@tonic-gate } while (start = end, mp = next_mp);
65647c478bd9Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65657c478bd9Sstevel@tonic-gate /* Anything ready to go? */
65667c478bd9Sstevel@tonic-gate if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
65677c478bd9Sstevel@tonic-gate return (NULL);
65687c478bd9Sstevel@tonic-gate /* Eat what we can off the queue */
65697c478bd9Sstevel@tonic-gate for (;;) {
65707c478bd9Sstevel@tonic-gate mp = mp1->b_cont;
65717c478bd9Sstevel@tonic-gate end = TCP_REASS_END(mp1);
65727c478bd9Sstevel@tonic-gate TCP_REASS_SET_SEQ(mp1, 0);
65737c478bd9Sstevel@tonic-gate TCP_REASS_SET_END(mp1, 0);
65747c478bd9Sstevel@tonic-gate if (!mp) {
65757c478bd9Sstevel@tonic-gate tcp->tcp_reass_tail = NULL;
65767c478bd9Sstevel@tonic-gate break;
65777c478bd9Sstevel@tonic-gate }
65787c478bd9Sstevel@tonic-gate if (end != TCP_REASS_SEQ(mp)) {
65797c478bd9Sstevel@tonic-gate mp1->b_cont = NULL;
65807c478bd9Sstevel@tonic-gate break;
65817c478bd9Sstevel@tonic-gate }
65827c478bd9Sstevel@tonic-gate mp1 = mp;
65837c478bd9Sstevel@tonic-gate }
65847c478bd9Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65857c478bd9Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65867c478bd9Sstevel@tonic-gate return (mp1);
65877c478bd9Sstevel@tonic-gate }
65887c478bd9Sstevel@tonic-gate
65897c478bd9Sstevel@tonic-gate /* Eliminate any overlap that mp may have over later mblks */
65907c478bd9Sstevel@tonic-gate static void
tcp_reass_elim_overlap(tcp_t * tcp,mblk_t * mp)65917c478bd9Sstevel@tonic-gate tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
65927c478bd9Sstevel@tonic-gate {
65937c478bd9Sstevel@tonic-gate uint32_t end;
65947c478bd9Sstevel@tonic-gate mblk_t *mp1;
65957c478bd9Sstevel@tonic-gate uint32_t u1;
65967c478bd9Sstevel@tonic-gate
65977c478bd9Sstevel@tonic-gate end = TCP_REASS_END(mp);
65987c478bd9Sstevel@tonic-gate while ((mp1 = mp->b_cont) != NULL) {
65997c478bd9Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp1);
66007c478bd9Sstevel@tonic-gate if (!SEQ_GT(end, u1))
66017c478bd9Sstevel@tonic-gate break;
66027c478bd9Sstevel@tonic-gate if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
66037c478bd9Sstevel@tonic-gate mp->b_wptr -= end - u1;
66047c478bd9Sstevel@tonic-gate TCP_REASS_SET_END(mp, u1);
66057c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPartDupSegs);
66067c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPartDupBytes, end - u1);
66077c478bd9Sstevel@tonic-gate break;
66087c478bd9Sstevel@tonic-gate }
66097c478bd9Sstevel@tonic-gate mp->b_cont = mp1->b_cont;
66107c478bd9Sstevel@tonic-gate freeb(mp1);
66117c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
66127c478bd9Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes, end - u1);
66137c478bd9Sstevel@tonic-gate }
66147c478bd9Sstevel@tonic-gate if (!mp1)
66157c478bd9Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
66167c478bd9Sstevel@tonic-gate }
66177c478bd9Sstevel@tonic-gate
66187c478bd9Sstevel@tonic-gate /*
66197c478bd9Sstevel@tonic-gate * Remove a connection from the list of detached TIME_WAIT connections.
66207c478bd9Sstevel@tonic-gate */
66217c478bd9Sstevel@tonic-gate static void
tcp_time_wait_remove(tcp_t * tcp)66227c478bd9Sstevel@tonic-gate tcp_time_wait_remove(tcp_t *tcp)
66237c478bd9Sstevel@tonic-gate {
66247c478bd9Sstevel@tonic-gate if (tcp->tcp_time_wait_expire == 0) {
66257c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_next == NULL);
66267c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev == NULL);
66277c478bd9Sstevel@tonic-gate return;
66287c478bd9Sstevel@tonic-gate }
66297c478bd9Sstevel@tonic-gate assert(tcp->tcp_state == TCPS_TIME_WAIT);
66307c478bd9Sstevel@tonic-gate if (tcp == tcp_time_wait_head) {
66317c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev == NULL);
66327c478bd9Sstevel@tonic-gate tcp_time_wait_head = tcp->tcp_time_wait_next;
66337c478bd9Sstevel@tonic-gate if (tcp_time_wait_head != NULL) {
66347c478bd9Sstevel@tonic-gate tcp_time_wait_head->tcp_time_wait_prev = NULL;
66357c478bd9Sstevel@tonic-gate } else {
66367c478bd9Sstevel@tonic-gate tcp_time_wait_tail = NULL;
66377c478bd9Sstevel@tonic-gate }
66387c478bd9Sstevel@tonic-gate } else if (tcp == tcp_time_wait_tail) {
66397c478bd9Sstevel@tonic-gate assert(tcp != tcp_time_wait_head);
66407c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_next == NULL);
66417c478bd9Sstevel@tonic-gate tcp_time_wait_tail = tcp->tcp_time_wait_prev;
66427c478bd9Sstevel@tonic-gate assert(tcp_time_wait_tail != NULL);
66437c478bd9Sstevel@tonic-gate tcp_time_wait_tail->tcp_time_wait_next = NULL;
66447c478bd9Sstevel@tonic-gate } else {
66457c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp);
66467c478bd9Sstevel@tonic-gate assert(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp);
66477c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_prev->tcp_time_wait_next =
66487c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_next;
66497c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_next->tcp_time_wait_prev =
66507c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_prev;
66517c478bd9Sstevel@tonic-gate }
66527c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_next = NULL;
66537c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_prev = NULL;
66547c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_expire = 0;
66557c478bd9Sstevel@tonic-gate }
66567c478bd9Sstevel@tonic-gate
66577c478bd9Sstevel@tonic-gate /*
66587c478bd9Sstevel@tonic-gate * Add a connection to the list of detached TIME_WAIT connections
66597c478bd9Sstevel@tonic-gate * and set its time to expire ...
66607c478bd9Sstevel@tonic-gate */
66617c478bd9Sstevel@tonic-gate static void
tcp_time_wait_append(tcp_t * tcp)66627c478bd9Sstevel@tonic-gate tcp_time_wait_append(tcp_t *tcp)
66637c478bd9Sstevel@tonic-gate {
66647c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_expire = prom_gettime() + tcp_time_wait_interval;
66657c478bd9Sstevel@tonic-gate if (tcp->tcp_time_wait_expire == 0)
66667c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_expire = 1;
66677c478bd9Sstevel@tonic-gate
66687c478bd9Sstevel@tonic-gate if (tcp_time_wait_head == NULL) {
66697c478bd9Sstevel@tonic-gate assert(tcp_time_wait_tail == NULL);
66707c478bd9Sstevel@tonic-gate tcp_time_wait_head = tcp;
66717c478bd9Sstevel@tonic-gate } else {
66727c478bd9Sstevel@tonic-gate assert(tcp_time_wait_tail != NULL);
66737c478bd9Sstevel@tonic-gate assert(tcp_time_wait_tail->tcp_state == TCPS_TIME_WAIT);
66747c478bd9Sstevel@tonic-gate tcp_time_wait_tail->tcp_time_wait_next = tcp;
66757c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_prev = tcp_time_wait_tail;
66767c478bd9Sstevel@tonic-gate }
66777c478bd9Sstevel@tonic-gate tcp_time_wait_tail = tcp;
66787c478bd9Sstevel@tonic-gate
66797c478bd9Sstevel@tonic-gate /* for ndd stats about compression */
66807c478bd9Sstevel@tonic-gate tcp_cum_timewait++;
66817c478bd9Sstevel@tonic-gate }
66827c478bd9Sstevel@tonic-gate
66837c478bd9Sstevel@tonic-gate /*
66847c478bd9Sstevel@tonic-gate * Periodic qtimeout routine run on the default queue.
66857c478bd9Sstevel@tonic-gate * Performs 2 functions.
66867c478bd9Sstevel@tonic-gate * 1. Does TIME_WAIT compression on all recently added tcps. List
66877c478bd9Sstevel@tonic-gate * traversal is done backwards from the tail.
66887c478bd9Sstevel@tonic-gate * 2. Blows away all tcps whose TIME_WAIT has expired. List traversal
66897c478bd9Sstevel@tonic-gate * is done forwards from the head.
66907c478bd9Sstevel@tonic-gate */
66917c478bd9Sstevel@tonic-gate void
tcp_time_wait_collector(void)66927c478bd9Sstevel@tonic-gate tcp_time_wait_collector(void)
66937c478bd9Sstevel@tonic-gate {
66947c478bd9Sstevel@tonic-gate tcp_t *tcp;
66957c478bd9Sstevel@tonic-gate uint32_t now;
66967c478bd9Sstevel@tonic-gate
66977c478bd9Sstevel@tonic-gate /*
66987c478bd9Sstevel@tonic-gate * In order to reap time waits reliably, we should use a
66997c478bd9Sstevel@tonic-gate * source of time that is not adjustable by the user
67007c478bd9Sstevel@tonic-gate */
67017c478bd9Sstevel@tonic-gate now = prom_gettime();
67027c478bd9Sstevel@tonic-gate while ((tcp = tcp_time_wait_head) != NULL) {
67037c478bd9Sstevel@tonic-gate /*
67047c478bd9Sstevel@tonic-gate * Compare times using modular arithmetic, since
67057c478bd9Sstevel@tonic-gate * lbolt can wrapover.
67067c478bd9Sstevel@tonic-gate */
67077c478bd9Sstevel@tonic-gate if ((int32_t)(now - tcp->tcp_time_wait_expire) < 0) {
67087c478bd9Sstevel@tonic-gate break;
67097c478bd9Sstevel@tonic-gate }
67107c478bd9Sstevel@tonic-gate /*
67117c478bd9Sstevel@tonic-gate * Note that the err must be 0 as there is no socket
67127c478bd9Sstevel@tonic-gate * associated with this TCP...
67137c478bd9Sstevel@tonic-gate */
67147c478bd9Sstevel@tonic-gate (void) tcp_clean_death(-1, tcp, 0);
67157c478bd9Sstevel@tonic-gate }
67167c478bd9Sstevel@tonic-gate /* Schedule next run time. */
67177c478bd9Sstevel@tonic-gate tcp_time_wait_runtime = prom_gettime() + 10000;
67187c478bd9Sstevel@tonic-gate }
67197c478bd9Sstevel@tonic-gate
67207c478bd9Sstevel@tonic-gate void
tcp_time_wait_report(void)67217c478bd9Sstevel@tonic-gate tcp_time_wait_report(void)
67227c478bd9Sstevel@tonic-gate {
67237c478bd9Sstevel@tonic-gate tcp_t *tcp;
67247c478bd9Sstevel@tonic-gate
67257c478bd9Sstevel@tonic-gate printf("Current time %u\n", prom_gettime());
67267c478bd9Sstevel@tonic-gate for (tcp = tcp_time_wait_head; tcp != NULL;
67277c478bd9Sstevel@tonic-gate tcp = tcp->tcp_time_wait_next) {
67287c478bd9Sstevel@tonic-gate printf("%s expires at %u\n", tcp_display(tcp, NULL,
67297c478bd9Sstevel@tonic-gate DISP_ADDR_AND_PORT), tcp->tcp_time_wait_expire);
67307c478bd9Sstevel@tonic-gate }
67317c478bd9Sstevel@tonic-gate }
67327c478bd9Sstevel@tonic-gate
67337c478bd9Sstevel@tonic-gate /*
67347c478bd9Sstevel@tonic-gate * Send up all messages queued on tcp_rcv_list.
67357c478bd9Sstevel@tonic-gate * Have to set tcp_co_norm since we use putnext.
67367c478bd9Sstevel@tonic-gate */
67377c478bd9Sstevel@tonic-gate static void
tcp_rcv_drain(int sock_id,tcp_t * tcp)67387c478bd9Sstevel@tonic-gate tcp_rcv_drain(int sock_id, tcp_t *tcp)
67397c478bd9Sstevel@tonic-gate {
67407c478bd9Sstevel@tonic-gate mblk_t *mp;
67417c478bd9Sstevel@tonic-gate struct inetgram *in_gram;
67427c478bd9Sstevel@tonic-gate mblk_t *in_mp;
67437c478bd9Sstevel@tonic-gate int len;
67447c478bd9Sstevel@tonic-gate
67457c478bd9Sstevel@tonic-gate /* Don't drain if the app has not finished reading all the data. */
67467c478bd9Sstevel@tonic-gate if (sockets[sock_id].so_rcvbuf <= 0)
67477c478bd9Sstevel@tonic-gate return;
67487c478bd9Sstevel@tonic-gate
67497c478bd9Sstevel@tonic-gate /* We might have come here just to updated the rwnd */
67507c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list == NULL)
67517c478bd9Sstevel@tonic-gate goto win_update;
67527c478bd9Sstevel@tonic-gate
67537c478bd9Sstevel@tonic-gate if ((in_gram = (struct inetgram *)bkmem_zalloc(
67547c478bd9Sstevel@tonic-gate sizeof (struct inetgram))) == NULL) {
67557c478bd9Sstevel@tonic-gate return;
67567c478bd9Sstevel@tonic-gate }
67577c478bd9Sstevel@tonic-gate if ((in_mp = allocb(tcp->tcp_rcv_cnt, 0)) == NULL) {
67587c478bd9Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
67597c478bd9Sstevel@tonic-gate return;
67607c478bd9Sstevel@tonic-gate }
67617c478bd9Sstevel@tonic-gate in_gram->igm_level = APP_LVL;
67627c478bd9Sstevel@tonic-gate in_gram->igm_mp = in_mp;
67637c478bd9Sstevel@tonic-gate in_gram->igm_id = 0;
67647c478bd9Sstevel@tonic-gate
67657c478bd9Sstevel@tonic-gate while ((mp = tcp->tcp_rcv_list) != NULL) {
67667c478bd9Sstevel@tonic-gate tcp->tcp_rcv_list = mp->b_cont;
67677c478bd9Sstevel@tonic-gate len = mp->b_wptr - mp->b_rptr;
67687c478bd9Sstevel@tonic-gate bcopy(mp->b_rptr, in_mp->b_wptr, len);
67697c478bd9Sstevel@tonic-gate in_mp->b_wptr += len;
67707c478bd9Sstevel@tonic-gate freeb(mp);
67717c478bd9Sstevel@tonic-gate }
67727c478bd9Sstevel@tonic-gate
67737c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_tail = NULL;
67747c478bd9Sstevel@tonic-gate tcp->tcp_rcv_cnt = 0;
67757c478bd9Sstevel@tonic-gate add_grams(&sockets[sock_id].inq, in_gram);
67767c478bd9Sstevel@tonic-gate
67777c478bd9Sstevel@tonic-gate /* This means that so_rcvbuf can be less than 0. */
67787c478bd9Sstevel@tonic-gate sockets[sock_id].so_rcvbuf -= in_mp->b_wptr - in_mp->b_rptr;
67797c478bd9Sstevel@tonic-gate win_update:
67807c478bd9Sstevel@tonic-gate /*
67817c478bd9Sstevel@tonic-gate * Increase the receive window to max. But we need to do receiver
67827c478bd9Sstevel@tonic-gate * SWS avoidance. This means that we need to check the increase of
67837c478bd9Sstevel@tonic-gate * of receive window is at least 1 MSS.
67847c478bd9Sstevel@tonic-gate */
67857c478bd9Sstevel@tonic-gate if (sockets[sock_id].so_rcvbuf > 0 &&
67867c478bd9Sstevel@tonic-gate (tcp->tcp_rwnd_max - tcp->tcp_rwnd >= tcp->tcp_mss)) {
67877c478bd9Sstevel@tonic-gate tcp->tcp_rwnd = tcp->tcp_rwnd_max;
67887c478bd9Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws,
67897c478bd9Sstevel@tonic-gate tcp->tcp_tcph->th_win);
67907c478bd9Sstevel@tonic-gate }
67917c478bd9Sstevel@tonic-gate }
67927c478bd9Sstevel@tonic-gate
67937c478bd9Sstevel@tonic-gate /*
67947c478bd9Sstevel@tonic-gate * Wrapper for recvfrom to call
67957c478bd9Sstevel@tonic-gate */
67967c478bd9Sstevel@tonic-gate void
tcp_rcv_drain_sock(int sock_id)67977c478bd9Sstevel@tonic-gate tcp_rcv_drain_sock(int sock_id)
67987c478bd9Sstevel@tonic-gate {
67997c478bd9Sstevel@tonic-gate tcp_t *tcp;
68007c478bd9Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL)
68017c478bd9Sstevel@tonic-gate return;
68027c478bd9Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
68037c478bd9Sstevel@tonic-gate }
68047c478bd9Sstevel@tonic-gate
68057c478bd9Sstevel@tonic-gate /*
68067c478bd9Sstevel@tonic-gate * If the inq == NULL and the tcp_rcv_list != NULL, we have data that
68077c478bd9Sstevel@tonic-gate * recvfrom could read. Place a magic message in the inq to let recvfrom
68087c478bd9Sstevel@tonic-gate * know that it needs to call tcp_rcv_drain_sock to pullup the data.
68097c478bd9Sstevel@tonic-gate */
68107c478bd9Sstevel@tonic-gate static void
tcp_drain_needed(int sock_id,tcp_t * tcp)68117c478bd9Sstevel@tonic-gate tcp_drain_needed(int sock_id, tcp_t *tcp)
68127c478bd9Sstevel@tonic-gate {
68137c478bd9Sstevel@tonic-gate struct inetgram *in_gram;
68147c478bd9Sstevel@tonic-gate #ifdef DEBUG
68157c478bd9Sstevel@tonic-gate printf("tcp_drain_needed: inq %x, tcp_rcv_list %x\n",
68167c478bd9Sstevel@tonic-gate sockets[sock_id].inq, tcp->tcp_rcv_list);
68177c478bd9Sstevel@tonic-gate #endif
68187c478bd9Sstevel@tonic-gate if ((sockets[sock_id].inq != NULL) ||
68197c478bd9Sstevel@tonic-gate (tcp->tcp_rcv_list == NULL))
68207c478bd9Sstevel@tonic-gate return;
68217c478bd9Sstevel@tonic-gate
68227c478bd9Sstevel@tonic-gate if ((in_gram = (struct inetgram *)bkmem_zalloc(
68237c478bd9Sstevel@tonic-gate sizeof (struct inetgram))) == NULL)
68247c478bd9Sstevel@tonic-gate return;
68257c478bd9Sstevel@tonic-gate
68267c478bd9Sstevel@tonic-gate in_gram->igm_level = APP_LVL;
68277c478bd9Sstevel@tonic-gate in_gram->igm_mp = NULL;
68287c478bd9Sstevel@tonic-gate in_gram->igm_id = TCP_CALLB_MAGIC_ID;
68297c478bd9Sstevel@tonic-gate
68307c478bd9Sstevel@tonic-gate add_grams(&sockets[sock_id].inq, in_gram);
68317c478bd9Sstevel@tonic-gate }
68327c478bd9Sstevel@tonic-gate
68337c478bd9Sstevel@tonic-gate /*
68347c478bd9Sstevel@tonic-gate * Queue data on tcp_rcv_list which is a b_next chain.
68357c478bd9Sstevel@tonic-gate * Each element of the chain is a b_cont chain.
68367c478bd9Sstevel@tonic-gate *
68377c478bd9Sstevel@tonic-gate * M_DATA messages are added to the current element.
68387c478bd9Sstevel@tonic-gate * Other messages are added as new (b_next) elements.
68397c478bd9Sstevel@tonic-gate */
68407c478bd9Sstevel@tonic-gate static void
tcp_rcv_enqueue(tcp_t * tcp,mblk_t * mp,uint_t seg_len)68417c478bd9Sstevel@tonic-gate tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len)
68427c478bd9Sstevel@tonic-gate {
68437c478bd9Sstevel@tonic-gate assert(seg_len == msgdsize(mp));
68447c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list == NULL) {
68457c478bd9Sstevel@tonic-gate tcp->tcp_rcv_list = mp;
68467c478bd9Sstevel@tonic-gate } else {
68477c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_tail->b_cont = mp;
68487c478bd9Sstevel@tonic-gate }
68497c478bd9Sstevel@tonic-gate while (mp->b_cont)
68507c478bd9Sstevel@tonic-gate mp = mp->b_cont;
68517c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_tail = mp;
68527c478bd9Sstevel@tonic-gate tcp->tcp_rcv_cnt += seg_len;
68537c478bd9Sstevel@tonic-gate tcp->tcp_rwnd -= seg_len;
68547c478bd9Sstevel@tonic-gate #ifdef DEBUG
68557c478bd9Sstevel@tonic-gate printf("tcp_rcv_enqueue rwnd %d\n", tcp->tcp_rwnd);
68567c478bd9Sstevel@tonic-gate #endif
68577c478bd9Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win);
68587c478bd9Sstevel@tonic-gate }
68597c478bd9Sstevel@tonic-gate
68607c478bd9Sstevel@tonic-gate /* The minimum of smoothed mean deviation in RTO calculation. */
68617c478bd9Sstevel@tonic-gate #define TCP_SD_MIN 400
68627c478bd9Sstevel@tonic-gate
68637c478bd9Sstevel@tonic-gate /*
68647c478bd9Sstevel@tonic-gate * Set RTO for this connection. The formula is from Jacobson and Karels'
68657c478bd9Sstevel@tonic-gate * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names
68667c478bd9Sstevel@tonic-gate * are the same as those in Appendix A.2 of that paper.
68677c478bd9Sstevel@tonic-gate *
68687c478bd9Sstevel@tonic-gate * m = new measurement
68697c478bd9Sstevel@tonic-gate * sa = smoothed RTT average (8 * average estimates).
68707c478bd9Sstevel@tonic-gate * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
68717c478bd9Sstevel@tonic-gate */
68727c478bd9Sstevel@tonic-gate static void
tcp_set_rto(tcp_t * tcp,int32_t rtt)68737c478bd9Sstevel@tonic-gate tcp_set_rto(tcp_t *tcp, int32_t rtt)
68747c478bd9Sstevel@tonic-gate {
68757c478bd9Sstevel@tonic-gate int32_t m = rtt;
68767c478bd9Sstevel@tonic-gate uint32_t sa = tcp->tcp_rtt_sa;
68777c478bd9Sstevel@tonic-gate uint32_t sv = tcp->tcp_rtt_sd;
68787c478bd9Sstevel@tonic-gate uint32_t rto;
68797c478bd9Sstevel@tonic-gate
68807c478bd9Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRttUpdate);
68817c478bd9Sstevel@tonic-gate tcp->tcp_rtt_update++;
68827c478bd9Sstevel@tonic-gate
68837c478bd9Sstevel@tonic-gate /* tcp_rtt_sa is not 0 means this is a new sample. */
68847c478bd9Sstevel@tonic-gate if (sa != 0) {
68857c478bd9Sstevel@tonic-gate /*
68867c478bd9Sstevel@tonic-gate * Update average estimator:
68877c478bd9Sstevel@tonic-gate * new rtt = 7/8 old rtt + 1/8 Error
68887c478bd9Sstevel@tonic-gate */
68897c478bd9Sstevel@tonic-gate
68907c478bd9Sstevel@tonic-gate /* m is now Error in estimate. */
68917c478bd9Sstevel@tonic-gate m -= sa >> 3;
68927c478bd9Sstevel@tonic-gate if ((int32_t)(sa += m) <= 0) {
68937c478bd9Sstevel@tonic-gate /*
68947c478bd9Sstevel@tonic-gate * Don't allow the smoothed average to be negative.
68957c478bd9Sstevel@tonic-gate * We use 0 to denote reinitialization of the
68967c478bd9Sstevel@tonic-gate * variables.
68977c478bd9Sstevel@tonic-gate */
68987c478bd9Sstevel@tonic-gate sa = 1;
68997c478bd9Sstevel@tonic-gate }
69007c478bd9Sstevel@tonic-gate
69017c478bd9Sstevel@tonic-gate /*
69027c478bd9Sstevel@tonic-gate * Update deviation estimator:
69037c478bd9Sstevel@tonic-gate * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
69047c478bd9Sstevel@tonic-gate */
69057c478bd9Sstevel@tonic-gate if (m < 0)
69067c478bd9Sstevel@tonic-gate m = -m;
69077c478bd9Sstevel@tonic-gate m -= sv >> 2;
69087c478bd9Sstevel@tonic-gate sv += m;
69097c478bd9Sstevel@tonic-gate } else {
69107c478bd9Sstevel@tonic-gate /*
69117c478bd9Sstevel@tonic-gate * This follows BSD's implementation. So the reinitialized
69127c478bd9Sstevel@tonic-gate * RTO is 3 * m. We cannot go less than 2 because if the
69137c478bd9Sstevel@tonic-gate * link is bandwidth dominated, doubling the window size
69147c478bd9Sstevel@tonic-gate * during slow start means doubling the RTT. We want to be
69157c478bd9Sstevel@tonic-gate * more conservative when we reinitialize our estimates. 3
69167c478bd9Sstevel@tonic-gate * is just a convenient number.
69177c478bd9Sstevel@tonic-gate */
69187c478bd9Sstevel@tonic-gate sa = m << 3;
69197c478bd9Sstevel@tonic-gate sv = m << 1;
69207c478bd9Sstevel@tonic-gate }
69217c478bd9Sstevel@tonic-gate if (sv < TCP_SD_MIN) {
69227c478bd9Sstevel@tonic-gate /*
69237c478bd9Sstevel@tonic-gate * We do not know that if sa captures the delay ACK
69247c478bd9Sstevel@tonic-gate * effect as in a long train of segments, a receiver
69257c478bd9Sstevel@tonic-gate * does not delay its ACKs. So set the minimum of sv
69267c478bd9Sstevel@tonic-gate * to be TCP_SD_MIN, which is default to 400 ms, twice
69277c478bd9Sstevel@tonic-gate * of BSD DATO. That means the minimum of mean
69287c478bd9Sstevel@tonic-gate * deviation is 100 ms.
69297c478bd9Sstevel@tonic-gate *
69307c478bd9Sstevel@tonic-gate */
69317c478bd9Sstevel@tonic-gate sv = TCP_SD_MIN;
69327c478bd9Sstevel@tonic-gate }
69337c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sa = sa;
69347c478bd9Sstevel@tonic-gate tcp->tcp_rtt_sd = sv;
69357c478bd9Sstevel@tonic-gate /*
69367c478bd9Sstevel@tonic-gate * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
69377c478bd9Sstevel@tonic-gate *
69387c478bd9Sstevel@tonic-gate * Add tcp_rexmit_interval extra in case of extreme environment
69397c478bd9Sstevel@tonic-gate * where the algorithm fails to work. The default value of
69407c478bd9Sstevel@tonic-gate * tcp_rexmit_interval_extra should be 0.
69417c478bd9Sstevel@tonic-gate *
69427c478bd9Sstevel@tonic-gate * As we use a finer grained clock than BSD and update
69437c478bd9Sstevel@tonic-gate * RTO for every ACKs, add in another .25 of RTT to the
69447c478bd9Sstevel@tonic-gate * deviation of RTO to accomodate burstiness of 1/4 of
69457c478bd9Sstevel@tonic-gate * window size.
69467c478bd9Sstevel@tonic-gate */
69477c478bd9Sstevel@tonic-gate rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5);
69487c478bd9Sstevel@tonic-gate
69497c478bd9Sstevel@tonic-gate if (rto > tcp_rexmit_interval_max) {
69507c478bd9Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_max;
69517c478bd9Sstevel@tonic-gate } else if (rto < tcp_rexmit_interval_min) {
69527c478bd9Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_min;
69537c478bd9Sstevel@tonic-gate } else {
69547c478bd9Sstevel@tonic-gate tcp->tcp_rto = rto;
69557c478bd9Sstevel@tonic-gate }
69567c478bd9Sstevel@tonic-gate
69577c478bd9Sstevel@tonic-gate /* Now, we can reset tcp_timer_backoff to use the new RTO... */
69587c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
69597c478bd9Sstevel@tonic-gate }
69607c478bd9Sstevel@tonic-gate
69617c478bd9Sstevel@tonic-gate /*
69627c478bd9Sstevel@tonic-gate * Initiate closedown sequence on an active connection.
69637c478bd9Sstevel@tonic-gate * Return value zero for OK return, non-zero for error return.
69647c478bd9Sstevel@tonic-gate */
69657c478bd9Sstevel@tonic-gate static int
tcp_xmit_end(tcp_t * tcp,int sock_id)69667c478bd9Sstevel@tonic-gate tcp_xmit_end(tcp_t *tcp, int sock_id)
69677c478bd9Sstevel@tonic-gate {
69687c478bd9Sstevel@tonic-gate mblk_t *mp;
69697c478bd9Sstevel@tonic-gate
69707c478bd9Sstevel@tonic-gate if (tcp->tcp_state < TCPS_SYN_RCVD ||
69717c478bd9Sstevel@tonic-gate tcp->tcp_state > TCPS_CLOSE_WAIT) {
69727c478bd9Sstevel@tonic-gate /*
69737c478bd9Sstevel@tonic-gate * Invalid state, only states TCPS_SYN_RCVD,
69747c478bd9Sstevel@tonic-gate * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
69757c478bd9Sstevel@tonic-gate */
69767c478bd9Sstevel@tonic-gate return (-1);
69777c478bd9Sstevel@tonic-gate }
69787c478bd9Sstevel@tonic-gate
69797c478bd9Sstevel@tonic-gate tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent;
69807c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits |= TCP_FSS_VALID;
69817c478bd9Sstevel@tonic-gate /*
69827c478bd9Sstevel@tonic-gate * If there is nothing more unsent, send the FIN now.
69837c478bd9Sstevel@tonic-gate * Otherwise, it will go out with the last segment.
69847c478bd9Sstevel@tonic-gate */
69857c478bd9Sstevel@tonic-gate if (tcp->tcp_unsent == 0) {
69867c478bd9Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
69877c478bd9Sstevel@tonic-gate tcp->tcp_fss, B_FALSE, NULL, B_FALSE);
69887c478bd9Sstevel@tonic-gate
69897c478bd9Sstevel@tonic-gate if (mp != NULL) {
69907c478bd9Sstevel@tonic-gate /* Dump the packet when debugging. */
69917c478bd9Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_end", mp);
69927c478bd9Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
69937c478bd9Sstevel@tonic-gate freeb(mp);
69947c478bd9Sstevel@tonic-gate } else {
69957c478bd9Sstevel@tonic-gate /*
69967c478bd9Sstevel@tonic-gate * Couldn't allocate msg. Pretend we got it out.
69977c478bd9Sstevel@tonic-gate * Wait for rexmit timeout.
69987c478bd9Sstevel@tonic-gate */
69997c478bd9Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_fss + 1;
70007c478bd9Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
70017c478bd9Sstevel@tonic-gate }
70027c478bd9Sstevel@tonic-gate
70037c478bd9Sstevel@tonic-gate /*
70047c478bd9Sstevel@tonic-gate * If needed, update tcp_rexmit_snxt as tcp_snxt is
70057c478bd9Sstevel@tonic-gate * changed.
70067c478bd9Sstevel@tonic-gate */
70077c478bd9Sstevel@tonic-gate if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) {
70087c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
70097c478bd9Sstevel@tonic-gate }
70107c478bd9Sstevel@tonic-gate } else {
70117c478bd9Sstevel@tonic-gate tcp_wput_data(tcp, NULL, B_FALSE);
70127c478bd9Sstevel@tonic-gate }
70137c478bd9Sstevel@tonic-gate
70147c478bd9Sstevel@tonic-gate return (0);
70157c478bd9Sstevel@tonic-gate }
70167c478bd9Sstevel@tonic-gate
70177c478bd9Sstevel@tonic-gate int
tcp_opt_set(tcp_t * tcp,int level,int option,const void * optval,socklen_t optlen)70187c478bd9Sstevel@tonic-gate tcp_opt_set(tcp_t *tcp, int level, int option, const void *optval,
70197c478bd9Sstevel@tonic-gate socklen_t optlen)
70207c478bd9Sstevel@tonic-gate {
70217c478bd9Sstevel@tonic-gate switch (level) {
70227c478bd9Sstevel@tonic-gate case SOL_SOCKET: {
70237c478bd9Sstevel@tonic-gate switch (option) {
70247c478bd9Sstevel@tonic-gate case SO_RCVBUF:
70257c478bd9Sstevel@tonic-gate if (optlen == sizeof (int)) {
70267c478bd9Sstevel@tonic-gate int val = *(int *)optval;
70277c478bd9Sstevel@tonic-gate
70287c478bd9Sstevel@tonic-gate if (val > tcp_max_buf) {
70297c478bd9Sstevel@tonic-gate errno = ENOBUFS;
70307c478bd9Sstevel@tonic-gate break;
70317c478bd9Sstevel@tonic-gate }
70327c478bd9Sstevel@tonic-gate /* Silently ignore zero */
70337c478bd9Sstevel@tonic-gate if (val != 0) {
70347c478bd9Sstevel@tonic-gate val = MSS_ROUNDUP(val, tcp->tcp_mss);
70357c478bd9Sstevel@tonic-gate (void) tcp_rwnd_set(tcp, val);
70367c478bd9Sstevel@tonic-gate }
70377c478bd9Sstevel@tonic-gate } else {
70387c478bd9Sstevel@tonic-gate errno = EINVAL;
70397c478bd9Sstevel@tonic-gate }
70407c478bd9Sstevel@tonic-gate break;
70417c478bd9Sstevel@tonic-gate case SO_SNDBUF:
70427c478bd9Sstevel@tonic-gate if (optlen == sizeof (int)) {
70437c478bd9Sstevel@tonic-gate tcp->tcp_xmit_hiwater = *(int *)optval;
70447c478bd9Sstevel@tonic-gate if (tcp->tcp_xmit_hiwater > tcp_max_buf)
70457c478bd9Sstevel@tonic-gate tcp->tcp_xmit_hiwater = tcp_max_buf;
70467c478bd9Sstevel@tonic-gate } else {
70477c478bd9Sstevel@tonic-gate errno = EINVAL;
70487c478bd9Sstevel@tonic-gate }
70497c478bd9Sstevel@tonic-gate break;
70507c478bd9Sstevel@tonic-gate case SO_LINGER:
70517c478bd9Sstevel@tonic-gate if (optlen == sizeof (struct linger)) {
70527c478bd9Sstevel@tonic-gate struct linger *lgr = (struct linger *)optval;
70537c478bd9Sstevel@tonic-gate
70547c478bd9Sstevel@tonic-gate if (lgr->l_onoff) {
70557c478bd9Sstevel@tonic-gate tcp->tcp_linger = 1;
70567c478bd9Sstevel@tonic-gate tcp->tcp_lingertime = lgr->l_linger;
70577c478bd9Sstevel@tonic-gate } else {
70587c478bd9Sstevel@tonic-gate tcp->tcp_linger = 0;
70597c478bd9Sstevel@tonic-gate tcp->tcp_lingertime = 0;
70607c478bd9Sstevel@tonic-gate }
70617c478bd9Sstevel@tonic-gate } else {
70627c478bd9Sstevel@tonic-gate errno = EINVAL;
70637c478bd9Sstevel@tonic-gate }
70647c478bd9Sstevel@tonic-gate break;
70657c478bd9Sstevel@tonic-gate default:
70667c478bd9Sstevel@tonic-gate errno = ENOPROTOOPT;
70677c478bd9Sstevel@tonic-gate break;
70687c478bd9Sstevel@tonic-gate }
70697c478bd9Sstevel@tonic-gate break;
70707c478bd9Sstevel@tonic-gate } /* case SOL_SOCKET */
70717c478bd9Sstevel@tonic-gate case IPPROTO_TCP: {
70727c478bd9Sstevel@tonic-gate switch (option) {
70737c478bd9Sstevel@tonic-gate default:
70747c478bd9Sstevel@tonic-gate errno = ENOPROTOOPT;
70757c478bd9Sstevel@tonic-gate break;
70767c478bd9Sstevel@tonic-gate }
70777c478bd9Sstevel@tonic-gate break;
70787c478bd9Sstevel@tonic-gate } /* case IPPROTO_TCP */
70797c478bd9Sstevel@tonic-gate case IPPROTO_IP: {
70807c478bd9Sstevel@tonic-gate switch (option) {
70817c478bd9Sstevel@tonic-gate default:
70827c478bd9Sstevel@tonic-gate errno = ENOPROTOOPT;
70837c478bd9Sstevel@tonic-gate break;
70847c478bd9Sstevel@tonic-gate }
70857c478bd9Sstevel@tonic-gate break;
70867c478bd9Sstevel@tonic-gate } /* case IPPROTO_IP */
70877c478bd9Sstevel@tonic-gate default:
70887c478bd9Sstevel@tonic-gate errno = ENOPROTOOPT;
70897c478bd9Sstevel@tonic-gate break;
70907c478bd9Sstevel@tonic-gate } /* switch (level) */
70917c478bd9Sstevel@tonic-gate
70927c478bd9Sstevel@tonic-gate if (errno != 0)
70937c478bd9Sstevel@tonic-gate return (-1);
70947c478bd9Sstevel@tonic-gate else
70957c478bd9Sstevel@tonic-gate return (0);
70967c478bd9Sstevel@tonic-gate }
7097