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