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