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