xref: /illumos-gate/usr/src/uts/common/inet/ip.h (revision eda50310abb3984bab11856a2aca8936d26881cb)
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 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #ifndef	_INET_IP_H
29 #define	_INET_IP_H
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 #ifdef	__cplusplus
34 extern "C" {
35 #endif
36 
37 #include <sys/isa_defs.h>
38 #include <sys/types.h>
39 #include <inet/mib2.h>
40 #include <inet/nd.h>
41 #include <sys/atomic.h>
42 #include <net/if_dl.h>
43 #include <net/if.h>
44 #include <netinet/ip.h>
45 #include <netinet/igmp.h>
46 #include <sys/neti.h>
47 #include <sys/hook.h>
48 #include <sys/hook_event.h>
49 #include <sys/hook_impl.h>
50 
51 #ifdef _KERNEL
52 #include <netinet/ip6.h>
53 #include <sys/avl.h>
54 #include <sys/vmem.h>
55 #include <sys/squeue.h>
56 #include <net/route.h>
57 #include <sys/systm.h>
58 #include <sys/multidata.h>
59 #include <net/radix.h>
60 
61 #ifdef DEBUG
62 #define	ILL_DEBUG
63 #define	IRE_DEBUG
64 #define	NCE_DEBUG
65 #define	CONN_DEBUG
66 #endif
67 
68 #define	IP_DEBUG
69 /*
70  * The mt-streams(9F) flags for the IP module; put here so that other
71  * "drivers" that are actually IP (e.g., ICMP, UDP) can use the same set
72  * of flags.
73  */
74 #define	IP_DEVMTFLAGS D_MP
75 #endif	/* _KERNEL */
76 
77 #define	IP_MOD_NAME	"ip"
78 #define	IP_DEV_NAME	"/dev/ip"
79 #define	IP6_DEV_NAME	"/dev/ip6"
80 
81 #define	UDP_MOD_NAME	"udp"
82 #define	UDP_DEV_NAME	"/dev/udp"
83 #define	UDP6_DEV_NAME	"/dev/udp6"
84 
85 #define	TCP_MOD_NAME	"tcp"
86 #define	TCP_DEV_NAME	"/dev/tcp"
87 #define	TCP6_DEV_NAME	"/dev/tcp6"
88 
89 #define	SCTP_MOD_NAME	"sctp"
90 
91 /* Minor numbers */
92 #define	IPV4_MINOR	0
93 #define	IPV6_MINOR	1
94 #define	TCP_MINOR	2
95 #define	TCP_MINOR6	3
96 
97 #ifndef	_IPADDR_T
98 #define	_IPADDR_T
99 typedef uint32_t ipaddr_t;
100 #endif
101 
102 /* Number of bits in an address */
103 #define	IP_ABITS		32
104 #define	IPV6_ABITS		128
105 
106 #define	IP_HOST_MASK		(ipaddr_t)0xffffffffU
107 
108 #define	IP_CSUM(mp, off, sum)		(~ip_cksum(mp, off, sum) & 0xFFFF)
109 #define	IP_CSUM_PARTIAL(mp, off, sum)	ip_cksum(mp, off, sum)
110 #define	IP_BCSUM_PARTIAL(bp, len, sum)	bcksum(bp, len, sum)
111 #define	IP_MD_CSUM(pd, off, sum)	(~ip_md_cksum(pd, off, sum) & 0xffff)
112 #define	IP_MD_CSUM_PARTIAL(pd, off, sum) ip_md_cksum(pd, off, sum)
113 
114 /*
115  * Flag to IP write side to indicate that the appln has sent in a pre-built
116  * IP header. Stored in ipha_ident (which is otherwise zero).
117  */
118 #define	IP_HDR_INCLUDED			0xFFFF
119 
120 #define	ILL_FRAG_HASH_TBL_COUNT	((unsigned int)64)
121 #define	ILL_FRAG_HASH_TBL_SIZE	(ILL_FRAG_HASH_TBL_COUNT * sizeof (ipfb_t))
122 
123 #define	IPV4_ADDR_LEN			4
124 #define	IP_ADDR_LEN			IPV4_ADDR_LEN
125 #define	IP_ARP_PROTO_TYPE		0x0800
126 
127 #define	IPV4_VERSION			4
128 #define	IP_VERSION			IPV4_VERSION
129 #define	IP_SIMPLE_HDR_LENGTH_IN_WORDS	5
130 #define	IP_SIMPLE_HDR_LENGTH		20
131 #define	IP_MAX_HDR_LENGTH		60
132 
133 #define	IP_MAX_OPT_LENGTH (IP_MAX_HDR_LENGTH-IP_SIMPLE_HDR_LENGTH)
134 
135 #define	IP_MIN_MTU			(IP_MAX_HDR_LENGTH + 8)	/* 68 bytes */
136 
137 /*
138  * XXX IP_MAXPACKET is defined in <netinet/ip.h> as well. At some point the
139  * 2 files should be cleaned up to remove all redundant definitions.
140  */
141 #define	IP_MAXPACKET			65535
142 #define	IP_SIMPLE_HDR_VERSION \
143 	((IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS)
144 
145 #define	UDPH_SIZE			8
146 
147 /* Leave room for ip_newroute to tack on the src and target addresses */
148 #define	OK_RESOLVER_MP(mp)						\
149 	((mp) && ((mp)->b_wptr - (mp)->b_rptr) >= (2 * IP_ADDR_LEN))
150 
151 /*
152  * Constants and type definitions to support IP IOCTL commands
153  */
154 #define	IP_IOCTL			(('i'<<8)|'p')
155 #define	IP_IOC_IRE_DELETE		4
156 #define	IP_IOC_IRE_DELETE_NO_REPLY	5
157 #define	IP_IOC_IRE_ADVISE_NO_REPLY	6
158 #define	IP_IOC_RTS_REQUEST		7
159 
160 /* Common definitions used by IP IOCTL data structures */
161 typedef struct ipllcmd_s {
162 	uint_t	ipllc_cmd;
163 	uint_t	ipllc_name_offset;
164 	uint_t	ipllc_name_length;
165 } ipllc_t;
166 
167 /* IP IRE Change Command Structure. */
168 typedef struct ipic_s {
169 	ipllc_t	ipic_ipllc;
170 	uint_t	ipic_ire_type;
171 	uint_t	ipic_max_frag;
172 	uint_t	ipic_addr_offset;
173 	uint_t	ipic_addr_length;
174 	uint_t	ipic_mask_offset;
175 	uint_t	ipic_mask_length;
176 	uint_t	ipic_src_addr_offset;
177 	uint_t	ipic_src_addr_length;
178 	uint_t	ipic_ll_hdr_offset;
179 	uint_t	ipic_ll_hdr_length;
180 	uint_t	ipic_gateway_addr_offset;
181 	uint_t	ipic_gateway_addr_length;
182 	clock_t	ipic_rtt;
183 	uint32_t ipic_ssthresh;
184 	clock_t	ipic_rtt_sd;
185 	uchar_t ipic_ire_marks;
186 } ipic_t;
187 
188 #define	ipic_cmd		ipic_ipllc.ipllc_cmd
189 #define	ipic_ll_name_length	ipic_ipllc.ipllc_name_length
190 #define	ipic_ll_name_offset	ipic_ipllc.ipllc_name_offset
191 
192 /* IP IRE Delete Command Structure. */
193 typedef struct ipid_s {
194 	ipllc_t	ipid_ipllc;
195 	uint_t	ipid_ire_type;
196 	uint_t	ipid_addr_offset;
197 	uint_t	ipid_addr_length;
198 	uint_t	ipid_mask_offset;
199 	uint_t	ipid_mask_length;
200 } ipid_t;
201 
202 #define	ipid_cmd		ipid_ipllc.ipllc_cmd
203 
204 #ifdef _KERNEL
205 /*
206  * Temporary state for ip options parser.
207  */
208 typedef struct ipoptp_s
209 {
210 	uint8_t		*ipoptp_next;	/* next option to look at */
211 	uint8_t		*ipoptp_end;	/* end of options */
212 	uint8_t		*ipoptp_cur;	/* start of current option */
213 	uint8_t		ipoptp_len;	/* length of current option */
214 	uint32_t	ipoptp_flags;
215 } ipoptp_t;
216 
217 /*
218  * Flag(s) for ipoptp_flags
219  */
220 #define	IPOPTP_ERROR	0x00000001
221 #endif	/* _KERNEL */
222 
223 
224 /* Controls forwarding of IP packets, set via ndd */
225 #define	IP_FORWARD_NEVER	0
226 #define	IP_FORWARD_ALWAYS	1
227 
228 #define	WE_ARE_FORWARDING	(ip_g_forward == IP_FORWARD_ALWAYS)
229 
230 #define	IPH_HDR_LENGTH(ipha)						\
231 	((int)(((ipha_t *)ipha)->ipha_version_and_hdr_length & 0xF) << 2)
232 
233 #define	IPH_HDR_VERSION(ipha)						\
234 	((int)(((ipha_t *)ipha)->ipha_version_and_hdr_length) >> 4)
235 
236 #ifdef _KERNEL
237 /*
238  * IP reassembly macros.  We hide starting and ending offsets in b_next and
239  * b_prev of messages on the reassembly queue.	The messages are chained using
240  * b_cont.  These macros are used in ip_reassemble() so we don't have to see
241  * the ugly casts and assignments.
242  * Note that the offsets are <= 64k i.e. a uint_t is sufficient to represent
243  * them.
244  */
245 #define	IP_REASS_START(mp)		((uint_t)(uintptr_t)((mp)->b_next))
246 #define	IP_REASS_SET_START(mp, u)	\
247 	((mp)->b_next = (mblk_t *)(uintptr_t)(u))
248 #define	IP_REASS_END(mp)		((uint_t)(uintptr_t)((mp)->b_prev))
249 #define	IP_REASS_SET_END(mp, u)		\
250 	((mp)->b_prev = (mblk_t *)(uintptr_t)(u))
251 
252 #define	IP_REASS_COMPLETE	0x1
253 #define	IP_REASS_PARTIAL	0x2
254 #define	IP_REASS_FAILED		0x4
255 
256 /*
257  * Test to determine whether this is a module instance of IP or a
258  * driver instance of IP.
259  */
260 #define	CONN_Q(q)	(WR(q)->q_next == NULL)
261 
262 #define	Q_TO_CONN(q)	((conn_t *)(q)->q_ptr)
263 #define	Q_TO_TCP(q)	(Q_TO_CONN((q))->conn_tcp)
264 #define	Q_TO_UDP(q)	(Q_TO_CONN((q))->conn_udp)
265 
266 /*
267  * The following two macros are used by IP to get the appropriate
268  * wq and rq for a conn. If it is a TCP conn, then we need
269  * tcp_wq/tcp_rq else, conn_wq/conn_rq. IP can use conn_wq and conn_rq
270  * from a conn directly if it knows that the conn is not TCP.
271  */
272 #define	CONNP_TO_WQ(connp)	\
273 	(IPCL_IS_TCP(connp) ? (connp)->conn_tcp->tcp_wq : (connp)->conn_wq)
274 
275 #define	CONNP_TO_RQ(connp)	RD(CONNP_TO_WQ(connp))
276 
277 #define	GRAB_CONN_LOCK(q)	{				\
278 	if (q != NULL && CONN_Q(q))				\
279 		mutex_enter(&(Q_TO_CONN(q))->conn_lock);	\
280 }
281 
282 #define	RELEASE_CONN_LOCK(q)	{				\
283 	if (q != NULL && CONN_Q(q))				\
284 		mutex_exit(&(Q_TO_CONN(q))->conn_lock);		\
285 }
286 
287 /* "Congestion controlled" protocol */
288 #define	IP_FLOW_CONTROLLED_ULP(p)   ((p) == IPPROTO_TCP || (p) == IPPROTO_SCTP)
289 
290 /*
291  * Complete the pending operation. Usually an ioctl. Can also
292  * be a bind or option management request that got enqueued
293  * in an ipsq_t. Called on completion of the operation.
294  */
295 #define	CONN_OPER_PENDING_DONE(connp)	{			\
296 	mutex_enter(&(connp)->conn_lock);			\
297 	(connp)->conn_oper_pending_ill = NULL;			\
298 	cv_broadcast(&(connp)->conn_refcv);			\
299 	mutex_exit(&(connp)->conn_lock);			\
300 	CONN_DEC_REF(connp);					\
301 }
302 
303 /* Get the credential of an IP queue of unknown type */
304 #define	GET_QUEUE_CRED(wq)						\
305 	((wq)->q_next ? (((ill_t *)(wq)->q_ptr)->ill_credp) \
306 	    : ((Q_TO_CONN((wq)))->conn_cred))
307 
308 /*
309  * Flags for the various ip_fanout_* routines.
310  */
311 #define	IP_FF_SEND_ICMP		0x01	/* Send an ICMP error */
312 #define	IP_FF_HDR_COMPLETE	0x02	/* Call ip_hdr_complete if error */
313 #define	IP_FF_CKSUM		0x04	/* Recompute ipha_cksum if error */
314 #define	IP_FF_RAWIP		0x08	/* Use rawip mib variable */
315 #define	IP_FF_SRC_QUENCH	0x10	/* OK to send ICMP_SOURCE_QUENCH */
316 #define	IP_FF_SYN_ADDIRE	0x20	/* Add IRE if TCP syn packet */
317 #define	IP_FF_IP6INFO		0x80	/* Add ip6i_t if needed */
318 #define	IP_FF_SEND_SLLA		0x100	/* Send source link layer info ? */
319 #define	IPV6_REACHABILITY_CONFIRMATION	0x200	/* Flags for ip_xmit_v6 */
320 #define	IP_FF_NO_MCAST_LOOP	0x400	/* No multicasts for sending zone */
321 
322 /*
323  * Following flags are used by IPQoS to determine if policy processing is
324  * required.
325  */
326 #define	IP6_NO_IPPOLICY		0x800	/* Don't do IPQoS processing */
327 #define	IP6_IN_LLMCAST		0x1000	/* Multicast */
328 
329 #define	IP_FF_LOOPBACK		0x2000	/* Loopback fanout */
330 
331 #ifndef	IRE_DB_TYPE
332 #define	IRE_DB_TYPE	M_SIG
333 #endif
334 
335 #ifndef	IRE_DB_REQ_TYPE
336 #define	IRE_DB_REQ_TYPE	M_PCSIG
337 #endif
338 
339 #ifndef	IRE_ARPRESOLVE_TYPE
340 #define	IRE_ARPRESOLVE_TYPE	M_EVENT
341 #endif
342 
343 /*
344  * Values for squeue switch:
345  */
346 
347 #define	IP_SQUEUE_ENTER_NODRAIN	1
348 #define	IP_SQUEUE_ENTER	2
349 /*
350  * This is part of the interface between Transport provider and
351  * IP which can be used to set policy information. This is usually
352  * accompanied with O_T_BIND_REQ/T_BIND_REQ.ip_bind assumes that
353  * only IPSEC_POLICY_SET is there when it is found in the chain.
354  * The information contained is an struct ipsec_req_t. On success
355  * or failure, either the T_BIND_ACK or the T_ERROR_ACK is returned.
356  * IPSEC_POLICY_SET is never returned.
357  */
358 #define	IPSEC_POLICY_SET	M_SETOPTS
359 
360 #define	IRE_IS_LOCAL(ire)	((ire != NULL) && \
361 				((ire)->ire_type & (IRE_LOCAL | IRE_LOOPBACK)))
362 
363 #define	IRE_IS_TARGET(ire)	((ire != NULL) && \
364 				((ire)->ire_type != IRE_BROADCAST))
365 
366 /* IP Fragmentation Reassembly Header */
367 typedef struct ipf_s {
368 	struct ipf_s	*ipf_hash_next;
369 	struct ipf_s	**ipf_ptphn;	/* Pointer to previous hash next. */
370 	uint32_t	ipf_ident;	/* Ident to match. */
371 	uint8_t		ipf_protocol;	/* Protocol to match. */
372 	uchar_t		ipf_last_frag_seen : 1;	/* Last fragment seen ? */
373 	time_t		ipf_timestamp;	/* Reassembly start time. */
374 	mblk_t		*ipf_mp;	/* mblk we live in. */
375 	mblk_t		*ipf_tail_mp;	/* Frag queue tail pointer. */
376 	int		ipf_hole_cnt;	/* Number of holes (hard-case). */
377 	int		ipf_end;	/* Tail end offset (0 -> hard-case). */
378 	uint_t		ipf_gen;	/* Frag queue generation */
379 	size_t		ipf_count;	/* Count of bytes used by frag */
380 	uint_t		ipf_nf_hdr_len; /* Length of nonfragmented header */
381 	in6_addr_t	ipf_v6src;	/* IPv6 source address */
382 	in6_addr_t	ipf_v6dst;	/* IPv6 dest address */
383 	uint_t		ipf_prev_nexthdr_offset; /* Offset for nexthdr value */
384 	uint8_t		ipf_ecn;	/* ECN info for the fragments */
385 	uint8_t		ipf_num_dups;	/* Number of times dup frags recvd */
386 	uint16_t	ipf_checksum_flags; /* Hardware checksum flags */
387 	uint32_t	ipf_checksum;	/* Partial checksum of fragment data */
388 } ipf_t;
389 
390 #define	ipf_src	V4_PART_OF_V6(ipf_v6src)
391 #define	ipf_dst	V4_PART_OF_V6(ipf_v6dst)
392 
393 typedef enum {
394 	IB_PKT =  0x01,
395 	OB_PKT = 0x02
396 } ip_pkt_t;
397 
398 #define	UPDATE_IB_PKT_COUNT(ire)\
399 	{ \
400 	(ire)->ire_ib_pkt_count++; \
401 	if ((ire)->ire_ipif != NULL) { \
402 		/* \
403 		 * forwarding packet \
404 		 */ \
405 		if ((ire)->ire_type & (IRE_LOCAL|IRE_BROADCAST)) \
406 			atomic_add_32(&(ire)->ire_ipif->ipif_ib_pkt_count, 1);\
407 		else \
408 			atomic_add_32(&(ire)->ire_ipif->ipif_fo_pkt_count, 1);\
409 	} \
410 	}
411 #define	UPDATE_OB_PKT_COUNT(ire)\
412 	{ \
413 	(ire)->ire_ob_pkt_count++;\
414 	if ((ire)->ire_ipif != NULL) { \
415 		atomic_add_32(&(ire)->ire_ipif->ipif_ob_pkt_count, 1); \
416 	} \
417 	}
418 
419 
420 #define	IP_RPUT_LOCAL(q, mp, ipha, ire, recv_ill) \
421 { \
422 	switch (ipha->ipha_protocol) { \
423 		case IPPROTO_UDP: \
424 			ip_udp_input(q, mp, ipha, ire, recv_ill); \
425 			break; \
426 		default: \
427 			ip_proto_input(q, mp, ipha, ire, recv_ill); \
428 			break; \
429 	} \
430 }
431 
432 /*
433  * NCE_EXPIRED is TRUE when we have a non-permanent nce that was
434  * found to be REACHABLE more than ip_ire_arp_interval ms ago.
435  * This macro is used to trigger re-arp of existing nce_t entries
436  * so that they can be updated with the latest information.
437  * nce's will get cleaned up (or updated with new info) in the following
438  * circumstances:
439  * - ip_ire_trash_reclaim will free nce's using ndp_cache_reclaim
440  *    when memory is low,
441  * - ip_arp_news, when updates are received.
442  * - if the nce is NCE_EXPIRED(), it will be re-initialized to ND_INITIAL,
443  *   so that a new arp request will be triggered.
444  * nce_last is the timestamp that indicates when the nce_res_mp in the
445  * nce_t was last updated to a valid link-layer address.  nce_last gets
446  * modified/updated :
447  *  - when the nce is created  or reinit-ed
448  *  - every time we get a sane arp response for the nce.
449  */
450 #define	NCE_EXPIRED(nce)	(nce->nce_last > 0 && \
451 	((nce->nce_flags & NCE_F_PERMANENT) == 0) && \
452 	((TICK_TO_MSEC(lbolt64) - nce->nce_last) > ip_ire_arp_interval))
453 
454 #endif /* _KERNEL */
455 
456 /* ICMP types */
457 #define	ICMP_ECHO_REPLY			0
458 #define	ICMP_DEST_UNREACHABLE		3
459 #define	ICMP_SOURCE_QUENCH		4
460 #define	ICMP_REDIRECT			5
461 #define	ICMP_ECHO_REQUEST		8
462 #define	ICMP_ROUTER_ADVERTISEMENT	9
463 #define	ICMP_ROUTER_SOLICITATION	10
464 #define	ICMP_TIME_EXCEEDED		11
465 #define	ICMP_PARAM_PROBLEM		12
466 #define	ICMP_TIME_STAMP_REQUEST		13
467 #define	ICMP_TIME_STAMP_REPLY		14
468 #define	ICMP_INFO_REQUEST		15
469 #define	ICMP_INFO_REPLY			16
470 #define	ICMP_ADDRESS_MASK_REQUEST	17
471 #define	ICMP_ADDRESS_MASK_REPLY		18
472 
473 /* ICMP_TIME_EXCEEDED codes */
474 #define	ICMP_TTL_EXCEEDED		0
475 #define	ICMP_REASSEMBLY_TIME_EXCEEDED	1
476 
477 /* ICMP_DEST_UNREACHABLE codes */
478 #define	ICMP_NET_UNREACHABLE		0
479 #define	ICMP_HOST_UNREACHABLE		1
480 #define	ICMP_PROTOCOL_UNREACHABLE	2
481 #define	ICMP_PORT_UNREACHABLE		3
482 #define	ICMP_FRAGMENTATION_NEEDED	4
483 #define	ICMP_SOURCE_ROUTE_FAILED	5
484 #define	ICMP_DEST_NET_UNKNOWN		6
485 #define	ICMP_DEST_HOST_UNKNOWN		7
486 #define	ICMP_SRC_HOST_ISOLATED		8
487 #define	ICMP_DEST_NET_UNREACH_ADMIN	9
488 #define	ICMP_DEST_HOST_UNREACH_ADMIN	10
489 #define	ICMP_DEST_NET_UNREACH_TOS	11
490 #define	ICMP_DEST_HOST_UNREACH_TOS	12
491 
492 /* ICMP Header Structure */
493 typedef struct icmph_s {
494 	uint8_t		icmph_type;
495 	uint8_t		icmph_code;
496 	uint16_t	icmph_checksum;
497 	union {
498 		struct { /* ECHO request/response structure */
499 			uint16_t	u_echo_ident;
500 			uint16_t	u_echo_seqnum;
501 		} u_echo;
502 		struct { /* Destination unreachable structure */
503 			uint16_t	u_du_zero;
504 			uint16_t	u_du_mtu;
505 		} u_du;
506 		struct { /* Parameter problem structure */
507 			uint8_t		u_pp_ptr;
508 			uint8_t		u_pp_rsvd[3];
509 		} u_pp;
510 		struct { /* Redirect structure */
511 			ipaddr_t	u_rd_gateway;
512 		} u_rd;
513 	} icmph_u;
514 } icmph_t;
515 
516 #define	icmph_echo_ident	icmph_u.u_echo.u_echo_ident
517 #define	icmph_echo_seqnum	icmph_u.u_echo.u_echo_seqnum
518 #define	icmph_du_zero		icmph_u.u_du.u_du_zero
519 #define	icmph_du_mtu		icmph_u.u_du.u_du_mtu
520 #define	icmph_pp_ptr		icmph_u.u_pp.u_pp_ptr
521 #define	icmph_rd_gateway	icmph_u.u_rd.u_rd_gateway
522 
523 #define	ICMPH_SIZE	8
524 
525 /*
526  * Minimum length of transport layer header included in an ICMP error
527  * message for it to be considered valid.
528  */
529 #define	ICMP_MIN_TP_HDR_LEN	8
530 
531 /* Aligned IP header */
532 typedef struct ipha_s {
533 	uint8_t		ipha_version_and_hdr_length;
534 	uint8_t		ipha_type_of_service;
535 	uint16_t	ipha_length;
536 	uint16_t	ipha_ident;
537 	uint16_t	ipha_fragment_offset_and_flags;
538 	uint8_t		ipha_ttl;
539 	uint8_t		ipha_protocol;
540 	uint16_t	ipha_hdr_checksum;
541 	ipaddr_t	ipha_src;
542 	ipaddr_t	ipha_dst;
543 } ipha_t;
544 
545 #define	IPH_DF		0x4000	/* Don't fragment */
546 #define	IPH_MF		0x2000	/* More fragments to come */
547 #define	IPH_OFFSET	0x1FFF	/* Where the offset lives */
548 #define	IPH_FRAG_HDR	0x8000	/* IPv6 don't fragment bit */
549 
550 /* ECN code points for IPv4 TOS byte and IPv6 traffic class octet. */
551 #define	IPH_ECN_NECT	0x0	/* Not ECN-Capabable Transport */
552 #define	IPH_ECN_ECT1	0x1	/* ECN-Capable Transport, ECT(1) */
553 #define	IPH_ECN_ECT0	0x2	/* ECN-Capable Transport, ECT(0) */
554 #define	IPH_ECN_CE	0x3	/* ECN-Congestion Experienced (CE) */
555 
556 /* IP Mac info structure */
557 typedef struct ip_m_s {
558 	t_uscalar_t	ip_m_mac_type;	/* From <sys/dlpi.h> */
559 	int		ip_m_type;	/* From <net/if_types.h> */
560 	boolean_t	(*ip_m_v4mapinfo)(uint_t, uint8_t *, uint8_t *,
561 			    uint32_t *, ipaddr_t *);
562 	boolean_t	(*ip_m_v6mapinfo)(uint_t, uint8_t *, uint8_t *,
563 			    uint32_t *, in6_addr_t *);
564 	boolean_t	(*ip_m_v6intfid)(uint_t, uint8_t *, in6_addr_t *);
565 } ip_m_t;
566 
567 /*
568  * The following functions attempt to reduce the link layer dependency
569  * of the IP stack. The current set of link specific operations are:
570  * a. map from IPv4 class D (224.0/4) multicast address range to the link
571  * layer multicast address range.
572  * b. map from IPv6 multicast address range (ff00::/8) to the link
573  * layer multicast address range.
574  * c. derive the default IPv6 interface identifier from the link layer
575  * address.
576  */
577 #define	MEDIA_V4MINFO(ip_m, plen, bphys, maddr, hwxp, v4ptr) \
578 	(((ip_m)->ip_m_v4mapinfo != NULL) && \
579 	(*(ip_m)->ip_m_v4mapinfo)(plen, bphys, maddr, hwxp, v4ptr))
580 #define	MEDIA_V6INTFID(ip_m, plen, phys, v6ptr) \
581 	(((ip_m)->ip_m_v6intfid != NULL) && \
582 	(*(ip_m)->ip_m_v6intfid)(plen, phys, v6ptr))
583 #define	MEDIA_V6MINFO(ip_m, plen, bphys, maddr, hwxp, v6ptr) \
584 	(((ip_m)->ip_m_v6mapinfo != NULL) && \
585 	(*(ip_m)->ip_m_v6mapinfo)(plen, bphys, maddr, hwxp, v6ptr))
586 
587 /* Router entry types */
588 #define	IRE_BROADCAST		0x0001	/* Route entry for broadcast address */
589 #define	IRE_DEFAULT		0x0002	/* Route entry for default gateway */
590 #define	IRE_LOCAL		0x0004	/* Route entry for local address */
591 #define	IRE_LOOPBACK		0x0008	/* Route entry for loopback address */
592 #define	IRE_PREFIX		0x0010	/* Route entry for prefix routes */
593 #define	IRE_CACHE		0x0020	/* Cached Route entry */
594 #define	IRE_IF_NORESOLVER	0x0040	/* Route entry for local interface */
595 					/* net without any address mapping. */
596 #define	IRE_IF_RESOLVER		0x0080	/* Route entry for local interface */
597 					/* net with resolver. */
598 #define	IRE_HOST		0x0100	/* Host route entry */
599 #define	IRE_HOST_REDIRECT	0x0200	/* Host route entry from redirects */
600 
601 /*
602  * IRE_MIPRTUN is only set on the ires in the ip_mrtun_table.
603  * This ire_type must not be set for ftable and ctable routing entries.
604  */
605 #define	IRE_MIPRTUN		0x0400	/* Reverse tunnel route entry */
606 
607 #define	IRE_INTERFACE		(IRE_IF_NORESOLVER | IRE_IF_RESOLVER)
608 #define	IRE_OFFSUBNET		(IRE_DEFAULT | IRE_PREFIX | IRE_HOST | \
609 				IRE_HOST_REDIRECT)
610 #define	IRE_CACHETABLE		(IRE_CACHE | IRE_BROADCAST | IRE_LOCAL | \
611 				IRE_LOOPBACK)
612 #define	IRE_FORWARDTABLE	(IRE_INTERFACE | IRE_OFFSUBNET)
613 
614 /*
615  * If an IRE is marked with IRE_MARK_CONDEMNED, the last walker of
616  * the bucket should delete this IRE from this bucket.
617  */
618 #define	IRE_MARK_CONDEMNED	0x0001
619 /*
620  * If a broadcast IRE is marked with IRE_MARK_NORECV, ip_rput will drop the
621  * broadcast packets received on that interface. This is marked only
622  * on broadcast ires. Employed by IPMP, where we have multiple NICs on the
623  * same subnet receiving the same broadcast packet.
624  */
625 #define	IRE_MARK_NORECV		0x0002
626 /*
627  * IRE_CACHE marked this way won't be returned by ire_cache_lookup. Need
628  * to look specifically using MATCH_IRE_MARK_HIDDEN. Used by IPMP.
629  */
630 #define	IRE_MARK_HIDDEN		0x0004	/* Typically Used by in.mpathd */
631 
632 /*
633  * ire with IRE_MARK_NOADD is  created in ip_newroute_ipif, when outgoing
634  * interface is specified by IP_XMIT_IF socket option. This ire is not
635  * added in IRE_CACHE.  For example, this is used by mipagent to prevent
636  * any entry to be added in the cache table. We do not want to add any
637  * entry for a mobile-node in the routing table for foreign agent originated
638  * packets. Adding routes in cache table in this case, may run the risks of
639  * incorrect routing path in case of private overlapping addresses.
640  */
641 #define	IRE_MARK_NOADD		0x0008	/* Mark not to add ire in cache */
642 
643 /*
644  * IRE marked with IRE_MARK_TEMPORARY means that this IRE has been used
645  * either for forwarding a packet or has not been used for sending
646  * traffic on TCP connections terminated on this system.  In both
647  * cases, this IRE is the first to go when IRE is being cleaned up.
648  */
649 #define	IRE_MARK_TEMPORARY	0x0010
650 
651 /*
652  * IRE marked with IRE_MARK_USESRC_CHECK means that while adding an IRE with
653  * this mark, additional atomic checks need to be performed. For eg: by the
654  * time an IRE_CACHE is created, sent up to ARP and then comes back to IP; the
655  * usesrc grouping could have changed in which case we want to fail adding
656  * the IRE_CACHE entry
657  */
658 #define	IRE_MARK_USESRC_CHECK	0x0020
659 
660 /*
661  * IRE_MARK_PRIVATE_ADDR is used for IP_NEXTHOP. When IP_NEXTHOP is set, the
662  * routing table lookup for the destination is bypassed and the packet is
663  * sent directly to the specified nexthop. The associated IRE_CACHE entries
664  * should be marked with IRE_MARK_PRIVATE_ADDR flag so that they don't show up
665  * in regular ire cache lookups.
666  */
667 #define	IRE_MARK_PRIVATE_ADDR	0x0040
668 
669 /*
670  * When we send an ARP resolution query for the nexthop gateway's ire,
671  * we use esballoc to create the ire_t in the AR_ENTRY_QUERY mblk
672  * chain, and mark its ire_marks with IRE_MARK_UNCACHED. This flag
673  * indicates that information from ARP has not been transferred to a
674  * permanent IRE_CACHE entry. The flag is reset only when the
675  * information is successfully transferred to an ire_cache entry (in
676  * ire_add()). Attempting to free the AR_ENTRY_QUERY mblk chain prior
677  * to ire_add (e.g., from arp, or from ip`ip_wput_nondata) will
678  * require that the resources (incomplete ire_cache and/or nce) must
679  * be cleaned up. The free callback routine (ire_freemblk()) checks
680  * for IRE_MARK_UNCACHED to see if any resources that are pinned down
681  * will need to be cleaned up or not.
682  */
683 
684 #define	IRE_MARK_UNCACHED	0x0080
685 
686 /* Flags with ire_expire routine */
687 #define	FLUSH_ARP_TIME		0x0001	/* ARP info potentially stale timer */
688 #define	FLUSH_REDIRECT_TIME	0x0002	/* Redirects potentially stale */
689 #define	FLUSH_MTU_TIME		0x0004	/* Include path MTU per RFC 1191 */
690 
691 /* Arguments to ire_flush_cache() */
692 #define	IRE_FLUSH_DELETE	0
693 #define	IRE_FLUSH_ADD		1
694 
695 /*
696  * Open/close synchronization flags.
697  * These are kept in a separate field in the conn and the synchronization
698  * depends on the atomic 32 bit access to that field.
699  */
700 #define	CONN_CLOSING		0x01	/* ip_close waiting for ip_wsrv */
701 #define	CONN_IPSEC_LOAD_WAIT	0x02	/* waiting for load */
702 #define	CONN_CONDEMNED		0x04	/* conn is closing, no more refs */
703 #define	CONN_INCIPIENT		0x08	/* conn not yet visible, no refs */
704 #define	CONN_QUIESCED		0x10	/* conn is now quiescent */
705 
706 /*
707  * Parameter to ip_output giving the identity of the caller.
708  * IP_WSRV means the packet was enqueued in the STREAMS queue
709  * due to flow control and is now being reprocessed in the context of
710  * the STREAMS service procedure, consequent to flow control relief.
711  * IRE_SEND means the packet is being reprocessed consequent to an
712  * ire cache creation and addition and this may or may not be happening
713  * in the service procedure context. Anything other than the above 2
714  * cases is identified as IP_WPUT. Most commonly this is the case of
715  * packets coming down from the application.
716  */
717 #ifdef _KERNEL
718 #define	IP_WSRV			1	/* Called from ip_wsrv */
719 #define	IP_WPUT			2	/* Called from ip_wput */
720 #define	IRE_SEND		3	/* Called from ire_send */
721 
722 /*
723  * Extra structures need for per-src-addr filtering (IGMPv3/MLDv2)
724  */
725 #define	MAX_FILTER_SIZE	64
726 
727 typedef struct slist_s {
728 	int		sl_numsrc;
729 	in6_addr_t	sl_addr[MAX_FILTER_SIZE];
730 } slist_t;
731 
732 /*
733  * Following struct is used to maintain retransmission state for
734  * a multicast group.  One rtx_state_t struct is an in-line field
735  * of the ilm_t struct; the slist_ts in the rtx_state_t struct are
736  * alloc'd as needed.
737  */
738 typedef struct rtx_state_s {
739 	uint_t		rtx_timer;	/* retrans timer */
740 	int		rtx_cnt;	/* retrans count */
741 	int		rtx_fmode_cnt;	/* retrans count for fmode change */
742 	slist_t		*rtx_allow;
743 	slist_t		*rtx_block;
744 } rtx_state_t;
745 
746 /*
747  * Used to construct list of multicast address records that will be
748  * sent in a single listener report.
749  */
750 typedef struct mrec_s {
751 	struct mrec_s	*mrec_next;
752 	uint8_t		mrec_type;
753 	uint8_t		mrec_auxlen;	/* currently unused */
754 	in6_addr_t	mrec_group;
755 	slist_t		mrec_srcs;
756 } mrec_t;
757 
758 /* Group membership list per upper conn */
759 /*
760  * XXX add ilg info for ifaddr/ifindex.
761  * XXX can we make ilg survive an ifconfig unplumb + plumb
762  * by setting the ipif/ill to NULL and recover that later?
763  *
764  * ilg_ipif is used by IPv4 as multicast groups are joined using an interface
765  * address (ipif).
766  * ilg_ill is used by IPv6 as multicast groups are joined using an interface
767  * index (phyint->phyint_ifindex).
768  * ilg_ill is NULL for IPv4 and ilg_ipif is NULL for IPv6.
769  *
770  * ilg records the state of multicast memberships of a socket end point.
771  * ilm records the state of multicast memberships with the driver and is
772  * maintained per interface.
773  *
774  * Notes :
775  *
776  * 1) There is no direct link between a given ilg and ilm. If the
777  *    application has joined a group G with ifindex I, we will have
778  *    an ilg with ilg_v6group and ilg_ill. There will be a corresponding
779  *    ilm with ilm_ill/ilm_v6addr recording the multicast membership.
780  *    To delete the membership,
781  *
782  *		a) Search for ilg matching on G and I with ilg_v6group
783  *		   and ilg_ill. Delete ilg_ill.
784  *		b) Search the corresponding ilm matching on G and I with
785  *		   ilm_v6addr and ilm_ill. Delete ilm.
786  *
787  *    In IPv4, the only difference is, we look using ipifs instead of
788  *    ills.
789  *
790  * 2) With IP multipathing, we want to keep receiving even after the
791  *    interface has failed. We do this by moving multicast memberships
792  *    to a new_ill within the group. This is acheived by sending
793  *    DL_DISABMULTI_REQS on ilg_ill/ilm_ill and sending DL_ENABMULTIREQS
794  *    on the new_ill and changing ilg_ill/ilm_ill to new_ill. But, we
795  *    need to be able to delete memberships which will still come down
796  *    with the ifindex of the old ill which is what the application
797  *    knows of. Thus we store the ilm_/ilg_orig_ifindex to keep track
798  *    of where we joined initially so that we can lookup even after we
799  *    moved the membership. It is also used for moving back the membership
800  *    when the old ill has been repaired. This is done by looking up for
801  *    ilms with ilm_orig_ifindex matching on the old ill's ifindex. Only
802  *    ilms actually move from old ill to new ill. ilgs don't move (just
803  *    the ilg_ill is changed when it moves) as it just records the state
804  *    of the application that has joined a group G where as ilm records
805  *    the state joined with the driver. Thus when we send DL_XXXMULTI_REQs
806  *    we also need to keep the ilm in the right ill.
807  *
808  *    In IPv4, as ipifs move from old ill to new_ill, ilgs and ilms move
809  *    implicitly as we use only ipifs in IPv4. Thus, one can always lookup
810  *    a given ilm/ilg even after it fails without the support of
811  *    orig_ifindex. We move ilms still to record the driver state as
812  *    mentioned above.
813  */
814 
815 /*
816  * The ilg_t and ilm_t members are protected by ipsq. They can be changed only
817  * by a thread executing in the ipsq. In other words add/delete of a
818  * multicast group has to execute in the ipsq.
819  */
820 #define	ILG_DELETED	0x1		/* ilg_flags */
821 typedef struct ilg_s {
822 	in6_addr_t	ilg_v6group;
823 	struct ipif_s	*ilg_ipif;	/* Logical interface we are member on */
824 	struct ill_s	*ilg_ill;	/* Used by IPv6 */
825 	int		ilg_orig_ifindex; /* Interface originally joined on */
826 	uint_t		ilg_flags;
827 	mcast_record_t	ilg_fmode;	/* MODE_IS_INCLUDE/MODE_IS_EXCLUDE */
828 	slist_t		*ilg_filter;
829 } ilg_t;
830 
831 
832 /*
833  * Multicast address list entry for lower ill.
834  * ilm_ipif is used by IPv4 as multicast groups are joined using ipif.
835  * ilm_ill is used by IPv6 as multicast groups are joined using ill.
836  * ilm_ill is NULL for IPv4 and ilm_ipif is NULL for IPv6.
837  */
838 #define	ILM_DELETED	0x1		/* ilm_flags */
839 typedef struct ilm_s {
840 	in6_addr_t	ilm_v6addr;
841 	int		ilm_refcnt;
842 	uint_t		ilm_timer;	/* IGMP/MLD query resp timer, in msec */
843 	struct ipif_s	*ilm_ipif;	/* Back pointer to ipif for IPv4 */
844 	struct ilm_s	*ilm_next;	/* Linked list for each ill */
845 	uint_t		ilm_state;	/* state of the membership */
846 	struct ill_s	*ilm_ill;	/* Back pointer to ill for IPv6 */
847 	int		ilm_orig_ifindex;  /* V6_MULTICAST_IF/ilm_ipif index */
848 	uint_t		ilm_flags;
849 	boolean_t	ilm_is_new;	/* new ilm */
850 	boolean_t	ilm_notify_driver; /* Need to notify the driver */
851 	zoneid_t	ilm_zoneid;
852 	int		ilm_no_ilg_cnt;	/* number of joins w/ no ilg */
853 	mcast_record_t	ilm_fmode;	/* MODE_IS_INCLUDE/MODE_IS_EXCLUDE */
854 	slist_t		*ilm_filter;	/* source filter list */
855 	slist_t		*ilm_pendsrcs;	/* relevant src addrs for pending req */
856 	rtx_state_t	ilm_rtx;	/* SCR retransmission state */
857 } ilm_t;
858 
859 #define	ilm_addr	V4_PART_OF_V6(ilm_v6addr)
860 
861 /*
862  * ilm_walker_cleanup needs to execute when the ilm_walker_cnt goes down to
863  * zero. In addition it needs to block new walkers while it is unlinking ilm's
864  * from the list. Thus simple atomics for the ill_ilm_walker_cnt don't suffice.
865  */
866 #define	ILM_WALKER_HOLD(ill)    {               \
867 	mutex_enter(&(ill)->ill_lock);          \
868 	ill->ill_ilm_walker_cnt++;              \
869 	mutex_exit(&(ill)->ill_lock);           \
870 }
871 
872 #define	ILM_WALKER_RELE(ill)	{ 		\
873 	mutex_enter(&(ill)->ill_lock);		\
874 	(ill)->ill_ilm_walker_cnt--;		\
875 	if ((ill)->ill_ilm_walker_cnt == 0 && (ill)->ill_ilm_cleanup_reqd) \
876 		ilm_walker_cleanup(ill);	\
877 	mutex_exit(&(ill)->ill_lock);		\
878 }
879 
880 /*
881  * Soft reference to an IPsec SA.
882  *
883  * On relative terms, conn's can be persistant (living as long as the
884  * processes which create them), while SA's are ephemeral (dying when
885  * they hit their time-based or byte-based lifetimes).
886  *
887  * We could hold a hard reference to an SA from an ipsec_latch_t,
888  * but this would cause expired SA's to linger for a potentially
889  * unbounded time.
890  *
891  * Instead, we remember the hash bucket number and bucket generation
892  * in addition to the pointer.  The bucket generation is incremented on
893  * each deletion.
894  */
895 typedef struct ipsa_ref_s
896 {
897 	struct ipsa_s	*ipsr_sa;
898 	struct isaf_s	*ipsr_bucket;
899 	uint64_t	ipsr_gen;
900 } ipsa_ref_t;
901 
902 /*
903  * IPsec "latching" state.
904  *
905  * In the presence of IPsec policy, fully-bound conn's bind a connection
906  * to more than just the 5-tuple, but also a specific IPsec action and
907  * identity-pair.
908  *
909  * As an optimization, we also cache soft references to IPsec SA's
910  * here so that we can fast-path around most of the work needed for
911  * outbound IPsec SA selection.
912  *
913  * Were it not for TCP's detached connections, this state would be
914  * in-line in conn_t; instead, this is in a separate structure so it
915  * can be handed off to TCP when a connection is detached.
916  */
917 typedef struct ipsec_latch_s
918 {
919 	kmutex_t	ipl_lock;
920 	uint32_t	ipl_refcnt;
921 
922 	uint64_t	ipl_unique;
923 	struct ipsec_policy_s	*ipl_in_policy; /* latched policy (in) */
924 	struct ipsec_policy_s	*ipl_out_policy; /* latched policy (out) */
925 	struct ipsec_action_s	*ipl_in_action;	/* latched action (in) */
926 	struct ipsec_action_s	*ipl_out_action; /* latched action (out) */
927 	cred_t		*ipl_local_id;
928 	struct ipsid_s	*ipl_local_cid;
929 	struct ipsid_s	*ipl_remote_cid;
930 	unsigned int
931 			ipl_out_action_latched : 1,
932 			ipl_in_action_latched : 1,
933 			ipl_out_policy_latched : 1,
934 			ipl_in_policy_latched : 1,
935 
936 			ipl_ids_latched : 1,
937 
938 			ipl_pad_to_bit_31 : 27;
939 
940 	ipsa_ref_t	ipl_ref[2]; /* 0: ESP, 1: AH */
941 
942 } ipsec_latch_t;
943 
944 #define	IPLATCH_REFHOLD(ipl) { \
945 	atomic_add_32(&(ipl)->ipl_refcnt, 1);		\
946 	ASSERT((ipl)->ipl_refcnt != 0);			\
947 }
948 
949 #define	IPLATCH_REFRELE(ipl) {					\
950 	ASSERT((ipl)->ipl_refcnt != 0);				\
951 	membar_exit();						\
952 	if (atomic_add_32_nv(&(ipl)->ipl_refcnt, -1) == 0)	\
953 		iplatch_free(ipl);				\
954 }
955 
956 /*
957  * peer identity structure.
958  */
959 
960 typedef struct conn_s conn_t;
961 
962 /*
963  * The old IP client structure "ipc_t" is gone. All the data is stored in the
964  * connection structure "conn_t" now. The mapping of old and new fields looks
965  * like this:
966  *
967  * ipc_ulp			conn_ulp
968  * ipc_rq			conn_rq
969  * ipc_wq			conn_wq
970  *
971  * ipc_laddr			conn_src
972  * ipc_faddr			conn_rem
973  * ipc_v6laddr			conn_srcv6
974  * ipc_v6faddr			conn_remv6
975  *
976  * ipc_lport			conn_lport
977  * ipc_fport			conn_fport
978  * ipc_ports			conn_ports
979  *
980  * ipc_policy			conn_policy
981  * ipc_latch			conn_latch
982  *
983  * ipc_irc_lock			conn_lock
984  * ipc_ire_cache		conn_ire_cache
985  *
986  * ipc_state_flags		conn_state_flags
987  * ipc_outgoing_ill		conn_outgoing_ill
988  *
989  * ipc_dontroute 		conn_dontroute
990  * ipc_loopback 		conn_loopback
991  * ipc_broadcast		conn_broadcast
992  * ipc_reuseaddr		conn_reuseaddr
993  *
994  * ipc_multicast_loop		conn_multicast_loop
995  * ipc_multi_router		conn_multi_router
996  * ipc_priv_stream 		conn_priv_stream
997  * ipc_draining 		conn_draining
998  *
999  * ipc_did_putbq		conn_did_putbq
1000  * ipc_unspec_src		conn_unspec_src
1001  * ipc_policy_cached		conn_policy_cached
1002  *
1003  * ipc_in_enforce_policy 	conn_in_enforce_policy
1004  * ipc_out_enforce_policy 	conn_out_enforce_policy
1005  * ipc_af_isv6			conn_af_isv6
1006  * ipc_pkt_isv6			conn_pkt_isv6
1007  *
1008  * ipc_ipv6_recvpktinfo		conn_ipv6_recvpktinfo
1009  *
1010  * ipc_ipv6_recvhoplimit	conn_ipv6_recvhoplimit
1011  * ipc_ipv6_recvhopopts		conn_ipv6_recvhopopts
1012  * ipc_ipv6_recvdstopts		conn_ipv6_recvdstopts
1013  *
1014  * ipc_ipv6_recvrthdr 		conn_ipv6_recvrthdr
1015  * ipc_ipv6_recvrtdstopts	conn_ipv6_recvrtdstopts
1016  * ipc_fully_bound		conn_fully_bound
1017  *
1018  * ipc_recvif			conn_recvif
1019  *
1020  * ipc_recvslla 		conn_recvslla
1021  * ipc_acking_unbind 		conn_acking_unbind
1022  * ipc_pad_to_bit_31 		conn_pad_to_bit_31
1023  *
1024  * ipc_xmit_if_ill		conn_xmit_if_ill
1025  * ipc_nofailover_ill		conn_nofailover_ill
1026  *
1027  * ipc_proto			conn_proto
1028  * ipc_incoming_ill		conn_incoming_ill
1029  * ipc_outgoing_pill		conn_outgoing_pill
1030  * ipc_pending_ill		conn_pending_ill
1031  * ipc_unbind_mp		conn_unbind_mp
1032  * ipc_ilg			conn_ilg
1033  * ipc_ilg_allocated		conn_ilg_allocated
1034  * ipc_ilg_inuse		conn_ilg_inuse
1035  * ipc_ilg_walker_cnt		conn_ilg_walker_cnt
1036  * ipc_refcv			conn_refcv
1037  * ipc_multicast_ipif		conn_multicast_ipif
1038  * ipc_multicast_ill		conn_multicast_ill
1039  * ipc_orig_bound_ifindex	conn_orig_bound_ifindex
1040  * ipc_orig_multicast_ifindex	conn_orig_multicast_ifindex
1041  * ipc_orig_xmit_ifindex	conn_orig_xmit_ifindex
1042  * ipc_drain_next		conn_drain_next
1043  * ipc_drain_prev		conn_drain_prev
1044  * ipc_idl			conn_idl
1045  */
1046 
1047 /*
1048  * This is used to match an inbound/outbound datagram with
1049  * policy.
1050  */
1051 
1052 typedef	struct ipsec_selector {
1053 	in6_addr_t	ips_local_addr_v6;
1054 	in6_addr_t	ips_remote_addr_v6;
1055 	uint16_t	ips_local_port;
1056 	uint16_t	ips_remote_port;
1057 	uint8_t		ips_icmp_type;
1058 	uint8_t		ips_icmp_code;
1059 	uint8_t		ips_protocol;
1060 	uint8_t		ips_isv4 : 1,
1061 			ips_is_icmp_inv_acq: 1;
1062 } ipsec_selector_t;
1063 
1064 /*
1065  * Note that we put v4 addresses in the *first* 32-bit word of the
1066  * selector rather than the last to simplify the prefix match/mask code
1067  * in spd.c
1068  */
1069 #define	ips_local_addr_v4 ips_local_addr_v6.s6_addr32[0]
1070 #define	ips_remote_addr_v4 ips_remote_addr_v6.s6_addr32[0]
1071 
1072 /* Values used in IP by IPSEC Code */
1073 #define		IPSEC_OUTBOUND		B_TRUE
1074 #define		IPSEC_INBOUND		B_FALSE
1075 
1076 /*
1077  * There are two variants in policy failures. The packet may come in
1078  * secure when not needed (IPSEC_POLICY_???_NOT_NEEDED) or it may not
1079  * have the desired level of protection (IPSEC_POLICY_MISMATCH).
1080  */
1081 #define	IPSEC_POLICY_NOT_NEEDED		0
1082 #define	IPSEC_POLICY_MISMATCH		1
1083 #define	IPSEC_POLICY_AUTH_NOT_NEEDED	2
1084 #define	IPSEC_POLICY_ENCR_NOT_NEEDED	3
1085 #define	IPSEC_POLICY_SE_NOT_NEEDED	4
1086 #define	IPSEC_POLICY_MAX		5	/* Always max + 1. */
1087 
1088 /*
1089  * Folowing macro is used whenever the code does not know whether there
1090  * is a M_CTL present in the front and it needs to examine the actual mp
1091  * i.e the IP header. As a M_CTL message could be in the front, this
1092  * extracts the packet into mp and the M_CTL mp into first_mp. If M_CTL
1093  * mp is not present, both first_mp and mp point to the same message.
1094  */
1095 #define	EXTRACT_PKT_MP(mp, first_mp, mctl_present)	\
1096 	(first_mp) = (mp);				\
1097 	if ((mp)->b_datap->db_type == M_CTL) {		\
1098 		(mp) = (mp)->b_cont;			\
1099 		(mctl_present) = B_TRUE;		\
1100 	} else {					\
1101 		(mctl_present) = B_FALSE;		\
1102 	}
1103 
1104 /*
1105  * Check with IPSEC inbound policy if
1106  *
1107  * 1) per-socket policy is present - indicated by conn_in_enforce_policy.
1108  * 2) Or if we have not cached policy on the conn and the global policy is
1109  *    non-empty.
1110  */
1111 #define	CONN_INBOUND_POLICY_PRESENT(connp)	\
1112 	((connp)->conn_in_enforce_policy ||	\
1113 	(!((connp)->conn_policy_cached) &&	\
1114 	ipsec_inbound_v4_policy_present))
1115 
1116 #define	CONN_INBOUND_POLICY_PRESENT_V6(connp)	\
1117 	((connp)->conn_in_enforce_policy ||	\
1118 	(!(connp)->conn_policy_cached &&	\
1119 	ipsec_inbound_v6_policy_present))
1120 
1121 #define	CONN_OUTBOUND_POLICY_PRESENT(connp)	\
1122 	((connp)->conn_out_enforce_policy ||	\
1123 	(!((connp)->conn_policy_cached) &&	\
1124 	ipsec_outbound_v4_policy_present))
1125 
1126 #define	CONN_OUTBOUND_POLICY_PRESENT_V6(connp)	\
1127 	((connp)->conn_out_enforce_policy ||	\
1128 	(!(connp)->conn_policy_cached &&	\
1129 	ipsec_outbound_v6_policy_present))
1130 
1131 /*
1132  * Information cached in IRE for upper layer protocol (ULP).
1133  *
1134  * Notice that ire_max_frag is not included in the iulp_t structure, which
1135  * it may seem that it should.  But ire_max_frag cannot really be cached.  It
1136  * is fixed for each interface.  For MTU found by PMTUd, we may want to cache
1137  * it.  But currently, we do not do that.
1138  */
1139 typedef struct iulp_s {
1140 	boolean_t	iulp_set;	/* Is any metric set? */
1141 	uint32_t	iulp_ssthresh;	/* Slow start threshold (TCP). */
1142 	clock_t		iulp_rtt;	/* Guestimate in millisecs. */
1143 	clock_t		iulp_rtt_sd;	/* Cached value of RTT variance. */
1144 	uint32_t	iulp_spipe;	/* Send pipe size. */
1145 	uint32_t	iulp_rpipe;	/* Receive pipe size. */
1146 	uint32_t	iulp_rtomax;	/* Max round trip timeout. */
1147 	uint32_t	iulp_sack;	/* Use SACK option (TCP)? */
1148 	uint32_t
1149 		iulp_tstamp_ok : 1,	/* Use timestamp option (TCP)? */
1150 		iulp_wscale_ok : 1,	/* Use window scale option (TCP)? */
1151 		iulp_ecn_ok : 1,	/* Enable ECN (for TCP)? */
1152 		iulp_pmtud_ok : 1,	/* Enable PMTUd? */
1153 
1154 		iulp_not_used : 28;
1155 } iulp_t;
1156 
1157 /* Zero iulp_t. */
1158 extern const iulp_t ire_uinfo_null;
1159 
1160 /*
1161  * The conn drain list structure.
1162  * The list is protected by idl_lock. Each conn_t inserted in the list
1163  * points back at this idl_t using conn_idl. IP primes the draining of the
1164  * conns queued in these lists, by qenabling the 1st conn of each list. This
1165  * occurs when STREAMS backenables ip_wsrv on the IP module. Each conn instance
1166  * of ip_wsrv successively qenables the next conn in the list.
1167  * idl_lock protects all other members of idl_t and conn_drain_next
1168  * and conn_drain_prev of conn_t. The conn_lock protects IPCF_DRAIN_DISABLED
1169  * flag of the conn_t and conn_idl.
1170  */
1171 typedef struct idl_s {
1172 	conn_t		*idl_conn;		/* Head of drain list */
1173 	kmutex_t	idl_lock;		/* Lock for this list */
1174 	conn_t		*idl_conn_draining;	/* conn that is draining */
1175 	uint32_t
1176 		idl_repeat : 1,			/* Last conn must re-enable */
1177 						/* drain list again */
1178 		idl_unused : 31;
1179 } idl_t;
1180 
1181 #define	CONN_DRAIN_LIST_LOCK(connp)	(&((connp)->conn_idl->idl_lock))
1182 /*
1183  * Interface route structure which holds the necessary information to recreate
1184  * routes that are tied to an interface (namely where ire_ipif != NULL).
1185  * These routes which were initially created via a routing socket or via the
1186  * SIOCADDRT ioctl may be gateway routes (RTF_GATEWAY being set) or may be
1187  * traditional interface routes.  When an interface comes back up after being
1188  * marked down, this information will be used to recreate the routes.  These
1189  * are part of an mblk_t chain that hangs off of the IPIF (ipif_saved_ire_mp).
1190  */
1191 typedef struct ifrt_s {
1192 	ushort_t	ifrt_type;		/* Type of IRE */
1193 	in6_addr_t	ifrt_v6addr;		/* Address IRE represents. */
1194 	in6_addr_t	ifrt_v6gateway_addr;	/* Gateway if IRE_OFFSUBNET */
1195 	in6_addr_t	ifrt_v6src_addr;	/* Src addr if RTF_SETSRC */
1196 	in6_addr_t	ifrt_v6mask;		/* Mask for matching IRE. */
1197 	uint32_t	ifrt_flags;		/* flags related to route */
1198 	uint_t		ifrt_max_frag;		/* MTU (next hop or path). */
1199 	iulp_t		ifrt_iulp_info;		/* Cached IRE ULP info. */
1200 } ifrt_t;
1201 
1202 #define	ifrt_addr		V4_PART_OF_V6(ifrt_v6addr)
1203 #define	ifrt_gateway_addr	V4_PART_OF_V6(ifrt_v6gateway_addr)
1204 #define	ifrt_src_addr		V4_PART_OF_V6(ifrt_v6src_addr)
1205 #define	ifrt_mask		V4_PART_OF_V6(ifrt_v6mask)
1206 
1207 /* Number of IP addresses that can be hosted on a physical interface */
1208 #define	MAX_ADDRS_PER_IF	8192
1209 /*
1210  * Number of Source addresses to be considered for source address
1211  * selection. Used by ipif_select_source[_v6].
1212  */
1213 #define	MAX_IPIF_SELECT_SOURCE	50
1214 
1215 #ifdef IP_DEBUG
1216 /*
1217  * Tracing refholds and refreleases for debugging. Existing tracing mechanisms
1218  * do not allow the granularity need to trace refrences to ipif/ill/ire's. This
1219  * mechanism should be revisited once dtrace is available.
1220  */
1221 #define	IP_STACK_DEPTH	15
1222 typedef struct tr_buf_s {
1223 	int	tr_depth;
1224 	pc_t	tr_stack[IP_STACK_DEPTH];
1225 } tr_buf_t;
1226 
1227 typedef struct th_trace_s {
1228 	struct	th_trace_s *th_next;
1229 	struct	th_trace_s **th_prev;
1230 	kthread_t	*th_id;
1231 	int	th_refcnt;
1232 	uint_t	th_trace_lastref;
1233 #define	TR_BUF_MAX	38
1234 	tr_buf_t th_trbuf[TR_BUF_MAX];
1235 } th_trace_t;
1236 #endif
1237 
1238 /* The following are ipif_state_flags */
1239 #define	IPIF_CONDEMNED		0x1	/* The ipif is being removed */
1240 #define	IPIF_CHANGING		0x2	/* A critcal ipif field is changing */
1241 #define	IPIF_MOVING		0x8	/* The ipif is being moved */
1242 #define	IPIF_SET_LINKLOCAL	0x10	/* transient flag during bringup */
1243 #define	IPIF_ZERO_SOURCE	0x20	/* transient flag during bringup */
1244 
1245 /* IP interface structure, one per local address */
1246 typedef struct ipif_s {
1247 	struct	ipif_s	*ipif_next;
1248 	struct	ill_s	*ipif_ill;	/* Back pointer to our ill */
1249 	int	ipif_id;		/* Logical unit number */
1250 	uint_t	ipif_mtu;		/* Starts at ipif_ill->ill_max_frag */
1251 	uint_t	ipif_saved_mtu;		/* Save of mtu during ipif_move() */
1252 	in6_addr_t ipif_v6lcl_addr;	/* Local IP address for this if. */
1253 	in6_addr_t ipif_v6src_addr;	/* Source IP address for this if. */
1254 	in6_addr_t ipif_v6subnet;	/* Subnet prefix for this if. */
1255 	in6_addr_t ipif_v6net_mask;	/* Net mask for this interface. */
1256 	in6_addr_t ipif_v6brd_addr;	/* Broadcast addr for this interface. */
1257 	in6_addr_t ipif_v6pp_dst_addr;	/* Point-to-point dest address. */
1258 	uint64_t ipif_flags;		/* Interface flags. */
1259 	uint_t	ipif_metric;		/* BSD if metric, for compatibility. */
1260 	uint_t	ipif_ire_type;		/* IRE_LOCAL or IRE_LOOPBACK */
1261 	mblk_t	*ipif_arp_del_mp;	/* Allocated at time arp comes up, to */
1262 					/* prevent awkward out of mem */
1263 					/* condition later */
1264 	mblk_t	*ipif_saved_ire_mp;	/* Allocated for each extra */
1265 					/* IRE_IF_NORESOLVER/IRE_IF_RESOLVER */
1266 					/* on this interface so that they */
1267 					/* can survive ifconfig down. */
1268 	kmutex_t ipif_saved_ire_lock;	/* Protects ipif_saved_ire_mp */
1269 
1270 	mrec_t	*ipif_igmp_rpt;		/* List of group memberships which */
1271 					/* will be reported on.  Used when */
1272 					/* handling an igmp timeout.	   */
1273 
1274 	/*
1275 	 * The packet counts in the ipif contain the sum of the
1276 	 * packet counts in dead IREs that were affiliated with
1277 	 * this ipif.
1278 	 */
1279 	uint_t	ipif_fo_pkt_count;	/* Forwarded thru our dead IREs */
1280 	uint_t	ipif_ib_pkt_count;	/* Inbound packets for our dead IREs */
1281 	uint_t	ipif_ob_pkt_count;	/* Outbound packets to our dead IREs */
1282 	/* Exclusive bit fields, protected by ipsq_t */
1283 	unsigned int
1284 		ipif_multicast_up : 1,	/* We have joined the allhosts group */
1285 		ipif_solmcast_up : 1,	/* We joined solicited node mcast */
1286 		ipif_replace_zero : 1,	/* Replacement for zero */
1287 		ipif_was_up : 1,	/* ipif was up before */
1288 
1289 		ipif_addr_ready : 1,	/* DAD is done */
1290 		ipif_was_dup : 1,	/* DAD had failed */
1291 		ipif_pad_to_31 : 26;
1292 
1293 	int	ipif_orig_ifindex;	/* ifindex before SLIFFAILOVER */
1294 	uint_t	ipif_seqid;		/* unique index across all ills */
1295 	uint_t	ipif_orig_ipifid;	/* ipif_id before SLIFFAILOVER */
1296 	uint_t	ipif_state_flags;	/* See IPIF_* flag defs above */
1297 	uint_t	ipif_refcnt;		/* active consistent reader cnt */
1298 	uint_t	ipif_ire_cnt;		/* Number of ire's referencing ipif */
1299 	uint_t	ipif_saved_ire_cnt;
1300 	zoneid_t
1301 		ipif_zoneid;		/* zone ID number */
1302 	timeout_id_t ipif_recovery_id;	/* Timer for DAD recovery */
1303 #ifdef ILL_DEBUG
1304 #define	IP_TR_HASH_MAX	64
1305 	th_trace_t *ipif_trace[IP_TR_HASH_MAX];
1306 	boolean_t	ipif_trace_disable;	/* True when alloc fails */
1307 #endif
1308 } ipif_t;
1309 
1310 /*
1311  * The following table lists the protection levels of the various members
1312  * of the ipif_t. The following notation is used.
1313  *
1314  * Write once - Written to only once at the time of bringing up
1315  * the interface and can be safely read after the bringup without any lock.
1316  *
1317  * ipsq - Need to execute in the ipsq to perform the indicated access.
1318  *
1319  * ill_lock - Need to hold this mutex to perform the indicated access.
1320  *
1321  * ill_g_lock - Need to hold this rw lock as reader/writer for read access or
1322  * write access respectively.
1323  *
1324  * down ill - Written to only when the ill is down (i.e all ipifs are down)
1325  * up ill - Read only when the ill is up (i.e. at least 1 ipif is up)
1326  *
1327  *		 Table of ipif_t members and their protection
1328  *
1329  * ipif_next		ill_g_lock		ill_g_lock
1330  * ipif_ill		ipsq + down ipif		write once
1331  * ipif_id		ipsq + down ipif		write once
1332  * ipif_mtu		ipsq
1333  * ipif_v6lcl_addr	ipsq + down ipif		up ipif
1334  * ipif_v6src_addr	ipsq + down ipif		up ipif
1335  * ipif_v6subnet	ipsq + down ipif		up ipif
1336  * ipif_v6net_mask	ipsq + down ipif		up ipif
1337  *
1338  * ipif_v6brd_addr
1339  * ipif_v6pp_dst_addr
1340  * ipif_flags		ill_lock		ill_lock
1341  * ipif_metric
1342  * ipif_ire_type	ipsq + down ill		up ill
1343  *
1344  * ipif_arp_del_mp	ipsq			ipsq
1345  * ipif_saved_ire_mp	ipif_saved_ire_lock	ipif_saved_ire_lock
1346  * ipif_igmp_rpt	ipsq			ipsq
1347  *
1348  * ipif_fo_pkt_count	Approx
1349  * ipif_ib_pkt_count	Approx
1350  * ipif_ob_pkt_count	Approx
1351  *
1352  * bit fields		ill_lock		ill_lock
1353  *
1354  * ipif_orig_ifindex	ipsq			None
1355  * ipif_orig_ipifid	ipsq			None
1356  * ipif_seqid		ipsq			Write once
1357  *
1358  * ipif_state_flags	ill_lock		ill_lock
1359  * ipif_refcnt		ill_lock		ill_lock
1360  * ipif_ire_cnt		ill_lock		ill_lock
1361  * ipif_saved_ire_cnt
1362  */
1363 
1364 #define	IP_TR_HASH(tid)	((((uintptr_t)tid) >> 6) & (IP_TR_HASH_MAX - 1))
1365 
1366 #ifdef ILL_DEBUG
1367 #define	IPIF_TRACE_REF(ipif)	ipif_trace_ref(ipif)
1368 #define	ILL_TRACE_REF(ill)	ill_trace_ref(ill)
1369 #define	IPIF_UNTRACE_REF(ipif)	ipif_untrace_ref(ipif)
1370 #define	ILL_UNTRACE_REF(ill)	ill_untrace_ref(ill)
1371 #define	ILL_TRACE_CLEANUP(ill)	ill_trace_cleanup(ill)
1372 #define	IPIF_TRACE_CLEANUP(ipif)	ipif_trace_cleanup(ipif)
1373 #else
1374 #define	IPIF_TRACE_REF(ipif)
1375 #define	ILL_TRACE_REF(ill)
1376 #define	IPIF_UNTRACE_REF(ipif)
1377 #define	ILL_UNTRACE_REF(ill)
1378 #define	ILL_TRACE_CLEANUP(ill)
1379 #define	IPIF_TRACE_CLEANUP(ipif)
1380 #endif
1381 
1382 /* IPv4 compatability macros */
1383 #define	ipif_lcl_addr		V4_PART_OF_V6(ipif_v6lcl_addr)
1384 #define	ipif_src_addr		V4_PART_OF_V6(ipif_v6src_addr)
1385 #define	ipif_subnet		V4_PART_OF_V6(ipif_v6subnet)
1386 #define	ipif_net_mask		V4_PART_OF_V6(ipif_v6net_mask)
1387 #define	ipif_brd_addr		V4_PART_OF_V6(ipif_v6brd_addr)
1388 #define	ipif_pp_dst_addr	V4_PART_OF_V6(ipif_v6pp_dst_addr)
1389 
1390 /* Macros for easy backreferences to the ill. */
1391 #define	ipif_wq			ipif_ill->ill_wq
1392 #define	ipif_rq			ipif_ill->ill_rq
1393 #define	ipif_net_type		ipif_ill->ill_net_type
1394 #define	ipif_resolver_mp	ipif_ill->ill_resolver_mp
1395 #define	ipif_ipif_up_count	ipif_ill->ill_ipif_up_count
1396 #define	ipif_bcast_mp		ipif_ill->ill_bcast_mp
1397 #define	ipif_type		ipif_ill->ill_type
1398 #define	ipif_isv6		ipif_ill->ill_isv6
1399 
1400 #define	SIOCLIFADDR_NDX 112	/* ndx of SIOCLIFADDR in the ndx ioctl table */
1401 
1402 /*
1403  * mode value for ip_ioctl_finish for finishing an ioctl
1404  */
1405 #define	CONN_CLOSE	1		/* No mi_copy */
1406 #define	COPYOUT		2		/* do an mi_copyout if needed */
1407 #define	NO_COPYOUT	3		/* do an mi_copy_done */
1408 
1409 /*
1410  * The IP-MT design revolves around the serialization object ipsq_t.
1411  * It is associated with an IPMP group. If IPMP is not enabled, there is
1412  * 1 ipsq_t per phyint. Eg. an ipsq_t would cover both hme0's IPv4 stream
1413  *
1414  * ipsq_lock protects
1415  *	ipsq_reentry_cnt, ipsq_writer, ipsq_xopq_mphead, ipsq_xopq_mptail,
1416  *	ipsq_mphead, ipsq_mptail, ipsq_split
1417  *
1418  *	ipsq_pending_ipif, ipsq_current_ipif, ipsq_pending_mp, ipsq_flags,
1419  *	ipsq_waitfor
1420  *
1421  * The fields in the last line above below are set mostly by a writer thread
1422  * But there is an exception in the last call to ipif_ill_refrele_tail which
1423  * could also race with a conn close which could be cleaning up the
1424  * fields. So we choose to protect using ipsq_lock instead of depending on
1425  * the property of the writer.
1426  * ill_g_lock protects
1427  *	ipsq_refs, ipsq_phyint_list
1428  */
1429 typedef struct ipsq_s {
1430 	kmutex_t ipsq_lock;
1431 	int	ipsq_reentry_cnt;
1432 	kthread_t	*ipsq_writer;	/* current owner (thread id) */
1433 	int	ipsq_flags;
1434 	mblk_t	*ipsq_xopq_mphead;	/* list of excl ops mostly ioctls */
1435 	mblk_t	*ipsq_xopq_mptail;
1436 	mblk_t	*ipsq_mphead;		/* msgs on ipsq linked thru b_next */
1437 	mblk_t	*ipsq_mptail;		/* msgs on ipsq linked thru b_next */
1438 	ipif_t	*ipsq_pending_ipif;	/* ipif associated w. ipsq_pending_mp */
1439 	ipif_t	*ipsq_current_ipif;	/* ipif associated with current ioctl */
1440 	mblk_t	*ipsq_pending_mp;	/* current ioctl mp while waiting for */
1441 					/* response from another module */
1442 	struct	ipsq_s	*ipsq_next;	/* list of all syncq's (ipsq_g_list) */
1443 	uint_t		ipsq_refs;	/* Number of phyints on this ipsq */
1444 	struct phyint	*ipsq_phyint_list; /* List of phyints on this ipsq */
1445 	boolean_t	ipsq_split;	/* ipsq may need to be split */
1446 	int		ipsq_waitfor;	/* Values encoded below */
1447 	char		ipsq_name[LIFNAMSIZ+1];	/* same as phyint_groupname */
1448 	int		ipsq_last_cmd;	/* debugging aid */
1449 #ifdef ILL_DEBUG
1450 	int		ipsq_depth;	/* debugging aid */
1451 	pc_t		ipsq_stack[IP_STACK_DEPTH];	/* debugging aid */
1452 #endif
1453 } ipsq_t;
1454 
1455 /* ipsq_flags */
1456 #define	IPSQ_GROUP	0x1	/* This ipsq belongs to an IPMP group */
1457 
1458 /*
1459  * ipsq_waitfor:
1460  *
1461  * IPIF_DOWN	1	ipif_down waiting for refcnts to drop
1462  * ILL_DOWN	2	ill_down waiting for refcnts to drop
1463  * IPIF_FREE	3	ipif_free waiting for refcnts to drop
1464  * ILL_FREE	4	ill unplumb waiting for refcnts to drop
1465  * ILL_MOVE_OK	5	failover waiting for refcnts to drop
1466  */
1467 
1468 enum { IPIF_DOWN = 1, ILL_DOWN, IPIF_FREE, ILL_FREE, ILL_MOVE_OK };
1469 
1470 /* Flags passed to ipsq_try_enter */
1471 #define	CUR_OP 0		/* Current ioctl continuing again */
1472 #define	NEW_OP 1		/* New ioctl starting afresh */
1473 
1474 /*
1475  * phyint represents state that is common to both IPv4 and IPv6 interfaces.
1476  * There is a separate ill_t representing IPv4 and IPv6 which has a
1477  * backpointer to the phyint structure for acessing common state.
1478  *
1479  * NOTE : It just stores the group name as there is only one name for
1480  *	  IPv4 and IPv6 i.e it is a underlying link property. Actually
1481  *        IPv4 and IPv6 ill are grouped together when their phyints have
1482  *        the same name.
1483  */
1484 typedef struct phyint {
1485 	struct ill_s	*phyint_illv4;
1486 	struct ill_s	*phyint_illv6;
1487 	uint_t		phyint_ifindex;		/* SIOCLSLIFINDEX */
1488 	uint_t		phyint_notify_delay;	/* SIOCSLIFNOTIFYDELAY */
1489 	char		*phyint_groupname;	/* SIOCSLIFGROUPNAME */
1490 	uint_t		phyint_groupname_len;
1491 	uint64_t	phyint_flags;
1492 	avl_node_t	phyint_avl_by_index;	/* avl tree by index */
1493 	avl_node_t	phyint_avl_by_name;	/* avl tree by name */
1494 	kmutex_t	phyint_lock;
1495 	struct ipsq_s	*phyint_ipsq;		/* back pointer to ipsq */
1496 	struct phyint	*phyint_ipsq_next;	/* phyint list on this ipsq */
1497 } phyint_t;
1498 
1499 #define	CACHE_ALIGN_SIZE 64
1500 
1501 #define	CACHE_ALIGN(align_struct)	P2ROUNDUP(sizeof (struct align_struct),\
1502 							CACHE_ALIGN_SIZE)
1503 struct _phyint_list_s_ {
1504 	avl_tree_t	phyint_list_avl_by_index;	/* avl tree by index */
1505 	avl_tree_t	phyint_list_avl_by_name;	/* avl tree by name */
1506 };
1507 
1508 typedef union phyint_list_u {
1509 	struct	_phyint_list_s_ phyint_list_s;
1510 	char	phyint_list_filler[CACHE_ALIGN(_phyint_list_s_)];
1511 } phyint_list_t;
1512 
1513 #define	phyint_list_avl_by_index	phyint_list_s.phyint_list_avl_by_index
1514 #define	phyint_list_avl_by_name		phyint_list_s.phyint_list_avl_by_name
1515 /*
1516  * ILL groups. We group ills,
1517  *
1518  * - if the ills have the same group name. (New way)
1519  *
1520  * ill_group locking notes:
1521  *
1522  * illgrp_lock protects ill_grp_ill_schednext.
1523  *
1524  * ill_g_lock protects ill_grp_next, illgrp_ill, illgrp_ill_count.
1525  * Holding ill_g_lock freezes the memberships of ills in IPMP groups.
1526  * It also freezes the global list of ills and all ipifs in all ills.
1527  *
1528  * To remove an ipif from the linked list of ipifs of that ill ipif_free_tail
1529  * holds both ill_g_lock, and ill_lock. Similarly to remove an ill from the
1530  * global list of ills, ill_delete_glist holds ill_g_lock as writer.
1531  * This simplifies things for ipif_select_source, illgrp_scheduler etc.
1532  * that need to walk the members of an illgrp. They just hold ill_g_lock
1533  * as reader to do the walk.
1534  *
1535  */
1536 typedef	struct ill_group {
1537 	kmutex_t	illgrp_lock;
1538 	struct ill_group *illgrp_next;		/* Next ill_group */
1539 	struct ill_s	*illgrp_ill_schednext;	/* Next ill to be scheduled */
1540 	struct ill_s	*illgrp_ill;		/* First ill in the group */
1541 	int		illgrp_ill_count;
1542 } ill_group_t;
1543 
1544 extern	ill_group_t	*illgrp_head_v6;
1545 
1546 /*
1547  * Fragmentation hash bucket
1548  */
1549 typedef struct ipfb_s {
1550 	struct ipf_s	*ipfb_ipf;	/* List of ... */
1551 	size_t		ipfb_count;	/* Count of bytes used by frag(s) */
1552 	kmutex_t	ipfb_lock;	/* Protect all ipf in list */
1553 	uint_t		ipfb_frag_pkts; /* num of distinct fragmented pkts */
1554 } ipfb_t;
1555 
1556 /*
1557  * IRE bucket structure. Usually there is an array of such structures,
1558  * each pointing to a linked list of ires. irb_refcnt counts the number
1559  * of walkers of a given hash bucket. Usually the reference count is
1560  * bumped up if the walker wants no IRES to be DELETED while walking the
1561  * list. Bumping up does not PREVENT ADDITION. This allows walking a given
1562  * hash bucket without stumbling up on a free pointer.
1563  *
1564  * irb_t structures in ip_ftable are dynamically allocated and freed.
1565  * In order to identify the irb_t structures that cna be safely kmem_free'd
1566  * we need to ensure that
1567  *  - the irb_refcnt is quiescent, indicating no other walkers,
1568  *  - no other threads or ire's are holding references to the irb,
1569  *	i.e., irb_nire == 0,
1570  *  - there are no active ire's in the bucket, i.e., irb_ire_cnt == 0
1571  */
1572 typedef struct irb {
1573 	struct ire_s	*irb_ire;	/* First ire in this bucket */
1574 					/* Should be first in this struct */
1575 	krwlock_t	irb_lock;	/* Protect this bucket */
1576 	uint_t		irb_refcnt;	/* Protected by irb_lock */
1577 	uchar_t		irb_marks;	/* CONDEMNED ires in this bucket ? */
1578 #define	IRB_MARK_CONDEMNED	0x0001
1579 #define	IRB_MARK_FTABLE		0x0002
1580 	uint_t		irb_ire_cnt;	/* Num of active IRE in this bucket */
1581 	uint_t		irb_tmp_ire_cnt; /* Num of temporary IRE */
1582 	struct ire_s	*irb_rr_origin;	/* origin for round-robin */
1583 	int		irb_nire;	/* Num of ftable ire's that ref irb */
1584 } irb_t;
1585 
1586 #define	IRB2RT(irb)	(rt_t *)((caddr_t)(irb) - offsetof(rt_t, rt_irb))
1587 
1588 /* The following are return values of ip_xmit_v4() */
1589 typedef enum {
1590 	SEND_PASSED = 0,	 /* sent packet out on wire */
1591 	SEND_FAILED,	 /* sending of packet failed */
1592 	LOOKUP_IN_PROGRESS, /* ire cache found, ARP resolution in progress */
1593 	LLHDR_RESLV_FAILED  /* macaddr resl of onlink dst or nexthop failed */
1594 } ipxmit_state_t;
1595 
1596 #define	IP_V4_G_HEAD	0
1597 #define	IP_V6_G_HEAD	1
1598 
1599 #define	MAX_G_HEADS	2
1600 
1601 /*
1602  * unpadded ill_if structure
1603  */
1604 struct 	_ill_if_s_ {
1605 	union ill_if_u	*illif_next;
1606 	union ill_if_u	*illif_prev;
1607 	avl_tree_t	illif_avl_by_ppa;	/* AVL tree sorted on ppa */
1608 	vmem_t		*illif_ppa_arena;	/* ppa index space */
1609 	uint16_t	illif_mcast_v1;		/* hints for		  */
1610 	uint16_t	illif_mcast_v2;		/* [igmp|mld]_slowtimo	  */
1611 	int		illif_name_len;		/* name length */
1612 	char		illif_name[LIFNAMSIZ];	/* name of interface type */
1613 };
1614 
1615 /* cache aligned ill_if structure */
1616 typedef union 	ill_if_u {
1617 	struct  _ill_if_s_ ill_if_s;
1618 	char 	illif_filler[CACHE_ALIGN(_ill_if_s_)];
1619 } ill_if_t;
1620 
1621 
1622 #define	illif_next		ill_if_s.illif_next
1623 #define	illif_prev		ill_if_s.illif_prev
1624 #define	illif_avl_by_ppa	ill_if_s.illif_avl_by_ppa
1625 #define	illif_ppa_arena		ill_if_s.illif_ppa_arena
1626 #define	illif_mcast_v1		ill_if_s.illif_mcast_v1
1627 #define	illif_mcast_v2		ill_if_s.illif_mcast_v2
1628 #define	illif_name		ill_if_s.illif_name
1629 #define	illif_name_len		ill_if_s.illif_name_len
1630 
1631 typedef struct ill_walk_context_s {
1632 	int	ctx_current_list; /* current list being searched */
1633 	int	ctx_last_list;	 /* last list to search */
1634 } ill_walk_context_t;
1635 
1636 /*
1637  * ill_gheads structure, one for IPV4 and one for IPV6
1638  */
1639 struct _ill_g_head_s_ {
1640 	ill_if_t	*ill_g_list_head;
1641 	ill_if_t	*ill_g_list_tail;
1642 };
1643 
1644 typedef union ill_g_head_u {
1645 	struct _ill_g_head_s_ ill_g_head_s;
1646 	char	ill_g_head_filler[CACHE_ALIGN(_ill_g_head_s_)];
1647 } ill_g_head_t;
1648 
1649 #define	ill_g_list_head	ill_g_head_s.ill_g_list_head
1650 #define	ill_g_list_tail	ill_g_head_s.ill_g_list_tail
1651 
1652 #pragma align CACHE_ALIGN_SIZE(ill_g_heads)
1653 extern ill_g_head_t	ill_g_heads[];	/* ILL List Head */
1654 
1655 
1656 #define	IP_V4_ILL_G_LIST	ill_g_heads[IP_V4_G_HEAD].ill_g_list_head
1657 #define	IP_V6_ILL_G_LIST	ill_g_heads[IP_V6_G_HEAD].ill_g_list_head
1658 #define	IP_VX_ILL_G_LIST(i)	ill_g_heads[i].ill_g_list_head
1659 
1660 #define	ILL_START_WALK_V4(ctx_ptr)	ill_first(IP_V4_G_HEAD, IP_V4_G_HEAD, \
1661 					ctx_ptr)
1662 #define	ILL_START_WALK_V6(ctx_ptr)	ill_first(IP_V6_G_HEAD, IP_V6_G_HEAD, \
1663 					ctx_ptr)
1664 #define	ILL_START_WALK_ALL(ctx_ptr)	ill_first(MAX_G_HEADS, MAX_G_HEADS, \
1665 					ctx_ptr)
1666 
1667 /*
1668  * Capabilities, possible flags for ill_capabilities.
1669  */
1670 
1671 #define	ILL_CAPAB_AH		0x01		/* IPsec AH acceleration */
1672 #define	ILL_CAPAB_ESP		0x02		/* IPsec ESP acceleration */
1673 #define	ILL_CAPAB_MDT		0x04		/* Multidata Transmit */
1674 #define	ILL_CAPAB_HCKSUM	0x08		/* Hardware checksumming */
1675 #define	ILL_CAPAB_ZEROCOPY	0x10		/* Zero-copy */
1676 #define	ILL_CAPAB_POLL		0x20		/* Polling Toggle */
1677 #define	ILL_CAPAB_SOFT_RING	0x40		/* Soft_Ring capability */
1678 
1679 /*
1680  * Per-ill Multidata Transmit capabilities.
1681  */
1682 typedef struct ill_mdt_capab_s ill_mdt_capab_t;
1683 
1684 /*
1685  * Per-ill IPsec capabilities.
1686  */
1687 typedef struct ill_ipsec_capab_s ill_ipsec_capab_t;
1688 
1689 /*
1690  * Per-ill Hardware Checksumming capbilities.
1691  */
1692 typedef struct ill_hcksum_capab_s ill_hcksum_capab_t;
1693 
1694 /*
1695  * Per-ill Zero-copy capabilities.
1696  */
1697 typedef struct ill_zerocopy_capab_s ill_zerocopy_capab_t;
1698 
1699 /*
1700  * Per-ill Polling/soft ring capbilities.
1701  */
1702 typedef struct ill_dls_capab_s ill_dls_capab_t;
1703 
1704 /*
1705  * Per-ill polling resource map.
1706  */
1707 typedef struct ill_rx_ring ill_rx_ring_t;
1708 
1709 /* The following are ill_state_flags */
1710 #define	ILL_LL_SUBNET_PENDING	0x01	/* Waiting for DL_INFO_ACK from drv */
1711 #define	ILL_CONDEMNED		0x02	/* No more new ref's to the ILL */
1712 #define	ILL_CHANGING		0x04	/* ILL not globally visible */
1713 #define	ILL_DL_UNBIND_IN_PROGRESS	0x08	/* UNBIND_REQ is sent */
1714 #define	ILL_SOFT_RING_ASSIGN	0x10	/* Makeing soft ring assigment */
1715 
1716 /* Is this an ILL whose source address is used by other ILL's ? */
1717 #define	IS_USESRC_ILL(ill)			\
1718 	(((ill)->ill_usesrc_ifindex == 0) &&	\
1719 	((ill)->ill_usesrc_grp_next != NULL))
1720 
1721 /* Is this a client/consumer of the usesrc ILL ? */
1722 #define	IS_USESRC_CLI_ILL(ill)			\
1723 	(((ill)->ill_usesrc_ifindex != 0) &&	\
1724 	((ill)->ill_usesrc_grp_next != NULL))
1725 
1726 /* Is this an virtual network interface (vni) ILL ? */
1727 #define	IS_VNI(ill)							     \
1728 	(((ill) != NULL) &&						     \
1729 	(((ill)->ill_phyint->phyint_flags & (PHYI_LOOPBACK|PHYI_VIRTUAL)) == \
1730 	PHYI_VIRTUAL))
1731 
1732 /*
1733  * IP Lower level Structure.
1734  * Instance data structure in ip_open when there is a device below us.
1735  */
1736 typedef struct ill_s {
1737 	ill_if_t *ill_ifptr;		/* pointer to interface type */
1738 	queue_t	*ill_rq;		/* Read queue. */
1739 	queue_t	*ill_wq;		/* Write queue. */
1740 
1741 	int	ill_error;		/* Error value sent up by device. */
1742 
1743 	ipif_t	*ill_ipif;		/* Interface chain for this ILL. */
1744 
1745 	uint_t	ill_ipif_up_count;	/* Number of IPIFs currently up. */
1746 	uint_t	ill_max_frag;		/* Max IDU from DLPI. */
1747 	char	*ill_name;		/* Our name. */
1748 	uint_t	ill_ipif_dup_count;	/* Number of duplicate addresses. */
1749 	uint_t	ill_name_length;	/* Name length, incl. terminator. */
1750 	char	*ill_ndd_name;		/* Name + ":ip?_forwarding" for NDD. */
1751 	uint_t	ill_net_type;		/* IRE_IF_RESOLVER/IRE_IF_NORESOLVER. */
1752 	/*
1753 	 * Physical Point of Attachment num.  If DLPI style 1 provider
1754 	 * then this is derived from the devname.
1755 	 */
1756 	uint_t	ill_ppa;
1757 	t_uscalar_t	ill_sap;
1758 	t_scalar_t	ill_sap_length;	/* Including sign (for position) */
1759 	uint_t	ill_phys_addr_length;	/* Excluding the sap. */
1760 	uint_t	ill_bcast_addr_length;	/* Only set when the DL provider */
1761 					/* supports broadcast. */
1762 	t_uscalar_t	ill_mactype;
1763 	uint8_t	*ill_frag_ptr;		/* Reassembly state. */
1764 	timeout_id_t ill_frag_timer_id; /* timeout id for the frag timer */
1765 	ipfb_t	*ill_frag_hash_tbl;	/* Fragment hash list head. */
1766 	ipif_t	*ill_pending_ipif;	/* IPIF waiting for DL operation. */
1767 
1768 	ilm_t	*ill_ilm;		/* Multicast mebership for lower ill */
1769 	uint_t	ill_global_timer;	/* for IGMPv3/MLDv2 general queries */
1770 	int	ill_mcast_type;		/* type of router which is querier */
1771 					/* on this interface */
1772 	uint16_t ill_mcast_v1_time;	/* # slow timeouts since last v1 qry */
1773 	uint16_t ill_mcast_v2_time;	/* # slow timeouts since last v2 qry */
1774 	uint8_t	ill_mcast_v1_tset;	/* 1 => timer is set; 0 => not set */
1775 	uint8_t	ill_mcast_v2_tset;	/* 1 => timer is set; 0 => not set */
1776 
1777 	uint8_t	ill_mcast_rv;		/* IGMPv3/MLDv2 robustness variable */
1778 	int	ill_mcast_qi;		/* IGMPv3/MLDv2 query interval var */
1779 
1780 	mblk_t	*ill_pending_mp;	/* IOCTL/DLPI awaiting completion. */
1781 	/*
1782 	 * All non-NULL cells between 'ill_first_mp_to_free' and
1783 	 * 'ill_last_mp_to_free' are freed in ill_delete.
1784 	 */
1785 #define	ill_first_mp_to_free	ill_bcast_mp
1786 	mblk_t	*ill_bcast_mp;		/* DLPI header for broadcasts. */
1787 	mblk_t	*ill_resolver_mp;	/* Resolver template. */
1788 	mblk_t	*ill_detach_mp;		/* detach mp, or NULL if style1 */
1789 	mblk_t	*ill_unbind_mp;		/* unbind mp from ill_dl_up() */
1790 	mblk_t	*ill_dlpi_deferred;	/* b_next chain of control messages */
1791 	mblk_t	*ill_phys_addr_mp;	/* mblk which holds ill_phys_addr */
1792 #define	ill_last_mp_to_free	ill_phys_addr_mp
1793 
1794 	cred_t	*ill_credp;		/* opener's credentials */
1795 	uint8_t	*ill_phys_addr;		/* ill_phys_addr_mp->b_rptr + off */
1796 
1797 	uint_t	ill_state_flags;	/* see ILL_* flags above */
1798 
1799 	/* Following bit fields protected by ipsq_t */
1800 	uint_t
1801 		ill_needs_attach : 1,
1802 		ill_reserved : 1,
1803 		ill_isv6 : 1,
1804 		ill_dlpi_style_set : 1,
1805 
1806 		ill_ifname_pending : 1,
1807 		ill_move_in_progress : 1, /* FAILOVER/FAILBACK in progress */
1808 		ill_join_allmulti : 1,
1809 		ill_logical_down : 1,
1810 
1811 		ill_is_6to4tun : 1,	/* Interface is a 6to4 tunnel */
1812 		ill_promisc_on_phys : 1, /* phys interface in promisc mode */
1813 		ill_dl_up : 1,
1814 		ill_up_ipifs : 1,
1815 
1816 		ill_note_link : 1,	/* supports link-up notification */
1817 
1818 		ill_pad_to_bit_31 : 19;
1819 
1820 	/* Following bit fields protected by ill_lock */
1821 	uint_t
1822 		ill_fragtimer_executing : 1,
1823 		ill_fragtimer_needrestart : 1,
1824 		ill_ilm_cleanup_reqd : 1,
1825 		ill_arp_closing : 1,
1826 
1827 		ill_arp_bringup_pending : 1,
1828 		ill_mtu_userspecified : 1, /* SIOCSLNKINFO has set the mtu */
1829 		ill_arp_extend : 1,	/* ARP has DAD extensions */
1830 		ill_pad_bit_31 : 25;
1831 
1832 	/*
1833 	 * Used in SIOCSIFMUXID and SIOCGIFMUXID for 'ifconfig unplumb'.
1834 	 */
1835 	int	ill_arp_muxid;		/* muxid returned from plink for arp */
1836 	int	ill_ip_muxid;		/* muxid returned from plink for ip */
1837 
1838 	/*
1839 	 * NIC event information attached, to be used by nic event hooks.
1840 	 */
1841 	hook_nic_event_t	*ill_nic_event_info;
1842 
1843 	/*
1844 	 * Used for IP frag reassembly throttling on a per ILL basis.
1845 	 *
1846 	 * Note: frag_count is approximate, its added to and subtracted from
1847 	 *	 without any locking, so simultaneous load/modify/stores can
1848 	 *	 collide, also ill_frag_purge() recalculates its value by
1849 	 *	 summing all the ipfb_count's without locking out updates
1850 	 *	 to the ipfb's.
1851 	 */
1852 	uint_t	ill_ipf_gen;		/* Generation of next fragment queue */
1853 	uint_t	ill_frag_count;		/* Approx count of all mblk bytes */
1854 	uint_t	ill_frag_free_num_pkts;	 /* num of fragmented packets to free */
1855 	clock_t	ill_last_frag_clean_time; /* time when frag's were pruned */
1856 	int	ill_type;		/* From <net/if_types.h> */
1857 	uint_t	ill_dlpi_multicast_state;	/* See below IDS_* */
1858 	uint_t	ill_dlpi_fastpath_state;	/* See below IDS_* */
1859 
1860 	/*
1861 	 * Capabilities related fields.
1862 	 */
1863 	uint_t  ill_dlpi_capab_state;	/* State of capability query, IDS_* */
1864 	uint64_t ill_capabilities;	/* Enabled capabilities, ILL_CAPAB_* */
1865 	ill_mdt_capab_t	*ill_mdt_capab;	/* Multidata Transmit capabilities */
1866 	ill_ipsec_capab_t *ill_ipsec_capab_ah;	/* IPsec AH capabilities */
1867 	ill_ipsec_capab_t *ill_ipsec_capab_esp;	/* IPsec ESP capabilities */
1868 	ill_hcksum_capab_t *ill_hcksum_capab; /* H/W cksumming capabilities */
1869 	ill_zerocopy_capab_t *ill_zerocopy_capab; /* Zero-copy capabilities */
1870 	ill_dls_capab_t *ill_dls_capab; /* Polling, soft ring capabilities */
1871 
1872 	/*
1873 	 * New fields for IPv6
1874 	 */
1875 	uint8_t	ill_max_hops;	/* Maximum hops for any logical interface */
1876 	uint_t	ill_max_mtu;	/* Maximum MTU for any logical interface */
1877 	uint32_t ill_reachable_time;	/* Value for ND algorithm in msec */
1878 	uint32_t ill_reachable_retrans_time; /* Value for ND algorithm msec */
1879 	uint_t	ill_max_buf;		/* Max # of req to buffer for ND */
1880 	in6_addr_t	ill_token;
1881 	uint_t		ill_token_length;
1882 	uint32_t	ill_xmit_count;		/* ndp max multicast xmits */
1883 	mib2_ipv6IfStatsEntry_t	*ill_ip6_mib;	/* Per interface mib */
1884 	mib2_ipv6IfIcmpEntry_t	*ill_icmp6_mib;	/* Per interface mib */
1885 	/*
1886 	 * Following two mblks are allocated common to all
1887 	 * the ipifs when the first interface is coming up.
1888 	 * It is sent up to arp when the last ipif is coming
1889 	 * down.
1890 	 */
1891 	mblk_t			*ill_arp_down_mp;
1892 	mblk_t			*ill_arp_del_mapping_mp;
1893 	/*
1894 	 * Used for implementing IFF_NOARP. As IFF_NOARP is used
1895 	 * to turn off for all the logicals, it is here instead
1896 	 * of the ipif.
1897 	 */
1898 	mblk_t			*ill_arp_on_mp;
1899 	/* Peer ill of an IPMP move operation */
1900 	struct ill_s		*ill_move_peer;
1901 
1902 	phyint_t		*ill_phyint;
1903 	uint64_t		ill_flags;
1904 	ill_group_t		*ill_group;
1905 	struct ill_s		*ill_group_next;
1906 	/*
1907 	 * Reverse tunnel related count. This count
1908 	 * determines how many mobile nodes are using this
1909 	 * ill to send packet to reverse tunnel via foreign
1910 	 * agent. A non-zero count specifies presence of
1911 	 * mobile node(s) using reverse tunnel through this
1912 	 * interface.
1913 	 */
1914 	uint32_t		ill_mrtun_refcnt;
1915 
1916 	/*
1917 	 * This count is bumped up when a route is added with
1918 	 * RTA_SRCIFP bit flag using routing socket.
1919 	 */
1920 	uint32_t		ill_srcif_refcnt;
1921 	/*
1922 	 * Pointer to the special interface based routing table.
1923 	 * This routing table is created dynamically when RTA_SRCIFP
1924 	 * is set by the routing socket.
1925 	 */
1926 	irb_t			*ill_srcif_table;
1927 	kmutex_t	ill_lock;	/* Please see table below */
1928 	/*
1929 	 * The ill_nd_lla* fields handle the link layer address option
1930 	 * from neighbor discovery. This is used for external IPv6
1931 	 * address resolution.
1932 	 */
1933 	mblk_t		*ill_nd_lla_mp;	/* mblk which holds ill_nd_lla */
1934 	uint8_t		*ill_nd_lla;	/* Link Layer Address */
1935 	uint_t		ill_nd_lla_len;	/* Link Layer Address length */
1936 	/*
1937 	 * We now have 3 phys_addr_req's sent down. This field keeps track
1938 	 * of which one is pending.
1939 	 */
1940 	t_uscalar_t	ill_phys_addr_pend; /* which dl_phys_addr_req pending */
1941 	/*
1942 	 * Used to save errors that occur during plumbing
1943 	 */
1944 	uint_t		ill_ifname_pending_err;
1945 	avl_node_t	ill_avl_byppa; /* avl node based on ppa */
1946 	void		*ill_fastpath_list; /* both ire and nce hang off this */
1947 	uint_t		ill_refcnt;	/* active refcnt by threads */
1948 	uint_t		ill_ire_cnt;	/* ires associated with this ill */
1949 	kcondvar_t	ill_cv;
1950 	uint_t		ill_ilm_walker_cnt;	/* snmp ilm walkers */
1951 	uint_t		ill_nce_cnt;	/* nces associated with this ill */
1952 	uint_t		ill_waiters;	/* threads waiting in ipsq_enter */
1953 	/*
1954 	 * Contains the upper read queue pointer of the module immediately
1955 	 * beneath IP.  This field allows IP to validate sub-capability
1956 	 * acknowledgments coming up from downstream.
1957 	 */
1958 	queue_t		*ill_lmod_rq;	/* read queue pointer of module below */
1959 	uint_t		ill_lmod_cnt;	/* number of modules beneath IP */
1960 	ip_m_t		*ill_media;	/* media specific params/functions */
1961 	t_uscalar_t	ill_dlpi_pending; /* Last DLPI primitive issued */
1962 	uint_t		ill_usesrc_ifindex; /* use src addr from this ILL */
1963 	struct ill_s	*ill_usesrc_grp_next; /* Next ILL in the usesrc group */
1964 #ifdef ILL_DEBUG
1965 	th_trace_t	*ill_trace[IP_TR_HASH_MAX];
1966 	boolean_t	ill_trace_disable;	/* True when alloc fails */
1967 #endif
1968 } ill_t;
1969 
1970 extern	void	ill_delete_glist(ill_t *);
1971 
1972 /*
1973  * The following table lists the protection levels of the various members
1974  * of the ill_t. Same notation as that used for ipif_t above is used.
1975  *
1976  *				Write			Read
1977  *
1978  * ill_ifptr			ill_g_lock + s		Write once
1979  * ill_rq			ipsq			Write once
1980  * ill_wq			ipsq			Write once
1981  *
1982  * ill_error			ipsq			None
1983  * ill_ipif			ill_g_lock + ipsq	ill_g_lock OR ipsq
1984  * ill_ipif_up_count		ill_lock + ipsq		ill_lock
1985  * ill_max_frag			ipsq			Write once
1986  *
1987  * ill_name			ill_g_lock + ipsq		Write once
1988  * ill_name_length		ill_g_lock + ipsq		Write once
1989  * ill_ndd_name			ipsq			Write once
1990  * ill_net_type			ipsq			Write once
1991  * ill_ppa			ill_g_lock + ipsq		Write once
1992  * ill_sap			ipsq + down ill		Write once
1993  * ill_sap_length		ipsq + down ill		Write once
1994  * ill_phys_addr_length		ipsq + down ill		Write once
1995  *
1996  * ill_bcast_addr_length	ipsq			ipsq
1997  * ill_mactype			ipsq			ipsq
1998  * ill_frag_ptr			ipsq			ipsq
1999  *
2000  * ill_frag_timer_id		ill_lock		ill_lock
2001  * ill_frag_hash_tbl		ipsq			up ill
2002  * ill_ilm			ipsq + ill_lock		ill_lock
2003  * ill_mcast_type		ill_lock		ill_lock
2004  * ill_mcast_v1_time		ill_lock		ill_lock
2005  * ill_mcast_v2_time		ill_lock		ill_lock
2006  * ill_mcast_v1_tset		ill_lock		ill_lock
2007  * ill_mcast_v2_tset		ill_lock		ill_lock
2008  * ill_mcast_rv			ill_lock		ill_lock
2009  * ill_mcast_qi			ill_lock		ill_lock
2010  * ill_pending_mp		ill_lock		ill_lock
2011  *
2012  * ill_bcast_mp			ipsq			ipsq
2013  * ill_resolver_mp		ipsq			only when ill is up
2014  * ill_down_mp			ipsq			ipsq
2015  * ill_dlpi_deferred		ipsq			ipsq
2016  * ill_phys_addr_mp		ipsq			ipsq
2017  * ill_phys_addr		ipsq			up ill
2018  * ill_ick			ipsq + down ill		only when ill is up
2019  *
2020  * ill_state_flags		ill_lock		ill_lock
2021  * exclusive bit flags		ipsq_t			ipsq_t
2022  * shared bit flags		ill_lock		ill_lock
2023  *
2024  * ill_arp_muxid		ipsq			Not atomic
2025  * ill_ip_muxid			ipsq			Not atomic
2026  *
2027  * ill_ipf_gen			Not atomic
2028  * ill_frag_count		Approx. not protected
2029  * ill_type			ipsq + down ill		only when ill is up
2030  * ill_dlpi_multicast_state	ill_lock		ill_lock
2031  * ill_dlpi_fastpath_state	ill_lock		ill_lock
2032  * ill_max_hops			ipsq			Not atomic
2033  *
2034  * ill_max_mtu
2035  *
2036  * ill_reachable_time		ipsq + ill_lock		ill_lock
2037  * ill_reachable_retrans_time	ipsq  + ill_lock		ill_lock
2038  * ill_max_buf			ipsq + ill_lock		ill_lock
2039  *
2040  * Next 2 fields need ill_lock because of the get ioctls. They should not
2041  * report partially updated results without executing in the ipsq.
2042  * ill_token			ipsq + ill_lock		ill_lock
2043  * ill_token_length		ipsq + ill_lock		ill_lock
2044  * ill_xmit_count		ipsq + down ill		write once
2045  * ill_ip6_mib			ipsq + down ill		only when ill is up
2046  * ill_icmp6_mib		ipsq + down ill		only when ill is up
2047  * ill_arp_down_mp		ipsq			ipsq
2048  * ill_arp_del_mapping_mp	ipsq			ipsq
2049  * ill_arp_on_mp		ipsq			ipsq
2050  * ill_move_peer		ipsq			ipsq
2051  *
2052  * ill_phyint			ipsq, ill_g_lock, ill_lock	Any of them
2053  * ill_flags			ill_lock		ill_lock
2054  * ill_group			ipsq, ill_g_lock, ill_lock	Any of them
2055  * ill_group_next		ipsq, ill_g_lock, ill_lock	Any of them
2056  * ill_mrtun_refcnt		ill_lock		ill_lock
2057  * ill_srcif_refcnt		ill_lock		ill_lock
2058  * ill_srcif_table		ill_lock		ill_lock
2059  * ill_nd_lla_mp		ill_lock		ill_lock
2060  * ill_nd_lla			ill_lock		ill_lock
2061  * ill_nd_lla_len		ill_lock		ill_lock
2062  * ill_phys_addr_pend		ipsq + down ill		only when ill is up
2063  * ill_ifname_pending_err	ipsq			ipsq
2064  * ill_avl_byppa		ipsq, ill_g_lock		Write once
2065  *
2066  * ill_fastpath_list		ill_lock		ill_lock
2067  * ill_refcnt			ill_lock		ill_lock
2068  * ill_ire_cnt			ill_lock		ill_lock
2069  * ill_cv			ill_lock		ill_lock
2070  * ill_ilm_walker_cnt		ill_lock		ill_lock
2071  * ill_nce_cnt			ill_lock		ill_lock
2072  * ill_trace			ill_lock		ill_lock
2073  * ill_usesrc_grp_next		ill_g_usesrc_lock	ill_g_usesrc_lock
2074  */
2075 
2076 /*
2077  * For ioctl restart mechanism see ip_reprocess_ioctl()
2078  */
2079 struct ip_ioctl_cmd_s;
2080 
2081 typedef	int (*ifunc_t)(ipif_t *, struct sockaddr_in *, queue_t *, mblk_t *,
2082     struct ip_ioctl_cmd_s *, void *);
2083 
2084 typedef struct ip_ioctl_cmd_s {
2085 	int	ipi_cmd;
2086 	size_t	ipi_copyin_size;
2087 	uint_t	ipi_flags;
2088 	uint_t	ipi_cmd_type;
2089 	ifunc_t	ipi_func;
2090 	ifunc_t	ipi_func_restart;
2091 } ip_ioctl_cmd_t;
2092 
2093 /*
2094  * ipi_cmd_type:
2095  *
2096  * IF_CMD		1	old style ifreq cmd
2097  * LIF_CMD		2	new style lifreq cmd
2098  * MISC_CMD		3	Misc. (non [l]ifreq, tun) cmds
2099  * TUN_CMD		4	tunnel related
2100  */
2101 
2102 enum { IF_CMD = 1, LIF_CMD, MISC_CMD, TUN_CMD };
2103 
2104 #define	IPI_DONTCARE	0	/* For ioctl encoded values that don't matter */
2105 
2106 /* Flag values in ipi_flags */
2107 #define	IPI_PRIV	0x1		/* Root only command */
2108 #define	IPI_MODOK	0x2		/* Permitted on mod instance of IP */
2109 #define	IPI_WR		0x4		/* Need to grab writer access */
2110 #define	IPI_GET_CMD	0x8		/* branch to mi_copyout on success */
2111 #define	IPI_REPL	0x10	/* valid for replacement ipif created in MOVE */
2112 #define	IPI_NULL_BCONT	0x20	/* ioctl has not data and hence no b_cont */
2113 #define	IPI_PASS_DOWN	0x40	/* pass this ioctl down when a module only */
2114 
2115 extern ip_ioctl_cmd_t	ip_ndx_ioctl_table[];
2116 extern ip_ioctl_cmd_t	ip_misc_ioctl_table[];
2117 extern int ip_ndx_ioctl_count;
2118 extern int ip_misc_ioctl_count;
2119 
2120 #define	ILL_CLEAR_MOVE(ill) {				\
2121 	ill_t *peer_ill;				\
2122 							\
2123 	peer_ill = (ill)->ill_move_peer;		\
2124 	ASSERT(peer_ill != NULL);			\
2125 	(ill)->ill_move_in_progress = B_FALSE;		\
2126 	peer_ill->ill_move_in_progress = B_FALSE;	\
2127 	(ill)->ill_move_peer = NULL;			\
2128 	peer_ill->ill_move_peer = NULL;			\
2129 }
2130 /* The 2 ill's are same or belong to the same IPMP group */
2131 #define	SAME_IPMP_GROUP(ill_1, ill_2)			\
2132 	(((ill_1)->ill_group != NULL) &&		\
2133 	    ((ill_1)->ill_group == (ill_2)->ill_group))
2134 
2135 /* Passed down by ARP to IP during I_PLINK/I_PUNLINK */
2136 typedef struct ipmx_s {
2137 	char	ipmx_name[LIFNAMSIZ];		/* if name */
2138 	uint_t
2139 		ipmx_arpdev_stream : 1,		/* This is the arp stream */
2140 		ipmx_notused : 31;
2141 } ipmx_t;
2142 
2143 /*
2144  * State for detecting if a driver supports certain features.
2145  * Support for DL_ENABMULTI_REQ uses ill_dlpi_multicast_state.
2146  * Support for DLPI M_DATA fastpath uses ill_dlpi_fastpath_state.
2147  * Support for DL_CAPABILITY_REQ uses ill_dlpi_capab_state.
2148  */
2149 #define	IDS_UNKNOWN	0	/* No DLPI request sent */
2150 #define	IDS_INPROGRESS	1	/* DLPI request sent */
2151 #define	IDS_OK		2	/* DLPI request completed successfully */
2152 #define	IDS_FAILED	3	/* DLPI request failed */
2153 #define	IDS_RENEG	4	/* Driver asked for a renegotiation */
2154 
2155 /* Named Dispatch Parameter Management Structure */
2156 typedef struct ipparam_s {
2157 	uint_t	ip_param_min;
2158 	uint_t	ip_param_max;
2159 	uint_t	ip_param_value;
2160 	char	*ip_param_name;
2161 } ipparam_t;
2162 
2163 /* Extended NDP Management Structure */
2164 typedef struct ipndp_s {
2165 	ndgetf_t	ip_ndp_getf;
2166 	ndsetf_t	ip_ndp_setf;
2167 	caddr_t		ip_ndp_data;
2168 	char		*ip_ndp_name;
2169 } ipndp_t;
2170 
2171 /*
2172  * The kernel stores security attributes of all gateways in a database made
2173  * up of one or more tsol_gcdb_t elements.  Each tsol_gcdb_t contains the
2174  * security-related credentials of the gateway.  More than one gateways may
2175  * share entries in the database.
2176  *
2177  * The tsol_gc_t structure represents the gateway to credential association,
2178  * and refers to an entry in the database.  One or more tsol_gc_t entities are
2179  * grouped together to form one or more tsol_gcgrp_t, each representing the
2180  * list of security attributes specific to the gateway.  A gateway may be
2181  * associated with at most one credentials group.
2182  */
2183 struct tsol_gcgrp_s;
2184 
2185 extern uchar_t	ip6opt_ls;	/* TX IPv6 enabler */
2186 
2187 /*
2188  * Gateway security credential record.
2189  */
2190 typedef struct tsol_gcdb_s {
2191 	uint_t		gcdb_refcnt;	/* reference count */
2192 	struct rtsa_s	gcdb_attr;	/* security attributes */
2193 #define	gcdb_mask	gcdb_attr.rtsa_mask
2194 #define	gcdb_doi	gcdb_attr.rtsa_doi
2195 #define	gcdb_slrange	gcdb_attr.rtsa_slrange
2196 } tsol_gcdb_t;
2197 
2198 /*
2199  * Gateway to credential association.
2200  */
2201 typedef struct tsol_gc_s {
2202 	uint_t		gc_refcnt;	/* reference count */
2203 	struct tsol_gcgrp_s *gc_grp;	/* pointer to group */
2204 	struct tsol_gc_s *gc_prev;	/* previous in list */
2205 	struct tsol_gc_s *gc_next;	/* next in list */
2206 	tsol_gcdb_t	*gc_db;		/* pointer to actual credentials */
2207 } tsol_gc_t;
2208 
2209 /*
2210  * Gateway credentials group address.
2211  */
2212 typedef struct tsol_gcgrp_addr_s {
2213 	int		ga_af;		/* address family */
2214 	in6_addr_t	ga_addr;	/* IPv4 mapped or IPv6 address */
2215 } tsol_gcgrp_addr_t;
2216 
2217 /*
2218  * Gateway credentials group.
2219  */
2220 typedef struct tsol_gcgrp_s {
2221 	uint_t		gcgrp_refcnt;	/* reference count */
2222 	krwlock_t	gcgrp_rwlock;	/* lock to protect following */
2223 	uint_t		gcgrp_count;	/* number of credentials */
2224 	tsol_gc_t	*gcgrp_head;	/* first credential in list */
2225 	tsol_gc_t	*gcgrp_tail;	/* last credential in list */
2226 	tsol_gcgrp_addr_t gcgrp_addr;	/* next-hop gateway address */
2227 } tsol_gcgrp_t;
2228 
2229 extern kmutex_t gcgrp_lock;
2230 
2231 #define	GC_REFRELE(p) {				\
2232 	ASSERT((p)->gc_grp != NULL);		\
2233 	rw_enter(&(p)->gc_grp->gcgrp_rwlock, RW_WRITER); \
2234 	ASSERT((p)->gc_refcnt > 0);		\
2235 	if (--((p)->gc_refcnt) == 0)		\
2236 		gc_inactive(p);			\
2237 	else					\
2238 		rw_exit(&(p)->gc_grp->gcgrp_rwlock); \
2239 }
2240 
2241 #define	GCGRP_REFHOLD(p) {			\
2242 	mutex_enter(&gcgrp_lock);		\
2243 	++((p)->gcgrp_refcnt);			\
2244 	ASSERT((p)->gcgrp_refcnt != 0);		\
2245 	mutex_exit(&gcgrp_lock);		\
2246 }
2247 
2248 #define	GCGRP_REFRELE(p) {			\
2249 	mutex_enter(&gcgrp_lock);		\
2250 	ASSERT((p)->gcgrp_refcnt > 0);		\
2251 	if (--((p)->gcgrp_refcnt) == 0)		\
2252 		gcgrp_inactive(p);		\
2253 	ASSERT(MUTEX_HELD(&gcgrp_lock));	\
2254 	mutex_exit(&gcgrp_lock);		\
2255 }
2256 
2257 /*
2258  * IRE gateway security attributes structure, pointed to by tsol_ire_gw_secattr
2259  */
2260 struct tsol_tnrhc;
2261 
2262 typedef struct tsol_ire_gw_secattr_s {
2263 	kmutex_t	igsa_lock;	/* lock to protect following */
2264 	struct tsol_tnrhc *igsa_rhc;	/* host entry for gateway */
2265 	tsol_gc_t	*igsa_gc;	/* for prefix IREs */
2266 	tsol_gcgrp_t	*igsa_gcgrp;	/* for cache IREs */
2267 } tsol_ire_gw_secattr_t;
2268 
2269 /*
2270  * Following are the macros to increment/decrement the reference
2271  * count of the IREs and IRBs (ire bucket).
2272  *
2273  * 1) We bump up the reference count of an IRE to make sure that
2274  *    it does not get deleted and freed while we are using it.
2275  *    Typically all the lookup functions hold the bucket lock,
2276  *    and look for the IRE. If it finds an IRE, it bumps up the
2277  *    reference count before dropping the lock. Sometimes we *may* want
2278  *    to bump up the reference count after we *looked* up i.e without
2279  *    holding the bucket lock. So, the IRE_REFHOLD macro does not assert
2280  *    on the bucket lock being held. Any thread trying to delete from
2281  *    the hash bucket can still do so but cannot free the IRE if
2282  *    ire_refcnt is not 0.
2283  *
2284  * 2) We bump up the reference count on the bucket where the IRE resides
2285  *    (IRB), when we want to prevent the IREs getting deleted from a given
2286  *    hash bucket. This makes life easier for ire_walk type functions which
2287  *    wants to walk the IRE list, call a function, but needs to drop
2288  *    the bucket lock to prevent recursive rw_enters. While the
2289  *    lock is dropped, the list could be changed by other threads or
2290  *    the same thread could end up deleting the ire or the ire pointed by
2291  *    ire_next. IRE_REFHOLDing the ire or ire_next is not sufficient as
2292  *    a delete will still remove the ire from the bucket while we have
2293  *    dropped the lock and hence the ire_next would be NULL. Thus, we
2294  *    need a mechanism to prevent deletions from a given bucket.
2295  *
2296  *    To prevent deletions, we bump up the reference count on the
2297  *    bucket. If the bucket is held, ire_delete just marks IRE_MARK_CONDEMNED
2298  *    both on the ire's ire_marks and the bucket's irb_marks. When the
2299  *    reference count on the bucket drops to zero, all the CONDEMNED ires
2300  *    are deleted. We don't have to bump up the reference count on the
2301  *    bucket if we are walking the bucket and never have to drop the bucket
2302  *    lock. Note that IRB_REFHOLD does not prevent addition of new ires
2303  *    in the list. It is okay because addition of new ires will not cause
2304  *    ire_next to point to freed memory. We do IRB_REFHOLD only when
2305  *    all of the 3 conditions are true :
2306  *
2307  *    1) The code needs to walk the IRE bucket from start to end.
2308  *    2) It may have to drop the bucket lock sometimes while doing (1)
2309  *    3) It does not want any ires to be deleted meanwhile.
2310  */
2311 
2312 /*
2313  * Bump up the reference count on the IRE. We cannot assert that the
2314  * bucket lock is being held as it is legal to bump up the reference
2315  * count after the first lookup has returned the IRE without
2316  * holding the lock. Currently ip_wput does this for caching IRE_CACHEs.
2317  */
2318 
2319 #ifndef IRE_DEBUG
2320 
2321 #define	IRE_REFHOLD_NOTR(ire)	IRE_REFHOLD(ire)
2322 #define	IRE_UNTRACE_REF(ire)
2323 #define	IRE_TRACE_REF(ire)
2324 
2325 #else
2326 
2327 #define	IRE_REFHOLD_NOTR(ire) {				\
2328 	atomic_add_32(&(ire)->ire_refcnt, 1);		\
2329 	ASSERT((ire)->ire_refcnt != 0);			\
2330 }
2331 
2332 #define	IRE_UNTRACE_REF(ire)	ire_untrace_ref(ire);
2333 #define	IRE_TRACE_REF(ire)	ire_trace_ref(ire);
2334 #endif
2335 
2336 #define	IRE_REFHOLD(ire) {				\
2337 	atomic_add_32(&(ire)->ire_refcnt, 1);		\
2338 	ASSERT((ire)->ire_refcnt != 0);			\
2339 	IRE_TRACE_REF(ire);				\
2340 }
2341 
2342 #define	IRE_REFHOLD_LOCKED(ire)	{			\
2343 	IRE_TRACE_REF(ire);				\
2344 	(ire)->ire_refcnt++;				\
2345 }
2346 
2347 /*
2348  * Decrement the reference count on the IRE.
2349  * In architectures e.g sun4u, where atomic_add_32_nv is just
2350  * a cas, we need to maintain the right memory barrier semantics
2351  * as that of mutex_exit i.e all the loads and stores should complete
2352  * before the cas is executed. membar_exit() does that here.
2353  *
2354  * NOTE : This macro is used only in places where we want performance.
2355  *	  To avoid bloating the code, we use the function "ire_refrele"
2356  *	  which essentially calls the macro.
2357  */
2358 #ifndef IRE_DEBUG
2359 #define	IRE_REFRELE(ire) {					\
2360 	ASSERT((ire)->ire_refcnt != 0);				\
2361 	membar_exit();						\
2362 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)	\
2363 		ire_inactive(ire);				\
2364 }
2365 #define	IRE_REFRELE_NOTR(ire)	IRE_REFRELE(ire)
2366 #else
2367 #define	IRE_REFRELE(ire) {					\
2368 	if (ire->ire_bucket != NULL)				\
2369 		ire_untrace_ref(ire);				\
2370 	ASSERT((ire)->ire_refcnt != 0);				\
2371 	membar_exit();						\
2372 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)	\
2373 		ire_inactive(ire);				\
2374 }
2375 #define	IRE_REFRELE_NOTR(ire) {					\
2376 	ASSERT((ire)->ire_refcnt != 0);				\
2377 	membar_exit();						\
2378 	if (atomic_add_32_nv(&(ire)->ire_refcnt, -1) == 0)	\
2379 		ire_inactive(ire);				\
2380 }
2381 #endif
2382 
2383 /*
2384  * Bump up the reference count on the hash bucket - IRB to
2385  * prevent ires from being deleted in this bucket.
2386  */
2387 #define	IRB_REFHOLD(irb) {				\
2388 	rw_enter(&(irb)->irb_lock, RW_WRITER);		\
2389 	(irb)->irb_refcnt++;				\
2390 	ASSERT((irb)->irb_refcnt != 0);			\
2391 	rw_exit(&(irb)->irb_lock);			\
2392 }
2393 #define	IRB_REFHOLD_LOCKED(irb) {			\
2394 	ASSERT(RW_WRITE_HELD(&(irb)->irb_lock));	\
2395 	(irb)->irb_refcnt++;				\
2396 	ASSERT((irb)->irb_refcnt != 0);			\
2397 }
2398 
2399 void irb_refrele_ftable(irb_t *);
2400 /*
2401  * Note: when IRB_MARK_FTABLE (i.e., IRE_CACHETABLE entry), the irb_t
2402  * is statically allocated, so that when the irb_refcnt goes to 0,
2403  * we simply clean up the ire list and continue.
2404  */
2405 #define	IRB_REFRELE(irb) {				\
2406 	if ((irb)->irb_marks & IRB_MARK_FTABLE) {	\
2407 		irb_refrele_ftable((irb));		\
2408 	} else {					\
2409 		rw_enter(&(irb)->irb_lock, RW_WRITER);		\
2410 		ASSERT((irb)->irb_refcnt != 0);			\
2411 		if (--(irb)->irb_refcnt	== 0 &&			\
2412 		    ((irb)->irb_marks & IRE_MARK_CONDEMNED)) {	\
2413 			ire_t *ire_list;			\
2414 								\
2415 			ire_list = ire_unlink(irb);		\
2416 			rw_exit(&(irb)->irb_lock);		\
2417 			ASSERT(ire_list != NULL);		\
2418 			ire_cleanup(ire_list);			\
2419 		} else {					\
2420 			rw_exit(&(irb)->irb_lock);		\
2421 		}						\
2422 	}							\
2423 }
2424 
2425 extern struct kmem_cache *rt_entry_cache;
2426 
2427 /*
2428  * Lock the fast path mp for access, since the fp_mp can be deleted
2429  * due a DL_NOTE_FASTPATH_FLUSH in the case of IRE_BROADCAST and IRE_MIPRTUN
2430  */
2431 
2432 #define	LOCK_IRE_FP_MP(ire) {				\
2433 		if ((ire)->ire_type == IRE_BROADCAST ||	\
2434 		    (ire)->ire_type == IRE_MIPRTUN)	\
2435 			mutex_enter(&ire->ire_nce->nce_lock);	\
2436 	}
2437 #define	UNLOCK_IRE_FP_MP(ire) {				\
2438 		if ((ire)->ire_type == IRE_BROADCAST ||	\
2439 		    (ire)->ire_type == IRE_MIPRTUN)	\
2440 			mutex_exit(&ire->ire_nce->nce_lock);	\
2441 	}
2442 
2443 typedef struct ire4 {
2444 	ipaddr_t ire4_src_addr;		/* Source address to use. */
2445 	ipaddr_t ire4_mask;		/* Mask for matching this IRE. */
2446 	ipaddr_t ire4_addr;		/* Address this IRE represents. */
2447 	ipaddr_t ire4_gateway_addr;	/* Gateway if IRE_CACHE/IRE_OFFSUBNET */
2448 	ipaddr_t ire4_cmask;		/* Mask from parent prefix route */
2449 } ire4_t;
2450 
2451 typedef struct ire6 {
2452 	in6_addr_t ire6_src_addr;	/* Source address to use. */
2453 	in6_addr_t ire6_mask;		/* Mask for matching this IRE. */
2454 	in6_addr_t ire6_addr;		/* Address this IRE represents. */
2455 	in6_addr_t ire6_gateway_addr;	/* Gateway if IRE_CACHE/IRE_OFFSUBNET */
2456 	in6_addr_t ire6_cmask;		/* Mask from parent prefix route */
2457 } ire6_t;
2458 
2459 typedef union ire_addr {
2460 	ire6_t	ire6_u;
2461 	ire4_t	ire4_u;
2462 } ire_addr_u_t;
2463 
2464 /* Internet Routing Entry */
2465 typedef struct ire_s {
2466 	struct	ire_s	*ire_next;	/* The hash chain must be first. */
2467 	struct	ire_s	**ire_ptpn;	/* Pointer to previous next. */
2468 	uint32_t	ire_refcnt;	/* Number of references */
2469 	mblk_t		*ire_mp;	/* Non-null if allocated as mblk */
2470 	queue_t		*ire_rfq;	/* recv from this queue */
2471 	queue_t		*ire_stq;	/* send to this queue */
2472 	union {
2473 		uint_t	*max_fragp;	/* Used only during ire creation */
2474 		uint_t	max_frag;	/* MTU (next hop or path). */
2475 	} imf_u;
2476 #define	ire_max_frag	imf_u.max_frag
2477 #define	ire_max_fragp	imf_u.max_fragp
2478 	uint32_t	ire_frag_flag;	/* IPH_DF or zero. */
2479 	uint32_t	ire_ident;	/* Per IRE IP ident. */
2480 	uint32_t	ire_tire_mark;	/* Used for reclaim of unused. */
2481 	uchar_t		ire_ipversion;	/* IPv4/IPv6 version */
2482 	uchar_t		ire_marks;	/* IRE_MARK_CONDEMNED etc. */
2483 	ushort_t	ire_type;	/* Type of IRE */
2484 	uint_t	ire_ib_pkt_count;	/* Inbound packets for ire_addr */
2485 	uint_t	ire_ob_pkt_count;	/* Outbound packets to ire_addr */
2486 	uint_t	ire_ll_hdr_length;	/* Non-zero if we do M_DATA prepends */
2487 	time_t	ire_create_time;	/* Time (in secs) IRE was created. */
2488 	uint32_t	ire_phandle;	/* Associate prefix IREs to cache */
2489 	uint32_t	ire_ihandle;	/* Associate interface IREs to cache */
2490 	ipif_t		*ire_ipif;	/* the interface that this ire uses */
2491 	uint32_t	ire_flags;	/* flags related to route (RTF_*) */
2492 	uint_t	ire_ipsec_overhead;	/* IPSEC overhead */
2493 	/*
2494 	 * Neighbor Cache Entry for IPv6; arp info for IPv4
2495 	 */
2496 	struct	nce_s	*ire_nce;
2497 	uint_t		ire_masklen;	/* # bits in ire_mask{,_v6} */
2498 	ire_addr_u_t	ire_u;		/* IPv4/IPv6 address info. */
2499 
2500 	irb_t		*ire_bucket;	/* Hash bucket when ire_ptphn is set */
2501 	iulp_t		ire_uinfo;	/* Upper layer protocol info. */
2502 	/*
2503 	 * Protects ire_uinfo, ire_max_frag, and ire_frag_flag.
2504 	 */
2505 	kmutex_t	ire_lock;
2506 	uint_t		ire_ipif_seqid; /* ipif_seqid of ire_ipif */
2507 	/*
2508 	 * For regular routes in forwarding table and cache table the
2509 	 * the following entries are NULL/zero. Only reverse tunnel
2510 	 * table and interface based forwarding table use these fields.
2511 	 * Routes added with RTA_SRCIFP and RTA_SRC respectively have
2512 	 * non-zero values for the following fields.
2513 	 */
2514 	ill_t		*ire_in_ill;	/* Incoming ill interface */
2515 	ipaddr_t	ire_in_src_addr;
2516 					/* source ip-addr of incoming packet */
2517 	clock_t		ire_last_used_time;	/* Last used time */
2518 	struct ire_s 	*ire_fastpath;	/* Pointer to next ire in fastpath */
2519 	tsol_ire_gw_secattr_t *ire_gw_secattr; /* gateway security attributes */
2520 	zoneid_t	ire_zoneid;	/* for local address discrimination */
2521 	/*
2522 	 * ire's that are embedded inside mblk_t and sent to the external
2523 	 * resolver use the ire_stq_ifindex to track the ifindex of the
2524 	 * ire_stq, so that the ill (if it exists) can be correctly recovered
2525 	 * for cleanup in the esbfree routine when arp failure occurs
2526 	 */
2527 	uint_t	ire_stq_ifindex;
2528 	uint_t		ire_defense_count;	/* number of ARP conflicts */
2529 	uint_t		ire_defense_time;	/* last time defended (secs) */
2530 #ifdef IRE_DEBUG
2531 	th_trace_t	*ire_trace[IP_TR_HASH_MAX];
2532 	boolean_t	ire_trace_disable;	/* True when alloc fails */
2533 #endif
2534 } ire_t;
2535 
2536 /* IPv4 compatiblity macros */
2537 #define	ire_src_addr		ire_u.ire4_u.ire4_src_addr
2538 #define	ire_mask		ire_u.ire4_u.ire4_mask
2539 #define	ire_addr		ire_u.ire4_u.ire4_addr
2540 #define	ire_gateway_addr	ire_u.ire4_u.ire4_gateway_addr
2541 #define	ire_cmask		ire_u.ire4_u.ire4_cmask
2542 
2543 #define	ire_src_addr_v6		ire_u.ire6_u.ire6_src_addr
2544 #define	ire_mask_v6		ire_u.ire6_u.ire6_mask
2545 #define	ire_addr_v6		ire_u.ire6_u.ire6_addr
2546 #define	ire_gateway_addr_v6	ire_u.ire6_u.ire6_gateway_addr
2547 #define	ire_cmask_v6		ire_u.ire6_u.ire6_cmask
2548 
2549 /* Convenient typedefs for sockaddrs */
2550 typedef	struct sockaddr_in	sin_t;
2551 typedef	struct sockaddr_in6	sin6_t;
2552 
2553 /* Address structure used for internal bind with IP */
2554 typedef struct ipa_conn_s {
2555 	ipaddr_t	ac_laddr;
2556 	ipaddr_t	ac_faddr;
2557 	uint16_t	ac_fport;
2558 	uint16_t	ac_lport;
2559 } ipa_conn_t;
2560 
2561 typedef struct ipa6_conn_s {
2562 	in6_addr_t	ac6_laddr;
2563 	in6_addr_t	ac6_faddr;
2564 	uint16_t	ac6_fport;
2565 	uint16_t	ac6_lport;
2566 } ipa6_conn_t;
2567 
2568 /*
2569  * Using ipa_conn_x_t or ipa6_conn_x_t allows us to modify the behavior of IP's
2570  * bind handler.
2571  */
2572 typedef struct ipa_conn_extended_s {
2573 	uint64_t	acx_flags;
2574 	ipa_conn_t	acx_conn;
2575 } ipa_conn_x_t;
2576 
2577 typedef struct ipa6_conn_extended_s {
2578 	uint64_t	ac6x_flags;
2579 	ipa6_conn_t	ac6x_conn;
2580 } ipa6_conn_x_t;
2581 
2582 /* flag values for ipa_conn_x_t and ipa6_conn_x_t. */
2583 #define	ACX_VERIFY_DST	0x1ULL	/* verify destination address is reachable */
2584 
2585 /* Name/Value Descriptor. */
2586 typedef struct nv_s {
2587 	uint64_t nv_value;
2588 	char	*nv_name;
2589 } nv_t;
2590 
2591 /* IP Forwarding Ticket */
2592 typedef	struct ipftk_s {
2593 	queue_t	*ipftk_queue;
2594 	ipaddr_t ipftk_dst;
2595 } ipftk_t;
2596 
2597 typedef struct ipt_s {
2598 	pfv_t	func;		/* Routine to call */
2599 	uchar_t	*arg;		/* ire or nce passed in */
2600 } ipt_t;
2601 
2602 #define	ILL_FRAG_HASH(s, i) \
2603 	((ntohl(s) ^ ((i) ^ ((i) >> 8))) % ILL_FRAG_HASH_TBL_COUNT)
2604 
2605 /*
2606  * The MAX number of allowed fragmented packets per hash bucket
2607  * calculation is based on the most common mtu size of 1500. This limit
2608  * will work well for other mtu sizes as well.
2609  */
2610 #define	COMMON_IP_MTU 1500
2611 #define	MAX_FRAG_MIN 10
2612 #define	MAX_FRAG_PKTS	\
2613 	MAX(MAX_FRAG_MIN, (2 * (ip_reass_queue_bytes / \
2614 	    (COMMON_IP_MTU * ILL_FRAG_HASH_TBL_COUNT))))
2615 
2616 /*
2617  * Maximum dups allowed per packet.
2618  */
2619 extern uint_t ip_max_frag_dups;
2620 
2621 /*
2622  * Per-packet information for received packets and transmitted.
2623  * Used by the transport protocols when converting between the packet
2624  * and ancillary data and socket options.
2625  *
2626  * Note: This private data structure and related IPPF_* constant
2627  * definitions are exposed to enable compilation of some debugging tools
2628  * like lsof which use struct tcp_t in <inet/tcp.h>. This is intended to be
2629  * a temporary hack and long term alternate interfaces should be defined
2630  * to support the needs of such tools and private definitions moved to
2631  * private headers.
2632  */
2633 struct ip6_pkt_s {
2634 	uint_t		ipp_fields;		/* Which fields are valid */
2635 	uint_t		ipp_sticky_ignored;	/* sticky fields to ignore */
2636 	uint_t		ipp_ifindex;		/* pktinfo ifindex */
2637 	in6_addr_t	ipp_addr;		/* pktinfo src/dst addr */
2638 	uint_t		ipp_unicast_hops;	/* IPV6_UNICAST_HOPS */
2639 	uint_t		ipp_multicast_hops;	/* IPV6_MULTICAST_HOPS */
2640 	uint_t		ipp_hoplimit;		/* IPV6_HOPLIMIT */
2641 	uint_t		ipp_hopoptslen;
2642 	uint_t		ipp_rtdstoptslen;
2643 	uint_t		ipp_rthdrlen;
2644 	uint_t		ipp_dstoptslen;
2645 	uint_t		ipp_pathmtulen;
2646 	ip6_hbh_t	*ipp_hopopts;
2647 	ip6_dest_t	*ipp_rtdstopts;
2648 	ip6_rthdr_t	*ipp_rthdr;
2649 	ip6_dest_t	*ipp_dstopts;
2650 	struct ip6_mtuinfo *ipp_pathmtu;
2651 	in6_addr_t	ipp_nexthop;		/* Transmit only */
2652 	uint8_t		ipp_tclass;
2653 	int8_t		ipp_use_min_mtu;
2654 };
2655 typedef struct ip6_pkt_s ip6_pkt_t;
2656 
2657 extern void ip6_pkt_free(ip6_pkt_t *);	/* free storage inside ip6_pkt_t */
2658 
2659 /*
2660  * This structure is used to convey information from IP and the ULP.
2661  * Currently used for the IP_RECVSLLA and IP_RECVIF options. The
2662  * type of information field is set to IN_PKTINFO (i.e inbound pkt info)
2663  */
2664 typedef struct in_pktinfo {
2665 	uint32_t		in_pkt_ulp_type;	/* type of info sent */
2666 							/* to UDP */
2667 	uint32_t		in_pkt_flags;	/* what is sent up by IP */
2668 	uint32_t		in_pkt_ifindex;	/* inbound interface index */
2669 	struct sockaddr_dl	in_pkt_slla;	/* has source link layer addr */
2670 } in_pktinfo_t;
2671 
2672 /*
2673  * flags to tell UDP what IP is sending; in_pkt_flags
2674  */
2675 #define	IPF_RECVIF	0x01	/* inbound interface index */
2676 #define	IPF_RECVSLLA	0x02	/* source link layer address */
2677 
2678 /* ipp_fields values */
2679 #define	IPPF_IFINDEX	0x0001	/* Part of in6_pktinfo: ifindex */
2680 #define	IPPF_ADDR	0x0002	/* Part of in6_pktinfo: src/dst addr */
2681 #define	IPPF_SCOPE_ID	0x0004	/* Add xmit ip6i_t for sin6_scope_id */
2682 #define	IPPF_NO_CKSUM	0x0008	/* Add xmit ip6i_t for IP6I_NO_*_CKSUM */
2683 
2684 #define	IPPF_RAW_CKSUM	0x0010	/* Add xmit ip6i_t for IP6I_RAW_CHECKSUM */
2685 #define	IPPF_HOPLIMIT	0x0020
2686 #define	IPPF_HOPOPTS	0x0040
2687 #define	IPPF_RTHDR	0x0080
2688 
2689 #define	IPPF_RTDSTOPTS	0x0100
2690 #define	IPPF_DSTOPTS	0x0200
2691 #define	IPPF_NEXTHOP	0x0400
2692 #define	IPPF_PATHMTU	0x0800
2693 
2694 #define	IPPF_TCLASS	0x1000
2695 #define	IPPF_DONTFRAG	0x2000
2696 #define	IPPF_USE_MIN_MTU	0x04000
2697 #define	IPPF_MULTICAST_HOPS	0x08000
2698 #define	IPPF_UNICAST_HOPS	0x10000
2699 
2700 #define	IPPF_HAS_IP6I \
2701 	(IPPF_IFINDEX|IPPF_ADDR|IPPF_NEXTHOP|IPPF_SCOPE_ID| \
2702 	IPPF_NO_CKSUM|IPPF_RAW_CKSUM|IPPF_HOPLIMIT|IPPF_DONTFRAG| \
2703 	IPPF_USE_MIN_MTU|IPPF_MULTICAST_HOPS|IPPF_UNICAST_HOPS)
2704 
2705 #define	TCP_PORTS_OFFSET	0
2706 #define	UDP_PORTS_OFFSET	0
2707 
2708 /*
2709  * lookups return the ill/ipif only if the flags are clear OR Iam writer.
2710  * ill / ipif lookup functions increment the refcnt on the ill / ipif only
2711  * after calling these macros. This ensures that the refcnt on the ipif or
2712  * ill will eventually drop down to zero.
2713  */
2714 #define	ILL_LOOKUP_FAILED	1	/* Used as error code */
2715 #define	IPIF_LOOKUP_FAILED	2	/* Used as error code */
2716 
2717 #define	ILL_CAN_LOOKUP(ill) 						\
2718 	(!((ill)->ill_state_flags & (ILL_CONDEMNED | ILL_CHANGING)) ||	\
2719 	IAM_WRITER_ILL(ill))
2720 
2721 #define	ILL_CAN_WAIT(ill, q)	\
2722 	(((q) != NULL) && !((ill)->ill_state_flags & (ILL_CONDEMNED)))
2723 
2724 #define	ILL_CAN_LOOKUP_WALKER(ill)	\
2725 	(!((ill)->ill_state_flags & ILL_CONDEMNED))
2726 
2727 #define	IPIF_CAN_LOOKUP(ipif)						\
2728 	(!((ipif)->ipif_state_flags & (IPIF_CONDEMNED | IPIF_CHANGING)) || \
2729 	IAM_WRITER_IPIF(ipif))
2730 
2731 /*
2732  * If the parameter 'q' is NULL, the caller is not interested in wait and
2733  * restart of the operation if the ILL or IPIF cannot be looked up when it is
2734  * marked as 'CHANGING'. Typically a thread that tries to send out data  will
2735  * end up passing NULLs as the last 4 parameters to ill_lookup_on_ifindex and
2736  * in this case 'q' is NULL
2737  */
2738 #define	IPIF_CAN_WAIT(ipif, q)	\
2739 	(((q) != NULL) && !((ipif)->ipif_state_flags & (IPIF_CONDEMNED)))
2740 
2741 #define	IPIF_CAN_LOOKUP_WALKER(ipif)					\
2742 	(!((ipif)->ipif_state_flags & (IPIF_CONDEMNED)) ||		\
2743 	IAM_WRITER_IPIF(ipif))
2744 
2745 /*
2746  * These macros are used by critical set ioctls and failover ioctls to
2747  * mark the ipif appropriately before starting the operation and to clear the
2748  * marks after completing the operation.
2749  */
2750 #define	IPIF_UNMARK_MOVING(ipif)                                \
2751 	(ipif)->ipif_state_flags &= ~IPIF_MOVING & ~IPIF_CHANGING;
2752 
2753 #define	ILL_UNMARK_CHANGING(ill)                                \
2754 	(ill)->ill_state_flags &= ~ILL_CHANGING;
2755 
2756 /* Macros used to assert that this thread is a writer  */
2757 #define	IAM_WRITER_IPSQ(ipsq)	((ipsq)->ipsq_writer == curthread)
2758 #define	IAM_WRITER_ILL(ill)					\
2759 	((ill)->ill_phyint->phyint_ipsq->ipsq_writer == curthread)
2760 #define	IAM_WRITER_IPIF(ipif)					\
2761 	((ipif)->ipif_ill->ill_phyint->phyint_ipsq->ipsq_writer == curthread)
2762 
2763 /*
2764  * Grab ill locks in the proper order. The order is highest addressed
2765  * ill is locked first.
2766  */
2767 #define	GRAB_ILL_LOCKS(ill_1, ill_2)				\
2768 {								\
2769 	if ((ill_1) > (ill_2)) {				\
2770 		if (ill_1 != NULL)				\
2771 			mutex_enter(&(ill_1)->ill_lock);	\
2772 		if (ill_2 != NULL)				\
2773 			mutex_enter(&(ill_2)->ill_lock);	\
2774 	} else {						\
2775 		if (ill_2 != NULL)				\
2776 			mutex_enter(&(ill_2)->ill_lock);	\
2777 		if (ill_1 != NULL && ill_1 != ill_2)		\
2778 			mutex_enter(&(ill_1)->ill_lock);	\
2779 	}							\
2780 }
2781 
2782 #define	RELEASE_ILL_LOCKS(ill_1, ill_2)		\
2783 {						\
2784 	if (ill_1 != NULL)			\
2785 		mutex_exit(&(ill_1)->ill_lock);	\
2786 	if (ill_2 != NULL && ill_2 != ill_1)	\
2787 		mutex_exit(&(ill_2)->ill_lock);	\
2788 }
2789 
2790 /* Get the other protocol instance ill */
2791 #define	ILL_OTHER(ill)						\
2792 	((ill)->ill_isv6 ? (ill)->ill_phyint->phyint_illv4 :	\
2793 	    (ill)->ill_phyint->phyint_illv6)
2794 
2795 #define	MATCH_V4_ONLY	0x1
2796 #define	MATCH_V6_ONLY	0x2
2797 #define	MATCH_ILL_ONLY	0x4
2798 
2799 /* ioctl command info: Ioctl properties extracted and stored in here */
2800 typedef struct cmd_info_s
2801 {
2802 	char    ci_groupname[LIFNAMSIZ + 1];	/* SIOCSLIFGROUPNAME */
2803 	ipif_t  *ci_ipif;	/* ipif associated with [l]ifreq ioctl's */
2804 	sin_t	*ci_sin;	/* the sin struct passed down */
2805 	sin6_t	*ci_sin6;	/* the sin6_t struct passed down */
2806 	struct lifreq *ci_lifr;	/* the lifreq struct passed down */
2807 } cmd_info_t;
2808 
2809 extern krwlock_t ill_g_lock;
2810 extern kmutex_t ip_addr_avail_lock;
2811 extern ipsq_t	*ipsq_g_head;
2812 
2813 extern ill_t	*ip_timer_ill;		/* ILL for IRE expiration timer. */
2814 extern timeout_id_t ip_ire_expire_id;	/* IRE expiration timeout id. */
2815 extern timeout_id_t ip_ire_reclaim_id;	/* IRE recalaim timeout id. */
2816 
2817 extern kmutex_t	ip_mi_lock;
2818 extern krwlock_t ip_g_nd_lock;		/* For adding/removing nd variables */
2819 extern kmutex_t ip_trash_timer_lock;	/* Protects ip_ire_expire_id */
2820 
2821 extern kmutex_t igmp_timer_lock;	/* Protects the igmp timer */
2822 extern kmutex_t mld_timer_lock;		/* Protects the mld timer */
2823 
2824 extern krwlock_t ill_g_usesrc_lock;	/* Protects usesrc related fields */
2825 
2826 extern struct kmem_cache *ire_cache;
2827 
2828 extern uint_t	ip_redirect_cnt;	/* Num of redirect routes in ftable */
2829 
2830 extern ipaddr_t	ip_g_all_ones;
2831 extern caddr_t	ip_g_nd;		/* Named Dispatch List Head */
2832 
2833 extern uint_t	ip_loopback_mtu;
2834 
2835 extern ipparam_t	*ip_param_arr;
2836 
2837 extern int ip_g_forward;
2838 extern int ipv6_forward;
2839 extern vmem_t *ip_minor_arena;
2840 
2841 #define	ip_respond_to_address_mask_broadcast ip_param_arr[0].ip_param_value
2842 #define	ip_g_resp_to_echo_bcast		ip_param_arr[1].ip_param_value
2843 #define	ip_g_resp_to_echo_mcast		ip_param_arr[2].ip_param_value
2844 #define	ip_g_resp_to_timestamp		ip_param_arr[3].ip_param_value
2845 #define	ip_g_resp_to_timestamp_bcast	ip_param_arr[4].ip_param_value
2846 #define	ip_g_send_redirects		ip_param_arr[5].ip_param_value
2847 #define	ip_g_forward_directed_bcast	ip_param_arr[6].ip_param_value
2848 #define	ip_debug			ip_param_arr[7].ip_param_value
2849 #define	ip_mrtdebug			ip_param_arr[8].ip_param_value
2850 #define	ip_timer_interval		ip_param_arr[9].ip_param_value
2851 #define	ip_ire_arp_interval		ip_param_arr[10].ip_param_value
2852 #define	ip_ire_redir_interval		ip_param_arr[11].ip_param_value
2853 #define	ip_def_ttl			ip_param_arr[12].ip_param_value
2854 #define	ip_forward_src_routed		ip_param_arr[13].ip_param_value
2855 #define	ip_wroff_extra			ip_param_arr[14].ip_param_value
2856 #define	ip_ire_pathmtu_interval		ip_param_arr[15].ip_param_value
2857 #define	ip_icmp_return			ip_param_arr[16].ip_param_value
2858 #define	ip_path_mtu_discovery		ip_param_arr[17].ip_param_value
2859 #define	ip_ignore_delete_time		ip_param_arr[18].ip_param_value
2860 #define	ip_ignore_redirect		ip_param_arr[19].ip_param_value
2861 #define	ip_output_queue			ip_param_arr[20].ip_param_value
2862 #define	ip_broadcast_ttl		ip_param_arr[21].ip_param_value
2863 #define	ip_icmp_err_interval		ip_param_arr[22].ip_param_value
2864 #define	ip_icmp_err_burst		ip_param_arr[23].ip_param_value
2865 #define	ip_reass_queue_bytes		ip_param_arr[24].ip_param_value
2866 #define	ip_strict_dst_multihoming	ip_param_arr[25].ip_param_value
2867 #define	ip_addrs_per_if			ip_param_arr[26].ip_param_value
2868 #define	ipsec_override_persocket_policy	ip_param_arr[27].ip_param_value
2869 #define	icmp_accept_clear_messages	ip_param_arr[28].ip_param_value
2870 #define	igmp_accept_clear_messages	ip_param_arr[29].ip_param_value
2871 
2872 /* IPv6 configuration knobs */
2873 #define	delay_first_probe_time		ip_param_arr[30].ip_param_value
2874 #define	max_unicast_solicit		ip_param_arr[31].ip_param_value
2875 #define	ipv6_def_hops			ip_param_arr[32].ip_param_value
2876 #define	ipv6_icmp_return		ip_param_arr[33].ip_param_value
2877 #define	ipv6_forward_src_routed		ip_param_arr[34].ip_param_value
2878 #define	ipv6_resp_echo_mcast		ip_param_arr[35].ip_param_value
2879 #define	ipv6_send_redirects		ip_param_arr[36].ip_param_value
2880 #define	ipv6_ignore_redirect		ip_param_arr[37].ip_param_value
2881 #define	ipv6_strict_dst_multihoming	ip_param_arr[38].ip_param_value
2882 #define	ip_ire_reclaim_fraction		ip_param_arr[39].ip_param_value
2883 #define	ipsec_policy_log_interval	ip_param_arr[40].ip_param_value
2884 #define	pim_accept_clear_messages	ip_param_arr[41].ip_param_value
2885 #define	ip_ndp_unsolicit_interval	ip_param_arr[42].ip_param_value
2886 #define	ip_ndp_unsolicit_count		ip_param_arr[43].ip_param_value
2887 #define	ipv6_ignore_home_address_opt	ip_param_arr[44].ip_param_value
2888 #define	ip_policy_mask			ip_param_arr[45].ip_param_value
2889 #define	ip_multirt_resolution_interval	ip_param_arr[46].ip_param_value
2890 #define	ip_multirt_ttl			ip_param_arr[47].ip_param_value
2891 #define	ip_multidata_outbound		ip_param_arr[48].ip_param_value
2892 #define	ip_ndp_defense_interval		ip_param_arr[49].ip_param_value
2893 #define	ip_max_temp_idle		ip_param_arr[50].ip_param_value
2894 #define	ip_max_temp_defend		ip_param_arr[51].ip_param_value
2895 #define	ip_max_defend			ip_param_arr[52].ip_param_value
2896 #define	ip_defend_interval		ip_param_arr[53].ip_param_value
2897 #define	ip_dup_recovery			ip_param_arr[54].ip_param_value
2898 #define	ip_restrict_interzone_loopback	ip_param_arr[55].ip_param_value
2899 #ifdef DEBUG
2900 #define	ipv6_drop_inbound_icmpv6	ip_param_arr[56].ip_param_value
2901 #else
2902 #define	ipv6_drop_inbound_icmpv6	0
2903 #endif
2904 
2905 extern hrtime_t	ipsec_policy_failure_last;
2906 
2907 extern int	dohwcksum;	/* use h/w cksum if supported by the h/w */
2908 #ifdef ZC_TEST
2909 extern int	noswcksum;
2910 #endif
2911 
2912 extern char	ipif_loopback_name[];
2913 
2914 extern nv_t	*ire_nv_tbl;
2915 
2916 extern time_t	ip_g_frag_timeout;
2917 extern clock_t	ip_g_frag_timo_ms;
2918 
2919 extern mib2_ip_t	ip_mib;	/* For tcpInErrs and udpNoPorts */
2920 
2921 extern struct module_info ip_mod_info;
2922 
2923 extern timeout_id_t	igmp_slowtimeout_id;
2924 extern timeout_id_t	mld_slowtimeout_id;
2925 
2926 extern uint_t	loopback_packets;
2927 
2928 /*
2929  * Hooks structures used inside of ip
2930  */
2931 extern hook_event_token_t	ipv4firewall_physical_in;
2932 extern hook_event_token_t	ipv4firewall_physical_out;
2933 extern hook_event_token_t	ipv4firewall_forwarding;
2934 extern hook_event_token_t	ipv4firewall_loopback_in;
2935 extern hook_event_token_t	ipv4firewall_loopback_out;
2936 extern hook_event_token_t	ipv4nicevents;
2937 
2938 extern hook_event_token_t	ipv6firewall_physical_in;
2939 extern hook_event_token_t	ipv6firewall_physical_out;
2940 extern hook_event_token_t	ipv6firewall_forwarding;
2941 extern hook_event_token_t	ipv6firewall_loopback_in;
2942 extern hook_event_token_t	ipv6firewall_loopback_out;
2943 extern hook_event_token_t	ipv6nicevents;
2944 
2945 extern hook_event_t	ip4_physical_in_event;
2946 extern hook_event_t	ip4_physical_out_event;
2947 extern hook_event_t	ip4_forwarding_event;
2948 extern hook_event_t	ip4_loopback_in_event;
2949 extern hook_event_t	ip4_loopback_out_event;
2950 extern hook_event_t	ip4_nic_events;
2951 
2952 extern hook_event_t	ip6_physical_in_event;
2953 extern hook_event_t	ip6_physical_out_event;
2954 extern hook_event_t	ip6_forwarding_event;
2955 extern hook_event_t	ip6_loopback_in_event;
2956 extern hook_event_t	ip6_loopback_out_event;
2957 extern hook_event_t	ip6_nic_events;
2958 
2959 #define	HOOKS4_INTERESTED_PHYSICAL_IN	\
2960 	(ip4_physical_in_event.he_interested)
2961 #define	HOOKS6_INTERESTED_PHYSICAL_IN	\
2962 	(ip6_physical_in_event.he_interested)
2963 #define	HOOKS4_INTERESTED_PHYSICAL_OUT	\
2964 	(ip4_physical_out_event.he_interested)
2965 #define	HOOKS6_INTERESTED_PHYSICAL_OUT	\
2966 	(ip6_physical_out_event.he_interested)
2967 #define	HOOKS4_INTERESTED_FORWARDING	\
2968 	(ip4_forwarding_event.he_interested)
2969 #define	HOOKS6_INTERESTED_FORWARDING	\
2970 	(ip6_forwarding_event.he_interested)
2971 #define	HOOKS4_INTERESTED_LOOPBACK_IN	\
2972 	(ip4_loopback_in_event.he_interested)
2973 #define	HOOKS6_INTERESTED_LOOPBACK_IN	\
2974 	(ip6_loopback_in_event.he_interested)
2975 #define	HOOKS4_INTERESTED_LOOPBACK_OUT	\
2976 	(ip4_loopback_out_event.he_interested)
2977 #define	HOOKS6_INTERESTED_LOOPBACK_OUT	\
2978 	(ip6_loopback_out_event.he_interested)
2979 
2980 /*
2981  * Hooks marcos used inside of ip
2982  */
2983 #define	IPHA_VHL	ipha_version_and_hdr_length
2984 
2985 #define	FW_HOOKS(_hook, _event, _flag, _ilp, _olp, _iph, _fm, _m)	\
2986 									\
2987 	if ((_hook).he_interested) {	\
2988 		hook_pkt_event_t info;					\
2989 									\
2990 		_NOTE(CONSTCOND)					\
2991 		ASSERT((_ilp != NULL) || (_olp != NULL));		\
2992 									\
2993 		_NOTE(CONSTCOND)					\
2994 		if ((_ilp != NULL) &&					\
2995 		    (((ill_t *)(_ilp))->ill_phyint != NULL))		\
2996 			info.hpe_ifp = (phy_if_t)((ill_t *)		\
2997 			    (_ilp))->ill_phyint->phyint_ifindex;	\
2998 		else							\
2999 			info.hpe_ifp = 0;				\
3000 									\
3001 		_NOTE(CONSTCOND)					\
3002 		if ((_olp != NULL) &&					\
3003 		    (((ill_t *)(_olp))->ill_phyint != NULL))		\
3004 			info.hpe_ofp = (phy_if_t)((ill_t *)		\
3005 			    (_olp))->ill_phyint->phyint_ifindex;	\
3006 		else							\
3007 			info.hpe_ofp = 0;				\
3008 		info.hpe_hdr = _iph;					\
3009 		info.hpe_mp = &(_fm);					\
3010 		info.hpe_mb = _m;					\
3011 		if (hook_run(_event, (hook_data_t)&info) != 0) {	\
3012 			ip2dbg(("%s hook dropped mblk chain %p hdr %p\n",\
3013 			    (_hook).he_name, (void *)_fm, (void *)_m));	\
3014 			if (_fm != NULL) {				\
3015 				freemsg(_fm);				\
3016 				_fm = NULL;				\
3017 			}						\
3018 			_iph = NULL;					\
3019 			_m = NULL;					\
3020 		} else {						\
3021 			_iph = info.hpe_hdr;				\
3022 			_m = info.hpe_mb;				\
3023 		}							\
3024 	}
3025 
3026 #define	FW_HOOKS6(_hook, _event, _flag, _ilp, _olp, _iph, _fm, _m)	\
3027 									\
3028 	if ((_hook).he_interested) {	\
3029 		hook_pkt_event_t info;					\
3030 									\
3031 		_NOTE(CONSTCOND)					\
3032 		ASSERT((_ilp != NULL) || (_olp != NULL));		\
3033 									\
3034 		_NOTE(CONSTCOND)					\
3035 		if ((_ilp != NULL) &&					\
3036 		    (((ill_t *)(_ilp))->ill_phyint != NULL))		\
3037 			info.hpe_ifp = (phy_if_t)((ill_t *)		\
3038 			    (_ilp))->ill_phyint->phyint_ifindex;	\
3039 		else							\
3040 			info.hpe_ifp = 0;				\
3041 									\
3042 		_NOTE(CONSTCOND)					\
3043 		if ((_olp != NULL) &&					\
3044 		    (((ill_t *)(_olp))->ill_phyint != NULL))		\
3045 			info.hpe_ofp = (phy_if_t)((ill_t *)		\
3046 			    (_olp))->ill_phyint->phyint_ifindex;	\
3047 		else							\
3048 			info.hpe_ofp = 0;				\
3049 		info.hpe_hdr = _iph;					\
3050 		info.hpe_mp = &(_fm);					\
3051 		info.hpe_mb = _m;					\
3052 		if (hook_run(_event, (hook_data_t)&info) != 0) {	\
3053 			ip2dbg(("%s hook dropped mblk chain %p hdr %p\n",\
3054 			    (_hook).he_name, (void *)_fm, (void *)_m));	\
3055 			if (_fm != NULL) {				\
3056 				freemsg(_fm);				\
3057 				_fm = NULL;				\
3058 			}						\
3059 			_iph = NULL;					\
3060 			_m = NULL;					\
3061 		} else {						\
3062 			_iph = info.hpe_hdr;				\
3063 			_m = info.hpe_mb;				\
3064 		}							\
3065 	}
3066 
3067 /*
3068  * Network byte order macros
3069  */
3070 #ifdef	_BIG_ENDIAN
3071 #define	N_IN_CLASSD_NET		IN_CLASSD_NET
3072 #define	N_INADDR_UNSPEC_GROUP	INADDR_UNSPEC_GROUP
3073 #else /* _BIG_ENDIAN */
3074 #define	N_IN_CLASSD_NET		(ipaddr_t)0x000000f0U
3075 #define	N_INADDR_UNSPEC_GROUP	(ipaddr_t)0x000000e0U
3076 #endif /* _BIG_ENDIAN */
3077 #define	CLASSD(addr)	(((addr) & N_IN_CLASSD_NET) == N_INADDR_UNSPEC_GROUP)
3078 
3079 #define	IP_LOOPBACK_ADDR(addr)			\
3080 	((ntohl(addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)
3081 
3082 #ifdef DEBUG
3083 /* IPsec HW acceleration debugging support */
3084 
3085 #define	IPSECHW_CAPAB		0x0001	/* capability negotiation */
3086 #define	IPSECHW_SADB		0x0002	/* SADB exchange */
3087 #define	IPSECHW_PKT		0x0004	/* general packet flow */
3088 #define	IPSECHW_PKTIN		0x0008	/* driver in pkt processing details */
3089 #define	IPSECHW_PKTOUT		0x0010	/* driver out pkt processing details */
3090 
3091 #define	IPSECHW_DEBUG(f, x)	if (ipsechw_debug & (f)) { (void) printf x; }
3092 #define	IPSECHW_CALL(f, r, x)	if (ipsechw_debug & (f)) { (void) r x; }
3093 
3094 extern uint32_t ipsechw_debug;
3095 #else
3096 #define	IPSECHW_DEBUG(f, x)	{}
3097 #define	IPSECHW_CALL(f, r, x)	{}
3098 #endif
3099 
3100 #ifdef IP_DEBUG
3101 #include <sys/debug.h>
3102 #include <sys/promif.h>
3103 
3104 #define	ip0dbg(a)	printf a
3105 #define	ip1dbg(a)	if (ip_debug > 2) printf a
3106 #define	ip2dbg(a)	if (ip_debug > 3) printf a
3107 #define	ip3dbg(a)	if (ip_debug > 4) printf a
3108 #else
3109 #define	ip0dbg(a)	/* */
3110 #define	ip1dbg(a)	/* */
3111 #define	ip2dbg(a)	/* */
3112 #define	ip3dbg(a)	/* */
3113 #endif	/* IP_DEBUG */
3114 
3115 /* Default MAC-layer address string length for mac_colon_addr */
3116 #define	MAC_STR_LEN	128
3117 
3118 struct	ipsec_out_s;
3119 
3120 struct	mac_header_info_s;
3121 
3122 extern const char *dlpi_prim_str(int);
3123 extern const char *dlpi_err_str(int);
3124 extern void	ill_frag_timer(void *);
3125 extern ill_t	*ill_first(int, int, ill_walk_context_t *);
3126 extern ill_t	*ill_next(ill_walk_context_t *, ill_t *);
3127 extern void	ill_frag_timer_start(ill_t *);
3128 extern mblk_t	*ip_carve_mp(mblk_t **, ssize_t);
3129 extern mblk_t	*ip_dlpi_alloc(size_t, t_uscalar_t);
3130 extern char	*ip_dot_addr(ipaddr_t, char *);
3131 extern const char *mac_colon_addr(const uint8_t *, size_t, char *, size_t);
3132 extern void	ip_lwput(queue_t *, mblk_t *);
3133 extern boolean_t icmp_err_rate_limit(void);
3134 extern void	icmp_time_exceeded(queue_t *, mblk_t *, uint8_t, zoneid_t);
3135 extern void	icmp_unreachable(queue_t *, mblk_t *, uint8_t, zoneid_t);
3136 extern mblk_t	*ip_add_info(mblk_t *, ill_t *, uint_t);
3137 extern mblk_t	*ip_bind_v4(queue_t *, mblk_t *, conn_t *);
3138 extern int	ip_bind_connected(conn_t *, mblk_t *, ipaddr_t *, uint16_t,
3139     ipaddr_t, uint16_t, boolean_t, boolean_t, boolean_t,
3140     boolean_t);
3141 extern boolean_t ip_bind_ipsec_policy_set(conn_t *, mblk_t *);
3142 extern int	ip_bind_laddr(conn_t *, mblk_t *, ipaddr_t, uint16_t,
3143     boolean_t, boolean_t, boolean_t);
3144 extern uint_t	ip_cksum(mblk_t *, int, uint32_t);
3145 extern int	ip_close(queue_t *, int);
3146 extern uint16_t	ip_csum_hdr(ipha_t *);
3147 extern void	ip_proto_not_sup(queue_t *, mblk_t *, uint_t, zoneid_t);
3148 extern void	ip_ire_fini(void);
3149 extern void	ip_ire_init(void);
3150 extern int	ip_open(queue_t *, dev_t *, int, int, cred_t *);
3151 extern int	ip_reassemble(mblk_t *, ipf_t *, uint_t, boolean_t, ill_t *,
3152     size_t);
3153 extern int	ip_opt_set_ill(conn_t *, int, boolean_t, boolean_t,
3154     int, int, mblk_t *);
3155 extern void	ip_rput(queue_t *, mblk_t *);
3156 extern void	ip_input(ill_t *, ill_rx_ring_t *, mblk_t *,
3157     struct mac_header_info_s *);
3158 extern void	ip_rput_dlpi(queue_t *, mblk_t *);
3159 extern void	ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *);
3160 extern void	ip_rput_forward_multicast(ipaddr_t, mblk_t *, ipif_t *);
3161 
3162 extern int	ip_snmpmod_close(queue_t *);
3163 extern void	ip_snmpmod_wput(queue_t *, mblk_t *);
3164 extern void	ip_udp_input(queue_t *, mblk_t *, ipha_t *, ire_t *, ill_t *);
3165 extern void	ip_proto_input(queue_t *, mblk_t *, ipha_t *, ire_t *, ill_t *);
3166 extern void	ip_rput_other(ipsq_t *, queue_t *, mblk_t *, void *);
3167 extern void	ip_setqinfo(queue_t *, minor_t, boolean_t);
3168 extern void	ip_trash_ire_reclaim(void *);
3169 extern void	ip_trash_timer_expire(void *);
3170 extern void	ip_wput(queue_t *, mblk_t *);
3171 extern void	ip_output(void *, mblk_t *, void *, int);
3172 extern void	ip_wput_md(queue_t *, mblk_t *, conn_t *);
3173 
3174 extern void	ip_wput_ire(queue_t *, mblk_t *, ire_t *, conn_t *, int,
3175 		    zoneid_t);
3176 extern void	ip_wput_local(queue_t *, ill_t *, ipha_t *, mblk_t *, ire_t *,
3177 		    int, zoneid_t);
3178 extern void	ip_wput_multicast(queue_t *, mblk_t *, ipif_t *, zoneid_t);
3179 extern void	ip_wput_nondata(ipsq_t *, queue_t *, mblk_t *, void *);
3180 extern void	ip_wsrv(queue_t *);
3181 extern char	*ip_nv_lookup(nv_t *, int);
3182 extern boolean_t ip_local_addr_ok_v6(const in6_addr_t *, const in6_addr_t *);
3183 extern boolean_t ip_remote_addr_ok_v6(const in6_addr_t *, const in6_addr_t *);
3184 extern ipaddr_t ip_massage_options(ipha_t *);
3185 extern ipaddr_t ip_net_mask(ipaddr_t);
3186 extern void	ip_newroute(queue_t *, mblk_t *, ipaddr_t, ill_t *, conn_t *,
3187 		    zoneid_t);
3188 extern ipxmit_state_t	ip_xmit_v4(mblk_t *, ire_t *, struct ipsec_out_s *,
3189     boolean_t);
3190 extern int	ip_hdr_complete(ipha_t *, zoneid_t);
3191 
3192 extern struct qinit rinit_ipv6;
3193 extern struct qinit winit_ipv6;
3194 extern struct qinit rinit_tcp;
3195 extern struct qinit rinit_tcp6;
3196 extern struct qinit winit_tcp;
3197 extern struct qinit rinit_acceptor_tcp;
3198 extern struct qinit winit_acceptor_tcp;
3199 
3200 extern net_data_t ipv4;
3201 extern net_data_t ipv6;
3202 
3203 extern void	conn_drain_insert(conn_t *connp);
3204 extern	int	conn_ipsec_length(conn_t *connp);
3205 extern void	ip_wput_ipsec_out(queue_t *, mblk_t *, ipha_t *, ill_t *,
3206     ire_t *);
3207 extern ipaddr_t	ip_get_dst(ipha_t *);
3208 extern int	ipsec_out_extra_length(mblk_t *);
3209 extern int	ipsec_in_extra_length(mblk_t *);
3210 extern mblk_t	*ipsec_in_alloc();
3211 extern boolean_t ipsec_in_is_secure(mblk_t *);
3212 extern void	ipsec_out_process(queue_t *, mblk_t *, ire_t *, uint_t);
3213 extern void	ipsec_out_to_in(mblk_t *);
3214 extern int	ill_forward_set(queue_t *, mblk_t *, boolean_t, caddr_t);
3215 extern void	ip_fanout_proto_again(mblk_t *, ill_t *, ill_t *, ire_t *);
3216 
3217 extern void	ire_cleanup(ire_t *);
3218 extern void	ire_inactive(ire_t *);
3219 extern boolean_t irb_inactive(irb_t *);
3220 extern ire_t	*ire_unlink(irb_t *);
3221 #ifdef IRE_DEBUG
3222 extern	void	ire_trace_ref(ire_t *ire);
3223 extern	void	ire_untrace_ref(ire_t *ire);
3224 extern	void	ire_thread_exit(ire_t *ire, caddr_t);
3225 #endif
3226 #ifdef ILL_DEBUG
3227 extern	void	ill_trace_cleanup(ill_t *);
3228 extern	void	ipif_trace_cleanup(ipif_t *);
3229 #endif
3230 
3231 extern int	ip_srcid_insert(const in6_addr_t *, zoneid_t);
3232 extern int	ip_srcid_remove(const in6_addr_t *, zoneid_t);
3233 extern void	ip_srcid_find_id(uint_t, in6_addr_t *, zoneid_t);
3234 extern uint_t	ip_srcid_find_addr(const in6_addr_t *, zoneid_t);
3235 extern int	ip_srcid_report(queue_t *, mblk_t *, caddr_t, cred_t *);
3236 
3237 extern uint8_t	ipoptp_next(ipoptp_t *);
3238 extern uint8_t	ipoptp_first(ipoptp_t *, ipha_t *);
3239 extern int	ip_opt_get_user(const ipha_t *, uchar_t *);
3240 extern ill_t	*ip_grab_attach_ill(ill_t *, mblk_t *, int, boolean_t);
3241 extern ire_t	*conn_set_outgoing_ill(conn_t *, ire_t *, ill_t **);
3242 extern int	ipsec_req_from_conn(conn_t *, ipsec_req_t *, int);
3243 extern int	ip_snmp_get(queue_t *q, mblk_t *mctl);
3244 extern int	ip_snmp_set(queue_t *q, int, int, uchar_t *, int);
3245 extern void	ip_process_ioctl(ipsq_t *, queue_t *, mblk_t *, void *);
3246 extern void	ip_quiesce_conn(conn_t *);
3247 extern  void    ip_reprocess_ioctl(ipsq_t *, queue_t *, mblk_t *, void *);
3248 extern void	ip_restart_optmgmt(ipsq_t *, queue_t *, mblk_t *, void *);
3249 extern void	ip_ioctl_finish(queue_t *, mblk_t *, int, int, ipif_t *,
3250     ipsq_t *);
3251 
3252 extern boolean_t ip_cmpbuf(const void *, uint_t, boolean_t, const void *,
3253     uint_t);
3254 extern boolean_t ip_allocbuf(void **, uint_t *, boolean_t, const void *,
3255     uint_t);
3256 extern void	ip_savebuf(void **, uint_t *, boolean_t, const void *, uint_t);
3257 
3258 extern boolean_t	ipsq_pending_mp_cleanup(ill_t *, conn_t *);
3259 extern void	conn_ioctl_cleanup(conn_t *);
3260 extern ill_t	*conn_get_held_ill(conn_t *, ill_t **, int *);
3261 extern ill_t	*ip_newroute_get_dst_ill(ill_t *);
3262 
3263 struct multidata_s;
3264 struct pdesc_s;
3265 
3266 extern mblk_t	*ip_mdinfo_alloc(ill_mdt_capab_t *);
3267 extern mblk_t	*ip_mdinfo_return(ire_t *, conn_t *, char *, ill_mdt_capab_t *);
3268 extern uint_t	ip_md_cksum(struct pdesc_s *, int, uint_t);
3269 extern boolean_t ip_md_addr_attr(struct multidata_s *, struct pdesc_s *,
3270 			const mblk_t *);
3271 extern boolean_t ip_md_hcksum_attr(struct multidata_s *, struct pdesc_s *,
3272 			uint32_t, uint32_t, uint32_t, uint32_t);
3273 extern boolean_t ip_md_zcopy_attr(struct multidata_s *, struct pdesc_s *,
3274 			uint_t);
3275 extern mblk_t	*ip_unbind(queue_t *, mblk_t *);
3276 
3277 extern void tnet_init(void);
3278 extern void tnet_fini(void);
3279 
3280 /* Hooks for CGTP (multirt routes) filtering module */
3281 #define	CGTP_FILTER_REV_1	1
3282 #define	CGTP_FILTER_REV_2	2
3283 #define	CGTP_FILTER_REV		CGTP_FILTER_REV_2
3284 
3285 /* cfo_filter, cfo_filter_fp, cfo_filter_v6 hooks return values */
3286 #define	CGTP_IP_PKT_NOT_CGTP	0
3287 #define	CGTP_IP_PKT_PREMIUM	1
3288 #define	CGTP_IP_PKT_DUPLICATE	2
3289 
3290 typedef struct cgtp_filter_ops {
3291 	int	cfo_filter_rev;
3292 	int	(*cfo_change_state)(int);
3293 	int	(*cfo_add_dest_v4)(ipaddr_t, ipaddr_t, ipaddr_t, ipaddr_t);
3294 	int	(*cfo_del_dest_v4)(ipaddr_t, ipaddr_t);
3295 	int	(*cfo_add_dest_v6)(in6_addr_t *, in6_addr_t *, in6_addr_t *,
3296 		    in6_addr_t *);
3297 	int	(*cfo_del_dest_v6)(in6_addr_t *, in6_addr_t *);
3298 	int	(*cfo_filter)(queue_t *, mblk_t *);
3299 	int	(*cfo_filter_fp)(queue_t *, mblk_t *);
3300 	int	(*cfo_filter_v6)(queue_t *, ip6_t *, ip6_frag_t *);
3301 } cgtp_filter_ops_t;
3302 
3303 #define	CGTP_MCAST_SUCCESS	1
3304 
3305 extern cgtp_filter_ops_t *ip_cgtp_filter_ops;
3306 extern boolean_t ip_cgtp_filter;
3307 
3308 extern int	ip_cgtp_filter_supported(void);
3309 extern int	ip_cgtp_filter_register(cgtp_filter_ops_t *);
3310 
3311 /* Flags for ire_multirt_lookup() */
3312 
3313 #define	MULTIRT_USESTAMP	0x0001
3314 #define	MULTIRT_SETSTAMP	0x0002
3315 #define	MULTIRT_CACHEGW		0x0004
3316 
3317 /* Debug stuff for multirt route resolution. */
3318 #if defined(DEBUG) && !defined(__lint)
3319 /* Our "don't send, rather drop" flag. */
3320 #define	MULTIRT_DEBUG_FLAG	0x8000
3321 
3322 #define	MULTIRT_TRACE(x)	ip2dbg(x)
3323 
3324 #define	MULTIRT_DEBUG_TAG(mblk)	\
3325 	do { \
3326 		ASSERT(mblk != NULL); \
3327 		MULTIRT_TRACE(("%s[%d]: tagging mblk %p, tag was %d\n", \
3328 		__FILE__, __LINE__, \
3329 		(void *)(mblk), (mblk)->b_flag & MULTIRT_DEBUG_FLAG)); \
3330 		(mblk)->b_flag |= MULTIRT_DEBUG_FLAG; \
3331 	} while (0)
3332 
3333 #define	MULTIRT_DEBUG_UNTAG(mblk) \
3334 	do { \
3335 		ASSERT(mblk != NULL); \
3336 		MULTIRT_TRACE(("%s[%d]: untagging mblk %p, tag was %d\n", \
3337 		__FILE__, __LINE__, \
3338 		(void *)(mblk), (mblk)->b_flag & MULTIRT_DEBUG_FLAG)); \
3339 		(mblk)->b_flag &= ~MULTIRT_DEBUG_FLAG; \
3340 	} while (0)
3341 
3342 #define	MULTIRT_DEBUG_TAGGED(mblk) \
3343 	(((mblk)->b_flag & MULTIRT_DEBUG_FLAG) ? B_TRUE : B_FALSE)
3344 #else
3345 #define	MULTIRT_DEBUG_TAG(mblk)		ASSERT(mblk != NULL)
3346 #define	MULTIRT_DEBUG_UNTAG(mblk)	ASSERT(mblk != NULL)
3347 #define	MULTIRT_DEBUG_TAGGED(mblk)	B_FALSE
3348 #endif
3349 
3350 /*
3351  * Per-ILL Multidata Transmit capabilities.
3352  */
3353 struct ill_mdt_capab_s {
3354 	uint_t ill_mdt_version;  /* interface version */
3355 	uint_t ill_mdt_on;	 /* on/off switch for MDT on this ILL */
3356 	uint_t ill_mdt_hdr_head; /* leading header fragment extra space */
3357 	uint_t ill_mdt_hdr_tail; /* trailing header fragment extra space */
3358 	uint_t ill_mdt_max_pld;	 /* maximum payload buffers per Multidata */
3359 	uint_t ill_mdt_span_limit; /* maximum payload span per packet */
3360 };
3361 
3362 struct ill_hcksum_capab_s {
3363 	uint_t	ill_hcksum_version;	/* interface version */
3364 	uint_t	ill_hcksum_txflags;	/* capabilities on transmit */
3365 };
3366 
3367 struct ill_zerocopy_capab_s {
3368 	uint_t	ill_zerocopy_version;	/* interface version */
3369 	uint_t	ill_zerocopy_flags;	/* capabilities */
3370 };
3371 
3372 /* Possible ill_states */
3373 #define	ILL_RING_INPROC		3	/* Being assigned to squeue */
3374 #define	ILL_RING_INUSE		2	/* Already Assigned to Rx Ring */
3375 #define	ILL_RING_BEING_FREED	1	/* Being Unassigned */
3376 #define	ILL_RING_FREE		0	/* Available to be assigned to Ring */
3377 
3378 #define	ILL_MAX_RINGS		256	/* Max num of rx rings we can manage */
3379 #define	ILL_POLLING		0x01	/* Polling in use */
3380 
3381 /*
3382  * These functions pointer types are exported by the mac/dls layer.
3383  * we need to duplicate the definitions here because we cannot
3384  * include mac/dls header files here.
3385  */
3386 typedef void	(*ip_mac_blank_t)(void *, time_t, uint_t);
3387 typedef void	(*ip_dld_tx_t)(void *, mblk_t *);
3388 
3389 typedef void	(*ip_dls_chg_soft_ring_t)(void *, int);
3390 typedef void	(*ip_dls_bind_t)(void *, processorid_t);
3391 typedef void	(*ip_dls_unbind_t)(void *);
3392 
3393 struct ill_rx_ring {
3394 	ip_mac_blank_t		rr_blank; /* Driver interrupt blanking func */
3395 	void			*rr_handle; /* Handle for Rx ring */
3396 	squeue_t		*rr_sqp; /* Squeue the ring is bound to */
3397 	ill_t			*rr_ill; /* back pointer to ill */
3398 	clock_t			rr_poll_time; /* Last lbolt polling was used */
3399 	uint32_t		rr_poll_state; /* polling state flags */
3400 	uint32_t		rr_max_blank_time; /* Max interrupt blank */
3401 	uint32_t		rr_min_blank_time; /* Min interrupt blank */
3402 	uint32_t		rr_max_pkt_cnt; /* Max pkts before interrupt */
3403 	uint32_t		rr_min_pkt_cnt; /* Mix pkts before interrupt */
3404 	uint32_t		rr_normal_blank_time; /* Normal intr freq */
3405 	uint32_t		rr_normal_pkt_cnt; /* Normal intr pkt cnt */
3406 	uint32_t		rr_ring_state; /* State of this ring */
3407 };
3408 
3409 struct ill_dls_capab_s {
3410 	ip_dld_tx_t		ill_tx;		/* Driver Tx routine */
3411 	void			*ill_tx_handle;	/* Driver Tx handle */
3412 	ip_dls_chg_soft_ring_t	ill_dls_change_status;
3413 						/* change soft ring fanout */
3414 	ip_dls_bind_t		ill_dls_bind;	/* to add CPU affinity */
3415 	ip_dls_unbind_t		ill_dls_unbind;	/* remove CPU affinity */
3416 	ill_rx_ring_t		*ill_ring_tbl; /* Ring to Sqp mapping table */
3417 	uint_t			ill_dls_soft_ring_cnt; /* Number of soft ring */
3418 	conn_t			*ill_unbind_conn; /* Conn used during unplumb */
3419 };
3420 
3421 /*
3422  * This message is sent by an upper-layer protocol to tell IP that it knows all
3423  * about labels and will construct them itself.  IP takes the slow path and
3424  * recomputes the label on every packet when this isn't true.
3425  */
3426 #define	IP_ULP_OUT_LABELED		(('O' << 8) + 'L')
3427 typedef struct out_labeled_s {
3428 	uint32_t	out_labeled_type;	/* OUT_LABELED */
3429 	queue_t		*out_qnext;		/* intermediate detection */
3430 } out_labeled_t;
3431 
3432 /*
3433  * IP squeues exports
3434  */
3435 extern int 		ip_squeue_profile;
3436 extern int 		ip_squeue_bind;
3437 extern boolean_t 	ip_squeue_fanout;
3438 extern boolean_t	ip_squeue_soft_ring;
3439 extern uint_t		ip_threads_per_cpu;
3440 extern uint_t		ip_squeues_per_cpu;
3441 extern uint_t		ip_soft_rings_cnt;
3442 
3443 typedef struct squeue_set_s {
3444 	kmutex_t	sqs_lock;
3445 	struct squeue_s	**sqs_list;
3446 	int		sqs_size;
3447 	int		sqs_max_size;
3448 	processorid_t	sqs_bind;
3449 } squeue_set_t;
3450 
3451 #define	IP_SQUEUE_GET(hint) 						\
3452 	((!ip_squeue_fanout) ?	(CPU->cpu_squeue_set->sqs_list[0]) :	\
3453 		ip_squeue_random(hint))
3454 
3455 typedef void (*squeue_func_t)(squeue_t *, mblk_t *, sqproc_t, void *, uint8_t);
3456 
3457 extern void ip_squeue_init(void (*)(squeue_t *));
3458 extern squeue_t	*ip_squeue_random(uint_t);
3459 extern squeue_t *ip_squeue_get(ill_rx_ring_t *);
3460 extern void ip_squeue_get_pkts(squeue_t *);
3461 extern int ip_squeue_bind_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *);
3462 extern int ip_squeue_bind_get(queue_t *, mblk_t *, caddr_t, cred_t *);
3463 extern void ip_squeue_clean(void *, mblk_t *, void *);
3464 extern void ip_resume_tcp_bind(void *, mblk_t *, void *);
3465 extern void	ip_soft_ring_assignment(ill_t *, ill_rx_ring_t *,
3466     mblk_t *, struct mac_header_info_s *);
3467 
3468 extern void tcp_wput(queue_t *, mblk_t *);
3469 
3470 extern int	ip_fill_mtuinfo(struct in6_addr *, in_port_t,
3471 	struct ip6_mtuinfo *);
3472 extern	ipif_t *conn_get_held_ipif(conn_t *, ipif_t **, int *);
3473 
3474 typedef void    (*ipsq_func_t)(ipsq_t *, queue_t *, mblk_t *, void *);
3475 
3476 /*
3477  * Squeue tags. Tags only need to be unique when the callback function is the
3478  * same to distinguish between different calls, but we use unique tags for
3479  * convenience anyway.
3480  */
3481 #define	SQTAG_IP_INPUT			1
3482 #define	SQTAG_TCP_INPUT_ICMP_ERR	2
3483 #define	SQTAG_TCP6_INPUT_ICMP_ERR	3
3484 #define	SQTAG_IP_TCP_INPUT		4
3485 #define	SQTAG_IP6_TCP_INPUT		5
3486 #define	SQTAG_IP_TCP_CLOSE		6
3487 #define	SQTAG_TCP_OUTPUT		7
3488 #define	SQTAG_TCP_TIMER			8
3489 #define	SQTAG_TCP_TIMEWAIT		9
3490 #define	SQTAG_TCP_ACCEPT_FINISH		10
3491 #define	SQTAG_TCP_ACCEPT_FINISH_Q0	11
3492 #define	SQTAG_TCP_ACCEPT_PENDING	12
3493 #define	SQTAG_TCP_LISTEN_DISCON		13
3494 #define	SQTAG_TCP_CONN_REQ		14
3495 #define	SQTAG_TCP_EAGER_BLOWOFF		15
3496 #define	SQTAG_TCP_EAGER_CLEANUP		16
3497 #define	SQTAG_TCP_EAGER_CLEANUP_Q0	17
3498 #define	SQTAG_TCP_CONN_IND		18
3499 #define	SQTAG_TCP_RSRV			19
3500 #define	SQTAG_TCP_ABORT_BUCKET		20
3501 #define	SQTAG_TCP_REINPUT		21
3502 #define	SQTAG_TCP_REINPUT_EAGER		22
3503 #define	SQTAG_TCP_INPUT_MCTL		23
3504 #define	SQTAG_TCP_RPUTOTHER		24
3505 #define	SQTAG_IP_PROTO_AGAIN		25
3506 #define	SQTAG_IP_FANOUT_TCP		26
3507 #define	SQTAG_IPSQ_CLEAN_RING		27
3508 #define	SQTAG_TCP_WPUT_OTHER		28
3509 #define	SQTAG_TCP_CONN_REQ_UNBOUND	29
3510 #define	SQTAG_TCP_SEND_PENDING		30
3511 #define	SQTAG_BIND_RETRY		31
3512 #define	SQTAG_UDP_FANOUT		32
3513 #define	SQTAG_UDP_INPUT			33
3514 #define	SQTAG_UDP_WPUT			34
3515 #define	SQTAG_UDP_OUTPUT		35
3516 #define	SQTAG_TCP_KSSL_INPUT		36
3517 
3518 #endif	/* _KERNEL */
3519 
3520 #ifdef	__cplusplus
3521 }
3522 #endif
3523 
3524 #endif	/* _INET_IP_H */
3525