10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51230Sss146032 * Common Development and Distribution License (the "License").
61230Sss146032 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211230Sss146032
220Sstevel@tonic-gate /*
231230Sss146032 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate *
260Sstevel@tonic-gate * tcp.c, Code implementing the TCP protocol.
270Sstevel@tonic-gate */
280Sstevel@tonic-gate
290Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
300Sstevel@tonic-gate
310Sstevel@tonic-gate #include <sys/types.h>
320Sstevel@tonic-gate #include <socket_impl.h>
330Sstevel@tonic-gate #include <socket_inet.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/promif.h>
360Sstevel@tonic-gate #include <sys/socket.h>
370Sstevel@tonic-gate #include <netinet/in_systm.h>
380Sstevel@tonic-gate #include <netinet/in.h>
390Sstevel@tonic-gate #include <netinet/ip.h>
400Sstevel@tonic-gate #include <netinet/tcp.h>
410Sstevel@tonic-gate #include <net/if_types.h>
420Sstevel@tonic-gate #include <sys/salib.h>
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include "ipv4.h"
450Sstevel@tonic-gate #include "ipv4_impl.h"
460Sstevel@tonic-gate #include "mac.h"
470Sstevel@tonic-gate #include "mac_impl.h"
480Sstevel@tonic-gate #include "v4_sum_impl.h"
490Sstevel@tonic-gate #include <sys/bootdebug.h>
500Sstevel@tonic-gate #include "tcp_inet.h"
510Sstevel@tonic-gate #include "tcp_sack.h"
520Sstevel@tonic-gate #include <inet/common.h>
530Sstevel@tonic-gate #include <inet/mib2.h>
540Sstevel@tonic-gate
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate * We need to redefine BUMP_MIB/UPDATE_MIB to not have DTrace probes.
570Sstevel@tonic-gate */
580Sstevel@tonic-gate #undef BUMP_MIB
590Sstevel@tonic-gate #define BUMP_MIB(x) (x)++
600Sstevel@tonic-gate
610Sstevel@tonic-gate #undef UPDATE_MIB
620Sstevel@tonic-gate #define UPDATE_MIB(x, y) x += y
630Sstevel@tonic-gate
640Sstevel@tonic-gate /*
650Sstevel@tonic-gate * MIB-2 stuff for SNMP
660Sstevel@tonic-gate */
670Sstevel@tonic-gate mib2_tcp_t tcp_mib; /* SNMP fixed size info */
680Sstevel@tonic-gate
690Sstevel@tonic-gate /* The TCP mib does not include the following errors. */
700Sstevel@tonic-gate static uint_t tcp_cksum_errors;
710Sstevel@tonic-gate static uint_t tcp_drops;
720Sstevel@tonic-gate
730Sstevel@tonic-gate /* Macros for timestamp comparisons */
740Sstevel@tonic-gate #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0)
750Sstevel@tonic-gate #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0)
760Sstevel@tonic-gate
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate * Parameters for TCP Initial Send Sequence number (ISS) generation.
790Sstevel@tonic-gate * The ISS is calculated by adding three components: a time component
800Sstevel@tonic-gate * which grows by 1 every 4096 nanoseconds (versus every 4 microseconds
810Sstevel@tonic-gate * suggested by RFC 793, page 27);
820Sstevel@tonic-gate * a per-connection component which grows by 125000 for every new connection;
830Sstevel@tonic-gate * and an "extra" component that grows by a random amount centered
840Sstevel@tonic-gate * approximately on 64000. This causes the the ISS generator to cycle every
850Sstevel@tonic-gate * 4.89 hours if no TCP connections are made, and faster if connections are
860Sstevel@tonic-gate * made.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate #define ISS_INCR 250000
890Sstevel@tonic-gate #define ISS_NSEC_SHT 0
900Sstevel@tonic-gate
910Sstevel@tonic-gate static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */
920Sstevel@tonic-gate
930Sstevel@tonic-gate #define TCP_XMIT_LOWATER 4096
940Sstevel@tonic-gate #define TCP_XMIT_HIWATER 49152
950Sstevel@tonic-gate #define TCP_RECV_LOWATER 2048
960Sstevel@tonic-gate #define TCP_RECV_HIWATER 49152
970Sstevel@tonic-gate
980Sstevel@tonic-gate /*
990Sstevel@tonic-gate * PAWS needs a timer for 24 days. This is the number of ms in 24 days
1000Sstevel@tonic-gate */
1010Sstevel@tonic-gate #define PAWS_TIMEOUT ((uint32_t)(24*24*60*60*1000))
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate * TCP options struct returned from tcp_parse_options.
1050Sstevel@tonic-gate */
1060Sstevel@tonic-gate typedef struct tcp_opt_s {
1070Sstevel@tonic-gate uint32_t tcp_opt_mss;
1080Sstevel@tonic-gate uint32_t tcp_opt_wscale;
1090Sstevel@tonic-gate uint32_t tcp_opt_ts_val;
1100Sstevel@tonic-gate uint32_t tcp_opt_ts_ecr;
1110Sstevel@tonic-gate tcp_t *tcp;
1120Sstevel@tonic-gate } tcp_opt_t;
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate * RFC1323-recommended phrasing of TSTAMP option, for easier parsing
1160Sstevel@tonic-gate */
1170Sstevel@tonic-gate
1180Sstevel@tonic-gate #ifdef _BIG_ENDIAN
1190Sstevel@tonic-gate #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \
1200Sstevel@tonic-gate (TCPOPT_TSTAMP << 8) | 10)
1210Sstevel@tonic-gate #else
1220Sstevel@tonic-gate #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \
1230Sstevel@tonic-gate (TCPOPT_NOP << 8) | TCPOPT_NOP)
1240Sstevel@tonic-gate #endif
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate /*
1270Sstevel@tonic-gate * Flags returned from tcp_parse_options.
1280Sstevel@tonic-gate */
1290Sstevel@tonic-gate #define TCP_OPT_MSS_PRESENT 1
1300Sstevel@tonic-gate #define TCP_OPT_WSCALE_PRESENT 2
1310Sstevel@tonic-gate #define TCP_OPT_TSTAMP_PRESENT 4
1320Sstevel@tonic-gate #define TCP_OPT_SACK_OK_PRESENT 8
1330Sstevel@tonic-gate #define TCP_OPT_SACK_PRESENT 16
1340Sstevel@tonic-gate
1350Sstevel@tonic-gate /* TCP option length */
1360Sstevel@tonic-gate #define TCPOPT_NOP_LEN 1
1370Sstevel@tonic-gate #define TCPOPT_MAXSEG_LEN 4
1380Sstevel@tonic-gate #define TCPOPT_WS_LEN 3
1390Sstevel@tonic-gate #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1)
1400Sstevel@tonic-gate #define TCPOPT_TSTAMP_LEN 10
1410Sstevel@tonic-gate #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2)
1420Sstevel@tonic-gate #define TCPOPT_SACK_OK_LEN 2
1430Sstevel@tonic-gate #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2)
1440Sstevel@tonic-gate #define TCPOPT_REAL_SACK_LEN 4
1450Sstevel@tonic-gate #define TCPOPT_MAX_SACK_LEN 36
1460Sstevel@tonic-gate #define TCPOPT_HEADER_LEN 2
1470Sstevel@tonic-gate
1480Sstevel@tonic-gate /* TCP cwnd burst factor. */
1490Sstevel@tonic-gate #define TCP_CWND_INFINITE 65535
1500Sstevel@tonic-gate #define TCP_CWND_SS 3
1510Sstevel@tonic-gate #define TCP_CWND_NORMAL 5
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate /* Named Dispatch Parameter Management Structure */
1540Sstevel@tonic-gate typedef struct tcpparam_s {
1550Sstevel@tonic-gate uint32_t tcp_param_min;
1560Sstevel@tonic-gate uint32_t tcp_param_max;
1570Sstevel@tonic-gate uint32_t tcp_param_val;
1580Sstevel@tonic-gate char *tcp_param_name;
1590Sstevel@tonic-gate } tcpparam_t;
1600Sstevel@tonic-gate
1610Sstevel@tonic-gate /* Max size IP datagram is 64k - 1 */
1620Sstevel@tonic-gate #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (struct ip) + \
1630Sstevel@tonic-gate sizeof (tcph_t)))
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate /* Max of the above */
1660Sstevel@tonic-gate #define TCP_MSS_MAX TCP_MSS_MAX_IPV4
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate /* Largest TCP port number */
1690Sstevel@tonic-gate #define TCP_MAX_PORT (64 * 1024 - 1)
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate /* Round up the value to the nearest mss. */
1720Sstevel@tonic-gate #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss))
1730Sstevel@tonic-gate
1740Sstevel@tonic-gate #define MS 1L
1750Sstevel@tonic-gate #define SECONDS (1000 * MS)
1760Sstevel@tonic-gate #define MINUTES (60 * SECONDS)
1770Sstevel@tonic-gate #define HOURS (60 * MINUTES)
1780Sstevel@tonic-gate #define DAYS (24 * HOURS)
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate /* All NDD params in the core TCP became static variables. */
1810Sstevel@tonic-gate static int tcp_time_wait_interval = 1 * MINUTES;
1820Sstevel@tonic-gate static int tcp_conn_req_max_q = 128;
1830Sstevel@tonic-gate static int tcp_conn_req_max_q0 = 1024;
1840Sstevel@tonic-gate static int tcp_conn_req_min = 1;
1850Sstevel@tonic-gate static int tcp_conn_grace_period = 0 * SECONDS;
1860Sstevel@tonic-gate static int tcp_cwnd_max_ = 1024 * 1024;
1870Sstevel@tonic-gate static int tcp_smallest_nonpriv_port = 1024;
1880Sstevel@tonic-gate static int tcp_ip_abort_cinterval = 3 * MINUTES;
1890Sstevel@tonic-gate static int tcp_ip_abort_linterval = 3 * MINUTES;
1900Sstevel@tonic-gate static int tcp_ip_abort_interval = 8 * MINUTES;
1910Sstevel@tonic-gate static int tcp_ip_notify_cinterval = 10 * SECONDS;
1920Sstevel@tonic-gate static int tcp_ip_notify_interval = 10 * SECONDS;
1930Sstevel@tonic-gate static int tcp_ipv4_ttl = 64;
1940Sstevel@tonic-gate static int tcp_mss_def_ipv4 = 536;
1950Sstevel@tonic-gate static int tcp_mss_max_ipv4 = TCP_MSS_MAX_IPV4;
1960Sstevel@tonic-gate static int tcp_mss_min = 108;
1970Sstevel@tonic-gate static int tcp_naglim_def = (4*1024)-1;
1980Sstevel@tonic-gate static int tcp_rexmit_interval_initial = 3 * SECONDS;
1990Sstevel@tonic-gate static int tcp_rexmit_interval_max = 60 * SECONDS;
2000Sstevel@tonic-gate static int tcp_rexmit_interval_min = 400 * MS;
2010Sstevel@tonic-gate static int tcp_dupack_fast_retransmit = 3;
2020Sstevel@tonic-gate static int tcp_smallest_anon_port = 32 * 1024;
2030Sstevel@tonic-gate static int tcp_largest_anon_port = TCP_MAX_PORT;
2040Sstevel@tonic-gate static int tcp_xmit_lowat = TCP_XMIT_LOWATER;
2050Sstevel@tonic-gate static int tcp_recv_hiwat_minmss = 4;
2060Sstevel@tonic-gate static int tcp_fin_wait_2_flush_interval = 1 * MINUTES;
2070Sstevel@tonic-gate static int tcp_max_buf = 1024 * 1024;
2080Sstevel@tonic-gate static int tcp_wscale_always = 1;
2090Sstevel@tonic-gate static int tcp_tstamp_always = 1;
2100Sstevel@tonic-gate static int tcp_tstamp_if_wscale = 1;
2110Sstevel@tonic-gate static int tcp_rexmit_interval_extra = 0;
2120Sstevel@tonic-gate static int tcp_slow_start_after_idle = 2;
2130Sstevel@tonic-gate static int tcp_slow_start_initial = 2;
2140Sstevel@tonic-gate static int tcp_sack_permitted = 2;
2150Sstevel@tonic-gate static int tcp_ecn_permitted = 2;
2160Sstevel@tonic-gate
2170Sstevel@tonic-gate /* Extra room to fit in headers. */
2180Sstevel@tonic-gate static uint_t tcp_wroff_xtra;
2190Sstevel@tonic-gate
2200Sstevel@tonic-gate /* Hint for next port to try. */
2210Sstevel@tonic-gate static in_port_t tcp_next_port_to_try = 32*1024;
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate * Figure out the value of window scale opton. Note that the rwnd is
2250Sstevel@tonic-gate * ASSUMED to be rounded up to the nearest MSS before the calculation.
2260Sstevel@tonic-gate * We cannot find the scale value and then do a round up of tcp_rwnd
2270Sstevel@tonic-gate * because the scale value may not be correct after that.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate #define SET_WS_VALUE(tcp) \
2300Sstevel@tonic-gate { \
2310Sstevel@tonic-gate int i; \
2320Sstevel@tonic-gate uint32_t rwnd = (tcp)->tcp_rwnd; \
2330Sstevel@tonic-gate for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; \
2340Sstevel@tonic-gate i++, rwnd >>= 1) \
2350Sstevel@tonic-gate ; \
2360Sstevel@tonic-gate (tcp)->tcp_rcv_ws = i; \
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate
2390Sstevel@tonic-gate /*
2400Sstevel@tonic-gate * Set ECN capable transport (ECT) code point in IP header.
2410Sstevel@tonic-gate *
2420Sstevel@tonic-gate * Note that there are 2 ECT code points '01' and '10', which are called
2430Sstevel@tonic-gate * ECT(1) and ECT(0) respectively. Here we follow the original ECT code
2440Sstevel@tonic-gate * point ECT(0) for TCP as described in RFC 2481.
2450Sstevel@tonic-gate */
2460Sstevel@tonic-gate #define SET_ECT(tcp, iph) \
2470Sstevel@tonic-gate if ((tcp)->tcp_ipversion == IPV4_VERSION) { \
2480Sstevel@tonic-gate /* We need to clear the code point first. */ \
2490Sstevel@tonic-gate ((struct ip *)(iph))->ip_tos &= 0xFC; \
2500Sstevel@tonic-gate ((struct ip *)(iph))->ip_tos |= IPH_ECN_ECT0; \
2510Sstevel@tonic-gate }
2520Sstevel@tonic-gate
2530Sstevel@tonic-gate /*
2540Sstevel@tonic-gate * The format argument to pass to tcp_display().
2550Sstevel@tonic-gate * DISP_PORT_ONLY means that the returned string has only port info.
2560Sstevel@tonic-gate * DISP_ADDR_AND_PORT means that the returned string also contains the
2570Sstevel@tonic-gate * remote and local IP address.
2580Sstevel@tonic-gate */
2590Sstevel@tonic-gate #define DISP_PORT_ONLY 1
2600Sstevel@tonic-gate #define DISP_ADDR_AND_PORT 2
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate /*
2630Sstevel@tonic-gate * TCP reassembly macros. We hide starting and ending sequence numbers in
2640Sstevel@tonic-gate * b_next and b_prev of messages on the reassembly queue. The messages are
2650Sstevel@tonic-gate * chained using b_cont. These macros are used in tcp_reass() so we don't
2660Sstevel@tonic-gate * have to see the ugly casts and assignments.
267785Seota * Note. use uintptr_t to suppress the gcc warning.
2680Sstevel@tonic-gate */
269785Seota #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next))
270785Seota #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \
271785Seota (mblk_t *)((uintptr_t)(u)))
272785Seota #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev))
273785Seota #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \
274785Seota (mblk_t *)((uintptr_t)(u)))
2750Sstevel@tonic-gate
2760Sstevel@tonic-gate #define TCP_TIMER_RESTART(tcp, intvl) \
2770Sstevel@tonic-gate (tcp)->tcp_rto_timeout = prom_gettime() + intvl; \
2780Sstevel@tonic-gate (tcp)->tcp_timer_running = B_TRUE;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate static int tcp_accept_comm(tcp_t *, tcp_t *, mblk_t *, uint_t);
2810Sstevel@tonic-gate static mblk_t *tcp_ack_mp(tcp_t *);
2820Sstevel@tonic-gate static in_port_t tcp_bindi(in_port_t, in_addr_t *, boolean_t, boolean_t);
2830Sstevel@tonic-gate static uint16_t tcp_cksum(uint16_t *, uint32_t);
2840Sstevel@tonic-gate static void tcp_clean_death(int, tcp_t *, int err);
2850Sstevel@tonic-gate static tcp_t *tcp_conn_request(tcp_t *, mblk_t *mp, uint_t, uint_t);
2860Sstevel@tonic-gate static char *tcp_display(tcp_t *, char *, char);
2870Sstevel@tonic-gate static int tcp_drain_input(tcp_t *, int, int);
2880Sstevel@tonic-gate static void tcp_drain_needed(int, tcp_t *);
2890Sstevel@tonic-gate static boolean_t tcp_drop_q0(tcp_t *);
2900Sstevel@tonic-gate static mblk_t *tcp_get_seg_mp(tcp_t *, uint32_t, int32_t *);
2910Sstevel@tonic-gate static int tcp_header_len(struct inetgram *);
2920Sstevel@tonic-gate static in_port_t tcp_report_ports(uint16_t *, enum Ports);
2930Sstevel@tonic-gate static int tcp_input(int);
2940Sstevel@tonic-gate static void tcp_iss_init(tcp_t *);
2950Sstevel@tonic-gate static tcp_t *tcp_lookup_ipv4(struct ip *, tcpha_t *, int, int *);
2960Sstevel@tonic-gate static tcp_t *tcp_lookup_listener_ipv4(in_addr_t, in_port_t, int *);
2970Sstevel@tonic-gate static int tcp_conn_check(tcp_t *);
2980Sstevel@tonic-gate static int tcp_close(int);
2990Sstevel@tonic-gate static void tcp_close_detached(tcp_t *);
3000Sstevel@tonic-gate static void tcp_eager_cleanup(tcp_t *, boolean_t, int);
3010Sstevel@tonic-gate static void tcp_eager_unlink(tcp_t *);
3020Sstevel@tonic-gate static void tcp_free(tcp_t *);
3030Sstevel@tonic-gate static int tcp_header_init_ipv4(tcp_t *);
3040Sstevel@tonic-gate static void tcp_mss_set(tcp_t *, uint32_t);
3050Sstevel@tonic-gate static int tcp_parse_options(tcph_t *, tcp_opt_t *);
3060Sstevel@tonic-gate static boolean_t tcp_paws_check(tcp_t *, tcph_t *, tcp_opt_t *);
3070Sstevel@tonic-gate static void tcp_process_options(tcp_t *, tcph_t *);
3080Sstevel@tonic-gate static int tcp_random(void);
3090Sstevel@tonic-gate static void tcp_random_init(void);
3100Sstevel@tonic-gate static mblk_t *tcp_reass(tcp_t *, mblk_t *, uint32_t);
3110Sstevel@tonic-gate static void tcp_reass_elim_overlap(tcp_t *, mblk_t *);
3120Sstevel@tonic-gate static void tcp_rcv_drain(int sock_id, tcp_t *);
3130Sstevel@tonic-gate static void tcp_rcv_enqueue(tcp_t *, mblk_t *, uint_t);
3140Sstevel@tonic-gate static void tcp_rput_data(tcp_t *, mblk_t *, int);
3150Sstevel@tonic-gate static int tcp_rwnd_set(tcp_t *, uint32_t);
3160Sstevel@tonic-gate static int32_t tcp_sack_rxmit(tcp_t *, int);
3170Sstevel@tonic-gate static void tcp_set_cksum(mblk_t *);
3180Sstevel@tonic-gate static void tcp_set_rto(tcp_t *, int32_t);
3190Sstevel@tonic-gate static void tcp_ss_rexmit(tcp_t *, int);
3200Sstevel@tonic-gate static int tcp_state_wait(int, tcp_t *, int);
3210Sstevel@tonic-gate static void tcp_timer(tcp_t *, int);
3220Sstevel@tonic-gate static void tcp_time_wait_append(tcp_t *);
3230Sstevel@tonic-gate static void tcp_time_wait_collector(void);
3240Sstevel@tonic-gate static void tcp_time_wait_processing(tcp_t *, mblk_t *, uint32_t,
3250Sstevel@tonic-gate uint32_t, int, tcph_t *, int sock_id);
3260Sstevel@tonic-gate static void tcp_time_wait_remove(tcp_t *);
3270Sstevel@tonic-gate static in_port_t tcp_update_next_port(in_port_t);
3280Sstevel@tonic-gate static int tcp_verify_cksum(mblk_t *);
3290Sstevel@tonic-gate static void tcp_wput_data(tcp_t *, mblk_t *, int);
3300Sstevel@tonic-gate static void tcp_xmit_ctl(char *, tcp_t *, mblk_t *, uint32_t, uint32_t,
3310Sstevel@tonic-gate int, uint_t, int);
3320Sstevel@tonic-gate static void tcp_xmit_early_reset(char *, int, mblk_t *, uint32_t, uint32_t,
3330Sstevel@tonic-gate int, uint_t);
3340Sstevel@tonic-gate static int tcp_xmit_end(tcp_t *, int);
3350Sstevel@tonic-gate static void tcp_xmit_listeners_reset(int, mblk_t *, uint_t);
3360Sstevel@tonic-gate static mblk_t *tcp_xmit_mp(tcp_t *, mblk_t *, int32_t, int32_t *,
3370Sstevel@tonic-gate mblk_t **, uint32_t, boolean_t, uint32_t *, boolean_t);
3380Sstevel@tonic-gate static int tcp_init_values(tcp_t *, struct inetboot_socket *);
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate #if DEBUG > 1
3410Sstevel@tonic-gate #define TCP_DUMP_PACKET(str, mp) \
3420Sstevel@tonic-gate { \
3430Sstevel@tonic-gate int len = (mp)->b_wptr - (mp)->b_rptr; \
3440Sstevel@tonic-gate \
3450Sstevel@tonic-gate printf("%s: dump TCP(%d): \n", (str), len); \
3460Sstevel@tonic-gate hexdump((char *)(mp)->b_rptr, len); \
3470Sstevel@tonic-gate }
3480Sstevel@tonic-gate #else
3490Sstevel@tonic-gate #define TCP_DUMP_PACKET(str, mp)
3500Sstevel@tonic-gate #endif
3510Sstevel@tonic-gate
3520Sstevel@tonic-gate #ifdef DEBUG
3530Sstevel@tonic-gate #define DEBUG_1(str, arg) printf(str, (arg))
3540Sstevel@tonic-gate #define DEBUG_2(str, arg1, arg2) printf(str, (arg1), (arg2))
3550Sstevel@tonic-gate #define DEBUG_3(str, arg1, arg2, arg3) printf(str, (arg1), (arg2), (arg3))
3560Sstevel@tonic-gate #else
3570Sstevel@tonic-gate #define DEBUG_1(str, arg)
3580Sstevel@tonic-gate #define DEBUG_2(str, arg1, arg2)
3590Sstevel@tonic-gate #define DEBUG_3(str, arg1, arg2, arg3)
3600Sstevel@tonic-gate #endif
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate /* Whether it is the first time TCP is used. */
3630Sstevel@tonic-gate static boolean_t tcp_initialized = B_FALSE;
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate /* TCP time wait list. */
3660Sstevel@tonic-gate static tcp_t *tcp_time_wait_head;
3670Sstevel@tonic-gate static tcp_t *tcp_time_wait_tail;
3680Sstevel@tonic-gate static uint32_t tcp_cum_timewait;
3690Sstevel@tonic-gate /* When the tcp_time_wait_collector is run. */
3700Sstevel@tonic-gate static uint32_t tcp_time_wait_runtime;
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate #define TCP_RUN_TIME_WAIT_COLLECTOR() \
3730Sstevel@tonic-gate if (prom_gettime() > tcp_time_wait_runtime) \
3740Sstevel@tonic-gate tcp_time_wait_collector();
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate /*
3770Sstevel@tonic-gate * Accept will return with an error if there is no connection coming in
3780Sstevel@tonic-gate * after this (in ms).
3790Sstevel@tonic-gate */
3800Sstevel@tonic-gate static int tcp_accept_timeout = 60000;
3810Sstevel@tonic-gate
3820Sstevel@tonic-gate /*
3830Sstevel@tonic-gate * Initialize the TCP-specific parts of a socket.
3840Sstevel@tonic-gate */
3850Sstevel@tonic-gate void
tcp_socket_init(struct inetboot_socket * isp)3860Sstevel@tonic-gate tcp_socket_init(struct inetboot_socket *isp)
3870Sstevel@tonic-gate {
3880Sstevel@tonic-gate /* Do some initializations. */
3890Sstevel@tonic-gate if (!tcp_initialized) {
3900Sstevel@tonic-gate tcp_random_init();
3910Sstevel@tonic-gate /* Extra head room for the MAC layer address. */
3920Sstevel@tonic-gate if ((tcp_wroff_xtra = mac_get_hdr_len()) & 0x3) {
3930Sstevel@tonic-gate tcp_wroff_xtra = (tcp_wroff_xtra & ~0x3) + 0x4;
3940Sstevel@tonic-gate }
3950Sstevel@tonic-gate /* Schedule the first time wait cleanup time */
3960Sstevel@tonic-gate tcp_time_wait_runtime = prom_gettime() + tcp_time_wait_interval;
3970Sstevel@tonic-gate tcp_initialized = B_TRUE;
3980Sstevel@tonic-gate }
3990Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
4000Sstevel@tonic-gate
4010Sstevel@tonic-gate isp->proto = IPPROTO_TCP;
4020Sstevel@tonic-gate isp->input[TRANSPORT_LVL] = tcp_input;
4030Sstevel@tonic-gate /* Socket layer should call tcp_send() directly. */
4040Sstevel@tonic-gate isp->output[TRANSPORT_LVL] = NULL;
4050Sstevel@tonic-gate isp->close[TRANSPORT_LVL] = tcp_close;
4060Sstevel@tonic-gate isp->headerlen[TRANSPORT_LVL] = tcp_header_len;
4070Sstevel@tonic-gate isp->ports = tcp_report_ports;
4080Sstevel@tonic-gate if ((isp->pcb = bkmem_alloc(sizeof (tcp_t))) == NULL) {
4090Sstevel@tonic-gate errno = ENOBUFS;
4100Sstevel@tonic-gate return;
4110Sstevel@tonic-gate }
4120Sstevel@tonic-gate if ((errno = tcp_init_values((tcp_t *)isp->pcb, isp)) != 0) {
4130Sstevel@tonic-gate bkmem_free(isp->pcb, sizeof (tcp_t));
4140Sstevel@tonic-gate return;
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate /*
4170Sstevel@tonic-gate * This is set last because this field is used to determine if
4180Sstevel@tonic-gate * a socket is in use or not.
4190Sstevel@tonic-gate */
4200Sstevel@tonic-gate isp->type = INETBOOT_STREAM;
4210Sstevel@tonic-gate }
4220Sstevel@tonic-gate
4230Sstevel@tonic-gate /*
4240Sstevel@tonic-gate * Return the size of a TCP header including TCP option.
4250Sstevel@tonic-gate */
4260Sstevel@tonic-gate static int
tcp_header_len(struct inetgram * igm)4270Sstevel@tonic-gate tcp_header_len(struct inetgram *igm)
4280Sstevel@tonic-gate {
4290Sstevel@tonic-gate mblk_t *pkt;
4300Sstevel@tonic-gate int ipvers;
4310Sstevel@tonic-gate
4320Sstevel@tonic-gate /* Just returns the standard TCP header without option */
4330Sstevel@tonic-gate if (igm == NULL)
4340Sstevel@tonic-gate return (sizeof (tcph_t));
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate if ((pkt = igm->igm_mp) == NULL)
4370Sstevel@tonic-gate return (0);
4380Sstevel@tonic-gate
4390Sstevel@tonic-gate ipvers = ((struct ip *)pkt->b_rptr)->ip_v;
4400Sstevel@tonic-gate if (ipvers == IPV4_VERSION) {
4410Sstevel@tonic-gate return (TCP_HDR_LENGTH((tcph_t *)(pkt + IPH_HDR_LENGTH(pkt))));
4420Sstevel@tonic-gate } else {
4430Sstevel@tonic-gate dprintf("tcp_header_len: non-IPv4 packet.\n");
4440Sstevel@tonic-gate return (0);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate }
4470Sstevel@tonic-gate
4480Sstevel@tonic-gate /*
4490Sstevel@tonic-gate * Return the requested port number in network order.
4500Sstevel@tonic-gate */
4510Sstevel@tonic-gate static in_port_t
tcp_report_ports(uint16_t * tcphp,enum Ports request)4520Sstevel@tonic-gate tcp_report_ports(uint16_t *tcphp, enum Ports request)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate if (request == SOURCE)
4550Sstevel@tonic-gate return (*(uint16_t *)(((tcph_t *)tcphp)->th_lport));
4560Sstevel@tonic-gate return (*(uint16_t *)(((tcph_t *)tcphp)->th_fport));
4570Sstevel@tonic-gate }
4580Sstevel@tonic-gate
4590Sstevel@tonic-gate /*
4600Sstevel@tonic-gate * Because inetboot is not interrupt driven, TCP can only poll. This
4610Sstevel@tonic-gate * means that there can be packets stuck in the NIC buffer waiting to
4620Sstevel@tonic-gate * be processed. Thus we need to drain them before, for example, sending
4630Sstevel@tonic-gate * anything because an ACK may actually be stuck there.
4640Sstevel@tonic-gate *
4650Sstevel@tonic-gate * The timeout arguments determine how long we should wait for draining.
4660Sstevel@tonic-gate */
4670Sstevel@tonic-gate static int
tcp_drain_input(tcp_t * tcp,int sock_id,int timeout)4680Sstevel@tonic-gate tcp_drain_input(tcp_t *tcp, int sock_id, int timeout)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate struct inetgram *in_gram;
4710Sstevel@tonic-gate struct inetgram *old_in_gram;
4720Sstevel@tonic-gate int old_timeout;
4730Sstevel@tonic-gate mblk_t *mp;
4740Sstevel@tonic-gate int i;
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate dprintf("tcp_drain_input(%d): %s\n", sock_id,
4770Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate /*
4800Sstevel@tonic-gate * Since the driver uses the in_timeout value in the socket
4810Sstevel@tonic-gate * structure to determine the timeout value, we need to save
4820Sstevel@tonic-gate * the original one so that we can restore that after draining.
4830Sstevel@tonic-gate */
4840Sstevel@tonic-gate old_timeout = sockets[sock_id].in_timeout;
4850Sstevel@tonic-gate sockets[sock_id].in_timeout = timeout;
4860Sstevel@tonic-gate
4870Sstevel@tonic-gate /*
4880Sstevel@tonic-gate * We do this because the input queue may have some user
4890Sstevel@tonic-gate * data already.
4900Sstevel@tonic-gate */
4910Sstevel@tonic-gate old_in_gram = sockets[sock_id].inq;
4920Sstevel@tonic-gate sockets[sock_id].inq = NULL;
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /* Go out and check the wire */
4950Sstevel@tonic-gate for (i = MEDIA_LVL; i < TRANSPORT_LVL; i++) {
4960Sstevel@tonic-gate if (sockets[sock_id].input[i] != NULL) {
4970Sstevel@tonic-gate if (sockets[sock_id].input[i](sock_id) < 0) {
4980Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
4990Sstevel@tonic-gate if (sockets[sock_id].inq != NULL)
5000Sstevel@tonic-gate nuke_grams(&sockets[sock_id].inq);
5010Sstevel@tonic-gate sockets[sock_id].inq = old_in_gram;
5020Sstevel@tonic-gate return (-1);
5030Sstevel@tonic-gate }
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate #if DEBUG
5070Sstevel@tonic-gate printf("tcp_drain_input: done with checking packets\n");
5080Sstevel@tonic-gate #endif
5090Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
5100Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
5110Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
5120Sstevel@tonic-gate #if DEBUG
5130Sstevel@tonic-gate printf("tcp_drain_input: unexpected packet "
5140Sstevel@tonic-gate "level %d frame found\n", in_gram->igm_level);
5150Sstevel@tonic-gate #endif
5160Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
5170Sstevel@tonic-gate continue;
5180Sstevel@tonic-gate }
5190Sstevel@tonic-gate mp = in_gram->igm_mp;
5200Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
5210Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
5220Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
5230Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
5240Sstevel@tonic-gate
5250Sstevel@tonic-gate /*
5260Sstevel@tonic-gate * The other side may have closed this connection or
5270Sstevel@tonic-gate * RST us. But we need to continue to process other
5280Sstevel@tonic-gate * packets in the socket's queue because they may be
5290Sstevel@tonic-gate * belong to another TCP connections.
5300Sstevel@tonic-gate */
5310Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
5320Sstevel@tonic-gate tcp = NULL;
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate
5350Sstevel@tonic-gate if (tcp == NULL || sockets[sock_id].pcb == NULL) {
5360Sstevel@tonic-gate if (sockets[sock_id].so_error != 0)
5370Sstevel@tonic-gate return (-1);
5380Sstevel@tonic-gate else
5390Sstevel@tonic-gate return (0);
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate #if DEBUG
5420Sstevel@tonic-gate printf("tcp_drain_input: done with processing packets\n");
5430Sstevel@tonic-gate #endif
5440Sstevel@tonic-gate sockets[sock_id].in_timeout = old_timeout;
5450Sstevel@tonic-gate sockets[sock_id].inq = old_in_gram;
5460Sstevel@tonic-gate
5470Sstevel@tonic-gate /*
5480Sstevel@tonic-gate * Data may have been received so indicate it is available
5490Sstevel@tonic-gate */
5500Sstevel@tonic-gate tcp_drain_needed(sock_id, tcp);
5510Sstevel@tonic-gate return (0);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate
5540Sstevel@tonic-gate /*
5550Sstevel@tonic-gate * The receive entry point for upper layer to call to get data. Note
5560Sstevel@tonic-gate * that this follows the current architecture that lower layer receive
5570Sstevel@tonic-gate * routines have been called already. Thus if the inq of socket is
5580Sstevel@tonic-gate * not NULL, the packets must be for us.
5590Sstevel@tonic-gate */
5600Sstevel@tonic-gate static int
tcp_input(int sock_id)5610Sstevel@tonic-gate tcp_input(int sock_id)
5620Sstevel@tonic-gate {
5630Sstevel@tonic-gate struct inetgram *in_gram;
5640Sstevel@tonic-gate mblk_t *mp;
5650Sstevel@tonic-gate tcp_t *tcp;
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL)
5700Sstevel@tonic-gate return (-1);
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
5730Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
5740Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
5750Sstevel@tonic-gate #ifdef DEBUG
5760Sstevel@tonic-gate printf("tcp_input: unexpected packet "
5770Sstevel@tonic-gate "level %d frame found\n", in_gram->igm_level);
5780Sstevel@tonic-gate #endif
5790Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
5800Sstevel@tonic-gate continue;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate mp = in_gram->igm_mp;
5830Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
5840Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
5850Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
5860Sstevel@tonic-gate /* The TCP may be gone because it gets a RST. */
5870Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
5880Sstevel@tonic-gate return (-1);
5890Sstevel@tonic-gate }
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate /* Flush the receive list. */
5920Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
5930Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
5940Sstevel@tonic-gate } else {
5950Sstevel@tonic-gate /* The other side has closed the connection, report this up. */
5960Sstevel@tonic-gate if (tcp->tcp_state == TCPS_CLOSE_WAIT) {
5970Sstevel@tonic-gate sockets[sock_id].so_state |= SS_CANTRCVMORE;
5980Sstevel@tonic-gate return (0);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate }
6010Sstevel@tonic-gate return (0);
6020Sstevel@tonic-gate }
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate /*
6050Sstevel@tonic-gate * The send entry point for upper layer to call to send data. In order
6060Sstevel@tonic-gate * to minimize changes to the core TCP code, we need to put the
6070Sstevel@tonic-gate * data into mblks.
6080Sstevel@tonic-gate */
6090Sstevel@tonic-gate int
tcp_send(int sock_id,tcp_t * tcp,const void * msg,int len)6100Sstevel@tonic-gate tcp_send(int sock_id, tcp_t *tcp, const void *msg, int len)
6110Sstevel@tonic-gate {
6120Sstevel@tonic-gate mblk_t *mp;
6130Sstevel@tonic-gate mblk_t *head = NULL;
6140Sstevel@tonic-gate mblk_t *tail;
6150Sstevel@tonic-gate int mss = tcp->tcp_mss;
6160Sstevel@tonic-gate int cnt = 0;
6170Sstevel@tonic-gate int win_size;
6180Sstevel@tonic-gate char *buf = (char *)msg;
6190Sstevel@tonic-gate
6200Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
6210Sstevel@tonic-gate
6220Sstevel@tonic-gate /* We don't want to append 0 size mblk. */
6230Sstevel@tonic-gate if (len == 0)
6240Sstevel@tonic-gate return (0);
6250Sstevel@tonic-gate while (len > 0) {
6260Sstevel@tonic-gate if (len < mss) {
6270Sstevel@tonic-gate mss = len;
6280Sstevel@tonic-gate }
6290Sstevel@tonic-gate /*
6300Sstevel@tonic-gate * If we cannot allocate more buffer, stop here and
6310Sstevel@tonic-gate * the number of bytes buffered will be returned.
6320Sstevel@tonic-gate *
6330Sstevel@tonic-gate * Note that we follow the core TCP optimization that
6340Sstevel@tonic-gate * each mblk contains only MSS bytes data.
6350Sstevel@tonic-gate */
6360Sstevel@tonic-gate if ((mp = allocb(mss + tcp->tcp_ip_hdr_len +
6370Sstevel@tonic-gate TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 0)) == NULL) {
6380Sstevel@tonic-gate break;
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate mp->b_rptr += tcp->tcp_hdr_len + tcp_wroff_xtra;
6410Sstevel@tonic-gate bcopy(buf, mp->b_rptr, mss);
6420Sstevel@tonic-gate mp->b_wptr = mp->b_rptr + mss;
6430Sstevel@tonic-gate buf += mss;
6440Sstevel@tonic-gate cnt += mss;
6450Sstevel@tonic-gate len -= mss;
6460Sstevel@tonic-gate
6470Sstevel@tonic-gate if (head == NULL) {
6480Sstevel@tonic-gate head = mp;
6490Sstevel@tonic-gate tail = mp;
6500Sstevel@tonic-gate } else {
6510Sstevel@tonic-gate tail->b_cont = mp;
6520Sstevel@tonic-gate tail = mp;
6530Sstevel@tonic-gate }
6540Sstevel@tonic-gate }
6550Sstevel@tonic-gate
6560Sstevel@tonic-gate /*
6570Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
6580Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
6590Sstevel@tonic-gate * otherwise, we may not be able to send.
6600Sstevel@tonic-gate *
6610Sstevel@tonic-gate * We expect an ACK in two cases:
6620Sstevel@tonic-gate *
6630Sstevel@tonic-gate * 1) We have un-ACK'ed data.
6640Sstevel@tonic-gate *
6650Sstevel@tonic-gate * 2) All ACK's have been received and the sender's window has been
6660Sstevel@tonic-gate * closed. We need an ACK back to open the window so that we can
6670Sstevel@tonic-gate * send. In this case, call tcp_drain_input() if the window size is
6680Sstevel@tonic-gate * less than 2 * MSS.
6690Sstevel@tonic-gate */
6700Sstevel@tonic-gate
6710Sstevel@tonic-gate /* window size = MIN(swnd, cwnd) - unacked bytes */
6720Sstevel@tonic-gate win_size = (tcp->tcp_swnd > tcp->tcp_cwnd) ? tcp->tcp_cwnd :
6730Sstevel@tonic-gate tcp->tcp_swnd;
6740Sstevel@tonic-gate win_size -= tcp->tcp_snxt;
6750Sstevel@tonic-gate win_size += tcp->tcp_suna;
6760Sstevel@tonic-gate if (win_size < (2 * tcp->tcp_mss))
6770Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0)
6780Sstevel@tonic-gate return (-1);
6790Sstevel@tonic-gate
6800Sstevel@tonic-gate tcp_wput_data(tcp, head, sock_id);
6811230Sss146032 /*
6821230Sss146032 * errno should be reset here as it may be
6831230Sss146032 * set to ETIMEDOUT. This may be set by
6841230Sss146032 * the MAC driver in case it has timed out
6851230Sss146032 * waiting for ARP reply. Any segment which
6861230Sss146032 * was not transmitted because of ARP timeout
6871230Sss146032 * will be retransmitted by TCP.
6881230Sss146032 */
6891230Sss146032 if (errno == ETIMEDOUT)
6901230Sss146032 errno = 0;
6910Sstevel@tonic-gate return (cnt);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate /* Free up all TCP related stuff */
6950Sstevel@tonic-gate static void
tcp_free(tcp_t * tcp)6960Sstevel@tonic-gate tcp_free(tcp_t *tcp)
6970Sstevel@tonic-gate {
6980Sstevel@tonic-gate if (tcp->tcp_iphc != NULL) {
6990Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_iphc, tcp->tcp_iphc_len);
7000Sstevel@tonic-gate tcp->tcp_iphc = NULL;
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate if (tcp->tcp_xmit_head != NULL) {
7030Sstevel@tonic-gate freemsg(tcp->tcp_xmit_head);
7040Sstevel@tonic-gate tcp->tcp_xmit_head = NULL;
7050Sstevel@tonic-gate }
7060Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
7070Sstevel@tonic-gate freemsg(tcp->tcp_rcv_list);
7080Sstevel@tonic-gate tcp->tcp_rcv_list = NULL;
7090Sstevel@tonic-gate }
7100Sstevel@tonic-gate if (tcp->tcp_reass_head != NULL) {
7110Sstevel@tonic-gate freemsg(tcp->tcp_reass_head);
7120Sstevel@tonic-gate tcp->tcp_reass_head = NULL;
7130Sstevel@tonic-gate }
7140Sstevel@tonic-gate if (tcp->tcp_sack_info != NULL) {
7150Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_sack_info,
7160Sstevel@tonic-gate sizeof (tcp_sack_info_t));
7170Sstevel@tonic-gate tcp->tcp_sack_info = NULL;
7180Sstevel@tonic-gate }
7190Sstevel@tonic-gate }
7200Sstevel@tonic-gate
7210Sstevel@tonic-gate static void
tcp_close_detached(tcp_t * tcp)7220Sstevel@tonic-gate tcp_close_detached(tcp_t *tcp)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate if (tcp->tcp_listener != NULL)
7250Sstevel@tonic-gate tcp_eager_unlink(tcp);
7260Sstevel@tonic-gate tcp_free(tcp);
7270Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
7280Sstevel@tonic-gate }
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate /*
7310Sstevel@tonic-gate * If we are an eager connection hanging off a listener that hasn't
7320Sstevel@tonic-gate * formally accepted the connection yet, get off his list and blow off
7330Sstevel@tonic-gate * any data that we have accumulated.
7340Sstevel@tonic-gate */
7350Sstevel@tonic-gate static void
tcp_eager_unlink(tcp_t * tcp)7360Sstevel@tonic-gate tcp_eager_unlink(tcp_t *tcp)
7370Sstevel@tonic-gate {
7380Sstevel@tonic-gate tcp_t *listener = tcp->tcp_listener;
7390Sstevel@tonic-gate
7400Sstevel@tonic-gate assert(listener != NULL);
7410Sstevel@tonic-gate if (tcp->tcp_eager_next_q0 != NULL) {
7420Sstevel@tonic-gate assert(tcp->tcp_eager_prev_q0 != NULL);
7430Sstevel@tonic-gate
7440Sstevel@tonic-gate /* Remove the eager tcp from q0 */
7450Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
7460Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
7470Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
7480Sstevel@tonic-gate tcp->tcp_eager_next_q0;
7490Sstevel@tonic-gate listener->tcp_conn_req_cnt_q0--;
7500Sstevel@tonic-gate } else {
7510Sstevel@tonic-gate tcp_t **tcpp = &listener->tcp_eager_next_q;
7520Sstevel@tonic-gate tcp_t *prev = NULL;
7530Sstevel@tonic-gate
7540Sstevel@tonic-gate for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) {
7550Sstevel@tonic-gate if (tcpp[0] == tcp) {
7560Sstevel@tonic-gate if (listener->tcp_eager_last_q == tcp) {
7570Sstevel@tonic-gate /*
7580Sstevel@tonic-gate * If we are unlinking the last
7590Sstevel@tonic-gate * element on the list, adjust
7600Sstevel@tonic-gate * tail pointer. Set tail pointer
7610Sstevel@tonic-gate * to nil when list is empty.
7620Sstevel@tonic-gate */
7630Sstevel@tonic-gate assert(tcp->tcp_eager_next_q == NULL);
7640Sstevel@tonic-gate if (listener->tcp_eager_last_q ==
7650Sstevel@tonic-gate listener->tcp_eager_next_q) {
7660Sstevel@tonic-gate listener->tcp_eager_last_q =
7670Sstevel@tonic-gate NULL;
7680Sstevel@tonic-gate } else {
7690Sstevel@tonic-gate /*
7700Sstevel@tonic-gate * We won't get here if there
7710Sstevel@tonic-gate * is only one eager in the
7720Sstevel@tonic-gate * list.
7730Sstevel@tonic-gate */
7740Sstevel@tonic-gate assert(prev != NULL);
7750Sstevel@tonic-gate listener->tcp_eager_last_q =
7760Sstevel@tonic-gate prev;
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate tcpp[0] = tcp->tcp_eager_next_q;
7800Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
7810Sstevel@tonic-gate tcp->tcp_eager_last_q = NULL;
7820Sstevel@tonic-gate listener->tcp_conn_req_cnt_q--;
7830Sstevel@tonic-gate break;
7840Sstevel@tonic-gate }
7850Sstevel@tonic-gate prev = tcpp[0];
7860Sstevel@tonic-gate }
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate tcp->tcp_listener = NULL;
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate * Reset any eager connection hanging off this listener
7930Sstevel@tonic-gate * and then reclaim it's resources.
7940Sstevel@tonic-gate */
7950Sstevel@tonic-gate static void
tcp_eager_cleanup(tcp_t * listener,boolean_t q0_only,int sock_id)7960Sstevel@tonic-gate tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only, int sock_id)
7970Sstevel@tonic-gate {
7980Sstevel@tonic-gate tcp_t *eager;
7990Sstevel@tonic-gate
8000Sstevel@tonic-gate if (!q0_only) {
8010Sstevel@tonic-gate /* First cleanup q */
8020Sstevel@tonic-gate while ((eager = listener->tcp_eager_next_q) != NULL) {
8030Sstevel@tonic-gate assert(listener->tcp_eager_last_q != NULL);
8040Sstevel@tonic-gate tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
8050Sstevel@tonic-gate eager, NULL, eager->tcp_snxt, 0, TH_RST, 0,
8060Sstevel@tonic-gate sock_id);
8070Sstevel@tonic-gate tcp_close_detached(eager);
8080Sstevel@tonic-gate }
8090Sstevel@tonic-gate assert(listener->tcp_eager_last_q == NULL);
8100Sstevel@tonic-gate }
8110Sstevel@tonic-gate /* Then cleanup q0 */
8120Sstevel@tonic-gate while ((eager = listener->tcp_eager_next_q0) != listener) {
8130Sstevel@tonic-gate tcp_xmit_ctl("tcp_eager_cleanup, can't wait",
8140Sstevel@tonic-gate eager, NULL, eager->tcp_snxt, 0, TH_RST, 0, sock_id);
8150Sstevel@tonic-gate tcp_close_detached(eager);
8160Sstevel@tonic-gate }
8170Sstevel@tonic-gate }
8180Sstevel@tonic-gate
8190Sstevel@tonic-gate /*
8200Sstevel@tonic-gate * To handle the shutdown request. Called from shutdown()
8210Sstevel@tonic-gate */
8220Sstevel@tonic-gate int
tcp_shutdown(int sock_id)8230Sstevel@tonic-gate tcp_shutdown(int sock_id)
8240Sstevel@tonic-gate {
8250Sstevel@tonic-gate tcp_t *tcp;
8260Sstevel@tonic-gate
8270Sstevel@tonic-gate DEBUG_1("tcp_shutdown: sock_id %x\n", sock_id);
8280Sstevel@tonic-gate
8290Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL) {
8300Sstevel@tonic-gate return (-1);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
8350Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
8360Sstevel@tonic-gate * otherwise, we may not be able to send.
8370Sstevel@tonic-gate */
8380Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
8390Sstevel@tonic-gate /*
8400Sstevel@tonic-gate * If we return now without freeing TCP, there will be
8410Sstevel@tonic-gate * a memory leak.
8420Sstevel@tonic-gate */
8430Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL)
8440Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
8450Sstevel@tonic-gate return (-1);
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate
8480Sstevel@tonic-gate DEBUG_1("tcp_shutdown: tcp_state %x\n", tcp->tcp_state);
8490Sstevel@tonic-gate switch (tcp->tcp_state) {
8500Sstevel@tonic-gate
8510Sstevel@tonic-gate case TCPS_SYN_RCVD:
8520Sstevel@tonic-gate /*
8530Sstevel@tonic-gate * Shutdown during the connect 3-way handshake
8540Sstevel@tonic-gate */
8550Sstevel@tonic-gate case TCPS_ESTABLISHED:
8560Sstevel@tonic-gate /*
8570Sstevel@tonic-gate * Transmit the FIN
8580Sstevel@tonic-gate * wait for the FIN to be ACKed,
8590Sstevel@tonic-gate * then remain in FIN_WAIT_2
8600Sstevel@tonic-gate */
8610Sstevel@tonic-gate dprintf("tcp_shutdown: sending fin\n");
8620Sstevel@tonic-gate if (tcp_xmit_end(tcp, sock_id) == 0 &&
8630Sstevel@tonic-gate tcp_state_wait(sock_id, tcp, TCPS_FIN_WAIT_2) < 0) {
8640Sstevel@tonic-gate /* During the wait, TCP may be gone... */
8650Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
8660Sstevel@tonic-gate return (-1);
8670Sstevel@tonic-gate }
8680Sstevel@tonic-gate dprintf("tcp_shutdown: done\n");
8690Sstevel@tonic-gate break;
8700Sstevel@tonic-gate
8710Sstevel@tonic-gate default:
8720Sstevel@tonic-gate break;
8730Sstevel@tonic-gate
8740Sstevel@tonic-gate }
8750Sstevel@tonic-gate return (0);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate
8780Sstevel@tonic-gate /* To handle closing of the socket */
8790Sstevel@tonic-gate static int
tcp_close(int sock_id)8800Sstevel@tonic-gate tcp_close(int sock_id)
8810Sstevel@tonic-gate {
8820Sstevel@tonic-gate char *msg;
8830Sstevel@tonic-gate tcp_t *tcp;
8840Sstevel@tonic-gate int error = 0;
8850Sstevel@tonic-gate
8860Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL) {
8870Sstevel@tonic-gate return (-1);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate
8900Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
8910Sstevel@tonic-gate
8920Sstevel@tonic-gate /*
8930Sstevel@tonic-gate * Since inetboot is not interrupt driven, there may be
8940Sstevel@tonic-gate * some ACKs in the MAC's buffer. Drain them first,
8950Sstevel@tonic-gate * otherwise, we may not be able to send.
8960Sstevel@tonic-gate */
8970Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
8980Sstevel@tonic-gate /*
8990Sstevel@tonic-gate * If we return now without freeing TCP, there will be
9000Sstevel@tonic-gate * a memory leak.
9010Sstevel@tonic-gate */
9020Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL)
9030Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
9040Sstevel@tonic-gate return (-1);
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate
9070Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
9080Sstevel@tonic-gate /* Cleanup for listener */
9090Sstevel@tonic-gate tcp_eager_cleanup(tcp, 0, sock_id);
9100Sstevel@tonic-gate }
9110Sstevel@tonic-gate
9120Sstevel@tonic-gate msg = NULL;
9130Sstevel@tonic-gate switch (tcp->tcp_state) {
9140Sstevel@tonic-gate case TCPS_CLOSED:
9150Sstevel@tonic-gate case TCPS_IDLE:
9160Sstevel@tonic-gate case TCPS_BOUND:
9170Sstevel@tonic-gate case TCPS_LISTEN:
9180Sstevel@tonic-gate break;
9190Sstevel@tonic-gate case TCPS_SYN_SENT:
9200Sstevel@tonic-gate msg = "tcp_close, during connect";
9210Sstevel@tonic-gate break;
9220Sstevel@tonic-gate case TCPS_SYN_RCVD:
9230Sstevel@tonic-gate /*
9240Sstevel@tonic-gate * Close during the connect 3-way handshake
9250Sstevel@tonic-gate * but here there may or may not be pending data
9260Sstevel@tonic-gate * already on queue. Process almost same as in
9270Sstevel@tonic-gate * the ESTABLISHED state.
9280Sstevel@tonic-gate */
9290Sstevel@tonic-gate /* FALLTHRU */
9300Sstevel@tonic-gate default:
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate * If SO_LINGER has set a zero linger time, abort the
9330Sstevel@tonic-gate * connection with a reset.
9340Sstevel@tonic-gate */
9350Sstevel@tonic-gate if (tcp->tcp_linger && tcp->tcp_lingertime == 0) {
9360Sstevel@tonic-gate msg = "tcp_close, zero lingertime";
9370Sstevel@tonic-gate break;
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate * Abort connection if there is unread data queued.
9420Sstevel@tonic-gate */
9430Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL ||
9440Sstevel@tonic-gate tcp->tcp_reass_head != NULL) {
9450Sstevel@tonic-gate msg = "tcp_close, unread data";
9460Sstevel@tonic-gate break;
9470Sstevel@tonic-gate }
9480Sstevel@tonic-gate if (tcp->tcp_state <= TCPS_LISTEN)
9490Sstevel@tonic-gate break;
9500Sstevel@tonic-gate
9510Sstevel@tonic-gate /*
9520Sstevel@tonic-gate * Transmit the FIN before detaching the tcp_t.
9530Sstevel@tonic-gate * After tcp_detach returns this queue/perimeter
9540Sstevel@tonic-gate * no longer owns the tcp_t thus others can modify it.
9550Sstevel@tonic-gate * The TCP could be closed in tcp_state_wait called by
9560Sstevel@tonic-gate * tcp_wput_data called by tcp_xmit_end.
9570Sstevel@tonic-gate */
9580Sstevel@tonic-gate (void) tcp_xmit_end(tcp, sock_id);
9590Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
9600Sstevel@tonic-gate return (0);
9610Sstevel@tonic-gate
9620Sstevel@tonic-gate /*
9630Sstevel@tonic-gate * If lingering on close then wait until the fin is acked,
9640Sstevel@tonic-gate * the SO_LINGER time passes, or a reset is sent/received.
9650Sstevel@tonic-gate */
9660Sstevel@tonic-gate if (tcp->tcp_linger && tcp->tcp_lingertime > 0 &&
9670Sstevel@tonic-gate !(tcp->tcp_fin_acked) &&
9680Sstevel@tonic-gate tcp->tcp_state >= TCPS_ESTABLISHED) {
9690Sstevel@tonic-gate uint32_t stoptime; /* in ms */
9700Sstevel@tonic-gate
9710Sstevel@tonic-gate tcp->tcp_client_errno = 0;
9720Sstevel@tonic-gate stoptime = prom_gettime() +
9730Sstevel@tonic-gate (tcp->tcp_lingertime * 1000);
9740Sstevel@tonic-gate while (!(tcp->tcp_fin_acked) &&
9750Sstevel@tonic-gate tcp->tcp_state >= TCPS_ESTABLISHED &&
9760Sstevel@tonic-gate tcp->tcp_client_errno == 0 &&
9770Sstevel@tonic-gate ((int32_t)(stoptime - prom_gettime()) > 0)) {
9780Sstevel@tonic-gate if (tcp_drain_input(tcp, sock_id, 5) < 0) {
9790Sstevel@tonic-gate if (sockets[sock_id].pcb != NULL) {
9800Sstevel@tonic-gate tcp_clean_death(sock_id,
9810Sstevel@tonic-gate tcp, 0);
9820Sstevel@tonic-gate }
9830Sstevel@tonic-gate return (-1);
9840Sstevel@tonic-gate }
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate tcp->tcp_client_errno = 0;
9870Sstevel@tonic-gate }
9880Sstevel@tonic-gate if (tcp_state_wait(sock_id, tcp, TCPS_TIME_WAIT) < 0) {
9890Sstevel@tonic-gate /* During the wait, TCP may be gone... */
9900Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
9910Sstevel@tonic-gate return (0);
9920Sstevel@tonic-gate msg = "tcp_close, couldn't detach";
9930Sstevel@tonic-gate } else {
9940Sstevel@tonic-gate return (0);
9950Sstevel@tonic-gate }
9960Sstevel@tonic-gate break;
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate
9990Sstevel@tonic-gate /* Something went wrong... Send a RST and report the error */
10000Sstevel@tonic-gate if (msg != NULL) {
10010Sstevel@tonic-gate if (tcp->tcp_state == TCPS_ESTABLISHED ||
10020Sstevel@tonic-gate tcp->tcp_state == TCPS_CLOSE_WAIT)
10030Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpEstabResets);
10040Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_SENT ||
10050Sstevel@tonic-gate tcp->tcp_state == TCPS_SYN_RCVD)
10060Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpAttemptFails);
10070Sstevel@tonic-gate tcp_xmit_ctl(msg, tcp, NULL, tcp->tcp_snxt, 0, TH_RST, 0,
10080Sstevel@tonic-gate sock_id);
10090Sstevel@tonic-gate }
10100Sstevel@tonic-gate
10110Sstevel@tonic-gate tcp_free(tcp);
10120Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
10130Sstevel@tonic-gate sockets[sock_id].pcb = NULL;
10140Sstevel@tonic-gate return (error);
10150Sstevel@tonic-gate }
10160Sstevel@tonic-gate
10170Sstevel@tonic-gate /* To make an endpoint a listener. */
10180Sstevel@tonic-gate int
tcp_listen(int sock_id,int backlog)10190Sstevel@tonic-gate tcp_listen(int sock_id, int backlog)
10200Sstevel@tonic-gate {
10210Sstevel@tonic-gate tcp_t *tcp;
10220Sstevel@tonic-gate
10230Sstevel@tonic-gate if ((tcp = (tcp_t *)(sockets[sock_id].pcb)) == NULL) {
10240Sstevel@tonic-gate errno = EINVAL;
10250Sstevel@tonic-gate return (-1);
10260Sstevel@tonic-gate }
10270Sstevel@tonic-gate /* We allow calling listen() multiple times to change the backlog. */
10280Sstevel@tonic-gate if (tcp->tcp_state > TCPS_LISTEN || tcp->tcp_state < TCPS_BOUND) {
10290Sstevel@tonic-gate errno = EOPNOTSUPP;
10300Sstevel@tonic-gate return (-1);
10310Sstevel@tonic-gate }
10320Sstevel@tonic-gate /* The following initialization should only be done once. */
10330Sstevel@tonic-gate if (tcp->tcp_state != TCPS_LISTEN) {
10340Sstevel@tonic-gate tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
10350Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
10360Sstevel@tonic-gate tcp->tcp_state = TCPS_LISTEN;
10370Sstevel@tonic-gate tcp->tcp_second_ctimer_threshold = tcp_ip_abort_linterval;
10380Sstevel@tonic-gate }
10390Sstevel@tonic-gate if ((tcp->tcp_conn_req_max = backlog) > tcp_conn_req_max_q) {
10400Sstevel@tonic-gate tcp->tcp_conn_req_max = tcp_conn_req_max_q;
10410Sstevel@tonic-gate }
10420Sstevel@tonic-gate if (tcp->tcp_conn_req_max < tcp_conn_req_min) {
10430Sstevel@tonic-gate tcp->tcp_conn_req_max = tcp_conn_req_min;
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate return (0);
10460Sstevel@tonic-gate }
10470Sstevel@tonic-gate
10480Sstevel@tonic-gate /* To accept connections. */
10490Sstevel@tonic-gate int
tcp_accept(int sock_id,struct sockaddr * addr,socklen_t * addr_len)10500Sstevel@tonic-gate tcp_accept(int sock_id, struct sockaddr *addr, socklen_t *addr_len)
10510Sstevel@tonic-gate {
10520Sstevel@tonic-gate tcp_t *listener;
10530Sstevel@tonic-gate tcp_t *eager;
10540Sstevel@tonic-gate int sd, new_sock_id;
10550Sstevel@tonic-gate struct sockaddr_in *new_addr = (struct sockaddr_in *)addr;
10560Sstevel@tonic-gate int timeout;
10570Sstevel@tonic-gate
10580Sstevel@tonic-gate /* Sanity check. */
10590Sstevel@tonic-gate if ((listener = (tcp_t *)(sockets[sock_id].pcb)) == NULL ||
10600Sstevel@tonic-gate new_addr == NULL || addr_len == NULL ||
10610Sstevel@tonic-gate *addr_len < sizeof (struct sockaddr_in) ||
10620Sstevel@tonic-gate listener->tcp_state != TCPS_LISTEN) {
10630Sstevel@tonic-gate errno = EINVAL;
10640Sstevel@tonic-gate return (-1);
10650Sstevel@tonic-gate }
10660Sstevel@tonic-gate
10670Sstevel@tonic-gate if (sockets[sock_id].in_timeout > tcp_accept_timeout)
10680Sstevel@tonic-gate timeout = prom_gettime() + sockets[sock_id].in_timeout;
10690Sstevel@tonic-gate else
10700Sstevel@tonic-gate timeout = prom_gettime() + tcp_accept_timeout;
10710Sstevel@tonic-gate while (listener->tcp_eager_next_q == NULL &&
10720Sstevel@tonic-gate timeout > prom_gettime()) {
10730Sstevel@tonic-gate #if DEBUG
10740Sstevel@tonic-gate printf("tcp_accept: Waiting in tcp_accept()\n");
10750Sstevel@tonic-gate #endif
10760Sstevel@tonic-gate if (tcp_drain_input(listener, sock_id, 5) < 0) {
10770Sstevel@tonic-gate return (-1);
10780Sstevel@tonic-gate }
10790Sstevel@tonic-gate }
10800Sstevel@tonic-gate /* If there is an eager, don't timeout... */
10810Sstevel@tonic-gate if (timeout <= prom_gettime() && listener->tcp_eager_next_q == NULL) {
10820Sstevel@tonic-gate #if DEBUG
10830Sstevel@tonic-gate printf("tcp_accept: timeout\n");
10840Sstevel@tonic-gate #endif
10850Sstevel@tonic-gate errno = ETIMEDOUT;
10860Sstevel@tonic-gate return (-1);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate #if DEBUG
10890Sstevel@tonic-gate printf("tcp_accept: got a connection\n");
10900Sstevel@tonic-gate #endif
10910Sstevel@tonic-gate
10920Sstevel@tonic-gate /* Now create the socket for this new TCP. */
10930Sstevel@tonic-gate if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
10940Sstevel@tonic-gate return (-1);
10950Sstevel@tonic-gate }
10960Sstevel@tonic-gate if ((new_sock_id = so_check_fd(sd, &errno)) == -1)
10970Sstevel@tonic-gate /* This should not happen! */
10980Sstevel@tonic-gate prom_panic("so_check_fd() fails in tcp_accept()");
10990Sstevel@tonic-gate /* Free the TCP PCB in the original socket. */
11000Sstevel@tonic-gate bkmem_free((caddr_t)(sockets[new_sock_id].pcb), sizeof (tcp_t));
11010Sstevel@tonic-gate /* Dequeue the eager and attach it to the socket. */
11020Sstevel@tonic-gate eager = listener->tcp_eager_next_q;
11030Sstevel@tonic-gate listener->tcp_eager_next_q = eager->tcp_eager_next_q;
11040Sstevel@tonic-gate if (listener->tcp_eager_last_q == eager)
11050Sstevel@tonic-gate listener->tcp_eager_last_q = NULL;
11060Sstevel@tonic-gate eager->tcp_eager_next_q = NULL;
11070Sstevel@tonic-gate sockets[new_sock_id].pcb = eager;
11080Sstevel@tonic-gate listener->tcp_conn_req_cnt_q--;
11090Sstevel@tonic-gate
11100Sstevel@tonic-gate /* Copy in the address info. */
11110Sstevel@tonic-gate bcopy(&eager->tcp_remote, &new_addr->sin_addr.s_addr,
11120Sstevel@tonic-gate sizeof (in_addr_t));
11130Sstevel@tonic-gate bcopy(&eager->tcp_fport, &new_addr->sin_port, sizeof (in_port_t));
11140Sstevel@tonic-gate new_addr->sin_family = AF_INET;
11150Sstevel@tonic-gate
11160Sstevel@tonic-gate #ifdef DEBUG
11170Sstevel@tonic-gate printf("tcp_accept(), new sock_id: %d\n", sd);
11180Sstevel@tonic-gate #endif
11190Sstevel@tonic-gate return (sd);
11200Sstevel@tonic-gate }
11210Sstevel@tonic-gate
11220Sstevel@tonic-gate /* Update the next anonymous port to use. */
11230Sstevel@tonic-gate static in_port_t
tcp_update_next_port(in_port_t port)11240Sstevel@tonic-gate tcp_update_next_port(in_port_t port)
11250Sstevel@tonic-gate {
11260Sstevel@tonic-gate /* Don't allow the port to fall out of the anonymous port range. */
11270Sstevel@tonic-gate if (port < tcp_smallest_anon_port || port > tcp_largest_anon_port)
11280Sstevel@tonic-gate port = (in_port_t)tcp_smallest_anon_port;
11290Sstevel@tonic-gate
11300Sstevel@tonic-gate if (port < tcp_smallest_nonpriv_port)
11310Sstevel@tonic-gate port = (in_port_t)tcp_smallest_nonpriv_port;
11320Sstevel@tonic-gate return (port);
11330Sstevel@tonic-gate }
11340Sstevel@tonic-gate
11350Sstevel@tonic-gate /* To check whether a bind to a port is allowed. */
11360Sstevel@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)11370Sstevel@tonic-gate tcp_bindi(in_port_t port, in_addr_t *addr, boolean_t reuseaddr,
11380Sstevel@tonic-gate boolean_t bind_to_req_port_only)
11390Sstevel@tonic-gate {
11400Sstevel@tonic-gate int i, count;
11410Sstevel@tonic-gate tcp_t *tcp;
11420Sstevel@tonic-gate
11430Sstevel@tonic-gate count = tcp_largest_anon_port - tcp_smallest_anon_port;
11440Sstevel@tonic-gate try_again:
11450Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
11460Sstevel@tonic-gate if (sockets[i].type != INETBOOT_STREAM ||
11470Sstevel@tonic-gate ((tcp = (tcp_t *)sockets[i].pcb) == NULL) ||
11480Sstevel@tonic-gate ntohs(tcp->tcp_lport) != port) {
11490Sstevel@tonic-gate continue;
11500Sstevel@tonic-gate }
11510Sstevel@tonic-gate /*
11520Sstevel@tonic-gate * Both TCPs have the same port. If SO_REUSEDADDR is
11530Sstevel@tonic-gate * set and the bound TCP has a state greater than
11540Sstevel@tonic-gate * TCPS_LISTEN, it is fine.
11550Sstevel@tonic-gate */
11560Sstevel@tonic-gate if (reuseaddr && tcp->tcp_state > TCPS_LISTEN) {
11570Sstevel@tonic-gate continue;
11580Sstevel@tonic-gate }
11590Sstevel@tonic-gate if (tcp->tcp_bound_source != INADDR_ANY &&
11600Sstevel@tonic-gate *addr != INADDR_ANY &&
11610Sstevel@tonic-gate tcp->tcp_bound_source != *addr) {
11620Sstevel@tonic-gate continue;
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate if (bind_to_req_port_only) {
11650Sstevel@tonic-gate return (0);
11660Sstevel@tonic-gate }
11670Sstevel@tonic-gate if (--count > 0) {
11680Sstevel@tonic-gate port = tcp_update_next_port(++port);
11690Sstevel@tonic-gate goto try_again;
11700Sstevel@tonic-gate } else {
11710Sstevel@tonic-gate return (0);
11720Sstevel@tonic-gate }
11730Sstevel@tonic-gate }
11740Sstevel@tonic-gate return (port);
11750Sstevel@tonic-gate }
11760Sstevel@tonic-gate
11770Sstevel@tonic-gate /* To handle the bind request. */
11780Sstevel@tonic-gate int
tcp_bind(int sock_id)11790Sstevel@tonic-gate tcp_bind(int sock_id)
11800Sstevel@tonic-gate {
11810Sstevel@tonic-gate tcp_t *tcp;
11820Sstevel@tonic-gate in_port_t requested_port, allocated_port;
11830Sstevel@tonic-gate boolean_t bind_to_req_port_only;
11840Sstevel@tonic-gate boolean_t reuseaddr;
11850Sstevel@tonic-gate
11860Sstevel@tonic-gate if ((tcp = (tcp_t *)sockets[sock_id].pcb) == NULL) {
11870Sstevel@tonic-gate errno = EINVAL;
11880Sstevel@tonic-gate return (-1);
11890Sstevel@tonic-gate }
11900Sstevel@tonic-gate
11910Sstevel@tonic-gate if (tcp->tcp_state >= TCPS_BOUND) {
11920Sstevel@tonic-gate /* We don't allow multiple bind(). */
11930Sstevel@tonic-gate errno = EPROTO;
11940Sstevel@tonic-gate return (-1);
11950Sstevel@tonic-gate }
11960Sstevel@tonic-gate
11970Sstevel@tonic-gate requested_port = ntohs(sockets[sock_id].bind.sin_port);
11980Sstevel@tonic-gate
11990Sstevel@tonic-gate /* The bound source can be INADDR_ANY. */
12000Sstevel@tonic-gate tcp->tcp_bound_source = sockets[sock_id].bind.sin_addr.s_addr;
12010Sstevel@tonic-gate
12020Sstevel@tonic-gate tcp->tcp_ipha->ip_src.s_addr = tcp->tcp_bound_source;
12030Sstevel@tonic-gate
12040Sstevel@tonic-gate /* Verify the port is available. */
12050Sstevel@tonic-gate if (requested_port == 0)
12060Sstevel@tonic-gate bind_to_req_port_only = B_FALSE;
12070Sstevel@tonic-gate else /* T_BIND_REQ and requested_port != 0 */
12080Sstevel@tonic-gate bind_to_req_port_only = B_TRUE;
12090Sstevel@tonic-gate
12100Sstevel@tonic-gate if (requested_port == 0) {
12110Sstevel@tonic-gate requested_port = tcp_update_next_port(++tcp_next_port_to_try);
12120Sstevel@tonic-gate }
12130Sstevel@tonic-gate reuseaddr = sockets[sock_id].so_opt & SO_REUSEADDR;
12140Sstevel@tonic-gate allocated_port = tcp_bindi(requested_port, &(tcp->tcp_bound_source),
12150Sstevel@tonic-gate reuseaddr, bind_to_req_port_only);
12160Sstevel@tonic-gate
12170Sstevel@tonic-gate if (allocated_port == 0) {
12180Sstevel@tonic-gate errno = EADDRINUSE;
12190Sstevel@tonic-gate return (-1);
12200Sstevel@tonic-gate }
12210Sstevel@tonic-gate tcp->tcp_lport = htons(allocated_port);
12220Sstevel@tonic-gate *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport;
12230Sstevel@tonic-gate sockets[sock_id].bind.sin_port = tcp->tcp_lport;
12240Sstevel@tonic-gate tcp->tcp_state = TCPS_BOUND;
12250Sstevel@tonic-gate return (0);
12260Sstevel@tonic-gate }
12270Sstevel@tonic-gate
12280Sstevel@tonic-gate /*
12290Sstevel@tonic-gate * Check for duplicate TCP connections.
12300Sstevel@tonic-gate */
12310Sstevel@tonic-gate static int
tcp_conn_check(tcp_t * tcp)12320Sstevel@tonic-gate tcp_conn_check(tcp_t *tcp)
12330Sstevel@tonic-gate {
12340Sstevel@tonic-gate int i;
12350Sstevel@tonic-gate tcp_t *tmp_tcp;
12360Sstevel@tonic-gate
12370Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
12380Sstevel@tonic-gate if (sockets[i].type != INETBOOT_STREAM)
12390Sstevel@tonic-gate continue;
12400Sstevel@tonic-gate /* Socket may not be closed but the TCP can be gone. */
12410Sstevel@tonic-gate if ((tmp_tcp = (tcp_t *)sockets[i].pcb) == NULL)
12420Sstevel@tonic-gate continue;
12430Sstevel@tonic-gate /* We only care about TCP in states later than SYN_SENT. */
12440Sstevel@tonic-gate if (tmp_tcp->tcp_state < TCPS_SYN_SENT)
12450Sstevel@tonic-gate continue;
12460Sstevel@tonic-gate if (tmp_tcp->tcp_lport != tcp->tcp_lport ||
12470Sstevel@tonic-gate tmp_tcp->tcp_fport != tcp->tcp_fport ||
12480Sstevel@tonic-gate tmp_tcp->tcp_bound_source != tcp->tcp_bound_source ||
12490Sstevel@tonic-gate tmp_tcp->tcp_remote != tcp->tcp_remote) {
12500Sstevel@tonic-gate continue;
12510Sstevel@tonic-gate } else {
12520Sstevel@tonic-gate return (-1);
12530Sstevel@tonic-gate }
12540Sstevel@tonic-gate }
12550Sstevel@tonic-gate return (0);
12560Sstevel@tonic-gate }
12570Sstevel@tonic-gate
12580Sstevel@tonic-gate /* To handle a connect request. */
12590Sstevel@tonic-gate int
tcp_connect(int sock_id)12600Sstevel@tonic-gate tcp_connect(int sock_id)
12610Sstevel@tonic-gate {
12620Sstevel@tonic-gate tcp_t *tcp;
12630Sstevel@tonic-gate in_addr_t dstaddr;
12640Sstevel@tonic-gate in_port_t dstport;
12650Sstevel@tonic-gate tcph_t *tcph;
12660Sstevel@tonic-gate int mss;
12670Sstevel@tonic-gate mblk_t *syn_mp;
12680Sstevel@tonic-gate
12690Sstevel@tonic-gate if ((tcp = (tcp_t *)(sockets[sock_id].pcb)) == NULL) {
12700Sstevel@tonic-gate errno = EINVAL;
12710Sstevel@tonic-gate return (-1);
12720Sstevel@tonic-gate }
12730Sstevel@tonic-gate
12740Sstevel@tonic-gate TCP_RUN_TIME_WAIT_COLLECTOR();
12750Sstevel@tonic-gate
12760Sstevel@tonic-gate dstaddr = sockets[sock_id].remote.sin_addr.s_addr;
12770Sstevel@tonic-gate dstport = sockets[sock_id].remote.sin_port;
12780Sstevel@tonic-gate
12790Sstevel@tonic-gate /*
12800Sstevel@tonic-gate * Check for attempt to connect to INADDR_ANY or non-unicast addrress.
12810Sstevel@tonic-gate * We don't have enough info to check for broadcast addr, except
12820Sstevel@tonic-gate * for the all 1 broadcast.
12830Sstevel@tonic-gate */
12840Sstevel@tonic-gate if (dstaddr == INADDR_ANY || IN_CLASSD(ntohl(dstaddr)) ||
12850Sstevel@tonic-gate dstaddr == INADDR_BROADCAST) {
12860Sstevel@tonic-gate /*
12870Sstevel@tonic-gate * SunOS 4.x and 4.3 BSD allow an application
12880Sstevel@tonic-gate * to connect a TCP socket to INADDR_ANY.
12890Sstevel@tonic-gate * When they do this, the kernel picks the
12900Sstevel@tonic-gate * address of one interface and uses it
12910Sstevel@tonic-gate * instead. The kernel usually ends up
12920Sstevel@tonic-gate * picking the address of the loopback
12930Sstevel@tonic-gate * interface. This is an undocumented feature.
12940Sstevel@tonic-gate * However, we provide the same thing here
12950Sstevel@tonic-gate * in order to have source and binary
12960Sstevel@tonic-gate * compatibility with SunOS 4.x.
12970Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to
12980Sstevel@tonic-gate * generate the T_CONN_CON.
12990Sstevel@tonic-gate *
13000Sstevel@tonic-gate * Fail this for inetboot TCP.
13010Sstevel@tonic-gate */
13020Sstevel@tonic-gate errno = EINVAL;
13030Sstevel@tonic-gate return (-1);
13040Sstevel@tonic-gate }
13050Sstevel@tonic-gate
13060Sstevel@tonic-gate /* It is not bound to any address yet... */
13070Sstevel@tonic-gate if (tcp->tcp_bound_source == INADDR_ANY) {
13080Sstevel@tonic-gate ipv4_getipaddr(&(sockets[sock_id].bind.sin_addr));
13090Sstevel@tonic-gate /* We don't have an address! */
13100Sstevel@tonic-gate if (ntohl(sockets[sock_id].bind.sin_addr.s_addr) ==
13110Sstevel@tonic-gate INADDR_ANY) {
13120Sstevel@tonic-gate errno = EPROTO;
13130Sstevel@tonic-gate return (-1);
13140Sstevel@tonic-gate }
13150Sstevel@tonic-gate tcp->tcp_bound_source = sockets[sock_id].bind.sin_addr.s_addr;
13160Sstevel@tonic-gate tcp->tcp_ipha->ip_src.s_addr = tcp->tcp_bound_source;
13170Sstevel@tonic-gate }
13180Sstevel@tonic-gate
13190Sstevel@tonic-gate /*
13200Sstevel@tonic-gate * Don't let an endpoint connect to itself.
13210Sstevel@tonic-gate */
13220Sstevel@tonic-gate if (dstaddr == tcp->tcp_ipha->ip_src.s_addr &&
13230Sstevel@tonic-gate dstport == tcp->tcp_lport) {
13240Sstevel@tonic-gate errno = EINVAL;
13250Sstevel@tonic-gate return (-1);
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate
13280Sstevel@tonic-gate tcp->tcp_ipha->ip_dst.s_addr = dstaddr;
13290Sstevel@tonic-gate tcp->tcp_remote = dstaddr;
13300Sstevel@tonic-gate tcph = tcp->tcp_tcph;
13310Sstevel@tonic-gate *(uint16_t *)tcph->th_fport = dstport;
13320Sstevel@tonic-gate tcp->tcp_fport = dstport;
13330Sstevel@tonic-gate
13340Sstevel@tonic-gate /*
13350Sstevel@tonic-gate * Don't allow this connection to completely duplicate
13360Sstevel@tonic-gate * an existing connection.
13370Sstevel@tonic-gate */
13380Sstevel@tonic-gate if (tcp_conn_check(tcp) < 0) {
13390Sstevel@tonic-gate errno = EADDRINUSE;
13400Sstevel@tonic-gate return (-1);
13410Sstevel@tonic-gate }
13420Sstevel@tonic-gate
13430Sstevel@tonic-gate /*
13440Sstevel@tonic-gate * Just make sure our rwnd is at
13450Sstevel@tonic-gate * least tcp_recv_hiwat_mss * MSS
13460Sstevel@tonic-gate * large, and round up to the nearest
13470Sstevel@tonic-gate * MSS.
13480Sstevel@tonic-gate *
13490Sstevel@tonic-gate * We do the round up here because
13500Sstevel@tonic-gate * we need to get the interface
13510Sstevel@tonic-gate * MTU first before we can do the
13520Sstevel@tonic-gate * round up.
13530Sstevel@tonic-gate */
13540Sstevel@tonic-gate mss = tcp->tcp_mss - tcp->tcp_hdr_len;
13550Sstevel@tonic-gate tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
13560Sstevel@tonic-gate tcp_recv_hiwat_minmss * mss);
13570Sstevel@tonic-gate tcp->tcp_rwnd_max = tcp->tcp_rwnd;
13580Sstevel@tonic-gate SET_WS_VALUE(tcp);
13590Sstevel@tonic-gate U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws),
13600Sstevel@tonic-gate tcp->tcp_tcph->th_win);
13610Sstevel@tonic-gate if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always)
13620Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_TRUE;
13630Sstevel@tonic-gate
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate * Set tcp_snd_ts_ok to true
13660Sstevel@tonic-gate * so that tcp_xmit_mp will
13670Sstevel@tonic-gate * include the timestamp
13680Sstevel@tonic-gate * option in the SYN segment.
13690Sstevel@tonic-gate */
13700Sstevel@tonic-gate if (tcp_tstamp_always ||
13710Sstevel@tonic-gate (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) {
13720Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_TRUE;
13730Sstevel@tonic-gate }
13740Sstevel@tonic-gate
13750Sstevel@tonic-gate if (tcp_sack_permitted == 2 ||
13760Sstevel@tonic-gate tcp->tcp_snd_sack_ok) {
13770Sstevel@tonic-gate assert(tcp->tcp_sack_info == NULL);
13780Sstevel@tonic-gate if ((tcp->tcp_sack_info = (tcp_sack_info_t *)bkmem_zalloc(
13790Sstevel@tonic-gate sizeof (tcp_sack_info_t))) == NULL) {
13800Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
13810Sstevel@tonic-gate } else {
13820Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
13830Sstevel@tonic-gate }
13840Sstevel@tonic-gate }
13850Sstevel@tonic-gate /*
13860Sstevel@tonic-gate * Should we use ECN? Note that the current
13870Sstevel@tonic-gate * default value (SunOS 5.9) of tcp_ecn_permitted
13880Sstevel@tonic-gate * is 2. The reason for doing this is that there
13890Sstevel@tonic-gate * are equipments out there that will drop ECN
13900Sstevel@tonic-gate * enabled IP packets. Setting it to 1 avoids
13910Sstevel@tonic-gate * compatibility problems.
13920Sstevel@tonic-gate */
13930Sstevel@tonic-gate if (tcp_ecn_permitted == 2)
13940Sstevel@tonic-gate tcp->tcp_ecn_ok = B_TRUE;
13950Sstevel@tonic-gate
13960Sstevel@tonic-gate tcp_iss_init(tcp);
13970Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
13980Sstevel@tonic-gate tcp->tcp_active_open = B_TRUE;
13990Sstevel@tonic-gate
14000Sstevel@tonic-gate tcp->tcp_state = TCPS_SYN_SENT;
14010Sstevel@tonic-gate syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, tcp->tcp_iss, B_FALSE,
14020Sstevel@tonic-gate NULL, B_FALSE);
14030Sstevel@tonic-gate if (syn_mp != NULL) {
14040Sstevel@tonic-gate int ret;
14050Sstevel@tonic-gate
14060Sstevel@tonic-gate /* Dump the packet when debugging. */
14070Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_connect", syn_mp);
14080Sstevel@tonic-gate /* Send out the SYN packet. */
14090Sstevel@tonic-gate ret = ipv4_tcp_output(sock_id, syn_mp);
14100Sstevel@tonic-gate freeb(syn_mp);
14111230Sss146032 /*
14121230Sss146032 * errno ETIMEDOUT is set by the mac driver
14131230Sss146032 * in case it is not able to receive ARP reply.
14141230Sss146032 * TCP will retransmit this segment so we can
14151230Sss146032 * ignore the ARP timeout.
14161230Sss146032 */
14171230Sss146032 if ((ret < 0) && (errno != ETIMEDOUT)) {
14180Sstevel@tonic-gate return (-1);
14190Sstevel@tonic-gate }
14200Sstevel@tonic-gate /* tcp_state_wait() will finish the 3 way handshake. */
14210Sstevel@tonic-gate return (tcp_state_wait(sock_id, tcp, TCPS_ESTABLISHED));
14220Sstevel@tonic-gate } else {
14230Sstevel@tonic-gate errno = ENOBUFS;
14240Sstevel@tonic-gate return (-1);
14250Sstevel@tonic-gate }
14260Sstevel@tonic-gate }
14270Sstevel@tonic-gate
14280Sstevel@tonic-gate /*
14290Sstevel@tonic-gate * Common accept code. Called by tcp_conn_request.
14300Sstevel@tonic-gate * cr_pkt is the SYN packet.
14310Sstevel@tonic-gate */
14320Sstevel@tonic-gate static int
tcp_accept_comm(tcp_t * listener,tcp_t * acceptor,mblk_t * cr_pkt,uint_t ip_hdr_len)14330Sstevel@tonic-gate tcp_accept_comm(tcp_t *listener, tcp_t *acceptor, mblk_t *cr_pkt,
14340Sstevel@tonic-gate uint_t ip_hdr_len)
14350Sstevel@tonic-gate {
14360Sstevel@tonic-gate tcph_t *tcph;
14370Sstevel@tonic-gate
14380Sstevel@tonic-gate #ifdef DEBUG
14390Sstevel@tonic-gate printf("tcp_accept_comm #######################\n");
14400Sstevel@tonic-gate #endif
14410Sstevel@tonic-gate
14420Sstevel@tonic-gate /*
14430Sstevel@tonic-gate * When we get here, we know that the acceptor header template
14440Sstevel@tonic-gate * has already been initialized.
14450Sstevel@tonic-gate * However, it may not match the listener if the listener
14460Sstevel@tonic-gate * includes options...
14470Sstevel@tonic-gate * It may also not match the listener if the listener is v6 and
14480Sstevel@tonic-gate * and the acceptor is v4
14490Sstevel@tonic-gate */
14500Sstevel@tonic-gate acceptor->tcp_lport = listener->tcp_lport;
14510Sstevel@tonic-gate
14520Sstevel@tonic-gate if (listener->tcp_ipversion == acceptor->tcp_ipversion) {
14530Sstevel@tonic-gate if (acceptor->tcp_iphc_len != listener->tcp_iphc_len) {
14540Sstevel@tonic-gate /*
14550Sstevel@tonic-gate * Listener had options of some sort; acceptor inherits.
14560Sstevel@tonic-gate * Free up the acceptor template and allocate one
14570Sstevel@tonic-gate * of the right size.
14580Sstevel@tonic-gate */
14590Sstevel@tonic-gate bkmem_free(acceptor->tcp_iphc, acceptor->tcp_iphc_len);
14600Sstevel@tonic-gate acceptor->tcp_iphc = bkmem_zalloc(
14610Sstevel@tonic-gate listener->tcp_iphc_len);
14620Sstevel@tonic-gate if (acceptor->tcp_iphc == NULL) {
14630Sstevel@tonic-gate acceptor->tcp_iphc_len = 0;
14640Sstevel@tonic-gate return (ENOMEM);
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate acceptor->tcp_iphc_len = listener->tcp_iphc_len;
14670Sstevel@tonic-gate }
14680Sstevel@tonic-gate acceptor->tcp_hdr_len = listener->tcp_hdr_len;
14690Sstevel@tonic-gate acceptor->tcp_ip_hdr_len = listener->tcp_ip_hdr_len;
14700Sstevel@tonic-gate acceptor->tcp_tcp_hdr_len = listener->tcp_tcp_hdr_len;
14710Sstevel@tonic-gate
14720Sstevel@tonic-gate /*
14730Sstevel@tonic-gate * Copy the IP+TCP header template from listener to acceptor
14740Sstevel@tonic-gate */
14750Sstevel@tonic-gate bcopy(listener->tcp_iphc, acceptor->tcp_iphc,
14760Sstevel@tonic-gate listener->tcp_hdr_len);
14770Sstevel@tonic-gate acceptor->tcp_ipha = (struct ip *)acceptor->tcp_iphc;
14780Sstevel@tonic-gate acceptor->tcp_tcph = (tcph_t *)(acceptor->tcp_iphc +
14790Sstevel@tonic-gate acceptor->tcp_ip_hdr_len);
14800Sstevel@tonic-gate } else {
14810Sstevel@tonic-gate prom_panic("tcp_accept_comm: version not equal");
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate
14840Sstevel@tonic-gate /* Copy our new dest and fport from the connection request packet */
14850Sstevel@tonic-gate if (acceptor->tcp_ipversion == IPV4_VERSION) {
14860Sstevel@tonic-gate struct ip *ipha;
14870Sstevel@tonic-gate
14880Sstevel@tonic-gate ipha = (struct ip *)cr_pkt->b_rptr;
14890Sstevel@tonic-gate acceptor->tcp_ipha->ip_dst = ipha->ip_src;
14900Sstevel@tonic-gate acceptor->tcp_remote = ipha->ip_src.s_addr;
14910Sstevel@tonic-gate acceptor->tcp_ipha->ip_src = ipha->ip_dst;
14920Sstevel@tonic-gate acceptor->tcp_bound_source = ipha->ip_dst.s_addr;
14930Sstevel@tonic-gate tcph = (tcph_t *)&cr_pkt->b_rptr[ip_hdr_len];
14940Sstevel@tonic-gate } else {
14950Sstevel@tonic-gate prom_panic("tcp_accept_comm: not IPv4");
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate bcopy(tcph->th_lport, acceptor->tcp_tcph->th_fport, sizeof (in_port_t));
14980Sstevel@tonic-gate bcopy(acceptor->tcp_tcph->th_fport, &acceptor->tcp_fport,
14990Sstevel@tonic-gate sizeof (in_port_t));
15000Sstevel@tonic-gate /*
15010Sstevel@tonic-gate * For an all-port proxy listener, the local port is determined by
15020Sstevel@tonic-gate * the port number field in the SYN packet.
15030Sstevel@tonic-gate */
15040Sstevel@tonic-gate if (listener->tcp_lport == 0) {
15050Sstevel@tonic-gate acceptor->tcp_lport = *(in_port_t *)tcph->th_fport;
15060Sstevel@tonic-gate bcopy(tcph->th_fport, acceptor->tcp_tcph->th_lport,
15070Sstevel@tonic-gate sizeof (in_port_t));
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate /* Inherit various TCP parameters from the listener */
15100Sstevel@tonic-gate acceptor->tcp_naglim = listener->tcp_naglim;
15110Sstevel@tonic-gate acceptor->tcp_first_timer_threshold =
15120Sstevel@tonic-gate listener->tcp_first_timer_threshold;
15130Sstevel@tonic-gate acceptor->tcp_second_timer_threshold =
15140Sstevel@tonic-gate listener->tcp_second_timer_threshold;
15150Sstevel@tonic-gate
15160Sstevel@tonic-gate acceptor->tcp_first_ctimer_threshold =
15170Sstevel@tonic-gate listener->tcp_first_ctimer_threshold;
15180Sstevel@tonic-gate acceptor->tcp_second_ctimer_threshold =
15190Sstevel@tonic-gate listener->tcp_second_ctimer_threshold;
15200Sstevel@tonic-gate
15210Sstevel@tonic-gate acceptor->tcp_xmit_hiwater = listener->tcp_xmit_hiwater;
15220Sstevel@tonic-gate
15230Sstevel@tonic-gate acceptor->tcp_state = TCPS_LISTEN;
15240Sstevel@tonic-gate tcp_iss_init(acceptor);
15250Sstevel@tonic-gate
15260Sstevel@tonic-gate /* Process all TCP options. */
15270Sstevel@tonic-gate tcp_process_options(acceptor, tcph);
15280Sstevel@tonic-gate
15290Sstevel@tonic-gate /* Is the other end ECN capable? */
15300Sstevel@tonic-gate if (tcp_ecn_permitted >= 1 &&
15310Sstevel@tonic-gate (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) {
15320Sstevel@tonic-gate acceptor->tcp_ecn_ok = B_TRUE;
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate
15350Sstevel@tonic-gate /*
15360Sstevel@tonic-gate * listener->tcp_rq->q_hiwat should be the default window size or a
15370Sstevel@tonic-gate * window size changed via SO_RCVBUF option. First round up the
15380Sstevel@tonic-gate * acceptor's tcp_rwnd to the nearest MSS. Then find out the window
15390Sstevel@tonic-gate * scale option value if needed. Call tcp_rwnd_set() to finish the
15400Sstevel@tonic-gate * setting.
15410Sstevel@tonic-gate *
15420Sstevel@tonic-gate * Note if there is a rpipe metric associated with the remote host,
15430Sstevel@tonic-gate * we should not inherit receive window size from listener.
15440Sstevel@tonic-gate */
15450Sstevel@tonic-gate acceptor->tcp_rwnd = MSS_ROUNDUP(
15460Sstevel@tonic-gate (acceptor->tcp_rwnd == 0 ? listener->tcp_rwnd_max :
15470Sstevel@tonic-gate acceptor->tcp_rwnd), acceptor->tcp_mss);
15480Sstevel@tonic-gate if (acceptor->tcp_snd_ws_ok)
15490Sstevel@tonic-gate SET_WS_VALUE(acceptor);
15500Sstevel@tonic-gate /*
15510Sstevel@tonic-gate * Note that this is the only place tcp_rwnd_set() is called for
15520Sstevel@tonic-gate * accepting a connection. We need to call it here instead of
15530Sstevel@tonic-gate * after the 3-way handshake because we need to tell the other
15540Sstevel@tonic-gate * side our rwnd in the SYN-ACK segment.
15550Sstevel@tonic-gate */
15560Sstevel@tonic-gate (void) tcp_rwnd_set(acceptor, acceptor->tcp_rwnd);
15570Sstevel@tonic-gate
15580Sstevel@tonic-gate return (0);
15590Sstevel@tonic-gate }
15600Sstevel@tonic-gate
15610Sstevel@tonic-gate /*
15620Sstevel@tonic-gate * Defense for the SYN attack -
15630Sstevel@tonic-gate * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest
15640Sstevel@tonic-gate * one that doesn't have the dontdrop bit set.
15650Sstevel@tonic-gate * 2. Don't drop a SYN request before its first timeout. This gives every
15660Sstevel@tonic-gate * request at least til the first timeout to complete its 3-way handshake.
15670Sstevel@tonic-gate * 3. The current threshold is - # of timeout > q0len/4 => SYN alert on
15680Sstevel@tonic-gate * # of timeout drops back to <= q0len/32 => SYN alert off
15690Sstevel@tonic-gate */
15700Sstevel@tonic-gate static boolean_t
tcp_drop_q0(tcp_t * tcp)15710Sstevel@tonic-gate tcp_drop_q0(tcp_t *tcp)
15720Sstevel@tonic-gate {
15730Sstevel@tonic-gate tcp_t *eager;
15740Sstevel@tonic-gate
15750Sstevel@tonic-gate assert(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0);
15760Sstevel@tonic-gate /*
15770Sstevel@tonic-gate * New one is added after next_q0 so prev_q0 points to the oldest
15780Sstevel@tonic-gate * Also do not drop any established connections that are deferred on
15790Sstevel@tonic-gate * q0 due to q being full
15800Sstevel@tonic-gate */
15810Sstevel@tonic-gate
15820Sstevel@tonic-gate eager = tcp->tcp_eager_prev_q0;
15830Sstevel@tonic-gate while (eager->tcp_dontdrop || eager->tcp_conn_def_q0) {
15840Sstevel@tonic-gate /* XXX should move the eager to the head */
15850Sstevel@tonic-gate eager = eager->tcp_eager_prev_q0;
15860Sstevel@tonic-gate if (eager == tcp) {
15870Sstevel@tonic-gate eager = tcp->tcp_eager_prev_q0;
15880Sstevel@tonic-gate break;
15890Sstevel@tonic-gate }
15900Sstevel@tonic-gate }
15910Sstevel@tonic-gate dprintf("tcp_drop_q0: listen half-open queue (max=%d) overflow"
15920Sstevel@tonic-gate " (%d pending) on %s, drop one", tcp_conn_req_max_q0,
15930Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0,
15940Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
15950Sstevel@tonic-gate
15960Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpHalfOpenDrop);
15970Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
15980Sstevel@tonic-gate return (B_TRUE);
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate
16010Sstevel@tonic-gate /* ARGSUSED */
16020Sstevel@tonic-gate static tcp_t *
tcp_conn_request(tcp_t * tcp,mblk_t * mp,uint_t sock_id,uint_t ip_hdr_len)16030Sstevel@tonic-gate tcp_conn_request(tcp_t *tcp, mblk_t *mp, uint_t sock_id, uint_t ip_hdr_len)
16040Sstevel@tonic-gate {
16050Sstevel@tonic-gate tcp_t *eager;
16060Sstevel@tonic-gate struct ip *ipha;
16070Sstevel@tonic-gate int err;
16080Sstevel@tonic-gate
16090Sstevel@tonic-gate #ifdef DEBUG
16100Sstevel@tonic-gate printf("tcp_conn_request ###################\n");
16110Sstevel@tonic-gate #endif
16120Sstevel@tonic-gate
16130Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) {
16140Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpListenDrop);
16150Sstevel@tonic-gate dprintf("tcp_conn_request: listen backlog (max=%d) "
16160Sstevel@tonic-gate "overflow (%d pending) on %s",
16170Sstevel@tonic-gate tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q,
16180Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
16190Sstevel@tonic-gate return (NULL);
16200Sstevel@tonic-gate }
16210Sstevel@tonic-gate
16220Sstevel@tonic-gate assert(OK_32PTR(mp->b_rptr));
16230Sstevel@tonic-gate
16240Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q0 >=
16250Sstevel@tonic-gate tcp->tcp_conn_req_max + tcp_conn_req_max_q0) {
16260Sstevel@tonic-gate /*
16270Sstevel@tonic-gate * Q0 is full. Drop a pending half-open req from the queue
16280Sstevel@tonic-gate * to make room for the new SYN req. Also mark the time we
16290Sstevel@tonic-gate * drop a SYN.
16300Sstevel@tonic-gate */
16310Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
16320Sstevel@tonic-gate if (!tcp_drop_q0(tcp)) {
16330Sstevel@tonic-gate freemsg(mp);
16340Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpListenDropQ0);
16350Sstevel@tonic-gate dprintf("tcp_conn_request: listen half-open queue "
16360Sstevel@tonic-gate "(max=%d) full (%d pending) on %s",
16370Sstevel@tonic-gate tcp_conn_req_max_q0,
16380Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0,
16390Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_PORT_ONLY));
16400Sstevel@tonic-gate return (NULL);
16410Sstevel@tonic-gate }
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate
16440Sstevel@tonic-gate ipha = (struct ip *)mp->b_rptr;
16450Sstevel@tonic-gate if (IN_CLASSD(ntohl(ipha->ip_src.s_addr)) ||
16460Sstevel@tonic-gate ipha->ip_src.s_addr == INADDR_BROADCAST ||
16470Sstevel@tonic-gate ipha->ip_src.s_addr == INADDR_ANY ||
16480Sstevel@tonic-gate ipha->ip_dst.s_addr == INADDR_BROADCAST) {
16490Sstevel@tonic-gate freemsg(mp);
16500Sstevel@tonic-gate return (NULL);
16510Sstevel@tonic-gate }
16520Sstevel@tonic-gate /*
16530Sstevel@tonic-gate * We allow the connection to proceed
16540Sstevel@tonic-gate * by generating a detached tcp state vector and put it in
16550Sstevel@tonic-gate * the eager queue. When an accept happens, it will be
16560Sstevel@tonic-gate * dequeued sequentially.
16570Sstevel@tonic-gate */
16580Sstevel@tonic-gate if ((eager = (tcp_t *)bkmem_alloc(sizeof (tcp_t))) == NULL) {
16590Sstevel@tonic-gate freemsg(mp);
16600Sstevel@tonic-gate errno = ENOBUFS;
16610Sstevel@tonic-gate return (NULL);
16620Sstevel@tonic-gate }
16630Sstevel@tonic-gate if ((errno = tcp_init_values(eager, NULL)) != 0) {
16640Sstevel@tonic-gate freemsg(mp);
16650Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
16660Sstevel@tonic-gate return (NULL);
16670Sstevel@tonic-gate }
16680Sstevel@tonic-gate
16690Sstevel@tonic-gate /*
16700Sstevel@tonic-gate * Eager connection inherits address form from its listener,
16710Sstevel@tonic-gate * but its packet form comes from the version of the received
16720Sstevel@tonic-gate * SYN segment.
16730Sstevel@tonic-gate */
16740Sstevel@tonic-gate eager->tcp_family = tcp->tcp_family;
16750Sstevel@tonic-gate
16760Sstevel@tonic-gate err = tcp_accept_comm(tcp, eager, mp, ip_hdr_len);
16770Sstevel@tonic-gate if (err) {
16780Sstevel@tonic-gate bkmem_free((caddr_t)eager, sizeof (tcp_t));
16790Sstevel@tonic-gate return (NULL);
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate
16820Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager;
16830Sstevel@tonic-gate eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0;
16840Sstevel@tonic-gate tcp->tcp_eager_next_q0 = eager;
16850Sstevel@tonic-gate eager->tcp_eager_prev_q0 = tcp;
16860Sstevel@tonic-gate
16870Sstevel@tonic-gate /* Set tcp_listener before adding it to tcp_conn_fanout */
16880Sstevel@tonic-gate eager->tcp_listener = tcp;
16890Sstevel@tonic-gate tcp->tcp_conn_req_cnt_q0++;
16900Sstevel@tonic-gate
16910Sstevel@tonic-gate return (eager);
16920Sstevel@tonic-gate }
16930Sstevel@tonic-gate
16940Sstevel@tonic-gate /*
16950Sstevel@tonic-gate * To get around the non-interrupt problem of inetboot.
16960Sstevel@tonic-gate * Keep on processing packets until a certain state is reached or the
16970Sstevel@tonic-gate * TCP is destroyed because of getting a RST packet.
16980Sstevel@tonic-gate */
16990Sstevel@tonic-gate static int
tcp_state_wait(int sock_id,tcp_t * tcp,int state)17000Sstevel@tonic-gate tcp_state_wait(int sock_id, tcp_t *tcp, int state)
17010Sstevel@tonic-gate {
17020Sstevel@tonic-gate int i;
17030Sstevel@tonic-gate struct inetgram *in_gram;
17040Sstevel@tonic-gate mblk_t *mp;
17050Sstevel@tonic-gate int timeout;
17060Sstevel@tonic-gate boolean_t changed = B_FALSE;
17070Sstevel@tonic-gate
17080Sstevel@tonic-gate /*
17090Sstevel@tonic-gate * We need to make sure that the MAC does not wait longer
17100Sstevel@tonic-gate * than RTO for any packet so that TCP can do retransmission.
17110Sstevel@tonic-gate * But if the MAC timeout is less than tcp_rto, we are fine
17120Sstevel@tonic-gate * and do not need to change it.
17130Sstevel@tonic-gate */
17140Sstevel@tonic-gate timeout = sockets[sock_id].in_timeout;
17150Sstevel@tonic-gate if (timeout > tcp->tcp_rto) {
17160Sstevel@tonic-gate sockets[sock_id].in_timeout = tcp->tcp_rto;
17170Sstevel@tonic-gate changed = B_TRUE;
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate retry:
17200Sstevel@tonic-gate if (sockets[sock_id].inq == NULL) {
17210Sstevel@tonic-gate /* Go out and check the wire */
17220Sstevel@tonic-gate for (i = MEDIA_LVL; i < TRANSPORT_LVL; i++) {
17230Sstevel@tonic-gate if (sockets[sock_id].input[i] != NULL) {
17240Sstevel@tonic-gate if (sockets[sock_id].input[i](sock_id) < 0) {
17250Sstevel@tonic-gate if (changed) {
17260Sstevel@tonic-gate sockets[sock_id].in_timeout =
17270Sstevel@tonic-gate timeout;
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate return (-1);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate }
17320Sstevel@tonic-gate }
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate
17350Sstevel@tonic-gate while ((in_gram = sockets[sock_id].inq) != NULL) {
17360Sstevel@tonic-gate if (tcp != NULL && tcp->tcp_state == state)
17370Sstevel@tonic-gate break;
17380Sstevel@tonic-gate
17390Sstevel@tonic-gate /* Remove unknown inetgrams from the head of inq. */
17400Sstevel@tonic-gate if (in_gram->igm_level != TRANSPORT_LVL) {
17410Sstevel@tonic-gate #ifdef DEBUG
17420Sstevel@tonic-gate printf("tcp_state_wait for state %d: unexpected "
17430Sstevel@tonic-gate "packet level %d frame found\n", state,
17440Sstevel@tonic-gate in_gram->igm_level);
17450Sstevel@tonic-gate #endif
17460Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_TRUE);
17470Sstevel@tonic-gate continue;
17480Sstevel@tonic-gate }
17490Sstevel@tonic-gate mp = in_gram->igm_mp;
17500Sstevel@tonic-gate del_gram(&sockets[sock_id].inq, in_gram, B_FALSE);
17510Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
17520Sstevel@tonic-gate tcp_rput_data(tcp, mp, sock_id);
17530Sstevel@tonic-gate
17540Sstevel@tonic-gate /*
17550Sstevel@tonic-gate * The other side may have closed this connection or
17560Sstevel@tonic-gate * RST us. But we need to continue to process other
17570Sstevel@tonic-gate * packets in the socket's queue because they may be
17580Sstevel@tonic-gate * belong to another TCP connections.
17590Sstevel@tonic-gate */
17600Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL) {
17610Sstevel@tonic-gate tcp = NULL;
17620Sstevel@tonic-gate }
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate
17650Sstevel@tonic-gate /* If the other side has closed the connection, just return. */
17660Sstevel@tonic-gate if (tcp == NULL || sockets[sock_id].pcb == NULL) {
17670Sstevel@tonic-gate #ifdef DEBUG
17680Sstevel@tonic-gate printf("tcp_state_wait other side dead: state %d "
17690Sstevel@tonic-gate "error %d\n", state, sockets[sock_id].so_error);
17700Sstevel@tonic-gate #endif
17710Sstevel@tonic-gate if (sockets[sock_id].so_error != 0)
17720Sstevel@tonic-gate return (-1);
17730Sstevel@tonic-gate else
17740Sstevel@tonic-gate return (0);
17750Sstevel@tonic-gate }
17760Sstevel@tonic-gate /*
17770Sstevel@tonic-gate * TCPS_ALL_ACKED is not a valid TCP state, it is just used as an
17780Sstevel@tonic-gate * indicator to tcp_state_wait to mean that it is being called
17790Sstevel@tonic-gate * to wait till we have received acks for all the new segments sent.
17800Sstevel@tonic-gate */
17810Sstevel@tonic-gate if ((state == TCPS_ALL_ACKED) && (tcp->tcp_suna == tcp->tcp_snxt)) {
17820Sstevel@tonic-gate goto done;
17830Sstevel@tonic-gate }
17840Sstevel@tonic-gate if (tcp->tcp_state != state) {
17850Sstevel@tonic-gate if (prom_gettime() > tcp->tcp_rto_timeout)
17860Sstevel@tonic-gate tcp_timer(tcp, sock_id);
17870Sstevel@tonic-gate goto retry;
17880Sstevel@tonic-gate }
17890Sstevel@tonic-gate done:
17900Sstevel@tonic-gate if (changed)
17910Sstevel@tonic-gate sockets[sock_id].in_timeout = timeout;
17920Sstevel@tonic-gate
17930Sstevel@tonic-gate tcp_drain_needed(sock_id, tcp);
17940Sstevel@tonic-gate return (0);
17950Sstevel@tonic-gate }
17960Sstevel@tonic-gate
17970Sstevel@tonic-gate /* Verify the checksum of a segment. */
17980Sstevel@tonic-gate static int
tcp_verify_cksum(mblk_t * mp)17990Sstevel@tonic-gate tcp_verify_cksum(mblk_t *mp)
18000Sstevel@tonic-gate {
18010Sstevel@tonic-gate struct ip *iph;
18020Sstevel@tonic-gate tcpha_t *tcph;
18030Sstevel@tonic-gate int len;
18040Sstevel@tonic-gate uint16_t old_sum;
18050Sstevel@tonic-gate
18060Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
18070Sstevel@tonic-gate tcph = (tcpha_t *)(iph + 1);
18080Sstevel@tonic-gate len = ntohs(iph->ip_len);
18090Sstevel@tonic-gate
18100Sstevel@tonic-gate /*
18110Sstevel@tonic-gate * Calculate the TCP checksum. Need to include the psuedo header,
18120Sstevel@tonic-gate * which is similar to the real IP header starting at the TTL field.
18130Sstevel@tonic-gate */
18140Sstevel@tonic-gate iph->ip_sum = htons(len - IP_SIMPLE_HDR_LENGTH);
18150Sstevel@tonic-gate old_sum = tcph->tha_sum;
18160Sstevel@tonic-gate tcph->tha_sum = 0;
18170Sstevel@tonic-gate iph->ip_ttl = 0;
18180Sstevel@tonic-gate if (old_sum == tcp_cksum((uint16_t *)&(iph->ip_ttl),
18190Sstevel@tonic-gate len - IP_SIMPLE_HDR_LENGTH + 12)) {
18200Sstevel@tonic-gate return (0);
18210Sstevel@tonic-gate } else {
18220Sstevel@tonic-gate tcp_cksum_errors++;
18230Sstevel@tonic-gate return (-1);
18240Sstevel@tonic-gate }
18250Sstevel@tonic-gate }
18260Sstevel@tonic-gate
18270Sstevel@tonic-gate /* To find a TCP connection matching the incoming segment. */
18280Sstevel@tonic-gate static tcp_t *
tcp_lookup_ipv4(struct ip * iph,tcpha_t * tcph,int min_state,int * sock_id)18290Sstevel@tonic-gate tcp_lookup_ipv4(struct ip *iph, tcpha_t *tcph, int min_state, int *sock_id)
18300Sstevel@tonic-gate {
18310Sstevel@tonic-gate int i;
18320Sstevel@tonic-gate tcp_t *tcp;
18330Sstevel@tonic-gate
18340Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
18350Sstevel@tonic-gate if (sockets[i].type == INETBOOT_STREAM &&
18360Sstevel@tonic-gate (tcp = (tcp_t *)sockets[i].pcb) != NULL) {
18370Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18380Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18390Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18400Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source &&
18410Sstevel@tonic-gate tcp->tcp_state >= min_state) {
18420Sstevel@tonic-gate *sock_id = i;
18430Sstevel@tonic-gate return (tcp);
18440Sstevel@tonic-gate }
18450Sstevel@tonic-gate }
18460Sstevel@tonic-gate }
18470Sstevel@tonic-gate /* Find it in the time wait list. */
18480Sstevel@tonic-gate for (tcp = tcp_time_wait_head; tcp != NULL;
18490Sstevel@tonic-gate tcp = tcp->tcp_time_wait_next) {
18500Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18510Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18520Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18530Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source &&
18540Sstevel@tonic-gate tcp->tcp_state >= min_state) {
18550Sstevel@tonic-gate *sock_id = -1;
18560Sstevel@tonic-gate return (tcp);
18570Sstevel@tonic-gate }
18580Sstevel@tonic-gate }
18590Sstevel@tonic-gate return (NULL);
18600Sstevel@tonic-gate }
18610Sstevel@tonic-gate
18620Sstevel@tonic-gate /* To find a TCP listening connection matching the incoming segment. */
18630Sstevel@tonic-gate static tcp_t *
tcp_lookup_listener_ipv4(in_addr_t addr,in_port_t port,int * sock_id)18640Sstevel@tonic-gate tcp_lookup_listener_ipv4(in_addr_t addr, in_port_t port, int *sock_id)
18650Sstevel@tonic-gate {
18660Sstevel@tonic-gate int i;
18670Sstevel@tonic-gate tcp_t *tcp;
18680Sstevel@tonic-gate
18690Sstevel@tonic-gate for (i = 0; i < MAXSOCKET; i++) {
18700Sstevel@tonic-gate if (sockets[i].type == INETBOOT_STREAM &&
18710Sstevel@tonic-gate (tcp = (tcp_t *)sockets[i].pcb) != NULL) {
18720Sstevel@tonic-gate if (tcp->tcp_lport == port &&
18730Sstevel@tonic-gate (tcp->tcp_bound_source == addr ||
18740Sstevel@tonic-gate tcp->tcp_bound_source == INADDR_ANY)) {
18750Sstevel@tonic-gate *sock_id = i;
18760Sstevel@tonic-gate return (tcp);
18770Sstevel@tonic-gate }
18780Sstevel@tonic-gate }
18790Sstevel@tonic-gate }
18800Sstevel@tonic-gate
18810Sstevel@tonic-gate return (NULL);
18820Sstevel@tonic-gate }
18830Sstevel@tonic-gate
18840Sstevel@tonic-gate /* To find a TCP eager matching the incoming segment. */
18850Sstevel@tonic-gate static tcp_t *
tcp_lookup_eager_ipv4(tcp_t * listener,struct ip * iph,tcpha_t * tcph)18860Sstevel@tonic-gate tcp_lookup_eager_ipv4(tcp_t *listener, struct ip *iph, tcpha_t *tcph)
18870Sstevel@tonic-gate {
18880Sstevel@tonic-gate tcp_t *tcp;
18890Sstevel@tonic-gate
18900Sstevel@tonic-gate #ifdef DEBUG
18910Sstevel@tonic-gate printf("tcp_lookup_eager_ipv4 ###############\n");
18920Sstevel@tonic-gate #endif
18930Sstevel@tonic-gate for (tcp = listener->tcp_eager_next_q; tcp != NULL;
18940Sstevel@tonic-gate tcp = tcp->tcp_eager_next_q) {
18950Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
18960Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
18970Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
18980Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source) {
18990Sstevel@tonic-gate return (tcp);
19000Sstevel@tonic-gate }
19010Sstevel@tonic-gate }
19020Sstevel@tonic-gate
19030Sstevel@tonic-gate for (tcp = listener->tcp_eager_next_q0; tcp != listener;
19040Sstevel@tonic-gate tcp = tcp->tcp_eager_next_q0) {
19050Sstevel@tonic-gate if (tcph->tha_lport == tcp->tcp_fport &&
19060Sstevel@tonic-gate tcph->tha_fport == tcp->tcp_lport &&
19070Sstevel@tonic-gate iph->ip_src.s_addr == tcp->tcp_remote &&
19080Sstevel@tonic-gate iph->ip_dst.s_addr == tcp->tcp_bound_source) {
19090Sstevel@tonic-gate return (tcp);
19100Sstevel@tonic-gate }
19110Sstevel@tonic-gate }
19120Sstevel@tonic-gate #ifdef DEBUG
19130Sstevel@tonic-gate printf("No eager found\n");
19140Sstevel@tonic-gate #endif
19150Sstevel@tonic-gate return (NULL);
19160Sstevel@tonic-gate }
19170Sstevel@tonic-gate
19180Sstevel@tonic-gate /* To destroy a TCP control block. */
19190Sstevel@tonic-gate static void
tcp_clean_death(int sock_id,tcp_t * tcp,int err)19200Sstevel@tonic-gate tcp_clean_death(int sock_id, tcp_t *tcp, int err)
19210Sstevel@tonic-gate {
19220Sstevel@tonic-gate tcp_free(tcp);
19230Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT)
19240Sstevel@tonic-gate tcp_time_wait_remove(tcp);
19250Sstevel@tonic-gate
19260Sstevel@tonic-gate if (sock_id >= 0) {
19270Sstevel@tonic-gate sockets[sock_id].pcb = NULL;
19280Sstevel@tonic-gate if (err != 0)
19290Sstevel@tonic-gate sockets[sock_id].so_error = err;
19300Sstevel@tonic-gate }
19310Sstevel@tonic-gate bkmem_free((caddr_t)tcp, sizeof (tcp_t));
19320Sstevel@tonic-gate }
19330Sstevel@tonic-gate
19340Sstevel@tonic-gate /*
19350Sstevel@tonic-gate * tcp_rwnd_set() is called to adjust the receive window to a desired value.
19360Sstevel@tonic-gate * We do not allow the receive window to shrink. After setting rwnd,
19370Sstevel@tonic-gate * set the flow control hiwat of the stream.
19380Sstevel@tonic-gate *
19390Sstevel@tonic-gate * This function is called in 2 cases:
19400Sstevel@tonic-gate *
19410Sstevel@tonic-gate * 1) Before data transfer begins, in tcp_accept_comm() for accepting a
19420Sstevel@tonic-gate * connection (passive open) and in tcp_rput_data() for active connect.
19430Sstevel@tonic-gate * This is called after tcp_mss_set() when the desired MSS value is known.
19440Sstevel@tonic-gate * This makes sure that our window size is a mutiple of the other side's
19450Sstevel@tonic-gate * MSS.
19460Sstevel@tonic-gate * 2) Handling SO_RCVBUF option.
19470Sstevel@tonic-gate *
19480Sstevel@tonic-gate * It is ASSUMED that the requested size is a multiple of the current MSS.
19490Sstevel@tonic-gate *
19500Sstevel@tonic-gate * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
19510Sstevel@tonic-gate * user requests so.
19520Sstevel@tonic-gate */
19530Sstevel@tonic-gate static int
tcp_rwnd_set(tcp_t * tcp,uint32_t rwnd)19540Sstevel@tonic-gate tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
19550Sstevel@tonic-gate {
19560Sstevel@tonic-gate uint32_t mss = tcp->tcp_mss;
19570Sstevel@tonic-gate uint32_t old_max_rwnd;
19580Sstevel@tonic-gate uint32_t max_transmittable_rwnd;
19590Sstevel@tonic-gate
19600Sstevel@tonic-gate if (tcp->tcp_rwnd_max != 0)
19610Sstevel@tonic-gate old_max_rwnd = tcp->tcp_rwnd_max;
19620Sstevel@tonic-gate else
19630Sstevel@tonic-gate old_max_rwnd = tcp->tcp_rwnd;
19640Sstevel@tonic-gate
19650Sstevel@tonic-gate /*
19660Sstevel@tonic-gate * Insist on a receive window that is at least
19670Sstevel@tonic-gate * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
19680Sstevel@tonic-gate * funny TCP interactions of Nagle algorithm, SWS avoidance
19690Sstevel@tonic-gate * and delayed acknowledgement.
19700Sstevel@tonic-gate */
19710Sstevel@tonic-gate rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss);
19720Sstevel@tonic-gate
19730Sstevel@tonic-gate /*
19740Sstevel@tonic-gate * If window size info has already been exchanged, TCP should not
19750Sstevel@tonic-gate * shrink the window. Shrinking window is doable if done carefully.
19760Sstevel@tonic-gate * We may add that support later. But so far there is not a real
19770Sstevel@tonic-gate * need to do that.
19780Sstevel@tonic-gate */
19790Sstevel@tonic-gate if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
19800Sstevel@tonic-gate /* MSS may have changed, do a round up again. */
19810Sstevel@tonic-gate rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
19820Sstevel@tonic-gate }
19830Sstevel@tonic-gate
19840Sstevel@tonic-gate /*
19850Sstevel@tonic-gate * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
19860Sstevel@tonic-gate * can be applied even before the window scale option is decided.
19870Sstevel@tonic-gate */
19880Sstevel@tonic-gate max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
19890Sstevel@tonic-gate if (rwnd > max_transmittable_rwnd) {
19900Sstevel@tonic-gate rwnd = max_transmittable_rwnd -
19910Sstevel@tonic-gate (max_transmittable_rwnd % mss);
19920Sstevel@tonic-gate if (rwnd < mss)
19930Sstevel@tonic-gate rwnd = max_transmittable_rwnd;
19940Sstevel@tonic-gate /*
19950Sstevel@tonic-gate * If we're over the limit we may have to back down tcp_rwnd.
19960Sstevel@tonic-gate * The increment below won't work for us. So we set all three
19970Sstevel@tonic-gate * here and the increment below will have no effect.
19980Sstevel@tonic-gate */
19990Sstevel@tonic-gate tcp->tcp_rwnd = old_max_rwnd = rwnd;
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate
20020Sstevel@tonic-gate /*
20030Sstevel@tonic-gate * Increment the current rwnd by the amount the maximum grew (we
20040Sstevel@tonic-gate * can not overwrite it since we might be in the middle of a
20050Sstevel@tonic-gate * connection.)
20060Sstevel@tonic-gate */
20070Sstevel@tonic-gate tcp->tcp_rwnd += rwnd - old_max_rwnd;
20080Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win);
20090Sstevel@tonic-gate if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
20100Sstevel@tonic-gate tcp->tcp_cwnd_max = rwnd;
20110Sstevel@tonic-gate tcp->tcp_rwnd_max = rwnd;
20120Sstevel@tonic-gate
20130Sstevel@tonic-gate return (rwnd);
20140Sstevel@tonic-gate }
20150Sstevel@tonic-gate
20160Sstevel@tonic-gate /*
20170Sstevel@tonic-gate * Extract option values from a tcp header. We put any found values into the
20180Sstevel@tonic-gate * tcpopt struct and return a bitmask saying which options were found.
20190Sstevel@tonic-gate */
20200Sstevel@tonic-gate static int
tcp_parse_options(tcph_t * tcph,tcp_opt_t * tcpopt)20210Sstevel@tonic-gate tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate uchar_t *endp;
20240Sstevel@tonic-gate int len;
20250Sstevel@tonic-gate uint32_t mss;
20260Sstevel@tonic-gate uchar_t *up = (uchar_t *)tcph;
20270Sstevel@tonic-gate int found = 0;
20280Sstevel@tonic-gate int32_t sack_len;
20290Sstevel@tonic-gate tcp_seq sack_begin, sack_end;
20300Sstevel@tonic-gate tcp_t *tcp;
20310Sstevel@tonic-gate
20320Sstevel@tonic-gate endp = up + TCP_HDR_LENGTH(tcph);
20330Sstevel@tonic-gate up += TCP_MIN_HEADER_LENGTH;
20340Sstevel@tonic-gate while (up < endp) {
20350Sstevel@tonic-gate len = endp - up;
20360Sstevel@tonic-gate switch (*up) {
20370Sstevel@tonic-gate case TCPOPT_EOL:
20380Sstevel@tonic-gate break;
20390Sstevel@tonic-gate
20400Sstevel@tonic-gate case TCPOPT_NOP:
20410Sstevel@tonic-gate up++;
20420Sstevel@tonic-gate continue;
20430Sstevel@tonic-gate
20440Sstevel@tonic-gate case TCPOPT_MAXSEG:
20450Sstevel@tonic-gate if (len < TCPOPT_MAXSEG_LEN ||
20460Sstevel@tonic-gate up[1] != TCPOPT_MAXSEG_LEN)
20470Sstevel@tonic-gate break;
20480Sstevel@tonic-gate
20490Sstevel@tonic-gate mss = BE16_TO_U16(up+2);
20500Sstevel@tonic-gate /* Caller must handle tcp_mss_min and tcp_mss_max_* */
20510Sstevel@tonic-gate tcpopt->tcp_opt_mss = mss;
20520Sstevel@tonic-gate found |= TCP_OPT_MSS_PRESENT;
20530Sstevel@tonic-gate
20540Sstevel@tonic-gate up += TCPOPT_MAXSEG_LEN;
20550Sstevel@tonic-gate continue;
20560Sstevel@tonic-gate
20570Sstevel@tonic-gate case TCPOPT_WSCALE:
20580Sstevel@tonic-gate if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN)
20590Sstevel@tonic-gate break;
20600Sstevel@tonic-gate
20610Sstevel@tonic-gate if (up[2] > TCP_MAX_WINSHIFT)
20620Sstevel@tonic-gate tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT;
20630Sstevel@tonic-gate else
20640Sstevel@tonic-gate tcpopt->tcp_opt_wscale = up[2];
20650Sstevel@tonic-gate found |= TCP_OPT_WSCALE_PRESENT;
20660Sstevel@tonic-gate
20670Sstevel@tonic-gate up += TCPOPT_WS_LEN;
20680Sstevel@tonic-gate continue;
20690Sstevel@tonic-gate
20700Sstevel@tonic-gate case TCPOPT_SACK_PERMITTED:
20710Sstevel@tonic-gate if (len < TCPOPT_SACK_OK_LEN ||
20720Sstevel@tonic-gate up[1] != TCPOPT_SACK_OK_LEN)
20730Sstevel@tonic-gate break;
20740Sstevel@tonic-gate found |= TCP_OPT_SACK_OK_PRESENT;
20750Sstevel@tonic-gate up += TCPOPT_SACK_OK_LEN;
20760Sstevel@tonic-gate continue;
20770Sstevel@tonic-gate
20780Sstevel@tonic-gate case TCPOPT_SACK:
20790Sstevel@tonic-gate if (len <= 2 || up[1] <= 2 || len < up[1])
20800Sstevel@tonic-gate break;
20810Sstevel@tonic-gate
20820Sstevel@tonic-gate /* If TCP is not interested in SACK blks... */
20830Sstevel@tonic-gate if ((tcp = tcpopt->tcp) == NULL) {
20840Sstevel@tonic-gate up += up[1];
20850Sstevel@tonic-gate continue;
20860Sstevel@tonic-gate }
20870Sstevel@tonic-gate sack_len = up[1] - TCPOPT_HEADER_LEN;
20880Sstevel@tonic-gate up += TCPOPT_HEADER_LEN;
20890Sstevel@tonic-gate
20900Sstevel@tonic-gate /*
20910Sstevel@tonic-gate * If the list is empty, allocate one and assume
20920Sstevel@tonic-gate * nothing is sack'ed.
20930Sstevel@tonic-gate */
20940Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
20950Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
20960Sstevel@tonic-gate tcp_notsack_update(&(tcp->tcp_notsack_list),
20970Sstevel@tonic-gate tcp->tcp_suna, tcp->tcp_snxt,
20980Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
20990Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
21000Sstevel@tonic-gate
21010Sstevel@tonic-gate /*
21020Sstevel@tonic-gate * Make sure tcp_notsack_list is not NULL.
21030Sstevel@tonic-gate * This happens when kmem_alloc(KM_NOSLEEP)
21040Sstevel@tonic-gate * returns NULL.
21050Sstevel@tonic-gate */
21060Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
21070Sstevel@tonic-gate up += sack_len;
21080Sstevel@tonic-gate continue;
21090Sstevel@tonic-gate }
21100Sstevel@tonic-gate tcp->tcp_fack = tcp->tcp_suna;
21110Sstevel@tonic-gate }
21120Sstevel@tonic-gate
21130Sstevel@tonic-gate while (sack_len > 0) {
21140Sstevel@tonic-gate if (up + 8 > endp) {
21150Sstevel@tonic-gate up = endp;
21160Sstevel@tonic-gate break;
21170Sstevel@tonic-gate }
21180Sstevel@tonic-gate sack_begin = BE32_TO_U32(up);
21190Sstevel@tonic-gate up += 4;
21200Sstevel@tonic-gate sack_end = BE32_TO_U32(up);
21210Sstevel@tonic-gate up += 4;
21220Sstevel@tonic-gate sack_len -= 8;
21230Sstevel@tonic-gate /*
21240Sstevel@tonic-gate * Bounds checking. Make sure the SACK
21250Sstevel@tonic-gate * info is within tcp_suna and tcp_snxt.
21260Sstevel@tonic-gate * If this SACK blk is out of bound, ignore
21270Sstevel@tonic-gate * it but continue to parse the following
21280Sstevel@tonic-gate * blks.
21290Sstevel@tonic-gate */
21300Sstevel@tonic-gate if (SEQ_LEQ(sack_end, sack_begin) ||
21310Sstevel@tonic-gate SEQ_LT(sack_begin, tcp->tcp_suna) ||
21320Sstevel@tonic-gate SEQ_GT(sack_end, tcp->tcp_snxt)) {
21330Sstevel@tonic-gate continue;
21340Sstevel@tonic-gate }
21350Sstevel@tonic-gate tcp_notsack_insert(&(tcp->tcp_notsack_list),
21360Sstevel@tonic-gate sack_begin, sack_end,
21370Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
21380Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
21390Sstevel@tonic-gate if (SEQ_GT(sack_end, tcp->tcp_fack)) {
21400Sstevel@tonic-gate tcp->tcp_fack = sack_end;
21410Sstevel@tonic-gate }
21420Sstevel@tonic-gate }
21430Sstevel@tonic-gate found |= TCP_OPT_SACK_PRESENT;
21440Sstevel@tonic-gate continue;
21450Sstevel@tonic-gate
21460Sstevel@tonic-gate case TCPOPT_TSTAMP:
21470Sstevel@tonic-gate if (len < TCPOPT_TSTAMP_LEN ||
21480Sstevel@tonic-gate up[1] != TCPOPT_TSTAMP_LEN)
21490Sstevel@tonic-gate break;
21500Sstevel@tonic-gate
21510Sstevel@tonic-gate tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2);
21520Sstevel@tonic-gate tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6);
21530Sstevel@tonic-gate
21540Sstevel@tonic-gate found |= TCP_OPT_TSTAMP_PRESENT;
21550Sstevel@tonic-gate
21560Sstevel@tonic-gate up += TCPOPT_TSTAMP_LEN;
21570Sstevel@tonic-gate continue;
21580Sstevel@tonic-gate
21590Sstevel@tonic-gate default:
21600Sstevel@tonic-gate if (len <= 1 || len < (int)up[1] || up[1] == 0)
21610Sstevel@tonic-gate break;
21620Sstevel@tonic-gate up += up[1];
21630Sstevel@tonic-gate continue;
21640Sstevel@tonic-gate }
21650Sstevel@tonic-gate break;
21660Sstevel@tonic-gate }
21670Sstevel@tonic-gate return (found);
21680Sstevel@tonic-gate }
21690Sstevel@tonic-gate
21700Sstevel@tonic-gate /*
21710Sstevel@tonic-gate * Set the mss associated with a particular tcp based on its current value,
21720Sstevel@tonic-gate * and a new one passed in. Observe minimums and maximums, and reset
21730Sstevel@tonic-gate * other state variables that we want to view as multiples of mss.
21740Sstevel@tonic-gate *
21750Sstevel@tonic-gate * This function is called in various places mainly because
21760Sstevel@tonic-gate * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the
21770Sstevel@tonic-gate * other side's SYN/SYN-ACK packet arrives.
21780Sstevel@tonic-gate * 2) PMTUd may get us a new MSS.
21790Sstevel@tonic-gate * 3) If the other side stops sending us timestamp option, we need to
21800Sstevel@tonic-gate * increase the MSS size to use the extra bytes available.
21810Sstevel@tonic-gate */
21820Sstevel@tonic-gate static void
tcp_mss_set(tcp_t * tcp,uint32_t mss)21830Sstevel@tonic-gate tcp_mss_set(tcp_t *tcp, uint32_t mss)
21840Sstevel@tonic-gate {
21850Sstevel@tonic-gate uint32_t mss_max;
21860Sstevel@tonic-gate
21870Sstevel@tonic-gate mss_max = tcp_mss_max_ipv4;
21880Sstevel@tonic-gate
21890Sstevel@tonic-gate if (mss < tcp_mss_min)
21900Sstevel@tonic-gate mss = tcp_mss_min;
21910Sstevel@tonic-gate if (mss > mss_max)
21920Sstevel@tonic-gate mss = mss_max;
21930Sstevel@tonic-gate /*
21940Sstevel@tonic-gate * Unless naglim has been set by our client to
21950Sstevel@tonic-gate * a non-mss value, force naglim to track mss.
21960Sstevel@tonic-gate * This can help to aggregate small writes.
21970Sstevel@tonic-gate */
21980Sstevel@tonic-gate if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim)
21990Sstevel@tonic-gate tcp->tcp_naglim = mss;
22000Sstevel@tonic-gate /*
22010Sstevel@tonic-gate * TCP should be able to buffer at least 4 MSS data for obvious
22020Sstevel@tonic-gate * performance reason.
22030Sstevel@tonic-gate */
22040Sstevel@tonic-gate if ((mss << 2) > tcp->tcp_xmit_hiwater)
22050Sstevel@tonic-gate tcp->tcp_xmit_hiwater = mss << 2;
22060Sstevel@tonic-gate tcp->tcp_mss = mss;
22070Sstevel@tonic-gate /*
22080Sstevel@tonic-gate * Initialize cwnd according to draft-floyd-incr-init-win-01.txt.
22090Sstevel@tonic-gate * Previously, we use tcp_slow_start_initial to control the size
22100Sstevel@tonic-gate * of the initial cwnd. Now, when tcp_slow_start_initial * mss
22110Sstevel@tonic-gate * is smaller than the cwnd calculated from the formula suggested in
22120Sstevel@tonic-gate * the draft, we use tcp_slow_start_initial * mss as the cwnd.
22130Sstevel@tonic-gate * Otherwise, use the cwnd from the draft's formula. The default
22140Sstevel@tonic-gate * of tcp_slow_start_initial is 2.
22150Sstevel@tonic-gate */
22160Sstevel@tonic-gate tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss,
22170Sstevel@tonic-gate MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss)));
22180Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
22190Sstevel@tonic-gate }
22200Sstevel@tonic-gate
22210Sstevel@tonic-gate /*
22220Sstevel@tonic-gate * Process all TCP option in SYN segment.
22230Sstevel@tonic-gate *
22240Sstevel@tonic-gate * This function sets up the correct tcp_mss value according to the
22250Sstevel@tonic-gate * MSS option value and our header size. It also sets up the window scale
22260Sstevel@tonic-gate * and timestamp values, and initialize SACK info blocks. But it does not
22270Sstevel@tonic-gate * change receive window size after setting the tcp_mss value. The caller
22280Sstevel@tonic-gate * should do the appropriate change.
22290Sstevel@tonic-gate */
22300Sstevel@tonic-gate void
tcp_process_options(tcp_t * tcp,tcph_t * tcph)22310Sstevel@tonic-gate tcp_process_options(tcp_t *tcp, tcph_t *tcph)
22320Sstevel@tonic-gate {
22330Sstevel@tonic-gate int options;
22340Sstevel@tonic-gate tcp_opt_t tcpopt;
22350Sstevel@tonic-gate uint32_t mss_max;
22360Sstevel@tonic-gate char *tmp_tcph;
22370Sstevel@tonic-gate
22380Sstevel@tonic-gate tcpopt.tcp = NULL;
22390Sstevel@tonic-gate options = tcp_parse_options(tcph, &tcpopt);
22400Sstevel@tonic-gate
22410Sstevel@tonic-gate /*
22420Sstevel@tonic-gate * Process MSS option. Note that MSS option value does not account
22430Sstevel@tonic-gate * for IP or TCP options. This means that it is equal to MTU - minimum
22440Sstevel@tonic-gate * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for
22450Sstevel@tonic-gate * IPv6.
22460Sstevel@tonic-gate */
22470Sstevel@tonic-gate if (!(options & TCP_OPT_MSS_PRESENT)) {
22480Sstevel@tonic-gate tcpopt.tcp_opt_mss = tcp_mss_def_ipv4;
22490Sstevel@tonic-gate } else {
22500Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
22510Sstevel@tonic-gate mss_max = tcp_mss_max_ipv4;
22520Sstevel@tonic-gate if (tcpopt.tcp_opt_mss < tcp_mss_min)
22530Sstevel@tonic-gate tcpopt.tcp_opt_mss = tcp_mss_min;
22540Sstevel@tonic-gate else if (tcpopt.tcp_opt_mss > mss_max)
22550Sstevel@tonic-gate tcpopt.tcp_opt_mss = mss_max;
22560Sstevel@tonic-gate }
22570Sstevel@tonic-gate
22580Sstevel@tonic-gate /* Process Window Scale option. */
22590Sstevel@tonic-gate if (options & TCP_OPT_WSCALE_PRESENT) {
22600Sstevel@tonic-gate tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale;
22610Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_TRUE;
22620Sstevel@tonic-gate } else {
22630Sstevel@tonic-gate tcp->tcp_snd_ws = B_FALSE;
22640Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_FALSE;
22650Sstevel@tonic-gate tcp->tcp_rcv_ws = B_FALSE;
22660Sstevel@tonic-gate }
22670Sstevel@tonic-gate
22680Sstevel@tonic-gate /* Process Timestamp option. */
22690Sstevel@tonic-gate if ((options & TCP_OPT_TSTAMP_PRESENT) &&
22700Sstevel@tonic-gate (tcp->tcp_snd_ts_ok || !tcp->tcp_active_open)) {
22710Sstevel@tonic-gate tmp_tcph = (char *)tcp->tcp_tcph;
22720Sstevel@tonic-gate
22730Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_TRUE;
22740Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
22750Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
22760Sstevel@tonic-gate assert(OK_32PTR(tmp_tcph));
22770Sstevel@tonic-gate assert(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH);
22780Sstevel@tonic-gate
22790Sstevel@tonic-gate /* Fill in our template header with basic timestamp option. */
22800Sstevel@tonic-gate tmp_tcph += tcp->tcp_tcp_hdr_len;
22810Sstevel@tonic-gate tmp_tcph[0] = TCPOPT_NOP;
22820Sstevel@tonic-gate tmp_tcph[1] = TCPOPT_NOP;
22830Sstevel@tonic-gate tmp_tcph[2] = TCPOPT_TSTAMP;
22840Sstevel@tonic-gate tmp_tcph[3] = TCPOPT_TSTAMP_LEN;
22850Sstevel@tonic-gate tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN;
22860Sstevel@tonic-gate tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN;
22870Sstevel@tonic-gate tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4);
22880Sstevel@tonic-gate } else {
22890Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_FALSE;
22900Sstevel@tonic-gate }
22910Sstevel@tonic-gate
22920Sstevel@tonic-gate /*
22930Sstevel@tonic-gate * Process SACK options. If SACK is enabled for this connection,
22940Sstevel@tonic-gate * then allocate the SACK info structure.
22950Sstevel@tonic-gate */
22960Sstevel@tonic-gate if ((options & TCP_OPT_SACK_OK_PRESENT) &&
22970Sstevel@tonic-gate (tcp->tcp_snd_sack_ok ||
22980Sstevel@tonic-gate (tcp_sack_permitted != 0 && !tcp->tcp_active_open))) {
22990Sstevel@tonic-gate /* This should be true only in the passive case. */
23000Sstevel@tonic-gate if (tcp->tcp_sack_info == NULL) {
23010Sstevel@tonic-gate tcp->tcp_sack_info = (tcp_sack_info_t *)bkmem_zalloc(
23020Sstevel@tonic-gate sizeof (tcp_sack_info_t));
23030Sstevel@tonic-gate }
23040Sstevel@tonic-gate if (tcp->tcp_sack_info == NULL) {
23050Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
23060Sstevel@tonic-gate } else {
23070Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
23080Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
23090Sstevel@tonic-gate tcp->tcp_max_sack_blk = 3;
23100Sstevel@tonic-gate } else {
23110Sstevel@tonic-gate tcp->tcp_max_sack_blk = 4;
23120Sstevel@tonic-gate }
23130Sstevel@tonic-gate }
23140Sstevel@tonic-gate } else {
23150Sstevel@tonic-gate /*
23160Sstevel@tonic-gate * Resetting tcp_snd_sack_ok to B_FALSE so that
23170Sstevel@tonic-gate * no SACK info will be used for this
23180Sstevel@tonic-gate * connection. This assumes that SACK usage
23190Sstevel@tonic-gate * permission is negotiated. This may need
23200Sstevel@tonic-gate * to be changed once this is clarified.
23210Sstevel@tonic-gate */
23220Sstevel@tonic-gate if (tcp->tcp_sack_info != NULL) {
23230Sstevel@tonic-gate bkmem_free((caddr_t)tcp->tcp_sack_info,
23240Sstevel@tonic-gate sizeof (tcp_sack_info_t));
23250Sstevel@tonic-gate tcp->tcp_sack_info = NULL;
23260Sstevel@tonic-gate }
23270Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
23280Sstevel@tonic-gate }
23290Sstevel@tonic-gate
23300Sstevel@tonic-gate /*
23310Sstevel@tonic-gate * Now we know the exact TCP/IP header length, subtract
23320Sstevel@tonic-gate * that from tcp_mss to get our side's MSS.
23330Sstevel@tonic-gate */
23340Sstevel@tonic-gate tcp->tcp_mss -= tcp->tcp_hdr_len;
23350Sstevel@tonic-gate /*
23360Sstevel@tonic-gate * Here we assume that the other side's header size will be equal to
23370Sstevel@tonic-gate * our header size. We calculate the real MSS accordingly. Need to
23380Sstevel@tonic-gate * take into additional stuffs IPsec puts in.
23390Sstevel@tonic-gate *
23400Sstevel@tonic-gate * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header)
23410Sstevel@tonic-gate */
23420Sstevel@tonic-gate tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len -
23430Sstevel@tonic-gate (IP_SIMPLE_HDR_LENGTH + TCP_MIN_HEADER_LENGTH);
23440Sstevel@tonic-gate
23450Sstevel@tonic-gate /*
23460Sstevel@tonic-gate * Set MSS to the smaller one of both ends of the connection.
23470Sstevel@tonic-gate * We should not have called tcp_mss_set() before, but our
23480Sstevel@tonic-gate * side of the MSS should have been set to a proper value
23490Sstevel@tonic-gate * by tcp_adapt_ire(). tcp_mss_set() will also set up the
23500Sstevel@tonic-gate * STREAM head parameters properly.
23510Sstevel@tonic-gate *
23520Sstevel@tonic-gate * If we have a larger-than-16-bit window but the other side
23530Sstevel@tonic-gate * didn't want to do window scale, tcp_rwnd_set() will take
23540Sstevel@tonic-gate * care of that.
23550Sstevel@tonic-gate */
23560Sstevel@tonic-gate tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss));
23570Sstevel@tonic-gate }
23580Sstevel@tonic-gate
23590Sstevel@tonic-gate /*
23600Sstevel@tonic-gate * This function does PAWS protection check. Returns B_TRUE if the
23610Sstevel@tonic-gate * segment passes the PAWS test, else returns B_FALSE.
23620Sstevel@tonic-gate */
23630Sstevel@tonic-gate boolean_t
tcp_paws_check(tcp_t * tcp,tcph_t * tcph,tcp_opt_t * tcpoptp)23640Sstevel@tonic-gate tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp)
23650Sstevel@tonic-gate {
23660Sstevel@tonic-gate uint8_t flags;
23670Sstevel@tonic-gate int options;
23680Sstevel@tonic-gate uint8_t *up;
23690Sstevel@tonic-gate
23700Sstevel@tonic-gate flags = (unsigned int)tcph->th_flags[0] & 0xFF;
23710Sstevel@tonic-gate /*
23720Sstevel@tonic-gate * If timestamp option is aligned nicely, get values inline,
23730Sstevel@tonic-gate * otherwise call general routine to parse. Only do that
23740Sstevel@tonic-gate * if timestamp is the only option.
23750Sstevel@tonic-gate */
23760Sstevel@tonic-gate if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH +
23770Sstevel@tonic-gate TCPOPT_REAL_TS_LEN &&
23780Sstevel@tonic-gate OK_32PTR((up = ((uint8_t *)tcph) +
23790Sstevel@tonic-gate TCP_MIN_HEADER_LENGTH)) &&
23800Sstevel@tonic-gate *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) {
23810Sstevel@tonic-gate tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4));
23820Sstevel@tonic-gate tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8));
23830Sstevel@tonic-gate
23840Sstevel@tonic-gate options = TCP_OPT_TSTAMP_PRESENT;
23850Sstevel@tonic-gate } else {
23860Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
23870Sstevel@tonic-gate tcpoptp->tcp = tcp;
23880Sstevel@tonic-gate } else {
23890Sstevel@tonic-gate tcpoptp->tcp = NULL;
23900Sstevel@tonic-gate }
23910Sstevel@tonic-gate options = tcp_parse_options(tcph, tcpoptp);
23920Sstevel@tonic-gate }
23930Sstevel@tonic-gate
23940Sstevel@tonic-gate if (options & TCP_OPT_TSTAMP_PRESENT) {
23950Sstevel@tonic-gate /*
23960Sstevel@tonic-gate * Do PAWS per RFC 1323 section 4.2. Accept RST
23970Sstevel@tonic-gate * regardless of the timestamp, page 18 RFC 1323.bis.
23980Sstevel@tonic-gate */
23990Sstevel@tonic-gate if ((flags & TH_RST) == 0 &&
24000Sstevel@tonic-gate TSTMP_LT(tcpoptp->tcp_opt_ts_val,
24010Sstevel@tonic-gate tcp->tcp_ts_recent)) {
24020Sstevel@tonic-gate if (TSTMP_LT(prom_gettime(),
24030Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt + PAWS_TIMEOUT)) {
24040Sstevel@tonic-gate /* This segment is not acceptable. */
24050Sstevel@tonic-gate return (B_FALSE);
24060Sstevel@tonic-gate } else {
24070Sstevel@tonic-gate /*
24080Sstevel@tonic-gate * Connection has been idle for
24090Sstevel@tonic-gate * too long. Reset the timestamp
24100Sstevel@tonic-gate * and assume the segment is valid.
24110Sstevel@tonic-gate */
24120Sstevel@tonic-gate tcp->tcp_ts_recent =
24130Sstevel@tonic-gate tcpoptp->tcp_opt_ts_val;
24140Sstevel@tonic-gate }
24150Sstevel@tonic-gate }
24160Sstevel@tonic-gate } else {
24170Sstevel@tonic-gate /*
24180Sstevel@tonic-gate * If we don't get a timestamp on every packet, we
24190Sstevel@tonic-gate * figure we can't really trust 'em, so we stop sending
24200Sstevel@tonic-gate * and parsing them.
24210Sstevel@tonic-gate */
24220Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_FALSE;
24230Sstevel@tonic-gate
24240Sstevel@tonic-gate tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN;
24250Sstevel@tonic-gate tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN;
24260Sstevel@tonic-gate tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4);
24270Sstevel@tonic-gate tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN);
24280Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
24290Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
24300Sstevel@tonic-gate tcp->tcp_max_sack_blk = 4;
24310Sstevel@tonic-gate }
24320Sstevel@tonic-gate }
24330Sstevel@tonic-gate return (B_TRUE);
24340Sstevel@tonic-gate }
24350Sstevel@tonic-gate
24360Sstevel@tonic-gate /*
24370Sstevel@tonic-gate * tcp_get_seg_mp() is called to get the pointer to a segment in the
24380Sstevel@tonic-gate * send queue which starts at the given seq. no.
24390Sstevel@tonic-gate *
24400Sstevel@tonic-gate * Parameters:
24410Sstevel@tonic-gate * tcp_t *tcp: the tcp instance pointer.
24420Sstevel@tonic-gate * uint32_t seq: the starting seq. no of the requested segment.
24430Sstevel@tonic-gate * int32_t *off: after the execution, *off will be the offset to
24440Sstevel@tonic-gate * the returned mblk which points to the requested seq no.
24450Sstevel@tonic-gate *
24460Sstevel@tonic-gate * Return:
24470Sstevel@tonic-gate * A mblk_t pointer pointing to the requested segment in send queue.
24480Sstevel@tonic-gate */
24490Sstevel@tonic-gate static mblk_t *
tcp_get_seg_mp(tcp_t * tcp,uint32_t seq,int32_t * off)24500Sstevel@tonic-gate tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off)
24510Sstevel@tonic-gate {
24520Sstevel@tonic-gate int32_t cnt;
24530Sstevel@tonic-gate mblk_t *mp;
24540Sstevel@tonic-gate
24550Sstevel@tonic-gate /* Defensive coding. Make sure we don't send incorrect data. */
24560Sstevel@tonic-gate if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt) ||
24570Sstevel@tonic-gate off == NULL) {
24580Sstevel@tonic-gate return (NULL);
24590Sstevel@tonic-gate }
24600Sstevel@tonic-gate cnt = seq - tcp->tcp_suna;
24610Sstevel@tonic-gate mp = tcp->tcp_xmit_head;
24620Sstevel@tonic-gate while (cnt > 0 && mp) {
24630Sstevel@tonic-gate cnt -= mp->b_wptr - mp->b_rptr;
24640Sstevel@tonic-gate if (cnt < 0) {
24650Sstevel@tonic-gate cnt += mp->b_wptr - mp->b_rptr;
24660Sstevel@tonic-gate break;
24670Sstevel@tonic-gate }
24680Sstevel@tonic-gate mp = mp->b_cont;
24690Sstevel@tonic-gate }
24700Sstevel@tonic-gate assert(mp != NULL);
24710Sstevel@tonic-gate *off = cnt;
24720Sstevel@tonic-gate return (mp);
24730Sstevel@tonic-gate }
24740Sstevel@tonic-gate
24750Sstevel@tonic-gate /*
24760Sstevel@tonic-gate * This function handles all retransmissions if SACK is enabled for this
24770Sstevel@tonic-gate * connection. First it calculates how many segments can be retransmitted
24780Sstevel@tonic-gate * based on tcp_pipe. Then it goes thru the notsack list to find eligible
24790Sstevel@tonic-gate * segments. A segment is eligible if sack_cnt for that segment is greater
24800Sstevel@tonic-gate * than or equal tcp_dupack_fast_retransmit. After it has retransmitted
24810Sstevel@tonic-gate * all eligible segments, it checks to see if TCP can send some new segments
24820Sstevel@tonic-gate * (fast recovery). If it can, it returns 1. Otherwise it returns 0.
24830Sstevel@tonic-gate *
24840Sstevel@tonic-gate * Parameters:
24850Sstevel@tonic-gate * tcp_t *tcp: the tcp structure of the connection.
24860Sstevel@tonic-gate *
24870Sstevel@tonic-gate * Return:
24880Sstevel@tonic-gate * 1 if the pipe is not full (new data can be sent), 0 otherwise
24890Sstevel@tonic-gate */
24900Sstevel@tonic-gate static int32_t
tcp_sack_rxmit(tcp_t * tcp,int sock_id)24910Sstevel@tonic-gate tcp_sack_rxmit(tcp_t *tcp, int sock_id)
24920Sstevel@tonic-gate {
24930Sstevel@tonic-gate notsack_blk_t *notsack_blk;
24940Sstevel@tonic-gate int32_t usable_swnd;
24950Sstevel@tonic-gate int32_t mss;
24960Sstevel@tonic-gate uint32_t seg_len;
24970Sstevel@tonic-gate mblk_t *xmit_mp;
24980Sstevel@tonic-gate
24990Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
25000Sstevel@tonic-gate assert(tcp->tcp_notsack_list != NULL);
25010Sstevel@tonic-gate assert(tcp->tcp_rexmit == B_FALSE);
25020Sstevel@tonic-gate
25030Sstevel@tonic-gate /* Defensive coding in case there is a bug... */
25040Sstevel@tonic-gate if (tcp->tcp_notsack_list == NULL) {
25050Sstevel@tonic-gate return (0);
25060Sstevel@tonic-gate }
25070Sstevel@tonic-gate notsack_blk = tcp->tcp_notsack_list;
25080Sstevel@tonic-gate mss = tcp->tcp_mss;
25090Sstevel@tonic-gate
25100Sstevel@tonic-gate /*
25110Sstevel@tonic-gate * Limit the num of outstanding data in the network to be
25120Sstevel@tonic-gate * tcp_cwnd_ssthresh, which is half of the original congestion wnd.
25130Sstevel@tonic-gate */
25140Sstevel@tonic-gate usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
25150Sstevel@tonic-gate
25160Sstevel@tonic-gate /* At least retransmit 1 MSS of data. */
25170Sstevel@tonic-gate if (usable_swnd <= 0) {
25180Sstevel@tonic-gate usable_swnd = mss;
25190Sstevel@tonic-gate }
25200Sstevel@tonic-gate
25210Sstevel@tonic-gate /* Make sure no new RTT samples will be taken. */
25220Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
25230Sstevel@tonic-gate
25240Sstevel@tonic-gate notsack_blk = tcp->tcp_notsack_list;
25250Sstevel@tonic-gate while (usable_swnd > 0) {
25260Sstevel@tonic-gate mblk_t *snxt_mp, *tmp_mp;
25270Sstevel@tonic-gate tcp_seq begin = tcp->tcp_sack_snxt;
25280Sstevel@tonic-gate tcp_seq end;
25290Sstevel@tonic-gate int32_t off;
25300Sstevel@tonic-gate
25310Sstevel@tonic-gate for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) {
25320Sstevel@tonic-gate if (SEQ_GT(notsack_blk->end, begin) &&
25330Sstevel@tonic-gate (notsack_blk->sack_cnt >=
25340Sstevel@tonic-gate tcp_dupack_fast_retransmit)) {
25350Sstevel@tonic-gate end = notsack_blk->end;
25360Sstevel@tonic-gate if (SEQ_LT(begin, notsack_blk->begin)) {
25370Sstevel@tonic-gate begin = notsack_blk->begin;
25380Sstevel@tonic-gate }
25390Sstevel@tonic-gate break;
25400Sstevel@tonic-gate }
25410Sstevel@tonic-gate }
25420Sstevel@tonic-gate /*
25430Sstevel@tonic-gate * All holes are filled. Manipulate tcp_cwnd to send more
25440Sstevel@tonic-gate * if we can. Note that after the SACK recovery, tcp_cwnd is
25450Sstevel@tonic-gate * set to tcp_cwnd_ssthresh.
25460Sstevel@tonic-gate */
25470Sstevel@tonic-gate if (notsack_blk == NULL) {
25480Sstevel@tonic-gate usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe;
25490Sstevel@tonic-gate if (usable_swnd <= 0) {
25500Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna;
25510Sstevel@tonic-gate assert(tcp->tcp_cwnd > 0);
25520Sstevel@tonic-gate return (0);
25530Sstevel@tonic-gate } else {
25540Sstevel@tonic-gate usable_swnd = usable_swnd / mss;
25550Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna +
25560Sstevel@tonic-gate MAX(usable_swnd * mss, mss);
25570Sstevel@tonic-gate return (1);
25580Sstevel@tonic-gate }
25590Sstevel@tonic-gate }
25600Sstevel@tonic-gate
25610Sstevel@tonic-gate /*
25620Sstevel@tonic-gate * Note that we may send more than usable_swnd allows here
25630Sstevel@tonic-gate * because of round off, but no more than 1 MSS of data.
25640Sstevel@tonic-gate */
25650Sstevel@tonic-gate seg_len = end - begin;
25660Sstevel@tonic-gate if (seg_len > mss)
25670Sstevel@tonic-gate seg_len = mss;
25680Sstevel@tonic-gate snxt_mp = tcp_get_seg_mp(tcp, begin, &off);
25690Sstevel@tonic-gate assert(snxt_mp != NULL);
25700Sstevel@tonic-gate /* This should not happen. Defensive coding again... */
25710Sstevel@tonic-gate if (snxt_mp == NULL) {
25720Sstevel@tonic-gate return (0);
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate
25750Sstevel@tonic-gate xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off,
25760Sstevel@tonic-gate &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE);
25770Sstevel@tonic-gate
25780Sstevel@tonic-gate if (xmit_mp == NULL)
25790Sstevel@tonic-gate return (0);
25800Sstevel@tonic-gate
25810Sstevel@tonic-gate usable_swnd -= seg_len;
25820Sstevel@tonic-gate tcp->tcp_pipe += seg_len;
25830Sstevel@tonic-gate tcp->tcp_sack_snxt = begin + seg_len;
25840Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_sack_rxmit", xmit_mp);
25850Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, xmit_mp);
25860Sstevel@tonic-gate freeb(xmit_mp);
25870Sstevel@tonic-gate
25880Sstevel@tonic-gate /*
25890Sstevel@tonic-gate * Update the send timestamp to avoid false retransmission.
2590785Seota * Note. use uintptr_t to suppress the gcc warning.
25910Sstevel@tonic-gate */
2592785Seota snxt_mp->b_prev = (mblk_t *)(uintptr_t)prom_gettime();
25930Sstevel@tonic-gate
25940Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
25950Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, seg_len);
25960Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutSackRetransSegs);
25970Sstevel@tonic-gate /*
25980Sstevel@tonic-gate * Update tcp_rexmit_max to extend this SACK recovery phase.
25990Sstevel@tonic-gate * This happens when new data sent during fast recovery is
26000Sstevel@tonic-gate * also lost. If TCP retransmits those new data, it needs
26010Sstevel@tonic-gate * to extend SACK recover phase to avoid starting another
26020Sstevel@tonic-gate * fast retransmit/recovery unnecessarily.
26030Sstevel@tonic-gate */
26040Sstevel@tonic-gate if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) {
26050Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_sack_snxt;
26060Sstevel@tonic-gate }
26070Sstevel@tonic-gate }
26080Sstevel@tonic-gate return (0);
26090Sstevel@tonic-gate }
26100Sstevel@tonic-gate
26110Sstevel@tonic-gate static void
tcp_rput_data(tcp_t * tcp,mblk_t * mp,int sock_id)26120Sstevel@tonic-gate tcp_rput_data(tcp_t *tcp, mblk_t *mp, int sock_id)
26130Sstevel@tonic-gate {
26140Sstevel@tonic-gate uchar_t *rptr;
26150Sstevel@tonic-gate struct ip *iph;
26160Sstevel@tonic-gate tcp_t *tcp1;
26170Sstevel@tonic-gate tcpha_t *tcph;
26180Sstevel@tonic-gate uint32_t seg_ack;
26190Sstevel@tonic-gate int seg_len;
26200Sstevel@tonic-gate uint_t ip_hdr_len;
26210Sstevel@tonic-gate uint32_t seg_seq;
26220Sstevel@tonic-gate mblk_t *mp1;
26230Sstevel@tonic-gate uint_t flags;
26240Sstevel@tonic-gate uint32_t new_swnd = 0;
26250Sstevel@tonic-gate int mss;
26260Sstevel@tonic-gate boolean_t ofo_seg = B_FALSE; /* Out of order segment */
26270Sstevel@tonic-gate int32_t gap;
26280Sstevel@tonic-gate int32_t rgap;
26290Sstevel@tonic-gate tcp_opt_t tcpopt;
26300Sstevel@tonic-gate int32_t bytes_acked;
26310Sstevel@tonic-gate int npkt;
26320Sstevel@tonic-gate uint32_t cwnd;
26330Sstevel@tonic-gate uint32_t add;
26340Sstevel@tonic-gate
26350Sstevel@tonic-gate #ifdef DEBUG
26360Sstevel@tonic-gate printf("tcp_rput_data sock %d mp %x mp_datap %x #################\n",
26370Sstevel@tonic-gate sock_id, mp, mp->b_datap);
26380Sstevel@tonic-gate #endif
26390Sstevel@tonic-gate
26400Sstevel@tonic-gate /* Dump the packet when debugging. */
26410Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data", mp);
26420Sstevel@tonic-gate
26430Sstevel@tonic-gate assert(OK_32PTR(mp->b_rptr));
26440Sstevel@tonic-gate
26450Sstevel@tonic-gate rptr = mp->b_rptr;
26460Sstevel@tonic-gate iph = (struct ip *)rptr;
26470Sstevel@tonic-gate ip_hdr_len = IPH_HDR_LENGTH(rptr);
26480Sstevel@tonic-gate if (ip_hdr_len != IP_SIMPLE_HDR_LENGTH) {
26490Sstevel@tonic-gate #ifdef DEBUG
26500Sstevel@tonic-gate printf("Not simple IP header\n");
26510Sstevel@tonic-gate #endif
26520Sstevel@tonic-gate /* We cannot handle IP option yet... */
26530Sstevel@tonic-gate tcp_drops++;
26540Sstevel@tonic-gate freeb(mp);
26550Sstevel@tonic-gate return;
26560Sstevel@tonic-gate }
26570Sstevel@tonic-gate /* The TCP header must be aligned. */
26580Sstevel@tonic-gate tcph = (tcpha_t *)&rptr[ip_hdr_len];
26590Sstevel@tonic-gate seg_seq = ntohl(tcph->tha_seq);
26600Sstevel@tonic-gate seg_ack = ntohl(tcph->tha_ack);
26610Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX);
26620Sstevel@tonic-gate seg_len = (int)(mp->b_wptr - rptr) -
26630Sstevel@tonic-gate (ip_hdr_len + TCP_HDR_LENGTH(((tcph_t *)tcph)));
26640Sstevel@tonic-gate /* In inetboot, b_cont should always be NULL. */
26650Sstevel@tonic-gate assert(mp->b_cont == NULL);
26660Sstevel@tonic-gate
26670Sstevel@tonic-gate /* Verify the checksum. */
26680Sstevel@tonic-gate if (tcp_verify_cksum(mp) < 0) {
26690Sstevel@tonic-gate #ifdef DEBUG
26700Sstevel@tonic-gate printf("tcp_rput_data: wrong cksum\n");
26710Sstevel@tonic-gate #endif
26720Sstevel@tonic-gate freemsg(mp);
26730Sstevel@tonic-gate return;
26740Sstevel@tonic-gate }
26750Sstevel@tonic-gate
26760Sstevel@tonic-gate /*
26770Sstevel@tonic-gate * This segment is not for us, try to find its
26780Sstevel@tonic-gate * intended receiver.
26790Sstevel@tonic-gate */
26800Sstevel@tonic-gate if (tcp == NULL ||
26810Sstevel@tonic-gate tcph->tha_lport != tcp->tcp_fport ||
26820Sstevel@tonic-gate tcph->tha_fport != tcp->tcp_lport ||
26830Sstevel@tonic-gate iph->ip_src.s_addr != tcp->tcp_remote ||
26840Sstevel@tonic-gate iph->ip_dst.s_addr != tcp->tcp_bound_source) {
26850Sstevel@tonic-gate #ifdef DEBUG
26860Sstevel@tonic-gate printf("tcp_rput_data: not for us, state %d\n",
26870Sstevel@tonic-gate tcp->tcp_state);
26880Sstevel@tonic-gate #endif
26890Sstevel@tonic-gate /*
26900Sstevel@tonic-gate * First try to find a established connection. If none
26910Sstevel@tonic-gate * is found, look for a listener.
26920Sstevel@tonic-gate *
26930Sstevel@tonic-gate * If a listener is found, we need to check to see if the
26940Sstevel@tonic-gate * incoming segment is for one of its eagers. If it is,
26950Sstevel@tonic-gate * give it to the eager. If not, listener should take care
26960Sstevel@tonic-gate * of it.
26970Sstevel@tonic-gate */
26980Sstevel@tonic-gate if ((tcp1 = tcp_lookup_ipv4(iph, tcph, TCPS_SYN_SENT,
26990Sstevel@tonic-gate &sock_id)) != NULL ||
27000Sstevel@tonic-gate (tcp1 = tcp_lookup_listener_ipv4(iph->ip_dst.s_addr,
27010Sstevel@tonic-gate tcph->tha_fport, &sock_id)) != NULL) {
27020Sstevel@tonic-gate if (tcp1->tcp_state == TCPS_LISTEN) {
27030Sstevel@tonic-gate if ((tcp = tcp_lookup_eager_ipv4(tcp1,
27040Sstevel@tonic-gate iph, tcph)) == NULL) {
27050Sstevel@tonic-gate /* No eager... sent to listener */
27060Sstevel@tonic-gate #ifdef DEBUG
27070Sstevel@tonic-gate printf("found the listener: %s\n",
27080Sstevel@tonic-gate tcp_display(tcp1, NULL,
27090Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27100Sstevel@tonic-gate #endif
27110Sstevel@tonic-gate tcp = tcp1;
27120Sstevel@tonic-gate }
27130Sstevel@tonic-gate #ifdef DEBUG
27140Sstevel@tonic-gate else {
27150Sstevel@tonic-gate printf("found the eager: %s\n",
27160Sstevel@tonic-gate tcp_display(tcp, NULL,
27170Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27180Sstevel@tonic-gate }
27190Sstevel@tonic-gate #endif
27200Sstevel@tonic-gate } else {
27210Sstevel@tonic-gate /* Non listener found... */
27220Sstevel@tonic-gate #ifdef DEBUG
27230Sstevel@tonic-gate printf("found the connection: %s\n",
27240Sstevel@tonic-gate tcp_display(tcp1, NULL,
27250Sstevel@tonic-gate DISP_ADDR_AND_PORT));
27260Sstevel@tonic-gate #endif
27270Sstevel@tonic-gate tcp = tcp1;
27280Sstevel@tonic-gate }
27290Sstevel@tonic-gate } else {
27300Sstevel@tonic-gate /*
27310Sstevel@tonic-gate * No connection for this segment...
27320Sstevel@tonic-gate * Send a RST to the other side.
27330Sstevel@tonic-gate */
27340Sstevel@tonic-gate tcp_xmit_listeners_reset(sock_id, mp, ip_hdr_len);
27350Sstevel@tonic-gate return;
27360Sstevel@tonic-gate }
27370Sstevel@tonic-gate }
27380Sstevel@tonic-gate
27390Sstevel@tonic-gate flags = tcph->tha_flags & 0xFF;
27400Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInSegs);
27410Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT) {
27420Sstevel@tonic-gate tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack,
27430Sstevel@tonic-gate seg_len, (tcph_t *)tcph, sock_id);
27440Sstevel@tonic-gate return;
27450Sstevel@tonic-gate }
27460Sstevel@tonic-gate /*
27470Sstevel@tonic-gate * From this point we can assume that the tcp is not compressed,
27480Sstevel@tonic-gate * since we would have branched off to tcp_time_wait_processing()
27490Sstevel@tonic-gate * in such a case.
27500Sstevel@tonic-gate */
27510Sstevel@tonic-gate assert(tcp != NULL && tcp->tcp_state != TCPS_TIME_WAIT);
27520Sstevel@tonic-gate
27530Sstevel@tonic-gate /*
27540Sstevel@tonic-gate * After this point, we know we have the correct TCP, so update
27550Sstevel@tonic-gate * the receive time.
27560Sstevel@tonic-gate */
27570Sstevel@tonic-gate tcp->tcp_last_recv_time = prom_gettime();
27580Sstevel@tonic-gate
27590Sstevel@tonic-gate /* In inetboot, we do not handle urgent pointer... */
27600Sstevel@tonic-gate if (flags & TH_URG) {
27610Sstevel@tonic-gate freemsg(mp);
27620Sstevel@tonic-gate DEBUG_1("tcp_rput_data(%d): received segment with urgent "
27630Sstevel@tonic-gate "pointer\n", sock_id);
27640Sstevel@tonic-gate tcp_drops++;
27650Sstevel@tonic-gate return;
27660Sstevel@tonic-gate }
27670Sstevel@tonic-gate
27680Sstevel@tonic-gate switch (tcp->tcp_state) {
27690Sstevel@tonic-gate case TCPS_LISTEN:
27700Sstevel@tonic-gate if ((flags & (TH_RST | TH_ACK | TH_SYN)) != TH_SYN) {
27710Sstevel@tonic-gate if (flags & TH_RST) {
27720Sstevel@tonic-gate freemsg(mp);
27730Sstevel@tonic-gate return;
27740Sstevel@tonic-gate }
27750Sstevel@tonic-gate if (flags & TH_ACK) {
27760Sstevel@tonic-gate tcp_xmit_early_reset("TCPS_LISTEN-TH_ACK",
27770Sstevel@tonic-gate sock_id, mp, seg_ack, 0, TH_RST,
27780Sstevel@tonic-gate ip_hdr_len);
27790Sstevel@tonic-gate return;
27800Sstevel@tonic-gate }
27810Sstevel@tonic-gate if (!(flags & TH_SYN)) {
27820Sstevel@tonic-gate freemsg(mp);
27830Sstevel@tonic-gate return;
27840Sstevel@tonic-gate }
27850Sstevel@tonic-gate printf("tcp_rput_data: %d\n", __LINE__);
27860Sstevel@tonic-gate prom_panic("inetboot");
27870Sstevel@tonic-gate }
27880Sstevel@tonic-gate if (tcp->tcp_conn_req_max > 0) {
27890Sstevel@tonic-gate tcp = tcp_conn_request(tcp, mp, sock_id, ip_hdr_len);
27900Sstevel@tonic-gate if (tcp == NULL) {
27910Sstevel@tonic-gate freemsg(mp);
27920Sstevel@tonic-gate return;
27930Sstevel@tonic-gate }
27940Sstevel@tonic-gate #ifdef DEBUG
27950Sstevel@tonic-gate printf("tcp_rput_data: new tcp created\n");
27960Sstevel@tonic-gate #endif
27970Sstevel@tonic-gate }
27980Sstevel@tonic-gate tcp->tcp_irs = seg_seq;
27990Sstevel@tonic-gate tcp->tcp_rack = seg_seq;
28000Sstevel@tonic-gate tcp->tcp_rnxt = seg_seq + 1;
28010Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
28020Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpPassiveOpens);
28030Sstevel@tonic-gate goto syn_rcvd;
28040Sstevel@tonic-gate case TCPS_SYN_SENT:
28050Sstevel@tonic-gate if (flags & TH_ACK) {
28060Sstevel@tonic-gate /*
28070Sstevel@tonic-gate * Note that our stack cannot send data before a
28080Sstevel@tonic-gate * connection is established, therefore the
28090Sstevel@tonic-gate * following check is valid. Otherwise, it has
28100Sstevel@tonic-gate * to be changed.
28110Sstevel@tonic-gate */
28120Sstevel@tonic-gate if (SEQ_LEQ(seg_ack, tcp->tcp_iss) ||
28130Sstevel@tonic-gate SEQ_GT(seg_ack, tcp->tcp_snxt)) {
28140Sstevel@tonic-gate if (flags & TH_RST) {
28150Sstevel@tonic-gate freemsg(mp);
28160Sstevel@tonic-gate return;
28170Sstevel@tonic-gate }
28180Sstevel@tonic-gate tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq",
28190Sstevel@tonic-gate tcp, mp, seg_ack, 0, TH_RST,
28200Sstevel@tonic-gate ip_hdr_len, sock_id);
28210Sstevel@tonic-gate return;
28220Sstevel@tonic-gate }
28230Sstevel@tonic-gate assert(tcp->tcp_suna + 1 == seg_ack);
28240Sstevel@tonic-gate }
28250Sstevel@tonic-gate if (flags & TH_RST) {
28260Sstevel@tonic-gate freemsg(mp);
28270Sstevel@tonic-gate if (flags & TH_ACK) {
28280Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, ECONNREFUSED);
28290Sstevel@tonic-gate }
28300Sstevel@tonic-gate return;
28310Sstevel@tonic-gate }
28320Sstevel@tonic-gate if (!(flags & TH_SYN)) {
28330Sstevel@tonic-gate freemsg(mp);
28340Sstevel@tonic-gate return;
28350Sstevel@tonic-gate }
28360Sstevel@tonic-gate
28370Sstevel@tonic-gate /* Process all TCP options. */
28380Sstevel@tonic-gate tcp_process_options(tcp, (tcph_t *)tcph);
28390Sstevel@tonic-gate /*
28400Sstevel@tonic-gate * The following changes our rwnd to be a multiple of the
28410Sstevel@tonic-gate * MIN(peer MSS, our MSS) for performance reason.
28420Sstevel@tonic-gate */
28430Sstevel@tonic-gate (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rwnd,
28440Sstevel@tonic-gate tcp->tcp_mss));
28450Sstevel@tonic-gate
28460Sstevel@tonic-gate /* Is the other end ECN capable? */
28470Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
28480Sstevel@tonic-gate if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) {
28490Sstevel@tonic-gate tcp->tcp_ecn_ok = B_FALSE;
28500Sstevel@tonic-gate }
28510Sstevel@tonic-gate }
28520Sstevel@tonic-gate /*
28530Sstevel@tonic-gate * Clear ECN flags because it may interfere with later
28540Sstevel@tonic-gate * processing.
28550Sstevel@tonic-gate */
28560Sstevel@tonic-gate flags &= ~(TH_ECE|TH_CWR);
28570Sstevel@tonic-gate
28580Sstevel@tonic-gate tcp->tcp_irs = seg_seq;
28590Sstevel@tonic-gate tcp->tcp_rack = seg_seq;
28600Sstevel@tonic-gate tcp->tcp_rnxt = seg_seq + 1;
28610Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
28620Sstevel@tonic-gate
28630Sstevel@tonic-gate if (flags & TH_ACK) {
28640Sstevel@tonic-gate /* One for the SYN */
28650Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss + 1;
28660Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
28670Sstevel@tonic-gate tcp->tcp_state = TCPS_ESTABLISHED;
28680Sstevel@tonic-gate
28690Sstevel@tonic-gate /*
28700Sstevel@tonic-gate * If SYN was retransmitted, need to reset all
28710Sstevel@tonic-gate * retransmission info. This is because this
28720Sstevel@tonic-gate * segment will be treated as a dup ACK.
28730Sstevel@tonic-gate */
28740Sstevel@tonic-gate if (tcp->tcp_rexmit) {
28750Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
28760Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
28770Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
28780Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
28790Sstevel@tonic-gate
28800Sstevel@tonic-gate /*
28810Sstevel@tonic-gate * Set tcp_cwnd back to 1 MSS, per
28820Sstevel@tonic-gate * recommendation from
28830Sstevel@tonic-gate * draft-floyd-incr-init-win-01.txt,
28840Sstevel@tonic-gate * Increasing TCP's Initial Window.
28850Sstevel@tonic-gate */
28860Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
28870Sstevel@tonic-gate }
28880Sstevel@tonic-gate
28890Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
28900Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
28910Sstevel@tonic-gate
28920Sstevel@tonic-gate new_swnd = BE16_TO_U16(((tcph_t *)tcph)->th_win);
28930Sstevel@tonic-gate tcp->tcp_swnd = new_swnd;
28940Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
28950Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
28960Sstevel@tonic-gate
28970Sstevel@tonic-gate /*
28980Sstevel@tonic-gate * Always send the three-way handshake ack immediately
28990Sstevel@tonic-gate * in order to make the connection complete as soon as
29000Sstevel@tonic-gate * possible on the accepting host.
29010Sstevel@tonic-gate */
29020Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
29030Sstevel@tonic-gate /*
29040Sstevel@tonic-gate * Check to see if there is data to be sent. If
29050Sstevel@tonic-gate * yes, set the transmit flag. Then check to see
29060Sstevel@tonic-gate * if received data processing needs to be done.
29070Sstevel@tonic-gate * If not, go straight to xmit_check. This short
29080Sstevel@tonic-gate * cut is OK as we don't support T/TCP.
29090Sstevel@tonic-gate */
29100Sstevel@tonic-gate if (tcp->tcp_unsent)
29110Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
29120Sstevel@tonic-gate
29130Sstevel@tonic-gate if (seg_len == 0) {
29140Sstevel@tonic-gate freemsg(mp);
29150Sstevel@tonic-gate goto xmit_check;
29160Sstevel@tonic-gate }
29170Sstevel@tonic-gate
29180Sstevel@tonic-gate flags &= ~TH_SYN;
29190Sstevel@tonic-gate seg_seq++;
29200Sstevel@tonic-gate break;
29210Sstevel@tonic-gate }
29220Sstevel@tonic-gate syn_rcvd:
29230Sstevel@tonic-gate tcp->tcp_state = TCPS_SYN_RCVD;
29240Sstevel@tonic-gate mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss,
29250Sstevel@tonic-gate NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
29260Sstevel@tonic-gate if (mp1 != NULL) {
29270Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data replying SYN", mp1);
29280Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
29290Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
29300Sstevel@tonic-gate freeb(mp1);
29310Sstevel@tonic-gate /*
29320Sstevel@tonic-gate * Let's wait till our SYN has been ACKED since we
29330Sstevel@tonic-gate * don't have a timer.
29340Sstevel@tonic-gate */
29350Sstevel@tonic-gate if (tcp_state_wait(sock_id, tcp, TCPS_ALL_ACKED) < 0) {
29360Sstevel@tonic-gate freemsg(mp);
29370Sstevel@tonic-gate return;
29380Sstevel@tonic-gate }
29390Sstevel@tonic-gate }
29400Sstevel@tonic-gate freemsg(mp);
29410Sstevel@tonic-gate return;
29420Sstevel@tonic-gate default:
29430Sstevel@tonic-gate break;
29440Sstevel@tonic-gate }
29450Sstevel@tonic-gate mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH((tcph_t *)tcph);
29460Sstevel@tonic-gate new_swnd = ntohs(tcph->tha_win) <<
29470Sstevel@tonic-gate ((flags & TH_SYN) ? 0 : tcp->tcp_snd_ws);
29480Sstevel@tonic-gate mss = tcp->tcp_mss;
29490Sstevel@tonic-gate
29500Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
29510Sstevel@tonic-gate if (!tcp_paws_check(tcp, (tcph_t *)tcph, &tcpopt)) {
29520Sstevel@tonic-gate /*
29530Sstevel@tonic-gate * This segment is not acceptable.
29540Sstevel@tonic-gate * Drop it and send back an ACK.
29550Sstevel@tonic-gate */
29560Sstevel@tonic-gate freemsg(mp);
29570Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
29580Sstevel@tonic-gate goto ack_check;
29590Sstevel@tonic-gate }
29600Sstevel@tonic-gate } else if (tcp->tcp_snd_sack_ok) {
29610Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
29620Sstevel@tonic-gate tcpopt.tcp = tcp;
29630Sstevel@tonic-gate /*
29640Sstevel@tonic-gate * SACK info in already updated in tcp_parse_options. Ignore
29650Sstevel@tonic-gate * all other TCP options...
29660Sstevel@tonic-gate */
29670Sstevel@tonic-gate (void) tcp_parse_options((tcph_t *)tcph, &tcpopt);
29680Sstevel@tonic-gate }
29690Sstevel@tonic-gate try_again:;
29700Sstevel@tonic-gate gap = seg_seq - tcp->tcp_rnxt;
29710Sstevel@tonic-gate rgap = tcp->tcp_rwnd - (gap + seg_len);
29720Sstevel@tonic-gate /*
29730Sstevel@tonic-gate * gap is the amount of sequence space between what we expect to see
29740Sstevel@tonic-gate * and what we got for seg_seq. A positive value for gap means
29750Sstevel@tonic-gate * something got lost. A negative value means we got some old stuff.
29760Sstevel@tonic-gate */
29770Sstevel@tonic-gate if (gap < 0) {
29780Sstevel@tonic-gate /* Old stuff present. Is the SYN in there? */
29790Sstevel@tonic-gate if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) &&
29800Sstevel@tonic-gate (seg_len != 0)) {
29810Sstevel@tonic-gate flags &= ~TH_SYN;
29820Sstevel@tonic-gate seg_seq++;
29830Sstevel@tonic-gate /* Recompute the gaps after noting the SYN. */
29840Sstevel@tonic-gate goto try_again;
29850Sstevel@tonic-gate }
29860Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
29870Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes,
29880Sstevel@tonic-gate (seg_len > -gap ? -gap : seg_len));
29890Sstevel@tonic-gate /* Remove the old stuff from seg_len. */
29900Sstevel@tonic-gate seg_len += gap;
29910Sstevel@tonic-gate /*
29920Sstevel@tonic-gate * Anything left?
29930Sstevel@tonic-gate * Make sure to check for unack'd FIN when rest of data
29940Sstevel@tonic-gate * has been previously ack'd.
29950Sstevel@tonic-gate */
29960Sstevel@tonic-gate if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
29970Sstevel@tonic-gate /*
29980Sstevel@tonic-gate * Resets are only valid if they lie within our offered
29990Sstevel@tonic-gate * window. If the RST bit is set, we just ignore this
30000Sstevel@tonic-gate * segment.
30010Sstevel@tonic-gate */
30020Sstevel@tonic-gate if (flags & TH_RST) {
30030Sstevel@tonic-gate freemsg(mp);
30040Sstevel@tonic-gate return;
30050Sstevel@tonic-gate }
30060Sstevel@tonic-gate
30070Sstevel@tonic-gate /*
30080Sstevel@tonic-gate * This segment is "unacceptable". None of its
30090Sstevel@tonic-gate * sequence space lies within our advertized window.
30100Sstevel@tonic-gate *
30110Sstevel@tonic-gate * Adjust seg_len to the original value for tracing.
30120Sstevel@tonic-gate */
30130Sstevel@tonic-gate seg_len -= gap;
30140Sstevel@tonic-gate #ifdef DEBUG
30150Sstevel@tonic-gate printf("tcp_rput: unacceptable, gap %d, rgap "
30160Sstevel@tonic-gate "%d, flags 0x%x, seg_seq %u, seg_ack %u, "
30170Sstevel@tonic-gate "seg_len %d, rnxt %u, snxt %u, %s",
30180Sstevel@tonic-gate gap, rgap, flags, seg_seq, seg_ack,
30190Sstevel@tonic-gate seg_len, tcp->tcp_rnxt, tcp->tcp_snxt,
30200Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
30210Sstevel@tonic-gate #endif
30220Sstevel@tonic-gate
30230Sstevel@tonic-gate /*
30240Sstevel@tonic-gate * Arrange to send an ACK in response to the
30250Sstevel@tonic-gate * unacceptable segment per RFC 793 page 69. There
30260Sstevel@tonic-gate * is only one small difference between ours and the
30270Sstevel@tonic-gate * acceptability test in the RFC - we accept ACK-only
30280Sstevel@tonic-gate * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK
30290Sstevel@tonic-gate * will be generated.
30300Sstevel@tonic-gate *
30310Sstevel@tonic-gate * Note that we have to ACK an ACK-only packet at least
30320Sstevel@tonic-gate * for stacks that send 0-length keep-alives with
30330Sstevel@tonic-gate * SEG.SEQ = SND.NXT-1 as recommended by RFC1122,
30340Sstevel@tonic-gate * section 4.2.3.6. As long as we don't ever generate
30350Sstevel@tonic-gate * an unacceptable packet in response to an incoming
30360Sstevel@tonic-gate * packet that is unacceptable, it should not cause
30370Sstevel@tonic-gate * "ACK wars".
30380Sstevel@tonic-gate */
30390Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
30400Sstevel@tonic-gate
30410Sstevel@tonic-gate /*
30420Sstevel@tonic-gate * Continue processing this segment in order to use the
30430Sstevel@tonic-gate * ACK information it contains, but skip all other
30440Sstevel@tonic-gate * sequence-number processing. Processing the ACK
30450Sstevel@tonic-gate * information is necessary in order to
30460Sstevel@tonic-gate * re-synchronize connections that may have lost
30470Sstevel@tonic-gate * synchronization.
30480Sstevel@tonic-gate *
30490Sstevel@tonic-gate * We clear seg_len and flag fields related to
30500Sstevel@tonic-gate * sequence number processing as they are not
30510Sstevel@tonic-gate * to be trusted for an unacceptable segment.
30520Sstevel@tonic-gate */
30530Sstevel@tonic-gate seg_len = 0;
30540Sstevel@tonic-gate flags &= ~(TH_SYN | TH_FIN | TH_URG);
30550Sstevel@tonic-gate goto process_ack;
30560Sstevel@tonic-gate }
30570Sstevel@tonic-gate
30580Sstevel@tonic-gate /* Fix seg_seq, and chew the gap off the front. */
30590Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
30600Sstevel@tonic-gate do {
30610Sstevel@tonic-gate mblk_t *mp2;
30620Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
30630Sstevel@tonic-gate (uintptr_t)UINT_MAX);
30640Sstevel@tonic-gate gap += (uint_t)(mp->b_wptr - mp->b_rptr);
30650Sstevel@tonic-gate if (gap > 0) {
30660Sstevel@tonic-gate mp->b_rptr = mp->b_wptr - gap;
30670Sstevel@tonic-gate break;
30680Sstevel@tonic-gate }
30690Sstevel@tonic-gate mp2 = mp;
30700Sstevel@tonic-gate mp = mp->b_cont;
30710Sstevel@tonic-gate freeb(mp2);
30720Sstevel@tonic-gate } while (gap < 0);
30730Sstevel@tonic-gate }
30740Sstevel@tonic-gate /*
30750Sstevel@tonic-gate * rgap is the amount of stuff received out of window. A negative
30760Sstevel@tonic-gate * value is the amount out of window.
30770Sstevel@tonic-gate */
30780Sstevel@tonic-gate if (rgap < 0) {
30790Sstevel@tonic-gate mblk_t *mp2;
30800Sstevel@tonic-gate
30810Sstevel@tonic-gate if (tcp->tcp_rwnd == 0)
30820Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInWinProbe);
30830Sstevel@tonic-gate else {
30840Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPastWinSegs);
30850Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPastWinBytes, -rgap);
30860Sstevel@tonic-gate }
30870Sstevel@tonic-gate
30880Sstevel@tonic-gate /*
30890Sstevel@tonic-gate * seg_len does not include the FIN, so if more than
30900Sstevel@tonic-gate * just the FIN is out of window, we act like we don't
30910Sstevel@tonic-gate * see it. (If just the FIN is out of window, rgap
30920Sstevel@tonic-gate * will be zero and we will go ahead and acknowledge
30930Sstevel@tonic-gate * the FIN.)
30940Sstevel@tonic-gate */
30950Sstevel@tonic-gate flags &= ~TH_FIN;
30960Sstevel@tonic-gate
30970Sstevel@tonic-gate /* Fix seg_len and make sure there is something left. */
30980Sstevel@tonic-gate seg_len += rgap;
30990Sstevel@tonic-gate if (seg_len <= 0) {
31000Sstevel@tonic-gate /*
31010Sstevel@tonic-gate * Resets are only valid if they lie within our offered
31020Sstevel@tonic-gate * window. If the RST bit is set, we just ignore this
31030Sstevel@tonic-gate * segment.
31040Sstevel@tonic-gate */
31050Sstevel@tonic-gate if (flags & TH_RST) {
31060Sstevel@tonic-gate freemsg(mp);
31070Sstevel@tonic-gate return;
31080Sstevel@tonic-gate }
31090Sstevel@tonic-gate
31100Sstevel@tonic-gate /* Per RFC 793, we need to send back an ACK. */
31110Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
31120Sstevel@tonic-gate
31130Sstevel@tonic-gate /*
31140Sstevel@tonic-gate * If this is a zero window probe, continue to
31150Sstevel@tonic-gate * process the ACK part. But we need to set seg_len
31160Sstevel@tonic-gate * to 0 to avoid data processing. Otherwise just
31170Sstevel@tonic-gate * drop the segment and send back an ACK.
31180Sstevel@tonic-gate */
31190Sstevel@tonic-gate if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) {
31200Sstevel@tonic-gate flags &= ~(TH_SYN | TH_URG);
31210Sstevel@tonic-gate seg_len = 0;
31220Sstevel@tonic-gate /* Let's see if we can update our rwnd */
31230Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
31240Sstevel@tonic-gate goto process_ack;
31250Sstevel@tonic-gate } else {
31260Sstevel@tonic-gate freemsg(mp);
31270Sstevel@tonic-gate goto ack_check;
31280Sstevel@tonic-gate }
31290Sstevel@tonic-gate }
31300Sstevel@tonic-gate /* Pitch out of window stuff off the end. */
31310Sstevel@tonic-gate rgap = seg_len;
31320Sstevel@tonic-gate mp2 = mp;
31330Sstevel@tonic-gate do {
31340Sstevel@tonic-gate assert((uintptr_t)(mp2->b_wptr -
31350Sstevel@tonic-gate mp2->b_rptr) <= (uintptr_t)INT_MAX);
31360Sstevel@tonic-gate rgap -= (int)(mp2->b_wptr - mp2->b_rptr);
31370Sstevel@tonic-gate if (rgap < 0) {
31380Sstevel@tonic-gate mp2->b_wptr += rgap;
31390Sstevel@tonic-gate if ((mp1 = mp2->b_cont) != NULL) {
31400Sstevel@tonic-gate mp2->b_cont = NULL;
31410Sstevel@tonic-gate freemsg(mp1);
31420Sstevel@tonic-gate }
31430Sstevel@tonic-gate break;
31440Sstevel@tonic-gate }
31450Sstevel@tonic-gate } while ((mp2 = mp2->b_cont) != NULL);
31460Sstevel@tonic-gate }
31470Sstevel@tonic-gate ok:;
31480Sstevel@tonic-gate /*
31490Sstevel@tonic-gate * TCP should check ECN info for segments inside the window only.
31500Sstevel@tonic-gate * Therefore the check should be done here.
31510Sstevel@tonic-gate */
31520Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
31530Sstevel@tonic-gate uchar_t tos = ((struct ip *)rptr)->ip_tos;
31540Sstevel@tonic-gate
31550Sstevel@tonic-gate if (flags & TH_CWR) {
31560Sstevel@tonic-gate tcp->tcp_ecn_echo_on = B_FALSE;
31570Sstevel@tonic-gate }
31580Sstevel@tonic-gate /*
31590Sstevel@tonic-gate * Note that both ECN_CE and CWR can be set in the
31600Sstevel@tonic-gate * same segment. In this case, we once again turn
31610Sstevel@tonic-gate * on ECN_ECHO.
31620Sstevel@tonic-gate */
31630Sstevel@tonic-gate if ((tos & IPH_ECN_CE) == IPH_ECN_CE) {
31640Sstevel@tonic-gate tcp->tcp_ecn_echo_on = B_TRUE;
31650Sstevel@tonic-gate }
31660Sstevel@tonic-gate }
31670Sstevel@tonic-gate
31680Sstevel@tonic-gate /*
31690Sstevel@tonic-gate * Check whether we can update tcp_ts_recent. This test is
31700Sstevel@tonic-gate * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
31710Sstevel@tonic-gate * Extensions for High Performance: An Update", Internet Draft.
31720Sstevel@tonic-gate */
31730Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok &&
31740Sstevel@tonic-gate TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
31750Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
31760Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
31770Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
31780Sstevel@tonic-gate }
31790Sstevel@tonic-gate
31800Sstevel@tonic-gate if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) {
31810Sstevel@tonic-gate /*
31820Sstevel@tonic-gate * FIN in an out of order segment. We record this in
31830Sstevel@tonic-gate * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq.
31840Sstevel@tonic-gate * Clear the FIN so that any check on FIN flag will fail.
31850Sstevel@tonic-gate * Remember that FIN also counts in the sequence number
31860Sstevel@tonic-gate * space. So we need to ack out of order FIN only segments.
31870Sstevel@tonic-gate */
31880Sstevel@tonic-gate if (flags & TH_FIN) {
31890Sstevel@tonic-gate tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID;
31900Sstevel@tonic-gate tcp->tcp_ofo_fin_seq = seg_seq + seg_len;
31910Sstevel@tonic-gate flags &= ~TH_FIN;
31920Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
31930Sstevel@tonic-gate }
31940Sstevel@tonic-gate if (seg_len > 0) {
31950Sstevel@tonic-gate /* Fill in the SACK blk list. */
31960Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
31970Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
31980Sstevel@tonic-gate tcp_sack_insert(tcp->tcp_sack_list,
31990Sstevel@tonic-gate seg_seq, seg_seq + seg_len,
32000Sstevel@tonic-gate &(tcp->tcp_num_sack_blk));
32010Sstevel@tonic-gate }
32020Sstevel@tonic-gate
32030Sstevel@tonic-gate /*
32040Sstevel@tonic-gate * Attempt reassembly and see if we have something
32050Sstevel@tonic-gate * ready to go.
32060Sstevel@tonic-gate */
32070Sstevel@tonic-gate mp = tcp_reass(tcp, mp, seg_seq);
32080Sstevel@tonic-gate /* Always ack out of order packets */
32090Sstevel@tonic-gate flags |= TH_ACK_NEEDED | TH_PUSH;
32100Sstevel@tonic-gate if (mp != NULL) {
32110Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr -
32120Sstevel@tonic-gate mp->b_rptr) <= (uintptr_t)INT_MAX);
32130Sstevel@tonic-gate seg_len = mp->b_cont ? msgdsize(mp) :
32140Sstevel@tonic-gate (int)(mp->b_wptr - mp->b_rptr);
32150Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
32160Sstevel@tonic-gate /*
32170Sstevel@tonic-gate * A gap is filled and the seq num and len
32180Sstevel@tonic-gate * of the gap match that of a previously
32190Sstevel@tonic-gate * received FIN, put the FIN flag back in.
32200Sstevel@tonic-gate */
32210Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
32220Sstevel@tonic-gate seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
32230Sstevel@tonic-gate flags |= TH_FIN;
32240Sstevel@tonic-gate tcp->tcp_valid_bits &=
32250Sstevel@tonic-gate ~TCP_OFO_FIN_VALID;
32260Sstevel@tonic-gate }
32270Sstevel@tonic-gate } else {
32280Sstevel@tonic-gate /*
32290Sstevel@tonic-gate * Keep going even with NULL mp.
32300Sstevel@tonic-gate * There may be a useful ACK or something else
32310Sstevel@tonic-gate * we don't want to miss.
32320Sstevel@tonic-gate *
32330Sstevel@tonic-gate * But TCP should not perform fast retransmit
32340Sstevel@tonic-gate * because of the ack number. TCP uses
32350Sstevel@tonic-gate * seg_len == 0 to determine if it is a pure
32360Sstevel@tonic-gate * ACK. And this is not a pure ACK.
32370Sstevel@tonic-gate */
32380Sstevel@tonic-gate seg_len = 0;
32390Sstevel@tonic-gate ofo_seg = B_TRUE;
32400Sstevel@tonic-gate }
32410Sstevel@tonic-gate }
32420Sstevel@tonic-gate } else if (seg_len > 0) {
32430Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataInorderSegs);
32440Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataInorderBytes, seg_len);
32450Sstevel@tonic-gate /*
32460Sstevel@tonic-gate * If an out of order FIN was received before, and the seq
32470Sstevel@tonic-gate * num and len of the new segment match that of the FIN,
32480Sstevel@tonic-gate * put the FIN flag back in.
32490Sstevel@tonic-gate */
32500Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) &&
32510Sstevel@tonic-gate seg_seq + seg_len == tcp->tcp_ofo_fin_seq) {
32520Sstevel@tonic-gate flags |= TH_FIN;
32530Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID;
32540Sstevel@tonic-gate }
32550Sstevel@tonic-gate }
32560Sstevel@tonic-gate if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) {
32570Sstevel@tonic-gate if (flags & TH_RST) {
32580Sstevel@tonic-gate freemsg(mp);
32590Sstevel@tonic-gate switch (tcp->tcp_state) {
32600Sstevel@tonic-gate case TCPS_SYN_RCVD:
32610Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNREFUSED);
32620Sstevel@tonic-gate break;
32630Sstevel@tonic-gate case TCPS_ESTABLISHED:
32640Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
32650Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
32660Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
32670Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNRESET);
32680Sstevel@tonic-gate break;
32690Sstevel@tonic-gate case TCPS_CLOSING:
32700Sstevel@tonic-gate case TCPS_LAST_ACK:
32710Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
32720Sstevel@tonic-gate break;
32730Sstevel@tonic-gate default:
32740Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
32750Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ENXIO);
32760Sstevel@tonic-gate break;
32770Sstevel@tonic-gate }
32780Sstevel@tonic-gate return;
32790Sstevel@tonic-gate }
32800Sstevel@tonic-gate if (flags & TH_SYN) {
32810Sstevel@tonic-gate /*
32820Sstevel@tonic-gate * See RFC 793, Page 71
32830Sstevel@tonic-gate *
32840Sstevel@tonic-gate * The seq number must be in the window as it should
32850Sstevel@tonic-gate * be "fixed" above. If it is outside window, it should
32860Sstevel@tonic-gate * be already rejected. Note that we allow seg_seq to be
32870Sstevel@tonic-gate * rnxt + rwnd because we want to accept 0 window probe.
32880Sstevel@tonic-gate */
32890Sstevel@tonic-gate assert(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) &&
32900Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd));
32910Sstevel@tonic-gate freemsg(mp);
32920Sstevel@tonic-gate /*
32930Sstevel@tonic-gate * If the ACK flag is not set, just use our snxt as the
32940Sstevel@tonic-gate * seq number of the RST segment.
32950Sstevel@tonic-gate */
32960Sstevel@tonic-gate if (!(flags & TH_ACK)) {
32970Sstevel@tonic-gate seg_ack = tcp->tcp_snxt;
32980Sstevel@tonic-gate }
32990Sstevel@tonic-gate tcp_xmit_ctl("TH_SYN", tcp, NULL, seg_ack,
33000Sstevel@tonic-gate seg_seq + 1, TH_RST|TH_ACK, 0, sock_id);
33010Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
33020Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, ECONNRESET);
33030Sstevel@tonic-gate return;
33040Sstevel@tonic-gate }
33050Sstevel@tonic-gate
33060Sstevel@tonic-gate process_ack:
33070Sstevel@tonic-gate if (!(flags & TH_ACK)) {
33080Sstevel@tonic-gate #ifdef DEBUG
33090Sstevel@tonic-gate printf("No ack in segment, dropped it, seq:%x\n", seg_seq);
33100Sstevel@tonic-gate #endif
33110Sstevel@tonic-gate freemsg(mp);
33120Sstevel@tonic-gate goto xmit_check;
33130Sstevel@tonic-gate }
33140Sstevel@tonic-gate }
33150Sstevel@tonic-gate bytes_acked = (int)(seg_ack - tcp->tcp_suna);
33160Sstevel@tonic-gate
33170Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_RCVD) {
33180Sstevel@tonic-gate tcp_t *listener = tcp->tcp_listener;
33190Sstevel@tonic-gate #ifdef DEBUG
33200Sstevel@tonic-gate printf("Done with eager 3-way handshake\n");
33210Sstevel@tonic-gate #endif
33220Sstevel@tonic-gate /*
33230Sstevel@tonic-gate * NOTE: RFC 793 pg. 72 says this should be 'bytes_acked < 0'
33240Sstevel@tonic-gate * but that would mean we have an ack that ignored our SYN.
33250Sstevel@tonic-gate */
33260Sstevel@tonic-gate if (bytes_acked < 1 || SEQ_GT(seg_ack, tcp->tcp_snxt)) {
33270Sstevel@tonic-gate freemsg(mp);
33280Sstevel@tonic-gate tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack",
33290Sstevel@tonic-gate tcp, NULL, seg_ack, 0, TH_RST, 0, sock_id);
33300Sstevel@tonic-gate return;
33310Sstevel@tonic-gate }
33320Sstevel@tonic-gate
33330Sstevel@tonic-gate /*
33340Sstevel@tonic-gate * if the conn_req_q is full defer processing
33350Sstevel@tonic-gate * until space is availabe after accept()
33360Sstevel@tonic-gate * processing
33370Sstevel@tonic-gate */
33380Sstevel@tonic-gate if (listener->tcp_conn_req_cnt_q <
33390Sstevel@tonic-gate listener->tcp_conn_req_max) {
33400Sstevel@tonic-gate tcp_t *tail;
33410Sstevel@tonic-gate
33420Sstevel@tonic-gate listener->tcp_conn_req_cnt_q0--;
33430Sstevel@tonic-gate listener->tcp_conn_req_cnt_q++;
33440Sstevel@tonic-gate
33450Sstevel@tonic-gate /* Move from SYN_RCVD to ESTABLISHED list */
33460Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
33470Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
33480Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
33490Sstevel@tonic-gate tcp->tcp_eager_next_q0;
33500Sstevel@tonic-gate tcp->tcp_eager_prev_q0 = NULL;
33510Sstevel@tonic-gate tcp->tcp_eager_next_q0 = NULL;
33520Sstevel@tonic-gate
33530Sstevel@tonic-gate /*
33540Sstevel@tonic-gate * Insert at end of the queue because sockfs
33550Sstevel@tonic-gate * sends down T_CONN_RES in chronological
33560Sstevel@tonic-gate * order. Leaving the older conn indications
33570Sstevel@tonic-gate * at front of the queue helps reducing search
33580Sstevel@tonic-gate * time.
33590Sstevel@tonic-gate */
33600Sstevel@tonic-gate tail = listener->tcp_eager_last_q;
33610Sstevel@tonic-gate if (tail != NULL) {
33620Sstevel@tonic-gate tail->tcp_eager_next_q = tcp;
33630Sstevel@tonic-gate } else {
33640Sstevel@tonic-gate listener->tcp_eager_next_q = tcp;
33650Sstevel@tonic-gate }
33660Sstevel@tonic-gate listener->tcp_eager_last_q = tcp;
33670Sstevel@tonic-gate tcp->tcp_eager_next_q = NULL;
33680Sstevel@tonic-gate } else {
33690Sstevel@tonic-gate /*
33700Sstevel@tonic-gate * Defer connection on q0 and set deferred
33710Sstevel@tonic-gate * connection bit true
33720Sstevel@tonic-gate */
33730Sstevel@tonic-gate tcp->tcp_conn_def_q0 = B_TRUE;
33740Sstevel@tonic-gate
33750Sstevel@tonic-gate /* take tcp out of q0 ... */
33760Sstevel@tonic-gate tcp->tcp_eager_prev_q0->tcp_eager_next_q0 =
33770Sstevel@tonic-gate tcp->tcp_eager_next_q0;
33780Sstevel@tonic-gate tcp->tcp_eager_next_q0->tcp_eager_prev_q0 =
33790Sstevel@tonic-gate tcp->tcp_eager_prev_q0;
33800Sstevel@tonic-gate
33810Sstevel@tonic-gate /* ... and place it at the end of q0 */
33820Sstevel@tonic-gate tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0;
33830Sstevel@tonic-gate tcp->tcp_eager_next_q0 = listener;
33840Sstevel@tonic-gate listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp;
33850Sstevel@tonic-gate listener->tcp_eager_prev_q0 = tcp;
33860Sstevel@tonic-gate }
33870Sstevel@tonic-gate
33880Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */
33890Sstevel@tonic-gate bytes_acked--;
33900Sstevel@tonic-gate
33910Sstevel@tonic-gate /*
33920Sstevel@tonic-gate * If SYN was retransmitted, need to reset all
33930Sstevel@tonic-gate * retransmission info as this segment will be
33940Sstevel@tonic-gate * treated as a dup ACK.
33950Sstevel@tonic-gate */
33960Sstevel@tonic-gate if (tcp->tcp_rexmit) {
33970Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
33980Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
33990Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
34000Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
34010Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
34020Sstevel@tonic-gate tcp->tcp_cwnd = mss;
34030Sstevel@tonic-gate }
34040Sstevel@tonic-gate
34050Sstevel@tonic-gate /*
34060Sstevel@tonic-gate * We set the send window to zero here.
34070Sstevel@tonic-gate * This is needed if there is data to be
34080Sstevel@tonic-gate * processed already on the queue.
34090Sstevel@tonic-gate * Later (at swnd_update label), the
34100Sstevel@tonic-gate * "new_swnd > tcp_swnd" condition is satisfied
34110Sstevel@tonic-gate * the XMIT_NEEDED flag is set in the current
34120Sstevel@tonic-gate * (SYN_RCVD) state. This ensures tcp_wput_data() is
34130Sstevel@tonic-gate * called if there is already data on queue in
34140Sstevel@tonic-gate * this state.
34150Sstevel@tonic-gate */
34160Sstevel@tonic-gate tcp->tcp_swnd = 0;
34170Sstevel@tonic-gate
34180Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
34190Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
34200Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
34210Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
34220Sstevel@tonic-gate tcp->tcp_state = TCPS_ESTABLISHED;
34230Sstevel@tonic-gate tcp->tcp_valid_bits &= ~TCP_ISS_VALID;
34240Sstevel@tonic-gate }
34250Sstevel@tonic-gate /* This code follows 4.4BSD-Lite2 mostly. */
34260Sstevel@tonic-gate if (bytes_acked < 0)
34270Sstevel@tonic-gate goto est;
34280Sstevel@tonic-gate
34290Sstevel@tonic-gate /*
34300Sstevel@tonic-gate * If TCP is ECN capable and the congestion experience bit is
34310Sstevel@tonic-gate * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be
34320Sstevel@tonic-gate * done once per window (or more loosely, per RTT).
34330Sstevel@tonic-gate */
34340Sstevel@tonic-gate if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max))
34350Sstevel@tonic-gate tcp->tcp_cwr = B_FALSE;
34360Sstevel@tonic-gate if (tcp->tcp_ecn_ok && (flags & TH_ECE)) {
34370Sstevel@tonic-gate if (!tcp->tcp_cwr) {
34380Sstevel@tonic-gate npkt = (MIN(tcp->tcp_cwnd, tcp->tcp_swnd) >> 1) / mss;
34390Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss;
34400Sstevel@tonic-gate tcp->tcp_cwnd = npkt * mss;
34410Sstevel@tonic-gate /*
34420Sstevel@tonic-gate * If the cwnd is 0, use the timer to clock out
34430Sstevel@tonic-gate * new segments. This is required by the ECN spec.
34440Sstevel@tonic-gate */
34450Sstevel@tonic-gate if (npkt == 0) {
34460Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
34470Sstevel@tonic-gate /*
34480Sstevel@tonic-gate * This makes sure that when the ACK comes
34490Sstevel@tonic-gate * back, we will increase tcp_cwnd by 1 MSS.
34500Sstevel@tonic-gate */
34510Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
34520Sstevel@tonic-gate }
34530Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
34540Sstevel@tonic-gate /*
34550Sstevel@tonic-gate * This marks the end of the current window of in
34560Sstevel@tonic-gate * flight data. That is why we don't use
34570Sstevel@tonic-gate * tcp_suna + tcp_swnd. Only data in flight can
34580Sstevel@tonic-gate * provide ECN info.
34590Sstevel@tonic-gate */
34600Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
34610Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
34620Sstevel@tonic-gate }
34630Sstevel@tonic-gate }
34640Sstevel@tonic-gate
34650Sstevel@tonic-gate mp1 = tcp->tcp_xmit_head;
34660Sstevel@tonic-gate if (bytes_acked == 0) {
34670Sstevel@tonic-gate if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) {
34680Sstevel@tonic-gate int dupack_cnt;
34690Sstevel@tonic-gate
34700Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDupAck);
34710Sstevel@tonic-gate /*
34720Sstevel@tonic-gate * Fast retransmit. When we have seen exactly three
34730Sstevel@tonic-gate * identical ACKs while we have unacked data
34740Sstevel@tonic-gate * outstanding we take it as a hint that our peer
34750Sstevel@tonic-gate * dropped something.
34760Sstevel@tonic-gate *
34770Sstevel@tonic-gate * If TCP is retransmitting, don't do fast retransmit.
34780Sstevel@tonic-gate */
34790Sstevel@tonic-gate if (mp1 != NULL && tcp->tcp_suna != tcp->tcp_snxt &&
34800Sstevel@tonic-gate ! tcp->tcp_rexmit) {
34810Sstevel@tonic-gate /* Do Limited Transmit */
34820Sstevel@tonic-gate if ((dupack_cnt = ++tcp->tcp_dupack_cnt) <
34830Sstevel@tonic-gate tcp_dupack_fast_retransmit) {
34840Sstevel@tonic-gate /*
34850Sstevel@tonic-gate * RFC 3042
34860Sstevel@tonic-gate *
34870Sstevel@tonic-gate * What we need to do is temporarily
34880Sstevel@tonic-gate * increase tcp_cwnd so that new
34890Sstevel@tonic-gate * data can be sent if it is allowed
34900Sstevel@tonic-gate * by the receive window (tcp_rwnd).
34910Sstevel@tonic-gate * tcp_wput_data() will take care of
34920Sstevel@tonic-gate * the rest.
34930Sstevel@tonic-gate *
34940Sstevel@tonic-gate * If the connection is SACK capable,
34950Sstevel@tonic-gate * only do limited xmit when there
34960Sstevel@tonic-gate * is SACK info.
34970Sstevel@tonic-gate *
34980Sstevel@tonic-gate * Note how tcp_cwnd is incremented.
34990Sstevel@tonic-gate * The first dup ACK will increase
35000Sstevel@tonic-gate * it by 1 MSS. The second dup ACK
35010Sstevel@tonic-gate * will increase it by 2 MSS. This
35020Sstevel@tonic-gate * means that only 1 new segment will
35030Sstevel@tonic-gate * be sent for each dup ACK.
35040Sstevel@tonic-gate */
35050Sstevel@tonic-gate if (tcp->tcp_unsent > 0 &&
35060Sstevel@tonic-gate (!tcp->tcp_snd_sack_ok ||
35070Sstevel@tonic-gate (tcp->tcp_snd_sack_ok &&
35080Sstevel@tonic-gate tcp->tcp_notsack_list != NULL))) {
35090Sstevel@tonic-gate tcp->tcp_cwnd += mss <<
35100Sstevel@tonic-gate (tcp->tcp_dupack_cnt - 1);
35110Sstevel@tonic-gate flags |= TH_LIMIT_XMIT;
35120Sstevel@tonic-gate }
35130Sstevel@tonic-gate } else if (dupack_cnt ==
35140Sstevel@tonic-gate tcp_dupack_fast_retransmit) {
35150Sstevel@tonic-gate
35160Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutFastRetrans);
35170Sstevel@tonic-gate /*
35180Sstevel@tonic-gate * If we have reduced tcp_ssthresh
35190Sstevel@tonic-gate * because of ECN, do not reduce it again
35200Sstevel@tonic-gate * unless it is already one window of data
35210Sstevel@tonic-gate * away. After one window of data, tcp_cwr
35220Sstevel@tonic-gate * should then be cleared. Note that
35230Sstevel@tonic-gate * for non ECN capable connection, tcp_cwr
35240Sstevel@tonic-gate * should always be false.
35250Sstevel@tonic-gate *
35260Sstevel@tonic-gate * Adjust cwnd since the duplicate
35270Sstevel@tonic-gate * ack indicates that a packet was
35280Sstevel@tonic-gate * dropped (due to congestion.)
35290Sstevel@tonic-gate */
35300Sstevel@tonic-gate if (!tcp->tcp_cwr) {
35310Sstevel@tonic-gate npkt = (MIN(tcp->tcp_cwnd,
35320Sstevel@tonic-gate tcp->tcp_swnd) >> 1) / mss;
35330Sstevel@tonic-gate if (npkt < 2)
35340Sstevel@tonic-gate npkt = 2;
35350Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = npkt * mss;
35360Sstevel@tonic-gate tcp->tcp_cwnd = (npkt +
35370Sstevel@tonic-gate tcp->tcp_dupack_cnt) * mss;
35380Sstevel@tonic-gate }
35390Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
35400Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
35410Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
35420Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
35430Sstevel@tonic-gate }
35440Sstevel@tonic-gate
35450Sstevel@tonic-gate /*
35460Sstevel@tonic-gate * We do Hoe's algorithm. Refer to her
35470Sstevel@tonic-gate * paper "Improving the Start-up Behavior
35480Sstevel@tonic-gate * of a Congestion Control Scheme for TCP,"
35490Sstevel@tonic-gate * appeared in SIGCOMM'96.
35500Sstevel@tonic-gate *
35510Sstevel@tonic-gate * Save highest seq no we have sent so far.
35520Sstevel@tonic-gate * Be careful about the invisible FIN byte.
35530Sstevel@tonic-gate */
35540Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
35550Sstevel@tonic-gate (tcp->tcp_unsent == 0)) {
35560Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_fss;
35570Sstevel@tonic-gate } else {
35580Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
35590Sstevel@tonic-gate }
35600Sstevel@tonic-gate
35610Sstevel@tonic-gate /*
35620Sstevel@tonic-gate * Do not allow bursty traffic during.
35630Sstevel@tonic-gate * fast recovery. Refer to Fall and Floyd's
35640Sstevel@tonic-gate * paper "Simulation-based Comparisons of
35650Sstevel@tonic-gate * Tahoe, Reno and SACK TCP" (in CCR ??)
35660Sstevel@tonic-gate * This is a best current practise.
35670Sstevel@tonic-gate */
35680Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_SS;
35690Sstevel@tonic-gate
35700Sstevel@tonic-gate /*
35710Sstevel@tonic-gate * For SACK:
35720Sstevel@tonic-gate * Calculate tcp_pipe, which is the
35730Sstevel@tonic-gate * estimated number of bytes in
35740Sstevel@tonic-gate * network.
35750Sstevel@tonic-gate *
35760Sstevel@tonic-gate * tcp_fack is the highest sack'ed seq num
35770Sstevel@tonic-gate * TCP has received.
35780Sstevel@tonic-gate *
35790Sstevel@tonic-gate * tcp_pipe is explained in the above quoted
35800Sstevel@tonic-gate * Fall and Floyd's paper. tcp_fack is
35810Sstevel@tonic-gate * explained in Mathis and Mahdavi's
35820Sstevel@tonic-gate * "Forward Acknowledgment: Refining TCP
35830Sstevel@tonic-gate * Congestion Control" in SIGCOMM '96.
35840Sstevel@tonic-gate */
35850Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
35860Sstevel@tonic-gate assert(tcp->tcp_sack_info != NULL);
35870Sstevel@tonic-gate if (tcp->tcp_notsack_list != NULL) {
35880Sstevel@tonic-gate tcp->tcp_pipe = tcp->tcp_snxt -
35890Sstevel@tonic-gate tcp->tcp_fack;
35900Sstevel@tonic-gate tcp->tcp_sack_snxt = seg_ack;
35910Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
35920Sstevel@tonic-gate } else {
35930Sstevel@tonic-gate /*
35940Sstevel@tonic-gate * Always initialize tcp_pipe
35950Sstevel@tonic-gate * even though we don't have
35960Sstevel@tonic-gate * any SACK info. If later
35970Sstevel@tonic-gate * we get SACK info and
35980Sstevel@tonic-gate * tcp_pipe is not initialized,
35990Sstevel@tonic-gate * funny things will happen.
36000Sstevel@tonic-gate */
36010Sstevel@tonic-gate tcp->tcp_pipe =
36020Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh;
36030Sstevel@tonic-gate }
36040Sstevel@tonic-gate } else {
36050Sstevel@tonic-gate flags |= TH_REXMIT_NEEDED;
36060Sstevel@tonic-gate } /* tcp_snd_sack_ok */
36070Sstevel@tonic-gate
36080Sstevel@tonic-gate } else {
36090Sstevel@tonic-gate /*
36100Sstevel@tonic-gate * Here we perform congestion
36110Sstevel@tonic-gate * avoidance, but NOT slow start.
36120Sstevel@tonic-gate * This is known as the Fast
36130Sstevel@tonic-gate * Recovery Algorithm.
36140Sstevel@tonic-gate */
36150Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
36160Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
36170Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
36180Sstevel@tonic-gate tcp->tcp_pipe -= mss;
36190Sstevel@tonic-gate if (tcp->tcp_pipe < 0)
36200Sstevel@tonic-gate tcp->tcp_pipe = 0;
36210Sstevel@tonic-gate } else {
36220Sstevel@tonic-gate /*
36230Sstevel@tonic-gate * We know that one more packet has
36240Sstevel@tonic-gate * left the pipe thus we can update
36250Sstevel@tonic-gate * cwnd.
36260Sstevel@tonic-gate */
36270Sstevel@tonic-gate cwnd = tcp->tcp_cwnd + mss;
36280Sstevel@tonic-gate if (cwnd > tcp->tcp_cwnd_max)
36290Sstevel@tonic-gate cwnd = tcp->tcp_cwnd_max;
36300Sstevel@tonic-gate tcp->tcp_cwnd = cwnd;
36310Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
36320Sstevel@tonic-gate }
36330Sstevel@tonic-gate }
36340Sstevel@tonic-gate }
36350Sstevel@tonic-gate } else if (tcp->tcp_zero_win_probe) {
36360Sstevel@tonic-gate /*
36370Sstevel@tonic-gate * If the window has opened, need to arrange
36380Sstevel@tonic-gate * to send additional data.
36390Sstevel@tonic-gate */
36400Sstevel@tonic-gate if (new_swnd != 0) {
36410Sstevel@tonic-gate /* tcp_suna != tcp_snxt */
36420Sstevel@tonic-gate /* Packet contains a window update */
36430Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInWinUpdate);
36440Sstevel@tonic-gate tcp->tcp_zero_win_probe = 0;
36450Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
36460Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
36470Sstevel@tonic-gate
36480Sstevel@tonic-gate /*
36490Sstevel@tonic-gate * Transmit starting with tcp_suna since
36500Sstevel@tonic-gate * the one byte probe is not ack'ed.
36510Sstevel@tonic-gate * If TCP has sent more than one identical
36520Sstevel@tonic-gate * probe, tcp_rexmit will be set. That means
36530Sstevel@tonic-gate * tcp_ss_rexmit() will send out the one
36540Sstevel@tonic-gate * byte along with new data. Otherwise,
36550Sstevel@tonic-gate * fake the retransmission.
36560Sstevel@tonic-gate */
36570Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
36580Sstevel@tonic-gate if (!tcp->tcp_rexmit) {
36590Sstevel@tonic-gate tcp->tcp_rexmit = B_TRUE;
36600Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
36610Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_suna;
36620Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_suna + 1;
36630Sstevel@tonic-gate }
36640Sstevel@tonic-gate }
36650Sstevel@tonic-gate }
36660Sstevel@tonic-gate goto swnd_update;
36670Sstevel@tonic-gate }
36680Sstevel@tonic-gate
36690Sstevel@tonic-gate /*
36700Sstevel@tonic-gate * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73.
36710Sstevel@tonic-gate * If the ACK value acks something that we have not yet sent, it might
36720Sstevel@tonic-gate * be an old duplicate segment. Send an ACK to re-synchronize the
36730Sstevel@tonic-gate * other side.
36740Sstevel@tonic-gate * Note: reset in response to unacceptable ACK in SYN_RECEIVE
36750Sstevel@tonic-gate * state is handled above, so we can always just drop the segment and
36760Sstevel@tonic-gate * send an ACK here.
36770Sstevel@tonic-gate *
36780Sstevel@tonic-gate * Should we send ACKs in response to ACK only segments?
36790Sstevel@tonic-gate */
36800Sstevel@tonic-gate if (SEQ_GT(seg_ack, tcp->tcp_snxt)) {
36810Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInAckUnsent);
36820Sstevel@tonic-gate /* drop the received segment */
36830Sstevel@tonic-gate freemsg(mp);
36840Sstevel@tonic-gate
36850Sstevel@tonic-gate /* Send back an ACK. */
36860Sstevel@tonic-gate mp = tcp_ack_mp(tcp);
36870Sstevel@tonic-gate
36880Sstevel@tonic-gate if (mp == NULL) {
36890Sstevel@tonic-gate return;
36900Sstevel@tonic-gate }
36910Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
36920Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
36930Sstevel@tonic-gate freeb(mp);
36940Sstevel@tonic-gate return;
36950Sstevel@tonic-gate }
36960Sstevel@tonic-gate
36970Sstevel@tonic-gate /*
36980Sstevel@tonic-gate * TCP gets a new ACK, update the notsack'ed list to delete those
36990Sstevel@tonic-gate * blocks that are covered by this ACK.
37000Sstevel@tonic-gate */
37010Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
37020Sstevel@tonic-gate tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack,
37030Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list));
37040Sstevel@tonic-gate }
37050Sstevel@tonic-gate
37060Sstevel@tonic-gate /*
37070Sstevel@tonic-gate * If we got an ACK after fast retransmit, check to see
37080Sstevel@tonic-gate * if it is a partial ACK. If it is not and the congestion
37090Sstevel@tonic-gate * window was inflated to account for the other side's
37100Sstevel@tonic-gate * cached packets, retract it. If it is, do Hoe's algorithm.
37110Sstevel@tonic-gate */
37120Sstevel@tonic-gate if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) {
37130Sstevel@tonic-gate assert(tcp->tcp_rexmit == B_FALSE);
37140Sstevel@tonic-gate if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) {
37150Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
37160Sstevel@tonic-gate /*
37170Sstevel@tonic-gate * Restore the orig tcp_cwnd_ssthresh after
37180Sstevel@tonic-gate * fast retransmit phase.
37190Sstevel@tonic-gate */
37200Sstevel@tonic-gate if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) {
37210Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh;
37220Sstevel@tonic-gate }
37230Sstevel@tonic-gate tcp->tcp_rexmit_max = seg_ack;
37240Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
37250Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
37260Sstevel@tonic-gate
37270Sstevel@tonic-gate /*
37280Sstevel@tonic-gate * Remove all notsack info to avoid confusion with
37290Sstevel@tonic-gate * the next fast retrasnmit/recovery phase.
37300Sstevel@tonic-gate */
37310Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
37320Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
37330Sstevel@tonic-gate TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list);
37340Sstevel@tonic-gate }
37350Sstevel@tonic-gate } else {
37360Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok &&
37370Sstevel@tonic-gate tcp->tcp_notsack_list != NULL) {
37380Sstevel@tonic-gate flags |= TH_NEED_SACK_REXMIT;
37390Sstevel@tonic-gate tcp->tcp_pipe -= mss;
37400Sstevel@tonic-gate if (tcp->tcp_pipe < 0)
37410Sstevel@tonic-gate tcp->tcp_pipe = 0;
37420Sstevel@tonic-gate } else {
37430Sstevel@tonic-gate /*
37440Sstevel@tonic-gate * Hoe's algorithm:
37450Sstevel@tonic-gate *
37460Sstevel@tonic-gate * Retransmit the unack'ed segment and
37470Sstevel@tonic-gate * restart fast recovery. Note that we
37480Sstevel@tonic-gate * need to scale back tcp_cwnd to the
37490Sstevel@tonic-gate * original value when we started fast
37500Sstevel@tonic-gate * recovery. This is to prevent overly
37510Sstevel@tonic-gate * aggressive behaviour in sending new
37520Sstevel@tonic-gate * segments.
37530Sstevel@tonic-gate */
37540Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh +
37550Sstevel@tonic-gate tcp_dupack_fast_retransmit * mss;
37560Sstevel@tonic-gate tcp->tcp_cwnd_cnt = tcp->tcp_cwnd;
37570Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutFastRetrans);
37580Sstevel@tonic-gate flags |= TH_REXMIT_NEEDED;
37590Sstevel@tonic-gate }
37600Sstevel@tonic-gate }
37610Sstevel@tonic-gate } else {
37620Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
37630Sstevel@tonic-gate if (tcp->tcp_rexmit) {
37640Sstevel@tonic-gate /*
37650Sstevel@tonic-gate * TCP is retranmitting. If the ACK ack's all
37660Sstevel@tonic-gate * outstanding data, update tcp_rexmit_max and
37670Sstevel@tonic-gate * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt
37680Sstevel@tonic-gate * to the correct value.
37690Sstevel@tonic-gate *
37700Sstevel@tonic-gate * Note that SEQ_LEQ() is used. This is to avoid
37710Sstevel@tonic-gate * unnecessary fast retransmit caused by dup ACKs
37720Sstevel@tonic-gate * received when TCP does slow start retransmission
37730Sstevel@tonic-gate * after a time out. During this phase, TCP may
37740Sstevel@tonic-gate * send out segments which are already received.
37750Sstevel@tonic-gate * This causes dup ACKs to be sent back.
37760Sstevel@tonic-gate */
37770Sstevel@tonic-gate if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) {
37780Sstevel@tonic-gate if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) {
37790Sstevel@tonic-gate tcp->tcp_rexmit_nxt = seg_ack;
37800Sstevel@tonic-gate }
37810Sstevel@tonic-gate if (seg_ack != tcp->tcp_rexmit_max) {
37820Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
37830Sstevel@tonic-gate }
37840Sstevel@tonic-gate } else {
37850Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
37860Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
37870Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_NORMAL;
37880Sstevel@tonic-gate }
37890Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
37900Sstevel@tonic-gate }
37910Sstevel@tonic-gate }
37920Sstevel@tonic-gate
37930Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInAckSegs);
37940Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInAckBytes, bytes_acked);
37950Sstevel@tonic-gate tcp->tcp_suna = seg_ack;
37960Sstevel@tonic-gate if (tcp->tcp_zero_win_probe != 0) {
37970Sstevel@tonic-gate tcp->tcp_zero_win_probe = 0;
37980Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
37990Sstevel@tonic-gate }
38000Sstevel@tonic-gate
38010Sstevel@tonic-gate /*
38020Sstevel@tonic-gate * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed.
38030Sstevel@tonic-gate * Note that it cannot be the SYN being ack'ed. The code flow
38040Sstevel@tonic-gate * will not reach here.
38050Sstevel@tonic-gate */
38060Sstevel@tonic-gate if (mp1 == NULL) {
38070Sstevel@tonic-gate goto fin_acked;
38080Sstevel@tonic-gate }
38090Sstevel@tonic-gate
38100Sstevel@tonic-gate /*
38110Sstevel@tonic-gate * Update the congestion window.
38120Sstevel@tonic-gate *
38130Sstevel@tonic-gate * If TCP is not ECN capable or TCP is ECN capable but the
38140Sstevel@tonic-gate * congestion experience bit is not set, increase the tcp_cwnd as
38150Sstevel@tonic-gate * usual.
38160Sstevel@tonic-gate */
38170Sstevel@tonic-gate if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) {
38180Sstevel@tonic-gate cwnd = tcp->tcp_cwnd;
38190Sstevel@tonic-gate add = mss;
38200Sstevel@tonic-gate
38210Sstevel@tonic-gate if (cwnd >= tcp->tcp_cwnd_ssthresh) {
38220Sstevel@tonic-gate /*
38230Sstevel@tonic-gate * This is to prevent an increase of less than 1 MSS of
38240Sstevel@tonic-gate * tcp_cwnd. With partial increase, tcp_wput_data()
38250Sstevel@tonic-gate * may send out tinygrams in order to preserve mblk
38260Sstevel@tonic-gate * boundaries.
38270Sstevel@tonic-gate *
38280Sstevel@tonic-gate * By initializing tcp_cwnd_cnt to new tcp_cwnd and
38290Sstevel@tonic-gate * decrementing it by 1 MSS for every ACKs, tcp_cwnd is
38300Sstevel@tonic-gate * increased by 1 MSS for every RTTs.
38310Sstevel@tonic-gate */
38320Sstevel@tonic-gate if (tcp->tcp_cwnd_cnt <= 0) {
38330Sstevel@tonic-gate tcp->tcp_cwnd_cnt = cwnd + add;
38340Sstevel@tonic-gate } else {
38350Sstevel@tonic-gate tcp->tcp_cwnd_cnt -= add;
38360Sstevel@tonic-gate add = 0;
38370Sstevel@tonic-gate }
38380Sstevel@tonic-gate }
38390Sstevel@tonic-gate tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max);
38400Sstevel@tonic-gate }
38410Sstevel@tonic-gate
38420Sstevel@tonic-gate /* Can we update the RTT estimates? */
38430Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
38440Sstevel@tonic-gate /* Ignore zero timestamp echo-reply. */
38450Sstevel@tonic-gate if (tcpopt.tcp_opt_ts_ecr != 0) {
38460Sstevel@tonic-gate tcp_set_rto(tcp, (int32_t)(prom_gettime() -
38470Sstevel@tonic-gate tcpopt.tcp_opt_ts_ecr));
38480Sstevel@tonic-gate }
38490Sstevel@tonic-gate
38500Sstevel@tonic-gate /* If needed, restart the timer. */
38510Sstevel@tonic-gate if (tcp->tcp_set_timer == 1) {
38520Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
38530Sstevel@tonic-gate tcp->tcp_set_timer = 0;
38540Sstevel@tonic-gate }
38550Sstevel@tonic-gate /*
38560Sstevel@tonic-gate * Update tcp_csuna in case the other side stops sending
38570Sstevel@tonic-gate * us timestamps.
38580Sstevel@tonic-gate */
38590Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
38600Sstevel@tonic-gate } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) {
38610Sstevel@tonic-gate /*
38620Sstevel@tonic-gate * An ACK sequence we haven't seen before, so get the RTT
38630Sstevel@tonic-gate * and update the RTO.
3864785Seota * Note. use uintptr_t to suppress the gcc warning.
38650Sstevel@tonic-gate */
38660Sstevel@tonic-gate tcp_set_rto(tcp, (int32_t)(prom_gettime() -
3867785Seota (uint32_t)(uintptr_t)mp1->b_prev));
38680Sstevel@tonic-gate
38690Sstevel@tonic-gate /* Remeber the last sequence to be ACKed */
38700Sstevel@tonic-gate tcp->tcp_csuna = seg_ack;
38710Sstevel@tonic-gate if (tcp->tcp_set_timer == 1) {
38720Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
38730Sstevel@tonic-gate tcp->tcp_set_timer = 0;
38740Sstevel@tonic-gate }
38750Sstevel@tonic-gate } else {
38760Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRttNoUpdate);
38770Sstevel@tonic-gate }
38780Sstevel@tonic-gate
38790Sstevel@tonic-gate /* Eat acknowledged bytes off the xmit queue. */
38800Sstevel@tonic-gate for (;;) {
38810Sstevel@tonic-gate mblk_t *mp2;
38820Sstevel@tonic-gate uchar_t *wptr;
38830Sstevel@tonic-gate
38840Sstevel@tonic-gate wptr = mp1->b_wptr;
38850Sstevel@tonic-gate assert((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX);
38860Sstevel@tonic-gate bytes_acked -= (int)(wptr - mp1->b_rptr);
38870Sstevel@tonic-gate if (bytes_acked < 0) {
38880Sstevel@tonic-gate mp1->b_rptr = wptr + bytes_acked;
38890Sstevel@tonic-gate break;
38900Sstevel@tonic-gate }
38910Sstevel@tonic-gate mp1->b_prev = NULL;
38920Sstevel@tonic-gate mp2 = mp1;
38930Sstevel@tonic-gate mp1 = mp1->b_cont;
38940Sstevel@tonic-gate freeb(mp2);
38950Sstevel@tonic-gate if (bytes_acked == 0) {
38960Sstevel@tonic-gate if (mp1 == NULL) {
38970Sstevel@tonic-gate /* Everything is ack'ed, clear the tail. */
38980Sstevel@tonic-gate tcp->tcp_xmit_tail = NULL;
38990Sstevel@tonic-gate goto pre_swnd_update;
39000Sstevel@tonic-gate }
39010Sstevel@tonic-gate if (mp2 != tcp->tcp_xmit_tail)
39020Sstevel@tonic-gate break;
39030Sstevel@tonic-gate tcp->tcp_xmit_tail = mp1;
39040Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
39050Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
39060Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr -
39070Sstevel@tonic-gate mp1->b_rptr);
39080Sstevel@tonic-gate break;
39090Sstevel@tonic-gate }
39100Sstevel@tonic-gate if (mp1 == NULL) {
39110Sstevel@tonic-gate /*
39120Sstevel@tonic-gate * More was acked but there is nothing more
39130Sstevel@tonic-gate * outstanding. This means that the FIN was
39140Sstevel@tonic-gate * just acked or that we're talking to a clown.
39150Sstevel@tonic-gate */
39160Sstevel@tonic-gate fin_acked:
39170Sstevel@tonic-gate assert(tcp->tcp_fin_sent);
39180Sstevel@tonic-gate tcp->tcp_xmit_tail = NULL;
39190Sstevel@tonic-gate if (tcp->tcp_fin_sent) {
39200Sstevel@tonic-gate tcp->tcp_fin_acked = B_TRUE;
39210Sstevel@tonic-gate } else {
39220Sstevel@tonic-gate /*
39230Sstevel@tonic-gate * We should never got here because
39240Sstevel@tonic-gate * we have already checked that the
39250Sstevel@tonic-gate * number of bytes ack'ed should be
39260Sstevel@tonic-gate * smaller than or equal to what we
39270Sstevel@tonic-gate * have sent so far (it is the
39280Sstevel@tonic-gate * acceptability check of the ACK).
39290Sstevel@tonic-gate * We can only get here if the send
39300Sstevel@tonic-gate * queue is corrupted.
39310Sstevel@tonic-gate *
39320Sstevel@tonic-gate * Terminate the connection and
39330Sstevel@tonic-gate * panic the system. It is better
39340Sstevel@tonic-gate * for us to panic instead of
39350Sstevel@tonic-gate * continuing to avoid other disaster.
39360Sstevel@tonic-gate */
39370Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
39380Sstevel@tonic-gate tcp->tcp_rnxt, TH_RST|TH_ACK, 0, sock_id);
39390Sstevel@tonic-gate printf("Memory corruption "
39400Sstevel@tonic-gate "detected for connection %s.\n",
39410Sstevel@tonic-gate tcp_display(tcp, NULL,
39420Sstevel@tonic-gate DISP_ADDR_AND_PORT));
39430Sstevel@tonic-gate /* We should never get here... */
39440Sstevel@tonic-gate prom_panic("tcp_rput_data");
39450Sstevel@tonic-gate return;
39460Sstevel@tonic-gate }
39470Sstevel@tonic-gate goto pre_swnd_update;
39480Sstevel@tonic-gate }
39490Sstevel@tonic-gate assert(mp2 != tcp->tcp_xmit_tail);
39500Sstevel@tonic-gate }
39510Sstevel@tonic-gate if (tcp->tcp_unsent) {
39520Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
39530Sstevel@tonic-gate }
39540Sstevel@tonic-gate pre_swnd_update:
39550Sstevel@tonic-gate tcp->tcp_xmit_head = mp1;
39560Sstevel@tonic-gate swnd_update:
39570Sstevel@tonic-gate /*
39580Sstevel@tonic-gate * The following check is different from most other implementations.
39590Sstevel@tonic-gate * For bi-directional transfer, when segments are dropped, the
39600Sstevel@tonic-gate * "normal" check will not accept a window update in those
39610Sstevel@tonic-gate * retransmitted segemnts. Failing to do that, TCP may send out
39620Sstevel@tonic-gate * segments which are outside receiver's window. As TCP accepts
39630Sstevel@tonic-gate * the ack in those retransmitted segments, if the window update in
39640Sstevel@tonic-gate * the same segment is not accepted, TCP will incorrectly calculates
39650Sstevel@tonic-gate * that it can send more segments. This can create a deadlock
39660Sstevel@tonic-gate * with the receiver if its window becomes zero.
39670Sstevel@tonic-gate */
39680Sstevel@tonic-gate if (SEQ_LT(tcp->tcp_swl2, seg_ack) ||
39690Sstevel@tonic-gate SEQ_LT(tcp->tcp_swl1, seg_seq) ||
39700Sstevel@tonic-gate (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) {
39710Sstevel@tonic-gate /*
39720Sstevel@tonic-gate * The criteria for update is:
39730Sstevel@tonic-gate *
39740Sstevel@tonic-gate * 1. the segment acknowledges some data. Or
39750Sstevel@tonic-gate * 2. the segment is new, i.e. it has a higher seq num. Or
39760Sstevel@tonic-gate * 3. the segment is not old and the advertised window is
39770Sstevel@tonic-gate * larger than the previous advertised window.
39780Sstevel@tonic-gate */
39790Sstevel@tonic-gate if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd)
39800Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
39810Sstevel@tonic-gate tcp->tcp_swnd = new_swnd;
39820Sstevel@tonic-gate if (new_swnd > tcp->tcp_max_swnd)
39830Sstevel@tonic-gate tcp->tcp_max_swnd = new_swnd;
39840Sstevel@tonic-gate tcp->tcp_swl1 = seg_seq;
39850Sstevel@tonic-gate tcp->tcp_swl2 = seg_ack;
39860Sstevel@tonic-gate }
39870Sstevel@tonic-gate est:
39880Sstevel@tonic-gate if (tcp->tcp_state > TCPS_ESTABLISHED) {
39890Sstevel@tonic-gate switch (tcp->tcp_state) {
39900Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
39910Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
39920Sstevel@tonic-gate tcp->tcp_state = TCPS_FIN_WAIT_2;
39930Sstevel@tonic-gate /*
39940Sstevel@tonic-gate * We implement the non-standard BSD/SunOS
39950Sstevel@tonic-gate * FIN_WAIT_2 flushing algorithm.
39960Sstevel@tonic-gate * If there is no user attached to this
39970Sstevel@tonic-gate * TCP endpoint, then this TCP struct
39980Sstevel@tonic-gate * could hang around forever in FIN_WAIT_2
39990Sstevel@tonic-gate * state if the peer forgets to send us
40000Sstevel@tonic-gate * a FIN. To prevent this, we wait only
40010Sstevel@tonic-gate * 2*MSL (a convenient time value) for
40020Sstevel@tonic-gate * the FIN to arrive. If it doesn't show up,
40030Sstevel@tonic-gate * we flush the TCP endpoint. This algorithm,
40040Sstevel@tonic-gate * though a violation of RFC-793, has worked
40050Sstevel@tonic-gate * for over 10 years in BSD systems.
40060Sstevel@tonic-gate * Note: SunOS 4.x waits 675 seconds before
40070Sstevel@tonic-gate * flushing the FIN_WAIT_2 connection.
40080Sstevel@tonic-gate */
40090Sstevel@tonic-gate TCP_TIMER_RESTART(tcp,
40100Sstevel@tonic-gate tcp_fin_wait_2_flush_interval);
40110Sstevel@tonic-gate }
40120Sstevel@tonic-gate break;
40130Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
40140Sstevel@tonic-gate break; /* Shutdown hook? */
40150Sstevel@tonic-gate case TCPS_LAST_ACK:
40160Sstevel@tonic-gate freemsg(mp);
40170Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
40180Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
40190Sstevel@tonic-gate return;
40200Sstevel@tonic-gate }
40210Sstevel@tonic-gate goto xmit_check;
40220Sstevel@tonic-gate case TCPS_CLOSING:
40230Sstevel@tonic-gate if (tcp->tcp_fin_acked) {
40240Sstevel@tonic-gate tcp->tcp_state = TCPS_TIME_WAIT;
40250Sstevel@tonic-gate tcp_time_wait_append(tcp);
40260Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
40270Sstevel@tonic-gate }
40280Sstevel@tonic-gate /*FALLTHRU*/
40290Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
40300Sstevel@tonic-gate freemsg(mp);
40310Sstevel@tonic-gate goto xmit_check;
40320Sstevel@tonic-gate default:
40330Sstevel@tonic-gate assert(tcp->tcp_state != TCPS_TIME_WAIT);
40340Sstevel@tonic-gate break;
40350Sstevel@tonic-gate }
40360Sstevel@tonic-gate }
40370Sstevel@tonic-gate if (flags & TH_FIN) {
40380Sstevel@tonic-gate /* Make sure we ack the fin */
40390Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
40400Sstevel@tonic-gate if (!tcp->tcp_fin_rcvd) {
40410Sstevel@tonic-gate tcp->tcp_fin_rcvd = B_TRUE;
40420Sstevel@tonic-gate tcp->tcp_rnxt++;
40430Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
40440Sstevel@tonic-gate
40450Sstevel@tonic-gate switch (tcp->tcp_state) {
40460Sstevel@tonic-gate case TCPS_SYN_RCVD:
40470Sstevel@tonic-gate case TCPS_ESTABLISHED:
40480Sstevel@tonic-gate tcp->tcp_state = TCPS_CLOSE_WAIT;
40490Sstevel@tonic-gate /* Keepalive? */
40500Sstevel@tonic-gate break;
40510Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
40520Sstevel@tonic-gate if (!tcp->tcp_fin_acked) {
40530Sstevel@tonic-gate tcp->tcp_state = TCPS_CLOSING;
40540Sstevel@tonic-gate break;
40550Sstevel@tonic-gate }
40560Sstevel@tonic-gate /* FALLTHRU */
40570Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
40580Sstevel@tonic-gate tcp->tcp_state = TCPS_TIME_WAIT;
40590Sstevel@tonic-gate tcp_time_wait_append(tcp);
40600Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
40610Sstevel@tonic-gate if (seg_len) {
40620Sstevel@tonic-gate /*
40630Sstevel@tonic-gate * implies data piggybacked on FIN.
40640Sstevel@tonic-gate * break to handle data.
40650Sstevel@tonic-gate */
40660Sstevel@tonic-gate break;
40670Sstevel@tonic-gate }
40680Sstevel@tonic-gate freemsg(mp);
40690Sstevel@tonic-gate goto ack_check;
40700Sstevel@tonic-gate }
40710Sstevel@tonic-gate }
40720Sstevel@tonic-gate }
40730Sstevel@tonic-gate if (mp == NULL)
40740Sstevel@tonic-gate goto xmit_check;
40750Sstevel@tonic-gate if (seg_len == 0) {
40760Sstevel@tonic-gate freemsg(mp);
40770Sstevel@tonic-gate goto xmit_check;
40780Sstevel@tonic-gate }
40790Sstevel@tonic-gate if (mp->b_rptr == mp->b_wptr) {
40800Sstevel@tonic-gate /*
40810Sstevel@tonic-gate * The header has been consumed, so we remove the
40820Sstevel@tonic-gate * zero-length mblk here.
40830Sstevel@tonic-gate */
40840Sstevel@tonic-gate mp1 = mp;
40850Sstevel@tonic-gate mp = mp->b_cont;
40860Sstevel@tonic-gate freeb(mp1);
40870Sstevel@tonic-gate }
40880Sstevel@tonic-gate /*
40890Sstevel@tonic-gate * ACK every other segments, unless the input queue is empty
40900Sstevel@tonic-gate * as we don't have a timer available.
40910Sstevel@tonic-gate */
40920Sstevel@tonic-gate if (++tcp->tcp_rack_cnt == 2 || sockets[sock_id].inq == NULL) {
40930Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
40940Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
40950Sstevel@tonic-gate }
40960Sstevel@tonic-gate tcp->tcp_rnxt += seg_len;
40970Sstevel@tonic-gate U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack);
40980Sstevel@tonic-gate
40990Sstevel@tonic-gate /* Update SACK list */
41000Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
41010Sstevel@tonic-gate tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt,
41020Sstevel@tonic-gate &(tcp->tcp_num_sack_blk));
41030Sstevel@tonic-gate }
41040Sstevel@tonic-gate
41050Sstevel@tonic-gate if (tcp->tcp_listener) {
41060Sstevel@tonic-gate /*
41070Sstevel@tonic-gate * Side queue inbound data until the accept happens.
41080Sstevel@tonic-gate * tcp_accept/tcp_rput drains this when the accept happens.
41090Sstevel@tonic-gate */
41100Sstevel@tonic-gate tcp_rcv_enqueue(tcp, mp, seg_len);
41110Sstevel@tonic-gate } else {
41120Sstevel@tonic-gate /* Just queue the data until the app calls read. */
41130Sstevel@tonic-gate tcp_rcv_enqueue(tcp, mp, seg_len);
41140Sstevel@tonic-gate /*
41150Sstevel@tonic-gate * Make sure the timer is running if we have data waiting
41160Sstevel@tonic-gate * for a push bit. This provides resiliency against
41170Sstevel@tonic-gate * implementations that do not correctly generate push bits.
41180Sstevel@tonic-gate */
41190Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL)
41200Sstevel@tonic-gate flags |= TH_TIMER_NEEDED;
41210Sstevel@tonic-gate }
41220Sstevel@tonic-gate
41230Sstevel@tonic-gate xmit_check:
41240Sstevel@tonic-gate /* Is there anything left to do? */
41250Sstevel@tonic-gate if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED|
41260Sstevel@tonic-gate TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_TIMER_NEEDED)) == 0)
41270Sstevel@tonic-gate return;
41280Sstevel@tonic-gate
41290Sstevel@tonic-gate /* Any transmit work to do and a non-zero window? */
41300Sstevel@tonic-gate if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT|
41310Sstevel@tonic-gate TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) {
41320Sstevel@tonic-gate if (flags & TH_REXMIT_NEEDED) {
41330Sstevel@tonic-gate uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna;
41340Sstevel@tonic-gate
41350Sstevel@tonic-gate if (snd_size > mss)
41360Sstevel@tonic-gate snd_size = mss;
41370Sstevel@tonic-gate if (snd_size > tcp->tcp_swnd)
41380Sstevel@tonic-gate snd_size = tcp->tcp_swnd;
41390Sstevel@tonic-gate mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size,
41400Sstevel@tonic-gate NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size,
41410Sstevel@tonic-gate B_TRUE);
41420Sstevel@tonic-gate
41430Sstevel@tonic-gate if (mp1 != NULL) {
4144785Seota /* use uintptr_t to suppress the gcc warning */
41450Sstevel@tonic-gate tcp->tcp_xmit_head->b_prev =
4146785Seota (mblk_t *)(uintptr_t)prom_gettime();
41470Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
41480Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
41490Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, snd_size);
41500Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
41510Sstevel@tonic-gate freeb(mp1);
41520Sstevel@tonic-gate }
41530Sstevel@tonic-gate }
41540Sstevel@tonic-gate if (flags & TH_NEED_SACK_REXMIT) {
41550Sstevel@tonic-gate if (tcp_sack_rxmit(tcp, sock_id) != 0) {
41560Sstevel@tonic-gate flags |= TH_XMIT_NEEDED;
41570Sstevel@tonic-gate }
41580Sstevel@tonic-gate }
41590Sstevel@tonic-gate /*
41600Sstevel@tonic-gate * For TH_LIMIT_XMIT, tcp_wput_data() is called to send
41610Sstevel@tonic-gate * out new segment. Note that tcp_rexmit should not be
41620Sstevel@tonic-gate * set, otherwise TH_LIMIT_XMIT should not be set.
41630Sstevel@tonic-gate */
41640Sstevel@tonic-gate if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) {
41650Sstevel@tonic-gate if (!tcp->tcp_rexmit) {
41660Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
41670Sstevel@tonic-gate } else {
41680Sstevel@tonic-gate tcp_ss_rexmit(tcp, sock_id);
41690Sstevel@tonic-gate }
41700Sstevel@tonic-gate /*
41710Sstevel@tonic-gate * The TCP could be closed in tcp_state_wait via
41720Sstevel@tonic-gate * tcp_wput_data (tcp_ss_rexmit could call
41730Sstevel@tonic-gate * tcp_wput_data as well).
41740Sstevel@tonic-gate */
41750Sstevel@tonic-gate if (sockets[sock_id].pcb == NULL)
41760Sstevel@tonic-gate return;
41770Sstevel@tonic-gate }
41780Sstevel@tonic-gate /*
41790Sstevel@tonic-gate * Adjust tcp_cwnd back to normal value after sending
41800Sstevel@tonic-gate * new data segments.
41810Sstevel@tonic-gate */
41820Sstevel@tonic-gate if (flags & TH_LIMIT_XMIT) {
41830Sstevel@tonic-gate tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1);
41840Sstevel@tonic-gate }
41850Sstevel@tonic-gate
41860Sstevel@tonic-gate /* Anything more to do? */
41870Sstevel@tonic-gate if ((flags & (TH_ACK_NEEDED|TH_TIMER_NEEDED)) == 0)
41880Sstevel@tonic-gate return;
41890Sstevel@tonic-gate }
41900Sstevel@tonic-gate ack_check:
41910Sstevel@tonic-gate if (flags & TH_ACK_NEEDED) {
41920Sstevel@tonic-gate /*
41930Sstevel@tonic-gate * Time to send an ack for some reason.
41940Sstevel@tonic-gate */
41950Sstevel@tonic-gate if ((mp1 = tcp_ack_mp(tcp)) != NULL) {
41960Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_rput_data: ack mp", mp1);
41970Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp1);
41980Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
41990Sstevel@tonic-gate freeb(mp1);
42000Sstevel@tonic-gate }
42010Sstevel@tonic-gate }
42020Sstevel@tonic-gate }
42030Sstevel@tonic-gate
42040Sstevel@tonic-gate /*
42050Sstevel@tonic-gate * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start
42060Sstevel@tonic-gate * retransmission after a timeout.
42070Sstevel@tonic-gate *
42080Sstevel@tonic-gate * To limit the number of duplicate segments, we limit the number of segment
42090Sstevel@tonic-gate * to be sent in one time to tcp_snd_burst, the burst variable.
42100Sstevel@tonic-gate */
42110Sstevel@tonic-gate static void
tcp_ss_rexmit(tcp_t * tcp,int sock_id)42120Sstevel@tonic-gate tcp_ss_rexmit(tcp_t *tcp, int sock_id)
42130Sstevel@tonic-gate {
42140Sstevel@tonic-gate uint32_t snxt;
42150Sstevel@tonic-gate uint32_t smax;
42160Sstevel@tonic-gate int32_t win;
42170Sstevel@tonic-gate int32_t mss;
42180Sstevel@tonic-gate int32_t off;
42190Sstevel@tonic-gate int32_t burst = tcp->tcp_snd_burst;
42200Sstevel@tonic-gate mblk_t *snxt_mp;
42210Sstevel@tonic-gate
42220Sstevel@tonic-gate /*
42230Sstevel@tonic-gate * Note that tcp_rexmit can be set even though TCP has retransmitted
42240Sstevel@tonic-gate * all unack'ed segments.
42250Sstevel@tonic-gate */
42260Sstevel@tonic-gate if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) {
42270Sstevel@tonic-gate smax = tcp->tcp_rexmit_max;
42280Sstevel@tonic-gate snxt = tcp->tcp_rexmit_nxt;
42290Sstevel@tonic-gate if (SEQ_LT(snxt, tcp->tcp_suna)) {
42300Sstevel@tonic-gate snxt = tcp->tcp_suna;
42310Sstevel@tonic-gate }
42320Sstevel@tonic-gate win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd);
42330Sstevel@tonic-gate win -= snxt - tcp->tcp_suna;
42340Sstevel@tonic-gate mss = tcp->tcp_mss;
42350Sstevel@tonic-gate snxt_mp = tcp_get_seg_mp(tcp, snxt, &off);
42360Sstevel@tonic-gate
42370Sstevel@tonic-gate while (SEQ_LT(snxt, smax) && (win > 0) &&
42380Sstevel@tonic-gate (burst > 0) && (snxt_mp != NULL)) {
42390Sstevel@tonic-gate mblk_t *xmit_mp;
42400Sstevel@tonic-gate mblk_t *old_snxt_mp = snxt_mp;
42410Sstevel@tonic-gate uint32_t cnt = mss;
42420Sstevel@tonic-gate
42430Sstevel@tonic-gate if (win < cnt) {
42440Sstevel@tonic-gate cnt = win;
42450Sstevel@tonic-gate }
42460Sstevel@tonic-gate if (SEQ_GT(snxt + cnt, smax)) {
42470Sstevel@tonic-gate cnt = smax - snxt;
42480Sstevel@tonic-gate }
42490Sstevel@tonic-gate xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off,
42500Sstevel@tonic-gate &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE);
42510Sstevel@tonic-gate
42520Sstevel@tonic-gate if (xmit_mp == NULL)
42530Sstevel@tonic-gate return;
42540Sstevel@tonic-gate
42550Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, xmit_mp);
42560Sstevel@tonic-gate freeb(xmit_mp);
42570Sstevel@tonic-gate
42580Sstevel@tonic-gate snxt += cnt;
42590Sstevel@tonic-gate win -= cnt;
42600Sstevel@tonic-gate /*
42610Sstevel@tonic-gate * Update the send timestamp to avoid false
42620Sstevel@tonic-gate * retransmission.
4263785Seota * Note. use uintptr_t to suppress the gcc warning.
42640Sstevel@tonic-gate */
4265785Seota old_snxt_mp->b_prev =
4266785Seota (mblk_t *)(uintptr_t)prom_gettime();
42670Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
42680Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, cnt);
42690Sstevel@tonic-gate
42700Sstevel@tonic-gate tcp->tcp_rexmit_nxt = snxt;
42710Sstevel@tonic-gate burst--;
42720Sstevel@tonic-gate }
42730Sstevel@tonic-gate /*
42740Sstevel@tonic-gate * If we have transmitted all we have at the time
42750Sstevel@tonic-gate * we started the retranmission, we can leave
42760Sstevel@tonic-gate * the rest of the job to tcp_wput_data(). But we
42770Sstevel@tonic-gate * need to check the send window first. If the
42780Sstevel@tonic-gate * win is not 0, go on with tcp_wput_data().
42790Sstevel@tonic-gate */
42800Sstevel@tonic-gate if (SEQ_LT(snxt, smax) || win == 0) {
42810Sstevel@tonic-gate return;
42820Sstevel@tonic-gate }
42830Sstevel@tonic-gate }
42840Sstevel@tonic-gate /* Only call tcp_wput_data() if there is data to be sent. */
42850Sstevel@tonic-gate if (tcp->tcp_unsent) {
42860Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
42870Sstevel@tonic-gate }
42880Sstevel@tonic-gate }
42890Sstevel@tonic-gate
42900Sstevel@tonic-gate /*
42910Sstevel@tonic-gate * tcp_timer is the timer service routine. It handles all timer events for
42920Sstevel@tonic-gate * a tcp instance except keepalives. It figures out from the state of the
42930Sstevel@tonic-gate * tcp instance what kind of action needs to be done at the time it is called.
42940Sstevel@tonic-gate */
42950Sstevel@tonic-gate static void
tcp_timer(tcp_t * tcp,int sock_id)42960Sstevel@tonic-gate tcp_timer(tcp_t *tcp, int sock_id)
42970Sstevel@tonic-gate {
42980Sstevel@tonic-gate mblk_t *mp;
42990Sstevel@tonic-gate uint32_t first_threshold;
43000Sstevel@tonic-gate uint32_t second_threshold;
43010Sstevel@tonic-gate uint32_t ms;
43020Sstevel@tonic-gate uint32_t mss;
43030Sstevel@tonic-gate
43040Sstevel@tonic-gate first_threshold = tcp->tcp_first_timer_threshold;
43050Sstevel@tonic-gate second_threshold = tcp->tcp_second_timer_threshold;
43060Sstevel@tonic-gate switch (tcp->tcp_state) {
43070Sstevel@tonic-gate case TCPS_IDLE:
43080Sstevel@tonic-gate case TCPS_BOUND:
43090Sstevel@tonic-gate case TCPS_LISTEN:
43100Sstevel@tonic-gate return;
43110Sstevel@tonic-gate case TCPS_SYN_RCVD:
43120Sstevel@tonic-gate case TCPS_SYN_SENT:
43130Sstevel@tonic-gate first_threshold = tcp->tcp_first_ctimer_threshold;
43140Sstevel@tonic-gate second_threshold = tcp->tcp_second_ctimer_threshold;
43150Sstevel@tonic-gate break;
43160Sstevel@tonic-gate case TCPS_ESTABLISHED:
43170Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
43180Sstevel@tonic-gate case TCPS_CLOSING:
43190Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
43200Sstevel@tonic-gate case TCPS_LAST_ACK:
43210Sstevel@tonic-gate /* If we have data to rexmit */
43220Sstevel@tonic-gate if (tcp->tcp_suna != tcp->tcp_snxt) {
43230Sstevel@tonic-gate int32_t time_to_wait;
43240Sstevel@tonic-gate
43250Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpTimRetrans);
43260Sstevel@tonic-gate if (tcp->tcp_xmit_head == NULL)
43270Sstevel@tonic-gate break;
4328785Seota /* use uintptr_t to suppress the gcc warning */
43290Sstevel@tonic-gate time_to_wait = (int32_t)(prom_gettime() -
4330785Seota (uint32_t)(uintptr_t)tcp->tcp_xmit_head->b_prev);
43310Sstevel@tonic-gate time_to_wait = tcp->tcp_rto - time_to_wait;
43320Sstevel@tonic-gate if (time_to_wait > 0) {
43330Sstevel@tonic-gate /*
43340Sstevel@tonic-gate * Timer fired too early, so restart it.
43350Sstevel@tonic-gate */
43360Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, time_to_wait);
43370Sstevel@tonic-gate return;
43380Sstevel@tonic-gate }
43390Sstevel@tonic-gate /*
43400Sstevel@tonic-gate * When we probe zero windows, we force the swnd open.
43410Sstevel@tonic-gate * If our peer acks with a closed window swnd will be
43420Sstevel@tonic-gate * set to zero by tcp_rput(). As long as we are
43430Sstevel@tonic-gate * receiving acks tcp_rput will
43440Sstevel@tonic-gate * reset 'tcp_ms_we_have_waited' so as not to trip the
43450Sstevel@tonic-gate * first and second interval actions. NOTE: the timer
43460Sstevel@tonic-gate * interval is allowed to continue its exponential
43470Sstevel@tonic-gate * backoff.
43480Sstevel@tonic-gate */
43490Sstevel@tonic-gate if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) {
43500Sstevel@tonic-gate DEBUG_1("tcp_timer (%d): zero win", sock_id);
43510Sstevel@tonic-gate break;
43520Sstevel@tonic-gate } else {
43530Sstevel@tonic-gate /*
43540Sstevel@tonic-gate * After retransmission, we need to do
43550Sstevel@tonic-gate * slow start. Set the ssthresh to one
43560Sstevel@tonic-gate * half of current effective window and
43570Sstevel@tonic-gate * cwnd to one MSS. Also reset
43580Sstevel@tonic-gate * tcp_cwnd_cnt.
43590Sstevel@tonic-gate *
43600Sstevel@tonic-gate * Note that if tcp_ssthresh is reduced because
43610Sstevel@tonic-gate * of ECN, do not reduce it again unless it is
43620Sstevel@tonic-gate * already one window of data away (tcp_cwr
43630Sstevel@tonic-gate * should then be cleared) or this is a
43640Sstevel@tonic-gate * timeout for a retransmitted segment.
43650Sstevel@tonic-gate */
43660Sstevel@tonic-gate uint32_t npkt;
43670Sstevel@tonic-gate
43680Sstevel@tonic-gate if (!tcp->tcp_cwr || tcp->tcp_rexmit) {
43690Sstevel@tonic-gate npkt = (MIN((tcp->tcp_timer_backoff ?
43700Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh :
43710Sstevel@tonic-gate tcp->tcp_cwnd),
43720Sstevel@tonic-gate tcp->tcp_swnd) >> 1) /
43730Sstevel@tonic-gate tcp->tcp_mss;
43740Sstevel@tonic-gate if (npkt < 2)
43750Sstevel@tonic-gate npkt = 2;
43760Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = npkt *
43770Sstevel@tonic-gate tcp->tcp_mss;
43780Sstevel@tonic-gate }
43790Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
43800Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
43810Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
43820Sstevel@tonic-gate tcp->tcp_cwr = B_TRUE;
43830Sstevel@tonic-gate tcp->tcp_cwr_snd_max = tcp->tcp_snxt;
43840Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_FALSE;
43850Sstevel@tonic-gate }
43860Sstevel@tonic-gate }
43870Sstevel@tonic-gate break;
43880Sstevel@tonic-gate }
43890Sstevel@tonic-gate /*
43900Sstevel@tonic-gate * We have something to send yet we cannot send. The
43910Sstevel@tonic-gate * reason can be:
43920Sstevel@tonic-gate *
43930Sstevel@tonic-gate * 1. Zero send window: we need to do zero window probe.
43940Sstevel@tonic-gate * 2. Zero cwnd: because of ECN, we need to "clock out
43950Sstevel@tonic-gate * segments.
43960Sstevel@tonic-gate * 3. SWS avoidance: receiver may have shrunk window,
43970Sstevel@tonic-gate * reset our knowledge.
43980Sstevel@tonic-gate *
43990Sstevel@tonic-gate * Note that condition 2 can happen with either 1 or
44000Sstevel@tonic-gate * 3. But 1 and 3 are exclusive.
44010Sstevel@tonic-gate */
44020Sstevel@tonic-gate if (tcp->tcp_unsent != 0) {
44030Sstevel@tonic-gate if (tcp->tcp_cwnd == 0) {
44040Sstevel@tonic-gate /*
44050Sstevel@tonic-gate * Set tcp_cwnd to 1 MSS so that a
44060Sstevel@tonic-gate * new segment can be sent out. We
44070Sstevel@tonic-gate * are "clocking out" new data when
44080Sstevel@tonic-gate * the network is really congested.
44090Sstevel@tonic-gate */
44100Sstevel@tonic-gate assert(tcp->tcp_ecn_ok);
44110Sstevel@tonic-gate tcp->tcp_cwnd = tcp->tcp_mss;
44120Sstevel@tonic-gate }
44130Sstevel@tonic-gate if (tcp->tcp_swnd == 0) {
44140Sstevel@tonic-gate /* Extend window for zero window probe */
44150Sstevel@tonic-gate tcp->tcp_swnd++;
44160Sstevel@tonic-gate tcp->tcp_zero_win_probe = B_TRUE;
44170Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutWinProbe);
44180Sstevel@tonic-gate } else {
44190Sstevel@tonic-gate /*
44200Sstevel@tonic-gate * Handle timeout from sender SWS avoidance.
44210Sstevel@tonic-gate * Reset our knowledge of the max send window
44220Sstevel@tonic-gate * since the receiver might have reduced its
44230Sstevel@tonic-gate * receive buffer. Avoid setting tcp_max_swnd
44240Sstevel@tonic-gate * to one since that will essentially disable
44250Sstevel@tonic-gate * the SWS checks.
44260Sstevel@tonic-gate *
44270Sstevel@tonic-gate * Note that since we don't have a SWS
44280Sstevel@tonic-gate * state variable, if the timeout is set
44290Sstevel@tonic-gate * for ECN but not for SWS, this
44300Sstevel@tonic-gate * code will also be executed. This is
44310Sstevel@tonic-gate * fine as tcp_max_swnd is updated
44320Sstevel@tonic-gate * constantly and it will not affect
44330Sstevel@tonic-gate * anything.
44340Sstevel@tonic-gate */
44350Sstevel@tonic-gate tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2);
44360Sstevel@tonic-gate }
44370Sstevel@tonic-gate tcp_wput_data(tcp, NULL, sock_id);
44380Sstevel@tonic-gate return;
44390Sstevel@tonic-gate }
44400Sstevel@tonic-gate /* Is there a FIN that needs to be to re retransmitted? */
44410Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
44420Sstevel@tonic-gate !tcp->tcp_fin_acked)
44430Sstevel@tonic-gate break;
44440Sstevel@tonic-gate /* Nothing to do, return without restarting timer. */
44450Sstevel@tonic-gate return;
44460Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
44470Sstevel@tonic-gate /*
44480Sstevel@tonic-gate * User closed the TCP endpoint and peer ACK'ed our FIN.
44490Sstevel@tonic-gate * We waited some time for for peer's FIN, but it hasn't
44500Sstevel@tonic-gate * arrived. We flush the connection now to avoid
44510Sstevel@tonic-gate * case where the peer has rebooted.
44520Sstevel@tonic-gate */
44530Sstevel@tonic-gate /* FALLTHRU */
44540Sstevel@tonic-gate case TCPS_TIME_WAIT:
44550Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
44560Sstevel@tonic-gate return;
44570Sstevel@tonic-gate default:
44580Sstevel@tonic-gate DEBUG_3("tcp_timer (%d): strange state (%d) %s", sock_id,
44590Sstevel@tonic-gate tcp->tcp_state, tcp_display(tcp, NULL,
44600Sstevel@tonic-gate DISP_PORT_ONLY));
44610Sstevel@tonic-gate return;
44620Sstevel@tonic-gate }
44630Sstevel@tonic-gate if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) {
44640Sstevel@tonic-gate /*
44650Sstevel@tonic-gate * For zero window probe, we need to send indefinitely,
44660Sstevel@tonic-gate * unless we have not heard from the other side for some
44670Sstevel@tonic-gate * time...
44680Sstevel@tonic-gate */
44690Sstevel@tonic-gate if ((tcp->tcp_zero_win_probe == 0) ||
44700Sstevel@tonic-gate ((prom_gettime() - tcp->tcp_last_recv_time) >
44710Sstevel@tonic-gate second_threshold)) {
44720Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpTimRetransDrop);
44730Sstevel@tonic-gate /*
44740Sstevel@tonic-gate * If TCP is in SYN_RCVD state, send back a
44750Sstevel@tonic-gate * RST|ACK as BSD does. Note that tcp_zero_win_probe
44760Sstevel@tonic-gate * should be zero in TCPS_SYN_RCVD state.
44770Sstevel@tonic-gate */
44780Sstevel@tonic-gate if (tcp->tcp_state == TCPS_SYN_RCVD) {
44790Sstevel@tonic-gate tcp_xmit_ctl("tcp_timer: RST sent on timeout "
44800Sstevel@tonic-gate "in SYN_RCVD",
44810Sstevel@tonic-gate tcp, NULL, tcp->tcp_snxt,
44820Sstevel@tonic-gate tcp->tcp_rnxt, TH_RST | TH_ACK, 0, sock_id);
44830Sstevel@tonic-gate }
44840Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp,
44850Sstevel@tonic-gate tcp->tcp_client_errno ?
44860Sstevel@tonic-gate tcp->tcp_client_errno : ETIMEDOUT);
44870Sstevel@tonic-gate return;
44880Sstevel@tonic-gate } else {
44890Sstevel@tonic-gate /*
44900Sstevel@tonic-gate * Set tcp_ms_we_have_waited to second_threshold
44910Sstevel@tonic-gate * so that in next timeout, we will do the above
44920Sstevel@tonic-gate * check (lbolt - tcp_last_recv_time). This is
44930Sstevel@tonic-gate * also to avoid overflow.
44940Sstevel@tonic-gate *
44950Sstevel@tonic-gate * We don't need to decrement tcp_timer_backoff
44960Sstevel@tonic-gate * to avoid overflow because it will be decremented
44970Sstevel@tonic-gate * later if new timeout value is greater than
44980Sstevel@tonic-gate * tcp_rexmit_interval_max. In the case when
44990Sstevel@tonic-gate * tcp_rexmit_interval_max is greater than
45000Sstevel@tonic-gate * second_threshold, it means that we will wait
45010Sstevel@tonic-gate * longer than second_threshold to send the next
45020Sstevel@tonic-gate * window probe.
45030Sstevel@tonic-gate */
45040Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = second_threshold;
45050Sstevel@tonic-gate }
45060Sstevel@tonic-gate } else if (ms > first_threshold && tcp->tcp_rtt_sa != 0) {
45070Sstevel@tonic-gate /*
45080Sstevel@tonic-gate * We have been retransmitting for too long... The RTT
45090Sstevel@tonic-gate * we calculated is probably incorrect. Reinitialize it.
45100Sstevel@tonic-gate * Need to compensate for 0 tcp_rtt_sa. Reset
45110Sstevel@tonic-gate * tcp_rtt_update so that we won't accidentally cache a
45120Sstevel@tonic-gate * bad value. But only do this if this is not a zero
45130Sstevel@tonic-gate * window probe.
45140Sstevel@tonic-gate */
45150Sstevel@tonic-gate if (tcp->tcp_zero_win_probe == 0) {
45160Sstevel@tonic-gate tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) +
45170Sstevel@tonic-gate (tcp->tcp_rtt_sa >> 5);
45180Sstevel@tonic-gate tcp->tcp_rtt_sa = 0;
45190Sstevel@tonic-gate tcp->tcp_rtt_update = 0;
45200Sstevel@tonic-gate }
45210Sstevel@tonic-gate }
45220Sstevel@tonic-gate tcp->tcp_timer_backoff++;
45230Sstevel@tonic-gate if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
45240Sstevel@tonic-gate tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) <
45250Sstevel@tonic-gate tcp_rexmit_interval_min) {
45260Sstevel@tonic-gate /*
45270Sstevel@tonic-gate * This means the original RTO is tcp_rexmit_interval_min.
45280Sstevel@tonic-gate * So we will use tcp_rexmit_interval_min as the RTO value
45290Sstevel@tonic-gate * and do the backoff.
45300Sstevel@tonic-gate */
45310Sstevel@tonic-gate ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff;
45320Sstevel@tonic-gate } else {
45330Sstevel@tonic-gate ms <<= tcp->tcp_timer_backoff;
45340Sstevel@tonic-gate }
45350Sstevel@tonic-gate if (ms > tcp_rexmit_interval_max) {
45360Sstevel@tonic-gate ms = tcp_rexmit_interval_max;
45370Sstevel@tonic-gate /*
45380Sstevel@tonic-gate * ms is at max, decrement tcp_timer_backoff to avoid
45390Sstevel@tonic-gate * overflow.
45400Sstevel@tonic-gate */
45410Sstevel@tonic-gate tcp->tcp_timer_backoff--;
45420Sstevel@tonic-gate }
45430Sstevel@tonic-gate tcp->tcp_ms_we_have_waited += ms;
45440Sstevel@tonic-gate if (tcp->tcp_zero_win_probe == 0) {
45450Sstevel@tonic-gate tcp->tcp_rto = ms;
45460Sstevel@tonic-gate }
45470Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, ms);
45480Sstevel@tonic-gate /*
45490Sstevel@tonic-gate * This is after a timeout and tcp_rto is backed off. Set
45500Sstevel@tonic-gate * tcp_set_timer to 1 so that next time RTO is updated, we will
45510Sstevel@tonic-gate * restart the timer with a correct value.
45520Sstevel@tonic-gate */
45530Sstevel@tonic-gate tcp->tcp_set_timer = 1;
45540Sstevel@tonic-gate mss = tcp->tcp_snxt - tcp->tcp_suna;
45550Sstevel@tonic-gate if (mss > tcp->tcp_mss)
45560Sstevel@tonic-gate mss = tcp->tcp_mss;
45570Sstevel@tonic-gate if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0)
45580Sstevel@tonic-gate mss = tcp->tcp_swnd;
45590Sstevel@tonic-gate
4560785Seota if ((mp = tcp->tcp_xmit_head) != NULL) {
4561785Seota /* use uintptr_t to suppress the gcc warning */
4562785Seota mp->b_prev = (mblk_t *)(uintptr_t)prom_gettime();
4563785Seota }
45640Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss,
45650Sstevel@tonic-gate B_TRUE);
45660Sstevel@tonic-gate if (mp == NULL)
45670Sstevel@tonic-gate return;
45680Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
45690Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRetransSegs);
45700Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpRetransBytes, mss);
45710Sstevel@tonic-gate /* Dump the packet when debugging. */
45720Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_timer", mp);
45730Sstevel@tonic-gate
45740Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
45750Sstevel@tonic-gate freeb(mp);
45760Sstevel@tonic-gate
45770Sstevel@tonic-gate /*
45780Sstevel@tonic-gate * When slow start after retransmission begins, start with
45790Sstevel@tonic-gate * this seq no. tcp_rexmit_max marks the end of special slow
45800Sstevel@tonic-gate * start phase. tcp_snd_burst controls how many segments
45810Sstevel@tonic-gate * can be sent because of an ack.
45820Sstevel@tonic-gate */
45830Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_suna;
45840Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_SS;
45850Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
45860Sstevel@tonic-gate (tcp->tcp_unsent == 0)) {
45870Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_fss;
45880Sstevel@tonic-gate } else {
45890Sstevel@tonic-gate tcp->tcp_rexmit_max = tcp->tcp_snxt;
45900Sstevel@tonic-gate }
45910Sstevel@tonic-gate tcp->tcp_rexmit = B_TRUE;
45920Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
45930Sstevel@tonic-gate
45940Sstevel@tonic-gate /*
45950Sstevel@tonic-gate * Remove all rexmit SACK blk to start from fresh.
45960Sstevel@tonic-gate */
45970Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
45980Sstevel@tonic-gate TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list);
45990Sstevel@tonic-gate tcp->tcp_num_notsack_blk = 0;
46000Sstevel@tonic-gate tcp->tcp_cnt_notsack_list = 0;
46010Sstevel@tonic-gate }
46020Sstevel@tonic-gate }
46030Sstevel@tonic-gate
46040Sstevel@tonic-gate /*
46050Sstevel@tonic-gate * The TCP normal data output path.
46060Sstevel@tonic-gate * NOTE: the logic of the fast path is duplicated from this function.
46070Sstevel@tonic-gate */
46080Sstevel@tonic-gate static void
tcp_wput_data(tcp_t * tcp,mblk_t * mp,int sock_id)46090Sstevel@tonic-gate tcp_wput_data(tcp_t *tcp, mblk_t *mp, int sock_id)
46100Sstevel@tonic-gate {
46110Sstevel@tonic-gate int len;
46120Sstevel@tonic-gate mblk_t *local_time;
46130Sstevel@tonic-gate mblk_t *mp1;
46140Sstevel@tonic-gate uchar_t *rptr;
46150Sstevel@tonic-gate uint32_t snxt;
46160Sstevel@tonic-gate int tail_unsent;
46170Sstevel@tonic-gate int tcpstate;
46180Sstevel@tonic-gate int usable = 0;
46190Sstevel@tonic-gate mblk_t *xmit_tail;
46200Sstevel@tonic-gate int32_t num_burst_seg;
46210Sstevel@tonic-gate int32_t mss;
46220Sstevel@tonic-gate int32_t num_sack_blk = 0;
46230Sstevel@tonic-gate int32_t tcp_hdr_len;
46240Sstevel@tonic-gate ipaddr_t *dst;
46250Sstevel@tonic-gate ipaddr_t *src;
46260Sstevel@tonic-gate
46270Sstevel@tonic-gate #ifdef DEBUG
46280Sstevel@tonic-gate printf("tcp_wput_data(%d) ##############################\n", sock_id);
46290Sstevel@tonic-gate #endif
46300Sstevel@tonic-gate tcpstate = tcp->tcp_state;
46310Sstevel@tonic-gate if (mp == NULL) {
46320Sstevel@tonic-gate /* Really tacky... but we need this for detached closes. */
46330Sstevel@tonic-gate len = tcp->tcp_unsent;
46340Sstevel@tonic-gate goto data_null;
46350Sstevel@tonic-gate }
46360Sstevel@tonic-gate
46370Sstevel@tonic-gate /*
46380Sstevel@tonic-gate * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ,
46390Sstevel@tonic-gate * or before a connection attempt has begun.
46400Sstevel@tonic-gate *
46410Sstevel@tonic-gate * The following should not happen in inetboot....
46420Sstevel@tonic-gate */
46430Sstevel@tonic-gate if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT ||
46440Sstevel@tonic-gate (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
46450Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) {
46460Sstevel@tonic-gate printf("tcp_wput_data: data after ordrel, %s\n",
46470Sstevel@tonic-gate tcp_display(tcp, NULL, DISP_ADDR_AND_PORT));
46480Sstevel@tonic-gate }
46490Sstevel@tonic-gate freemsg(mp);
46500Sstevel@tonic-gate return;
46510Sstevel@tonic-gate }
46520Sstevel@tonic-gate
46530Sstevel@tonic-gate /* Strip empties */
46540Sstevel@tonic-gate for (;;) {
46550Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
46560Sstevel@tonic-gate (uintptr_t)INT_MAX);
46570Sstevel@tonic-gate len = (int)(mp->b_wptr - mp->b_rptr);
46580Sstevel@tonic-gate if (len > 0)
46590Sstevel@tonic-gate break;
46600Sstevel@tonic-gate mp1 = mp;
46610Sstevel@tonic-gate mp = mp->b_cont;
46620Sstevel@tonic-gate freeb(mp1);
46630Sstevel@tonic-gate if (mp == NULL) {
46640Sstevel@tonic-gate return;
46650Sstevel@tonic-gate }
46660Sstevel@tonic-gate }
46670Sstevel@tonic-gate
46680Sstevel@tonic-gate /* If we are the first on the list ... */
46690Sstevel@tonic-gate if (tcp->tcp_xmit_head == NULL) {
46700Sstevel@tonic-gate tcp->tcp_xmit_head = mp;
46710Sstevel@tonic-gate tcp->tcp_xmit_tail = mp;
46720Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = len;
46730Sstevel@tonic-gate } else {
46740Sstevel@tonic-gate tcp->tcp_xmit_last->b_cont = mp;
46750Sstevel@tonic-gate len += tcp->tcp_unsent;
46760Sstevel@tonic-gate }
46770Sstevel@tonic-gate
46780Sstevel@tonic-gate /* Tack on however many more positive length mblks we have */
46790Sstevel@tonic-gate if ((mp1 = mp->b_cont) != NULL) {
46800Sstevel@tonic-gate do {
46810Sstevel@tonic-gate int tlen;
46820Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
46830Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
46840Sstevel@tonic-gate tlen = (int)(mp1->b_wptr - mp1->b_rptr);
46850Sstevel@tonic-gate if (tlen <= 0) {
46860Sstevel@tonic-gate mp->b_cont = mp1->b_cont;
46870Sstevel@tonic-gate freeb(mp1);
46880Sstevel@tonic-gate } else {
46890Sstevel@tonic-gate len += tlen;
46900Sstevel@tonic-gate mp = mp1;
46910Sstevel@tonic-gate }
46920Sstevel@tonic-gate } while ((mp1 = mp->b_cont) != NULL);
46930Sstevel@tonic-gate }
46940Sstevel@tonic-gate tcp->tcp_xmit_last = mp;
46950Sstevel@tonic-gate tcp->tcp_unsent = len;
46960Sstevel@tonic-gate
46970Sstevel@tonic-gate data_null:
46980Sstevel@tonic-gate snxt = tcp->tcp_snxt;
46990Sstevel@tonic-gate xmit_tail = tcp->tcp_xmit_tail;
47000Sstevel@tonic-gate tail_unsent = tcp->tcp_xmit_tail_unsent;
47010Sstevel@tonic-gate
47020Sstevel@tonic-gate /*
47030Sstevel@tonic-gate * Note that tcp_mss has been adjusted to take into account the
47040Sstevel@tonic-gate * timestamp option if applicable. Because SACK options do not
47050Sstevel@tonic-gate * appear in every TCP segments and they are of variable lengths,
47060Sstevel@tonic-gate * they cannot be included in tcp_mss. Thus we need to calculate
47070Sstevel@tonic-gate * the actual segment length when we need to send a segment which
47080Sstevel@tonic-gate * includes SACK options.
47090Sstevel@tonic-gate */
47100Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
47110Sstevel@tonic-gate int32_t opt_len;
47120Sstevel@tonic-gate
47130Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
47140Sstevel@tonic-gate tcp->tcp_num_sack_blk);
47150Sstevel@tonic-gate opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN *
47160Sstevel@tonic-gate 2 + TCPOPT_HEADER_LEN;
47170Sstevel@tonic-gate mss = tcp->tcp_mss - opt_len;
47180Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len + opt_len;
47190Sstevel@tonic-gate } else {
47200Sstevel@tonic-gate mss = tcp->tcp_mss;
47210Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
47220Sstevel@tonic-gate }
47230Sstevel@tonic-gate
47240Sstevel@tonic-gate if ((tcp->tcp_suna == snxt) &&
47250Sstevel@tonic-gate (prom_gettime() - tcp->tcp_last_recv_time) >= tcp->tcp_rto) {
47260Sstevel@tonic-gate tcp->tcp_cwnd = MIN(tcp_slow_start_after_idle * mss,
47270Sstevel@tonic-gate MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss)));
47280Sstevel@tonic-gate }
47290Sstevel@tonic-gate if (tcpstate == TCPS_SYN_RCVD) {
47300Sstevel@tonic-gate /*
47310Sstevel@tonic-gate * The three-way connection establishment handshake is not
47320Sstevel@tonic-gate * complete yet. We want to queue the data for transmission
47330Sstevel@tonic-gate * after entering ESTABLISHED state (RFC793). Setting usable to
47340Sstevel@tonic-gate * zero cause a jump to "done" label effectively leaving data
47350Sstevel@tonic-gate * on the queue.
47360Sstevel@tonic-gate */
47370Sstevel@tonic-gate
47380Sstevel@tonic-gate usable = 0;
47390Sstevel@tonic-gate } else {
47400Sstevel@tonic-gate int usable_r = tcp->tcp_swnd;
47410Sstevel@tonic-gate
47420Sstevel@tonic-gate /*
47430Sstevel@tonic-gate * In the special case when cwnd is zero, which can only
47440Sstevel@tonic-gate * happen if the connection is ECN capable, return now.
47450Sstevel@tonic-gate * New segments is sent using tcp_timer(). The timer
47460Sstevel@tonic-gate * is set in tcp_rput_data().
47470Sstevel@tonic-gate */
47480Sstevel@tonic-gate if (tcp->tcp_cwnd == 0) {
47490Sstevel@tonic-gate /*
47500Sstevel@tonic-gate * Note that tcp_cwnd is 0 before 3-way handshake is
47510Sstevel@tonic-gate * finished.
47520Sstevel@tonic-gate */
47530Sstevel@tonic-gate assert(tcp->tcp_ecn_ok ||
47540Sstevel@tonic-gate tcp->tcp_state < TCPS_ESTABLISHED);
47550Sstevel@tonic-gate return;
47560Sstevel@tonic-gate }
47570Sstevel@tonic-gate
47580Sstevel@tonic-gate /* usable = MIN(swnd, cwnd) - unacked_bytes */
47590Sstevel@tonic-gate if (usable_r > tcp->tcp_cwnd)
47600Sstevel@tonic-gate usable_r = tcp->tcp_cwnd;
47610Sstevel@tonic-gate
47620Sstevel@tonic-gate /* NOTE: trouble if xmitting while SYN not acked? */
47630Sstevel@tonic-gate usable_r -= snxt;
47640Sstevel@tonic-gate usable_r += tcp->tcp_suna;
47650Sstevel@tonic-gate
47660Sstevel@tonic-gate /* usable = MIN(usable, unsent) */
47670Sstevel@tonic-gate if (usable_r > len)
47680Sstevel@tonic-gate usable_r = len;
47690Sstevel@tonic-gate
47700Sstevel@tonic-gate /* usable = MAX(usable, {1 for urgent, 0 for data}) */
47710Sstevel@tonic-gate if (usable_r != 0)
47720Sstevel@tonic-gate usable = usable_r;
47730Sstevel@tonic-gate }
47740Sstevel@tonic-gate
4775785Seota /* use uintptr_t to suppress the gcc warning */
4776785Seota local_time = (mblk_t *)(uintptr_t)prom_gettime();
47770Sstevel@tonic-gate
47780Sstevel@tonic-gate /*
47790Sstevel@tonic-gate * "Our" Nagle Algorithm. This is not the same as in the old
47800Sstevel@tonic-gate * BSD. This is more in line with the true intent of Nagle.
47810Sstevel@tonic-gate *
47820Sstevel@tonic-gate * The conditions are:
47830Sstevel@tonic-gate * 1. The amount of unsent data (or amount of data which can be
47840Sstevel@tonic-gate * sent, whichever is smaller) is less than Nagle limit.
47850Sstevel@tonic-gate * 2. The last sent size is also less than Nagle limit.
47860Sstevel@tonic-gate * 3. There is unack'ed data.
47870Sstevel@tonic-gate * 4. Urgent pointer is not set. Send urgent data ignoring the
47880Sstevel@tonic-gate * Nagle algorithm. This reduces the probability that urgent
47890Sstevel@tonic-gate * bytes get "merged" together.
47900Sstevel@tonic-gate * 5. The app has not closed the connection. This eliminates the
47910Sstevel@tonic-gate * wait time of the receiving side waiting for the last piece of
47920Sstevel@tonic-gate * (small) data.
47930Sstevel@tonic-gate *
47940Sstevel@tonic-gate * If all are satisified, exit without sending anything. Note
47950Sstevel@tonic-gate * that Nagle limit can be smaller than 1 MSS. Nagle limit is
47960Sstevel@tonic-gate * the smaller of 1 MSS and global tcp_naglim_def (default to be
47970Sstevel@tonic-gate * 4095).
47980Sstevel@tonic-gate */
47990Sstevel@tonic-gate if (usable < (int)tcp->tcp_naglim &&
48000Sstevel@tonic-gate tcp->tcp_naglim > tcp->tcp_last_sent_len &&
48010Sstevel@tonic-gate snxt != tcp->tcp_suna &&
48020Sstevel@tonic-gate !(tcp->tcp_valid_bits & TCP_URG_VALID))
48030Sstevel@tonic-gate goto done;
48040Sstevel@tonic-gate
48050Sstevel@tonic-gate num_burst_seg = tcp->tcp_snd_burst;
48060Sstevel@tonic-gate for (;;) {
48070Sstevel@tonic-gate tcph_t *tcph;
48080Sstevel@tonic-gate mblk_t *new_mp;
48090Sstevel@tonic-gate
48100Sstevel@tonic-gate if (num_burst_seg-- == 0)
48110Sstevel@tonic-gate goto done;
48120Sstevel@tonic-gate
48130Sstevel@tonic-gate len = mss;
48140Sstevel@tonic-gate if (len > usable) {
48150Sstevel@tonic-gate len = usable;
48160Sstevel@tonic-gate if (len <= 0) {
48170Sstevel@tonic-gate /* Terminate the loop */
48180Sstevel@tonic-gate goto done;
48190Sstevel@tonic-gate }
48200Sstevel@tonic-gate /*
48210Sstevel@tonic-gate * Sender silly-window avoidance.
48220Sstevel@tonic-gate * Ignore this if we are going to send a
48230Sstevel@tonic-gate * zero window probe out.
48240Sstevel@tonic-gate *
48250Sstevel@tonic-gate * TODO: force data into microscopic window ??
48260Sstevel@tonic-gate * ==> (!pushed || (unsent > usable))
48270Sstevel@tonic-gate */
48280Sstevel@tonic-gate if (len < (tcp->tcp_max_swnd >> 1) &&
48290Sstevel@tonic-gate (tcp->tcp_unsent - (snxt - tcp->tcp_snxt)) > len &&
48300Sstevel@tonic-gate !((tcp->tcp_valid_bits & TCP_URG_VALID) &&
48310Sstevel@tonic-gate len == 1) && (! tcp->tcp_zero_win_probe)) {
48320Sstevel@tonic-gate /*
48330Sstevel@tonic-gate * If the retransmit timer is not running
48340Sstevel@tonic-gate * we start it so that we will retransmit
48350Sstevel@tonic-gate * in the case when the the receiver has
48360Sstevel@tonic-gate * decremented the window.
48370Sstevel@tonic-gate */
48380Sstevel@tonic-gate if (snxt == tcp->tcp_snxt &&
48390Sstevel@tonic-gate snxt == tcp->tcp_suna) {
48400Sstevel@tonic-gate /*
48410Sstevel@tonic-gate * We are not supposed to send
48420Sstevel@tonic-gate * anything. So let's wait a little
48430Sstevel@tonic-gate * bit longer before breaking SWS
48440Sstevel@tonic-gate * avoidance.
48450Sstevel@tonic-gate *
48460Sstevel@tonic-gate * What should the value be?
48470Sstevel@tonic-gate * Suggestion: MAX(init rexmit time,
48480Sstevel@tonic-gate * tcp->tcp_rto)
48490Sstevel@tonic-gate */
48500Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
48510Sstevel@tonic-gate }
48520Sstevel@tonic-gate goto done;
48530Sstevel@tonic-gate }
48540Sstevel@tonic-gate }
48550Sstevel@tonic-gate
48560Sstevel@tonic-gate tcph = tcp->tcp_tcph;
48570Sstevel@tonic-gate
48580Sstevel@tonic-gate usable -= len; /* Approximate - can be adjusted later */
48590Sstevel@tonic-gate if (usable > 0)
48600Sstevel@tonic-gate tcph->th_flags[0] = TH_ACK;
48610Sstevel@tonic-gate else
48620Sstevel@tonic-gate tcph->th_flags[0] = (TH_ACK | TH_PUSH);
48630Sstevel@tonic-gate
48640Sstevel@tonic-gate U32_TO_ABE32(snxt, tcph->th_seq);
48650Sstevel@tonic-gate
48660Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
48670Sstevel@tonic-gate uchar_t *prev_rptr = xmit_tail->b_rptr;
48680Sstevel@tonic-gate uint32_t prev_snxt = tcp->tcp_snxt;
48690Sstevel@tonic-gate
48700Sstevel@tonic-gate if (tail_unsent == 0) {
48710Sstevel@tonic-gate assert(xmit_tail->b_cont != NULL);
48720Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
48730Sstevel@tonic-gate prev_rptr = xmit_tail->b_rptr;
48740Sstevel@tonic-gate tail_unsent = (int)(xmit_tail->b_wptr -
48750Sstevel@tonic-gate xmit_tail->b_rptr);
48760Sstevel@tonic-gate } else {
48770Sstevel@tonic-gate xmit_tail->b_rptr = xmit_tail->b_wptr -
48780Sstevel@tonic-gate tail_unsent;
48790Sstevel@tonic-gate }
48800Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, xmit_tail, len, NULL, NULL,
48810Sstevel@tonic-gate snxt, B_FALSE, (uint32_t *)&len, B_FALSE);
48820Sstevel@tonic-gate /* Restore tcp_snxt so we get amount sent right. */
48830Sstevel@tonic-gate tcp->tcp_snxt = prev_snxt;
48840Sstevel@tonic-gate if (prev_rptr == xmit_tail->b_rptr)
48850Sstevel@tonic-gate xmit_tail->b_prev = local_time;
48860Sstevel@tonic-gate else
48870Sstevel@tonic-gate xmit_tail->b_rptr = prev_rptr;
48880Sstevel@tonic-gate
48890Sstevel@tonic-gate if (mp == NULL)
48900Sstevel@tonic-gate break;
48910Sstevel@tonic-gate
48920Sstevel@tonic-gate mp1 = mp->b_cont;
48930Sstevel@tonic-gate
48940Sstevel@tonic-gate snxt += len;
48950Sstevel@tonic-gate tcp->tcp_last_sent_len = (ushort_t)len;
48960Sstevel@tonic-gate while (mp1->b_cont) {
48970Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
48980Sstevel@tonic-gate xmit_tail->b_prev = local_time;
48990Sstevel@tonic-gate mp1 = mp1->b_cont;
49000Sstevel@tonic-gate }
49010Sstevel@tonic-gate tail_unsent = xmit_tail->b_wptr - mp1->b_wptr;
49020Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutDataSegs);
49030Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes, len);
49040Sstevel@tonic-gate /* Dump the packet when debugging. */
49050Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_wput_data (valid bits)", mp);
49060Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
49070Sstevel@tonic-gate freeb(mp);
49080Sstevel@tonic-gate continue;
49090Sstevel@tonic-gate }
49100Sstevel@tonic-gate
49110Sstevel@tonic-gate snxt += len; /* Adjust later if we don't send all of len */
49120Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutDataSegs);
49130Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes, len);
49140Sstevel@tonic-gate
49150Sstevel@tonic-gate if (tail_unsent) {
49160Sstevel@tonic-gate /* Are the bytes above us in flight? */
49170Sstevel@tonic-gate rptr = xmit_tail->b_wptr - tail_unsent;
49180Sstevel@tonic-gate if (rptr != xmit_tail->b_rptr) {
49190Sstevel@tonic-gate tail_unsent -= len;
49200Sstevel@tonic-gate len += tcp_hdr_len;
49210Sstevel@tonic-gate tcp->tcp_ipha->ip_len = htons(len);
49220Sstevel@tonic-gate mp = dupb(xmit_tail);
49230Sstevel@tonic-gate if (!mp)
49240Sstevel@tonic-gate break;
49250Sstevel@tonic-gate mp->b_rptr = rptr;
49260Sstevel@tonic-gate goto must_alloc;
49270Sstevel@tonic-gate }
49280Sstevel@tonic-gate } else {
49290Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
49300Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
49310Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
49320Sstevel@tonic-gate tail_unsent = (int)(xmit_tail->b_wptr -
49330Sstevel@tonic-gate xmit_tail->b_rptr);
49340Sstevel@tonic-gate }
49350Sstevel@tonic-gate
49360Sstevel@tonic-gate tail_unsent -= len;
49370Sstevel@tonic-gate tcp->tcp_last_sent_len = (ushort_t)len;
49380Sstevel@tonic-gate
49390Sstevel@tonic-gate len += tcp_hdr_len;
49400Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
49410Sstevel@tonic-gate tcp->tcp_ipha->ip_len = htons(len);
49420Sstevel@tonic-gate
49430Sstevel@tonic-gate xmit_tail->b_prev = local_time;
49440Sstevel@tonic-gate
49450Sstevel@tonic-gate mp = dupb(xmit_tail);
49460Sstevel@tonic-gate if (mp == NULL)
49470Sstevel@tonic-gate goto out_of_mem;
49480Sstevel@tonic-gate
49490Sstevel@tonic-gate len = tcp_hdr_len;
49500Sstevel@tonic-gate /*
49510Sstevel@tonic-gate * There are four reasons to allocate a new hdr mblk:
49520Sstevel@tonic-gate * 1) The bytes above us are in use by another packet
49530Sstevel@tonic-gate * 2) We don't have good alignment
49540Sstevel@tonic-gate * 3) The mblk is being shared
49550Sstevel@tonic-gate * 4) We don't have enough room for a header
49560Sstevel@tonic-gate */
49570Sstevel@tonic-gate rptr = mp->b_rptr - len;
49580Sstevel@tonic-gate if (!OK_32PTR(rptr) ||
49590Sstevel@tonic-gate rptr < mp->b_datap) {
49600Sstevel@tonic-gate /* NOTE: we assume allocb returns an OK_32PTR */
49610Sstevel@tonic-gate
49620Sstevel@tonic-gate must_alloc:;
49630Sstevel@tonic-gate mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH +
49640Sstevel@tonic-gate tcp_wroff_xtra, 0);
49650Sstevel@tonic-gate if (mp1 == NULL) {
49660Sstevel@tonic-gate freemsg(mp);
49670Sstevel@tonic-gate goto out_of_mem;
49680Sstevel@tonic-gate }
49690Sstevel@tonic-gate mp1->b_cont = mp;
49700Sstevel@tonic-gate mp = mp1;
49710Sstevel@tonic-gate /* Leave room for Link Level header */
49720Sstevel@tonic-gate len = tcp_hdr_len;
49730Sstevel@tonic-gate rptr = &mp->b_rptr[tcp_wroff_xtra];
49740Sstevel@tonic-gate mp->b_wptr = &rptr[len];
49750Sstevel@tonic-gate }
49760Sstevel@tonic-gate
49770Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
4978785Seota /* use uintptr_t to suppress the gcc warning */
4979785Seota U32_TO_BE32((uint32_t)(uintptr_t)local_time,
49800Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
49810Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
49820Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
49830Sstevel@tonic-gate } else {
49840Sstevel@tonic-gate assert(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH);
49850Sstevel@tonic-gate }
49860Sstevel@tonic-gate
49870Sstevel@tonic-gate mp->b_rptr = rptr;
49880Sstevel@tonic-gate
49890Sstevel@tonic-gate /* Copy the template header. */
49900Sstevel@tonic-gate dst = (ipaddr_t *)rptr;
49910Sstevel@tonic-gate src = (ipaddr_t *)tcp->tcp_iphc;
49920Sstevel@tonic-gate dst[0] = src[0];
49930Sstevel@tonic-gate dst[1] = src[1];
49940Sstevel@tonic-gate dst[2] = src[2];
49950Sstevel@tonic-gate dst[3] = src[3];
49960Sstevel@tonic-gate dst[4] = src[4];
49970Sstevel@tonic-gate dst[5] = src[5];
49980Sstevel@tonic-gate dst[6] = src[6];
49990Sstevel@tonic-gate dst[7] = src[7];
50000Sstevel@tonic-gate dst[8] = src[8];
50010Sstevel@tonic-gate dst[9] = src[9];
50020Sstevel@tonic-gate len = tcp->tcp_hdr_len;
50030Sstevel@tonic-gate if (len -= 40) {
50040Sstevel@tonic-gate len >>= 2;
50050Sstevel@tonic-gate dst += 10;
50060Sstevel@tonic-gate src += 10;
50070Sstevel@tonic-gate do {
50080Sstevel@tonic-gate *dst++ = *src++;
50090Sstevel@tonic-gate } while (--len);
50100Sstevel@tonic-gate }
50110Sstevel@tonic-gate
50120Sstevel@tonic-gate /*
50130Sstevel@tonic-gate * Set tcph to point to the header of the outgoing packet,
50140Sstevel@tonic-gate * not to the template header.
50150Sstevel@tonic-gate */
50160Sstevel@tonic-gate tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len);
50170Sstevel@tonic-gate
50180Sstevel@tonic-gate /*
50190Sstevel@tonic-gate * Set the ECN info in the TCP header if it is not a zero
50200Sstevel@tonic-gate * window probe. Zero window probe is only sent in
50210Sstevel@tonic-gate * tcp_wput_data() and tcp_timer().
50220Sstevel@tonic-gate */
50230Sstevel@tonic-gate if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) {
50240Sstevel@tonic-gate SET_ECT(tcp, rptr);
50250Sstevel@tonic-gate
50260Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
50270Sstevel@tonic-gate tcph->th_flags[0] |= TH_ECE;
50280Sstevel@tonic-gate if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
50290Sstevel@tonic-gate tcph->th_flags[0] |= TH_CWR;
50300Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_TRUE;
50310Sstevel@tonic-gate }
50320Sstevel@tonic-gate }
50330Sstevel@tonic-gate
50340Sstevel@tonic-gate /* Fill in SACK options */
50350Sstevel@tonic-gate if (num_sack_blk > 0) {
50360Sstevel@tonic-gate uchar_t *wptr = rptr + tcp->tcp_hdr_len;
50370Sstevel@tonic-gate sack_blk_t *tmp;
50380Sstevel@tonic-gate int32_t i;
50390Sstevel@tonic-gate
50400Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
50410Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
50420Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
50430Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
50440Sstevel@tonic-gate sizeof (sack_blk_t);
50450Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
50460Sstevel@tonic-gate
50470Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
50480Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
50490Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
50500Sstevel@tonic-gate wptr += sizeof (tcp_seq);
50510Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
50520Sstevel@tonic-gate wptr += sizeof (tcp_seq);
50530Sstevel@tonic-gate }
50540Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1)
50550Sstevel@tonic-gate << 4);
50560Sstevel@tonic-gate }
50570Sstevel@tonic-gate
50580Sstevel@tonic-gate if (tail_unsent) {
50590Sstevel@tonic-gate mp1 = mp->b_cont;
50600Sstevel@tonic-gate if (mp1 == NULL)
50610Sstevel@tonic-gate mp1 = mp;
50620Sstevel@tonic-gate /*
50630Sstevel@tonic-gate * If we're a little short, tack on more mblks
50640Sstevel@tonic-gate * as long as we don't need to split an mblk.
50650Sstevel@tonic-gate */
50660Sstevel@tonic-gate while (tail_unsent < 0 &&
50670Sstevel@tonic-gate tail_unsent + (int)(xmit_tail->b_cont->b_wptr -
50680Sstevel@tonic-gate xmit_tail->b_cont->b_rptr) <= 0) {
50690Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
50700Sstevel@tonic-gate /* Stash for rtt use later */
50710Sstevel@tonic-gate xmit_tail->b_prev = local_time;
50720Sstevel@tonic-gate mp1->b_cont = dupb(xmit_tail);
50730Sstevel@tonic-gate mp1 = mp1->b_cont;
50740Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
50750Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
50760Sstevel@tonic-gate tail_unsent += (int)(xmit_tail->b_wptr -
50770Sstevel@tonic-gate xmit_tail->b_rptr);
50780Sstevel@tonic-gate if (mp1 == NULL) {
50790Sstevel@tonic-gate freemsg(mp);
50800Sstevel@tonic-gate goto out_of_mem;
50810Sstevel@tonic-gate }
50820Sstevel@tonic-gate }
50830Sstevel@tonic-gate /* Trim back any surplus on the last mblk */
50840Sstevel@tonic-gate if (tail_unsent > 0)
50850Sstevel@tonic-gate mp1->b_wptr -= tail_unsent;
50860Sstevel@tonic-gate if (tail_unsent < 0) {
50870Sstevel@tonic-gate uint32_t ip_len;
50880Sstevel@tonic-gate
50890Sstevel@tonic-gate /*
50900Sstevel@tonic-gate * We did not send everything we could in
50910Sstevel@tonic-gate * order to preserve mblk boundaries.
50920Sstevel@tonic-gate */
50930Sstevel@tonic-gate usable -= tail_unsent;
50940Sstevel@tonic-gate snxt += tail_unsent;
50950Sstevel@tonic-gate tcp->tcp_last_sent_len += tail_unsent;
50960Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpOutDataBytes,
50970Sstevel@tonic-gate tail_unsent);
50980Sstevel@tonic-gate /* Adjust the IP length field. */
50990Sstevel@tonic-gate ip_len = ntohs(((struct ip *)rptr)->ip_len) +
51000Sstevel@tonic-gate tail_unsent;
51010Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(ip_len);
51020Sstevel@tonic-gate tail_unsent = 0;
51030Sstevel@tonic-gate }
51040Sstevel@tonic-gate }
51050Sstevel@tonic-gate
51060Sstevel@tonic-gate if (mp == NULL)
51070Sstevel@tonic-gate goto out_of_mem;
51080Sstevel@tonic-gate
51090Sstevel@tonic-gate /*
51100Sstevel@tonic-gate * Performance hit! We need to pullup the whole message
51110Sstevel@tonic-gate * in order to do checksum and for the MAC output routine.
51120Sstevel@tonic-gate */
51130Sstevel@tonic-gate if (mp->b_cont != NULL) {
51140Sstevel@tonic-gate int mp_size;
51150Sstevel@tonic-gate #ifdef DEBUG
51160Sstevel@tonic-gate printf("Multiple mblk %d\n", msgdsize(mp));
51170Sstevel@tonic-gate #endif
51180Sstevel@tonic-gate new_mp = allocb(msgdsize(mp) + tcp_wroff_xtra, 0);
51190Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
51200Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr;
51210Sstevel@tonic-gate while (mp != NULL) {
51220Sstevel@tonic-gate mp_size = mp->b_wptr - mp->b_rptr;
51230Sstevel@tonic-gate bcopy(mp->b_rptr, new_mp->b_wptr, mp_size);
51240Sstevel@tonic-gate new_mp->b_wptr += mp_size;
51250Sstevel@tonic-gate mp = mp->b_cont;
51260Sstevel@tonic-gate }
51270Sstevel@tonic-gate freemsg(mp);
51280Sstevel@tonic-gate mp = new_mp;
51290Sstevel@tonic-gate }
51300Sstevel@tonic-gate tcp_set_cksum(mp);
51310Sstevel@tonic-gate ((struct ip *)mp->b_rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
51320Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_wput_data", mp);
51330Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
51340Sstevel@tonic-gate freemsg(mp);
51350Sstevel@tonic-gate }
51360Sstevel@tonic-gate out_of_mem:;
51370Sstevel@tonic-gate /* Pretend that all we were trying to send really got sent */
51380Sstevel@tonic-gate if (tail_unsent < 0) {
51390Sstevel@tonic-gate do {
51400Sstevel@tonic-gate xmit_tail = xmit_tail->b_cont;
51410Sstevel@tonic-gate xmit_tail->b_prev = local_time;
51420Sstevel@tonic-gate assert((uintptr_t)(xmit_tail->b_wptr -
51430Sstevel@tonic-gate xmit_tail->b_rptr) <= (uintptr_t)INT_MAX);
51440Sstevel@tonic-gate tail_unsent += (int)(xmit_tail->b_wptr -
51450Sstevel@tonic-gate xmit_tail->b_rptr);
51460Sstevel@tonic-gate } while (tail_unsent < 0);
51470Sstevel@tonic-gate }
51480Sstevel@tonic-gate done:;
51490Sstevel@tonic-gate tcp->tcp_xmit_tail = xmit_tail;
51500Sstevel@tonic-gate tcp->tcp_xmit_tail_unsent = tail_unsent;
51510Sstevel@tonic-gate len = tcp->tcp_snxt - snxt;
51520Sstevel@tonic-gate if (len) {
51530Sstevel@tonic-gate /*
51540Sstevel@tonic-gate * If new data was sent, need to update the notsack
51550Sstevel@tonic-gate * list, which is, afterall, data blocks that have
51560Sstevel@tonic-gate * not been sack'ed by the receiver. New data is
51570Sstevel@tonic-gate * not sack'ed.
51580Sstevel@tonic-gate */
51590Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) {
51600Sstevel@tonic-gate /* len is a negative value. */
51610Sstevel@tonic-gate tcp->tcp_pipe -= len;
51620Sstevel@tonic-gate tcp_notsack_update(&(tcp->tcp_notsack_list),
51630Sstevel@tonic-gate tcp->tcp_snxt, snxt,
51640Sstevel@tonic-gate &(tcp->tcp_num_notsack_blk),
51650Sstevel@tonic-gate &(tcp->tcp_cnt_notsack_list));
51660Sstevel@tonic-gate }
51670Sstevel@tonic-gate tcp->tcp_snxt = snxt + tcp->tcp_fin_sent;
51680Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
51690Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
51700Sstevel@tonic-gate if ((snxt + len) == tcp->tcp_suna) {
51710Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
51720Sstevel@tonic-gate }
51730Sstevel@tonic-gate /*
51740Sstevel@tonic-gate * Note that len is the amount we just sent but with a negative
51750Sstevel@tonic-gate * sign. We update tcp_unsent here since we may come back to
51760Sstevel@tonic-gate * tcp_wput_data from tcp_state_wait.
51770Sstevel@tonic-gate */
51780Sstevel@tonic-gate len += tcp->tcp_unsent;
51790Sstevel@tonic-gate tcp->tcp_unsent = len;
51800Sstevel@tonic-gate
51810Sstevel@tonic-gate /*
51820Sstevel@tonic-gate * Let's wait till all the segments have been acked, since we
51830Sstevel@tonic-gate * don't have a timer.
51840Sstevel@tonic-gate */
51850Sstevel@tonic-gate (void) tcp_state_wait(sock_id, tcp, TCPS_ALL_ACKED);
51860Sstevel@tonic-gate return;
51870Sstevel@tonic-gate } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) {
51880Sstevel@tonic-gate /*
51890Sstevel@tonic-gate * Didn't send anything. Make sure the timer is running
51900Sstevel@tonic-gate * so that we will probe a zero window.
51910Sstevel@tonic-gate */
51920Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
51930Sstevel@tonic-gate }
51940Sstevel@tonic-gate
51950Sstevel@tonic-gate /* Note that len is the amount we just sent but with a negative sign */
51960Sstevel@tonic-gate len += tcp->tcp_unsent;
51970Sstevel@tonic-gate tcp->tcp_unsent = len;
51980Sstevel@tonic-gate
51990Sstevel@tonic-gate }
52000Sstevel@tonic-gate
52010Sstevel@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)52020Sstevel@tonic-gate tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp,
52030Sstevel@tonic-gate uint32_t seg_seq, uint32_t seg_ack, int seg_len, tcph_t *tcph,
52040Sstevel@tonic-gate int sock_id)
52050Sstevel@tonic-gate {
52060Sstevel@tonic-gate int32_t bytes_acked;
52070Sstevel@tonic-gate int32_t gap;
52080Sstevel@tonic-gate int32_t rgap;
52090Sstevel@tonic-gate tcp_opt_t tcpopt;
52100Sstevel@tonic-gate uint_t flags;
52110Sstevel@tonic-gate uint32_t new_swnd = 0;
52120Sstevel@tonic-gate
52130Sstevel@tonic-gate #ifdef DEBUG
52140Sstevel@tonic-gate printf("Time wait processing called ###############3\n");
52150Sstevel@tonic-gate #endif
52160Sstevel@tonic-gate
52170Sstevel@tonic-gate /* Just make sure we send the right sock_id to tcp_clean_death */
52180Sstevel@tonic-gate if ((sockets[sock_id].pcb == NULL) || (sockets[sock_id].pcb != tcp))
52190Sstevel@tonic-gate sock_id = -1;
52200Sstevel@tonic-gate
52210Sstevel@tonic-gate flags = (unsigned int)tcph->th_flags[0] & 0xFF;
52220Sstevel@tonic-gate new_swnd = BE16_TO_U16(tcph->th_win) <<
52230Sstevel@tonic-gate ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws);
52240Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
52250Sstevel@tonic-gate if (!tcp_paws_check(tcp, tcph, &tcpopt)) {
52260Sstevel@tonic-gate freemsg(mp);
52270Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
52280Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
52290Sstevel@tonic-gate return;
52300Sstevel@tonic-gate }
52310Sstevel@tonic-gate }
52320Sstevel@tonic-gate gap = seg_seq - tcp->tcp_rnxt;
52330Sstevel@tonic-gate rgap = tcp->tcp_rwnd - (gap + seg_len);
52340Sstevel@tonic-gate if (gap < 0) {
52350Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
52360Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes,
52370Sstevel@tonic-gate (seg_len > -gap ? -gap : seg_len));
52380Sstevel@tonic-gate seg_len += gap;
52390Sstevel@tonic-gate if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) {
52400Sstevel@tonic-gate if (flags & TH_RST) {
52410Sstevel@tonic-gate freemsg(mp);
52420Sstevel@tonic-gate return;
52430Sstevel@tonic-gate }
52440Sstevel@tonic-gate if ((flags & TH_FIN) && seg_len == -1) {
52450Sstevel@tonic-gate /*
52460Sstevel@tonic-gate * When TCP receives a duplicate FIN in
52470Sstevel@tonic-gate * TIME_WAIT state, restart the 2 MSL timer.
52480Sstevel@tonic-gate * See page 73 in RFC 793. Make sure this TCP
52490Sstevel@tonic-gate * is already on the TIME_WAIT list. If not,
52500Sstevel@tonic-gate * just restart the timer.
52510Sstevel@tonic-gate */
52520Sstevel@tonic-gate tcp_time_wait_remove(tcp);
52530Sstevel@tonic-gate tcp_time_wait_append(tcp);
52540Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp_time_wait_interval);
52550Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
52560Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
52570Sstevel@tonic-gate freemsg(mp);
52580Sstevel@tonic-gate return;
52590Sstevel@tonic-gate }
52600Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
52610Sstevel@tonic-gate seg_len = 0;
52620Sstevel@tonic-gate goto process_ack;
52630Sstevel@tonic-gate }
52640Sstevel@tonic-gate
52650Sstevel@tonic-gate /* Fix seg_seq, and chew the gap off the front. */
52660Sstevel@tonic-gate seg_seq = tcp->tcp_rnxt;
52670Sstevel@tonic-gate }
52680Sstevel@tonic-gate
52690Sstevel@tonic-gate if ((flags & TH_SYN) && gap > 0 && rgap < 0) {
52700Sstevel@tonic-gate /*
52710Sstevel@tonic-gate * Make sure that when we accept the connection, pick
52720Sstevel@tonic-gate * an ISS greater than (tcp_snxt + ISS_INCR/2) for the
52730Sstevel@tonic-gate * old connection.
52740Sstevel@tonic-gate *
52750Sstevel@tonic-gate * The next ISS generated is equal to tcp_iss_incr_extra
52760Sstevel@tonic-gate * + ISS_INCR/2 + other components depending on the
52770Sstevel@tonic-gate * value of tcp_strong_iss. We pre-calculate the new
52780Sstevel@tonic-gate * ISS here and compare with tcp_snxt to determine if
52790Sstevel@tonic-gate * we need to make adjustment to tcp_iss_incr_extra.
52800Sstevel@tonic-gate *
52810Sstevel@tonic-gate * Note that since we are now in the global queue
52820Sstevel@tonic-gate * perimeter and need to do a lateral_put() to the
52830Sstevel@tonic-gate * listener queue, there can be other connection requests/
52840Sstevel@tonic-gate * attempts while the lateral_put() is going on. That
52850Sstevel@tonic-gate * means what we calculate here may not be correct. This
52860Sstevel@tonic-gate * is extremely difficult to solve unless TCP and IP
52870Sstevel@tonic-gate * modules are merged and there is no perimeter, but just
52880Sstevel@tonic-gate * locks. The above calculation is ugly and is a
52890Sstevel@tonic-gate * waste of CPU cycles...
52900Sstevel@tonic-gate */
52910Sstevel@tonic-gate uint32_t new_iss = tcp_iss_incr_extra;
52920Sstevel@tonic-gate int32_t adj;
52930Sstevel@tonic-gate
52940Sstevel@tonic-gate /* Add time component and min random (i.e. 1). */
52950Sstevel@tonic-gate new_iss += (prom_gettime() >> ISS_NSEC_SHT) + 1;
52960Sstevel@tonic-gate if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) {
52970Sstevel@tonic-gate /*
52980Sstevel@tonic-gate * New ISS not guaranteed to be ISS_INCR/2
52990Sstevel@tonic-gate * ahead of the current tcp_snxt, so add the
53000Sstevel@tonic-gate * difference to tcp_iss_incr_extra.
53010Sstevel@tonic-gate */
53020Sstevel@tonic-gate tcp_iss_incr_extra += adj;
53030Sstevel@tonic-gate }
53040Sstevel@tonic-gate tcp_clean_death(sock_id, tcp, 0);
53050Sstevel@tonic-gate
53060Sstevel@tonic-gate /*
53070Sstevel@tonic-gate * This is a passive open. Right now we do not
53080Sstevel@tonic-gate * do anything...
53090Sstevel@tonic-gate */
53100Sstevel@tonic-gate freemsg(mp);
53110Sstevel@tonic-gate return;
53120Sstevel@tonic-gate }
53130Sstevel@tonic-gate
53140Sstevel@tonic-gate /*
53150Sstevel@tonic-gate * rgap is the amount of stuff received out of window. A negative
53160Sstevel@tonic-gate * value is the amount out of window.
53170Sstevel@tonic-gate */
53180Sstevel@tonic-gate if (rgap < 0) {
53190Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPastWinSegs);
53200Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPastWinBytes, -rgap);
53210Sstevel@tonic-gate /* Fix seg_len and make sure there is something left. */
53220Sstevel@tonic-gate seg_len += rgap;
53230Sstevel@tonic-gate if (seg_len <= 0) {
53240Sstevel@tonic-gate if (flags & TH_RST) {
53250Sstevel@tonic-gate freemsg(mp);
53260Sstevel@tonic-gate return;
53270Sstevel@tonic-gate }
53280Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53290Sstevel@tonic-gate seg_len = 0;
53300Sstevel@tonic-gate goto process_ack;
53310Sstevel@tonic-gate }
53320Sstevel@tonic-gate }
53330Sstevel@tonic-gate /*
53340Sstevel@tonic-gate * Check whether we can update tcp_ts_recent. This test is
53350Sstevel@tonic-gate * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP
53360Sstevel@tonic-gate * Extensions for High Performance: An Update", Internet Draft.
53370Sstevel@tonic-gate */
53380Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok &&
53390Sstevel@tonic-gate TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) &&
53400Sstevel@tonic-gate SEQ_LEQ(seg_seq, tcp->tcp_rack)) {
53410Sstevel@tonic-gate tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val;
53420Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = prom_gettime();
53430Sstevel@tonic-gate }
53440Sstevel@tonic-gate
53450Sstevel@tonic-gate if (seg_seq != tcp->tcp_rnxt && seg_len > 0) {
53460Sstevel@tonic-gate /* Always ack out of order packets */
53470Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53480Sstevel@tonic-gate seg_len = 0;
53490Sstevel@tonic-gate } else if (seg_len > 0) {
53500Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataInorderSegs);
53510Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataInorderBytes, seg_len);
53520Sstevel@tonic-gate }
53530Sstevel@tonic-gate if (flags & TH_RST) {
53540Sstevel@tonic-gate freemsg(mp);
53550Sstevel@tonic-gate (void) tcp_clean_death(sock_id, tcp, 0);
53560Sstevel@tonic-gate return;
53570Sstevel@tonic-gate }
53580Sstevel@tonic-gate if (flags & TH_SYN) {
53590Sstevel@tonic-gate freemsg(mp);
53600Sstevel@tonic-gate tcp_xmit_ctl("TH_SYN", tcp, NULL, seg_ack, seg_seq + 1,
53610Sstevel@tonic-gate TH_RST|TH_ACK, 0, -1);
53620Sstevel@tonic-gate /*
53630Sstevel@tonic-gate * Do not delete the TCP structure if it is in
53640Sstevel@tonic-gate * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13.
53650Sstevel@tonic-gate */
53660Sstevel@tonic-gate return;
53670Sstevel@tonic-gate }
53680Sstevel@tonic-gate process_ack:
53690Sstevel@tonic-gate if (flags & TH_ACK) {
53700Sstevel@tonic-gate bytes_acked = (int)(seg_ack - tcp->tcp_suna);
53710Sstevel@tonic-gate if (bytes_acked <= 0) {
53720Sstevel@tonic-gate if (bytes_acked == 0 && seg_len == 0 &&
53730Sstevel@tonic-gate new_swnd == tcp->tcp_swnd)
53740Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDupAck);
53750Sstevel@tonic-gate } else {
53760Sstevel@tonic-gate /* Acks something not sent */
53770Sstevel@tonic-gate flags |= TH_ACK_NEEDED;
53780Sstevel@tonic-gate }
53790Sstevel@tonic-gate }
53800Sstevel@tonic-gate freemsg(mp);
53810Sstevel@tonic-gate if (flags & TH_ACK_NEEDED) {
53820Sstevel@tonic-gate /*
53830Sstevel@tonic-gate * Time to send an ack for some reason.
53840Sstevel@tonic-gate */
53850Sstevel@tonic-gate tcp_xmit_ctl(NULL, tcp, NULL, tcp->tcp_snxt,
53860Sstevel@tonic-gate tcp->tcp_rnxt, TH_ACK, 0, -1);
53870Sstevel@tonic-gate }
53880Sstevel@tonic-gate }
53890Sstevel@tonic-gate
53900Sstevel@tonic-gate static int
tcp_init_values(tcp_t * tcp,struct inetboot_socket * isp)53910Sstevel@tonic-gate tcp_init_values(tcp_t *tcp, struct inetboot_socket *isp)
53920Sstevel@tonic-gate {
53930Sstevel@tonic-gate int err;
53940Sstevel@tonic-gate
53950Sstevel@tonic-gate tcp->tcp_family = AF_INET;
53960Sstevel@tonic-gate tcp->tcp_ipversion = IPV4_VERSION;
53970Sstevel@tonic-gate
53980Sstevel@tonic-gate /*
53990Sstevel@tonic-gate * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
54000Sstevel@tonic-gate * will be close to tcp_rexmit_interval_initial. By doing this, we
54010Sstevel@tonic-gate * allow the algorithm to adjust slowly to large fluctuations of RTT
54020Sstevel@tonic-gate * during first few transmissions of a connection as seen in slow
54030Sstevel@tonic-gate * links.
54040Sstevel@tonic-gate */
54050Sstevel@tonic-gate tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2;
54060Sstevel@tonic-gate tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1;
54070Sstevel@tonic-gate tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
54080Sstevel@tonic-gate tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) +
54090Sstevel@tonic-gate tcp_conn_grace_period;
54100Sstevel@tonic-gate if (tcp->tcp_rto < tcp_rexmit_interval_min)
54110Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_min;
54120Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
54130Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
54140Sstevel@tonic-gate tcp->tcp_last_recv_time = prom_gettime();
54150Sstevel@tonic-gate tcp->tcp_cwnd_max = tcp_cwnd_max_;
54160Sstevel@tonic-gate tcp->tcp_snd_burst = TCP_CWND_INFINITE;
54170Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
54180Sstevel@tonic-gate /* For Ethernet, the mtu returned is actually 1550... */
54190Sstevel@tonic-gate if (mac_get_type() == IFT_ETHER) {
54200Sstevel@tonic-gate tcp->tcp_if_mtu = mac_get_mtu() - 50;
54210Sstevel@tonic-gate } else {
54220Sstevel@tonic-gate tcp->tcp_if_mtu = mac_get_mtu();
54230Sstevel@tonic-gate }
54240Sstevel@tonic-gate tcp->tcp_mss = tcp->tcp_if_mtu;
54250Sstevel@tonic-gate
54260Sstevel@tonic-gate tcp->tcp_first_timer_threshold = tcp_ip_notify_interval;
54270Sstevel@tonic-gate tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval;
54280Sstevel@tonic-gate tcp->tcp_second_timer_threshold = tcp_ip_abort_interval;
54290Sstevel@tonic-gate /*
54300Sstevel@tonic-gate * Fix it to tcp_ip_abort_linterval later if it turns out to be a
54310Sstevel@tonic-gate * passive open.
54320Sstevel@tonic-gate */
54330Sstevel@tonic-gate tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval;
54340Sstevel@tonic-gate
54350Sstevel@tonic-gate tcp->tcp_naglim = tcp_naglim_def;
54360Sstevel@tonic-gate
54370Sstevel@tonic-gate /* NOTE: ISS is now set in tcp_adapt_ire(). */
54380Sstevel@tonic-gate
54390Sstevel@tonic-gate /* Initialize the header template */
54400Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION) {
54410Sstevel@tonic-gate err = tcp_header_init_ipv4(tcp);
54420Sstevel@tonic-gate }
54430Sstevel@tonic-gate if (err)
54440Sstevel@tonic-gate return (err);
54450Sstevel@tonic-gate
54460Sstevel@tonic-gate /*
54470Sstevel@tonic-gate * Init the window scale to the max so tcp_rwnd_set() won't pare
54480Sstevel@tonic-gate * down tcp_rwnd. tcp_adapt_ire() will set the right value later.
54490Sstevel@tonic-gate */
54500Sstevel@tonic-gate tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
54510Sstevel@tonic-gate tcp->tcp_xmit_lowater = tcp_xmit_lowat;
54520Sstevel@tonic-gate if (isp != NULL) {
54530Sstevel@tonic-gate tcp->tcp_xmit_hiwater = isp->so_sndbuf;
54540Sstevel@tonic-gate tcp->tcp_rwnd = isp->so_rcvbuf;
54550Sstevel@tonic-gate tcp->tcp_rwnd_max = isp->so_rcvbuf;
54560Sstevel@tonic-gate }
54570Sstevel@tonic-gate tcp->tcp_state = TCPS_IDLE;
54580Sstevel@tonic-gate return (0);
54590Sstevel@tonic-gate }
54600Sstevel@tonic-gate
54610Sstevel@tonic-gate /*
54620Sstevel@tonic-gate * Initialize the IPv4 header. Loses any record of any IP options.
54630Sstevel@tonic-gate */
54640Sstevel@tonic-gate static int
tcp_header_init_ipv4(tcp_t * tcp)54650Sstevel@tonic-gate tcp_header_init_ipv4(tcp_t *tcp)
54660Sstevel@tonic-gate {
54670Sstevel@tonic-gate tcph_t *tcph;
54680Sstevel@tonic-gate
54690Sstevel@tonic-gate /*
54700Sstevel@tonic-gate * This is a simple initialization. If there's
54710Sstevel@tonic-gate * already a template, it should never be too small,
54720Sstevel@tonic-gate * so reuse it. Otherwise, allocate space for the new one.
54730Sstevel@tonic-gate */
54740Sstevel@tonic-gate if (tcp->tcp_iphc != NULL) {
54750Sstevel@tonic-gate assert(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH);
54760Sstevel@tonic-gate bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
54770Sstevel@tonic-gate } else {
54780Sstevel@tonic-gate tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH;
54790Sstevel@tonic-gate tcp->tcp_iphc = bkmem_zalloc(tcp->tcp_iphc_len);
54800Sstevel@tonic-gate if (tcp->tcp_iphc == NULL) {
54810Sstevel@tonic-gate tcp->tcp_iphc_len = 0;
54820Sstevel@tonic-gate return (ENOMEM);
54830Sstevel@tonic-gate }
54840Sstevel@tonic-gate }
54850Sstevel@tonic-gate tcp->tcp_ipha = (struct ip *)tcp->tcp_iphc;
54860Sstevel@tonic-gate tcp->tcp_ipversion = IPV4_VERSION;
54870Sstevel@tonic-gate
54880Sstevel@tonic-gate /*
54890Sstevel@tonic-gate * Note that it does not include TCP options yet. It will
54900Sstevel@tonic-gate * after the connection is established.
54910Sstevel@tonic-gate */
54920Sstevel@tonic-gate tcp->tcp_hdr_len = sizeof (struct ip) + sizeof (tcph_t);
54930Sstevel@tonic-gate tcp->tcp_tcp_hdr_len = sizeof (tcph_t);
54940Sstevel@tonic-gate tcp->tcp_ip_hdr_len = sizeof (struct ip);
54950Sstevel@tonic-gate tcp->tcp_ipha->ip_v = IP_VERSION;
54960Sstevel@tonic-gate /* We don't support IP options... */
54970Sstevel@tonic-gate tcp->tcp_ipha->ip_hl = IP_SIMPLE_HDR_LENGTH_IN_WORDS;
54980Sstevel@tonic-gate tcp->tcp_ipha->ip_p = IPPROTO_TCP;
54990Sstevel@tonic-gate /* We are not supposed to do PMTU discovery... */
55000Sstevel@tonic-gate tcp->tcp_ipha->ip_sum = 0;
55010Sstevel@tonic-gate
55020Sstevel@tonic-gate tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (struct ip));
55030Sstevel@tonic-gate tcp->tcp_tcph = tcph;
55040Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] = (5 << 4);
55050Sstevel@tonic-gate return (0);
55060Sstevel@tonic-gate }
55070Sstevel@tonic-gate
55080Sstevel@tonic-gate /*
55090Sstevel@tonic-gate * Send out a control packet on the tcp connection specified. This routine
55100Sstevel@tonic-gate * is typically called where we need a simple ACK or RST generated.
55110Sstevel@tonic-gate *
55120Sstevel@tonic-gate * This function is called with or without a mp.
55130Sstevel@tonic-gate */
55140Sstevel@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)55150Sstevel@tonic-gate tcp_xmit_ctl(char *str, tcp_t *tcp, mblk_t *mp, uint32_t seq,
55160Sstevel@tonic-gate uint32_t ack, int ctl, uint_t ip_hdr_len, int sock_id)
55170Sstevel@tonic-gate {
55180Sstevel@tonic-gate uchar_t *rptr;
55190Sstevel@tonic-gate tcph_t *tcph;
55200Sstevel@tonic-gate struct ip *iph = NULL;
55210Sstevel@tonic-gate int tcp_hdr_len;
55220Sstevel@tonic-gate int tcp_ip_hdr_len;
55230Sstevel@tonic-gate
55240Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
55250Sstevel@tonic-gate tcp_ip_hdr_len = tcp->tcp_ip_hdr_len;
55260Sstevel@tonic-gate
55270Sstevel@tonic-gate if (mp) {
55280Sstevel@tonic-gate assert(ip_hdr_len != 0);
55290Sstevel@tonic-gate rptr = mp->b_rptr;
55300Sstevel@tonic-gate tcph = (tcph_t *)(rptr + ip_hdr_len);
55310Sstevel@tonic-gate /* Don't reply to a RST segment. */
55320Sstevel@tonic-gate if (tcph->th_flags[0] & TH_RST) {
55330Sstevel@tonic-gate freeb(mp);
55340Sstevel@tonic-gate return;
55350Sstevel@tonic-gate }
55360Sstevel@tonic-gate freemsg(mp);
55370Sstevel@tonic-gate rptr = NULL;
55380Sstevel@tonic-gate } else {
55390Sstevel@tonic-gate assert(ip_hdr_len == 0);
55400Sstevel@tonic-gate }
55410Sstevel@tonic-gate /* If a text string is passed in with the request, print it out. */
55420Sstevel@tonic-gate if (str != NULL) {
55430Sstevel@tonic-gate dprintf("tcp_xmit_ctl(%d): '%s', seq 0x%x, ack 0x%x, "
55440Sstevel@tonic-gate "ctl 0x%x\n", sock_id, str, seq, ack, ctl);
55450Sstevel@tonic-gate }
55460Sstevel@tonic-gate mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 0);
55470Sstevel@tonic-gate if (mp == NULL) {
55480Sstevel@tonic-gate dprintf("tcp_xmit_ctl(%d): Cannot allocate memory\n", sock_id);
55490Sstevel@tonic-gate return;
55500Sstevel@tonic-gate }
55510Sstevel@tonic-gate rptr = &mp->b_rptr[tcp_wroff_xtra];
55520Sstevel@tonic-gate mp->b_rptr = rptr;
55530Sstevel@tonic-gate mp->b_wptr = &rptr[tcp_hdr_len];
55540Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len);
55550Sstevel@tonic-gate
55560Sstevel@tonic-gate iph = (struct ip *)rptr;
55570Sstevel@tonic-gate iph->ip_len = htons(tcp_hdr_len);
55580Sstevel@tonic-gate
55590Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp_ip_hdr_len];
55600Sstevel@tonic-gate tcph->th_flags[0] = (uint8_t)ctl;
55610Sstevel@tonic-gate if (ctl & TH_RST) {
55620Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutRsts);
55630Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
55640Sstevel@tonic-gate /*
55650Sstevel@tonic-gate * Don't send TSopt w/ TH_RST packets per RFC 1323.
55660Sstevel@tonic-gate */
55670Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok && tcp->tcp_state > TCPS_SYN_SENT) {
55680Sstevel@tonic-gate mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN];
55690Sstevel@tonic-gate *(mp->b_wptr) = TCPOPT_EOL;
55700Sstevel@tonic-gate iph->ip_len = htons(tcp_hdr_len -
55710Sstevel@tonic-gate TCPOPT_REAL_TS_LEN);
55720Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] -= (3 << 4);
55730Sstevel@tonic-gate }
55740Sstevel@tonic-gate }
55750Sstevel@tonic-gate if (ctl & TH_ACK) {
55760Sstevel@tonic-gate uint32_t now = prom_gettime();
55770Sstevel@tonic-gate
55780Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
55790Sstevel@tonic-gate U32_TO_BE32(now,
55800Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
55810Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
55820Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
55830Sstevel@tonic-gate }
55840Sstevel@tonic-gate tcp->tcp_rack = ack;
55850Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
55860Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutAck);
55870Sstevel@tonic-gate }
55880Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutSegs);
55890Sstevel@tonic-gate U32_TO_BE32(seq, tcph->th_seq);
55900Sstevel@tonic-gate U32_TO_BE32(ack, tcph->th_ack);
55910Sstevel@tonic-gate
55920Sstevel@tonic-gate tcp_set_cksum(mp);
55930Sstevel@tonic-gate iph->ip_ttl = (uint8_t)tcp_ipv4_ttl;
55940Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_ctl", mp);
55950Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
55960Sstevel@tonic-gate freeb(mp);
55970Sstevel@tonic-gate }
55980Sstevel@tonic-gate
55990Sstevel@tonic-gate /* Generate an ACK-only (no data) segment for a TCP endpoint */
56000Sstevel@tonic-gate static mblk_t *
tcp_ack_mp(tcp_t * tcp)56010Sstevel@tonic-gate tcp_ack_mp(tcp_t *tcp)
56020Sstevel@tonic-gate {
56030Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
56040Sstevel@tonic-gate /*
56050Sstevel@tonic-gate * For the complex case where we have to send some
56060Sstevel@tonic-gate * controls (FIN or SYN), let tcp_xmit_mp do it.
56070Sstevel@tonic-gate * When sending an ACK-only segment (no data)
56080Sstevel@tonic-gate * into a zero window, always set the seq number to
56090Sstevel@tonic-gate * suna, since snxt will be extended past the window.
56100Sstevel@tonic-gate * If we used snxt, the receiver might consider the ACK
56110Sstevel@tonic-gate * unacceptable.
56120Sstevel@tonic-gate */
56130Sstevel@tonic-gate return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
56140Sstevel@tonic-gate (tcp->tcp_zero_win_probe) ?
56150Sstevel@tonic-gate tcp->tcp_suna :
56160Sstevel@tonic-gate tcp->tcp_snxt, B_FALSE, NULL, B_FALSE));
56170Sstevel@tonic-gate } else {
56180Sstevel@tonic-gate /* Generate a simple ACK */
56190Sstevel@tonic-gate uchar_t *rptr;
56200Sstevel@tonic-gate tcph_t *tcph;
56210Sstevel@tonic-gate mblk_t *mp1;
56220Sstevel@tonic-gate int32_t tcp_hdr_len;
56230Sstevel@tonic-gate int32_t num_sack_blk = 0;
56240Sstevel@tonic-gate int32_t sack_opt_len;
56250Sstevel@tonic-gate
56260Sstevel@tonic-gate /*
56270Sstevel@tonic-gate * Allocate space for TCP + IP headers
56280Sstevel@tonic-gate * and link-level header
56290Sstevel@tonic-gate */
56300Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
56310Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
56320Sstevel@tonic-gate tcp->tcp_num_sack_blk);
56330Sstevel@tonic-gate sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
56340Sstevel@tonic-gate TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
56350Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len;
56360Sstevel@tonic-gate } else {
56370Sstevel@tonic-gate tcp_hdr_len = tcp->tcp_hdr_len;
56380Sstevel@tonic-gate }
56390Sstevel@tonic-gate mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, 0);
56400Sstevel@tonic-gate if (mp1 == NULL)
56410Sstevel@tonic-gate return (NULL);
56420Sstevel@tonic-gate
56430Sstevel@tonic-gate /* copy in prototype TCP + IP header */
56440Sstevel@tonic-gate rptr = mp1->b_rptr + tcp_wroff_xtra;
56450Sstevel@tonic-gate mp1->b_rptr = rptr;
56460Sstevel@tonic-gate mp1->b_wptr = rptr + tcp_hdr_len;
56470Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len);
56480Sstevel@tonic-gate
56490Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len];
56500Sstevel@tonic-gate
56510Sstevel@tonic-gate /*
56520Sstevel@tonic-gate * Set the TCP sequence number.
56530Sstevel@tonic-gate * When sending an ACK-only segment (no data)
56540Sstevel@tonic-gate * into a zero window, always set the seq number to
56550Sstevel@tonic-gate * suna, since snxt will be extended past the window.
56560Sstevel@tonic-gate * If we used snxt, the receiver might consider the ACK
56570Sstevel@tonic-gate * unacceptable.
56580Sstevel@tonic-gate */
56590Sstevel@tonic-gate U32_TO_ABE32((tcp->tcp_zero_win_probe) ?
56600Sstevel@tonic-gate tcp->tcp_suna : tcp->tcp_snxt, tcph->th_seq);
56610Sstevel@tonic-gate
56620Sstevel@tonic-gate /* Set up the TCP flag field. */
56630Sstevel@tonic-gate tcph->th_flags[0] = (uchar_t)TH_ACK;
56640Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
56650Sstevel@tonic-gate tcph->th_flags[0] |= TH_ECE;
56660Sstevel@tonic-gate
56670Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
56680Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
56690Sstevel@tonic-gate
56700Sstevel@tonic-gate /* fill in timestamp option if in use */
56710Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
56720Sstevel@tonic-gate uint32_t llbolt = (uint32_t)prom_gettime();
56730Sstevel@tonic-gate
56740Sstevel@tonic-gate U32_TO_BE32(llbolt,
56750Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
56760Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
56770Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
56780Sstevel@tonic-gate }
56790Sstevel@tonic-gate
56800Sstevel@tonic-gate /* Fill in SACK options */
56810Sstevel@tonic-gate if (num_sack_blk > 0) {
56820Sstevel@tonic-gate uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len;
56830Sstevel@tonic-gate sack_blk_t *tmp;
56840Sstevel@tonic-gate int32_t i;
56850Sstevel@tonic-gate
56860Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
56870Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
56880Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
56890Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
56900Sstevel@tonic-gate sizeof (sack_blk_t);
56910Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
56920Sstevel@tonic-gate
56930Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
56940Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
56950Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
56960Sstevel@tonic-gate wptr += sizeof (tcp_seq);
56970Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
56980Sstevel@tonic-gate wptr += sizeof (tcp_seq);
56990Sstevel@tonic-gate }
57000Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1)
57010Sstevel@tonic-gate << 4);
57020Sstevel@tonic-gate }
57030Sstevel@tonic-gate
57040Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(tcp_hdr_len);
57050Sstevel@tonic-gate tcp_set_cksum(mp1);
57060Sstevel@tonic-gate ((struct ip *)rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
57070Sstevel@tonic-gate return (mp1);
57080Sstevel@tonic-gate }
57090Sstevel@tonic-gate }
57100Sstevel@tonic-gate
57110Sstevel@tonic-gate /*
57120Sstevel@tonic-gate * tcp_xmit_mp is called to return a pointer to an mblk chain complete with
57130Sstevel@tonic-gate * ip and tcp header ready to pass down to IP. If the mp passed in is
57140Sstevel@tonic-gate * non-NULL, then up to max_to_send bytes of data will be dup'ed off that
57150Sstevel@tonic-gate * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary
57160Sstevel@tonic-gate * otherwise it will dup partial mblks.)
57170Sstevel@tonic-gate * Otherwise, an appropriate ACK packet will be generated. This
57180Sstevel@tonic-gate * routine is not usually called to send new data for the first time. It
57190Sstevel@tonic-gate * is mostly called out of the timer for retransmits, and to generate ACKs.
57200Sstevel@tonic-gate *
57210Sstevel@tonic-gate * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will
57220Sstevel@tonic-gate * be adjusted by *offset. And after dupb(), the offset and the ending mblk
57230Sstevel@tonic-gate * of the original mblk chain will be returned in *offset and *end_mp.
57240Sstevel@tonic-gate */
57250Sstevel@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)57260Sstevel@tonic-gate tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset,
57270Sstevel@tonic-gate mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len,
57280Sstevel@tonic-gate boolean_t rexmit)
57290Sstevel@tonic-gate {
57300Sstevel@tonic-gate int data_length;
57310Sstevel@tonic-gate int32_t off = 0;
57320Sstevel@tonic-gate uint_t flags;
57330Sstevel@tonic-gate mblk_t *mp1;
57340Sstevel@tonic-gate mblk_t *mp2;
57350Sstevel@tonic-gate mblk_t *new_mp;
57360Sstevel@tonic-gate uchar_t *rptr;
57370Sstevel@tonic-gate tcph_t *tcph;
57380Sstevel@tonic-gate int32_t num_sack_blk = 0;
57390Sstevel@tonic-gate int32_t sack_opt_len = 0;
57400Sstevel@tonic-gate
57410Sstevel@tonic-gate /* Allocate for our maximum TCP header + link-level */
57420Sstevel@tonic-gate mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH +
57430Sstevel@tonic-gate tcp_wroff_xtra, 0);
57440Sstevel@tonic-gate if (mp1 == NULL)
57450Sstevel@tonic-gate return (NULL);
57460Sstevel@tonic-gate data_length = 0;
57470Sstevel@tonic-gate
57480Sstevel@tonic-gate /*
57490Sstevel@tonic-gate * Note that tcp_mss has been adjusted to take into account the
57500Sstevel@tonic-gate * timestamp option if applicable. Because SACK options do not
57510Sstevel@tonic-gate * appear in every TCP segments and they are of variable lengths,
57520Sstevel@tonic-gate * they cannot be included in tcp_mss. Thus we need to calculate
57530Sstevel@tonic-gate * the actual segment length when we need to send a segment which
57540Sstevel@tonic-gate * includes SACK options.
57550Sstevel@tonic-gate */
57560Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) {
57570Sstevel@tonic-gate num_sack_blk = MIN(tcp->tcp_max_sack_blk,
57580Sstevel@tonic-gate tcp->tcp_num_sack_blk);
57590Sstevel@tonic-gate sack_opt_len = num_sack_blk * sizeof (sack_blk_t) +
57600Sstevel@tonic-gate TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN;
57610Sstevel@tonic-gate if (max_to_send + sack_opt_len > tcp->tcp_mss)
57620Sstevel@tonic-gate max_to_send -= sack_opt_len;
57630Sstevel@tonic-gate }
57640Sstevel@tonic-gate
57650Sstevel@tonic-gate if (offset != NULL) {
57660Sstevel@tonic-gate off = *offset;
57670Sstevel@tonic-gate /* We use offset as an indicator that end_mp is not NULL. */
57680Sstevel@tonic-gate *end_mp = NULL;
57690Sstevel@tonic-gate }
57700Sstevel@tonic-gate for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) {
57710Sstevel@tonic-gate /* This could be faster with cooperation from downstream */
57720Sstevel@tonic-gate if (mp2 != mp1 && !sendall &&
57730Sstevel@tonic-gate data_length + (int)(mp->b_wptr - mp->b_rptr) >
57740Sstevel@tonic-gate max_to_send)
57750Sstevel@tonic-gate /*
57760Sstevel@tonic-gate * Don't send the next mblk since the whole mblk
57770Sstevel@tonic-gate * does not fit.
57780Sstevel@tonic-gate */
57790Sstevel@tonic-gate break;
57800Sstevel@tonic-gate mp2->b_cont = dupb(mp);
57810Sstevel@tonic-gate mp2 = mp2->b_cont;
57820Sstevel@tonic-gate if (mp2 == NULL) {
57830Sstevel@tonic-gate freemsg(mp1);
57840Sstevel@tonic-gate return (NULL);
57850Sstevel@tonic-gate }
57860Sstevel@tonic-gate mp2->b_rptr += off;
57870Sstevel@tonic-gate assert((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <=
57880Sstevel@tonic-gate (uintptr_t)INT_MAX);
57890Sstevel@tonic-gate
57900Sstevel@tonic-gate data_length += (int)(mp2->b_wptr - mp2->b_rptr);
57910Sstevel@tonic-gate if (data_length > max_to_send) {
57920Sstevel@tonic-gate mp2->b_wptr -= data_length - max_to_send;
57930Sstevel@tonic-gate data_length = max_to_send;
57940Sstevel@tonic-gate off = mp2->b_wptr - mp->b_rptr;
57950Sstevel@tonic-gate break;
57960Sstevel@tonic-gate } else {
57970Sstevel@tonic-gate off = 0;
57980Sstevel@tonic-gate }
57990Sstevel@tonic-gate }
58000Sstevel@tonic-gate if (offset != NULL) {
58010Sstevel@tonic-gate *offset = off;
58020Sstevel@tonic-gate *end_mp = mp;
58030Sstevel@tonic-gate }
58040Sstevel@tonic-gate if (seg_len != NULL) {
58050Sstevel@tonic-gate *seg_len = data_length;
58060Sstevel@tonic-gate }
58070Sstevel@tonic-gate
58080Sstevel@tonic-gate rptr = mp1->b_rptr + tcp_wroff_xtra;
58090Sstevel@tonic-gate mp1->b_rptr = rptr;
58100Sstevel@tonic-gate mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len;
58110Sstevel@tonic-gate bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len);
58120Sstevel@tonic-gate tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len];
58130Sstevel@tonic-gate U32_TO_ABE32(seq, tcph->th_seq);
58140Sstevel@tonic-gate
58150Sstevel@tonic-gate /*
58160Sstevel@tonic-gate * Use tcp_unsent to determine if the PUSH bit should be used assumes
58170Sstevel@tonic-gate * that this function was called from tcp_wput_data. Thus, when called
58180Sstevel@tonic-gate * to retransmit data the setting of the PUSH bit may appear some
58190Sstevel@tonic-gate * what random in that it might get set when it should not. This
58200Sstevel@tonic-gate * should not pose any performance issues.
58210Sstevel@tonic-gate */
58220Sstevel@tonic-gate if (data_length != 0 && (tcp->tcp_unsent == 0 ||
58230Sstevel@tonic-gate tcp->tcp_unsent == data_length)) {
58240Sstevel@tonic-gate flags = TH_ACK | TH_PUSH;
58250Sstevel@tonic-gate } else {
58260Sstevel@tonic-gate flags = TH_ACK;
58270Sstevel@tonic-gate }
58280Sstevel@tonic-gate
58290Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
58300Sstevel@tonic-gate if (tcp->tcp_ecn_echo_on)
58310Sstevel@tonic-gate flags |= TH_ECE;
58320Sstevel@tonic-gate
58330Sstevel@tonic-gate /*
58340Sstevel@tonic-gate * Only set ECT bit and ECN_CWR if a segment contains new data.
58350Sstevel@tonic-gate * There is no TCP flow control for non-data segments, and
58360Sstevel@tonic-gate * only data segment is transmitted reliably.
58370Sstevel@tonic-gate */
58380Sstevel@tonic-gate if (data_length > 0 && !rexmit) {
58390Sstevel@tonic-gate SET_ECT(tcp, rptr);
58400Sstevel@tonic-gate if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) {
58410Sstevel@tonic-gate flags |= TH_CWR;
58420Sstevel@tonic-gate tcp->tcp_ecn_cwr_sent = B_TRUE;
58430Sstevel@tonic-gate }
58440Sstevel@tonic-gate }
58450Sstevel@tonic-gate }
58460Sstevel@tonic-gate
58470Sstevel@tonic-gate if (tcp->tcp_valid_bits) {
58480Sstevel@tonic-gate uint32_t u1;
58490Sstevel@tonic-gate
58500Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_ISS_VALID) &&
58510Sstevel@tonic-gate seq == tcp->tcp_iss) {
58520Sstevel@tonic-gate uchar_t *wptr;
58530Sstevel@tonic-gate
58540Sstevel@tonic-gate /*
58550Sstevel@tonic-gate * Tack on the MSS option. It is always needed
58560Sstevel@tonic-gate * for both active and passive open.
58570Sstevel@tonic-gate */
58580Sstevel@tonic-gate wptr = mp1->b_wptr;
58590Sstevel@tonic-gate wptr[0] = TCPOPT_MAXSEG;
58600Sstevel@tonic-gate wptr[1] = TCPOPT_MAXSEG_LEN;
58610Sstevel@tonic-gate wptr += 2;
58620Sstevel@tonic-gate /*
58630Sstevel@tonic-gate * MSS option value should be interface MTU - MIN
58640Sstevel@tonic-gate * TCP/IP header.
58650Sstevel@tonic-gate */
58660Sstevel@tonic-gate u1 = tcp->tcp_if_mtu - IP_SIMPLE_HDR_LENGTH -
58670Sstevel@tonic-gate TCP_MIN_HEADER_LENGTH;
58680Sstevel@tonic-gate U16_TO_BE16(u1, wptr);
58690Sstevel@tonic-gate mp1->b_wptr = wptr + 2;
58700Sstevel@tonic-gate /* Update the offset to cover the additional word */
58710Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += (1 << 4);
58720Sstevel@tonic-gate
58730Sstevel@tonic-gate /*
58740Sstevel@tonic-gate * Note that the following way of filling in
58750Sstevel@tonic-gate * TCP options are not optimal. Some NOPs can
58760Sstevel@tonic-gate * be saved. But there is no need at this time
58770Sstevel@tonic-gate * to optimize it. When it is needed, we will
58780Sstevel@tonic-gate * do it.
58790Sstevel@tonic-gate */
58800Sstevel@tonic-gate switch (tcp->tcp_state) {
58810Sstevel@tonic-gate case TCPS_SYN_SENT:
58820Sstevel@tonic-gate flags = TH_SYN;
58830Sstevel@tonic-gate
58840Sstevel@tonic-gate if (tcp->tcp_snd_ws_ok) {
58850Sstevel@tonic-gate wptr = mp1->b_wptr;
58860Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
58870Sstevel@tonic-gate wptr[1] = TCPOPT_WSCALE;
58880Sstevel@tonic-gate wptr[2] = TCPOPT_WS_LEN;
58890Sstevel@tonic-gate wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
58900Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_WS_LEN;
58910Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
58920Sstevel@tonic-gate (1 << 4);
58930Sstevel@tonic-gate }
58940Sstevel@tonic-gate
58950Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
58960Sstevel@tonic-gate uint32_t llbolt;
58970Sstevel@tonic-gate
58980Sstevel@tonic-gate llbolt = prom_gettime();
58990Sstevel@tonic-gate wptr = mp1->b_wptr;
59000Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59010Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59020Sstevel@tonic-gate wptr[2] = TCPOPT_TSTAMP;
59030Sstevel@tonic-gate wptr[3] = TCPOPT_TSTAMP_LEN;
59040Sstevel@tonic-gate wptr += 4;
59050Sstevel@tonic-gate U32_TO_BE32(llbolt, wptr);
59060Sstevel@tonic-gate wptr += 4;
59070Sstevel@tonic-gate assert(tcp->tcp_ts_recent == 0);
59080Sstevel@tonic-gate U32_TO_BE32(0L, wptr);
59090Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_TS_LEN;
59100Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59110Sstevel@tonic-gate (3 << 4);
59120Sstevel@tonic-gate }
59130Sstevel@tonic-gate
59140Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
59150Sstevel@tonic-gate wptr = mp1->b_wptr;
59160Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59170Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59180Sstevel@tonic-gate wptr[2] = TCPOPT_SACK_PERMITTED;
59190Sstevel@tonic-gate wptr[3] = TCPOPT_SACK_OK_LEN;
59200Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN;
59210Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59220Sstevel@tonic-gate (1 << 4);
59230Sstevel@tonic-gate }
59240Sstevel@tonic-gate
59250Sstevel@tonic-gate /*
59260Sstevel@tonic-gate * Set up all the bits to tell other side
59270Sstevel@tonic-gate * we are ECN capable.
59280Sstevel@tonic-gate */
59290Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
59300Sstevel@tonic-gate flags |= (TH_ECE | TH_CWR);
59310Sstevel@tonic-gate }
59320Sstevel@tonic-gate break;
59330Sstevel@tonic-gate case TCPS_SYN_RCVD:
59340Sstevel@tonic-gate flags |= TH_SYN;
59350Sstevel@tonic-gate
59360Sstevel@tonic-gate if (tcp->tcp_snd_ws_ok) {
59370Sstevel@tonic-gate wptr = mp1->b_wptr;
59380Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59390Sstevel@tonic-gate wptr[1] = TCPOPT_WSCALE;
59400Sstevel@tonic-gate wptr[2] = TCPOPT_WS_LEN;
59410Sstevel@tonic-gate wptr[3] = (uchar_t)tcp->tcp_rcv_ws;
59420Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_WS_LEN;
59430Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += (1 << 4);
59440Sstevel@tonic-gate }
59450Sstevel@tonic-gate
59460Sstevel@tonic-gate if (tcp->tcp_snd_sack_ok) {
59470Sstevel@tonic-gate wptr = mp1->b_wptr;
59480Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
59490Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
59500Sstevel@tonic-gate wptr[2] = TCPOPT_SACK_PERMITTED;
59510Sstevel@tonic-gate wptr[3] = TCPOPT_SACK_OK_LEN;
59520Sstevel@tonic-gate mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN;
59530Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] +=
59540Sstevel@tonic-gate (1 << 4);
59550Sstevel@tonic-gate }
59560Sstevel@tonic-gate
59570Sstevel@tonic-gate /*
59580Sstevel@tonic-gate * If the other side is ECN capable, reply
59590Sstevel@tonic-gate * that we are also ECN capable.
59600Sstevel@tonic-gate */
59610Sstevel@tonic-gate if (tcp->tcp_ecn_ok) {
59620Sstevel@tonic-gate flags |= TH_ECE;
59630Sstevel@tonic-gate }
59640Sstevel@tonic-gate break;
59650Sstevel@tonic-gate default:
59660Sstevel@tonic-gate break;
59670Sstevel@tonic-gate }
59680Sstevel@tonic-gate /* allocb() of adequate mblk assures space */
59690Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr -
59700Sstevel@tonic-gate mp1->b_rptr) <= (uintptr_t)INT_MAX);
59710Sstevel@tonic-gate if (flags & TH_SYN)
59720Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
59730Sstevel@tonic-gate }
59740Sstevel@tonic-gate if ((tcp->tcp_valid_bits & TCP_FSS_VALID) &&
59750Sstevel@tonic-gate (seq + data_length) == tcp->tcp_fss) {
59760Sstevel@tonic-gate if (!tcp->tcp_fin_acked) {
59770Sstevel@tonic-gate flags |= TH_FIN;
59780Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
59790Sstevel@tonic-gate }
59800Sstevel@tonic-gate if (!tcp->tcp_fin_sent) {
59810Sstevel@tonic-gate tcp->tcp_fin_sent = B_TRUE;
59820Sstevel@tonic-gate switch (tcp->tcp_state) {
59830Sstevel@tonic-gate case TCPS_SYN_RCVD:
59840Sstevel@tonic-gate case TCPS_ESTABLISHED:
59850Sstevel@tonic-gate tcp->tcp_state = TCPS_FIN_WAIT_1;
59860Sstevel@tonic-gate break;
59870Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
59880Sstevel@tonic-gate tcp->tcp_state = TCPS_LAST_ACK;
59890Sstevel@tonic-gate break;
59900Sstevel@tonic-gate }
59910Sstevel@tonic-gate if (tcp->tcp_suna == tcp->tcp_snxt)
59920Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
59930Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_fss + 1;
59940Sstevel@tonic-gate }
59950Sstevel@tonic-gate }
59960Sstevel@tonic-gate }
59970Sstevel@tonic-gate tcph->th_flags[0] = (uchar_t)flags;
59980Sstevel@tonic-gate tcp->tcp_rack = tcp->tcp_rnxt;
59990Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
60000Sstevel@tonic-gate
60010Sstevel@tonic-gate if (tcp->tcp_snd_ts_ok) {
60020Sstevel@tonic-gate if (tcp->tcp_state != TCPS_SYN_SENT) {
60030Sstevel@tonic-gate uint32_t llbolt = prom_gettime();
60040Sstevel@tonic-gate
60050Sstevel@tonic-gate U32_TO_BE32(llbolt,
60060Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+4);
60070Sstevel@tonic-gate U32_TO_BE32(tcp->tcp_ts_recent,
60080Sstevel@tonic-gate (char *)tcph+TCP_MIN_HEADER_LENGTH+8);
60090Sstevel@tonic-gate }
60100Sstevel@tonic-gate }
60110Sstevel@tonic-gate
60120Sstevel@tonic-gate if (num_sack_blk > 0) {
60130Sstevel@tonic-gate uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len;
60140Sstevel@tonic-gate sack_blk_t *tmp;
60150Sstevel@tonic-gate int32_t i;
60160Sstevel@tonic-gate
60170Sstevel@tonic-gate wptr[0] = TCPOPT_NOP;
60180Sstevel@tonic-gate wptr[1] = TCPOPT_NOP;
60190Sstevel@tonic-gate wptr[2] = TCPOPT_SACK;
60200Sstevel@tonic-gate wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk *
60210Sstevel@tonic-gate sizeof (sack_blk_t);
60220Sstevel@tonic-gate wptr += TCPOPT_REAL_SACK_LEN;
60230Sstevel@tonic-gate
60240Sstevel@tonic-gate tmp = tcp->tcp_sack_list;
60250Sstevel@tonic-gate for (i = 0; i < num_sack_blk; i++) {
60260Sstevel@tonic-gate U32_TO_BE32(tmp[i].begin, wptr);
60270Sstevel@tonic-gate wptr += sizeof (tcp_seq);
60280Sstevel@tonic-gate U32_TO_BE32(tmp[i].end, wptr);
60290Sstevel@tonic-gate wptr += sizeof (tcp_seq);
60300Sstevel@tonic-gate }
60310Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4);
60320Sstevel@tonic-gate }
60330Sstevel@tonic-gate assert((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX);
60340Sstevel@tonic-gate data_length += (int)(mp1->b_wptr - rptr);
60350Sstevel@tonic-gate if (tcp->tcp_ipversion == IPV4_VERSION)
60360Sstevel@tonic-gate ((struct ip *)rptr)->ip_len = htons(data_length);
60370Sstevel@tonic-gate
60380Sstevel@tonic-gate /*
60390Sstevel@tonic-gate * Performance hit! We need to pullup the whole message
60400Sstevel@tonic-gate * in order to do checksum and for the MAC output routine.
60410Sstevel@tonic-gate */
60420Sstevel@tonic-gate if (mp1->b_cont != NULL) {
60430Sstevel@tonic-gate int mp_size;
60440Sstevel@tonic-gate #ifdef DEBUG
60450Sstevel@tonic-gate printf("Multiple mblk %d\n", msgdsize(mp1));
60460Sstevel@tonic-gate #endif
6047*5866Sjgj mp2 = mp1;
60480Sstevel@tonic-gate new_mp = allocb(msgdsize(mp1) + tcp_wroff_xtra, 0);
60490Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
60500Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr;
60510Sstevel@tonic-gate while (mp1 != NULL) {
60520Sstevel@tonic-gate mp_size = mp1->b_wptr - mp1->b_rptr;
60530Sstevel@tonic-gate bcopy(mp1->b_rptr, new_mp->b_wptr, mp_size);
60540Sstevel@tonic-gate new_mp->b_wptr += mp_size;
60550Sstevel@tonic-gate mp1 = mp1->b_cont;
60560Sstevel@tonic-gate }
6057*5866Sjgj freemsg(mp2);
60580Sstevel@tonic-gate mp1 = new_mp;
60590Sstevel@tonic-gate }
60600Sstevel@tonic-gate tcp_set_cksum(mp1);
60610Sstevel@tonic-gate /* Fill in the TTL field as it is 0 in the header template. */
60620Sstevel@tonic-gate ((struct ip *)mp1->b_rptr)->ip_ttl = (uint8_t)tcp_ipv4_ttl;
60630Sstevel@tonic-gate
60640Sstevel@tonic-gate return (mp1);
60650Sstevel@tonic-gate }
60660Sstevel@tonic-gate
60670Sstevel@tonic-gate /*
60680Sstevel@tonic-gate * Generate a "no listener here" reset in response to the
60690Sstevel@tonic-gate * connection request contained within 'mp'
60700Sstevel@tonic-gate */
60710Sstevel@tonic-gate static void
tcp_xmit_listeners_reset(int sock_id,mblk_t * mp,uint_t ip_hdr_len)60720Sstevel@tonic-gate tcp_xmit_listeners_reset(int sock_id, mblk_t *mp, uint_t ip_hdr_len)
60730Sstevel@tonic-gate {
60740Sstevel@tonic-gate uchar_t *rptr;
60750Sstevel@tonic-gate uint32_t seg_len;
60760Sstevel@tonic-gate tcph_t *tcph;
60770Sstevel@tonic-gate uint32_t seg_seq;
60780Sstevel@tonic-gate uint32_t seg_ack;
60790Sstevel@tonic-gate uint_t flags;
60800Sstevel@tonic-gate
60810Sstevel@tonic-gate rptr = mp->b_rptr;
60820Sstevel@tonic-gate
60830Sstevel@tonic-gate tcph = (tcph_t *)&rptr[ip_hdr_len];
60840Sstevel@tonic-gate seg_seq = BE32_TO_U32(tcph->th_seq);
60850Sstevel@tonic-gate seg_ack = BE32_TO_U32(tcph->th_ack);
60860Sstevel@tonic-gate flags = tcph->th_flags[0];
60870Sstevel@tonic-gate
60880Sstevel@tonic-gate seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len);
60890Sstevel@tonic-gate if (flags & TH_RST) {
60900Sstevel@tonic-gate freeb(mp);
60910Sstevel@tonic-gate } else if (flags & TH_ACK) {
60920Sstevel@tonic-gate tcp_xmit_early_reset("no tcp, reset",
60930Sstevel@tonic-gate sock_id, mp, seg_ack, 0, TH_RST, ip_hdr_len);
60940Sstevel@tonic-gate } else {
60950Sstevel@tonic-gate if (flags & TH_SYN)
60960Sstevel@tonic-gate seg_len++;
60970Sstevel@tonic-gate tcp_xmit_early_reset("no tcp, reset/ack", sock_id,
60980Sstevel@tonic-gate mp, 0, seg_seq + seg_len,
60990Sstevel@tonic-gate TH_RST | TH_ACK, ip_hdr_len);
61000Sstevel@tonic-gate }
61010Sstevel@tonic-gate }
61020Sstevel@tonic-gate
61030Sstevel@tonic-gate /* Non overlapping byte exchanger */
61040Sstevel@tonic-gate static void
tcp_xchg(uchar_t * a,uchar_t * b,int len)61050Sstevel@tonic-gate tcp_xchg(uchar_t *a, uchar_t *b, int len)
61060Sstevel@tonic-gate {
61070Sstevel@tonic-gate uchar_t uch;
61080Sstevel@tonic-gate
61090Sstevel@tonic-gate while (len-- > 0) {
61100Sstevel@tonic-gate uch = a[len];
61110Sstevel@tonic-gate a[len] = b[len];
61120Sstevel@tonic-gate b[len] = uch;
61130Sstevel@tonic-gate }
61140Sstevel@tonic-gate }
61150Sstevel@tonic-gate
61160Sstevel@tonic-gate /*
61170Sstevel@tonic-gate * Generate a reset based on an inbound packet for which there is no active
61180Sstevel@tonic-gate * tcp state that we can find.
61190Sstevel@tonic-gate */
61200Sstevel@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)61210Sstevel@tonic-gate tcp_xmit_early_reset(char *str, int sock_id, mblk_t *mp, uint32_t seq,
61220Sstevel@tonic-gate uint32_t ack, int ctl, uint_t ip_hdr_len)
61230Sstevel@tonic-gate {
61240Sstevel@tonic-gate struct ip *iph = NULL;
61250Sstevel@tonic-gate ushort_t len;
61260Sstevel@tonic-gate tcph_t *tcph;
61270Sstevel@tonic-gate int i;
61280Sstevel@tonic-gate ipaddr_t addr;
61290Sstevel@tonic-gate mblk_t *new_mp;
61300Sstevel@tonic-gate
61310Sstevel@tonic-gate if (str != NULL) {
61320Sstevel@tonic-gate dprintf("tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, "
61330Sstevel@tonic-gate "flags 0x%x\n", str, seq, ack, ctl);
61340Sstevel@tonic-gate }
61350Sstevel@tonic-gate
61360Sstevel@tonic-gate /*
61370Sstevel@tonic-gate * We skip reversing source route here.
61380Sstevel@tonic-gate * (for now we replace all IP options with EOL)
61390Sstevel@tonic-gate */
61400Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
61410Sstevel@tonic-gate for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++)
61420Sstevel@tonic-gate mp->b_rptr[i] = IPOPT_EOL;
61430Sstevel@tonic-gate /*
61440Sstevel@tonic-gate * Make sure that src address is not a limited broadcast
61450Sstevel@tonic-gate * address. Not all broadcast address checking for the
61460Sstevel@tonic-gate * src address is possible, since we don't know the
61470Sstevel@tonic-gate * netmask of the src addr.
61480Sstevel@tonic-gate * No check for destination address is done, since
61490Sstevel@tonic-gate * IP will not pass up a packet with a broadcast dest address
61500Sstevel@tonic-gate * to TCP.
61510Sstevel@tonic-gate */
61520Sstevel@tonic-gate if (iph->ip_src.s_addr == INADDR_ANY ||
61530Sstevel@tonic-gate iph->ip_src.s_addr == INADDR_BROADCAST) {
61540Sstevel@tonic-gate freemsg(mp);
61550Sstevel@tonic-gate return;
61560Sstevel@tonic-gate }
61570Sstevel@tonic-gate
61580Sstevel@tonic-gate tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
61590Sstevel@tonic-gate if (tcph->th_flags[0] & TH_RST) {
61600Sstevel@tonic-gate freemsg(mp);
61610Sstevel@tonic-gate return;
61620Sstevel@tonic-gate }
61630Sstevel@tonic-gate /*
61640Sstevel@tonic-gate * Now copy the original header to a new buffer. The reason
61650Sstevel@tonic-gate * for doing this is that we need to put extra room before
61660Sstevel@tonic-gate * the header for the MAC layer address. The original mblk
61670Sstevel@tonic-gate * does not have this extra head room.
61680Sstevel@tonic-gate */
61690Sstevel@tonic-gate len = ip_hdr_len + sizeof (tcph_t);
61700Sstevel@tonic-gate if ((new_mp = allocb(len + tcp_wroff_xtra, 0)) == NULL) {
61710Sstevel@tonic-gate freemsg(mp);
61720Sstevel@tonic-gate return;
61730Sstevel@tonic-gate }
61740Sstevel@tonic-gate new_mp->b_rptr += tcp_wroff_xtra;
61750Sstevel@tonic-gate bcopy(mp->b_rptr, new_mp->b_rptr, len);
61760Sstevel@tonic-gate new_mp->b_wptr = new_mp->b_rptr + len;
61770Sstevel@tonic-gate freemsg(mp);
61780Sstevel@tonic-gate mp = new_mp;
61790Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
61800Sstevel@tonic-gate tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
61810Sstevel@tonic-gate
61820Sstevel@tonic-gate tcph->th_offset_and_rsrvd[0] = (5 << 4);
61830Sstevel@tonic-gate tcp_xchg(tcph->th_fport, tcph->th_lport, 2);
61840Sstevel@tonic-gate U32_TO_BE32(ack, tcph->th_ack);
61850Sstevel@tonic-gate U32_TO_BE32(seq, tcph->th_seq);
61860Sstevel@tonic-gate U16_TO_BE16(0, tcph->th_win);
61870Sstevel@tonic-gate bzero(tcph->th_sum, sizeof (int16_t));
61880Sstevel@tonic-gate tcph->th_flags[0] = (uint8_t)ctl;
61890Sstevel@tonic-gate if (ctl & TH_RST) {
61900Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutRsts);
61910Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpOutControl);
61920Sstevel@tonic-gate }
61930Sstevel@tonic-gate
61940Sstevel@tonic-gate iph->ip_len = htons(len);
61950Sstevel@tonic-gate /* Swap addresses */
61960Sstevel@tonic-gate addr = iph->ip_src.s_addr;
61970Sstevel@tonic-gate iph->ip_src = iph->ip_dst;
61980Sstevel@tonic-gate iph->ip_dst.s_addr = addr;
61990Sstevel@tonic-gate iph->ip_id = 0;
62000Sstevel@tonic-gate iph->ip_ttl = 0;
62010Sstevel@tonic-gate tcp_set_cksum(mp);
62020Sstevel@tonic-gate iph->ip_ttl = (uint8_t)tcp_ipv4_ttl;
62030Sstevel@tonic-gate
62040Sstevel@tonic-gate /* Dump the packet when debugging. */
62050Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_early_reset", mp);
62060Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
62070Sstevel@tonic-gate freemsg(mp);
62080Sstevel@tonic-gate }
62090Sstevel@tonic-gate
62100Sstevel@tonic-gate static void
tcp_set_cksum(mblk_t * mp)62110Sstevel@tonic-gate tcp_set_cksum(mblk_t *mp)
62120Sstevel@tonic-gate {
62130Sstevel@tonic-gate struct ip *iph;
62140Sstevel@tonic-gate tcpha_t *tcph;
62150Sstevel@tonic-gate int len;
62160Sstevel@tonic-gate
62170Sstevel@tonic-gate iph = (struct ip *)mp->b_rptr;
62180Sstevel@tonic-gate tcph = (tcpha_t *)(iph + 1);
62190Sstevel@tonic-gate len = ntohs(iph->ip_len);
62200Sstevel@tonic-gate /*
62210Sstevel@tonic-gate * Calculate the TCP checksum. Need to include the psuedo header,
62220Sstevel@tonic-gate * which is similar to the real IP header starting at the TTL field.
62230Sstevel@tonic-gate */
62240Sstevel@tonic-gate iph->ip_sum = htons(len - IP_SIMPLE_HDR_LENGTH);
62250Sstevel@tonic-gate tcph->tha_sum = 0;
62260Sstevel@tonic-gate tcph->tha_sum = tcp_cksum((uint16_t *)&(iph->ip_ttl),
62270Sstevel@tonic-gate len - IP_SIMPLE_HDR_LENGTH + 12);
62280Sstevel@tonic-gate iph->ip_sum = 0;
62290Sstevel@tonic-gate }
62300Sstevel@tonic-gate
62310Sstevel@tonic-gate static uint16_t
tcp_cksum(uint16_t * buf,uint32_t len)62320Sstevel@tonic-gate tcp_cksum(uint16_t *buf, uint32_t len)
62330Sstevel@tonic-gate {
62340Sstevel@tonic-gate /*
62350Sstevel@tonic-gate * Compute Internet Checksum for "count" bytes
62360Sstevel@tonic-gate * beginning at location "addr".
62370Sstevel@tonic-gate */
62380Sstevel@tonic-gate int32_t sum = 0;
62390Sstevel@tonic-gate
62400Sstevel@tonic-gate while (len > 1) {
62410Sstevel@tonic-gate /* This is the inner loop */
62420Sstevel@tonic-gate sum += *buf++;
62430Sstevel@tonic-gate len -= 2;
62440Sstevel@tonic-gate }
62450Sstevel@tonic-gate
62460Sstevel@tonic-gate /* Add left-over byte, if any */
62470Sstevel@tonic-gate if (len > 0)
62480Sstevel@tonic-gate sum += *(unsigned char *)buf * 256;
62490Sstevel@tonic-gate
62500Sstevel@tonic-gate /* Fold 32-bit sum to 16 bits */
62510Sstevel@tonic-gate while (sum >> 16)
62520Sstevel@tonic-gate sum = (sum & 0xffff) + (sum >> 16);
62530Sstevel@tonic-gate
62540Sstevel@tonic-gate return ((uint16_t)~sum);
62550Sstevel@tonic-gate }
62560Sstevel@tonic-gate
62570Sstevel@tonic-gate /*
62580Sstevel@tonic-gate * Type three generator adapted from the random() function in 4.4 BSD:
62590Sstevel@tonic-gate */
62600Sstevel@tonic-gate
62610Sstevel@tonic-gate /*
62620Sstevel@tonic-gate * Copyright (c) 1983, 1993
62630Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
62640Sstevel@tonic-gate *
62650Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
62660Sstevel@tonic-gate * modification, are permitted provided that the following conditions
62670Sstevel@tonic-gate * are met:
62680Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
62690Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
62700Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
62710Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
62720Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
62730Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
62740Sstevel@tonic-gate * must display the following acknowledgement:
62750Sstevel@tonic-gate * This product includes software developed by the University of
62760Sstevel@tonic-gate * California, Berkeley and its contributors.
62770Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
62780Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
62790Sstevel@tonic-gate * without specific prior written permission.
62800Sstevel@tonic-gate *
62810Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62820Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62830Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62840Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62850Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62860Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62870Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
62880Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
62890Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62900Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62910Sstevel@tonic-gate * SUCH DAMAGE.
62920Sstevel@tonic-gate */
62930Sstevel@tonic-gate
62940Sstevel@tonic-gate /* Type 3 -- x**31 + x**3 + 1 */
62950Sstevel@tonic-gate #define DEG_3 31
62960Sstevel@tonic-gate #define SEP_3 3
62970Sstevel@tonic-gate
62980Sstevel@tonic-gate
62990Sstevel@tonic-gate /* Protected by tcp_random_lock */
63000Sstevel@tonic-gate static int tcp_randtbl[DEG_3 + 1];
63010Sstevel@tonic-gate
63020Sstevel@tonic-gate static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
63030Sstevel@tonic-gate static int *tcp_random_rptr = &tcp_randtbl[1];
63040Sstevel@tonic-gate
63050Sstevel@tonic-gate static int *tcp_random_state = &tcp_randtbl[1];
63060Sstevel@tonic-gate static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
63070Sstevel@tonic-gate
63080Sstevel@tonic-gate static void
tcp_random_init(void)63090Sstevel@tonic-gate tcp_random_init(void)
63100Sstevel@tonic-gate {
63110Sstevel@tonic-gate int i;
63120Sstevel@tonic-gate uint32_t hrt;
63130Sstevel@tonic-gate uint32_t wallclock;
63140Sstevel@tonic-gate uint32_t result;
63150Sstevel@tonic-gate
63160Sstevel@tonic-gate /*
63170Sstevel@tonic-gate *
63180Sstevel@tonic-gate * XXX We don't have high resolution time in standalone... The
63190Sstevel@tonic-gate * following is just some approximation on the comment below.
63200Sstevel@tonic-gate *
63210Sstevel@tonic-gate * Use high-res timer and current time for seed. Gethrtime() returns
63220Sstevel@tonic-gate * a longlong, which may contain resolution down to nanoseconds.
63230Sstevel@tonic-gate * The current time will either be a 32-bit or a 64-bit quantity.
63240Sstevel@tonic-gate * XOR the two together in a 64-bit result variable.
63250Sstevel@tonic-gate * Convert the result to a 32-bit value by multiplying the high-order
63260Sstevel@tonic-gate * 32-bits by the low-order 32-bits.
63270Sstevel@tonic-gate *
63280Sstevel@tonic-gate * XXX We don't have gethrtime() in prom and the wallclock....
63290Sstevel@tonic-gate */
63300Sstevel@tonic-gate
63310Sstevel@tonic-gate hrt = prom_gettime();
63320Sstevel@tonic-gate wallclock = (uint32_t)time(NULL);
63330Sstevel@tonic-gate result = wallclock ^ hrt;
63340Sstevel@tonic-gate tcp_random_state[0] = result;
63350Sstevel@tonic-gate
63360Sstevel@tonic-gate for (i = 1; i < DEG_3; i++)
63370Sstevel@tonic-gate tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
63380Sstevel@tonic-gate + 12345;
63390Sstevel@tonic-gate tcp_random_fptr = &tcp_random_state[SEP_3];
63400Sstevel@tonic-gate tcp_random_rptr = &tcp_random_state[0];
63410Sstevel@tonic-gate for (i = 0; i < 10 * DEG_3; i++)
63420Sstevel@tonic-gate (void) tcp_random();
63430Sstevel@tonic-gate }
63440Sstevel@tonic-gate
63450Sstevel@tonic-gate /*
63460Sstevel@tonic-gate * tcp_random: Return a random number in the range [1 - (128K + 1)].
63470Sstevel@tonic-gate * This range is selected to be approximately centered on TCP_ISS / 2,
63480Sstevel@tonic-gate * and easy to compute. We get this value by generating a 32-bit random
63490Sstevel@tonic-gate * number, selecting out the high-order 17 bits, and then adding one so
63500Sstevel@tonic-gate * that we never return zero.
63510Sstevel@tonic-gate */
63520Sstevel@tonic-gate static int
tcp_random(void)63530Sstevel@tonic-gate tcp_random(void)
63540Sstevel@tonic-gate {
63550Sstevel@tonic-gate int i;
63560Sstevel@tonic-gate
63570Sstevel@tonic-gate *tcp_random_fptr += *tcp_random_rptr;
63580Sstevel@tonic-gate
63590Sstevel@tonic-gate /*
63600Sstevel@tonic-gate * The high-order bits are more random than the low-order bits,
63610Sstevel@tonic-gate * so we select out the high-order 17 bits and add one so that
63620Sstevel@tonic-gate * we never return zero.
63630Sstevel@tonic-gate */
63640Sstevel@tonic-gate i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
63650Sstevel@tonic-gate if (++tcp_random_fptr >= tcp_random_end_ptr) {
63660Sstevel@tonic-gate tcp_random_fptr = tcp_random_state;
63670Sstevel@tonic-gate ++tcp_random_rptr;
63680Sstevel@tonic-gate } else if (++tcp_random_rptr >= tcp_random_end_ptr)
63690Sstevel@tonic-gate tcp_random_rptr = tcp_random_state;
63700Sstevel@tonic-gate
63710Sstevel@tonic-gate return (i);
63720Sstevel@tonic-gate }
63730Sstevel@tonic-gate
63740Sstevel@tonic-gate /*
63750Sstevel@tonic-gate * Generate ISS, taking into account NDD changes may happen halfway through.
63760Sstevel@tonic-gate * (If the iss is not zero, set it.)
63770Sstevel@tonic-gate */
63780Sstevel@tonic-gate static void
tcp_iss_init(tcp_t * tcp)63790Sstevel@tonic-gate tcp_iss_init(tcp_t *tcp)
63800Sstevel@tonic-gate {
63810Sstevel@tonic-gate tcp_iss_incr_extra += (ISS_INCR >> 1);
63820Sstevel@tonic-gate tcp->tcp_iss = tcp_iss_incr_extra;
63830Sstevel@tonic-gate tcp->tcp_iss += (prom_gettime() >> ISS_NSEC_SHT) + tcp_random();
63840Sstevel@tonic-gate tcp->tcp_valid_bits = TCP_ISS_VALID;
63850Sstevel@tonic-gate tcp->tcp_fss = tcp->tcp_iss - 1;
63860Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss;
63870Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_iss + 1;
63880Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
63890Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
63900Sstevel@tonic-gate }
63910Sstevel@tonic-gate
63920Sstevel@tonic-gate /*
63930Sstevel@tonic-gate * Diagnostic routine used to return a string associated with the tcp state.
63940Sstevel@tonic-gate * Note that if the caller does not supply a buffer, it will use an internal
63950Sstevel@tonic-gate * static string. This means that if multiple threads call this function at
63960Sstevel@tonic-gate * the same time, output can be corrupted... Note also that this function
63970Sstevel@tonic-gate * does not check the size of the supplied buffer. The caller has to make
63980Sstevel@tonic-gate * sure that it is big enough.
63990Sstevel@tonic-gate */
64000Sstevel@tonic-gate static char *
tcp_display(tcp_t * tcp,char * sup_buf,char format)64010Sstevel@tonic-gate tcp_display(tcp_t *tcp, char *sup_buf, char format)
64020Sstevel@tonic-gate {
64030Sstevel@tonic-gate char buf1[30];
64040Sstevel@tonic-gate static char priv_buf[INET_ADDRSTRLEN * 2 + 80];
64050Sstevel@tonic-gate char *buf;
64060Sstevel@tonic-gate char *cp;
64070Sstevel@tonic-gate char local_addrbuf[INET_ADDRSTRLEN];
64080Sstevel@tonic-gate char remote_addrbuf[INET_ADDRSTRLEN];
64090Sstevel@tonic-gate struct in_addr addr;
64100Sstevel@tonic-gate
64110Sstevel@tonic-gate if (sup_buf != NULL)
64120Sstevel@tonic-gate buf = sup_buf;
64130Sstevel@tonic-gate else
64140Sstevel@tonic-gate buf = priv_buf;
64150Sstevel@tonic-gate
64160Sstevel@tonic-gate if (tcp == NULL)
64170Sstevel@tonic-gate return ("NULL_TCP");
64180Sstevel@tonic-gate switch (tcp->tcp_state) {
64190Sstevel@tonic-gate case TCPS_CLOSED:
64200Sstevel@tonic-gate cp = "TCP_CLOSED";
64210Sstevel@tonic-gate break;
64220Sstevel@tonic-gate case TCPS_IDLE:
64230Sstevel@tonic-gate cp = "TCP_IDLE";
64240Sstevel@tonic-gate break;
64250Sstevel@tonic-gate case TCPS_BOUND:
64260Sstevel@tonic-gate cp = "TCP_BOUND";
64270Sstevel@tonic-gate break;
64280Sstevel@tonic-gate case TCPS_LISTEN:
64290Sstevel@tonic-gate cp = "TCP_LISTEN";
64300Sstevel@tonic-gate break;
64310Sstevel@tonic-gate case TCPS_SYN_SENT:
64320Sstevel@tonic-gate cp = "TCP_SYN_SENT";
64330Sstevel@tonic-gate break;
64340Sstevel@tonic-gate case TCPS_SYN_RCVD:
64350Sstevel@tonic-gate cp = "TCP_SYN_RCVD";
64360Sstevel@tonic-gate break;
64370Sstevel@tonic-gate case TCPS_ESTABLISHED:
64380Sstevel@tonic-gate cp = "TCP_ESTABLISHED";
64390Sstevel@tonic-gate break;
64400Sstevel@tonic-gate case TCPS_CLOSE_WAIT:
64410Sstevel@tonic-gate cp = "TCP_CLOSE_WAIT";
64420Sstevel@tonic-gate break;
64430Sstevel@tonic-gate case TCPS_FIN_WAIT_1:
64440Sstevel@tonic-gate cp = "TCP_FIN_WAIT_1";
64450Sstevel@tonic-gate break;
64460Sstevel@tonic-gate case TCPS_CLOSING:
64470Sstevel@tonic-gate cp = "TCP_CLOSING";
64480Sstevel@tonic-gate break;
64490Sstevel@tonic-gate case TCPS_LAST_ACK:
64500Sstevel@tonic-gate cp = "TCP_LAST_ACK";
64510Sstevel@tonic-gate break;
64520Sstevel@tonic-gate case TCPS_FIN_WAIT_2:
64530Sstevel@tonic-gate cp = "TCP_FIN_WAIT_2";
64540Sstevel@tonic-gate break;
64550Sstevel@tonic-gate case TCPS_TIME_WAIT:
64560Sstevel@tonic-gate cp = "TCP_TIME_WAIT";
64570Sstevel@tonic-gate break;
64580Sstevel@tonic-gate default:
64590Sstevel@tonic-gate (void) sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state);
64600Sstevel@tonic-gate cp = buf1;
64610Sstevel@tonic-gate break;
64620Sstevel@tonic-gate }
64630Sstevel@tonic-gate switch (format) {
64640Sstevel@tonic-gate case DISP_ADDR_AND_PORT:
64650Sstevel@tonic-gate /*
64660Sstevel@tonic-gate * Note that we use the remote address in the tcp_b
64670Sstevel@tonic-gate * structure. This means that it will print out
64680Sstevel@tonic-gate * the real destination address, not the next hop's
64690Sstevel@tonic-gate * address if source routing is used.
64700Sstevel@tonic-gate */
64710Sstevel@tonic-gate addr.s_addr = tcp->tcp_bound_source;
64720Sstevel@tonic-gate bcopy(inet_ntoa(addr), local_addrbuf, sizeof (local_addrbuf));
64730Sstevel@tonic-gate addr.s_addr = tcp->tcp_remote;
64740Sstevel@tonic-gate bcopy(inet_ntoa(addr), remote_addrbuf, sizeof (remote_addrbuf));
64750Sstevel@tonic-gate (void) snprintf(buf, sizeof (priv_buf), "[%s.%u, %s.%u] %s",
64760Sstevel@tonic-gate local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf,
64770Sstevel@tonic-gate ntohs(tcp->tcp_fport), cp);
64780Sstevel@tonic-gate break;
64790Sstevel@tonic-gate case DISP_PORT_ONLY:
64800Sstevel@tonic-gate default:
64810Sstevel@tonic-gate (void) snprintf(buf, sizeof (priv_buf), "[%u, %u] %s",
64820Sstevel@tonic-gate ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp);
64830Sstevel@tonic-gate break;
64840Sstevel@tonic-gate }
64850Sstevel@tonic-gate
64860Sstevel@tonic-gate return (buf);
64870Sstevel@tonic-gate }
64880Sstevel@tonic-gate
64890Sstevel@tonic-gate /*
64900Sstevel@tonic-gate * Add a new piece to the tcp reassembly queue. If the gap at the beginning
64910Sstevel@tonic-gate * is filled, return as much as we can. The message passed in may be
64920Sstevel@tonic-gate * multi-part, chained using b_cont. "start" is the starting sequence
64930Sstevel@tonic-gate * number for this piece.
64940Sstevel@tonic-gate */
64950Sstevel@tonic-gate static mblk_t *
tcp_reass(tcp_t * tcp,mblk_t * mp,uint32_t start)64960Sstevel@tonic-gate tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start)
64970Sstevel@tonic-gate {
64980Sstevel@tonic-gate uint32_t end;
64990Sstevel@tonic-gate mblk_t *mp1;
65000Sstevel@tonic-gate mblk_t *mp2;
65010Sstevel@tonic-gate mblk_t *next_mp;
65020Sstevel@tonic-gate uint32_t u1;
65030Sstevel@tonic-gate
65040Sstevel@tonic-gate /* Walk through all the new pieces. */
65050Sstevel@tonic-gate do {
65060Sstevel@tonic-gate assert((uintptr_t)(mp->b_wptr - mp->b_rptr) <=
65070Sstevel@tonic-gate (uintptr_t)INT_MAX);
65080Sstevel@tonic-gate end = start + (int)(mp->b_wptr - mp->b_rptr);
65090Sstevel@tonic-gate next_mp = mp->b_cont;
65100Sstevel@tonic-gate if (start == end) {
65110Sstevel@tonic-gate /* Empty. Blast it. */
65120Sstevel@tonic-gate freeb(mp);
65130Sstevel@tonic-gate continue;
65140Sstevel@tonic-gate }
65150Sstevel@tonic-gate mp->b_cont = NULL;
65160Sstevel@tonic-gate TCP_REASS_SET_SEQ(mp, start);
65170Sstevel@tonic-gate TCP_REASS_SET_END(mp, end);
65180Sstevel@tonic-gate mp1 = tcp->tcp_reass_tail;
65190Sstevel@tonic-gate if (!mp1) {
65200Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
65210Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65220Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataUnorderSegs);
65230Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataUnorderBytes, end - start);
65240Sstevel@tonic-gate continue;
65250Sstevel@tonic-gate }
65260Sstevel@tonic-gate /* New stuff completely beyond tail? */
65270Sstevel@tonic-gate if (SEQ_GEQ(start, TCP_REASS_END(mp1))) {
65280Sstevel@tonic-gate /* Link it on end. */
65290Sstevel@tonic-gate mp1->b_cont = mp;
65300Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
65310Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataUnorderSegs);
65320Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataUnorderBytes, end - start);
65330Sstevel@tonic-gate continue;
65340Sstevel@tonic-gate }
65350Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65360Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp1);
65370Sstevel@tonic-gate /* New stuff at the front? */
65380Sstevel@tonic-gate if (SEQ_LT(start, u1)) {
65390Sstevel@tonic-gate /* Yes... Check for overlap. */
65400Sstevel@tonic-gate mp->b_cont = mp1;
65410Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65420Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp);
65430Sstevel@tonic-gate continue;
65440Sstevel@tonic-gate }
65450Sstevel@tonic-gate /*
65460Sstevel@tonic-gate * The new piece fits somewhere between the head and tail.
65470Sstevel@tonic-gate * We find our slot, where mp1 precedes us and mp2 trails.
65480Sstevel@tonic-gate */
65490Sstevel@tonic-gate for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) {
65500Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp2);
65510Sstevel@tonic-gate if (SEQ_LEQ(start, u1))
65520Sstevel@tonic-gate break;
65530Sstevel@tonic-gate }
65540Sstevel@tonic-gate /* Link ourselves in */
65550Sstevel@tonic-gate mp->b_cont = mp2;
65560Sstevel@tonic-gate mp1->b_cont = mp;
65570Sstevel@tonic-gate
65580Sstevel@tonic-gate /* Trim overlap with following mblk(s) first */
65590Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp);
65600Sstevel@tonic-gate
65610Sstevel@tonic-gate /* Trim overlap with preceding mblk */
65620Sstevel@tonic-gate tcp_reass_elim_overlap(tcp, mp1);
65630Sstevel@tonic-gate
65640Sstevel@tonic-gate } while (start = end, mp = next_mp);
65650Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65660Sstevel@tonic-gate /* Anything ready to go? */
65670Sstevel@tonic-gate if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt)
65680Sstevel@tonic-gate return (NULL);
65690Sstevel@tonic-gate /* Eat what we can off the queue */
65700Sstevel@tonic-gate for (;;) {
65710Sstevel@tonic-gate mp = mp1->b_cont;
65720Sstevel@tonic-gate end = TCP_REASS_END(mp1);
65730Sstevel@tonic-gate TCP_REASS_SET_SEQ(mp1, 0);
65740Sstevel@tonic-gate TCP_REASS_SET_END(mp1, 0);
65750Sstevel@tonic-gate if (!mp) {
65760Sstevel@tonic-gate tcp->tcp_reass_tail = NULL;
65770Sstevel@tonic-gate break;
65780Sstevel@tonic-gate }
65790Sstevel@tonic-gate if (end != TCP_REASS_SEQ(mp)) {
65800Sstevel@tonic-gate mp1->b_cont = NULL;
65810Sstevel@tonic-gate break;
65820Sstevel@tonic-gate }
65830Sstevel@tonic-gate mp1 = mp;
65840Sstevel@tonic-gate }
65850Sstevel@tonic-gate mp1 = tcp->tcp_reass_head;
65860Sstevel@tonic-gate tcp->tcp_reass_head = mp;
65870Sstevel@tonic-gate return (mp1);
65880Sstevel@tonic-gate }
65890Sstevel@tonic-gate
65900Sstevel@tonic-gate /* Eliminate any overlap that mp may have over later mblks */
65910Sstevel@tonic-gate static void
tcp_reass_elim_overlap(tcp_t * tcp,mblk_t * mp)65920Sstevel@tonic-gate tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp)
65930Sstevel@tonic-gate {
65940Sstevel@tonic-gate uint32_t end;
65950Sstevel@tonic-gate mblk_t *mp1;
65960Sstevel@tonic-gate uint32_t u1;
65970Sstevel@tonic-gate
65980Sstevel@tonic-gate end = TCP_REASS_END(mp);
65990Sstevel@tonic-gate while ((mp1 = mp->b_cont) != NULL) {
66000Sstevel@tonic-gate u1 = TCP_REASS_SEQ(mp1);
66010Sstevel@tonic-gate if (!SEQ_GT(end, u1))
66020Sstevel@tonic-gate break;
66030Sstevel@tonic-gate if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) {
66040Sstevel@tonic-gate mp->b_wptr -= end - u1;
66050Sstevel@tonic-gate TCP_REASS_SET_END(mp, u1);
66060Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataPartDupSegs);
66070Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataPartDupBytes, end - u1);
66080Sstevel@tonic-gate break;
66090Sstevel@tonic-gate }
66100Sstevel@tonic-gate mp->b_cont = mp1->b_cont;
66110Sstevel@tonic-gate freeb(mp1);
66120Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpInDataDupSegs);
66130Sstevel@tonic-gate UPDATE_MIB(tcp_mib.tcpInDataDupBytes, end - u1);
66140Sstevel@tonic-gate }
66150Sstevel@tonic-gate if (!mp1)
66160Sstevel@tonic-gate tcp->tcp_reass_tail = mp;
66170Sstevel@tonic-gate }
66180Sstevel@tonic-gate
66190Sstevel@tonic-gate /*
66200Sstevel@tonic-gate * Remove a connection from the list of detached TIME_WAIT connections.
66210Sstevel@tonic-gate */
66220Sstevel@tonic-gate static void
tcp_time_wait_remove(tcp_t * tcp)66230Sstevel@tonic-gate tcp_time_wait_remove(tcp_t *tcp)
66240Sstevel@tonic-gate {
66250Sstevel@tonic-gate if (tcp->tcp_time_wait_expire == 0) {
66260Sstevel@tonic-gate assert(tcp->tcp_time_wait_next == NULL);
66270Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev == NULL);
66280Sstevel@tonic-gate return;
66290Sstevel@tonic-gate }
66300Sstevel@tonic-gate assert(tcp->tcp_state == TCPS_TIME_WAIT);
66310Sstevel@tonic-gate if (tcp == tcp_time_wait_head) {
66320Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev == NULL);
66330Sstevel@tonic-gate tcp_time_wait_head = tcp->tcp_time_wait_next;
66340Sstevel@tonic-gate if (tcp_time_wait_head != NULL) {
66350Sstevel@tonic-gate tcp_time_wait_head->tcp_time_wait_prev = NULL;
66360Sstevel@tonic-gate } else {
66370Sstevel@tonic-gate tcp_time_wait_tail = NULL;
66380Sstevel@tonic-gate }
66390Sstevel@tonic-gate } else if (tcp == tcp_time_wait_tail) {
66400Sstevel@tonic-gate assert(tcp != tcp_time_wait_head);
66410Sstevel@tonic-gate assert(tcp->tcp_time_wait_next == NULL);
66420Sstevel@tonic-gate tcp_time_wait_tail = tcp->tcp_time_wait_prev;
66430Sstevel@tonic-gate assert(tcp_time_wait_tail != NULL);
66440Sstevel@tonic-gate tcp_time_wait_tail->tcp_time_wait_next = NULL;
66450Sstevel@tonic-gate } else {
66460Sstevel@tonic-gate assert(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp);
66470Sstevel@tonic-gate assert(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp);
66480Sstevel@tonic-gate tcp->tcp_time_wait_prev->tcp_time_wait_next =
66490Sstevel@tonic-gate tcp->tcp_time_wait_next;
66500Sstevel@tonic-gate tcp->tcp_time_wait_next->tcp_time_wait_prev =
66510Sstevel@tonic-gate tcp->tcp_time_wait_prev;
66520Sstevel@tonic-gate }
66530Sstevel@tonic-gate tcp->tcp_time_wait_next = NULL;
66540Sstevel@tonic-gate tcp->tcp_time_wait_prev = NULL;
66550Sstevel@tonic-gate tcp->tcp_time_wait_expire = 0;
66560Sstevel@tonic-gate }
66570Sstevel@tonic-gate
66580Sstevel@tonic-gate /*
66590Sstevel@tonic-gate * Add a connection to the list of detached TIME_WAIT connections
66600Sstevel@tonic-gate * and set its time to expire ...
66610Sstevel@tonic-gate */
66620Sstevel@tonic-gate static void
tcp_time_wait_append(tcp_t * tcp)66630Sstevel@tonic-gate tcp_time_wait_append(tcp_t *tcp)
66640Sstevel@tonic-gate {
66650Sstevel@tonic-gate tcp->tcp_time_wait_expire = prom_gettime() + tcp_time_wait_interval;
66660Sstevel@tonic-gate if (tcp->tcp_time_wait_expire == 0)
66670Sstevel@tonic-gate tcp->tcp_time_wait_expire = 1;
66680Sstevel@tonic-gate
66690Sstevel@tonic-gate if (tcp_time_wait_head == NULL) {
66700Sstevel@tonic-gate assert(tcp_time_wait_tail == NULL);
66710Sstevel@tonic-gate tcp_time_wait_head = tcp;
66720Sstevel@tonic-gate } else {
66730Sstevel@tonic-gate assert(tcp_time_wait_tail != NULL);
66740Sstevel@tonic-gate assert(tcp_time_wait_tail->tcp_state == TCPS_TIME_WAIT);
66750Sstevel@tonic-gate tcp_time_wait_tail->tcp_time_wait_next = tcp;
66760Sstevel@tonic-gate tcp->tcp_time_wait_prev = tcp_time_wait_tail;
66770Sstevel@tonic-gate }
66780Sstevel@tonic-gate tcp_time_wait_tail = tcp;
66790Sstevel@tonic-gate
66800Sstevel@tonic-gate /* for ndd stats about compression */
66810Sstevel@tonic-gate tcp_cum_timewait++;
66820Sstevel@tonic-gate }
66830Sstevel@tonic-gate
66840Sstevel@tonic-gate /*
66850Sstevel@tonic-gate * Periodic qtimeout routine run on the default queue.
66860Sstevel@tonic-gate * Performs 2 functions.
66870Sstevel@tonic-gate * 1. Does TIME_WAIT compression on all recently added tcps. List
66880Sstevel@tonic-gate * traversal is done backwards from the tail.
66890Sstevel@tonic-gate * 2. Blows away all tcps whose TIME_WAIT has expired. List traversal
66900Sstevel@tonic-gate * is done forwards from the head.
66910Sstevel@tonic-gate */
66920Sstevel@tonic-gate void
tcp_time_wait_collector(void)66930Sstevel@tonic-gate tcp_time_wait_collector(void)
66940Sstevel@tonic-gate {
66950Sstevel@tonic-gate tcp_t *tcp;
66960Sstevel@tonic-gate uint32_t now;
66970Sstevel@tonic-gate
66980Sstevel@tonic-gate /*
66990Sstevel@tonic-gate * In order to reap time waits reliably, we should use a
67000Sstevel@tonic-gate * source of time that is not adjustable by the user
67010Sstevel@tonic-gate */
67020Sstevel@tonic-gate now = prom_gettime();
67030Sstevel@tonic-gate while ((tcp = tcp_time_wait_head) != NULL) {
67040Sstevel@tonic-gate /*
67050Sstevel@tonic-gate * Compare times using modular arithmetic, since
67060Sstevel@tonic-gate * lbolt can wrapover.
67070Sstevel@tonic-gate */
67080Sstevel@tonic-gate if ((int32_t)(now - tcp->tcp_time_wait_expire) < 0) {
67090Sstevel@tonic-gate break;
67100Sstevel@tonic-gate }
67110Sstevel@tonic-gate /*
67120Sstevel@tonic-gate * Note that the err must be 0 as there is no socket
67130Sstevel@tonic-gate * associated with this TCP...
67140Sstevel@tonic-gate */
67150Sstevel@tonic-gate (void) tcp_clean_death(-1, tcp, 0);
67160Sstevel@tonic-gate }
67170Sstevel@tonic-gate /* Schedule next run time. */
67180Sstevel@tonic-gate tcp_time_wait_runtime = prom_gettime() + 10000;
67190Sstevel@tonic-gate }
67200Sstevel@tonic-gate
67210Sstevel@tonic-gate void
tcp_time_wait_report(void)67220Sstevel@tonic-gate tcp_time_wait_report(void)
67230Sstevel@tonic-gate {
67240Sstevel@tonic-gate tcp_t *tcp;
67250Sstevel@tonic-gate
67260Sstevel@tonic-gate printf("Current time %u\n", prom_gettime());
67270Sstevel@tonic-gate for (tcp = tcp_time_wait_head; tcp != NULL;
67280Sstevel@tonic-gate tcp = tcp->tcp_time_wait_next) {
67290Sstevel@tonic-gate printf("%s expires at %u\n", tcp_display(tcp, NULL,
67300Sstevel@tonic-gate DISP_ADDR_AND_PORT), tcp->tcp_time_wait_expire);
67310Sstevel@tonic-gate }
67320Sstevel@tonic-gate }
67330Sstevel@tonic-gate
67340Sstevel@tonic-gate /*
67350Sstevel@tonic-gate * Send up all messages queued on tcp_rcv_list.
67360Sstevel@tonic-gate * Have to set tcp_co_norm since we use putnext.
67370Sstevel@tonic-gate */
67380Sstevel@tonic-gate static void
tcp_rcv_drain(int sock_id,tcp_t * tcp)67390Sstevel@tonic-gate tcp_rcv_drain(int sock_id, tcp_t *tcp)
67400Sstevel@tonic-gate {
67410Sstevel@tonic-gate mblk_t *mp;
67420Sstevel@tonic-gate struct inetgram *in_gram;
67430Sstevel@tonic-gate mblk_t *in_mp;
67440Sstevel@tonic-gate int len;
67450Sstevel@tonic-gate
67460Sstevel@tonic-gate /* Don't drain if the app has not finished reading all the data. */
67470Sstevel@tonic-gate if (sockets[sock_id].so_rcvbuf <= 0)
67480Sstevel@tonic-gate return;
67490Sstevel@tonic-gate
67500Sstevel@tonic-gate /* We might have come here just to updated the rwnd */
67510Sstevel@tonic-gate if (tcp->tcp_rcv_list == NULL)
67520Sstevel@tonic-gate goto win_update;
67530Sstevel@tonic-gate
67540Sstevel@tonic-gate if ((in_gram = (struct inetgram *)bkmem_zalloc(
67550Sstevel@tonic-gate sizeof (struct inetgram))) == NULL) {
67560Sstevel@tonic-gate return;
67570Sstevel@tonic-gate }
67580Sstevel@tonic-gate if ((in_mp = allocb(tcp->tcp_rcv_cnt, 0)) == NULL) {
67590Sstevel@tonic-gate bkmem_free((caddr_t)in_gram, sizeof (struct inetgram));
67600Sstevel@tonic-gate return;
67610Sstevel@tonic-gate }
67620Sstevel@tonic-gate in_gram->igm_level = APP_LVL;
67630Sstevel@tonic-gate in_gram->igm_mp = in_mp;
67640Sstevel@tonic-gate in_gram->igm_id = 0;
67650Sstevel@tonic-gate
67660Sstevel@tonic-gate while ((mp = tcp->tcp_rcv_list) != NULL) {
67670Sstevel@tonic-gate tcp->tcp_rcv_list = mp->b_cont;
67680Sstevel@tonic-gate len = mp->b_wptr - mp->b_rptr;
67690Sstevel@tonic-gate bcopy(mp->b_rptr, in_mp->b_wptr, len);
67700Sstevel@tonic-gate in_mp->b_wptr += len;
67710Sstevel@tonic-gate freeb(mp);
67720Sstevel@tonic-gate }
67730Sstevel@tonic-gate
67740Sstevel@tonic-gate tcp->tcp_rcv_last_tail = NULL;
67750Sstevel@tonic-gate tcp->tcp_rcv_cnt = 0;
67760Sstevel@tonic-gate add_grams(&sockets[sock_id].inq, in_gram);
67770Sstevel@tonic-gate
67780Sstevel@tonic-gate /* This means that so_rcvbuf can be less than 0. */
67790Sstevel@tonic-gate sockets[sock_id].so_rcvbuf -= in_mp->b_wptr - in_mp->b_rptr;
67800Sstevel@tonic-gate win_update:
67810Sstevel@tonic-gate /*
67820Sstevel@tonic-gate * Increase the receive window to max. But we need to do receiver
67830Sstevel@tonic-gate * SWS avoidance. This means that we need to check the increase of
67840Sstevel@tonic-gate * of receive window is at least 1 MSS.
67850Sstevel@tonic-gate */
67860Sstevel@tonic-gate if (sockets[sock_id].so_rcvbuf > 0 &&
67870Sstevel@tonic-gate (tcp->tcp_rwnd_max - tcp->tcp_rwnd >= tcp->tcp_mss)) {
67880Sstevel@tonic-gate tcp->tcp_rwnd = tcp->tcp_rwnd_max;
67890Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws,
67900Sstevel@tonic-gate tcp->tcp_tcph->th_win);
67910Sstevel@tonic-gate }
67920Sstevel@tonic-gate }
67930Sstevel@tonic-gate
67940Sstevel@tonic-gate /*
67950Sstevel@tonic-gate * Wrapper for recvfrom to call
67960Sstevel@tonic-gate */
67970Sstevel@tonic-gate void
tcp_rcv_drain_sock(int sock_id)67980Sstevel@tonic-gate tcp_rcv_drain_sock(int sock_id)
67990Sstevel@tonic-gate {
68000Sstevel@tonic-gate tcp_t *tcp;
68010Sstevel@tonic-gate if ((tcp = sockets[sock_id].pcb) == NULL)
68020Sstevel@tonic-gate return;
68030Sstevel@tonic-gate tcp_rcv_drain(sock_id, tcp);
68040Sstevel@tonic-gate }
68050Sstevel@tonic-gate
68060Sstevel@tonic-gate /*
68070Sstevel@tonic-gate * If the inq == NULL and the tcp_rcv_list != NULL, we have data that
68080Sstevel@tonic-gate * recvfrom could read. Place a magic message in the inq to let recvfrom
68090Sstevel@tonic-gate * know that it needs to call tcp_rcv_drain_sock to pullup the data.
68100Sstevel@tonic-gate */
68110Sstevel@tonic-gate static void
tcp_drain_needed(int sock_id,tcp_t * tcp)68120Sstevel@tonic-gate tcp_drain_needed(int sock_id, tcp_t *tcp)
68130Sstevel@tonic-gate {
68140Sstevel@tonic-gate struct inetgram *in_gram;
68150Sstevel@tonic-gate #ifdef DEBUG
68160Sstevel@tonic-gate printf("tcp_drain_needed: inq %x, tcp_rcv_list %x\n",
68170Sstevel@tonic-gate sockets[sock_id].inq, tcp->tcp_rcv_list);
68180Sstevel@tonic-gate #endif
68190Sstevel@tonic-gate if ((sockets[sock_id].inq != NULL) ||
68200Sstevel@tonic-gate (tcp->tcp_rcv_list == NULL))
68210Sstevel@tonic-gate return;
68220Sstevel@tonic-gate
68230Sstevel@tonic-gate if ((in_gram = (struct inetgram *)bkmem_zalloc(
68240Sstevel@tonic-gate sizeof (struct inetgram))) == NULL)
68250Sstevel@tonic-gate return;
68260Sstevel@tonic-gate
68270Sstevel@tonic-gate in_gram->igm_level = APP_LVL;
68280Sstevel@tonic-gate in_gram->igm_mp = NULL;
68290Sstevel@tonic-gate in_gram->igm_id = TCP_CALLB_MAGIC_ID;
68300Sstevel@tonic-gate
68310Sstevel@tonic-gate add_grams(&sockets[sock_id].inq, in_gram);
68320Sstevel@tonic-gate }
68330Sstevel@tonic-gate
68340Sstevel@tonic-gate /*
68350Sstevel@tonic-gate * Queue data on tcp_rcv_list which is a b_next chain.
68360Sstevel@tonic-gate * Each element of the chain is a b_cont chain.
68370Sstevel@tonic-gate *
68380Sstevel@tonic-gate * M_DATA messages are added to the current element.
68390Sstevel@tonic-gate * Other messages are added as new (b_next) elements.
68400Sstevel@tonic-gate */
68410Sstevel@tonic-gate static void
tcp_rcv_enqueue(tcp_t * tcp,mblk_t * mp,uint_t seg_len)68420Sstevel@tonic-gate tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len)
68430Sstevel@tonic-gate {
68440Sstevel@tonic-gate assert(seg_len == msgdsize(mp));
68450Sstevel@tonic-gate if (tcp->tcp_rcv_list == NULL) {
68460Sstevel@tonic-gate tcp->tcp_rcv_list = mp;
68470Sstevel@tonic-gate } else {
68480Sstevel@tonic-gate tcp->tcp_rcv_last_tail->b_cont = mp;
68490Sstevel@tonic-gate }
68500Sstevel@tonic-gate while (mp->b_cont)
68510Sstevel@tonic-gate mp = mp->b_cont;
68520Sstevel@tonic-gate tcp->tcp_rcv_last_tail = mp;
68530Sstevel@tonic-gate tcp->tcp_rcv_cnt += seg_len;
68540Sstevel@tonic-gate tcp->tcp_rwnd -= seg_len;
68550Sstevel@tonic-gate #ifdef DEBUG
68560Sstevel@tonic-gate printf("tcp_rcv_enqueue rwnd %d\n", tcp->tcp_rwnd);
68570Sstevel@tonic-gate #endif
68580Sstevel@tonic-gate U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win);
68590Sstevel@tonic-gate }
68600Sstevel@tonic-gate
68610Sstevel@tonic-gate /* The minimum of smoothed mean deviation in RTO calculation. */
68620Sstevel@tonic-gate #define TCP_SD_MIN 400
68630Sstevel@tonic-gate
68640Sstevel@tonic-gate /*
68650Sstevel@tonic-gate * Set RTO for this connection. The formula is from Jacobson and Karels'
68660Sstevel@tonic-gate * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names
68670Sstevel@tonic-gate * are the same as those in Appendix A.2 of that paper.
68680Sstevel@tonic-gate *
68690Sstevel@tonic-gate * m = new measurement
68700Sstevel@tonic-gate * sa = smoothed RTT average (8 * average estimates).
68710Sstevel@tonic-gate * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates).
68720Sstevel@tonic-gate */
68730Sstevel@tonic-gate static void
tcp_set_rto(tcp_t * tcp,int32_t rtt)68740Sstevel@tonic-gate tcp_set_rto(tcp_t *tcp, int32_t rtt)
68750Sstevel@tonic-gate {
68760Sstevel@tonic-gate int32_t m = rtt;
68770Sstevel@tonic-gate uint32_t sa = tcp->tcp_rtt_sa;
68780Sstevel@tonic-gate uint32_t sv = tcp->tcp_rtt_sd;
68790Sstevel@tonic-gate uint32_t rto;
68800Sstevel@tonic-gate
68810Sstevel@tonic-gate BUMP_MIB(tcp_mib.tcpRttUpdate);
68820Sstevel@tonic-gate tcp->tcp_rtt_update++;
68830Sstevel@tonic-gate
68840Sstevel@tonic-gate /* tcp_rtt_sa is not 0 means this is a new sample. */
68850Sstevel@tonic-gate if (sa != 0) {
68860Sstevel@tonic-gate /*
68870Sstevel@tonic-gate * Update average estimator:
68880Sstevel@tonic-gate * new rtt = 7/8 old rtt + 1/8 Error
68890Sstevel@tonic-gate */
68900Sstevel@tonic-gate
68910Sstevel@tonic-gate /* m is now Error in estimate. */
68920Sstevel@tonic-gate m -= sa >> 3;
68930Sstevel@tonic-gate if ((int32_t)(sa += m) <= 0) {
68940Sstevel@tonic-gate /*
68950Sstevel@tonic-gate * Don't allow the smoothed average to be negative.
68960Sstevel@tonic-gate * We use 0 to denote reinitialization of the
68970Sstevel@tonic-gate * variables.
68980Sstevel@tonic-gate */
68990Sstevel@tonic-gate sa = 1;
69000Sstevel@tonic-gate }
69010Sstevel@tonic-gate
69020Sstevel@tonic-gate /*
69030Sstevel@tonic-gate * Update deviation estimator:
69040Sstevel@tonic-gate * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev)
69050Sstevel@tonic-gate */
69060Sstevel@tonic-gate if (m < 0)
69070Sstevel@tonic-gate m = -m;
69080Sstevel@tonic-gate m -= sv >> 2;
69090Sstevel@tonic-gate sv += m;
69100Sstevel@tonic-gate } else {
69110Sstevel@tonic-gate /*
69120Sstevel@tonic-gate * This follows BSD's implementation. So the reinitialized
69130Sstevel@tonic-gate * RTO is 3 * m. We cannot go less than 2 because if the
69140Sstevel@tonic-gate * link is bandwidth dominated, doubling the window size
69150Sstevel@tonic-gate * during slow start means doubling the RTT. We want to be
69160Sstevel@tonic-gate * more conservative when we reinitialize our estimates. 3
69170Sstevel@tonic-gate * is just a convenient number.
69180Sstevel@tonic-gate */
69190Sstevel@tonic-gate sa = m << 3;
69200Sstevel@tonic-gate sv = m << 1;
69210Sstevel@tonic-gate }
69220Sstevel@tonic-gate if (sv < TCP_SD_MIN) {
69230Sstevel@tonic-gate /*
69240Sstevel@tonic-gate * We do not know that if sa captures the delay ACK
69250Sstevel@tonic-gate * effect as in a long train of segments, a receiver
69260Sstevel@tonic-gate * does not delay its ACKs. So set the minimum of sv
69270Sstevel@tonic-gate * to be TCP_SD_MIN, which is default to 400 ms, twice
69280Sstevel@tonic-gate * of BSD DATO. That means the minimum of mean
69290Sstevel@tonic-gate * deviation is 100 ms.
69300Sstevel@tonic-gate *
69310Sstevel@tonic-gate */
69320Sstevel@tonic-gate sv = TCP_SD_MIN;
69330Sstevel@tonic-gate }
69340Sstevel@tonic-gate tcp->tcp_rtt_sa = sa;
69350Sstevel@tonic-gate tcp->tcp_rtt_sd = sv;
69360Sstevel@tonic-gate /*
69370Sstevel@tonic-gate * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv)
69380Sstevel@tonic-gate *
69390Sstevel@tonic-gate * Add tcp_rexmit_interval extra in case of extreme environment
69400Sstevel@tonic-gate * where the algorithm fails to work. The default value of
69410Sstevel@tonic-gate * tcp_rexmit_interval_extra should be 0.
69420Sstevel@tonic-gate *
69430Sstevel@tonic-gate * As we use a finer grained clock than BSD and update
69440Sstevel@tonic-gate * RTO for every ACKs, add in another .25 of RTT to the
69450Sstevel@tonic-gate * deviation of RTO to accomodate burstiness of 1/4 of
69460Sstevel@tonic-gate * window size.
69470Sstevel@tonic-gate */
69480Sstevel@tonic-gate rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5);
69490Sstevel@tonic-gate
69500Sstevel@tonic-gate if (rto > tcp_rexmit_interval_max) {
69510Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_max;
69520Sstevel@tonic-gate } else if (rto < tcp_rexmit_interval_min) {
69530Sstevel@tonic-gate tcp->tcp_rto = tcp_rexmit_interval_min;
69540Sstevel@tonic-gate } else {
69550Sstevel@tonic-gate tcp->tcp_rto = rto;
69560Sstevel@tonic-gate }
69570Sstevel@tonic-gate
69580Sstevel@tonic-gate /* Now, we can reset tcp_timer_backoff to use the new RTO... */
69590Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
69600Sstevel@tonic-gate }
69610Sstevel@tonic-gate
69620Sstevel@tonic-gate /*
69630Sstevel@tonic-gate * Initiate closedown sequence on an active connection.
69640Sstevel@tonic-gate * Return value zero for OK return, non-zero for error return.
69650Sstevel@tonic-gate */
69660Sstevel@tonic-gate static int
tcp_xmit_end(tcp_t * tcp,int sock_id)69670Sstevel@tonic-gate tcp_xmit_end(tcp_t *tcp, int sock_id)
69680Sstevel@tonic-gate {
69690Sstevel@tonic-gate mblk_t *mp;
69700Sstevel@tonic-gate
69710Sstevel@tonic-gate if (tcp->tcp_state < TCPS_SYN_RCVD ||
69720Sstevel@tonic-gate tcp->tcp_state > TCPS_CLOSE_WAIT) {
69730Sstevel@tonic-gate /*
69740Sstevel@tonic-gate * Invalid state, only states TCPS_SYN_RCVD,
69750Sstevel@tonic-gate * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid
69760Sstevel@tonic-gate */
69770Sstevel@tonic-gate return (-1);
69780Sstevel@tonic-gate }
69790Sstevel@tonic-gate
69800Sstevel@tonic-gate tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent;
69810Sstevel@tonic-gate tcp->tcp_valid_bits |= TCP_FSS_VALID;
69820Sstevel@tonic-gate /*
69830Sstevel@tonic-gate * If there is nothing more unsent, send the FIN now.
69840Sstevel@tonic-gate * Otherwise, it will go out with the last segment.
69850Sstevel@tonic-gate */
69860Sstevel@tonic-gate if (tcp->tcp_unsent == 0) {
69870Sstevel@tonic-gate mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
69880Sstevel@tonic-gate tcp->tcp_fss, B_FALSE, NULL, B_FALSE);
69890Sstevel@tonic-gate
69900Sstevel@tonic-gate if (mp != NULL) {
69910Sstevel@tonic-gate /* Dump the packet when debugging. */
69920Sstevel@tonic-gate TCP_DUMP_PACKET("tcp_xmit_end", mp);
69930Sstevel@tonic-gate (void) ipv4_tcp_output(sock_id, mp);
69940Sstevel@tonic-gate freeb(mp);
69950Sstevel@tonic-gate } else {
69960Sstevel@tonic-gate /*
69970Sstevel@tonic-gate * Couldn't allocate msg. Pretend we got it out.
69980Sstevel@tonic-gate * Wait for rexmit timeout.
69990Sstevel@tonic-gate */
70000Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_fss + 1;
70010Sstevel@tonic-gate TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
70020Sstevel@tonic-gate }
70030Sstevel@tonic-gate
70040Sstevel@tonic-gate /*
70050Sstevel@tonic-gate * If needed, update tcp_rexmit_snxt as tcp_snxt is
70060Sstevel@tonic-gate * changed.
70070Sstevel@tonic-gate */
70080Sstevel@tonic-gate if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) {
70090Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
70100Sstevel@tonic-gate }
70110Sstevel@tonic-gate } else {
70120Sstevel@tonic-gate tcp_wput_data(tcp, NULL, B_FALSE);
70130Sstevel@tonic-gate }
70140Sstevel@tonic-gate
70150Sstevel@tonic-gate return (0);
70160Sstevel@tonic-gate }
70170Sstevel@tonic-gate
70180Sstevel@tonic-gate int
tcp_opt_set(tcp_t * tcp,int level,int option,const void * optval,socklen_t optlen)70190Sstevel@tonic-gate tcp_opt_set(tcp_t *tcp, int level, int option, const void *optval,
70200Sstevel@tonic-gate socklen_t optlen)
70210Sstevel@tonic-gate {
70220Sstevel@tonic-gate switch (level) {
70230Sstevel@tonic-gate case SOL_SOCKET: {
70240Sstevel@tonic-gate switch (option) {
70250Sstevel@tonic-gate case SO_RCVBUF:
70260Sstevel@tonic-gate if (optlen == sizeof (int)) {
70270Sstevel@tonic-gate int val = *(int *)optval;
70280Sstevel@tonic-gate
70290Sstevel@tonic-gate if (val > tcp_max_buf) {
70300Sstevel@tonic-gate errno = ENOBUFS;
70310Sstevel@tonic-gate break;
70320Sstevel@tonic-gate }
70330Sstevel@tonic-gate /* Silently ignore zero */
70340Sstevel@tonic-gate if (val != 0) {
70350Sstevel@tonic-gate val = MSS_ROUNDUP(val, tcp->tcp_mss);
70360Sstevel@tonic-gate (void) tcp_rwnd_set(tcp, val);
70370Sstevel@tonic-gate }
70380Sstevel@tonic-gate } else {
70390Sstevel@tonic-gate errno = EINVAL;
70400Sstevel@tonic-gate }
70410Sstevel@tonic-gate break;
70420Sstevel@tonic-gate case SO_SNDBUF:
70430Sstevel@tonic-gate if (optlen == sizeof (int)) {
70440Sstevel@tonic-gate tcp->tcp_xmit_hiwater = *(int *)optval;
70450Sstevel@tonic-gate if (tcp->tcp_xmit_hiwater > tcp_max_buf)
70460Sstevel@tonic-gate tcp->tcp_xmit_hiwater = tcp_max_buf;
70470Sstevel@tonic-gate } else {
70480Sstevel@tonic-gate errno = EINVAL;
70490Sstevel@tonic-gate }
70500Sstevel@tonic-gate break;
70510Sstevel@tonic-gate case SO_LINGER:
70520Sstevel@tonic-gate if (optlen == sizeof (struct linger)) {
70530Sstevel@tonic-gate struct linger *lgr = (struct linger *)optval;
70540Sstevel@tonic-gate
70550Sstevel@tonic-gate if (lgr->l_onoff) {
70560Sstevel@tonic-gate tcp->tcp_linger = 1;
70570Sstevel@tonic-gate tcp->tcp_lingertime = lgr->l_linger;
70580Sstevel@tonic-gate } else {
70590Sstevel@tonic-gate tcp->tcp_linger = 0;
70600Sstevel@tonic-gate tcp->tcp_lingertime = 0;
70610Sstevel@tonic-gate }
70620Sstevel@tonic-gate } else {
70630Sstevel@tonic-gate errno = EINVAL;
70640Sstevel@tonic-gate }
70650Sstevel@tonic-gate break;
70660Sstevel@tonic-gate default:
70670Sstevel@tonic-gate errno = ENOPROTOOPT;
70680Sstevel@tonic-gate break;
70690Sstevel@tonic-gate }
70700Sstevel@tonic-gate break;
70710Sstevel@tonic-gate } /* case SOL_SOCKET */
70720Sstevel@tonic-gate case IPPROTO_TCP: {
70730Sstevel@tonic-gate switch (option) {
70740Sstevel@tonic-gate default:
70750Sstevel@tonic-gate errno = ENOPROTOOPT;
70760Sstevel@tonic-gate break;
70770Sstevel@tonic-gate }
70780Sstevel@tonic-gate break;
70790Sstevel@tonic-gate } /* case IPPROTO_TCP */
70800Sstevel@tonic-gate case IPPROTO_IP: {
70810Sstevel@tonic-gate switch (option) {
70820Sstevel@tonic-gate default:
70830Sstevel@tonic-gate errno = ENOPROTOOPT;
70840Sstevel@tonic-gate break;
70850Sstevel@tonic-gate }
70860Sstevel@tonic-gate break;
70870Sstevel@tonic-gate } /* case IPPROTO_IP */
70880Sstevel@tonic-gate default:
70890Sstevel@tonic-gate errno = ENOPROTOOPT;
70900Sstevel@tonic-gate break;
70910Sstevel@tonic-gate } /* switch (level) */
70920Sstevel@tonic-gate
70930Sstevel@tonic-gate if (errno != 0)
70940Sstevel@tonic-gate return (-1);
70950Sstevel@tonic-gate else
70960Sstevel@tonic-gate return (0);
70970Sstevel@tonic-gate }
7098