xref: /illumos-gate/usr/src/uts/common/inet/ip6.h (revision 8119dad84d6416f13557b0ba8e2aaf9064cbcfd3)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright 2019 Joyent, Inc.
27  */
28 
29 #ifndef	_INET_IP6_H
30 #define	_INET_IP6_H
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 /* From RFC 3542 - setting for IPV6_USE_MIN_MTU socket option */
64 #define	IPV6_USE_MIN_MTU_MULTICAST	-1	/* Default */
65 #define	IPV6_USE_MIN_MTU_NEVER		0
66 #define	IPV6_USE_MIN_MTU_ALWAYS		1
67 
68 #ifdef	_KERNEL
69 
70 /* Extract the scope from a multicast address */
71 #ifdef _BIG_ENDIAN
72 #define	IN6_ADDR_MC_SCOPE(addr) \
73 	(((addr)->s6_addr32[0] & 0x000f0000) >> 16)
74 #else
75 #define	IN6_ADDR_MC_SCOPE(addr) \
76 	(((addr)->s6_addr32[0] & 0x00000f00) >> 8)
77 #endif
78 
79 /* Default IPv4 TTL for IPv6-in-IPv4 encapsulated packets */
80 #define	IPV6_DEFAULT_HOPS	60	/* XXX What should it be? */
81 
82 /* Max IPv6 TTL */
83 #define	IPV6_MAX_HOPS	255
84 
85 /* Minimum IPv6 MTU from rfc2460 */
86 #define	IPV6_MIN_MTU		1280
87 
88 /* EUI-64 based token length */
89 #define	IPV6_TOKEN_LEN		64
90 
91 /* Length of an advertised IPv6 prefix */
92 #define	IPV6_PREFIX_LEN		64
93 
94 /* Default and maximum tunnel encapsulation limits.  See RFC 2473. */
95 #define	IPV6_DEFAULT_ENCAPLIMIT	4
96 #define	IPV6_MAX_ENCAPLIMIT	255
97 
98 /*
99  * Minimum and maximum extension header lengths for IPv6.  The 8-bit
100  * length field of each extension header (see rfc2460) specifies the
101  * number of 8 octet units of data in the header not including the
102  * first 8 octets.  A value of 0 would indicate 8 bytes (0 * 8 + 8),
103  * and 255 would indicate 2048 bytes (255 * 8 + 8).
104  */
105 #define	MIN_EHDR_LEN		8
106 #define	MAX_EHDR_LEN		2048
107 
108 #ifdef _BIG_ENDIAN
109 #define	IPV6_DEFAULT_VERS_AND_FLOW	0x60000000
110 #define	IPV6_VERS_AND_FLOW_MASK		0xF0000000
111 #define	V6_MCAST			0xFF000000
112 #define	V6_LINKLOCAL			0xFE800000
113 
114 #define	IPV6_FLOW_TCLASS(x)		(((x) & IPV6_FLOWINFO_TCLASS) >> 20)
115 #define	IPV6_TCLASS_FLOW(f, c)		(((f) & ~IPV6_FLOWINFO_TCLASS) |\
116 					((c) << 20))
117 #else
118 #define	IPV6_DEFAULT_VERS_AND_FLOW	0x00000060
119 #define	IPV6_VERS_AND_FLOW_MASK		0x000000F0
120 #define	V6_MCAST			0x000000FF
121 #define	V6_LINKLOCAL			0x000080FE
122 
123 #define	IPV6_FLOW_TCLASS(x)		((((x) & 0xf000U) >> 12) |\
124 					(((x) & 0xf) << 4))
125 #define	IPV6_TCLASS_FLOW(f, c)		(((f) & ~IPV6_FLOWINFO_TCLASS) |\
126 					((((c) & 0xf) << 12) |\
127 					(((c) & 0xf0) >> 4)))
128 #endif
129 
130 /*
131  * UTILITY MACROS FOR ADDRESSES.
132  */
133 
134 /*
135  * Convert an IPv4 address mask to an IPv6 mask.   Pad with 1-bits.
136  */
137 #define	V4MASK_TO_V6(v4, v6)	((v6).s6_addr32[0] = 0xffffffffUL,	\
138 				(v6).s6_addr32[1] = 0xffffffffUL,	\
139 				(v6).s6_addr32[2] = 0xffffffffUL,	\
140 				(v6).s6_addr32[3] = (v4))
141 
142 /*
143  * Convert aligned IPv4-mapped IPv6 address into an IPv4 address.
144  * Note: We use "v6" here in definition of macro instead of "(v6)"
145  * Not possible to use "(v6)" here since macro is used with struct
146  * field names as arguments.
147  */
148 #define	V4_PART_OF_V6(v6)	v6.s6_addr32[3]
149 
150 #ifdef _BIG_ENDIAN
151 #define	V6_OR_V4_INADDR_ANY(a)	((a).s6_addr32[3] == 0 &&		\
152 				((a).s6_addr32[2] == 0xffffU ||	\
153 				(a).s6_addr32[2] == 0) &&		\
154 				(a).s6_addr32[1] == 0 &&		\
155 				(a).s6_addr32[0] == 0)
156 
157 #else
158 #define	V6_OR_V4_INADDR_ANY(a)	((a).s6_addr32[3] == 0 &&		\
159 				((a).s6_addr32[2] == 0xffff0000U ||	\
160 				(a).s6_addr32[2] == 0) &&		\
161 				(a).s6_addr32[1] == 0 &&		\
162 				(a).s6_addr32[0] == 0)
163 #endif /* _BIG_ENDIAN */
164 
165 /* IPv4-mapped CLASSD addresses */
166 #ifdef _BIG_ENDIAN
167 #define	IN6_IS_ADDR_V4MAPPED_CLASSD(addr) \
168 	(((addr)->_S6_un._S6_u32[2] == 0x0000ffff) && \
169 	(CLASSD((addr)->_S6_un._S6_u32[3])) && \
170 	((addr)->_S6_un._S6_u32[1] == 0) && \
171 	((addr)->_S6_un._S6_u32[0] == 0))
172 #else  /* _BIG_ENDIAN */
173 #define	IN6_IS_ADDR_V4MAPPED_CLASSD(addr) \
174 	(((addr)->_S6_un._S6_u32[2] == 0xffff0000U) && \
175 	(CLASSD((addr)->_S6_un._S6_u32[3])) && \
176 	((addr)->_S6_un._S6_u32[1] == 0) && \
177 	((addr)->_S6_un._S6_u32[0] == 0))
178 #endif /* _BIG_ENDIAN */
179 
180 /* Clear an IPv6 addr */
181 #define	V6_SET_ZERO(a)		((a).s6_addr32[0] = 0,			\
182 				(a).s6_addr32[1] = 0,			\
183 				(a).s6_addr32[2] = 0,			\
184 				(a).s6_addr32[3] = 0)
185 
186 /* Mask comparison: is IPv6 addr a, and'ed with mask m, equal to addr b? */
187 #define	V6_MASK_EQ(a, m, b)						\
188 	((((a).s6_addr32[0] & (m).s6_addr32[0]) == (b).s6_addr32[0]) &&	\
189 	(((a).s6_addr32[1] & (m).s6_addr32[1]) == (b).s6_addr32[1]) &&	\
190 	(((a).s6_addr32[2] & (m).s6_addr32[2]) == (b).s6_addr32[2]) &&	\
191 	(((a).s6_addr32[3] & (m).s6_addr32[3]) == (b).s6_addr32[3]))
192 
193 #define	V6_MASK_EQ_2(a, m, b)						\
194 	((((a).s6_addr32[0] & (m).s6_addr32[0]) ==			\
195 	    ((b).s6_addr32[0]  & (m).s6_addr32[0])) &&			\
196 	(((a).s6_addr32[1] & (m).s6_addr32[1]) ==			\
197 	    ((b).s6_addr32[1]  & (m).s6_addr32[1])) &&			\
198 	(((a).s6_addr32[2] & (m).s6_addr32[2]) ==			\
199 	    ((b).s6_addr32[2]  & (m).s6_addr32[2])) &&			\
200 	(((a).s6_addr32[3] & (m).s6_addr32[3]) ==			\
201 	    ((b).s6_addr32[3]  & (m).s6_addr32[3])))
202 
203 /* Copy IPv6 address (s), logically and'ed with mask (m), into (d) */
204 #define	V6_MASK_COPY(s, m, d)						\
205 	((d).s6_addr32[0] = (s).s6_addr32[0] & (m).s6_addr32[0],	\
206 	(d).s6_addr32[1] = (s).s6_addr32[1] & (m).s6_addr32[1],		\
207 	(d).s6_addr32[2] = (s).s6_addr32[2] & (m).s6_addr32[2],		\
208 	(d).s6_addr32[3] = (s).s6_addr32[3] & (m).s6_addr32[3])
209 
210 #define	ILL_FRAG_HASH_V6(v6addr, i)					\
211 	((ntohl((v6addr).s6_addr32[3]) ^ (i ^ (i >> 8))) %		\
212 						ILL_FRAG_HASH_TBL_COUNT)
213 
214 
215 /*
216  * GLOBAL EXTERNALS
217  */
218 extern const in6_addr_t	ipv6_all_ones;
219 extern const in6_addr_t	ipv6_all_zeros;
220 extern const in6_addr_t	ipv6_loopback;
221 extern const in6_addr_t	ipv6_all_hosts_mcast;
222 extern const in6_addr_t	ipv6_all_rtrs_mcast;
223 extern const in6_addr_t	ipv6_all_v2rtrs_mcast;
224 extern const in6_addr_t	ipv6_solicited_node_mcast;
225 extern const in6_addr_t	ipv6_unspecified_group;
226 
227 /*
228  * FUNCTION PROTOTYPES
229  */
230 extern void	icmp_param_problem_nexthdr_v6(mblk_t *, boolean_t,
231     ip_recv_attr_t *);
232 extern void	icmp_pkt2big_v6(mblk_t *, uint32_t, boolean_t,
233     ip_recv_attr_t *);
234 extern void	icmp_time_exceeded_v6(mblk_t *, uint8_t, boolean_t,
235     ip_recv_attr_t *);
236 extern void	icmp_unreachable_v6(mblk_t *, uint8_t, boolean_t,
237     ip_recv_attr_t *);
238 extern mblk_t	*icmp_inbound_v6(mblk_t *, ip_recv_attr_t *);
239 extern void	icmp_inbound_error_fanout_v6(mblk_t *, icmp6_t *,
240     ip_recv_attr_t *);
241 extern void	icmp_update_out_mib_v6(ill_t *, icmp6_t *);
242 
243 extern boolean_t conn_wantpacket_v6(conn_t *, ip_recv_attr_t *, ip6_t *);
244 
245 extern in6addr_scope_t	ip_addr_scope_v6(const in6_addr_t *);
246 extern void	ip_build_hdrs_v6(uchar_t *, uint_t, const ip_pkt_t *, uint8_t,
247     uint32_t);
248 extern void	ip_fanout_udp_multi_v6(mblk_t *, ip6_t *, uint16_t, uint16_t,
249     ip_recv_attr_t *);
250 extern void	ip_fanout_send_icmp_v6(mblk_t *, uint_t, uint8_t,
251     ip_recv_attr_t *);
252 extern void	ip_fanout_proto_v6(mblk_t *, ip6_t *, ip_recv_attr_t *);
253 extern int	ip_find_hdr_v6(mblk_t *, ip6_t *, boolean_t, ip_pkt_t *,
254     uint8_t *);
255 extern in6_addr_t ip_get_dst_v6(ip6_t *, const mblk_t *, boolean_t *);
256 extern ip6_rthdr_t	*ip_find_rthdr_v6(ip6_t *, uint8_t *);
257 extern boolean_t	ip_hdr_length_nexthdr_v6(mblk_t *, ip6_t *,
258     uint16_t *, uint8_t **);
259 extern uint16_t	ip_hdr_length_v6(mblk_t *, ip6_t *);
260 extern uint32_t	ip_massage_options_v6(ip6_t *, ip6_rthdr_t *, netstack_t *);
261 extern void	ip_forward_xmit_v6(nce_t *, mblk_t *, ip6_t *, ip_recv_attr_t *,
262     uint32_t, uint32_t);
263 extern mblk_t	*ip_fraghdr_add_v6(mblk_t *, uint32_t, ip_xmit_attr_t *);
264 extern int	ip_fragment_v6(mblk_t *, nce_t *, iaflags_t, uint_t, uint32_t,
265     uint32_t, zoneid_t, zoneid_t, pfirepostfrag_t postfragfn,
266     uintptr_t *ixa_cookie);
267 extern int	ip_process_options_v6(mblk_t *, ip6_t *,
268     uint8_t *, uint_t, uint8_t, ip_recv_attr_t *);
269 extern void	ip_process_rthdr(mblk_t *, ip6_t *, ip6_rthdr_t *,
270     ip_recv_attr_t *);
271 extern int	ip_total_hdrs_len_v6(const ip_pkt_t *);
272 extern mblk_t	*ipsec_early_ah_v6(mblk_t *, ip_recv_attr_t *);
273 extern int	ipsec_ah_get_hdr_size_v6(mblk_t *, boolean_t);
274 extern void	ip_send_potential_redirect_v6(mblk_t *, ip6_t *, ire_t *,
275     ip_recv_attr_t *);
276 extern int	ip_rput_v6(queue_t *, mblk_t *);
277 extern mblk_t	*mld_input(mblk_t *, ip_recv_attr_t *);
278 extern void	mld_joingroup(ilm_t *);
279 extern void	mld_leavegroup(ilm_t *);
280 extern void	mld_timeout_handler(void *);
281 
282 extern void	pr_addr_dbg(char *, int, const void *);
283 extern void	*ip6_kstat_init(netstackid_t, ip6_stat_t *);
284 extern void	ip6_kstat_fini(netstackid_t, kstat_t *);
285 extern size_t	ip6_get_src_preferences(ip_xmit_attr_t *, uint32_t *);
286 extern int	ip6_set_src_preferences(ip_xmit_attr_t *, uint32_t);
287 
288 #endif	/* _KERNEL */
289 
290 #ifdef	__cplusplus
291 }
292 #endif
293 
294 #endif	/* _INET_IP6_H */
295