xref: /freebsd/sys/netpfil/ipfw/nat64/nat64clat_control.c (revision fdafd315ad0d0f28a11b9fb4476a9ab059c62b92)
15c04f73eSAndrey V. Elsukov /*-
2*4d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
35c04f73eSAndrey V. Elsukov  *
45c04f73eSAndrey V. Elsukov  * Copyright (c) 2019 Yandex LLC
55c04f73eSAndrey V. Elsukov  * Copyright (c) 2019 Andrey V. Elsukov <ae@FreeBSD.org>
65c04f73eSAndrey V. Elsukov  * Copyright (c) 2019 Boris N. Lytochkin <lytboris@gmail.com>
75c04f73eSAndrey V. Elsukov  *
85c04f73eSAndrey V. Elsukov  * Redistribution and use in source and binary forms, with or without
95c04f73eSAndrey V. Elsukov  * modification, are permitted provided that the following conditions
105c04f73eSAndrey V. Elsukov  * are met:
115c04f73eSAndrey V. Elsukov  *
125c04f73eSAndrey V. Elsukov  * 1. Redistributions of source code must retain the above copyright
135c04f73eSAndrey V. Elsukov  *    notice, this list of conditions and the following disclaimer.
145c04f73eSAndrey V. Elsukov  * 2. Redistributions in binary form must reproduce the above copyright
155c04f73eSAndrey V. Elsukov  *    notice, this list of conditions and the following disclaimer in the
165c04f73eSAndrey V. Elsukov  *    documentation and/or other materials provided with the distribution.
175c04f73eSAndrey V. Elsukov  *
185c04f73eSAndrey V. Elsukov  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
195c04f73eSAndrey V. Elsukov  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
205c04f73eSAndrey V. Elsukov  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
215c04f73eSAndrey V. Elsukov  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
225c04f73eSAndrey V. Elsukov  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
235c04f73eSAndrey V. Elsukov  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
245c04f73eSAndrey V. Elsukov  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
255c04f73eSAndrey V. Elsukov  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
265c04f73eSAndrey V. Elsukov  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
275c04f73eSAndrey V. Elsukov  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
285c04f73eSAndrey V. Elsukov  */
295c04f73eSAndrey V. Elsukov 
305c04f73eSAndrey V. Elsukov #include <sys/param.h>
315c04f73eSAndrey V. Elsukov #include <sys/systm.h>
325c04f73eSAndrey V. Elsukov #include <sys/counter.h>
335c04f73eSAndrey V. Elsukov #include <sys/errno.h>
345c04f73eSAndrey V. Elsukov #include <sys/kernel.h>
355c04f73eSAndrey V. Elsukov #include <sys/lock.h>
365c04f73eSAndrey V. Elsukov #include <sys/malloc.h>
375c04f73eSAndrey V. Elsukov #include <sys/mbuf.h>
385c04f73eSAndrey V. Elsukov #include <sys/module.h>
395c04f73eSAndrey V. Elsukov #include <sys/rmlock.h>
405c04f73eSAndrey V. Elsukov #include <sys/rwlock.h>
415c04f73eSAndrey V. Elsukov #include <sys/socket.h>
425c04f73eSAndrey V. Elsukov #include <sys/sockopt.h>
435c04f73eSAndrey V. Elsukov #include <sys/syslog.h>
445c04f73eSAndrey V. Elsukov #include <sys/sysctl.h>
455c04f73eSAndrey V. Elsukov 
465c04f73eSAndrey V. Elsukov #include <net/if.h>
475c04f73eSAndrey V. Elsukov #include <net/if_var.h>
485c04f73eSAndrey V. Elsukov #include <net/route.h>
495c04f73eSAndrey V. Elsukov #include <net/vnet.h>
505c04f73eSAndrey V. Elsukov 
515c04f73eSAndrey V. Elsukov #include <netinet/in.h>
525c04f73eSAndrey V. Elsukov #include <netinet/ip_var.h>
535c04f73eSAndrey V. Elsukov #include <netinet/ip_fw.h>
545c04f73eSAndrey V. Elsukov #include <netinet6/in6_var.h>
555c04f73eSAndrey V. Elsukov #include <netinet6/ip6_var.h>
565c04f73eSAndrey V. Elsukov #include <netinet6/ip_fw_nat64.h>
575c04f73eSAndrey V. Elsukov 
585c04f73eSAndrey V. Elsukov #include <netpfil/ipfw/ip_fw_private.h>
595c04f73eSAndrey V. Elsukov 
605c04f73eSAndrey V. Elsukov #include "nat64clat.h"
615c04f73eSAndrey V. Elsukov 
625c04f73eSAndrey V. Elsukov VNET_DEFINE(uint16_t, nat64clat_eid) = 0;
635c04f73eSAndrey V. Elsukov 
645c04f73eSAndrey V. Elsukov static struct nat64clat_cfg *nat64clat_alloc_config(const char *name,
655c04f73eSAndrey V. Elsukov     uint8_t set);
665c04f73eSAndrey V. Elsukov static void nat64clat_free_config(struct nat64clat_cfg *cfg);
675c04f73eSAndrey V. Elsukov static struct nat64clat_cfg *nat64clat_find(struct namedobj_instance *ni,
685c04f73eSAndrey V. Elsukov     const char *name, uint8_t set);
695c04f73eSAndrey V. Elsukov 
705c04f73eSAndrey V. Elsukov static struct nat64clat_cfg *
nat64clat_alloc_config(const char * name,uint8_t set)715c04f73eSAndrey V. Elsukov nat64clat_alloc_config(const char *name, uint8_t set)
725c04f73eSAndrey V. Elsukov {
735c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
745c04f73eSAndrey V. Elsukov 
755c04f73eSAndrey V. Elsukov 	cfg = malloc(sizeof(struct nat64clat_cfg), M_IPFW, M_WAITOK | M_ZERO);
765c04f73eSAndrey V. Elsukov 	COUNTER_ARRAY_ALLOC(cfg->base.stats.cnt, NAT64STATS, M_WAITOK);
775c04f73eSAndrey V. Elsukov 	cfg->no.name = cfg->name;
785c04f73eSAndrey V. Elsukov 	cfg->no.etlv = IPFW_TLV_NAT64CLAT_NAME;
795c04f73eSAndrey V. Elsukov 	cfg->no.set = set;
805c04f73eSAndrey V. Elsukov 	strlcpy(cfg->name, name, sizeof(cfg->name));
815c04f73eSAndrey V. Elsukov 	return (cfg);
825c04f73eSAndrey V. Elsukov }
835c04f73eSAndrey V. Elsukov 
845c04f73eSAndrey V. Elsukov static void
nat64clat_free_config(struct nat64clat_cfg * cfg)855c04f73eSAndrey V. Elsukov nat64clat_free_config(struct nat64clat_cfg *cfg)
865c04f73eSAndrey V. Elsukov {
875c04f73eSAndrey V. Elsukov 
885c04f73eSAndrey V. Elsukov 	COUNTER_ARRAY_FREE(cfg->base.stats.cnt, NAT64STATS);
895c04f73eSAndrey V. Elsukov 	free(cfg, M_IPFW);
905c04f73eSAndrey V. Elsukov }
915c04f73eSAndrey V. Elsukov 
925c04f73eSAndrey V. Elsukov static void
nat64clat_export_config(struct ip_fw_chain * ch,struct nat64clat_cfg * cfg,ipfw_nat64clat_cfg * uc)935c04f73eSAndrey V. Elsukov nat64clat_export_config(struct ip_fw_chain *ch, struct nat64clat_cfg *cfg,
945c04f73eSAndrey V. Elsukov     ipfw_nat64clat_cfg *uc)
955c04f73eSAndrey V. Elsukov {
965c04f73eSAndrey V. Elsukov 	uc->plat_prefix = cfg->base.plat_prefix;
975c04f73eSAndrey V. Elsukov 	uc->plat_plen = cfg->base.plat_plen;
985c04f73eSAndrey V. Elsukov 	uc->clat_prefix = cfg->base.clat_prefix;
995c04f73eSAndrey V. Elsukov 	uc->clat_plen = cfg->base.clat_plen;
1005c04f73eSAndrey V. Elsukov 	uc->flags = cfg->base.flags & NAT64CLAT_FLAGSMASK;
1015c04f73eSAndrey V. Elsukov 	uc->set = cfg->no.set;
1025c04f73eSAndrey V. Elsukov 	strlcpy(uc->name, cfg->no.name, sizeof(uc->name));
1035c04f73eSAndrey V. Elsukov }
1045c04f73eSAndrey V. Elsukov 
1055c04f73eSAndrey V. Elsukov struct nat64clat_dump_arg {
1065c04f73eSAndrey V. Elsukov 	struct ip_fw_chain *ch;
1075c04f73eSAndrey V. Elsukov 	struct sockopt_data *sd;
1085c04f73eSAndrey V. Elsukov };
1095c04f73eSAndrey V. Elsukov 
1105c04f73eSAndrey V. Elsukov static int
export_config_cb(struct namedobj_instance * ni,struct named_object * no,void * arg)1115c04f73eSAndrey V. Elsukov export_config_cb(struct namedobj_instance *ni, struct named_object *no,
1125c04f73eSAndrey V. Elsukov     void *arg)
1135c04f73eSAndrey V. Elsukov {
1145c04f73eSAndrey V. Elsukov 	struct nat64clat_dump_arg *da = (struct nat64clat_dump_arg *)arg;
1155c04f73eSAndrey V. Elsukov 	ipfw_nat64clat_cfg *uc;
1165c04f73eSAndrey V. Elsukov 
1175c04f73eSAndrey V. Elsukov 	uc = (ipfw_nat64clat_cfg *)ipfw_get_sopt_space(da->sd, sizeof(*uc));
1185c04f73eSAndrey V. Elsukov 	nat64clat_export_config(da->ch, (struct nat64clat_cfg *)no, uc);
1195c04f73eSAndrey V. Elsukov 	return (0);
1205c04f73eSAndrey V. Elsukov }
1215c04f73eSAndrey V. Elsukov 
1225c04f73eSAndrey V. Elsukov static struct nat64clat_cfg *
nat64clat_find(struct namedobj_instance * ni,const char * name,uint8_t set)1235c04f73eSAndrey V. Elsukov nat64clat_find(struct namedobj_instance *ni, const char *name, uint8_t set)
1245c04f73eSAndrey V. Elsukov {
1255c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
1265c04f73eSAndrey V. Elsukov 
1275c04f73eSAndrey V. Elsukov 	cfg = (struct nat64clat_cfg *)ipfw_objhash_lookup_name_type(ni, set,
1285c04f73eSAndrey V. Elsukov 	    IPFW_TLV_NAT64CLAT_NAME, name);
1295c04f73eSAndrey V. Elsukov 
1305c04f73eSAndrey V. Elsukov 	return (cfg);
1315c04f73eSAndrey V. Elsukov }
1325c04f73eSAndrey V. Elsukov 
1335c04f73eSAndrey V. Elsukov /*
1345c04f73eSAndrey V. Elsukov  * Creates new consumer-side nat64 translator instance.
1355c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
1365c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_lheader ipfw_nat64clat_cfg ]
1375c04f73eSAndrey V. Elsukov  *
1385c04f73eSAndrey V. Elsukov  * Returns 0 on success
1395c04f73eSAndrey V. Elsukov  */
1405c04f73eSAndrey V. Elsukov static int
nat64clat_create(struct ip_fw_chain * ch,ip_fw3_opheader * op3,struct sockopt_data * sd)1415c04f73eSAndrey V. Elsukov nat64clat_create(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
1425c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
1435c04f73eSAndrey V. Elsukov {
1445c04f73eSAndrey V. Elsukov 	ipfw_obj_lheader *olh;
1455c04f73eSAndrey V. Elsukov 	ipfw_nat64clat_cfg *uc;
1465c04f73eSAndrey V. Elsukov 	struct namedobj_instance *ni;
1475c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
1485c04f73eSAndrey V. Elsukov 
1495c04f73eSAndrey V. Elsukov 	if (sd->valsize != sizeof(*olh) + sizeof(*uc))
1505c04f73eSAndrey V. Elsukov 		return (EINVAL);
1515c04f73eSAndrey V. Elsukov 
1525c04f73eSAndrey V. Elsukov 	olh = (ipfw_obj_lheader *)sd->kbuf;
1535c04f73eSAndrey V. Elsukov 	uc = (ipfw_nat64clat_cfg *)(olh + 1);
1545c04f73eSAndrey V. Elsukov 
1555c04f73eSAndrey V. Elsukov 	if (ipfw_check_object_name_generic(uc->name) != 0)
1565c04f73eSAndrey V. Elsukov 		return (EINVAL);
1575c04f73eSAndrey V. Elsukov 
1585c04f73eSAndrey V. Elsukov 	if (uc->set >= IPFW_MAX_SETS ||
1595c04f73eSAndrey V. Elsukov 	    nat64_check_prefix6(&uc->plat_prefix, uc->plat_plen) != 0 ||
1605c04f73eSAndrey V. Elsukov 	    nat64_check_prefix6(&uc->clat_prefix, uc->clat_plen) != 0)
1615c04f73eSAndrey V. Elsukov 		return (EINVAL);
1625c04f73eSAndrey V. Elsukov 
1635c04f73eSAndrey V. Elsukov 	ni = CHAIN_TO_SRV(ch);
1645c04f73eSAndrey V. Elsukov 
1655c04f73eSAndrey V. Elsukov 	IPFW_UH_RLOCK(ch);
1665c04f73eSAndrey V. Elsukov 	if (nat64clat_find(ni, uc->name, uc->set) != NULL) {
1675c04f73eSAndrey V. Elsukov 		IPFW_UH_RUNLOCK(ch);
1685c04f73eSAndrey V. Elsukov 		return (EEXIST);
1695c04f73eSAndrey V. Elsukov 	}
1705c04f73eSAndrey V. Elsukov 	IPFW_UH_RUNLOCK(ch);
1715c04f73eSAndrey V. Elsukov 
1725c04f73eSAndrey V. Elsukov 	cfg = nat64clat_alloc_config(uc->name, uc->set);
1735c04f73eSAndrey V. Elsukov 	cfg->base.plat_prefix = uc->plat_prefix;
1745c04f73eSAndrey V. Elsukov 	cfg->base.plat_plen = uc->plat_plen;
1755c04f73eSAndrey V. Elsukov 	cfg->base.clat_prefix = uc->clat_prefix;
1765c04f73eSAndrey V. Elsukov 	cfg->base.clat_plen = uc->clat_plen;
1775c04f73eSAndrey V. Elsukov 	cfg->base.flags = (uc->flags & NAT64CLAT_FLAGSMASK) |
1785c04f73eSAndrey V. Elsukov 	    NAT64_CLATPFX | NAT64_PLATPFX;
1795c04f73eSAndrey V. Elsukov 	if (IN6_IS_ADDR_WKPFX(&cfg->base.plat_prefix))
1805c04f73eSAndrey V. Elsukov 		cfg->base.flags |= NAT64_WKPFX;
1815c04f73eSAndrey V. Elsukov 
1825c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK(ch);
1835c04f73eSAndrey V. Elsukov 
1845c04f73eSAndrey V. Elsukov 	if (nat64clat_find(ni, uc->name, uc->set) != NULL) {
1855c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
1865c04f73eSAndrey V. Elsukov 		nat64clat_free_config(cfg);
1875c04f73eSAndrey V. Elsukov 		return (EEXIST);
1885c04f73eSAndrey V. Elsukov 	}
1895c04f73eSAndrey V. Elsukov 
1905c04f73eSAndrey V. Elsukov 	if (ipfw_objhash_alloc_idx(ni, &cfg->no.kidx) != 0) {
1915c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
1925c04f73eSAndrey V. Elsukov 		nat64clat_free_config(cfg);
1935c04f73eSAndrey V. Elsukov 		return (ENOSPC);
1945c04f73eSAndrey V. Elsukov 	}
1955c04f73eSAndrey V. Elsukov 	ipfw_objhash_add(CHAIN_TO_SRV(ch), &cfg->no);
1965c04f73eSAndrey V. Elsukov 	/* Okay, let's link data */
1975c04f73eSAndrey V. Elsukov 	SRV_OBJECT(ch, cfg->no.kidx) = cfg;
1985c04f73eSAndrey V. Elsukov 	IPFW_UH_WUNLOCK(ch);
1995c04f73eSAndrey V. Elsukov 
2005c04f73eSAndrey V. Elsukov 	return (0);
2015c04f73eSAndrey V. Elsukov }
2025c04f73eSAndrey V. Elsukov 
2035c04f73eSAndrey V. Elsukov /*
2045c04f73eSAndrey V. Elsukov  * Change existing nat64clat instance configuration.
2055c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
2065c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ipfw_nat64clat_cfg ]
2075c04f73eSAndrey V. Elsukov  * Reply: [ ipfw_obj_header ipfw_nat64clat_cfg ]
2085c04f73eSAndrey V. Elsukov  *
2095c04f73eSAndrey V. Elsukov  * Returns 0 on success
2105c04f73eSAndrey V. Elsukov  */
2115c04f73eSAndrey V. Elsukov static int
nat64clat_config(struct ip_fw_chain * ch,ip_fw3_opheader * op,struct sockopt_data * sd)2125c04f73eSAndrey V. Elsukov nat64clat_config(struct ip_fw_chain *ch, ip_fw3_opheader *op,
2135c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
2145c04f73eSAndrey V. Elsukov {
2155c04f73eSAndrey V. Elsukov 	ipfw_obj_header *oh;
2165c04f73eSAndrey V. Elsukov 	ipfw_nat64clat_cfg *uc;
2175c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
2185c04f73eSAndrey V. Elsukov 	struct namedobj_instance *ni;
2195c04f73eSAndrey V. Elsukov 	uint32_t flags;
2205c04f73eSAndrey V. Elsukov 
2215c04f73eSAndrey V. Elsukov 	if (sd->valsize != sizeof(*oh) + sizeof(*uc))
2225c04f73eSAndrey V. Elsukov 		return (EINVAL);
2235c04f73eSAndrey V. Elsukov 
2245c04f73eSAndrey V. Elsukov 	oh = (ipfw_obj_header *)ipfw_get_sopt_space(sd,
2255c04f73eSAndrey V. Elsukov 	    sizeof(*oh) + sizeof(*uc));
2265c04f73eSAndrey V. Elsukov 	uc = (ipfw_nat64clat_cfg *)(oh + 1);
2275c04f73eSAndrey V. Elsukov 
2285c04f73eSAndrey V. Elsukov 	if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 ||
2295c04f73eSAndrey V. Elsukov 	    oh->ntlv.set >= IPFW_MAX_SETS)
2305c04f73eSAndrey V. Elsukov 		return (EINVAL);
2315c04f73eSAndrey V. Elsukov 
2325c04f73eSAndrey V. Elsukov 	ni = CHAIN_TO_SRV(ch);
2335c04f73eSAndrey V. Elsukov 	if (sd->sopt->sopt_dir == SOPT_GET) {
2345c04f73eSAndrey V. Elsukov 		IPFW_UH_RLOCK(ch);
2355c04f73eSAndrey V. Elsukov 		cfg = nat64clat_find(ni, oh->ntlv.name, oh->ntlv.set);
2365c04f73eSAndrey V. Elsukov 		if (cfg == NULL) {
2375c04f73eSAndrey V. Elsukov 			IPFW_UH_RUNLOCK(ch);
2385c04f73eSAndrey V. Elsukov 			return (ENOENT);
2395c04f73eSAndrey V. Elsukov 		}
2405c04f73eSAndrey V. Elsukov 		nat64clat_export_config(ch, cfg, uc);
2415c04f73eSAndrey V. Elsukov 		IPFW_UH_RUNLOCK(ch);
2425c04f73eSAndrey V. Elsukov 		return (0);
2435c04f73eSAndrey V. Elsukov 	}
2445c04f73eSAndrey V. Elsukov 
2455c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK(ch);
2465c04f73eSAndrey V. Elsukov 	cfg = nat64clat_find(ni, oh->ntlv.name, oh->ntlv.set);
2475c04f73eSAndrey V. Elsukov 	if (cfg == NULL) {
2485c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
2495c04f73eSAndrey V. Elsukov 		return (ENOENT);
2505c04f73eSAndrey V. Elsukov 	}
2515c04f73eSAndrey V. Elsukov 
2525c04f73eSAndrey V. Elsukov 	/*
2535c04f73eSAndrey V. Elsukov 	 * For now allow to change only following values:
2545c04f73eSAndrey V. Elsukov 	 *  plat_prefix, plat_plen, clat_prefix, clat_plen, flags.
2555c04f73eSAndrey V. Elsukov 	 */
2565c04f73eSAndrey V. Elsukov 	flags = 0;
2575c04f73eSAndrey V. Elsukov 	if (uc->plat_plen != cfg->base.plat_plen ||
2585c04f73eSAndrey V. Elsukov 	    !IN6_ARE_ADDR_EQUAL(&uc->plat_prefix, &cfg->base.plat_prefix)) {
2595c04f73eSAndrey V. Elsukov 		if (nat64_check_prefix6(&uc->plat_prefix, uc->plat_plen) != 0) {
2605c04f73eSAndrey V. Elsukov 			IPFW_UH_WUNLOCK(ch);
2615c04f73eSAndrey V. Elsukov 			return (EINVAL);
2625c04f73eSAndrey V. Elsukov 		}
2635c04f73eSAndrey V. Elsukov 		flags |= NAT64_PLATPFX;
2645c04f73eSAndrey V. Elsukov 	}
2655c04f73eSAndrey V. Elsukov 
2665c04f73eSAndrey V. Elsukov 	if (uc->clat_plen != cfg->base.clat_plen ||
2675c04f73eSAndrey V. Elsukov 	    !IN6_ARE_ADDR_EQUAL(&uc->clat_prefix, &cfg->base.clat_prefix)) {
2685c04f73eSAndrey V. Elsukov 		if (nat64_check_prefix6(&uc->clat_prefix, uc->clat_plen) != 0) {
2695c04f73eSAndrey V. Elsukov 			IPFW_UH_WUNLOCK(ch);
2705c04f73eSAndrey V. Elsukov 			return (EINVAL);
2715c04f73eSAndrey V. Elsukov 		}
2725c04f73eSAndrey V. Elsukov 		flags |= NAT64_CLATPFX;
2735c04f73eSAndrey V. Elsukov 	}
2745c04f73eSAndrey V. Elsukov 
2755c04f73eSAndrey V. Elsukov 	if (flags != 0) {
2765c04f73eSAndrey V. Elsukov 		IPFW_WLOCK(ch);
2775c04f73eSAndrey V. Elsukov 		if (flags & NAT64_PLATPFX) {
2785c04f73eSAndrey V. Elsukov 			cfg->base.plat_prefix = uc->plat_prefix;
2795c04f73eSAndrey V. Elsukov 			cfg->base.plat_plen = uc->plat_plen;
2805c04f73eSAndrey V. Elsukov 		}
2815c04f73eSAndrey V. Elsukov 		if (flags & NAT64_CLATPFX) {
2825c04f73eSAndrey V. Elsukov 			cfg->base.clat_prefix = uc->clat_prefix;
2835c04f73eSAndrey V. Elsukov 			cfg->base.clat_plen = uc->clat_plen;
2845c04f73eSAndrey V. Elsukov 		}
2855c04f73eSAndrey V. Elsukov 		IPFW_WUNLOCK(ch);
2865c04f73eSAndrey V. Elsukov 	}
2875c04f73eSAndrey V. Elsukov 
2885c04f73eSAndrey V. Elsukov 	cfg->base.flags &= ~NAT64CLAT_FLAGSMASK;
2895c04f73eSAndrey V. Elsukov 	cfg->base.flags |= uc->flags & NAT64CLAT_FLAGSMASK;
2905c04f73eSAndrey V. Elsukov 
2915c04f73eSAndrey V. Elsukov 	IPFW_UH_WUNLOCK(ch);
2925c04f73eSAndrey V. Elsukov 	return (0);
2935c04f73eSAndrey V. Elsukov }
2945c04f73eSAndrey V. Elsukov 
2955c04f73eSAndrey V. Elsukov static void
nat64clat_detach_config(struct ip_fw_chain * ch,struct nat64clat_cfg * cfg)2965c04f73eSAndrey V. Elsukov nat64clat_detach_config(struct ip_fw_chain *ch, struct nat64clat_cfg *cfg)
2975c04f73eSAndrey V. Elsukov {
2985c04f73eSAndrey V. Elsukov 
2995c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK_ASSERT(ch);
3005c04f73eSAndrey V. Elsukov 
3015c04f73eSAndrey V. Elsukov 	ipfw_objhash_del(CHAIN_TO_SRV(ch), &cfg->no);
3025c04f73eSAndrey V. Elsukov 	ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), cfg->no.kidx);
3035c04f73eSAndrey V. Elsukov }
3045c04f73eSAndrey V. Elsukov 
3055c04f73eSAndrey V. Elsukov /*
3065c04f73eSAndrey V. Elsukov  * Destroys nat64 instance.
3075c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
3085c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
3095c04f73eSAndrey V. Elsukov  *
3105c04f73eSAndrey V. Elsukov  * Returns 0 on success
3115c04f73eSAndrey V. Elsukov  */
3125c04f73eSAndrey V. Elsukov static int
nat64clat_destroy(struct ip_fw_chain * ch,ip_fw3_opheader * op3,struct sockopt_data * sd)3135c04f73eSAndrey V. Elsukov nat64clat_destroy(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
3145c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
3155c04f73eSAndrey V. Elsukov {
3165c04f73eSAndrey V. Elsukov 	ipfw_obj_header *oh;
3175c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
3185c04f73eSAndrey V. Elsukov 
3195c04f73eSAndrey V. Elsukov 	if (sd->valsize != sizeof(*oh))
3205c04f73eSAndrey V. Elsukov 		return (EINVAL);
3215c04f73eSAndrey V. Elsukov 
3225c04f73eSAndrey V. Elsukov 	oh = (ipfw_obj_header *)sd->kbuf;
3235c04f73eSAndrey V. Elsukov 	if (ipfw_check_object_name_generic(oh->ntlv.name) != 0)
3245c04f73eSAndrey V. Elsukov 		return (EINVAL);
3255c04f73eSAndrey V. Elsukov 
3265c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK(ch);
3275c04f73eSAndrey V. Elsukov 	cfg = nat64clat_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
3285c04f73eSAndrey V. Elsukov 	if (cfg == NULL) {
3295c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
3305c04f73eSAndrey V. Elsukov 		return (ENOENT);
3315c04f73eSAndrey V. Elsukov 	}
3325c04f73eSAndrey V. Elsukov 	if (cfg->no.refcnt > 0) {
3335c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
3345c04f73eSAndrey V. Elsukov 		return (EBUSY);
3355c04f73eSAndrey V. Elsukov 	}
3365c04f73eSAndrey V. Elsukov 
3375c04f73eSAndrey V. Elsukov 	ipfw_reset_eaction_instance(ch, V_nat64clat_eid, cfg->no.kidx);
3385c04f73eSAndrey V. Elsukov 	SRV_OBJECT(ch, cfg->no.kidx) = NULL;
3395c04f73eSAndrey V. Elsukov 	nat64clat_detach_config(ch, cfg);
3405c04f73eSAndrey V. Elsukov 	IPFW_UH_WUNLOCK(ch);
3415c04f73eSAndrey V. Elsukov 
3425c04f73eSAndrey V. Elsukov 	nat64clat_free_config(cfg);
3435c04f73eSAndrey V. Elsukov 	return (0);
3445c04f73eSAndrey V. Elsukov }
3455c04f73eSAndrey V. Elsukov 
3465c04f73eSAndrey V. Elsukov /*
3475c04f73eSAndrey V. Elsukov  * Lists all nat64clat instances currently available in kernel.
3485c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
3495c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_lheader ]
3505c04f73eSAndrey V. Elsukov  * Reply: [ ipfw_obj_lheader ipfw_nat64clat_cfg x N ]
3515c04f73eSAndrey V. Elsukov  *
3525c04f73eSAndrey V. Elsukov  * Returns 0 on success
3535c04f73eSAndrey V. Elsukov  */
3545c04f73eSAndrey V. Elsukov static int
nat64clat_list(struct ip_fw_chain * ch,ip_fw3_opheader * op3,struct sockopt_data * sd)3555c04f73eSAndrey V. Elsukov nat64clat_list(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
3565c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
3575c04f73eSAndrey V. Elsukov {
3585c04f73eSAndrey V. Elsukov 	ipfw_obj_lheader *olh;
3595c04f73eSAndrey V. Elsukov 	struct nat64clat_dump_arg da;
3605c04f73eSAndrey V. Elsukov 
3615c04f73eSAndrey V. Elsukov 	/* Check minimum header size */
3625c04f73eSAndrey V. Elsukov 	if (sd->valsize < sizeof(ipfw_obj_lheader))
3635c04f73eSAndrey V. Elsukov 		return (EINVAL);
3645c04f73eSAndrey V. Elsukov 
3655c04f73eSAndrey V. Elsukov 	olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh));
3665c04f73eSAndrey V. Elsukov 
3675c04f73eSAndrey V. Elsukov 	IPFW_UH_RLOCK(ch);
3685c04f73eSAndrey V. Elsukov 	olh->count = ipfw_objhash_count_type(CHAIN_TO_SRV(ch),
3695c04f73eSAndrey V. Elsukov 	    IPFW_TLV_NAT64CLAT_NAME);
3705c04f73eSAndrey V. Elsukov 	olh->objsize = sizeof(ipfw_nat64clat_cfg);
3715c04f73eSAndrey V. Elsukov 	olh->size = sizeof(*olh) + olh->count * olh->objsize;
3725c04f73eSAndrey V. Elsukov 
3735c04f73eSAndrey V. Elsukov 	if (sd->valsize < olh->size) {
3745c04f73eSAndrey V. Elsukov 		IPFW_UH_RUNLOCK(ch);
3755c04f73eSAndrey V. Elsukov 		return (ENOMEM);
3765c04f73eSAndrey V. Elsukov 	}
3775c04f73eSAndrey V. Elsukov 	memset(&da, 0, sizeof(da));
3785c04f73eSAndrey V. Elsukov 	da.ch = ch;
3795c04f73eSAndrey V. Elsukov 	da.sd = sd;
3805c04f73eSAndrey V. Elsukov 	ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), export_config_cb,
3815c04f73eSAndrey V. Elsukov 	    &da, IPFW_TLV_NAT64CLAT_NAME);
3825c04f73eSAndrey V. Elsukov 	IPFW_UH_RUNLOCK(ch);
3835c04f73eSAndrey V. Elsukov 
3845c04f73eSAndrey V. Elsukov 	return (0);
3855c04f73eSAndrey V. Elsukov }
3865c04f73eSAndrey V. Elsukov 
3875c04f73eSAndrey V. Elsukov #define	__COPY_STAT_FIELD(_cfg, _stats, _field)	\
3885c04f73eSAndrey V. Elsukov 	(_stats)->_field = NAT64STAT_FETCH(&(_cfg)->base.stats, _field)
3895c04f73eSAndrey V. Elsukov static void
export_stats(struct ip_fw_chain * ch,struct nat64clat_cfg * cfg,struct ipfw_nat64clat_stats * stats)3905c04f73eSAndrey V. Elsukov export_stats(struct ip_fw_chain *ch, struct nat64clat_cfg *cfg,
3915c04f73eSAndrey V. Elsukov     struct ipfw_nat64clat_stats *stats)
3925c04f73eSAndrey V. Elsukov {
3935c04f73eSAndrey V. Elsukov 
3945c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, opcnt64);
3955c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, opcnt46);
3965c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, ofrags);
3975c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, ifrags);
3985c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, oerrors);
3995c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, noroute4);
4005c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, noroute6);
4015c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, noproto);
4025c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, nomem);
4035c04f73eSAndrey V. Elsukov 	__COPY_STAT_FIELD(cfg, stats, dropped);
4045c04f73eSAndrey V. Elsukov }
4055c04f73eSAndrey V. Elsukov 
4065c04f73eSAndrey V. Elsukov /*
4075c04f73eSAndrey V. Elsukov  * Get nat64clat statistics.
4085c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
4095c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
4105c04f73eSAndrey V. Elsukov  * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ]]
4115c04f73eSAndrey V. Elsukov  *
4125c04f73eSAndrey V. Elsukov  * Returns 0 on success
4135c04f73eSAndrey V. Elsukov  */
4145c04f73eSAndrey V. Elsukov static int
nat64clat_stats(struct ip_fw_chain * ch,ip_fw3_opheader * op,struct sockopt_data * sd)4155c04f73eSAndrey V. Elsukov nat64clat_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op,
4165c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
4175c04f73eSAndrey V. Elsukov {
4185c04f73eSAndrey V. Elsukov 	struct ipfw_nat64clat_stats stats;
4195c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
4205c04f73eSAndrey V. Elsukov 	ipfw_obj_header *oh;
4215c04f73eSAndrey V. Elsukov 	ipfw_obj_ctlv *ctlv;
4225c04f73eSAndrey V. Elsukov 	size_t sz;
4235c04f73eSAndrey V. Elsukov 
4245c04f73eSAndrey V. Elsukov 	sz = sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ctlv) + sizeof(stats);
4255c04f73eSAndrey V. Elsukov 	if (sd->valsize % sizeof(uint64_t))
4265c04f73eSAndrey V. Elsukov 		return (EINVAL);
4275c04f73eSAndrey V. Elsukov 	if (sd->valsize < sz)
4285c04f73eSAndrey V. Elsukov 		return (ENOMEM);
4295c04f73eSAndrey V. Elsukov 	oh = (ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
4305c04f73eSAndrey V. Elsukov 	if (oh == NULL)
4315c04f73eSAndrey V. Elsukov 		return (EINVAL);
4325c04f73eSAndrey V. Elsukov 	memset(&stats, 0, sizeof(stats));
4335c04f73eSAndrey V. Elsukov 
4345c04f73eSAndrey V. Elsukov 	IPFW_UH_RLOCK(ch);
4355c04f73eSAndrey V. Elsukov 	cfg = nat64clat_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
4365c04f73eSAndrey V. Elsukov 	if (cfg == NULL) {
4375c04f73eSAndrey V. Elsukov 		IPFW_UH_RUNLOCK(ch);
4385c04f73eSAndrey V. Elsukov 		return (ENOENT);
4395c04f73eSAndrey V. Elsukov 	}
4405c04f73eSAndrey V. Elsukov 	export_stats(ch, cfg, &stats);
4415c04f73eSAndrey V. Elsukov 	IPFW_UH_RUNLOCK(ch);
4425c04f73eSAndrey V. Elsukov 
4435c04f73eSAndrey V. Elsukov 	ctlv = (ipfw_obj_ctlv *)(oh + 1);
4445c04f73eSAndrey V. Elsukov 	memset(ctlv, 0, sizeof(*ctlv));
4455c04f73eSAndrey V. Elsukov 	ctlv->head.type = IPFW_TLV_COUNTERS;
4465c04f73eSAndrey V. Elsukov 	ctlv->head.length = sz - sizeof(ipfw_obj_header);
4475c04f73eSAndrey V. Elsukov 	ctlv->count = sizeof(stats) / sizeof(uint64_t);
4485c04f73eSAndrey V. Elsukov 	ctlv->objsize = sizeof(uint64_t);
4495c04f73eSAndrey V. Elsukov 	ctlv->version = IPFW_NAT64_VERSION;
4505c04f73eSAndrey V. Elsukov 	memcpy(ctlv + 1, &stats, sizeof(stats));
4515c04f73eSAndrey V. Elsukov 	return (0);
4525c04f73eSAndrey V. Elsukov }
4535c04f73eSAndrey V. Elsukov 
4545c04f73eSAndrey V. Elsukov /*
4555c04f73eSAndrey V. Elsukov  * Reset nat64clat statistics.
4565c04f73eSAndrey V. Elsukov  * Data layout (v0)(current):
4575c04f73eSAndrey V. Elsukov  * Request: [ ipfw_obj_header ]
4585c04f73eSAndrey V. Elsukov  *
4595c04f73eSAndrey V. Elsukov  * Returns 0 on success
4605c04f73eSAndrey V. Elsukov  */
4615c04f73eSAndrey V. Elsukov static int
nat64clat_reset_stats(struct ip_fw_chain * ch,ip_fw3_opheader * op,struct sockopt_data * sd)4625c04f73eSAndrey V. Elsukov nat64clat_reset_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op,
4635c04f73eSAndrey V. Elsukov     struct sockopt_data *sd)
4645c04f73eSAndrey V. Elsukov {
4655c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
4665c04f73eSAndrey V. Elsukov 	ipfw_obj_header *oh;
4675c04f73eSAndrey V. Elsukov 
4685c04f73eSAndrey V. Elsukov 	if (sd->valsize != sizeof(*oh))
4695c04f73eSAndrey V. Elsukov 		return (EINVAL);
4705c04f73eSAndrey V. Elsukov 	oh = (ipfw_obj_header *)sd->kbuf;
4715c04f73eSAndrey V. Elsukov 	if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 ||
4725c04f73eSAndrey V. Elsukov 	    oh->ntlv.set >= IPFW_MAX_SETS)
4735c04f73eSAndrey V. Elsukov 		return (EINVAL);
4745c04f73eSAndrey V. Elsukov 
4755c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK(ch);
4765c04f73eSAndrey V. Elsukov 	cfg = nat64clat_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
4775c04f73eSAndrey V. Elsukov 	if (cfg == NULL) {
4785c04f73eSAndrey V. Elsukov 		IPFW_UH_WUNLOCK(ch);
4795c04f73eSAndrey V. Elsukov 		return (ENOENT);
4805c04f73eSAndrey V. Elsukov 	}
4815c04f73eSAndrey V. Elsukov 	COUNTER_ARRAY_ZERO(cfg->base.stats.cnt, NAT64STATS);
4825c04f73eSAndrey V. Elsukov 	IPFW_UH_WUNLOCK(ch);
4835c04f73eSAndrey V. Elsukov 	return (0);
4845c04f73eSAndrey V. Elsukov }
4855c04f73eSAndrey V. Elsukov 
4865c04f73eSAndrey V. Elsukov static struct ipfw_sopt_handler	scodes[] = {
4875c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_CREATE, 0,	HDIR_SET,	nat64clat_create },
4885c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_DESTROY,0,	HDIR_SET,	nat64clat_destroy },
4895c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_CONFIG, 0,	HDIR_BOTH,	nat64clat_config },
4905c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_LIST,   0,	HDIR_GET,	nat64clat_list },
4915c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_STATS,  0,	HDIR_GET,	nat64clat_stats },
4925c04f73eSAndrey V. Elsukov 	{ IP_FW_NAT64CLAT_RESET_STATS,0,	HDIR_SET,	nat64clat_reset_stats },
4935c04f73eSAndrey V. Elsukov };
4945c04f73eSAndrey V. Elsukov 
4955c04f73eSAndrey V. Elsukov static int
nat64clat_classify(ipfw_insn * cmd,uint16_t * puidx,uint8_t * ptype)4965c04f73eSAndrey V. Elsukov nat64clat_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
4975c04f73eSAndrey V. Elsukov {
4985c04f73eSAndrey V. Elsukov 	ipfw_insn *icmd;
4995c04f73eSAndrey V. Elsukov 
5005c04f73eSAndrey V. Elsukov 	icmd = cmd - 1;
5015c04f73eSAndrey V. Elsukov 	if (icmd->opcode != O_EXTERNAL_ACTION ||
5025c04f73eSAndrey V. Elsukov 	    icmd->arg1 != V_nat64clat_eid)
5035c04f73eSAndrey V. Elsukov 		return (1);
5045c04f73eSAndrey V. Elsukov 
5055c04f73eSAndrey V. Elsukov 	*puidx = cmd->arg1;
5065c04f73eSAndrey V. Elsukov 	*ptype = 0;
5075c04f73eSAndrey V. Elsukov 	return (0);
5085c04f73eSAndrey V. Elsukov }
5095c04f73eSAndrey V. Elsukov 
5105c04f73eSAndrey V. Elsukov static void
nat64clat_update_arg1(ipfw_insn * cmd,uint16_t idx)5115c04f73eSAndrey V. Elsukov nat64clat_update_arg1(ipfw_insn *cmd, uint16_t idx)
5125c04f73eSAndrey V. Elsukov {
5135c04f73eSAndrey V. Elsukov 
5145c04f73eSAndrey V. Elsukov 	cmd->arg1 = idx;
5155c04f73eSAndrey V. Elsukov }
5165c04f73eSAndrey V. Elsukov 
5175c04f73eSAndrey V. Elsukov static int
nat64clat_findbyname(struct ip_fw_chain * ch,struct tid_info * ti,struct named_object ** pno)5185c04f73eSAndrey V. Elsukov nat64clat_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
5195c04f73eSAndrey V. Elsukov     struct named_object **pno)
5205c04f73eSAndrey V. Elsukov {
5215c04f73eSAndrey V. Elsukov 	int err;
5225c04f73eSAndrey V. Elsukov 
5235c04f73eSAndrey V. Elsukov 	err = ipfw_objhash_find_type(CHAIN_TO_SRV(ch), ti,
5245c04f73eSAndrey V. Elsukov 	    IPFW_TLV_NAT64CLAT_NAME, pno);
5255c04f73eSAndrey V. Elsukov 	return (err);
5265c04f73eSAndrey V. Elsukov }
5275c04f73eSAndrey V. Elsukov 
5285c04f73eSAndrey V. Elsukov static struct named_object *
nat64clat_findbykidx(struct ip_fw_chain * ch,uint16_t idx)5295c04f73eSAndrey V. Elsukov nat64clat_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
5305c04f73eSAndrey V. Elsukov {
5315c04f73eSAndrey V. Elsukov 	struct namedobj_instance *ni;
5325c04f73eSAndrey V. Elsukov 	struct named_object *no;
5335c04f73eSAndrey V. Elsukov 
5345c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK_ASSERT(ch);
5355c04f73eSAndrey V. Elsukov 	ni = CHAIN_TO_SRV(ch);
5365c04f73eSAndrey V. Elsukov 	no = ipfw_objhash_lookup_kidx(ni, idx);
5375c04f73eSAndrey V. Elsukov 	KASSERT(no != NULL, ("NAT with index %d not found", idx));
5385c04f73eSAndrey V. Elsukov 
5395c04f73eSAndrey V. Elsukov 	return (no);
5405c04f73eSAndrey V. Elsukov }
5415c04f73eSAndrey V. Elsukov 
5425c04f73eSAndrey V. Elsukov static int
nat64clat_manage_sets(struct ip_fw_chain * ch,uint16_t set,uint8_t new_set,enum ipfw_sets_cmd cmd)5435c04f73eSAndrey V. Elsukov nat64clat_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
5445c04f73eSAndrey V. Elsukov     enum ipfw_sets_cmd cmd)
5455c04f73eSAndrey V. Elsukov {
5465c04f73eSAndrey V. Elsukov 
5475c04f73eSAndrey V. Elsukov 	return (ipfw_obj_manage_sets(CHAIN_TO_SRV(ch), IPFW_TLV_NAT64CLAT_NAME,
5485c04f73eSAndrey V. Elsukov 	    set, new_set, cmd));
5495c04f73eSAndrey V. Elsukov }
5505c04f73eSAndrey V. Elsukov 
5515c04f73eSAndrey V. Elsukov static struct opcode_obj_rewrite opcodes[] = {
5525c04f73eSAndrey V. Elsukov 	{
5535c04f73eSAndrey V. Elsukov 		.opcode = O_EXTERNAL_INSTANCE,
5545c04f73eSAndrey V. Elsukov 		.etlv = IPFW_TLV_EACTION /* just show it isn't table */,
5555c04f73eSAndrey V. Elsukov 		.classifier = nat64clat_classify,
5565c04f73eSAndrey V. Elsukov 		.update = nat64clat_update_arg1,
5575c04f73eSAndrey V. Elsukov 		.find_byname = nat64clat_findbyname,
5585c04f73eSAndrey V. Elsukov 		.find_bykidx = nat64clat_findbykidx,
5595c04f73eSAndrey V. Elsukov 		.manage_sets = nat64clat_manage_sets,
5605c04f73eSAndrey V. Elsukov 	},
5615c04f73eSAndrey V. Elsukov };
5625c04f73eSAndrey V. Elsukov 
5635c04f73eSAndrey V. Elsukov static int
destroy_config_cb(struct namedobj_instance * ni,struct named_object * no,void * arg)5645c04f73eSAndrey V. Elsukov destroy_config_cb(struct namedobj_instance *ni, struct named_object *no,
5655c04f73eSAndrey V. Elsukov     void *arg)
5665c04f73eSAndrey V. Elsukov {
5675c04f73eSAndrey V. Elsukov 	struct nat64clat_cfg *cfg;
5685c04f73eSAndrey V. Elsukov 	struct ip_fw_chain *ch;
5695c04f73eSAndrey V. Elsukov 
5705c04f73eSAndrey V. Elsukov 	ch = (struct ip_fw_chain *)arg;
5715c04f73eSAndrey V. Elsukov 	cfg = (struct nat64clat_cfg *)SRV_OBJECT(ch, no->kidx);
5725c04f73eSAndrey V. Elsukov 	SRV_OBJECT(ch, no->kidx) = NULL;
5735c04f73eSAndrey V. Elsukov 	nat64clat_detach_config(ch, cfg);
5745c04f73eSAndrey V. Elsukov 	nat64clat_free_config(cfg);
5755c04f73eSAndrey V. Elsukov 	return (0);
5765c04f73eSAndrey V. Elsukov }
5775c04f73eSAndrey V. Elsukov 
5785c04f73eSAndrey V. Elsukov int
nat64clat_init(struct ip_fw_chain * ch,int first)5795c04f73eSAndrey V. Elsukov nat64clat_init(struct ip_fw_chain *ch, int first)
5805c04f73eSAndrey V. Elsukov {
5815c04f73eSAndrey V. Elsukov 
5825c04f73eSAndrey V. Elsukov 	V_nat64clat_eid = ipfw_add_eaction(ch, ipfw_nat64clat, "nat64clat");
5835c04f73eSAndrey V. Elsukov 	if (V_nat64clat_eid == 0)
5845c04f73eSAndrey V. Elsukov 		return (ENXIO);
5855c04f73eSAndrey V. Elsukov 	IPFW_ADD_SOPT_HANDLER(first, scodes);
5865c04f73eSAndrey V. Elsukov 	IPFW_ADD_OBJ_REWRITER(first, opcodes);
5875c04f73eSAndrey V. Elsukov 	return (0);
5885c04f73eSAndrey V. Elsukov }
5895c04f73eSAndrey V. Elsukov 
5905c04f73eSAndrey V. Elsukov void
nat64clat_uninit(struct ip_fw_chain * ch,int last)5915c04f73eSAndrey V. Elsukov nat64clat_uninit(struct ip_fw_chain *ch, int last)
5925c04f73eSAndrey V. Elsukov {
5935c04f73eSAndrey V. Elsukov 
5945c04f73eSAndrey V. Elsukov 	IPFW_DEL_OBJ_REWRITER(last, opcodes);
5955c04f73eSAndrey V. Elsukov 	IPFW_DEL_SOPT_HANDLER(last, scodes);
5965c04f73eSAndrey V. Elsukov 	ipfw_del_eaction(ch, V_nat64clat_eid);
5975c04f73eSAndrey V. Elsukov 	/*
5985c04f73eSAndrey V. Elsukov 	 * Since we already have deregistered external action,
5995c04f73eSAndrey V. Elsukov 	 * our named objects become unaccessible via rules, because
6005c04f73eSAndrey V. Elsukov 	 * all rules were truncated by ipfw_del_eaction().
6015c04f73eSAndrey V. Elsukov 	 * So, we can unlink and destroy our named objects without holding
6025c04f73eSAndrey V. Elsukov 	 * IPFW_WLOCK().
6035c04f73eSAndrey V. Elsukov 	 */
6045c04f73eSAndrey V. Elsukov 	IPFW_UH_WLOCK(ch);
6055c04f73eSAndrey V. Elsukov 	ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), destroy_config_cb, ch,
6065c04f73eSAndrey V. Elsukov 	    IPFW_TLV_NAT64CLAT_NAME);
6075c04f73eSAndrey V. Elsukov 	V_nat64clat_eid = 0;
6085c04f73eSAndrey V. Elsukov 	IPFW_UH_WUNLOCK(ch);
6095c04f73eSAndrey V. Elsukov }
610