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