Print this page
3942 inject sanity into ipadm tcp buffer size properties
3943 _snd_lowat_fraction tcp tunable has no effect
Reviewed by: Adam Leventhal <ahl@delphix.com>
Reviewed by: Peng Dai <peng.dai@delphix.com>
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/tcp/tcp.c
+++ new/usr/src/uts/common/inet/tcp/tcp.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 2011, Joyent Inc. All rights reserved.
25 25 * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
26 + * Copyright (c) 2013 by Delphix. All rights reserved.
26 27 */
27 28 /* Copyright (c) 1990 Mentat Inc. */
28 29
29 30 #include <sys/types.h>
30 31 #include <sys/stream.h>
31 32 #include <sys/strsun.h>
32 33 #include <sys/strsubr.h>
33 34 #include <sys/stropts.h>
34 35 #include <sys/strlog.h>
35 36 #define _SUN_TPI_VERSION 2
36 37 #include <sys/tihdr.h>
37 38 #include <sys/timod.h>
38 39 #include <sys/ddi.h>
39 40 #include <sys/sunddi.h>
40 41 #include <sys/suntpi.h>
41 42 #include <sys/xti_inet.h>
42 43 #include <sys/cmn_err.h>
43 44 #include <sys/debug.h>
44 45 #include <sys/sdt.h>
45 46 #include <sys/vtrace.h>
46 47 #include <sys/kmem.h>
47 48 #include <sys/ethernet.h>
48 49 #include <sys/cpuvar.h>
49 50 #include <sys/dlpi.h>
50 51 #include <sys/pattr.h>
51 52 #include <sys/policy.h>
52 53 #include <sys/priv.h>
53 54 #include <sys/zone.h>
54 55 #include <sys/sunldi.h>
55 56
56 57 #include <sys/errno.h>
57 58 #include <sys/signal.h>
58 59 #include <sys/socket.h>
59 60 #include <sys/socketvar.h>
60 61 #include <sys/sockio.h>
61 62 #include <sys/isa_defs.h>
62 63 #include <sys/md5.h>
63 64 #include <sys/random.h>
64 65 #include <sys/uio.h>
65 66 #include <sys/systm.h>
66 67 #include <netinet/in.h>
67 68 #include <netinet/tcp.h>
68 69 #include <netinet/ip6.h>
69 70 #include <netinet/icmp6.h>
70 71 #include <net/if.h>
71 72 #include <net/route.h>
72 73 #include <inet/ipsec_impl.h>
73 74
74 75 #include <inet/common.h>
75 76 #include <inet/ip.h>
76 77 #include <inet/ip_impl.h>
77 78 #include <inet/ip6.h>
78 79 #include <inet/ip_ndp.h>
79 80 #include <inet/proto_set.h>
80 81 #include <inet/mib2.h>
81 82 #include <inet/optcom.h>
82 83 #include <inet/snmpcom.h>
83 84 #include <inet/kstatcom.h>
84 85 #include <inet/tcp.h>
85 86 #include <inet/tcp_impl.h>
86 87 #include <inet/tcp_cluster.h>
87 88 #include <inet/udp_impl.h>
88 89 #include <net/pfkeyv2.h>
89 90 #include <inet/ipdrop.h>
90 91
91 92 #include <inet/ipclassifier.h>
92 93 #include <inet/ip_ire.h>
93 94 #include <inet/ip_ftable.h>
94 95 #include <inet/ip_if.h>
95 96 #include <inet/ipp_common.h>
96 97 #include <inet/ip_rts.h>
97 98 #include <inet/ip_netinfo.h>
98 99 #include <sys/squeue_impl.h>
99 100 #include <sys/squeue.h>
100 101 #include <sys/tsol/label.h>
101 102 #include <sys/tsol/tnet.h>
102 103 #include <rpc/pmap_prot.h>
103 104 #include <sys/callo.h>
104 105
105 106 /*
106 107 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433)
107 108 *
108 109 * (Read the detailed design doc in PSARC case directory)
109 110 *
110 111 * The entire tcp state is contained in tcp_t and conn_t structure
111 112 * which are allocated in tandem using ipcl_conn_create() and passing
112 113 * IPCL_TCPCONN as a flag. We use 'conn_ref' and 'conn_lock' to protect
113 114 * the references on the tcp_t. The tcp_t structure is never compressed
114 115 * and packets always land on the correct TCP perimeter from the time
115 116 * eager is created till the time tcp_t dies (as such the old mentat
116 117 * TCP global queue is not used for detached state and no IPSEC checking
117 118 * is required). The global queue is still allocated to send out resets
118 119 * for connection which have no listeners and IP directly calls
119 120 * tcp_xmit_listeners_reset() which does any policy check.
120 121 *
121 122 * Protection and Synchronisation mechanism:
122 123 *
123 124 * The tcp data structure does not use any kind of lock for protecting
124 125 * its state but instead uses 'squeues' for mutual exclusion from various
125 126 * read and write side threads. To access a tcp member, the thread should
126 127 * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS,
127 128 * or SQ_NODRAIN). Since the squeues allow a direct function call, caller
128 129 * can pass any tcp function having prototype of edesc_t as argument
129 130 * (different from traditional STREAMs model where packets come in only
130 131 * designated entry points). The list of functions that can be directly
131 132 * called via squeue are listed before the usual function prototype.
132 133 *
133 134 * Referencing:
134 135 *
135 136 * TCP is MT-Hot and we use a reference based scheme to make sure that the
136 137 * tcp structure doesn't disappear when its needed. When the application
137 138 * creates an outgoing connection or accepts an incoming connection, we
138 139 * start out with 2 references on 'conn_ref'. One for TCP and one for IP.
139 140 * The IP reference is just a symbolic reference since ip_tcpclose()
140 141 * looks at tcp structure after tcp_close_output() returns which could
141 142 * have dropped the last TCP reference. So as long as the connection is
142 143 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the
143 144 * conn_t. The classifier puts its own reference when the connection is
144 145 * inserted in listen or connected hash. Anytime a thread needs to enter
145 146 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr
146 147 * on write side or by doing a classify on read side and then puts a
147 148 * reference on the conn before doing squeue_enter/tryenter/fill. For
148 149 * read side, the classifier itself puts the reference under fanout lock
149 150 * to make sure that tcp can't disappear before it gets processed. The
150 151 * squeue will drop this reference automatically so the called function
151 152 * doesn't have to do a DEC_REF.
152 153 *
153 154 * Opening a new connection:
154 155 *
155 156 * The outgoing connection open is pretty simple. tcp_open() does the
156 157 * work in creating the conn/tcp structure and initializing it. The
157 158 * squeue assignment is done based on the CPU the application
158 159 * is running on. So for outbound connections, processing is always done
159 160 * on application CPU which might be different from the incoming CPU
160 161 * being interrupted by the NIC. An optimal way would be to figure out
161 162 * the NIC <-> CPU binding at listen time, and assign the outgoing
162 163 * connection to the squeue attached to the CPU that will be interrupted
163 164 * for incoming packets (we know the NIC based on the bind IP address).
164 165 * This might seem like a problem if more data is going out but the
165 166 * fact is that in most cases the transmit is ACK driven transmit where
166 167 * the outgoing data normally sits on TCP's xmit queue waiting to be
167 168 * transmitted.
168 169 *
169 170 * Accepting a connection:
170 171 *
171 172 * This is a more interesting case because of various races involved in
172 173 * establishing a eager in its own perimeter. Read the meta comment on
173 174 * top of tcp_input_listener(). But briefly, the squeue is picked by
174 175 * ip_fanout based on the ring or the sender (if loopback).
175 176 *
176 177 * Closing a connection:
177 178 *
178 179 * The close is fairly straight forward. tcp_close() calls tcp_close_output()
179 180 * via squeue to do the close and mark the tcp as detached if the connection
180 181 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its
181 182 * reference but tcp_close() drop IP's reference always. So if tcp was
182 183 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP
183 184 * and 1 because it is in classifier's connected hash. This is the condition
184 185 * we use to determine that its OK to clean up the tcp outside of squeue
185 186 * when time wait expires (check the ref under fanout and conn_lock and
186 187 * if it is 2, remove it from fanout hash and kill it).
187 188 *
188 189 * Although close just drops the necessary references and marks the
189 190 * tcp_detached state, tcp_close needs to know the tcp_detached has been
190 191 * set (under squeue) before letting the STREAM go away (because a
191 192 * inbound packet might attempt to go up the STREAM while the close
192 193 * has happened and tcp_detached is not set). So a special lock and
193 194 * flag is used along with a condition variable (tcp_closelock, tcp_closed,
194 195 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked
195 196 * tcp_detached.
196 197 *
197 198 * Special provisions and fast paths:
198 199 *
199 200 * We make special provisions for sockfs by marking tcp_issocket
200 201 * whenever we have only sockfs on top of TCP. This allows us to skip
201 202 * putting the tcp in acceptor hash since a sockfs listener can never
202 203 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM
203 204 * since eager has already been allocated and the accept now happens
204 205 * on acceptor STREAM. There is a big blob of comment on top of
205 206 * tcp_input_listener explaining the new accept. When socket is POP'd,
206 207 * sockfs sends us an ioctl to mark the fact and we go back to old
207 208 * behaviour. Once tcp_issocket is unset, its never set for the
208 209 * life of that connection.
209 210 *
210 211 * IPsec notes :
211 212 *
212 213 * Since a packet is always executed on the correct TCP perimeter
213 214 * all IPsec processing is defered to IP including checking new
214 215 * connections and setting IPSEC policies for new connection. The
215 216 * only exception is tcp_xmit_listeners_reset() which is called
216 217 * directly from IP and needs to policy check to see if TH_RST
217 218 * can be sent out.
218 219 */
219 220
220 221 /*
221 222 * Values for squeue switch:
222 223 * 1: SQ_NODRAIN
223 224 * 2: SQ_PROCESS
224 225 * 3: SQ_FILL
↓ open down ↓ |
189 lines elided |
↑ open up ↑ |
225 226 */
226 227 int tcp_squeue_wput = 2; /* /etc/systems */
227 228 int tcp_squeue_flag;
228 229
229 230 /*
230 231 * To prevent memory hog, limit the number of entries in tcp_free_list
231 232 * to 1% of available memory / number of cpus
232 233 */
233 234 uint_t tcp_free_list_max_cnt = 0;
234 235
235 -#define TCP_XMIT_LOWATER 4096
236 -#define TCP_XMIT_HIWATER 49152
237 -#define TCP_RECV_LOWATER 2048
238 -#define TCP_RECV_HIWATER 128000
239 -
240 236 #define TIDUSZ 4096 /* transport interface data unit size */
241 237
242 238 /*
243 239 * Size of acceptor hash list. It has to be a power of 2 for hashing.
244 240 */
245 241 #define TCP_ACCEPTOR_FANOUT_SIZE 512
246 242
247 243 #ifdef _ILP32
248 244 #define TCP_ACCEPTOR_HASH(accid) \
249 245 (((uint_t)(accid) >> 8) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
250 246 #else
251 247 #define TCP_ACCEPTOR_HASH(accid) \
252 248 ((uint_t)(accid) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
253 249 #endif /* _ILP32 */
254 250
255 251 /*
256 252 * Minimum number of connections which can be created per listener. Used
257 253 * when the listener connection count is in effect.
258 254 */
259 255 static uint32_t tcp_min_conn_listener = 2;
260 256
261 257 uint32_t tcp_early_abort = 30;
262 258
263 259 /* TCP Timer control structure */
264 260 typedef struct tcpt_s {
265 261 pfv_t tcpt_pfv; /* The routine we are to call */
266 262 tcp_t *tcpt_tcp; /* The parameter we are to pass in */
267 263 } tcpt_t;
268 264
269 265 /*
270 266 * Functions called directly via squeue having a prototype of edesc_t.
271 267 */
272 268 void tcp_input_listener(void *arg, mblk_t *mp, void *arg2,
273 269 ip_recv_attr_t *ira);
274 270 void tcp_input_data(void *arg, mblk_t *mp, void *arg2,
275 271 ip_recv_attr_t *ira);
276 272 static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2,
277 273 ip_recv_attr_t *dummy);
278 274
279 275
280 276 /* Prototype for TCP functions */
281 277 static void tcp_random_init(void);
282 278 int tcp_random(void);
283 279 static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp,
284 280 in_port_t dstport, uint_t srcid);
285 281 static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp,
286 282 in_port_t dstport, uint32_t flowinfo,
287 283 uint_t srcid, uint32_t scope_id);
288 284 static void tcp_iss_init(tcp_t *tcp);
289 285 static void tcp_reinit(tcp_t *tcp);
290 286 static void tcp_reinit_values(tcp_t *tcp);
291 287
292 288 static void tcp_wsrv(queue_t *q);
293 289 static void tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa);
294 290 static void tcp_update_zcopy(tcp_t *tcp);
295 291 static void tcp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
296 292 ixa_notify_arg_t);
297 293 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns);
298 294 static void tcp_stack_fini(netstackid_t stackid, void *arg);
299 295
300 296 static int tcp_squeue_switch(int);
301 297
302 298 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t);
303 299 static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *);
304 300 static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *);
305 301
306 302 static void tcp_squeue_add(squeue_t *);
307 303
308 304 struct module_info tcp_rinfo = {
309 305 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER
310 306 };
311 307
312 308 static struct module_info tcp_winfo = {
313 309 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16
314 310 };
315 311
316 312 /*
317 313 * Entry points for TCP as a device. The normal case which supports
318 314 * the TCP functionality.
319 315 * We have separate open functions for the /dev/tcp and /dev/tcp6 devices.
320 316 */
321 317 struct qinit tcp_rinitv4 = {
322 318 NULL, (pfi_t)tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo
323 319 };
324 320
325 321 struct qinit tcp_rinitv6 = {
326 322 NULL, (pfi_t)tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo
327 323 };
328 324
329 325 struct qinit tcp_winit = {
330 326 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
331 327 };
332 328
333 329 /* Initial entry point for TCP in socket mode. */
334 330 struct qinit tcp_sock_winit = {
335 331 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
336 332 };
337 333
338 334 /* TCP entry point during fallback */
339 335 struct qinit tcp_fallback_sock_winit = {
340 336 (pfi_t)tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo
341 337 };
342 338
343 339 /*
344 340 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing
345 341 * an accept. Avoid allocating data structures since eager has already
346 342 * been created.
347 343 */
348 344 struct qinit tcp_acceptor_rinit = {
349 345 NULL, (pfi_t)tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo
350 346 };
351 347
352 348 struct qinit tcp_acceptor_winit = {
353 349 (pfi_t)tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo
354 350 };
355 351
356 352 /* For AF_INET aka /dev/tcp */
357 353 struct streamtab tcpinfov4 = {
358 354 &tcp_rinitv4, &tcp_winit
359 355 };
360 356
361 357 /* For AF_INET6 aka /dev/tcp6 */
362 358 struct streamtab tcpinfov6 = {
363 359 &tcp_rinitv6, &tcp_winit
364 360 };
365 361
366 362 /*
367 363 * Following assumes TPI alignment requirements stay along 32 bit
368 364 * boundaries
369 365 */
370 366 #define ROUNDUP32(x) \
371 367 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1))
372 368
373 369 /* Template for response to info request. */
374 370 struct T_info_ack tcp_g_t_info_ack = {
375 371 T_INFO_ACK, /* PRIM_type */
376 372 0, /* TSDU_size */
377 373 T_INFINITE, /* ETSDU_size */
378 374 T_INVALID, /* CDATA_size */
379 375 T_INVALID, /* DDATA_size */
380 376 sizeof (sin_t), /* ADDR_size */
381 377 0, /* OPT_size - not initialized here */
382 378 TIDUSZ, /* TIDU_size */
383 379 T_COTS_ORD, /* SERV_type */
384 380 TCPS_IDLE, /* CURRENT_state */
385 381 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
386 382 };
387 383
388 384 struct T_info_ack tcp_g_t_info_ack_v6 = {
389 385 T_INFO_ACK, /* PRIM_type */
390 386 0, /* TSDU_size */
391 387 T_INFINITE, /* ETSDU_size */
392 388 T_INVALID, /* CDATA_size */
393 389 T_INVALID, /* DDATA_size */
394 390 sizeof (sin6_t), /* ADDR_size */
395 391 0, /* OPT_size - not initialized here */
396 392 TIDUSZ, /* TIDU_size */
397 393 T_COTS_ORD, /* SERV_type */
398 394 TCPS_IDLE, /* CURRENT_state */
399 395 (XPG4_1|EXPINLINE) /* PROVIDER_flag */
400 396 };
401 397
402 398 /*
403 399 * TCP tunables related declarations. Definitions are in tcp_tunables.c
404 400 */
405 401 extern mod_prop_info_t tcp_propinfo_tbl[];
406 402 extern int tcp_propinfo_count;
407 403
408 404 #define IS_VMLOANED_MBLK(mp) \
409 405 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0)
410 406
411 407 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */
412 408
413 409 /*
414 410 * Forces all connections to obey the value of the tcps_maxpsz_multiplier
415 411 * tunable settable via NDD. Otherwise, the per-connection behavior is
416 412 * determined dynamically during tcp_set_destination(), which is the default.
417 413 */
418 414 boolean_t tcp_static_maxpsz = B_FALSE;
419 415
420 416 /*
421 417 * If the receive buffer size is changed, this function is called to update
422 418 * the upper socket layer on the new delayed receive wake up threshold.
423 419 */
424 420 static void
425 421 tcp_set_recv_threshold(tcp_t *tcp, uint32_t new_rcvthresh)
426 422 {
427 423 uint32_t default_threshold = SOCKET_RECVHIWATER >> 3;
428 424
429 425 if (IPCL_IS_NONSTR(tcp->tcp_connp)) {
430 426 conn_t *connp = tcp->tcp_connp;
431 427 struct sock_proto_props sopp;
432 428
433 429 /*
434 430 * only increase rcvthresh upto default_threshold
435 431 */
436 432 if (new_rcvthresh > default_threshold)
437 433 new_rcvthresh = default_threshold;
438 434
439 435 sopp.sopp_flags = SOCKOPT_RCVTHRESH;
440 436 sopp.sopp_rcvthresh = new_rcvthresh;
441 437
442 438 (*connp->conn_upcalls->su_set_proto_props)
443 439 (connp->conn_upper_handle, &sopp);
444 440 }
445 441 }
446 442
447 443 /*
448 444 * Figure out the value of window scale opton. Note that the rwnd is
449 445 * ASSUMED to be rounded up to the nearest MSS before the calculation.
450 446 * We cannot find the scale value and then do a round up of tcp_rwnd
451 447 * because the scale value may not be correct after that.
452 448 *
453 449 * Set the compiler flag to make this function inline.
454 450 */
455 451 void
456 452 tcp_set_ws_value(tcp_t *tcp)
457 453 {
458 454 int i;
459 455 uint32_t rwnd = tcp->tcp_rwnd;
460 456
461 457 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT;
462 458 i++, rwnd >>= 1)
463 459 ;
464 460 tcp->tcp_rcv_ws = i;
465 461 }
466 462
467 463 /*
468 464 * Remove cached/latched IPsec references.
469 465 */
470 466 void
471 467 tcp_ipsec_cleanup(tcp_t *tcp)
472 468 {
473 469 conn_t *connp = tcp->tcp_connp;
474 470
475 471 ASSERT(connp->conn_flags & IPCL_TCPCONN);
476 472
477 473 if (connp->conn_latch != NULL) {
478 474 IPLATCH_REFRELE(connp->conn_latch);
479 475 connp->conn_latch = NULL;
480 476 }
481 477 if (connp->conn_latch_in_policy != NULL) {
482 478 IPPOL_REFRELE(connp->conn_latch_in_policy);
483 479 connp->conn_latch_in_policy = NULL;
484 480 }
485 481 if (connp->conn_latch_in_action != NULL) {
486 482 IPACT_REFRELE(connp->conn_latch_in_action);
487 483 connp->conn_latch_in_action = NULL;
488 484 }
489 485 if (connp->conn_policy != NULL) {
490 486 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
491 487 connp->conn_policy = NULL;
492 488 }
493 489 }
494 490
495 491 /*
496 492 * Cleaup before placing on free list.
497 493 * Disassociate from the netstack/tcp_stack_t since the freelist
498 494 * is per squeue and not per netstack.
499 495 */
500 496 void
501 497 tcp_cleanup(tcp_t *tcp)
502 498 {
503 499 mblk_t *mp;
504 500 conn_t *connp = tcp->tcp_connp;
505 501 tcp_stack_t *tcps = tcp->tcp_tcps;
506 502 netstack_t *ns = tcps->tcps_netstack;
507 503 mblk_t *tcp_rsrv_mp;
508 504
509 505 tcp_bind_hash_remove(tcp);
510 506
511 507 /* Cleanup that which needs the netstack first */
512 508 tcp_ipsec_cleanup(tcp);
513 509 ixa_cleanup(connp->conn_ixa);
514 510
515 511 if (connp->conn_ht_iphc != NULL) {
516 512 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
517 513 connp->conn_ht_iphc = NULL;
518 514 connp->conn_ht_iphc_allocated = 0;
519 515 connp->conn_ht_iphc_len = 0;
520 516 connp->conn_ht_ulp = NULL;
521 517 connp->conn_ht_ulp_len = 0;
522 518 tcp->tcp_ipha = NULL;
523 519 tcp->tcp_ip6h = NULL;
524 520 tcp->tcp_tcpha = NULL;
525 521 }
526 522
527 523 /* We clear any IP_OPTIONS and extension headers */
528 524 ip_pkt_free(&connp->conn_xmit_ipp);
529 525
530 526 tcp_free(tcp);
531 527
532 528 /*
533 529 * Since we will bzero the entire structure, we need to
534 530 * remove it and reinsert it in global hash list. We
535 531 * know the walkers can't get to this conn because we
536 532 * had set CONDEMNED flag earlier and checked reference
537 533 * under conn_lock so walker won't pick it and when we
538 534 * go the ipcl_globalhash_remove() below, no walker
539 535 * can get to it.
540 536 */
541 537 ipcl_globalhash_remove(connp);
542 538
543 539 /* Save some state */
544 540 mp = tcp->tcp_timercache;
545 541
546 542 tcp_rsrv_mp = tcp->tcp_rsrv_mp;
547 543
548 544 if (connp->conn_cred != NULL) {
549 545 crfree(connp->conn_cred);
550 546 connp->conn_cred = NULL;
551 547 }
552 548 ipcl_conn_cleanup(connp);
553 549 connp->conn_flags = IPCL_TCPCONN;
554 550
555 551 /*
556 552 * Now it is safe to decrement the reference counts.
557 553 * This might be the last reference on the netstack
558 554 * in which case it will cause the freeing of the IP Instance.
559 555 */
560 556 connp->conn_netstack = NULL;
561 557 connp->conn_ixa->ixa_ipst = NULL;
562 558 netstack_rele(ns);
563 559 ASSERT(tcps != NULL);
564 560 tcp->tcp_tcps = NULL;
565 561
566 562 bzero(tcp, sizeof (tcp_t));
567 563
568 564 /* restore the state */
569 565 tcp->tcp_timercache = mp;
570 566
571 567 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
572 568
573 569 tcp->tcp_connp = connp;
574 570
575 571 ASSERT(connp->conn_tcp == tcp);
576 572 ASSERT(connp->conn_flags & IPCL_TCPCONN);
577 573 connp->conn_state_flags = CONN_INCIPIENT;
578 574 ASSERT(connp->conn_proto == IPPROTO_TCP);
579 575 ASSERT(connp->conn_ref == 1);
580 576 }
581 577
582 578 /*
583 579 * Adapt to the information, such as rtt and rtt_sd, provided from the
584 580 * DCE and IRE maintained by IP.
585 581 *
586 582 * Checks for multicast and broadcast destination address.
587 583 * Returns zero if ok; an errno on failure.
588 584 *
589 585 * Note that the MSS calculation here is based on the info given in
590 586 * the DCE and IRE. We do not do any calculation based on TCP options. They
591 587 * will be handled in tcp_input_data() when TCP knows which options to use.
592 588 *
593 589 * Note on how TCP gets its parameters for a connection.
594 590 *
595 591 * When a tcp_t structure is allocated, it gets all the default parameters.
596 592 * In tcp_set_destination(), it gets those metric parameters, like rtt, rtt_sd,
597 593 * spipe, rpipe, ... from the route metrics. Route metric overrides the
598 594 * default.
599 595 *
600 596 * An incoming SYN with a multicast or broadcast destination address is dropped
601 597 * in ip_fanout_v4/v6.
602 598 *
603 599 * An incoming SYN with a multicast or broadcast source address is always
604 600 * dropped in tcp_set_destination, since IPDF_ALLOW_MCBC is not set in
605 601 * conn_connect.
606 602 * The same logic in tcp_set_destination also serves to
607 603 * reject an attempt to connect to a broadcast or multicast (destination)
608 604 * address.
609 605 */
610 606 int
611 607 tcp_set_destination(tcp_t *tcp)
612 608 {
613 609 uint32_t mss_max;
614 610 uint32_t mss;
615 611 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
616 612 conn_t *connp = tcp->tcp_connp;
617 613 tcp_stack_t *tcps = tcp->tcp_tcps;
618 614 iulp_t uinfo;
619 615 int error;
620 616 uint32_t flags;
621 617
622 618 flags = IPDF_LSO | IPDF_ZCOPY;
623 619 /*
624 620 * Make sure we have a dce for the destination to avoid dce_ident
625 621 * contention for connected sockets.
626 622 */
627 623 flags |= IPDF_UNIQUE_DCE;
628 624
629 625 if (!tcps->tcps_ignore_path_mtu)
630 626 connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
631 627
632 628 /* Use conn_lock to satify ASSERT; tcp is already serialized */
633 629 mutex_enter(&connp->conn_lock);
634 630 error = conn_connect(connp, &uinfo, flags);
635 631 mutex_exit(&connp->conn_lock);
636 632 if (error != 0)
637 633 return (error);
638 634
639 635 error = tcp_build_hdrs(tcp);
640 636 if (error != 0)
641 637 return (error);
642 638
643 639 tcp->tcp_localnet = uinfo.iulp_localnet;
644 640
645 641 if (uinfo.iulp_rtt != 0) {
646 642 clock_t rto;
647 643
648 644 tcp->tcp_rtt_sa = uinfo.iulp_rtt;
649 645 tcp->tcp_rtt_sd = uinfo.iulp_rtt_sd;
650 646 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
651 647 tcps->tcps_rexmit_interval_extra +
652 648 (tcp->tcp_rtt_sa >> 5);
653 649
654 650 TCP_SET_RTO(tcp, rto);
655 651 }
656 652 if (uinfo.iulp_ssthresh != 0)
657 653 tcp->tcp_cwnd_ssthresh = uinfo.iulp_ssthresh;
658 654 else
659 655 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
660 656 if (uinfo.iulp_spipe > 0) {
661 657 connp->conn_sndbuf = MIN(uinfo.iulp_spipe,
662 658 tcps->tcps_max_buf);
663 659 if (tcps->tcps_snd_lowat_fraction != 0) {
664 660 connp->conn_sndlowat = connp->conn_sndbuf /
665 661 tcps->tcps_snd_lowat_fraction;
666 662 }
667 663 (void) tcp_maxpsz_set(tcp, B_TRUE);
668 664 }
669 665 /*
670 666 * Note that up till now, acceptor always inherits receive
671 667 * window from the listener. But if there is a metrics
672 668 * associated with a host, we should use that instead of
673 669 * inheriting it from listener. Thus we need to pass this
674 670 * info back to the caller.
675 671 */
676 672 if (uinfo.iulp_rpipe > 0) {
677 673 tcp->tcp_rwnd = MIN(uinfo.iulp_rpipe,
678 674 tcps->tcps_max_buf);
679 675 }
680 676
681 677 if (uinfo.iulp_rtomax > 0) {
682 678 tcp->tcp_second_timer_threshold =
683 679 uinfo.iulp_rtomax;
684 680 }
685 681
686 682 /*
687 683 * Use the metric option settings, iulp_tstamp_ok and
688 684 * iulp_wscale_ok, only for active open. What this means
689 685 * is that if the other side uses timestamp or window
690 686 * scale option, TCP will also use those options. That
691 687 * is for passive open. If the application sets a
692 688 * large window, window scale is enabled regardless of
693 689 * the value in iulp_wscale_ok. This is the behavior
694 690 * since 2.6. So we keep it.
695 691 * The only case left in passive open processing is the
696 692 * check for SACK.
697 693 * For ECN, it should probably be like SACK. But the
698 694 * current value is binary, so we treat it like the other
699 695 * cases. The metric only controls active open.For passive
700 696 * open, the ndd param, tcp_ecn_permitted, controls the
701 697 * behavior.
702 698 */
703 699 if (!tcp_detached) {
704 700 /*
705 701 * The if check means that the following can only
706 702 * be turned on by the metrics only IRE, but not off.
707 703 */
708 704 if (uinfo.iulp_tstamp_ok)
709 705 tcp->tcp_snd_ts_ok = B_TRUE;
710 706 if (uinfo.iulp_wscale_ok)
711 707 tcp->tcp_snd_ws_ok = B_TRUE;
712 708 if (uinfo.iulp_sack == 2)
713 709 tcp->tcp_snd_sack_ok = B_TRUE;
714 710 if (uinfo.iulp_ecn_ok)
715 711 tcp->tcp_ecn_ok = B_TRUE;
716 712 } else {
717 713 /*
718 714 * Passive open.
719 715 *
720 716 * As above, the if check means that SACK can only be
721 717 * turned on by the metric only IRE.
722 718 */
723 719 if (uinfo.iulp_sack > 0) {
724 720 tcp->tcp_snd_sack_ok = B_TRUE;
725 721 }
726 722 }
727 723
728 724 /*
729 725 * XXX Note that currently, iulp_mtu can be as small as 68
730 726 * because of PMTUd. So tcp_mss may go to negative if combined
731 727 * length of all those options exceeds 28 bytes. But because
732 728 * of the tcp_mss_min check below, we may not have a problem if
733 729 * tcp_mss_min is of a reasonable value. The default is 1 so
734 730 * the negative problem still exists. And the check defeats PMTUd.
735 731 * In fact, if PMTUd finds that the MSS should be smaller than
736 732 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min
737 733 * value.
738 734 *
739 735 * We do not deal with that now. All those problems related to
740 736 * PMTUd will be fixed later.
741 737 */
742 738 ASSERT(uinfo.iulp_mtu != 0);
743 739 mss = tcp->tcp_initial_pmtu = uinfo.iulp_mtu;
744 740
745 741 /* Sanity check for MSS value. */
746 742 if (connp->conn_ipversion == IPV4_VERSION)
747 743 mss_max = tcps->tcps_mss_max_ipv4;
748 744 else
749 745 mss_max = tcps->tcps_mss_max_ipv6;
750 746
751 747 if (tcp->tcp_ipsec_overhead == 0)
752 748 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp);
753 749
754 750 mss -= tcp->tcp_ipsec_overhead;
755 751
756 752 if (mss < tcps->tcps_mss_min)
757 753 mss = tcps->tcps_mss_min;
758 754 if (mss > mss_max)
759 755 mss = mss_max;
760 756
761 757 /* Note that this is the maximum MSS, excluding all options. */
762 758 tcp->tcp_mss = mss;
763 759
764 760 /*
765 761 * Update the tcp connection with LSO capability.
766 762 */
767 763 tcp_update_lso(tcp, connp->conn_ixa);
768 764
769 765 /*
770 766 * Initialize the ISS here now that we have the full connection ID.
771 767 * The RFC 1948 method of initial sequence number generation requires
772 768 * knowledge of the full connection ID before setting the ISS.
773 769 */
774 770 tcp_iss_init(tcp);
775 771
776 772 tcp->tcp_loopback = (uinfo.iulp_loopback | uinfo.iulp_local);
777 773
778 774 /*
779 775 * Make sure that conn is not marked incipient
780 776 * for incoming connections. A blind
781 777 * removal of incipient flag is cheaper than
782 778 * check and removal.
783 779 */
784 780 mutex_enter(&connp->conn_lock);
785 781 connp->conn_state_flags &= ~CONN_INCIPIENT;
786 782 mutex_exit(&connp->conn_lock);
787 783 return (0);
788 784 }
789 785
790 786 /*
791 787 * tcp_clean_death / tcp_close_detached must not be called more than once
792 788 * on a tcp. Thus every function that potentially calls tcp_clean_death
793 789 * must check for the tcp state before calling tcp_clean_death.
794 790 * Eg. tcp_input_data, tcp_eager_kill, tcp_clean_death_wrapper,
795 791 * tcp_timer_handler, all check for the tcp state.
796 792 */
797 793 /* ARGSUSED */
798 794 void
799 795 tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2,
800 796 ip_recv_attr_t *dummy)
801 797 {
802 798 tcp_t *tcp = ((conn_t *)arg)->conn_tcp;
803 799
804 800 freemsg(mp);
805 801 if (tcp->tcp_state > TCPS_BOUND)
806 802 (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT);
807 803 }
808 804
809 805 /*
810 806 * We are dying for some reason. Try to do it gracefully. (May be called
811 807 * as writer.)
812 808 *
813 809 * Return -1 if the structure was not cleaned up (if the cleanup had to be
814 810 * done by a service procedure).
815 811 * TBD - Should the return value distinguish between the tcp_t being
816 812 * freed and it being reinitialized?
817 813 */
818 814 int
819 815 tcp_clean_death(tcp_t *tcp, int err)
820 816 {
821 817 mblk_t *mp;
822 818 queue_t *q;
823 819 conn_t *connp = tcp->tcp_connp;
824 820 tcp_stack_t *tcps = tcp->tcp_tcps;
825 821
826 822 if (tcp->tcp_fused)
827 823 tcp_unfuse(tcp);
828 824
829 825 if (tcp->tcp_linger_tid != 0 &&
830 826 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
831 827 tcp_stop_lingering(tcp);
832 828 }
833 829
834 830 ASSERT(tcp != NULL);
835 831 ASSERT((connp->conn_family == AF_INET &&
836 832 connp->conn_ipversion == IPV4_VERSION) ||
837 833 (connp->conn_family == AF_INET6 &&
838 834 (connp->conn_ipversion == IPV4_VERSION ||
839 835 connp->conn_ipversion == IPV6_VERSION)));
840 836
841 837 if (TCP_IS_DETACHED(tcp)) {
842 838 if (tcp->tcp_hard_binding) {
843 839 /*
844 840 * Its an eager that we are dealing with. We close the
845 841 * eager but in case a conn_ind has already gone to the
846 842 * listener, let tcp_accept_finish() send a discon_ind
847 843 * to the listener and drop the last reference. If the
848 844 * listener doesn't even know about the eager i.e. the
849 845 * conn_ind hasn't gone up, blow away the eager and drop
850 846 * the last reference as well. If the conn_ind has gone
851 847 * up, state should be BOUND. tcp_accept_finish
852 848 * will figure out that the connection has received a
853 849 * RST and will send a DISCON_IND to the application.
854 850 */
855 851 tcp_closei_local(tcp);
856 852 if (!tcp->tcp_tconnind_started) {
857 853 CONN_DEC_REF(connp);
858 854 } else {
859 855 tcp->tcp_state = TCPS_BOUND;
860 856 DTRACE_TCP6(state__change, void, NULL,
861 857 ip_xmit_attr_t *, connp->conn_ixa,
862 858 void, NULL, tcp_t *, tcp, void, NULL,
863 859 int32_t, TCPS_CLOSED);
864 860 }
865 861 } else {
866 862 tcp_close_detached(tcp);
867 863 }
868 864 return (0);
869 865 }
870 866
871 867 TCP_STAT(tcps, tcp_clean_death_nondetached);
872 868
873 869 /*
874 870 * The connection is dead. Decrement listener connection counter if
875 871 * necessary.
876 872 */
877 873 if (tcp->tcp_listen_cnt != NULL)
878 874 TCP_DECR_LISTEN_CNT(tcp);
879 875
880 876 /*
881 877 * When a connection is moved to TIME_WAIT state, the connection
882 878 * counter is already decremented. So no need to decrement here
883 879 * again. See SET_TIME_WAIT() macro.
884 880 */
885 881 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
886 882 tcp->tcp_state < TCPS_TIME_WAIT) {
887 883 TCPS_CONN_DEC(tcps);
888 884 }
889 885
890 886 q = connp->conn_rq;
891 887
892 888 /* Trash all inbound data */
893 889 if (!IPCL_IS_NONSTR(connp)) {
894 890 ASSERT(q != NULL);
895 891 flushq(q, FLUSHALL);
896 892 }
897 893
898 894 /*
899 895 * If we are at least part way open and there is error
900 896 * (err==0 implies no error)
901 897 * notify our client by a T_DISCON_IND.
902 898 */
903 899 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) {
904 900 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
905 901 !TCP_IS_SOCKET(tcp)) {
906 902 /*
907 903 * Send M_FLUSH according to TPI. Because sockets will
908 904 * (and must) ignore FLUSHR we do that only for TPI
909 905 * endpoints and sockets in STREAMS mode.
910 906 */
911 907 (void) putnextctl1(q, M_FLUSH, FLUSHR);
912 908 }
913 909 if (connp->conn_debug) {
914 910 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
915 911 "tcp_clean_death: discon err %d", err);
916 912 }
917 913 if (IPCL_IS_NONSTR(connp)) {
918 914 /* Direct socket, use upcall */
919 915 (*connp->conn_upcalls->su_disconnected)(
920 916 connp->conn_upper_handle, tcp->tcp_connid, err);
921 917 } else {
922 918 mp = mi_tpi_discon_ind(NULL, err, 0);
923 919 if (mp != NULL) {
924 920 putnext(q, mp);
925 921 } else {
926 922 if (connp->conn_debug) {
927 923 (void) strlog(TCP_MOD_ID, 0, 1,
928 924 SL_ERROR|SL_TRACE,
929 925 "tcp_clean_death, sending M_ERROR");
930 926 }
931 927 (void) putnextctl1(q, M_ERROR, EPROTO);
932 928 }
933 929 }
934 930 if (tcp->tcp_state <= TCPS_SYN_RCVD) {
935 931 /* SYN_SENT or SYN_RCVD */
936 932 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
937 933 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) {
938 934 /* ESTABLISHED or CLOSE_WAIT */
939 935 TCPS_BUMP_MIB(tcps, tcpEstabResets);
940 936 }
941 937 }
942 938
943 939 /*
944 940 * ESTABLISHED non-STREAMS eagers are not 'detached' because
945 941 * an upper handle is obtained when the SYN-ACK comes in. So it
946 942 * should receive the 'disconnected' upcall, but tcp_reinit should
947 943 * not be called since this is an eager.
948 944 */
949 945 if (tcp->tcp_listener != NULL && IPCL_IS_NONSTR(connp)) {
950 946 tcp_closei_local(tcp);
951 947 tcp->tcp_state = TCPS_BOUND;
952 948 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
953 949 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
954 950 int32_t, TCPS_CLOSED);
955 951 return (0);
956 952 }
957 953
958 954 tcp_reinit(tcp);
959 955 if (IPCL_IS_NONSTR(connp))
960 956 (void) tcp_do_unbind(connp);
961 957
962 958 return (-1);
963 959 }
964 960
965 961 /*
966 962 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout
967 963 * to expire, stop the wait and finish the close.
968 964 */
969 965 void
970 966 tcp_stop_lingering(tcp_t *tcp)
971 967 {
972 968 clock_t delta = 0;
973 969 tcp_stack_t *tcps = tcp->tcp_tcps;
974 970 conn_t *connp = tcp->tcp_connp;
975 971
976 972 tcp->tcp_linger_tid = 0;
977 973 if (tcp->tcp_state > TCPS_LISTEN) {
978 974 tcp_acceptor_hash_remove(tcp);
979 975 mutex_enter(&tcp->tcp_non_sq_lock);
980 976 if (tcp->tcp_flow_stopped) {
981 977 tcp_clrqfull(tcp);
982 978 }
983 979 mutex_exit(&tcp->tcp_non_sq_lock);
984 980
985 981 if (tcp->tcp_timer_tid != 0) {
986 982 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
987 983 tcp->tcp_timer_tid = 0;
988 984 }
989 985 /*
990 986 * Need to cancel those timers which will not be used when
991 987 * TCP is detached. This has to be done before the conn_wq
992 988 * is cleared.
993 989 */
994 990 tcp_timers_stop(tcp);
995 991
996 992 tcp->tcp_detached = B_TRUE;
997 993 connp->conn_rq = NULL;
998 994 connp->conn_wq = NULL;
999 995
1000 996 if (tcp->tcp_state == TCPS_TIME_WAIT) {
1001 997 tcp_time_wait_append(tcp);
1002 998 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
1003 999 goto finish;
1004 1000 }
1005 1001
1006 1002 /*
1007 1003 * If delta is zero the timer event wasn't executed and was
1008 1004 * successfully canceled. In this case we need to restart it
1009 1005 * with the minimal delta possible.
1010 1006 */
1011 1007 if (delta >= 0) {
1012 1008 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
1013 1009 delta ? delta : 1);
1014 1010 }
1015 1011 } else {
1016 1012 tcp_closei_local(tcp);
1017 1013 CONN_DEC_REF(connp);
1018 1014 }
1019 1015 finish:
1020 1016 tcp->tcp_detached = B_TRUE;
1021 1017 connp->conn_rq = NULL;
1022 1018 connp->conn_wq = NULL;
1023 1019
1024 1020 /* Signal closing thread that it can complete close */
1025 1021 mutex_enter(&tcp->tcp_closelock);
1026 1022 tcp->tcp_closed = 1;
1027 1023 cv_signal(&tcp->tcp_closecv);
1028 1024 mutex_exit(&tcp->tcp_closelock);
1029 1025
1030 1026 /* If we have an upper handle (socket), release it */
1031 1027 if (IPCL_IS_NONSTR(connp)) {
1032 1028 ASSERT(connp->conn_upper_handle != NULL);
1033 1029 (*connp->conn_upcalls->su_closed)(connp->conn_upper_handle);
1034 1030 connp->conn_upper_handle = NULL;
1035 1031 connp->conn_upcalls = NULL;
1036 1032 }
1037 1033 }
1038 1034
1039 1035 void
1040 1036 tcp_close_common(conn_t *connp, int flags)
1041 1037 {
1042 1038 tcp_t *tcp = connp->conn_tcp;
1043 1039 mblk_t *mp = &tcp->tcp_closemp;
1044 1040 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
1045 1041 mblk_t *bp;
1046 1042
1047 1043 ASSERT(connp->conn_ref >= 2);
1048 1044
1049 1045 /*
1050 1046 * Mark the conn as closing. ipsq_pending_mp_add will not
1051 1047 * add any mp to the pending mp list, after this conn has
1052 1048 * started closing.
1053 1049 */
1054 1050 mutex_enter(&connp->conn_lock);
1055 1051 connp->conn_state_flags |= CONN_CLOSING;
1056 1052 if (connp->conn_oper_pending_ill != NULL)
1057 1053 conn_ioctl_cleanup_reqd = B_TRUE;
1058 1054 CONN_INC_REF_LOCKED(connp);
1059 1055 mutex_exit(&connp->conn_lock);
1060 1056 tcp->tcp_closeflags = (uint8_t)flags;
1061 1057 ASSERT(connp->conn_ref >= 3);
1062 1058
1063 1059 /*
1064 1060 * tcp_closemp_used is used below without any protection of a lock
1065 1061 * as we don't expect any one else to use it concurrently at this
1066 1062 * point otherwise it would be a major defect.
1067 1063 */
1068 1064
1069 1065 if (mp->b_prev == NULL)
1070 1066 tcp->tcp_closemp_used = B_TRUE;
1071 1067 else
1072 1068 cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: "
1073 1069 "connp %p tcp %p\n", (void *)connp, (void *)tcp);
1074 1070
1075 1071 TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15);
1076 1072
1077 1073 /*
1078 1074 * Cleanup any queued ioctls here. This must be done before the wq/rq
1079 1075 * are re-written by tcp_close_output().
1080 1076 */
1081 1077 if (conn_ioctl_cleanup_reqd)
1082 1078 conn_ioctl_cleanup(connp);
1083 1079
1084 1080 /*
1085 1081 * As CONN_CLOSING is set, no further ioctls should be passed down to
1086 1082 * IP for this conn (see the guards in tcp_ioctl, tcp_wput_ioctl and
1087 1083 * tcp_wput_iocdata). If the ioctl was queued on an ipsq,
1088 1084 * conn_ioctl_cleanup should have found it and removed it. If the ioctl
1089 1085 * was still in flight at the time, we wait for it here. See comments
1090 1086 * for CONN_INC_IOCTLREF in ip.h for details.
1091 1087 */
1092 1088 mutex_enter(&connp->conn_lock);
1093 1089 while (connp->conn_ioctlref > 0)
1094 1090 cv_wait(&connp->conn_cv, &connp->conn_lock);
1095 1091 ASSERT(connp->conn_ioctlref == 0);
1096 1092 ASSERT(connp->conn_oper_pending_ill == NULL);
1097 1093 mutex_exit(&connp->conn_lock);
1098 1094
1099 1095 SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp,
1100 1096 NULL, tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1101 1097
1102 1098 /*
1103 1099 * For non-STREAMS sockets, the normal case is that the conn makes
1104 1100 * an upcall when it's finally closed, so there is no need to wait
1105 1101 * in the protocol. But in case of SO_LINGER the thread sleeps here
1106 1102 * so it can properly deal with the thread being interrupted.
1107 1103 */
1108 1104 if (IPCL_IS_NONSTR(connp) && connp->conn_linger == 0)
1109 1105 goto nowait;
1110 1106
1111 1107 mutex_enter(&tcp->tcp_closelock);
1112 1108 while (!tcp->tcp_closed) {
1113 1109 if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) {
1114 1110 /*
1115 1111 * The cv_wait_sig() was interrupted. We now do the
1116 1112 * following:
1117 1113 *
1118 1114 * 1) If the endpoint was lingering, we allow this
1119 1115 * to be interrupted by cancelling the linger timeout
1120 1116 * and closing normally.
1121 1117 *
1122 1118 * 2) Revert to calling cv_wait()
1123 1119 *
1124 1120 * We revert to using cv_wait() to avoid an
1125 1121 * infinite loop which can occur if the calling
1126 1122 * thread is higher priority than the squeue worker
1127 1123 * thread and is bound to the same cpu.
1128 1124 */
1129 1125 if (connp->conn_linger && connp->conn_lingertime > 0) {
1130 1126 mutex_exit(&tcp->tcp_closelock);
1131 1127 /* Entering squeue, bump ref count. */
1132 1128 CONN_INC_REF(connp);
1133 1129 bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL);
1134 1130 SQUEUE_ENTER_ONE(connp->conn_sqp, bp,
1135 1131 tcp_linger_interrupted, connp, NULL,
1136 1132 tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
1137 1133 mutex_enter(&tcp->tcp_closelock);
1138 1134 }
1139 1135 break;
1140 1136 }
1141 1137 }
1142 1138 while (!tcp->tcp_closed)
1143 1139 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock);
1144 1140 mutex_exit(&tcp->tcp_closelock);
1145 1141
1146 1142 /*
1147 1143 * In the case of listener streams that have eagers in the q or q0
1148 1144 * we wait for the eagers to drop their reference to us. conn_rq and
1149 1145 * conn_wq of the eagers point to our queues. By waiting for the
1150 1146 * refcnt to drop to 1, we are sure that the eagers have cleaned
1151 1147 * up their queue pointers and also dropped their references to us.
1152 1148 *
1153 1149 * For non-STREAMS sockets we do not have to wait here; the
1154 1150 * listener will instead make a su_closed upcall when the last
1155 1151 * reference is dropped.
1156 1152 */
1157 1153 if (tcp->tcp_wait_for_eagers && !IPCL_IS_NONSTR(connp)) {
1158 1154 mutex_enter(&connp->conn_lock);
1159 1155 while (connp->conn_ref != 1) {
1160 1156 cv_wait(&connp->conn_cv, &connp->conn_lock);
1161 1157 }
1162 1158 mutex_exit(&connp->conn_lock);
1163 1159 }
1164 1160
1165 1161 nowait:
1166 1162 connp->conn_cpid = NOPID;
1167 1163 }
1168 1164
1169 1165 /*
1170 1166 * Called by tcp_close() routine via squeue when lingering is
1171 1167 * interrupted by a signal.
1172 1168 */
1173 1169
1174 1170 /* ARGSUSED */
1175 1171 static void
1176 1172 tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
1177 1173 {
1178 1174 conn_t *connp = (conn_t *)arg;
1179 1175 tcp_t *tcp = connp->conn_tcp;
1180 1176
1181 1177 freeb(mp);
1182 1178 if (tcp->tcp_linger_tid != 0 &&
1183 1179 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
1184 1180 tcp_stop_lingering(tcp);
1185 1181 tcp->tcp_client_errno = EINTR;
1186 1182 }
1187 1183 }
1188 1184
1189 1185 /*
1190 1186 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp.
1191 1187 * Some stream heads get upset if they see these later on as anything but NULL.
1192 1188 */
1193 1189 void
1194 1190 tcp_close_mpp(mblk_t **mpp)
1195 1191 {
1196 1192 mblk_t *mp;
1197 1193
1198 1194 if ((mp = *mpp) != NULL) {
1199 1195 do {
1200 1196 mp->b_next = NULL;
1201 1197 mp->b_prev = NULL;
1202 1198 } while ((mp = mp->b_cont) != NULL);
1203 1199
1204 1200 mp = *mpp;
1205 1201 *mpp = NULL;
1206 1202 freemsg(mp);
1207 1203 }
1208 1204 }
1209 1205
1210 1206 /* Do detached close. */
1211 1207 void
1212 1208 tcp_close_detached(tcp_t *tcp)
1213 1209 {
1214 1210 if (tcp->tcp_fused)
1215 1211 tcp_unfuse(tcp);
1216 1212
1217 1213 /*
1218 1214 * Clustering code serializes TCP disconnect callbacks and
1219 1215 * cluster tcp list walks by blocking a TCP disconnect callback
1220 1216 * if a cluster tcp list walk is in progress. This ensures
1221 1217 * accurate accounting of TCPs in the cluster code even though
1222 1218 * the TCP list walk itself is not atomic.
1223 1219 */
1224 1220 tcp_closei_local(tcp);
1225 1221 CONN_DEC_REF(tcp->tcp_connp);
1226 1222 }
1227 1223
1228 1224 /*
1229 1225 * The tcp_t is going away. Remove it from all lists and set it
1230 1226 * to TCPS_CLOSED. The freeing up of memory is deferred until
1231 1227 * tcp_inactive. This is needed since a thread in tcp_rput might have
1232 1228 * done a CONN_INC_REF on this structure before it was removed from the
1233 1229 * hashes.
1234 1230 */
1235 1231 void
1236 1232 tcp_closei_local(tcp_t *tcp)
1237 1233 {
1238 1234 conn_t *connp = tcp->tcp_connp;
1239 1235 tcp_stack_t *tcps = tcp->tcp_tcps;
1240 1236 int32_t oldstate;
1241 1237
1242 1238 if (!TCP_IS_SOCKET(tcp))
1243 1239 tcp_acceptor_hash_remove(tcp);
1244 1240
1245 1241 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1246 1242 tcp->tcp_ibsegs = 0;
1247 1243 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1248 1244 tcp->tcp_obsegs = 0;
1249 1245
1250 1246 /*
1251 1247 * This can be called via tcp_time_wait_processing() if TCP gets a
1252 1248 * SYN with sequence number outside the TIME-WAIT connection's
1253 1249 * window. So we need to check for TIME-WAIT state here as the
1254 1250 * connection counter is already decremented. See SET_TIME_WAIT()
1255 1251 * macro
1256 1252 */
1257 1253 if (tcp->tcp_state >= TCPS_ESTABLISHED &&
1258 1254 tcp->tcp_state < TCPS_TIME_WAIT) {
1259 1255 TCPS_CONN_DEC(tcps);
1260 1256 }
1261 1257
1262 1258 /*
1263 1259 * If we are an eager connection hanging off a listener that
1264 1260 * hasn't formally accepted the connection yet, get off his
1265 1261 * list and blow off any data that we have accumulated.
1266 1262 */
1267 1263 if (tcp->tcp_listener != NULL) {
1268 1264 tcp_t *listener = tcp->tcp_listener;
1269 1265 mutex_enter(&listener->tcp_eager_lock);
1270 1266 /*
1271 1267 * tcp_tconnind_started == B_TRUE means that the
1272 1268 * conn_ind has already gone to listener. At
1273 1269 * this point, eager will be closed but we
1274 1270 * leave it in listeners eager list so that
1275 1271 * if listener decides to close without doing
1276 1272 * accept, we can clean this up. In tcp_tli_accept
1277 1273 * we take care of the case of accept on closed
1278 1274 * eager.
1279 1275 */
1280 1276 if (!tcp->tcp_tconnind_started) {
1281 1277 tcp_eager_unlink(tcp);
1282 1278 mutex_exit(&listener->tcp_eager_lock);
1283 1279 /*
1284 1280 * We don't want to have any pointers to the
1285 1281 * listener queue, after we have released our
1286 1282 * reference on the listener
1287 1283 */
1288 1284 ASSERT(tcp->tcp_detached);
1289 1285 connp->conn_rq = NULL;
1290 1286 connp->conn_wq = NULL;
1291 1287 CONN_DEC_REF(listener->tcp_connp);
1292 1288 } else {
1293 1289 mutex_exit(&listener->tcp_eager_lock);
1294 1290 }
1295 1291 }
1296 1292
1297 1293 /* Stop all the timers */
1298 1294 tcp_timers_stop(tcp);
1299 1295
1300 1296 if (tcp->tcp_state == TCPS_LISTEN) {
1301 1297 if (tcp->tcp_ip_addr_cache) {
1302 1298 kmem_free((void *)tcp->tcp_ip_addr_cache,
1303 1299 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t));
1304 1300 tcp->tcp_ip_addr_cache = NULL;
1305 1301 }
1306 1302 }
1307 1303
1308 1304 /* Decrement listerner connection counter if necessary. */
1309 1305 if (tcp->tcp_listen_cnt != NULL)
1310 1306 TCP_DECR_LISTEN_CNT(tcp);
1311 1307
1312 1308 mutex_enter(&tcp->tcp_non_sq_lock);
1313 1309 if (tcp->tcp_flow_stopped)
1314 1310 tcp_clrqfull(tcp);
1315 1311 mutex_exit(&tcp->tcp_non_sq_lock);
1316 1312
1317 1313 tcp_bind_hash_remove(tcp);
1318 1314 /*
1319 1315 * If the tcp_time_wait_collector (which runs outside the squeue)
1320 1316 * is trying to remove this tcp from the time wait list, we will
1321 1317 * block in tcp_time_wait_remove while trying to acquire the
1322 1318 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also
1323 1319 * requires the ipcl_hash_remove to be ordered after the
1324 1320 * tcp_time_wait_remove for the refcnt checks to work correctly.
1325 1321 */
1326 1322 if (tcp->tcp_state == TCPS_TIME_WAIT)
1327 1323 (void) tcp_time_wait_remove(tcp, NULL);
1328 1324 CL_INET_DISCONNECT(connp);
1329 1325 ipcl_hash_remove(connp);
1330 1326 oldstate = tcp->tcp_state;
1331 1327 tcp->tcp_state = TCPS_CLOSED;
1332 1328 /* Need to probe before ixa_cleanup() is called */
1333 1329 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1334 1330 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
1335 1331 int32_t, oldstate);
1336 1332 ixa_cleanup(connp->conn_ixa);
1337 1333
1338 1334 /*
1339 1335 * Mark the conn as CONDEMNED
1340 1336 */
1341 1337 mutex_enter(&connp->conn_lock);
1342 1338 connp->conn_state_flags |= CONN_CONDEMNED;
1343 1339 mutex_exit(&connp->conn_lock);
1344 1340
1345 1341 ASSERT(tcp->tcp_time_wait_next == NULL);
1346 1342 ASSERT(tcp->tcp_time_wait_prev == NULL);
1347 1343 ASSERT(tcp->tcp_time_wait_expire == 0);
1348 1344
1349 1345 tcp_ipsec_cleanup(tcp);
1350 1346 }
1351 1347
1352 1348 /*
1353 1349 * tcp is dying (called from ipcl_conn_destroy and error cases).
1354 1350 * Free the tcp_t in either case.
1355 1351 */
1356 1352 void
1357 1353 tcp_free(tcp_t *tcp)
1358 1354 {
1359 1355 mblk_t *mp;
1360 1356 conn_t *connp = tcp->tcp_connp;
1361 1357
1362 1358 ASSERT(tcp != NULL);
1363 1359 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL);
1364 1360
1365 1361 connp->conn_rq = NULL;
1366 1362 connp->conn_wq = NULL;
1367 1363
1368 1364 tcp_close_mpp(&tcp->tcp_xmit_head);
1369 1365 tcp_close_mpp(&tcp->tcp_reass_head);
1370 1366 if (tcp->tcp_rcv_list != NULL) {
1371 1367 /* Free b_next chain */
1372 1368 tcp_close_mpp(&tcp->tcp_rcv_list);
1373 1369 }
1374 1370 if ((mp = tcp->tcp_urp_mp) != NULL) {
1375 1371 freemsg(mp);
1376 1372 }
1377 1373 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1378 1374 freemsg(mp);
1379 1375 }
1380 1376
1381 1377 if (tcp->tcp_fused_sigurg_mp != NULL) {
1382 1378 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1383 1379 freeb(tcp->tcp_fused_sigurg_mp);
1384 1380 tcp->tcp_fused_sigurg_mp = NULL;
1385 1381 }
1386 1382
1387 1383 if (tcp->tcp_ordrel_mp != NULL) {
1388 1384 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1389 1385 freeb(tcp->tcp_ordrel_mp);
1390 1386 tcp->tcp_ordrel_mp = NULL;
1391 1387 }
1392 1388
1393 1389 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
1394 1390 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
1395 1391
1396 1392 if (tcp->tcp_hopopts != NULL) {
1397 1393 mi_free(tcp->tcp_hopopts);
1398 1394 tcp->tcp_hopopts = NULL;
1399 1395 tcp->tcp_hopoptslen = 0;
1400 1396 }
1401 1397 ASSERT(tcp->tcp_hopoptslen == 0);
1402 1398 if (tcp->tcp_dstopts != NULL) {
1403 1399 mi_free(tcp->tcp_dstopts);
1404 1400 tcp->tcp_dstopts = NULL;
1405 1401 tcp->tcp_dstoptslen = 0;
1406 1402 }
1407 1403 ASSERT(tcp->tcp_dstoptslen == 0);
1408 1404 if (tcp->tcp_rthdrdstopts != NULL) {
1409 1405 mi_free(tcp->tcp_rthdrdstopts);
1410 1406 tcp->tcp_rthdrdstopts = NULL;
1411 1407 tcp->tcp_rthdrdstoptslen = 0;
1412 1408 }
1413 1409 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
1414 1410 if (tcp->tcp_rthdr != NULL) {
1415 1411 mi_free(tcp->tcp_rthdr);
1416 1412 tcp->tcp_rthdr = NULL;
1417 1413 tcp->tcp_rthdrlen = 0;
1418 1414 }
1419 1415 ASSERT(tcp->tcp_rthdrlen == 0);
1420 1416
1421 1417 /*
1422 1418 * Following is really a blowing away a union.
1423 1419 * It happens to have exactly two members of identical size
1424 1420 * the following code is enough.
1425 1421 */
1426 1422 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1427 1423
1428 1424 /*
1429 1425 * If this is a non-STREAM socket still holding on to an upper
1430 1426 * handle, release it. As a result of fallback we might also see
1431 1427 * STREAMS based conns with upper handles, in which case there is
1432 1428 * nothing to do other than clearing the field.
1433 1429 */
1434 1430 if (connp->conn_upper_handle != NULL) {
1435 1431 if (IPCL_IS_NONSTR(connp)) {
1436 1432 (*connp->conn_upcalls->su_closed)(
1437 1433 connp->conn_upper_handle);
1438 1434 tcp->tcp_detached = B_TRUE;
1439 1435 }
1440 1436 connp->conn_upper_handle = NULL;
1441 1437 connp->conn_upcalls = NULL;
1442 1438 }
1443 1439 }
1444 1440
1445 1441 /*
1446 1442 * tcp_get_conn/tcp_free_conn
1447 1443 *
1448 1444 * tcp_get_conn is used to get a clean tcp connection structure.
1449 1445 * It tries to reuse the connections put on the freelist by the
1450 1446 * time_wait_collector failing which it goes to kmem_cache. This
1451 1447 * way has two benefits compared to just allocating from and
1452 1448 * freeing to kmem_cache.
1453 1449 * 1) The time_wait_collector can free (which includes the cleanup)
1454 1450 * outside the squeue. So when the interrupt comes, we have a clean
1455 1451 * connection sitting in the freelist. Obviously, this buys us
1456 1452 * performance.
1457 1453 *
1458 1454 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_input_listener
1459 1455 * has multiple disadvantages - tying up the squeue during alloc.
1460 1456 * But allocating the conn/tcp in IP land is also not the best since
1461 1457 * we can't check the 'q' and 'q0' which are protected by squeue and
1462 1458 * blindly allocate memory which might have to be freed here if we are
1463 1459 * not allowed to accept the connection. By using the freelist and
1464 1460 * putting the conn/tcp back in freelist, we don't pay a penalty for
1465 1461 * allocating memory without checking 'q/q0' and freeing it if we can't
1466 1462 * accept the connection.
1467 1463 *
1468 1464 * Care should be taken to put the conn back in the same squeue's freelist
1469 1465 * from which it was allocated. Best results are obtained if conn is
1470 1466 * allocated from listener's squeue and freed to the same. Time wait
1471 1467 * collector will free up the freelist is the connection ends up sitting
1472 1468 * there for too long.
1473 1469 */
1474 1470 void *
1475 1471 tcp_get_conn(void *arg, tcp_stack_t *tcps)
1476 1472 {
1477 1473 tcp_t *tcp = NULL;
1478 1474 conn_t *connp = NULL;
1479 1475 squeue_t *sqp = (squeue_t *)arg;
1480 1476 tcp_squeue_priv_t *tcp_time_wait;
1481 1477 netstack_t *ns;
1482 1478 mblk_t *tcp_rsrv_mp = NULL;
1483 1479
1484 1480 tcp_time_wait =
1485 1481 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP));
1486 1482
1487 1483 mutex_enter(&tcp_time_wait->tcp_time_wait_lock);
1488 1484 tcp = tcp_time_wait->tcp_free_list;
1489 1485 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0));
1490 1486 if (tcp != NULL) {
1491 1487 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next;
1492 1488 tcp_time_wait->tcp_free_list_cnt--;
1493 1489 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1494 1490 tcp->tcp_time_wait_next = NULL;
1495 1491 connp = tcp->tcp_connp;
1496 1492 connp->conn_flags |= IPCL_REUSED;
1497 1493
1498 1494 ASSERT(tcp->tcp_tcps == NULL);
1499 1495 ASSERT(connp->conn_netstack == NULL);
1500 1496 ASSERT(tcp->tcp_rsrv_mp != NULL);
1501 1497 ns = tcps->tcps_netstack;
1502 1498 netstack_hold(ns);
1503 1499 connp->conn_netstack = ns;
1504 1500 connp->conn_ixa->ixa_ipst = ns->netstack_ip;
1505 1501 tcp->tcp_tcps = tcps;
1506 1502 ipcl_globalhash_insert(connp);
1507 1503
1508 1504 connp->conn_ixa->ixa_notify_cookie = tcp;
1509 1505 ASSERT(connp->conn_ixa->ixa_notify == tcp_notify);
1510 1506 connp->conn_recv = tcp_input_data;
1511 1507 ASSERT(connp->conn_recvicmp == tcp_icmp_input);
1512 1508 ASSERT(connp->conn_verifyicmp == tcp_verifyicmp);
1513 1509 return ((void *)connp);
1514 1510 }
1515 1511 mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1516 1512 /*
1517 1513 * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until
1518 1514 * this conn_t/tcp_t is freed at ipcl_conn_destroy().
1519 1515 */
1520 1516 tcp_rsrv_mp = allocb(0, BPRI_HI);
1521 1517 if (tcp_rsrv_mp == NULL)
1522 1518 return (NULL);
1523 1519
1524 1520 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
1525 1521 tcps->tcps_netstack)) == NULL) {
1526 1522 freeb(tcp_rsrv_mp);
1527 1523 return (NULL);
1528 1524 }
1529 1525
1530 1526 tcp = connp->conn_tcp;
1531 1527 tcp->tcp_rsrv_mp = tcp_rsrv_mp;
1532 1528 mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL);
1533 1529
1534 1530 tcp->tcp_tcps = tcps;
1535 1531
1536 1532 connp->conn_recv = tcp_input_data;
1537 1533 connp->conn_recvicmp = tcp_icmp_input;
1538 1534 connp->conn_verifyicmp = tcp_verifyicmp;
1539 1535
1540 1536 /*
1541 1537 * Register tcp_notify to listen to capability changes detected by IP.
1542 1538 * This upcall is made in the context of the call to conn_ip_output
1543 1539 * thus it is inside the squeue.
1544 1540 */
1545 1541 connp->conn_ixa->ixa_notify = tcp_notify;
1546 1542 connp->conn_ixa->ixa_notify_cookie = tcp;
1547 1543
1548 1544 return ((void *)connp);
1549 1545 }
1550 1546
1551 1547 /*
1552 1548 * Handle connect to IPv4 destinations, including connections for AF_INET6
1553 1549 * sockets connecting to IPv4 mapped IPv6 destinations.
1554 1550 * Returns zero if OK, a positive errno, or a negative TLI error.
1555 1551 */
1556 1552 static int
1557 1553 tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport,
1558 1554 uint_t srcid)
1559 1555 {
1560 1556 ipaddr_t dstaddr = *dstaddrp;
1561 1557 uint16_t lport;
1562 1558 conn_t *connp = tcp->tcp_connp;
1563 1559 tcp_stack_t *tcps = tcp->tcp_tcps;
1564 1560 int error;
1565 1561
1566 1562 ASSERT(connp->conn_ipversion == IPV4_VERSION);
1567 1563
1568 1564 /* Check for attempt to connect to INADDR_ANY */
1569 1565 if (dstaddr == INADDR_ANY) {
1570 1566 /*
1571 1567 * SunOS 4.x and 4.3 BSD allow an application
1572 1568 * to connect a TCP socket to INADDR_ANY.
1573 1569 * When they do this, the kernel picks the
1574 1570 * address of one interface and uses it
1575 1571 * instead. The kernel usually ends up
1576 1572 * picking the address of the loopback
1577 1573 * interface. This is an undocumented feature.
1578 1574 * However, we provide the same thing here
1579 1575 * in order to have source and binary
1580 1576 * compatibility with SunOS 4.x.
1581 1577 * Update the T_CONN_REQ (sin/sin6) since it is used to
1582 1578 * generate the T_CONN_CON.
1583 1579 */
1584 1580 dstaddr = htonl(INADDR_LOOPBACK);
1585 1581 *dstaddrp = dstaddr;
1586 1582 }
1587 1583
1588 1584 /* Handle __sin6_src_id if socket not bound to an IP address */
1589 1585 if (srcid != 0 && connp->conn_laddr_v4 == INADDR_ANY) {
1590 1586 ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1591 1587 IPCL_ZONEID(connp), tcps->tcps_netstack);
1592 1588 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1593 1589 }
1594 1590
1595 1591 IN6_IPADDR_TO_V4MAPPED(dstaddr, &connp->conn_faddr_v6);
1596 1592 connp->conn_fport = dstport;
1597 1593
1598 1594 /*
1599 1595 * At this point the remote destination address and remote port fields
1600 1596 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1601 1597 * have to see which state tcp was in so we can take appropriate action.
1602 1598 */
1603 1599 if (tcp->tcp_state == TCPS_IDLE) {
1604 1600 /*
1605 1601 * We support a quick connect capability here, allowing
1606 1602 * clients to transition directly from IDLE to SYN_SENT
1607 1603 * tcp_bindi will pick an unused port, insert the connection
1608 1604 * in the bind hash and transition to BOUND state.
1609 1605 */
1610 1606 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1611 1607 tcp, B_TRUE);
1612 1608 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1613 1609 B_FALSE, B_FALSE);
1614 1610 if (lport == 0)
1615 1611 return (-TNOADDR);
1616 1612 }
1617 1613
1618 1614 /*
1619 1615 * Lookup the route to determine a source address and the uinfo.
1620 1616 * Setup TCP parameters based on the metrics/DCE.
1621 1617 */
1622 1618 error = tcp_set_destination(tcp);
1623 1619 if (error != 0)
1624 1620 return (error);
1625 1621
1626 1622 /*
1627 1623 * Don't let an endpoint connect to itself.
1628 1624 */
1629 1625 if (connp->conn_faddr_v4 == connp->conn_laddr_v4 &&
1630 1626 connp->conn_fport == connp->conn_lport)
1631 1627 return (-TBADADDR);
1632 1628
1633 1629 tcp->tcp_state = TCPS_SYN_SENT;
1634 1630
1635 1631 return (ipcl_conn_insert_v4(connp));
1636 1632 }
1637 1633
1638 1634 /*
1639 1635 * Handle connect to IPv6 destinations.
1640 1636 * Returns zero if OK, a positive errno, or a negative TLI error.
1641 1637 */
1642 1638 static int
1643 1639 tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport,
1644 1640 uint32_t flowinfo, uint_t srcid, uint32_t scope_id)
1645 1641 {
1646 1642 uint16_t lport;
1647 1643 conn_t *connp = tcp->tcp_connp;
1648 1644 tcp_stack_t *tcps = tcp->tcp_tcps;
1649 1645 int error;
1650 1646
1651 1647 ASSERT(connp->conn_family == AF_INET6);
1652 1648
1653 1649 /*
1654 1650 * If we're here, it means that the destination address is a native
1655 1651 * IPv6 address. Return an error if conn_ipversion is not IPv6. A
1656 1652 * reason why it might not be IPv6 is if the socket was bound to an
1657 1653 * IPv4-mapped IPv6 address.
1658 1654 */
1659 1655 if (connp->conn_ipversion != IPV6_VERSION)
1660 1656 return (-TBADADDR);
1661 1657
1662 1658 /*
1663 1659 * Interpret a zero destination to mean loopback.
1664 1660 * Update the T_CONN_REQ (sin/sin6) since it is used to
1665 1661 * generate the T_CONN_CON.
1666 1662 */
1667 1663 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp))
1668 1664 *dstaddrp = ipv6_loopback;
1669 1665
1670 1666 /* Handle __sin6_src_id if socket not bound to an IP address */
1671 1667 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
1672 1668 ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1673 1669 IPCL_ZONEID(connp), tcps->tcps_netstack);
1674 1670 connp->conn_saddr_v6 = connp->conn_laddr_v6;
1675 1671 }
1676 1672
1677 1673 /*
1678 1674 * Take care of the scope_id now.
1679 1675 */
1680 1676 if (scope_id != 0 && IN6_IS_ADDR_LINKSCOPE(dstaddrp)) {
1681 1677 connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1682 1678 connp->conn_ixa->ixa_scopeid = scope_id;
1683 1679 } else {
1684 1680 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
1685 1681 }
1686 1682
1687 1683 connp->conn_flowinfo = flowinfo;
1688 1684 connp->conn_faddr_v6 = *dstaddrp;
1689 1685 connp->conn_fport = dstport;
1690 1686
1691 1687 /*
1692 1688 * At this point the remote destination address and remote port fields
1693 1689 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1694 1690 * have to see which state tcp was in so we can take appropriate action.
1695 1691 */
1696 1692 if (tcp->tcp_state == TCPS_IDLE) {
1697 1693 /*
1698 1694 * We support a quick connect capability here, allowing
1699 1695 * clients to transition directly from IDLE to SYN_SENT
1700 1696 * tcp_bindi will pick an unused port, insert the connection
1701 1697 * in the bind hash and transition to BOUND state.
1702 1698 */
1703 1699 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1704 1700 tcp, B_TRUE);
1705 1701 lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
1706 1702 B_FALSE, B_FALSE);
1707 1703 if (lport == 0)
1708 1704 return (-TNOADDR);
1709 1705 }
1710 1706
1711 1707 /*
1712 1708 * Lookup the route to determine a source address and the uinfo.
1713 1709 * Setup TCP parameters based on the metrics/DCE.
1714 1710 */
1715 1711 error = tcp_set_destination(tcp);
1716 1712 if (error != 0)
1717 1713 return (error);
1718 1714
1719 1715 /*
1720 1716 * Don't let an endpoint connect to itself.
1721 1717 */
1722 1718 if (IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6, &connp->conn_laddr_v6) &&
1723 1719 connp->conn_fport == connp->conn_lport)
1724 1720 return (-TBADADDR);
1725 1721
1726 1722 tcp->tcp_state = TCPS_SYN_SENT;
1727 1723
1728 1724 return (ipcl_conn_insert_v6(connp));
1729 1725 }
1730 1726
1731 1727 /*
1732 1728 * Disconnect
1733 1729 * Note that unlike other functions this returns a positive tli error
1734 1730 * when it fails; it never returns an errno.
1735 1731 */
1736 1732 static int
1737 1733 tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum)
1738 1734 {
1739 1735 conn_t *lconnp;
1740 1736 tcp_stack_t *tcps = tcp->tcp_tcps;
1741 1737 conn_t *connp = tcp->tcp_connp;
1742 1738
1743 1739 /*
1744 1740 * Right now, upper modules pass down a T_DISCON_REQ to TCP,
1745 1741 * when the stream is in BOUND state. Do not send a reset,
1746 1742 * since the destination IP address is not valid, and it can
1747 1743 * be the initialized value of all zeros (broadcast address).
1748 1744 */
1749 1745 if (tcp->tcp_state <= TCPS_BOUND) {
1750 1746 if (connp->conn_debug) {
1751 1747 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
1752 1748 "tcp_disconnect: bad state, %d", tcp->tcp_state);
1753 1749 }
1754 1750 return (TOUTSTATE);
1755 1751 } else if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1756 1752 TCPS_CONN_DEC(tcps);
1757 1753 }
1758 1754
1759 1755 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) {
1760 1756
1761 1757 /*
1762 1758 * According to TPI, for non-listeners, ignore seqnum
1763 1759 * and disconnect.
1764 1760 * Following interpretation of -1 seqnum is historical
1765 1761 * and implied TPI ? (TPI only states that for T_CONN_IND,
1766 1762 * a valid seqnum should not be -1).
1767 1763 *
1768 1764 * -1 means disconnect everything
1769 1765 * regardless even on a listener.
1770 1766 */
1771 1767
1772 1768 int old_state = tcp->tcp_state;
1773 1769 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
1774 1770
1775 1771 /*
1776 1772 * The connection can't be on the tcp_time_wait_head list
1777 1773 * since it is not detached.
1778 1774 */
1779 1775 ASSERT(tcp->tcp_time_wait_next == NULL);
1780 1776 ASSERT(tcp->tcp_time_wait_prev == NULL);
1781 1777 ASSERT(tcp->tcp_time_wait_expire == 0);
1782 1778 /*
1783 1779 * If it used to be a listener, check to make sure no one else
1784 1780 * has taken the port before switching back to LISTEN state.
1785 1781 */
1786 1782 if (connp->conn_ipversion == IPV4_VERSION) {
1787 1783 lconnp = ipcl_lookup_listener_v4(connp->conn_lport,
1788 1784 connp->conn_laddr_v4, IPCL_ZONEID(connp), ipst);
1789 1785 } else {
1790 1786 uint_t ifindex = 0;
1791 1787
1792 1788 if (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET)
1793 1789 ifindex = connp->conn_ixa->ixa_scopeid;
1794 1790
1795 1791 /* Allow conn_bound_if listeners? */
1796 1792 lconnp = ipcl_lookup_listener_v6(connp->conn_lport,
1797 1793 &connp->conn_laddr_v6, ifindex, IPCL_ZONEID(connp),
1798 1794 ipst);
1799 1795 }
1800 1796 if (tcp->tcp_conn_req_max && lconnp == NULL) {
1801 1797 tcp->tcp_state = TCPS_LISTEN;
1802 1798 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1803 1799 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1804 1800 NULL, int32_t, old_state);
1805 1801 } else if (old_state > TCPS_BOUND) {
1806 1802 tcp->tcp_conn_req_max = 0;
1807 1803 tcp->tcp_state = TCPS_BOUND;
1808 1804 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
1809 1805 connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
1810 1806 NULL, int32_t, old_state);
1811 1807
1812 1808 /*
1813 1809 * If this end point is not going to become a listener,
1814 1810 * decrement the listener connection count if
1815 1811 * necessary. Note that we do not do this if it is
1816 1812 * going to be a listner (the above if case) since
1817 1813 * then it may remove the counter struct.
1818 1814 */
1819 1815 if (tcp->tcp_listen_cnt != NULL)
1820 1816 TCP_DECR_LISTEN_CNT(tcp);
1821 1817 }
1822 1818 if (lconnp != NULL)
1823 1819 CONN_DEC_REF(lconnp);
1824 1820 switch (old_state) {
1825 1821 case TCPS_SYN_SENT:
1826 1822 case TCPS_SYN_RCVD:
1827 1823 TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1828 1824 break;
1829 1825 case TCPS_ESTABLISHED:
1830 1826 case TCPS_CLOSE_WAIT:
1831 1827 TCPS_BUMP_MIB(tcps, tcpEstabResets);
1832 1828 break;
1833 1829 }
1834 1830
1835 1831 if (tcp->tcp_fused)
1836 1832 tcp_unfuse(tcp);
1837 1833
1838 1834 mutex_enter(&tcp->tcp_eager_lock);
1839 1835 if ((tcp->tcp_conn_req_cnt_q0 != 0) ||
1840 1836 (tcp->tcp_conn_req_cnt_q != 0)) {
1841 1837 tcp_eager_cleanup(tcp, 0);
1842 1838 }
1843 1839 mutex_exit(&tcp->tcp_eager_lock);
1844 1840
1845 1841 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt,
1846 1842 tcp->tcp_rnxt, TH_RST | TH_ACK);
1847 1843
1848 1844 tcp_reinit(tcp);
1849 1845
1850 1846 return (0);
1851 1847 } else if (!tcp_eager_blowoff(tcp, seqnum)) {
1852 1848 return (TBADSEQ);
1853 1849 }
1854 1850 return (0);
1855 1851 }
1856 1852
1857 1853 /*
1858 1854 * Our client hereby directs us to reject the connection request
1859 1855 * that tcp_input_listener() marked with 'seqnum'. Rejection consists
1860 1856 * of sending the appropriate RST, not an ICMP error.
1861 1857 */
1862 1858 void
1863 1859 tcp_disconnect(tcp_t *tcp, mblk_t *mp)
1864 1860 {
1865 1861 t_scalar_t seqnum;
1866 1862 int error;
1867 1863 conn_t *connp = tcp->tcp_connp;
1868 1864
1869 1865 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
1870 1866 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) {
1871 1867 tcp_err_ack(tcp, mp, TPROTO, 0);
1872 1868 return;
1873 1869 }
1874 1870 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number;
1875 1871 error = tcp_disconnect_common(tcp, seqnum);
1876 1872 if (error != 0)
1877 1873 tcp_err_ack(tcp, mp, error, 0);
1878 1874 else {
1879 1875 if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1880 1876 /* Send M_FLUSH according to TPI */
1881 1877 (void) putnextctl1(connp->conn_rq, M_FLUSH, FLUSHRW);
1882 1878 }
1883 1879 mp = mi_tpi_ok_ack_alloc(mp);
1884 1880 if (mp != NULL)
1885 1881 putnext(connp->conn_rq, mp);
1886 1882 }
1887 1883 }
1888 1884
1889 1885 /*
1890 1886 * Handle reinitialization of a tcp structure.
1891 1887 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE.
1892 1888 */
1893 1889 static void
1894 1890 tcp_reinit(tcp_t *tcp)
1895 1891 {
1896 1892 mblk_t *mp;
1897 1893 tcp_stack_t *tcps = tcp->tcp_tcps;
1898 1894 conn_t *connp = tcp->tcp_connp;
1899 1895 int32_t oldstate;
1900 1896
1901 1897 /* tcp_reinit should never be called for detached tcp_t's */
1902 1898 ASSERT(tcp->tcp_listener == NULL);
1903 1899 ASSERT((connp->conn_family == AF_INET &&
1904 1900 connp->conn_ipversion == IPV4_VERSION) ||
1905 1901 (connp->conn_family == AF_INET6 &&
1906 1902 (connp->conn_ipversion == IPV4_VERSION ||
1907 1903 connp->conn_ipversion == IPV6_VERSION)));
1908 1904
1909 1905 /* Cancel outstanding timers */
1910 1906 tcp_timers_stop(tcp);
1911 1907
1912 1908 /*
1913 1909 * Reset everything in the state vector, after updating global
1914 1910 * MIB data from instance counters.
1915 1911 */
1916 1912 TCPS_UPDATE_MIB(tcps, tcpHCInSegs, tcp->tcp_ibsegs);
1917 1913 tcp->tcp_ibsegs = 0;
1918 1914 TCPS_UPDATE_MIB(tcps, tcpHCOutSegs, tcp->tcp_obsegs);
1919 1915 tcp->tcp_obsegs = 0;
1920 1916
1921 1917 tcp_close_mpp(&tcp->tcp_xmit_head);
1922 1918 if (tcp->tcp_snd_zcopy_aware)
1923 1919 tcp_zcopy_notify(tcp);
1924 1920 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL;
1925 1921 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0;
1926 1922 mutex_enter(&tcp->tcp_non_sq_lock);
1927 1923 if (tcp->tcp_flow_stopped &&
1928 1924 TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1929 1925 tcp_clrqfull(tcp);
1930 1926 }
1931 1927 mutex_exit(&tcp->tcp_non_sq_lock);
1932 1928 tcp_close_mpp(&tcp->tcp_reass_head);
1933 1929 tcp->tcp_reass_tail = NULL;
1934 1930 if (tcp->tcp_rcv_list != NULL) {
1935 1931 /* Free b_next chain */
1936 1932 tcp_close_mpp(&tcp->tcp_rcv_list);
1937 1933 tcp->tcp_rcv_last_head = NULL;
1938 1934 tcp->tcp_rcv_last_tail = NULL;
1939 1935 tcp->tcp_rcv_cnt = 0;
1940 1936 }
1941 1937 tcp->tcp_rcv_last_tail = NULL;
1942 1938
1943 1939 if ((mp = tcp->tcp_urp_mp) != NULL) {
1944 1940 freemsg(mp);
1945 1941 tcp->tcp_urp_mp = NULL;
1946 1942 }
1947 1943 if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
1948 1944 freemsg(mp);
1949 1945 tcp->tcp_urp_mark_mp = NULL;
1950 1946 }
1951 1947 if (tcp->tcp_fused_sigurg_mp != NULL) {
1952 1948 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1953 1949 freeb(tcp->tcp_fused_sigurg_mp);
1954 1950 tcp->tcp_fused_sigurg_mp = NULL;
1955 1951 }
1956 1952 if (tcp->tcp_ordrel_mp != NULL) {
1957 1953 ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1958 1954 freeb(tcp->tcp_ordrel_mp);
1959 1955 tcp->tcp_ordrel_mp = NULL;
1960 1956 }
1961 1957
1962 1958 /*
1963 1959 * Following is a union with two members which are
1964 1960 * identical types and size so the following cleanup
1965 1961 * is enough.
1966 1962 */
1967 1963 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
1968 1964
1969 1965 CL_INET_DISCONNECT(connp);
1970 1966
1971 1967 /*
1972 1968 * The connection can't be on the tcp_time_wait_head list
1973 1969 * since it is not detached.
1974 1970 */
1975 1971 ASSERT(tcp->tcp_time_wait_next == NULL);
1976 1972 ASSERT(tcp->tcp_time_wait_prev == NULL);
1977 1973 ASSERT(tcp->tcp_time_wait_expire == 0);
1978 1974
1979 1975 /*
1980 1976 * Reset/preserve other values
1981 1977 */
1982 1978 tcp_reinit_values(tcp);
1983 1979 ipcl_hash_remove(connp);
1984 1980 /* Note that ixa_cred gets cleared in ixa_cleanup */
1985 1981 ixa_cleanup(connp->conn_ixa);
1986 1982 tcp_ipsec_cleanup(tcp);
1987 1983
1988 1984 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
1989 1985 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
1990 1986 oldstate = tcp->tcp_state;
1991 1987
1992 1988 if (tcp->tcp_conn_req_max != 0) {
1993 1989 /*
1994 1990 * This is the case when a TLI program uses the same
1995 1991 * transport end point to accept a connection. This
1996 1992 * makes the TCP both a listener and acceptor. When
1997 1993 * this connection is closed, we need to set the state
1998 1994 * back to TCPS_LISTEN. Make sure that the eager list
1999 1995 * is reinitialized.
2000 1996 *
2001 1997 * Note that this stream is still bound to the four
2002 1998 * tuples of the previous connection in IP. If a new
2003 1999 * SYN with different foreign address comes in, IP will
2004 2000 * not find it and will send it to the global queue. In
2005 2001 * the global queue, TCP will do a tcp_lookup_listener()
2006 2002 * to find this stream. This works because this stream
2007 2003 * is only removed from connected hash.
2008 2004 *
2009 2005 */
2010 2006 tcp->tcp_state = TCPS_LISTEN;
2011 2007 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
2012 2008 tcp->tcp_eager_next_drop_q0 = tcp;
2013 2009 tcp->tcp_eager_prev_drop_q0 = tcp;
2014 2010 /*
2015 2011 * Initially set conn_recv to tcp_input_listener_unbound to try
2016 2012 * to pick a good squeue for the listener when the first SYN
2017 2013 * arrives. tcp_input_listener_unbound sets it to
2018 2014 * tcp_input_listener on that first SYN.
2019 2015 */
2020 2016 connp->conn_recv = tcp_input_listener_unbound;
2021 2017
2022 2018 connp->conn_proto = IPPROTO_TCP;
2023 2019 connp->conn_faddr_v6 = ipv6_all_zeros;
2024 2020 connp->conn_fport = 0;
2025 2021
2026 2022 (void) ipcl_bind_insert(connp);
2027 2023 } else {
2028 2024 tcp->tcp_state = TCPS_BOUND;
2029 2025 }
2030 2026
2031 2027 /*
2032 2028 * Initialize to default values
2033 2029 */
2034 2030 tcp_init_values(tcp, NULL);
2035 2031
2036 2032 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
2037 2033 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
2038 2034 int32_t, oldstate);
2039 2035
2040 2036 ASSERT(tcp->tcp_ptpbhn != NULL);
2041 2037 tcp->tcp_rwnd = connp->conn_rcvbuf;
2042 2038 tcp->tcp_mss = connp->conn_ipversion != IPV4_VERSION ?
2043 2039 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4;
2044 2040 }
2045 2041
2046 2042 /*
2047 2043 * Force values to zero that need be zero.
2048 2044 * Do not touch values asociated with the BOUND or LISTEN state
2049 2045 * since the connection will end up in that state after the reinit.
2050 2046 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t
2051 2047 * structure!
2052 2048 */
2053 2049 static void
2054 2050 tcp_reinit_values(tcp)
2055 2051 tcp_t *tcp;
2056 2052 {
2057 2053 tcp_stack_t *tcps = tcp->tcp_tcps;
2058 2054 conn_t *connp = tcp->tcp_connp;
2059 2055
2060 2056 #ifndef lint
2061 2057 #define DONTCARE(x)
2062 2058 #define PRESERVE(x)
2063 2059 #else
2064 2060 #define DONTCARE(x) ((x) = (x))
2065 2061 #define PRESERVE(x) ((x) = (x))
2066 2062 #endif /* lint */
2067 2063
2068 2064 PRESERVE(tcp->tcp_bind_hash_port);
2069 2065 PRESERVE(tcp->tcp_bind_hash);
2070 2066 PRESERVE(tcp->tcp_ptpbhn);
2071 2067 PRESERVE(tcp->tcp_acceptor_hash);
2072 2068 PRESERVE(tcp->tcp_ptpahn);
2073 2069
2074 2070 /* Should be ASSERT NULL on these with new code! */
2075 2071 ASSERT(tcp->tcp_time_wait_next == NULL);
2076 2072 ASSERT(tcp->tcp_time_wait_prev == NULL);
2077 2073 ASSERT(tcp->tcp_time_wait_expire == 0);
2078 2074 PRESERVE(tcp->tcp_state);
2079 2075 PRESERVE(connp->conn_rq);
2080 2076 PRESERVE(connp->conn_wq);
2081 2077
2082 2078 ASSERT(tcp->tcp_xmit_head == NULL);
2083 2079 ASSERT(tcp->tcp_xmit_last == NULL);
2084 2080 ASSERT(tcp->tcp_unsent == 0);
2085 2081 ASSERT(tcp->tcp_xmit_tail == NULL);
2086 2082 ASSERT(tcp->tcp_xmit_tail_unsent == 0);
2087 2083
2088 2084 tcp->tcp_snxt = 0; /* Displayed in mib */
2089 2085 tcp->tcp_suna = 0; /* Displayed in mib */
2090 2086 tcp->tcp_swnd = 0;
2091 2087 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_process_options */
2092 2088
2093 2089 ASSERT(tcp->tcp_ibsegs == 0);
2094 2090 ASSERT(tcp->tcp_obsegs == 0);
2095 2091
2096 2092 if (connp->conn_ht_iphc != NULL) {
2097 2093 kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
2098 2094 connp->conn_ht_iphc = NULL;
2099 2095 connp->conn_ht_iphc_allocated = 0;
2100 2096 connp->conn_ht_iphc_len = 0;
2101 2097 connp->conn_ht_ulp = NULL;
2102 2098 connp->conn_ht_ulp_len = 0;
2103 2099 tcp->tcp_ipha = NULL;
2104 2100 tcp->tcp_ip6h = NULL;
2105 2101 tcp->tcp_tcpha = NULL;
2106 2102 }
2107 2103
2108 2104 /* We clear any IP_OPTIONS and extension headers */
2109 2105 ip_pkt_free(&connp->conn_xmit_ipp);
2110 2106
2111 2107 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */
2112 2108 DONTCARE(tcp->tcp_ipha);
2113 2109 DONTCARE(tcp->tcp_ip6h);
2114 2110 DONTCARE(tcp->tcp_tcpha);
2115 2111 tcp->tcp_valid_bits = 0;
2116 2112
2117 2113 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */
2118 2114 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */
2119 2115 tcp->tcp_last_rcv_lbolt = 0;
2120 2116
2121 2117 tcp->tcp_init_cwnd = 0;
2122 2118
2123 2119 tcp->tcp_urp_last_valid = 0;
2124 2120 tcp->tcp_hard_binding = 0;
2125 2121
2126 2122 tcp->tcp_fin_acked = 0;
2127 2123 tcp->tcp_fin_rcvd = 0;
2128 2124 tcp->tcp_fin_sent = 0;
2129 2125 tcp->tcp_ordrel_done = 0;
2130 2126
2131 2127 tcp->tcp_detached = 0;
2132 2128
2133 2129 tcp->tcp_snd_ws_ok = B_FALSE;
2134 2130 tcp->tcp_snd_ts_ok = B_FALSE;
2135 2131 tcp->tcp_zero_win_probe = 0;
2136 2132
2137 2133 tcp->tcp_loopback = 0;
2138 2134 tcp->tcp_localnet = 0;
2139 2135 tcp->tcp_syn_defense = 0;
2140 2136 tcp->tcp_set_timer = 0;
2141 2137
2142 2138 tcp->tcp_active_open = 0;
2143 2139 tcp->tcp_rexmit = B_FALSE;
2144 2140 tcp->tcp_xmit_zc_clean = B_FALSE;
2145 2141
2146 2142 tcp->tcp_snd_sack_ok = B_FALSE;
2147 2143 tcp->tcp_hwcksum = B_FALSE;
2148 2144
2149 2145 DONTCARE(tcp->tcp_maxpsz_multiplier); /* Init in tcp_init_values */
2150 2146
2151 2147 tcp->tcp_conn_def_q0 = 0;
2152 2148 tcp->tcp_ip_forward_progress = B_FALSE;
2153 2149 tcp->tcp_ecn_ok = B_FALSE;
2154 2150
2155 2151 tcp->tcp_cwr = B_FALSE;
2156 2152 tcp->tcp_ecn_echo_on = B_FALSE;
2157 2153 tcp->tcp_is_wnd_shrnk = B_FALSE;
2158 2154
2159 2155 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
2160 2156 bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
2161 2157
2162 2158 tcp->tcp_rcv_ws = 0;
2163 2159 tcp->tcp_snd_ws = 0;
2164 2160 tcp->tcp_ts_recent = 0;
2165 2161 tcp->tcp_rnxt = 0; /* Displayed in mib */
2166 2162 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */
2167 2163 tcp->tcp_initial_pmtu = 0;
2168 2164
2169 2165 ASSERT(tcp->tcp_reass_head == NULL);
2170 2166 ASSERT(tcp->tcp_reass_tail == NULL);
2171 2167
2172 2168 tcp->tcp_cwnd_cnt = 0;
2173 2169
2174 2170 ASSERT(tcp->tcp_rcv_list == NULL);
2175 2171 ASSERT(tcp->tcp_rcv_last_head == NULL);
2176 2172 ASSERT(tcp->tcp_rcv_last_tail == NULL);
2177 2173 ASSERT(tcp->tcp_rcv_cnt == 0);
2178 2174
2179 2175 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_set_destination */
2180 2176 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */
2181 2177 tcp->tcp_csuna = 0;
2182 2178
2183 2179 tcp->tcp_rto = 0; /* Displayed in MIB */
2184 2180 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */
2185 2181 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */
2186 2182 tcp->tcp_rtt_update = 0;
2187 2183
2188 2184 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2189 2185 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
2190 2186
2191 2187 tcp->tcp_rack = 0; /* Displayed in mib */
2192 2188 tcp->tcp_rack_cnt = 0;
2193 2189 tcp->tcp_rack_cur_max = 0;
2194 2190 tcp->tcp_rack_abs_max = 0;
2195 2191
2196 2192 tcp->tcp_max_swnd = 0;
2197 2193
2198 2194 ASSERT(tcp->tcp_listener == NULL);
2199 2195
2200 2196 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */
2201 2197 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */
2202 2198 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */
2203 2199 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */
2204 2200
2205 2201 ASSERT(tcp->tcp_conn_req_cnt_q == 0);
2206 2202 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0);
2207 2203 PRESERVE(tcp->tcp_conn_req_max);
2208 2204 PRESERVE(tcp->tcp_conn_req_seqnum);
2209 2205
2210 2206 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */
2211 2207 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */
2212 2208 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */
2213 2209 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */
2214 2210
2215 2211 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */
2216 2212 ASSERT(tcp->tcp_urp_mp == NULL);
2217 2213 ASSERT(tcp->tcp_urp_mark_mp == NULL);
2218 2214 ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
2219 2215
2220 2216 ASSERT(tcp->tcp_eager_next_q == NULL);
2221 2217 ASSERT(tcp->tcp_eager_last_q == NULL);
2222 2218 ASSERT((tcp->tcp_eager_next_q0 == NULL &&
2223 2219 tcp->tcp_eager_prev_q0 == NULL) ||
2224 2220 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0);
2225 2221 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL);
2226 2222
2227 2223 ASSERT((tcp->tcp_eager_next_drop_q0 == NULL &&
2228 2224 tcp->tcp_eager_prev_drop_q0 == NULL) ||
2229 2225 tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0);
2230 2226
2231 2227 DONTCARE(tcp->tcp_ka_rinterval); /* Init in tcp_init_values */
2232 2228 DONTCARE(tcp->tcp_ka_abort_thres); /* Init in tcp_init_values */
2233 2229 DONTCARE(tcp->tcp_ka_cnt); /* Init in tcp_init_values */
2234 2230
2235 2231 tcp->tcp_client_errno = 0;
2236 2232
2237 2233 DONTCARE(connp->conn_sum); /* Init in tcp_init_values */
2238 2234
2239 2235 connp->conn_faddr_v6 = ipv6_all_zeros; /* Displayed in MIB */
2240 2236
2241 2237 PRESERVE(connp->conn_bound_addr_v6);
2242 2238 tcp->tcp_last_sent_len = 0;
2243 2239 tcp->tcp_dupack_cnt = 0;
2244 2240
2245 2241 connp->conn_fport = 0; /* Displayed in MIB */
2246 2242 PRESERVE(connp->conn_lport);
2247 2243
2248 2244 PRESERVE(tcp->tcp_acceptor_lockp);
2249 2245
2250 2246 ASSERT(tcp->tcp_ordrel_mp == NULL);
2251 2247 PRESERVE(tcp->tcp_acceptor_id);
2252 2248 DONTCARE(tcp->tcp_ipsec_overhead);
2253 2249
2254 2250 PRESERVE(connp->conn_family);
2255 2251 /* Remove any remnants of mapped address binding */
2256 2252 if (connp->conn_family == AF_INET6) {
2257 2253 connp->conn_ipversion = IPV6_VERSION;
2258 2254 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2259 2255 } else {
2260 2256 connp->conn_ipversion = IPV4_VERSION;
2261 2257 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2262 2258 }
2263 2259
2264 2260 connp->conn_bound_if = 0;
2265 2261 connp->conn_recv_ancillary.crb_all = 0;
2266 2262 tcp->tcp_recvifindex = 0;
2267 2263 tcp->tcp_recvhops = 0;
2268 2264 tcp->tcp_closed = 0;
2269 2265 if (tcp->tcp_hopopts != NULL) {
2270 2266 mi_free(tcp->tcp_hopopts);
2271 2267 tcp->tcp_hopopts = NULL;
2272 2268 tcp->tcp_hopoptslen = 0;
2273 2269 }
2274 2270 ASSERT(tcp->tcp_hopoptslen == 0);
2275 2271 if (tcp->tcp_dstopts != NULL) {
2276 2272 mi_free(tcp->tcp_dstopts);
2277 2273 tcp->tcp_dstopts = NULL;
2278 2274 tcp->tcp_dstoptslen = 0;
2279 2275 }
2280 2276 ASSERT(tcp->tcp_dstoptslen == 0);
2281 2277 if (tcp->tcp_rthdrdstopts != NULL) {
2282 2278 mi_free(tcp->tcp_rthdrdstopts);
2283 2279 tcp->tcp_rthdrdstopts = NULL;
2284 2280 tcp->tcp_rthdrdstoptslen = 0;
2285 2281 }
2286 2282 ASSERT(tcp->tcp_rthdrdstoptslen == 0);
2287 2283 if (tcp->tcp_rthdr != NULL) {
2288 2284 mi_free(tcp->tcp_rthdr);
2289 2285 tcp->tcp_rthdr = NULL;
2290 2286 tcp->tcp_rthdrlen = 0;
2291 2287 }
2292 2288 ASSERT(tcp->tcp_rthdrlen == 0);
2293 2289
2294 2290 /* Reset fusion-related fields */
2295 2291 tcp->tcp_fused = B_FALSE;
2296 2292 tcp->tcp_unfusable = B_FALSE;
2297 2293 tcp->tcp_fused_sigurg = B_FALSE;
2298 2294 tcp->tcp_loopback_peer = NULL;
2299 2295
2300 2296 tcp->tcp_lso = B_FALSE;
2301 2297
2302 2298 tcp->tcp_in_ack_unsent = 0;
2303 2299 tcp->tcp_cork = B_FALSE;
2304 2300 tcp->tcp_tconnind_started = B_FALSE;
2305 2301
2306 2302 PRESERVE(tcp->tcp_squeue_bytes);
2307 2303
2308 2304 tcp->tcp_closemp_used = B_FALSE;
2309 2305
2310 2306 PRESERVE(tcp->tcp_rsrv_mp);
2311 2307 PRESERVE(tcp->tcp_rsrv_mp_lock);
2312 2308
2313 2309 #ifdef DEBUG
2314 2310 DONTCARE(tcp->tcmp_stk[0]);
2315 2311 #endif
2316 2312
2317 2313 PRESERVE(tcp->tcp_connid);
2318 2314
2319 2315 ASSERT(tcp->tcp_listen_cnt == NULL);
2320 2316 ASSERT(tcp->tcp_reass_tid == 0);
2321 2317
2322 2318 #undef DONTCARE
2323 2319 #undef PRESERVE
2324 2320 }
2325 2321
2326 2322 /*
2327 2323 * Initialize the various fields in tcp_t. If parent (the listener) is non
2328 2324 * NULL, certain values will be inheritted from it.
2329 2325 */
2330 2326 void
2331 2327 tcp_init_values(tcp_t *tcp, tcp_t *parent)
2332 2328 {
2333 2329 tcp_stack_t *tcps = tcp->tcp_tcps;
2334 2330 conn_t *connp = tcp->tcp_connp;
2335 2331 clock_t rto;
2336 2332
2337 2333 ASSERT((connp->conn_family == AF_INET &&
2338 2334 connp->conn_ipversion == IPV4_VERSION) ||
2339 2335 (connp->conn_family == AF_INET6 &&
2340 2336 (connp->conn_ipversion == IPV4_VERSION ||
2341 2337 connp->conn_ipversion == IPV6_VERSION)));
2342 2338
2343 2339 if (parent == NULL) {
2344 2340 tcp->tcp_naglim = tcps->tcps_naglim_def;
2345 2341
2346 2342 tcp->tcp_rto_initial = tcps->tcps_rexmit_interval_initial;
2347 2343 tcp->tcp_rto_min = tcps->tcps_rexmit_interval_min;
2348 2344 tcp->tcp_rto_max = tcps->tcps_rexmit_interval_max;
2349 2345
2350 2346 tcp->tcp_first_ctimer_threshold =
2351 2347 tcps->tcps_ip_notify_cinterval;
2352 2348 tcp->tcp_second_ctimer_threshold =
2353 2349 tcps->tcps_ip_abort_cinterval;
2354 2350 tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval;
2355 2351 tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval;
2356 2352
2357 2353 tcp->tcp_fin_wait_2_flush_interval =
2358 2354 tcps->tcps_fin_wait_2_flush_interval;
2359 2355
2360 2356 tcp->tcp_ka_interval = tcps->tcps_keepalive_interval;
2361 2357 tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval;
2362 2358 tcp->tcp_ka_cnt = 0;
2363 2359 tcp->tcp_ka_rinterval = 0;
2364 2360
2365 2361 /*
2366 2362 * Default value of tcp_init_cwnd is 0, so no need to set here
2367 2363 * if parent is NULL. But we need to inherit it from parent.
2368 2364 */
2369 2365 } else {
2370 2366 /* Inherit various TCP parameters from the parent. */
2371 2367 tcp->tcp_naglim = parent->tcp_naglim;
2372 2368
2373 2369 tcp->tcp_rto_initial = parent->tcp_rto_initial;
2374 2370 tcp->tcp_rto_min = parent->tcp_rto_min;
2375 2371 tcp->tcp_rto_max = parent->tcp_rto_max;
2376 2372
2377 2373 tcp->tcp_first_ctimer_threshold =
2378 2374 parent->tcp_first_ctimer_threshold;
2379 2375 tcp->tcp_second_ctimer_threshold =
2380 2376 parent->tcp_second_ctimer_threshold;
2381 2377 tcp->tcp_first_timer_threshold =
2382 2378 parent->tcp_first_timer_threshold;
2383 2379 tcp->tcp_second_timer_threshold =
2384 2380 parent->tcp_second_timer_threshold;
2385 2381
2386 2382 tcp->tcp_fin_wait_2_flush_interval =
2387 2383 parent->tcp_fin_wait_2_flush_interval;
2388 2384
2389 2385 tcp->tcp_ka_interval = parent->tcp_ka_interval;
2390 2386 tcp->tcp_ka_abort_thres = parent->tcp_ka_abort_thres;
2391 2387 tcp->tcp_ka_cnt = parent->tcp_ka_cnt;
2392 2388 tcp->tcp_ka_rinterval = parent->tcp_ka_rinterval;
2393 2389
2394 2390 tcp->tcp_init_cwnd = parent->tcp_init_cwnd;
2395 2391 }
2396 2392
2397 2393 /*
2398 2394 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
2399 2395 * will be close to tcp_rexmit_interval_initial. By doing this, we
2400 2396 * allow the algorithm to adjust slowly to large fluctuations of RTT
2401 2397 * during first few transmissions of a connection as seen in slow
2402 2398 * links.
2403 2399 */
2404 2400 tcp->tcp_rtt_sa = tcp->tcp_rto_initial << 2;
2405 2401 tcp->tcp_rtt_sd = tcp->tcp_rto_initial >> 1;
2406 2402 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd +
2407 2403 tcps->tcps_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) +
2408 2404 tcps->tcps_conn_grace_period;
2409 2405 TCP_SET_RTO(tcp, rto);
2410 2406
2411 2407 tcp->tcp_timer_backoff = 0;
2412 2408 tcp->tcp_ms_we_have_waited = 0;
2413 2409 tcp->tcp_last_recv_time = ddi_get_lbolt();
2414 2410 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_;
2415 2411 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
2416 2412 tcp->tcp_snd_burst = TCP_CWND_INFINITE;
2417 2413
2418 2414 tcp->tcp_maxpsz_multiplier = tcps->tcps_maxpsz_multiplier;
2419 2415
2420 2416 /* NOTE: ISS is now set in tcp_set_destination(). */
2421 2417
2422 2418 /* Reset fusion-related fields */
2423 2419 tcp->tcp_fused = B_FALSE;
2424 2420 tcp->tcp_unfusable = B_FALSE;
2425 2421 tcp->tcp_fused_sigurg = B_FALSE;
2426 2422 tcp->tcp_loopback_peer = NULL;
2427 2423
2428 2424 /* We rebuild the header template on the next connect/conn_request */
2429 2425
2430 2426 connp->conn_mlp_type = mlptSingle;
2431 2427
2432 2428 /*
2433 2429 * Init the window scale to the max so tcp_rwnd_set() won't pare
2434 2430 * down tcp_rwnd. tcp_set_destination() will set the right value later.
2435 2431 */
2436 2432 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
2437 2433 tcp->tcp_rwnd = connp->conn_rcvbuf;
2438 2434
2439 2435 tcp->tcp_cork = B_FALSE;
2440 2436 /*
2441 2437 * Init the tcp_debug option if it wasn't already set. This value
2442 2438 * determines whether TCP
2443 2439 * calls strlog() to print out debug messages. Doing this
2444 2440 * initialization here means that this value is not inherited thru
2445 2441 * tcp_reinit().
2446 2442 */
2447 2443 if (!connp->conn_debug)
2448 2444 connp->conn_debug = tcps->tcps_dbg;
2449 2445 }
2450 2446
2451 2447 /*
2452 2448 * Update the TCP connection according to change of PMTU.
2453 2449 *
2454 2450 * Path MTU might have changed by either increase or decrease, so need to
2455 2451 * adjust the MSS based on the value of ixa_pmtu. No need to handle tiny
2456 2452 * or negative MSS, since tcp_mss_set() will do it.
2457 2453 */
2458 2454 void
2459 2455 tcp_update_pmtu(tcp_t *tcp, boolean_t decrease_only)
2460 2456 {
2461 2457 uint32_t pmtu;
2462 2458 int32_t mss;
2463 2459 conn_t *connp = tcp->tcp_connp;
2464 2460 ip_xmit_attr_t *ixa = connp->conn_ixa;
2465 2461 iaflags_t ixaflags;
2466 2462
2467 2463 if (tcp->tcp_tcps->tcps_ignore_path_mtu)
2468 2464 return;
2469 2465
2470 2466 if (tcp->tcp_state < TCPS_ESTABLISHED)
2471 2467 return;
2472 2468
2473 2469 /*
2474 2470 * Always call ip_get_pmtu() to make sure that IP has updated
2475 2471 * ixa_flags properly.
2476 2472 */
2477 2473 pmtu = ip_get_pmtu(ixa);
2478 2474 ixaflags = ixa->ixa_flags;
2479 2475
2480 2476 /*
2481 2477 * Calculate the MSS by decreasing the PMTU by conn_ht_iphc_len and
2482 2478 * IPsec overhead if applied. Make sure to use the most recent
2483 2479 * IPsec information.
2484 2480 */
2485 2481 mss = pmtu - connp->conn_ht_iphc_len - conn_ipsec_length(connp);
2486 2482
2487 2483 /*
2488 2484 * Nothing to change, so just return.
2489 2485 */
2490 2486 if (mss == tcp->tcp_mss)
2491 2487 return;
2492 2488
2493 2489 /*
2494 2490 * Currently, for ICMP errors, only PMTU decrease is handled.
2495 2491 */
2496 2492 if (mss > tcp->tcp_mss && decrease_only)
2497 2493 return;
2498 2494
2499 2495 DTRACE_PROBE2(tcp_update_pmtu, int32_t, tcp->tcp_mss, uint32_t, mss);
2500 2496
2501 2497 /*
2502 2498 * Update ixa_fragsize and ixa_pmtu.
2503 2499 */
2504 2500 ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
2505 2501
2506 2502 /*
2507 2503 * Adjust MSS and all relevant variables.
2508 2504 */
2509 2505 tcp_mss_set(tcp, mss);
2510 2506
2511 2507 /*
2512 2508 * If the PMTU is below the min size maintained by IP, then ip_get_pmtu
2513 2509 * has set IXAF_PMTU_TOO_SMALL and cleared IXAF_PMTU_IPV4_DF. Since TCP
2514 2510 * has a (potentially different) min size we do the same. Make sure to
2515 2511 * clear IXAF_DONTFRAG, which is used by IP to decide whether to
2516 2512 * fragment the packet.
2517 2513 *
2518 2514 * LSO over IPv6 can not be fragmented. So need to disable LSO
2519 2515 * when IPv6 fragmentation is needed.
2520 2516 */
2521 2517 if (mss < tcp->tcp_tcps->tcps_mss_min)
2522 2518 ixaflags |= IXAF_PMTU_TOO_SMALL;
2523 2519
2524 2520 if (ixaflags & IXAF_PMTU_TOO_SMALL)
2525 2521 ixaflags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
2526 2522
2527 2523 if ((connp->conn_ipversion == IPV4_VERSION) &&
2528 2524 !(ixaflags & IXAF_PMTU_IPV4_DF)) {
2529 2525 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0;
2530 2526 }
2531 2527 ixa->ixa_flags = ixaflags;
2532 2528 }
2533 2529
2534 2530 int
2535 2531 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk)
2536 2532 {
2537 2533 conn_t *connp = tcp->tcp_connp;
2538 2534 queue_t *q = connp->conn_rq;
2539 2535 int32_t mss = tcp->tcp_mss;
2540 2536 int maxpsz;
2541 2537
2542 2538 if (TCP_IS_DETACHED(tcp))
2543 2539 return (mss);
2544 2540 if (tcp->tcp_fused) {
2545 2541 maxpsz = tcp_fuse_maxpsz(tcp);
2546 2542 mss = INFPSZ;
2547 2543 } else if (tcp->tcp_maxpsz_multiplier == 0) {
2548 2544 /*
2549 2545 * Set the sd_qn_maxpsz according to the socket send buffer
2550 2546 * size, and sd_maxblk to INFPSZ (-1). This will essentially
2551 2547 * instruct the stream head to copyin user data into contiguous
2552 2548 * kernel-allocated buffers without breaking it up into smaller
2553 2549 * chunks. We round up the buffer size to the nearest SMSS.
2554 2550 */
2555 2551 maxpsz = MSS_ROUNDUP(connp->conn_sndbuf, mss);
2556 2552 mss = INFPSZ;
2557 2553 } else {
2558 2554 /*
2559 2555 * Set sd_qn_maxpsz to approx half the (receivers) buffer
2560 2556 * (and a multiple of the mss). This instructs the stream
2561 2557 * head to break down larger than SMSS writes into SMSS-
2562 2558 * size mblks, up to tcp_maxpsz_multiplier mblks at a time.
2563 2559 */
2564 2560 maxpsz = tcp->tcp_maxpsz_multiplier * mss;
2565 2561 if (maxpsz > connp->conn_sndbuf / 2) {
2566 2562 maxpsz = connp->conn_sndbuf / 2;
2567 2563 /* Round up to nearest mss */
2568 2564 maxpsz = MSS_ROUNDUP(maxpsz, mss);
2569 2565 }
2570 2566 }
2571 2567
2572 2568 (void) proto_set_maxpsz(q, connp, maxpsz);
2573 2569 if (!(IPCL_IS_NONSTR(connp)))
2574 2570 connp->conn_wq->q_maxpsz = maxpsz;
2575 2571 if (set_maxblk)
2576 2572 (void) proto_set_tx_maxblk(q, connp, mss);
2577 2573 return (mss);
2578 2574 }
2579 2575
2580 2576 /* For /dev/tcp aka AF_INET open */
2581 2577 static int
2582 2578 tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2583 2579 {
2584 2580 return (tcp_open(q, devp, flag, sflag, credp, B_FALSE));
2585 2581 }
2586 2582
2587 2583 /* For /dev/tcp6 aka AF_INET6 open */
2588 2584 static int
2589 2585 tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2590 2586 {
2591 2587 return (tcp_open(q, devp, flag, sflag, credp, B_TRUE));
2592 2588 }
2593 2589
2594 2590 conn_t *
2595 2591 tcp_create_common(cred_t *credp, boolean_t isv6, boolean_t issocket,
2596 2592 int *errorp)
2597 2593 {
2598 2594 tcp_t *tcp = NULL;
2599 2595 conn_t *connp;
2600 2596 zoneid_t zoneid;
2601 2597 tcp_stack_t *tcps;
2602 2598 squeue_t *sqp;
2603 2599
2604 2600 ASSERT(errorp != NULL);
2605 2601 /*
2606 2602 * Find the proper zoneid and netstack.
2607 2603 */
2608 2604 /*
2609 2605 * Special case for install: miniroot needs to be able to
2610 2606 * access files via NFS as though it were always in the
2611 2607 * global zone.
2612 2608 */
2613 2609 if (credp == kcred && nfs_global_client_only != 0) {
2614 2610 zoneid = GLOBAL_ZONEID;
2615 2611 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->
2616 2612 netstack_tcp;
2617 2613 ASSERT(tcps != NULL);
2618 2614 } else {
2619 2615 netstack_t *ns;
2620 2616 int err;
2621 2617
2622 2618 if ((err = secpolicy_basic_net_access(credp)) != 0) {
2623 2619 *errorp = err;
2624 2620 return (NULL);
2625 2621 }
2626 2622
2627 2623 ns = netstack_find_by_cred(credp);
2628 2624 ASSERT(ns != NULL);
2629 2625 tcps = ns->netstack_tcp;
2630 2626 ASSERT(tcps != NULL);
2631 2627
2632 2628 /*
2633 2629 * For exclusive stacks we set the zoneid to zero
2634 2630 * to make TCP operate as if in the global zone.
2635 2631 */
2636 2632 if (tcps->tcps_netstack->netstack_stackid !=
2637 2633 GLOBAL_NETSTACKID)
2638 2634 zoneid = GLOBAL_ZONEID;
2639 2635 else
2640 2636 zoneid = crgetzoneid(credp);
2641 2637 }
2642 2638
2643 2639 sqp = IP_SQUEUE_GET((uint_t)gethrtime());
2644 2640 connp = (conn_t *)tcp_get_conn(sqp, tcps);
2645 2641 /*
2646 2642 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt,
2647 2643 * so we drop it by one.
2648 2644 */
2649 2645 netstack_rele(tcps->tcps_netstack);
2650 2646 if (connp == NULL) {
2651 2647 *errorp = ENOSR;
2652 2648 return (NULL);
2653 2649 }
2654 2650 ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2655 2651
2656 2652 connp->conn_sqp = sqp;
2657 2653 connp->conn_initial_sqp = connp->conn_sqp;
2658 2654 connp->conn_ixa->ixa_sqp = connp->conn_sqp;
2659 2655 tcp = connp->conn_tcp;
2660 2656
2661 2657 /*
2662 2658 * Besides asking IP to set the checksum for us, have conn_ip_output
2663 2659 * to do the following checks when necessary:
2664 2660 *
2665 2661 * IXAF_VERIFY_SOURCE: drop packets when our outer source goes invalid
2666 2662 * IXAF_VERIFY_PMTU: verify PMTU changes
2667 2663 * IXAF_VERIFY_LSO: verify LSO capability changes
2668 2664 */
2669 2665 connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2670 2666 IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO;
2671 2667
2672 2668 if (!tcps->tcps_dev_flow_ctl)
2673 2669 connp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
2674 2670
2675 2671 if (isv6) {
2676 2672 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
2677 2673 connp->conn_ipversion = IPV6_VERSION;
2678 2674 connp->conn_family = AF_INET6;
2679 2675 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2680 2676 connp->conn_default_ttl = tcps->tcps_ipv6_hoplimit;
2681 2677 } else {
2682 2678 connp->conn_ipversion = IPV4_VERSION;
2683 2679 connp->conn_family = AF_INET;
2684 2680 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2685 2681 connp->conn_default_ttl = tcps->tcps_ipv4_ttl;
2686 2682 }
2687 2683 connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl;
2688 2684
2689 2685 crhold(credp);
2690 2686 connp->conn_cred = credp;
2691 2687 connp->conn_cpid = curproc->p_pid;
2692 2688 connp->conn_open_time = ddi_get_lbolt64();
2693 2689
2694 2690 /* Cache things in the ixa without any refhold */
2695 2691 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
2696 2692 connp->conn_ixa->ixa_cred = credp;
2697 2693 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
2698 2694
2699 2695 connp->conn_zoneid = zoneid;
2700 2696 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
2701 2697 connp->conn_ixa->ixa_zoneid = zoneid;
2702 2698 connp->conn_mlp_type = mlptSingle;
2703 2699 ASSERT(connp->conn_netstack == tcps->tcps_netstack);
2704 2700 ASSERT(tcp->tcp_tcps == tcps);
2705 2701
2706 2702 /*
2707 2703 * If the caller has the process-wide flag set, then default to MAC
2708 2704 * exempt mode. This allows read-down to unlabeled hosts.
2709 2705 */
2710 2706 if (getpflags(NET_MAC_AWARE, credp) != 0)
↓ open down ↓ |
2461 lines elided |
↑ open up ↑ |
2711 2707 connp->conn_mac_mode = CONN_MAC_AWARE;
2712 2708
2713 2709 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
2714 2710
2715 2711 if (issocket) {
2716 2712 tcp->tcp_issocket = 1;
2717 2713 }
2718 2714
2719 2715 connp->conn_rcvbuf = tcps->tcps_recv_hiwat;
2720 2716 connp->conn_sndbuf = tcps->tcps_xmit_hiwat;
2721 - connp->conn_sndlowat = tcps->tcps_xmit_lowat;
2717 + if (tcps->tcps_snd_lowat_fraction != 0) {
2718 + connp->conn_sndlowat = connp->conn_sndbuf /
2719 + tcps->tcps_snd_lowat_fraction;
2720 + } else {
2721 + connp->conn_sndlowat = tcps->tcps_xmit_lowat;
2722 + }
2722 2723 connp->conn_so_type = SOCK_STREAM;
2723 2724 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2724 2725 tcps->tcps_wroff_xtra;
2725 2726
2726 2727 SOCK_CONNID_INIT(tcp->tcp_connid);
2727 2728 /* DTrace ignores this - it isn't a tcp:::state-change */
2728 2729 tcp->tcp_state = TCPS_IDLE;
2729 2730 tcp_init_values(tcp, NULL);
2730 2731 return (connp);
2731 2732 }
2732 2733
2733 2734 static int
2734 2735 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
2735 2736 boolean_t isv6)
2736 2737 {
2737 2738 tcp_t *tcp = NULL;
2738 2739 conn_t *connp = NULL;
2739 2740 int err;
2740 2741 vmem_t *minor_arena = NULL;
2741 2742 dev_t conn_dev;
2742 2743 boolean_t issocket;
2743 2744
2744 2745 if (q->q_ptr != NULL)
2745 2746 return (0);
2746 2747
2747 2748 if (sflag == MODOPEN)
2748 2749 return (EINVAL);
2749 2750
2750 2751 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
2751 2752 ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
2752 2753 minor_arena = ip_minor_arena_la;
2753 2754 } else {
2754 2755 /*
2755 2756 * Either minor numbers in the large arena were exhausted
2756 2757 * or a non socket application is doing the open.
2757 2758 * Try to allocate from the small arena.
2758 2759 */
2759 2760 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
2760 2761 return (EBUSY);
2761 2762 }
2762 2763 minor_arena = ip_minor_arena_sa;
2763 2764 }
2764 2765
2765 2766 ASSERT(minor_arena != NULL);
2766 2767
2767 2768 *devp = makedevice(getmajor(*devp), (minor_t)conn_dev);
2768 2769
2769 2770 if (flag & SO_FALLBACK) {
2770 2771 /*
2771 2772 * Non streams socket needs a stream to fallback to
2772 2773 */
2773 2774 RD(q)->q_ptr = (void *)conn_dev;
2774 2775 WR(q)->q_qinfo = &tcp_fallback_sock_winit;
2775 2776 WR(q)->q_ptr = (void *)minor_arena;
2776 2777 qprocson(q);
2777 2778 return (0);
2778 2779 } else if (flag & SO_ACCEPTOR) {
2779 2780 q->q_qinfo = &tcp_acceptor_rinit;
2780 2781 /*
2781 2782 * the conn_dev and minor_arena will be subsequently used by
2782 2783 * tcp_tli_accept() and tcp_tpi_close_accept() to figure out
2783 2784 * the minor device number for this connection from the q_ptr.
2784 2785 */
2785 2786 RD(q)->q_ptr = (void *)conn_dev;
2786 2787 WR(q)->q_qinfo = &tcp_acceptor_winit;
2787 2788 WR(q)->q_ptr = (void *)minor_arena;
2788 2789 qprocson(q);
2789 2790 return (0);
2790 2791 }
2791 2792
2792 2793 issocket = flag & SO_SOCKSTR;
2793 2794 connp = tcp_create_common(credp, isv6, issocket, &err);
2794 2795
2795 2796 if (connp == NULL) {
2796 2797 inet_minor_free(minor_arena, conn_dev);
2797 2798 q->q_ptr = WR(q)->q_ptr = NULL;
2798 2799 return (err);
2799 2800 }
2800 2801
2801 2802 connp->conn_rq = q;
2802 2803 connp->conn_wq = WR(q);
2803 2804 q->q_ptr = WR(q)->q_ptr = connp;
2804 2805
2805 2806 connp->conn_dev = conn_dev;
2806 2807 connp->conn_minor_arena = minor_arena;
2807 2808
2808 2809 ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6);
2809 2810 ASSERT(WR(q)->q_qinfo == &tcp_winit);
2810 2811
2811 2812 tcp = connp->conn_tcp;
2812 2813
2813 2814 if (issocket) {
2814 2815 WR(q)->q_qinfo = &tcp_sock_winit;
2815 2816 } else {
2816 2817 #ifdef _ILP32
2817 2818 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q);
2818 2819 #else
2819 2820 tcp->tcp_acceptor_id = conn_dev;
2820 2821 #endif /* _ILP32 */
2821 2822 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp);
2822 2823 }
2823 2824
2824 2825 /*
2825 2826 * Put the ref for TCP. Ref for IP was already put
2826 2827 * by ipcl_conn_create. Also Make the conn_t globally
2827 2828 * visible to walkers
2828 2829 */
2829 2830 mutex_enter(&connp->conn_lock);
2830 2831 CONN_INC_REF_LOCKED(connp);
2831 2832 ASSERT(connp->conn_ref == 2);
2832 2833 connp->conn_state_flags &= ~CONN_INCIPIENT;
2833 2834 mutex_exit(&connp->conn_lock);
2834 2835
2835 2836 qprocson(q);
2836 2837 return (0);
2837 2838 }
2838 2839
2839 2840 /*
2840 2841 * Build/update the tcp header template (in conn_ht_iphc) based on
2841 2842 * conn_xmit_ipp. The headers include ip6_t, any extension
2842 2843 * headers, and the maximum size tcp header (to avoid reallocation
2843 2844 * on the fly for additional tcp options).
2844 2845 *
2845 2846 * Assumes the caller has already set conn_{faddr,laddr,fport,lport,flowinfo}.
2846 2847 * Returns failure if can't allocate memory.
2847 2848 */
2848 2849 int
2849 2850 tcp_build_hdrs(tcp_t *tcp)
2850 2851 {
2851 2852 tcp_stack_t *tcps = tcp->tcp_tcps;
2852 2853 conn_t *connp = tcp->tcp_connp;
2853 2854 char buf[TCP_MAX_HDR_LENGTH];
2854 2855 uint_t buflen;
2855 2856 uint_t ulplen = TCP_MIN_HEADER_LENGTH;
2856 2857 uint_t extralen = TCP_MAX_TCP_OPTIONS_LENGTH;
2857 2858 tcpha_t *tcpha;
2858 2859 uint32_t cksum;
2859 2860 int error;
2860 2861
2861 2862 /*
2862 2863 * We might be called after the connection is set up, and we might
2863 2864 * have TS options already in the TCP header. Thus we save any
2864 2865 * existing tcp header.
2865 2866 */
2866 2867 buflen = connp->conn_ht_ulp_len;
2867 2868 if (buflen != 0) {
2868 2869 bcopy(connp->conn_ht_ulp, buf, buflen);
2869 2870 extralen -= buflen - ulplen;
2870 2871 ulplen = buflen;
2871 2872 }
2872 2873
2873 2874 /* Grab lock to satisfy ASSERT; TCP is serialized using squeue */
2874 2875 mutex_enter(&connp->conn_lock);
2875 2876 error = conn_build_hdr_template(connp, ulplen, extralen,
2876 2877 &connp->conn_laddr_v6, &connp->conn_faddr_v6, connp->conn_flowinfo);
2877 2878 mutex_exit(&connp->conn_lock);
2878 2879 if (error != 0)
2879 2880 return (error);
2880 2881
2881 2882 /*
2882 2883 * Any routing header/option has been massaged. The checksum difference
2883 2884 * is stored in conn_sum for later use.
2884 2885 */
2885 2886 tcpha = (tcpha_t *)connp->conn_ht_ulp;
2886 2887 tcp->tcp_tcpha = tcpha;
2887 2888
2888 2889 /* restore any old tcp header */
2889 2890 if (buflen != 0) {
2890 2891 bcopy(buf, connp->conn_ht_ulp, buflen);
2891 2892 } else {
2892 2893 tcpha->tha_sum = 0;
2893 2894 tcpha->tha_urp = 0;
2894 2895 tcpha->tha_ack = 0;
2895 2896 tcpha->tha_offset_and_reserved = (5 << 4);
2896 2897 tcpha->tha_lport = connp->conn_lport;
2897 2898 tcpha->tha_fport = connp->conn_fport;
2898 2899 }
2899 2900
2900 2901 /*
2901 2902 * IP wants our header length in the checksum field to
2902 2903 * allow it to perform a single pseudo-header+checksum
2903 2904 * calculation on behalf of TCP.
2904 2905 * Include the adjustment for a source route once IP_OPTIONS is set.
2905 2906 */
2906 2907 cksum = sizeof (tcpha_t) + connp->conn_sum;
2907 2908 cksum = (cksum >> 16) + (cksum & 0xFFFF);
2908 2909 ASSERT(cksum < 0x10000);
2909 2910 tcpha->tha_sum = htons(cksum);
2910 2911
2911 2912 if (connp->conn_ipversion == IPV4_VERSION)
2912 2913 tcp->tcp_ipha = (ipha_t *)connp->conn_ht_iphc;
2913 2914 else
2914 2915 tcp->tcp_ip6h = (ip6_t *)connp->conn_ht_iphc;
2915 2916
2916 2917 if (connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra >
2917 2918 connp->conn_wroff) {
2918 2919 connp->conn_wroff = connp->conn_ht_iphc_allocated +
2919 2920 tcps->tcps_wroff_xtra;
2920 2921 (void) proto_set_tx_wroff(connp->conn_rq, connp,
2921 2922 connp->conn_wroff);
2922 2923 }
2923 2924 return (0);
2924 2925 }
2925 2926
2926 2927 /*
2927 2928 * tcp_rwnd_set() is called to adjust the receive window to a desired value.
2928 2929 * We do not allow the receive window to shrink. After setting rwnd,
2929 2930 * set the flow control hiwat of the stream.
2930 2931 *
2931 2932 * This function is called in 2 cases:
2932 2933 *
2933 2934 * 1) Before data transfer begins, in tcp_input_listener() for accepting a
2934 2935 * connection (passive open) and in tcp_input_data() for active connect.
2935 2936 * This is called after tcp_mss_set() when the desired MSS value is known.
2936 2937 * This makes sure that our window size is a mutiple of the other side's
2937 2938 * MSS.
2938 2939 * 2) Handling SO_RCVBUF option.
2939 2940 *
2940 2941 * It is ASSUMED that the requested size is a multiple of the current MSS.
2941 2942 *
2942 2943 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
2943 2944 * user requests so.
2944 2945 */
2945 2946 int
2946 2947 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
2947 2948 {
2948 2949 uint32_t mss = tcp->tcp_mss;
2949 2950 uint32_t old_max_rwnd;
2950 2951 uint32_t max_transmittable_rwnd;
2951 2952 boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
2952 2953 tcp_stack_t *tcps = tcp->tcp_tcps;
2953 2954 conn_t *connp = tcp->tcp_connp;
2954 2955
2955 2956 /*
2956 2957 * Insist on a receive window that is at least
2957 2958 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
2958 2959 * funny TCP interactions of Nagle algorithm, SWS avoidance
2959 2960 * and delayed acknowledgement.
2960 2961 */
2961 2962 rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss);
2962 2963
2963 2964 if (tcp->tcp_fused) {
2964 2965 size_t sth_hiwat;
2965 2966 tcp_t *peer_tcp = tcp->tcp_loopback_peer;
2966 2967
2967 2968 ASSERT(peer_tcp != NULL);
2968 2969 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd);
2969 2970 if (!tcp_detached) {
2970 2971 (void) proto_set_rx_hiwat(connp->conn_rq, connp,
2971 2972 sth_hiwat);
2972 2973 tcp_set_recv_threshold(tcp, sth_hiwat >> 3);
2973 2974 }
2974 2975
2975 2976 /* Caller could have changed tcp_rwnd; update tha_win */
2976 2977 if (tcp->tcp_tcpha != NULL) {
2977 2978 tcp->tcp_tcpha->tha_win =
2978 2979 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
2979 2980 }
2980 2981 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
2981 2982 tcp->tcp_cwnd_max = rwnd;
2982 2983
2983 2984 /*
2984 2985 * In the fusion case, the maxpsz stream head value of
2985 2986 * our peer is set according to its send buffer size
2986 2987 * and our receive buffer size; since the latter may
2987 2988 * have changed we need to update the peer's maxpsz.
2988 2989 */
2989 2990 (void) tcp_maxpsz_set(peer_tcp, B_TRUE);
2990 2991 return (sth_hiwat);
2991 2992 }
2992 2993
2993 2994 if (tcp_detached)
2994 2995 old_max_rwnd = tcp->tcp_rwnd;
2995 2996 else
2996 2997 old_max_rwnd = connp->conn_rcvbuf;
2997 2998
2998 2999
2999 3000 /*
3000 3001 * If window size info has already been exchanged, TCP should not
3001 3002 * shrink the window. Shrinking window is doable if done carefully.
3002 3003 * We may add that support later. But so far there is not a real
3003 3004 * need to do that.
3004 3005 */
3005 3006 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
3006 3007 /* MSS may have changed, do a round up again. */
3007 3008 rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
3008 3009 }
3009 3010
3010 3011 /*
3011 3012 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
3012 3013 * can be applied even before the window scale option is decided.
3013 3014 */
3014 3015 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
3015 3016 if (rwnd > max_transmittable_rwnd) {
3016 3017 rwnd = max_transmittable_rwnd -
3017 3018 (max_transmittable_rwnd % mss);
3018 3019 if (rwnd < mss)
3019 3020 rwnd = max_transmittable_rwnd;
3020 3021 /*
3021 3022 * If we're over the limit we may have to back down tcp_rwnd.
3022 3023 * The increment below won't work for us. So we set all three
3023 3024 * here and the increment below will have no effect.
3024 3025 */
3025 3026 tcp->tcp_rwnd = old_max_rwnd = rwnd;
3026 3027 }
3027 3028 if (tcp->tcp_localnet) {
3028 3029 tcp->tcp_rack_abs_max =
3029 3030 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2);
3030 3031 } else {
3031 3032 /*
3032 3033 * For a remote host on a different subnet (through a router),
3033 3034 * we ack every other packet to be conforming to RFC1122.
3034 3035 * tcp_deferred_acks_max is default to 2.
3035 3036 */
3036 3037 tcp->tcp_rack_abs_max =
3037 3038 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2);
3038 3039 }
3039 3040 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max)
3040 3041 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
3041 3042 else
3042 3043 tcp->tcp_rack_cur_max = 0;
3043 3044 /*
3044 3045 * Increment the current rwnd by the amount the maximum grew (we
3045 3046 * can not overwrite it since we might be in the middle of a
3046 3047 * connection.)
3047 3048 */
3048 3049 tcp->tcp_rwnd += rwnd - old_max_rwnd;
3049 3050 connp->conn_rcvbuf = rwnd;
3050 3051
3051 3052 /* Are we already connected? */
3052 3053 if (tcp->tcp_tcpha != NULL) {
3053 3054 tcp->tcp_tcpha->tha_win =
3054 3055 htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3055 3056 }
3056 3057
3057 3058 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
3058 3059 tcp->tcp_cwnd_max = rwnd;
3059 3060
3060 3061 if (tcp_detached)
3061 3062 return (rwnd);
3062 3063
3063 3064 tcp_set_recv_threshold(tcp, rwnd >> 3);
3064 3065
3065 3066 (void) proto_set_rx_hiwat(connp->conn_rq, connp, rwnd);
3066 3067 return (rwnd);
3067 3068 }
3068 3069
3069 3070 int
3070 3071 tcp_do_unbind(conn_t *connp)
3071 3072 {
3072 3073 tcp_t *tcp = connp->conn_tcp;
3073 3074 int32_t oldstate;
3074 3075
3075 3076 switch (tcp->tcp_state) {
3076 3077 case TCPS_BOUND:
3077 3078 case TCPS_LISTEN:
3078 3079 break;
3079 3080 default:
3080 3081 return (-TOUTSTATE);
3081 3082 }
3082 3083
3083 3084 /*
3084 3085 * Need to clean up all the eagers since after the unbind, segments
3085 3086 * will no longer be delivered to this listener stream.
3086 3087 */
3087 3088 mutex_enter(&tcp->tcp_eager_lock);
3088 3089 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
3089 3090 tcp_eager_cleanup(tcp, 0);
3090 3091 }
3091 3092 mutex_exit(&tcp->tcp_eager_lock);
3092 3093
3093 3094 /* Clean up the listener connection counter if necessary. */
3094 3095 if (tcp->tcp_listen_cnt != NULL)
3095 3096 TCP_DECR_LISTEN_CNT(tcp);
3096 3097 connp->conn_laddr_v6 = ipv6_all_zeros;
3097 3098 connp->conn_saddr_v6 = ipv6_all_zeros;
3098 3099 tcp_bind_hash_remove(tcp);
3099 3100 oldstate = tcp->tcp_state;
3100 3101 tcp->tcp_state = TCPS_IDLE;
3101 3102 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
3102 3103 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
3103 3104 int32_t, oldstate);
3104 3105
3105 3106 ip_unbind(connp);
3106 3107 bzero(&connp->conn_ports, sizeof (connp->conn_ports));
3107 3108
3108 3109 return (0);
3109 3110 }
3110 3111
3111 3112 /*
3112 3113 * Collect protocol properties to send to the upper handle.
3113 3114 */
3114 3115 void
3115 3116 tcp_get_proto_props(tcp_t *tcp, struct sock_proto_props *sopp)
3116 3117 {
3117 3118 conn_t *connp = tcp->tcp_connp;
3118 3119
3119 3120 sopp->sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF;
3120 3121 sopp->sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE);
3121 3122
3122 3123 sopp->sopp_rxhiwat = tcp->tcp_fused ?
3123 3124 tcp_fuse_set_rcv_hiwat(tcp, connp->conn_rcvbuf) :
3124 3125 connp->conn_rcvbuf;
3125 3126 /*
3126 3127 * Determine what write offset value to use depending on SACK and
3127 3128 * whether the endpoint is fused or not.
3128 3129 */
3129 3130 if (tcp->tcp_fused) {
3130 3131 ASSERT(tcp->tcp_loopback);
3131 3132 ASSERT(tcp->tcp_loopback_peer != NULL);
3132 3133 /*
3133 3134 * For fused tcp loopback, set the stream head's write
3134 3135 * offset value to zero since we won't be needing any room
3135 3136 * for TCP/IP headers. This would also improve performance
3136 3137 * since it would reduce the amount of work done by kmem.
3137 3138 * Non-fused tcp loopback case is handled separately below.
3138 3139 */
3139 3140 sopp->sopp_wroff = 0;
3140 3141 /*
3141 3142 * Update the peer's transmit parameters according to
3142 3143 * our recently calculated high water mark value.
3143 3144 */
3144 3145 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE);
3145 3146 } else if (tcp->tcp_snd_sack_ok) {
3146 3147 sopp->sopp_wroff = connp->conn_ht_iphc_allocated +
3147 3148 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3148 3149 } else {
3149 3150 sopp->sopp_wroff = connp->conn_ht_iphc_len +
3150 3151 (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
3151 3152 }
3152 3153
3153 3154 if (tcp->tcp_loopback) {
3154 3155 sopp->sopp_flags |= SOCKOPT_LOOPBACK;
3155 3156 sopp->sopp_loopback = B_TRUE;
3156 3157 }
3157 3158 }
3158 3159
3159 3160 /*
3160 3161 * Check the usability of ZEROCOPY. It's instead checking the flag set by IP.
3161 3162 */
3162 3163 boolean_t
3163 3164 tcp_zcopy_check(tcp_t *tcp)
3164 3165 {
3165 3166 conn_t *connp = tcp->tcp_connp;
3166 3167 ip_xmit_attr_t *ixa = connp->conn_ixa;
3167 3168 boolean_t zc_enabled = B_FALSE;
3168 3169 tcp_stack_t *tcps = tcp->tcp_tcps;
3169 3170
3170 3171 if (do_tcpzcopy == 2)
3171 3172 zc_enabled = B_TRUE;
3172 3173 else if ((do_tcpzcopy == 1) && (ixa->ixa_flags & IXAF_ZCOPY_CAPAB))
3173 3174 zc_enabled = B_TRUE;
3174 3175
3175 3176 tcp->tcp_snd_zcopy_on = zc_enabled;
3176 3177 if (!TCP_IS_DETACHED(tcp)) {
3177 3178 if (zc_enabled) {
3178 3179 ixa->ixa_flags |= IXAF_VERIFY_ZCOPY;
3179 3180 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3180 3181 ZCVMSAFE);
3181 3182 TCP_STAT(tcps, tcp_zcopy_on);
3182 3183 } else {
3183 3184 ixa->ixa_flags &= ~IXAF_VERIFY_ZCOPY;
3184 3185 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3185 3186 ZCVMUNSAFE);
3186 3187 TCP_STAT(tcps, tcp_zcopy_off);
3187 3188 }
3188 3189 }
3189 3190 return (zc_enabled);
3190 3191 }
3191 3192
3192 3193 /*
3193 3194 * Backoff from a zero-copy message by copying data to a new allocated
3194 3195 * message and freeing the original desballoca'ed segmapped message.
3195 3196 *
3196 3197 * This function is called by following two callers:
3197 3198 * 1. tcp_timer: fix_xmitlist is set to B_TRUE, because it's safe to free
3198 3199 * the origial desballoca'ed message and notify sockfs. This is in re-
3199 3200 * transmit state.
3200 3201 * 2. tcp_output: fix_xmitlist is set to B_FALSE. Flag STRUIO_ZCNOTIFY need
3201 3202 * to be copied to new message.
3202 3203 */
3203 3204 mblk_t *
3204 3205 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, boolean_t fix_xmitlist)
3205 3206 {
3206 3207 mblk_t *nbp;
3207 3208 mblk_t *head = NULL;
3208 3209 mblk_t *tail = NULL;
3209 3210 tcp_stack_t *tcps = tcp->tcp_tcps;
3210 3211
3211 3212 ASSERT(bp != NULL);
3212 3213 while (bp != NULL) {
3213 3214 if (IS_VMLOANED_MBLK(bp)) {
3214 3215 TCP_STAT(tcps, tcp_zcopy_backoff);
3215 3216 if ((nbp = copyb(bp)) == NULL) {
3216 3217 tcp->tcp_xmit_zc_clean = B_FALSE;
3217 3218 if (tail != NULL)
3218 3219 tail->b_cont = bp;
3219 3220 return ((head == NULL) ? bp : head);
3220 3221 }
3221 3222
3222 3223 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) {
3223 3224 if (fix_xmitlist)
3224 3225 tcp_zcopy_notify(tcp);
3225 3226 else
3226 3227 nbp->b_datap->db_struioflag |=
3227 3228 STRUIO_ZCNOTIFY;
3228 3229 }
3229 3230 nbp->b_cont = bp->b_cont;
3230 3231
3231 3232 /*
3232 3233 * Copy saved information and adjust tcp_xmit_tail
3233 3234 * if needed.
3234 3235 */
3235 3236 if (fix_xmitlist) {
3236 3237 nbp->b_prev = bp->b_prev;
3237 3238 nbp->b_next = bp->b_next;
3238 3239
3239 3240 if (tcp->tcp_xmit_tail == bp)
3240 3241 tcp->tcp_xmit_tail = nbp;
3241 3242 }
3242 3243
3243 3244 /* Free the original message. */
3244 3245 bp->b_prev = NULL;
3245 3246 bp->b_next = NULL;
3246 3247 freeb(bp);
3247 3248
3248 3249 bp = nbp;
3249 3250 }
3250 3251
3251 3252 if (head == NULL) {
3252 3253 head = bp;
3253 3254 }
3254 3255 if (tail == NULL) {
3255 3256 tail = bp;
3256 3257 } else {
3257 3258 tail->b_cont = bp;
3258 3259 tail = bp;
3259 3260 }
3260 3261
3261 3262 /* Move forward. */
3262 3263 bp = bp->b_cont;
3263 3264 }
3264 3265
3265 3266 if (fix_xmitlist) {
3266 3267 tcp->tcp_xmit_last = tail;
3267 3268 tcp->tcp_xmit_zc_clean = B_TRUE;
3268 3269 }
3269 3270
3270 3271 return (head);
3271 3272 }
3272 3273
3273 3274 void
3274 3275 tcp_zcopy_notify(tcp_t *tcp)
3275 3276 {
3276 3277 struct stdata *stp;
3277 3278 conn_t *connp;
3278 3279
3279 3280 if (tcp->tcp_detached)
3280 3281 return;
3281 3282 connp = tcp->tcp_connp;
3282 3283 if (IPCL_IS_NONSTR(connp)) {
3283 3284 (*connp->conn_upcalls->su_zcopy_notify)
3284 3285 (connp->conn_upper_handle);
3285 3286 return;
3286 3287 }
3287 3288 stp = STREAM(connp->conn_rq);
3288 3289 mutex_enter(&stp->sd_lock);
3289 3290 stp->sd_flag |= STZCNOTIFY;
3290 3291 cv_broadcast(&stp->sd_zcopy_wait);
3291 3292 mutex_exit(&stp->sd_lock);
3292 3293 }
3293 3294
3294 3295 /*
3295 3296 * Update the TCP connection according to change of LSO capability.
3296 3297 */
3297 3298 static void
3298 3299 tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa)
3299 3300 {
3300 3301 /*
3301 3302 * We check against IPv4 header length to preserve the old behavior
3302 3303 * of only enabling LSO when there are no IP options.
3303 3304 * But this restriction might not be necessary at all. Before removing
3304 3305 * it, need to verify how LSO is handled for source routing case, with
3305 3306 * which IP does software checksum.
3306 3307 *
3307 3308 * For IPv6, whenever any extension header is needed, LSO is supressed.
3308 3309 */
3309 3310 if (ixa->ixa_ip_hdr_length != ((ixa->ixa_flags & IXAF_IS_IPV4) ?
3310 3311 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN))
3311 3312 return;
3312 3313
3313 3314 /*
3314 3315 * Either the LSO capability newly became usable, or it has changed.
3315 3316 */
3316 3317 if (ixa->ixa_flags & IXAF_LSO_CAPAB) {
3317 3318 ill_lso_capab_t *lsoc = &ixa->ixa_lso_capab;
3318 3319
3319 3320 ASSERT(lsoc->ill_lso_max > 0);
3320 3321 tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, lsoc->ill_lso_max);
3321 3322
3322 3323 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3323 3324 boolean_t, B_TRUE, uint32_t, tcp->tcp_lso_max);
3324 3325
3325 3326 /*
3326 3327 * If LSO to be enabled, notify the STREAM header with larger
3327 3328 * data block.
3328 3329 */
3329 3330 if (!tcp->tcp_lso)
3330 3331 tcp->tcp_maxpsz_multiplier = 0;
3331 3332
3332 3333 tcp->tcp_lso = B_TRUE;
3333 3334 TCP_STAT(tcp->tcp_tcps, tcp_lso_enabled);
3334 3335 } else { /* LSO capability is not usable any more. */
3335 3336 DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3336 3337 boolean_t, B_FALSE, uint32_t, tcp->tcp_lso_max);
3337 3338
3338 3339 /*
3339 3340 * If LSO to be disabled, notify the STREAM header with smaller
3340 3341 * data block. And need to restore fragsize to PMTU.
3341 3342 */
3342 3343 if (tcp->tcp_lso) {
3343 3344 tcp->tcp_maxpsz_multiplier =
3344 3345 tcp->tcp_tcps->tcps_maxpsz_multiplier;
3345 3346 ixa->ixa_fragsize = ixa->ixa_pmtu;
3346 3347 tcp->tcp_lso = B_FALSE;
3347 3348 TCP_STAT(tcp->tcp_tcps, tcp_lso_disabled);
3348 3349 }
3349 3350 }
3350 3351
3351 3352 (void) tcp_maxpsz_set(tcp, B_TRUE);
3352 3353 }
3353 3354
3354 3355 /*
3355 3356 * Update the TCP connection according to change of ZEROCOPY capability.
3356 3357 */
3357 3358 static void
3358 3359 tcp_update_zcopy(tcp_t *tcp)
3359 3360 {
3360 3361 conn_t *connp = tcp->tcp_connp;
3361 3362 tcp_stack_t *tcps = tcp->tcp_tcps;
3362 3363
3363 3364 if (tcp->tcp_snd_zcopy_on) {
3364 3365 tcp->tcp_snd_zcopy_on = B_FALSE;
3365 3366 if (!TCP_IS_DETACHED(tcp)) {
3366 3367 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3367 3368 ZCVMUNSAFE);
3368 3369 TCP_STAT(tcps, tcp_zcopy_off);
3369 3370 }
3370 3371 } else {
3371 3372 tcp->tcp_snd_zcopy_on = B_TRUE;
3372 3373 if (!TCP_IS_DETACHED(tcp)) {
3373 3374 (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3374 3375 ZCVMSAFE);
3375 3376 TCP_STAT(tcps, tcp_zcopy_on);
3376 3377 }
3377 3378 }
3378 3379 }
3379 3380
3380 3381 /*
3381 3382 * Notify function registered with ip_xmit_attr_t. It's called in the squeue
3382 3383 * so it's safe to update the TCP connection.
3383 3384 */
3384 3385 /* ARGSUSED1 */
3385 3386 static void
3386 3387 tcp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
3387 3388 ixa_notify_arg_t narg)
3388 3389 {
3389 3390 tcp_t *tcp = (tcp_t *)arg;
3390 3391 conn_t *connp = tcp->tcp_connp;
3391 3392
3392 3393 switch (ntype) {
3393 3394 case IXAN_LSO:
3394 3395 tcp_update_lso(tcp, connp->conn_ixa);
3395 3396 break;
3396 3397 case IXAN_PMTU:
3397 3398 tcp_update_pmtu(tcp, B_FALSE);
3398 3399 break;
3399 3400 case IXAN_ZCOPY:
3400 3401 tcp_update_zcopy(tcp);
3401 3402 break;
3402 3403 default:
3403 3404 break;
3404 3405 }
3405 3406 }
3406 3407
3407 3408 /*
3408 3409 * The TCP write service routine should never be called...
3409 3410 */
3410 3411 /* ARGSUSED */
3411 3412 static void
3412 3413 tcp_wsrv(queue_t *q)
3413 3414 {
3414 3415 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps;
3415 3416
3416 3417 TCP_STAT(tcps, tcp_wsrv_called);
3417 3418 }
3418 3419
3419 3420 /*
3420 3421 * Hash list lookup routine for tcp_t structures.
3421 3422 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF.
3422 3423 */
3423 3424 tcp_t *
3424 3425 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps)
3425 3426 {
3426 3427 tf_t *tf;
3427 3428 tcp_t *tcp;
3428 3429
3429 3430 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3430 3431 mutex_enter(&tf->tf_lock);
3431 3432 for (tcp = tf->tf_tcp; tcp != NULL;
3432 3433 tcp = tcp->tcp_acceptor_hash) {
3433 3434 if (tcp->tcp_acceptor_id == id) {
3434 3435 CONN_INC_REF(tcp->tcp_connp);
3435 3436 mutex_exit(&tf->tf_lock);
3436 3437 return (tcp);
3437 3438 }
3438 3439 }
3439 3440 mutex_exit(&tf->tf_lock);
3440 3441 return (NULL);
3441 3442 }
3442 3443
3443 3444 /*
3444 3445 * Hash list insertion routine for tcp_t structures.
3445 3446 */
3446 3447 void
3447 3448 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp)
3448 3449 {
3449 3450 tf_t *tf;
3450 3451 tcp_t **tcpp;
3451 3452 tcp_t *tcpnext;
3452 3453 tcp_stack_t *tcps = tcp->tcp_tcps;
3453 3454
3454 3455 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
3455 3456
3456 3457 if (tcp->tcp_ptpahn != NULL)
3457 3458 tcp_acceptor_hash_remove(tcp);
3458 3459 tcpp = &tf->tf_tcp;
3459 3460 mutex_enter(&tf->tf_lock);
3460 3461 tcpnext = tcpp[0];
3461 3462 if (tcpnext)
3462 3463 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash;
3463 3464 tcp->tcp_acceptor_hash = tcpnext;
3464 3465 tcp->tcp_ptpahn = tcpp;
3465 3466 tcpp[0] = tcp;
3466 3467 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */
3467 3468 mutex_exit(&tf->tf_lock);
3468 3469 }
3469 3470
3470 3471 /*
3471 3472 * Hash list removal routine for tcp_t structures.
3472 3473 */
3473 3474 void
3474 3475 tcp_acceptor_hash_remove(tcp_t *tcp)
3475 3476 {
3476 3477 tcp_t *tcpnext;
3477 3478 kmutex_t *lockp;
3478 3479
3479 3480 /*
3480 3481 * Extract the lock pointer in case there are concurrent
3481 3482 * hash_remove's for this instance.
3482 3483 */
3483 3484 lockp = tcp->tcp_acceptor_lockp;
3484 3485
3485 3486 if (tcp->tcp_ptpahn == NULL)
3486 3487 return;
3487 3488
3488 3489 ASSERT(lockp != NULL);
3489 3490 mutex_enter(lockp);
3490 3491 if (tcp->tcp_ptpahn) {
3491 3492 tcpnext = tcp->tcp_acceptor_hash;
3492 3493 if (tcpnext) {
3493 3494 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn;
3494 3495 tcp->tcp_acceptor_hash = NULL;
3495 3496 }
3496 3497 *tcp->tcp_ptpahn = tcpnext;
3497 3498 tcp->tcp_ptpahn = NULL;
3498 3499 }
3499 3500 mutex_exit(lockp);
3500 3501 tcp->tcp_acceptor_lockp = NULL;
3501 3502 }
3502 3503
3503 3504 /*
3504 3505 * Type three generator adapted from the random() function in 4.4 BSD:
3505 3506 */
3506 3507
3507 3508 /*
3508 3509 * Copyright (c) 1983, 1993
3509 3510 * The Regents of the University of California. All rights reserved.
3510 3511 *
3511 3512 * Redistribution and use in source and binary forms, with or without
3512 3513 * modification, are permitted provided that the following conditions
3513 3514 * are met:
3514 3515 * 1. Redistributions of source code must retain the above copyright
3515 3516 * notice, this list of conditions and the following disclaimer.
3516 3517 * 2. Redistributions in binary form must reproduce the above copyright
3517 3518 * notice, this list of conditions and the following disclaimer in the
3518 3519 * documentation and/or other materials provided with the distribution.
3519 3520 * 3. All advertising materials mentioning features or use of this software
3520 3521 * must display the following acknowledgement:
3521 3522 * This product includes software developed by the University of
3522 3523 * California, Berkeley and its contributors.
3523 3524 * 4. Neither the name of the University nor the names of its contributors
3524 3525 * may be used to endorse or promote products derived from this software
3525 3526 * without specific prior written permission.
3526 3527 *
3527 3528 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
3528 3529 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3529 3530 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3530 3531 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
3531 3532 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3532 3533 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3533 3534 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3534 3535 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3535 3536 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3536 3537 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3537 3538 * SUCH DAMAGE.
3538 3539 */
3539 3540
3540 3541 /* Type 3 -- x**31 + x**3 + 1 */
3541 3542 #define DEG_3 31
3542 3543 #define SEP_3 3
3543 3544
3544 3545
3545 3546 /* Protected by tcp_random_lock */
3546 3547 static int tcp_randtbl[DEG_3 + 1];
3547 3548
3548 3549 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
3549 3550 static int *tcp_random_rptr = &tcp_randtbl[1];
3550 3551
3551 3552 static int *tcp_random_state = &tcp_randtbl[1];
3552 3553 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
3553 3554
3554 3555 kmutex_t tcp_random_lock;
3555 3556
3556 3557 void
3557 3558 tcp_random_init(void)
3558 3559 {
3559 3560 int i;
3560 3561 hrtime_t hrt;
3561 3562 time_t wallclock;
3562 3563 uint64_t result;
3563 3564
3564 3565 /*
3565 3566 * Use high-res timer and current time for seed. Gethrtime() returns
3566 3567 * a longlong, which may contain resolution down to nanoseconds.
3567 3568 * The current time will either be a 32-bit or a 64-bit quantity.
3568 3569 * XOR the two together in a 64-bit result variable.
3569 3570 * Convert the result to a 32-bit value by multiplying the high-order
3570 3571 * 32-bits by the low-order 32-bits.
3571 3572 */
3572 3573
3573 3574 hrt = gethrtime();
3574 3575 (void) drv_getparm(TIME, &wallclock);
3575 3576 result = (uint64_t)wallclock ^ (uint64_t)hrt;
3576 3577 mutex_enter(&tcp_random_lock);
3577 3578 tcp_random_state[0] = ((result >> 32) & 0xffffffff) *
3578 3579 (result & 0xffffffff);
3579 3580
3580 3581 for (i = 1; i < DEG_3; i++)
3581 3582 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
3582 3583 + 12345;
3583 3584 tcp_random_fptr = &tcp_random_state[SEP_3];
3584 3585 tcp_random_rptr = &tcp_random_state[0];
3585 3586 mutex_exit(&tcp_random_lock);
3586 3587 for (i = 0; i < 10 * DEG_3; i++)
3587 3588 (void) tcp_random();
3588 3589 }
3589 3590
3590 3591 /*
3591 3592 * tcp_random: Return a random number in the range [1 - (128K + 1)].
3592 3593 * This range is selected to be approximately centered on TCP_ISS / 2,
3593 3594 * and easy to compute. We get this value by generating a 32-bit random
3594 3595 * number, selecting out the high-order 17 bits, and then adding one so
3595 3596 * that we never return zero.
3596 3597 */
3597 3598 int
3598 3599 tcp_random(void)
3599 3600 {
3600 3601 int i;
3601 3602
3602 3603 mutex_enter(&tcp_random_lock);
3603 3604 *tcp_random_fptr += *tcp_random_rptr;
3604 3605
3605 3606 /*
3606 3607 * The high-order bits are more random than the low-order bits,
3607 3608 * so we select out the high-order 17 bits and add one so that
3608 3609 * we never return zero.
3609 3610 */
3610 3611 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
3611 3612 if (++tcp_random_fptr >= tcp_random_end_ptr) {
3612 3613 tcp_random_fptr = tcp_random_state;
3613 3614 ++tcp_random_rptr;
3614 3615 } else if (++tcp_random_rptr >= tcp_random_end_ptr)
3615 3616 tcp_random_rptr = tcp_random_state;
3616 3617
3617 3618 mutex_exit(&tcp_random_lock);
3618 3619 return (i);
3619 3620 }
3620 3621
3621 3622 /*
3622 3623 * Split this function out so that if the secret changes, I'm okay.
3623 3624 *
3624 3625 * Initialize the tcp_iss_cookie and tcp_iss_key.
3625 3626 */
3626 3627
3627 3628 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */
3628 3629
3629 3630 void
3630 3631 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps)
3631 3632 {
3632 3633 struct {
3633 3634 int32_t current_time;
3634 3635 uint32_t randnum;
3635 3636 uint16_t pad;
3636 3637 uint8_t ether[6];
3637 3638 uint8_t passwd[PASSWD_SIZE];
3638 3639 } tcp_iss_cookie;
3639 3640 time_t t;
3640 3641
3641 3642 /*
3642 3643 * Start with the current absolute time.
3643 3644 */
3644 3645 (void) drv_getparm(TIME, &t);
3645 3646 tcp_iss_cookie.current_time = t;
3646 3647
3647 3648 /*
3648 3649 * XXX - Need a more random number per RFC 1750, not this crap.
3649 3650 * OTOH, if what follows is pretty random, then I'm in better shape.
3650 3651 */
3651 3652 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random());
3652 3653 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */
3653 3654
3654 3655 /*
3655 3656 * The cpu_type_info is pretty non-random. Ugggh. It does serve
3656 3657 * as a good template.
3657 3658 */
3658 3659 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd,
3659 3660 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info)));
3660 3661
3661 3662 /*
3662 3663 * The pass-phrase. Normally this is supplied by user-called NDD.
3663 3664 */
3664 3665 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len));
3665 3666
3666 3667 /*
3667 3668 * See 4010593 if this section becomes a problem again,
3668 3669 * but the local ethernet address is useful here.
3669 3670 */
3670 3671 (void) localetheraddr(NULL,
3671 3672 (struct ether_addr *)&tcp_iss_cookie.ether);
3672 3673
3673 3674 /*
3674 3675 * Hash 'em all together. The MD5Final is called per-connection.
3675 3676 */
3676 3677 mutex_enter(&tcps->tcps_iss_key_lock);
3677 3678 MD5Init(&tcps->tcps_iss_key);
3678 3679 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie,
3679 3680 sizeof (tcp_iss_cookie));
3680 3681 mutex_exit(&tcps->tcps_iss_key_lock);
3681 3682 }
3682 3683
3683 3684 /*
3684 3685 * Called by IP when IP is loaded into the kernel
3685 3686 */
3686 3687 void
3687 3688 tcp_ddi_g_init(void)
3688 3689 {
3689 3690 tcp_timercache = kmem_cache_create("tcp_timercache",
3690 3691 sizeof (tcp_timer_t) + sizeof (mblk_t), 0,
3691 3692 NULL, NULL, NULL, NULL, NULL, 0);
3692 3693
3693 3694 tcp_notsack_blk_cache = kmem_cache_create("tcp_notsack_blk_cache",
3694 3695 sizeof (notsack_blk_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
3695 3696
3696 3697 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL);
3697 3698
3698 3699 /* Initialize the random number generator */
3699 3700 tcp_random_init();
3700 3701
3701 3702 /* A single callback independently of how many netstacks we have */
3702 3703 ip_squeue_init(tcp_squeue_add);
3703 3704
3704 3705 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics);
3705 3706
3706 3707 tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput);
3707 3708
3708 3709 /*
3709 3710 * We want to be informed each time a stack is created or
3710 3711 * destroyed in the kernel, so we can maintain the
3711 3712 * set of tcp_stack_t's.
3712 3713 */
3713 3714 netstack_register(NS_TCP, tcp_stack_init, NULL, tcp_stack_fini);
3714 3715 }
3715 3716
3716 3717
3717 3718 #define INET_NAME "ip"
3718 3719
3719 3720 /*
3720 3721 * Initialize the TCP stack instance.
3721 3722 */
3722 3723 static void *
3723 3724 tcp_stack_init(netstackid_t stackid, netstack_t *ns)
3724 3725 {
3725 3726 tcp_stack_t *tcps;
3726 3727 int i;
3727 3728 int error = 0;
3728 3729 major_t major;
3729 3730 size_t arrsz;
3730 3731
3731 3732 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP);
3732 3733 tcps->tcps_netstack = ns;
3733 3734
3734 3735 /* Initialize locks */
3735 3736 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL);
3736 3737 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
3737 3738
3738 3739 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS;
3739 3740 tcps->tcps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
3740 3741 tcps->tcps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
3741 3742 tcps->tcps_min_anonpriv_port = 512;
3742 3743
3743 3744 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) *
3744 3745 TCP_BIND_FANOUT_SIZE, KM_SLEEP);
3745 3746 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) *
3746 3747 TCP_ACCEPTOR_FANOUT_SIZE, KM_SLEEP);
3747 3748
3748 3749 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3749 3750 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL,
3750 3751 MUTEX_DEFAULT, NULL);
3751 3752 }
3752 3753
3753 3754 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3754 3755 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL,
3755 3756 MUTEX_DEFAULT, NULL);
3756 3757 }
3757 3758
3758 3759 /* TCP's IPsec code calls the packet dropper. */
3759 3760 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement");
3760 3761
3761 3762 arrsz = tcp_propinfo_count * sizeof (mod_prop_info_t);
3762 3763 tcps->tcps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
3763 3764 KM_SLEEP);
3764 3765 bcopy(tcp_propinfo_tbl, tcps->tcps_propinfo_tbl, arrsz);
3765 3766
3766 3767 /*
3767 3768 * Note: To really walk the device tree you need the devinfo
3768 3769 * pointer to your device which is only available after probe/attach.
3769 3770 * The following is safe only because it uses ddi_root_node()
3770 3771 */
3771 3772 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr,
3772 3773 tcp_opt_obj.odb_opt_arr_cnt);
3773 3774
3774 3775 /*
3775 3776 * Initialize RFC 1948 secret values. This will probably be reset once
3776 3777 * by the boot scripts.
3777 3778 *
3778 3779 * Use NULL name, as the name is caught by the new lockstats.
3779 3780 *
3780 3781 * Initialize with some random, non-guessable string, like the global
3781 3782 * T_INFO_ACK.
3782 3783 */
3783 3784
3784 3785 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack,
3785 3786 sizeof (tcp_g_t_info_ack), tcps);
3786 3787
3787 3788 tcps->tcps_kstat = tcp_kstat2_init(stackid);
3788 3789 tcps->tcps_mibkp = tcp_kstat_init(stackid);
3789 3790
3790 3791 major = mod_name_to_major(INET_NAME);
3791 3792 error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident);
3792 3793 ASSERT(error == 0);
3793 3794 tcps->tcps_ixa_cleanup_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
3794 3795 ASSERT(tcps->tcps_ixa_cleanup_mp != NULL);
3795 3796 cv_init(&tcps->tcps_ixa_cleanup_cv, NULL, CV_DEFAULT, NULL);
3796 3797 mutex_init(&tcps->tcps_ixa_cleanup_lock, NULL, MUTEX_DEFAULT, NULL);
3797 3798
3798 3799 mutex_init(&tcps->tcps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
3799 3800 tcps->tcps_reclaim = B_FALSE;
3800 3801 tcps->tcps_reclaim_tid = 0;
3801 3802 tcps->tcps_reclaim_period = tcps->tcps_rexmit_interval_max;
3802 3803
3803 3804 /*
3804 3805 * ncpus is the current number of CPUs, which can be bigger than
3805 3806 * boot_ncpus. But we don't want to use ncpus to allocate all the
3806 3807 * tcp_stats_cpu_t at system boot up time since it will be 1. While
3807 3808 * we handle adding CPU in tcp_cpu_update(), it will be slow if
3808 3809 * there are many CPUs as we will be adding them 1 by 1.
3809 3810 *
3810 3811 * Note that tcps_sc_cnt never decreases and the tcps_sc[x] pointers
3811 3812 * are not freed until the stack is going away. So there is no need
3812 3813 * to grab a lock to access the per CPU tcps_sc[x] pointer.
3813 3814 */
3814 3815 mutex_enter(&cpu_lock);
3815 3816 tcps->tcps_sc_cnt = MAX(ncpus, boot_ncpus);
3816 3817 mutex_exit(&cpu_lock);
3817 3818 tcps->tcps_sc = kmem_zalloc(max_ncpus * sizeof (tcp_stats_cpu_t *),
3818 3819 KM_SLEEP);
3819 3820 for (i = 0; i < tcps->tcps_sc_cnt; i++) {
3820 3821 tcps->tcps_sc[i] = kmem_zalloc(sizeof (tcp_stats_cpu_t),
3821 3822 KM_SLEEP);
3822 3823 }
3823 3824
3824 3825 mutex_init(&tcps->tcps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
3825 3826 list_create(&tcps->tcps_listener_conf, sizeof (tcp_listener_t),
3826 3827 offsetof(tcp_listener_t, tl_link));
3827 3828
3828 3829 return (tcps);
3829 3830 }
3830 3831
3831 3832 /*
3832 3833 * Called when the IP module is about to be unloaded.
3833 3834 */
3834 3835 void
3835 3836 tcp_ddi_g_destroy(void)
3836 3837 {
3837 3838 tcp_g_kstat_fini(tcp_g_kstat);
3838 3839 tcp_g_kstat = NULL;
3839 3840 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics));
3840 3841
3841 3842 mutex_destroy(&tcp_random_lock);
3842 3843
3843 3844 kmem_cache_destroy(tcp_timercache);
3844 3845 kmem_cache_destroy(tcp_notsack_blk_cache);
3845 3846
3846 3847 netstack_unregister(NS_TCP);
3847 3848 }
3848 3849
3849 3850 /*
3850 3851 * Free the TCP stack instance.
3851 3852 */
3852 3853 static void
3853 3854 tcp_stack_fini(netstackid_t stackid, void *arg)
3854 3855 {
3855 3856 tcp_stack_t *tcps = (tcp_stack_t *)arg;
3856 3857 int i;
3857 3858
3858 3859 freeb(tcps->tcps_ixa_cleanup_mp);
3859 3860 tcps->tcps_ixa_cleanup_mp = NULL;
3860 3861 cv_destroy(&tcps->tcps_ixa_cleanup_cv);
3861 3862 mutex_destroy(&tcps->tcps_ixa_cleanup_lock);
3862 3863
3863 3864 /*
3864 3865 * Set tcps_reclaim to false tells tcp_reclaim_timer() not to restart
3865 3866 * the timer.
3866 3867 */
3867 3868 mutex_enter(&tcps->tcps_reclaim_lock);
3868 3869 tcps->tcps_reclaim = B_FALSE;
3869 3870 mutex_exit(&tcps->tcps_reclaim_lock);
3870 3871 if (tcps->tcps_reclaim_tid != 0)
3871 3872 (void) untimeout(tcps->tcps_reclaim_tid);
3872 3873 mutex_destroy(&tcps->tcps_reclaim_lock);
3873 3874
3874 3875 tcp_listener_conf_cleanup(tcps);
3875 3876
3876 3877 for (i = 0; i < tcps->tcps_sc_cnt; i++)
3877 3878 kmem_free(tcps->tcps_sc[i], sizeof (tcp_stats_cpu_t));
3878 3879 kmem_free(tcps->tcps_sc, max_ncpus * sizeof (tcp_stats_cpu_t *));
3879 3880
3880 3881 kmem_free(tcps->tcps_propinfo_tbl,
3881 3882 tcp_propinfo_count * sizeof (mod_prop_info_t));
3882 3883 tcps->tcps_propinfo_tbl = NULL;
3883 3884
3884 3885 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3885 3886 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL);
3886 3887 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock);
3887 3888 }
3888 3889
3889 3890 for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3890 3891 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL);
3891 3892 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock);
3892 3893 }
3893 3894
3894 3895 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE);
3895 3896 tcps->tcps_bind_fanout = NULL;
3896 3897
3897 3898 kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) *
3898 3899 TCP_ACCEPTOR_FANOUT_SIZE);
3899 3900 tcps->tcps_acceptor_fanout = NULL;
3900 3901
3901 3902 mutex_destroy(&tcps->tcps_iss_key_lock);
3902 3903 mutex_destroy(&tcps->tcps_epriv_port_lock);
3903 3904
3904 3905 ip_drop_unregister(&tcps->tcps_dropper);
3905 3906
3906 3907 tcp_kstat2_fini(stackid, tcps->tcps_kstat);
3907 3908 tcps->tcps_kstat = NULL;
3908 3909
3909 3910 tcp_kstat_fini(stackid, tcps->tcps_mibkp);
3910 3911 tcps->tcps_mibkp = NULL;
3911 3912
3912 3913 ldi_ident_release(tcps->tcps_ldi_ident);
3913 3914 kmem_free(tcps, sizeof (*tcps));
3914 3915 }
3915 3916
3916 3917 /*
3917 3918 * Generate ISS, taking into account NDD changes may happen halfway through.
3918 3919 * (If the iss is not zero, set it.)
3919 3920 */
3920 3921
3921 3922 static void
3922 3923 tcp_iss_init(tcp_t *tcp)
3923 3924 {
3924 3925 MD5_CTX context;
3925 3926 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg;
3926 3927 uint32_t answer[4];
3927 3928 tcp_stack_t *tcps = tcp->tcp_tcps;
3928 3929 conn_t *connp = tcp->tcp_connp;
3929 3930
3930 3931 tcps->tcps_iss_incr_extra += (tcps->tcps_iss_incr >> 1);
3931 3932 tcp->tcp_iss = tcps->tcps_iss_incr_extra;
3932 3933 switch (tcps->tcps_strong_iss) {
3933 3934 case 2:
3934 3935 mutex_enter(&tcps->tcps_iss_key_lock);
3935 3936 context = tcps->tcps_iss_key;
3936 3937 mutex_exit(&tcps->tcps_iss_key_lock);
3937 3938 arg.ports = connp->conn_ports;
3938 3939 arg.src = connp->conn_laddr_v6;
3939 3940 arg.dst = connp->conn_faddr_v6;
3940 3941 MD5Update(&context, (uchar_t *)&arg, sizeof (arg));
3941 3942 MD5Final((uchar_t *)answer, &context);
3942 3943 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3];
3943 3944 /*
3944 3945 * Now that we've hashed into a unique per-connection sequence
3945 3946 * space, add a random increment per strong_iss == 1. So I
3946 3947 * guess we'll have to...
3947 3948 */
3948 3949 /* FALLTHRU */
3949 3950 case 1:
3950 3951 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random();
3951 3952 break;
3952 3953 default:
3953 3954 tcp->tcp_iss += (uint32_t)gethrestime_sec() *
3954 3955 tcps->tcps_iss_incr;
3955 3956 break;
3956 3957 }
3957 3958 tcp->tcp_valid_bits = TCP_ISS_VALID;
3958 3959 tcp->tcp_fss = tcp->tcp_iss - 1;
3959 3960 tcp->tcp_suna = tcp->tcp_iss;
3960 3961 tcp->tcp_snxt = tcp->tcp_iss + 1;
3961 3962 tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
3962 3963 tcp->tcp_csuna = tcp->tcp_snxt;
3963 3964 }
3964 3965
3965 3966 /*
3966 3967 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL
3967 3968 * on the specified backing STREAMS q. Note, the caller may make the
3968 3969 * decision to call based on the tcp_t.tcp_flow_stopped value which
3969 3970 * when check outside the q's lock is only an advisory check ...
3970 3971 */
3971 3972 void
3972 3973 tcp_setqfull(tcp_t *tcp)
3973 3974 {
3974 3975 tcp_stack_t *tcps = tcp->tcp_tcps;
3975 3976 conn_t *connp = tcp->tcp_connp;
3976 3977
3977 3978 if (tcp->tcp_closed)
3978 3979 return;
3979 3980
3980 3981 conn_setqfull(connp, &tcp->tcp_flow_stopped);
3981 3982 if (tcp->tcp_flow_stopped)
3982 3983 TCP_STAT(tcps, tcp_flwctl_on);
3983 3984 }
3984 3985
3985 3986 void
3986 3987 tcp_clrqfull(tcp_t *tcp)
3987 3988 {
3988 3989 conn_t *connp = tcp->tcp_connp;
3989 3990
3990 3991 if (tcp->tcp_closed)
3991 3992 return;
3992 3993 conn_clrqfull(connp, &tcp->tcp_flow_stopped);
3993 3994 }
3994 3995
3995 3996 static int
3996 3997 tcp_squeue_switch(int val)
3997 3998 {
3998 3999 int rval = SQ_FILL;
3999 4000
4000 4001 switch (val) {
4001 4002 case 1:
4002 4003 rval = SQ_NODRAIN;
4003 4004 break;
4004 4005 case 2:
4005 4006 rval = SQ_PROCESS;
4006 4007 break;
4007 4008 default:
4008 4009 break;
4009 4010 }
4010 4011 return (rval);
4011 4012 }
4012 4013
4013 4014 /*
4014 4015 * This is called once for each squeue - globally for all stack
4015 4016 * instances.
4016 4017 */
4017 4018 static void
4018 4019 tcp_squeue_add(squeue_t *sqp)
4019 4020 {
4020 4021 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc(
4021 4022 sizeof (tcp_squeue_priv_t), KM_SLEEP);
4022 4023
4023 4024 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait;
4024 4025 if (tcp_free_list_max_cnt == 0) {
4025 4026 int tcp_ncpus = ((boot_max_ncpus == -1) ?
4026 4027 max_ncpus : boot_max_ncpus);
4027 4028
4028 4029 /*
4029 4030 * Limit number of entries to 1% of availble memory / tcp_ncpus
4030 4031 */
4031 4032 tcp_free_list_max_cnt = (freemem * PAGESIZE) /
4032 4033 (tcp_ncpus * sizeof (tcp_t) * 100);
4033 4034 }
4034 4035 tcp_time_wait->tcp_free_list_cnt = 0;
4035 4036 }
4036 4037 /*
4037 4038 * Return unix error is tli error is TSYSERR, otherwise return a negative
4038 4039 * tli error.
4039 4040 */
4040 4041 int
4041 4042 tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr,
4042 4043 boolean_t bind_to_req_port_only)
4043 4044 {
4044 4045 int error;
4045 4046 tcp_t *tcp = connp->conn_tcp;
4046 4047
4047 4048 if (tcp->tcp_state >= TCPS_BOUND) {
4048 4049 if (connp->conn_debug) {
4049 4050 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4050 4051 "tcp_bind: bad state, %d", tcp->tcp_state);
4051 4052 }
4052 4053 return (-TOUTSTATE);
4053 4054 }
4054 4055
4055 4056 error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only);
4056 4057 if (error != 0)
4057 4058 return (error);
4058 4059
4059 4060 ASSERT(tcp->tcp_state == TCPS_BOUND);
4060 4061 tcp->tcp_conn_req_max = 0;
4061 4062 return (0);
4062 4063 }
4063 4064
4064 4065 /*
4065 4066 * If the return value from this function is positive, it's a UNIX error.
4066 4067 * Otherwise, if it's negative, then the absolute value is a TLI error.
4067 4068 * the TPI routine tcp_tpi_connect() is a wrapper function for this.
4068 4069 */
4069 4070 int
4070 4071 tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len,
4071 4072 cred_t *cr, pid_t pid)
4072 4073 {
4073 4074 tcp_t *tcp = connp->conn_tcp;
4074 4075 sin_t *sin = (sin_t *)sa;
4075 4076 sin6_t *sin6 = (sin6_t *)sa;
4076 4077 ipaddr_t *dstaddrp;
4077 4078 in_port_t dstport;
4078 4079 uint_t srcid;
4079 4080 int error;
4080 4081 uint32_t mss;
4081 4082 mblk_t *syn_mp;
4082 4083 tcp_stack_t *tcps = tcp->tcp_tcps;
4083 4084 int32_t oldstate;
4084 4085 ip_xmit_attr_t *ixa = connp->conn_ixa;
4085 4086
4086 4087 oldstate = tcp->tcp_state;
4087 4088
4088 4089 switch (len) {
4089 4090 default:
4090 4091 /*
4091 4092 * Should never happen
4092 4093 */
4093 4094 return (EINVAL);
4094 4095
4095 4096 case sizeof (sin_t):
4096 4097 sin = (sin_t *)sa;
4097 4098 if (sin->sin_port == 0) {
4098 4099 return (-TBADADDR);
4099 4100 }
4100 4101 if (connp->conn_ipv6_v6only) {
4101 4102 return (EAFNOSUPPORT);
4102 4103 }
4103 4104 break;
4104 4105
4105 4106 case sizeof (sin6_t):
4106 4107 sin6 = (sin6_t *)sa;
4107 4108 if (sin6->sin6_port == 0) {
4108 4109 return (-TBADADDR);
4109 4110 }
4110 4111 break;
4111 4112 }
4112 4113 /*
4113 4114 * If we're connecting to an IPv4-mapped IPv6 address, we need to
4114 4115 * make sure that the conn_ipversion is IPV4_VERSION. We
4115 4116 * need to this before we call tcp_bindi() so that the port lookup
4116 4117 * code will look for ports in the correct port space (IPv4 and
4117 4118 * IPv6 have separate port spaces).
4118 4119 */
4119 4120 if (connp->conn_family == AF_INET6 &&
4120 4121 connp->conn_ipversion == IPV6_VERSION &&
4121 4122 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4122 4123 if (connp->conn_ipv6_v6only)
4123 4124 return (EADDRNOTAVAIL);
4124 4125
4125 4126 connp->conn_ipversion = IPV4_VERSION;
4126 4127 }
4127 4128
4128 4129 switch (tcp->tcp_state) {
4129 4130 case TCPS_LISTEN:
4130 4131 /*
4131 4132 * Listening sockets are not allowed to issue connect().
4132 4133 */
4133 4134 if (IPCL_IS_NONSTR(connp))
4134 4135 return (EOPNOTSUPP);
4135 4136 /* FALLTHRU */
4136 4137 case TCPS_IDLE:
4137 4138 /*
4138 4139 * We support quick connect, refer to comments in
4139 4140 * tcp_connect_*()
4140 4141 */
4141 4142 /* FALLTHRU */
4142 4143 case TCPS_BOUND:
4143 4144 break;
4144 4145 default:
4145 4146 return (-TOUTSTATE);
4146 4147 }
4147 4148
4148 4149 /*
4149 4150 * We update our cred/cpid based on the caller of connect
4150 4151 */
4151 4152 if (connp->conn_cred != cr) {
4152 4153 crhold(cr);
4153 4154 crfree(connp->conn_cred);
4154 4155 connp->conn_cred = cr;
4155 4156 }
4156 4157 connp->conn_cpid = pid;
4157 4158
4158 4159 /* Cache things in the ixa without any refhold */
4159 4160 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4160 4161 ixa->ixa_cred = cr;
4161 4162 ixa->ixa_cpid = pid;
4162 4163 if (is_system_labeled()) {
4163 4164 /* We need to restart with a label based on the cred */
4164 4165 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
4165 4166 }
4166 4167
4167 4168 if (connp->conn_family == AF_INET6) {
4168 4169 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4169 4170 error = tcp_connect_ipv6(tcp, &sin6->sin6_addr,
4170 4171 sin6->sin6_port, sin6->sin6_flowinfo,
4171 4172 sin6->__sin6_src_id, sin6->sin6_scope_id);
4172 4173 } else {
4173 4174 /*
4174 4175 * Destination adress is mapped IPv6 address.
4175 4176 * Source bound address should be unspecified or
4176 4177 * IPv6 mapped address as well.
4177 4178 */
4178 4179 if (!IN6_IS_ADDR_UNSPECIFIED(
4179 4180 &connp->conn_bound_addr_v6) &&
4180 4181 !IN6_IS_ADDR_V4MAPPED(&connp->conn_bound_addr_v6)) {
4181 4182 return (EADDRNOTAVAIL);
4182 4183 }
4183 4184 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr));
4184 4185 dstport = sin6->sin6_port;
4185 4186 srcid = sin6->__sin6_src_id;
4186 4187 error = tcp_connect_ipv4(tcp, dstaddrp, dstport,
4187 4188 srcid);
4188 4189 }
4189 4190 } else {
4190 4191 dstaddrp = &sin->sin_addr.s_addr;
4191 4192 dstport = sin->sin_port;
4192 4193 srcid = 0;
4193 4194 error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid);
4194 4195 }
4195 4196
4196 4197 if (error != 0)
4197 4198 goto connect_failed;
4198 4199
4199 4200 CL_INET_CONNECT(connp, B_TRUE, error);
4200 4201 if (error != 0)
4201 4202 goto connect_failed;
4202 4203
4203 4204 /* connect succeeded */
4204 4205 TCPS_BUMP_MIB(tcps, tcpActiveOpens);
4205 4206 tcp->tcp_active_open = 1;
4206 4207
4207 4208 /*
4208 4209 * tcp_set_destination() does not adjust for TCP/IP header length.
4209 4210 */
4210 4211 mss = tcp->tcp_mss - connp->conn_ht_iphc_len;
4211 4212
4212 4213 /*
4213 4214 * Just make sure our rwnd is at least rcvbuf * MSS large, and round up
4214 4215 * to the nearest MSS.
4215 4216 *
4216 4217 * We do the round up here because we need to get the interface MTU
4217 4218 * first before we can do the round up.
4218 4219 */
4219 4220 tcp->tcp_rwnd = connp->conn_rcvbuf;
4220 4221 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
4221 4222 tcps->tcps_recv_hiwat_minmss * mss);
4222 4223 connp->conn_rcvbuf = tcp->tcp_rwnd;
4223 4224 tcp_set_ws_value(tcp);
4224 4225 tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
4225 4226 if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always)
4226 4227 tcp->tcp_snd_ws_ok = B_TRUE;
4227 4228
4228 4229 /*
4229 4230 * Set tcp_snd_ts_ok to true
4230 4231 * so that tcp_xmit_mp will
4231 4232 * include the timestamp
4232 4233 * option in the SYN segment.
4233 4234 */
4234 4235 if (tcps->tcps_tstamp_always ||
4235 4236 (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) {
4236 4237 tcp->tcp_snd_ts_ok = B_TRUE;
4237 4238 }
4238 4239
4239 4240 /*
4240 4241 * Note that tcp_snd_sack_ok can be set in tcp_set_destination() if
4241 4242 * the SACK metric is set. So here we just check the per stack SACK
4242 4243 * permitted param.
4243 4244 */
4244 4245 if (tcps->tcps_sack_permitted == 2) {
4245 4246 ASSERT(tcp->tcp_num_sack_blk == 0);
4246 4247 ASSERT(tcp->tcp_notsack_list == NULL);
4247 4248 tcp->tcp_snd_sack_ok = B_TRUE;
4248 4249 }
4249 4250
4250 4251 /*
4251 4252 * Should we use ECN? Note that the current
4252 4253 * default value (SunOS 5.9) of tcp_ecn_permitted
4253 4254 * is 1. The reason for doing this is that there
4254 4255 * are equipments out there that will drop ECN
4255 4256 * enabled IP packets. Setting it to 1 avoids
4256 4257 * compatibility problems.
4257 4258 */
4258 4259 if (tcps->tcps_ecn_permitted == 2)
4259 4260 tcp->tcp_ecn_ok = B_TRUE;
4260 4261
4261 4262 /* Trace change from BOUND -> SYN_SENT here */
4262 4263 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4263 4264 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4264 4265 int32_t, TCPS_BOUND);
4265 4266
4266 4267 TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4267 4268 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
4268 4269 tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
4269 4270 if (syn_mp != NULL) {
4270 4271 /*
4271 4272 * We must bump the generation before sending the syn
4272 4273 * to ensure that we use the right generation in case
4273 4274 * this thread issues a "connected" up call.
4274 4275 */
4275 4276 SOCK_CONNID_BUMP(tcp->tcp_connid);
4276 4277 /*
4277 4278 * DTrace sending the first SYN as a
4278 4279 * tcp:::connect-request event.
4279 4280 */
4280 4281 DTRACE_TCP5(connect__request, mblk_t *, NULL,
4281 4282 ip_xmit_attr_t *, connp->conn_ixa,
4282 4283 void_ip_t *, syn_mp->b_rptr, tcp_t *, tcp,
4283 4284 tcph_t *,
4284 4285 &syn_mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
4285 4286 tcp_send_data(tcp, syn_mp);
4286 4287 }
4287 4288
4288 4289 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4289 4290 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4290 4291 return (0);
4291 4292
4292 4293 connect_failed:
4293 4294 connp->conn_faddr_v6 = ipv6_all_zeros;
4294 4295 connp->conn_fport = 0;
4295 4296 tcp->tcp_state = oldstate;
4296 4297 if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4297 4298 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4298 4299 return (error);
4299 4300 }
4300 4301
4301 4302 int
4302 4303 tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len,
4303 4304 int backlog, cred_t *cr, boolean_t bind_to_req_port_only)
4304 4305 {
4305 4306 tcp_t *tcp = connp->conn_tcp;
4306 4307 int error = 0;
4307 4308 tcp_stack_t *tcps = tcp->tcp_tcps;
4308 4309 int32_t oldstate;
4309 4310
4310 4311 /* All Solaris components should pass a cred for this operation. */
4311 4312 ASSERT(cr != NULL);
4312 4313
4313 4314 if (tcp->tcp_state >= TCPS_BOUND) {
4314 4315 if ((tcp->tcp_state == TCPS_BOUND ||
4315 4316 tcp->tcp_state == TCPS_LISTEN) && backlog > 0) {
4316 4317 /*
4317 4318 * Handle listen() increasing backlog.
4318 4319 * This is more "liberal" then what the TPI spec
4319 4320 * requires but is needed to avoid a t_unbind
4320 4321 * when handling listen() since the port number
4321 4322 * might be "stolen" between the unbind and bind.
4322 4323 */
4323 4324 goto do_listen;
4324 4325 }
4325 4326 if (connp->conn_debug) {
4326 4327 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4327 4328 "tcp_listen: bad state, %d", tcp->tcp_state);
4328 4329 }
4329 4330 return (-TOUTSTATE);
4330 4331 } else {
4331 4332 if (sa == NULL) {
4332 4333 sin6_t addr;
4333 4334 sin_t *sin;
4334 4335 sin6_t *sin6;
4335 4336
4336 4337 ASSERT(IPCL_IS_NONSTR(connp));
4337 4338 /* Do an implicit bind: Request for a generic port. */
4338 4339 if (connp->conn_family == AF_INET) {
4339 4340 len = sizeof (sin_t);
4340 4341 sin = (sin_t *)&addr;
4341 4342 *sin = sin_null;
4342 4343 sin->sin_family = AF_INET;
4343 4344 } else {
4344 4345 ASSERT(connp->conn_family == AF_INET6);
4345 4346 len = sizeof (sin6_t);
4346 4347 sin6 = (sin6_t *)&addr;
4347 4348 *sin6 = sin6_null;
4348 4349 sin6->sin6_family = AF_INET6;
4349 4350 }
4350 4351 sa = (struct sockaddr *)&addr;
4351 4352 }
4352 4353
4353 4354 error = tcp_bind_check(connp, sa, len, cr,
4354 4355 bind_to_req_port_only);
4355 4356 if (error)
4356 4357 return (error);
4357 4358 /* Fall through and do the fanout insertion */
4358 4359 }
4359 4360
4360 4361 do_listen:
4361 4362 ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN);
4362 4363 tcp->tcp_conn_req_max = backlog;
4363 4364 if (tcp->tcp_conn_req_max) {
4364 4365 if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min)
4365 4366 tcp->tcp_conn_req_max = tcps->tcps_conn_req_min;
4366 4367 if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q)
4367 4368 tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q;
4368 4369 /*
4369 4370 * If this is a listener, do not reset the eager list
4370 4371 * and other stuffs. Note that we don't check if the
4371 4372 * existing eager list meets the new tcp_conn_req_max
4372 4373 * requirement.
4373 4374 */
4374 4375 if (tcp->tcp_state != TCPS_LISTEN) {
4375 4376 tcp->tcp_state = TCPS_LISTEN;
4376 4377 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4377 4378 connp->conn_ixa, void, NULL, tcp_t *, tcp,
4378 4379 void, NULL, int32_t, TCPS_BOUND);
4379 4380 /* Initialize the chain. Don't need the eager_lock */
4380 4381 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
4381 4382 tcp->tcp_eager_next_drop_q0 = tcp;
4382 4383 tcp->tcp_eager_prev_drop_q0 = tcp;
4383 4384 tcp->tcp_second_ctimer_threshold =
4384 4385 tcps->tcps_ip_abort_linterval;
4385 4386 }
4386 4387 }
4387 4388
4388 4389 /*
4389 4390 * We need to make sure that the conn_recv is set to a non-null
4390 4391 * value before we insert the conn into the classifier table.
4391 4392 * This is to avoid a race with an incoming packet which does an
4392 4393 * ipcl_classify().
4393 4394 * We initially set it to tcp_input_listener_unbound to try to
4394 4395 * pick a good squeue for the listener when the first SYN arrives.
4395 4396 * tcp_input_listener_unbound sets it to tcp_input_listener on that
4396 4397 * first SYN.
4397 4398 */
4398 4399 connp->conn_recv = tcp_input_listener_unbound;
4399 4400
4400 4401 /* Insert the listener in the classifier table */
4401 4402 error = ip_laddr_fanout_insert(connp);
4402 4403 if (error != 0) {
4403 4404 /* Undo the bind - release the port number */
4404 4405 oldstate = tcp->tcp_state;
4405 4406 tcp->tcp_state = TCPS_IDLE;
4406 4407 DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
4407 4408 connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
4408 4409 int32_t, oldstate);
4409 4410 connp->conn_bound_addr_v6 = ipv6_all_zeros;
4410 4411
4411 4412 connp->conn_laddr_v6 = ipv6_all_zeros;
4412 4413 connp->conn_saddr_v6 = ipv6_all_zeros;
4413 4414 connp->conn_ports = 0;
4414 4415
4415 4416 if (connp->conn_anon_port) {
4416 4417 zone_t *zone;
4417 4418
4418 4419 zone = crgetzone(cr);
4419 4420 connp->conn_anon_port = B_FALSE;
4420 4421 (void) tsol_mlp_anon(zone, connp->conn_mlp_type,
4421 4422 connp->conn_proto, connp->conn_lport, B_FALSE);
4422 4423 }
4423 4424 connp->conn_mlp_type = mlptSingle;
4424 4425
4425 4426 tcp_bind_hash_remove(tcp);
4426 4427 return (error);
4427 4428 } else {
4428 4429 /*
4429 4430 * If there is a connection limit, allocate and initialize
4430 4431 * the counter struct. Note that since listen can be called
4431 4432 * multiple times, the struct may have been allready allocated.
4432 4433 */
4433 4434 if (!list_is_empty(&tcps->tcps_listener_conf) &&
4434 4435 tcp->tcp_listen_cnt == NULL) {
4435 4436 tcp_listen_cnt_t *tlc;
4436 4437 uint32_t ratio;
4437 4438
4438 4439 ratio = tcp_find_listener_conf(tcps,
4439 4440 ntohs(connp->conn_lport));
4440 4441 if (ratio != 0) {
4441 4442 uint32_t mem_ratio, tot_buf;
4442 4443
4443 4444 tlc = kmem_alloc(sizeof (tcp_listen_cnt_t),
4444 4445 KM_SLEEP);
4445 4446 /*
4446 4447 * Calculate the connection limit based on
4447 4448 * the configured ratio and maxusers. Maxusers
4448 4449 * are calculated based on memory size,
4449 4450 * ~ 1 user per MB. Note that the conn_rcvbuf
4450 4451 * and conn_sndbuf may change after a
4451 4452 * connection is accepted. So what we have
4452 4453 * is only an approximation.
4453 4454 */
4454 4455 if ((tot_buf = connp->conn_rcvbuf +
4455 4456 connp->conn_sndbuf) < MB) {
4456 4457 mem_ratio = MB / tot_buf;
4457 4458 tlc->tlc_max = maxusers / ratio *
4458 4459 mem_ratio;
4459 4460 } else {
4460 4461 mem_ratio = tot_buf / MB;
4461 4462 tlc->tlc_max = maxusers / ratio /
4462 4463 mem_ratio;
4463 4464 }
4464 4465 /* At least we should allow two connections! */
4465 4466 if (tlc->tlc_max <= tcp_min_conn_listener)
4466 4467 tlc->tlc_max = tcp_min_conn_listener;
4467 4468 tlc->tlc_cnt = 1;
4468 4469 tlc->tlc_drop = 0;
4469 4470 tcp->tcp_listen_cnt = tlc;
4470 4471 }
4471 4472 }
4472 4473 }
4473 4474 return (error);
4474 4475 }
↓ open down ↓ |
1743 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX