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