2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2019 Yandex LLC
5 * Copyright (c) 2019 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Copyright (c) 2019 Boris N. Lytochkin <lytboris@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/counter.h>
36 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/rmlock.h>
41 #include <sys/rwlock.h>
42 #include <sys/socket.h>
43 #include <sys/sysctl.h>
46 #include <net/if_var.h>
47 #include <net/if_pflog.h>
50 #include <netinet/in.h>
51 #include <netinet/ip.h>
52 #include <netinet/ip_icmp.h>
53 #include <netinet/ip_var.h>
54 #include <netinet/ip_fw.h>
55 #include <netinet/ip6.h>
56 #include <netinet/icmp6.h>
57 #include <netinet6/ip_fw_nat64.h>
59 #include <netpfil/ipfw/ip_fw_private.h>
60 #include <netpfil/pf/pf.h>
62 #include "nat64clat.h"
64 #define NAT64_LOOKUP(chain, cmd) \
65 (struct nat64clat_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
68 nat64clat_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
71 static uint32_t pktid = 0;
73 memset(plog, 0, sizeof(*plog));
74 plog->length = PFLOG_REAL_HDRLEN;
76 plog->action = PF_NAT;
78 plog->rulenr = htonl(kidx);
80 plog->subrulenr = htonl(pktid);
81 plog->ruleset[0] = '\0';
82 strlcpy(plog->ifname, "NAT64CLAT", sizeof(plog->ifname));
83 ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
87 nat64clat_handle_ip4(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
90 struct pfloghdr loghdr, *logdata;
91 struct in6_addr saddr, daddr;
94 ip = mtod(m, struct ip*);
95 /* source address for CLAT may be private with no harm */
96 if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||
97 nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||
98 nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)
101 memcpy(&saddr, &cfg->base.clat_prefix, sizeof(saddr));
102 nat64_embed_ip4(&saddr, cfg->base.clat_plen, ip->ip_src.s_addr);
103 memcpy(&daddr, &cfg->base.plat_prefix, sizeof(daddr));
104 nat64_embed_ip4(&daddr, cfg->base.plat_plen, ip->ip_dst.s_addr);
105 if (cfg->base.flags & NAT64_LOG) {
107 nat64clat_log(logdata, m, AF_INET, cfg->no.kidx);
110 return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,
115 nat64clat_handle_ip6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
118 struct pfloghdr loghdr, *logdata;
123 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
124 * protocol's headers. Also we skip some checks, that ip6_input(),
125 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
127 ip6 = mtod(m, struct ip6_hdr *);
128 /* Check ip6_dst matches configured prefix */
129 if (memcmp(&ip6->ip6_dst, &cfg->base.clat_prefix,
130 cfg->base.clat_plen / 8) != 0)
132 /* Check ip6_src matches configured prefix */
133 if (memcmp(&ip6->ip6_src, &cfg->base.plat_prefix,
134 cfg->base.plat_plen / 8) != 0)
137 if (cfg->base.flags & NAT64_LOG) {
139 nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);
143 aaddr = nat64_extract_ip4(&ip6->ip6_src, cfg->base.plat_plen);
144 return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
148 nat64clat_handle_icmp6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
151 struct pfloghdr loghdr, *logdata;
152 struct nat64_counters *stats;
153 struct ip6_hdr *ip6i;
154 struct icmp6_hdr *icmp6;
159 stats = &cfg->base.stats;
160 proto = nat64_getlasthdr(m, &hlen);
161 if (proto != IPPROTO_ICMPV6) {
162 NAT64STAT_INC(stats, dropped);
165 icmp6 = mtodo(m, hlen);
166 switch (icmp6->icmp6_type) {
167 case ICMP6_DST_UNREACH:
168 case ICMP6_PACKET_TOO_BIG:
169 case ICMP6_TIME_EXCEED_TRANSIT:
170 case ICMP6_PARAM_PROB:
173 NAT64STAT_INC(stats, dropped);
176 hlen += sizeof(struct icmp6_hdr);
177 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
178 NAT64STAT_INC(stats, dropped);
181 if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
182 m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
184 NAT64STAT_INC(stats, nomem);
185 return (NAT64RETURN);
188 * Use destination address from inner IPv6 header to determine
189 * IPv4 mapped address.
191 ip6i = mtodo(m, hlen);
192 daddr = nat64_extract_ip4(&ip6i->ip6_dst, cfg->base.clat_plen);
194 NAT64STAT_INC(stats, dropped);
197 if (cfg->base.flags & NAT64_LOG) {
199 nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);
202 return (nat64_handle_icmp6(m, 0, daddr, 0, &cfg->base, logdata));
206 ipfw_nat64clat(struct ip_fw_chain *chain, struct ip_fw_args *args,
207 ipfw_insn *cmd, int *done)
210 struct nat64clat_cfg *cfg;
213 IPFW_RLOCK_ASSERT(chain);
215 *done = 0; /* try next rule if not matched */
217 if (cmd->opcode != O_EXTERNAL_ACTION ||
218 cmd->arg1 != V_nat64clat_eid ||
219 icmd->opcode != O_EXTERNAL_INSTANCE ||
220 (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
223 switch (args->f_id.addr_type) {
225 ret = nat64clat_handle_ip4(chain, cfg, args->m);
228 ret = nat64clat_handle_ip6(chain, cfg, args->m);
234 if (ret == NAT64SKIP) {
236 * In case when packet is ICMPv6 message from an intermediate
237 * router, the source address of message will not match the
238 * addresses from configured prefixes.
240 if (args->f_id.proto != IPPROTO_ICMPV6)
243 ret = nat64clat_handle_icmp6(chain, cfg, args->m);
246 if (ret == NAT64SKIP)
249 *done = 1; /* terminate the search */
250 if (ret == NAT64MFREE)
254 return (IP_FW_NAT64);