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