xref: /freebsd/sys/netpfil/ipfw/nat64/nat64stl.c (revision 0957b409)
1 /*-
2  * Copyright (c) 2015-2016 Yandex LLC
3  * Copyright (c) 2015-2016 Andrey V. Elsukov <ae@FreeBSD.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
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.
15  *
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.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
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 "nat64stl.h"
61 
62 #define	NAT64_LOOKUP(chain, cmd)	\
63 	(struct nat64stl_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
64 
65 static void
66 nat64stl_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_REAL_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, "NAT64STL", sizeof(plog->ifname));
81 	ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
82 }
83 
84 static int
85 nat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
86     struct mbuf *m, uint32_t tablearg)
87 {
88 	struct pfloghdr loghdr, *logdata;
89 	struct in6_addr saddr, daddr;
90 	struct ip *ip;
91 
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)
97 		return (NAT64SKIP);
98 
99 	daddr = TARG_VAL(chain, tablearg, nh6);
100 	if (nat64_check_ip6(&daddr) != 0)
101 		return (NAT64MFREE);
102 	nat64_embed_ip4(&cfg->base, ip->ip_src.s_addr, &saddr);
103 	if (cfg->base.flags & NAT64_LOG) {
104 		logdata = &loghdr;
105 		nat64stl_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 nat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
114     struct mbuf *m, uint32_t tablearg)
115 {
116 	struct pfloghdr loghdr, *logdata;
117 	struct ip6_hdr *ip6;
118 	uint32_t aaddr;
119 
120 	aaddr = htonl(TARG_VAL(chain, tablearg, nh4));
121 
122 	/*
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.
126 	 */
127 	ip6 = mtod(m, struct ip6_hdr *);
128 	/* Check ip6_dst matches configured prefix */
129 	if (bcmp(&ip6->ip6_dst, &cfg->base.prefix6, cfg->base.plen6 / 8) != 0)
130 		return (NAT64SKIP);
131 
132 	if (cfg->base.flags & NAT64_LOG) {
133 		logdata = &loghdr;
134 		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
135 	} else
136 		logdata = NULL;
137 	return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
138 }
139 
140 static int
141 nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
142     struct mbuf *m)
143 {
144 	struct pfloghdr loghdr, *logdata;
145 	struct nat64_counters *stats;
146 	struct ip6_hdr *ip6i;
147 	struct icmp6_hdr *icmp6;
148 	uint32_t tablearg;
149 	int hlen, proto;
150 
151 	hlen = 0;
152 	stats = &cfg->base.stats;
153 	proto = nat64_getlasthdr(m, &hlen);
154 	if (proto != IPPROTO_ICMPV6) {
155 		NAT64STAT_INC(stats, dropped);
156 		return (NAT64MFREE);
157 	}
158 	icmp6 = mtodo(m, hlen);
159 	switch (icmp6->icmp6_type) {
160 	case ICMP6_DST_UNREACH:
161 	case ICMP6_PACKET_TOO_BIG:
162 	case ICMP6_TIME_EXCEED_TRANSIT:
163 	case ICMP6_PARAM_PROB:
164 		break;
165 	default:
166 		NAT64STAT_INC(stats, dropped);
167 		return (NAT64MFREE);
168 	}
169 	hlen += sizeof(struct icmp6_hdr);
170 	if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
171 		NAT64STAT_INC(stats, dropped);
172 		return (NAT64MFREE);
173 	}
174 	if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
175 		m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
176 	if (m == NULL) {
177 		NAT64STAT_INC(stats, nomem);
178 		return (NAT64RETURN);
179 	}
180 	/*
181 	 * Use destination address from inner IPv6 header to determine
182 	 * IPv4 mapped address.
183 	 */
184 	ip6i = mtodo(m, hlen);
185 	if (ipfw_lookup_table(chain, cfg->map64,
186 	    sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {
187 		m_freem(m);
188 		return (NAT64RETURN);
189 	}
190 	if (cfg->base.flags & NAT64_LOG) {
191 		logdata = &loghdr;
192 		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
193 	} else
194 		logdata = NULL;
195 	return (nat64_handle_icmp6(m, 0,
196 	    htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata));
197 }
198 
199 int
200 ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,
201     ipfw_insn *cmd, int *done)
202 {
203 	ipfw_insn *icmd;
204 	struct nat64stl_cfg *cfg;
205 	in_addr_t dst4;
206 	uint32_t tablearg;
207 	int ret;
208 
209 	IPFW_RLOCK_ASSERT(chain);
210 
211 	*done = 0; /* try next rule if not matched */
212 	icmd = cmd + 1;
213 	if (cmd->opcode != O_EXTERNAL_ACTION ||
214 	    cmd->arg1 != V_nat64stl_eid ||
215 	    icmd->opcode != O_EXTERNAL_INSTANCE ||
216 	    (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
217 		return (0);
218 
219 	switch (args->f_id.addr_type) {
220 	case 4:
221 		dst4 = htonl(args->f_id.dst_ip);
222 		ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),
223 		    &dst4, &tablearg);
224 		break;
225 	case 6:
226 		ret = ipfw_lookup_table(chain, cfg->map64,
227 		    sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);
228 		break;
229 	default:
230 		return (0);
231 	}
232 	if (ret == 0) {
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 our map64 table.
237 		 */
238 		if (args->f_id.proto != IPPROTO_ICMPV6)
239 			return (0);
240 
241 		ret = nat64stl_handle_icmp6(chain, cfg, args->m);
242 	} else {
243 		if (args->f_id.addr_type == 4)
244 			ret = nat64stl_handle_ip4(chain, cfg, args->m,
245 			    tablearg);
246 		else
247 			ret = nat64stl_handle_ip6(chain, cfg, args->m,
248 			    tablearg);
249 	}
250 	if (ret == NAT64SKIP)
251 		return (0);
252 
253 	*done = 1; /* terminate the search */
254 	if (ret == NAT64MFREE)
255 		m_freem(args->m);
256 	args->m = NULL;
257 	return (IP_FW_DENY);
258 }
259 
260 
261