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