nat64lsn.c revision 334836
1304046Sae/*- 2304046Sae * Copyright (c) 2015-2016 Yandex LLC 3304046Sae * Copyright (c) 2015 Alexander V. Chernikov <melifaro@FreeBSD.org> 4304046Sae * Copyright (c) 2016 Andrey V. Elsukov <ae@FreeBSD.org> 5304046Sae * All rights reserved. 6304046Sae * 7304046Sae * Redistribution and use in source and binary forms, with or without 8304046Sae * modification, are permitted provided that the following conditions 9304046Sae * are met: 10304046Sae * 11304046Sae * 1. Redistributions of source code must retain the above copyright 12304046Sae * notice, this list of conditions and the following disclaimer. 13304046Sae * 2. Redistributions in binary form must reproduce the above copyright 14304046Sae * notice, this list of conditions and the following disclaimer in the 15304046Sae * documentation and/or other materials provided with the distribution. 16304046Sae * 17304046Sae * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18304046Sae * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19304046Sae * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20304046Sae * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21304046Sae * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22304046Sae * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23304046Sae * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24304046Sae * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25304046Sae * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26304046Sae * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27304046Sae */ 28304046Sae 29304046Sae#include <sys/cdefs.h> 30304046Sae__FBSDID("$FreeBSD: stable/11/sys/netpfil/ipfw/nat64/nat64lsn.c 334836 2018-06-08 10:09:30Z ae $"); 31304046Sae 32304046Sae#include <sys/param.h> 33304046Sae#include <sys/systm.h> 34304046Sae#include <sys/counter.h> 35304046Sae#include <sys/errno.h> 36304046Sae#include <sys/kernel.h> 37304046Sae#include <sys/lock.h> 38304046Sae#include <sys/malloc.h> 39304046Sae#include <sys/mbuf.h> 40304046Sae#include <sys/module.h> 41304046Sae#include <sys/rmlock.h> 42304046Sae#include <sys/rwlock.h> 43304046Sae#include <sys/socket.h> 44304046Sae#include <sys/queue.h> 45304046Sae#include <sys/syslog.h> 46304046Sae#include <sys/sysctl.h> 47304046Sae 48304046Sae#include <net/if.h> 49304046Sae#include <net/if_var.h> 50304046Sae#include <net/if_pflog.h> 51304046Sae#include <net/pfil.h> 52304046Sae 53304046Sae#include <netinet/in.h> 54304046Sae#include <netinet/ip.h> 55304046Sae#include <netinet/ip_var.h> 56304046Sae#include <netinet/ip_fw.h> 57304046Sae#include <netinet/ip6.h> 58304046Sae#include <netinet/icmp6.h> 59304046Sae#include <netinet/ip_icmp.h> 60304046Sae#include <netinet/tcp.h> 61304046Sae#include <netinet/udp.h> 62304046Sae#include <netinet6/in6_var.h> 63304046Sae#include <netinet6/ip6_var.h> 64304046Sae#include <netinet6/ip_fw_nat64.h> 65304046Sae 66304046Sae#include <netpfil/ipfw/ip_fw_private.h> 67304046Sae#include <netpfil/pf/pf.h> 68304046Sae 69334836Sae#include "nat64lsn.h" 70334836Sae 71304046SaeMALLOC_DEFINE(M_NAT64LSN, "NAT64LSN", "NAT64LSN"); 72304046Sae 73304046Saestatic void nat64lsn_periodic(void *data); 74304046Sae#define PERIODIC_DELAY 4 75304046Saestatic uint8_t nat64lsn_proto_map[256]; 76304046Saeuint8_t nat64lsn_rproto_map[NAT_MAX_PROTO]; 77304046Sae 78304046Sae#define NAT64_FLAG_FIN 0x01 /* FIN was seen */ 79304046Sae#define NAT64_FLAG_SYN 0x02 /* First syn in->out */ 80304046Sae#define NAT64_FLAG_ESTAB 0x04 /* Packet with Ack */ 81304046Sae#define NAT64_FLAGS_TCP (NAT64_FLAG_SYN|NAT64_FLAG_ESTAB|NAT64_FLAG_FIN) 82304046Sae 83304046Sae#define NAT64_FLAG_RDR 0x80 /* Port redirect */ 84304046Sae#define NAT64_LOOKUP(chain, cmd) \ 85304046Sae (struct nat64lsn_cfg *)SRV_OBJECT((chain), (cmd)->arg1) 86304046Sae/* 87304046Sae * Delayed job queue, used to create new hosts 88304046Sae * and new portgroups 89304046Sae */ 90304046Saeenum nat64lsn_jtype { 91304046Sae JTYPE_NEWHOST = 1, 92304046Sae JTYPE_NEWPORTGROUP, 93304046Sae JTYPE_DELPORTGROUP, 94304046Sae}; 95304046Sae 96304046Saestruct nat64lsn_job_item { 97304046Sae TAILQ_ENTRY(nat64lsn_job_item) next; 98304046Sae enum nat64lsn_jtype jtype; 99304046Sae struct nat64lsn_host *nh; 100304046Sae struct nat64lsn_portgroup *pg; 101304046Sae void *spare_idx; 102304046Sae struct in6_addr haddr; 103304046Sae uint8_t nat_proto; 104304046Sae uint8_t done; 105304046Sae int needs_idx; 106304046Sae int delcount; 107304046Sae unsigned int fhash; /* Flow hash */ 108304046Sae uint32_t aaddr; /* Last used address (net) */ 109304046Sae struct mbuf *m; 110304046Sae struct ipfw_flow_id f_id; 111304046Sae uint64_t delmask[NAT64LSN_PGPTRNMASK]; 112304046Sae}; 113304046Sae 114304046Saestatic struct mtx jmtx; 115304046Sae#define JQUEUE_LOCK_INIT() mtx_init(&jmtx, "qlock", NULL, MTX_DEF) 116304046Sae#define JQUEUE_LOCK_DESTROY() mtx_destroy(&jmtx) 117304046Sae#define JQUEUE_LOCK() mtx_lock(&jmtx) 118304046Sae#define JQUEUE_UNLOCK() mtx_unlock(&jmtx) 119304046Sae 120304046Saestatic void nat64lsn_enqueue_job(struct nat64lsn_cfg *cfg, 121304046Sae struct nat64lsn_job_item *ji); 122304046Saestatic void nat64lsn_enqueue_jobs(struct nat64lsn_cfg *cfg, 123304046Sae struct nat64lsn_job_head *jhead, int jlen); 124304046Sae 125304046Saestatic struct nat64lsn_job_item *nat64lsn_create_job(struct nat64lsn_cfg *cfg, 126304046Sae const struct ipfw_flow_id *f_id, int jtype); 127304046Saestatic int nat64lsn_request_portgroup(struct nat64lsn_cfg *cfg, 128304046Sae const struct ipfw_flow_id *f_id, struct mbuf **pm, uint32_t aaddr, 129304046Sae int needs_idx); 130304046Saestatic int nat64lsn_request_host(struct nat64lsn_cfg *cfg, 131304046Sae const struct ipfw_flow_id *f_id, struct mbuf **pm); 132304046Saestatic int nat64lsn_translate4(struct nat64lsn_cfg *cfg, 133304046Sae const struct ipfw_flow_id *f_id, struct mbuf **pm); 134304046Saestatic int nat64lsn_translate6(struct nat64lsn_cfg *cfg, 135304046Sae struct ipfw_flow_id *f_id, struct mbuf **pm); 136304046Sae 137304046Saestatic int alloc_portgroup(struct nat64lsn_job_item *ji); 138304046Saestatic void destroy_portgroup(struct nat64lsn_portgroup *pg); 139304046Saestatic void destroy_host6(struct nat64lsn_host *nh); 140304046Saestatic int alloc_host6(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji); 141304046Sae 142304046Saestatic int attach_portgroup(struct nat64lsn_cfg *cfg, 143304046Sae struct nat64lsn_job_item *ji); 144304046Saestatic int attach_host6(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji); 145304046Sae 146304046Sae 147304046Sae/* XXX tmp */ 148304046Saestatic uma_zone_t nat64lsn_host_zone; 149304046Saestatic uma_zone_t nat64lsn_pg_zone; 150304046Saestatic uma_zone_t nat64lsn_pgidx_zone; 151304046Sae 152304046Saestatic unsigned int nat64lsn_periodic_chkstates(struct nat64lsn_cfg *cfg, 153304046Sae struct nat64lsn_host *nh); 154304046Sae 155304046Sae#define I6_hash(x) (djb_hash((const unsigned char *)(x), 16)) 156304046Sae#define I6_first(_ph, h) (_ph)[h] 157304046Sae#define I6_next(x) (x)->next 158304046Sae#define I6_val(x) (&(x)->addr) 159304046Sae#define I6_cmp(a, b) IN6_ARE_ADDR_EQUAL(a, b) 160304046Sae#define I6_lock(a, b) 161304046Sae#define I6_unlock(a, b) 162304046Sae 163304046Sae#define I6HASH_FIND(_cfg, _res, _a) \ 164304046Sae CHT_FIND(_cfg->ih, _cfg->ihsize, I6_, _res, _a) 165304046Sae#define I6HASH_INSERT(_cfg, _i) \ 166304046Sae CHT_INSERT_HEAD(_cfg->ih, _cfg->ihsize, I6_, _i) 167304046Sae#define I6HASH_REMOVE(_cfg, _res, _tmp, _a) \ 168304046Sae CHT_REMOVE(_cfg->ih, _cfg->ihsize, I6_, _res, _tmp, _a) 169304046Sae 170304046Sae#define I6HASH_FOREACH_SAFE(_cfg, _x, _tmp, _cb, _arg) \ 171304046Sae CHT_FOREACH_SAFE(_cfg->ih, _cfg->ihsize, I6_, _x, _tmp, _cb, _arg) 172304046Sae 173304046Sae#define HASH_IN4(x) djb_hash((const unsigned char *)(x), 8) 174304046Sae 175304046Saestatic unsigned 176304046Saedjb_hash(const unsigned char *h, const int len) 177304046Sae{ 178304046Sae unsigned int result = 0; 179304046Sae int i; 180304046Sae 181304046Sae for (i = 0; i < len; i++) 182304046Sae result = 33 * result ^ h[i]; 183304046Sae 184304046Sae return (result); 185304046Sae} 186304046Sae 187304046Sae/* 188304046Saestatic size_t 189304046Saebitmask_size(size_t num, int *level) 190304046Sae{ 191304046Sae size_t x; 192304046Sae int c; 193304046Sae 194304046Sae for (c = 0, x = num; num > 1; num /= 64, c++) 195304046Sae ; 196304046Sae 197304046Sae return (x); 198304046Sae} 199304046Sae 200304046Saestatic void 201304046Saebitmask_prepare(uint64_t *pmask, size_t bufsize, int level) 202304046Sae{ 203304046Sae size_t x, z; 204304046Sae 205304046Sae memset(pmask, 0xFF, bufsize); 206304046Sae for (x = 0, z = 1; level > 1; x += z, z *= 64, level--) 207304046Sae ; 208304046Sae pmask[x] ~= 0x01; 209304046Sae} 210304046Sae*/ 211304046Sae 212304046Saestatic void 213304046Saenat64lsn_log(struct pfloghdr *plog, struct mbuf *m, sa_family_t family, 214304046Sae uint32_t n, uint32_t sn) 215304046Sae{ 216304046Sae 217316446Sae memset(plog, 0, sizeof(*plog)); 218304046Sae plog->length = PFLOG_REAL_HDRLEN; 219304046Sae plog->af = family; 220304046Sae plog->action = PF_NAT; 221304046Sae plog->dir = PF_IN; 222304046Sae plog->rulenr = htonl(n); 223304046Sae plog->subrulenr = htonl(sn); 224304046Sae plog->ruleset[0] = '\0'; 225304046Sae strlcpy(plog->ifname, "NAT64LSN", sizeof(plog->ifname)); 226304046Sae ipfw_bpf_mtap2(plog, PFLOG_HDRLEN, m); 227304046Sae} 228304046Sae/* 229304046Sae * Inspects icmp packets to see if the message contains different 230304046Sae * packet header so we need to alter @addr and @port. 231304046Sae */ 232304046Saestatic int 233304046Saeinspect_icmp_mbuf(struct mbuf **m, uint8_t *nat_proto, uint32_t *addr, 234304046Sae uint16_t *port) 235304046Sae{ 236304046Sae struct ip *ip; 237304046Sae struct tcphdr *tcp; 238304046Sae struct udphdr *udp; 239304046Sae struct icmphdr *icmp; 240304046Sae int off; 241304046Sae uint8_t proto; 242304046Sae 243304046Sae ip = mtod(*m, struct ip *); /* Outer IP header */ 244304046Sae off = (ip->ip_hl << 2) + ICMP_MINLEN; 245304046Sae if ((*m)->m_len < off) 246304046Sae *m = m_pullup(*m, off); 247304046Sae if (*m == NULL) 248304046Sae return (ENOMEM); 249304046Sae 250304046Sae ip = mtod(*m, struct ip *); /* Outer IP header */ 251304046Sae icmp = L3HDR(ip, struct icmphdr *); 252304046Sae switch (icmp->icmp_type) { 253304046Sae case ICMP_ECHO: 254304046Sae case ICMP_ECHOREPLY: 255304046Sae /* Use icmp ID as distinguisher */ 256304046Sae *port = ntohs(*((uint16_t *)(icmp + 1))); 257304046Sae return (0); 258304046Sae case ICMP_UNREACH: 259304046Sae case ICMP_TIMXCEED: 260304046Sae break; 261304046Sae default: 262304046Sae return (EOPNOTSUPP); 263304046Sae } 264304046Sae /* 265304046Sae * ICMP_UNREACH and ICMP_TIMXCEED contains IP header + 64 bits 266304046Sae * of ULP header. 267304046Sae */ 268304046Sae if ((*m)->m_pkthdr.len < off + sizeof(struct ip) + ICMP_MINLEN) 269304046Sae return (EINVAL); 270304046Sae if ((*m)->m_len < off + sizeof(struct ip) + ICMP_MINLEN) 271304046Sae *m = m_pullup(*m, off + sizeof(struct ip) + ICMP_MINLEN); 272304046Sae if (*m == NULL) 273304046Sae return (ENOMEM); 274304046Sae ip = mtodo(*m, off); /* Inner IP header */ 275304046Sae proto = ip->ip_p; 276304046Sae off += ip->ip_hl << 2; /* Skip inner IP header */ 277304046Sae *addr = ntohl(ip->ip_src.s_addr); 278304046Sae if ((*m)->m_len < off + ICMP_MINLEN) 279304046Sae *m = m_pullup(*m, off + ICMP_MINLEN); 280304046Sae if (*m == NULL) 281304046Sae return (ENOMEM); 282304046Sae switch (proto) { 283304046Sae case IPPROTO_TCP: 284304046Sae tcp = mtodo(*m, off); 285304046Sae *nat_proto = NAT_PROTO_TCP; 286304046Sae *port = ntohs(tcp->th_sport); 287304046Sae return (0); 288304046Sae case IPPROTO_UDP: 289304046Sae udp = mtodo(*m, off); 290304046Sae *nat_proto = NAT_PROTO_UDP; 291304046Sae *port = ntohs(udp->uh_sport); 292304046Sae return (0); 293304046Sae case IPPROTO_ICMP: 294304046Sae /* 295304046Sae * We will translate only ICMP errors for our ICMP 296304046Sae * echo requests. 297304046Sae */ 298304046Sae icmp = mtodo(*m, off); 299304046Sae if (icmp->icmp_type != ICMP_ECHO) 300304046Sae return (EOPNOTSUPP); 301304046Sae *port = ntohs(*((uint16_t *)(icmp + 1))); 302304046Sae return (0); 303304046Sae }; 304304046Sae return (EOPNOTSUPP); 305304046Sae} 306304046Sae 307304046Saestatic inline uint8_t 308304046Saeconvert_tcp_flags(uint8_t flags) 309304046Sae{ 310304046Sae uint8_t result; 311304046Sae 312304046Sae result = flags & (TH_FIN|TH_SYN); 313304046Sae result |= (flags & TH_RST) >> 2; /* Treat RST as FIN */ 314304046Sae result |= (flags & TH_ACK) >> 2; /* Treat ACK as estab */ 315304046Sae 316304046Sae return (result); 317304046Sae} 318304046Sae 319304046Saestatic NAT64NOINLINE int 320304046Saenat64lsn_translate4(struct nat64lsn_cfg *cfg, const struct ipfw_flow_id *f_id, 321304046Sae struct mbuf **pm) 322304046Sae{ 323304046Sae struct pfloghdr loghdr, *logdata; 324304046Sae struct in6_addr src6; 325304046Sae struct nat64lsn_portgroup *pg; 326304046Sae struct nat64lsn_host *nh; 327304046Sae struct nat64lsn_state *st; 328304046Sae struct ip *ip; 329304046Sae uint32_t addr; 330304046Sae uint16_t state_flags, state_ts; 331304046Sae uint16_t port, lport; 332304046Sae uint8_t nat_proto; 333304046Sae int ret; 334304046Sae 335304046Sae addr = f_id->dst_ip; 336304046Sae port = f_id->dst_port; 337304046Sae if (addr < cfg->prefix4 || addr > cfg->pmask4) { 338334836Sae NAT64STAT_INC(&cfg->base.stats, nomatch4); 339304046Sae return (cfg->nomatch_verdict); 340304046Sae } 341304046Sae 342304046Sae /* Check if protocol is supported and get its short id */ 343304046Sae nat_proto = nat64lsn_proto_map[f_id->proto]; 344304046Sae if (nat_proto == 0) { 345334836Sae NAT64STAT_INC(&cfg->base.stats, noproto); 346304046Sae return (cfg->nomatch_verdict); 347304046Sae } 348304046Sae 349304046Sae /* We might need to handle icmp differently */ 350304046Sae if (nat_proto == NAT_PROTO_ICMP) { 351304046Sae ret = inspect_icmp_mbuf(pm, &nat_proto, &addr, &port); 352304046Sae if (ret != 0) { 353332767Sae if (ret == ENOMEM) { 354334836Sae NAT64STAT_INC(&cfg->base.stats, nomem); 355332767Sae return (IP_FW_DENY); 356332767Sae } 357334836Sae NAT64STAT_INC(&cfg->base.stats, noproto); 358304046Sae return (cfg->nomatch_verdict); 359304046Sae } 360304046Sae /* XXX: Check addr for validity */ 361304046Sae if (addr < cfg->prefix4 || addr > cfg->pmask4) { 362334836Sae NAT64STAT_INC(&cfg->base.stats, nomatch4); 363304046Sae return (cfg->nomatch_verdict); 364304046Sae } 365304046Sae } 366304046Sae 367304046Sae /* Calc portgroup offset w.r.t protocol */ 368304046Sae pg = GET_PORTGROUP(cfg, addr, nat_proto, port); 369304046Sae 370304046Sae /* Check if this port is occupied by any portgroup */ 371304046Sae if (pg == NULL) { 372334836Sae NAT64STAT_INC(&cfg->base.stats, nomatch4); 373304046Sae#if 0 374304046Sae DPRINTF(DP_STATE, "NOMATCH %u %d %d (%d)", addr, nat_proto, port, 375304046Sae _GET_PORTGROUP_IDX(cfg, addr, nat_proto, port)); 376304046Sae#endif 377304046Sae return (cfg->nomatch_verdict); 378304046Sae } 379304046Sae 380304046Sae /* TODO: Check flags to see if we need to do some static mapping */ 381304046Sae nh = pg->host; 382304046Sae 383304046Sae /* Prepare some fields we might need to update */ 384304046Sae SET_AGE(state_ts); 385304046Sae ip = mtod(*pm, struct ip *); 386304046Sae if (ip->ip_p == IPPROTO_TCP) 387304046Sae state_flags = convert_tcp_flags( 388304046Sae L3HDR(ip, struct tcphdr *)->th_flags); 389304046Sae else 390304046Sae state_flags = 0; 391304046Sae 392304046Sae /* Lock host and get port mapping */ 393304046Sae NAT64_LOCK(nh); 394304046Sae 395304046Sae st = &pg->states[port & (NAT64_CHUNK_SIZE - 1)]; 396304046Sae if (st->timestamp != state_ts) 397304046Sae st->timestamp = state_ts; 398304046Sae if ((st->flags & state_flags) != state_flags) 399304046Sae st->flags |= state_flags; 400304046Sae lport = htons(st->u.s.lport); 401304046Sae 402304046Sae NAT64_UNLOCK(nh); 403304046Sae 404334836Sae if (cfg->base.flags & NAT64_LOG) { 405304046Sae logdata = &loghdr; 406304046Sae nat64lsn_log(logdata, *pm, AF_INET, pg->idx, st->cur.off); 407304046Sae } else 408304046Sae logdata = NULL; 409304046Sae 410334836Sae nat64_embed_ip4(&cfg->base, htonl(f_id->src_ip), &src6); 411304046Sae ret = nat64_do_handle_ip4(*pm, &src6, &nh->addr, lport, 412334836Sae &cfg->base, logdata); 413304046Sae 414304046Sae if (ret == NAT64SKIP) 415332767Sae return (cfg->nomatch_verdict); 416304046Sae if (ret == NAT64MFREE) 417304046Sae m_freem(*pm); 418304046Sae *pm = NULL; 419304046Sae 420304046Sae return (IP_FW_DENY); 421304046Sae} 422304046Sae 423304046Saevoid 424304046Saenat64lsn_dump_state(const struct nat64lsn_cfg *cfg, 425304046Sae const struct nat64lsn_portgroup *pg, const struct nat64lsn_state *st, 426304046Sae const char *px, int off) 427304046Sae{ 428304046Sae char s[INET6_ADDRSTRLEN], a[INET_ADDRSTRLEN], d[INET_ADDRSTRLEN]; 429304046Sae 430334836Sae if ((V_nat64_debug & DP_STATE) == 0) 431304046Sae return; 432304046Sae inet_ntop(AF_INET6, &pg->host->addr, s, sizeof(s)); 433304046Sae inet_ntop(AF_INET, &pg->aaddr, a, sizeof(a)); 434304046Sae inet_ntop(AF_INET, &st->u.s.faddr, d, sizeof(d)); 435304046Sae 436304046Sae DPRINTF(DP_STATE, "%s: PG %d ST [%p|%d]: %s:%d/%d <%s:%d> " 437304046Sae "%s:%d AGE %d", px, pg->idx, st, off, 438304046Sae s, st->u.s.lport, pg->nat_proto, a, pg->aport + off, 439304046Sae d, st->u.s.fport, GET_AGE(st->timestamp)); 440304046Sae} 441304046Sae 442304046Sae/* 443304046Sae * Check if particular TCP state is stale and should be deleted. 444304046Sae * Return 1 if true, 0 otherwise. 445304046Sae */ 446304046Saestatic int 447304046Saenat64lsn_periodic_check_tcp(const struct nat64lsn_cfg *cfg, 448304046Sae const struct nat64lsn_state *st, int age) 449304046Sae{ 450304046Sae int ttl; 451304046Sae 452304046Sae if (st->flags & NAT64_FLAG_FIN) 453304046Sae ttl = cfg->st_close_ttl; 454304046Sae else if (st->flags & NAT64_FLAG_ESTAB) 455304046Sae ttl = cfg->st_estab_ttl; 456304046Sae else if (st->flags & NAT64_FLAG_SYN) 457304046Sae ttl = cfg->st_syn_ttl; 458304046Sae else 459304046Sae ttl = cfg->st_syn_ttl; 460304046Sae 461304046Sae if (age > ttl) 462304046Sae return (1); 463304046Sae return (0); 464304046Sae} 465304046Sae 466304046Sae/* 467304046Sae * Check if nat state @st is stale and should be deleted. 468304046Sae * Return 1 if true, 0 otherwise. 469304046Sae */ 470304046Saestatic NAT64NOINLINE int 471304046Saenat64lsn_periodic_chkstate(const struct nat64lsn_cfg *cfg, 472304046Sae const struct nat64lsn_portgroup *pg, const struct nat64lsn_state *st) 473304046Sae{ 474304046Sae int age, delete; 475304046Sae 476304046Sae age = GET_AGE(st->timestamp); 477304046Sae delete = 0; 478304046Sae 479304046Sae /* Skip immutable records */ 480304046Sae if (st->flags & NAT64_FLAG_RDR) 481304046Sae return (0); 482304046Sae 483304046Sae switch (pg->nat_proto) { 484304046Sae case NAT_PROTO_TCP: 485304046Sae delete = nat64lsn_periodic_check_tcp(cfg, st, age); 486304046Sae break; 487304046Sae case NAT_PROTO_UDP: 488304046Sae if (age > cfg->st_udp_ttl) 489304046Sae delete = 1; 490304046Sae break; 491304046Sae case NAT_PROTO_ICMP: 492304046Sae if (age > cfg->st_icmp_ttl) 493304046Sae delete = 1; 494304046Sae break; 495304046Sae } 496304046Sae 497304046Sae return (delete); 498304046Sae} 499304046Sae 500304046Sae 501304046Sae/* 502304046Sae * The following structures and functions 503304046Sae * are used to perform SLIST_FOREACH_SAFE() 504304046Sae * analog for states identified by struct st_ptr. 505304046Sae */ 506304046Sae 507304046Saestruct st_idx { 508304046Sae struct nat64lsn_portgroup *pg; 509304046Sae struct nat64lsn_state *st; 510304046Sae struct st_ptr sidx_next; 511304046Sae}; 512304046Sae 513304046Saestatic struct st_idx * 514304046Saest_first(const struct nat64lsn_cfg *cfg, const struct nat64lsn_host *nh, 515304046Sae struct st_ptr *sidx, struct st_idx *si) 516304046Sae{ 517304046Sae struct nat64lsn_portgroup *pg; 518304046Sae struct nat64lsn_state *st; 519304046Sae 520304046Sae if (sidx->idx == 0) { 521304046Sae memset(si, 0, sizeof(*si)); 522304046Sae return (si); 523304046Sae } 524304046Sae 525304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, sidx->idx); 526304046Sae st = &pg->states[sidx->off]; 527304046Sae 528304046Sae si->pg = pg; 529304046Sae si->st = st; 530304046Sae si->sidx_next = st->next; 531304046Sae 532304046Sae return (si); 533304046Sae} 534304046Sae 535304046Saestatic struct st_idx * 536304046Saest_next(const struct nat64lsn_cfg *cfg, const struct nat64lsn_host *nh, 537304046Sae struct st_idx *si) 538304046Sae{ 539304046Sae struct st_ptr sidx; 540304046Sae struct nat64lsn_portgroup *pg; 541304046Sae struct nat64lsn_state *st; 542304046Sae 543304046Sae sidx = si->sidx_next; 544304046Sae if (sidx.idx == 0) { 545304046Sae memset(si, 0, sizeof(*si)); 546304046Sae si->st = NULL; 547304046Sae si->pg = NULL; 548304046Sae return (si); 549304046Sae } 550304046Sae 551304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, sidx.idx); 552304046Sae st = &pg->states[sidx.off]; 553304046Sae 554304046Sae si->pg = pg; 555304046Sae si->st = st; 556304046Sae si->sidx_next = st->next; 557304046Sae 558304046Sae return (si); 559304046Sae} 560304046Sae 561304046Saestatic struct st_idx * 562304046Saest_save_cond(struct st_idx *si_dst, struct st_idx *si) 563304046Sae{ 564304046Sae if (si->st != NULL) 565304046Sae *si_dst = *si; 566304046Sae 567304046Sae return (si_dst); 568304046Sae} 569304046Sae 570304046Saeunsigned int 571304046Saenat64lsn_periodic_chkstates(struct nat64lsn_cfg *cfg, struct nat64lsn_host *nh) 572304046Sae{ 573304046Sae struct st_idx si, si_prev; 574304046Sae int i; 575304046Sae unsigned int delcount; 576304046Sae 577304046Sae delcount = 0; 578304046Sae for (i = 0; i < nh->hsize; i++) { 579304046Sae memset(&si_prev, 0, sizeof(si_prev)); 580304046Sae for (st_first(cfg, nh, &nh->phash[i], &si); 581304046Sae si.st != NULL; 582304046Sae st_save_cond(&si_prev, &si), st_next(cfg, nh, &si)) { 583304046Sae if (nat64lsn_periodic_chkstate(cfg, si.pg, si.st) == 0) 584304046Sae continue; 585304046Sae nat64lsn_dump_state(cfg, si.pg, si.st, "DELETE STATE", 586304046Sae si.st->cur.off); 587304046Sae /* Unlink from hash */ 588304046Sae if (si_prev.st != NULL) 589304046Sae si_prev.st->next = si.st->next; 590304046Sae else 591304046Sae nh->phash[i] = si.st->next; 592304046Sae /* Delete state and free its data */ 593304046Sae PG_MARK_FREE_IDX(si.pg, si.st->cur.off); 594304046Sae memset(si.st, 0, sizeof(struct nat64lsn_state)); 595304046Sae si.st = NULL; 596304046Sae delcount++; 597304046Sae 598304046Sae /* Update portgroup timestamp */ 599304046Sae SET_AGE(si.pg->timestamp); 600304046Sae } 601304046Sae } 602334836Sae NAT64STAT_ADD(&cfg->base.stats, sdeleted, delcount); 603304046Sae return (delcount); 604304046Sae} 605304046Sae 606304046Sae/* 607304046Sae * Checks if portgroup is not used and can be deleted, 608304046Sae * Returns 1 if stale, 0 otherwise 609304046Sae */ 610304046Saestatic int 611304046Saestale_pg(const struct nat64lsn_cfg *cfg, const struct nat64lsn_portgroup *pg) 612304046Sae{ 613304046Sae 614304046Sae if (!PG_IS_EMPTY(pg)) 615304046Sae return (0); 616304046Sae if (GET_AGE(pg->timestamp) < cfg->pg_delete_delay) 617304046Sae return (0); 618304046Sae return (1); 619304046Sae} 620304046Sae 621304046Sae/* 622304046Sae * Checks if host record is not used and can be deleted, 623304046Sae * Returns 1 if stale, 0 otherwise 624304046Sae */ 625304046Saestatic int 626304046Saestale_nh(const struct nat64lsn_cfg *cfg, const struct nat64lsn_host *nh) 627304046Sae{ 628304046Sae 629304046Sae if (nh->pg_used != 0) 630304046Sae return (0); 631304046Sae if (GET_AGE(nh->timestamp) < cfg->nh_delete_delay) 632304046Sae return (0); 633304046Sae return (1); 634304046Sae} 635304046Sae 636304046Saestruct nat64lsn_periodic_data { 637304046Sae struct nat64lsn_cfg *cfg; 638304046Sae struct nat64lsn_job_head jhead; 639304046Sae int jlen; 640304046Sae}; 641304046Sae 642304046Saestatic NAT64NOINLINE int 643304046Saenat64lsn_periodic_chkhost(struct nat64lsn_host *nh, 644304046Sae struct nat64lsn_periodic_data *d) 645304046Sae{ 646304046Sae char a[INET6_ADDRSTRLEN]; 647304046Sae struct nat64lsn_portgroup *pg; 648304046Sae struct nat64lsn_job_item *ji; 649304046Sae uint64_t delmask[NAT64LSN_PGPTRNMASK]; 650304046Sae int delcount, i; 651304046Sae 652304046Sae delcount = 0; 653304046Sae memset(delmask, 0, sizeof(delmask)); 654304046Sae 655304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 656304046Sae DPRINTF(DP_JQUEUE, "Checking %s host %s on cpu %d", 657304046Sae stale_nh(d->cfg, nh) ? "stale" : "non-stale", a, curcpu); 658304046Sae if (!stale_nh(d->cfg, nh)) { 659304046Sae /* Non-stale host. Inspect internals */ 660304046Sae NAT64_LOCK(nh); 661304046Sae 662304046Sae /* Stage 1: Check&expire states */ 663304046Sae if (nat64lsn_periodic_chkstates(d->cfg, nh) != 0) 664304046Sae SET_AGE(nh->timestamp); 665304046Sae 666304046Sae /* Stage 2: Check if we need to expire */ 667304046Sae for (i = 0; i < nh->pg_used; i++) { 668304046Sae pg = PORTGROUP_BYSIDX(d->cfg, nh, i + 1); 669304046Sae if (pg == NULL) 670304046Sae continue; 671304046Sae 672304046Sae /* Check if we can delete portgroup */ 673304046Sae if (stale_pg(d->cfg, pg) == 0) 674304046Sae continue; 675304046Sae 676304046Sae DPRINTF(DP_JQUEUE, "Check PG %d", i); 677304046Sae delmask[i / 64] |= ((uint64_t)1 << (i % 64)); 678304046Sae delcount++; 679304046Sae } 680304046Sae 681304046Sae NAT64_UNLOCK(nh); 682304046Sae if (delcount == 0) 683304046Sae return (0); 684304046Sae } 685304046Sae 686304046Sae DPRINTF(DP_JQUEUE, "Queueing %d portgroups for deleting", delcount); 687304046Sae /* We have something to delete - add it to queue */ 688304046Sae ji = nat64lsn_create_job(d->cfg, NULL, JTYPE_DELPORTGROUP); 689304046Sae if (ji == NULL) 690304046Sae return (0); 691304046Sae 692304046Sae ji->haddr = nh->addr; 693304046Sae ji->delcount = delcount; 694304046Sae memcpy(ji->delmask, delmask, sizeof(ji->delmask)); 695304046Sae 696304046Sae TAILQ_INSERT_TAIL(&d->jhead, ji, next); 697304046Sae d->jlen++; 698304046Sae return (0); 699304046Sae} 700304046Sae 701304046Sae/* 702304046Sae * This procedure is used to perform various maintance 703304046Sae * on dynamic hash list. Currently it is called every second. 704304046Sae */ 705304046Saestatic void 706304046Saenat64lsn_periodic(void *data) 707304046Sae{ 708304046Sae struct ip_fw_chain *ch; 709304046Sae IPFW_RLOCK_TRACKER; 710304046Sae struct nat64lsn_cfg *cfg; 711304046Sae struct nat64lsn_periodic_data d; 712304046Sae struct nat64lsn_host *nh, *tmp; 713304046Sae 714304046Sae cfg = (struct nat64lsn_cfg *) data; 715304046Sae ch = cfg->ch; 716304046Sae CURVNET_SET(cfg->vp); 717304046Sae 718304046Sae memset(&d, 0, sizeof(d)); 719304046Sae d.cfg = cfg; 720304046Sae TAILQ_INIT(&d.jhead); 721304046Sae 722304046Sae IPFW_RLOCK(ch); 723304046Sae 724304046Sae /* Stage 1: foreach host, check all its portgroups */ 725304046Sae I6HASH_FOREACH_SAFE(cfg, nh, tmp, nat64lsn_periodic_chkhost, &d); 726304046Sae 727304046Sae /* Enqueue everything we have requested */ 728304046Sae nat64lsn_enqueue_jobs(cfg, &d.jhead, d.jlen); 729304046Sae 730304046Sae callout_schedule(&cfg->periodic, hz * PERIODIC_DELAY); 731304046Sae 732304046Sae IPFW_RUNLOCK(ch); 733304046Sae 734304046Sae CURVNET_RESTORE(); 735304046Sae} 736304046Sae 737304046Saestatic NAT64NOINLINE void 738304046Saereinject_mbuf(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 739304046Sae{ 740304046Sae 741304046Sae if (ji->m == NULL) 742304046Sae return; 743304046Sae 744304046Sae /* Request has failed or packet type is wrong */ 745304046Sae if (ji->f_id.addr_type != 6 || ji->done == 0) { 746304046Sae m_freem(ji->m); 747304046Sae ji->m = NULL; 748334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 749304046Sae DPRINTF(DP_DROPS, "mbuf dropped: type %d, done %d", 750304046Sae ji->jtype, ji->done); 751304046Sae return; 752304046Sae } 753304046Sae 754304046Sae /* 755304046Sae * XXX: Limit recursion level 756304046Sae */ 757304046Sae 758334836Sae NAT64STAT_INC(&cfg->base.stats, jreinjected); 759304046Sae DPRINTF(DP_JQUEUE, "Reinject mbuf"); 760304046Sae nat64lsn_translate6(cfg, &ji->f_id, &ji->m); 761304046Sae} 762304046Sae 763304046Saestatic void 764304046Saedestroy_portgroup(struct nat64lsn_portgroup *pg) 765304046Sae{ 766304046Sae 767304046Sae DPRINTF(DP_OBJ, "DESTROY PORTGROUP %d %p", pg->idx, pg); 768304046Sae uma_zfree(nat64lsn_pg_zone, pg); 769304046Sae} 770304046Sae 771304046Saestatic NAT64NOINLINE int 772304046Saealloc_portgroup(struct nat64lsn_job_item *ji) 773304046Sae{ 774304046Sae struct nat64lsn_portgroup *pg; 775304046Sae 776304046Sae pg = uma_zalloc(nat64lsn_pg_zone, M_NOWAIT); 777304046Sae if (pg == NULL) 778304046Sae return (1); 779304046Sae 780304046Sae if (ji->needs_idx != 0) { 781304046Sae ji->spare_idx = uma_zalloc(nat64lsn_pgidx_zone, M_NOWAIT); 782304046Sae /* Failed alloc isn't always fatal, so don't check */ 783304046Sae } 784304046Sae memset(&pg->freemask, 0xFF, sizeof(pg->freemask)); 785304046Sae pg->nat_proto = ji->nat_proto; 786304046Sae ji->pg = pg; 787304046Sae return (0); 788304046Sae 789304046Sae} 790304046Sae 791304046Saestatic void 792304046Saedestroy_host6(struct nat64lsn_host *nh) 793304046Sae{ 794304046Sae char a[INET6_ADDRSTRLEN]; 795304046Sae int i; 796304046Sae 797304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 798304046Sae DPRINTF(DP_OBJ, "DESTROY HOST %s %p (pg used %d)", a, nh, 799304046Sae nh->pg_used); 800304046Sae NAT64_LOCK_DESTROY(nh); 801304046Sae for (i = 0; i < nh->pg_allocated / NAT64LSN_PGIDX_CHUNK; i++) 802304046Sae uma_zfree(nat64lsn_pgidx_zone, PORTGROUP_CHUNK(nh, i)); 803304046Sae uma_zfree(nat64lsn_host_zone, nh); 804304046Sae} 805304046Sae 806304046Saestatic NAT64NOINLINE int 807304046Saealloc_host6(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 808304046Sae{ 809304046Sae struct nat64lsn_host *nh; 810304046Sae char a[INET6_ADDRSTRLEN]; 811304046Sae 812304046Sae nh = uma_zalloc(nat64lsn_host_zone, M_NOWAIT); 813304046Sae if (nh == NULL) 814304046Sae return (1); 815304046Sae PORTGROUP_CHUNK(nh, 0) = uma_zalloc(nat64lsn_pgidx_zone, M_NOWAIT); 816304046Sae if (PORTGROUP_CHUNK(nh, 0) == NULL) { 817304046Sae uma_zfree(nat64lsn_host_zone, nh); 818304046Sae return (2); 819304046Sae } 820304046Sae if (alloc_portgroup(ji) != 0) { 821334836Sae NAT64STAT_INC(&cfg->base.stats, jportfails); 822304046Sae uma_zfree(nat64lsn_pgidx_zone, PORTGROUP_CHUNK(nh, 0)); 823304046Sae uma_zfree(nat64lsn_host_zone, nh); 824304046Sae return (3); 825304046Sae } 826304046Sae 827304046Sae NAT64_LOCK_INIT(nh); 828304046Sae nh->addr = ji->haddr; 829304046Sae nh->hsize = NAT64LSN_HSIZE; /* XXX: hardcoded size */ 830304046Sae nh->pg_allocated = NAT64LSN_PGIDX_CHUNK; 831304046Sae nh->pg_used = 0; 832304046Sae ji->nh = nh; 833304046Sae 834304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 835304046Sae DPRINTF(DP_OBJ, "ALLOC HOST %s %p", a, ji->nh); 836304046Sae return (0); 837304046Sae} 838304046Sae 839304046Sae/* 840304046Sae * Finds free @pg index inside @nh 841304046Sae */ 842304046Saestatic NAT64NOINLINE int 843304046Saefind_nh_pg_idx(struct nat64lsn_cfg *cfg, struct nat64lsn_host *nh, int *idx) 844304046Sae{ 845304046Sae int i; 846304046Sae 847304046Sae for (i = 0; i < nh->pg_allocated; i++) { 848304046Sae if (PORTGROUP_BYSIDX(cfg, nh, i + 1) == NULL) { 849304046Sae *idx = i; 850304046Sae return (0); 851304046Sae } 852304046Sae } 853304046Sae return (1); 854304046Sae} 855304046Sae 856304046Saestatic NAT64NOINLINE int 857304046Saeattach_host6(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 858304046Sae{ 859304046Sae char a[INET6_ADDRSTRLEN]; 860304046Sae struct nat64lsn_host *nh; 861304046Sae 862304046Sae I6HASH_FIND(cfg, nh, &ji->haddr); 863304046Sae if (nh == NULL) { 864304046Sae /* Add new host to list */ 865304046Sae nh = ji->nh; 866304046Sae I6HASH_INSERT(cfg, nh); 867304046Sae cfg->ihcount++; 868304046Sae ji->nh = NULL; 869304046Sae 870304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 871304046Sae DPRINTF(DP_OBJ, "ATTACH HOST %s %p", a, nh); 872304046Sae /* 873304046Sae * Try to add portgroup. 874304046Sae * Note it will automatically set 875304046Sae * 'done' on ji if successful. 876304046Sae */ 877304046Sae if (attach_portgroup(cfg, ji) != 0) { 878304046Sae DPRINTF(DP_DROPS, "%s %p failed to attach PG", 879304046Sae a, nh); 880334836Sae NAT64STAT_INC(&cfg->base.stats, jportfails); 881304046Sae return (1); 882304046Sae } 883304046Sae return (0); 884304046Sae } 885304046Sae 886304046Sae /* 887304046Sae * nh isn't NULL. This probably means we had several simultaneous 888304046Sae * host requests. The previous one request has already attached 889304046Sae * this host. Requeue attached mbuf and mark job as done, but 890304046Sae * leave nh and pg pointers not changed, so nat64lsn_do_request() 891304046Sae * will release all allocated resources. 892304046Sae */ 893304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 894304046Sae DPRINTF(DP_OBJ, "%s %p is already attached as %p", 895304046Sae a, ji->nh, nh); 896304046Sae ji->done = 1; 897304046Sae return (0); 898304046Sae} 899304046Sae 900304046Saestatic NAT64NOINLINE int 901304046Saefind_pg_place_addr(const struct nat64lsn_cfg *cfg, int addr_off, 902304046Sae int nat_proto, uint16_t *aport, int *ppg_idx) 903304046Sae{ 904304046Sae int j, pg_idx; 905304046Sae 906304046Sae pg_idx = addr_off * _ADDR_PG_COUNT + 907304046Sae (nat_proto - 1) * _ADDR_PG_PROTO_COUNT; 908304046Sae 909304046Sae for (j = NAT64_MIN_CHUNK; j < _ADDR_PG_PROTO_COUNT; j++) { 910304046Sae if (cfg->pg[pg_idx + j] != NULL) 911304046Sae continue; 912304046Sae 913304046Sae *aport = j * NAT64_CHUNK_SIZE; 914304046Sae *ppg_idx = pg_idx + j; 915304046Sae return (1); 916304046Sae } 917304046Sae 918304046Sae return (0); 919304046Sae} 920304046Sae 921304046Sae/* 922304046Sae * XXX: This function needs to be rewritten to 923304046Sae * use free bitmask for faster pg finding, 924304046Sae * additionally, it should take into consideration 925304046Sae * a) randomization and 926304046Sae * b) previous addresses allocated to given nat instance 927304046Sae * 928304046Sae */ 929304046Saestatic NAT64NOINLINE int 930304046Saefind_portgroup_place(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji, 931304046Sae uint32_t *aaddr, uint16_t *aport, int *ppg_idx) 932304046Sae{ 933304046Sae int i, nat_proto; 934304046Sae 935304046Sae /* 936304046Sae * XXX: Use bitmask index to be able to find/check if IP address 937304046Sae * has some spare pg's 938304046Sae */ 939304046Sae nat_proto = ji->nat_proto; 940304046Sae 941304046Sae /* First, try to use same address */ 942304046Sae if (ji->aaddr != 0) { 943304046Sae i = ntohl(ji->aaddr) - cfg->prefix4; 944304046Sae if (find_pg_place_addr(cfg, i, nat_proto, aport, 945304046Sae ppg_idx) != 0){ 946304046Sae /* Found! */ 947304046Sae *aaddr = htonl(cfg->prefix4 + i); 948304046Sae return (0); 949304046Sae } 950304046Sae } 951304046Sae 952304046Sae /* Next, try to use random address based on flow hash */ 953304046Sae i = ji->fhash % (1 << (32 - cfg->plen4)); 954304046Sae if (find_pg_place_addr(cfg, i, nat_proto, aport, ppg_idx) != 0) { 955304046Sae /* Found! */ 956304046Sae *aaddr = htonl(cfg->prefix4 + i); 957304046Sae return (0); 958304046Sae } 959304046Sae 960304046Sae 961304046Sae /* Last one: simply find ANY available */ 962304046Sae for (i = 0; i < (1 << (32 - cfg->plen4)); i++) { 963304046Sae if (find_pg_place_addr(cfg, i, nat_proto, aport, 964304046Sae ppg_idx) != 0){ 965304046Sae /* Found! */ 966304046Sae *aaddr = htonl(cfg->prefix4 + i); 967304046Sae return (0); 968304046Sae } 969304046Sae } 970304046Sae 971304046Sae return (1); 972304046Sae} 973304046Sae 974304046Saestatic NAT64NOINLINE int 975304046Saeattach_portgroup(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 976304046Sae{ 977304046Sae char a[INET6_ADDRSTRLEN]; 978304046Sae struct nat64lsn_portgroup *pg; 979304046Sae struct nat64lsn_host *nh; 980304046Sae uint32_t aaddr; 981304046Sae uint16_t aport; 982304046Sae int nh_pg_idx, pg_idx; 983304046Sae 984304046Sae pg = ji->pg; 985304046Sae 986304046Sae /* 987304046Sae * Find source host and bind: we can't rely on 988304046Sae * pg->host 989304046Sae */ 990304046Sae I6HASH_FIND(cfg, nh, &ji->haddr); 991304046Sae if (nh == NULL) 992304046Sae return (1); 993304046Sae 994304046Sae /* Find spare port chunk */ 995304046Sae if (find_portgroup_place(cfg, ji, &aaddr, &aport, &pg_idx) != 0) { 996304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 997304046Sae DPRINTF(DP_OBJ | DP_DROPS, "empty PG not found for %s", a); 998304046Sae return (2); 999304046Sae } 1000304046Sae 1001304046Sae /* Expand PG indexes if needed */ 1002304046Sae if (nh->pg_allocated < cfg->max_chunks && ji->spare_idx != NULL) { 1003304046Sae PORTGROUP_CHUNK(nh, nh->pg_allocated / NAT64LSN_PGIDX_CHUNK) = 1004304046Sae ji->spare_idx; 1005304046Sae nh->pg_allocated += NAT64LSN_PGIDX_CHUNK; 1006304046Sae ji->spare_idx = NULL; 1007304046Sae } 1008304046Sae 1009304046Sae /* Find empty index to store PG in the @nh */ 1010304046Sae if (find_nh_pg_idx(cfg, nh, &nh_pg_idx) != 0) { 1011304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 1012304046Sae DPRINTF(DP_OBJ | DP_DROPS, "free PG index not found for %s", 1013304046Sae a); 1014304046Sae return (3); 1015304046Sae } 1016304046Sae 1017304046Sae cfg->pg[pg_idx] = pg; 1018304046Sae cfg->protochunks[pg->nat_proto]++; 1019334836Sae NAT64STAT_INC(&cfg->base.stats, spgcreated); 1020304046Sae 1021304046Sae pg->aaddr = aaddr; 1022304046Sae pg->aport = aport; 1023304046Sae pg->host = nh; 1024304046Sae pg->idx = pg_idx; 1025304046Sae SET_AGE(pg->timestamp); 1026304046Sae 1027304046Sae PORTGROUP_BYSIDX(cfg, nh, nh_pg_idx + 1) = pg; 1028304046Sae if (nh->pg_used == nh_pg_idx) 1029304046Sae nh->pg_used++; 1030304046Sae SET_AGE(nh->timestamp); 1031304046Sae 1032304046Sae ji->pg = NULL; 1033304046Sae ji->done = 1; 1034304046Sae 1035304046Sae return (0); 1036304046Sae} 1037304046Sae 1038304046Saestatic NAT64NOINLINE void 1039304046Saeconsider_del_portgroup(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 1040304046Sae{ 1041304046Sae struct nat64lsn_host *nh, *nh_tmp; 1042304046Sae struct nat64lsn_portgroup *pg, *pg_list[256]; 1043304046Sae int i, pg_lidx, idx; 1044304046Sae 1045304046Sae /* Find source host */ 1046304046Sae I6HASH_FIND(cfg, nh, &ji->haddr); 1047304046Sae if (nh == NULL || nh->pg_used == 0) 1048304046Sae return; 1049304046Sae 1050304046Sae memset(pg_list, 0, sizeof(pg_list)); 1051304046Sae pg_lidx = 0; 1052304046Sae 1053304046Sae NAT64_LOCK(nh); 1054304046Sae 1055304046Sae for (i = nh->pg_used - 1; i >= 0; i--) { 1056304046Sae if ((ji->delmask[i / 64] & ((uint64_t)1 << (i % 64))) == 0) 1057304046Sae continue; 1058304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, i + 1); 1059304046Sae 1060304046Sae /* Check that PG isn't busy. */ 1061304046Sae if (stale_pg(cfg, pg) == 0) 1062304046Sae continue; 1063304046Sae 1064304046Sae /* DO delete */ 1065304046Sae pg_list[pg_lidx++] = pg; 1066304046Sae PORTGROUP_BYSIDX(cfg, nh, i + 1) = NULL; 1067304046Sae 1068304046Sae idx = _GET_PORTGROUP_IDX(cfg, ntohl(pg->aaddr), pg->nat_proto, 1069304046Sae pg->aport); 1070304046Sae KASSERT(cfg->pg[idx] == pg, ("Non matched pg")); 1071304046Sae cfg->pg[idx] = NULL; 1072304046Sae cfg->protochunks[pg->nat_proto]--; 1073334836Sae NAT64STAT_INC(&cfg->base.stats, spgdeleted); 1074304046Sae 1075304046Sae /* Decrease pg_used */ 1076304046Sae while (nh->pg_used > 0 && 1077304046Sae PORTGROUP_BYSIDX(cfg, nh, nh->pg_used) == NULL) 1078304046Sae nh->pg_used--; 1079304046Sae 1080304046Sae /* Check if on-stack buffer has ended */ 1081304046Sae if (pg_lidx == nitems(pg_list)) 1082304046Sae break; 1083304046Sae } 1084304046Sae 1085304046Sae NAT64_UNLOCK(nh); 1086304046Sae 1087304046Sae if (stale_nh(cfg, nh)) { 1088304046Sae I6HASH_REMOVE(cfg, nh, nh_tmp, &ji->haddr); 1089304046Sae KASSERT(nh != NULL, ("Unable to find address")); 1090304046Sae cfg->ihcount--; 1091304046Sae ji->nh = nh; 1092304046Sae I6HASH_FIND(cfg, nh, &ji->haddr); 1093304046Sae KASSERT(nh == NULL, ("Failed to delete address")); 1094304046Sae } 1095304046Sae 1096304046Sae /* TODO: Delay freeing portgroups */ 1097304046Sae while (pg_lidx > 0) { 1098304046Sae pg_lidx--; 1099334836Sae NAT64STAT_INC(&cfg->base.stats, spgdeleted); 1100304046Sae destroy_portgroup(pg_list[pg_lidx]); 1101304046Sae } 1102304046Sae} 1103304046Sae 1104304046Sae/* 1105304046Sae * Main request handler. 1106304046Sae * Responsible for handling jqueue, e.g. 1107304046Sae * creating new hosts, addind/deleting portgroups. 1108304046Sae */ 1109304046Saestatic NAT64NOINLINE void 1110304046Saenat64lsn_do_request(void *data) 1111304046Sae{ 1112304046Sae IPFW_RLOCK_TRACKER; 1113304046Sae struct nat64lsn_job_head jhead; 1114304046Sae struct nat64lsn_job_item *ji; 1115304046Sae int jcount, nhsize; 1116304046Sae struct nat64lsn_cfg *cfg = (struct nat64lsn_cfg *) data; 1117304046Sae struct ip_fw_chain *ch; 1118304046Sae int delcount; 1119304046Sae 1120304046Sae CURVNET_SET(cfg->vp); 1121304046Sae 1122304046Sae TAILQ_INIT(&jhead); 1123304046Sae 1124304046Sae /* XXX: We're running unlocked here */ 1125304046Sae 1126304046Sae ch = cfg->ch; 1127304046Sae delcount = 0; 1128304046Sae IPFW_RLOCK(ch); 1129304046Sae 1130304046Sae /* Grab queue */ 1131304046Sae JQUEUE_LOCK(); 1132304046Sae TAILQ_SWAP(&jhead, &cfg->jhead, nat64lsn_job_item, next); 1133304046Sae jcount = cfg->jlen; 1134304046Sae cfg->jlen = 0; 1135304046Sae JQUEUE_UNLOCK(); 1136304046Sae 1137304046Sae /* check if we need to resize hash */ 1138304046Sae nhsize = 0; 1139304046Sae if (cfg->ihcount > cfg->ihsize && cfg->ihsize < 65536) { 1140304046Sae nhsize = cfg->ihsize; 1141304046Sae for ( ; cfg->ihcount > nhsize && nhsize < 65536; nhsize *= 2) 1142304046Sae ; 1143304046Sae } else if (cfg->ihcount < cfg->ihsize * 4) { 1144304046Sae nhsize = cfg->ihsize; 1145304046Sae for ( ; cfg->ihcount < nhsize * 4 && nhsize > 32; nhsize /= 2) 1146304046Sae ; 1147304046Sae } 1148304046Sae 1149304046Sae IPFW_RUNLOCK(ch); 1150304046Sae 1151304046Sae if (TAILQ_EMPTY(&jhead)) { 1152304046Sae CURVNET_RESTORE(); 1153304046Sae return; 1154304046Sae } 1155304046Sae 1156334836Sae NAT64STAT_INC(&cfg->base.stats, jcalls); 1157304046Sae DPRINTF(DP_JQUEUE, "count=%d", jcount); 1158304046Sae 1159304046Sae /* 1160304046Sae * TODO: 1161304046Sae * What we should do here is to build a hash 1162304046Sae * to ensure we don't have lots of duplicate requests. 1163304046Sae * Skip this for now. 1164304046Sae * 1165304046Sae * TODO: Limit per-call number of items 1166304046Sae */ 1167304046Sae 1168304046Sae /* Pre-allocate everything for entire chain */ 1169304046Sae TAILQ_FOREACH(ji, &jhead, next) { 1170304046Sae switch (ji->jtype) { 1171304046Sae case JTYPE_NEWHOST: 1172304046Sae if (alloc_host6(cfg, ji) != 0) 1173334836Sae NAT64STAT_INC(&cfg->base.stats, 1174334836Sae jhostfails); 1175304046Sae break; 1176304046Sae case JTYPE_NEWPORTGROUP: 1177304046Sae if (alloc_portgroup(ji) != 0) 1178334836Sae NAT64STAT_INC(&cfg->base.stats, 1179334836Sae jportfails); 1180304046Sae break; 1181304046Sae case JTYPE_DELPORTGROUP: 1182304046Sae delcount += ji->delcount; 1183304046Sae break; 1184304046Sae default: 1185304046Sae break; 1186304046Sae } 1187304046Sae } 1188304046Sae 1189304046Sae /* 1190304046Sae * TODO: Alloc hew hash 1191304046Sae */ 1192304046Sae nhsize = 0; 1193304046Sae if (nhsize > 0) { 1194304046Sae /* XXX: */ 1195304046Sae } 1196304046Sae 1197304046Sae /* Apply all changes in batch */ 1198304046Sae IPFW_UH_WLOCK(ch); 1199304046Sae IPFW_WLOCK(ch); 1200304046Sae 1201304046Sae TAILQ_FOREACH(ji, &jhead, next) { 1202304046Sae switch (ji->jtype) { 1203304046Sae case JTYPE_NEWHOST: 1204304046Sae if (ji->nh != NULL) 1205304046Sae attach_host6(cfg, ji); 1206304046Sae break; 1207304046Sae case JTYPE_NEWPORTGROUP: 1208304046Sae if (ji->pg != NULL && 1209304046Sae attach_portgroup(cfg, ji) != 0) 1210334836Sae NAT64STAT_INC(&cfg->base.stats, 1211334836Sae jportfails); 1212304046Sae break; 1213304046Sae case JTYPE_DELPORTGROUP: 1214304046Sae consider_del_portgroup(cfg, ji); 1215304046Sae break; 1216304046Sae } 1217304046Sae } 1218304046Sae 1219304046Sae if (nhsize > 0) { 1220304046Sae /* XXX: Move everything to new hash */ 1221304046Sae } 1222304046Sae 1223304046Sae IPFW_WUNLOCK(ch); 1224304046Sae IPFW_UH_WUNLOCK(ch); 1225304046Sae 1226304046Sae /* Flush unused entries */ 1227304046Sae while (!TAILQ_EMPTY(&jhead)) { 1228304046Sae ji = TAILQ_FIRST(&jhead); 1229304046Sae TAILQ_REMOVE(&jhead, ji, next); 1230304046Sae if (ji->nh != NULL) 1231304046Sae destroy_host6(ji->nh); 1232304046Sae if (ji->pg != NULL) 1233304046Sae destroy_portgroup(ji->pg); 1234304046Sae if (ji->m != NULL) 1235304046Sae reinject_mbuf(cfg, ji); 1236304046Sae if (ji->spare_idx != NULL) 1237304046Sae uma_zfree(nat64lsn_pgidx_zone, ji->spare_idx); 1238304046Sae free(ji, M_IPFW); 1239304046Sae } 1240304046Sae CURVNET_RESTORE(); 1241304046Sae} 1242304046Sae 1243304046Saestatic NAT64NOINLINE struct nat64lsn_job_item * 1244304046Saenat64lsn_create_job(struct nat64lsn_cfg *cfg, const struct ipfw_flow_id *f_id, 1245304046Sae int jtype) 1246304046Sae{ 1247304046Sae struct nat64lsn_job_item *ji; 1248304046Sae struct in6_addr haddr; 1249304046Sae uint8_t nat_proto; 1250304046Sae 1251304046Sae /* 1252304046Sae * Do not try to lock possibly contested mutex if we're near the limit. 1253304046Sae * Drop packet instead. 1254304046Sae */ 1255304046Sae if (cfg->jlen >= cfg->jmaxlen) { 1256334836Sae NAT64STAT_INC(&cfg->base.stats, jmaxlen); 1257304046Sae return (NULL); 1258304046Sae } 1259304046Sae 1260304046Sae memset(&haddr, 0, sizeof(haddr)); 1261304046Sae nat_proto = 0; 1262304046Sae if (f_id != NULL) { 1263304046Sae haddr = f_id->src_ip6; 1264304046Sae nat_proto = nat64lsn_proto_map[f_id->proto]; 1265304046Sae 1266304046Sae DPRINTF(DP_JQUEUE, "REQUEST pg nat_proto %d on proto %d", 1267304046Sae nat_proto, f_id->proto); 1268304046Sae 1269304046Sae if (nat_proto == 0) 1270304046Sae return (NULL); 1271304046Sae } 1272304046Sae 1273304046Sae ji = malloc(sizeof(struct nat64lsn_job_item), M_IPFW, 1274304046Sae M_NOWAIT | M_ZERO); 1275304046Sae 1276304046Sae if (ji == NULL) { 1277334836Sae NAT64STAT_INC(&cfg->base.stats, jnomem); 1278304046Sae return (NULL); 1279304046Sae } 1280304046Sae 1281304046Sae ji->jtype = jtype; 1282304046Sae 1283304046Sae if (f_id != NULL) { 1284304046Sae ji->f_id = *f_id; 1285304046Sae ji->haddr = haddr; 1286304046Sae ji->nat_proto = nat_proto; 1287304046Sae } 1288304046Sae 1289304046Sae return (ji); 1290304046Sae} 1291304046Sae 1292304046Saestatic NAT64NOINLINE void 1293304046Saenat64lsn_enqueue_job(struct nat64lsn_cfg *cfg, struct nat64lsn_job_item *ji) 1294304046Sae{ 1295304046Sae 1296304046Sae if (ji == NULL) 1297304046Sae return; 1298304046Sae 1299304046Sae JQUEUE_LOCK(); 1300304046Sae TAILQ_INSERT_TAIL(&cfg->jhead, ji, next); 1301304046Sae cfg->jlen++; 1302334836Sae NAT64STAT_INC(&cfg->base.stats, jrequests); 1303304046Sae 1304304046Sae if (callout_pending(&cfg->jcallout) == 0) 1305304046Sae callout_reset(&cfg->jcallout, 1, nat64lsn_do_request, cfg); 1306304046Sae JQUEUE_UNLOCK(); 1307304046Sae} 1308304046Sae 1309304046Saestatic NAT64NOINLINE void 1310304046Saenat64lsn_enqueue_jobs(struct nat64lsn_cfg *cfg, 1311304046Sae struct nat64lsn_job_head *jhead, int jlen) 1312304046Sae{ 1313304046Sae 1314304046Sae if (TAILQ_EMPTY(jhead)) 1315304046Sae return; 1316304046Sae 1317304046Sae /* Attach current queue to execution one */ 1318304046Sae JQUEUE_LOCK(); 1319304046Sae TAILQ_CONCAT(&cfg->jhead, jhead, next); 1320304046Sae cfg->jlen += jlen; 1321334836Sae NAT64STAT_ADD(&cfg->base.stats, jrequests, jlen); 1322304046Sae 1323304046Sae if (callout_pending(&cfg->jcallout) == 0) 1324304046Sae callout_reset(&cfg->jcallout, 1, nat64lsn_do_request, cfg); 1325304046Sae JQUEUE_UNLOCK(); 1326304046Sae} 1327304046Sae 1328304046Saestatic unsigned int 1329304046Saeflow6_hash(const struct ipfw_flow_id *f_id) 1330304046Sae{ 1331304046Sae unsigned char hbuf[36]; 1332304046Sae 1333304046Sae memcpy(hbuf, &f_id->dst_ip6, 16); 1334304046Sae memcpy(&hbuf[16], &f_id->src_ip6, 16); 1335304046Sae memcpy(&hbuf[32], &f_id->dst_port, 2); 1336304046Sae memcpy(&hbuf[32], &f_id->src_port, 2); 1337304046Sae 1338304046Sae return (djb_hash(hbuf, sizeof(hbuf))); 1339304046Sae} 1340304046Sae 1341304046Saestatic NAT64NOINLINE int 1342304046Saenat64lsn_request_host(struct nat64lsn_cfg *cfg, 1343304046Sae const struct ipfw_flow_id *f_id, struct mbuf **pm) 1344304046Sae{ 1345304046Sae struct nat64lsn_job_item *ji; 1346304046Sae struct mbuf *m; 1347304046Sae 1348304046Sae m = *pm; 1349304046Sae *pm = NULL; 1350304046Sae 1351304046Sae ji = nat64lsn_create_job(cfg, f_id, JTYPE_NEWHOST); 1352304046Sae if (ji == NULL) { 1353304046Sae m_freem(m); 1354334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 1355304046Sae DPRINTF(DP_DROPS, "failed to create job"); 1356304046Sae } else { 1357304046Sae ji->m = m; 1358304046Sae /* Provide pseudo-random value based on flow */ 1359304046Sae ji->fhash = flow6_hash(f_id); 1360304046Sae nat64lsn_enqueue_job(cfg, ji); 1361334836Sae NAT64STAT_INC(&cfg->base.stats, jhostsreq); 1362304046Sae } 1363304046Sae 1364332767Sae return (IP_FW_DENY); 1365304046Sae} 1366304046Sae 1367304046Saestatic NAT64NOINLINE int 1368304046Saenat64lsn_request_portgroup(struct nat64lsn_cfg *cfg, 1369304046Sae const struct ipfw_flow_id *f_id, struct mbuf **pm, uint32_t aaddr, 1370304046Sae int needs_idx) 1371304046Sae{ 1372304046Sae struct nat64lsn_job_item *ji; 1373304046Sae struct mbuf *m; 1374304046Sae 1375304046Sae m = *pm; 1376304046Sae *pm = NULL; 1377304046Sae 1378304046Sae ji = nat64lsn_create_job(cfg, f_id, JTYPE_NEWPORTGROUP); 1379304046Sae if (ji == NULL) { 1380304046Sae m_freem(m); 1381334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 1382304046Sae DPRINTF(DP_DROPS, "failed to create job"); 1383304046Sae } else { 1384304046Sae ji->m = m; 1385304046Sae /* Provide pseudo-random value based on flow */ 1386304046Sae ji->fhash = flow6_hash(f_id); 1387304046Sae ji->aaddr = aaddr; 1388304046Sae ji->needs_idx = needs_idx; 1389304046Sae nat64lsn_enqueue_job(cfg, ji); 1390334836Sae NAT64STAT_INC(&cfg->base.stats, jportreq); 1391304046Sae } 1392304046Sae 1393332767Sae return (IP_FW_DENY); 1394304046Sae} 1395304046Sae 1396304046Saestatic NAT64NOINLINE struct nat64lsn_state * 1397304046Saenat64lsn_create_state(struct nat64lsn_cfg *cfg, struct nat64lsn_host *nh, 1398304046Sae int nat_proto, struct nat64lsn_state *kst, uint32_t *aaddr) 1399304046Sae{ 1400304046Sae struct nat64lsn_portgroup *pg; 1401304046Sae struct nat64lsn_state *st; 1402304046Sae int i, hval, off; 1403304046Sae 1404304046Sae /* XXX: create additional bitmask for selecting proper portgroup */ 1405304046Sae for (i = 0; i < nh->pg_used; i++) { 1406304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, i + 1); 1407304046Sae if (pg == NULL) 1408304046Sae continue; 1409304046Sae if (*aaddr == 0) 1410304046Sae *aaddr = pg->aaddr; 1411304046Sae if (pg->nat_proto != nat_proto) 1412304046Sae continue; 1413304046Sae 1414304046Sae off = PG_GET_FREE_IDX(pg); 1415304046Sae if (off != 0) { 1416304046Sae /* We have found spare state. Use it */ 1417304046Sae off--; 1418304046Sae PG_MARK_BUSY_IDX(pg, off); 1419304046Sae st = &pg->states[off]; 1420304046Sae 1421304046Sae /* 1422304046Sae * Fill in new info. Assume state was zeroed. 1423304046Sae * Timestamp and flags will be filled by caller. 1424304046Sae */ 1425304046Sae st->u.s = kst->u.s; 1426304046Sae st->cur.idx = i + 1; 1427304046Sae st->cur.off = off; 1428304046Sae 1429304046Sae /* Insert into host hash table */ 1430304046Sae hval = HASH_IN4(&st->u.hkey) & (nh->hsize - 1); 1431304046Sae st->next = nh->phash[hval]; 1432304046Sae nh->phash[hval] = st->cur; 1433304046Sae 1434304046Sae nat64lsn_dump_state(cfg, pg, st, "ALLOC STATE", off); 1435304046Sae 1436334836Sae NAT64STAT_INC(&cfg->base.stats, screated); 1437304046Sae 1438304046Sae return (st); 1439304046Sae } 1440304046Sae /* Saev last used alias affress */ 1441304046Sae *aaddr = pg->aaddr; 1442304046Sae } 1443304046Sae 1444304046Sae return (NULL); 1445304046Sae} 1446304046Sae 1447304046Saestatic NAT64NOINLINE int 1448304046Saenat64lsn_translate6(struct nat64lsn_cfg *cfg, struct ipfw_flow_id *f_id, 1449304046Sae struct mbuf **pm) 1450304046Sae{ 1451304046Sae struct pfloghdr loghdr, *logdata; 1452304046Sae char a[INET6_ADDRSTRLEN]; 1453304046Sae struct nat64lsn_host *nh; 1454304046Sae struct st_ptr sidx; 1455304046Sae struct nat64lsn_state *st, kst; 1456304046Sae struct nat64lsn_portgroup *pg; 1457304046Sae struct icmp6_hdr *icmp6; 1458304046Sae uint32_t aaddr; 1459304046Sae int action, hval, nat_proto, proto; 1460304046Sae uint16_t aport, state_ts, state_flags; 1461304046Sae 1462304046Sae /* Check if af/protocol is supported and get it short id */ 1463304046Sae nat_proto = nat64lsn_proto_map[f_id->proto]; 1464304046Sae if (nat_proto == 0) { 1465304046Sae /* 1466304046Sae * Since we can be called from jobs handler, we need 1467304046Sae * to free mbuf by self, do not leave this task to 1468304046Sae * ipfw_check_packet(). 1469304046Sae */ 1470334836Sae NAT64STAT_INC(&cfg->base.stats, noproto); 1471334836Sae goto drop; 1472304046Sae } 1473304046Sae 1474304046Sae /* Try to find host first */ 1475304046Sae I6HASH_FIND(cfg, nh, &f_id->src_ip6); 1476304046Sae 1477304046Sae if (nh == NULL) 1478304046Sae return (nat64lsn_request_host(cfg, f_id, pm)); 1479304046Sae 1480304046Sae /* Fill-in on-stack state structure */ 1481334836Sae kst.u.s.faddr = nat64_extract_ip4(&cfg->base, &f_id->dst_ip6); 1482334836Sae if (kst.u.s.faddr == 0) { 1483334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 1484334836Sae goto drop; 1485334836Sae } 1486304046Sae kst.u.s.fport = f_id->dst_port; 1487304046Sae kst.u.s.lport = f_id->src_port; 1488304046Sae 1489304046Sae /* Prepare some fields we might need to update */ 1490304046Sae hval = 0; 1491304046Sae proto = nat64_getlasthdr(*pm, &hval); 1492304046Sae if (proto < 0) { 1493334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 1494304046Sae DPRINTF(DP_DROPS, "dropped due to mbuf isn't contigious"); 1495334836Sae goto drop; 1496304046Sae } 1497304046Sae 1498304046Sae SET_AGE(state_ts); 1499304046Sae if (proto == IPPROTO_TCP) 1500304046Sae state_flags = convert_tcp_flags( 1501304046Sae TCP(mtodo(*pm, hval))->th_flags); 1502304046Sae else 1503304046Sae state_flags = 0; 1504304046Sae if (proto == IPPROTO_ICMPV6) { 1505304046Sae /* Alter local port data */ 1506304046Sae icmp6 = mtodo(*pm, hval); 1507304046Sae if (icmp6->icmp6_type == ICMP6_ECHO_REQUEST || 1508304046Sae icmp6->icmp6_type == ICMP6_ECHO_REPLY) 1509304046Sae kst.u.s.lport = ntohs(icmp6->icmp6_id); 1510304046Sae } 1511304046Sae 1512304046Sae hval = HASH_IN4(&kst.u.hkey) & (nh->hsize - 1); 1513304046Sae pg = NULL; 1514304046Sae st = NULL; 1515304046Sae 1516304046Sae /* OK, let's find state in host hash */ 1517304046Sae NAT64_LOCK(nh); 1518304046Sae sidx = nh->phash[hval]; 1519304046Sae int k = 0; 1520304046Sae while (sidx.idx != 0) { 1521304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, sidx.idx); 1522304046Sae st = &pg->states[sidx.off]; 1523304046Sae //DPRINTF("SISX: %d/%d next: %d/%d", sidx.idx, sidx.off, 1524304046Sae //st->next.idx, st->next.off); 1525304046Sae if (st->u.hkey == kst.u.hkey && pg->nat_proto == nat_proto) 1526304046Sae break; 1527304046Sae if (k++ > 1000) { 1528304046Sae DPRINTF(DP_ALL, "XXX: too long %d/%d %d/%d\n", 1529304046Sae sidx.idx, sidx.off, st->next.idx, st->next.off); 1530304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 1531304046Sae DPRINTF(DP_GENERIC, "TR host %s %p on cpu %d", 1532304046Sae a, nh, curcpu); 1533304046Sae k = 0; 1534304046Sae } 1535304046Sae sidx = st->next; 1536304046Sae } 1537304046Sae 1538304046Sae if (sidx.idx == 0) { 1539304046Sae aaddr = 0; 1540304046Sae st = nat64lsn_create_state(cfg, nh, nat_proto, &kst, &aaddr); 1541304046Sae if (st == NULL) { 1542304046Sae /* No free states. Request more if we can */ 1543304046Sae if (nh->pg_used >= cfg->max_chunks) { 1544304046Sae /* Limit reached */ 1545304046Sae inet_ntop(AF_INET6, &nh->addr, a, sizeof(a)); 1546304046Sae DPRINTF(DP_DROPS, "PG limit reached " 1547304046Sae " for host %s (used %u, allocated %u, " 1548304046Sae "limit %u)", a, 1549304046Sae nh->pg_used * NAT64_CHUNK_SIZE, 1550304046Sae nh->pg_allocated * NAT64_CHUNK_SIZE, 1551304046Sae cfg->max_chunks * NAT64_CHUNK_SIZE); 1552304046Sae NAT64_UNLOCK(nh); 1553334836Sae NAT64STAT_INC(&cfg->base.stats, dropped); 1554334836Sae goto drop; 1555304046Sae } 1556304046Sae if ((nh->pg_allocated <= 1557304046Sae nh->pg_used + NAT64LSN_REMAININGPG) && 1558304046Sae nh->pg_allocated < cfg->max_chunks) 1559304046Sae action = 1; /* Request new indexes */ 1560304046Sae else 1561304046Sae action = 0; 1562304046Sae NAT64_UNLOCK(nh); 1563304046Sae //DPRINTF("No state, unlock for %p", nh); 1564304046Sae return (nat64lsn_request_portgroup(cfg, f_id, 1565304046Sae pm, aaddr, action)); 1566304046Sae } 1567304046Sae 1568304046Sae /* We've got new state. */ 1569304046Sae sidx = st->cur; 1570304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, sidx.idx); 1571304046Sae } 1572304046Sae 1573304046Sae /* Okay, state found */ 1574304046Sae 1575304046Sae /* Update necessary fileds */ 1576304046Sae if (st->timestamp != state_ts) 1577304046Sae st->timestamp = state_ts; 1578304046Sae if ((st->flags & state_flags) != 0) 1579304046Sae st->flags |= state_flags; 1580304046Sae 1581304046Sae /* Copy needed state data */ 1582304046Sae aaddr = pg->aaddr; 1583304046Sae aport = htons(pg->aport + sidx.off); 1584304046Sae 1585304046Sae NAT64_UNLOCK(nh); 1586304046Sae 1587334836Sae if (cfg->base.flags & NAT64_LOG) { 1588304046Sae logdata = &loghdr; 1589304046Sae nat64lsn_log(logdata, *pm, AF_INET6, pg->idx, st->cur.off); 1590304046Sae } else 1591304046Sae logdata = NULL; 1592304046Sae 1593334836Sae action = nat64_do_handle_ip6(*pm, aaddr, aport, &cfg->base, logdata); 1594304046Sae if (action == NAT64SKIP) 1595332767Sae return (cfg->nomatch_verdict); 1596334836Sae if (action == NAT64MFREE) { 1597334836Saedrop: 1598304046Sae m_freem(*pm); 1599334836Sae } 1600304046Sae *pm = NULL; /* mark mbuf as consumed */ 1601304046Sae return (IP_FW_DENY); 1602304046Sae} 1603304046Sae 1604304046Sae/* 1605304046Sae * Main dataplane entry point. 1606304046Sae */ 1607304046Saeint 1608304046Saeipfw_nat64lsn(struct ip_fw_chain *ch, struct ip_fw_args *args, 1609304046Sae ipfw_insn *cmd, int *done) 1610304046Sae{ 1611304046Sae ipfw_insn *icmd; 1612304046Sae struct nat64lsn_cfg *cfg; 1613304046Sae int ret; 1614304046Sae 1615304046Sae IPFW_RLOCK_ASSERT(ch); 1616304046Sae 1617304046Sae *done = 1; /* terminate the search */ 1618304046Sae icmd = cmd + 1; 1619304046Sae if (cmd->opcode != O_EXTERNAL_ACTION || 1620304046Sae cmd->arg1 != V_nat64lsn_eid || 1621304046Sae icmd->opcode != O_EXTERNAL_INSTANCE || 1622304046Sae (cfg = NAT64_LOOKUP(ch, icmd)) == NULL) 1623304046Sae return (0); 1624304046Sae 1625304046Sae switch (args->f_id.addr_type) { 1626304046Sae case 4: 1627304046Sae ret = nat64lsn_translate4(cfg, &args->f_id, &args->m); 1628304046Sae break; 1629304046Sae case 6: 1630304046Sae ret = nat64lsn_translate6(cfg, &args->f_id, &args->m); 1631304046Sae break; 1632304046Sae default: 1633332767Sae return (cfg->nomatch_verdict); 1634304046Sae } 1635304046Sae return (ret); 1636304046Sae} 1637304046Sae 1638304046Saestatic int 1639304046Saenat64lsn_ctor_host(void *mem, int size, void *arg, int flags) 1640304046Sae{ 1641304046Sae struct nat64lsn_host *nh; 1642304046Sae 1643304046Sae nh = (struct nat64lsn_host *)mem; 1644304046Sae memset(nh->pg_ptr, 0, sizeof(nh->pg_ptr)); 1645304046Sae memset(nh->phash, 0, sizeof(nh->phash)); 1646304046Sae return (0); 1647304046Sae} 1648304046Sae 1649304046Saestatic int 1650304046Saenat64lsn_ctor_pgidx(void *mem, int size, void *arg, int flags) 1651304046Sae{ 1652304046Sae 1653304046Sae memset(mem, 0, size); 1654304046Sae return (0); 1655304046Sae} 1656304046Sae 1657304046Saevoid 1658304046Saenat64lsn_init_internal(void) 1659304046Sae{ 1660304046Sae 1661304046Sae memset(nat64lsn_proto_map, 0, sizeof(nat64lsn_proto_map)); 1662304046Sae /* Set up supported protocol map */ 1663304046Sae nat64lsn_proto_map[IPPROTO_TCP] = NAT_PROTO_TCP; 1664304046Sae nat64lsn_proto_map[IPPROTO_UDP] = NAT_PROTO_UDP; 1665304046Sae nat64lsn_proto_map[IPPROTO_ICMP] = NAT_PROTO_ICMP; 1666304046Sae nat64lsn_proto_map[IPPROTO_ICMPV6] = NAT_PROTO_ICMP; 1667304046Sae /* Fill in reverse proto map */ 1668304046Sae memset(nat64lsn_rproto_map, 0, sizeof(nat64lsn_rproto_map)); 1669304046Sae nat64lsn_rproto_map[NAT_PROTO_TCP] = IPPROTO_TCP; 1670304046Sae nat64lsn_rproto_map[NAT_PROTO_UDP] = IPPROTO_UDP; 1671304046Sae nat64lsn_rproto_map[NAT_PROTO_ICMP] = IPPROTO_ICMPV6; 1672304046Sae 1673304046Sae JQUEUE_LOCK_INIT(); 1674304046Sae nat64lsn_host_zone = uma_zcreate("NAT64 hosts zone", 1675304046Sae sizeof(struct nat64lsn_host), nat64lsn_ctor_host, NULL, 1676304046Sae NULL, NULL, UMA_ALIGN_PTR, 0); 1677304046Sae nat64lsn_pg_zone = uma_zcreate("NAT64 portgroups zone", 1678304046Sae sizeof(struct nat64lsn_portgroup), NULL, NULL, NULL, NULL, 1679304046Sae UMA_ALIGN_PTR, 0); 1680304046Sae nat64lsn_pgidx_zone = uma_zcreate("NAT64 portgroup indexes zone", 1681304046Sae sizeof(struct nat64lsn_portgroup *) * NAT64LSN_PGIDX_CHUNK, 1682304046Sae nat64lsn_ctor_pgidx, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1683304046Sae} 1684304046Sae 1685304046Saevoid 1686304046Saenat64lsn_uninit_internal(void) 1687304046Sae{ 1688304046Sae 1689304046Sae JQUEUE_LOCK_DESTROY(); 1690304046Sae uma_zdestroy(nat64lsn_host_zone); 1691304046Sae uma_zdestroy(nat64lsn_pg_zone); 1692304046Sae uma_zdestroy(nat64lsn_pgidx_zone); 1693304046Sae} 1694304046Sae 1695304046Saevoid 1696304046Saenat64lsn_start_instance(struct nat64lsn_cfg *cfg) 1697304046Sae{ 1698304046Sae 1699304046Sae callout_reset(&cfg->periodic, hz * PERIODIC_DELAY, 1700304046Sae nat64lsn_periodic, cfg); 1701304046Sae} 1702304046Sae 1703304046Saestruct nat64lsn_cfg * 1704304046Saenat64lsn_init_instance(struct ip_fw_chain *ch, size_t numaddr) 1705304046Sae{ 1706304046Sae struct nat64lsn_cfg *cfg; 1707304046Sae 1708304046Sae cfg = malloc(sizeof(struct nat64lsn_cfg), M_IPFW, M_WAITOK | M_ZERO); 1709304046Sae TAILQ_INIT(&cfg->jhead); 1710304046Sae cfg->vp = curvnet; 1711304046Sae cfg->ch = ch; 1712334836Sae COUNTER_ARRAY_ALLOC(cfg->base.stats.cnt, NAT64STATS, M_WAITOK); 1713304046Sae 1714304046Sae cfg->ihsize = NAT64LSN_HSIZE; 1715304046Sae cfg->ih = malloc(sizeof(void *) * cfg->ihsize, M_IPFW, 1716304046Sae M_WAITOK | M_ZERO); 1717304046Sae 1718304046Sae cfg->pg = malloc(sizeof(void *) * numaddr * _ADDR_PG_COUNT, M_IPFW, 1719304046Sae M_WAITOK | M_ZERO); 1720304046Sae 1721304046Sae callout_init(&cfg->periodic, CALLOUT_MPSAFE); 1722304046Sae callout_init(&cfg->jcallout, CALLOUT_MPSAFE); 1723304046Sae 1724304046Sae return (cfg); 1725304046Sae} 1726304046Sae 1727304046Sae/* 1728304046Sae * Destroy all hosts callback. 1729304046Sae * Called on module unload when all activity already finished, so 1730304046Sae * can work without any locks. 1731304046Sae */ 1732304046Saestatic NAT64NOINLINE int 1733304046Saenat64lsn_destroy_host(struct nat64lsn_host *nh, struct nat64lsn_cfg *cfg) 1734304046Sae{ 1735304046Sae struct nat64lsn_portgroup *pg; 1736304046Sae int i; 1737304046Sae 1738304046Sae for (i = nh->pg_used; i > 0; i--) { 1739304046Sae pg = PORTGROUP_BYSIDX(cfg, nh, i); 1740304046Sae if (pg == NULL) 1741304046Sae continue; 1742304046Sae cfg->pg[pg->idx] = NULL; 1743304046Sae destroy_portgroup(pg); 1744304046Sae nh->pg_used--; 1745304046Sae } 1746304046Sae destroy_host6(nh); 1747304046Sae cfg->ihcount--; 1748304046Sae return (0); 1749304046Sae} 1750304046Sae 1751304046Saevoid 1752304046Saenat64lsn_destroy_instance(struct nat64lsn_cfg *cfg) 1753304046Sae{ 1754304046Sae struct nat64lsn_host *nh, *tmp; 1755304046Sae 1756304046Sae callout_drain(&cfg->jcallout); 1757304046Sae callout_drain(&cfg->periodic); 1758304046Sae I6HASH_FOREACH_SAFE(cfg, nh, tmp, nat64lsn_destroy_host, cfg); 1759304046Sae DPRINTF(DP_OBJ, "instance %s: hosts %d", cfg->name, cfg->ihcount); 1760304046Sae 1761334836Sae COUNTER_ARRAY_FREE(cfg->base.stats.cnt, NAT64STATS); 1762304046Sae free(cfg->ih, M_IPFW); 1763304046Sae free(cfg->pg, M_IPFW); 1764304046Sae free(cfg, M_IPFW); 1765304046Sae} 1766304046Sae 1767