Path: blob/main/sys/netpfil/ipfw/nat64/nat64clat.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2019 Yandex LLC4* Copyright (c) 2019 Andrey V. Elsukov <[email protected]>5* Copyright (c) 2019 Boris N. Lytochkin <[email protected]>6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10*11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* 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, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF26* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/systm.h>31#include <sys/counter.h>32#include <sys/kernel.h>33#include <sys/lock.h>34#include <sys/mbuf.h>35#include <sys/module.h>36#include <sys/rmlock.h>37#include <sys/rwlock.h>38#include <sys/socket.h>39#include <sys/sysctl.h>4041#include <net/if.h>42#include <net/if_var.h>43#include <net/if_pflog.h>44#include <net/pfil.h>4546#include <netinet/in.h>47#include <netinet/ip.h>48#include <netinet/ip_icmp.h>49#include <netinet/ip_var.h>50#include <netinet/ip_fw.h>51#include <netinet/ip6.h>52#include <netinet/icmp6.h>53#include <netinet6/ip_fw_nat64.h>5455#include <netpfil/ipfw/ip_fw_private.h>56#include <netpfil/pf/pf.h>5758#include "nat64clat.h"5960#define NAT64_LOOKUP(chain, cmd) \61(struct nat64clat_cfg *)SRV_OBJECT((chain), insntod(cmd, kidx)->kidx)6263static void64nat64clat_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family,65uint32_t kidx)66{67static uint32_t pktid = 0;6869memset(plog, 0, sizeof(*plog));70plog->length = PFLOG_REAL_HDRLEN;71plog->af = family;72plog->action = PF_NAT;73plog->dir = PF_IN;74plog->rulenr = htonl(kidx);75pktid++;76plog->subrulenr = htonl(pktid);77plog->ruleset[0] = '\0';78strlcpy(plog->ifname, "NAT64CLAT", sizeof(plog->ifname));79ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m);80}8182static int83nat64clat_handle_ip4(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,84struct mbuf *m)85{86struct pfloghdr loghdr, *logdata;87struct in6_addr saddr, daddr;88struct ip *ip;8990ip = mtod(m, struct ip*);91/* source address for CLAT may be private with no harm */92if (nat64_check_ip4(ip->ip_src.s_addr) != 0 ||93nat64_check_ip4(ip->ip_dst.s_addr) != 0 ||94nat64_check_private_ip4(&cfg->base, ip->ip_dst.s_addr) != 0)95return (NAT64SKIP);9697memcpy(&saddr, &cfg->base.clat_prefix, sizeof(saddr));98nat64_embed_ip4(&saddr, cfg->base.clat_plen, ip->ip_src.s_addr);99memcpy(&daddr, &cfg->base.plat_prefix, sizeof(daddr));100nat64_embed_ip4(&daddr, cfg->base.plat_plen, ip->ip_dst.s_addr);101if (cfg->base.flags & NAT64_LOG) {102logdata = &loghdr;103nat64clat_log(logdata, m, AF_INET, cfg->no.kidx);104} else105logdata = NULL;106return (nat64_do_handle_ip4(m, &saddr, &daddr, 0, &cfg->base,107logdata));108}109110static int111nat64clat_handle_ip6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,112struct mbuf *m)113{114struct pfloghdr loghdr, *logdata;115struct ip6_hdr *ip6;116uint32_t aaddr;117118/*119* NOTE: we expect ipfw_chk() did m_pullup() up to upper level120* protocol's headers. Also we skip some checks, that ip6_input(),121* ip6_forward(), ip6_fastfwd() and ipfw_chk() already did.122*/123ip6 = mtod(m, struct ip6_hdr *);124/* Check ip6_dst matches configured prefix */125if (memcmp(&ip6->ip6_dst, &cfg->base.clat_prefix,126cfg->base.clat_plen / 8) != 0)127return (NAT64SKIP);128/* Check ip6_src matches configured prefix */129if (memcmp(&ip6->ip6_src, &cfg->base.plat_prefix,130cfg->base.plat_plen / 8) != 0)131return (NAT64SKIP);132133if (cfg->base.flags & NAT64_LOG) {134logdata = &loghdr;135nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);136} else137logdata = NULL;138139aaddr = nat64_extract_ip4(&ip6->ip6_src, cfg->base.plat_plen);140return (nat64_do_handle_ip6(m, aaddr, 0, &cfg->base, logdata));141}142143static int144nat64clat_handle_icmp6(struct ip_fw_chain *chain, struct nat64clat_cfg *cfg,145struct mbuf *m)146{147struct pfloghdr loghdr, *logdata;148struct nat64_counters *stats;149struct ip6_hdr *ip6i;150struct icmp6_hdr *icmp6;151uint32_t daddr;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);188daddr = nat64_extract_ip4(&ip6i->ip6_dst, cfg->base.clat_plen);189if (daddr == 0) {190NAT64STAT_INC(stats, dropped);191return (NAT64MFREE);192}193if (cfg->base.flags & NAT64_LOG) {194logdata = &loghdr;195nat64clat_log(logdata, m, AF_INET6, cfg->no.kidx);196} else197logdata = NULL;198return (nat64_handle_icmp6(m, 0, daddr, 0, &cfg->base, logdata));199}200201int202ipfw_nat64clat(struct ip_fw_chain *chain, struct ip_fw_args *args,203ipfw_insn *cmd, int *done)204{205ipfw_insn *icmd;206struct nat64clat_cfg *cfg;207int ret;208209IPFW_RLOCK_ASSERT(chain);210211*done = 0; /* try next rule if not matched */212icmd = cmd + F_LEN(cmd);213if (cmd->opcode != O_EXTERNAL_ACTION ||214insntod(cmd, kidx)->kidx != V_nat64clat_eid ||215icmd->opcode != O_EXTERNAL_INSTANCE ||216(cfg = NAT64_LOOKUP(chain, icmd)) == NULL)217return (0);218219switch (args->f_id.addr_type) {220case 4:221ret = nat64clat_handle_ip4(chain, cfg, args->m);222break;223case 6:224ret = nat64clat_handle_ip6(chain, cfg, args->m);225break;226default:227return (0);228}229230if (ret == NAT64SKIP) {231/*232* In case when packet is ICMPv6 message from an intermediate233* router, the source address of message will not match the234* addresses from configured prefixes.235*/236if (args->f_id.proto != IPPROTO_ICMPV6)237return (0);238239ret = nat64clat_handle_icmp6(chain, cfg, args->m);240}241242if (ret == NAT64SKIP)243return (0);244245*done = 1; /* terminate the search */246if (ret == NAT64MFREE)247m_freem(args->m);248249args->m = NULL;250return (IP_FW_NAT64);251}252253254