1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 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 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/counter.h> 35 #include <sys/kernel.h> 36 #include <sys/lock.h> 37 #include <sys/mbuf.h> 38 #include <sys/module.h> 39 #include <sys/rmlock.h> 40 #include <sys/rwlock.h> 41 #include <sys/socket.h> 42 #include <sys/sysctl.h> 43 44 #include <net/if.h> 45 #include <net/if_var.h> 46 #include <net/if_pflog.h> 47 #include <net/pfil.h> 48 49 #include <netinet/in.h> 50 #include <netinet/ip.h> 51 #include <netinet/ip_icmp.h> 52 #include <netinet/ip_var.h> 53 #include <netinet/ip_fw.h> 54 #include <netinet/ip6.h> 55 #include <netinet/icmp6.h> 56 #include <netinet6/ip_fw_nat64.h> 57 58 #include <netpfil/ipfw/ip_fw_private.h> 59 #include <netpfil/pf/pf.h> 60 61 #include "nat64stl.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_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(&cfg->base, ip->ip_src.s_addr) != 0 || 97 nat64_check_private_ip4(&cfg->base, 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 104 saddr = cfg->base.plat_prefix; 105 nat64_embed_ip4(&saddr, cfg->base.plat_plen, ip->ip_src.s_addr); 106 if (cfg->base.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->base, 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 if (nat64_check_private_ip4(&cfg->base, aaddr) != 0) { 125 NAT64STAT_INC(&cfg->base.stats, dropped); 126 return (NAT64MFREE); 127 } 128 /* 129 * NOTE: we expect ipfw_chk() did m_pullup() up to upper level 130 * protocol's headers. Also we skip some checks, that ip6_input(), 131 * ip6_forward(), ip6_fastfwd() and ipfw_chk() already did. 132 */ 133 ip6 = mtod(m, struct ip6_hdr *); 134 /* Check ip6_dst matches configured prefix */ 135 if (memcmp(&ip6->ip6_dst, &cfg->base.plat_prefix, 136 cfg->base.plat_plen / 8) != 0) 137 return (NAT64SKIP); 138 139 if (cfg->base.flags & NAT64_LOG) { 140 logdata = &loghdr; 141 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx); 142 } else 143 logdata = NULL; 144 return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata)); 145 } 146 147 static int 148 nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_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 tablearg; 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 if (ipfw_lookup_table(chain, cfg->map64, 193 sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) { 194 m_freem(m); 195 return (NAT64RETURN); 196 } 197 if (cfg->base.flags & NAT64_LOG) { 198 logdata = &loghdr; 199 nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx); 200 } else 201 logdata = NULL; 202 return (nat64_handle_icmp6(m, 0, 203 htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata)); 204 } 205 206 int 207 ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args, 208 ipfw_insn *cmd, int *done) 209 { 210 ipfw_insn *icmd; 211 struct nat64stl_cfg *cfg; 212 in_addr_t dst4; 213 uint32_t tablearg; 214 int ret; 215 216 IPFW_RLOCK_ASSERT(chain); 217 218 *done = 0; /* try next rule if not matched */ 219 icmd = cmd + 1; 220 if (cmd->opcode != O_EXTERNAL_ACTION || 221 cmd->arg1 != V_nat64stl_eid || 222 icmd->opcode != O_EXTERNAL_INSTANCE || 223 (cfg = NAT64_LOOKUP(chain, icmd)) == NULL) 224 return (0); 225 226 switch (args->f_id.addr_type) { 227 case 4: 228 dst4 = htonl(args->f_id.dst_ip); 229 ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t), 230 &dst4, &tablearg); 231 break; 232 case 6: 233 ret = ipfw_lookup_table(chain, cfg->map64, 234 sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg); 235 break; 236 default: 237 return (0); 238 } 239 if (ret == 0) { 240 /* 241 * In case when packet is ICMPv6 message from an intermediate 242 * router, the source address of message will not match the 243 * addresses from our map64 table. 244 */ 245 if (args->f_id.proto != IPPROTO_ICMPV6) 246 return (0); 247 248 ret = nat64stl_handle_icmp6(chain, cfg, args->m); 249 } else { 250 if (args->f_id.addr_type == 4) 251 ret = nat64stl_handle_ip4(chain, cfg, args->m, 252 tablearg); 253 else 254 ret = nat64stl_handle_ip6(chain, cfg, args->m, 255 tablearg); 256 } 257 if (ret == NAT64SKIP) 258 return (0); 259 260 *done = 1; /* terminate the search */ 261 if (ret == NAT64MFREE) 262 m_freem(args->m); 263 args->m = NULL; 264 return (IP_FW_NAT64); 265 } 266