xref: /titanic_50/usr/src/uts/common/inet/tcp.h (revision 5705cefc5ef4cb218637b543df189a91959c6aa9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #ifndef	_INET_TCP_H
28 #define	_INET_TCP_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/inttypes.h>
35 #include <netinet/ip6.h>
36 #include <netinet/tcp.h>
37 #include <sys/socket.h>
38 #include <sys/socket_proto.h>
39 #include <sys/sodirect.h>
40 #include <sys/multidata.h>
41 #include <sys/md5.h>
42 #include <inet/common.h>
43 #include <inet/ip.h>
44 #include <inet/ip6.h>
45 #include <inet/mi.h>
46 #include <inet/mib2.h>
47 #include <inet/tcp_stack.h>
48 #include <inet/tcp_sack.h>
49 #include <inet/kssl/ksslapi.h>
50 
51 /*
52  * Private (and possibly temporary) ioctl used by configuration code
53  * to lock in the "default" stream for detached closes.
54  */
55 #define	TCP_IOC_DEFAULT_Q	(('T' << 8) + 51)
56 
57 /* TCP states */
58 #define	TCPS_CLOSED		-6
59 #define	TCPS_IDLE		-5	/* idle (opened, but not bound) */
60 #define	TCPS_BOUND		-4	/* bound, ready to connect or accept */
61 #define	TCPS_LISTEN		-3	/* listening for connection */
62 #define	TCPS_SYN_SENT		-2	/* active, have sent syn */
63 #define	TCPS_SYN_RCVD		-1	/* have received syn (and sent ours) */
64 /* states < TCPS_ESTABLISHED are those where connections not established */
65 #define	TCPS_ESTABLISHED	0	/* established */
66 #define	TCPS_CLOSE_WAIT		1	/* rcvd fin, waiting for close */
67 /* states > TCPS_CLOSE_WAIT are those where user has closed */
68 #define	TCPS_FIN_WAIT_1		2	/* have closed and sent fin */
69 #define	TCPS_CLOSING		3	/* closed, xchd FIN, await FIN ACK */
70 #define	TCPS_LAST_ACK		4	/* had fin and close; await FIN ACK */
71 /* states > TCPS_CLOSE_WAIT && < TCPS_FIN_WAIT_2 await ACK of FIN */
72 #define	TCPS_FIN_WAIT_2		5	/* have closed, fin is acked */
73 #define	TCPS_TIME_WAIT		6	/* in 2*msl quiet wait after close */
74 
75 /*
76  * Internal flags used in conjunction with the packet header flags.
77  * Used in tcp_rput_data to keep track of what needs to be done.
78  */
79 #define	TH_LIMIT_XMIT		0x0400	/* Limited xmit is needed */
80 #define	TH_XMIT_NEEDED		0x0800	/* Window opened - send queued data */
81 #define	TH_REXMIT_NEEDED	0x1000	/* Time expired for unacked data */
82 #define	TH_ACK_NEEDED		0x2000	/* Send an ack now. */
83 #define	TH_NEED_SACK_REXMIT	0x4000	/* Use SACK info to retransmission */
84 #define	TH_ACK_TIMER_NEEDED	0x8000	/* Start the delayed ACK timer */
85 #define	TH_ORDREL_NEEDED	0x10000	/* Generate an ordrel indication */
86 #define	TH_MARKNEXT_NEEDED	0x20000	/* Data should have MSGMARKNEXT */
87 #define	TH_SEND_URP_MARK	0x40000	/* Send up tcp_urp_mark_mp */
88 
89 /*
90  * TCP sequence numbers are 32 bit integers operated
91  * on with modular arithmetic.  These macros can be
92  * used to compare such integers.
93  */
94 #define	SEQ_LT(a, b)	((int32_t)((a)-(b)) < 0)
95 #define	SEQ_LEQ(a, b)	((int32_t)((a)-(b)) <= 0)
96 #define	SEQ_GT(a, b)	((int32_t)((a)-(b)) > 0)
97 #define	SEQ_GEQ(a, b)	((int32_t)((a)-(b)) >= 0)
98 
99 /* TCP Protocol header */
100 typedef	struct tcphdr_s {
101 	uint8_t		th_lport[2];	/* Source port */
102 	uint8_t		th_fport[2];	/* Destination port */
103 	uint8_t		th_seq[4];	/* Sequence number */
104 	uint8_t		th_ack[4];	/* Acknowledgement number */
105 	uint8_t		th_offset_and_rsrvd[1]; /* Offset to the packet data */
106 	uint8_t		th_flags[1];
107 	uint8_t		th_win[2];	/* Allocation number */
108 	uint8_t		th_sum[2];	/* TCP checksum */
109 	uint8_t		th_urp[2];	/* Urgent pointer */
110 } tcph_t;
111 
112 #define	TCP_HDR_LENGTH(tcph) (((tcph)->th_offset_and_rsrvd[0] >>2) &(0xF << 2))
113 #define	TCP_MAX_COMBINED_HEADER_LENGTH	(60 + 60) /* Maxed out ip + tcp */
114 #define	TCP_MAX_IP_OPTIONS_LENGTH	(60 - IP_SIMPLE_HDR_LENGTH)
115 #define	TCP_MAX_HDR_LENGTH		60
116 #define	TCP_MAX_TCP_OPTIONS_LENGTH	(60 - sizeof (tcph_t))
117 #define	TCP_MIN_HEADER_LENGTH		20
118 #define	TCP_MAXWIN			65535
119 #define	TCP_PORT_LEN			sizeof (in_port_t)
120 #define	TCP_MAX_WINSHIFT		14
121 #define	TCP_MAX_LARGEWIN		(TCP_MAXWIN << TCP_MAX_WINSHIFT)
122 #define	TCP_MAX_LSO_LENGTH	(IP_MAXPACKET - TCP_MAX_COMBINED_HEADER_LENGTH)
123 
124 #define	TCPIP_HDR_LENGTH(mp, n)					\
125 	(n) = IPH_HDR_LENGTH((mp)->b_rptr),			\
126 	(n) += TCP_HDR_LENGTH((tcph_t *)&(mp)->b_rptr[(n)])
127 
128 /* TCP Protocol header (used if the header is known to be 32-bit aligned) */
129 typedef	struct tcphdra_s {
130 	in_port_t	tha_lport;	/* Source port */
131 	in_port_t	tha_fport;	/* Destination port */
132 	uint32_t	tha_seq;	/* Sequence number */
133 	uint32_t	tha_ack;	/* Acknowledgement number */
134 	uint8_t tha_offset_and_reserved; /* Offset to the packet data */
135 	uint8_t		tha_flags;
136 	uint16_t	tha_win;	/* Allocation number */
137 	uint16_t	tha_sum;	/* TCP checksum */
138 	uint16_t	tha_urp;	/* Urgent pointer */
139 } tcpha_t;
140 
141 struct conn_s;
142 
143 /*
144  * Control structure for each open TCP stream,
145  * defined only within the kernel or for a kmem user.
146  * NOTE: tcp_reinit_values MUST have a line for each field in this structure!
147  */
148 #if (defined(_KERNEL) || defined(_KMEMUSER))
149 
150 typedef struct tcp_s {
151 				/* Pointer to previous bind hash next. */
152 	struct tcp_s	*tcp_time_wait_next;
153 				/* Pointer to next T/W block */
154 	struct tcp_s	*tcp_time_wait_prev;
155 				/* Pointer to previous T/W next */
156 	clock_t		tcp_time_wait_expire;
157 
158 	struct conn_s	*tcp_connp;
159 	tcp_stack_t	*tcp_tcps;	/* Shortcut via conn_netstack */
160 
161 	int32_t	tcp_state;
162 	int32_t	tcp_rcv_ws;		/* My window scale power */
163 	int32_t	tcp_snd_ws;		/* Sender's window scale power */
164 	uint32_t tcp_ts_recent;		/* Timestamp of earliest unacked */
165 					/*  data segment */
166 	clock_t	tcp_rto;		/* Round trip timeout */
167 	clock_t	tcp_last_rcv_lbolt;
168 				/* lbolt on last packet, used for PAWS */
169 
170 	uint32_t tcp_snxt;		/* Senders next seq num */
171 	uint32_t tcp_swnd;		/* Senders window (relative to suna) */
172 	uint32_t tcp_mss;		/* Max segment size */
173 	uint32_t tcp_iss;		/* Initial send seq num */
174 	uint32_t tcp_rnxt;		/* Seq we expect to recv next */
175 	uint32_t tcp_rwnd;
176 
177 	queue_t	*tcp_rq;		/* Our upstream neighbor (client) */
178 	queue_t	*tcp_wq;		/* Our downstream neighbor */
179 
180 	/* Fields arranged in approximate access order along main paths */
181 	mblk_t	*tcp_xmit_head;		/* Head of rexmit list */
182 	mblk_t	*tcp_xmit_last;		/* last valid data seen by tcp_wput */
183 	mblk_t	*tcp_xmit_tail;		/* Last rexmit data sent */
184 	uint32_t tcp_unsent;		/* # of bytes in hand that are unsent */
185 	uint32_t tcp_xmit_tail_unsent;	/* # of unsent bytes in xmit_tail */
186 
187 	uint32_t tcp_suna;		/* Sender unacknowledged */
188 	uint32_t tcp_rexmit_nxt;	/* Next rexmit seq num */
189 	uint32_t tcp_rexmit_max;	/* Max retran seq num */
190 	int32_t	tcp_snd_burst;		/* Send burst factor */
191 	uint32_t tcp_cwnd;		/* Congestion window */
192 	int32_t tcp_cwnd_cnt;		/* cwnd cnt in congestion avoidance */
193 
194 	uint32_t tcp_ibsegs;		/* Inbound segments on this stream */
195 	uint32_t tcp_obsegs;		/* Outbound segments on this stream */
196 
197 	uint32_t tcp_naglim;		/* Tunable nagle limit */
198 	uint32_t	tcp_valid_bits;
199 #define	TCP_ISS_VALID	0x1	/* Is the tcp_iss seq num active? */
200 #define	TCP_FSS_VALID	0x2	/* Is the tcp_fss seq num active? */
201 #define	TCP_URG_VALID	0x4	/* Is the tcp_urg seq num active? */
202 #define	TCP_OFO_FIN_VALID 0x8	/* Has TCP received an out of order FIN? */
203 
204 
205 
206 	timeout_id_t	tcp_timer_tid;	/* Control block for timer service */
207 	uchar_t	tcp_timer_backoff;	/* Backoff shift count. */
208 	int64_t tcp_last_recv_time;	/* Last time we receive a segment. */
209 	uint32_t tcp_init_cwnd;		/* Initial cwnd (start/restart) */
210 
211 	/*
212 	 * Following socket options are set by sockfs outside the squeue
213 	 * and we want to separate these bit fields from the other bit fields
214 	 * set by TCP to avoid grabbing locks. sockfs ensures that only one
215 	 * thread in sockfs can set a socket option at a time on a conn_t.
216 	 * However TCP may read these options concurrently. The linger option
217 	 * needs atomicity since tcp_lingertime also needs to be in sync.
218 	 * However TCP uses it only during close, and by then no socket option
219 	 * can come down. So we don't need any locks, instead just separating
220 	 * the sockfs settable bit fields from the other bit fields is
221 	 * sufficient.
222 	 */
223 	uint32_t
224 		tcp_debug : 1,		/* SO_DEBUG "socket" option. */
225 		tcp_dontroute : 1,	/* SO_DONTROUTE "socket" option. */
226 		tcp_broadcast : 1,	/* SO_BROADCAST "socket" option. */
227 		tcp_useloopback : 1,	/* SO_USELOOPBACK "socket" option. */
228 
229 		tcp_oobinline : 1,	/* SO_OOBINLINE "socket" option. */
230 		tcp_dgram_errind : 1,	/* SO_DGRAM_ERRIND option */
231 		tcp_linger : 1,		/* SO_LINGER turned on */
232 		tcp_reuseaddr	: 1,	/* SO_REUSEADDR "socket" option. */
233 
234 		tcp_junk_to_bit_31 : 24;
235 
236 	/* Following manipulated by TCP under squeue protection */
237 	uint32_t
238 		tcp_urp_last_valid : 1,	/* Is tcp_urp_last valid? */
239 		tcp_hard_binding : 1,	/* If we've started a full bind */
240 		tcp_hard_bound : 1,	/* If we've done a full bind with IP */
241 		tcp_fin_acked : 1,	/* Has our FIN been acked? */
242 
243 		tcp_fin_rcvd : 1,	/* Have we seen a FIN? */
244 		tcp_fin_sent : 1,	/* Have we sent our FIN yet? */
245 		tcp_ordrel_done : 1,	/* Have we sent the ord_rel upstream? */
246 		tcp_detached : 1,	/* If we're detached from a stream */
247 
248 		tcp_bind_pending : 1,	/* Client is waiting for bind ack */
249 		tcp_unbind_pending : 1, /* Client sent T_UNBIND_REQ */
250 		tcp_ka_enabled: 1,	/* Connection KeepAlive Timer needed */
251 		tcp_zero_win_probe: 1,	/* Zero win probing is in progress */
252 
253 		tcp_loopback: 1,	/* src and dst are the same machine */
254 		tcp_localnet: 1,	/* src and dst are on the same subnet */
255 		tcp_syn_defense: 1,	/* For defense against SYN attack */
256 #define	tcp_dontdrop	tcp_syn_defense
257 		tcp_set_timer : 1,
258 
259 		tcp_active_open: 1,	/* This is a active open */
260 		tcp_rexmit : 1,		/* TCP is retransmitting */
261 		tcp_snd_sack_ok : 1,	/* Can use SACK for this connection */
262 		tcp_empty_flag : 1,	/* Empty flag for future use */
263 
264 		tcp_recvdstaddr : 1,	/* return T_EXTCONN_IND with dst addr */
265 		tcp_hwcksum : 1,	/* The NIC is capable of hwcksum */
266 		tcp_ip_forward_progress : 1,
267 		tcp_anon_priv_bind : 1,
268 
269 		tcp_ecn_ok : 1,		/* Can use ECN for this connection */
270 		tcp_ecn_echo_on : 1,	/* Need to do ECN echo */
271 		tcp_ecn_cwr_sent : 1,	/* ECN_CWR has been sent */
272 		tcp_cwr : 1,		/* Cwnd has reduced recently */
273 
274 		tcp_pad_to_bit31 : 4;
275 	/* Following manipulated by TCP under squeue protection */
276 	uint32_t
277 		tcp_mdt : 1,		/* Lower layer is capable of MDT */
278 		tcp_snd_ts_ok  : 1,
279 		tcp_snd_ws_ok  : 1,
280 		tcp_exclbind	: 1,	/* ``exclusive'' binding */
281 
282 		tcp_hdr_grown	: 1,
283 		tcp_in_free_list : 1,
284 		tcp_snd_zcopy_on : 1,	/* xmit zero-copy enabled */
285 
286 		tcp_snd_zcopy_aware : 1, /* client is zero-copy aware */
287 		tcp_xmit_zc_clean : 1,	/* the xmit list is free of zc-mblk */
288 		tcp_wait_for_eagers : 1, /* Wait for eagers to disappear */
289 		tcp_accept_error : 1,	/* Error during TLI accept */
290 
291 		tcp_send_discon_ind : 1, /* TLI accept err, send discon ind */
292 		tcp_cork : 1,		/* tcp_cork option */
293 		tcp_tconnind_started : 1, /* conn_ind message is being sent */
294 		tcp_lso :1,		/* Lower layer is capable of LSO */
295 		tcp_refuse :1,		/* Connection needs refusing */
296 		tcp_pad_to_bit_31 : 16;
297 
298 	uint32_t	tcp_if_mtu;	/* Outgoing interface MTU. */
299 
300 	mblk_t	*tcp_reass_head;	/* Out of order reassembly list head */
301 	mblk_t	*tcp_reass_tail;	/* Out of order reassembly list tail */
302 
303 	tcp_sack_info_t	*tcp_sack_info;
304 
305 #define	tcp_pipe	tcp_sack_info->tcp_pipe
306 #define	tcp_fack	tcp_sack_info->tcp_fack
307 #define	tcp_sack_snxt	tcp_sack_info->tcp_sack_snxt
308 #define	tcp_max_sack_blk	tcp_sack_info->tcp_max_sack_blk
309 #define	tcp_num_sack_blk	tcp_sack_info->tcp_num_sack_blk
310 #define	tcp_sack_list		tcp_sack_info->tcp_sack_list
311 #define	tcp_num_notsack_blk	tcp_sack_info->tcp_num_notsack_blk
312 #define	tcp_cnt_notsack_list	tcp_sack_info->tcp_cnt_notsack_list
313 #define	tcp_notsack_list		tcp_sack_info->tcp_notsack_list
314 
315 	mblk_t	*tcp_rcv_list;		/* Queued until push, urgent data, */
316 	mblk_t	*tcp_rcv_last_head;	/* optdata, or the count exceeds */
317 	mblk_t	*tcp_rcv_last_tail;	/* tcp_rcv_push_wait. */
318 	uint32_t tcp_rcv_cnt;		/* tcp_rcv_list is b_next chain. */
319 
320 	uint32_t tcp_cwnd_ssthresh;	/* Congestion window */
321 	uint32_t tcp_cwnd_max;
322 	uint32_t tcp_csuna;		/* Clear (no rexmits in window) suna */
323 
324 	clock_t	tcp_rtt_sa;		/* Round trip smoothed average */
325 	clock_t	tcp_rtt_sd;		/* Round trip smoothed deviation */
326 	clock_t	tcp_rtt_update;		/* Round trip update(s) */
327 	clock_t tcp_ms_we_have_waited;	/* Total retrans time */
328 
329 	uint32_t tcp_swl1;		/* These help us avoid using stale */
330 	uint32_t tcp_swl2;		/*  packets to update state */
331 
332 	uint32_t tcp_rack;		/* Seq # we have acked */
333 	uint32_t tcp_rack_cnt;		/* # of segs we have deferred ack */
334 	uint32_t tcp_rack_cur_max;	/* # of segs we may defer ack for now */
335 	uint32_t tcp_rack_abs_max;	/* # of segs we may defer ack ever */
336 	timeout_id_t	tcp_ack_tid;	/* Delayed ACK timer ID */
337 	timeout_id_t	tcp_push_tid;	/* Push timer ID */
338 
339 	uint32_t tcp_max_swnd;		/* Maximum swnd we have seen */
340 
341 	struct tcp_s *tcp_listener;	/* Our listener */
342 
343 	size_t	tcp_xmit_hiwater;	/* Send buffer high water mark. */
344 	size_t	tcp_xmit_lowater;	/* Send buffer low water mark. */
345 	size_t	tcp_recv_hiwater;	/* Recv high water mark */
346 	size_t	tcp_recv_lowater;	/* Recv low water mark */
347 
348 	uint32_t tcp_irs;		/* Initial recv seq num */
349 	uint32_t tcp_fss;		/* Final/fin send seq num */
350 	uint32_t tcp_urg;		/* Urgent data seq num */
351 
352 	clock_t	tcp_first_timer_threshold;  /* When to prod IP */
353 	clock_t	tcp_second_timer_threshold; /* When to give up completely */
354 	clock_t	tcp_first_ctimer_threshold; /* 1st threshold while connecting */
355 	clock_t tcp_second_ctimer_threshold; /* 2nd ... while connecting */
356 
357 	int	tcp_lingertime;		/* Close linger time (in seconds) */
358 
359 	uint32_t tcp_urp_last;		/* Last urp for which signal sent */
360 	mblk_t	*tcp_urp_mp;		/* T_EXDATA_IND for urgent byte */
361 	mblk_t	*tcp_urp_mark_mp;	/* zero-length marked/unmarked msg */
362 
363 	int tcp_conn_req_cnt_q0;	/* # of conn reqs in SYN_RCVD */
364 	int tcp_conn_req_cnt_q;	/* # of conn reqs in ESTABLISHED */
365 	int tcp_conn_req_max;	/* # of ESTABLISHED conn reqs allowed */
366 	t_scalar_t tcp_conn_req_seqnum;	/* Incrementing pending conn req ID */
367 #define	tcp_ip_addr_cache	tcp_reass_tail
368 					/* Cache ip addresses that */
369 					/* complete the 3-way handshake */
370 	kmutex_t  tcp_eager_lock;
371 	struct tcp_s *tcp_eager_next_q; /* next eager in ESTABLISHED state */
372 	struct tcp_s *tcp_eager_last_q;	/* last eager in ESTABLISHED state */
373 	struct tcp_s *tcp_eager_next_q0; /* next eager in SYN_RCVD state */
374 	struct tcp_s *tcp_eager_prev_q0; /* prev eager in SYN_RCVD state */
375 					/* all eagers form a circular list */
376 	boolean_t tcp_conn_def_q0;	/* move from q0 to q deferred */
377 
378 	union {
379 	    mblk_t *tcp_eager_conn_ind; /* T_CONN_IND waiting for 3rd ack. */
380 	    mblk_t *tcp_opts_conn_req; /* T_CONN_REQ w/ options processed */
381 	} tcp_conn;
382 	uint32_t tcp_syn_rcvd_timeout;	/* How many SYN_RCVD timeout in q0 */
383 
384 	/* TCP Keepalive Timer members */
385 	int32_t	tcp_ka_last_intrvl;	/* Last probe interval */
386 	timeout_id_t tcp_ka_tid;	/* Keepalive timer ID */
387 	uint32_t tcp_ka_interval;	/* Keepalive interval */
388 	uint32_t tcp_ka_abort_thres;	/* Keepalive abort threshold */
389 
390 	int32_t	tcp_client_errno;	/* How the client screwed up */
391 
392 	char	*tcp_iphc;		/* Buffer holding tcp/ip hdr template */
393 	int	tcp_iphc_len;		/* actual allocated buffer size */
394 	int32_t	tcp_hdr_len;		/* Byte len of combined TCP/IP hdr */
395 	ipha_t	*tcp_ipha;		/* IPv4 header in the buffer */
396 	ip6_t	*tcp_ip6h;		/* IPv6 header in the buffer */
397 	int	tcp_ip_hdr_len;		/* Byte len of our current IPvx hdr */
398 	tcph_t	*tcp_tcph;		/* tcp header within combined hdr */
399 	int32_t	tcp_tcp_hdr_len;	/* tcp header len within combined */
400 	/* Saved peer headers in the case of re-fusion */
401 	ipha_t	tcp_saved_ipha;
402 	ip6_t	tcp_saved_ip6h;
403 	tcph_t	tcp_saved_tcph;
404 
405 	uint32_t tcp_sum;		/* checksum to compensate for source */
406 					/* routed packets. Host byte order */
407 	uint16_t tcp_last_sent_len;	/* Record length for nagle */
408 	uint16_t tcp_dupack_cnt;	/* # of consequtive duplicate acks */
409 
410 	kmutex_t	*tcp_acceptor_lockp;	/* Ptr to tf_lock */
411 
412 	mblk_t		*tcp_ordrel_mp;		/* T_ordrel_ind mblk */
413 	t_uscalar_t	tcp_acceptor_id;	/* ACCEPTOR_id */
414 
415 	int		tcp_ipsec_overhead;
416 	/*
417 	 * Address family that app wishes returned addrsses to be in.
418 	 * Currently taken from address family used in T_BIND_REQ, but
419 	 * should really come from family used in original socket() call.
420 	 * Value can be AF_INET or AF_INET6.
421 	 */
422 	uint_t	tcp_family;
423 	/*
424 	 * used for a quick test to determine if any ancillary bits are
425 	 * set
426 	 */
427 	uint_t		tcp_ipv6_recvancillary;		/* Flags */
428 #define	TCP_IPV6_RECVPKTINFO	0x01	/* IPV6_RECVPKTINFO option  */
429 #define	TCP_IPV6_RECVHOPLIMIT	0x02	/* IPV6_RECVHOPLIMIT option */
430 #define	TCP_IPV6_RECVHOPOPTS	0x04	/* IPV6_RECVHOPOPTS option */
431 #define	TCP_IPV6_RECVDSTOPTS	0x08	/* IPV6_RECVDSTOPTS option */
432 #define	TCP_IPV6_RECVRTHDR	0x10	/* IPV6_RECVRTHDR option */
433 #define	TCP_IPV6_RECVRTDSTOPTS	0x20	/* IPV6_RECVRTHDRDSTOPTS option */
434 #define	TCP_IPV6_RECVTCLASS	0x40	/* IPV6_RECVTCLASS option */
435 #define	TCP_OLD_IPV6_RECVDSTOPTS 0x80	/* old IPV6_RECVDSTOPTS option */
436 
437 	uint_t		tcp_recvifindex; /* Last received IPV6_RCVPKTINFO */
438 	uint_t		tcp_recvhops;	/* Last received IPV6_RECVHOPLIMIT */
439 	uint_t		tcp_recvtclass;	/* Last received IPV6_RECVTCLASS */
440 	ip6_hbh_t	*tcp_hopopts;	/* Last received IPV6_RECVHOPOPTS */
441 	ip6_dest_t	*tcp_dstopts;	/* Last received IPV6_RECVDSTOPTS */
442 	ip6_dest_t	*tcp_rtdstopts;	/* Last recvd IPV6_RECVRTHDRDSTOPTS */
443 	ip6_rthdr_t	*tcp_rthdr;	/* Last received IPV6_RECVRTHDR */
444 	uint_t		tcp_hopoptslen;
445 	uint_t		tcp_dstoptslen;
446 	uint_t		tcp_rtdstoptslen;
447 	uint_t		tcp_rthdrlen;
448 
449 	mblk_t		*tcp_timercache;
450 	cred_t		*tcp_cred;	/* Credentials when this was opened */
451 	pid_t		tcp_cpid;	/* Process id when this was opened */
452 	uint64_t	tcp_open_time;	/* time when this was opened */
453 
454 
455 	union {
456 		struct {
457 			uchar_t	v4_ttl;
458 				/* Dup of tcp_ipha.iph_type_of_service */
459 			uchar_t	v4_tos; /* Dup of tcp_ipha.iph_ttl */
460 		} v4_hdr_info;
461 		struct {
462 			uint_t	v6_vcf;		/* Dup of tcp_ip6h.ip6h_vcf */
463 			uchar_t	v6_hops;	/* Dup of tcp_ip6h.ip6h_hops */
464 		} v6_hdr_info;
465 	} tcp_hdr_info;
466 #define	tcp_ttl	tcp_hdr_info.v4_hdr_info.v4_ttl
467 #define	tcp_tos	tcp_hdr_info.v4_hdr_info.v4_tos
468 #define	tcp_ip6_vcf	tcp_hdr_info.v6_hdr_info.v6_vcf
469 #define	tcp_ip6_hops	tcp_hdr_info.v6_hdr_info.v6_hops
470 
471 	ushort_t	tcp_ipversion;
472 	uint_t		tcp_bound_if;	/* IPV6_BOUND_IF */
473 
474 #define	tcp_lport	tcp_connp->conn_lport
475 #define	tcp_fport	tcp_connp->conn_fport
476 #define	tcp_ports	tcp_connp->conn_ports
477 
478 #define	tcp_remote	tcp_connp->conn_rem
479 #define	tcp_ip_src	tcp_connp->conn_src
480 
481 #define	tcp_remote_v6	tcp_connp->conn_remv6
482 #define	tcp_ip_src_v6	tcp_connp->conn_srcv6
483 #define	tcp_bound_source_v6	tcp_connp->conn_bound_source_v6
484 #define	tcp_bound_source	tcp_connp->conn_bound_source
485 
486 	kmutex_t	tcp_closelock;
487 	kcondvar_t	tcp_closecv;
488 	uint8_t		tcp_closed;
489 	uint8_t		tcp_closeflags;
490 	uint8_t		tcp_cleandeathtag;
491 	mblk_t		tcp_closemp;
492 	timeout_id_t	tcp_linger_tid;	/* Linger timer ID */
493 
494 	struct tcp_s *tcp_acceptor_hash; /* Acceptor hash chain */
495 	struct tcp_s **tcp_ptpahn; /* Pointer to previous accept hash next. */
496 	struct tcp_s *tcp_bind_hash; /* Bind hash chain */
497 	struct tcp_s *tcp_bind_hash_port; /* tcp_t's bound to the same lport */
498 	struct tcp_s **tcp_ptpbhn;
499 
500 	boolean_t	tcp_ire_ill_check_done;
501 	uint_t		tcp_maxpsz;
502 
503 	/*
504 	 * used for Multidata Transmit
505 	 */
506 	uint_t	tcp_mdt_hdr_head; /* leading header fragment extra space */
507 	uint_t	tcp_mdt_hdr_tail; /* trailing header fragment extra space */
508 	int	tcp_mdt_max_pld;  /* maximum payload buffers per Multidata */
509 
510 	uint32_t	tcp_lso_max; /* maximum LSO payload */
511 
512 	uint32_t	tcp_ofo_fin_seq; /* Recv out of order FIN seq num */
513 	uint32_t	tcp_cwr_snd_max;
514 	uint_t		tcp_drop_opt_ack_cnt; /* # tcp generated optmgmt */
515 	ip6_pkt_t	tcp_sticky_ipp;			/* Sticky options */
516 #define	tcp_ipp_fields	tcp_sticky_ipp.ipp_fields	/* valid fields */
517 #define	tcp_ipp_ifindex	tcp_sticky_ipp.ipp_ifindex	/* pktinfo ifindex */
518 #define	tcp_ipp_addr	tcp_sticky_ipp.ipp_addr	/* pktinfo src/dst addr */
519 #define	tcp_ipp_hoplimit	tcp_sticky_ipp.ipp_hoplimit
520 #define	tcp_ipp_hopoptslen	tcp_sticky_ipp.ipp_hopoptslen
521 #define	tcp_ipp_rtdstoptslen	tcp_sticky_ipp.ipp_rtdstoptslen
522 #define	tcp_ipp_rthdrlen	tcp_sticky_ipp.ipp_rthdrlen
523 #define	tcp_ipp_dstoptslen	tcp_sticky_ipp.ipp_dstoptslen
524 #define	tcp_ipp_hopopts		tcp_sticky_ipp.ipp_hopopts
525 #define	tcp_ipp_rtdstopts	tcp_sticky_ipp.ipp_rtdstopts
526 #define	tcp_ipp_rthdr		tcp_sticky_ipp.ipp_rthdr
527 #define	tcp_ipp_dstopts		tcp_sticky_ipp.ipp_dstopts
528 #define	tcp_ipp_nexthop		tcp_sticky_ipp.ipp_nexthop
529 #define	tcp_ipp_use_min_mtu	tcp_sticky_ipp.ipp_use_min_mtu
530 	struct tcp_s *tcp_saved_listener;	/* saved value of listener */
531 
532 	uint32_t	tcp_in_ack_unsent;	/* ACK for unsent data cnt. */
533 
534 	/*
535 	 * The following fusion-related fields are protected by squeue.
536 	 */
537 	struct tcp_s *tcp_loopback_peer;	/* peer tcp for loopback */
538 	mblk_t	*tcp_fused_sigurg_mp;		/* M_PCSIG mblk for SIGURG */
539 	size_t	tcp_fuse_rcv_hiwater;		/* fusion receive queue size */
540 	uint_t	tcp_fuse_rcv_unread_hiwater;	/* max # of outstanding pkts */
541 	/*
542 	 * The following fusion-related fields and bit fields are to be
543 	 * manipulated with squeue protection or with tcp_non_sq_lock held.
544 	 * tcp_non_sq_lock is used to protect fields that may be modified
545 	 * accessed outside the squeue.
546 	 */
547 	kmutex_t tcp_non_sq_lock;
548 	kcondvar_t tcp_fuse_plugcv;
549 	uint_t tcp_fuse_rcv_unread_cnt;	/* # of outstanding pkts */
550 	uint32_t
551 		tcp_fused : 1,		/* loopback tcp in fusion mode */
552 		tcp_unfusable : 1,	/* fusion not allowed on endpoint */
553 		tcp_fused_sigurg : 1,	/* send SIGURG upon draining */
554 		tcp_direct_sockfs : 1,	/* direct calls to sockfs */
555 
556 		tcp_fuse_syncstr_stopped : 1, /* synchronous streams stopped */
557 		tcp_fuse_syncstr_plugged : 1, /* synchronous streams plugged */
558 		tcp_fuse_to_bit_31 : 26;
559 
560 	/*
561 	 * This variable is accessed without any lock protection
562 	 * and therefore must not be declared as a bit field along
563 	 * with the rest which require such condition.
564 	 */
565 	boolean_t	tcp_issocket;	/* this is a socket tcp */
566 
567 	/* protected by the tcp_non_sq_lock lock */
568 	uint32_t	tcp_squeue_bytes;
569 	/*
570 	 * Kernel SSL session information
571 	 */
572 	boolean_t		tcp_kssl_pending; /* waiting for 1st SSL rec. */
573 	boolean_t		tcp_kssl_inhandshake; /* during SSL handshake */
574 	kssl_ent_t		tcp_kssl_ent;	/* SSL table entry */
575 	kssl_ctx_t		tcp_kssl_ctx;	/* SSL session */
576 	uint_t	tcp_label_len;	/* length of cached label */
577 
578 	/*
579 	 * tcp_closemp_used is protected by listener's tcp_eager_lock
580 	 * when used for eagers. When used for a tcp in TIME_WAIT state
581 	 * or in tcp_close(), it is not protected by any lock as we
582 	 * do not expect any other thread to use it concurrently.
583 	 * We do allow re-use of tcp_closemp in tcp_time_wait_collector()
584 	 * and tcp_close() but not concurrently.
585 	 */
586 	boolean_t tcp_closemp_used;
587 
588 	/*
589 	 * previous and next eagers in the list of droppable eagers. See
590 	 * the comments before MAKE_DROPPABLE(). These pointers are
591 	 * protected by listener's tcp_eager_lock.
592 	 */
593 	struct tcp_s	*tcp_eager_prev_drop_q0;
594 	struct tcp_s	*tcp_eager_next_drop_q0;
595 
596 	/*
597 	 * Have we flow controlled xmitter?
598 	 * This variable can be modified outside the squeue and hence must
599 	 * not be declared as a bit field along with the rest that are
600 	 * modified only within the squeue.
601 	 * protected by the tcp_non_sq_lock lock.
602 	 */
603 	boolean_t	tcp_flow_stopped;
604 
605 	/*
606 	 * The socket generation number is bumped when an outgoing connection
607 	 * attempts is made, and it sent up to the socket when the
608 	 * connection was successfully established, or an error occured. The
609 	 * generation is used to ensure that the socket does not miss the
610 	 * asynchronous notification.
611 	 */
612 	sock_connid_t	tcp_connid;
613 
614 	/*
615 	 * tcp_sodirect is used by tcp on the receive side to push mblk_t(s)
616 	 * directly to sockfs. Also, to schedule asynchronous copyout directly
617 	 * to a pending user-land uio buffer.
618 	 */
619 	sodirect_t	*tcp_sodirect;
620 
621 	/* mblk_t used to enter TCP's squeue from the service routine. */
622 	mblk_t		*tcp_rsrv_mp;
623 	/* Mutex for accessing tcp_rsrv_mp */
624 	kmutex_t	tcp_rsrv_mp_lock;
625 
626 #ifdef DEBUG
627 	pc_t			tcmp_stk[15];
628 #endif
629 } tcp_t;
630 
631 #ifdef DEBUG
632 #define	TCP_DEBUG_GETPCSTACK(buffer, depth)	((void) getpcstack(buffer, \
633 						    depth))
634 #else
635 #define	TCP_DEBUG_GETPCSTACK(buffer, depth)
636 #endif
637 
638 /*
639  * Track a reference count on the tcps in order to know when
640  * the tcps_g_q can be removed. As long as there is any
641  * tcp_t, other that the tcps_g_q itself, in the tcp_stack_t we
642  * need to keep tcps_g_q around so that a closing connection can
643  * switch to using tcps_g_q as part of it closing.
644  */
645 #define	TCPS_REFHOLD(tcps) {					\
646 	atomic_add_32(&(tcps)->tcps_refcnt, 1);			\
647 	ASSERT((tcps)->tcps_refcnt != 0);			\
648 	DTRACE_PROBE1(tcps__refhold, tcp_stack_t, tcps);	\
649 }
650 
651 /*
652  * Decrement the reference count on the tcp_stack_t.
653  * In architectures e.g sun4u, where atomic_add_32_nv is just
654  * a cas, we need to maintain the right memory barrier semantics
655  * as that of mutex_exit i.e all the loads and stores should complete
656  * before the cas is executed. membar_exit() does that here.
657  */
658 #define	TCPS_REFRELE(tcps) {					\
659 	ASSERT((tcps)->tcps_refcnt != 0);			\
660 	membar_exit();						\
661 	DTRACE_PROBE1(tcps__refrele, tcp_stack_t, tcps);	\
662 	if (atomic_add_32_nv(&(tcps)->tcps_refcnt, -1) == 0 &&	\
663 	    (tcps)->tcps_g_q != NULL) {				\
664 		/* Only tcps_g_q left */			\
665 		tcp_g_q_inactive(tcps);				\
666 	}							\
667 }
668 
669 extern void 	tcp_free(tcp_t *tcp);
670 extern void	tcp_ddi_g_init(void);
671 extern void	tcp_ddi_g_destroy(void);
672 extern void	tcp_g_q_inactive(tcp_stack_t *);
673 extern void	tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len,
674     zoneid_t zoneid, tcp_stack_t *, conn_t *connp);
675 extern void	tcp_conn_request(void *arg, mblk_t *mp, void *arg2);
676 extern void	tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2);
677 extern void 	tcp_input(void *arg, mblk_t *mp, void *arg2);
678 extern void	tcp_rput_data(void *arg, mblk_t *mp, void *arg2);
679 extern void 	*tcp_get_conn(void *arg, tcp_stack_t *, boolean_t);
680 extern void	tcp_time_wait_collector(void *arg);
681 extern mblk_t	*tcp_snmp_get(queue_t *, mblk_t *);
682 extern int	tcp_snmp_set(queue_t *, int, int, uchar_t *, int len);
683 extern mblk_t	*tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send,
684 		    int32_t *offset, mblk_t **end_mp, uint32_t seq,
685 		    boolean_t sendall, uint32_t *seg_len, boolean_t rexmit);
686 extern void	tcp_xmit_reset(void *arg, mblk_t *mp, void *arg2);
687 
688 /*
689  * The TCP Fanout structure.
690  * The hash tables and their linkage (tcp_*_hash_next, tcp_ptp*hn) are
691  * protected by the per-bucket tf_lock. Each tcp_t
692  * inserted in the list points back at this lock using tcp_*_lockp.
693  *
694  * The listener and acceptor hash queues are lists of tcp_t.
695  */
696 /* listener hash and acceptor hash queue head */
697 typedef struct tf_s {
698 	tcp_t		*tf_tcp;
699 	kmutex_t	tf_lock;
700 } tf_t;
701 #endif	/* (defined(_KERNEL) || defined(_KMEMUSER)) */
702 
703 /* Contract private interface between TCP and Clustering. */
704 
705 #define	CL_TCPI_V1	1	/* cl_tcpi_version number */
706 
707 typedef struct cl_tcp_info_s {
708 	ushort_t	cl_tcpi_version;	/* cl_tcp_info_t's version no */
709 	ushort_t	cl_tcpi_ipversion;	/* IP version */
710 	int32_t		cl_tcpi_state;		/* TCP state */
711 	in_port_t	cl_tcpi_lport;		/* Local port */
712 	in_port_t	cl_tcpi_fport;		/* Remote port */
713 	in6_addr_t	cl_tcpi_laddr_v6;	/* Local IP address */
714 	in6_addr_t	cl_tcpi_faddr_v6;	/* Remote IP address */
715 #ifdef _KERNEL
716 /* Note: V4_PART_OF_V6 is meant to be used only for _KERNEL defined stuff */
717 #define	cl_tcpi_laddr	V4_PART_OF_V6(cl_tcpi_laddr_v6)
718 #define	cl_tcpi_faddr	V4_PART_OF_V6(cl_tcpi_faddr_v6)
719 
720 #endif	/* _KERNEL */
721 } cl_tcp_info_t;
722 
723 /*
724  * Contracted Consolidation Private ioctl for aborting TCP connections.
725  * In order to keep the offsets and size of the structure the same between
726  * a 32-bit application and a 64-bit amd64 kernel, we use a #pragma
727  * pack(4).
728  */
729 #define	TCP_IOC_ABORT_CONN	(('T' << 8) + 91)
730 
731 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
732 #pragma pack(4)
733 #endif
734 
735 typedef struct tcp_ioc_abort_conn_s {
736 	struct sockaddr_storage ac_local;	/* local addr and port */
737 	struct sockaddr_storage ac_remote;	/* remote addr and port */
738 	int32_t ac_start;			/* start state */
739 	int32_t ac_end;				/* end state  */
740 	int32_t ac_zoneid;			/* zoneid */
741 } tcp_ioc_abort_conn_t;
742 
743 #if _LONG_LONG_ALIGNMENT == 8 && _LONG_LONG_ALIGNMENT_32 == 4
744 #pragma pack()
745 #endif
746 
747 #if (defined(_KERNEL) || defined(_KMEMUSER))
748 extern void tcp_rput_other(tcp_t *tcp, mblk_t *mp);
749 #endif
750 
751 #if (defined(_KERNEL))
752 #define	TCP_XRE_EVENT_IP_FANOUT_TCP 1
753 
754 /*
755  * This is a private structure used to pass data to an squeue function during
756  * tcp's listener reset sending path.
757  */
758 typedef struct tcp_xmit_reset_event {
759 	int		tcp_xre_event;
760 	int		tcp_xre_iphdrlen;
761 	zoneid_t	tcp_xre_zoneid;
762 	tcp_stack_t	*tcp_xre_tcps;
763 } tcp_xmit_reset_event_t;
764 #endif
765 
766 #ifdef	__cplusplus
767 }
768 #endif
769 
770 #endif	/* _INET_TCP_H */
771