summaryrefslogtreecommitdiffstats
path: root/extensions/libipt_DNAT.c
diff options
context:
space:
mode:
authorPhil Sutter <phil@nwl.cc>2022-03-25 19:49:43 +0100
committerPhil Sutter <phil@nwl.cc>2022-04-08 18:00:42 +0200
commit9621318b8eb6242419ac67260ff0e56d6904c8d1 (patch)
tree131dda4c7ee7e4a202197cda8066eef0b1181439 /extensions/libipt_DNAT.c
parent2e0c9a406d07fcff42b2e4345bf52600479989d6 (diff)
extensions: DNAT: Rename from libipt to libxt
Prepare for merge of libipt and libip6t DNAT extensions, allow for better code review. Signed-off-by: Phil Sutter <phil@nwl.cc>
Diffstat (limited to 'extensions/libipt_DNAT.c')
-rw-r--r--extensions/libipt_DNAT.c370
1 files changed, 0 insertions, 370 deletions
diff --git a/extensions/libipt_DNAT.c b/extensions/libipt_DNAT.c
deleted file mode 100644
index 9a179919..00000000
--- a/extensions/libipt_DNAT.c
+++ /dev/null
@@ -1,370 +0,0 @@
-#include <stdio.h>
-#include <netdb.h>
-#include <string.h>
-#include <stdlib.h>
-#include <xtables.h>
-#include <iptables.h> /* get_kernel_version */
-#include <limits.h> /* INT_MAX in ip_tables.h */
-#include <arpa/inet.h>
-#include <linux/netfilter_ipv4/ip_tables.h>
-#include <linux/netfilter/nf_nat.h>
-
-#define TO_IPV4_MRC(ptr) ((const struct nf_nat_ipv4_multi_range_compat *)(ptr))
-#define RANGE2_INIT_FROM_IPV4_MRC(ptr) { \
- .flags = TO_IPV4_MRC(ptr)->range[0].flags, \
- .min_addr.ip = TO_IPV4_MRC(ptr)->range[0].min_ip, \
- .max_addr.ip = TO_IPV4_MRC(ptr)->range[0].max_ip, \
- .min_proto = TO_IPV4_MRC(ptr)->range[0].min, \
- .max_proto = TO_IPV4_MRC(ptr)->range[0].max, \
-};
-
-enum {
- O_TO_DEST = 0,
- O_RANDOM,
- O_PERSISTENT,
- F_TO_DEST = 1 << O_TO_DEST,
- F_RANDOM = 1 << O_RANDOM,
-};
-
-static void DNAT_help(void)
-{
- printf(
-"DNAT target options:\n"
-" --to-destination [<ipaddr>[-<ipaddr>]][:port[-port]]\n"
-" Address to map destination to.\n"
-"[--random] [--persistent]\n");
-}
-
-static void DNAT_help_v2(void)
-{
- printf(
-"DNAT target options:\n"
-" --to-destination [<ipaddr>[-<ipaddr>]][:port[-port[/port]]]\n"
-" Address to map destination to.\n"
-"[--random] [--persistent]\n");
-}
-
-static const struct xt_option_entry DNAT_opts[] = {
- {.name = "to-destination", .id = O_TO_DEST, .type = XTTYPE_STRING,
- .flags = XTOPT_MAND},
- {.name = "random", .id = O_RANDOM, .type = XTTYPE_NONE},
- {.name = "persistent", .id = O_PERSISTENT, .type = XTTYPE_NONE},
- XTOPT_TABLEEND,
-};
-
-/* Parses ports */
-static void
-parse_ports(const char *arg, bool portok, struct nf_nat_range2 *range)
-{
- unsigned int port, maxport, baseport;
- char *end = NULL;
-
- if (!portok)
- xtables_error(PARAMETER_PROBLEM,
- "Need TCP, UDP, SCTP or DCCP with port specification");
-
- range->flags |= NF_NAT_RANGE_PROTO_SPECIFIED;
-
- if (!xtables_strtoui(arg, &end, &port, 1, UINT16_MAX))
- xtables_error(PARAMETER_PROBLEM,
- "Port `%s' not valid", arg);
-
- switch (*end) {
- case '\0':
- range->min_proto.tcp.port
- = range->max_proto.tcp.port
- = htons(port);
- return;
- case '-':
- arg = end + 1;
- break;
- case ':':
- xtables_error(PARAMETER_PROBLEM,
- "Invalid port:port syntax - use dash");
- default:
- xtables_error(PARAMETER_PROBLEM,
- "Garbage after port value: `%s'", end);
- }
-
- if (!xtables_strtoui(arg, &end, &maxport, 1, UINT16_MAX))
- xtables_error(PARAMETER_PROBLEM,
- "Port `%s' not valid", arg);
-
- if (maxport < port)
- /* People are stupid. */
- xtables_error(PARAMETER_PROBLEM,
- "Port range `%s' funky", arg);
-
- range->min_proto.tcp.port = htons(port);
- range->max_proto.tcp.port = htons(maxport);
-
- switch (*end) {
- case '\0':
- return;
- case '/':
- arg = end + 1;
- break;
- default:
- xtables_error(PARAMETER_PROBLEM,
- "Garbage after port range: `%s'", end);
- }
-
- if (!xtables_strtoui(arg, &end, &baseport, 1, UINT16_MAX))
- xtables_error(PARAMETER_PROBLEM,
- "Port `%s' not valid", arg);
-
- range->flags |= NF_NAT_RANGE_PROTO_OFFSET;
- range->base_proto.tcp.port = htons(baseport);
-}
-
-/* Ranges expected in network order. */
-static void
-parse_to(const char *orig_arg, bool portok, struct nf_nat_range2 *range)
-{
- char *arg, *colon, *dash;
-
- arg = xtables_strdup(orig_arg);
- colon = strchr(arg, ':');
-
- if (colon) {
- parse_ports(colon + 1, portok, range);
-
- /* Starts with a colon? No IP info...*/
- if (colon == arg) {
- free(arg);
- return;
- }
- *colon = '\0';
- }
-
- range->flags |= NF_NAT_RANGE_MAP_IPS;
- dash = strchr(arg, '-');
- if (colon && dash && dash > colon)
- dash = NULL;
-
- if (dash)
- *dash = '\0';
-
- if (!inet_pton(AF_INET, arg, &range->min_addr))
- xtables_error(PARAMETER_PROBLEM,
- "Bad IP address \"%s\"\n", arg);
- if (dash) {
- if (!inet_pton(AF_INET, dash + 1, &range->max_addr))
- xtables_error(PARAMETER_PROBLEM,
- "Bad IP address \"%s\"\n", dash + 1);
- } else {
- range->max_addr = range->min_addr;
- }
- free(arg);
- return;
-}
-
-static void __DNAT_parse(struct xt_option_call *cb, __u16 proto,
- struct nf_nat_range2 *range)
-{
- bool portok = proto == IPPROTO_TCP ||
- proto == IPPROTO_UDP ||
- proto == IPPROTO_SCTP ||
- proto == IPPROTO_DCCP ||
- proto == IPPROTO_ICMP;
-
- xtables_option_parse(cb);
- switch (cb->entry->id) {
- case O_TO_DEST:
- parse_to(cb->arg, portok, range);
- break;
- case O_PERSISTENT:
- range->flags |= NF_NAT_RANGE_PERSISTENT;
- break;
- }
-}
-
-static void DNAT_parse(struct xt_option_call *cb)
-{
- struct nf_nat_ipv4_multi_range_compat *mr = (void *)cb->data;
- const struct ipt_entry *entry = cb->xt_entry;
- struct nf_nat_range2 range = {};
-
- __DNAT_parse(cb, entry->ip.proto, &range);
-
- switch (cb->entry->id) {
- case O_TO_DEST:
- mr->range->min_ip = range.min_addr.ip;
- mr->range->max_ip = range.max_addr.ip;
- mr->range->min = range.min_proto;
- mr->range->max = range.max_proto;
- /* fall through */
- case O_PERSISTENT:
- mr->range->flags |= range.flags;
- break;
- }
-}
-
-static void DNAT_fcheck(struct xt_fcheck_call *cb)
-{
- static const unsigned int f = F_TO_DEST | F_RANDOM;
- struct nf_nat_ipv4_multi_range_compat *mr = cb->data;
-
- if ((cb->xflags & f) == f)
- mr->range[0].flags |= NF_NAT_RANGE_PROTO_RANDOM;
-
- mr->rangesize = 1;
-
- if (mr->range[0].flags & NF_NAT_RANGE_PROTO_OFFSET)
- xtables_error(PARAMETER_PROBLEM,
- "Shifted portmap ranges not supported with this kernel");
-}
-
-static char *sprint_range(const struct nf_nat_range2 *r)
-{
- static char buf[INET_ADDRSTRLEN * 2 + 1 + 6 * 3];
-
- if (r->flags & NF_NAT_RANGE_MAP_IPS) {
- sprintf(buf, "%s", xtables_ipaddr_to_numeric(&r->min_addr.in));
- if (memcmp(&r->min_addr, &r->max_addr, sizeof(r->min_addr)))
- sprintf(buf + strlen(buf), "-%s",
- xtables_ipaddr_to_numeric(&r->max_addr.in));
- } else {
- buf[0] = '\0';
- }
- if (r->flags & NF_NAT_RANGE_PROTO_SPECIFIED) {
- sprintf(buf + strlen(buf), ":%hu",
- ntohs(r->min_proto.tcp.port));
- if (r->max_proto.tcp.port != r->min_proto.tcp.port)
- sprintf(buf + strlen(buf), "-%hu",
- ntohs(r->max_proto.tcp.port));
- if (r->flags & NF_NAT_RANGE_PROTO_OFFSET)
- sprintf(buf + strlen(buf), "/%hu",
- ntohs(r->base_proto.tcp.port));
- }
- return buf;
-}
-
-static void __DNAT_print(const struct nf_nat_range2 *r, bool save)
-{
- const char *dashdash = save ? "--" : "";
-
- printf(" %s%s", save ? "--to-destination " : "to:", sprint_range(r));
- if (r->flags & NF_NAT_RANGE_PROTO_RANDOM)
- printf(" %srandom", dashdash);
- if (r->flags & NF_NAT_RANGE_PERSISTENT)
- printf(" %spersistent", dashdash);
-}
-
-static void DNAT_print(const void *ip, const struct xt_entry_target *target,
- int numeric)
-{
- struct nf_nat_range2 range = RANGE2_INIT_FROM_IPV4_MRC(target->data);
-
- __DNAT_print(&range, false);
-}
-
-static void DNAT_save(const void *ip, const struct xt_entry_target *target)
-{
- struct nf_nat_range2 range = RANGE2_INIT_FROM_IPV4_MRC(target->data);
-
- __DNAT_print(&range, true);
-}
-
-static int __DNAT_xlate(struct xt_xlate *xl, const struct nf_nat_range2 *r)
-{
- char *range_str = sprint_range(r);
- const char *sep = " ";
-
- /* shifted portmap ranges are not supported by nftables */
- if (r->flags & NF_NAT_RANGE_PROTO_OFFSET)
- return 0;
-
- xt_xlate_add(xl, "dnat");
- if (strlen(range_str))
- xt_xlate_add(xl, " to %s", range_str);
- if (r->flags & NF_NAT_RANGE_PROTO_RANDOM) {
- xt_xlate_add(xl, "%srandom", sep);
- sep = ",";
- }
- if (r->flags & NF_NAT_RANGE_PERSISTENT) {
- xt_xlate_add(xl, "%spersistent", sep);
- sep = ",";
- }
- return 1;
-}
-
-static int DNAT_xlate(struct xt_xlate *xl,
- const struct xt_xlate_tg_params *params)
-{
- struct nf_nat_range2 range =
- RANGE2_INIT_FROM_IPV4_MRC(params->target->data);
-
- return __DNAT_xlate(xl, &range);
-}
-
-static void DNAT_parse_v2(struct xt_option_call *cb)
-{
- const struct ipt_entry *entry = cb->xt_entry;
-
- __DNAT_parse(cb, entry->ip.proto, cb->data);
-}
-
-static void DNAT_fcheck_v2(struct xt_fcheck_call *cb)
-{
- static const unsigned int f = F_TO_DEST | F_RANDOM;
- struct nf_nat_range2 *range = cb->data;
-
- if ((cb->xflags & f) == f)
- range->flags |= NF_NAT_RANGE_PROTO_RANDOM;
-}
-
-static void DNAT_print_v2(const void *ip, const struct xt_entry_target *target,
- int numeric)
-{
- __DNAT_print((const void *)target->data, false);
-}
-
-static void DNAT_save_v2(const void *ip, const struct xt_entry_target *target)
-{
- __DNAT_print((const void *)target->data, true);
-}
-
-static int DNAT_xlate_v2(struct xt_xlate *xl,
- const struct xt_xlate_tg_params *params)
-{
- return __DNAT_xlate(xl, (const void *)params->target->data);
-}
-
-static struct xtables_target dnat_tg_reg[] = {
- {
- .name = "DNAT",
- .version = XTABLES_VERSION,
- .family = NFPROTO_IPV4,
- .revision = 0,
- .size = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
- .userspacesize = XT_ALIGN(sizeof(struct nf_nat_ipv4_multi_range_compat)),
- .help = DNAT_help,
- .print = DNAT_print,
- .save = DNAT_save,
- .x6_parse = DNAT_parse,
- .x6_fcheck = DNAT_fcheck,
- .x6_options = DNAT_opts,
- .xlate = DNAT_xlate,
- },
- {
- .name = "DNAT",
- .version = XTABLES_VERSION,
- .family = NFPROTO_IPV4,
- .revision = 2,
- .size = XT_ALIGN(sizeof(struct nf_nat_range2)),
- .userspacesize = XT_ALIGN(sizeof(struct nf_nat_range2)),
- .help = DNAT_help_v2,
- .print = DNAT_print_v2,
- .save = DNAT_save_v2,
- .x6_parse = DNAT_parse_v2,
- .x6_fcheck = DNAT_fcheck_v2,
- .x6_options = DNAT_opts,
- .xlate = DNAT_xlate_v2,
- },
-};
-
-void _init(void)
-{
- xtables_register_targets(dnat_tg_reg, ARRAY_SIZE(dnat_tg_reg));
-}