xref: /titanic_50/usr/src/uts/common/inet/ip6.h (revision 4f85d229295a756a4e6f1759b47df7b97412db7d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_INET_IP6_H
28 #define	_INET_IP6_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/isa_defs.h>
37 
38 #ifdef	_KERNEL
39 /* icmp6_t is used in the prototype of icmp_inbound_error_fanout_v6() */
40 #include <netinet/icmp6.h>
41 #endif	/* _KERNEL */
42 
43 /* version number for IPv6 - hard to get this one wrong! */
44 #define	IPV6_VERSION		6
45 
46 #define	IPV6_HDR_LEN		40
47 
48 #define	IPV6_ADDR_LEN		16
49 
50 /*
51  * IPv6 address scopes.  The values of these enums also match the scope
52  * field of multicast addresses.
53  */
54 typedef enum {
55 	IP6_SCOPE_INTFLOCAL = 1,	/* Multicast addresses only */
56 	IP6_SCOPE_LINKLOCAL,
57 	IP6_SCOPE_SUBNETLOCAL,		/* Multicast addresses only */
58 	IP6_SCOPE_ADMINLOCAL,		/* Multicast addresses only */
59 	IP6_SCOPE_SITELOCAL,
60 	IP6_SCOPE_GLOBAL
61 } in6addr_scope_t;
62 
63 #ifdef	_KERNEL
64 
65 /*
66  * Private header used between the transports and IP to carry the content
67  * of the options IPV6_PKTINFO/IPV6_RECVPKTINFO (the interface index only)
68  * and IPV6_NEXTHOP.
69  * Also used to specify that raw sockets do not want the UDP/TCP transport
70  * checksums calculated in IP (akin to IP_HDR_INCLUDED) and provide for
71  * IPV6_CHECKSUM on the transmit side (using ip6i_checksum_off).
72  *
73  * When this header is used it must be the first header in the packet i.e.
74  * before the real ip6 header. The use of a next header value of 255
75  * (IPPROTO_RAW) in this header indicates its presence. Note that
76  * ip6_nxt = IPPROTO_RAW indicates that "this" header is ip6_info - the
77  * next header is always IPv6.
78  *
79  * Note that ip6i_nexthop is at the same offset as ip6_dst so that
80  * this header can be kept in the packet while the it passes through
81  * ip_newroute* and the ndp code. Those routines will use ip6_dst for
82  * resolution.
83  *
84  * Implementation offset assumptions about ip6_info_t and ip6_t fields
85  * and their alignments shown in figure below
86  *
87  * ip6_info (Private headers from transports to IP) header below
88  * _______________________________________________________________ _ _ _ _ _
89  * | .... | ip6i_nxt (255)| ......................|ip6i_nexthop| ...ip6_t.
90  * --------------------------------------------------------------- - - - - -
91  *        ^                                       ^
92  * <---- >| same offset for {ip6i_nxt,ip6_nxt}    ^
93  *        ^                                       ^
94  * <------^-------------------------------------->| same offset for
95  *        ^                                       ^ {ip6i_nxthop,ip6_dst}
96  * _______________________________________________________________ _ _ _
97  * | .... | ip6_nxt       | ......................|ip6_dst     | .other hdrs...
98  * --------------------------------------------------------------- - - -
99  * ip6_t (IPv6 protocol) header above
100  */
101 struct ip6_info {
102 	union {
103 		struct ip6_info_ctl {
104 			uint32_t	ip6i_un1_flow;
105 			uint16_t	ip6i_un1_plen;   /* payload length */
106 			uint8_t		ip6i_un1_nxt;    /* next header */
107 			uint8_t		ip6i_un1_hlim;   /* hop limit */
108 		} ip6i_un1;
109 	} ip6i_ctlun;
110 	int		ip6i_flags;	/* See below */
111 	int		ip6i_ifindex;
112 	int		ip6i_checksum_off;
113 	int		ip6i_pad;
114 	in6_addr_t	ip6i_nexthop;	/* Same offset as ip6_dst */
115 };
116 typedef struct ip6_info	ip6i_t;
117 
118 #define	ip6i_flow	ip6i_ctlun.ip6i_un1.ip6i_un1_flow
119 #define	ip6i_vcf	ip6i_flow		/* Version, class, flow */
120 #define	ip6i_nxt	ip6i_ctlun.ip6i_un1.ip6i_un1_nxt
121 #define	ip6i_hops	ip6i_ctlun.ip6i_un1.ip6i_un1_hlim
122 
123 /* ip6_info flags */
124 #define	IP6I_IFINDEX	0x1	/* ip6i_ifindex is set (to nonzero value) */
125 #define	IP6I_NEXTHOP	0x2	/* ip6i_nexthop is different than ip6_dst */
126 #define	IP6I_NO_ULP_CKSUM	0x4
127 			/*
128 			 * Do not generate TCP/UDP/SCTP transport checksum.
129 			 * Used by raw sockets. Does not affect the
130 			 * generation of transport checksums for ICMPv6
131 			 * since such packets always arrive through
132 			 * a raw socket.
133 			 */
134 #define	IP6I_UNSPEC_SRC	0x8
135 			/* Used to carry conn_unspec_src through ip_newroute* */
136 #define	IP6I_RAW_CHECKSUM	0x10
137 			/* Compute checksum and stuff in ip6i_checksum_off */
138 #define	IP6I_VERIFY_SRC	0x20	/* Verify ip6_src. Used when IPV6_PKTINFO */
139 #define	IP6I_ATTACH_IF	0x40	/* Bind to no failover address or BOUND_PIF. */
140 #define	IP6I_DROP_IFDELAYED	0x80
141 			/* Drop the packet if delayed in ndp resolver */
142 #define	IP6I_ND_DELAYED 0x100	/* Packet was delayed in ndp resolver */
143 #define	IP6I_DONTFRAG	0x200	/* Don't fragment this packet */
144 #define	IP6I_HOPLIMIT	0x400	/* hoplimit has been set by the sender */
145 
146 /*
147  * These constants refer to the IPV6_USE_MIN_MTU API.  The
148  * actually values used in the API are these values shifted down
149  * 10 bits minus 2 [-1, 1].  0 (-2 after conversion) is considered
150  * the same as the default (-1).  IP6I_API_USE_MIN_MTU(f, x) returns
151  * the flags field updated with min mtu.  IP6I_USE_MIN_MTU_API takes the
152  * field and returns the API value (+ the -2 value).
153  */
154 #define	IP6I_USE_MIN_MTU_UNICAST	0x400
155 #define	IP6I_USE_MIN_MTU_ALWAYS		0x800
156 #define	IP6I_USE_MIN_MTU_NEVER		0xC00
157 #define	IP6I_USE_MIN_MTU_API(x)		((((x) & 0xC00) >> 10) - 2)
158 #define	IP6I_API_USE_MIN_MTU(f, x)	(((f) & ~0xC00) &\
159 					((((x) + 2) & 0x3) << 11))
160 #define	IPV6_USE_MIN_MTU_DEFAULT	-2
161 #define	IPV6_USE_MIN_MTU_UNICAST	-1
162 #define	IPV6_USE_MIN_MTU_ALWAYS		0
163 #define	IPV6_USE_MIN_MTU_NEVER		1
164 
165 /* Extract the scope from a multicast address */
166 #ifdef _BIG_ENDIAN
167 #define	IN6_ADDR_MC_SCOPE(addr) \
168 	(((addr)->s6_addr32[0] & 0x000f0000) >> 16)
169 #else
170 #define	IN6_ADDR_MC_SCOPE(addr) \
171 	(((addr)->s6_addr32[0] & 0x00000f00) >> 8)
172 #endif
173 
174 /* Default IPv4 TTL for IPv6-in-IPv4 encapsulated packets */
175 #define	IPV6_DEFAULT_HOPS	60	/* XXX What should it be? */
176 
177 /* Max IPv6 TTL */
178 #define	IPV6_MAX_HOPS	255
179 
180 /* Minimum IPv6 MTU from rfc2460 */
181 #define	IPV6_MIN_MTU		1280
182 
183 /* EUI-64 based token length */
184 #define	IPV6_TOKEN_LEN		64
185 
186 /* Length of an advertised IPv6 prefix */
187 #define	IPV6_PREFIX_LEN		64
188 
189 /* Default and maximum tunnel encapsulation limits.  See RFC 2473. */
190 #define	IPV6_DEFAULT_ENCAPLIMIT	4
191 #define	IPV6_MAX_ENCAPLIMIT	255
192 
193 /*
194  * Minimum and maximum extension header lengths for IPv6.  The 8-bit
195  * length field of each extension header (see rfc2460) specifies the
196  * number of 8 octet units of data in the header not including the
197  * first 8 octets.  A value of 0 would indicate 8 bytes (0 * 8 + 8),
198  * and 255 would indicate 2048 bytes (255 * 8 + 8).
199  */
200 #define	MIN_EHDR_LEN		8
201 #define	MAX_EHDR_LEN		2048
202 
203 /*
204  * The high-order bit of the version field is used by the transports to
205  * indicate a rechability confirmation to IP.
206  */
207 #ifdef _BIG_ENDIAN
208 #define	IPV6_DEFAULT_VERS_AND_FLOW	0x60000000
209 #define	IPV6_VERS_AND_FLOW_MASK		0xF0000000
210 #define	IP_FORWARD_PROG			0x80000000
211 
212 #define	V6_MCAST			0xFF000000
213 #define	V6_LINKLOCAL			0xFE800000
214 
215 #define	IPV6_FLOW_TCLASS(x)		(((x) & IPV6_FLOWINFO_TCLASS) >> 20)
216 #define	IPV6_TCLASS_FLOW(f, c)		(((f) & ~IPV6_FLOWINFO_TCLASS) |\
217 					((c) << 20))
218 
219 #else
220 #define	IPV6_DEFAULT_VERS_AND_FLOW	0x00000060
221 #define	IPV6_VERS_AND_FLOW_MASK		0x000000F0
222 #define	IP_FORWARD_PROG			0x00000080
223 
224 #define	V6_MCAST			0x000000FF
225 #define	V6_LINKLOCAL			0x000080FE
226 
227 #define	IPV6_FLOW_TCLASS(x)		((((x) & 0xf000U) >> 12) |\
228 					(((x) & 0xf) << 4))
229 #define	IPV6_TCLASS_FLOW(f, c)		(((f) & ~IPV6_FLOWINFO_TCLASS) |\
230 					((((c) & 0xf) << 12) |\
231 					(((c) & 0xf0) >> 4)))
232 #endif
233 
234 /*
235  * UTILITY MACROS FOR ADDRESSES.
236  */
237 
238 /*
239  * Convert an IPv4 address mask to an IPv6 mask.   Pad with 1-bits.
240  */
241 #define	V4MASK_TO_V6(v4, v6)	((v6).s6_addr32[0] = 0xffffffffUL,	\
242 				(v6).s6_addr32[1] = 0xffffffffUL,	\
243 				(v6).s6_addr32[2] = 0xffffffffUL,	\
244 				(v6).s6_addr32[3] = (v4))
245 
246 /*
247  * Convert aligned IPv4-mapped IPv6 address into an IPv4 address.
248  * Note: We use "v6" here in definition of macro instead of "(v6)"
249  * Not possible to use "(v6)" here since macro is used with struct
250  * field names as arguments.
251  */
252 #define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
253 
254 #ifdef _BIG_ENDIAN
255 #define	V6_OR_V4_INADDR_ANY(a)	((a).s6_addr32[3] == 0 &&		\
256 				((a).s6_addr32[2] == 0xffffU ||	\
257 				(a).s6_addr32[2] == 0) &&		\
258 				(a).s6_addr32[1] == 0 &&		\
259 				(a).s6_addr32[0] == 0)
260 
261 #else
262 #define	V6_OR_V4_INADDR_ANY(a)	((a).s6_addr32[3] == 0 && 		\
263 				((a).s6_addr32[2] == 0xffff0000U ||	\
264 				(a).s6_addr32[2] == 0) &&		\
265 				(a).s6_addr32[1] == 0 &&		\
266 				(a).s6_addr32[0] == 0)
267 #endif /* _BIG_ENDIAN */
268 
269 /* IPv4-mapped CLASSD addresses */
270 #ifdef _BIG_ENDIAN
271 #define	IN6_IS_ADDR_V4MAPPED_CLASSD(addr) \
272 	(((addr)->_S6_un._S6_u32[2] == 0x0000ffff) && \
273 	(CLASSD((addr)->_S6_un._S6_u32[3])) && \
274 	((addr)->_S6_un._S6_u32[1] == 0) && \
275 	((addr)->_S6_un._S6_u32[0] == 0))
276 #else  /* _BIG_ENDIAN */
277 #define	IN6_IS_ADDR_V4MAPPED_CLASSD(addr) \
278 	(((addr)->_S6_un._S6_u32[2] == 0xffff0000U) && \
279 	(CLASSD((addr)->_S6_un._S6_u32[3])) && \
280 	((addr)->_S6_un._S6_u32[1] == 0) && \
281 	((addr)->_S6_un._S6_u32[0] == 0))
282 #endif /* _BIG_ENDIAN */
283 
284 /* Clear an IPv6 addr */
285 #define	V6_SET_ZERO(a)		((a).s6_addr32[0] = 0,			\
286 				(a).s6_addr32[1] = 0,			\
287 				(a).s6_addr32[2] = 0,			\
288 				(a).s6_addr32[3] = 0)
289 
290 /* Mask comparison: is IPv6 addr a, and'ed with mask m, equal to addr b? */
291 #define	V6_MASK_EQ(a, m, b)						\
292 	((((a).s6_addr32[0] & (m).s6_addr32[0]) == (b).s6_addr32[0]) &&	\
293 	(((a).s6_addr32[1] & (m).s6_addr32[1]) == (b).s6_addr32[1]) &&	\
294 	(((a).s6_addr32[2] & (m).s6_addr32[2]) == (b).s6_addr32[2]) &&	\
295 	(((a).s6_addr32[3] & (m).s6_addr32[3]) == (b).s6_addr32[3]))
296 
297 #define	V6_MASK_EQ_2(a, m, b)						\
298 	((((a).s6_addr32[0] & (m).s6_addr32[0]) ==			\
299 	    ((b).s6_addr32[0]  & (m).s6_addr32[0])) &&			\
300 	(((a).s6_addr32[1] & (m).s6_addr32[1]) ==			\
301 	    ((b).s6_addr32[1]  & (m).s6_addr32[1])) &&			\
302 	(((a).s6_addr32[2] & (m).s6_addr32[2]) ==			\
303 	    ((b).s6_addr32[2]  & (m).s6_addr32[2])) &&			\
304 	(((a).s6_addr32[3] & (m).s6_addr32[3]) ==			\
305 	    ((b).s6_addr32[3]  & (m).s6_addr32[3])))
306 
307 /* Copy IPv6 address (s), logically and'ed with mask (m), into (d) */
308 #define	V6_MASK_COPY(s, m, d)						\
309 	((d).s6_addr32[0] = (s).s6_addr32[0] & (m).s6_addr32[0],	\
310 	(d).s6_addr32[1] = (s).s6_addr32[1] & (m).s6_addr32[1],		\
311 	(d).s6_addr32[2] = (s).s6_addr32[2] & (m).s6_addr32[2],		\
312 	(d).s6_addr32[3] = (s).s6_addr32[3] & (m).s6_addr32[3])
313 
314 #define	ILL_FRAG_HASH_V6(v6addr, i)					\
315 	((ntohl((v6addr).s6_addr32[3]) ^ (i ^ (i >> 8))) % 		\
316 						ILL_FRAG_HASH_TBL_COUNT)
317 
318 /*
319  * GLOBAL EXTERNALS
320  */
321 extern uint_t ipv6_ire_default_count;	/* Number of IPv6 IRE_DEFAULT entries */
322 extern uint_t ipv6_ire_default_index;	/* Walking IPv6 index used to mod in */
323 extern int ipv6_ire_cache_cnt;	/* Number of IPv6 IRE_CACHE entries */
324 
325 extern const in6_addr_t	ipv6_all_ones;
326 extern const in6_addr_t	ipv6_all_zeros;
327 extern const in6_addr_t	ipv6_loopback;
328 extern const in6_addr_t	ipv6_all_hosts_mcast;
329 extern const in6_addr_t	ipv6_all_rtrs_mcast;
330 extern const in6_addr_t	ipv6_all_v2rtrs_mcast;
331 extern const in6_addr_t	ipv6_solicited_node_mcast;
332 extern const in6_addr_t	ipv6_unspecified_group;
333 
334 /*
335  * IPv6 mibs when the interface (ill) is not known.
336  * When the ill is known the per-interface mib in the ill is used.
337  */
338 extern mib2_ipv6IfStatsEntry_t	ip6_mib;
339 extern mib2_ipv6IfIcmpEntry_t	icmp6_mib;
340 
341 /*
342  * FUNCTION PROTOTYPES
343  */
344 
345 struct ipsec_out_s;
346 
347 extern void	convert2ascii(char *buf, const in6_addr_t *addr);
348 extern char	*inet_ntop(int, const void *, char *, int);
349 extern int	inet_pton(int, char *, void *);
350 extern void	icmp_time_exceeded_v6(queue_t *, mblk_t *, uint8_t,
351 		    boolean_t, boolean_t);
352 extern void	icmp_unreachable_v6(queue_t *, mblk_t *, uint8_t,
353 		    boolean_t, boolean_t);
354 extern void	icmp_inbound_error_fanout_v6(queue_t *, mblk_t *, ip6_t *,
355 		    icmp6_t *, ill_t *, boolean_t, zoneid_t);
356 extern boolean_t conn_wantpacket_v6(conn_t *, ill_t *, ip6_t *, int, zoneid_t);
357 extern mblk_t	*ip_add_info_v6(mblk_t *, ill_t *, const in6_addr_t *);
358 extern in6addr_scope_t	ip_addr_scope_v6(const in6_addr_t *);
359 extern mblk_t	*ip_bind_v6(queue_t *, mblk_t *, conn_t *, ip6_pkt_t *);
360 extern void	ip_build_hdrs_v6(uchar_t *, uint_t, ip6_pkt_t *, uint8_t);
361 extern void	ip_deliver_local_v6(queue_t *, mblk_t *, ill_t *, uint8_t,
362 		    uint_t, uint_t);
363 extern int	ip_fanout_send_icmp_v6(queue_t *, mblk_t *, uint_t,
364 		    uint_t, uint8_t, uint_t, boolean_t, zoneid_t);
365 extern int	ip_find_hdr_v6(mblk_t *, ip6_t *, ip6_pkt_t *, uint8_t *);
366 extern in6_addr_t ip_get_dst_v6(ip6_t *, boolean_t *);
367 extern ip6_rthdr_t	*ip_find_rthdr_v6(ip6_t *, uint8_t *);
368 extern int	ip_hdr_complete_v6(ip6_t *, zoneid_t);
369 extern boolean_t	ip_hdr_length_nexthdr_v6(mblk_t *, ip6_t *,
370 		    uint16_t *, uint8_t **);
371 extern int	ip_hdr_length_v6(mblk_t *, ip6_t *);
372 extern uint32_t	ip_massage_options_v6(ip6_t *, ip6_rthdr_t *);
373 extern void	ip_wput_frag_v6(mblk_t *, ire_t *, uint_t, conn_t *, int, int);
374 extern void 	ip_wput_ipsec_out_v6(queue_t *, mblk_t *, ip6_t *, ill_t *,
375     ire_t *);
376 extern int	ip_total_hdrs_len_v6(ip6_pkt_t *);
377 extern int	ipsec_ah_get_hdr_size_v6(mblk_t *, boolean_t);
378 extern void	ip_wput_local_v6(queue_t *, ill_t *, ip6_t *, mblk_t *,
379 		    ire_t *, int);
380 extern void	ip_wput_md_v6(queue_t *, mblk_t *, conn_t *);
381 extern void	ip_output_v6(void *, mblk_t *, void *, int);
382 extern void	ip_xmit_v6(mblk_t *, ire_t *, uint_t, conn_t *, int,
383 			struct ipsec_out_s *);
384 extern void	ip_rput_data_v6(queue_t *, ill_t *, mblk_t *, ip6_t *,
385 		    uint_t, mblk_t *);
386 extern void	mld_input(queue_t *, mblk_t *, ill_t *);
387 extern void	mld_joingroup(ilm_t *);
388 extern void	mld_leavegroup(ilm_t *);
389 extern void	mld_timeout_handler(void *);
390 extern void	mld_timeout_start(int);
391 
392 extern void	pr_addr_dbg(char *, int, const void *);
393 extern ipif_t	*ip_newroute_get_src_ipif_v6(ipif_t *, boolean_t,
394 		const in6_addr_t *);
395 extern int	ip_multirt_apply_membership_v6(int (*fn)(conn_t *, boolean_t,
396 		const in6_addr_t *, int, mcast_record_t, const in6_addr_t *,
397 		mblk_t *), ire_t *, conn_t *, boolean_t, const in6_addr_t *,
398 		mcast_record_t, const in6_addr_t *, mblk_t *);
399 extern void	ip_newroute_ipif_v6(queue_t *, mblk_t *, ipif_t *,
400 		in6_addr_t, int, zoneid_t);
401 extern void	ip_newroute_v6(queue_t *, mblk_t *, const in6_addr_t *,
402 		const in6_addr_t *, ill_t *, zoneid_t);
403 extern void	ip6_kstat_init(void);
404 extern size_t	ip6_get_src_preferences(conn_t *, uint32_t *);
405 extern int	ip6_set_src_preferences(conn_t *, uint32_t);
406 extern int	ip6_set_pktinfo(cred_t *, conn_t *, struct in6_pktinfo *,
407     mblk_t *);
408 
409 #endif	/* _KERNEL */
410 
411 #ifdef	__cplusplus
412 }
413 #endif
414 
415 #endif	/* _INET_IP6_H */
416