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