xref: /freebsd/sys/netpfil/ipfw/nat64/nat64stl.c (revision ca987d4641cdcd7f27e153db17c5bf064934faf5)
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/ipfw/nat64/ip_fw_nat64.h>
59 #include <netpfil/ipfw/nat64/nat64_translate.h>
60 #include <netpfil/ipfw/nat64/nat64stl.h>
61 #include <netpfil/pf/pf.h>
62 
63 #define	NAT64_LOOKUP(chain, cmd)	\
64 	(struct nat64stl_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
65 
66 static void
67 nat64stl_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
68     uint32_t kidx)
69 {
70 	static uint32_t pktid = 0;
71 
72 	memset(plog, 0, sizeof(*plog));
73 	plog->length = PFLOG_REAL_HDRLEN;
74 	plog->af = family;
75 	plog->action = PF_NAT;
76 	plog->dir = PF_IN;
77 	plog->rulenr = htonl(kidx);
78 	pktid++;
79 	plog->subrulenr = htonl(pktid);
80 	plog->ruleset[0] = '\0';
81 	strlcpy(plog->ifname, "NAT64STL", sizeof(plog->ifname));
82 	ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
83 }
84 
85 static int
86 nat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
87     struct mbuf *m, uint32_t tablearg)
88 {
89 	struct pfloghdr loghdr, *logdata;
90 	struct in6_addr saddr, daddr;
91 	struct ip *ip;
92 
93 	ip = mtod(m, struct ip*);
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(ip->ip_src.s_addr) != 0 ||
97 	    nat64_check_private_ip4(ip->ip_dst.s_addr) != 0)
98 		return (NAT64SKIP);
99 
100 	daddr = TARG_VAL(chain, tablearg, nh6);
101 	if (nat64_check_ip6(&daddr) != 0)
102 		return (NAT64MFREE);
103 	saddr = cfg->prefix6;
104 	nat64_set_ip4(&saddr, ip->ip_src.s_addr);
105 
106 	if (cfg->flags & NAT64_LOG) {
107 		logdata = &loghdr;
108 		nat64stl_log(logdata, m, AF_INET, cfg->no.kidx);
109 	} else
110 		logdata = NULL;
111 	return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->stats,
112 	    logdata));
113 }
114 
115 static int
116 nat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
117     struct mbuf *m, uint32_t tablearg)
118 {
119 	struct pfloghdr loghdr, *logdata;
120 	struct ip6_hdr *ip6;
121 	uint32_t aaddr;
122 
123 	aaddr = htonl(TARG_VAL(chain, tablearg, nh4));
124 
125 	/*
126 	 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
127 	 * protocol's headers. Also we skip some checks, that ip6_input(),
128 	 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
129 	 */
130 	ip6 = mtod(m, struct ip6_hdr *);
131 	/* Check ip6_dst matches configured prefix */
132 	if (bcmp(&ip6->ip6_dst, &cfg->prefix6, cfg->plen6 / 8) != 0)
133 		return (NAT64SKIP);
134 
135 	if (cfg->flags & NAT64_LOG) {
136 		logdata = &loghdr;
137 		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
138 	} else
139 		logdata = NULL;
140 	return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->stats, logdata));
141 }
142 
143 static int
144 nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
145     struct mbuf *m)
146 {
147 	struct pfloghdr loghdr, *logdata;
148 	nat64_stats_block *stats;
149 	struct ip6_hdr *ip6i;
150 	struct icmp6_hdr *icmp6;
151 	uint32_t tablearg;
152 	int hlen, proto;
153 
154 	hlen = 0;
155 	stats = &cfg->stats;
156 	proto = nat64_getlasthdr(m, &hlen);
157 	if (proto != IPPROTO_ICMPV6) {
158 		NAT64STAT_INC(stats, dropped);
159 		return (NAT64MFREE);
160 	}
161 	icmp6 = mtodo(m, hlen);
162 	switch (icmp6->icmp6_type) {
163 	case ICMP6_DST_UNREACH:
164 	case ICMP6_PACKET_TOO_BIG:
165 	case ICMP6_TIME_EXCEED_TRANSIT:
166 	case ICMP6_PARAM_PROB:
167 		break;
168 	default:
169 		NAT64STAT_INC(stats, dropped);
170 		return (NAT64MFREE);
171 	}
172 	hlen += sizeof(struct icmp6_hdr);
173 	if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {
174 		NAT64STAT_INC(stats, dropped);
175 		return (NAT64MFREE);
176 	}
177 	if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)
178 		m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);
179 	if (m == NULL) {
180 		NAT64STAT_INC(stats, nomem);
181 		return (NAT64RETURN);
182 	}
183 	/*
184 	 * Use destination address from inner IPv6 header to determine
185 	 * IPv4 mapped address.
186 	 */
187 	ip6i = mtodo(m, hlen);
188 	if (ipfw_lookup_table(chain, cfg->map64,
189 	    sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {
190 		m_freem(m);
191 		return (NAT64RETURN);
192 	}
193 	if (cfg->flags & NAT64_LOG) {
194 		logdata = &loghdr;
195 		nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
196 	} else
197 		logdata = NULL;
198 	return (nat64_handle_icmp6(m, 0,
199 	    htonl(TARG_VAL(chain, tablearg, nh4)), 0, stats, logdata));
200 }
201 
202 int
203 ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,
204     ipfw_insn *cmd, int *done)
205 {
206 	ipfw_insn *icmd;
207 	struct nat64stl_cfg *cfg;
208 	in_addr_t dst4;
209 	uint32_t tablearg;
210 	int ret;
211 
212 	IPFW_RLOCK_ASSERT(chain);
213 
214 	*done = 0; /* try next rule if not matched */
215 	icmd = cmd + 1;
216 	if (cmd->opcode != O_EXTERNAL_ACTION ||
217 	    cmd->arg1 != V_nat64stl_eid ||
218 	    icmd->opcode != O_EXTERNAL_INSTANCE ||
219 	    (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
220 		return (0);
221 
222 	switch (args->f_id.addr_type) {
223 	case 4:
224 		dst4 = htonl(args->f_id.dst_ip);
225 		ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),
226 		    &dst4, &tablearg);
227 		break;
228 	case 6:
229 		ret = ipfw_lookup_table(chain, cfg->map64,
230 		    sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);
231 		break;
232 	default:
233 		return (0);
234 	}
235 	if (ret == 0) {
236 		/*
237 		 * In case when packet is ICMPv6 message from an intermediate
238 		 * router, the source address of message will not match the
239 		 * addresses from our map64 table.
240 		 */
241 		if (args->f_id.proto != IPPROTO_ICMPV6)
242 			return (0);
243 
244 		ret = nat64stl_handle_icmp6(chain, cfg, args->m);
245 	} else {
246 		if (args->f_id.addr_type == 4)
247 			ret = nat64stl_handle_ip4(chain, cfg, args->m,
248 			    tablearg);
249 		else
250 			ret = nat64stl_handle_ip6(chain, cfg, args->m,
251 			    tablearg);
252 	}
253 	if (ret == NAT64SKIP)
254 		return (0);
255 
256 	*done = 1; /* terminate the search */
257 	if (ret == NAT64MFREE)
258 		m_freem(args->m);
259 	args->m = NULL;
260 	return (IP_FW_DENY);
261 }
262 
263 
264