Path: blob/main/sys/netpfil/ipfw/nat64/nat64stl.c
109343 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2015-2019 Yandex LLC4* Copyright (c) 2015-2019 Andrey V. Elsukov <[email protected]>5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions8* are met:9*10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR17* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES18* 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, BUT21* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,22* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY23* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT24* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF25* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.26*/2728#include <sys/param.h>29#include <sys/systm.h>30#include <sys/counter.h>31#include <sys/kernel.h>32#include <sys/lock.h>33#include <sys/mbuf.h>34#include <sys/module.h>35#include <sys/rmlock.h>36#include <sys/rwlock.h>37#include <sys/socket.h>38#include <sys/sysctl.h>3940#include <net/if.h>41#include <net/if_var.h>42#include <net/if_pflog.h>43#include <net/pfil.h>4445#include <netinet/in.h>46#include <netinet/ip.h>47#include <netinet/ip_icmp.h>48#include <netinet/ip_var.h>49#include <netinet/ip_fw.h>50#include <netinet/ip6.h>51#include <netinet/icmp6.h>52#include <netinet6/ip_fw_nat64.h>5354#include <netpfil/ipfw/ip_fw_private.h>55#include <netpfil/pf/pf.h>5657#include "nat64stl.h"5859#define NAT64_LOOKUP(chain, cmd) \60(struct nat64stl_cfg *)SRV_OBJECT((chain), insntod(cmd, kidx)->kidx)6162static void63nat64stl_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,64uint32_t kidx)65{66static uint32_t pktid = 0;6768memset(plog, 0, sizeof(*plog));69plog->length = PFLOG_REAL_HDRLEN;70plog->af = family;71plog->action = PF_NAT;72plog->dir = PF_IN;73plog->rulenr = htonl(kidx);74pktid++;75plog->subrulenr = htonl(pktid);76plog->ruleset[0] = '\0';77strlcpy(plog->ifname, "NAT64STL", sizeof(plog->ifname));78ipfw_pflog_tap(plog, m);79}8081static int82nat64stl_handle_ip4(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,83struct mbuf *m, uint32_t tablearg)84{85struct pfloghdr loghdr, *logdata;86struct in6_addr saddr, daddr;87struct ip *ip;8889ip = mtod(m, struct ip*);90if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||91nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||92nat64_check_private_ip4(&cfg->base, ip->ip_src.s_addr) != 0 ||93nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)94return (NAT64SKIP);9596daddr = TARG_VAL(chain, tablearg, nh6);97if (nat64_check_ip6(&daddr) != 0)98return (NAT64MFREE);99100saddr = cfg->base.plat_prefix;101nat64_embed_ip4(&saddr, cfg->base.plat_plen, ip->ip_src.s_addr);102if (cfg->base.flags & NAT64_LOG) {103logdata = &loghdr;104nat64stl_log(logdata, m, AF_INET, cfg->no.kidx);105} else106logdata = NULL;107return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,108logdata));109}110111static int112nat64stl_handle_ip6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,113struct mbuf *m, uint32_t tablearg)114{115struct pfloghdr loghdr, *logdata;116struct ip6_hdr *ip6;117uint32_t aaddr;118119aaddr = htonl(TARG_VAL(chain, tablearg, nh4));120if (nat64_check_private_ip4(&cfg->base, aaddr) != 0) {121NAT64STAT_INC(&cfg->base.stats, dropped);122return (NAT64MFREE);123}124/*125* NOTE: we expect ipfw_chk() did m_pullup() up to upper level126* protocol's headers. Also we skip some checks, that ip6_input(),127* ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.128*/129ip6 = mtod(m, struct ip6_hdr *);130/* Check ip6_dst matches configured prefix */131if (memcmp(&ip6->ip6_dst, &cfg->base.plat_prefix,132cfg->base.plat_plen / 8) != 0)133return (NAT64SKIP);134135if (cfg->base.flags & NAT64_LOG) {136logdata = &loghdr;137nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);138} else139logdata = NULL;140return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));141}142143static int144nat64stl_handle_icmp6(struct ip_fw_chain *chain, struct nat64stl_cfg *cfg,145struct mbuf *m)146{147struct pfloghdr loghdr, *logdata;148struct nat64_counters *stats;149struct ip6_hdr *ip6i;150struct icmp6_hdr *icmp6;151uint32_t tablearg;152int hlen, proto;153154hlen = 0;155stats = &cfg->base.stats;156proto = nat64_getlasthdr(m, &hlen);157if (proto != IPPROTO_ICMPV6) {158NAT64STAT_INC(stats, dropped);159return (NAT64MFREE);160}161icmp6 = mtodo(m, hlen);162switch (icmp6->icmp6_type) {163case ICMP6_DST_UNREACH:164case ICMP6_PACKET_TOO_BIG:165case ICMP6_TIME_EXCEED_TRANSIT:166case ICMP6_PARAM_PROB:167break;168default:169NAT64STAT_INC(stats, dropped);170return (NAT64MFREE);171}172hlen += sizeof(struct icmp6_hdr);173if (m->m_pkthdr.len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN) {174NAT64STAT_INC(stats, dropped);175return (NAT64MFREE);176}177if (m->m_len < hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN)178m = m_pullup(m, hlen + sizeof(struct ip6_hdr) + ICMP_MINLEN);179if (m == NULL) {180NAT64STAT_INC(stats, nomem);181return (NAT64RETURN);182}183/*184* Use destination address from inner IPv6 header to determine185* IPv4 mapped address.186*/187ip6i = mtodo(m, hlen);188if (ipfw_lookup_table(chain, cfg->map64,189sizeof(struct in6_addr), &ip6i->ip6_dst, &tablearg) == 0) {190m_freem(m);191return (NAT64RETURN);192}193if (cfg->base.flags & NAT64_LOG) {194logdata = &loghdr;195nat64stl_log(logdata, m, AF_INET6, cfg->no.kidx);196} else197logdata = NULL;198return (nat64_handle_icmp6(m, 0,199htonl(TARG_VAL(chain, tablearg, nh4)), 0, &cfg->base, logdata));200}201202int203ipfw_nat64stl(struct ip_fw_chain *chain, struct ip_fw_args *args,204ipfw_insn *cmd, int *done)205{206struct nat64stl_cfg *cfg;207ipfw_insn *icmd;208in_addr_t dst4;209uint32_t tablearg;210int ret;211212IPFW_RLOCK_ASSERT(chain);213214*done = 0; /* try next rule if not matched */215icmd = cmd + F_LEN(cmd);216if (cmd->opcode != O_EXTERNAL_ACTION ||217insntod(cmd, kidx)->kidx != V_nat64stl_eid ||218icmd->opcode != O_EXTERNAL_INSTANCE ||219(cfg = NAT64_LOOKUP(chain, icmd)) == NULL)220return (0);221222switch (args->f_id.addr_type) {223case 4:224dst4 = htonl(args->f_id.dst_ip);225ret = ipfw_lookup_table(chain, cfg->map46, sizeof(in_addr_t),226&dst4, &tablearg);227break;228case 6:229ret = ipfw_lookup_table(chain, cfg->map64,230sizeof(struct in6_addr), &args->f_id.src_ip6, &tablearg);231break;232default:233return (0);234}235if (ret == 0) {236/*237* In case when packet is ICMPv6 message from an intermediate238* router, the source address of message will not match the239* addresses from our map64 table.240*/241if (args->f_id.proto != IPPROTO_ICMPV6)242return (0);243244ret = nat64stl_handle_icmp6(chain, cfg, args->m);245} else {246if (args->f_id.addr_type == 4)247ret = nat64stl_handle_ip4(chain, cfg, args->m,248tablearg);249else250ret = nat64stl_handle_ip6(chain, cfg, args->m,251tablearg);252}253if (ret == NAT64SKIP)254return (0);255256*done = 1; /* terminate the search */257if (ret == NAT64MFREE)258m_freem(args->m);259args->m = NULL;260return (IP_FW_NAT64);261}262263264