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