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