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