2 * Copyright (c) 2015-2016 Yandex LLC
3 * Copyright (c) 2015-2016 Andrey V. Elsukov <ae@FreeBSD.org>
4 * Copyright (c) 2015 Alexander V. Chernikov <melifaro@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/counter.h>
35 #include <sys/errno.h>
36 #include <sys/kernel.h>
38 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/rmlock.h>
42 #include <sys/rwlock.h>
43 #include <sys/socket.h>
44 #include <sys/sockopt.h>
45 #include <sys/queue.h>
46 #include <sys/syslog.h>
47 #include <sys/sysctl.h>
50 #include <net/if_var.h>
52 #include <net/route.h>
55 #include <netinet/in.h>
56 #include <netinet/ip_var.h>
57 #include <netinet/ip_fw.h>
58 #include <netinet6/in6_var.h>
59 #include <netinet6/ip6_var.h>
60 #include <netinet6/ip_fw_nat64.h>
62 #include <netpfil/ipfw/ip_fw_private.h>
66 VNET_DEFINE(uint16_t, nat64stl_eid) = 0;
68 static struct nat64stl_cfg *nat64stl_alloc_config(const char *name,
70 static void nat64stl_free_config(struct nat64stl_cfg *cfg);
71 static struct nat64stl_cfg *nat64stl_find(struct namedobj_instance *ni,
72 const char *name, uint8_t set);
74 static struct nat64stl_cfg *
75 nat64stl_alloc_config(const char *name, uint8_t set)
77 struct nat64stl_cfg *cfg;
79 cfg = malloc(sizeof(struct nat64stl_cfg), M_IPFW, M_WAITOK | M_ZERO);
80 COUNTER_ARRAY_ALLOC(cfg->base.stats.cnt, NAT64STATS, M_WAITOK);
81 cfg->no.name = cfg->name;
82 cfg->no.etlv = IPFW_TLV_NAT64STL_NAME;
84 strlcpy(cfg->name, name, sizeof(cfg->name));
89 nat64stl_free_config(struct nat64stl_cfg *cfg)
92 COUNTER_ARRAY_FREE(cfg->base.stats.cnt, NAT64STATS);
97 nat64stl_export_config(struct ip_fw_chain *ch, struct nat64stl_cfg *cfg,
98 ipfw_nat64stl_cfg *uc)
100 struct named_object *no;
102 uc->prefix6 = cfg->base.plat_prefix;
103 uc->plen6 = cfg->base.plat_plen;
104 uc->flags = cfg->base.flags & NAT64STL_FLAGSMASK;
105 uc->set = cfg->no.set;
106 strlcpy(uc->name, cfg->no.name, sizeof(uc->name));
108 no = ipfw_objhash_lookup_table_kidx(ch, cfg->map64);
109 ipfw_export_obj_ntlv(no, &uc->ntlv6);
110 no = ipfw_objhash_lookup_table_kidx(ch, cfg->map46);
111 ipfw_export_obj_ntlv(no, &uc->ntlv4);
114 struct nat64stl_dump_arg {
115 struct ip_fw_chain *ch;
116 struct sockopt_data *sd;
120 export_config_cb(struct namedobj_instance *ni, struct named_object *no,
123 struct nat64stl_dump_arg *da = (struct nat64stl_dump_arg *)arg;
124 ipfw_nat64stl_cfg *uc;
126 uc = (ipfw_nat64stl_cfg *)ipfw_get_sopt_space(da->sd, sizeof(*uc));
127 nat64stl_export_config(da->ch, (struct nat64stl_cfg *)no, uc);
131 static struct nat64stl_cfg *
132 nat64stl_find(struct namedobj_instance *ni, const char *name, uint8_t set)
134 struct nat64stl_cfg *cfg;
136 cfg = (struct nat64stl_cfg *)ipfw_objhash_lookup_name_type(ni, set,
137 IPFW_TLV_NAT64STL_NAME, name);
144 nat64stl_create_internal(struct ip_fw_chain *ch, struct nat64stl_cfg *cfg,
145 ipfw_nat64stl_cfg *i)
148 IPFW_UH_WLOCK_ASSERT(ch);
150 if (ipfw_objhash_alloc_idx(CHAIN_TO_SRV(ch), &cfg->no.kidx) != 0)
152 cfg->base.flags |= NAT64STL_KIDX;
154 if (ipfw_ref_table(ch, &i->ntlv4, &cfg->map46) != 0)
156 cfg->base.flags |= NAT64STL_46T;
158 if (ipfw_ref_table(ch, &i->ntlv6, &cfg->map64) != 0)
160 cfg->base.flags |= NAT64STL_64T;
162 ipfw_objhash_add(CHAIN_TO_SRV(ch), &cfg->no);
168 * Creates new nat64 instance.
169 * Data layout (v0)(current):
170 * Request: [ ipfw_obj_lheader ipfw_nat64stl_cfg ]
172 * Returns 0 on success
175 nat64stl_create(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
176 struct sockopt_data *sd)
178 ipfw_obj_lheader *olh;
179 ipfw_nat64stl_cfg *uc;
180 struct namedobj_instance *ni;
181 struct nat64stl_cfg *cfg;
184 if (sd->valsize != sizeof(*olh) + sizeof(*uc))
187 olh = (ipfw_obj_lheader *)sd->kbuf;
188 uc = (ipfw_nat64stl_cfg *)(olh + 1);
190 if (ipfw_check_object_name_generic(uc->name) != 0)
192 if (uc->set >= IPFW_MAX_SETS ||
193 nat64_check_prefix6(&uc->prefix6, uc->plen6) != 0)
196 /* XXX: check types of tables */
198 ni = CHAIN_TO_SRV(ch);
202 if (nat64stl_find(ni, uc->name, uc->set) != NULL) {
208 cfg = nat64stl_alloc_config(uc->name, uc->set);
209 cfg->base.plat_prefix = uc->prefix6;
210 cfg->base.plat_plen = uc->plen6;
211 cfg->base.flags = (uc->flags & NAT64STL_FLAGSMASK) | NAT64_PLATPFX;
212 if (IN6_IS_ADDR_WKPFX(&cfg->base.plat_prefix))
213 cfg->base.flags |= NAT64_WKPFX;
217 if (nat64stl_find(ni, uc->name, uc->set) != NULL) {
219 nat64stl_free_config(cfg);
222 error = nat64stl_create_internal(ch, cfg, uc);
224 /* Okay, let's link data */
225 SRV_OBJECT(ch, cfg->no.kidx) = cfg;
230 if (cfg->base.flags & NAT64STL_KIDX)
231 ipfw_objhash_free_idx(ni, cfg->no.kidx);
232 if (cfg->base.flags & NAT64STL_46T)
233 ipfw_unref_table(ch, cfg->map46);
234 if (cfg->base.flags & NAT64STL_64T)
235 ipfw_unref_table(ch, cfg->map64);
238 nat64stl_free_config(cfg);
243 * Change existing nat64stl instance configuration.
244 * Data layout (v0)(current):
245 * Request: [ ipfw_obj_header ipfw_nat64stl_cfg ]
246 * Reply: [ ipfw_obj_header ipfw_nat64stl_cfg ]
248 * Returns 0 on success
251 nat64stl_config(struct ip_fw_chain *ch, ip_fw3_opheader *op,
252 struct sockopt_data *sd)
255 ipfw_nat64stl_cfg *uc;
256 struct nat64stl_cfg *cfg;
257 struct namedobj_instance *ni;
259 if (sd->valsize != sizeof(*oh) + sizeof(*uc))
262 oh = (ipfw_obj_header *)ipfw_get_sopt_space(sd,
263 sizeof(*oh) + sizeof(*uc));
264 uc = (ipfw_nat64stl_cfg *)(oh + 1);
266 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 ||
267 oh->ntlv.set >= IPFW_MAX_SETS)
270 ni = CHAIN_TO_SRV(ch);
271 if (sd->sopt->sopt_dir == SOPT_GET) {
273 cfg = nat64stl_find(ni, oh->ntlv.name, oh->ntlv.set);
278 nat64stl_export_config(ch, cfg, uc);
284 cfg = nat64stl_find(ni, oh->ntlv.name, oh->ntlv.set);
291 * For now allow to change only following values:
294 cfg->base.flags &= ~NAT64STL_FLAGSMASK;
295 cfg->base.flags |= uc->flags & NAT64STL_FLAGSMASK;
302 nat64stl_detach_config(struct ip_fw_chain *ch, struct nat64stl_cfg *cfg)
305 IPFW_UH_WLOCK_ASSERT(ch);
307 ipfw_objhash_del(CHAIN_TO_SRV(ch), &cfg->no);
308 ipfw_objhash_free_idx(CHAIN_TO_SRV(ch), cfg->no.kidx);
309 ipfw_unref_table(ch, cfg->map46);
310 ipfw_unref_table(ch, cfg->map64);
314 * Destroys nat64 instance.
315 * Data layout (v0)(current):
316 * Request: [ ipfw_obj_header ]
318 * Returns 0 on success
321 nat64stl_destroy(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
322 struct sockopt_data *sd)
325 struct nat64stl_cfg *cfg;
327 if (sd->valsize != sizeof(*oh))
330 oh = (ipfw_obj_header *)sd->kbuf;
331 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0)
335 cfg = nat64stl_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
340 if (cfg->no.refcnt > 0) {
345 ipfw_reset_eaction_instance(ch, V_nat64stl_eid, cfg->no.kidx);
346 SRV_OBJECT(ch, cfg->no.kidx) = NULL;
347 nat64stl_detach_config(ch, cfg);
350 nat64stl_free_config(cfg);
355 * Lists all nat64stl instances currently available in kernel.
356 * Data layout (v0)(current):
357 * Request: [ ipfw_obj_lheader ]
358 * Reply: [ ipfw_obj_lheader ipfw_nat64stl_cfg x N ]
360 * Returns 0 on success
363 nat64stl_list(struct ip_fw_chain *ch, ip_fw3_opheader *op3,
364 struct sockopt_data *sd)
366 ipfw_obj_lheader *olh;
367 struct nat64stl_dump_arg da;
369 /* Check minimum header size */
370 if (sd->valsize < sizeof(ipfw_obj_lheader))
373 olh = (ipfw_obj_lheader *)ipfw_get_sopt_header(sd, sizeof(*olh));
376 olh->count = ipfw_objhash_count_type(CHAIN_TO_SRV(ch),
377 IPFW_TLV_NAT64STL_NAME);
378 olh->objsize = sizeof(ipfw_nat64stl_cfg);
379 olh->size = sizeof(*olh) + olh->count * olh->objsize;
381 if (sd->valsize < olh->size) {
385 memset(&da, 0, sizeof(da));
388 ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), export_config_cb,
389 &da, IPFW_TLV_NAT64STL_NAME);
395 #define __COPY_STAT_FIELD(_cfg, _stats, _field) \
396 (_stats)->_field = NAT64STAT_FETCH(&(_cfg)->base.stats, _field)
398 export_stats(struct ip_fw_chain *ch, struct nat64stl_cfg *cfg,
399 struct ipfw_nat64stl_stats *stats)
402 __COPY_STAT_FIELD(cfg, stats, opcnt64);
403 __COPY_STAT_FIELD(cfg, stats, opcnt46);
404 __COPY_STAT_FIELD(cfg, stats, ofrags);
405 __COPY_STAT_FIELD(cfg, stats, ifrags);
406 __COPY_STAT_FIELD(cfg, stats, oerrors);
407 __COPY_STAT_FIELD(cfg, stats, noroute4);
408 __COPY_STAT_FIELD(cfg, stats, noroute6);
409 __COPY_STAT_FIELD(cfg, stats, noproto);
410 __COPY_STAT_FIELD(cfg, stats, nomem);
411 __COPY_STAT_FIELD(cfg, stats, dropped);
415 * Get nat64stl statistics.
416 * Data layout (v0)(current):
417 * Request: [ ipfw_obj_header ]
418 * Reply: [ ipfw_obj_header ipfw_obj_ctlv [ uint64_t x N ]]
420 * Returns 0 on success
423 nat64stl_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op,
424 struct sockopt_data *sd)
426 struct ipfw_nat64stl_stats stats;
427 struct nat64stl_cfg *cfg;
432 sz = sizeof(ipfw_obj_header) + sizeof(ipfw_obj_ctlv) + sizeof(stats);
433 if (sd->valsize % sizeof(uint64_t))
435 if (sd->valsize < sz)
437 oh = (ipfw_obj_header *)ipfw_get_sopt_header(sd, sz);
440 memset(&stats, 0, sizeof(stats));
443 cfg = nat64stl_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
448 export_stats(ch, cfg, &stats);
451 ctlv = (ipfw_obj_ctlv *)(oh + 1);
452 memset(ctlv, 0, sizeof(*ctlv));
453 ctlv->head.type = IPFW_TLV_COUNTERS;
454 ctlv->head.length = sz - sizeof(ipfw_obj_header);
455 ctlv->count = sizeof(stats) / sizeof(uint64_t);
456 ctlv->objsize = sizeof(uint64_t);
457 ctlv->version = IPFW_NAT64_VERSION;
458 memcpy(ctlv + 1, &stats, sizeof(stats));
463 * Reset nat64stl statistics.
464 * Data layout (v0)(current):
465 * Request: [ ipfw_obj_header ]
467 * Returns 0 on success
470 nat64stl_reset_stats(struct ip_fw_chain *ch, ip_fw3_opheader *op,
471 struct sockopt_data *sd)
473 struct nat64stl_cfg *cfg;
476 if (sd->valsize != sizeof(*oh))
478 oh = (ipfw_obj_header *)sd->kbuf;
479 if (ipfw_check_object_name_generic(oh->ntlv.name) != 0 ||
480 oh->ntlv.set >= IPFW_MAX_SETS)
484 cfg = nat64stl_find(CHAIN_TO_SRV(ch), oh->ntlv.name, oh->ntlv.set);
489 COUNTER_ARRAY_ZERO(cfg->base.stats.cnt, NAT64STATS);
494 static struct ipfw_sopt_handler scodes[] = {
496 { IP_FW_NAT64STL_CREATE, 0, HDIR_SET, nat64stl_create },
497 { IP_FW_NAT64STL_DESTROY,0, HDIR_SET, nat64stl_destroy },
498 { IP_FW_NAT64STL_CONFIG, 0, HDIR_BOTH, nat64stl_config },
499 { IP_FW_NAT64STL_LIST, 0, HDIR_GET, nat64stl_list },
500 { IP_FW_NAT64STL_STATS, 0, HDIR_GET, nat64stl_stats },
501 { IP_FW_NAT64STL_RESET_STATS,0, HDIR_SET, nat64stl_reset_stats },
505 nat64stl_classify(ipfw_insn *cmd, uint16_t *puidx, uint8_t *ptype)
510 if (icmd->opcode != O_EXTERNAL_ACTION ||
511 icmd->arg1 != V_nat64stl_eid)
520 nat64stl_update_arg1(ipfw_insn *cmd, uint16_t idx)
527 nat64stl_findbyname(struct ip_fw_chain *ch, struct tid_info *ti,
528 struct named_object **pno)
532 err = ipfw_objhash_find_type(CHAIN_TO_SRV(ch), ti,
533 IPFW_TLV_NAT64STL_NAME, pno);
537 static struct named_object *
538 nat64stl_findbykidx(struct ip_fw_chain *ch, uint16_t idx)
540 struct namedobj_instance *ni;
541 struct named_object *no;
543 IPFW_UH_WLOCK_ASSERT(ch);
544 ni = CHAIN_TO_SRV(ch);
545 no = ipfw_objhash_lookup_kidx(ni, idx);
546 KASSERT(no != NULL, ("NAT with index %d not found", idx));
552 nat64stl_manage_sets(struct ip_fw_chain *ch, uint16_t set, uint8_t new_set,
553 enum ipfw_sets_cmd cmd)
556 return (ipfw_obj_manage_sets(CHAIN_TO_SRV(ch), IPFW_TLV_NAT64STL_NAME,
560 static struct opcode_obj_rewrite opcodes[] = {
562 .opcode = O_EXTERNAL_INSTANCE,
563 .etlv = IPFW_TLV_EACTION /* just show it isn't table */,
564 .classifier = nat64stl_classify,
565 .update = nat64stl_update_arg1,
566 .find_byname = nat64stl_findbyname,
567 .find_bykidx = nat64stl_findbykidx,
568 .manage_sets = nat64stl_manage_sets,
573 destroy_config_cb(struct namedobj_instance *ni, struct named_object *no,
576 struct nat64stl_cfg *cfg;
577 struct ip_fw_chain *ch;
579 ch = (struct ip_fw_chain *)arg;
580 cfg = (struct nat64stl_cfg *)SRV_OBJECT(ch, no->kidx);
581 SRV_OBJECT(ch, no->kidx) = NULL;
582 nat64stl_detach_config(ch, cfg);
583 nat64stl_free_config(cfg);
588 nat64stl_init(struct ip_fw_chain *ch, int first)
591 V_nat64stl_eid = ipfw_add_eaction(ch, ipfw_nat64stl, "nat64stl");
592 if (V_nat64stl_eid == 0)
594 IPFW_ADD_SOPT_HANDLER(first, scodes);
595 IPFW_ADD_OBJ_REWRITER(first, opcodes);
600 nat64stl_uninit(struct ip_fw_chain *ch, int last)
603 IPFW_DEL_OBJ_REWRITER(last, opcodes);
604 IPFW_DEL_SOPT_HANDLER(last, scodes);
605 ipfw_del_eaction(ch, V_nat64stl_eid);
607 * Since we already have deregistered external action,
608 * our named objects become unaccessible via rules, because
609 * all rules were truncated by ipfw_del_eaction().
610 * So, we can unlink and destroy our named objects without holding
614 ipfw_objhash_foreach_type(CHAIN_TO_SRV(ch), destroy_config_cb, ch,
615 IPFW_TLV_NAT64STL_NAME);