1 /* 2 * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 1982, 1986 Regents of the University of California. 8 * All rights reserved. The Berkeley software License Agreement 9 * specifies the terms and conditions for redistribution. 10 */ 11 12 #ifndef _TCP_INET_H 13 #define _TCP_INET_H 14 15 #pragma ident "%Z%%M% %I% %E% SMI" 16 17 #ifdef __cplusplus 18 extern "C" { 19 #endif 20 21 #include "tcp_sack.h" 22 23 /* TCP states */ 24 #define TCPS_ALL_ACKED -7 /* Internal state for retransmissions */ 25 #define TCPS_CLOSED -6 26 #define TCPS_IDLE -5 /* idle (opened, but not bound) */ 27 #define TCPS_BOUND -4 /* bound, ready to connect or accept */ 28 #define TCPS_LISTEN -3 /* listening for connection */ 29 #define TCPS_SYN_SENT -2 /* active, have sent syn */ 30 #define TCPS_SYN_RCVD -1 /* have received syn (and sent ours) */ 31 /* states < TCPS_ESTABLISHED are those where connections not established */ 32 #define TCPS_ESTABLISHED 0 /* established */ 33 #define TCPS_CLOSE_WAIT 1 /* rcvd fin, waiting for close */ 34 /* states > TCPS_CLOSE_WAIT are those where user has closed */ 35 #define TCPS_FIN_WAIT_1 2 /* have closed and sent fin */ 36 #define TCPS_CLOSING 3 /* closed, xchd FIN, await FIN ACK */ 37 #define TCPS_LAST_ACK 4 /* had fin and close; await FIN ACK */ 38 /* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */ 39 #define TCPS_FIN_WAIT_2 5 /* have closed, fin is acked */ 40 #define TCPS_TIME_WAIT 6 /* in 2*msl quiet wait after close */ 41 42 /* 43 * Internal flags used in conjunction with the packet header flags. 44 * Used in tcp_rput_data to keep track of what needs to be done. 45 */ 46 #define TH_LIMIT_XMIT 0x0400 /* Limited xmit is needed */ 47 #define TH_XMIT_NEEDED 0x0800 /* Window opened - send queued data */ 48 #define TH_REXMIT_NEEDED 0x1000 /* Time expired for unacked data */ 49 #define TH_ACK_NEEDED 0x2000 /* Send an ack now. */ 50 #define TH_NEED_SACK_REXMIT 0x4000 /* Use SACK info to retransmission */ 51 #define TH_TIMER_NEEDED 0x8000 /* Start the delayed ack/push bit timer */ 52 53 /* 54 * Special Magic number used by TCP to issue a callback to recvfrom() in the 55 * form of a dummy inetgram. 56 */ 57 #define TCP_CALLB_MAGIC_ID 0xFFFF 58 59 /* 60 * TCP sequence numbers are 32 bit integers operated 61 * on with modular arithmetic. These macros can be 62 * used to compare such integers. 63 */ 64 #define SEQ_LT(a, b) ((int32_t)((a)-(b)) < 0) 65 #define SEQ_LEQ(a, b) ((int32_t)((a)-(b)) <= 0) 66 #define SEQ_GT(a, b) ((int32_t)((a)-(b)) > 0) 67 #define SEQ_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 68 69 /* TCP Protocol header */ 70 typedef struct tcphdr_s { 71 uint8_t th_lport[2]; /* Source port */ 72 uint8_t th_fport[2]; /* Destination port */ 73 uint8_t th_seq[4]; /* Sequence number */ 74 uint8_t th_ack[4]; /* Acknowledgement number */ 75 uint8_t th_offset_and_rsrvd[1]; /* Offset to the packet data */ 76 uint8_t th_flags[1]; 77 uint8_t th_win[2]; /* Allocation number */ 78 uint8_t th_sum[2]; /* TCP checksum */ 79 uint8_t th_urp[2]; /* Urgent pointer */ 80 } tcph_t; 81 82 #define TCP_HDR_LENGTH(tcph) (((tcph)->th_offset_and_rsrvd[0] >>2) &(0xF << 2)) 83 #define TCP_MAX_COMBINED_HEADER_LENGTH (60 + 60) /* Maxed out ip + tcp */ 84 #define TCP_MAX_IP_OPTIONS_LENGTH (60 - IP_SIMPLE_HDR_LENGTH) 85 #define TCP_MAX_HDR_LENGTH 60 86 #define TCP_MAX_TCP_OPTIONS_LENGTH (60 - sizeof (tcph_t)) 87 #define TCP_MIN_HEADER_LENGTH 20 88 #define TCP_MAXWIN 65535 89 #define TCP_PORT_LEN sizeof (in_port_t) 90 #define TCP_MAX_WINSHIFT 14 91 #define TCP_MAX_LARGEWIN (TCP_MAXWIN << TCP_MAX_WINSHIFT) 92 93 #define TCPIP_HDR_LENGTH(mp, n) \ 94 (n) = IPH_HDR_LENGTH((mp)->b_rptr), \ 95 (n) += TCP_HDR_LENGTH((tcph_t *)&(mp)->b_rptr[(n)]) 96 97 /* TCP Protocol header (used if the header is known to be 32-bit aligned) */ 98 typedef struct tcphdra_s { 99 in_port_t tha_lport; /* Source port */ 100 in_port_t tha_fport; /* Destination port */ 101 uint32_t tha_seq; /* Sequence number */ 102 uint32_t tha_ack; /* Acknowledgement number */ 103 uint8_t tha_offset_and_reserved; /* Offset to the packet data */ 104 uint8_t tha_flags; 105 uint16_t tha_win; /* Allocation number */ 106 uint16_t tha_sum; /* TCP checksum */ 107 uint16_t tha_urp; /* Urgent pointer */ 108 } tcpha_t; 109 110 typedef struct tcp_s { 111 struct tcp_s *tcp_time_wait_next; /* Next TCP in TIME_WAIT list */ 112 struct tcp_s *tcp_time_wait_prev; /* Prev TCP in TIME_WAIT list */ 113 uint32_t tcp_max_swnd; /* Maximum swnd we have seen */ 114 uint32_t tcp_suna; /* Sender unacknowledged */ 115 uint32_t tcp_csuna; /* Clear (no rexmits in window) suna */ 116 uint32_t tcp_snxt; /* Senders next seq num */ 117 uint32_t tcp_swnd; /* Senders window (relative to suna) */ 118 uint32_t tcp_mss; /* Max segment size */ 119 uint32_t tcp_iss; /* Initial send seq num */ 120 uint32_t tcp_rnxt; /* Seq we expect to recv next */ 121 uint32_t tcp_rwnd; /* Current receive window */ 122 uint32_t tcp_rwnd_max; /* Max receive window */ 123 124 uint32_t tcp_irs; /* Initial recv seq num */ 125 uint32_t tcp_fss; /* Final/fin send seq num */ 126 127 uint32_t tcp_swl1; /* These help us avoid using stale */ 128 uint32_t tcp_swl2; /* packets to update state */ 129 130 uint32_t tcp_cwnd; /* Congestion window */ 131 int32_t tcp_cwnd_cnt; /* cwnd cnt in congestion avoidance */ 132 uint32_t tcp_cwnd_ssthresh; /* Congestion window */ 133 uint32_t tcp_cwnd_max; 134 135 int32_t tcp_snd_burst; /* Send burst factor */ 136 137 int32_t tcp_state; 138 int32_t tcp_rcv_ws; /* My window scale power */ 139 int32_t tcp_snd_ws; /* Sender's window scale power */ 140 uint32_t tcp_ts_recent; /* Timestamp of earliest unacked */ 141 /* data segment */ 142 143 uint32_t tcp_if_mtu; /* Outgoing interface MTU. */ 144 145 uint32_t tcp_rtt_sa; /* Round trip smoothed average */ 146 uint32_t tcp_rtt_sd; /* Round trip smoothed deviation */ 147 uint32_t tcp_rtt_update; /* Round trip update(s) */ 148 uint32_t tcp_ms_we_have_waited; /* Total retrans time */ 149 uint32_t tcp_rto; /* Round trip timeout */ 150 uint32_t tcp_rto_timeout; /* RTT timeout time */ 151 uint32_t tcp_time_wait_expire; 152 /* time in hz when t/w expires */ 153 uint32_t tcp_last_rcv_lbolt; 154 /* lbolt on last packet, used for PAWS */ 155 int tcp_lingertime; /* Close linger time (in seconds) */ 156 157 uint32_t tcp_first_timer_threshold; /* When to prod IP */ 158 uint32_t tcp_second_timer_threshold; /* When to give up completely */ 159 uint32_t tcp_first_ctimer_threshold; /* 1st threshold when connecting */ 160 uint32_t tcp_second_ctimer_threshold; /* 2nd ... while connecting */ 161 162 uint32_t tcp_rexmit_nxt; /* Next rexmit seq num */ 163 uint32_t tcp_rexmit_max; /* Max retran seq num */ 164 165 uint32_t tcp_naglim; /* Tunable nagle limit */ 166 uint32_t tcp_valid_bits; 167 #define TCP_ISS_VALID 0x1 /* Is the tcp_iss seq num active? */ 168 #define TCP_FSS_VALID 0x2 /* Is the tcp_fss seq num active? */ 169 #define TCP_URG_VALID 0x4 /* Is the tcp_urg seq num active? */ 170 #define TCP_OFO_FIN_VALID 0x8 /* Has TCP received an out of order FIN? */ 171 172 int32_t tcp_xmit_hiwater; /* Send buffer high water mark. */ 173 int32_t tcp_xmit_lowater; /* Send buffer low water mark. */ 174 175 uchar_t tcp_timer_backoff; /* Backoff shift count. */ 176 uint32_t tcp_last_recv_time; /* Last time we receive a segment. */ 177 178 /* Fields arranged in approximate access order along main paths */ 179 mblk_t *tcp_xmit_head; /* Head of rexmit list */ 180 mblk_t *tcp_xmit_last; /* last valid data seen by tcp_wput */ 181 uint32_t tcp_unsent; /* # of bytes in hand that are unsent */ 182 mblk_t *tcp_xmit_tail; /* Last rexmit data sent */ 183 uint32_t tcp_xmit_tail_unsent; /* # of unsent bytes in xmit_tail */ 184 mblk_t *tcp_rcv_list; /* Queued until push or exceed */ 185 mblk_t *tcp_rcv_last_tail; /* tcp_rcv_push_wait. */ 186 uint32_t tcp_rcv_cnt; /* tcp_rcv_list is b_next chain. */ 187 188 uint32_t tcp_rack; /* Seq # we have acked */ 189 uint32_t tcp_rack_cnt; /* # of bytes we have deferred ack */ 190 191 mblk_t *tcp_reass_head; /* Out of order reassembly list head */ 192 mblk_t *tcp_reass_tail; /* Out of order reassembly list tail */ 193 194 tcp_sack_info_t *tcp_sack_info; 195 196 #define tcp_pipe tcp_sack_info->tcp_pipe 197 #define tcp_fack tcp_sack_info->tcp_fack 198 #define tcp_sack_snxt tcp_sack_info->tcp_sack_snxt 199 #define tcp_max_sack_blk tcp_sack_info->tcp_max_sack_blk 200 #define tcp_num_sack_blk tcp_sack_info->tcp_num_sack_blk 201 #define tcp_sack_list tcp_sack_info->tcp_sack_list 202 #define tcp_num_notsack_blk tcp_sack_info->tcp_num_notsack_blk 203 #define tcp_cnt_notsack_list tcp_sack_info->tcp_cnt_notsack_list 204 #define tcp_notsack_list tcp_sack_info->tcp_notsack_list 205 206 int tcp_conn_req_cnt_q0; /* # of conn reqs in SYN_RCVD */ 207 int tcp_conn_req_cnt_q; /* # of conn reqs in ESTABLISHED */ 208 int tcp_conn_req_max; /* # of ESTABLISHED conn reqs allowed */ 209 210 int tcp_iphc_len; /* actual allocated buffer size */ 211 int32_t tcp_hdr_len; /* Byte len of combined TCP/IP hdr */ 212 struct ip *tcp_ipha; /* IPv4 header in the buffer */ 213 int tcp_ip_hdr_len; /* Byte len of our current IPvx hdr */ 214 tcph_t *tcp_tcph; /* tcp header within combined hdr */ 215 int32_t tcp_tcp_hdr_len; /* tcp header len within combined */ 216 217 uint16_t tcp_last_sent_len; /* Record length for nagle */ 218 uint16_t tcp_dupack_cnt; /* # of consequtive duplicate acks */ 219 220 /* 221 * Address family that app wishes returned addrsses to be in. 222 * Currently taken from address family used in T_BIND_REQ, but 223 * should really come from family used in original socket() call. 224 * Value can be AF_INET or AF_INET6. 225 */ 226 uint_t tcp_family; 227 228 uint32_t tcp_ofo_fin_seq; /* Recv out of order FIN seq num */ 229 uint32_t tcp_cwr_snd_max; 230 231 uint32_t 232 tcp_fin_acked : 1, /* Has our FIN been acked? */ 233 tcp_fin_rcvd : 1, /* Have we seen a FIN? */ 234 tcp_fin_sent : 1, /* Have we sent our FIN yet? */ 235 tcp_useloopback : 1, /* SO_USELOOPBACK "socket" option. */ 236 237 tcp_conn_def_q0: 1, /* move from q0 to q deferred */ 238 tcp_zero_win_probe: 1, /* Zero win probing is in progress */ 239 tcp_set_timer : 1, 240 tcp_active_open: 1, /* This is a active open */ 241 242 tcp_timer_running: 1, /* Retransmission timer running */ 243 tcp_rexmit : 1, /* TCP is retransmitting */ 244 tcp_snd_sack_ok : 1, /* Can use SACK for this connection */ 245 tcp_ecn_ok : 1, /* Can use ECN for this connection */ 246 247 tcp_ecn_echo_on : 1, /* Need to do ECN echo */ 248 tcp_ecn_cwr_sent : 1, /* ECN_CWR has been sent */ 249 tcp_cwr : 1, /* Cwnd has reduced recently */ 250 tcp_snd_ts_ok : 1, 251 252 tcp_snd_ws_ok : 1, 253 tcp_linger : 1, 254 tcp_dontdrop : 1, 255 tcp_junk_fill_thru_bit_31 : 13; 256 257 char *tcp_iphc; /* Buffer holding tcp/ip hdr template */ 258 259 in_addr_t tcp_remote; /* true remote address - needed for */ 260 /* source routing. */ 261 in_addr_t tcp_bound_source; /* IP address in bind_req */ 262 /* 263 * These fields contain the same information as tcp_tcph->th_*port. 264 * However, the lookup functions can not use the header fields 265 * since during IP option manipulation the tcp_tcph pointer 266 * changes. 267 */ 268 union { 269 struct { 270 in_port_t tcpu_fport; /* Remote port */ 271 in_port_t tcpu_lport; /* Local port */ 272 } tcpu_ports1; 273 uint32_t tcpu_ports2; /* Rem port, */ 274 /* local port */ 275 /* Used for TCP_MATCH performance */ 276 } tcp_tcpu; 277 #define tcp_fport tcp_tcpu.tcpu_ports1.tcpu_fport 278 #define tcp_lport tcp_tcpu.tcpu_ports1.tcpu_lport 279 #define tcp_ports tcp_tcpu.tcpu_ports2 280 /* 281 * IP format that packets transmitted from this struct should use. 282 * Value can be IPV4_VERSION or IPV6_VERSION. Determines whether 283 * IP+TCP header template above stores an IPv4 or IPv6 header. 284 */ 285 ushort_t tcp_ipversion; 286 int tcp_client_errno; 287 struct tcp_s *tcp_eager_next_q; /* next eager in ESTABLISHED state */ 288 struct tcp_s *tcp_eager_last_q; /* last eager in ESTABLISHED state */ 289 struct tcp_s *tcp_eager_next_q0; /* next eager in SYN_RCVD state */ 290 struct tcp_s *tcp_eager_prev_q0; /* prev eager in SYN_RCVD state */ 291 struct tcp_s *tcp_listener; /* Our listener */ 292 } tcp_t; 293 294 /* External TCP functions. */ 295 extern void tcp_socket_init(struct inetboot_socket *); 296 extern int tcp_connect(int); 297 extern int tcp_listen(int, int); 298 extern int tcp_bind(int); 299 extern int tcp_send(int, tcp_t *, const void *, int); 300 extern int tcp_opt_set(tcp_t *, int, int, const void *, socklen_t); 301 extern int tcp_accept(int, struct sockaddr *, socklen_t *); 302 extern int tcp_shutdown(int); 303 304 /* Exported for recvfrom */ 305 extern void tcp_rcv_drain_sock(int); 306 307 /* Exported for stand/lib/sock/sock_test.c */ 308 extern void tcp_time_wait_report(void); 309 310 #ifdef __cplusplus 311 } 312 #endif 313 314 #endif /* _TCP_INET_H */ 315