Rusty Russell | 5245182 | 2000-08-27 07:47:46 +0000 | [diff] [blame] | 1 | /* Shared library add-on to iptables to add IP pool mangling target. */ |
| 2 | #include <stdio.h> |
| 3 | #include <netdb.h> |
| 4 | #include <string.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <getopt.h> |
| 7 | #include <ctype.h> |
| 8 | |
| 9 | #include <iptables.h> |
| 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> |
| 14 | |
| 15 | #include <libippool/ip_pool_support.h> |
| 16 | |
| 17 | /* FIXME --RR */ |
| 18 | #include "../ippool/libippool.c" |
| 19 | |
| 20 | /* Function which prints out usage message. */ |
| 21 | static void |
| 22 | help(void) |
| 23 | { |
| 24 | printf( |
| 25 | "POOL v%s options:\n" |
| 26 | " --add-srcip <pool>\n" |
| 27 | " --del-srcip <pool>\n" |
| 28 | " --add-dstip <pool>\n" |
| 29 | " --del-dstip <pool>\n" |
| 30 | " add/del src/dst IP from pool.\n\n", |
| 31 | NETFILTER_VERSION); |
| 32 | } |
| 33 | |
| 34 | static struct option opts[] = { |
| 35 | { "add-srcip", 1, 0, '1' }, |
| 36 | { "del-srcip", 1, 0, '2' }, |
| 37 | { "add-dstip", 1, 0, '3' }, |
| 38 | { "del-dstip", 1, 0, '4' }, |
| 39 | { 0 } |
| 40 | }; |
| 41 | |
| 42 | /* Initialize the target. */ |
| 43 | static void |
| 44 | init(struct ipt_entry_target *target, unsigned int *nfcache) |
| 45 | { |
| 46 | struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data; |
| 47 | |
| 48 | ipi->src = ipi->dst = IP_POOL_NONE; |
| 49 | ipi->flags = 0; |
| 50 | |
| 51 | /* Can't cache this */ |
| 52 | *nfcache |= NFC_UNKNOWN; |
| 53 | } |
| 54 | |
| 55 | /* Function which parses command options; returns true if it |
| 56 | ate an option */ |
| 57 | static int |
| 58 | parse(int c, char **argv, int invert, unsigned int *flags, |
| 59 | const struct ipt_entry *entry, |
| 60 | struct ipt_entry_target **target) |
| 61 | { |
| 62 | struct ipt_pool_info *ipi = (struct ipt_pool_info *) (*target)->data; |
| 63 | switch (c) { |
| 64 | case '1': /* --add-srcip <pool> */ |
| 65 | ipi->src = ip_pool_get_index(optarg); |
| 66 | ipi->flags &= ~IPT_POOL_DEL_SRC; |
| 67 | break; |
| 68 | case '2': /* --del-srcip <pool> */ |
| 69 | ipi->src = ip_pool_get_index(optarg); |
| 70 | ipi->flags |= IPT_POOL_DEL_SRC; |
| 71 | break; |
| 72 | case '3': /* --add-dstip <pool> */ |
| 73 | ipi->dst = ip_pool_get_index(optarg); |
| 74 | ipi->flags &= ~IPT_POOL_DEL_DST; |
| 75 | break; |
| 76 | case '4': /* --del-dstip <pool> */ |
| 77 | ipi->dst = ip_pool_get_index(optarg); |
| 78 | ipi->flags |= IPT_POOL_DEL_DST; |
| 79 | break; |
| 80 | default: |
| 81 | return 0; |
| 82 | } |
| 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | /* Final check; don't care. */ |
| 87 | static void final_check(unsigned int flags) |
| 88 | { |
| 89 | } |
| 90 | |
| 91 | /* Prints out the targinfo. */ |
| 92 | static void |
| 93 | print(const struct ipt_ip *ip, |
| 94 | const struct ipt_entry_target *target, |
| 95 | int numeric) |
| 96 | { |
| 97 | char buf[256]; |
| 98 | struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data; |
| 99 | |
| 100 | printf("POOL"); |
| 101 | if (ipi->src != IP_POOL_NONE) { |
| 102 | printf(" --%s-srcip %s", |
| 103 | (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add", |
| 104 | ip_pool_get_name(buf, sizeof(buf), ipi->src, numeric)); |
| 105 | } |
| 106 | if (ipi->dst != IP_POOL_NONE) { |
| 107 | printf(" --%s-dstip %s", |
| 108 | (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add", |
| 109 | ip_pool_get_name(buf, sizeof(buf), ipi->dst, numeric)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /* Saves the union ipt_targinfo in parsable form to stdout. */ |
| 114 | static void |
| 115 | save(const struct ipt_ip *ip, const struct ipt_entry_target *target) |
| 116 | { |
| 117 | char buf[256]; |
| 118 | struct ipt_pool_info *ipi = (struct ipt_pool_info *) target->data; |
| 119 | |
| 120 | printf("-j POOL"); |
| 121 | if (ipi->src != IP_POOL_NONE) { |
| 122 | printf(" --%s-srcip %s", |
| 123 | (ipi->flags & IPT_POOL_DEL_SRC) ? "del" : "add", |
| 124 | ip_pool_get_name(buf, sizeof(buf), ipi->src, 0)); |
| 125 | } |
| 126 | if (ipi->dst != IP_POOL_NONE) { |
| 127 | printf(" --%s-dstip %s", |
| 128 | (ipi->flags & IPT_POOL_DEL_DST) ? "del" : "add", |
| 129 | ip_pool_get_name(buf, sizeof(buf), ipi->dst, 0)); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | struct iptables_target ipt_pool_target |
| 134 | = { NULL, |
| 135 | "POOL", |
| 136 | NETFILTER_VERSION, |
| 137 | IPT_ALIGN(sizeof(struct ipt_pool_info)), |
| 138 | IPT_ALIGN(sizeof(struct ipt_pool_info)), |
| 139 | &help, |
| 140 | &init, |
| 141 | &parse, |
| 142 | &final_check, |
| 143 | &print, |
| 144 | &save, |
| 145 | opts |
| 146 | }; |
| 147 | |
| 148 | void _init(void) |
| 149 | { |
| 150 | register_target(&ipt_pool_target); |
| 151 | } |