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