1 /* Shared library add-on to iptables to add IP pool mangling target. */
10 #include <linux/netfilter_ipv4/ip_tables.h>
11 #include <linux/netfilter_ipv4/ip_nat_rule.h>
12 #include <linux/netfilter_ipv4/ip_pool.h>
13 #include <linux/netfilter_ipv4/ipt_pool.h>
15 #include <libippool/ip_pool_support.h>
18 #define ip_pool_init ip_POOL_init
19 #define ip_pool_get_index ip_POOL_get_index
20 #define ip_pool_get_name ip_POOL_get_name
21 #include "../ippool/libippool.c"
23 /* Function which prints out usage message. */
29 " --add-srcip <pool>\n"
30 " --del-srcip <pool>\n"
31 " --add-dstip <pool>\n"
32 " --del-dstip <pool>\n"
33 " add/del src/dst IP from pool.\n\n",
37 static struct option opts[] = {
38 { "add-srcip", 1, 0, '1' },
39 { "del-srcip", 1, 0, '2' },
40 { "add-dstip", 1, 0, '3' },
41 { "del-dstip", 1, 0, '4' },
45 /* Initialize the target. */
47 init(struct ipt_entry_target *target, unsigned int *nfcache)
49 struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
51 ipi->src = ipi->dst = IP_POOL_NONE;
56 /* Function which parses command options; returns true if it
59 parse(int c, char **argv, int invert, unsigned int *flags,
60 const struct ipt_entry *entry,
61 struct ipt_entry_target **target)
63 struct ipt_pool_info *ipi = (struct ipt_pool_info *) (*target)->data;
65 case '1': /* --add-srcip <pool> */
66 ipi->src = ip_pool_get_index(optarg);
67 ipi->flags &= ~IPT_POOL_DEL_SRC;
69 case '2': /* --del-srcip <pool> */
70 ipi->src = ip_pool_get_index(optarg);
71 ipi->flags |= IPT_POOL_DEL_SRC;
73 case '3': /* --add-dstip <pool> */
74 ipi->dst = ip_pool_get_index(optarg);
75 ipi->flags &= ~IPT_POOL_DEL_DST;
77 case '4': /* --del-dstip <pool> */
78 ipi->dst = ip_pool_get_index(optarg);
79 ipi->flags |= IPT_POOL_DEL_DST;
87 /* Final check; don't care. */
88 static void final_check(unsigned int flags)
92 /* Prints out the targinfo. */
94 print(const struct ipt_ip *ip,
95 const struct ipt_entry_target *target,
99 struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
102 if (ipi->src != IP_POOL_NONE) {
103 printf(" --%s-srcip %s",
104 (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add",
105 ip_pool_get_name(buf, sizeof(buf), ipi->src, numeric));
107 if (ipi->dst != IP_POOL_NONE) {
108 printf(" --%s-dstip %s",
109 (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add",
110 ip_pool_get_name(buf, sizeof(buf), ipi->dst, numeric));
114 /* Saves the union ipt_targinfo in parsable form to stdout. */
116 save(const struct ipt_ip *ip, const struct ipt_entry_target *target)
119 struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data;
122 if (ipi->src != IP_POOL_NONE) {
123 printf(" --%s-srcip %s",
124 (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add",
125 ip_pool_get_name(buf, sizeof(buf), ipi->src, 0));
127 if (ipi->dst != IP_POOL_NONE) {
128 printf(" --%s-dstip %s",
129 (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add",
130 ip_pool_get_name(buf, sizeof(buf), ipi->dst, 0));
134 static struct iptables_target ipt_pool_target = {
137 .version = IPTABLES_VERSION,
138 .size = IPT_ALIGN(sizeof(struct ipt_pool_info)),
139 .userspacesize = IPT_ALIGN(sizeof(struct ipt_pool_info)),
143 .final_check = &final_check,
151 register_target(&ipt_pool_target);