xref: /titanic_44/usr/src/uts/common/inet/ip6.h (revision 458cf4d67cff5ff99a68a866b7657f1b1d6fe61c)
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 2007 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 reachability confirmation to IP.
205  */
206 #define	IP_FORWARD_PROG_BIT		0x8
207 
208 #ifdef _BIG_ENDIAN
209 #define	IPV6_DEFAULT_VERS_AND_FLOW	0x60000000
210 #define	IPV6_VERS_AND_FLOW_MASK		0xF0000000
211 #define	IP_FORWARD_PROG			((uint32_t)IP_FORWARD_PROG_BIT << 28)
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			((uint32_t)IP_FORWARD_PROG_BIT << 4)
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 /*
320  * GLOBAL EXTERNALS
321  */
322 extern const in6_addr_t	ipv6_all_ones;
323 extern const in6_addr_t	ipv6_all_zeros;
324 extern const in6_addr_t	ipv6_loopback;
325 extern const in6_addr_t	ipv6_all_hosts_mcast;
326 extern const in6_addr_t	ipv6_all_rtrs_mcast;
327 extern const in6_addr_t	ipv6_all_v2rtrs_mcast;
328 extern const in6_addr_t	ipv6_solicited_node_mcast;
329 extern const in6_addr_t	ipv6_unspecified_group;
330 
331 /*
332  * FUNCTION PROTOTYPES
333  */
334 
335 struct ipsec_out_s;
336 
337 extern void	convert2ascii(char *buf, const in6_addr_t *addr);
338 extern char	*inet_ntop(int, const void *, char *, int);
339 extern int	inet_pton(int, char *, void *);
340 extern void	icmp_time_exceeded_v6(queue_t *, mblk_t *, uint8_t,
341     boolean_t, boolean_t, zoneid_t, ip_stack_t *);
342 extern void	icmp_unreachable_v6(queue_t *, mblk_t *, uint8_t,
343     boolean_t, boolean_t, zoneid_t, ip_stack_t *);
344 extern void	icmp_inbound_error_fanout_v6(queue_t *, mblk_t *, ip6_t *,
345     icmp6_t *, ill_t *, boolean_t, zoneid_t);
346 extern boolean_t conn_wantpacket_v6(conn_t *, ill_t *, ip6_t *, int, zoneid_t);
347 extern mblk_t	*ip_add_info_v6(mblk_t *, ill_t *, const in6_addr_t *);
348 extern in6addr_scope_t	ip_addr_scope_v6(const in6_addr_t *);
349 extern mblk_t	*ip_bind_v6(queue_t *, mblk_t *, conn_t *, ip6_pkt_t *);
350 extern void	ip_build_hdrs_v6(uchar_t *, uint_t, ip6_pkt_t *, uint8_t);
351 extern int	ip_fanout_send_icmp_v6(queue_t *, mblk_t *, uint_t,
352     uint_t, uint8_t, uint_t, boolean_t, zoneid_t, ip_stack_t *);
353 extern int	ip_find_hdr_v6(mblk_t *, ip6_t *, ip6_pkt_t *, uint8_t *);
354 extern in6_addr_t ip_get_dst_v6(ip6_t *, boolean_t *);
355 extern ip6_rthdr_t	*ip_find_rthdr_v6(ip6_t *, uint8_t *);
356 extern int	ip_hdr_complete_v6(ip6_t *, zoneid_t, ip_stack_t *);
357 extern boolean_t	ip_hdr_length_nexthdr_v6(mblk_t *, ip6_t *,
358     uint16_t *, uint8_t **);
359 extern int	ip_hdr_length_v6(mblk_t *, ip6_t *);
360 extern int	ip_check_v6_mblk(mblk_t *, ill_t *);
361 extern uint32_t	ip_massage_options_v6(ip6_t *, ip6_rthdr_t *, netstack_t *);
362 extern void	ip_wput_frag_v6(mblk_t *, ire_t *, uint_t, conn_t *, int, int);
363 extern void 	ip_wput_ipsec_out_v6(queue_t *, mblk_t *, ip6_t *, ill_t *,
364     ire_t *);
365 extern int	ip_total_hdrs_len_v6(ip6_pkt_t *);
366 extern int	ipsec_ah_get_hdr_size_v6(mblk_t *, boolean_t);
367 extern void	ip_wput_local_v6(queue_t *, ill_t *, ip6_t *, mblk_t *,
368     ire_t *, int);
369 extern void	ip_output_v6(void *, mblk_t *, void *, int);
370 extern void	ip_xmit_v6(mblk_t *, ire_t *, uint_t, conn_t *, int,
371     struct ipsec_out_s *);
372 extern void	ip_rput_v6(queue_t *, mblk_t *);
373 extern void	ip_rput_data_v6(queue_t *, ill_t *, mblk_t *, ip6_t *,
374     uint_t, mblk_t *, mblk_t *);
375 extern void	mld_input(queue_t *, mblk_t *, ill_t *);
376 extern void	mld_joingroup(ilm_t *);
377 extern void	mld_leavegroup(ilm_t *);
378 extern void	mld_timeout_handler(void *);
379 
380 extern void	pr_addr_dbg(char *, int, const void *);
381 extern int	ip_multirt_apply_membership_v6(int (*fn)(conn_t *, boolean_t,
382     const in6_addr_t *, int, mcast_record_t, const in6_addr_t *,
383     mblk_t *), ire_t *, conn_t *, boolean_t, const in6_addr_t *,
384     mcast_record_t, const in6_addr_t *, mblk_t *);
385 extern void	ip_newroute_ipif_v6(queue_t *, mblk_t *, ipif_t *,
386     in6_addr_t, int, zoneid_t);
387 extern void	ip_newroute_v6(queue_t *, mblk_t *, const in6_addr_t *,
388     const in6_addr_t *, ill_t *, zoneid_t, ip_stack_t *);
389 extern void	*ip6_kstat_init(netstackid_t, ip6_stat_t *);
390 extern void	ip6_kstat_fini(netstackid_t, kstat_t *);
391 extern size_t	ip6_get_src_preferences(conn_t *, uint32_t *);
392 extern int	ip6_set_src_preferences(conn_t *, uint32_t);
393 extern int	ip6_set_pktinfo(cred_t *, conn_t *, struct in6_pktinfo *,
394     mblk_t *);
395 
396 #endif	/* _KERNEL */
397 
398 #ifdef	__cplusplus
399 }
400 #endif
401 
402 #endif	/* _INET_IP6_H */
403