2 * Copyright (c) 2015-2016 Yandex LLC
3 * Copyright (c) 2015-2016 Andrey V. Elsukov <ae@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/counter.h>
34 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/rmlock.h>
39 #include <sys/rwlock.h>
40 #include <sys/socket.h>
41 #include <sys/sysctl.h>
44 #include <net/if_var.h>
45 #include <net/if_pflog.h>
48 #include <netinet/in.h>
49 #include <netinet/ip.h>
50 #include <netinet/ip_icmp.h>
51 #include <netinet/ip_var.h>
52 #include <netinet/ip_fw.h>
53 #include <netinet/ip6.h>
54 #include <netinet/icmp6.h>
55 #include <netinet6/ip_fw_nat64.h>
57 #include <netpfil/ipfw/ip_fw_private.h>
58 #include <netpfil/pf/pf.h>
62 #define NAT64_LOOKUP(chain, cmd) \
63 (struct nat64stl_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
66 nat64stl_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
69 static uint32_t pktid = 0;
71 memset(plog, 0, sizeof(*plog));
72 plog->length = PFLOG_REAL_HDRLEN;
74 plog->action = PF_NAT;
76 plog->rulenr = htonl(kidx);
78 plog->subrulenr = htonl(pktid);
79 plog->ruleset[0] = '\0';
80 strlcpy(plog->ifname, "NAT64STL", sizeof(plog->ifname));
81 ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
85 nat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
86 struct mbuf *m, uint32_t tablearg)
88 struct pfloghdr loghdr, *logdata;
89 struct in6_addr saddr, daddr;
92 ip = mtod(m, struct ip*);
93 if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||
94 nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||
95 nat64_check_private_ip4(&cfg->base, ip->ip_src.s_addr) != 0 ||
96 nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)
99 daddr = TARG_VAL(chain, tablearg, nh6);
100 if (nat64_check_ip6(&daddr) != 0)
103 saddr = cfg->base.plat_prefix;
104 nat64_embed_ip4(&saddr, cfg->base.plat_plen, ip->ip_src.s_addr);
105 if (cfg->base.flags & NAT64_LOG) {
107 nat64stl_log(logdata, m, AF_INET, cfg->no.kidx);
110 return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,
115 nat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
116 struct mbuf *m, uint32_t tablearg)
118 struct pfloghdr loghdr, *logdata;
122 aaddr = htonl(TARG_VAL(chain, tablearg, nh4));
123 if (nat64_check_private_ip4(&cfg->base, aaddr) != 0) {
124 NAT64STAT_INC(&cfg->base.stats, dropped);
128 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
129 * protocol's headers. Also we skip some checks, that ip6_input(),
130 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
132 ip6 = mtod(m, struct ip6_hdr *);
133 /* Check ip6_dst matches configured prefix */
134 if (memcmp(&ip6->ip6_dst, &cfg->base.plat_prefix,
135 cfg->base.plat_plen / 8) != 0)
138 if (cfg->base.flags & NAT64_LOG) {
140 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
143 return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
147 nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
150 struct pfloghdr loghdr, *logdata;
151 struct nat64_counters *stats;
152 struct ip6_hdr *ip6i;
153 struct icmp6_hdr *icmp6;
158 stats = &cfg->base.stats;
159 proto = nat64_getlasthdr(m, &hlen);
160 if (proto != IPPROTO_ICMPV6) {
161 NAT64STAT_INC(stats, dropped);
164 icmp6 = mtodo(m, hlen);
165 switch (icmp6->icmp6_type) {
166 case ICMP6_DST_UNREACH:
167 case ICMP6_PACKET_TOO_BIG:
168 case ICMP6_TIME_EXCEED_TRANSIT:
169 case ICMP6_PARAM_PROB:
172 NAT64STAT_INC(stats, dropped);
175 hlen += sizeof(struct icmp6_hdr);
176 if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
177 NAT64STAT_INC(stats, dropped);
180 if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
181 m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
183 NAT64STAT_INC(stats, nomem);
184 return (NAT64RETURN);
187 * Use destination address from inner IPv6 header to determine
188 * IPv4 mapped address.
190 ip6i = mtodo(m, hlen);
191 if (ipfw_lookup_table(chain, cfg->map64,
192 sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {
194 return (NAT64RETURN);
196 if (cfg->base.flags & NAT64_LOG) {
198 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
201 return (nat64_handle_icmp6(m, 0,
202 htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata));
206 ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,
207 ipfw_insn *cmd, int *done)
210 struct nat64stl_cfg *cfg;
215 IPFW_RLOCK_ASSERT(chain);
217 *done = 0; /* try next rule if not matched */
219 if (cmd->opcode != O_EXTERNAL_ACTION ||
220 cmd->arg1 != V_nat64stl_eid ||
221 icmd->opcode != O_EXTERNAL_INSTANCE ||
222 (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
225 switch (args->f_id.addr_type) {
227 dst4 = htonl(args->f_id.dst_ip);
228 ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),
232 ret = ipfw_lookup_table(chain, cfg->map64,
233 sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);
240 * In case when packet is ICMPv6 message from an intermediate
241 * router, the source address of message will not match the
242 * addresses from our map64 table.
244 if (args->f_id.proto != IPPROTO_ICMPV6)
247 ret = nat64stl_handle_icmp6(chain, cfg, args->m);
249 if (args->f_id.addr_type == 4)
250 ret = nat64stl_handle_ip4(chain, cfg, args->m,
253 ret = nat64stl_handle_ip6(chain, cfg, args->m,
256 if (ret == NAT64SKIP)
259 *done = 1; /* terminate the search */
260 if (ret == NAT64MFREE)
263 return (IP_FW_NAT64);