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