xref: /freebsd/sys/netpfil/ipfw/nat64/nat64clat.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
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>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
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.
17  *
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.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/counter.h>
34 #include <sys/kernel.h>
35 #include <sys/lock.h>
36 #include <sys/mbuf.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>
42 
43 #include <net/if.h>
44 #include <net/if_var.h>
45 #include <net/if_pflog.h>
46 #include <net/pfil.h>
47 
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>
56 
57 #include <netpfil/ipfw/ip_fw_private.h>
58 #include <netpfil/pf/pf.h>
59 
60 #include "nat64clat.h"
61 
62 #define	NAT64_LOOKUP(chain, cmd)	\
63 	(struct nat64clat_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
64 
65 static void
66 nat64clat_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
67     uint32_t kidx)
68 {
69 	static uint32_t pktid = 0;
70 
71 	memset(plog, 0, sizeof(*plog));
72 	plog->length = PFLOG_HDRLEN;
73 	plog->af = family;
74 	plog->action = PF_NAT;
75 	plog->dir = PF_IN;
76 	plog->rulenr = htonl(kidx);
77 	pktid++;
78 	plog->subrulenr = htonl(pktid);
79 	plog->ruleset[0] = '\0';
80 	strlcpy(plog->ifname, "NAT64CLAT", sizeof(plog->ifname));
81 	ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
82 }
83 
84 static int
85 nat64clat_handle_ip4(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
86     struct mbuf *m)
87 {
88 	struct pfloghdr loghdr, *logdata;
89 	struct in6_addr saddr, daddr;
90 	struct ip *ip;
91 
92 	ip = mtod(m, struct ip*);
93 	/* source address for CLAT may be private with no harm */
94 	if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||
95 	    nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||
96 	    nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)
97 		return (NAT64SKIP);
98 
99 	memcpy(&saddr, &cfg->base.clat_prefix, sizeof(saddr));
100 	nat64_embed_ip4(&saddr, cfg->base.clat_plen, ip->ip_src.s_addr);
101 	memcpy(&daddr, &cfg->base.plat_prefix, sizeof(daddr));
102 	nat64_embed_ip4(&daddr, cfg->base.plat_plen, ip->ip_dst.s_addr);
103 	if (cfg->base.flags & NAT64_LOG) {
104 		logdata = &loghdr;
105 		nat64clat_log(logdata, m, AF_INET, cfg->no.kidx);
106 	} else
107 		logdata = NULL;
108 	return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,
109 	    logdata));
110 }
111 
112 static int
113 nat64clat_handle_ip6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
114     struct mbuf *m)
115 {
116 	struct pfloghdr loghdr, *logdata;
117 	struct ip6_hdr *ip6;
118 	uint32_t aaddr;
119 
120 	/*
121 	 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
122 	 * protocol's headers. Also we skip some checks, that ip6_input(),
123 	 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
124 	 */
125 	ip6 = mtod(m, struct ip6_hdr *);
126 	/* Check ip6_dst matches configured prefix */
127 	if (memcmp(&ip6->ip6_dst, &cfg->base.clat_prefix,
128 	    cfg->base.clat_plen / 8) != 0)
129 		return (NAT64SKIP);
130 	/* Check ip6_src matches configured prefix */
131 	if (memcmp(&ip6->ip6_src, &cfg->base.plat_prefix,
132 	    cfg->base.plat_plen / 8) != 0)
133 		return (NAT64SKIP);
134 
135 	if (cfg->base.flags & NAT64_LOG) {
136 		logdata = &loghdr;
137 		nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);
138 	} else
139 		logdata = NULL;
140 
141 	aaddr = nat64_extract_ip4(&ip6->ip6_src, cfg->base.plat_plen);
142 	return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
143 }
144 
145 static int
146 nat64clat_handle_icmp6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,
147     struct mbuf *m)
148 {
149 	struct pfloghdr loghdr, *logdata;
150 	struct nat64_counters *stats;
151 	struct ip6_hdr *ip6i;
152 	struct icmp6_hdr *icmp6;
153 	uint32_t daddr;
154 	int hlen, proto;
155 
156 	hlen = 0;
157 	stats = &cfg->base.stats;
158 	proto = nat64_getlasthdr(m, &hlen);
159 	if (proto != IPPROTO_ICMPV6) {
160 		NAT64STAT_INC(stats, dropped);
161 		return (NAT64MFREE);
162 	}
163 	icmp6 = mtodo(m, hlen);
164 	switch (icmp6->icmp6_type) {
165 	case ICMP6_DST_UNREACH:
166 	case ICMP6_PACKET_TOO_BIG:
167 	case ICMP6_TIME_EXCEED_TRANSIT:
168 	case ICMP6_PARAM_PROB:
169 		break;
170 	default:
171 		NAT64STAT_INC(stats, dropped);
172 		return (NAT64MFREE);
173 	}
174 	hlen += sizeof(struct icmp6_hdr);
175 	if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
176 		NAT64STAT_INC(stats, dropped);
177 		return (NAT64MFREE);
178 	}
179 	if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
180 		m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
181 	if (m == NULL) {
182 		NAT64STAT_INC(stats, nomem);
183 		return (NAT64RETURN);
184 	}
185 	/*
186 	 * Use destination address from inner IPv6 header to determine
187 	 * IPv4 mapped address.
188 	 */
189 	ip6i = mtodo(m, hlen);
190 	daddr = nat64_extract_ip4(&ip6i->ip6_dst, cfg->base.clat_plen);
191 	if (daddr == 0) {
192 		NAT64STAT_INC(stats, dropped);
193 		return (NAT64MFREE);
194 	}
195 	if (cfg->base.flags & NAT64_LOG) {
196 		logdata = &loghdr;
197 		nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);
198 	} else
199 		logdata = NULL;
200 	return (nat64_handle_icmp6(m, 0, daddr, 0, &cfg->base, logdata));
201 }
202 
203 int
204 ipfw_nat64clat(struct ip_fw_chain *chain, struct ip_fw_args *args,
205     ipfw_insn *cmd, int *done)
206 {
207 	ipfw_insn *icmd;
208 	struct nat64clat_cfg *cfg;
209 	int ret;
210 
211 	IPFW_RLOCK_ASSERT(chain);
212 
213 	*done = 0; /* try next rule if not matched */
214 	icmd = cmd + 1;
215 	if (cmd->opcode != O_EXTERNAL_ACTION ||
216 	    cmd->arg1 != V_nat64clat_eid ||
217 	    icmd->opcode != O_EXTERNAL_INSTANCE ||
218 	    (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
219 		return (0);
220 
221 	switch (args->f_id.addr_type) {
222 	case 4:
223 		ret = nat64clat_handle_ip4(chain, cfg, args->m);
224 		break;
225 	case 6:
226 		ret = nat64clat_handle_ip6(chain, cfg, args->m);
227 		break;
228 	default:
229 		return (0);
230 	}
231 
232 	if (ret == NAT64SKIP) {
233 		/*
234 		 * In case when packet is ICMPv6 message from an intermediate
235 		 * router, the source address of message will not match the
236 		 * addresses from configured prefixes.
237 		 */
238 		if (args->f_id.proto != IPPROTO_ICMPV6)
239 			return (0);
240 
241 		ret = nat64clat_handle_icmp6(chain, cfg, args->m);
242 	}
243 
244 	if (ret == NAT64SKIP)
245 		return (0);
246 
247 	*done = 1; /* terminate the search */
248 	if (ret == NAT64MFREE)
249 		m_freem(args->m);
250 
251 	args->m = NULL;
252 	return (IP_FW_NAT64);
253 }
254