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