xref: /xnu-10002.1.13/bsd/net/nat464_utils.h (revision 1031c584a5e37aff177559b9f69dbd3c8c3fd30a)
1*1031c584SApple OSS Distributions /*
2*1031c584SApple OSS Distributions  * Copyright (c) 2018 Apple Inc. All rights reserved.
3*1031c584SApple OSS Distributions  *
4*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1031c584SApple OSS Distributions  *
6*1031c584SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1031c584SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1031c584SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1031c584SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1031c584SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1031c584SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1031c584SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1031c584SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1031c584SApple OSS Distributions  *
15*1031c584SApple OSS Distributions  * Please obtain a copy of the License at
16*1031c584SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1031c584SApple OSS Distributions  *
18*1031c584SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1031c584SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1031c584SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1031c584SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1031c584SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1031c584SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1031c584SApple OSS Distributions  * limitations under the License.
25*1031c584SApple OSS Distributions  *
26*1031c584SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1031c584SApple OSS Distributions  */
28*1031c584SApple OSS Distributions 
29*1031c584SApple OSS Distributions /*
30*1031c584SApple OSS Distributions  * Copyright (c) 2001 Daniel Hartmeier
31*1031c584SApple OSS Distributions  * Copyright (c) 2002 - 2013 Henning Brauer
32*1031c584SApple OSS Distributions  * NAT64 - Copyright (c) 2010 Viagenie Inc. (http://www.viagenie.ca)
33*1031c584SApple OSS Distributions  * All rights reserved.
34*1031c584SApple OSS Distributions  *
35*1031c584SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
36*1031c584SApple OSS Distributions  * modification, are permitted provided that the following conditions
37*1031c584SApple OSS Distributions  * are met:
38*1031c584SApple OSS Distributions  *
39*1031c584SApple OSS Distributions  *    - Redistributions of source code must retain the above copyright
40*1031c584SApple OSS Distributions  *	notice, this list of conditions and the following disclaimer.
41*1031c584SApple OSS Distributions  *    - Redistributions in binary form must reproduce the above
42*1031c584SApple OSS Distributions  *	copyright notice, this list of conditions and the following
43*1031c584SApple OSS Distributions  *	disclaimer in the documentation and/or other materials provided
44*1031c584SApple OSS Distributions  *	with the distribution.
45*1031c584SApple OSS Distributions  *
46*1031c584SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
47*1031c584SApple OSS Distributions  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
48*1031c584SApple OSS Distributions  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
49*1031c584SApple OSS Distributions  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
50*1031c584SApple OSS Distributions  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
51*1031c584SApple OSS Distributions  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
52*1031c584SApple OSS Distributions  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
53*1031c584SApple OSS Distributions  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
54*1031c584SApple OSS Distributions  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
55*1031c584SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
56*1031c584SApple OSS Distributions  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
57*1031c584SApple OSS Distributions  * POSSIBILITY OF SUCH DAMAGE.
58*1031c584SApple OSS Distributions  *
59*1031c584SApple OSS Distributions  * Effort sponsored in part by the Defense Advanced Research Projects
60*1031c584SApple OSS Distributions  * Agency (DARPA) and Air Force Research Laboratory, Air Force
61*1031c584SApple OSS Distributions  * Materiel Command, USAF, under agreement number F30602-01-2-0537.
62*1031c584SApple OSS Distributions  *
63*1031c584SApple OSS Distributions  */
64*1031c584SApple OSS Distributions #ifndef _NET_NAT464_UTILS_H_
65*1031c584SApple OSS Distributions #define _NET_NAT464_UTILS_H_
66*1031c584SApple OSS Distributions #include <netinet/in.h>
67*1031c584SApple OSS Distributions #include <net/pf_pbuf.h>
68*1031c584SApple OSS Distributions 
69*1031c584SApple OSS Distributions #define clat_log0(x)    do { log x; } while (0)
70*1031c584SApple OSS Distributions #define clat_log1(x)    do { if (clat_debug >= 1) log x; } while (0)
71*1031c584SApple OSS Distributions #define clat_log2(x)    do { if (clat_debug >= 2) log x; } while (0)
72*1031c584SApple OSS Distributions 
73*1031c584SApple OSS Distributions #define CLAT46_NEEDED(x)                                                        \
74*1031c584SApple OSS Distributions 	(!IN_LOOPBACK(x) && !IN_LINKLOCAL(x) && !IN_MULTICAST(x) &&             \
75*1031c584SApple OSS Distributions 	INADDR_BROADCAST != x)
76*1031c584SApple OSS Distributions 
77*1031c584SApple OSS Distributions #define CLAT64_NEEDED(x)                                                        \
78*1031c584SApple OSS Distributions 	(!IN6_IS_ADDR_LOOPBACK(x) && !IN6_IS_ADDR_LINKLOCAL(x) &&               \
79*1031c584SApple OSS Distributions 	!IN6_IS_ADDR_MULTICAST(x))
80*1031c584SApple OSS Distributions 
81*1031c584SApple OSS Distributions extern int clat_debug;
82*1031c584SApple OSS Distributions 
83*1031c584SApple OSS Distributions enum    { NT_DROP, NT_NAT64 };
84*1031c584SApple OSS Distributions enum    { NT_IN, NT_OUT };
85*1031c584SApple OSS Distributions struct nat464_addr {
86*1031c584SApple OSS Distributions 	union {
87*1031c584SApple OSS Distributions 		struct in_addr          _v4addr;
88*1031c584SApple OSS Distributions 		struct in6_addr         _v6addr;
89*1031c584SApple OSS Distributions 		uint8_t         _addr8[16];
90*1031c584SApple OSS Distributions 		uint16_t                _addr16[8];
91*1031c584SApple OSS Distributions 		uint32_t                _addr32[4];
92*1031c584SApple OSS Distributions 	} nat464a;                  /* 128-bit address */
93*1031c584SApple OSS Distributions #define natv4addr       nat464a._v4addr
94*1031c584SApple OSS Distributions #define natv6addr       nat464a._v6addr
95*1031c584SApple OSS Distributions #define nataddr8        nat464a._addr8
96*1031c584SApple OSS Distributions #define nataddr16       nat464a._addr16
97*1031c584SApple OSS Distributions #define nataddr32       nat464a._addr32
98*1031c584SApple OSS Distributions };
99*1031c584SApple OSS Distributions 
100*1031c584SApple OSS Distributions int
101*1031c584SApple OSS Distributions nat464_translate_icmp(int, void *);
102*1031c584SApple OSS Distributions 
103*1031c584SApple OSS Distributions int
104*1031c584SApple OSS Distributions     nat464_translate_icmp_ip(pbuf_t *, uint16_t, uint16_t *, uint16_t *,
105*1031c584SApple OSS Distributions     uint8_t, uint8_t, uint16_t, struct nat464_addr *,
106*1031c584SApple OSS Distributions     struct nat464_addr *, protocol_family_t, protocol_family_t );
107*1031c584SApple OSS Distributions 
108*1031c584SApple OSS Distributions int
109*1031c584SApple OSS Distributions     nat464_synthesize_ipv6(ifnet_t, const struct in_addr *, struct in6_addr *);
110*1031c584SApple OSS Distributions 
111*1031c584SApple OSS Distributions int
112*1031c584SApple OSS Distributions     nat464_synthesize_ipv4(ifnet_t, const struct in6_addr *, struct in_addr *);
113*1031c584SApple OSS Distributions 
114*1031c584SApple OSS Distributions int
115*1031c584SApple OSS Distributions     nat464_translate_64(pbuf_t *, int, uint8_t, uint8_t *, uint8_t, struct in_addr,
116*1031c584SApple OSS Distributions     struct in_addr, uint64_t, boolean_t *);
117*1031c584SApple OSS Distributions 
118*1031c584SApple OSS Distributions int
119*1031c584SApple OSS Distributions     nat464_translate_46(pbuf_t *, uint16_t, uint8_t, uint8_t, uint8_t, struct in6_addr,
120*1031c584SApple OSS Distributions     struct in6_addr, uint16_t);
121*1031c584SApple OSS Distributions 
122*1031c584SApple OSS Distributions int
123*1031c584SApple OSS Distributions     nat464_translate_proto(pbuf_t *, struct nat464_addr *, struct nat464_addr *,
124*1031c584SApple OSS Distributions     uint8_t, protocol_family_t, protocol_family_t, int, boolean_t);
125*1031c584SApple OSS Distributions 
126*1031c584SApple OSS Distributions int
127*1031c584SApple OSS Distributions     nat464_insert_frag46(pbuf_t *, uint16_t, uint16_t, boolean_t);
128*1031c584SApple OSS Distributions 
129*1031c584SApple OSS Distributions int
130*1031c584SApple OSS Distributions     nat464_remove_frag64(pbuf_t *, uint32_t, uint16_t, boolean_t);
131*1031c584SApple OSS Distributions 
132*1031c584SApple OSS Distributions uint16_t
133*1031c584SApple OSS Distributions     nat464_cksum_fixup(uint16_t, uint16_t, uint16_t, uint8_t);
134*1031c584SApple OSS Distributions #endif /* !_NET_NAT464_UTILS_H_ */
135