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