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