1304046Sae/*-
2346211Sae * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3304046Sae *
4346211Sae * Copyright (c) 2015-2019 Yandex LLC
5346211Sae * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
6346211Sae *
7304046Sae * Redistribution and use in source and binary forms, with or without
8304046Sae * modification, are permitted provided that the following conditions
9304046Sae * are met:
10304046Sae *
11304046Sae * 1. Redistributions of source code must retain the above copyright
12304046Sae *    notice, this list of conditions and the following disclaimer.
13304046Sae * 2. Redistributions in binary form must reproduce the above copyright
14304046Sae *    notice, this list of conditions and the following disclaimer in the
15304046Sae *    documentation and/or other materials provided with the distribution.
16304046Sae *
17304046Sae * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18304046Sae * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19304046Sae * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20304046Sae * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21304046Sae * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22304046Sae * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23304046Sae * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24304046Sae * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25304046Sae * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26304046Sae * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27304046Sae */
28304046Sae
29304046Sae#include <sys/cdefs.h>
30304046Sae__FBSDID("$FreeBSD: stable/11/sys/netpfil/ipfw/nat64/nat64stl.c 346211 2019-04-14 12:35:58Z ae $");
31304046Sae
32304046Sae#include <sys/param.h>
33304046Sae#include <sys/systm.h>
34304046Sae#include <sys/counter.h>
35304046Sae#include <sys/kernel.h>
36304046Sae#include <sys/lock.h>
37304046Sae#include <sys/mbuf.h>
38304046Sae#include <sys/module.h>
39304046Sae#include <sys/rmlock.h>
40304046Sae#include <sys/rwlock.h>
41304046Sae#include <sys/socket.h>
42304046Sae#include <sys/sysctl.h>
43304046Sae
44304046Sae#include <net/if.h>
45304046Sae#include <net/if_var.h>
46304046Sae#include <net/if_pflog.h>
47304046Sae#include <net/pfil.h>
48304046Sae
49304046Sae#include <netinet/in.h>
50304046Sae#include <netinet/ip.h>
51304046Sae#include <netinet/ip_icmp.h>
52304046Sae#include <netinet/ip_var.h>
53304046Sae#include <netinet/ip_fw.h>
54304046Sae#include <netinet/ip6.h>
55304046Sae#include <netinet/icmp6.h>
56304046Sae#include <netinet6/ip_fw_nat64.h>
57304046Sae
58304046Sae#include <netpfil/ipfw/ip_fw_private.h>
59304046Sae#include <netpfil/pf/pf.h>
60304046Sae
61334836Sae#include "nat64stl.h"
62334836Sae
63304046Sae#define	NAT64_LOOKUP(chain, cmd)	\
64304046Sae	(struct nat64stl_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
65304046Sae
66304046Saestatic void
67304046Saenat64stl_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
68304046Sae    uint32_t kidx)
69304046Sae{
70304046Sae	static uint32_t pktid = 0;
71304046Sae
72316446Sae	memset(plog, 0, sizeof(*plog));
73304046Sae	plog->length = PFLOG_REAL_HDRLEN;
74304046Sae	plog->af = family;
75304046Sae	plog->action = PF_NAT;
76304046Sae	plog->dir = PF_IN;
77304046Sae	plog->rulenr = htonl(kidx);
78332765Sae	pktid++;
79332765Sae	plog->subrulenr = htonl(pktid);
80304046Sae	plog->ruleset[0] = '\0';
81304046Sae	strlcpy(plog->ifname, "NAT64STL", sizeof(plog->ifname));
82304046Sae	ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
83304046Sae}
84304046Sae
85304046Saestatic int
86304046Saenat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
87304046Sae    struct mbuf *m, uint32_t tablearg)
88304046Sae{
89304046Sae	struct pfloghdr loghdr, *logdata;
90304046Sae	struct in6_addr saddr, daddr;
91304046Sae	struct ip *ip;
92304046Sae
93304046Sae	ip = mtod(m, struct ip*);
94304046Sae	if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||
95304046Sae	    nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||
96334836Sae	    nat64_check_private_ip4(&cfg->base, ip->ip_src.s_addr) != 0 ||
97334836Sae	    nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)
98304046Sae		return (NAT64SKIP);
99304046Sae
100304046Sae	daddr = TARG_VAL(chain, tablearg, nh6);
101304046Sae	if (nat64_check_ip6(&daddr) != 0)
102304046Sae		return (NAT64MFREE);
103346210Sae
104346210Sae	saddr = cfg->base.plat_prefix;
105346210Sae	nat64_embed_ip4(&saddr, cfg->base.plat_plen, ip->ip_src.s_addr);
106334836Sae	if (cfg->base.flags & NAT64_LOG) {
107304046Sae		logdata = &loghdr;
108304046Sae		nat64stl_log(logdata, m, AF_INET, cfg->no.kidx);
109304046Sae	} else
110304046Sae		logdata = NULL;
111334836Sae	return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,
112304046Sae	    logdata));
113304046Sae}
114304046Sae
115304046Saestatic int
116304046Saenat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
117304046Sae    struct mbuf *m, uint32_t tablearg)
118304046Sae{
119304046Sae	struct pfloghdr loghdr, *logdata;
120304046Sae	struct ip6_hdr *ip6;
121304046Sae	uint32_t aaddr;
122304046Sae
123304046Sae	aaddr = htonl(TARG_VAL(chain, tablearg, nh4));
124346210Sae	if (nat64_check_private_ip4(&cfg->base, aaddr) != 0) {
125346210Sae		NAT64STAT_INC(&cfg->base.stats, dropped);
126346210Sae		return (NAT64MFREE);
127346210Sae	}
128304046Sae	/*
129304046Sae	 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
130304046Sae	 * protocol's headers. Also we skip some checks, that ip6_input(),
131304046Sae	 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
132304046Sae	 */
133304046Sae	ip6 = mtod(m, struct ip6_hdr *);
134304046Sae	/* Check ip6_dst matches configured prefix */
135346210Sae	if (memcmp(&ip6->ip6_dst, &cfg->base.plat_prefix,
136346210Sae	    cfg->base.plat_plen / 8) != 0)
137304046Sae		return (NAT64SKIP);
138304046Sae
139334836Sae	if (cfg->base.flags & NAT64_LOG) {
140304046Sae		logdata = &loghdr;
141304046Sae		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
142304046Sae	} else
143304046Sae		logdata = NULL;
144334836Sae	return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
145304046Sae}
146304046Sae
147304046Saestatic int
148304046Saenat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
149304046Sae    struct mbuf *m)
150304046Sae{
151304046Sae	struct pfloghdr loghdr, *logdata;
152334836Sae	struct nat64_counters *stats;
153304046Sae	struct ip6_hdr *ip6i;
154304046Sae	struct icmp6_hdr *icmp6;
155304046Sae	uint32_t tablearg;
156304046Sae	int hlen, proto;
157304046Sae
158304046Sae	hlen = 0;
159334836Sae	stats = &cfg->base.stats;
160304046Sae	proto = nat64_getlasthdr(m, &hlen);
161304046Sae	if (proto != IPPROTO_ICMPV6) {
162304046Sae		NAT64STAT_INC(stats, dropped);
163304046Sae		return (NAT64MFREE);
164304046Sae	}
165304046Sae	icmp6 = mtodo(m, hlen);
166304046Sae	switch (icmp6->icmp6_type) {
167304046Sae	case ICMP6_DST_UNREACH:
168304046Sae	case ICMP6_PACKET_TOO_BIG:
169304046Sae	case ICMP6_TIME_EXCEED_TRANSIT:
170304046Sae	case ICMP6_PARAM_PROB:
171304046Sae		break;
172304046Sae	default:
173304046Sae		NAT64STAT_INC(stats, dropped);
174304046Sae		return (NAT64MFREE);
175304046Sae	}
176304046Sae	hlen += sizeof(struct icmp6_hdr);
177304046Sae	if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
178304046Sae		NAT64STAT_INC(stats, dropped);
179304046Sae		return (NAT64MFREE);
180304046Sae	}
181304046Sae	if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
182304046Sae		m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
183304046Sae	if (m == NULL) {
184304046Sae		NAT64STAT_INC(stats, nomem);
185304046Sae		return (NAT64RETURN);
186304046Sae	}
187304046Sae	/*
188304046Sae	 * Use destination address from inner IPv6 header to determine
189304046Sae	 * IPv4 mapped address.
190304046Sae	 */
191304046Sae	ip6i = mtodo(m, hlen);
192316446Sae	if (ipfw_lookup_table(chain, cfg->map64,
193304046Sae	    sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {
194304046Sae		m_freem(m);
195304046Sae		return (NAT64RETURN);
196304046Sae	}
197334836Sae	if (cfg->base.flags & NAT64_LOG) {
198304046Sae		logdata = &loghdr;
199304046Sae		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
200304046Sae	} else
201304046Sae		logdata = NULL;
202304046Sae	return (nat64_handle_icmp6(m, 0,
203334836Sae	    htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata));
204304046Sae}
205304046Sae
206304046Saeint
207304046Saeipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,
208304046Sae    ipfw_insn *cmd, int *done)
209304046Sae{
210304046Sae	ipfw_insn *icmd;
211304046Sae	struct nat64stl_cfg *cfg;
212316446Sae	in_addr_t dst4;
213304046Sae	uint32_t tablearg;
214304046Sae	int ret;
215304046Sae
216304046Sae	IPFW_RLOCK_ASSERT(chain);
217304046Sae
218304046Sae	*done = 0; /* try next rule if not matched */
219304046Sae	icmd = cmd + 1;
220304046Sae	if (cmd->opcode != O_EXTERNAL_ACTION ||
221304046Sae	    cmd->arg1 != V_nat64stl_eid ||
222304046Sae	    icmd->opcode != O_EXTERNAL_INSTANCE ||
223304046Sae	    (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
224304046Sae		return (0);
225304046Sae
226304046Sae	switch (args->f_id.addr_type) {
227304046Sae	case 4:
228316446Sae		dst4 = htonl(args->f_id.dst_ip);
229316446Sae		ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),
230316446Sae		    &dst4, &tablearg);
231304046Sae		break;
232304046Sae	case 6:
233316446Sae		ret = ipfw_lookup_table(chain, cfg->map64,
234304046Sae		    sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);
235304046Sae		break;
236304046Sae	default:
237304046Sae		return (0);
238304046Sae	}
239304046Sae	if (ret == 0) {
240304046Sae		/*
241304046Sae		 * In case when packet is ICMPv6 message from an intermediate
242304046Sae		 * router, the source address of message will not match the
243304046Sae		 * addresses from our map64 table.
244304046Sae		 */
245304046Sae		if (args->f_id.proto != IPPROTO_ICMPV6)
246304046Sae			return (0);
247304046Sae
248304046Sae		ret = nat64stl_handle_icmp6(chain, cfg, args->m);
249304046Sae	} else {
250304046Sae		if (args->f_id.addr_type == 4)
251304046Sae			ret = nat64stl_handle_ip4(chain, cfg, args->m,
252304046Sae			    tablearg);
253304046Sae		else
254304046Sae			ret = nat64stl_handle_ip6(chain, cfg, args->m,
255304046Sae			    tablearg);
256304046Sae	}
257304046Sae	if (ret == NAT64SKIP)
258304046Sae		return (0);
259304046Sae
260304046Sae	*done = 1; /* terminate the search */
261304046Sae	if (ret == NAT64MFREE)
262304046Sae		m_freem(args->m);
263304046Sae	args->m = NULL;
264346210Sae	return (IP_FW_NAT64);
265304046Sae}
266304046Sae
267304046Sae
268