xref: /titanic_41/usr/src/uts/common/inet/ip/ipclassifier.c (revision 74e20cfe817b82802b16fac8690dadcda76f54f5)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 const char ipclassifier_version[] = "@(#)ipclassifier.c	%I%	%E% SMI";
29 
30 /*
31  * IP PACKET CLASSIFIER
32  *
33  * The IP packet classifier provides mapping between IP packets and persistent
34  * connection state for connection-oriented protocols. It also provides
35  * interface for managing connection states.
36  *
37  * The connection state is kept in conn_t data structure and contains, among
38  * other things:
39  *
40  *	o local/remote address and ports
41  *	o Transport protocol
42  *	o squeue for the connection (for TCP only)
43  *	o reference counter
44  *	o Connection state
45  *	o hash table linkage
46  *	o interface/ire information
47  *	o credentials
48  *	o ipsec policy
49  *	o send and receive functions.
50  *	o mutex lock.
51  *
52  * Connections use a reference counting scheme. They are freed when the
53  * reference counter drops to zero. A reference is incremented when connection
54  * is placed in a list or table, when incoming packet for the connection arrives
55  * and when connection is processed via squeue (squeue processing may be
56  * asynchronous and the reference protects the connection from being destroyed
57  * before its processing is finished).
58  *
59  * send and receive functions are currently used for TCP only. The send function
60  * determines the IP entry point for the packet once it leaves TCP to be sent to
61  * the destination address. The receive function is used by IP when the packet
62  * should be passed for TCP processing. When a new connection is created these
63  * are set to ip_output() and tcp_input() respectively. During the lifetime of
64  * the connection the send and receive functions may change depending on the
65  * changes in the connection state. For example, Once the connection is bound to
66  * an addresse, the receive function for this connection is set to
67  * tcp_conn_request().  This allows incoming SYNs to go directly into the
68  * listener SYN processing function without going to tcp_input() first.
69  *
70  * Classifier uses several hash tables:
71  *
72  * 	ipcl_conn_fanout:	contains all TCP connections in CONNECTED state
73  *	ipcl_bind_fanout:	contains all connections in BOUND state
74  *	ipcl_proto_fanout:	IPv4 protocol fanout
75  *	ipcl_proto_fanout_v6:	IPv6 protocol fanout
76  *	ipcl_udp_fanout:	contains all UDP connections
77  *	ipcl_globalhash_fanout:	contains all connections
78  *
79  * The ipcl_globalhash_fanout is used for any walkers (like snmp and Clustering)
80  * which need to view all existing connections.
81  *
82  * All tables are protected by per-bucket locks. When both per-bucket lock and
83  * connection lock need to be held, the per-bucket lock should be acquired
84  * first, followed by the connection lock.
85  *
86  * All functions doing search in one of these tables increment a reference
87  * counter on the connection found (if any). This reference should be dropped
88  * when the caller has finished processing the connection.
89  *
90  *
91  * INTERFACES:
92  * ===========
93  *
94  * Connection Lookup:
95  * ------------------
96  *
97  * conn_t *ipcl_classify_v4(mp, protocol, hdr_len, zoneid)
98  * conn_t *ipcl_classify_v6(mp, protocol, hdr_len, zoneid)
99  *
100  * Finds connection for an incoming IPv4 or IPv6 packet. Returns NULL if
101  * it can't find any associated connection. If the connection is found, its
102  * reference counter is incremented.
103  *
104  *	mp:	mblock, containing packet header. The full header should fit
105  *		into a single mblock. It should also contain at least full IP
106  *		and TCP or UDP header.
107  *
108  *	protocol: Either IPPROTO_TCP or IPPROTO_UDP.
109  *
110  *	hdr_len: The size of IP header. It is used to find TCP or UDP header in
111  *		 the packet.
112  *
113  * 	zoneid: The zone in which the returned connection must be; the zoneid
114  *		corresponding to the ire_zoneid on the IRE located for the
115  *		packet's destination address.
116  *
117  *	For TCP connections, the lookup order is as follows:
118  *		5-tuple {src, dst, protocol, local port, remote port}
119  *			lookup in ipcl_conn_fanout table.
120  *		3-tuple {dst, remote port, protocol} lookup in
121  *			ipcl_bind_fanout table.
122  *
123  *	For UDP connections, a 5-tuple {src, dst, protocol, local port,
124  *	remote port} lookup is done on ipcl_udp_fanout. Note that,
125  *	these interfaces do not handle cases where a packets belongs
126  *	to multiple UDP clients, which is handled in IP itself.
127  *
128  * If the destination IRE is ALL_ZONES (indicated by zoneid), then we must
129  * determine which actual zone gets the segment.  This is used only in a
130  * labeled environment.  The matching rules are:
131  *
132  *	- If it's not a multilevel port, then the label on the packet selects
133  *	  the zone.  Unlabeled packets are delivered to the global zone.
134  *
135  *	- If it's a multilevel port, then only the zone registered to receive
136  *	  packets on that port matches.
137  *
138  * Also, in a labeled environment, packet labels need to be checked.  For fully
139  * bound TCP connections, we can assume that the packet label was checked
140  * during connection establishment, and doesn't need to be checked on each
141  * packet.  For others, though, we need to check for strict equality or, for
142  * multilevel ports, membership in the range or set.  This part currently does
143  * a tnrh lookup on each packet, but could be optimized to use cached results
144  * if that were necessary.  (SCTP doesn't come through here, but if it did,
145  * we would apply the same rules as TCP.)
146  *
147  * An implication of the above is that fully-bound TCP sockets must always use
148  * distinct 4-tuples; they can't be discriminated by label alone.
149  *
150  * Note that we cannot trust labels on packets sent to fully-bound UDP sockets,
151  * as there's no connection set-up handshake and no shared state.
152  *
153  * Labels on looped-back packets within a single zone do not need to be
154  * checked, as all processes in the same zone have the same label.
155  *
156  * Finally, for unlabeled packets received by a labeled system, special rules
157  * apply.  We consider only the MLP if there is one.  Otherwise, we prefer a
158  * socket in the zone whose label matches the default label of the sender, if
159  * any.  In any event, the receiving socket must have SO_MAC_EXEMPT set and the
160  * receiver's label must dominate the sender's default label.
161  *
162  * conn_t	*ipcl_tcp_lookup_reversed_ipv4(ipha_t *, tcph_t *, int);
163  * conn_t	*ipcl_tcp_lookup_reversed_ipv6(ip6_t *, tcpha_t *, int, uint_t);
164  *
165  *	Lookup routine to find a exact match for {src, dst, local port,
166  *	remote port) for TCP connections in ipcl_conn_fanout. The address and
167  *	ports are read from the IP and TCP header respectively.
168  *
169  * conn_t	*ipcl_lookup_listener_v4(lport, laddr, protocol);
170  * conn_t	*ipcl_lookup_listener_v6(lport, laddr, protocol, ifindex);
171  *
172  * 	Lookup routine to find a listener with the tuple {lport, laddr,
173  * 	protocol} in the ipcl_bind_fanout table. For IPv6, an additional
174  * 	parameter interface index is also compared.
175  *
176  * void ipcl_walk(func, arg)
177  *
178  * 	Apply 'func' to every connection available. The 'func' is called as
179  *	(*func)(connp, arg). The walk is non-atomic so connections may be
180  *	created and destroyed during the walk. The CONN_CONDEMNED and
181  *	CONN_INCIPIENT flags ensure that connections which are newly created
182  *	or being destroyed are not selected by the walker.
183  *
184  * Table Updates
185  * -------------
186  *
187  * int ipcl_conn_insert(connp, protocol, src, dst, ports)
188  * int ipcl_conn_insert_v6(connp, protocol, src, dst, ports, ifindex)
189  *
190  *	Insert 'connp' in the ipcl_conn_fanout.
191  *	Arguements :
192  *		connp		conn_t to be inserted
193  *		protocol	connection protocol
194  *		src		source address
195  *		dst		destination address
196  *		ports		local and remote port
197  *		ifindex		interface index for IPv6 connections
198  *
199  *	Return value :
200  *		0		if connp was inserted
201  *		EADDRINUSE	if the connection with the same tuple
202  *				already exists.
203  *
204  * int ipcl_bind_insert(connp, protocol, src, lport);
205  * int ipcl_bind_insert_v6(connp, protocol, src, lport);
206  *
207  * 	Insert 'connp' in ipcl_bind_fanout.
208  * 	Arguements :
209  * 		connp		conn_t to be inserted
210  * 		protocol	connection protocol
211  * 		src		source address connection wants
212  * 				to bind to
213  * 		lport		local port connection wants to
214  * 				bind to
215  *
216  *
217  * void ipcl_hash_remove(connp);
218  *
219  * 	Removes the 'connp' from the connection fanout table.
220  *
221  * Connection Creation/Destruction
222  * -------------------------------
223  *
224  * conn_t *ipcl_conn_create(type, sleep)
225  *
226  * 	Creates a new conn based on the type flag, inserts it into
227  * 	globalhash table.
228  *
229  *	type:	This flag determines the type of conn_t which needs to be
230  *		created.
231  *		IPCL_TCPCONN	indicates a TCP connection
232  *		IPCL_IPCONN	indicates all non-TCP connections.
233  *
234  * void ipcl_conn_destroy(connp)
235  *
236  * 	Destroys the connection state, removes it from the global
237  * 	connection hash table and frees its memory.
238  */
239 
240 #include <sys/types.h>
241 #include <sys/stream.h>
242 #include <sys/stropts.h>
243 #include <sys/sysmacros.h>
244 #include <sys/strsubr.h>
245 #include <sys/strsun.h>
246 #define	_SUN_TPI_VERSION 2
247 #include <sys/ddi.h>
248 #include <sys/cmn_err.h>
249 #include <sys/debug.h>
250 
251 #include <sys/systm.h>
252 #include <sys/param.h>
253 #include <sys/kmem.h>
254 #include <sys/isa_defs.h>
255 #include <inet/common.h>
256 #include <netinet/ip6.h>
257 #include <netinet/icmp6.h>
258 
259 #include <inet/ip.h>
260 #include <inet/ip6.h>
261 #include <inet/tcp.h>
262 #include <inet/ip_ndp.h>
263 #include <inet/udp_impl.h>
264 #include <inet/sctp_ip.h>
265 
266 #include <sys/cpuvar.h>
267 
268 #include <inet/ipclassifier.h>
269 #include <inet/ipsec_impl.h>
270 
271 #include <sys/tsol/tnet.h>
272 
273 #ifdef DEBUG
274 #define	IPCL_DEBUG
275 #else
276 #undef	IPCL_DEBUG
277 #endif
278 
279 #ifdef	IPCL_DEBUG
280 int	ipcl_debug_level = 0;
281 #define	IPCL_DEBUG_LVL(level, args)	\
282 	if (ipcl_debug_level  & level) { printf args; }
283 #else
284 #define	IPCL_DEBUG_LVL(level, args) {; }
285 #endif
286 connf_t	*ipcl_conn_fanout;
287 connf_t	*ipcl_bind_fanout;
288 connf_t	ipcl_proto_fanout[IPPROTO_MAX + 1];
289 connf_t	ipcl_proto_fanout_v6[IPPROTO_MAX + 1];
290 connf_t	*ipcl_udp_fanout;
291 
292 /* A separate hash list for raw socket. */
293 connf_t *ipcl_raw_fanout;
294 
295 connf_t rts_clients;
296 
297 /* Old value for compatibility */
298 uint_t tcp_conn_hash_size = 0;
299 
300 /* New value. Zero means choose automatically. */
301 uint_t ipcl_conn_hash_size = 0;
302 uint_t ipcl_conn_hash_memfactor = 8192;
303 uint_t ipcl_conn_hash_maxsize = 82500;
304 
305 uint_t ipcl_conn_fanout_size = 0;
306 
307 
308 /* bind/udp fanout table size */
309 uint_t ipcl_bind_fanout_size = 512;
310 uint_t ipcl_udp_fanout_size = 16384;
311 
312 /* Raw socket fanout size.  Must be a power of 2. */
313 uint_t ipcl_raw_fanout_size = 256;
314 
315 /*
316  * Power of 2^N Primes useful for hashing for N of 0-28,
317  * these primes are the nearest prime <= 2^N - 2^(N-2).
318  */
319 
320 #define	P2Ps() {0, 0, 0, 5, 11, 23, 47, 89, 191, 383, 761, 1531, 3067,	\
321 		6143, 12281, 24571, 49139, 98299, 196597, 393209,	\
322 		786431, 1572853, 3145721, 6291449, 12582893, 25165813,	\
323 		50331599, 100663291, 201326557, 0}
324 
325 /*
326  * wrapper structure to ensure that conn+tcpb are aligned
327  * on cache lines.
328  */
329 typedef struct itc_s {
330 	union {
331 		conn_t	itcu_conn;
332 		char	itcu_filler[CACHE_ALIGN(conn_s)];
333 	}	itc_u;
334 	tcp_t	itc_tcp;
335 } itc_t;
336 
337 #define	itc_conn	itc_u.itcu_conn
338 
339 struct kmem_cache  *ipcl_tcpconn_cache;
340 struct kmem_cache  *ipcl_tcp_cache;
341 struct kmem_cache  *ipcl_conn_cache;
342 extern struct kmem_cache  *sctp_conn_cache;
343 extern struct kmem_cache  *tcp_sack_info_cache;
344 extern struct kmem_cache  *tcp_iphc_cache;
345 
346 extern void	tcp_timermp_free(tcp_t *);
347 extern mblk_t	*tcp_timermp_alloc(int);
348 
349 static int	ipcl_tcpconn_constructor(void *, void *, int);
350 static void	ipcl_tcpconn_destructor(void *, void *);
351 
352 static int conn_g_index;
353 connf_t	*ipcl_globalhash_fanout;
354 
355 #ifdef	IPCL_DEBUG
356 #define	INET_NTOA_BUFSIZE	18
357 
358 static char *
359 inet_ntoa_r(uint32_t in, char *b)
360 {
361 	unsigned char	*p;
362 
363 	p = (unsigned char *)&in;
364 	(void) sprintf(b, "%d.%d.%d.%d", p[0], p[1], p[2], p[3]);
365 	return (b);
366 }
367 #endif
368 
369 /*
370  * ipclassifier intialization routine, sets up hash tables and
371  * conn caches.
372  */
373 void
374 ipcl_init(void)
375 {
376 	int i;
377 	int sizes[] = P2Ps();
378 
379 	ipcl_conn_cache = kmem_cache_create("ipcl_conn_cache",
380 	    sizeof (conn_t), CACHE_ALIGN_SIZE,
381 	    NULL, NULL, NULL, NULL, NULL, 0);
382 
383 	ipcl_tcpconn_cache = kmem_cache_create("ipcl_tcpconn_cache",
384 	    sizeof (itc_t), CACHE_ALIGN_SIZE,
385 	    ipcl_tcpconn_constructor, ipcl_tcpconn_destructor,
386 	    NULL, NULL, NULL, 0);
387 
388 	/*
389 	 * Calculate size of conn fanout table.
390 	 */
391 	if (ipcl_conn_hash_size != 0) {
392 		ipcl_conn_fanout_size = ipcl_conn_hash_size;
393 	} else if (tcp_conn_hash_size != 0) {
394 		ipcl_conn_fanout_size = tcp_conn_hash_size;
395 	} else {
396 		extern pgcnt_t freemem;
397 
398 		ipcl_conn_fanout_size =
399 		    (freemem * PAGESIZE) / ipcl_conn_hash_memfactor;
400 
401 		if (ipcl_conn_fanout_size > ipcl_conn_hash_maxsize)
402 			ipcl_conn_fanout_size = ipcl_conn_hash_maxsize;
403 	}
404 
405 	for (i = 9; i < sizeof (sizes) / sizeof (*sizes) - 1; i++) {
406 		if (sizes[i] >= ipcl_conn_fanout_size) {
407 			break;
408 		}
409 	}
410 	if ((ipcl_conn_fanout_size = sizes[i]) == 0) {
411 		/* Out of range, use the 2^16 value */
412 		ipcl_conn_fanout_size = sizes[16];
413 	}
414 	ipcl_conn_fanout = (connf_t *)kmem_zalloc(ipcl_conn_fanout_size *
415 	    sizeof (*ipcl_conn_fanout), KM_SLEEP);
416 
417 	for (i = 0; i < ipcl_conn_fanout_size; i++) {
418 		mutex_init(&ipcl_conn_fanout[i].connf_lock, NULL,
419 		    MUTEX_DEFAULT, NULL);
420 	}
421 
422 	ipcl_bind_fanout = (connf_t *)kmem_zalloc(ipcl_bind_fanout_size *
423 	    sizeof (*ipcl_bind_fanout), KM_SLEEP);
424 
425 	for (i = 0; i < ipcl_bind_fanout_size; i++) {
426 		mutex_init(&ipcl_bind_fanout[i].connf_lock, NULL,
427 		    MUTEX_DEFAULT, NULL);
428 	}
429 
430 	for (i = 0; i < A_CNT(ipcl_proto_fanout); i++) {
431 		mutex_init(&ipcl_proto_fanout[i].connf_lock, NULL,
432 		    MUTEX_DEFAULT, NULL);
433 	}
434 	for (i = 0; i < A_CNT(ipcl_proto_fanout_v6); i++) {
435 		mutex_init(&ipcl_proto_fanout_v6[i].connf_lock, NULL,
436 		    MUTEX_DEFAULT, NULL);
437 	}
438 
439 	mutex_init(&rts_clients.connf_lock, NULL, MUTEX_DEFAULT, NULL);
440 
441 	ipcl_udp_fanout = (connf_t *)kmem_zalloc(ipcl_udp_fanout_size *
442 	    sizeof (*ipcl_udp_fanout), KM_SLEEP);
443 
444 	for (i = 0; i < ipcl_udp_fanout_size; i++) {
445 		mutex_init(&ipcl_udp_fanout[i].connf_lock, NULL,
446 		    MUTEX_DEFAULT, NULL);
447 	}
448 
449 	ipcl_raw_fanout = (connf_t *)kmem_zalloc(ipcl_raw_fanout_size *
450 	    sizeof (*ipcl_raw_fanout), KM_SLEEP);
451 
452 	for (i = 0; i < ipcl_raw_fanout_size; i++) {
453 		mutex_init(&ipcl_raw_fanout[i].connf_lock, NULL,
454 		    MUTEX_DEFAULT, NULL);
455 	}
456 
457 	ipcl_globalhash_fanout = (connf_t *)kmem_zalloc(sizeof (connf_t) *
458 	    CONN_G_HASH_SIZE, KM_SLEEP);
459 
460 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
461 		mutex_init(&ipcl_globalhash_fanout[i].connf_lock, NULL,
462 		    MUTEX_DEFAULT, NULL);
463 	}
464 }
465 
466 void
467 ipcl_destroy(void)
468 {
469 	int i;
470 	kmem_cache_destroy(ipcl_conn_cache);
471 	kmem_cache_destroy(ipcl_tcpconn_cache);
472 	for (i = 0; i < ipcl_conn_fanout_size; i++)
473 		mutex_destroy(&ipcl_conn_fanout[i].connf_lock);
474 	kmem_free(ipcl_conn_fanout, ipcl_conn_fanout_size *
475 	    sizeof (*ipcl_conn_fanout));
476 	for (i = 0; i < ipcl_bind_fanout_size; i++)
477 		mutex_destroy(&ipcl_bind_fanout[i].connf_lock);
478 	kmem_free(ipcl_bind_fanout, ipcl_bind_fanout_size *
479 	    sizeof (*ipcl_bind_fanout));
480 
481 	for (i = 0; i < A_CNT(ipcl_proto_fanout); i++)
482 		mutex_destroy(&ipcl_proto_fanout[i].connf_lock);
483 	for (i = 0; i < A_CNT(ipcl_proto_fanout_v6); i++)
484 		mutex_destroy(&ipcl_proto_fanout_v6[i].connf_lock);
485 
486 	for (i = 0; i < ipcl_udp_fanout_size; i++)
487 		mutex_destroy(&ipcl_udp_fanout[i].connf_lock);
488 	kmem_free(ipcl_udp_fanout, ipcl_udp_fanout_size *
489 	    sizeof (*ipcl_udp_fanout));
490 
491 	for (i = 0; i < ipcl_raw_fanout_size; i++)
492 		mutex_destroy(&ipcl_raw_fanout[i].connf_lock);
493 	kmem_free(ipcl_raw_fanout, ipcl_raw_fanout_size *
494 	    sizeof (*ipcl_raw_fanout));
495 
496 	kmem_free(ipcl_globalhash_fanout, sizeof (connf_t) * CONN_G_HASH_SIZE);
497 	mutex_destroy(&rts_clients.connf_lock);
498 }
499 
500 /*
501  * conn creation routine. initialize the conn, sets the reference
502  * and inserts it in the global hash table.
503  */
504 conn_t *
505 ipcl_conn_create(uint32_t type, int sleep)
506 {
507 	itc_t	*itc;
508 	conn_t	*connp;
509 
510 	switch (type) {
511 	case IPCL_TCPCONN:
512 		if ((itc = kmem_cache_alloc(ipcl_tcpconn_cache,
513 		    sleep)) == NULL)
514 			return (NULL);
515 		connp = &itc->itc_conn;
516 		connp->conn_ref = 1;
517 		IPCL_DEBUG_LVL(1,
518 		    ("ipcl_conn_create: connp = %p tcp (%p)",
519 		    (void *)connp, (void *)connp->conn_tcp));
520 		ipcl_globalhash_insert(connp);
521 		break;
522 	case IPCL_SCTPCONN:
523 		if ((connp = kmem_cache_alloc(sctp_conn_cache, sleep)) == NULL)
524 			return (NULL);
525 		connp->conn_flags = IPCL_SCTPCONN;
526 		break;
527 	case IPCL_IPCCONN:
528 		connp = kmem_cache_alloc(ipcl_conn_cache, sleep);
529 		if (connp == NULL)
530 			return (NULL);
531 		bzero(connp, sizeof (conn_t));
532 		mutex_init(&connp->conn_lock, NULL, MUTEX_DEFAULT, NULL);
533 		cv_init(&connp->conn_cv, NULL, CV_DEFAULT, NULL);
534 		connp->conn_flags = IPCL_IPCCONN;
535 		connp->conn_ref = 1;
536 		IPCL_DEBUG_LVL(1,
537 		    ("ipcl_conn_create: connp = %p\n", (void *)connp));
538 		ipcl_globalhash_insert(connp);
539 		break;
540 	default:
541 		connp = NULL;
542 		ASSERT(0);
543 	}
544 
545 	return (connp);
546 }
547 
548 void
549 ipcl_conn_destroy(conn_t *connp)
550 {
551 	mblk_t	*mp;
552 
553 	ASSERT(!MUTEX_HELD(&connp->conn_lock));
554 	ASSERT(connp->conn_ref == 0);
555 	ASSERT(connp->conn_ire_cache == NULL);
556 
557 	if (connp->conn_peercred != NULL &&
558 	    connp->conn_peercred != connp->conn_cred)
559 		crfree(connp->conn_peercred);
560 	connp->conn_peercred = NULL;
561 
562 	if (connp->conn_cred != NULL) {
563 		crfree(connp->conn_cred);
564 		connp->conn_cred = NULL;
565 	}
566 
567 	ipcl_globalhash_remove(connp);
568 
569 	cv_destroy(&connp->conn_cv);
570 	if (connp->conn_flags & IPCL_TCPCONN) {
571 		tcp_t	*tcp = connp->conn_tcp;
572 
573 		mutex_destroy(&connp->conn_lock);
574 		ASSERT(connp->conn_tcp != NULL);
575 		tcp_free(tcp);
576 		mp = tcp->tcp_timercache;
577 		tcp->tcp_cred = NULL;
578 
579 		if (tcp->tcp_sack_info != NULL) {
580 			bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
581 			kmem_cache_free(tcp_sack_info_cache,
582 			    tcp->tcp_sack_info);
583 		}
584 		if (tcp->tcp_iphc != NULL) {
585 			if (tcp->tcp_hdr_grown) {
586 				kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len);
587 			} else {
588 				bzero(tcp->tcp_iphc, tcp->tcp_iphc_len);
589 				kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc);
590 			}
591 			tcp->tcp_iphc_len = 0;
592 		}
593 		ASSERT(tcp->tcp_iphc_len == 0);
594 
595 		if (connp->conn_latch != NULL)
596 			IPLATCH_REFRELE(connp->conn_latch);
597 		if (connp->conn_policy != NULL)
598 			IPPH_REFRELE(connp->conn_policy);
599 		bzero(connp, sizeof (itc_t));
600 
601 		tcp->tcp_timercache = mp;
602 		connp->conn_tcp = tcp;
603 		connp->conn_flags = IPCL_TCPCONN;
604 		connp->conn_ulp = IPPROTO_TCP;
605 		tcp->tcp_connp = connp;
606 		kmem_cache_free(ipcl_tcpconn_cache, connp);
607 	} else if (connp->conn_flags & IPCL_SCTPCONN) {
608 		sctp_free(connp);
609 	} else {
610 		ASSERT(connp->conn_udp == NULL);
611 		mutex_destroy(&connp->conn_lock);
612 		kmem_cache_free(ipcl_conn_cache, connp);
613 	}
614 }
615 
616 /*
617  * Running in cluster mode - deregister listener information
618  */
619 
620 static void
621 ipcl_conn_unlisten(conn_t *connp)
622 {
623 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) != 0);
624 	ASSERT(connp->conn_lport != 0);
625 
626 	if (cl_inet_unlisten != NULL) {
627 		sa_family_t	addr_family;
628 		uint8_t		*laddrp;
629 
630 		if (connp->conn_pkt_isv6) {
631 			addr_family = AF_INET6;
632 			laddrp = (uint8_t *)&connp->conn_bound_source_v6;
633 		} else {
634 			addr_family = AF_INET;
635 			laddrp = (uint8_t *)&connp->conn_bound_source;
636 		}
637 		(*cl_inet_unlisten)(IPPROTO_TCP, addr_family, laddrp,
638 		    connp->conn_lport);
639 	}
640 	connp->conn_flags &= ~IPCL_CL_LISTENER;
641 }
642 
643 /*
644  * We set the IPCL_REMOVED flag (instead of clearing the flag indicating
645  * which table the conn belonged to). So for debugging we can see which hash
646  * table this connection was in.
647  */
648 #define	IPCL_HASH_REMOVE(connp)	{					\
649 	connf_t	*connfp = (connp)->conn_fanout;				\
650 	ASSERT(!MUTEX_HELD(&((connp)->conn_lock)));			\
651 	if (connfp != NULL) {						\
652 		IPCL_DEBUG_LVL(4, ("IPCL_HASH_REMOVE: connp %p",	\
653 		    (void *)(connp)));					\
654 		mutex_enter(&connfp->connf_lock);			\
655 		if ((connp)->conn_next != NULL)				\
656 			(connp)->conn_next->conn_prev =			\
657 			    (connp)->conn_prev;				\
658 		if ((connp)->conn_prev != NULL)				\
659 			(connp)->conn_prev->conn_next =			\
660 			    (connp)->conn_next;				\
661 		else							\
662 			connfp->connf_head = (connp)->conn_next;	\
663 		(connp)->conn_fanout = NULL;				\
664 		(connp)->conn_next = NULL;				\
665 		(connp)->conn_prev = NULL;				\
666 		(connp)->conn_flags |= IPCL_REMOVED;			\
667 		if (((connp)->conn_flags & IPCL_CL_LISTENER) != 0)	\
668 			ipcl_conn_unlisten((connp));			\
669 		CONN_DEC_REF((connp));					\
670 		mutex_exit(&connfp->connf_lock);			\
671 	}								\
672 }
673 
674 void
675 ipcl_hash_remove(conn_t *connp)
676 {
677 	IPCL_HASH_REMOVE(connp);
678 }
679 
680 /*
681  * The whole purpose of this function is allow removal of
682  * a conn_t from the connected hash for timewait reclaim.
683  * This is essentially a TW reclaim fastpath where timewait
684  * collector checks under fanout lock (so no one else can
685  * get access to the conn_t) that refcnt is 2 i.e. one for
686  * TCP and one for the classifier hash list. If ref count
687  * is indeed 2, we can just remove the conn under lock and
688  * avoid cleaning up the conn under squeue. This gives us
689  * improved performance.
690  */
691 void
692 ipcl_hash_remove_locked(conn_t *connp, connf_t	*connfp)
693 {
694 	ASSERT(MUTEX_HELD(&connfp->connf_lock));
695 	ASSERT(MUTEX_HELD(&connp->conn_lock));
696 	ASSERT((connp->conn_flags & IPCL_CL_LISTENER) == 0);
697 
698 	if ((connp)->conn_next != NULL) {
699 		(connp)->conn_next->conn_prev =
700 			(connp)->conn_prev;
701 	}
702 	if ((connp)->conn_prev != NULL) {
703 		(connp)->conn_prev->conn_next =
704 			(connp)->conn_next;
705 	} else {
706 		connfp->connf_head = (connp)->conn_next;
707 	}
708 	(connp)->conn_fanout = NULL;
709 	(connp)->conn_next = NULL;
710 	(connp)->conn_prev = NULL;
711 	(connp)->conn_flags |= IPCL_REMOVED;
712 	ASSERT((connp)->conn_ref == 2);
713 	(connp)->conn_ref--;
714 }
715 
716 #define	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp) {		\
717 	ASSERT((connp)->conn_fanout == NULL);				\
718 	ASSERT((connp)->conn_next == NULL);				\
719 	ASSERT((connp)->conn_prev == NULL);				\
720 	if ((connfp)->connf_head != NULL) {				\
721 		(connfp)->connf_head->conn_prev = (connp);		\
722 		(connp)->conn_next = (connfp)->connf_head;		\
723 	}								\
724 	(connp)->conn_fanout = (connfp);				\
725 	(connfp)->connf_head = (connp);					\
726 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
727 	    IPCL_CONNECTED;						\
728 	CONN_INC_REF(connp);						\
729 }
730 
731 #define	IPCL_HASH_INSERT_CONNECTED(connfp, connp) {			\
732 	IPCL_DEBUG_LVL(8, ("IPCL_HASH_INSERT_CONNECTED: connfp %p "	\
733 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
734 	IPCL_HASH_REMOVE((connp));					\
735 	mutex_enter(&(connfp)->connf_lock);				\
736 	IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);		\
737 	mutex_exit(&(connfp)->connf_lock);				\
738 }
739 
740 #define	IPCL_HASH_INSERT_BOUND(connfp, connp) {				\
741 	conn_t *pconnp = NULL, *nconnp;					\
742 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_BOUND: connfp %p "	\
743 	    "connp %p", (void *)connfp, (void *)(connp)));		\
744 	IPCL_HASH_REMOVE((connp));					\
745 	mutex_enter(&(connfp)->connf_lock);				\
746 	nconnp = (connfp)->connf_head;					\
747 	while (nconnp != NULL &&					\
748 	    !_IPCL_V4_MATCH_ANY(nconnp->conn_srcv6)) {			\
749 		pconnp = nconnp;					\
750 		nconnp = nconnp->conn_next;				\
751 	}								\
752 	if (pconnp != NULL) {						\
753 		pconnp->conn_next = (connp);				\
754 		(connp)->conn_prev = pconnp;				\
755 	} else {							\
756 		(connfp)->connf_head = (connp);				\
757 	}								\
758 	if (nconnp != NULL) {						\
759 		(connp)->conn_next = nconnp;				\
760 		nconnp->conn_prev = (connp);				\
761 	}								\
762 	(connp)->conn_fanout = (connfp);				\
763 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
764 	    IPCL_BOUND;							\
765 	CONN_INC_REF(connp);						\
766 	mutex_exit(&(connfp)->connf_lock);				\
767 }
768 
769 #define	IPCL_HASH_INSERT_WILDCARD(connfp, connp) {			\
770 	conn_t **list, *prev, *next;					\
771 	boolean_t isv4mapped =						\
772 	    IN6_IS_ADDR_V4MAPPED(&(connp)->conn_srcv6);			\
773 	IPCL_DEBUG_LVL(32, ("IPCL_HASH_INSERT_WILDCARD: connfp %p "	\
774 	    "connp %p", (void *)(connfp), (void *)(connp)));		\
775 	IPCL_HASH_REMOVE((connp));					\
776 	mutex_enter(&(connfp)->connf_lock);				\
777 	list = &(connfp)->connf_head;					\
778 	prev = NULL;							\
779 	while ((next = *list) != NULL) {				\
780 		if (isv4mapped &&					\
781 		    IN6_IS_ADDR_UNSPECIFIED(&next->conn_srcv6) &&	\
782 		    connp->conn_zoneid == next->conn_zoneid) {		\
783 			(connp)->conn_next = next;			\
784 			if (prev != NULL)				\
785 				prev = next->conn_prev;			\
786 			next->conn_prev = (connp);			\
787 			break;						\
788 		}							\
789 		list = &next->conn_next;				\
790 		prev = next;						\
791 	}								\
792 	(connp)->conn_prev = prev;					\
793 	*list = (connp);						\
794 	(connp)->conn_fanout = (connfp);				\
795 	(connp)->conn_flags = ((connp)->conn_flags & ~IPCL_REMOVED) |	\
796 	    IPCL_BOUND;							\
797 	CONN_INC_REF((connp));						\
798 	mutex_exit(&(connfp)->connf_lock);				\
799 }
800 
801 void
802 ipcl_hash_insert_wildcard(connf_t *connfp, conn_t *connp)
803 {
804 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
805 }
806 
807 void
808 ipcl_proto_insert(conn_t *connp, uint8_t protocol)
809 {
810 	connf_t	*connfp;
811 
812 	ASSERT(connp != NULL);
813 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
814 	    protocol == IPPROTO_ESP);
815 
816 	connp->conn_ulp = protocol;
817 
818 	/* Insert it in the protocol hash */
819 	connfp = &ipcl_proto_fanout[protocol];
820 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
821 }
822 
823 void
824 ipcl_proto_insert_v6(conn_t *connp, uint8_t protocol)
825 {
826 	connf_t	*connfp;
827 
828 	ASSERT(connp != NULL);
829 	ASSERT(!connp->conn_mac_exempt || protocol == IPPROTO_AH ||
830 	    protocol == IPPROTO_ESP);
831 
832 	connp->conn_ulp = protocol;
833 
834 	/* Insert it in the Bind Hash */
835 	connfp = &ipcl_proto_fanout_v6[protocol];
836 	IPCL_HASH_INSERT_WILDCARD(connfp, connp);
837 }
838 
839 /*
840  * This function is used only for inserting SCTP raw socket now.
841  * This may change later.
842  *
843  * Note that only one raw socket can be bound to a port.  The param
844  * lport is in network byte order.
845  */
846 static int
847 ipcl_sctp_hash_insert(conn_t *connp, in_port_t lport)
848 {
849 	connf_t	*connfp;
850 	conn_t	*oconnp;
851 
852 	connfp = &ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport))];
853 
854 	/* Check for existing raw socket already bound to the port. */
855 	mutex_enter(&connfp->connf_lock);
856 	for (oconnp = connfp->connf_head; oconnp != NULL;
857 	    oconnp = oconnp->conn_next) {
858 		if (oconnp->conn_lport == lport &&
859 		    oconnp->conn_zoneid == connp->conn_zoneid &&
860 		    oconnp->conn_af_isv6 == connp->conn_af_isv6 &&
861 		    ((IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
862 		    IN6_IS_ADDR_UNSPECIFIED(&oconnp->conn_srcv6) ||
863 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6) ||
864 		    IN6_IS_ADDR_V4MAPPED_ANY(&oconnp->conn_srcv6)) ||
865 		    IN6_ARE_ADDR_EQUAL(&oconnp->conn_srcv6,
866 		    &connp->conn_srcv6))) {
867 			break;
868 		}
869 	}
870 	mutex_exit(&connfp->connf_lock);
871 	if (oconnp != NULL)
872 		return (EADDRNOTAVAIL);
873 
874 	if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6) ||
875 	    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_remv6)) {
876 		if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) ||
877 		    IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_srcv6)) {
878 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
879 		} else {
880 			IPCL_HASH_INSERT_BOUND(connfp, connp);
881 		}
882 	} else {
883 		IPCL_HASH_INSERT_CONNECTED(connfp, connp);
884 	}
885 	return (0);
886 }
887 
888 /*
889  * Check for a MAC exemption conflict on a labeled system.  Note that for
890  * protocols that use port numbers (UDP, TCP, SCTP), we do this check up in the
891  * transport layer.  This check is for binding all other protocols.
892  *
893  * Returns true if there's a conflict.
894  */
895 static boolean_t
896 check_exempt_conflict_v4(conn_t *connp)
897 {
898 	connf_t	*connfp;
899 	conn_t *tconn;
900 
901 	connfp = &ipcl_proto_fanout[connp->conn_ulp];
902 	mutex_enter(&connfp->connf_lock);
903 	for (tconn = connfp->connf_head; tconn != NULL;
904 	    tconn = tconn->conn_next) {
905 		/* We don't allow v4 fallback for v6 raw socket */
906 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
907 			continue;
908 		/* If neither is exempt, then there's no conflict */
909 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
910 			continue;
911 		/* If both are bound to different specific addrs, ok */
912 		if (connp->conn_src != INADDR_ANY &&
913 		    tconn->conn_src != INADDR_ANY &&
914 		    connp->conn_src != tconn->conn_src)
915 			continue;
916 		/* These two conflict; fail */
917 		break;
918 	}
919 	mutex_exit(&connfp->connf_lock);
920 	return (tconn != NULL);
921 }
922 
923 static boolean_t
924 check_exempt_conflict_v6(conn_t *connp)
925 {
926 	connf_t	*connfp;
927 	conn_t *tconn;
928 
929 	connfp = &ipcl_proto_fanout[connp->conn_ulp];
930 	mutex_enter(&connfp->connf_lock);
931 	for (tconn = connfp->connf_head; tconn != NULL;
932 	    tconn = tconn->conn_next) {
933 		/* We don't allow v4 fallback for v6 raw socket */
934 		if (connp->conn_af_isv6 != tconn->conn_af_isv6)
935 			continue;
936 		/* If neither is exempt, then there's no conflict */
937 		if (!connp->conn_mac_exempt && !tconn->conn_mac_exempt)
938 			continue;
939 		/* If both are bound to different addrs, ok */
940 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6) &&
941 		    !IN6_IS_ADDR_UNSPECIFIED(&tconn->conn_srcv6) &&
942 		    !IN6_ARE_ADDR_EQUAL(&connp->conn_srcv6, &tconn->conn_srcv6))
943 			continue;
944 		/* These two conflict; fail */
945 		break;
946 	}
947 	mutex_exit(&connfp->connf_lock);
948 	return (tconn != NULL);
949 }
950 
951 /*
952  * (v4, v6) bind hash insertion routines
953  */
954 int
955 ipcl_bind_insert(conn_t *connp, uint8_t protocol, ipaddr_t src, uint16_t lport)
956 {
957 	connf_t	*connfp;
958 #ifdef	IPCL_DEBUG
959 	char	buf[INET_NTOA_BUFSIZE];
960 #endif
961 	int	ret = 0;
962 
963 	ASSERT(connp);
964 
965 	IPCL_DEBUG_LVL(64, ("ipcl_bind_insert: connp %p, src = %s, "
966 	    "port = %d\n", (void *)connp, inet_ntoa_r(src, buf), lport));
967 
968 	connp->conn_ulp = protocol;
969 	IN6_IPADDR_TO_V4MAPPED(src, &connp->conn_srcv6);
970 	connp->conn_lport = lport;
971 
972 	switch (protocol) {
973 	default:
974 		if (is_system_labeled() && check_exempt_conflict_v4(connp))
975 			return (EADDRINUSE);
976 		/* FALLTHROUGH */
977 	case IPPROTO_UDP:
978 		if (protocol == IPPROTO_UDP) {
979 			IPCL_DEBUG_LVL(64,
980 			    ("ipcl_bind_insert: connp %p - udp\n",
981 			    (void *)connp));
982 			connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(lport)];
983 		} else {
984 			IPCL_DEBUG_LVL(64,
985 			    ("ipcl_bind_insert: connp %p - protocol\n",
986 			    (void *)connp));
987 			connfp = &ipcl_proto_fanout[protocol];
988 		}
989 
990 		if (connp->conn_rem != INADDR_ANY) {
991 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
992 		} else if (connp->conn_src != INADDR_ANY) {
993 			IPCL_HASH_INSERT_BOUND(connfp, connp);
994 		} else {
995 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
996 		}
997 		break;
998 
999 	case IPPROTO_TCP:
1000 
1001 		/* Insert it in the Bind Hash */
1002 		ASSERT(connp->conn_zoneid != ALL_ZONES);
1003 		connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
1004 		if (connp->conn_src != INADDR_ANY) {
1005 			IPCL_HASH_INSERT_BOUND(connfp, connp);
1006 		} else {
1007 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
1008 		}
1009 		if (cl_inet_listen != NULL) {
1010 			ASSERT(!connp->conn_pkt_isv6);
1011 			connp->conn_flags |= IPCL_CL_LISTENER;
1012 			(*cl_inet_listen)(IPPROTO_TCP, AF_INET,
1013 			    (uint8_t *)&connp->conn_bound_source, lport);
1014 		}
1015 		break;
1016 
1017 	case IPPROTO_SCTP:
1018 		ret = ipcl_sctp_hash_insert(connp, lport);
1019 		break;
1020 	}
1021 
1022 	return (ret);
1023 }
1024 
1025 int
1026 ipcl_bind_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
1027     uint16_t lport)
1028 {
1029 	connf_t	*connfp;
1030 	int	ret = 0;
1031 
1032 	ASSERT(connp);
1033 
1034 	connp->conn_ulp = protocol;
1035 	connp->conn_srcv6 = *src;
1036 	connp->conn_lport = lport;
1037 
1038 	switch (protocol) {
1039 	default:
1040 		if (is_system_labeled() && check_exempt_conflict_v6(connp))
1041 			return (EADDRINUSE);
1042 		/* FALLTHROUGH */
1043 	case IPPROTO_UDP:
1044 		if (protocol == IPPROTO_UDP) {
1045 			IPCL_DEBUG_LVL(128,
1046 			    ("ipcl_bind_insert_v6: connp %p - udp\n",
1047 			    (void *)connp));
1048 			connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(lport)];
1049 		} else {
1050 			IPCL_DEBUG_LVL(128,
1051 			    ("ipcl_bind_insert_v6: connp %p - protocol\n",
1052 			    (void *)connp));
1053 			connfp = &ipcl_proto_fanout_v6[protocol];
1054 		}
1055 
1056 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
1057 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
1058 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
1059 			IPCL_HASH_INSERT_BOUND(connfp, connp);
1060 		} else {
1061 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
1062 		}
1063 		break;
1064 
1065 	case IPPROTO_TCP:
1066 		/* XXX - Need a separate table for IN6_IS_ADDR_UNSPECIFIED? */
1067 
1068 		/* Insert it in the Bind Hash */
1069 		ASSERT(connp->conn_zoneid != ALL_ZONES);
1070 		connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
1071 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
1072 			IPCL_HASH_INSERT_BOUND(connfp, connp);
1073 		} else {
1074 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
1075 		}
1076 		if (cl_inet_listen != NULL) {
1077 			sa_family_t	addr_family;
1078 			uint8_t		*laddrp;
1079 
1080 			if (connp->conn_pkt_isv6) {
1081 				addr_family = AF_INET6;
1082 				laddrp =
1083 				    (uint8_t *)&connp->conn_bound_source_v6;
1084 			} else {
1085 				addr_family = AF_INET;
1086 				laddrp = (uint8_t *)&connp->conn_bound_source;
1087 			}
1088 			connp->conn_flags |= IPCL_CL_LISTENER;
1089 			(*cl_inet_listen)(IPPROTO_TCP, addr_family, laddrp,
1090 			    lport);
1091 		}
1092 		break;
1093 
1094 	case IPPROTO_SCTP:
1095 		ret = ipcl_sctp_hash_insert(connp, lport);
1096 		break;
1097 	}
1098 
1099 	return (ret);
1100 }
1101 
1102 /*
1103  * ipcl_conn_hash insertion routines.
1104  */
1105 int
1106 ipcl_conn_insert(conn_t *connp, uint8_t protocol, ipaddr_t src,
1107     ipaddr_t rem, uint32_t ports)
1108 {
1109 	connf_t		*connfp;
1110 	uint16_t	*up;
1111 	conn_t		*tconnp;
1112 #ifdef	IPCL_DEBUG
1113 	char	sbuf[INET_NTOA_BUFSIZE], rbuf[INET_NTOA_BUFSIZE];
1114 #endif
1115 	in_port_t	lport;
1116 	int		ret = 0;
1117 
1118 	IPCL_DEBUG_LVL(256, ("ipcl_conn_insert: connp %p, src = %s, "
1119 	    "dst = %s, ports = %x, protocol = %x", (void *)connp,
1120 	    inet_ntoa_r(src, sbuf), inet_ntoa_r(rem, rbuf),
1121 	    ports, protocol));
1122 
1123 	switch (protocol) {
1124 	case IPPROTO_TCP:
1125 		if (!(connp->conn_flags & IPCL_EAGER)) {
1126 			/*
1127 			 * for a eager connection, i.e connections which
1128 			 * have just been created, the initialization is
1129 			 * already done in ip at conn_creation time, so
1130 			 * we can skip the checks here.
1131 			 */
1132 			IPCL_CONN_INIT(connp, protocol, src, rem, ports);
1133 		}
1134 		connfp = &ipcl_conn_fanout[IPCL_CONN_HASH(connp->conn_rem,
1135 		    connp->conn_ports)];
1136 		mutex_enter(&connfp->connf_lock);
1137 		for (tconnp = connfp->connf_head; tconnp != NULL;
1138 		    tconnp = tconnp->conn_next) {
1139 			if (IPCL_CONN_MATCH(tconnp, connp->conn_ulp,
1140 			    connp->conn_rem, connp->conn_src,
1141 			    connp->conn_ports)) {
1142 
1143 				/* Already have a conn. bail out */
1144 				mutex_exit(&connfp->connf_lock);
1145 				return (EADDRINUSE);
1146 			}
1147 		}
1148 		if (connp->conn_fanout != NULL) {
1149 			/*
1150 			 * Probably a XTI/TLI application trying to do a
1151 			 * rebind. Let it happen.
1152 			 */
1153 			mutex_exit(&connfp->connf_lock);
1154 			IPCL_HASH_REMOVE(connp);
1155 			mutex_enter(&connfp->connf_lock);
1156 		}
1157 
1158 		ASSERT(connp->conn_recv != NULL);
1159 
1160 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
1161 		mutex_exit(&connfp->connf_lock);
1162 		break;
1163 
1164 	case IPPROTO_SCTP:
1165 		/*
1166 		 * The raw socket may have already been bound, remove it
1167 		 * from the hash first.
1168 		 */
1169 		IPCL_HASH_REMOVE(connp);
1170 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
1171 		ret = ipcl_sctp_hash_insert(connp, lport);
1172 		break;
1173 
1174 	default:
1175 		/*
1176 		 * Check for conflicts among MAC exempt bindings.  For
1177 		 * transports with port numbers, this is done by the upper
1178 		 * level per-transport binding logic.  For all others, it's
1179 		 * done here.
1180 		 */
1181 		if (is_system_labeled() && check_exempt_conflict_v4(connp))
1182 			return (EADDRINUSE);
1183 		/* FALLTHROUGH */
1184 
1185 	case IPPROTO_UDP:
1186 		up = (uint16_t *)&ports;
1187 		IPCL_CONN_INIT(connp, protocol, src, rem, ports);
1188 		if (protocol == IPPROTO_UDP) {
1189 			connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(up[1])];
1190 		} else {
1191 			connfp = &ipcl_proto_fanout[protocol];
1192 		}
1193 
1194 		if (connp->conn_rem != INADDR_ANY) {
1195 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
1196 		} else if (connp->conn_src != INADDR_ANY) {
1197 			IPCL_HASH_INSERT_BOUND(connfp, connp);
1198 		} else {
1199 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
1200 		}
1201 		break;
1202 	}
1203 
1204 	return (ret);
1205 }
1206 
1207 int
1208 ipcl_conn_insert_v6(conn_t *connp, uint8_t protocol, const in6_addr_t *src,
1209     const in6_addr_t *rem, uint32_t ports, uint_t ifindex)
1210 {
1211 	connf_t		*connfp;
1212 	uint16_t	*up;
1213 	conn_t		*tconnp;
1214 	in_port_t	lport;
1215 	int		ret = 0;
1216 
1217 	switch (protocol) {
1218 	case IPPROTO_TCP:
1219 		/* Just need to insert a conn struct */
1220 		if (!(connp->conn_flags & IPCL_EAGER)) {
1221 			IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
1222 		}
1223 		connfp = &ipcl_conn_fanout[IPCL_CONN_HASH_V6(connp->conn_remv6,
1224 		    connp->conn_ports)];
1225 		mutex_enter(&connfp->connf_lock);
1226 		for (tconnp = connfp->connf_head; tconnp != NULL;
1227 		    tconnp = tconnp->conn_next) {
1228 			if (IPCL_CONN_MATCH_V6(tconnp, connp->conn_ulp,
1229 			    connp->conn_remv6, connp->conn_srcv6,
1230 			    connp->conn_ports) &&
1231 			    (tconnp->conn_tcp->tcp_bound_if == 0 ||
1232 			    tconnp->conn_tcp->tcp_bound_if == ifindex)) {
1233 				/* Already have a conn. bail out */
1234 				mutex_exit(&connfp->connf_lock);
1235 				return (EADDRINUSE);
1236 			}
1237 		}
1238 		if (connp->conn_fanout != NULL) {
1239 			/*
1240 			 * Probably a XTI/TLI application trying to do a
1241 			 * rebind. Let it happen.
1242 			 */
1243 			mutex_exit(&connfp->connf_lock);
1244 			IPCL_HASH_REMOVE(connp);
1245 			mutex_enter(&connfp->connf_lock);
1246 		}
1247 		IPCL_HASH_INSERT_CONNECTED_LOCKED(connfp, connp);
1248 		mutex_exit(&connfp->connf_lock);
1249 		break;
1250 
1251 	case IPPROTO_SCTP:
1252 		IPCL_HASH_REMOVE(connp);
1253 		lport = htons((uint16_t)(ntohl(ports) & 0xFFFF));
1254 		ret = ipcl_sctp_hash_insert(connp, lport);
1255 		break;
1256 
1257 	default:
1258 		if (is_system_labeled() && check_exempt_conflict_v6(connp))
1259 			return (EADDRINUSE);
1260 		/* FALLTHROUGH */
1261 	case IPPROTO_UDP:
1262 		up = (uint16_t *)&ports;
1263 		IPCL_CONN_INIT_V6(connp, protocol, *src, *rem, ports);
1264 		if (protocol == IPPROTO_UDP) {
1265 			connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(up[1])];
1266 		} else {
1267 			connfp = &ipcl_proto_fanout_v6[protocol];
1268 		}
1269 
1270 		if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_remv6)) {
1271 			IPCL_HASH_INSERT_CONNECTED(connfp, connp);
1272 		} else if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_srcv6)) {
1273 			IPCL_HASH_INSERT_BOUND(connfp, connp);
1274 		} else {
1275 			IPCL_HASH_INSERT_WILDCARD(connfp, connp);
1276 		}
1277 		break;
1278 	}
1279 
1280 	return (ret);
1281 }
1282 
1283 /*
1284  * v4 packet classifying function. looks up the fanout table to
1285  * find the conn, the packet belongs to. returns the conn with
1286  * the reference held, null otherwise.
1287  *
1288  * If zoneid is ALL_ZONES, then the search rules described in the "Connection
1289  * Lookup" comment block are applied.  Labels are also checked as described
1290  * above.  If the packet is from the inside (looped back), and is from the same
1291  * zone, then label checks are omitted.
1292  */
1293 conn_t *
1294 ipcl_classify_v4(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid)
1295 {
1296 	ipha_t	*ipha;
1297 	connf_t	*connfp, *bind_connfp;
1298 	uint16_t lport;
1299 	uint16_t fport;
1300 	uint32_t ports;
1301 	conn_t	*connp;
1302 	uint16_t  *up;
1303 	boolean_t shared_addr;
1304 	boolean_t unlabeled;
1305 
1306 	ipha = (ipha_t *)mp->b_rptr;
1307 	up = (uint16_t *)((uchar_t *)ipha + hdr_len + TCP_PORTS_OFFSET);
1308 
1309 	switch (protocol) {
1310 	case IPPROTO_TCP:
1311 		ports = *(uint32_t *)up;
1312 		connfp =
1313 		    &ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_src, ports)];
1314 		mutex_enter(&connfp->connf_lock);
1315 		for (connp = connfp->connf_head; connp != NULL;
1316 		    connp = connp->conn_next) {
1317 			if (IPCL_CONN_MATCH(connp, protocol,
1318 			    ipha->ipha_src, ipha->ipha_dst, ports))
1319 				break;
1320 		}
1321 
1322 		if (connp != NULL) {
1323 			/*
1324 			 * We have a fully-bound TCP connection.
1325 			 *
1326 			 * For labeled systems, there's no need to check the
1327 			 * label here.  It's known to be good as we checked
1328 			 * before allowing the connection to become bound.
1329 			 */
1330 			CONN_INC_REF(connp);
1331 			mutex_exit(&connfp->connf_lock);
1332 			return (connp);
1333 		}
1334 
1335 		mutex_exit(&connfp->connf_lock);
1336 
1337 		lport = up[1];
1338 		unlabeled = B_FALSE;
1339 		/* Cred cannot be null on IPv4 */
1340 		if (is_system_labeled())
1341 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
1342 			    TSLF_UNLABELED) != 0;
1343 		shared_addr = (zoneid == ALL_ZONES);
1344 		if (shared_addr) {
1345 			zoneid = tsol_mlp_findzone(protocol, lport);
1346 			/*
1347 			 * If no shared MLP is found, tsol_mlp_findzone returns
1348 			 * ALL_ZONES.  In that case, we assume it's SLP, and
1349 			 * search for the zone based on the packet label.
1350 			 *
1351 			 * If there is such a zone, we prefer to find a
1352 			 * connection in it.  Otherwise, we look for a
1353 			 * MAC-exempt connection in any zone whose label
1354 			 * dominates the default label on the packet.
1355 			 */
1356 			if (zoneid == ALL_ZONES)
1357 				zoneid = tsol_packet_to_zoneid(mp);
1358 			else
1359 				unlabeled = B_FALSE;
1360 		}
1361 
1362 		bind_connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
1363 		mutex_enter(&bind_connfp->connf_lock);
1364 		for (connp = bind_connfp->connf_head; connp != NULL;
1365 		    connp = connp->conn_next) {
1366 			if (IPCL_BIND_MATCH(connp, protocol, ipha->ipha_dst,
1367 			    lport) && (IPCL_ZONE_MATCH(connp, zoneid) ||
1368 			    (unlabeled && connp->conn_mac_exempt)))
1369 				break;
1370 		}
1371 
1372 		/*
1373 		 * If the matching connection is SLP on a private address, then
1374 		 * the label on the packet must match the local zone's label.
1375 		 * Otherwise, it must be in the label range defined by tnrh.
1376 		 * This is ensured by tsol_receive_label.
1377 		 */
1378 		if (connp != NULL && is_system_labeled() &&
1379 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
1380 		    shared_addr, connp)) {
1381 				DTRACE_PROBE3(
1382 				    tx__ip__log__info__classify__tcp,
1383 				    char *,
1384 				    "connp(1) could not receive mp(2)",
1385 				    conn_t *, connp, mblk_t *, mp);
1386 			connp = NULL;
1387 		}
1388 
1389 		if (connp != NULL) {
1390 			/* Have a listener at least */
1391 			CONN_INC_REF(connp);
1392 			mutex_exit(&bind_connfp->connf_lock);
1393 			return (connp);
1394 		}
1395 
1396 		mutex_exit(&bind_connfp->connf_lock);
1397 
1398 		IPCL_DEBUG_LVL(512,
1399 		    ("ipcl_classify: couldn't classify mp = %p\n",
1400 		    (void *)mp));
1401 		break;
1402 
1403 	case IPPROTO_UDP:
1404 		lport = up[1];
1405 		unlabeled = B_FALSE;
1406 		/* Cred cannot be null on IPv4 */
1407 		if (is_system_labeled())
1408 			unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
1409 			    TSLF_UNLABELED) != 0;
1410 		shared_addr = (zoneid == ALL_ZONES);
1411 		if (shared_addr) {
1412 			zoneid = tsol_mlp_findzone(protocol, lport);
1413 			/*
1414 			 * If no shared MLP is found, tsol_mlp_findzone returns
1415 			 * ALL_ZONES.  In that case, we assume it's SLP, and
1416 			 * search for the zone based on the packet label.
1417 			 *
1418 			 * If there is such a zone, we prefer to find a
1419 			 * connection in it.  Otherwise, we look for a
1420 			 * MAC-exempt connection in any zone whose label
1421 			 * dominates the default label on the packet.
1422 			 */
1423 			if (zoneid == ALL_ZONES)
1424 				zoneid = tsol_packet_to_zoneid(mp);
1425 			else
1426 				unlabeled = B_FALSE;
1427 		}
1428 		fport = up[0];
1429 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify %x %x", lport, fport));
1430 		connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(lport)];
1431 		mutex_enter(&connfp->connf_lock);
1432 		for (connp = connfp->connf_head; connp != NULL;
1433 		    connp = connp->conn_next) {
1434 			if (IPCL_UDP_MATCH(connp, lport, ipha->ipha_dst,
1435 			    fport, ipha->ipha_src) &&
1436 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
1437 			    (unlabeled && connp->conn_mac_exempt)))
1438 				break;
1439 		}
1440 
1441 		if (connp != NULL && is_system_labeled() &&
1442 		    !tsol_receive_local(mp, &ipha->ipha_dst, IPV4_VERSION,
1443 		    shared_addr, connp)) {
1444 			DTRACE_PROBE3(tx__ip__log__info__classify__udp,
1445 			    char *, "connp(1) could not receive mp(2)",
1446 			    conn_t *, connp, mblk_t *, mp);
1447 			connp = NULL;
1448 		}
1449 
1450 		if (connp != NULL) {
1451 			CONN_INC_REF(connp);
1452 			mutex_exit(&connfp->connf_lock);
1453 			return (connp);
1454 		}
1455 
1456 		/*
1457 		 * We shouldn't come here for multicast/broadcast packets
1458 		 */
1459 		mutex_exit(&connfp->connf_lock);
1460 		IPCL_DEBUG_LVL(512,
1461 		    ("ipcl_classify: cant find udp conn_t for ports : %x %x",
1462 		    lport, fport));
1463 		break;
1464 	}
1465 
1466 	return (NULL);
1467 }
1468 
1469 conn_t *
1470 ipcl_classify_v6(mblk_t *mp, uint8_t protocol, uint_t hdr_len, zoneid_t zoneid)
1471 {
1472 	ip6_t		*ip6h;
1473 	connf_t		*connfp, *bind_connfp;
1474 	uint16_t	lport;
1475 	uint16_t	fport;
1476 	tcph_t		*tcph;
1477 	uint32_t	ports;
1478 	conn_t		*connp;
1479 	uint16_t	*up;
1480 	boolean_t	shared_addr;
1481 	boolean_t	unlabeled;
1482 
1483 	ip6h = (ip6_t *)mp->b_rptr;
1484 
1485 	switch (protocol) {
1486 	case IPPROTO_TCP:
1487 		tcph = (tcph_t *)&mp->b_rptr[hdr_len];
1488 		up = (uint16_t *)tcph->th_lport;
1489 		ports = *(uint32_t *)up;
1490 
1491 		connfp =
1492 		    &ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_src, ports)];
1493 		mutex_enter(&connfp->connf_lock);
1494 		for (connp = connfp->connf_head; connp != NULL;
1495 		    connp = connp->conn_next) {
1496 			if (IPCL_CONN_MATCH_V6(connp, protocol,
1497 			    ip6h->ip6_src, ip6h->ip6_dst, ports))
1498 				break;
1499 		}
1500 
1501 		if (connp != NULL) {
1502 			/*
1503 			 * We have a fully-bound TCP connection.
1504 			 *
1505 			 * For labeled systems, there's no need to check the
1506 			 * label here.  It's known to be good as we checked
1507 			 * before allowing the connection to become bound.
1508 			 */
1509 			CONN_INC_REF(connp);
1510 			mutex_exit(&connfp->connf_lock);
1511 			return (connp);
1512 		}
1513 
1514 		mutex_exit(&connfp->connf_lock);
1515 
1516 		lport = up[1];
1517 		unlabeled = B_FALSE;
1518 		/* Cred can be null on IPv6 */
1519 		if (is_system_labeled()) {
1520 			cred_t *cr = DB_CRED(mp);
1521 
1522 			unlabeled = (cr != NULL &&
1523 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
1524 		}
1525 		shared_addr = (zoneid == ALL_ZONES);
1526 		if (shared_addr) {
1527 			zoneid = tsol_mlp_findzone(protocol, lport);
1528 			/*
1529 			 * If no shared MLP is found, tsol_mlp_findzone returns
1530 			 * ALL_ZONES.  In that case, we assume it's SLP, and
1531 			 * search for the zone based on the packet label.
1532 			 *
1533 			 * If there is such a zone, we prefer to find a
1534 			 * connection in it.  Otherwise, we look for a
1535 			 * MAC-exempt connection in any zone whose label
1536 			 * dominates the default label on the packet.
1537 			 */
1538 			if (zoneid == ALL_ZONES)
1539 				zoneid = tsol_packet_to_zoneid(mp);
1540 			else
1541 				unlabeled = B_FALSE;
1542 		}
1543 
1544 		bind_connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
1545 		mutex_enter(&bind_connfp->connf_lock);
1546 		for (connp = bind_connfp->connf_head; connp != NULL;
1547 		    connp = connp->conn_next) {
1548 			if (IPCL_BIND_MATCH_V6(connp, protocol,
1549 			    ip6h->ip6_dst, lport) &&
1550 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
1551 			    (unlabeled && connp->conn_mac_exempt)))
1552 				break;
1553 		}
1554 
1555 		if (connp != NULL && is_system_labeled() &&
1556 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
1557 		    shared_addr, connp)) {
1558 			DTRACE_PROBE3(tx__ip__log__info__classify__tcp6,
1559 			    char *, "connp(1) could not receive mp(2)",
1560 			    conn_t *, connp, mblk_t *, mp);
1561 			connp = NULL;
1562 		}
1563 
1564 		if (connp != NULL) {
1565 			/* Have a listner at least */
1566 			CONN_INC_REF(connp);
1567 			mutex_exit(&bind_connfp->connf_lock);
1568 			IPCL_DEBUG_LVL(512,
1569 			    ("ipcl_classify_v6: found listner "
1570 			    "connp = %p\n", (void *)connp));
1571 
1572 			return (connp);
1573 		}
1574 
1575 		mutex_exit(&bind_connfp->connf_lock);
1576 
1577 		IPCL_DEBUG_LVL(512,
1578 		    ("ipcl_classify_v6: couldn't classify mp = %p\n",
1579 		    (void *)mp));
1580 		break;
1581 
1582 	case IPPROTO_UDP:
1583 		up = (uint16_t *)&mp->b_rptr[hdr_len];
1584 		lport = up[1];
1585 		unlabeled = B_FALSE;
1586 		/* Cred can be null on IPv6 */
1587 		if (is_system_labeled()) {
1588 			cred_t *cr = DB_CRED(mp);
1589 
1590 			unlabeled = (cr != NULL &&
1591 			    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
1592 		}
1593 		shared_addr = (zoneid == ALL_ZONES);
1594 		if (shared_addr) {
1595 			zoneid = tsol_mlp_findzone(protocol, lport);
1596 			/*
1597 			 * If no shared MLP is found, tsol_mlp_findzone returns
1598 			 * ALL_ZONES.  In that case, we assume it's SLP, and
1599 			 * search for the zone based on the packet label.
1600 			 *
1601 			 * If there is such a zone, we prefer to find a
1602 			 * connection in it.  Otherwise, we look for a
1603 			 * MAC-exempt connection in any zone whose label
1604 			 * dominates the default label on the packet.
1605 			 */
1606 			if (zoneid == ALL_ZONES)
1607 				zoneid = tsol_packet_to_zoneid(mp);
1608 			else
1609 				unlabeled = B_FALSE;
1610 		}
1611 
1612 		fport = up[0];
1613 		IPCL_DEBUG_LVL(512, ("ipcl_udp_classify_v6 %x %x", lport,
1614 		    fport));
1615 		connfp = &ipcl_udp_fanout[IPCL_UDP_HASH(lport)];
1616 		mutex_enter(&connfp->connf_lock);
1617 		for (connp = connfp->connf_head; connp != NULL;
1618 		    connp = connp->conn_next) {
1619 			if (IPCL_UDP_MATCH_V6(connp, lport, ip6h->ip6_dst,
1620 			    fport, ip6h->ip6_src) &&
1621 			    (IPCL_ZONE_MATCH(connp, zoneid) ||
1622 			    (unlabeled && connp->conn_mac_exempt)))
1623 				break;
1624 		}
1625 
1626 		if (connp != NULL && is_system_labeled() &&
1627 		    !tsol_receive_local(mp, &ip6h->ip6_dst, IPV6_VERSION,
1628 		    shared_addr, connp)) {
1629 			DTRACE_PROBE3(tx__ip__log__info__classify__udp6,
1630 			    char *, "connp(1) could not receive mp(2)",
1631 			    conn_t *, connp, mblk_t *, mp);
1632 			connp = NULL;
1633 		}
1634 
1635 		if (connp != NULL) {
1636 			CONN_INC_REF(connp);
1637 			mutex_exit(&connfp->connf_lock);
1638 			return (connp);
1639 		}
1640 
1641 		/*
1642 		 * We shouldn't come here for multicast/broadcast packets
1643 		 */
1644 		mutex_exit(&connfp->connf_lock);
1645 		IPCL_DEBUG_LVL(512,
1646 		    ("ipcl_classify_v6: cant find udp conn_t for ports : %x %x",
1647 		    lport, fport));
1648 		break;
1649 	}
1650 
1651 	return (NULL);
1652 }
1653 
1654 /*
1655  * wrapper around ipcl_classify_(v4,v6) routines.
1656  */
1657 conn_t *
1658 ipcl_classify(mblk_t *mp, zoneid_t zoneid)
1659 {
1660 	uint16_t	hdr_len;
1661 	ipha_t		*ipha;
1662 	uint8_t		*nexthdrp;
1663 
1664 	if (MBLKL(mp) < sizeof (ipha_t))
1665 		return (NULL);
1666 
1667 	switch (IPH_HDR_VERSION(mp->b_rptr)) {
1668 	case IPV4_VERSION:
1669 		ipha = (ipha_t *)mp->b_rptr;
1670 		hdr_len = IPH_HDR_LENGTH(ipha);
1671 		return (ipcl_classify_v4(mp, ipha->ipha_protocol, hdr_len,
1672 		    zoneid));
1673 	case IPV6_VERSION:
1674 		if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr,
1675 		    &hdr_len, &nexthdrp))
1676 			return (NULL);
1677 
1678 		return (ipcl_classify_v6(mp, *nexthdrp, hdr_len, zoneid));
1679 	}
1680 
1681 	return (NULL);
1682 }
1683 
1684 conn_t *
1685 ipcl_classify_raw(mblk_t *mp, uint8_t protocol, zoneid_t zoneid,
1686     uint32_t ports, ipha_t *hdr)
1687 {
1688 	connf_t		*connfp;
1689 	conn_t		*connp;
1690 	in_port_t	lport;
1691 	int		af;
1692 	boolean_t	shared_addr;
1693 	boolean_t	unlabeled;
1694 	const void	*dst;
1695 
1696 	lport = ((uint16_t *)&ports)[1];
1697 
1698 	unlabeled = B_FALSE;
1699 	/* Cred can be null on IPv6 */
1700 	if (is_system_labeled()) {
1701 		cred_t *cr = DB_CRED(mp);
1702 
1703 		unlabeled = (cr != NULL &&
1704 		    crgetlabel(cr)->tsl_flags & TSLF_UNLABELED) != 0;
1705 	}
1706 	shared_addr = (zoneid == ALL_ZONES);
1707 	if (shared_addr) {
1708 		zoneid = tsol_mlp_findzone(protocol, lport);
1709 		/*
1710 		 * If no shared MLP is found, tsol_mlp_findzone returns
1711 		 * ALL_ZONES.  In that case, we assume it's SLP, and search for
1712 		 * the zone based on the packet label.
1713 		 *
1714 		 * If there is such a zone, we prefer to find a connection in
1715 		 * it.  Otherwise, we look for a MAC-exempt connection in any
1716 		 * zone whose label dominates the default label on the packet.
1717 		 */
1718 		if (zoneid == ALL_ZONES)
1719 			zoneid = tsol_packet_to_zoneid(mp);
1720 		else
1721 			unlabeled = B_FALSE;
1722 	}
1723 
1724 	af = IPH_HDR_VERSION(hdr);
1725 	dst = af == IPV4_VERSION ? (const void *)&hdr->ipha_dst :
1726 	    (const void *)&((ip6_t *)hdr)->ip6_dst;
1727 	connfp = &ipcl_raw_fanout[IPCL_RAW_HASH(ntohs(lport))];
1728 
1729 	mutex_enter(&connfp->connf_lock);
1730 	for (connp = connfp->connf_head; connp != NULL;
1731 	    connp = connp->conn_next) {
1732 		/* We don't allow v4 fallback for v6 raw socket. */
1733 		if (af == (connp->conn_af_isv6 ? IPV4_VERSION :
1734 		    IPV6_VERSION))
1735 			continue;
1736 		if (connp->conn_fully_bound) {
1737 			if (af == IPV4_VERSION) {
1738 				if (!IPCL_CONN_MATCH(connp, protocol,
1739 				    hdr->ipha_src, hdr->ipha_dst, ports))
1740 					continue;
1741 			} else {
1742 				if (!IPCL_CONN_MATCH_V6(connp, protocol,
1743 				    ((ip6_t *)hdr)->ip6_src,
1744 				    ((ip6_t *)hdr)->ip6_dst, ports))
1745 					continue;
1746 			}
1747 		} else {
1748 			if (af == IPV4_VERSION) {
1749 				if (!IPCL_BIND_MATCH(connp, protocol,
1750 				    hdr->ipha_dst, lport))
1751 					continue;
1752 			} else {
1753 				if (!IPCL_BIND_MATCH_V6(connp, protocol,
1754 				    ((ip6_t *)hdr)->ip6_dst, lport))
1755 					continue;
1756 			}
1757 		}
1758 
1759 		if (IPCL_ZONE_MATCH(connp, zoneid) ||
1760 		    (unlabeled && connp->conn_mac_exempt))
1761 			break;
1762 	}
1763 	/*
1764 	 * If the connection is fully-bound and connection-oriented (TCP or
1765 	 * SCTP), then we've already validated the remote system's label.
1766 	 * There's no need to do it again for every packet.
1767 	 */
1768 	if (connp != NULL && is_system_labeled() && (!connp->conn_fully_bound ||
1769 	    !(connp->conn_flags & (IPCL_TCP|IPCL_SCTPCONN))) &&
1770 	    !tsol_receive_local(mp, dst, af, shared_addr, connp)) {
1771 		DTRACE_PROBE3(tx__ip__log__info__classify__rawip,
1772 		    char *, "connp(1) could not receive mp(2)",
1773 		    conn_t *, connp, mblk_t *, mp);
1774 		connp = NULL;
1775 	}
1776 
1777 	if (connp != NULL)
1778 		goto found;
1779 	mutex_exit(&connfp->connf_lock);
1780 
1781 	/* Try to look for a wildcard match. */
1782 	connfp = &ipcl_raw_fanout[IPCL_RAW_HASH(0)];
1783 	mutex_enter(&connfp->connf_lock);
1784 	for (connp = connfp->connf_head; connp != NULL;
1785 	    connp = connp->conn_next) {
1786 		/* We don't allow v4 fallback for v6 raw socket. */
1787 		if ((af == (connp->conn_af_isv6 ? IPV4_VERSION :
1788 		    IPV6_VERSION)) || !IPCL_ZONE_MATCH(connp, zoneid)) {
1789 			continue;
1790 		}
1791 		if (af == IPV4_VERSION) {
1792 			if (IPCL_RAW_MATCH(connp, protocol, hdr->ipha_dst))
1793 				break;
1794 		} else {
1795 			if (IPCL_RAW_MATCH_V6(connp, protocol,
1796 			    ((ip6_t *)hdr)->ip6_dst)) {
1797 				break;
1798 			}
1799 		}
1800 	}
1801 
1802 	if (connp != NULL)
1803 		goto found;
1804 
1805 	mutex_exit(&connfp->connf_lock);
1806 	return (NULL);
1807 
1808 found:
1809 	ASSERT(connp != NULL);
1810 	CONN_INC_REF(connp);
1811 	mutex_exit(&connfp->connf_lock);
1812 	return (connp);
1813 }
1814 
1815 /* ARGSUSED */
1816 static int
1817 ipcl_tcpconn_constructor(void *buf, void *cdrarg, int kmflags)
1818 {
1819 	itc_t	*itc = (itc_t *)buf;
1820 	conn_t 	*connp = &itc->itc_conn;
1821 	tcp_t	*tcp = &itc->itc_tcp;
1822 	bzero(itc, sizeof (itc_t));
1823 	tcp->tcp_timercache = tcp_timermp_alloc(KM_NOSLEEP);
1824 	connp->conn_tcp = tcp;
1825 	connp->conn_flags = IPCL_TCPCONN;
1826 	connp->conn_ulp = IPPROTO_TCP;
1827 	tcp->tcp_connp = connp;
1828 	return (0);
1829 }
1830 
1831 /* ARGSUSED */
1832 static void
1833 ipcl_tcpconn_destructor(void *buf, void *cdrarg)
1834 {
1835 	tcp_timermp_free(((conn_t *)buf)->conn_tcp);
1836 }
1837 
1838 /*
1839  * All conns are inserted in a global multi-list for the benefit of
1840  * walkers. The walk is guaranteed to walk all open conns at the time
1841  * of the start of the walk exactly once. This property is needed to
1842  * achieve some cleanups during unplumb of interfaces. This is achieved
1843  * as follows.
1844  *
1845  * ipcl_conn_create and ipcl_conn_destroy are the only functions that
1846  * call the insert and delete functions below at creation and deletion
1847  * time respectively. The conn never moves or changes its position in this
1848  * multi-list during its lifetime. CONN_CONDEMNED ensures that the refcnt
1849  * won't increase due to walkers, once the conn deletion has started. Note
1850  * that we can't remove the conn from the global list and then wait for
1851  * the refcnt to drop to zero, since walkers would then see a truncated
1852  * list. CONN_INCIPIENT ensures that walkers don't start looking at
1853  * conns until ip_open is ready to make them globally visible.
1854  * The global round robin multi-list locks are held only to get the
1855  * next member/insertion/deletion and contention should be negligible
1856  * if the multi-list is much greater than the number of cpus.
1857  */
1858 void
1859 ipcl_globalhash_insert(conn_t *connp)
1860 {
1861 	int	index;
1862 
1863 	/*
1864 	 * No need for atomic here. Approximate even distribution
1865 	 * in the global lists is sufficient.
1866 	 */
1867 	conn_g_index++;
1868 	index = conn_g_index & (CONN_G_HASH_SIZE - 1);
1869 
1870 	connp->conn_g_prev = NULL;
1871 	/*
1872 	 * Mark as INCIPIENT, so that walkers will ignore this
1873 	 * for now, till ip_open is ready to make it visible globally.
1874 	 */
1875 	connp->conn_state_flags |= CONN_INCIPIENT;
1876 
1877 	/* Insert at the head of the list */
1878 	mutex_enter(&ipcl_globalhash_fanout[index].connf_lock);
1879 	connp->conn_g_next = ipcl_globalhash_fanout[index].connf_head;
1880 	if (connp->conn_g_next != NULL)
1881 		connp->conn_g_next->conn_g_prev = connp;
1882 	ipcl_globalhash_fanout[index].connf_head = connp;
1883 
1884 	/* The fanout bucket this conn points to */
1885 	connp->conn_g_fanout = &ipcl_globalhash_fanout[index];
1886 
1887 	mutex_exit(&ipcl_globalhash_fanout[index].connf_lock);
1888 }
1889 
1890 void
1891 ipcl_globalhash_remove(conn_t *connp)
1892 {
1893 	/*
1894 	 * We were never inserted in the global multi list.
1895 	 * IPCL_NONE variety is never inserted in the global multilist
1896 	 * since it is presumed to not need any cleanup and is transient.
1897 	 */
1898 	if (connp->conn_g_fanout == NULL)
1899 		return;
1900 
1901 	mutex_enter(&connp->conn_g_fanout->connf_lock);
1902 	if (connp->conn_g_prev != NULL)
1903 		connp->conn_g_prev->conn_g_next = connp->conn_g_next;
1904 	else
1905 		connp->conn_g_fanout->connf_head = connp->conn_g_next;
1906 	if (connp->conn_g_next != NULL)
1907 		connp->conn_g_next->conn_g_prev = connp->conn_g_prev;
1908 	mutex_exit(&connp->conn_g_fanout->connf_lock);
1909 
1910 	/* Better to stumble on a null pointer than to corrupt memory */
1911 	connp->conn_g_next = NULL;
1912 	connp->conn_g_prev = NULL;
1913 }
1914 
1915 /*
1916  * Walk the list of all conn_t's in the system, calling the function provided
1917  * with the specified argument for each.
1918  * Applies to both IPv4 and IPv6.
1919  *
1920  * IPCs may hold pointers to ipif/ill. To guard against stale pointers
1921  * ipcl_walk() is called to cleanup the conn_t's, typically when an interface is
1922  * unplumbed or removed. New conn_t's that are created while we are walking
1923  * may be missed by this walk, because they are not necessarily inserted
1924  * at the tail of the list. They are new conn_t's and thus don't have any
1925  * stale pointers. The CONN_CLOSING flag ensures that no new reference
1926  * is created to the struct that is going away.
1927  */
1928 void
1929 ipcl_walk(pfv_t func, void *arg)
1930 {
1931 	int	i;
1932 	conn_t	*connp;
1933 	conn_t	*prev_connp;
1934 
1935 	for (i = 0; i < CONN_G_HASH_SIZE; i++) {
1936 		mutex_enter(&ipcl_globalhash_fanout[i].connf_lock);
1937 		prev_connp = NULL;
1938 		connp = ipcl_globalhash_fanout[i].connf_head;
1939 		while (connp != NULL) {
1940 			mutex_enter(&connp->conn_lock);
1941 			if (connp->conn_state_flags &
1942 			    (CONN_CONDEMNED | CONN_INCIPIENT)) {
1943 				mutex_exit(&connp->conn_lock);
1944 				connp = connp->conn_g_next;
1945 				continue;
1946 			}
1947 			CONN_INC_REF_LOCKED(connp);
1948 			mutex_exit(&connp->conn_lock);
1949 			mutex_exit(&ipcl_globalhash_fanout[i].connf_lock);
1950 			(*func)(connp, arg);
1951 			if (prev_connp != NULL)
1952 				CONN_DEC_REF(prev_connp);
1953 			mutex_enter(&ipcl_globalhash_fanout[i].connf_lock);
1954 			prev_connp = connp;
1955 			connp = connp->conn_g_next;
1956 		}
1957 		mutex_exit(&ipcl_globalhash_fanout[i].connf_lock);
1958 		if (prev_connp != NULL)
1959 			CONN_DEC_REF(prev_connp);
1960 	}
1961 }
1962 
1963 /*
1964  * Search for a peer TCP/IPv4 loopback conn by doing a reverse lookup on
1965  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
1966  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
1967  * (peer tcp in ESTABLISHED state).
1968  */
1969 conn_t *
1970 ipcl_conn_tcp_lookup_reversed_ipv4(conn_t *connp, ipha_t *ipha, tcph_t *tcph)
1971 {
1972 	uint32_t ports;
1973 	uint16_t *pports = (uint16_t *)&ports;
1974 	connf_t	*connfp;
1975 	conn_t	*tconnp;
1976 	boolean_t zone_chk;
1977 
1978 	/*
1979 	 * If either the source of destination address is loopback, then
1980 	 * both endpoints must be in the same Zone.  Otherwise, both of
1981 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
1982 	 * state) and the endpoints may reside in different Zones.
1983 	 */
1984 	zone_chk = (ipha->ipha_src == htonl(INADDR_LOOPBACK) ||
1985 	    ipha->ipha_dst == htonl(INADDR_LOOPBACK));
1986 
1987 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
1988 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
1989 
1990 	connfp = &ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst, ports)];
1991 
1992 	mutex_enter(&connfp->connf_lock);
1993 	for (tconnp = connfp->connf_head; tconnp != NULL;
1994 	    tconnp = tconnp->conn_next) {
1995 
1996 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
1997 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
1998 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
1999 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
2000 
2001 			ASSERT(tconnp != connp);
2002 			CONN_INC_REF(tconnp);
2003 			mutex_exit(&connfp->connf_lock);
2004 			return (tconnp);
2005 		}
2006 	}
2007 	mutex_exit(&connfp->connf_lock);
2008 	return (NULL);
2009 }
2010 
2011 /*
2012  * Search for a peer TCP/IPv6 loopback conn by doing a reverse lookup on
2013  * the {src, dst, lport, fport} quadruplet.  Returns with conn reference
2014  * held; caller must call CONN_DEC_REF.  Only checks for connected entries
2015  * (peer tcp in ESTABLISHED state).
2016  */
2017 conn_t *
2018 ipcl_conn_tcp_lookup_reversed_ipv6(conn_t *connp, ip6_t *ip6h, tcph_t *tcph)
2019 {
2020 	uint32_t ports;
2021 	uint16_t *pports = (uint16_t *)&ports;
2022 	connf_t	*connfp;
2023 	conn_t	*tconnp;
2024 	boolean_t zone_chk;
2025 
2026 	/*
2027 	 * If either the source of destination address is loopback, then
2028 	 * both endpoints must be in the same Zone.  Otherwise, both of
2029 	 * the addresses are system-wide unique (tcp is in ESTABLISHED
2030 	 * state) and the endpoints may reside in different Zones.  We
2031 	 * don't do Zone check for link local address(es) because the
2032 	 * current Zone implementation treats each link local address as
2033 	 * being unique per system node, i.e. they belong to global Zone.
2034 	 */
2035 	zone_chk = (IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_src) ||
2036 	    IN6_IS_ADDR_LOOPBACK(&ip6h->ip6_dst));
2037 
2038 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
2039 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
2040 
2041 	connfp = &ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst, ports)];
2042 
2043 	mutex_enter(&connfp->connf_lock);
2044 	for (tconnp = connfp->connf_head; tconnp != NULL;
2045 	    tconnp = tconnp->conn_next) {
2046 
2047 		/* We skip tcp_bound_if check here as this is loopback tcp */
2048 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
2049 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
2050 		    tconnp->conn_tcp->tcp_state == TCPS_ESTABLISHED &&
2051 		    (!zone_chk || tconnp->conn_zoneid == connp->conn_zoneid)) {
2052 
2053 			ASSERT(tconnp != connp);
2054 			CONN_INC_REF(tconnp);
2055 			mutex_exit(&connfp->connf_lock);
2056 			return (tconnp);
2057 		}
2058 	}
2059 	mutex_exit(&connfp->connf_lock);
2060 	return (NULL);
2061 }
2062 
2063 /*
2064  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
2065  * Returns with conn reference held. Caller must call CONN_DEC_REF.
2066  * Only checks for connected entries i.e. no INADDR_ANY checks.
2067  */
2068 conn_t *
2069 ipcl_tcp_lookup_reversed_ipv4(ipha_t *ipha, tcph_t *tcph, int min_state)
2070 {
2071 	uint32_t ports;
2072 	uint16_t *pports;
2073 	connf_t	*connfp;
2074 	conn_t	*tconnp;
2075 
2076 	pports = (uint16_t *)&ports;
2077 	bcopy(tcph->th_fport, &pports[0], sizeof (uint16_t));
2078 	bcopy(tcph->th_lport, &pports[1], sizeof (uint16_t));
2079 
2080 	connfp = &ipcl_conn_fanout[IPCL_CONN_HASH(ipha->ipha_dst, ports)];
2081 
2082 	mutex_enter(&connfp->connf_lock);
2083 	for (tconnp = connfp->connf_head; tconnp != NULL;
2084 	    tconnp = tconnp->conn_next) {
2085 
2086 		if (IPCL_CONN_MATCH(tconnp, IPPROTO_TCP,
2087 		    ipha->ipha_dst, ipha->ipha_src, ports) &&
2088 		    tconnp->conn_tcp->tcp_state >= min_state) {
2089 
2090 			CONN_INC_REF(tconnp);
2091 			mutex_exit(&connfp->connf_lock);
2092 			return (tconnp);
2093 		}
2094 	}
2095 	mutex_exit(&connfp->connf_lock);
2096 	return (NULL);
2097 }
2098 
2099 /*
2100  * Find an exact {src, dst, lport, fport} match for a bounced datagram.
2101  * Returns with conn reference held. Caller must call CONN_DEC_REF.
2102  * Only checks for connected entries i.e. no INADDR_ANY checks.
2103  * Match on ifindex in addition to addresses.
2104  */
2105 conn_t *
2106 ipcl_tcp_lookup_reversed_ipv6(ip6_t *ip6h, tcpha_t *tcpha, int min_state,
2107     uint_t ifindex)
2108 {
2109 	tcp_t	*tcp;
2110 	uint32_t ports;
2111 	uint16_t *pports;
2112 	connf_t	*connfp;
2113 	conn_t	*tconnp;
2114 
2115 	pports = (uint16_t *)&ports;
2116 	pports[0] = tcpha->tha_fport;
2117 	pports[1] = tcpha->tha_lport;
2118 
2119 	connfp = &ipcl_conn_fanout[IPCL_CONN_HASH_V6(ip6h->ip6_dst, ports)];
2120 
2121 	mutex_enter(&connfp->connf_lock);
2122 	for (tconnp = connfp->connf_head; tconnp != NULL;
2123 	    tconnp = tconnp->conn_next) {
2124 
2125 		tcp = tconnp->conn_tcp;
2126 		if (IPCL_CONN_MATCH_V6(tconnp, IPPROTO_TCP,
2127 		    ip6h->ip6_dst, ip6h->ip6_src, ports) &&
2128 		    tcp->tcp_state >= min_state &&
2129 		    (tcp->tcp_bound_if == 0 ||
2130 		    tcp->tcp_bound_if == ifindex)) {
2131 
2132 			CONN_INC_REF(tconnp);
2133 			mutex_exit(&connfp->connf_lock);
2134 			return (tconnp);
2135 		}
2136 	}
2137 	mutex_exit(&connfp->connf_lock);
2138 	return (NULL);
2139 }
2140 
2141 /*
2142  * Finds a TCP/IPv4 listening connection; called by tcp_disconnect to locate
2143  * a listener when changing state.
2144  */
2145 conn_t *
2146 ipcl_lookup_listener_v4(uint16_t lport, ipaddr_t laddr, zoneid_t zoneid)
2147 {
2148 	connf_t		*bind_connfp;
2149 	conn_t		*connp;
2150 	tcp_t		*tcp;
2151 
2152 	/*
2153 	 * Avoid false matches for packets sent to an IP destination of
2154 	 * all zeros.
2155 	 */
2156 	if (laddr == 0)
2157 		return (NULL);
2158 
2159 	ASSERT(zoneid != ALL_ZONES);
2160 
2161 	bind_connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
2162 	mutex_enter(&bind_connfp->connf_lock);
2163 	for (connp = bind_connfp->connf_head; connp != NULL;
2164 	    connp = connp->conn_next) {
2165 		tcp = connp->conn_tcp;
2166 		if (IPCL_BIND_MATCH(connp, IPPROTO_TCP, laddr, lport) &&
2167 		    IPCL_ZONE_MATCH(connp, zoneid) &&
2168 		    (tcp->tcp_listener == NULL)) {
2169 			CONN_INC_REF(connp);
2170 			mutex_exit(&bind_connfp->connf_lock);
2171 			return (connp);
2172 		}
2173 	}
2174 	mutex_exit(&bind_connfp->connf_lock);
2175 	return (NULL);
2176 }
2177 
2178 /*
2179  * Finds a TCP/IPv6 listening connection; called by tcp_disconnect to locate
2180  * a listener when changing state.
2181  */
2182 conn_t *
2183 ipcl_lookup_listener_v6(uint16_t lport, in6_addr_t *laddr, uint_t ifindex,
2184     zoneid_t zoneid)
2185 {
2186 	connf_t		*bind_connfp;
2187 	conn_t		*connp = NULL;
2188 	tcp_t		*tcp;
2189 
2190 	/*
2191 	 * Avoid false matches for packets sent to an IP destination of
2192 	 * all zeros.
2193 	 */
2194 	if (IN6_IS_ADDR_UNSPECIFIED(laddr))
2195 		return (NULL);
2196 
2197 	ASSERT(zoneid != ALL_ZONES);
2198 
2199 	bind_connfp = &ipcl_bind_fanout[IPCL_BIND_HASH(lport)];
2200 	mutex_enter(&bind_connfp->connf_lock);
2201 	for (connp = bind_connfp->connf_head; connp != NULL;
2202 	    connp = connp->conn_next) {
2203 		tcp = connp->conn_tcp;
2204 		if (IPCL_BIND_MATCH_V6(connp, IPPROTO_TCP, *laddr, lport) &&
2205 		    IPCL_ZONE_MATCH(connp, zoneid) &&
2206 		    (tcp->tcp_bound_if == 0 ||
2207 		    tcp->tcp_bound_if == ifindex) &&
2208 		    tcp->tcp_listener == NULL) {
2209 			CONN_INC_REF(connp);
2210 			mutex_exit(&bind_connfp->connf_lock);
2211 			return (connp);
2212 		}
2213 	}
2214 	mutex_exit(&bind_connfp->connf_lock);
2215 	return (NULL);
2216 }
2217 
2218 /*
2219  * ipcl_get_next_conn
2220  *	get the next entry in the conn global list
2221  *	and put a reference on the next_conn.
2222  *	decrement the reference on the current conn.
2223  *
2224  * This is an iterator based walker function that also provides for
2225  * some selection by the caller. It walks through the conn_hash bucket
2226  * searching for the next valid connp in the list, and selects connections
2227  * that are neither closed nor condemned. It also REFHOLDS the conn
2228  * thus ensuring that the conn exists when the caller uses the conn.
2229  */
2230 conn_t *
2231 ipcl_get_next_conn(connf_t *connfp, conn_t *connp, uint32_t conn_flags)
2232 {
2233 	conn_t	*next_connp;
2234 
2235 	if (connfp == NULL)
2236 		return (NULL);
2237 
2238 	mutex_enter(&connfp->connf_lock);
2239 
2240 	next_connp = (connp == NULL) ?
2241 	    connfp->connf_head : connp->conn_g_next;
2242 
2243 	while (next_connp != NULL) {
2244 		mutex_enter(&next_connp->conn_lock);
2245 		if (!(next_connp->conn_flags & conn_flags) ||
2246 		    (next_connp->conn_state_flags &
2247 		    (CONN_CONDEMNED | CONN_INCIPIENT))) {
2248 			/*
2249 			 * This conn has been condemned or
2250 			 * is closing, or the flags don't match
2251 			 */
2252 			mutex_exit(&next_connp->conn_lock);
2253 			next_connp = next_connp->conn_g_next;
2254 			continue;
2255 		}
2256 		CONN_INC_REF_LOCKED(next_connp);
2257 		mutex_exit(&next_connp->conn_lock);
2258 		break;
2259 	}
2260 
2261 	mutex_exit(&connfp->connf_lock);
2262 
2263 	if (connp != NULL)
2264 		CONN_DEC_REF(connp);
2265 
2266 	return (next_connp);
2267 }
2268 
2269 #ifdef CONN_DEBUG
2270 /*
2271  * Trace of the last NBUF refhold/refrele
2272  */
2273 int
2274 conn_trace_ref(conn_t *connp)
2275 {
2276 	int	last;
2277 	conn_trace_t	*ctb;
2278 
2279 	ASSERT(MUTEX_HELD(&connp->conn_lock));
2280 	last = connp->conn_trace_last;
2281 	last++;
2282 	if (last == CONN_TRACE_MAX)
2283 		last = 0;
2284 
2285 	ctb = &connp->conn_trace_buf[last];
2286 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, IP_STACK_DEPTH);
2287 	connp->conn_trace_last = last;
2288 	return (1);
2289 }
2290 
2291 int
2292 conn_untrace_ref(conn_t *connp)
2293 {
2294 	int	last;
2295 	conn_trace_t	*ctb;
2296 
2297 	ASSERT(MUTEX_HELD(&connp->conn_lock));
2298 	last = connp->conn_trace_last;
2299 	last++;
2300 	if (last == CONN_TRACE_MAX)
2301 		last = 0;
2302 
2303 	ctb = &connp->conn_trace_buf[last];
2304 	ctb->ctb_depth = getpcstack(ctb->ctb_stack, IP_STACK_DEPTH);
2305 	connp->conn_trace_last = last;
2306 	return (1);
2307 }
2308 #endif
2309