lin
2025-08-21 57113df3a0e2be01232281fad9a5f2c060567981
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (c) 2012-2013 Patrick McHardy <kaber@trash.net>
 */
 
#include <stdio.h>
#include <string.h>
#include <xtables.h>
#include <linux/netfilter_ipv6/ip6_tables.h>
#include <linux/netfilter_ipv6/ip6t_NPT.h>
 
enum {
   O_SRC_PFX    = 1 << 0,
   O_DST_PFX    = 1 << 1,
};
 
static const struct xt_option_entry SNPT_options[] = {
   { .name = "src-pfx", .id = O_SRC_PFX, .type = XTTYPE_HOSTMASK,
     .flags = XTOPT_MAND },
   { .name = "dst-pfx", .id = O_DST_PFX, .type = XTTYPE_HOSTMASK,
     .flags = XTOPT_MAND },
   { }
};
 
static void SNPT_help(void)
{
   printf("SNPT target options:"
          "\n"
          " --src-pfx prefix/length\n"
          " --dst-pfx prefix/length\n"
          "\n");
}
 
static void SNPT_parse(struct xt_option_call *cb)
{
   struct ip6t_npt_tginfo *npt = cb->data;
 
   xtables_option_parse(cb);
   switch (cb->entry->id) {
   case O_SRC_PFX:
       npt->src_pfx = cb->val.haddr;
       npt->src_pfx_len = cb->val.hlen;
       break;
   case O_DST_PFX:
       npt->dst_pfx = cb->val.haddr;
       npt->dst_pfx_len = cb->val.hlen;
       break;
   }
}
 
static void SNPT_print(const void *ip, const struct xt_entry_target *target,
              int numeric)
{
   const struct ip6t_npt_tginfo *npt = (const void *)target->data;
 
   printf(" SNPT src-pfx %s/%u", xtables_ip6addr_to_numeric(&npt->src_pfx.in6),
                npt->src_pfx_len);
   printf(" dst-pfx %s/%u", xtables_ip6addr_to_numeric(&npt->dst_pfx.in6),
                npt->dst_pfx_len);
}
 
static void SNPT_save(const void *ip, const struct xt_entry_target *target)
{
   static const struct in6_addr zero_addr;
   const struct ip6t_npt_tginfo *info = (const void *)target->data;
 
   if (memcmp(&info->src_pfx.in6, &zero_addr, sizeof(zero_addr)) != 0 ||
       info->src_pfx_len != 0)
       printf(" --src-pfx %s/%u",
              xtables_ip6addr_to_numeric(&info->src_pfx.in6),
              info->src_pfx_len);
   if (memcmp(&info->dst_pfx.in6, &zero_addr, sizeof(zero_addr)) != 0 ||
       info->dst_pfx_len != 0)
       printf(" --dst-pfx %s/%u",
              xtables_ip6addr_to_numeric(&info->dst_pfx.in6),
              info->dst_pfx_len);
}
 
static struct xtables_target snpt_tg_reg = {
   .name        = "SNPT",
   .version    = XTABLES_VERSION,
   .family        = NFPROTO_IPV6,
   .size        = XT_ALIGN(sizeof(struct ip6t_npt_tginfo)),
   .userspacesize    = offsetof(struct ip6t_npt_tginfo, adjustment),
   .help        = SNPT_help,
   .x6_parse    = SNPT_parse,
   .print        = SNPT_print,
   .save        = SNPT_save,
   .x6_options    = SNPT_options,
};
 
void _init(void)
{
   xtables_register_target(&snpt_tg_reg);
}