tcp: remove _strong_iss tunable
[unleashed.git] / kernel / net / tcp / tcp.c
blob90e11989085127063dff2014e6d5f799c72753f5
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
26 * Copyright (c) 2013,2014 by Delphix. All rights reserved.
27 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
29 /* Copyright (c) 1990 Mentat Inc. */
31 #include <sys/types.h>
32 #include <sys/stream.h>
33 #include <sys/strsun.h>
34 #include <sys/strsubr.h>
35 #include <sys/stropts.h>
36 #include <sys/strlog.h>
37 #define _SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/timod.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/suntpi.h>
43 #include <sys/xti_inet.h>
44 #include <sys/cmn_err.h>
45 #include <sys/debug.h>
46 #include <sys/sdt.h>
47 #include <sys/vtrace.h>
48 #include <sys/kmem.h>
49 #include <sys/ethernet.h>
50 #include <sys/cpuvar.h>
51 #include <sys/dlpi.h>
52 #include <sys/pattr.h>
53 #include <sys/policy.h>
54 #include <sys/priv.h>
55 #include <sys/zone.h>
56 #include <sys/sunldi.h>
58 #include <sys/errno.h>
59 #include <sys/signal.h>
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 #include <sys/sockio.h>
63 #include <sys/isa_defs.h>
64 #include <sys/md5.h>
65 #include <sys/random.h>
66 #include <sys/uio.h>
67 #include <sys/systm.h>
68 #include <netinet/in.h>
69 #include <netinet/tcp.h>
70 #include <netinet/ip6.h>
71 #include <netinet/icmp6.h>
72 #include <net/if.h>
73 #include <net/route.h>
74 #include <inet/ipsec_impl.h>
76 #include <inet/common.h>
77 #include <inet/ip.h>
78 #include <inet/ip_impl.h>
79 #include <inet/ip6.h>
80 #include <inet/ip_ndp.h>
81 #include <inet/proto_set.h>
82 #include <inet/mib2.h>
83 #include <inet/optcom.h>
84 #include <inet/snmpcom.h>
85 #include <inet/kstatcom.h>
86 #include <inet/tcp.h>
87 #include <inet/tcp_impl.h>
88 #include <inet/udp_impl.h>
89 #include <net/pfkeyv2.h>
90 #include <inet/ipdrop.h>
92 #include <inet/ipclassifier.h>
93 #include <inet/ip_ire.h>
94 #include <inet/ip_ftable.h>
95 #include <inet/ip_if.h>
96 #include <inet/ipp_common.h>
97 #include <inet/ip_rts.h>
98 #include <inet/ip_netinfo.h>
99 #include <sys/squeue_impl.h>
100 #include <sys/squeue.h>
101 #include <rpc/pmap_prot.h>
102 #include <sys/callo.h>
105 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433)
107 * (Read the detailed design doc in PSARC case directory)
109 * The entire tcp state is contained in tcp_t and conn_t structure
110 * which are allocated in tandem using ipcl_conn_create() and passing
111 * IPCL_TCPCONN as a flag. We use 'conn_ref' and 'conn_lock' to protect
112 * the references on the tcp_t. The tcp_t structure is never compressed
113 * and packets always land on the correct TCP perimeter from the time
114 * eager is created till the time tcp_t dies (as such the old mentat
115 * TCP global queue is not used for detached state and no IPSEC checking
116 * is required). The global queue is still allocated to send out resets
117 * for connection which have no listeners and IP directly calls
118 * tcp_xmit_listeners_reset() which does any policy check.
120 * Protection and Synchronisation mechanism:
122 * The tcp data structure does not use any kind of lock for protecting
123 * its state but instead uses 'squeues' for mutual exclusion from various
124 * read and write side threads. To access a tcp member, the thread should
125 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS,
126 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller
127 * can pass any tcp function having prototype of edesc_t as argument
128 * (different from traditional STREAMs model where packets come in only
129 * designated entry points). The list of functions that can be directly
130 * called via squeue are listed before the usual function prototype.
132 * Referencing:
134 * TCP is MT-Hot and we use a reference based scheme to make sure that the
135 * tcp structure doesn't disappear when its needed. When the application
136 * creates an outgoing connection or accepts an incoming connection, we
137 * start out with 2 references on 'conn_ref'. One for TCP and one for IP.
138 * The IP reference is just a symbolic reference since ip_tcpclose()
139 * looks at tcp structure after tcp_close_output() returns which could
140 * have dropped the last TCP reference. So as long as the connection is
141 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the
142 * conn_t. The classifier puts its own reference when the connection is
143 * inserted in listen or connected hash. Anytime a thread needs to enter
144 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr
145 * on write side or by doing a classify on read side and then puts a
146 * reference on the conn before doing squeue_enter/tryenter/fill. For
147 * read side, the classifier itself puts the reference under fanout lock
148 * to make sure that tcp can't disappear before it gets processed. The
149 * squeue will drop this reference automatically so the called function
150 * doesn't have to do a DEC_REF.
152 * Opening a new connection:
154 * The outgoing connection open is pretty simple. tcp_open() does the
155 * work in creating the conn/tcp structure and initializing it. The
156 * squeue assignment is done based on the CPU the application
157 * is running on. So for outbound connections, processing is always done
158 * on application CPU which might be different from the incoming CPU
159 * being interrupted by the NIC. An optimal way would be to figure out
160 * the NIC <-> CPU binding at listen time, and assign the outgoing
161 * connection to the squeue attached to the CPU that will be interrupted
162 * for incoming packets (we know the NIC based on the bind IP address).
163 * This might seem like a problem if more data is going out but the
164 * fact is that in most cases the transmit is ACK driven transmit where
165 * the outgoing data normally sits on TCP's xmit queue waiting to be
166 * transmitted.
168 * Accepting a connection:
170 * This is a more interesting case because of various races involved in
171 * establishing a eager in its own perimeter. Read the meta comment on
172 * top of tcp_input_listener(). But briefly, the squeue is picked by
173 * ip_fanout based on the ring or the sender (if loopback).
175 * Closing a connection:
177 * The close is fairly straight forward. tcp_close() calls tcp_close_output()
178 * via squeue to do the close and mark the tcp as detached if the connection
179 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its
180 * reference but tcp_close() drop IP's reference always. So if tcp was
181 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP
182 * and 1 because it is in classifier's connected hash. This is the condition
183 * we use to determine that its OK to clean up the tcp outside of squeue
184 * when time wait expires (check the ref under fanout and conn_lock and
185 * if it is 2, remove it from fanout hash and kill it).
187 * Although close just drops the necessary references and marks the
188 * tcp_detached state, tcp_close needs to know the tcp_detached has been
189 * set (under squeue) before letting the STREAM go away (because a
190 * inbound packet might attempt to go up the STREAM while the close
191 * has happened and tcp_detached is not set). So a special lock and
192 * flag is used along with a condition variable (tcp_closelock, tcp_closed,
193 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked
194 * tcp_detached.
196 * Special provisions and fast paths:
198 * We make special provisions for sockfs by marking tcp_issocket
199 * whenever we have only sockfs on top of TCP. This allows us to skip
200 * putting the tcp in acceptor hash since a sockfs listener can never
201 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM
202 * since eager has already been allocated and the accept now happens
203 * on acceptor STREAM. There is a big blob of comment on top of
204 * tcp_input_listener explaining the new accept. When socket is POP'd,
205 * sockfs sends us an ioctl to mark the fact and we go back to old
206 * behaviour. Once tcp_issocket is unset, its never set for the
207 * life of that connection.
209 * IPsec notes :
211 * Since a packet is always executed on the correct TCP perimeter
212 * all IPsec processing is defered to IP including checking new
213 * connections and setting IPSEC policies for new connection. The
214 * only exception is tcp_xmit_listeners_reset() which is called
215 * directly from IP and needs to policy check to see if TH_RST
216 * can be sent out.
220 * Values for squeue switch:
221 * 1: SQ_NODRAIN
222 * 2: SQ_PROCESS
223 * 3: SQ_FILL
225 int tcp_squeue_wput = 2; /* /etc/systems */
226 int tcp_squeue_flag;
229 * To prevent memory hog, limit the number of entries in tcp_free_list
230 * to 1% of available memory / number of cpus
232 uint_t tcp_free_list_max_cnt = 0;
234 #define TIDUSZ 4096 /* transport interface data unit size */
237 * Size of acceptor hash list. It has to be a power of 2 for hashing.
239 #define TCP_ACCEPTOR_FANOUT_SIZE 512
241 #ifdef _ILP32
242 #define TCP_ACCEPTOR_HASH(accid) \
243 (((uint_t)(accid) >> 8) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
244 #else
245 #define TCP_ACCEPTOR_HASH(accid) \
246 ((uint_t)(accid) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
247 #endif /* _ILP32 */
250 * Minimum number of connections which can be created per listener. Used
251 * when the listener connection count is in effect.
253 static uint32_t tcp_min_conn_listener = 2;
255 uint32_t tcp_early_abort = 30;
257 /* TCP Timer control structure */
258 typedef struct tcpt_s {
259 pfv_t tcpt_pfv; /* The routine we are to call */
260 tcp_t *tcpt_tcp; /* The parameter we are to pass in */
261 } tcpt_t;
264 * Functions called directly via squeue having a prototype of edesc_t.
266 void tcp_input_listener(void *arg, mblk_t *mp, void *arg2,
267 ip_recv_attr_t *ira);
268 void tcp_input_data(void *arg, mblk_t *mp, void *arg2,
269 ip_recv_attr_t *ira);
270 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2,
271 ip_recv_attr_t *dummy);
274 /* Prototype for TCP functions */
275 static void tcp_random_init(void);
276 int tcp_random(void);
277 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp,
278 in_port_t dstport, uint_t srcid);
279 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp,
280 in_port_t dstport, uint32_t flowinfo,
281 uint_t srcid, uint32_t scope_id);
282 static void tcp_iss_init(tcp_t *tcp);
283 static void tcp_reinit(tcp_t *tcp);
284 static void tcp_reinit_values(tcp_t *tcp);
286 static void tcp_wsrv(queue_t *q);
287 static void tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa);
288 static void tcp_update_zcopy(tcp_t *tcp);
289 static void tcp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
290 ixa_notify_arg_t);
291 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns);
292 static void tcp_stack_fini(netstackid_t stackid, void *arg);
294 static int tcp_squeue_switch(int);
296 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t);
297 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *);
298 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *);
300 static void tcp_squeue_add(squeue_t *);
302 struct module_info tcp_rinfo = {
303 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER
306 static struct module_info tcp_winfo = {
307 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16
311 * Entry points for TCP as a device. The normal case which supports
312 * the TCP functionality.
313 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices.
315 struct qinit tcp_rinitv4 = {
316 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo
319 struct qinit tcp_rinitv6 = {
320 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo
323 struct qinit tcp_winit = {
324 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
327 /* Initial entry point for TCP in socket mode. */
328 struct qinit tcp_sock_winit = {
329 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
332 /* TCP entry point during fallback */
333 struct qinit tcp_fallback_sock_winit = {
334 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo
338 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing
339 * an accept. Avoid allocating data structures since eager has already
340 * been created.
342 struct qinit tcp_acceptor_rinit = {
343 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo
346 struct qinit tcp_acceptor_winit = {
347 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo
350 /* For AF_INET aka /dev/tcp */
351 struct streamtab tcpinfov4 = {
352 &tcp_rinitv4, &tcp_winit
355 /* For AF_INET6 aka /dev/tcp6 */
356 struct streamtab tcpinfov6 = {
357 &tcp_rinitv6, &tcp_winit
361 * Following assumes TPI alignment requirements stay along 32 bit
362 * boundaries
364 #define ROUNDUP32(x) \
365 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1))
367 /* Template for response to info request. */
368 struct T_info_ack tcp_g_t_info_ack = {
369 T_INFO_ACK, /* PRIM_type */
370 0, /* TSDU_size */
371 T_INFINITE, /* ETSDU_size */
372 T_INVALID, /* CDATA_size */
373 T_INVALID, /* DDATA_size */
374 sizeof (sin_t), /* ADDR_size */
375 0, /* OPT_size - not initialized here */
376 TIDUSZ, /* TIDU_size */
377 T_COTS_ORD, /* SERV_type */
378 TCPS_IDLE, /* CURRENT_state */
379 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
382 struct T_info_ack tcp_g_t_info_ack_v6 = {
383 T_INFO_ACK, /* PRIM_type */
384 0, /* TSDU_size */
385 T_INFINITE, /* ETSDU_size */
386 T_INVALID, /* CDATA_size */
387 T_INVALID, /* DDATA_size */
388 sizeof (sin6_t), /* ADDR_size */
389 0, /* OPT_size - not initialized here */
390 TIDUSZ, /* TIDU_size */
391 T_COTS_ORD, /* SERV_type */
392 TCPS_IDLE, /* CURRENT_state */
393 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
397 * TCP tunables related declarations. Definitions are in tcp_tunables.c
399 extern mod_prop_info_t tcp_propinfo_tbl[];
400 extern int tcp_propinfo_count;
402 #define IS_VMLOANED_MBLK(mp) \
403 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0)
405 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */
408 * Forces all connections to obey the value of the tcps_maxpsz_multiplier
409 * tunable settable via NDD. Otherwise, the per-connection behavior is
410 * determined dynamically during tcp_set_destination(), which is the default.
412 boolean_t tcp_static_maxpsz = B_FALSE;
415 * If the receive buffer size is changed, this function is called to update
416 * the upper socket layer on the new delayed receive wake up threshold.
418 static void
419 tcp_set_recv_threshold(tcp_t *tcp, uint32_t new_rcvthresh)
421 uint32_t default_threshold = SOCKET_RECVHIWATER >> 3;
423 if (IPCL_IS_NONSTR(tcp->tcp_connp)) {
424 conn_t *connp = tcp->tcp_connp;
425 struct sock_proto_props sopp;
428 * only increase rcvthresh upto default_threshold
430 if (new_rcvthresh > default_threshold)
431 new_rcvthresh = default_threshold;
433 sopp.sopp_flags = SOCKOPT_RCVTHRESH;
434 sopp.sopp_rcvthresh = new_rcvthresh;
436 (*connp->conn_upcalls->su_set_proto_props)
437 (connp->conn_upper_handle, &sopp);
442 * Figure out the value of window scale opton. Note that the rwnd is
443 * ASSUMED to be rounded up to the nearest MSS before the calculation.
444 * We cannot find the scale value and then do a round up of tcp_rwnd
445 * because the scale value may not be correct after that.
447 * Set the compiler flag to make this function inline.
449 void
450 tcp_set_ws_value(tcp_t *tcp)
452 int i;
453 uint32_t rwnd = tcp->tcp_rwnd;
455 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT;
456 i++, rwnd >>= 1)
458 tcp->tcp_rcv_ws = i;
462 * Remove cached/latched IPsec references.
464 void
465 tcp_ipsec_cleanup(tcp_t *tcp)
467 conn_t *connp = tcp->tcp_connp;
469 ASSERT(connp->conn_flags & IPCL_TCPCONN);
471 if (connp->conn_latch != NULL) {
472 IPLATCH_REFRELE(connp->conn_latch);
473 connp->conn_latch = NULL;
475 if (connp->conn_latch_in_policy != NULL) {
476 IPPOL_REFRELE(connp->conn_latch_in_policy);
477 connp->conn_latch_in_policy = NULL;
479 if (connp->conn_latch_in_action != NULL) {
480 IPACT_REFRELE(connp->conn_latch_in_action);
481 connp->conn_latch_in_action = NULL;
483 if (connp->conn_policy != NULL) {
484 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
485 connp->conn_policy = NULL;
490 * Cleaup before placing on free list.
491 * Disassociate from the netstack/tcp_stack_t since the freelist
492 * is per squeue and not per netstack.
494 void
495 tcp_cleanup(tcp_t *tcp)
497 mblk_t *mp;
498 conn_t *connp = tcp->tcp_connp;
499 tcp_stack_t *tcps = tcp->tcp_tcps;
500 netstack_t *ns = tcps->tcps_netstack;
501 mblk_t *tcp_rsrv_mp;
503 tcp_bind_hash_remove(tcp);
505 /* Cleanup that which needs the netstack first */
506 tcp_ipsec_cleanup(tcp);
507 ixa_cleanup(connp->conn_ixa);
509 if (connp->conn_ht_iphc != NULL) {
510 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
511 connp->conn_ht_iphc = NULL;
512 connp->conn_ht_iphc_allocated = 0;
513 connp->conn_ht_iphc_len = 0;
514 connp->conn_ht_ulp = NULL;
515 connp->conn_ht_ulp_len = 0;
516 tcp->tcp_ipha = NULL;
517 tcp->tcp_ip6h = NULL;
518 tcp->tcp_tcpha = NULL;
521 /* We clear any IP_OPTIONS and extension headers */
522 ip_pkt_free(&connp->conn_xmit_ipp);
524 tcp_free(tcp);
527 * Since we will bzero the entire structure, we need to
528 * remove it and reinsert it in global hash list. We
529 * know the walkers can't get to this conn because we
530 * had set CONDEMNED flag earlier and checked reference
531 * under conn_lock so walker won't pick it and when we
532 * go the ipcl_globalhash_remove() below, no walker
533 * can get to it.
535 ipcl_globalhash_remove(connp);
537 /* Save some state */
538 mp = tcp->tcp_timercache;
540 tcp_rsrv_mp = tcp->tcp_rsrv_mp;
542 if (connp->conn_cred != NULL) {
543 crfree(connp->conn_cred);
544 connp->conn_cred = NULL;
546 ipcl_conn_cleanup(connp);
547 connp->conn_flags = IPCL_TCPCONN;
550 * Now it is safe to decrement the reference counts.
551 * This might be the last reference on the netstack
552 * in which case it will cause the freeing of the IP Instance.
554 connp->conn_netstack = NULL;
555 connp->conn_ixa->ixa_ipst = NULL;
556 netstack_rele(ns);
557 ASSERT(tcps != NULL);
558 tcp->tcp_tcps = NULL;
560 bzero(tcp, sizeof (tcp_t));
562 /* restore the state */
563 tcp->tcp_timercache = mp;
565 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
567 tcp->tcp_connp = connp;
569 ASSERT(connp->conn_tcp == tcp);
570 ASSERT(connp->conn_flags & IPCL_TCPCONN);
571 connp->conn_state_flags = CONN_INCIPIENT;
572 ASSERT(connp->conn_proto == IPPROTO_TCP);
573 ASSERT(connp->conn_ref == 1);
577 * Adapt to the information, such as rtt and rtt_sd, provided from the
578 * DCE and IRE maintained by IP.
580 * Checks for multicast and broadcast destination address.
581 * Returns zero if ok; an errno on failure.
583 * Note that the MSS calculation here is based on the info given in
584 * the DCE and IRE. We do not do any calculation based on TCP options. They
585 * will be handled in tcp_input_data() when TCP knows which options to use.
587 * Note on how TCP gets its parameters for a connection.
589 * When a tcp_t structure is allocated, it gets all the default parameters.
590 * In tcp_set_destination(), it gets those metric parameters, like rtt, rtt_sd,
591 * spipe, rpipe, ... from the route metrics. Route metric overrides the
592 * default.
594 * An incoming SYN with a multicast or broadcast destination address is dropped
595 * in ip_fanout_v4/v6.
597 * An incoming SYN with a multicast or broadcast source address is always
598 * dropped in tcp_set_destination, since IPDF_ALLOW_MCBC is not set in
599 * conn_connect.
600 * The same logic in tcp_set_destination also serves to
601 * reject an attempt to connect to a broadcast or multicast (destination)
602 * address.
605 tcp_set_destination(tcp_t *tcp)
607 uint32_t mss_max;
608 uint32_t mss;
609 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
610 conn_t *connp = tcp->tcp_connp;
611 tcp_stack_t *tcps = tcp->tcp_tcps;
612 iulp_t uinfo;
613 int error;
614 uint32_t flags;
616 flags = IPDF_LSO | IPDF_ZCOPY;
618 * Make sure we have a dce for the destination to avoid dce_ident
619 * contention for connected sockets.
621 flags |= IPDF_UNIQUE_DCE;
623 if (!tcps->tcps_ignore_path_mtu)
624 connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
626 /* Use conn_lock to satify ASSERT; tcp is already serialized */
627 mutex_enter(&connp->conn_lock);
628 error = conn_connect(connp, &uinfo, flags);
629 mutex_exit(&connp->conn_lock);
630 if (error != 0)
631 return (error);
633 error = tcp_build_hdrs(tcp);
634 if (error != 0)
635 return (error);
637 tcp->tcp_localnet = uinfo.iulp_localnet;
639 if (uinfo.iulp_rtt != 0) {
640 clock_t rto;
642 tcp->tcp_rtt_sa = uinfo.iulp_rtt;
643 tcp->tcp_rtt_sd = uinfo.iulp_rtt_sd;
644 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
645 tcps->tcps_rexmit_interval_extra +
646 (tcp->tcp_rtt_sa >> 5);
648 TCP_SET_RTO(tcp, rto);
650 if (uinfo.iulp_ssthresh != 0)
651 tcp->tcp_cwnd_ssthresh = uinfo.iulp_ssthresh;
652 else
653 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
654 if (uinfo.iulp_spipe > 0) {
655 connp->conn_sndbuf = MIN(uinfo.iulp_spipe,
656 tcps->tcps_max_buf);
657 if (tcps->tcps_snd_lowat_fraction != 0) {
658 connp->conn_sndlowat = connp->conn_sndbuf /
659 tcps->tcps_snd_lowat_fraction;
661 (void) tcp_maxpsz_set(tcp, B_TRUE);
664 * Note that up till now, acceptor always inherits receive
665 * window from the listener. But if there is a metrics
666 * associated with a host, we should use that instead of
667 * inheriting it from listener. Thus we need to pass this
668 * info back to the caller.
670 if (uinfo.iulp_rpipe > 0) {
671 tcp->tcp_rwnd = MIN(uinfo.iulp_rpipe,
672 tcps->tcps_max_buf);
675 if (uinfo.iulp_rtomax > 0) {
676 tcp->tcp_second_timer_threshold =
677 uinfo.iulp_rtomax;
681 * Use the metric option settings, iulp_tstamp_ok and
682 * iulp_wscale_ok, only for active open. What this means
683 * is that if the other side uses timestamp or window
684 * scale option, TCP will also use those options. That
685 * is for passive open. If the application sets a
686 * large window, window scale is enabled regardless of
687 * the value in iulp_wscale_ok. This is the behavior
688 * since 2.6. So we keep it.
689 * The only case left in passive open processing is the
690 * check for SACK.
691 * For ECN, it should probably be like SACK. But the
692 * current value is binary, so we treat it like the other
693 * cases. The metric only controls active open.For passive
694 * open, the ndd param, tcp_ecn_permitted, controls the
695 * behavior.
697 if (!tcp_detached) {
699 * The if check means that the following can only
700 * be turned on by the metrics only IRE, but not off.
702 if (uinfo.iulp_tstamp_ok)
703 tcp->tcp_snd_ts_ok = B_TRUE;
704 if (uinfo.iulp_wscale_ok)
705 tcp->tcp_snd_ws_ok = B_TRUE;
706 if (uinfo.iulp_sack == 2)
707 tcp->tcp_snd_sack_ok = B_TRUE;
708 if (uinfo.iulp_ecn_ok)
709 tcp->tcp_ecn_ok = B_TRUE;
710 } else {
712 * Passive open.
714 * As above, the if check means that SACK can only be
715 * turned on by the metric only IRE.
717 if (uinfo.iulp_sack > 0) {
718 tcp->tcp_snd_sack_ok = B_TRUE;
723 * XXX Note that currently, iulp_mtu can be as small as 68
724 * because of PMTUd. So tcp_mss may go to negative if combined
725 * length of all those options exceeds 28 bytes. But because
726 * of the tcp_mss_min check below, we may not have a problem if
727 * tcp_mss_min is of a reasonable value. The default is 1 so
728 * the negative problem still exists. And the check defeats PMTUd.
729 * In fact, if PMTUd finds that the MSS should be smaller than
730 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min
731 * value.
733 * We do not deal with that now. All those problems related to
734 * PMTUd will be fixed later.
736 ASSERT(uinfo.iulp_mtu != 0);
737 mss = tcp->tcp_initial_pmtu = uinfo.iulp_mtu;
739 /* Sanity check for MSS value. */
740 if (connp->conn_ipversion == IPV4_VERSION)
741 mss_max = tcps->tcps_mss_max_ipv4;
742 else
743 mss_max = tcps->tcps_mss_max_ipv6;
745 if (tcp->tcp_ipsec_overhead == 0)
746 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp);
748 mss -= tcp->tcp_ipsec_overhead;
750 if (mss < tcps->tcps_mss_min)
751 mss = tcps->tcps_mss_min;
752 if (mss > mss_max)
753 mss = mss_max;
755 /* Note that this is the maximum MSS, excluding all options. */
756 tcp->tcp_mss = mss;
759 * Update the tcp connection with LSO capability.
761 tcp_update_lso(tcp, connp->conn_ixa);
764 * Initialize the ISS here now that we have the full connection ID.
765 * The RFC 1948 method of initial sequence number generation requires
766 * knowledge of the full connection ID before setting the ISS.
768 tcp_iss_init(tcp);
770 tcp->tcp_loopback = (uinfo.iulp_loopback | uinfo.iulp_local);
773 * Make sure that conn is not marked incipient
774 * for incoming connections. A blind
775 * removal of incipient flag is cheaper than
776 * check and removal.
778 mutex_enter(&connp->conn_lock);
779 connp->conn_state_flags &= ~CONN_INCIPIENT;
780 mutex_exit(&connp->conn_lock);
781 return (0);
785 * tcp_clean_death / tcp_close_detached must not be called more than once
786 * on a tcp. Thus every function that potentially calls tcp_clean_death
787 * must check for the tcp state before calling tcp_clean_death.
788 * Eg. tcp_input_data, tcp_eager_kill, tcp_clean_death_wrapper,
789 * tcp_timer_handler, all check for the tcp state.
791 /* ARGSUSED */
792 void
793 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2,
794 ip_recv_attr_t *dummy)
796 tcp_t *tcp = ((conn_t *)arg)->conn_tcp;
798 freemsg(mp);
799 if (tcp->tcp_state > TCPS_BOUND)
800 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT);
804 * We are dying for some reason. Try to do it gracefully. (May be called
805 * as writer.)
807 * Return -1 if the structure was not cleaned up (if the cleanup had to be
808 * done by a service procedure).
809 * TBD - Should the return value distinguish between the tcp_t being
810 * freed and it being reinitialized?
813 tcp_clean_death(tcp_t *tcp, int err)
815 mblk_t *mp;
816 queue_t *q;
817 conn_t *connp = tcp->tcp_connp;
818 tcp_stack_t *tcps = tcp->tcp_tcps;
820 if (tcp->tcp_fused)
821 tcp_unfuse(tcp);
823 if (tcp->tcp_linger_tid != 0 &&
824 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
825 tcp_stop_lingering(tcp);
828 ASSERT(tcp != NULL);
829 ASSERT((connp->conn_family == AF_INET &&
830 connp->conn_ipversion == IPV4_VERSION) ||
831 (connp->conn_family == AF_INET6 &&
832 (connp->conn_ipversion == IPV4_VERSION ||
833 connp->conn_ipversion == IPV6_VERSION)));
835 if (TCP_IS_DETACHED(tcp)) {
836 if (tcp->tcp_hard_binding) {
838 * Its an eager that we are dealing with. We close the
839 * eager but in case a conn_ind has already gone to the
840 * listener, let tcp_accept_finish() send a discon_ind
841 * to the listener and drop the last reference. If the
842 * listener doesn't even know about the eager i.e. the
843 * conn_ind hasn't gone up, blow away the eager and drop
844 * the last reference as well. If the conn_ind has gone
845 * up, state should be BOUND. tcp_accept_finish
846 * will figure out that the connection has received a
847 * RST and will send a DISCON_IND to the application.
849 tcp_closei_local(tcp);
850 if (!tcp->tcp_tconnind_started) {
851 CONN_DEC_REF(connp);
852 } else {
853 tcp->tcp_state = TCPS_BOUND;
854 DTRACE_TCP6(state__change, void, NULL,
855 ip_xmit_attr_t *, connp->conn_ixa,
856 void, NULL, tcp_t *, tcp, void, NULL,
857 int32_t, TCPS_CLOSED);
859 } else {
860 tcp_close_detached(tcp);
862 return (0);
865 TCP_STAT(tcps, tcp_clean_death_nondetached);
868 * The connection is dead. Decrement listener connection counter if
869 * necessary.
871 if (tcp->tcp_listen_cnt != NULL)
872 TCP_DECR_LISTEN_CNT(tcp);
875 * When a connection is moved to TIME_WAIT state, the connection
876 * counter is already decremented. So no need to decrement here
877 * again. See SET_TIME_WAIT() macro.
879 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
880 tcp->tcp_state < TCPS_TIME_WAIT) {
881 TCPS_CONN_DEC(tcps);
884 q = connp->conn_rq;
886 /* Trash all inbound data */
887 if (!IPCL_IS_NONSTR(connp)) {
888 ASSERT(q != NULL);
889 flushq(q, FLUSHALL);
893 * If we are at least part way open and there is error
894 * (err==0 implies no error)
895 * notify our client by a T_DISCON_IND.
897 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) {
898 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
899 !TCP_IS_SOCKET(tcp)) {
901 * Send M_FLUSH according to TPI. Because sockets will
902 * (and must) ignore FLUSHR we do that only for TPI
903 * endpoints and sockets in STREAMS mode.
905 (void) putnextctl1(q, M_FLUSH, FLUSHR);
907 if (connp->conn_debug) {
908 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
909 "tcp_clean_death: discon err %d", err);
911 if (IPCL_IS_NONSTR(connp)) {
912 /* Direct socket, use upcall */
913 (*connp->conn_upcalls->su_disconnected)(
914 connp->conn_upper_handle, tcp->tcp_connid, err);
915 } else {
916 mp = mi_tpi_discon_ind(NULL, err, 0);
917 if (mp != NULL) {
918 putnext(q, mp);
919 } else {
920 if (connp->conn_debug) {
921 (void) strlog(TCP_MOD_ID, 0, 1,
922 SL_ERROR|SL_TRACE,
923 "tcp_clean_death, sending M_ERROR");
925 (void) putnextctl1(q, M_ERROR, EPROTO);
928 if (tcp->tcp_state <= TCPS_SYN_RCVD) {
929 /* SYN_SENT or SYN_RCVD */
930 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
931 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) {
932 /* ESTABLISHED or CLOSE_WAIT */
933 TCPS_BUMP_MIB(tcps, tcpEstabResets);
938 * ESTABLISHED non-STREAMS eagers are not 'detached' because
939 * an upper handle is obtained when the SYN-ACK comes in. So it
940 * should receive the 'disconnected' upcall, but tcp_reinit should
941 * not be called since this is an eager.
943 if (tcp->tcp_listener != NULL && IPCL_IS_NONSTR(connp)) {
944 tcp_closei_local(tcp);
945 tcp->tcp_state = TCPS_BOUND;
946 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
947 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
948 int32_t, TCPS_CLOSED);
949 return (0);
952 tcp_reinit(tcp);
953 if (IPCL_IS_NONSTR(connp))
954 (void) tcp_do_unbind(connp);
956 return (-1);
960 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout
961 * to expire, stop the wait and finish the close.
963 void
964 tcp_stop_lingering(tcp_t *tcp)
966 clock_t delta = 0;
967 tcp_stack_t *tcps = tcp->tcp_tcps;
968 conn_t *connp = tcp->tcp_connp;
970 tcp->tcp_linger_tid = 0;
971 if (tcp->tcp_state > TCPS_LISTEN) {
972 tcp_acceptor_hash_remove(tcp);
973 mutex_enter(&tcp->tcp_non_sq_lock);
974 if (tcp->tcp_flow_stopped) {
975 tcp_clrqfull(tcp);
977 mutex_exit(&tcp->tcp_non_sq_lock);
979 if (tcp->tcp_timer_tid != 0) {
980 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
981 tcp->tcp_timer_tid = 0;
984 * Need to cancel those timers which will not be used when
985 * TCP is detached. This has to be done before the conn_wq
986 * is cleared.
988 tcp_timers_stop(tcp);
990 tcp->tcp_detached = B_TRUE;
991 connp->conn_rq = NULL;
992 connp->conn_wq = NULL;
994 if (tcp->tcp_state == TCPS_TIME_WAIT) {
995 tcp_time_wait_append(tcp);
996 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
997 goto finish;
1001 * If delta is zero the timer event wasn't executed and was
1002 * successfully canceled. In this case we need to restart it
1003 * with the minimal delta possible.
1005 if (delta >= 0) {
1006 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1007 delta ? delta : 1);
1009 } else {
1010 tcp_closei_local(tcp);
1011 CONN_DEC_REF(connp);
1013 finish:
1014 tcp->tcp_detached = B_TRUE;
1015 connp->conn_rq = NULL;
1016 connp->conn_wq = NULL;
1018 /* Signal closing thread that it can complete close */
1019 mutex_enter(&tcp->tcp_closelock);
1020 tcp->tcp_closed = 1;
1021 cv_signal(&tcp->tcp_closecv);
1022 mutex_exit(&tcp->tcp_closelock);
1024 /* If we have an upper handle (socket), release it */
1025 if (IPCL_IS_NONSTR(connp)) {
1026 ASSERT(connp->conn_upper_handle != NULL);
1027 (*connp->conn_upcalls->su_closed)(connp->conn_upper_handle);
1028 connp->conn_upper_handle = NULL;
1029 connp->conn_upcalls = NULL;
1033 void
1034 tcp_close_common(conn_t *connp, int flags)
1036 tcp_t *tcp = connp->conn_tcp;
1037 mblk_t *mp = &tcp->tcp_closemp;
1038 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
1039 mblk_t *bp;
1041 ASSERT(connp->conn_ref >= 2);
1044 * Mark the conn as closing. ipsq_pending_mp_add will not
1045 * add any mp to the pending mp list, after this conn has
1046 * started closing.
1048 mutex_enter(&connp->conn_lock);
1049 connp->conn_state_flags |= CONN_CLOSING;
1050 if (connp->conn_oper_pending_ill != NULL)
1051 conn_ioctl_cleanup_reqd = B_TRUE;
1052 CONN_INC_REF_LOCKED(connp);
1053 mutex_exit(&connp->conn_lock);
1054 tcp->tcp_closeflags = (uint8_t)flags;
1055 ASSERT(connp->conn_ref >= 3);
1058 * tcp_closemp_used is used below without any protection of a lock
1059 * as we don't expect any one else to use it concurrently at this
1060 * point otherwise it would be a major defect.
1063 if (mp->b_prev == NULL)
1064 tcp->tcp_closemp_used = B_TRUE;
1065 else
1066 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: "
1067 "connp %p tcp %p\n", (void *)connp, (void *)tcp);
1069 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15);
1072 * Cleanup any queued ioctls here. This must be done before the wq/rq
1073 * are re-written by tcp_close_output().
1075 if (conn_ioctl_cleanup_reqd)
1076 conn_ioctl_cleanup(connp);
1079 * As CONN_CLOSING is set, no further ioctls should be passed down to
1080 * IP for this conn (see the guards in tcp_ioctl, tcp_wput_ioctl and
1081 * tcp_wput_iocdata). If the ioctl was queued on an ipsq,
1082 * conn_ioctl_cleanup should have found it and removed it. If the ioctl
1083 * was still in flight at the time, we wait for it here. See comments
1084 * for CONN_INC_IOCTLREF in ip.h for details.
1086 mutex_enter(&connp->conn_lock);
1087 while (connp->conn_ioctlref > 0)
1088 cv_wait(&connp->conn_cv, &connp->conn_lock);
1089 ASSERT(connp->conn_ioctlref == 0);
1090 ASSERT(connp->conn_oper_pending_ill == NULL);
1091 mutex_exit(&connp->conn_lock);
1093 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp,
1094 NULL, tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1097 * For non-STREAMS sockets, the normal case is that the conn makes
1098 * an upcall when it's finally closed, so there is no need to wait
1099 * in the protocol. But in case of SO_LINGER the thread sleeps here
1100 * so it can properly deal with the thread being interrupted.
1102 if (IPCL_IS_NONSTR(connp) && connp->conn_linger == 0)
1103 goto nowait;
1105 mutex_enter(&tcp->tcp_closelock);
1106 while (!tcp->tcp_closed) {
1107 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) {
1109 * The cv_wait_sig() was interrupted. We now do the
1110 * following:
1112 * 1) If the endpoint was lingering, we allow this
1113 * to be interrupted by cancelling the linger timeout
1114 * and closing normally.
1116 * 2) Revert to calling cv_wait()
1118 * We revert to using cv_wait() to avoid an
1119 * infinite loop which can occur if the calling
1120 * thread is higher priority than the squeue worker
1121 * thread and is bound to the same cpu.
1123 if (connp->conn_linger && connp->conn_lingertime > 0) {
1124 mutex_exit(&tcp->tcp_closelock);
1125 /* Entering squeue, bump ref count. */
1126 CONN_INC_REF(connp);
1127 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL);
1128 SQUEUE_ENTER_ONE(connp->conn_sqp, bp,
1129 tcp_linger_interrupted, connp, NULL,
1130 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1131 mutex_enter(&tcp->tcp_closelock);
1133 break;
1136 while (!tcp->tcp_closed)
1137 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock);
1138 mutex_exit(&tcp->tcp_closelock);
1141 * In the case of listener streams that have eagers in the q or q0
1142 * we wait for the eagers to drop their reference to us. conn_rq and
1143 * conn_wq of the eagers point to our queues. By waiting for the
1144 * refcnt to drop to 1, we are sure that the eagers have cleaned
1145 * up their queue pointers and also dropped their references to us.
1147 * For non-STREAMS sockets we do not have to wait here; the
1148 * listener will instead make a su_closed upcall when the last
1149 * reference is dropped.
1151 if (tcp->tcp_wait_for_eagers && !IPCL_IS_NONSTR(connp)) {
1152 mutex_enter(&connp->conn_lock);
1153 while (connp->conn_ref != 1) {
1154 cv_wait(&connp->conn_cv, &connp->conn_lock);
1156 mutex_exit(&connp->conn_lock);
1159 nowait:
1160 connp->conn_cpid = NOPID;
1164 * Called by tcp_close() routine via squeue when lingering is
1165 * interrupted by a signal.
1168 /* ARGSUSED */
1169 static void
1170 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1172 conn_t *connp = (conn_t *)arg;
1173 tcp_t *tcp = connp->conn_tcp;
1175 freeb(mp);
1176 if (tcp->tcp_linger_tid != 0 &&
1177 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
1178 tcp_stop_lingering(tcp);
1179 tcp->tcp_client_errno = EINTR;
1184 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp.
1185 * Some stream heads get upset if they see these later on as anything but NULL.
1187 void
1188 tcp_close_mpp(mblk_t **mpp)
1190 mblk_t *mp;
1192 if ((mp = *mpp) != NULL) {
1193 do {
1194 mp->b_next = NULL;
1195 mp->b_prev = NULL;
1196 } while ((mp = mp->b_cont) != NULL);
1198 mp = *mpp;
1199 *mpp = NULL;
1200 freemsg(mp);
1204 /* Do detached close. */
1205 void
1206 tcp_close_detached(tcp_t *tcp)
1208 if (tcp->tcp_fused)
1209 tcp_unfuse(tcp);
1211 tcp_closei_local(tcp);
1212 CONN_DEC_REF(tcp->tcp_connp);
1216 * The tcp_t is going away. Remove it from all lists and set it
1217 * to TCPS_CLOSED. The freeing up of memory is deferred until
1218 * tcp_inactive. This is needed since a thread in tcp_rput might have
1219 * done a CONN_INC_REF on this structure before it was removed from the
1220 * hashes.
1222 void
1223 tcp_closei_local(tcp_t *tcp)
1225 conn_t *connp = tcp->tcp_connp;
1226 tcp_stack_t *tcps = tcp->tcp_tcps;
1227 int32_t oldstate;
1229 if (!TCP_IS_SOCKET(tcp))
1230 tcp_acceptor_hash_remove(tcp);
1232 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1233 tcp->tcp_ibsegs = 0;
1234 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1235 tcp->tcp_obsegs = 0;
1238 * This can be called via tcp_time_wait_processing() if TCP gets a
1239 * SYN with sequence number outside the TIME-WAIT connection's
1240 * window. So we need to check for TIME-WAIT state here as the
1241 * connection counter is already decremented. See SET_TIME_WAIT()
1242 * macro
1244 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
1245 tcp->tcp_state < TCPS_TIME_WAIT) {
1246 TCPS_CONN_DEC(tcps);
1250 * If we are an eager connection hanging off a listener that
1251 * hasn't formally accepted the connection yet, get off its
1252 * list and blow off any data that we have accumulated.
1254 if (tcp->tcp_listener != NULL) {
1255 tcp_t *listener = tcp->tcp_listener;
1256 mutex_enter(&listener->tcp_eager_lock);
1258 * tcp_tconnind_started == B_TRUE means that the
1259 * conn_ind has already gone to listener. At
1260 * this point, eager will be closed but we
1261 * leave it in listeners eager list so that
1262 * if listener decides to close without doing
1263 * accept, we can clean this up. In tcp_tli_accept
1264 * we take care of the case of accept on closed
1265 * eager.
1267 if (!tcp->tcp_tconnind_started) {
1268 tcp_eager_unlink(tcp);
1269 mutex_exit(&listener->tcp_eager_lock);
1271 * We don't want to have any pointers to the
1272 * listener queue, after we have released our
1273 * reference on the listener
1275 ASSERT(tcp->tcp_detached);
1276 connp->conn_rq = NULL;
1277 connp->conn_wq = NULL;
1278 CONN_DEC_REF(listener->tcp_connp);
1279 } else {
1280 mutex_exit(&listener->tcp_eager_lock);
1284 /* Stop all the timers */
1285 tcp_timers_stop(tcp);
1287 if (tcp->tcp_state == TCPS_LISTEN) {
1288 if (tcp->tcp_ip_addr_cache) {
1289 kmem_free((void *)tcp->tcp_ip_addr_cache,
1290 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t));
1291 tcp->tcp_ip_addr_cache = NULL;
1295 /* Decrement listerner connection counter if necessary. */
1296 if (tcp->tcp_listen_cnt != NULL)
1297 TCP_DECR_LISTEN_CNT(tcp);
1299 mutex_enter(&tcp->tcp_non_sq_lock);
1300 if (tcp->tcp_flow_stopped)
1301 tcp_clrqfull(tcp);
1302 mutex_exit(&tcp->tcp_non_sq_lock);
1304 tcp_bind_hash_remove(tcp);
1306 * If the tcp_time_wait_collector (which runs outside the squeue)
1307 * is trying to remove this tcp from the time wait list, we will
1308 * block in tcp_time_wait_remove while trying to acquire the
1309 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also
1310 * requires the ipcl_hash_remove to be ordered after the
1311 * tcp_time_wait_remove for the refcnt checks to work correctly.
1313 if (tcp->tcp_state == TCPS_TIME_WAIT)
1314 (void) tcp_time_wait_remove(tcp, NULL);
1315 ipcl_hash_remove(connp);
1316 oldstate = tcp->tcp_state;
1317 tcp->tcp_state = TCPS_CLOSED;
1318 /* Need to probe before ixa_cleanup() is called */
1319 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1320 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
1321 int32_t, oldstate);
1322 ixa_cleanup(connp->conn_ixa);
1325 * Mark the conn as CONDEMNED
1327 mutex_enter(&connp->conn_lock);
1328 connp->conn_state_flags |= CONN_CONDEMNED;
1329 mutex_exit(&connp->conn_lock);
1331 ASSERT(tcp->tcp_time_wait_next == NULL);
1332 ASSERT(tcp->tcp_time_wait_prev == NULL);
1333 ASSERT(tcp->tcp_time_wait_expire == 0);
1335 tcp_ipsec_cleanup(tcp);
1339 * tcp is dying (called from ipcl_conn_destroy and error cases).
1340 * Free the tcp_t in either case.
1342 void
1343 tcp_free(tcp_t *tcp)
1345 mblk_t *mp;
1346 conn_t *connp = tcp->tcp_connp;
1348 ASSERT(tcp != NULL);
1349 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL);
1351 connp->conn_rq = NULL;
1352 connp->conn_wq = NULL;
1354 tcp_close_mpp(&tcp->tcp_xmit_head);
1355 tcp_close_mpp(&tcp->tcp_reass_head);
1356 if (tcp->tcp_rcv_list != NULL) {
1357 /* Free b_next chain */
1358 tcp_close_mpp(&tcp->tcp_rcv_list);
1360 if ((mp = tcp->tcp_urp_mp) != NULL) {
1361 freemsg(mp);
1363 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1364 freemsg(mp);
1367 if (tcp->tcp_fused_sigurg_mp != NULL) {
1368 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1369 freeb(tcp->tcp_fused_sigurg_mp);
1370 tcp->tcp_fused_sigurg_mp = NULL;
1373 if (tcp->tcp_ordrel_mp != NULL) {
1374 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1375 freeb(tcp->tcp_ordrel_mp);
1376 tcp->tcp_ordrel_mp = NULL;
1379 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
1380 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
1382 if (tcp->tcp_hopopts != NULL) {
1383 mi_free(tcp->tcp_hopopts);
1384 tcp->tcp_hopopts = NULL;
1385 tcp->tcp_hopoptslen = 0;
1387 ASSERT(tcp->tcp_hopoptslen == 0);
1388 if (tcp->tcp_dstopts != NULL) {
1389 mi_free(tcp->tcp_dstopts);
1390 tcp->tcp_dstopts = NULL;
1391 tcp->tcp_dstoptslen = 0;
1393 ASSERT(tcp->tcp_dstoptslen == 0);
1394 if (tcp->tcp_rthdrdstopts != NULL) {
1395 mi_free(tcp->tcp_rthdrdstopts);
1396 tcp->tcp_rthdrdstopts = NULL;
1397 tcp->tcp_rthdrdstoptslen = 0;
1399 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
1400 if (tcp->tcp_rthdr != NULL) {
1401 mi_free(tcp->tcp_rthdr);
1402 tcp->tcp_rthdr = NULL;
1403 tcp->tcp_rthdrlen = 0;
1405 ASSERT(tcp->tcp_rthdrlen == 0);
1408 * Following is really a blowing away a union.
1409 * It happens to have exactly two members of identical size
1410 * the following code is enough.
1412 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1415 * If this is a non-STREAM socket still holding on to an upper
1416 * handle, release it. As a result of fallback we might also see
1417 * STREAMS based conns with upper handles, in which case there is
1418 * nothing to do other than clearing the field.
1420 if (connp->conn_upper_handle != NULL) {
1421 if (IPCL_IS_NONSTR(connp)) {
1422 (*connp->conn_upcalls->su_closed)(
1423 connp->conn_upper_handle);
1424 tcp->tcp_detached = B_TRUE;
1426 connp->conn_upper_handle = NULL;
1427 connp->conn_upcalls = NULL;
1432 * tcp_get_conn/tcp_free_conn
1434 * tcp_get_conn is used to get a clean tcp connection structure.
1435 * It tries to reuse the connections put on the freelist by the
1436 * time_wait_collector failing which it goes to kmem_cache. This
1437 * way has two benefits compared to just allocating from and
1438 * freeing to kmem_cache.
1439 * 1) The time_wait_collector can free (which includes the cleanup)
1440 * outside the squeue. So when the interrupt comes, we have a clean
1441 * connection sitting in the freelist. Obviously, this buys us
1442 * performance.
1444 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_input_listener
1445 * has multiple disadvantages - tying up the squeue during alloc.
1446 * But allocating the conn/tcp in IP land is also not the best since
1447 * we can't check the 'q' and 'q0' which are protected by squeue and
1448 * blindly allocate memory which might have to be freed here if we are
1449 * not allowed to accept the connection. By using the freelist and
1450 * putting the conn/tcp back in freelist, we don't pay a penalty for
1451 * allocating memory without checking 'q/q0' and freeing it if we can't
1452 * accept the connection.
1454 * Care should be taken to put the conn back in the same squeue's freelist
1455 * from which it was allocated. Best results are obtained if conn is
1456 * allocated from listener's squeue and freed to the same. Time wait
1457 * collector will free up the freelist is the connection ends up sitting
1458 * there for too long.
1460 void *
1461 tcp_get_conn(void *arg, tcp_stack_t *tcps)
1463 tcp_t *tcp = NULL;
1464 conn_t *connp = NULL;
1465 squeue_t *sqp = (squeue_t *)arg;
1466 tcp_squeue_priv_t *tcp_time_wait;
1467 netstack_t *ns;
1468 mblk_t *tcp_rsrv_mp = NULL;
1470 tcp_time_wait =
1471 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP));
1473 mutex_enter(&tcp_time_wait->tcp_time_wait_lock);
1474 tcp = tcp_time_wait->tcp_free_list;
1475 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0));
1476 if (tcp != NULL) {
1477 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next;
1478 tcp_time_wait->tcp_free_list_cnt--;
1479 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1480 tcp->tcp_time_wait_next = NULL;
1481 connp = tcp->tcp_connp;
1482 connp->conn_flags |= IPCL_REUSED;
1484 ASSERT(tcp->tcp_tcps == NULL);
1485 ASSERT(connp->conn_netstack == NULL);
1486 ASSERT(tcp->tcp_rsrv_mp != NULL);
1487 ns = tcps->tcps_netstack;
1488 netstack_hold(ns);
1489 connp->conn_netstack = ns;
1490 connp->conn_ixa->ixa_ipst = ns->netstack_ip;
1491 tcp->tcp_tcps = tcps;
1492 ipcl_globalhash_insert(connp);
1494 connp->conn_ixa->ixa_notify_cookie = tcp;
1495 ASSERT(connp->conn_ixa->ixa_notify == tcp_notify);
1496 connp->conn_recv = tcp_input_data;
1497 ASSERT(connp->conn_recvicmp == tcp_icmp_input);
1498 ASSERT(connp->conn_verifyicmp == tcp_verifyicmp);
1499 return ((void *)connp);
1501 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1503 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until
1504 * this conn_t/tcp_t is freed at ipcl_conn_destroy().
1506 tcp_rsrv_mp = allocb(0, BPRI_HI);
1507 if (tcp_rsrv_mp == NULL)
1508 return (NULL);
1510 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
1511 tcps->tcps_netstack)) == NULL) {
1512 freeb(tcp_rsrv_mp);
1513 return (NULL);
1516 tcp = connp->conn_tcp;
1517 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
1518 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL);
1520 tcp->tcp_tcps = tcps;
1522 connp->conn_recv = tcp_input_data;
1523 connp->conn_recvicmp = tcp_icmp_input;
1524 connp->conn_verifyicmp = tcp_verifyicmp;
1527 * Register tcp_notify to listen to capability changes detected by IP.
1528 * This upcall is made in the context of the call to conn_ip_output
1529 * thus it is inside the squeue.
1531 connp->conn_ixa->ixa_notify = tcp_notify;
1532 connp->conn_ixa->ixa_notify_cookie = tcp;
1534 return ((void *)connp);
1538 * Handle connect to IPv4 destinations, including connections for AF_INET6
1539 * sockets connecting to IPv4 mapped IPv6 destinations.
1540 * Returns zero if OK, a positive errno, or a negative TLI error.
1542 static int
1543 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport,
1544 uint_t srcid)
1546 ipaddr_t dstaddr = *dstaddrp;
1547 uint16_t lport;
1548 conn_t *connp = tcp->tcp_connp;
1549 tcp_stack_t *tcps = tcp->tcp_tcps;
1550 int error;
1552 ASSERT(connp->conn_ipversion == IPV4_VERSION);
1554 /* Check for attempt to connect to INADDR_ANY */
1555 if (dstaddr == INADDR_ANY) {
1557 * SunOS 4.x and 4.3 BSD allow an application
1558 * to connect a TCP socket to INADDR_ANY.
1559 * When they do this, the kernel picks the
1560 * address of one interface and uses it
1561 * instead. The kernel usually ends up
1562 * picking the address of the loopback
1563 * interface. This is an undocumented feature.
1564 * However, we provide the same thing here
1565 * in order to have source and binary
1566 * compatibility with SunOS 4.x.
1567 * Update the T_CONN_REQ (sin/sin6) since it is used to
1568 * generate the T_CONN_CON.
1570 dstaddr = htonl(INADDR_LOOPBACK);
1571 *dstaddrp = dstaddr;
1574 /* Handle __sin6_src_id if socket not bound to an IP address */
1575 if (srcid != 0 && connp->conn_laddr_v4 == INADDR_ANY) {
1576 if (!ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1577 IPCL_ZONEID(connp), B_TRUE, tcps->tcps_netstack)) {
1578 /* Mismatch - conn_laddr_v6 would be v6 address. */
1579 return (EADDRNOTAVAIL);
1581 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1584 IN6_IPADDR_TO_V4MAPPED(dstaddr, &connp->conn_faddr_v6);
1585 connp->conn_fport = dstport;
1588 * At this point the remote destination address and remote port fields
1589 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1590 * have to see which state tcp was in so we can take appropriate action.
1592 if (tcp->tcp_state == TCPS_IDLE) {
1594 * We support a quick connect capability here, allowing
1595 * clients to transition directly from IDLE to SYN_SENT
1596 * tcp_bindi will pick an unused port, insert the connection
1597 * in the bind hash and transition to BOUND state.
1599 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1600 tcp, B_TRUE);
1601 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1602 B_FALSE, B_FALSE);
1603 if (lport == 0)
1604 return (-TNOADDR);
1608 * Lookup the route to determine a source address and the uinfo.
1609 * Setup TCP parameters based on the metrics/DCE.
1611 error = tcp_set_destination(tcp);
1612 if (error != 0)
1613 return (error);
1616 * Don't let an endpoint connect to itself.
1618 if (connp->conn_faddr_v4 == connp->conn_laddr_v4 &&
1619 connp->conn_fport == connp->conn_lport)
1620 return (-TBADADDR);
1622 tcp->tcp_state = TCPS_SYN_SENT;
1624 return (ipcl_conn_insert_v4(connp));
1628 * Handle connect to IPv6 destinations.
1629 * Returns zero if OK, a positive errno, or a negative TLI error.
1631 static int
1632 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport,
1633 uint32_t flowinfo, uint_t srcid, uint32_t scope_id)
1635 uint16_t lport;
1636 conn_t *connp = tcp->tcp_connp;
1637 tcp_stack_t *tcps = tcp->tcp_tcps;
1638 int error;
1640 ASSERT(connp->conn_family == AF_INET6);
1643 * If we're here, it means that the destination address is a native
1644 * IPv6 address. Return an error if conn_ipversion is not IPv6. A
1645 * reason why it might not be IPv6 is if the socket was bound to an
1646 * IPv4-mapped IPv6 address.
1648 if (connp->conn_ipversion != IPV6_VERSION)
1649 return (-TBADADDR);
1652 * Interpret a zero destination to mean loopback.
1653 * Update the T_CONN_REQ (sin/sin6) since it is used to
1654 * generate the T_CONN_CON.
1656 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp))
1657 *dstaddrp = ipv6_loopback;
1659 /* Handle __sin6_src_id if socket not bound to an IP address */
1660 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
1661 if (!ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1662 IPCL_ZONEID(connp), B_FALSE, tcps->tcps_netstack)) {
1663 /* Mismatch - conn_laddr_v6 would be v4-mapped. */
1664 return (EADDRNOTAVAIL);
1666 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1670 * Take care of the scope_id now.
1672 if (scope_id != 0 && IN6_IS_ADDR_LINKSCOPE(dstaddrp)) {
1673 connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1674 connp->conn_ixa->ixa_scopeid = scope_id;
1675 } else {
1676 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
1679 connp->conn_flowinfo = flowinfo;
1680 connp->conn_faddr_v6 = *dstaddrp;
1681 connp->conn_fport = dstport;
1684 * At this point the remote destination address and remote port fields
1685 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1686 * have to see which state tcp was in so we can take appropriate action.
1688 if (tcp->tcp_state == TCPS_IDLE) {
1690 * We support a quick connect capability here, allowing
1691 * clients to transition directly from IDLE to SYN_SENT
1692 * tcp_bindi will pick an unused port, insert the connection
1693 * in the bind hash and transition to BOUND state.
1695 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1696 tcp, B_TRUE);
1697 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1698 B_FALSE, B_FALSE);
1699 if (lport == 0)
1700 return (-TNOADDR);
1704 * Lookup the route to determine a source address and the uinfo.
1705 * Setup TCP parameters based on the metrics/DCE.
1707 error = tcp_set_destination(tcp);
1708 if (error != 0)
1709 return (error);
1712 * Don't let an endpoint connect to itself.
1714 if (IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6, &connp->conn_laddr_v6) &&
1715 connp->conn_fport == connp->conn_lport)
1716 return (-TBADADDR);
1718 tcp->tcp_state = TCPS_SYN_SENT;
1720 return (ipcl_conn_insert_v6(connp));
1724 * Disconnect
1725 * Note that unlike other functions this returns a positive tli error
1726 * when it fails; it never returns an errno.
1728 static int
1729 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum)
1731 conn_t *lconnp;
1732 tcp_stack_t *tcps = tcp->tcp_tcps;
1733 conn_t *connp = tcp->tcp_connp;
1736 * Right now, upper modules pass down a T_DISCON_REQ to TCP,
1737 * when the stream is in BOUND state. Do not send a reset,
1738 * since the destination IP address is not valid, and it can
1739 * be the initialized value of all zeros (broadcast address).
1741 if (tcp->tcp_state <= TCPS_BOUND) {
1742 if (connp->conn_debug) {
1743 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1744 "tcp_disconnect: bad state, %d", tcp->tcp_state);
1746 return (TOUTSTATE);
1747 } else if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1748 TCPS_CONN_DEC(tcps);
1751 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) {
1754 * According to TPI, for non-listeners, ignore seqnum
1755 * and disconnect.
1756 * Following interpretation of -1 seqnum is historical
1757 * and implied TPI ? (TPI only states that for T_CONN_IND,
1758 * a valid seqnum should not be -1).
1760 * -1 means disconnect everything
1761 * regardless even on a listener.
1764 int old_state = tcp->tcp_state;
1765 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
1768 * The connection can't be on the tcp_time_wait_head list
1769 * since it is not detached.
1771 ASSERT(tcp->tcp_time_wait_next == NULL);
1772 ASSERT(tcp->tcp_time_wait_prev == NULL);
1773 ASSERT(tcp->tcp_time_wait_expire == 0);
1775 * If it used to be a listener, check to make sure no one else
1776 * has taken the port before switching back to LISTEN state.
1778 if (connp->conn_ipversion == IPV4_VERSION) {
1779 lconnp = ipcl_lookup_listener_v4(connp->conn_lport,
1780 connp->conn_laddr_v4, IPCL_ZONEID(connp), ipst);
1781 } else {
1782 uint_t ifindex = 0;
1784 if (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET)
1785 ifindex = connp->conn_ixa->ixa_scopeid;
1787 /* Allow conn_bound_if listeners? */
1788 lconnp = ipcl_lookup_listener_v6(connp->conn_lport,
1789 &connp->conn_laddr_v6, ifindex, IPCL_ZONEID(connp),
1790 ipst);
1792 if (tcp->tcp_conn_req_max && lconnp == NULL) {
1793 tcp->tcp_state = TCPS_LISTEN;
1794 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1795 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1796 NULL, int32_t, old_state);
1797 } else if (old_state > TCPS_BOUND) {
1798 tcp->tcp_conn_req_max = 0;
1799 tcp->tcp_state = TCPS_BOUND;
1800 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1801 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1802 NULL, int32_t, old_state);
1805 * If this end point is not going to become a listener,
1806 * decrement the listener connection count if
1807 * necessary. Note that we do not do this if it is
1808 * going to be a listner (the above if case) since
1809 * then it may remove the counter struct.
1811 if (tcp->tcp_listen_cnt != NULL)
1812 TCP_DECR_LISTEN_CNT(tcp);
1814 if (lconnp != NULL)
1815 CONN_DEC_REF(lconnp);
1816 switch (old_state) {
1817 case TCPS_SYN_SENT:
1818 case TCPS_SYN_RCVD:
1819 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1820 break;
1821 case TCPS_ESTABLISHED:
1822 case TCPS_CLOSE_WAIT:
1823 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1824 break;
1827 if (tcp->tcp_fused)
1828 tcp_unfuse(tcp);
1830 mutex_enter(&tcp->tcp_eager_lock);
1831 if ((tcp->tcp_conn_req_cnt_q0 != 0) ||
1832 (tcp->tcp_conn_req_cnt_q != 0)) {
1833 tcp_eager_cleanup(tcp, 0);
1835 mutex_exit(&tcp->tcp_eager_lock);
1837 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt,
1838 tcp->tcp_rnxt, TH_RST | TH_ACK);
1840 tcp_reinit(tcp);
1842 return (0);
1843 } else if (!tcp_eager_blowoff(tcp, seqnum)) {
1844 return (TBADSEQ);
1846 return (0);
1850 * Our client hereby directs us to reject the connection request
1851 * that tcp_input_listener() marked with 'seqnum'. Rejection consists
1852 * of sending the appropriate RST, not an ICMP error.
1854 void
1855 tcp_disconnect(tcp_t *tcp, mblk_t *mp)
1857 t_scalar_t seqnum;
1858 int error;
1859 conn_t *connp = tcp->tcp_connp;
1861 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1862 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) {
1863 tcp_err_ack(tcp, mp, TPROTO, 0);
1864 return;
1866 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number;
1867 error = tcp_disconnect_common(tcp, seqnum);
1868 if (error != 0)
1869 tcp_err_ack(tcp, mp, error, 0);
1870 else {
1871 if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1872 /* Send M_FLUSH according to TPI */
1873 (void) putnextctl1(connp->conn_rq, M_FLUSH, FLUSHRW);
1875 mp = mi_tpi_ok_ack_alloc(mp);
1876 if (mp != NULL)
1877 putnext(connp->conn_rq, mp);
1882 * Handle reinitialization of a tcp structure.
1883 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE.
1885 static void
1886 tcp_reinit(tcp_t *tcp)
1888 mblk_t *mp;
1889 tcp_stack_t *tcps = tcp->tcp_tcps;
1890 conn_t *connp = tcp->tcp_connp;
1891 int32_t oldstate;
1893 /* tcp_reinit should never be called for detached tcp_t's */
1894 ASSERT(tcp->tcp_listener == NULL);
1895 ASSERT((connp->conn_family == AF_INET &&
1896 connp->conn_ipversion == IPV4_VERSION) ||
1897 (connp->conn_family == AF_INET6 &&
1898 (connp->conn_ipversion == IPV4_VERSION ||
1899 connp->conn_ipversion == IPV6_VERSION)));
1901 /* Cancel outstanding timers */
1902 tcp_timers_stop(tcp);
1905 * Reset everything in the state vector, after updating global
1906 * MIB data from instance counters.
1908 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1909 tcp->tcp_ibsegs = 0;
1910 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1911 tcp->tcp_obsegs = 0;
1913 tcp_close_mpp(&tcp->tcp_xmit_head);
1914 if (tcp->tcp_snd_zcopy_aware)
1915 tcp_zcopy_notify(tcp);
1916 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL;
1917 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0;
1918 mutex_enter(&tcp->tcp_non_sq_lock);
1919 if (tcp->tcp_flow_stopped &&
1920 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1921 tcp_clrqfull(tcp);
1923 mutex_exit(&tcp->tcp_non_sq_lock);
1924 tcp_close_mpp(&tcp->tcp_reass_head);
1925 tcp->tcp_reass_tail = NULL;
1926 if (tcp->tcp_rcv_list != NULL) {
1927 /* Free b_next chain */
1928 tcp_close_mpp(&tcp->tcp_rcv_list);
1929 tcp->tcp_rcv_last_head = NULL;
1930 tcp->tcp_rcv_last_tail = NULL;
1931 tcp->tcp_rcv_cnt = 0;
1933 tcp->tcp_rcv_last_tail = NULL;
1935 if ((mp = tcp->tcp_urp_mp) != NULL) {
1936 freemsg(mp);
1937 tcp->tcp_urp_mp = NULL;
1939 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1940 freemsg(mp);
1941 tcp->tcp_urp_mark_mp = NULL;
1943 if (tcp->tcp_fused_sigurg_mp != NULL) {
1944 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1945 freeb(tcp->tcp_fused_sigurg_mp);
1946 tcp->tcp_fused_sigurg_mp = NULL;
1948 if (tcp->tcp_ordrel_mp != NULL) {
1949 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1950 freeb(tcp->tcp_ordrel_mp);
1951 tcp->tcp_ordrel_mp = NULL;
1955 * Following is a union with two members which are
1956 * identical types and size so the following cleanup
1957 * is enough.
1959 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1962 * The connection can't be on the tcp_time_wait_head list
1963 * since it is not detached.
1965 ASSERT(tcp->tcp_time_wait_next == NULL);
1966 ASSERT(tcp->tcp_time_wait_prev == NULL);
1967 ASSERT(tcp->tcp_time_wait_expire == 0);
1970 * Reset/preserve other values
1972 tcp_reinit_values(tcp);
1973 ipcl_hash_remove(connp);
1974 /* Note that ixa_cred gets cleared in ixa_cleanup */
1975 ixa_cleanup(connp->conn_ixa);
1976 tcp_ipsec_cleanup(tcp);
1978 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
1979 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
1980 oldstate = tcp->tcp_state;
1982 if (tcp->tcp_conn_req_max != 0) {
1984 * This is the case when a TLI program uses the same
1985 * transport end point to accept a connection. This
1986 * makes the TCP both a listener and acceptor. When
1987 * this connection is closed, we need to set the state
1988 * back to TCPS_LISTEN. Make sure that the eager list
1989 * is reinitialized.
1991 * Note that this stream is still bound to the four
1992 * tuples of the previous connection in IP. If a new
1993 * SYN with different foreign address comes in, IP will
1994 * not find it and will send it to the global queue. In
1995 * the global queue, TCP will do a tcp_lookup_listener()
1996 * to find this stream. This works because this stream
1997 * is only removed from connected hash.
2000 tcp->tcp_state = TCPS_LISTEN;
2001 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
2002 tcp->tcp_eager_next_drop_q0 = tcp;
2003 tcp->tcp_eager_prev_drop_q0 = tcp;
2005 * Initially set conn_recv to tcp_input_listener_unbound to try
2006 * to pick a good squeue for the listener when the first SYN
2007 * arrives. tcp_input_listener_unbound sets it to
2008 * tcp_input_listener on that first SYN.
2010 connp->conn_recv = tcp_input_listener_unbound;
2012 connp->conn_proto = IPPROTO_TCP;
2013 connp->conn_faddr_v6 = ipv6_all_zeros;
2014 connp->conn_fport = 0;
2016 (void) ipcl_bind_insert(connp);
2017 } else {
2018 tcp->tcp_state = TCPS_BOUND;
2022 * Initialize to default values
2024 tcp_init_values(tcp, NULL);
2026 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2027 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
2028 int32_t, oldstate);
2030 ASSERT(tcp->tcp_ptpbhn != NULL);
2031 tcp->tcp_rwnd = connp->conn_rcvbuf;
2032 tcp->tcp_mss = connp->conn_ipversion != IPV4_VERSION ?
2033 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4;
2037 * Force values to zero that need be zero.
2038 * Do not touch values asociated with the BOUND or LISTEN state
2039 * since the connection will end up in that state after the reinit.
2040 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t
2041 * structure!
2043 static void
2044 tcp_reinit_values(tcp)
2045 tcp_t *tcp;
2047 tcp_stack_t *tcps = tcp->tcp_tcps;
2048 conn_t *connp = tcp->tcp_connp;
2050 #ifndef lint
2051 #define DONTCARE(x)
2052 #define PRESERVE(x)
2053 #else
2054 #define DONTCARE(x) ((x) = (x))
2055 #define PRESERVE(x) ((x) = (x))
2056 #endif /* lint */
2058 PRESERVE(tcp->tcp_bind_hash_port);
2059 PRESERVE(tcp->tcp_bind_hash);
2060 PRESERVE(tcp->tcp_ptpbhn);
2061 PRESERVE(tcp->tcp_acceptor_hash);
2062 PRESERVE(tcp->tcp_ptpahn);
2064 /* Should be ASSERT NULL on these with new code! */
2065 ASSERT(tcp->tcp_time_wait_next == NULL);
2066 ASSERT(tcp->tcp_time_wait_prev == NULL);
2067 ASSERT(tcp->tcp_time_wait_expire == 0);
2068 PRESERVE(tcp->tcp_state);
2069 PRESERVE(connp->conn_rq);
2070 PRESERVE(connp->conn_wq);
2072 ASSERT(tcp->tcp_xmit_head == NULL);
2073 ASSERT(tcp->tcp_xmit_last == NULL);
2074 ASSERT(tcp->tcp_unsent == 0);
2075 ASSERT(tcp->tcp_xmit_tail == NULL);
2076 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
2078 tcp->tcp_snxt = 0; /* Displayed in mib */
2079 tcp->tcp_suna = 0; /* Displayed in mib */
2080 tcp->tcp_swnd = 0;
2081 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_process_options */
2083 ASSERT(tcp->tcp_ibsegs == 0);
2084 ASSERT(tcp->tcp_obsegs == 0);
2086 if (connp->conn_ht_iphc != NULL) {
2087 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
2088 connp->conn_ht_iphc = NULL;
2089 connp->conn_ht_iphc_allocated = 0;
2090 connp->conn_ht_iphc_len = 0;
2091 connp->conn_ht_ulp = NULL;
2092 connp->conn_ht_ulp_len = 0;
2093 tcp->tcp_ipha = NULL;
2094 tcp->tcp_ip6h = NULL;
2095 tcp->tcp_tcpha = NULL;
2098 /* We clear any IP_OPTIONS and extension headers */
2099 ip_pkt_free(&connp->conn_xmit_ipp);
2101 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */
2102 DONTCARE(tcp->tcp_ipha);
2103 DONTCARE(tcp->tcp_ip6h);
2104 DONTCARE(tcp->tcp_tcpha);
2105 tcp->tcp_valid_bits = 0;
2107 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */
2108 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */
2109 tcp->tcp_last_rcv_lbolt = 0;
2111 tcp->tcp_init_cwnd = 0;
2113 tcp->tcp_urp_last_valid = 0;
2114 tcp->tcp_hard_binding = 0;
2116 tcp->tcp_fin_acked = 0;
2117 tcp->tcp_fin_rcvd = 0;
2118 tcp->tcp_fin_sent = 0;
2119 tcp->tcp_ordrel_done = 0;
2121 tcp->tcp_detached = 0;
2123 tcp->tcp_snd_ws_ok = B_FALSE;
2124 tcp->tcp_snd_ts_ok = B_FALSE;
2125 tcp->tcp_zero_win_probe = 0;
2127 tcp->tcp_loopback = 0;
2128 tcp->tcp_localnet = 0;
2129 tcp->tcp_syn_defense = 0;
2130 tcp->tcp_set_timer = 0;
2132 tcp->tcp_active_open = 0;
2133 tcp->tcp_rexmit = B_FALSE;
2134 tcp->tcp_xmit_zc_clean = B_FALSE;
2136 tcp->tcp_snd_sack_ok = B_FALSE;
2137 tcp->tcp_hwcksum = B_FALSE;
2139 DONTCARE(tcp->tcp_maxpsz_multiplier); /* Init in tcp_init_values */
2141 tcp->tcp_conn_def_q0 = 0;
2142 tcp->tcp_ip_forward_progress = B_FALSE;
2143 tcp->tcp_ecn_ok = B_FALSE;
2145 tcp->tcp_cwr = B_FALSE;
2146 tcp->tcp_ecn_echo_on = B_FALSE;
2147 tcp->tcp_is_wnd_shrnk = B_FALSE;
2149 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
2150 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
2152 tcp->tcp_rcv_ws = 0;
2153 tcp->tcp_snd_ws = 0;
2154 tcp->tcp_ts_recent = 0;
2155 tcp->tcp_rnxt = 0; /* Displayed in mib */
2156 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */
2157 tcp->tcp_initial_pmtu = 0;
2159 ASSERT(tcp->tcp_reass_head == NULL);
2160 ASSERT(tcp->tcp_reass_tail == NULL);
2162 tcp->tcp_cwnd_cnt = 0;
2164 ASSERT(tcp->tcp_rcv_list == NULL);
2165 ASSERT(tcp->tcp_rcv_last_head == NULL);
2166 ASSERT(tcp->tcp_rcv_last_tail == NULL);
2167 ASSERT(tcp->tcp_rcv_cnt == 0);
2169 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_set_destination */
2170 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */
2171 tcp->tcp_csuna = 0;
2173 tcp->tcp_rto = 0; /* Displayed in MIB */
2174 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */
2175 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */
2176 tcp->tcp_rtt_update = 0;
2178 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2179 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2181 tcp->tcp_rack = 0; /* Displayed in mib */
2182 tcp->tcp_rack_cnt = 0;
2183 tcp->tcp_rack_cur_max = 0;
2184 tcp->tcp_rack_abs_max = 0;
2186 tcp->tcp_max_swnd = 0;
2188 ASSERT(tcp->tcp_listener == NULL);
2190 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */
2191 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */
2192 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */
2193 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */
2195 ASSERT(tcp->tcp_conn_req_cnt_q == 0);
2196 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0);
2197 PRESERVE(tcp->tcp_conn_req_max);
2198 PRESERVE(tcp->tcp_conn_req_seqnum);
2200 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */
2201 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */
2202 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */
2203 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */
2205 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */
2206 ASSERT(tcp->tcp_urp_mp == NULL);
2207 ASSERT(tcp->tcp_urp_mark_mp == NULL);
2208 ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
2210 ASSERT(tcp->tcp_eager_next_q == NULL);
2211 ASSERT(tcp->tcp_eager_last_q == NULL);
2212 ASSERT((tcp->tcp_eager_next_q0 == NULL &&
2213 tcp->tcp_eager_prev_q0 == NULL) ||
2214 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0);
2215 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL);
2217 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL &&
2218 tcp->tcp_eager_prev_drop_q0 == NULL) ||
2219 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0);
2221 DONTCARE(tcp->tcp_ka_rinterval); /* Init in tcp_init_values */
2222 DONTCARE(tcp->tcp_ka_abort_thres); /* Init in tcp_init_values */
2223 DONTCARE(tcp->tcp_ka_cnt); /* Init in tcp_init_values */
2225 tcp->tcp_client_errno = 0;
2227 DONTCARE(connp->conn_sum); /* Init in tcp_init_values */
2229 connp->conn_faddr_v6 = ipv6_all_zeros; /* Displayed in MIB */
2231 PRESERVE(connp->conn_bound_addr_v6);
2232 tcp->tcp_last_sent_len = 0;
2233 tcp->tcp_dupack_cnt = 0;
2235 connp->conn_fport = 0; /* Displayed in MIB */
2236 PRESERVE(connp->conn_lport);
2238 PRESERVE(tcp->tcp_acceptor_lockp);
2240 ASSERT(tcp->tcp_ordrel_mp == NULL);
2241 PRESERVE(tcp->tcp_acceptor_id);
2242 DONTCARE(tcp->tcp_ipsec_overhead);
2244 PRESERVE(connp->conn_family);
2245 /* Remove any remnants of mapped address binding */
2246 if (connp->conn_family == AF_INET6) {
2247 connp->conn_ipversion = IPV6_VERSION;
2248 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2249 } else {
2250 connp->conn_ipversion = IPV4_VERSION;
2251 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2254 connp->conn_bound_if = 0;
2255 connp->conn_recv_ancillary.crb_all = 0;
2256 tcp->tcp_recvifindex = 0;
2257 tcp->tcp_recvhops = 0;
2258 tcp->tcp_closed = 0;
2259 if (tcp->tcp_hopopts != NULL) {
2260 mi_free(tcp->tcp_hopopts);
2261 tcp->tcp_hopopts = NULL;
2262 tcp->tcp_hopoptslen = 0;
2264 ASSERT(tcp->tcp_hopoptslen == 0);
2265 if (tcp->tcp_dstopts != NULL) {
2266 mi_free(tcp->tcp_dstopts);
2267 tcp->tcp_dstopts = NULL;
2268 tcp->tcp_dstoptslen = 0;
2270 ASSERT(tcp->tcp_dstoptslen == 0);
2271 if (tcp->tcp_rthdrdstopts != NULL) {
2272 mi_free(tcp->tcp_rthdrdstopts);
2273 tcp->tcp_rthdrdstopts = NULL;
2274 tcp->tcp_rthdrdstoptslen = 0;
2276 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
2277 if (tcp->tcp_rthdr != NULL) {
2278 mi_free(tcp->tcp_rthdr);
2279 tcp->tcp_rthdr = NULL;
2280 tcp->tcp_rthdrlen = 0;
2282 ASSERT(tcp->tcp_rthdrlen == 0);
2284 /* Reset fusion-related fields */
2285 tcp->tcp_fused = B_FALSE;
2286 tcp->tcp_unfusable = B_FALSE;
2287 tcp->tcp_fused_sigurg = B_FALSE;
2288 tcp->tcp_loopback_peer = NULL;
2290 tcp->tcp_lso = B_FALSE;
2292 tcp->tcp_in_ack_unsent = 0;
2293 tcp->tcp_cork = B_FALSE;
2294 tcp->tcp_tconnind_started = B_FALSE;
2296 PRESERVE(tcp->tcp_squeue_bytes);
2298 tcp->tcp_closemp_used = B_FALSE;
2300 PRESERVE(tcp->tcp_rsrv_mp);
2301 PRESERVE(tcp->tcp_rsrv_mp_lock);
2303 #ifdef DEBUG
2304 DONTCARE(tcp->tcmp_stk[0]);
2305 #endif
2307 PRESERVE(tcp->tcp_connid);
2309 ASSERT(tcp->tcp_listen_cnt == NULL);
2310 ASSERT(tcp->tcp_reass_tid == 0);
2312 #undef DONTCARE
2313 #undef PRESERVE
2317 * Initialize the various fields in tcp_t. If parent (the listener) is non
2318 * NULL, certain values will be inheritted from it.
2320 void
2321 tcp_init_values(tcp_t *tcp, tcp_t *parent)
2323 tcp_stack_t *tcps = tcp->tcp_tcps;
2324 conn_t *connp = tcp->tcp_connp;
2325 clock_t rto;
2327 ASSERT((connp->conn_family == AF_INET &&
2328 connp->conn_ipversion == IPV4_VERSION) ||
2329 (connp->conn_family == AF_INET6 &&
2330 (connp->conn_ipversion == IPV4_VERSION ||
2331 connp->conn_ipversion == IPV6_VERSION)));
2333 if (parent == NULL) {
2334 tcp->tcp_naglim = tcps->tcps_naglim_def;
2336 tcp->tcp_rto_initial = tcps->tcps_rexmit_interval_initial;
2337 tcp->tcp_rto_min = tcps->tcps_rexmit_interval_min;
2338 tcp->tcp_rto_max = tcps->tcps_rexmit_interval_max;
2340 tcp->tcp_first_ctimer_threshold =
2341 tcps->tcps_ip_notify_cinterval;
2342 tcp->tcp_second_ctimer_threshold =
2343 tcps->tcps_ip_abort_cinterval;
2344 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval;
2345 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval;
2347 tcp->tcp_fin_wait_2_flush_interval =
2348 tcps->tcps_fin_wait_2_flush_interval;
2350 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval;
2351 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval;
2352 tcp->tcp_ka_cnt = 0;
2353 tcp->tcp_ka_rinterval = 0;
2356 * Default value of tcp_init_cwnd is 0, so no need to set here
2357 * if parent is NULL. But we need to inherit it from parent.
2359 } else {
2360 /* Inherit various TCP parameters from the parent. */
2361 tcp->tcp_naglim = parent->tcp_naglim;
2363 tcp->tcp_rto_initial = parent->tcp_rto_initial;
2364 tcp->tcp_rto_min = parent->tcp_rto_min;
2365 tcp->tcp_rto_max = parent->tcp_rto_max;
2367 tcp->tcp_first_ctimer_threshold =
2368 parent->tcp_first_ctimer_threshold;
2369 tcp->tcp_second_ctimer_threshold =
2370 parent->tcp_second_ctimer_threshold;
2371 tcp->tcp_first_timer_threshold =
2372 parent->tcp_first_timer_threshold;
2373 tcp->tcp_second_timer_threshold =
2374 parent->tcp_second_timer_threshold;
2376 tcp->tcp_fin_wait_2_flush_interval =
2377 parent->tcp_fin_wait_2_flush_interval;
2379 tcp->tcp_ka_interval = parent->tcp_ka_interval;
2380 tcp->tcp_ka_abort_thres = parent->tcp_ka_abort_thres;
2381 tcp->tcp_ka_cnt = parent->tcp_ka_cnt;
2382 tcp->tcp_ka_rinterval = parent->tcp_ka_rinterval;
2384 tcp->tcp_init_cwnd = parent->tcp_init_cwnd;
2388 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
2389 * will be close to tcp_rexmit_interval_initial. By doing this, we
2390 * allow the algorithm to adjust slowly to large fluctuations of RTT
2391 * during first few transmissions of a connection as seen in slow
2392 * links.
2394 tcp->tcp_rtt_sa = tcp->tcp_rto_initial << 2;
2395 tcp->tcp_rtt_sd = tcp->tcp_rto_initial >> 1;
2396 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
2397 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) +
2398 tcps->tcps_conn_grace_period;
2399 TCP_SET_RTO(tcp, rto);
2401 tcp->tcp_timer_backoff = 0;
2402 tcp->tcp_ms_we_have_waited = 0;
2403 tcp->tcp_last_recv_time = ddi_get_lbolt();
2404 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_;
2405 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
2407 tcp->tcp_maxpsz_multiplier = tcps->tcps_maxpsz_multiplier;
2409 /* NOTE: ISS is now set in tcp_set_destination(). */
2411 /* Reset fusion-related fields */
2412 tcp->tcp_fused = B_FALSE;
2413 tcp->tcp_unfusable = B_FALSE;
2414 tcp->tcp_fused_sigurg = B_FALSE;
2415 tcp->tcp_loopback_peer = NULL;
2417 /* We rebuild the header template on the next connect/conn_request */
2420 * Init the window scale to the max so tcp_rwnd_set() won't pare
2421 * down tcp_rwnd. tcp_set_destination() will set the right value later.
2423 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
2424 tcp->tcp_rwnd = connp->conn_rcvbuf;
2426 tcp->tcp_cork = B_FALSE;
2428 * Init the tcp_debug option if it wasn't already set. This value
2429 * determines whether TCP
2430 * calls strlog() to print out debug messages. Doing this
2431 * initialization here means that this value is not inherited thru
2432 * tcp_reinit().
2434 if (!connp->conn_debug)
2435 connp->conn_debug = tcps->tcps_dbg;
2439 * Update the TCP connection according to change of PMTU.
2441 * Path MTU might have changed by either increase or decrease, so need to
2442 * adjust the MSS based on the value of ixa_pmtu. No need to handle tiny
2443 * or negative MSS, since tcp_mss_set() will do it.
2445 void
2446 tcp_update_pmtu(tcp_t *tcp, boolean_t decrease_only)
2448 uint32_t pmtu;
2449 int32_t mss;
2450 conn_t *connp = tcp->tcp_connp;
2451 ip_xmit_attr_t *ixa = connp->conn_ixa;
2452 iaflags_t ixaflags;
2454 if (tcp->tcp_tcps->tcps_ignore_path_mtu)
2455 return;
2457 if (tcp->tcp_state < TCPS_ESTABLISHED)
2458 return;
2461 * Always call ip_get_pmtu() to make sure that IP has updated
2462 * ixa_flags properly.
2464 pmtu = ip_get_pmtu(ixa);
2465 ixaflags = ixa->ixa_flags;
2468 * Calculate the MSS by decreasing the PMTU by conn_ht_iphc_len and
2469 * IPsec overhead if applied. Make sure to use the most recent
2470 * IPsec information.
2472 mss = pmtu - connp->conn_ht_iphc_len - conn_ipsec_length(connp);
2475 * Nothing to change, so just return.
2477 if (mss == tcp->tcp_mss)
2478 return;
2481 * Currently, for ICMP errors, only PMTU decrease is handled.
2483 if (mss > tcp->tcp_mss && decrease_only)
2484 return;
2486 DTRACE_PROBE2(tcp_update_pmtu, int32_t, tcp->tcp_mss, uint32_t, mss);
2489 * Update ixa_fragsize and ixa_pmtu.
2491 ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
2494 * Adjust MSS and all relevant variables.
2496 tcp_mss_set(tcp, mss);
2499 * If the PMTU is below the min size maintained by IP, then ip_get_pmtu
2500 * has set IXAF_PMTU_TOO_SMALL and cleared IXAF_PMTU_IPV4_DF. Since TCP
2501 * has a (potentially different) min size we do the same. Make sure to
2502 * clear IXAF_DONTFRAG, which is used by IP to decide whether to
2503 * fragment the packet.
2505 * LSO over IPv6 can not be fragmented. So need to disable LSO
2506 * when IPv6 fragmentation is needed.
2508 if (mss < tcp->tcp_tcps->tcps_mss_min)
2509 ixaflags |= IXAF_PMTU_TOO_SMALL;
2511 if (ixaflags & IXAF_PMTU_TOO_SMALL)
2512 ixaflags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
2514 if ((connp->conn_ipversion == IPV4_VERSION) &&
2515 !(ixaflags & IXAF_PMTU_IPV4_DF)) {
2516 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0;
2518 ixa->ixa_flags = ixaflags;
2522 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk)
2524 conn_t *connp = tcp->tcp_connp;
2525 queue_t *q = connp->conn_rq;
2526 int32_t mss = tcp->tcp_mss;
2527 int maxpsz;
2529 if (TCP_IS_DETACHED(tcp))
2530 return (mss);
2531 if (tcp->tcp_fused) {
2532 maxpsz = tcp_fuse_maxpsz(tcp);
2533 mss = INFPSZ;
2534 } else if (tcp->tcp_maxpsz_multiplier == 0) {
2536 * Set the sd_qn_maxpsz according to the socket send buffer
2537 * size, and sd_maxblk to INFPSZ (-1). This will essentially
2538 * instruct the stream head to copyin user data into contiguous
2539 * kernel-allocated buffers without breaking it up into smaller
2540 * chunks. We round up the buffer size to the nearest SMSS.
2542 maxpsz = MSS_ROUNDUP(connp->conn_sndbuf, mss);
2543 mss = INFPSZ;
2544 } else {
2546 * Set sd_qn_maxpsz to approx half the (receivers) buffer
2547 * (and a multiple of the mss). This instructs the stream
2548 * head to break down larger than SMSS writes into SMSS-
2549 * size mblks, up to tcp_maxpsz_multiplier mblks at a time.
2551 maxpsz = tcp->tcp_maxpsz_multiplier * mss;
2552 if (maxpsz > connp->conn_sndbuf / 2) {
2553 maxpsz = connp->conn_sndbuf / 2;
2554 /* Round up to nearest mss */
2555 maxpsz = MSS_ROUNDUP(maxpsz, mss);
2559 (void) proto_set_maxpsz(q, connp, maxpsz);
2560 if (!(IPCL_IS_NONSTR(connp)))
2561 connp->conn_wq->q_maxpsz = maxpsz;
2562 if (set_maxblk)
2563 (void) proto_set_tx_maxblk(q, connp, mss);
2564 return (mss);
2567 /* For /dev/tcp aka AF_INET open */
2568 static int
2569 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2571 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE));
2574 /* For /dev/tcp6 aka AF_INET6 open */
2575 static int
2576 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2578 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE));
2581 conn_t *
2582 tcp_create_common(cred_t *credp, boolean_t isv6, boolean_t issocket,
2583 int *errorp)
2585 tcp_t *tcp = NULL;
2586 conn_t *connp;
2587 zoneid_t zoneid;
2588 tcp_stack_t *tcps;
2589 squeue_t *sqp;
2591 ASSERT(errorp != NULL);
2593 * Find the proper zoneid and netstack.
2596 * Special case for install: miniroot needs to be able to
2597 * access files via NFS as though it were always in the
2598 * global zone.
2600 if (credp == kcred && nfs_global_client_only != 0) {
2601 zoneid = GLOBAL_ZONEID;
2602 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->
2603 netstack_tcp;
2604 ASSERT(tcps != NULL);
2605 } else {
2606 netstack_t *ns;
2607 int err;
2609 if ((err = secpolicy_basic_net_access(credp)) != 0) {
2610 *errorp = err;
2611 return (NULL);
2614 ns = netstack_find_by_cred(credp);
2615 ASSERT(ns != NULL);
2616 tcps = ns->netstack_tcp;
2617 ASSERT(tcps != NULL);
2620 * For exclusive stacks we set the zoneid to zero
2621 * to make TCP operate as if in the global zone.
2623 if (tcps->tcps_netstack->netstack_stackid !=
2624 GLOBAL_NETSTACKID)
2625 zoneid = GLOBAL_ZONEID;
2626 else
2627 zoneid = crgetzoneid(credp);
2630 sqp = IP_SQUEUE_GET((uint_t)gethrtime());
2631 connp = (conn_t *)tcp_get_conn(sqp, tcps);
2633 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt,
2634 * so we drop it by one.
2636 netstack_rele(tcps->tcps_netstack);
2637 if (connp == NULL) {
2638 *errorp = ENOSR;
2639 return (NULL);
2641 ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2643 connp->conn_sqp = sqp;
2644 connp->conn_initial_sqp = connp->conn_sqp;
2645 connp->conn_ixa->ixa_sqp = connp->conn_sqp;
2646 tcp = connp->conn_tcp;
2649 * Besides asking IP to set the checksum for us, have conn_ip_output
2650 * to do the following checks when necessary:
2652 * IXAF_VERIFY_SOURCE: drop packets when our outer source goes invalid
2653 * IXAF_VERIFY_PMTU: verify PMTU changes
2654 * IXAF_VERIFY_LSO: verify LSO capability changes
2656 connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2657 IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO;
2659 if (!tcps->tcps_dev_flow_ctl)
2660 connp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
2662 if (isv6) {
2663 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
2664 connp->conn_ipversion = IPV6_VERSION;
2665 connp->conn_family = AF_INET6;
2666 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2667 connp->conn_default_ttl = tcps->tcps_ipv6_hoplimit;
2668 } else {
2669 connp->conn_ipversion = IPV4_VERSION;
2670 connp->conn_family = AF_INET;
2671 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2672 connp->conn_default_ttl = tcps->tcps_ipv4_ttl;
2674 connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl;
2676 crhold(credp);
2677 connp->conn_cred = credp;
2678 connp->conn_cpid = curproc->p_pid;
2679 connp->conn_open_time = ddi_get_lbolt64();
2681 /* Cache things in the ixa without any refhold */
2682 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
2683 connp->conn_ixa->ixa_cred = credp;
2684 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
2686 connp->conn_zoneid = zoneid;
2687 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
2688 connp->conn_ixa->ixa_zoneid = zoneid;
2689 ASSERT(connp->conn_netstack == tcps->tcps_netstack);
2690 ASSERT(tcp->tcp_tcps == tcps);
2692 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
2694 if (issocket) {
2695 tcp->tcp_issocket = 1;
2698 connp->conn_rcvbuf = tcps->tcps_recv_hiwat;
2699 connp->conn_sndbuf = tcps->tcps_xmit_hiwat;
2700 if (tcps->tcps_snd_lowat_fraction != 0) {
2701 connp->conn_sndlowat = connp->conn_sndbuf /
2702 tcps->tcps_snd_lowat_fraction;
2703 } else {
2704 connp->conn_sndlowat = tcps->tcps_xmit_lowat;
2706 connp->conn_so_type = SOCK_STREAM;
2707 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2708 tcps->tcps_wroff_xtra;
2710 SOCK_CONNID_INIT(tcp->tcp_connid);
2711 /* DTrace ignores this - it isn't a tcp:::state-change */
2712 tcp->tcp_state = TCPS_IDLE;
2713 tcp_init_values(tcp, NULL);
2714 return (connp);
2717 static int
2718 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
2719 boolean_t isv6)
2721 tcp_t *tcp = NULL;
2722 conn_t *connp = NULL;
2723 int err;
2724 vmem_t *minor_arena = NULL;
2725 dev_t conn_dev;
2726 boolean_t issocket;
2728 if (q->q_ptr != NULL)
2729 return (0);
2731 if (sflag == MODOPEN)
2732 return (EINVAL);
2734 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
2735 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
2736 minor_arena = ip_minor_arena_la;
2737 } else {
2739 * Either minor numbers in the large arena were exhausted
2740 * or a non socket application is doing the open.
2741 * Try to allocate from the small arena.
2743 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
2744 return (EBUSY);
2746 minor_arena = ip_minor_arena_sa;
2749 ASSERT(minor_arena != NULL);
2751 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev);
2753 if (flag & SO_FALLBACK) {
2755 * Non streams socket needs a stream to fallback to
2757 RD(q)->q_ptr = (void *)conn_dev;
2758 WR(q)->q_qinfo = &tcp_fallback_sock_winit;
2759 WR(q)->q_ptr = (void *)minor_arena;
2760 qprocson(q);
2761 return (0);
2762 } else if (flag & SO_ACCEPTOR) {
2763 q->q_qinfo = &tcp_acceptor_rinit;
2765 * the conn_dev and minor_arena will be subsequently used by
2766 * tcp_tli_accept() and tcp_tpi_close_accept() to figure out
2767 * the minor device number for this connection from the q_ptr.
2769 RD(q)->q_ptr = (void *)conn_dev;
2770 WR(q)->q_qinfo = &tcp_acceptor_winit;
2771 WR(q)->q_ptr = (void *)minor_arena;
2772 qprocson(q);
2773 return (0);
2776 issocket = flag & SO_SOCKSTR;
2777 connp = tcp_create_common(credp, isv6, issocket, &err);
2779 if (connp == NULL) {
2780 inet_minor_free(minor_arena, conn_dev);
2781 q->q_ptr = WR(q)->q_ptr = NULL;
2782 return (err);
2785 connp->conn_rq = q;
2786 connp->conn_wq = WR(q);
2787 q->q_ptr = WR(q)->q_ptr = connp;
2789 connp->conn_dev = conn_dev;
2790 connp->conn_minor_arena = minor_arena;
2792 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6);
2793 ASSERT(WR(q)->q_qinfo == &tcp_winit);
2795 tcp = connp->conn_tcp;
2797 if (issocket) {
2798 WR(q)->q_qinfo = &tcp_sock_winit;
2799 } else {
2800 #ifdef _ILP32
2801 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q);
2802 #else
2803 tcp->tcp_acceptor_id = conn_dev;
2804 #endif /* _ILP32 */
2805 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp);
2809 * Put the ref for TCP. Ref for IP was already put
2810 * by ipcl_conn_create. Also Make the conn_t globally
2811 * visible to walkers
2813 mutex_enter(&connp->conn_lock);
2814 CONN_INC_REF_LOCKED(connp);
2815 ASSERT(connp->conn_ref == 2);
2816 connp->conn_state_flags &= ~CONN_INCIPIENT;
2817 mutex_exit(&connp->conn_lock);
2819 qprocson(q);
2820 return (0);
2824 * Build/update the tcp header template (in conn_ht_iphc) based on
2825 * conn_xmit_ipp. The headers include ip6_t, any extension
2826 * headers, and the maximum size tcp header (to avoid reallocation
2827 * on the fly for additional tcp options).
2829 * Assumes the caller has already set conn_{faddr,laddr,fport,lport,flowinfo}.
2830 * Returns failure if can't allocate memory.
2833 tcp_build_hdrs(tcp_t *tcp)
2835 tcp_stack_t *tcps = tcp->tcp_tcps;
2836 conn_t *connp = tcp->tcp_connp;
2837 char buf[TCP_MAX_HDR_LENGTH];
2838 uint_t buflen;
2839 uint_t ulplen = TCP_MIN_HEADER_LENGTH;
2840 uint_t extralen = TCP_MAX_TCP_OPTIONS_LENGTH;
2841 tcpha_t *tcpha;
2842 uint32_t cksum;
2843 int error;
2846 * We might be called after the connection is set up, and we might
2847 * have TS options already in the TCP header. Thus we save any
2848 * existing tcp header.
2850 buflen = connp->conn_ht_ulp_len;
2851 if (buflen != 0) {
2852 bcopy(connp->conn_ht_ulp, buf, buflen);
2853 extralen -= buflen - ulplen;
2854 ulplen = buflen;
2857 /* Grab lock to satisfy ASSERT; TCP is serialized using squeue */
2858 mutex_enter(&connp->conn_lock);
2859 error = conn_build_hdr_template(connp, ulplen, extralen,
2860 &connp->conn_laddr_v6, &connp->conn_faddr_v6, connp->conn_flowinfo);
2861 mutex_exit(&connp->conn_lock);
2862 if (error != 0)
2863 return (error);
2866 * Any routing header/option has been massaged. The checksum difference
2867 * is stored in conn_sum for later use.
2869 tcpha = (tcpha_t *)connp->conn_ht_ulp;
2870 tcp->tcp_tcpha = tcpha;
2872 /* restore any old tcp header */
2873 if (buflen != 0) {
2874 bcopy(buf, connp->conn_ht_ulp, buflen);
2875 } else {
2876 tcpha->tha_sum = 0;
2877 tcpha->tha_urp = 0;
2878 tcpha->tha_ack = 0;
2879 tcpha->tha_offset_and_reserved = (5 << 4);
2880 tcpha->tha_lport = connp->conn_lport;
2881 tcpha->tha_fport = connp->conn_fport;
2885 * IP wants our header length in the checksum field to
2886 * allow it to perform a single pseudo-header+checksum
2887 * calculation on behalf of TCP.
2888 * Include the adjustment for a source route once IP_OPTIONS is set.
2890 cksum = sizeof (tcpha_t) + connp->conn_sum;
2891 cksum = (cksum >> 16) + (cksum & 0xFFFF);
2892 ASSERT(cksum < 0x10000);
2893 tcpha->tha_sum = htons(cksum);
2895 if (connp->conn_ipversion == IPV4_VERSION)
2896 tcp->tcp_ipha = (ipha_t *)connp->conn_ht_iphc;
2897 else
2898 tcp->tcp_ip6h = (ip6_t *)connp->conn_ht_iphc;
2900 if (connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra >
2901 connp->conn_wroff) {
2902 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2903 tcps->tcps_wroff_xtra;
2904 (void) proto_set_tx_wroff(connp->conn_rq, connp,
2905 connp->conn_wroff);
2907 return (0);
2911 * tcp_rwnd_set() is called to adjust the receive window to a desired value.
2912 * We do not allow the receive window to shrink. After setting rwnd,
2913 * set the flow control hiwat of the stream.
2915 * This function is called in 2 cases:
2917 * 1) Before data transfer begins, in tcp_input_listener() for accepting a
2918 * connection (passive open) and in tcp_input_data() for active connect.
2919 * This is called after tcp_mss_set() when the desired MSS value is known.
2920 * This makes sure that our window size is a mutiple of the other side's
2921 * MSS.
2922 * 2) Handling SO_RCVBUF option.
2924 * It is ASSUMED that the requested size is a multiple of the current MSS.
2926 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
2927 * user requests so.
2930 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
2932 uint32_t mss = tcp->tcp_mss;
2933 uint32_t old_max_rwnd;
2934 uint32_t max_transmittable_rwnd;
2935 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
2936 tcp_stack_t *tcps = tcp->tcp_tcps;
2937 conn_t *connp = tcp->tcp_connp;
2940 * Insist on a receive window that is at least
2941 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
2942 * funny TCP interactions of Nagle algorithm, SWS avoidance
2943 * and delayed acknowledgement.
2945 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss);
2947 if (tcp->tcp_fused) {
2948 size_t sth_hiwat;
2949 tcp_t *peer_tcp = tcp->tcp_loopback_peer;
2951 ASSERT(peer_tcp != NULL);
2952 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd);
2953 if (!tcp_detached) {
2954 (void) proto_set_rx_hiwat(connp->conn_rq, connp,
2955 sth_hiwat);
2956 tcp_set_recv_threshold(tcp, sth_hiwat >> 3);
2959 /* Caller could have changed tcp_rwnd; update tha_win */
2960 if (tcp->tcp_tcpha != NULL) {
2961 tcp->tcp_tcpha->tha_win =
2962 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2964 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
2965 tcp->tcp_cwnd_max = rwnd;
2968 * In the fusion case, the maxpsz stream head value of
2969 * our peer is set according to its send buffer size
2970 * and our receive buffer size; since the latter may
2971 * have changed we need to update the peer's maxpsz.
2973 (void) tcp_maxpsz_set(peer_tcp, B_TRUE);
2974 return (sth_hiwat);
2977 if (tcp_detached)
2978 old_max_rwnd = tcp->tcp_rwnd;
2979 else
2980 old_max_rwnd = connp->conn_rcvbuf;
2984 * If window size info has already been exchanged, TCP should not
2985 * shrink the window. Shrinking window is doable if done carefully.
2986 * We may add that support later. But so far there is not a real
2987 * need to do that.
2989 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
2990 /* MSS may have changed, do a round up again. */
2991 rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
2995 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
2996 * can be applied even before the window scale option is decided.
2998 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
2999 if (rwnd > max_transmittable_rwnd) {
3000 rwnd = max_transmittable_rwnd -
3001 (max_transmittable_rwnd % mss);
3002 if (rwnd < mss)
3003 rwnd = max_transmittable_rwnd;
3005 * If we're over the limit we may have to back down tcp_rwnd.
3006 * The increment below won't work for us. So we set all three
3007 * here and the increment below will have no effect.
3009 tcp->tcp_rwnd = old_max_rwnd = rwnd;
3011 if (tcp->tcp_localnet) {
3012 tcp->tcp_rack_abs_max =
3013 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2);
3014 } else {
3016 * For a remote host on a different subnet (through a router),
3017 * we ack every other packet to be conforming to RFC1122.
3018 * tcp_deferred_acks_max is default to 2.
3020 tcp->tcp_rack_abs_max =
3021 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2);
3023 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max)
3024 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
3025 else
3026 tcp->tcp_rack_cur_max = 0;
3028 * Increment the current rwnd by the amount the maximum grew (we
3029 * can not overwrite it since we might be in the middle of a
3030 * connection.)
3032 tcp->tcp_rwnd += rwnd - old_max_rwnd;
3033 connp->conn_rcvbuf = rwnd;
3035 /* Are we already connected? */
3036 if (tcp->tcp_tcpha != NULL) {
3037 tcp->tcp_tcpha->tha_win =
3038 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3041 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
3042 tcp->tcp_cwnd_max = rwnd;
3044 if (tcp_detached)
3045 return (rwnd);
3047 tcp_set_recv_threshold(tcp, rwnd >> 3);
3049 (void) proto_set_rx_hiwat(connp->conn_rq, connp, rwnd);
3050 return (rwnd);
3054 tcp_do_unbind(conn_t *connp)
3056 tcp_t *tcp = connp->conn_tcp;
3057 int32_t oldstate;
3059 switch (tcp->tcp_state) {
3060 case TCPS_BOUND:
3061 case TCPS_LISTEN:
3062 break;
3063 default:
3064 return (-TOUTSTATE);
3068 * Need to clean up all the eagers since after the unbind, segments
3069 * will no longer be delivered to this listener stream.
3071 mutex_enter(&tcp->tcp_eager_lock);
3072 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
3073 tcp_eager_cleanup(tcp, 0);
3075 mutex_exit(&tcp->tcp_eager_lock);
3077 /* Clean up the listener connection counter if necessary. */
3078 if (tcp->tcp_listen_cnt != NULL)
3079 TCP_DECR_LISTEN_CNT(tcp);
3080 connp->conn_laddr_v6 = ipv6_all_zeros;
3081 connp->conn_saddr_v6 = ipv6_all_zeros;
3082 tcp_bind_hash_remove(tcp);
3083 oldstate = tcp->tcp_state;
3084 tcp->tcp_state = TCPS_IDLE;
3085 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
3086 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
3087 int32_t, oldstate);
3089 ip_unbind(connp);
3090 bzero(&connp->conn_ports, sizeof (connp->conn_ports));
3092 return (0);
3096 * Collect protocol properties to send to the upper handle.
3098 void
3099 tcp_get_proto_props(tcp_t *tcp, struct sock_proto_props *sopp)
3101 conn_t *connp = tcp->tcp_connp;
3103 sopp->sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF;
3104 sopp->sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE);
3106 sopp->sopp_rxhiwat = tcp->tcp_fused ?
3107 tcp_fuse_set_rcv_hiwat(tcp, connp->conn_rcvbuf) :
3108 connp->conn_rcvbuf;
3110 * Determine what write offset value to use depending on SACK and
3111 * whether the endpoint is fused or not.
3113 if (tcp->tcp_fused) {
3114 ASSERT(tcp->tcp_loopback);
3115 ASSERT(tcp->tcp_loopback_peer != NULL);
3117 * For fused tcp loopback, set the stream head's write
3118 * offset value to zero since we won't be needing any room
3119 * for TCP/IP headers. This would also improve performance
3120 * since it would reduce the amount of work done by kmem.
3121 * Non-fused tcp loopback case is handled separately below.
3123 sopp->sopp_wroff = 0;
3125 * Update the peer's transmit parameters according to
3126 * our recently calculated high water mark value.
3128 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE);
3129 } else if (tcp->tcp_snd_sack_ok) {
3130 sopp->sopp_wroff = connp->conn_ht_iphc_allocated +
3131 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3132 } else {
3133 sopp->sopp_wroff = connp->conn_ht_iphc_len +
3134 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3137 if (tcp->tcp_loopback) {
3138 sopp->sopp_flags |= SOCKOPT_LOOPBACK;
3139 sopp->sopp_loopback = B_TRUE;
3144 * Check the usability of ZEROCOPY. It's instead checking the flag set by IP.
3146 boolean_t
3147 tcp_zcopy_check(tcp_t *tcp)
3149 conn_t *connp = tcp->tcp_connp;
3150 ip_xmit_attr_t *ixa = connp->conn_ixa;
3151 boolean_t zc_enabled = B_FALSE;
3152 tcp_stack_t *tcps = tcp->tcp_tcps;
3154 if (do_tcpzcopy == 2)
3155 zc_enabled = B_TRUE;
3156 else if ((do_tcpzcopy == 1) && (ixa->ixa_flags & IXAF_ZCOPY_CAPAB))
3157 zc_enabled = B_TRUE;
3159 tcp->tcp_snd_zcopy_on = zc_enabled;
3160 if (!TCP_IS_DETACHED(tcp)) {
3161 if (zc_enabled) {
3162 ixa->ixa_flags |= IXAF_VERIFY_ZCOPY;
3163 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3164 ZCVMSAFE);
3165 TCP_STAT(tcps, tcp_zcopy_on);
3166 } else {
3167 ixa->ixa_flags &= ~IXAF_VERIFY_ZCOPY;
3168 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3169 ZCVMUNSAFE);
3170 TCP_STAT(tcps, tcp_zcopy_off);
3173 return (zc_enabled);
3177 * Backoff from a zero-copy message by copying data to a new allocated
3178 * message and freeing the original desballoca'ed segmapped message.
3180 * This function is called by following two callers:
3181 * 1. tcp_timer: fix_xmitlist is set to B_TRUE, because it's safe to free
3182 * the origial desballoca'ed message and notify sockfs. This is in re-
3183 * transmit state.
3184 * 2. tcp_output: fix_xmitlist is set to B_FALSE. Flag STRUIO_ZCNOTIFY need
3185 * to be copied to new message.
3187 mblk_t *
3188 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, boolean_t fix_xmitlist)
3190 mblk_t *nbp;
3191 mblk_t *head = NULL;
3192 mblk_t *tail = NULL;
3193 tcp_stack_t *tcps = tcp->tcp_tcps;
3195 ASSERT(bp != NULL);
3196 while (bp != NULL) {
3197 if (IS_VMLOANED_MBLK(bp)) {
3198 TCP_STAT(tcps, tcp_zcopy_backoff);
3199 if ((nbp = copyb(bp)) == NULL) {
3200 tcp->tcp_xmit_zc_clean = B_FALSE;
3201 if (tail != NULL)
3202 tail->b_cont = bp;
3203 return ((head == NULL) ? bp : head);
3206 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) {
3207 if (fix_xmitlist)
3208 tcp_zcopy_notify(tcp);
3209 else
3210 nbp->b_datap->db_struioflag |=
3211 STRUIO_ZCNOTIFY;
3213 nbp->b_cont = bp->b_cont;
3216 * Copy saved information and adjust tcp_xmit_tail
3217 * if needed.
3219 if (fix_xmitlist) {
3220 nbp->b_prev = bp->b_prev;
3221 nbp->b_next = bp->b_next;
3223 if (tcp->tcp_xmit_tail == bp)
3224 tcp->tcp_xmit_tail = nbp;
3227 /* Free the original message. */
3228 bp->b_prev = NULL;
3229 bp->b_next = NULL;
3230 freeb(bp);
3232 bp = nbp;
3235 if (head == NULL) {
3236 head = bp;
3238 if (tail == NULL) {
3239 tail = bp;
3240 } else {
3241 tail->b_cont = bp;
3242 tail = bp;
3245 /* Move forward. */
3246 bp = bp->b_cont;
3249 if (fix_xmitlist) {
3250 tcp->tcp_xmit_last = tail;
3251 tcp->tcp_xmit_zc_clean = B_TRUE;
3254 return (head);
3257 void
3258 tcp_zcopy_notify(tcp_t *tcp)
3260 struct stdata *stp;
3261 conn_t *connp;
3263 if (tcp->tcp_detached)
3264 return;
3265 connp = tcp->tcp_connp;
3266 if (IPCL_IS_NONSTR(connp)) {
3267 (*connp->conn_upcalls->su_zcopy_notify)
3268 (connp->conn_upper_handle);
3269 return;
3271 stp = STREAM(connp->conn_rq);
3272 mutex_enter(&stp->sd_lock);
3273 stp->sd_flag |= STZCNOTIFY;
3274 cv_broadcast(&stp->sd_zcopy_wait);
3275 mutex_exit(&stp->sd_lock);
3279 * Update the TCP connection according to change of LSO capability.
3281 static void
3282 tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa)
3285 * We check against IPv4 header length to preserve the old behavior
3286 * of only enabling LSO when there are no IP options.
3287 * But this restriction might not be necessary at all. Before removing
3288 * it, need to verify how LSO is handled for source routing case, with
3289 * which IP does software checksum.
3291 * For IPv6, whenever any extension header is needed, LSO is supressed.
3293 if (ixa->ixa_ip_hdr_length != ((ixa->ixa_flags & IXAF_IS_IPV4) ?
3294 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN))
3295 return;
3298 * Either the LSO capability newly became usable, or it has changed.
3300 if (ixa->ixa_flags & IXAF_LSO_CAPAB) {
3301 ill_lso_capab_t *lsoc = &ixa->ixa_lso_capab;
3303 ASSERT(lsoc->ill_lso_max > 0);
3304 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, lsoc->ill_lso_max);
3306 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3307 boolean_t, B_TRUE, uint32_t, tcp->tcp_lso_max);
3310 * If LSO to be enabled, notify the STREAM header with larger
3311 * data block.
3313 if (!tcp->tcp_lso)
3314 tcp->tcp_maxpsz_multiplier = 0;
3316 tcp->tcp_lso = B_TRUE;
3317 TCP_STAT(tcp->tcp_tcps, tcp_lso_enabled);
3318 } else { /* LSO capability is not usable any more. */
3319 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3320 boolean_t, B_FALSE, uint32_t, tcp->tcp_lso_max);
3323 * If LSO to be disabled, notify the STREAM header with smaller
3324 * data block. And need to restore fragsize to PMTU.
3326 if (tcp->tcp_lso) {
3327 tcp->tcp_maxpsz_multiplier =
3328 tcp->tcp_tcps->tcps_maxpsz_multiplier;
3329 ixa->ixa_fragsize = ixa->ixa_pmtu;
3330 tcp->tcp_lso = B_FALSE;
3331 TCP_STAT(tcp->tcp_tcps, tcp_lso_disabled);
3335 (void) tcp_maxpsz_set(tcp, B_TRUE);
3339 * Update the TCP connection according to change of ZEROCOPY capability.
3341 static void
3342 tcp_update_zcopy(tcp_t *tcp)
3344 conn_t *connp = tcp->tcp_connp;
3345 tcp_stack_t *tcps = tcp->tcp_tcps;
3347 if (tcp->tcp_snd_zcopy_on) {
3348 tcp->tcp_snd_zcopy_on = B_FALSE;
3349 if (!TCP_IS_DETACHED(tcp)) {
3350 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3351 ZCVMUNSAFE);
3352 TCP_STAT(tcps, tcp_zcopy_off);
3354 } else {
3355 tcp->tcp_snd_zcopy_on = B_TRUE;
3356 if (!TCP_IS_DETACHED(tcp)) {
3357 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3358 ZCVMSAFE);
3359 TCP_STAT(tcps, tcp_zcopy_on);
3365 * Notify function registered with ip_xmit_attr_t. It's called in the squeue
3366 * so it's safe to update the TCP connection.
3368 /* ARGSUSED1 */
3369 static void
3370 tcp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
3371 ixa_notify_arg_t narg)
3373 tcp_t *tcp = (tcp_t *)arg;
3374 conn_t *connp = tcp->tcp_connp;
3376 switch (ntype) {
3377 case IXAN_LSO:
3378 tcp_update_lso(tcp, connp->conn_ixa);
3379 break;
3380 case IXAN_PMTU:
3381 tcp_update_pmtu(tcp, B_FALSE);
3382 break;
3383 case IXAN_ZCOPY:
3384 tcp_update_zcopy(tcp);
3385 break;
3386 default:
3387 break;
3392 * The TCP write service routine should never be called...
3394 /* ARGSUSED */
3395 static void
3396 tcp_wsrv(queue_t *q)
3398 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps;
3400 TCP_STAT(tcps, tcp_wsrv_called);
3404 * Hash list lookup routine for tcp_t structures.
3405 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF.
3407 tcp_t *
3408 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps)
3410 tf_t *tf;
3411 tcp_t *tcp;
3413 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3414 mutex_enter(&tf->tf_lock);
3415 for (tcp = tf->tf_tcp; tcp != NULL;
3416 tcp = tcp->tcp_acceptor_hash) {
3417 if (tcp->tcp_acceptor_id == id) {
3418 CONN_INC_REF(tcp->tcp_connp);
3419 mutex_exit(&tf->tf_lock);
3420 return (tcp);
3423 mutex_exit(&tf->tf_lock);
3424 return (NULL);
3428 * Hash list insertion routine for tcp_t structures.
3430 void
3431 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp)
3433 tf_t *tf;
3434 tcp_t **tcpp;
3435 tcp_t *tcpnext;
3436 tcp_stack_t *tcps = tcp->tcp_tcps;
3438 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3440 if (tcp->tcp_ptpahn != NULL)
3441 tcp_acceptor_hash_remove(tcp);
3442 tcpp = &tf->tf_tcp;
3443 mutex_enter(&tf->tf_lock);
3444 tcpnext = tcpp[0];
3445 if (tcpnext)
3446 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash;
3447 tcp->tcp_acceptor_hash = tcpnext;
3448 tcp->tcp_ptpahn = tcpp;
3449 tcpp[0] = tcp;
3450 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */
3451 mutex_exit(&tf->tf_lock);
3455 * Hash list removal routine for tcp_t structures.
3457 void
3458 tcp_acceptor_hash_remove(tcp_t *tcp)
3460 tcp_t *tcpnext;
3461 kmutex_t *lockp;
3464 * Extract the lock pointer in case there are concurrent
3465 * hash_remove's for this instance.
3467 lockp = tcp->tcp_acceptor_lockp;
3469 if (tcp->tcp_ptpahn == NULL)
3470 return;
3472 ASSERT(lockp != NULL);
3473 mutex_enter(lockp);
3474 if (tcp->tcp_ptpahn) {
3475 tcpnext = tcp->tcp_acceptor_hash;
3476 if (tcpnext) {
3477 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn;
3478 tcp->tcp_acceptor_hash = NULL;
3480 *tcp->tcp_ptpahn = tcpnext;
3481 tcp->tcp_ptpahn = NULL;
3483 mutex_exit(lockp);
3484 tcp->tcp_acceptor_lockp = NULL;
3488 * Type three generator adapted from the random() function in 4.4 BSD:
3492 * Copyright (c) 1983, 1993
3493 * The Regents of the University of California. All rights reserved.
3495 * Redistribution and use in source and binary forms, with or without
3496 * modification, are permitted provided that the following conditions
3497 * are met:
3498 * 1. Redistributions of source code must retain the above copyright
3499 * notice, this list of conditions and the following disclaimer.
3500 * 2. Redistributions in binary form must reproduce the above copyright
3501 * notice, this list of conditions and the following disclaimer in the
3502 * documentation and/or other materials provided with the distribution.
3503 * 3. All advertising materials mentioning features or use of this software
3504 * must display the following acknowledgement:
3505 * This product includes software developed by the University of
3506 * California, Berkeley and its contributors.
3507 * 4. Neither the name of the University nor the names of its contributors
3508 * may be used to endorse or promote products derived from this software
3509 * without specific prior written permission.
3511 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3512 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3513 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3514 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3515 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3516 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3517 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3518 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3519 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3520 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3521 * SUCH DAMAGE.
3524 /* Type 3 -- x**31 + x**3 + 1 */
3525 #define DEG_3 31
3526 #define SEP_3 3
3529 /* Protected by tcp_random_lock */
3530 static int tcp_randtbl[DEG_3 + 1];
3532 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
3533 static int *tcp_random_rptr = &tcp_randtbl[1];
3535 static int *tcp_random_state = &tcp_randtbl[1];
3536 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
3538 kmutex_t tcp_random_lock;
3540 void
3541 tcp_random_init(void)
3543 int i;
3544 hrtime_t hrt;
3545 time_t wallclock;
3546 uint64_t result;
3549 * Use high-res timer and current time for seed. Gethrtime() returns
3550 * a longlong, which may contain resolution down to nanoseconds.
3551 * The current time will either be a 32-bit or a 64-bit quantity.
3552 * XOR the two together in a 64-bit result variable.
3553 * Convert the result to a 32-bit value by multiplying the high-order
3554 * 32-bits by the low-order 32-bits.
3557 hrt = gethrtime();
3558 (void) drv_getparm(TIME, &wallclock);
3559 result = (uint64_t)wallclock ^ (uint64_t)hrt;
3560 mutex_enter(&tcp_random_lock);
3561 tcp_random_state[0] = ((result >> 32) & 0xffffffff) *
3562 (result & 0xffffffff);
3564 for (i = 1; i < DEG_3; i++)
3565 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
3566 + 12345;
3567 tcp_random_fptr = &tcp_random_state[SEP_3];
3568 tcp_random_rptr = &tcp_random_state[0];
3569 mutex_exit(&tcp_random_lock);
3570 for (i = 0; i < 10 * DEG_3; i++)
3571 (void) tcp_random();
3575 * tcp_random: Return a random number in the range [1 - (128K + 1)].
3576 * This range is selected to be approximately centered on TCP_ISS / 2,
3577 * and easy to compute. We get this value by generating a 32-bit random
3578 * number, selecting out the high-order 17 bits, and then adding one so
3579 * that we never return zero.
3582 tcp_random(void)
3584 int i;
3586 mutex_enter(&tcp_random_lock);
3587 *tcp_random_fptr += *tcp_random_rptr;
3590 * The high-order bits are more random than the low-order bits,
3591 * so we select out the high-order 17 bits and add one so that
3592 * we never return zero.
3594 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
3595 if (++tcp_random_fptr >= tcp_random_end_ptr) {
3596 tcp_random_fptr = tcp_random_state;
3597 ++tcp_random_rptr;
3598 } else if (++tcp_random_rptr >= tcp_random_end_ptr)
3599 tcp_random_rptr = tcp_random_state;
3601 mutex_exit(&tcp_random_lock);
3602 return (i);
3606 * Called by IP when IP is loaded into the kernel
3608 void
3609 tcp_ddi_g_init(void)
3611 tcp_timercache = kmem_cache_create("tcp_timercache",
3612 sizeof (tcp_timer_t) + sizeof (mblk_t), 0,
3613 NULL, NULL, NULL, NULL, NULL, 0);
3615 tcp_notsack_blk_cache = kmem_cache_create("tcp_notsack_blk_cache",
3616 sizeof (notsack_blk_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3618 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL);
3620 /* Initialize the random number generator */
3621 tcp_random_init();
3623 /* A single callback independently of how many netstacks we have */
3624 ip_squeue_init(tcp_squeue_add);
3626 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics);
3628 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput);
3631 * We want to be informed each time a stack is created or
3632 * destroyed in the kernel, so we can maintain the
3633 * set of tcp_stack_t's.
3635 netstack_register(NS_TCP, tcp_stack_init, NULL, tcp_stack_fini);
3639 #define INET_NAME "ip"
3642 * Initialize the TCP stack instance.
3644 static void *
3645 tcp_stack_init(netstackid_t stackid, netstack_t *ns)
3647 tcp_stack_t *tcps;
3648 int i;
3649 int error = 0;
3650 major_t major;
3651 size_t arrsz;
3652 uint8_t secret[16];
3654 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP);
3655 tcps->tcps_netstack = ns;
3657 /* Initialize locks */
3658 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
3660 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS;
3661 tcps->tcps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
3662 tcps->tcps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
3663 tcps->tcps_min_anonpriv_port = 512;
3665 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) *
3666 TCP_BIND_FANOUT_SIZE, KM_SLEEP);
3667 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) *
3668 TCP_ACCEPTOR_FANOUT_SIZE, KM_SLEEP);
3670 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3671 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL,
3672 MUTEX_DEFAULT, NULL);
3675 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3676 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL,
3677 MUTEX_DEFAULT, NULL);
3680 /* TCP's IPsec code calls the packet dropper. */
3681 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement");
3683 arrsz = tcp_propinfo_count * sizeof (mod_prop_info_t);
3684 tcps->tcps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
3685 KM_SLEEP);
3686 bcopy(tcp_propinfo_tbl, tcps->tcps_propinfo_tbl, arrsz);
3689 * Note: To really walk the device tree you need the devinfo
3690 * pointer to your device which is only available after probe/attach.
3691 * The following is safe only because it uses ddi_root_node()
3693 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr,
3694 tcp_opt_obj.odb_opt_arr_cnt);
3696 /* Initialize the RFC 6528 ISS. */
3697 random_get_pseudo_bytes(secret, sizeof(secret));
3698 MD5Init(&tcps->tcps_iss_key);
3699 MD5Update(&tcps->tcps_iss_key, secret, sizeof(secret));
3701 tcps->tcps_kstat = tcp_kstat2_init(stackid);
3702 tcps->tcps_mibkp = tcp_kstat_init(stackid);
3704 major = mod_name_to_major(INET_NAME);
3705 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident);
3706 ASSERT(error == 0);
3707 tcps->tcps_ixa_cleanup_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
3708 ASSERT(tcps->tcps_ixa_cleanup_mp != NULL);
3709 cv_init(&tcps->tcps_ixa_cleanup_ready_cv, NULL, CV_DEFAULT, NULL);
3710 cv_init(&tcps->tcps_ixa_cleanup_done_cv, NULL, CV_DEFAULT, NULL);
3711 mutex_init(&tcps->tcps_ixa_cleanup_lock, NULL, MUTEX_DEFAULT, NULL);
3713 mutex_init(&tcps->tcps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
3714 tcps->tcps_reclaim = B_FALSE;
3715 tcps->tcps_reclaim_tid = 0;
3716 tcps->tcps_reclaim_period = tcps->tcps_rexmit_interval_max;
3719 * ncpus is the current number of CPUs, which can be bigger than
3720 * boot_ncpus. But we don't want to use ncpus to allocate all the
3721 * tcp_stats_cpu_t at system boot up time since it will be 1. While
3722 * we handle adding CPU in tcp_cpu_update(), it will be slow if
3723 * there are many CPUs as we will be adding them 1 by 1.
3725 * Note that tcps_sc_cnt never decreases and the tcps_sc[x] pointers
3726 * are not freed until the stack is going away. So there is no need
3727 * to grab a lock to access the per CPU tcps_sc[x] pointer.
3729 mutex_enter(&cpu_lock);
3730 tcps->tcps_sc_cnt = MAX(ncpus, boot_ncpus);
3731 mutex_exit(&cpu_lock);
3732 tcps->tcps_sc = kmem_zalloc(max_ncpus * sizeof (tcp_stats_cpu_t *),
3733 KM_SLEEP);
3734 for (i = 0; i < tcps->tcps_sc_cnt; i++) {
3735 tcps->tcps_sc[i] = kmem_zalloc(sizeof (tcp_stats_cpu_t),
3736 KM_SLEEP);
3739 mutex_init(&tcps->tcps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
3740 list_create(&tcps->tcps_listener_conf, sizeof (tcp_listener_t),
3741 offsetof(tcp_listener_t, tl_link));
3743 return (tcps);
3747 * Called when the IP module is about to be unloaded.
3749 void
3750 tcp_ddi_g_destroy(void)
3752 tcp_g_kstat_fini(tcp_g_kstat);
3753 tcp_g_kstat = NULL;
3754 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics));
3756 mutex_destroy(&tcp_random_lock);
3758 kmem_cache_destroy(tcp_timercache);
3759 kmem_cache_destroy(tcp_notsack_blk_cache);
3761 netstack_unregister(NS_TCP);
3765 * Free the TCP stack instance.
3767 static void
3768 tcp_stack_fini(netstackid_t stackid, void *arg)
3770 tcp_stack_t *tcps = (tcp_stack_t *)arg;
3771 int i;
3773 freeb(tcps->tcps_ixa_cleanup_mp);
3774 tcps->tcps_ixa_cleanup_mp = NULL;
3775 cv_destroy(&tcps->tcps_ixa_cleanup_ready_cv);
3776 cv_destroy(&tcps->tcps_ixa_cleanup_done_cv);
3777 mutex_destroy(&tcps->tcps_ixa_cleanup_lock);
3780 * Set tcps_reclaim to false tells tcp_reclaim_timer() not to restart
3781 * the timer.
3783 mutex_enter(&tcps->tcps_reclaim_lock);
3784 tcps->tcps_reclaim = B_FALSE;
3785 mutex_exit(&tcps->tcps_reclaim_lock);
3786 if (tcps->tcps_reclaim_tid != 0)
3787 (void) untimeout(tcps->tcps_reclaim_tid);
3788 mutex_destroy(&tcps->tcps_reclaim_lock);
3790 tcp_listener_conf_cleanup(tcps);
3792 for (i = 0; i < tcps->tcps_sc_cnt; i++)
3793 kmem_free(tcps->tcps_sc[i], sizeof (tcp_stats_cpu_t));
3794 kmem_free(tcps->tcps_sc, max_ncpus * sizeof (tcp_stats_cpu_t *));
3796 kmem_free(tcps->tcps_propinfo_tbl,
3797 tcp_propinfo_count * sizeof (mod_prop_info_t));
3798 tcps->tcps_propinfo_tbl = NULL;
3800 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3801 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL);
3802 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock);
3805 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3806 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL);
3807 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock);
3810 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE);
3811 tcps->tcps_bind_fanout = NULL;
3813 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) *
3814 TCP_ACCEPTOR_FANOUT_SIZE);
3815 tcps->tcps_acceptor_fanout = NULL;
3817 mutex_destroy(&tcps->tcps_epriv_port_lock);
3819 ip_drop_unregister(&tcps->tcps_dropper);
3821 tcp_kstat2_fini(stackid, tcps->tcps_kstat);
3822 tcps->tcps_kstat = NULL;
3824 tcp_kstat_fini(stackid, tcps->tcps_mibkp);
3825 tcps->tcps_mibkp = NULL;
3827 ldi_ident_release(tcps->tcps_ldi_ident);
3828 kmem_free(tcps, sizeof (*tcps));
3831 static void
3832 tcp_iss_init(tcp_t *tcp)
3834 MD5_CTX context;
3835 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg;
3836 uint32_t answer[4];
3837 tcp_stack_t *tcps = tcp->tcp_tcps;
3838 conn_t *connp = tcp->tcp_connp;
3840 /* tcp_iss may already have been set in tcp_input_listener */
3841 if (!tcp->tcp_iss) {
3842 tcps->tcps_iss_incr_extra += (tcps->tcps_iss_incr >> 1);
3843 tcp->tcp_iss = tcps->tcps_iss_incr_extra;
3844 context = tcps->tcps_iss_key;
3845 arg.ports = connp->conn_ports;
3846 arg.src = connp->conn_laddr_v6;
3847 arg.dst = connp->conn_faddr_v6;
3848 MD5Update(&context, (uchar_t *)&arg, sizeof (arg));
3849 MD5Final((uchar_t *)answer, &context);
3850 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3];
3852 tcp->tcp_valid_bits = TCP_ISS_VALID;
3853 tcp->tcp_fss = tcp->tcp_iss - 1;
3854 tcp->tcp_suna = tcp->tcp_iss;
3855 tcp->tcp_snxt = tcp->tcp_iss + 1;
3856 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3857 tcp->tcp_csuna = tcp->tcp_snxt;
3861 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL
3862 * on the specified backing STREAMS q. Note, the caller may make the
3863 * decision to call based on the tcp_t.tcp_flow_stopped value which
3864 * when check outside the q's lock is only an advisory check ...
3866 void
3867 tcp_setqfull(tcp_t *tcp)
3869 tcp_stack_t *tcps = tcp->tcp_tcps;
3870 conn_t *connp = tcp->tcp_connp;
3872 if (tcp->tcp_closed)
3873 return;
3875 conn_setqfull(connp, &tcp->tcp_flow_stopped);
3876 if (tcp->tcp_flow_stopped)
3877 TCP_STAT(tcps, tcp_flwctl_on);
3880 void
3881 tcp_clrqfull(tcp_t *tcp)
3883 conn_t *connp = tcp->tcp_connp;
3885 if (tcp->tcp_closed)
3886 return;
3887 conn_clrqfull(connp, &tcp->tcp_flow_stopped);
3890 static int
3891 tcp_squeue_switch(int val)
3893 int rval = SQ_FILL;
3895 switch (val) {
3896 case 1:
3897 rval = SQ_NODRAIN;
3898 break;
3899 case 2:
3900 rval = SQ_PROCESS;
3901 break;
3902 default:
3903 break;
3905 return (rval);
3909 * This is called once for each squeue - globally for all stack
3910 * instances.
3912 static void
3913 tcp_squeue_add(squeue_t *sqp)
3915 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc(
3916 sizeof (tcp_squeue_priv_t), KM_SLEEP);
3918 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait;
3919 if (tcp_free_list_max_cnt == 0) {
3920 int tcp_ncpus = ((boot_max_ncpus == -1) ?
3921 max_ncpus : boot_max_ncpus);
3924 * Limit number of entries to 1% of availble memory / tcp_ncpus
3926 tcp_free_list_max_cnt = (freemem * PAGESIZE) /
3927 (tcp_ncpus * sizeof (tcp_t) * 100);
3929 tcp_time_wait->tcp_free_list_cnt = 0;
3932 * Return unix error is tli error is TSYSERR, otherwise return a negative
3933 * tli error.
3936 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr,
3937 boolean_t bind_to_req_port_only)
3939 int error;
3940 tcp_t *tcp = connp->conn_tcp;
3942 if (tcp->tcp_state >= TCPS_BOUND) {
3943 if (connp->conn_debug) {
3944 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
3945 "tcp_bind: bad state, %d", tcp->tcp_state);
3947 return (-TOUTSTATE);
3950 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only);
3951 if (error != 0)
3952 return (error);
3954 ASSERT(tcp->tcp_state == TCPS_BOUND);
3955 tcp->tcp_conn_req_max = 0;
3956 return (0);
3960 * If the return value from this function is positive, it's a UNIX error.
3961 * Otherwise, if it's negative, then the absolute value is a TLI error.
3962 * the TPI routine tcp_tpi_connect() is a wrapper function for this.
3965 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len,
3966 cred_t *cr, pid_t pid)
3968 tcp_t *tcp = connp->conn_tcp;
3969 sin_t *sin = (sin_t *)sa;
3970 sin6_t *sin6 = (sin6_t *)sa;
3971 ipaddr_t *dstaddrp;
3972 in_port_t dstport;
3973 uint_t srcid;
3974 int error;
3975 uint32_t mss;
3976 mblk_t *syn_mp;
3977 tcp_stack_t *tcps = tcp->tcp_tcps;
3978 int32_t oldstate;
3979 ip_xmit_attr_t *ixa = connp->conn_ixa;
3981 oldstate = tcp->tcp_state;
3983 switch (len) {
3984 default:
3986 * Should never happen
3988 return (EINVAL);
3990 case sizeof (sin_t):
3991 sin = (sin_t *)sa;
3992 if (sin->sin_port == 0) {
3993 return (-TBADADDR);
3995 if (connp->conn_ipv6_v6only) {
3996 return (EAFNOSUPPORT);
3998 break;
4000 case sizeof (sin6_t):
4001 sin6 = (sin6_t *)sa;
4002 if (sin6->sin6_port == 0) {
4003 return (-TBADADDR);
4005 break;
4008 * If we're connecting to an IPv4-mapped IPv6 address, we need to
4009 * make sure that the conn_ipversion is IPV4_VERSION. We
4010 * need to this before we call tcp_bindi() so that the port lookup
4011 * code will look for ports in the correct port space (IPv4 and
4012 * IPv6 have separate port spaces).
4014 if (connp->conn_family == AF_INET6 &&
4015 connp->conn_ipversion == IPV6_VERSION &&
4016 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4017 if (connp->conn_ipv6_v6only)
4018 return (EADDRNOTAVAIL);
4020 connp->conn_ipversion = IPV4_VERSION;
4023 switch (tcp->tcp_state) {
4024 case TCPS_LISTEN:
4026 * Listening sockets are not allowed to issue connect().
4028 if (IPCL_IS_NONSTR(connp))
4029 return (EOPNOTSUPP);
4030 /* FALLTHRU */
4031 case TCPS_IDLE:
4033 * We support quick connect, refer to comments in
4034 * tcp_connect_*()
4036 /* FALLTHRU */
4037 case TCPS_BOUND:
4038 break;
4039 default:
4040 return (-TOUTSTATE);
4044 * We update our cred/cpid based on the caller of connect
4046 if (connp->conn_cred != cr) {
4047 crhold(cr);
4048 crfree(connp->conn_cred);
4049 connp->conn_cred = cr;
4051 connp->conn_cpid = pid;
4053 /* Cache things in the ixa without any refhold */
4054 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4055 ixa->ixa_cred = cr;
4056 ixa->ixa_cpid = pid;
4058 if (connp->conn_family == AF_INET6) {
4059 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4060 error = tcp_connect_ipv6(tcp, &sin6->sin6_addr,
4061 sin6->sin6_port, sin6->sin6_flowinfo,
4062 sin6->__sin6_src_id, sin6->sin6_scope_id);
4063 } else {
4065 * Destination adress is mapped IPv6 address.
4066 * Source bound address should be unspecified or
4067 * IPv6 mapped address as well.
4069 if (!IN6_IS_ADDR_UNSPECIFIED(
4070 &connp->conn_bound_addr_v6) &&
4071 !IN6_IS_ADDR_V4MAPPED(&connp->conn_bound_addr_v6)) {
4072 return (EADDRNOTAVAIL);
4074 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr));
4075 dstport = sin6->sin6_port;
4076 srcid = sin6->__sin6_src_id;
4077 error = tcp_connect_ipv4(tcp, dstaddrp, dstport,
4078 srcid);
4080 } else {
4081 dstaddrp = &sin->sin_addr.s_addr;
4082 dstport = sin->sin_port;
4083 srcid = 0;
4084 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid);
4087 if (error != 0)
4088 goto connect_failed;
4090 /* connect succeeded */
4091 TCPS_BUMP_MIB(tcps, tcpActiveOpens);
4092 tcp->tcp_active_open = 1;
4095 * tcp_set_destination() does not adjust for TCP/IP header length.
4097 mss = tcp->tcp_mss - connp->conn_ht_iphc_len;
4100 * Just make sure our rwnd is at least rcvbuf * MSS large, and round up
4101 * to the nearest MSS.
4103 * We do the round up here because we need to get the interface MTU
4104 * first before we can do the round up.
4106 tcp->tcp_rwnd = connp->conn_rcvbuf;
4107 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
4108 tcps->tcps_recv_hiwat_minmss * mss);
4109 connp->conn_rcvbuf = tcp->tcp_rwnd;
4110 tcp_set_ws_value(tcp);
4111 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
4112 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always)
4113 tcp->tcp_snd_ws_ok = B_TRUE;
4116 * Set tcp_snd_ts_ok to true
4117 * so that tcp_xmit_mp will
4118 * include the timestamp
4119 * option in the SYN segment.
4121 if (tcps->tcps_tstamp_always ||
4122 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) {
4123 tcp->tcp_snd_ts_ok = B_TRUE;
4127 * Note that tcp_snd_sack_ok can be set in tcp_set_destination() if
4128 * the SACK metric is set. So here we just check the per stack SACK
4129 * permitted param.
4131 if (tcps->tcps_sack_permitted == 2) {
4132 ASSERT(tcp->tcp_num_sack_blk == 0);
4133 ASSERT(tcp->tcp_notsack_list == NULL);
4134 tcp->tcp_snd_sack_ok = B_TRUE;
4138 * Should we use ECN? Note that the current
4139 * default value (SunOS 5.9) of tcp_ecn_permitted
4140 * is 1. The reason for doing this is that there
4141 * are equipments out there that will drop ECN
4142 * enabled IP packets. Setting it to 1 avoids
4143 * compatibility problems.
4145 if (tcps->tcps_ecn_permitted == 2)
4146 tcp->tcp_ecn_ok = B_TRUE;
4148 /* Trace change from BOUND -> SYN_SENT here */
4149 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4150 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4151 int32_t, TCPS_BOUND);
4153 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4154 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
4155 tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
4156 if (syn_mp != NULL) {
4158 * We must bump the generation before sending the syn
4159 * to ensure that we use the right generation in case
4160 * this thread issues a "connected" up call.
4162 SOCK_CONNID_BUMP(tcp->tcp_connid);
4164 * DTrace sending the first SYN as a
4165 * tcp:::connect-request event.
4167 DTRACE_TCP5(connect__request, mblk_t *, NULL,
4168 ip_xmit_attr_t *, connp->conn_ixa,
4169 void_ip_t *, syn_mp->b_rptr, tcp_t *, tcp,
4170 tcph_t *,
4171 &syn_mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
4172 tcp_send_data(tcp, syn_mp);
4175 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4176 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4177 return (0);
4179 connect_failed:
4180 connp->conn_faddr_v6 = ipv6_all_zeros;
4181 connp->conn_fport = 0;
4182 tcp->tcp_state = oldstate;
4183 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4184 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4185 return (error);
4189 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len,
4190 int backlog, cred_t *cr, boolean_t bind_to_req_port_only)
4192 tcp_t *tcp = connp->conn_tcp;
4193 int error = 0;
4194 tcp_stack_t *tcps = tcp->tcp_tcps;
4195 int32_t oldstate;
4197 /* All Solaris components should pass a cred for this operation. */
4198 ASSERT(cr != NULL);
4200 if (tcp->tcp_state >= TCPS_BOUND) {
4201 if ((tcp->tcp_state == TCPS_BOUND ||
4202 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) {
4204 * Handle listen() increasing backlog.
4205 * This is more "liberal" then what the TPI spec
4206 * requires but is needed to avoid a t_unbind
4207 * when handling listen() since the port number
4208 * might be "stolen" between the unbind and bind.
4210 goto do_listen;
4212 if (connp->conn_debug) {
4213 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4214 "tcp_listen: bad state, %d", tcp->tcp_state);
4216 return (-TOUTSTATE);
4217 } else {
4218 if (sa == NULL) {
4219 sin6_t addr;
4220 sin_t *sin;
4221 sin6_t *sin6;
4223 ASSERT(IPCL_IS_NONSTR(connp));
4224 /* Do an implicit bind: Request for a generic port. */
4225 if (connp->conn_family == AF_INET) {
4226 len = sizeof (sin_t);
4227 sin = (sin_t *)&addr;
4228 *sin = sin_null;
4229 sin->sin_family = AF_INET;
4230 } else {
4231 ASSERT(connp->conn_family == AF_INET6);
4232 len = sizeof (sin6_t);
4233 sin6 = (sin6_t *)&addr;
4234 *sin6 = sin6_null;
4235 sin6->sin6_family = AF_INET6;
4237 sa = (struct sockaddr *)&addr;
4240 error = tcp_bind_check(connp, sa, len, cr,
4241 bind_to_req_port_only);
4242 if (error)
4243 return (error);
4244 /* Fall through and do the fanout insertion */
4247 do_listen:
4248 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN);
4249 tcp->tcp_conn_req_max = backlog;
4250 if (tcp->tcp_conn_req_max) {
4251 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min)
4252 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min;
4253 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q)
4254 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q;
4256 * If this is a listener, do not reset the eager list
4257 * and other stuffs. Note that we don't check if the
4258 * existing eager list meets the new tcp_conn_req_max
4259 * requirement.
4261 if (tcp->tcp_state != TCPS_LISTEN) {
4262 tcp->tcp_state = TCPS_LISTEN;
4263 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4264 connp->conn_ixa, void, NULL, tcp_t *, tcp,
4265 void, NULL, int32_t, TCPS_BOUND);
4266 /* Initialize the chain. Don't need the eager_lock */
4267 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
4268 tcp->tcp_eager_next_drop_q0 = tcp;
4269 tcp->tcp_eager_prev_drop_q0 = tcp;
4270 tcp->tcp_second_ctimer_threshold =
4271 tcps->tcps_ip_abort_linterval;
4276 * We need to make sure that the conn_recv is set to a non-null
4277 * value before we insert the conn into the classifier table.
4278 * This is to avoid a race with an incoming packet which does an
4279 * ipcl_classify().
4280 * We initially set it to tcp_input_listener_unbound to try to
4281 * pick a good squeue for the listener when the first SYN arrives.
4282 * tcp_input_listener_unbound sets it to tcp_input_listener on that
4283 * first SYN.
4285 connp->conn_recv = tcp_input_listener_unbound;
4287 /* Insert the listener in the classifier table */
4288 error = ip_laddr_fanout_insert(connp);
4289 if (error != 0) {
4290 /* Undo the bind - release the port number */
4291 oldstate = tcp->tcp_state;
4292 tcp->tcp_state = TCPS_IDLE;
4293 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4294 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4295 int32_t, oldstate);
4296 connp->conn_bound_addr_v6 = ipv6_all_zeros;
4298 connp->conn_laddr_v6 = ipv6_all_zeros;
4299 connp->conn_saddr_v6 = ipv6_all_zeros;
4300 connp->conn_ports = 0;
4302 tcp_bind_hash_remove(tcp);
4303 return (error);
4304 } else {
4306 * If there is a connection limit, allocate and initialize
4307 * the counter struct. Note that since listen can be called
4308 * multiple times, the struct may have been allready allocated.
4310 if (!list_is_empty(&tcps->tcps_listener_conf) &&
4311 tcp->tcp_listen_cnt == NULL) {
4312 tcp_listen_cnt_t *tlc;
4313 uint32_t ratio;
4315 ratio = tcp_find_listener_conf(tcps,
4316 ntohs(connp->conn_lport));
4317 if (ratio != 0) {
4318 uint32_t mem_ratio, tot_buf;
4320 tlc = kmem_alloc(sizeof (tcp_listen_cnt_t),
4321 KM_SLEEP);
4323 * Calculate the connection limit based on
4324 * the configured ratio and maxusers. Maxusers
4325 * are calculated based on memory size,
4326 * ~ 1 user per MB. Note that the conn_rcvbuf
4327 * and conn_sndbuf may change after a
4328 * connection is accepted. So what we have
4329 * is only an approximation.
4331 if ((tot_buf = connp->conn_rcvbuf +
4332 connp->conn_sndbuf) < MB) {
4333 mem_ratio = MB / tot_buf;
4334 tlc->tlc_max = maxusers / ratio *
4335 mem_ratio;
4336 } else {
4337 mem_ratio = tot_buf / MB;
4338 tlc->tlc_max = maxusers / ratio /
4339 mem_ratio;
4341 /* At least we should allow two connections! */
4342 if (tlc->tlc_max <= tcp_min_conn_listener)
4343 tlc->tlc_max = tcp_min_conn_listener;
4344 tlc->tlc_cnt = 1;
4345 tlc->tlc_drop = 0;
4346 tcp->tcp_listen_cnt = tlc;
4350 return (error);