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