1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2015-2019 Yandex LLC
5 * Copyright (c) 2015-2019 Andrey V. Elsukov <ae@FreeBSD.org>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/counter.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/mbuf.h>
36 #include <sys/module.h>
37 #include <sys/rmlock.h>
38 #include <sys/rwlock.h>
39 #include <sys/socket.h>
40 #include <sys/sysctl.h>
41
42 #include <net/if.h>
43 #include <net/if_var.h>
44 #include <net/if_pflog.h>
45 #include <net/pfil.h>
46
47 #include <netinet/in.h>
48 #include <netinet/ip.h>
49 #include <netinet/ip_icmp.h>
50 #include <netinet/ip_var.h>
51 #include <netinet/ip_fw.h>
52 #include <netinet/ip6.h>
53 #include <netinet/icmp6.h>
54 #include <netinet6/ip_fw_nat64.h>
55
56 #include <netpfil/ipfw/ip_fw_private.h>
57 #include <netpfil/pf/pf.h>
58
59 #include "nat64stl.h"
60
61 #define NAT64_LOOKUP(chain, cmd) \
62 (struct nat64stl_cfg *)SRV_OBJECT((chain), (cmd)->arg1)
63
64 static void
nat64stl_log(struct pfloghdr * plog,struct mbuf * m,sa_family_t family,uint32_t kidx)65 nat64stl_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,
66 uint32_t kidx)
67 {
68 static uint32_t pktid = 0;
69
70 memset(plog, 0, sizeof(*plog));
71 plog->length = PFLOG_HDRLEN;
72 plog->af = family;
73 plog->action = PF_NAT;
74 plog->dir = PF_IN;
75 plog->rulenr = htonl(kidx);
76 pktid++;
77 plog->subrulenr = htonl(pktid);
78 plog->ruleset[0] = '\0';
79 strlcpy(plog->ifname, "NAT64STL", sizeof(plog->ifname));
80 ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);
81 }
82
83 static int
nat64stl_handle_ip4(struct ip_fw_chain * chain,struct nat64stl_cfg * cfg,struct mbuf * m,uint32_t tablearg)84 nat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
85 struct mbuf *m, uint32_t tablearg)
86 {
87 struct pfloghdr loghdr, *logdata;
88 struct in6_addr saddr, daddr;
89 struct ip *ip;
90
91 ip = mtod(m, struct ip*);
92 if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||
93 nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||
94 nat64_check_private_ip4(&cfg->base, ip->ip_src.s_addr) != 0 ||
95 nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)
96 return (NAT64SKIP);
97
98 daddr = TARG_VAL(chain, tablearg, nh6);
99 if (nat64_check_ip6(&daddr) != 0)
100 return (NAT64MFREE);
101
102 saddr = cfg->base.plat_prefix;
103 nat64_embed_ip4(&saddr, cfg->base.plat_plen, ip->ip_src.s_addr);
104 if (cfg->base.flags & NAT64_LOG) {
105 logdata = &loghdr;
106 nat64stl_log(logdata, m, AF_INET, cfg->no.kidx);
107 } else
108 logdata = NULL;
109 return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,
110 logdata));
111 }
112
113 static int
nat64stl_handle_ip6(struct ip_fw_chain * chain,struct nat64stl_cfg * cfg,struct mbuf * m,uint32_t tablearg)114 nat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,
115 struct mbuf *m, uint32_t tablearg)
116 {
117 struct pfloghdr loghdr, *logdata;
118 struct ip6_hdr *ip6;
119 uint32_t aaddr;
120
121 aaddr = htonl(TARG_VAL(chain, tablearg, nh4));
122 if (nat64_check_private_ip4(&cfg->base, aaddr) != 0) {
123 NAT64STAT_INC(&cfg->base.stats, dropped);
124 return (NAT64MFREE);
125 }
126 /*
127 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level
128 * protocol's headers. Also we skip some checks, that ip6_input(),
129 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.
130 */
131 ip6 = mtod(m, struct ip6_hdr *);
132 /* Check ip6_dst matches configured prefix */
133 if (memcmp(&ip6->ip6_dst, &cfg->base.plat_prefix,
134 cfg->base.plat_plen / 8) != 0)
135 return (NAT64SKIP);
136
137 if (cfg->base.flags & NAT64_LOG) {
138 logdata = &loghdr;
139 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
140 } else
141 logdata = NULL;
142 return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));
143 }
144
145 static int
nat64stl_handle_icmp6(struct ip_fw_chain * chain,struct nat64stl_cfg * cfg,struct mbuf * m)146 nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_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 tablearg;
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 if (ipfw_lookup_table(chain, cfg->map64,
191 sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {
192 m_freem(m);
193 return (NAT64RETURN);
194 }
195 if (cfg->base.flags & NAT64_LOG) {
196 logdata = &loghdr;
197 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);
198 } else
199 logdata = NULL;
200 return (nat64_handle_icmp6(m, 0,
201 htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata));
202 }
203
204 int
ipfw_nat64stl(struct ip_fw_chain * chain,struct ip_fw_args * args,ipfw_insn * cmd,int * done)205 ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,
206 ipfw_insn *cmd, int *done)
207 {
208 ipfw_insn *icmd;
209 struct nat64stl_cfg *cfg;
210 in_addr_t dst4;
211 uint32_t tablearg;
212 int ret;
213
214 IPFW_RLOCK_ASSERT(chain);
215
216 *done = 0; /* try next rule if not matched */
217 icmd = cmd + 1;
218 if (cmd->opcode != O_EXTERNAL_ACTION ||
219 cmd->arg1 != V_nat64stl_eid ||
220 icmd->opcode != O_EXTERNAL_INSTANCE ||
221 (cfg = NAT64_LOOKUP(chain, icmd)) == NULL)
222 return (0);
223
224 switch (args->f_id.addr_type) {
225 case 4:
226 dst4 = htonl(args->f_id.dst_ip);
227 ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),
228 &dst4, &tablearg);
229 break;
230 case 6:
231 ret = ipfw_lookup_table(chain, cfg->map64,
232 sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);
233 break;
234 default:
235 return (0);
236 }
237 if (ret == 0) {
238 /*
239 * In case when packet is ICMPv6 message from an intermediate
240 * router, the source address of message will not match the
241 * addresses from our map64 table.
242 */
243 if (args->f_id.proto != IPPROTO_ICMPV6)
244 return (0);
245
246 ret = nat64stl_handle_icmp6(chain, cfg, args->m);
247 } else {
248 if (args->f_id.addr_type == 4)
249 ret = nat64stl_handle_ip4(chain, cfg, args->m,
250 tablearg);
251 else
252 ret = nat64stl_handle_ip6(chain, cfg, args->m,
253 tablearg);
254 }
255 if (ret == NAT64SKIP)
256 return (0);
257
258 *done = 1; /* terminate the search */
259 if (ret == NAT64MFREE)
260 m_freem(args->m);
261 args->m = NULL;
262 return (IP_FW_NAT64);
263 }
264